* window.h (struct window): Convert left_margin_cols and
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
315
316 #ifndef FRAME_X_OUTPUT
317 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
318 #endif
319
320 #define INFINITY 10000000
321
322 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
323 Lisp_Object Qwindow_scroll_functions;
324 static Lisp_Object Qwindow_text_change_functions;
325 static Lisp_Object Qredisplay_end_trigger_functions;
326 Lisp_Object Qinhibit_point_motion_hooks;
327 static Lisp_Object QCeval, QCpropertize;
328 Lisp_Object QCfile, QCdata;
329 static Lisp_Object Qfontified;
330 static Lisp_Object Qgrow_only;
331 static Lisp_Object Qinhibit_eval_during_redisplay;
332 static Lisp_Object Qbuffer_position, Qposition, Qobject;
333 static Lisp_Object Qright_to_left, Qleft_to_right;
334
335 /* Cursor shapes. */
336 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
337
338 /* Pointer shapes. */
339 static Lisp_Object Qarrow, Qhand;
340 Lisp_Object Qtext;
341
342 /* Holds the list (error). */
343 static Lisp_Object list_of_error;
344
345 static Lisp_Object Qfontification_functions;
346
347 static Lisp_Object Qwrap_prefix;
348 static Lisp_Object Qline_prefix;
349 static Lisp_Object Qredisplay_internal;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x)
380
381 #else /* !HAVE_WINDOW_SYSTEM */
382 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
383 #endif /* HAVE_WINDOW_SYSTEM */
384
385 /* Test if the display element loaded in IT, or the underlying buffer
386 or string character, is a space or a TAB character. This is used
387 to determine where word wrapping can occur. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
391 || ((STRINGP (it->string) \
392 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
393 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
394 || (it->s \
395 && (it->s[IT_BYTEPOS (*it)] == ' ' \
396 || it->s[IT_BYTEPOS (*it)] == '\t')) \
397 || (IT_BYTEPOS (*it) < ZV_BYTE \
398 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
399 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
400
401 /* Name of the face used to highlight trailing whitespace. */
402
403 static Lisp_Object Qtrailing_whitespace;
404
405 /* Name and number of the face used to highlight escape glyphs. */
406
407 static Lisp_Object Qescape_glyph;
408
409 /* Name and number of the face used to highlight non-breaking spaces. */
410
411 static Lisp_Object Qnobreak_space;
412
413 /* The symbol `image' which is the car of the lists used to represent
414 images in Lisp. Also a tool bar style. */
415
416 Lisp_Object Qimage;
417
418 /* The image map types. */
419 Lisp_Object QCmap;
420 static Lisp_Object QCpointer;
421 static Lisp_Object Qrect, Qcircle, Qpoly;
422
423 /* Tool bar styles */
424 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
425
426 /* Non-zero means print newline to stdout before next mini-buffer
427 message. */
428
429 int noninteractive_need_newline;
430
431 /* Non-zero means print newline to message log before next message. */
432
433 static int message_log_need_newline;
434
435 /* Three markers that message_dolog uses.
436 It could allocate them itself, but that causes trouble
437 in handling memory-full errors. */
438 static Lisp_Object message_dolog_marker1;
439 static Lisp_Object message_dolog_marker2;
440 static Lisp_Object message_dolog_marker3;
441 \f
442 /* The buffer position of the first character appearing entirely or
443 partially on the line of the selected window which contains the
444 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
445 redisplay optimization in redisplay_internal. */
446
447 static struct text_pos this_line_start_pos;
448
449 /* Number of characters past the end of the line above, including the
450 terminating newline. */
451
452 static struct text_pos this_line_end_pos;
453
454 /* The vertical positions and the height of this line. */
455
456 static int this_line_vpos;
457 static int this_line_y;
458 static int this_line_pixel_height;
459
460 /* X position at which this display line starts. Usually zero;
461 negative if first character is partially visible. */
462
463 static int this_line_start_x;
464
465 /* The smallest character position seen by move_it_* functions as they
466 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
467 hscrolled lines, see display_line. */
468
469 static struct text_pos this_line_min_pos;
470
471 /* Buffer that this_line_.* variables are referring to. */
472
473 static struct buffer *this_line_buffer;
474
475
476 /* Values of those variables at last redisplay are stored as
477 properties on `overlay-arrow-position' symbol. However, if
478 Voverlay_arrow_position is a marker, last-arrow-position is its
479 numerical position. */
480
481 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
482
483 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
484 properties on a symbol in overlay-arrow-variable-list. */
485
486 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
487
488 Lisp_Object Qmenu_bar_update_hook;
489
490 /* Nonzero if an overlay arrow has been displayed in this window. */
491
492 static int overlay_arrow_seen;
493
494 /* Vector containing glyphs for an ellipsis `...'. */
495
496 static Lisp_Object default_invis_vector[3];
497
498 /* This is the window where the echo area message was displayed. It
499 is always a mini-buffer window, but it may not be the same window
500 currently active as a mini-buffer. */
501
502 Lisp_Object echo_area_window;
503
504 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
505 pushes the current message and the value of
506 message_enable_multibyte on the stack, the function restore_message
507 pops the stack and displays MESSAGE again. */
508
509 static Lisp_Object Vmessage_stack;
510
511 /* Nonzero means multibyte characters were enabled when the echo area
512 message was specified. */
513
514 static int message_enable_multibyte;
515
516 /* Nonzero if we should redraw the mode lines on the next redisplay. */
517
518 int update_mode_lines;
519
520 /* Nonzero if window sizes or contents have changed since last
521 redisplay that finished. */
522
523 int windows_or_buffers_changed;
524
525 /* Nonzero means a frame's cursor type has been changed. */
526
527 int cursor_type_changed;
528
529 /* Nonzero after display_mode_line if %l was used and it displayed a
530 line number. */
531
532 static int line_number_displayed;
533
534 /* The name of the *Messages* buffer, a string. */
535
536 static Lisp_Object Vmessages_buffer_name;
537
538 /* Current, index 0, and last displayed echo area message. Either
539 buffers from echo_buffers, or nil to indicate no message. */
540
541 Lisp_Object echo_area_buffer[2];
542
543 /* The buffers referenced from echo_area_buffer. */
544
545 static Lisp_Object echo_buffer[2];
546
547 /* A vector saved used in with_area_buffer to reduce consing. */
548
549 static Lisp_Object Vwith_echo_area_save_vector;
550
551 /* Non-zero means display_echo_area should display the last echo area
552 message again. Set by redisplay_preserve_echo_area. */
553
554 static int display_last_displayed_message_p;
555
556 /* Nonzero if echo area is being used by print; zero if being used by
557 message. */
558
559 static int message_buf_print;
560
561 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
562
563 static Lisp_Object Qinhibit_menubar_update;
564 static Lisp_Object Qmessage_truncate_lines;
565
566 /* Set to 1 in clear_message to make redisplay_internal aware
567 of an emptied echo area. */
568
569 static int message_cleared_p;
570
571 /* A scratch glyph row with contents used for generating truncation
572 glyphs. Also used in direct_output_for_insert. */
573
574 #define MAX_SCRATCH_GLYPHS 100
575 static struct glyph_row scratch_glyph_row;
576 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
577
578 /* Ascent and height of the last line processed by move_it_to. */
579
580 static int last_height;
581
582 /* Non-zero if there's a help-echo in the echo area. */
583
584 int help_echo_showing_p;
585
586 /* If >= 0, computed, exact values of mode-line and header-line height
587 to use in the macros CURRENT_MODE_LINE_HEIGHT and
588 CURRENT_HEADER_LINE_HEIGHT. */
589
590 int current_mode_line_height, current_header_line_height;
591
592 /* The maximum distance to look ahead for text properties. Values
593 that are too small let us call compute_char_face and similar
594 functions too often which is expensive. Values that are too large
595 let us call compute_char_face and alike too often because we
596 might not be interested in text properties that far away. */
597
598 #define TEXT_PROP_DISTANCE_LIMIT 100
599
600 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
601 iterator state and later restore it. This is needed because the
602 bidi iterator on bidi.c keeps a stacked cache of its states, which
603 is really a singleton. When we use scratch iterator objects to
604 move around the buffer, we can cause the bidi cache to be pushed or
605 popped, and therefore we need to restore the cache state when we
606 return to the original iterator. */
607 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
608 do { \
609 if (CACHE) \
610 bidi_unshelve_cache (CACHE, 1); \
611 ITCOPY = ITORIG; \
612 CACHE = bidi_shelve_cache (); \
613 } while (0)
614
615 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
616 do { \
617 if (pITORIG != pITCOPY) \
618 *(pITORIG) = *(pITCOPY); \
619 bidi_unshelve_cache (CACHE, 0); \
620 CACHE = NULL; \
621 } while (0)
622
623 #ifdef GLYPH_DEBUG
624
625 /* Non-zero means print traces of redisplay if compiled with
626 GLYPH_DEBUG defined. */
627
628 int trace_redisplay_p;
629
630 #endif /* GLYPH_DEBUG */
631
632 #ifdef DEBUG_TRACE_MOVE
633 /* Non-zero means trace with TRACE_MOVE to stderr. */
634 int trace_move;
635
636 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
637 #else
638 #define TRACE_MOVE(x) (void) 0
639 #endif
640
641 static Lisp_Object Qauto_hscroll_mode;
642
643 /* Buffer being redisplayed -- for redisplay_window_error. */
644
645 static struct buffer *displayed_buffer;
646
647 /* Value returned from text property handlers (see below). */
648
649 enum prop_handled
650 {
651 HANDLED_NORMALLY,
652 HANDLED_RECOMPUTE_PROPS,
653 HANDLED_OVERLAY_STRING_CONSUMED,
654 HANDLED_RETURN
655 };
656
657 /* A description of text properties that redisplay is interested
658 in. */
659
660 struct props
661 {
662 /* The name of the property. */
663 Lisp_Object *name;
664
665 /* A unique index for the property. */
666 enum prop_idx idx;
667
668 /* A handler function called to set up iterator IT from the property
669 at IT's current position. Value is used to steer handle_stop. */
670 enum prop_handled (*handler) (struct it *it);
671 };
672
673 static enum prop_handled handle_face_prop (struct it *);
674 static enum prop_handled handle_invisible_prop (struct it *);
675 static enum prop_handled handle_display_prop (struct it *);
676 static enum prop_handled handle_composition_prop (struct it *);
677 static enum prop_handled handle_overlay_change (struct it *);
678 static enum prop_handled handle_fontified_prop (struct it *);
679
680 /* Properties handled by iterators. */
681
682 static struct props it_props[] =
683 {
684 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
685 /* Handle `face' before `display' because some sub-properties of
686 `display' need to know the face. */
687 {&Qface, FACE_PROP_IDX, handle_face_prop},
688 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
689 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
690 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
691 {NULL, 0, NULL}
692 };
693
694 /* Value is the position described by X. If X is a marker, value is
695 the marker_position of X. Otherwise, value is X. */
696
697 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
698
699 /* Enumeration returned by some move_it_.* functions internally. */
700
701 enum move_it_result
702 {
703 /* Not used. Undefined value. */
704 MOVE_UNDEFINED,
705
706 /* Move ended at the requested buffer position or ZV. */
707 MOVE_POS_MATCH_OR_ZV,
708
709 /* Move ended at the requested X pixel position. */
710 MOVE_X_REACHED,
711
712 /* Move within a line ended at the end of a line that must be
713 continued. */
714 MOVE_LINE_CONTINUED,
715
716 /* Move within a line ended at the end of a line that would
717 be displayed truncated. */
718 MOVE_LINE_TRUNCATED,
719
720 /* Move within a line ended at a line end. */
721 MOVE_NEWLINE_OR_CR
722 };
723
724 /* This counter is used to clear the face cache every once in a while
725 in redisplay_internal. It is incremented for each redisplay.
726 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
727 cleared. */
728
729 #define CLEAR_FACE_CACHE_COUNT 500
730 static int clear_face_cache_count;
731
732 /* Similarly for the image cache. */
733
734 #ifdef HAVE_WINDOW_SYSTEM
735 #define CLEAR_IMAGE_CACHE_COUNT 101
736 static int clear_image_cache_count;
737
738 /* Null glyph slice */
739 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
740 #endif
741
742 /* True while redisplay_internal is in progress. */
743
744 bool redisplaying_p;
745
746 static Lisp_Object Qinhibit_free_realized_faces;
747 static Lisp_Object Qmode_line_default_help_echo;
748
749 /* If a string, XTread_socket generates an event to display that string.
750 (The display is done in read_char.) */
751
752 Lisp_Object help_echo_string;
753 Lisp_Object help_echo_window;
754 Lisp_Object help_echo_object;
755 ptrdiff_t help_echo_pos;
756
757 /* Temporary variable for XTread_socket. */
758
759 Lisp_Object previous_help_echo_string;
760
761 /* Platform-independent portion of hourglass implementation. */
762
763 /* Non-zero means an hourglass cursor is currently shown. */
764 int hourglass_shown_p;
765
766 /* If non-null, an asynchronous timer that, when it expires, displays
767 an hourglass cursor on all frames. */
768 struct atimer *hourglass_atimer;
769
770 /* Name of the face used to display glyphless characters. */
771 Lisp_Object Qglyphless_char;
772
773 /* Symbol for the purpose of Vglyphless_char_display. */
774 static Lisp_Object Qglyphless_char_display;
775
776 /* Method symbols for Vglyphless_char_display. */
777 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
778
779 /* Default pixel width of `thin-space' display method. */
780 #define THIN_SPACE_WIDTH 1
781
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
785
786 \f
787 /* Function prototypes. */
788
789 static void setup_for_ellipsis (struct it *, int);
790 static void set_iterator_to_next (struct it *, int);
791 static void mark_window_display_accurate_1 (struct window *, int);
792 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
793 static int display_prop_string_p (Lisp_Object, Lisp_Object);
794 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
795 static int cursor_row_p (struct glyph_row *);
796 static int redisplay_mode_lines (Lisp_Object, int);
797 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
798
799 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
800
801 static void handle_line_prefix (struct it *);
802
803 static void pint2str (char *, int, ptrdiff_t);
804 static void pint2hrstr (char *, int, ptrdiff_t);
805 static struct text_pos run_window_scroll_functions (Lisp_Object,
806 struct text_pos);
807 static int text_outside_line_unchanged_p (struct window *,
808 ptrdiff_t, ptrdiff_t);
809 static void store_mode_line_noprop_char (char);
810 static int store_mode_line_noprop (const char *, int, int);
811 static void handle_stop (struct it *);
812 static void handle_stop_backwards (struct it *, ptrdiff_t);
813 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
814 static void ensure_echo_area_buffers (void);
815 static void unwind_with_echo_area_buffer (Lisp_Object);
816 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
817 static int with_echo_area_buffer (struct window *, int,
818 int (*) (ptrdiff_t, Lisp_Object),
819 ptrdiff_t, Lisp_Object);
820 static void clear_garbaged_frames (void);
821 static int current_message_1 (ptrdiff_t, Lisp_Object);
822 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
823 static void set_message (Lisp_Object);
824 static int set_message_1 (ptrdiff_t, Lisp_Object);
825 static int display_echo_area (struct window *);
826 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
827 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
828 static void unwind_redisplay (void);
829 static int string_char_and_length (const unsigned char *, int *);
830 static struct text_pos display_prop_end (struct it *, Lisp_Object,
831 struct text_pos);
832 static int compute_window_start_on_continuation_line (struct window *);
833 static void insert_left_trunc_glyphs (struct it *);
834 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
835 Lisp_Object);
836 static void extend_face_to_end_of_line (struct it *);
837 static int append_space_for_newline (struct it *, int);
838 static int cursor_row_fully_visible_p (struct window *, int, int);
839 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
840 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
841 static int trailing_whitespace_p (ptrdiff_t);
842 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
843 static void push_it (struct it *, struct text_pos *);
844 static void iterate_out_of_display_property (struct it *);
845 static void pop_it (struct it *);
846 static void sync_frame_with_window_matrix_rows (struct window *);
847 static void redisplay_internal (void);
848 static int echo_area_display (int);
849 static void redisplay_windows (Lisp_Object);
850 static void redisplay_window (Lisp_Object, int);
851 static Lisp_Object redisplay_window_error (Lisp_Object);
852 static Lisp_Object redisplay_window_0 (Lisp_Object);
853 static Lisp_Object redisplay_window_1 (Lisp_Object);
854 static int set_cursor_from_row (struct window *, struct glyph_row *,
855 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
856 int, int);
857 static int update_menu_bar (struct frame *, int, int);
858 static int try_window_reusing_current_matrix (struct window *);
859 static int try_window_id (struct window *);
860 static int display_line (struct it *);
861 static int display_mode_lines (struct window *);
862 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
863 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
864 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
865 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
866 static void display_menu_bar (struct window *);
867 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
868 ptrdiff_t *);
869 static int display_string (const char *, Lisp_Object, Lisp_Object,
870 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
871 static void compute_line_metrics (struct it *);
872 static void run_redisplay_end_trigger_hook (struct it *);
873 static int get_overlay_strings (struct it *, ptrdiff_t);
874 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
875 static void next_overlay_string (struct it *);
876 static void reseat (struct it *, struct text_pos, int);
877 static void reseat_1 (struct it *, struct text_pos, int);
878 static void back_to_previous_visible_line_start (struct it *);
879 static void reseat_at_next_visible_line_start (struct it *, int);
880 static int next_element_from_ellipsis (struct it *);
881 static int next_element_from_display_vector (struct it *);
882 static int next_element_from_string (struct it *);
883 static int next_element_from_c_string (struct it *);
884 static int next_element_from_buffer (struct it *);
885 static int next_element_from_composition (struct it *);
886 static int next_element_from_image (struct it *);
887 static int next_element_from_stretch (struct it *);
888 static void load_overlay_strings (struct it *, ptrdiff_t);
889 static int init_from_display_pos (struct it *, struct window *,
890 struct display_pos *);
891 static void reseat_to_string (struct it *, const char *,
892 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
893 static int get_next_display_element (struct it *);
894 static enum move_it_result
895 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
896 enum move_operation_enum);
897 static void get_visually_first_element (struct it *);
898 static void init_to_row_start (struct it *, struct window *,
899 struct glyph_row *);
900 static int init_to_row_end (struct it *, struct window *,
901 struct glyph_row *);
902 static void back_to_previous_line_start (struct it *);
903 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
904 static struct text_pos string_pos_nchars_ahead (struct text_pos,
905 Lisp_Object, ptrdiff_t);
906 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
907 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
908 static ptrdiff_t number_of_chars (const char *, bool);
909 static void compute_stop_pos (struct it *);
910 static void compute_string_pos (struct text_pos *, struct text_pos,
911 Lisp_Object);
912 static int face_before_or_after_it_pos (struct it *, int);
913 static ptrdiff_t next_overlay_change (ptrdiff_t);
914 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
915 Lisp_Object, struct text_pos *, ptrdiff_t, int);
916 static int handle_single_display_spec (struct it *, Lisp_Object,
917 Lisp_Object, Lisp_Object,
918 struct text_pos *, ptrdiff_t, int, int);
919 static int underlying_face_id (struct it *);
920 static int in_ellipses_for_invisible_text_p (struct display_pos *,
921 struct window *);
922
923 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
924 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
925
926 #ifdef HAVE_WINDOW_SYSTEM
927
928 static void x_consider_frame_title (Lisp_Object);
929 static int tool_bar_lines_needed (struct frame *, int *);
930 static void update_tool_bar (struct frame *, int);
931 static void build_desired_tool_bar_string (struct frame *f);
932 static int redisplay_tool_bar (struct frame *);
933 static void display_tool_bar_line (struct it *, int);
934 static void notice_overwritten_cursor (struct window *,
935 enum glyph_row_area,
936 int, int, int, int);
937 static void append_stretch_glyph (struct it *, Lisp_Object,
938 int, int, int);
939
940
941 #endif /* HAVE_WINDOW_SYSTEM */
942
943 static void produce_special_glyphs (struct it *, enum display_element_type);
944 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
945 static int coords_in_mouse_face_p (struct window *, int, int);
946
947
948 \f
949 /***********************************************************************
950 Window display dimensions
951 ***********************************************************************/
952
953 /* Return the bottom boundary y-position for text lines in window W.
954 This is the first y position at which a line cannot start.
955 It is relative to the top of the window.
956
957 This is the height of W minus the height of a mode line, if any. */
958
959 int
960 window_text_bottom_y (struct window *w)
961 {
962 int height = WINDOW_TOTAL_HEIGHT (w);
963
964 if (WINDOW_WANTS_MODELINE_P (w))
965 height -= CURRENT_MODE_LINE_HEIGHT (w);
966 return height;
967 }
968
969 /* Return the pixel width of display area AREA of window W. AREA < 0
970 means return the total width of W, not including fringes to
971 the left and right of the window. */
972
973 int
974 window_box_width (struct window *w, int area)
975 {
976 int cols = w->total_cols;
977 int pixels = 0;
978
979 if (!w->pseudo_window_p)
980 {
981 cols -= WINDOW_SCROLL_BAR_COLS (w);
982
983 if (area == TEXT_AREA)
984 {
985 cols -= max (0, w->left_margin_cols);
986 cols -= max (0, w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
988 }
989 else if (area == LEFT_MARGIN_AREA)
990 {
991 cols = max (0, w->left_margin_cols);
992 pixels = 0;
993 }
994 else if (area == RIGHT_MARGIN_AREA)
995 {
996 cols = max (0, w->right_margin_cols);
997 pixels = 0;
998 }
999 }
1000
1001 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1002 }
1003
1004
1005 /* Return the pixel height of the display area of window W, not
1006 including mode lines of W, if any. */
1007
1008 int
1009 window_box_height (struct window *w)
1010 {
1011 struct frame *f = XFRAME (w->frame);
1012 int height = WINDOW_TOTAL_HEIGHT (w);
1013
1014 eassert (height >= 0);
1015
1016 /* Note: the code below that determines the mode-line/header-line
1017 height is essentially the same as that contained in the macro
1018 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1019 the appropriate glyph row has its `mode_line_p' flag set,
1020 and if it doesn't, uses estimate_mode_line_height instead. */
1021
1022 if (WINDOW_WANTS_MODELINE_P (w))
1023 {
1024 struct glyph_row *ml_row
1025 = (w->current_matrix && w->current_matrix->rows
1026 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1027 : 0);
1028 if (ml_row && ml_row->mode_line_p)
1029 height -= ml_row->height;
1030 else
1031 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1032 }
1033
1034 if (WINDOW_WANTS_HEADER_LINE_P (w))
1035 {
1036 struct glyph_row *hl_row
1037 = (w->current_matrix && w->current_matrix->rows
1038 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1039 : 0);
1040 if (hl_row && hl_row->mode_line_p)
1041 height -= hl_row->height;
1042 else
1043 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1044 }
1045
1046 /* With a very small font and a mode-line that's taller than
1047 default, we might end up with a negative height. */
1048 return max (0, height);
1049 }
1050
1051 /* Return the window-relative coordinate of the left edge of display
1052 area AREA of window W. AREA < 0 means return the left edge of the
1053 whole window, to the right of the left fringe of W. */
1054
1055 int
1056 window_box_left_offset (struct window *w, int area)
1057 {
1058 int x;
1059
1060 if (w->pseudo_window_p)
1061 return 0;
1062
1063 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1064
1065 if (area == TEXT_AREA)
1066 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1067 + window_box_width (w, LEFT_MARGIN_AREA));
1068 else if (area == RIGHT_MARGIN_AREA)
1069 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1070 + window_box_width (w, LEFT_MARGIN_AREA)
1071 + window_box_width (w, TEXT_AREA)
1072 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1073 ? 0
1074 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1075 else if (area == LEFT_MARGIN_AREA
1076 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1077 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1078
1079 return x;
1080 }
1081
1082
1083 /* Return the window-relative coordinate of the right edge of display
1084 area AREA of window W. AREA < 0 means return the right edge of the
1085 whole window, to the left of the right fringe of W. */
1086
1087 int
1088 window_box_right_offset (struct window *w, int area)
1089 {
1090 return window_box_left_offset (w, area) + window_box_width (w, area);
1091 }
1092
1093 /* Return the frame-relative coordinate of the left edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the right of the left fringe of W. */
1096
1097 int
1098 window_box_left (struct window *w, int area)
1099 {
1100 struct frame *f = XFRAME (w->frame);
1101 int x;
1102
1103 if (w->pseudo_window_p)
1104 return FRAME_INTERNAL_BORDER_WIDTH (f);
1105
1106 x = (WINDOW_LEFT_EDGE_X (w)
1107 + window_box_left_offset (w, area));
1108
1109 return x;
1110 }
1111
1112
1113 /* Return the frame-relative coordinate of the right edge of display
1114 area AREA of window W. AREA < 0 means return the right edge of the
1115 whole window, to the left of the right fringe of W. */
1116
1117 int
1118 window_box_right (struct window *w, int area)
1119 {
1120 return window_box_left (w, area) + window_box_width (w, area);
1121 }
1122
1123 /* Get the bounding box of the display area AREA of window W, without
1124 mode lines, in frame-relative coordinates. AREA < 0 means the
1125 whole window, not including the left and right fringes of
1126 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1127 coordinates of the upper-left corner of the box. Return in
1128 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1129
1130 void
1131 window_box (struct window *w, int area, int *box_x, int *box_y,
1132 int *box_width, int *box_height)
1133 {
1134 if (box_width)
1135 *box_width = window_box_width (w, area);
1136 if (box_height)
1137 *box_height = window_box_height (w);
1138 if (box_x)
1139 *box_x = window_box_left (w, area);
1140 if (box_y)
1141 {
1142 *box_y = WINDOW_TOP_EDGE_Y (w);
1143 if (WINDOW_WANTS_HEADER_LINE_P (w))
1144 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1145 }
1146 }
1147
1148
1149 /* Get the bounding box of the display area AREA of window W, without
1150 mode lines. AREA < 0 means the whole window, not including the
1151 left and right fringe of the window. Return in *TOP_LEFT_X
1152 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1153 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1154 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1155 box. */
1156
1157 static void
1158 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1159 int *bottom_right_x, int *bottom_right_y)
1160 {
1161 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1162 bottom_right_y);
1163 *bottom_right_x += *top_left_x;
1164 *bottom_right_y += *top_left_y;
1165 }
1166
1167
1168 \f
1169 /***********************************************************************
1170 Utilities
1171 ***********************************************************************/
1172
1173 /* Return the bottom y-position of the line the iterator IT is in.
1174 This can modify IT's settings. */
1175
1176 int
1177 line_bottom_y (struct it *it)
1178 {
1179 int line_height = it->max_ascent + it->max_descent;
1180 int line_top_y = it->current_y;
1181
1182 if (line_height == 0)
1183 {
1184 if (last_height)
1185 line_height = last_height;
1186 else if (IT_CHARPOS (*it) < ZV)
1187 {
1188 move_it_by_lines (it, 1);
1189 line_height = (it->max_ascent || it->max_descent
1190 ? it->max_ascent + it->max_descent
1191 : last_height);
1192 }
1193 else
1194 {
1195 struct glyph_row *row = it->glyph_row;
1196
1197 /* Use the default character height. */
1198 it->glyph_row = NULL;
1199 it->what = IT_CHARACTER;
1200 it->c = ' ';
1201 it->len = 1;
1202 PRODUCE_GLYPHS (it);
1203 line_height = it->ascent + it->descent;
1204 it->glyph_row = row;
1205 }
1206 }
1207
1208 return line_top_y + line_height;
1209 }
1210
1211 DEFUN ("line-pixel-height", Fline_pixel_height,
1212 Sline_pixel_height, 0, 0, 0,
1213 doc: /* Return height in pixels of text line in the selected window.
1214
1215 Value is the height in pixels of the line at point. */)
1216 (void)
1217 {
1218 struct it it;
1219 struct text_pos pt;
1220 struct window *w = XWINDOW (selected_window);
1221
1222 SET_TEXT_POS (pt, PT, PT_BYTE);
1223 start_display (&it, w, pt);
1224 it.vpos = it.current_y = 0;
1225 last_height = 0;
1226 return make_number (line_bottom_y (&it));
1227 }
1228
1229 /* Return the default pixel height of text lines in window W. The
1230 value is the canonical height of the W frame's default font, plus
1231 any extra space required by the line-spacing variable or frame
1232 parameter.
1233
1234 Implementation note: this ignores any line-spacing text properties
1235 put on the newline characters. This is because those properties
1236 only affect the _screen_ line ending in the newline (i.e., in a
1237 continued line, only the last screen line will be affected), which
1238 means only a small number of lines in a buffer can ever use this
1239 feature. Since this function is used to compute the default pixel
1240 equivalent of text lines in a window, we can safely ignore those
1241 few lines. For the same reasons, we ignore the line-height
1242 properties. */
1243 int
1244 default_line_pixel_height (struct window *w)
1245 {
1246 struct frame *f = WINDOW_XFRAME (w);
1247 int height = FRAME_LINE_HEIGHT (f);
1248
1249 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1250 {
1251 struct buffer *b = XBUFFER (w->contents);
1252 Lisp_Object val = BVAR (b, extra_line_spacing);
1253
1254 if (NILP (val))
1255 val = BVAR (&buffer_defaults, extra_line_spacing);
1256 if (!NILP (val))
1257 {
1258 if (RANGED_INTEGERP (0, val, INT_MAX))
1259 height += XFASTINT (val);
1260 else if (FLOATP (val))
1261 {
1262 int addon = XFLOAT_DATA (val) * height + 0.5;
1263
1264 if (addon >= 0)
1265 height += addon;
1266 }
1267 }
1268 else
1269 height += f->extra_line_spacing;
1270 }
1271
1272 return height;
1273 }
1274
1275 /* Subroutine of pos_visible_p below. Extracts a display string, if
1276 any, from the display spec given as its argument. */
1277 static Lisp_Object
1278 string_from_display_spec (Lisp_Object spec)
1279 {
1280 if (CONSP (spec))
1281 {
1282 while (CONSP (spec))
1283 {
1284 if (STRINGP (XCAR (spec)))
1285 return XCAR (spec);
1286 spec = XCDR (spec);
1287 }
1288 }
1289 else if (VECTORP (spec))
1290 {
1291 ptrdiff_t i;
1292
1293 for (i = 0; i < ASIZE (spec); i++)
1294 {
1295 if (STRINGP (AREF (spec, i)))
1296 return AREF (spec, i);
1297 }
1298 return Qnil;
1299 }
1300
1301 return spec;
1302 }
1303
1304
1305 /* Limit insanely large values of W->hscroll on frame F to the largest
1306 value that will still prevent first_visible_x and last_visible_x of
1307 'struct it' from overflowing an int. */
1308 static int
1309 window_hscroll_limited (struct window *w, struct frame *f)
1310 {
1311 ptrdiff_t window_hscroll = w->hscroll;
1312 int window_text_width = window_box_width (w, TEXT_AREA);
1313 int colwidth = FRAME_COLUMN_WIDTH (f);
1314
1315 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1316 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1317
1318 return window_hscroll;
1319 }
1320
1321 /* Return 1 if position CHARPOS is visible in window W.
1322 CHARPOS < 0 means return info about WINDOW_END position.
1323 If visible, set *X and *Y to pixel coordinates of top left corner.
1324 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1325 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1326
1327 int
1328 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1329 int *rtop, int *rbot, int *rowh, int *vpos)
1330 {
1331 struct it it;
1332 void *itdata = bidi_shelve_cache ();
1333 struct text_pos top;
1334 int visible_p = 0;
1335 struct buffer *old_buffer = NULL;
1336
1337 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1338 return visible_p;
1339
1340 if (XBUFFER (w->contents) != current_buffer)
1341 {
1342 old_buffer = current_buffer;
1343 set_buffer_internal_1 (XBUFFER (w->contents));
1344 }
1345
1346 SET_TEXT_POS_FROM_MARKER (top, w->start);
1347 /* Scrolling a minibuffer window via scroll bar when the echo area
1348 shows long text sometimes resets the minibuffer contents behind
1349 our backs. */
1350 if (CHARPOS (top) > ZV)
1351 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1352
1353 /* Compute exact mode line heights. */
1354 if (WINDOW_WANTS_MODELINE_P (w))
1355 current_mode_line_height
1356 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1357 BVAR (current_buffer, mode_line_format));
1358
1359 if (WINDOW_WANTS_HEADER_LINE_P (w))
1360 current_header_line_height
1361 = display_mode_line (w, HEADER_LINE_FACE_ID,
1362 BVAR (current_buffer, header_line_format));
1363
1364 start_display (&it, w, top);
1365 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1366 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1367
1368 if (charpos >= 0
1369 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1370 && IT_CHARPOS (it) >= charpos)
1371 /* When scanning backwards under bidi iteration, move_it_to
1372 stops at or _before_ CHARPOS, because it stops at or to
1373 the _right_ of the character at CHARPOS. */
1374 || (it.bidi_p && it.bidi_it.scan_dir == -1
1375 && IT_CHARPOS (it) <= charpos)))
1376 {
1377 /* We have reached CHARPOS, or passed it. How the call to
1378 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1379 or covered by a display property, move_it_to stops at the end
1380 of the invisible text, to the right of CHARPOS. (ii) If
1381 CHARPOS is in a display vector, move_it_to stops on its last
1382 glyph. */
1383 int top_x = it.current_x;
1384 int top_y = it.current_y;
1385 /* Calling line_bottom_y may change it.method, it.position, etc. */
1386 enum it_method it_method = it.method;
1387 int bottom_y = (last_height = 0, line_bottom_y (&it));
1388 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1389
1390 if (top_y < window_top_y)
1391 visible_p = bottom_y > window_top_y;
1392 else if (top_y < it.last_visible_y)
1393 visible_p = 1;
1394 if (bottom_y >= it.last_visible_y
1395 && it.bidi_p && it.bidi_it.scan_dir == -1
1396 && IT_CHARPOS (it) < charpos)
1397 {
1398 /* When the last line of the window is scanned backwards
1399 under bidi iteration, we could be duped into thinking
1400 that we have passed CHARPOS, when in fact move_it_to
1401 simply stopped short of CHARPOS because it reached
1402 last_visible_y. To see if that's what happened, we call
1403 move_it_to again with a slightly larger vertical limit,
1404 and see if it actually moved vertically; if it did, we
1405 didn't really reach CHARPOS, which is beyond window end. */
1406 struct it save_it = it;
1407 /* Why 10? because we don't know how many canonical lines
1408 will the height of the next line(s) be. So we guess. */
1409 int ten_more_lines = 10 * default_line_pixel_height (w);
1410
1411 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1412 MOVE_TO_POS | MOVE_TO_Y);
1413 if (it.current_y > top_y)
1414 visible_p = 0;
1415
1416 it = save_it;
1417 }
1418 if (visible_p)
1419 {
1420 if (it_method == GET_FROM_DISPLAY_VECTOR)
1421 {
1422 /* We stopped on the last glyph of a display vector.
1423 Try and recompute. Hack alert! */
1424 if (charpos < 2 || top.charpos >= charpos)
1425 top_x = it.glyph_row->x;
1426 else
1427 {
1428 struct it it2, it2_prev;
1429 /* The idea is to get to the previous buffer
1430 position, consume the character there, and use
1431 the pixel coordinates we get after that. But if
1432 the previous buffer position is also displayed
1433 from a display vector, we need to consume all of
1434 the glyphs from that display vector. */
1435 start_display (&it2, w, top);
1436 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1437 /* If we didn't get to CHARPOS - 1, there's some
1438 replacing display property at that position, and
1439 we stopped after it. That is exactly the place
1440 whose coordinates we want. */
1441 if (IT_CHARPOS (it2) != charpos - 1)
1442 it2_prev = it2;
1443 else
1444 {
1445 /* Iterate until we get out of the display
1446 vector that displays the character at
1447 CHARPOS - 1. */
1448 do {
1449 get_next_display_element (&it2);
1450 PRODUCE_GLYPHS (&it2);
1451 it2_prev = it2;
1452 set_iterator_to_next (&it2, 1);
1453 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1454 && IT_CHARPOS (it2) < charpos);
1455 }
1456 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1457 || it2_prev.current_x > it2_prev.last_visible_x)
1458 top_x = it.glyph_row->x;
1459 else
1460 {
1461 top_x = it2_prev.current_x;
1462 top_y = it2_prev.current_y;
1463 }
1464 }
1465 }
1466 else if (IT_CHARPOS (it) != charpos)
1467 {
1468 Lisp_Object cpos = make_number (charpos);
1469 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1470 Lisp_Object string = string_from_display_spec (spec);
1471 struct text_pos tpos;
1472 int replacing_spec_p;
1473 bool newline_in_string
1474 = (STRINGP (string)
1475 && memchr (SDATA (string), '\n', SBYTES (string)));
1476
1477 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1478 replacing_spec_p
1479 = (!NILP (spec)
1480 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1481 charpos, FRAME_WINDOW_P (it.f)));
1482 /* The tricky code below is needed because there's a
1483 discrepancy between move_it_to and how we set cursor
1484 when PT is at the beginning of a portion of text
1485 covered by a display property or an overlay with a
1486 display property, or the display line ends in a
1487 newline from a display string. move_it_to will stop
1488 _after_ such display strings, whereas
1489 set_cursor_from_row conspires with cursor_row_p to
1490 place the cursor on the first glyph produced from the
1491 display string. */
1492
1493 /* We have overshoot PT because it is covered by a
1494 display property that replaces the text it covers.
1495 If the string includes embedded newlines, we are also
1496 in the wrong display line. Backtrack to the correct
1497 line, where the display property begins. */
1498 if (replacing_spec_p)
1499 {
1500 Lisp_Object startpos, endpos;
1501 EMACS_INT start, end;
1502 struct it it3;
1503 int it3_moved;
1504
1505 /* Find the first and the last buffer positions
1506 covered by the display string. */
1507 endpos =
1508 Fnext_single_char_property_change (cpos, Qdisplay,
1509 Qnil, Qnil);
1510 startpos =
1511 Fprevious_single_char_property_change (endpos, Qdisplay,
1512 Qnil, Qnil);
1513 start = XFASTINT (startpos);
1514 end = XFASTINT (endpos);
1515 /* Move to the last buffer position before the
1516 display property. */
1517 start_display (&it3, w, top);
1518 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1519 /* Move forward one more line if the position before
1520 the display string is a newline or if it is the
1521 rightmost character on a line that is
1522 continued or word-wrapped. */
1523 if (it3.method == GET_FROM_BUFFER
1524 && (it3.c == '\n'
1525 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1526 move_it_by_lines (&it3, 1);
1527 else if (move_it_in_display_line_to (&it3, -1,
1528 it3.current_x
1529 + it3.pixel_width,
1530 MOVE_TO_X)
1531 == MOVE_LINE_CONTINUED)
1532 {
1533 move_it_by_lines (&it3, 1);
1534 /* When we are under word-wrap, the #$@%!
1535 move_it_by_lines moves 2 lines, so we need to
1536 fix that up. */
1537 if (it3.line_wrap == WORD_WRAP)
1538 move_it_by_lines (&it3, -1);
1539 }
1540
1541 /* Record the vertical coordinate of the display
1542 line where we wound up. */
1543 top_y = it3.current_y;
1544 if (it3.bidi_p)
1545 {
1546 /* When characters are reordered for display,
1547 the character displayed to the left of the
1548 display string could be _after_ the display
1549 property in the logical order. Use the
1550 smallest vertical position of these two. */
1551 start_display (&it3, w, top);
1552 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1553 if (it3.current_y < top_y)
1554 top_y = it3.current_y;
1555 }
1556 /* Move from the top of the window to the beginning
1557 of the display line where the display string
1558 begins. */
1559 start_display (&it3, w, top);
1560 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1561 /* If it3_moved stays zero after the 'while' loop
1562 below, that means we already were at a newline
1563 before the loop (e.g., the display string begins
1564 with a newline), so we don't need to (and cannot)
1565 inspect the glyphs of it3.glyph_row, because
1566 PRODUCE_GLYPHS will not produce anything for a
1567 newline, and thus it3.glyph_row stays at its
1568 stale content it got at top of the window. */
1569 it3_moved = 0;
1570 /* Finally, advance the iterator until we hit the
1571 first display element whose character position is
1572 CHARPOS, or until the first newline from the
1573 display string, which signals the end of the
1574 display line. */
1575 while (get_next_display_element (&it3))
1576 {
1577 PRODUCE_GLYPHS (&it3);
1578 if (IT_CHARPOS (it3) == charpos
1579 || ITERATOR_AT_END_OF_LINE_P (&it3))
1580 break;
1581 it3_moved = 1;
1582 set_iterator_to_next (&it3, 0);
1583 }
1584 top_x = it3.current_x - it3.pixel_width;
1585 /* Normally, we would exit the above loop because we
1586 found the display element whose character
1587 position is CHARPOS. For the contingency that we
1588 didn't, and stopped at the first newline from the
1589 display string, move back over the glyphs
1590 produced from the string, until we find the
1591 rightmost glyph not from the string. */
1592 if (it3_moved
1593 && newline_in_string
1594 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1595 {
1596 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1597 + it3.glyph_row->used[TEXT_AREA];
1598
1599 while (EQ ((g - 1)->object, string))
1600 {
1601 --g;
1602 top_x -= g->pixel_width;
1603 }
1604 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1605 + it3.glyph_row->used[TEXT_AREA]);
1606 }
1607 }
1608 }
1609
1610 *x = top_x;
1611 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1612 *rtop = max (0, window_top_y - top_y);
1613 *rbot = max (0, bottom_y - it.last_visible_y);
1614 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1615 - max (top_y, window_top_y)));
1616 *vpos = it.vpos;
1617 }
1618 }
1619 else
1620 {
1621 /* We were asked to provide info about WINDOW_END. */
1622 struct it it2;
1623 void *it2data = NULL;
1624
1625 SAVE_IT (it2, it, it2data);
1626 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1627 move_it_by_lines (&it, 1);
1628 if (charpos < IT_CHARPOS (it)
1629 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1630 {
1631 visible_p = 1;
1632 RESTORE_IT (&it2, &it2, it2data);
1633 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1634 *x = it2.current_x;
1635 *y = it2.current_y + it2.max_ascent - it2.ascent;
1636 *rtop = max (0, -it2.current_y);
1637 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1638 - it.last_visible_y));
1639 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1640 it.last_visible_y)
1641 - max (it2.current_y,
1642 WINDOW_HEADER_LINE_HEIGHT (w))));
1643 *vpos = it2.vpos;
1644 }
1645 else
1646 bidi_unshelve_cache (it2data, 1);
1647 }
1648 bidi_unshelve_cache (itdata, 0);
1649
1650 if (old_buffer)
1651 set_buffer_internal_1 (old_buffer);
1652
1653 current_header_line_height = current_mode_line_height = -1;
1654
1655 if (visible_p && w->hscroll > 0)
1656 *x -=
1657 window_hscroll_limited (w, WINDOW_XFRAME (w))
1658 * WINDOW_FRAME_COLUMN_WIDTH (w);
1659
1660 #if 0
1661 /* Debugging code. */
1662 if (visible_p)
1663 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1664 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1665 else
1666 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1667 #endif
1668
1669 return visible_p;
1670 }
1671
1672
1673 /* Return the next character from STR. Return in *LEN the length of
1674 the character. This is like STRING_CHAR_AND_LENGTH but never
1675 returns an invalid character. If we find one, we return a `?', but
1676 with the length of the invalid character. */
1677
1678 static int
1679 string_char_and_length (const unsigned char *str, int *len)
1680 {
1681 int c;
1682
1683 c = STRING_CHAR_AND_LENGTH (str, *len);
1684 if (!CHAR_VALID_P (c))
1685 /* We may not change the length here because other places in Emacs
1686 don't use this function, i.e. they silently accept invalid
1687 characters. */
1688 c = '?';
1689
1690 return c;
1691 }
1692
1693
1694
1695 /* Given a position POS containing a valid character and byte position
1696 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1697
1698 static struct text_pos
1699 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1700 {
1701 eassert (STRINGP (string) && nchars >= 0);
1702
1703 if (STRING_MULTIBYTE (string))
1704 {
1705 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1706 int len;
1707
1708 while (nchars--)
1709 {
1710 string_char_and_length (p, &len);
1711 p += len;
1712 CHARPOS (pos) += 1;
1713 BYTEPOS (pos) += len;
1714 }
1715 }
1716 else
1717 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1718
1719 return pos;
1720 }
1721
1722
1723 /* Value is the text position, i.e. character and byte position,
1724 for character position CHARPOS in STRING. */
1725
1726 static struct text_pos
1727 string_pos (ptrdiff_t charpos, Lisp_Object string)
1728 {
1729 struct text_pos pos;
1730 eassert (STRINGP (string));
1731 eassert (charpos >= 0);
1732 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1733 return pos;
1734 }
1735
1736
1737 /* Value is a text position, i.e. character and byte position, for
1738 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1739 means recognize multibyte characters. */
1740
1741 static struct text_pos
1742 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1743 {
1744 struct text_pos pos;
1745
1746 eassert (s != NULL);
1747 eassert (charpos >= 0);
1748
1749 if (multibyte_p)
1750 {
1751 int len;
1752
1753 SET_TEXT_POS (pos, 0, 0);
1754 while (charpos--)
1755 {
1756 string_char_and_length ((const unsigned char *) s, &len);
1757 s += len;
1758 CHARPOS (pos) += 1;
1759 BYTEPOS (pos) += len;
1760 }
1761 }
1762 else
1763 SET_TEXT_POS (pos, charpos, charpos);
1764
1765 return pos;
1766 }
1767
1768
1769 /* Value is the number of characters in C string S. MULTIBYTE_P
1770 non-zero means recognize multibyte characters. */
1771
1772 static ptrdiff_t
1773 number_of_chars (const char *s, bool multibyte_p)
1774 {
1775 ptrdiff_t nchars;
1776
1777 if (multibyte_p)
1778 {
1779 ptrdiff_t rest = strlen (s);
1780 int len;
1781 const unsigned char *p = (const unsigned char *) s;
1782
1783 for (nchars = 0; rest > 0; ++nchars)
1784 {
1785 string_char_and_length (p, &len);
1786 rest -= len, p += len;
1787 }
1788 }
1789 else
1790 nchars = strlen (s);
1791
1792 return nchars;
1793 }
1794
1795
1796 /* Compute byte position NEWPOS->bytepos corresponding to
1797 NEWPOS->charpos. POS is a known position in string STRING.
1798 NEWPOS->charpos must be >= POS.charpos. */
1799
1800 static void
1801 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1802 {
1803 eassert (STRINGP (string));
1804 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1805
1806 if (STRING_MULTIBYTE (string))
1807 *newpos = string_pos_nchars_ahead (pos, string,
1808 CHARPOS (*newpos) - CHARPOS (pos));
1809 else
1810 BYTEPOS (*newpos) = CHARPOS (*newpos);
1811 }
1812
1813 /* EXPORT:
1814 Return an estimation of the pixel height of mode or header lines on
1815 frame F. FACE_ID specifies what line's height to estimate. */
1816
1817 int
1818 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1819 {
1820 #ifdef HAVE_WINDOW_SYSTEM
1821 if (FRAME_WINDOW_P (f))
1822 {
1823 int height = FONT_HEIGHT (FRAME_FONT (f));
1824
1825 /* This function is called so early when Emacs starts that the face
1826 cache and mode line face are not yet initialized. */
1827 if (FRAME_FACE_CACHE (f))
1828 {
1829 struct face *face = FACE_FROM_ID (f, face_id);
1830 if (face)
1831 {
1832 if (face->font)
1833 height = FONT_HEIGHT (face->font);
1834 if (face->box_line_width > 0)
1835 height += 2 * face->box_line_width;
1836 }
1837 }
1838
1839 return height;
1840 }
1841 #endif
1842
1843 return 1;
1844 }
1845
1846 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1847 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1848 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1849 not force the value into range. */
1850
1851 void
1852 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1853 int *x, int *y, NativeRectangle *bounds, int noclip)
1854 {
1855
1856 #ifdef HAVE_WINDOW_SYSTEM
1857 if (FRAME_WINDOW_P (f))
1858 {
1859 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1860 even for negative values. */
1861 if (pix_x < 0)
1862 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1863 if (pix_y < 0)
1864 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1865
1866 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1867 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1868
1869 if (bounds)
1870 STORE_NATIVE_RECT (*bounds,
1871 FRAME_COL_TO_PIXEL_X (f, pix_x),
1872 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1873 FRAME_COLUMN_WIDTH (f) - 1,
1874 FRAME_LINE_HEIGHT (f) - 1);
1875
1876 if (!noclip)
1877 {
1878 if (pix_x < 0)
1879 pix_x = 0;
1880 else if (pix_x > FRAME_TOTAL_COLS (f))
1881 pix_x = FRAME_TOTAL_COLS (f);
1882
1883 if (pix_y < 0)
1884 pix_y = 0;
1885 else if (pix_y > FRAME_LINES (f))
1886 pix_y = FRAME_LINES (f);
1887 }
1888 }
1889 #endif
1890
1891 *x = pix_x;
1892 *y = pix_y;
1893 }
1894
1895
1896 /* Find the glyph under window-relative coordinates X/Y in window W.
1897 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1898 strings. Return in *HPOS and *VPOS the row and column number of
1899 the glyph found. Return in *AREA the glyph area containing X.
1900 Value is a pointer to the glyph found or null if X/Y is not on
1901 text, or we can't tell because W's current matrix is not up to
1902 date. */
1903
1904 static
1905 struct glyph *
1906 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1907 int *dx, int *dy, int *area)
1908 {
1909 struct glyph *glyph, *end;
1910 struct glyph_row *row = NULL;
1911 int x0, i;
1912
1913 /* Find row containing Y. Give up if some row is not enabled. */
1914 for (i = 0; i < w->current_matrix->nrows; ++i)
1915 {
1916 row = MATRIX_ROW (w->current_matrix, i);
1917 if (!row->enabled_p)
1918 return NULL;
1919 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1920 break;
1921 }
1922
1923 *vpos = i;
1924 *hpos = 0;
1925
1926 /* Give up if Y is not in the window. */
1927 if (i == w->current_matrix->nrows)
1928 return NULL;
1929
1930 /* Get the glyph area containing X. */
1931 if (w->pseudo_window_p)
1932 {
1933 *area = TEXT_AREA;
1934 x0 = 0;
1935 }
1936 else
1937 {
1938 if (x < window_box_left_offset (w, TEXT_AREA))
1939 {
1940 *area = LEFT_MARGIN_AREA;
1941 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1942 }
1943 else if (x < window_box_right_offset (w, TEXT_AREA))
1944 {
1945 *area = TEXT_AREA;
1946 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1947 }
1948 else
1949 {
1950 *area = RIGHT_MARGIN_AREA;
1951 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1952 }
1953 }
1954
1955 /* Find glyph containing X. */
1956 glyph = row->glyphs[*area];
1957 end = glyph + row->used[*area];
1958 x -= x0;
1959 while (glyph < end && x >= glyph->pixel_width)
1960 {
1961 x -= glyph->pixel_width;
1962 ++glyph;
1963 }
1964
1965 if (glyph == end)
1966 return NULL;
1967
1968 if (dx)
1969 {
1970 *dx = x;
1971 *dy = y - (row->y + row->ascent - glyph->ascent);
1972 }
1973
1974 *hpos = glyph - row->glyphs[*area];
1975 return glyph;
1976 }
1977
1978 /* Convert frame-relative x/y to coordinates relative to window W.
1979 Takes pseudo-windows into account. */
1980
1981 static void
1982 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1983 {
1984 if (w->pseudo_window_p)
1985 {
1986 /* A pseudo-window is always full-width, and starts at the
1987 left edge of the frame, plus a frame border. */
1988 struct frame *f = XFRAME (w->frame);
1989 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1990 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1991 }
1992 else
1993 {
1994 *x -= WINDOW_LEFT_EDGE_X (w);
1995 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1996 }
1997 }
1998
1999 #ifdef HAVE_WINDOW_SYSTEM
2000
2001 /* EXPORT:
2002 Return in RECTS[] at most N clipping rectangles for glyph string S.
2003 Return the number of stored rectangles. */
2004
2005 int
2006 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2007 {
2008 XRectangle r;
2009
2010 if (n <= 0)
2011 return 0;
2012
2013 if (s->row->full_width_p)
2014 {
2015 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2016 r.x = WINDOW_LEFT_EDGE_X (s->w);
2017 r.width = WINDOW_TOTAL_WIDTH (s->w);
2018
2019 /* Unless displaying a mode or menu bar line, which are always
2020 fully visible, clip to the visible part of the row. */
2021 if (s->w->pseudo_window_p)
2022 r.height = s->row->visible_height;
2023 else
2024 r.height = s->height;
2025 }
2026 else
2027 {
2028 /* This is a text line that may be partially visible. */
2029 r.x = window_box_left (s->w, s->area);
2030 r.width = window_box_width (s->w, s->area);
2031 r.height = s->row->visible_height;
2032 }
2033
2034 if (s->clip_head)
2035 if (r.x < s->clip_head->x)
2036 {
2037 if (r.width >= s->clip_head->x - r.x)
2038 r.width -= s->clip_head->x - r.x;
2039 else
2040 r.width = 0;
2041 r.x = s->clip_head->x;
2042 }
2043 if (s->clip_tail)
2044 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2045 {
2046 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2047 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2048 else
2049 r.width = 0;
2050 }
2051
2052 /* If S draws overlapping rows, it's sufficient to use the top and
2053 bottom of the window for clipping because this glyph string
2054 intentionally draws over other lines. */
2055 if (s->for_overlaps)
2056 {
2057 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2058 r.height = window_text_bottom_y (s->w) - r.y;
2059
2060 /* Alas, the above simple strategy does not work for the
2061 environments with anti-aliased text: if the same text is
2062 drawn onto the same place multiple times, it gets thicker.
2063 If the overlap we are processing is for the erased cursor, we
2064 take the intersection with the rectangle of the cursor. */
2065 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2066 {
2067 XRectangle rc, r_save = r;
2068
2069 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2070 rc.y = s->w->phys_cursor.y;
2071 rc.width = s->w->phys_cursor_width;
2072 rc.height = s->w->phys_cursor_height;
2073
2074 x_intersect_rectangles (&r_save, &rc, &r);
2075 }
2076 }
2077 else
2078 {
2079 /* Don't use S->y for clipping because it doesn't take partially
2080 visible lines into account. For example, it can be negative for
2081 partially visible lines at the top of a window. */
2082 if (!s->row->full_width_p
2083 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2084 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2085 else
2086 r.y = max (0, s->row->y);
2087 }
2088
2089 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2090
2091 /* If drawing the cursor, don't let glyph draw outside its
2092 advertised boundaries. Cleartype does this under some circumstances. */
2093 if (s->hl == DRAW_CURSOR)
2094 {
2095 struct glyph *glyph = s->first_glyph;
2096 int height, max_y;
2097
2098 if (s->x > r.x)
2099 {
2100 r.width -= s->x - r.x;
2101 r.x = s->x;
2102 }
2103 r.width = min (r.width, glyph->pixel_width);
2104
2105 /* If r.y is below window bottom, ensure that we still see a cursor. */
2106 height = min (glyph->ascent + glyph->descent,
2107 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2108 max_y = window_text_bottom_y (s->w) - height;
2109 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2110 if (s->ybase - glyph->ascent > max_y)
2111 {
2112 r.y = max_y;
2113 r.height = height;
2114 }
2115 else
2116 {
2117 /* Don't draw cursor glyph taller than our actual glyph. */
2118 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2119 if (height < r.height)
2120 {
2121 max_y = r.y + r.height;
2122 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2123 r.height = min (max_y - r.y, height);
2124 }
2125 }
2126 }
2127
2128 if (s->row->clip)
2129 {
2130 XRectangle r_save = r;
2131
2132 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2133 r.width = 0;
2134 }
2135
2136 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2137 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2138 {
2139 #ifdef CONVERT_FROM_XRECT
2140 CONVERT_FROM_XRECT (r, *rects);
2141 #else
2142 *rects = r;
2143 #endif
2144 return 1;
2145 }
2146 else
2147 {
2148 /* If we are processing overlapping and allowed to return
2149 multiple clipping rectangles, we exclude the row of the glyph
2150 string from the clipping rectangle. This is to avoid drawing
2151 the same text on the environment with anti-aliasing. */
2152 #ifdef CONVERT_FROM_XRECT
2153 XRectangle rs[2];
2154 #else
2155 XRectangle *rs = rects;
2156 #endif
2157 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2158
2159 if (s->for_overlaps & OVERLAPS_PRED)
2160 {
2161 rs[i] = r;
2162 if (r.y + r.height > row_y)
2163 {
2164 if (r.y < row_y)
2165 rs[i].height = row_y - r.y;
2166 else
2167 rs[i].height = 0;
2168 }
2169 i++;
2170 }
2171 if (s->for_overlaps & OVERLAPS_SUCC)
2172 {
2173 rs[i] = r;
2174 if (r.y < row_y + s->row->visible_height)
2175 {
2176 if (r.y + r.height > row_y + s->row->visible_height)
2177 {
2178 rs[i].y = row_y + s->row->visible_height;
2179 rs[i].height = r.y + r.height - rs[i].y;
2180 }
2181 else
2182 rs[i].height = 0;
2183 }
2184 i++;
2185 }
2186
2187 n = i;
2188 #ifdef CONVERT_FROM_XRECT
2189 for (i = 0; i < n; i++)
2190 CONVERT_FROM_XRECT (rs[i], rects[i]);
2191 #endif
2192 return n;
2193 }
2194 }
2195
2196 /* EXPORT:
2197 Return in *NR the clipping rectangle for glyph string S. */
2198
2199 void
2200 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2201 {
2202 get_glyph_string_clip_rects (s, nr, 1);
2203 }
2204
2205
2206 /* EXPORT:
2207 Return the position and height of the phys cursor in window W.
2208 Set w->phys_cursor_width to width of phys cursor.
2209 */
2210
2211 void
2212 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2213 struct glyph *glyph, int *xp, int *yp, int *heightp)
2214 {
2215 struct frame *f = XFRAME (WINDOW_FRAME (w));
2216 int x, y, wd, h, h0, y0;
2217
2218 /* Compute the width of the rectangle to draw. If on a stretch
2219 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2220 rectangle as wide as the glyph, but use a canonical character
2221 width instead. */
2222 wd = glyph->pixel_width - 1;
2223 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2224 wd++; /* Why? */
2225 #endif
2226
2227 x = w->phys_cursor.x;
2228 if (x < 0)
2229 {
2230 wd += x;
2231 x = 0;
2232 }
2233
2234 if (glyph->type == STRETCH_GLYPH
2235 && !x_stretch_cursor_p)
2236 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2237 w->phys_cursor_width = wd;
2238
2239 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2240
2241 /* If y is below window bottom, ensure that we still see a cursor. */
2242 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2243
2244 h = max (h0, glyph->ascent + glyph->descent);
2245 h0 = min (h0, glyph->ascent + glyph->descent);
2246
2247 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2248 if (y < y0)
2249 {
2250 h = max (h - (y0 - y) + 1, h0);
2251 y = y0 - 1;
2252 }
2253 else
2254 {
2255 y0 = window_text_bottom_y (w) - h0;
2256 if (y > y0)
2257 {
2258 h += y - y0;
2259 y = y0;
2260 }
2261 }
2262
2263 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2264 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2265 *heightp = h;
2266 }
2267
2268 /*
2269 * Remember which glyph the mouse is over.
2270 */
2271
2272 void
2273 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2274 {
2275 Lisp_Object window;
2276 struct window *w;
2277 struct glyph_row *r, *gr, *end_row;
2278 enum window_part part;
2279 enum glyph_row_area area;
2280 int x, y, width, height;
2281
2282 /* Try to determine frame pixel position and size of the glyph under
2283 frame pixel coordinates X/Y on frame F. */
2284
2285 if (!f->glyphs_initialized_p
2286 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2287 NILP (window)))
2288 {
2289 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2290 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2291 goto virtual_glyph;
2292 }
2293
2294 w = XWINDOW (window);
2295 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2296 height = WINDOW_FRAME_LINE_HEIGHT (w);
2297
2298 x = window_relative_x_coord (w, part, gx);
2299 y = gy - WINDOW_TOP_EDGE_Y (w);
2300
2301 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2302 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2303
2304 if (w->pseudo_window_p)
2305 {
2306 area = TEXT_AREA;
2307 part = ON_MODE_LINE; /* Don't adjust margin. */
2308 goto text_glyph;
2309 }
2310
2311 switch (part)
2312 {
2313 case ON_LEFT_MARGIN:
2314 area = LEFT_MARGIN_AREA;
2315 goto text_glyph;
2316
2317 case ON_RIGHT_MARGIN:
2318 area = RIGHT_MARGIN_AREA;
2319 goto text_glyph;
2320
2321 case ON_HEADER_LINE:
2322 case ON_MODE_LINE:
2323 gr = (part == ON_HEADER_LINE
2324 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2325 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2326 gy = gr->y;
2327 area = TEXT_AREA;
2328 goto text_glyph_row_found;
2329
2330 case ON_TEXT:
2331 area = TEXT_AREA;
2332
2333 text_glyph:
2334 gr = 0; gy = 0;
2335 for (; r <= end_row && r->enabled_p; ++r)
2336 if (r->y + r->height > y)
2337 {
2338 gr = r; gy = r->y;
2339 break;
2340 }
2341
2342 text_glyph_row_found:
2343 if (gr && gy <= y)
2344 {
2345 struct glyph *g = gr->glyphs[area];
2346 struct glyph *end = g + gr->used[area];
2347
2348 height = gr->height;
2349 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2350 if (gx + g->pixel_width > x)
2351 break;
2352
2353 if (g < end)
2354 {
2355 if (g->type == IMAGE_GLYPH)
2356 {
2357 /* Don't remember when mouse is over image, as
2358 image may have hot-spots. */
2359 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2360 return;
2361 }
2362 width = g->pixel_width;
2363 }
2364 else
2365 {
2366 /* Use nominal char spacing at end of line. */
2367 x -= gx;
2368 gx += (x / width) * width;
2369 }
2370
2371 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2372 gx += window_box_left_offset (w, area);
2373 }
2374 else
2375 {
2376 /* Use nominal line height at end of window. */
2377 gx = (x / width) * width;
2378 y -= gy;
2379 gy += (y / height) * height;
2380 }
2381 break;
2382
2383 case ON_LEFT_FRINGE:
2384 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2385 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2386 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2387 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2388 goto row_glyph;
2389
2390 case ON_RIGHT_FRINGE:
2391 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2392 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2393 : window_box_right_offset (w, TEXT_AREA));
2394 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2395 goto row_glyph;
2396
2397 case ON_SCROLL_BAR:
2398 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2399 ? 0
2400 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2401 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2402 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2403 : 0)));
2404 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2405
2406 row_glyph:
2407 gr = 0, gy = 0;
2408 for (; r <= end_row && r->enabled_p; ++r)
2409 if (r->y + r->height > y)
2410 {
2411 gr = r; gy = r->y;
2412 break;
2413 }
2414
2415 if (gr && gy <= y)
2416 height = gr->height;
2417 else
2418 {
2419 /* Use nominal line height at end of window. */
2420 y -= gy;
2421 gy += (y / height) * height;
2422 }
2423 break;
2424
2425 default:
2426 ;
2427 virtual_glyph:
2428 /* If there is no glyph under the mouse, then we divide the screen
2429 into a grid of the smallest glyph in the frame, and use that
2430 as our "glyph". */
2431
2432 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2433 round down even for negative values. */
2434 if (gx < 0)
2435 gx -= width - 1;
2436 if (gy < 0)
2437 gy -= height - 1;
2438
2439 gx = (gx / width) * width;
2440 gy = (gy / height) * height;
2441
2442 goto store_rect;
2443 }
2444
2445 gx += WINDOW_LEFT_EDGE_X (w);
2446 gy += WINDOW_TOP_EDGE_Y (w);
2447
2448 store_rect:
2449 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2450
2451 /* Visible feedback for debugging. */
2452 #if 0
2453 #if HAVE_X_WINDOWS
2454 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2455 f->output_data.x->normal_gc,
2456 gx, gy, width, height);
2457 #endif
2458 #endif
2459 }
2460
2461
2462 #endif /* HAVE_WINDOW_SYSTEM */
2463
2464 \f
2465 /***********************************************************************
2466 Lisp form evaluation
2467 ***********************************************************************/
2468
2469 /* Error handler for safe_eval and safe_call. */
2470
2471 static Lisp_Object
2472 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2473 {
2474 add_to_log ("Error during redisplay: %S signaled %S",
2475 Flist (nargs, args), arg);
2476 return Qnil;
2477 }
2478
2479 /* Call function FUNC with the rest of NARGS - 1 arguments
2480 following. Return the result, or nil if something went
2481 wrong. Prevent redisplay during the evaluation. */
2482
2483 Lisp_Object
2484 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2485 {
2486 Lisp_Object val;
2487
2488 if (inhibit_eval_during_redisplay)
2489 val = Qnil;
2490 else
2491 {
2492 va_list ap;
2493 ptrdiff_t i;
2494 ptrdiff_t count = SPECPDL_INDEX ();
2495 struct gcpro gcpro1;
2496 Lisp_Object *args = alloca (nargs * word_size);
2497
2498 args[0] = func;
2499 va_start (ap, func);
2500 for (i = 1; i < nargs; i++)
2501 args[i] = va_arg (ap, Lisp_Object);
2502 va_end (ap);
2503
2504 GCPRO1 (args[0]);
2505 gcpro1.nvars = nargs;
2506 specbind (Qinhibit_redisplay, Qt);
2507 /* Use Qt to ensure debugger does not run,
2508 so there is no possibility of wanting to redisplay. */
2509 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2510 safe_eval_handler);
2511 UNGCPRO;
2512 val = unbind_to (count, val);
2513 }
2514
2515 return val;
2516 }
2517
2518
2519 /* Call function FN with one argument ARG.
2520 Return the result, or nil if something went wrong. */
2521
2522 Lisp_Object
2523 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2524 {
2525 return safe_call (2, fn, arg);
2526 }
2527
2528 static Lisp_Object Qeval;
2529
2530 Lisp_Object
2531 safe_eval (Lisp_Object sexpr)
2532 {
2533 return safe_call1 (Qeval, sexpr);
2534 }
2535
2536 /* Call function FN with two arguments ARG1 and ARG2.
2537 Return the result, or nil if something went wrong. */
2538
2539 Lisp_Object
2540 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2541 {
2542 return safe_call (3, fn, arg1, arg2);
2543 }
2544
2545
2546 \f
2547 /***********************************************************************
2548 Debugging
2549 ***********************************************************************/
2550
2551 #if 0
2552
2553 /* Define CHECK_IT to perform sanity checks on iterators.
2554 This is for debugging. It is too slow to do unconditionally. */
2555
2556 static void
2557 check_it (struct it *it)
2558 {
2559 if (it->method == GET_FROM_STRING)
2560 {
2561 eassert (STRINGP (it->string));
2562 eassert (IT_STRING_CHARPOS (*it) >= 0);
2563 }
2564 else
2565 {
2566 eassert (IT_STRING_CHARPOS (*it) < 0);
2567 if (it->method == GET_FROM_BUFFER)
2568 {
2569 /* Check that character and byte positions agree. */
2570 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2571 }
2572 }
2573
2574 if (it->dpvec)
2575 eassert (it->current.dpvec_index >= 0);
2576 else
2577 eassert (it->current.dpvec_index < 0);
2578 }
2579
2580 #define CHECK_IT(IT) check_it ((IT))
2581
2582 #else /* not 0 */
2583
2584 #define CHECK_IT(IT) (void) 0
2585
2586 #endif /* not 0 */
2587
2588
2589 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2590
2591 /* Check that the window end of window W is what we expect it
2592 to be---the last row in the current matrix displaying text. */
2593
2594 static void
2595 check_window_end (struct window *w)
2596 {
2597 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2598 {
2599 struct glyph_row *row;
2600 eassert ((row = MATRIX_ROW (w->current_matrix,
2601 XFASTINT (w->window_end_vpos)),
2602 !row->enabled_p
2603 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2604 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2605 }
2606 }
2607
2608 #define CHECK_WINDOW_END(W) check_window_end ((W))
2609
2610 #else
2611
2612 #define CHECK_WINDOW_END(W) (void) 0
2613
2614 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2615
2616 /* Return mark position if current buffer has the region of non-zero length,
2617 or -1 otherwise. */
2618
2619 static ptrdiff_t
2620 markpos_of_region (void)
2621 {
2622 if (!NILP (Vtransient_mark_mode)
2623 && !NILP (BVAR (current_buffer, mark_active))
2624 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2625 {
2626 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2627
2628 if (markpos != PT)
2629 return markpos;
2630 }
2631 return -1;
2632 }
2633
2634 /***********************************************************************
2635 Iterator initialization
2636 ***********************************************************************/
2637
2638 /* Initialize IT for displaying current_buffer in window W, starting
2639 at character position CHARPOS. CHARPOS < 0 means that no buffer
2640 position is specified which is useful when the iterator is assigned
2641 a position later. BYTEPOS is the byte position corresponding to
2642 CHARPOS.
2643
2644 If ROW is not null, calls to produce_glyphs with IT as parameter
2645 will produce glyphs in that row.
2646
2647 BASE_FACE_ID is the id of a base face to use. It must be one of
2648 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2649 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2650 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2651
2652 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2653 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2654 will be initialized to use the corresponding mode line glyph row of
2655 the desired matrix of W. */
2656
2657 void
2658 init_iterator (struct it *it, struct window *w,
2659 ptrdiff_t charpos, ptrdiff_t bytepos,
2660 struct glyph_row *row, enum face_id base_face_id)
2661 {
2662 ptrdiff_t markpos;
2663 enum face_id remapped_base_face_id = base_face_id;
2664
2665 /* Some precondition checks. */
2666 eassert (w != NULL && it != NULL);
2667 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2668 && charpos <= ZV));
2669
2670 /* If face attributes have been changed since the last redisplay,
2671 free realized faces now because they depend on face definitions
2672 that might have changed. Don't free faces while there might be
2673 desired matrices pending which reference these faces. */
2674 if (face_change_count && !inhibit_free_realized_faces)
2675 {
2676 face_change_count = 0;
2677 free_all_realized_faces (Qnil);
2678 }
2679
2680 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2681 if (! NILP (Vface_remapping_alist))
2682 remapped_base_face_id
2683 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2684
2685 /* Use one of the mode line rows of W's desired matrix if
2686 appropriate. */
2687 if (row == NULL)
2688 {
2689 if (base_face_id == MODE_LINE_FACE_ID
2690 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2691 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2692 else if (base_face_id == HEADER_LINE_FACE_ID)
2693 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2694 }
2695
2696 /* Clear IT. */
2697 memset (it, 0, sizeof *it);
2698 it->current.overlay_string_index = -1;
2699 it->current.dpvec_index = -1;
2700 it->base_face_id = remapped_base_face_id;
2701 it->string = Qnil;
2702 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2703 it->paragraph_embedding = L2R;
2704 it->bidi_it.string.lstring = Qnil;
2705 it->bidi_it.string.s = NULL;
2706 it->bidi_it.string.bufpos = 0;
2707 it->bidi_it.w = w;
2708
2709 /* The window in which we iterate over current_buffer: */
2710 XSETWINDOW (it->window, w);
2711 it->w = w;
2712 it->f = XFRAME (w->frame);
2713
2714 it->cmp_it.id = -1;
2715
2716 /* Extra space between lines (on window systems only). */
2717 if (base_face_id == DEFAULT_FACE_ID
2718 && FRAME_WINDOW_P (it->f))
2719 {
2720 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2721 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2722 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2723 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2724 * FRAME_LINE_HEIGHT (it->f));
2725 else if (it->f->extra_line_spacing > 0)
2726 it->extra_line_spacing = it->f->extra_line_spacing;
2727 it->max_extra_line_spacing = 0;
2728 }
2729
2730 /* If realized faces have been removed, e.g. because of face
2731 attribute changes of named faces, recompute them. When running
2732 in batch mode, the face cache of the initial frame is null. If
2733 we happen to get called, make a dummy face cache. */
2734 if (FRAME_FACE_CACHE (it->f) == NULL)
2735 init_frame_faces (it->f);
2736 if (FRAME_FACE_CACHE (it->f)->used == 0)
2737 recompute_basic_faces (it->f);
2738
2739 /* Current value of the `slice', `space-width', and 'height' properties. */
2740 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2741 it->space_width = Qnil;
2742 it->font_height = Qnil;
2743 it->override_ascent = -1;
2744
2745 /* Are control characters displayed as `^C'? */
2746 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2747
2748 /* -1 means everything between a CR and the following line end
2749 is invisible. >0 means lines indented more than this value are
2750 invisible. */
2751 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2752 ? (clip_to_bounds
2753 (-1, XINT (BVAR (current_buffer, selective_display)),
2754 PTRDIFF_MAX))
2755 : (!NILP (BVAR (current_buffer, selective_display))
2756 ? -1 : 0));
2757 it->selective_display_ellipsis_p
2758 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2759
2760 /* Display table to use. */
2761 it->dp = window_display_table (w);
2762
2763 /* Are multibyte characters enabled in current_buffer? */
2764 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2765
2766 /* If visible region is of non-zero length, set IT->region_beg_charpos
2767 and IT->region_end_charpos to the start and end of a visible region
2768 in window IT->w. Set both to -1 to indicate no region. */
2769 markpos = markpos_of_region ();
2770 if (markpos >= 0
2771 /* Maybe highlight only in selected window. */
2772 && (/* Either show region everywhere. */
2773 highlight_nonselected_windows
2774 /* Or show region in the selected window. */
2775 || w == XWINDOW (selected_window)
2776 /* Or show the region if we are in the mini-buffer and W is
2777 the window the mini-buffer refers to. */
2778 || (MINI_WINDOW_P (XWINDOW (selected_window))
2779 && WINDOWP (minibuf_selected_window)
2780 && w == XWINDOW (minibuf_selected_window))))
2781 {
2782 it->region_beg_charpos = min (PT, markpos);
2783 it->region_end_charpos = max (PT, markpos);
2784 }
2785 else
2786 it->region_beg_charpos = it->region_end_charpos = -1;
2787
2788 /* Get the position at which the redisplay_end_trigger hook should
2789 be run, if it is to be run at all. */
2790 if (MARKERP (w->redisplay_end_trigger)
2791 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2792 it->redisplay_end_trigger_charpos
2793 = marker_position (w->redisplay_end_trigger);
2794 else if (INTEGERP (w->redisplay_end_trigger))
2795 it->redisplay_end_trigger_charpos =
2796 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2797
2798 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2799
2800 /* Are lines in the display truncated? */
2801 if (base_face_id != DEFAULT_FACE_ID
2802 || it->w->hscroll
2803 || (! WINDOW_FULL_WIDTH_P (it->w)
2804 && ((!NILP (Vtruncate_partial_width_windows)
2805 && !INTEGERP (Vtruncate_partial_width_windows))
2806 || (INTEGERP (Vtruncate_partial_width_windows)
2807 && (WINDOW_TOTAL_COLS (it->w)
2808 < XINT (Vtruncate_partial_width_windows))))))
2809 it->line_wrap = TRUNCATE;
2810 else if (NILP (BVAR (current_buffer, truncate_lines)))
2811 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2812 ? WINDOW_WRAP : WORD_WRAP;
2813 else
2814 it->line_wrap = TRUNCATE;
2815
2816 /* Get dimensions of truncation and continuation glyphs. These are
2817 displayed as fringe bitmaps under X, but we need them for such
2818 frames when the fringes are turned off. But leave the dimensions
2819 zero for tooltip frames, as these glyphs look ugly there and also
2820 sabotage calculations of tooltip dimensions in x-show-tip. */
2821 #ifdef HAVE_WINDOW_SYSTEM
2822 if (!(FRAME_WINDOW_P (it->f)
2823 && FRAMEP (tip_frame)
2824 && it->f == XFRAME (tip_frame)))
2825 #endif
2826 {
2827 if (it->line_wrap == TRUNCATE)
2828 {
2829 /* We will need the truncation glyph. */
2830 eassert (it->glyph_row == NULL);
2831 produce_special_glyphs (it, IT_TRUNCATION);
2832 it->truncation_pixel_width = it->pixel_width;
2833 }
2834 else
2835 {
2836 /* We will need the continuation glyph. */
2837 eassert (it->glyph_row == NULL);
2838 produce_special_glyphs (it, IT_CONTINUATION);
2839 it->continuation_pixel_width = it->pixel_width;
2840 }
2841 }
2842
2843 /* Reset these values to zero because the produce_special_glyphs
2844 above has changed them. */
2845 it->pixel_width = it->ascent = it->descent = 0;
2846 it->phys_ascent = it->phys_descent = 0;
2847
2848 /* Set this after getting the dimensions of truncation and
2849 continuation glyphs, so that we don't produce glyphs when calling
2850 produce_special_glyphs, above. */
2851 it->glyph_row = row;
2852 it->area = TEXT_AREA;
2853
2854 /* Forget any previous info about this row being reversed. */
2855 if (it->glyph_row)
2856 it->glyph_row->reversed_p = 0;
2857
2858 /* Get the dimensions of the display area. The display area
2859 consists of the visible window area plus a horizontally scrolled
2860 part to the left of the window. All x-values are relative to the
2861 start of this total display area. */
2862 if (base_face_id != DEFAULT_FACE_ID)
2863 {
2864 /* Mode lines, menu bar in terminal frames. */
2865 it->first_visible_x = 0;
2866 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2867 }
2868 else
2869 {
2870 it->first_visible_x =
2871 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2872 it->last_visible_x = (it->first_visible_x
2873 + window_box_width (w, TEXT_AREA));
2874
2875 /* If we truncate lines, leave room for the truncation glyph(s) at
2876 the right margin. Otherwise, leave room for the continuation
2877 glyph(s). Done only if the window has no fringes. Since we
2878 don't know at this point whether there will be any R2L lines in
2879 the window, we reserve space for truncation/continuation glyphs
2880 even if only one of the fringes is absent. */
2881 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2882 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2883 {
2884 if (it->line_wrap == TRUNCATE)
2885 it->last_visible_x -= it->truncation_pixel_width;
2886 else
2887 it->last_visible_x -= it->continuation_pixel_width;
2888 }
2889
2890 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2891 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2892 }
2893
2894 /* Leave room for a border glyph. */
2895 if (!FRAME_WINDOW_P (it->f)
2896 && !WINDOW_RIGHTMOST_P (it->w))
2897 it->last_visible_x -= 1;
2898
2899 it->last_visible_y = window_text_bottom_y (w);
2900
2901 /* For mode lines and alike, arrange for the first glyph having a
2902 left box line if the face specifies a box. */
2903 if (base_face_id != DEFAULT_FACE_ID)
2904 {
2905 struct face *face;
2906
2907 it->face_id = remapped_base_face_id;
2908
2909 /* If we have a boxed mode line, make the first character appear
2910 with a left box line. */
2911 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2912 if (face->box != FACE_NO_BOX)
2913 it->start_of_box_run_p = 1;
2914 }
2915
2916 /* If a buffer position was specified, set the iterator there,
2917 getting overlays and face properties from that position. */
2918 if (charpos >= BUF_BEG (current_buffer))
2919 {
2920 it->end_charpos = ZV;
2921 eassert (charpos == BYTE_TO_CHAR (bytepos));
2922 IT_CHARPOS (*it) = charpos;
2923 IT_BYTEPOS (*it) = bytepos;
2924
2925 /* We will rely on `reseat' to set this up properly, via
2926 handle_face_prop. */
2927 it->face_id = it->base_face_id;
2928
2929 it->start = it->current;
2930 /* Do we need to reorder bidirectional text? Not if this is a
2931 unibyte buffer: by definition, none of the single-byte
2932 characters are strong R2L, so no reordering is needed. And
2933 bidi.c doesn't support unibyte buffers anyway. Also, don't
2934 reorder while we are loading loadup.el, since the tables of
2935 character properties needed for reordering are not yet
2936 available. */
2937 it->bidi_p =
2938 NILP (Vpurify_flag)
2939 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2940 && it->multibyte_p;
2941
2942 /* If we are to reorder bidirectional text, init the bidi
2943 iterator. */
2944 if (it->bidi_p)
2945 {
2946 /* Note the paragraph direction that this buffer wants to
2947 use. */
2948 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2949 Qleft_to_right))
2950 it->paragraph_embedding = L2R;
2951 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2952 Qright_to_left))
2953 it->paragraph_embedding = R2L;
2954 else
2955 it->paragraph_embedding = NEUTRAL_DIR;
2956 bidi_unshelve_cache (NULL, 0);
2957 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2958 &it->bidi_it);
2959 }
2960
2961 /* Compute faces etc. */
2962 reseat (it, it->current.pos, 1);
2963 }
2964
2965 CHECK_IT (it);
2966 }
2967
2968
2969 /* Initialize IT for the display of window W with window start POS. */
2970
2971 void
2972 start_display (struct it *it, struct window *w, struct text_pos pos)
2973 {
2974 struct glyph_row *row;
2975 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2976
2977 row = w->desired_matrix->rows + first_vpos;
2978 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2979 it->first_vpos = first_vpos;
2980
2981 /* Don't reseat to previous visible line start if current start
2982 position is in a string or image. */
2983 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2984 {
2985 int start_at_line_beg_p;
2986 int first_y = it->current_y;
2987
2988 /* If window start is not at a line start, skip forward to POS to
2989 get the correct continuation lines width. */
2990 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2991 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2992 if (!start_at_line_beg_p)
2993 {
2994 int new_x;
2995
2996 reseat_at_previous_visible_line_start (it);
2997 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2998
2999 new_x = it->current_x + it->pixel_width;
3000
3001 /* If lines are continued, this line may end in the middle
3002 of a multi-glyph character (e.g. a control character
3003 displayed as \003, or in the middle of an overlay
3004 string). In this case move_it_to above will not have
3005 taken us to the start of the continuation line but to the
3006 end of the continued line. */
3007 if (it->current_x > 0
3008 && it->line_wrap != TRUNCATE /* Lines are continued. */
3009 && (/* And glyph doesn't fit on the line. */
3010 new_x > it->last_visible_x
3011 /* Or it fits exactly and we're on a window
3012 system frame. */
3013 || (new_x == it->last_visible_x
3014 && FRAME_WINDOW_P (it->f)
3015 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3016 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3017 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3018 {
3019 if ((it->current.dpvec_index >= 0
3020 || it->current.overlay_string_index >= 0)
3021 /* If we are on a newline from a display vector or
3022 overlay string, then we are already at the end of
3023 a screen line; no need to go to the next line in
3024 that case, as this line is not really continued.
3025 (If we do go to the next line, C-e will not DTRT.) */
3026 && it->c != '\n')
3027 {
3028 set_iterator_to_next (it, 1);
3029 move_it_in_display_line_to (it, -1, -1, 0);
3030 }
3031
3032 it->continuation_lines_width += it->current_x;
3033 }
3034 /* If the character at POS is displayed via a display
3035 vector, move_it_to above stops at the final glyph of
3036 IT->dpvec. To make the caller redisplay that character
3037 again (a.k.a. start at POS), we need to reset the
3038 dpvec_index to the beginning of IT->dpvec. */
3039 else if (it->current.dpvec_index >= 0)
3040 it->current.dpvec_index = 0;
3041
3042 /* We're starting a new display line, not affected by the
3043 height of the continued line, so clear the appropriate
3044 fields in the iterator structure. */
3045 it->max_ascent = it->max_descent = 0;
3046 it->max_phys_ascent = it->max_phys_descent = 0;
3047
3048 it->current_y = first_y;
3049 it->vpos = 0;
3050 it->current_x = it->hpos = 0;
3051 }
3052 }
3053 }
3054
3055
3056 /* Return 1 if POS is a position in ellipses displayed for invisible
3057 text. W is the window we display, for text property lookup. */
3058
3059 static int
3060 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3061 {
3062 Lisp_Object prop, window;
3063 int ellipses_p = 0;
3064 ptrdiff_t charpos = CHARPOS (pos->pos);
3065
3066 /* If POS specifies a position in a display vector, this might
3067 be for an ellipsis displayed for invisible text. We won't
3068 get the iterator set up for delivering that ellipsis unless
3069 we make sure that it gets aware of the invisible text. */
3070 if (pos->dpvec_index >= 0
3071 && pos->overlay_string_index < 0
3072 && CHARPOS (pos->string_pos) < 0
3073 && charpos > BEGV
3074 && (XSETWINDOW (window, w),
3075 prop = Fget_char_property (make_number (charpos),
3076 Qinvisible, window),
3077 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3078 {
3079 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3080 window);
3081 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3082 }
3083
3084 return ellipses_p;
3085 }
3086
3087
3088 /* Initialize IT for stepping through current_buffer in window W,
3089 starting at position POS that includes overlay string and display
3090 vector/ control character translation position information. Value
3091 is zero if there are overlay strings with newlines at POS. */
3092
3093 static int
3094 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3095 {
3096 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3097 int i, overlay_strings_with_newlines = 0;
3098
3099 /* If POS specifies a position in a display vector, this might
3100 be for an ellipsis displayed for invisible text. We won't
3101 get the iterator set up for delivering that ellipsis unless
3102 we make sure that it gets aware of the invisible text. */
3103 if (in_ellipses_for_invisible_text_p (pos, w))
3104 {
3105 --charpos;
3106 bytepos = 0;
3107 }
3108
3109 /* Keep in mind: the call to reseat in init_iterator skips invisible
3110 text, so we might end up at a position different from POS. This
3111 is only a problem when POS is a row start after a newline and an
3112 overlay starts there with an after-string, and the overlay has an
3113 invisible property. Since we don't skip invisible text in
3114 display_line and elsewhere immediately after consuming the
3115 newline before the row start, such a POS will not be in a string,
3116 but the call to init_iterator below will move us to the
3117 after-string. */
3118 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3119
3120 /* This only scans the current chunk -- it should scan all chunks.
3121 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3122 to 16 in 22.1 to make this a lesser problem. */
3123 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3124 {
3125 const char *s = SSDATA (it->overlay_strings[i]);
3126 const char *e = s + SBYTES (it->overlay_strings[i]);
3127
3128 while (s < e && *s != '\n')
3129 ++s;
3130
3131 if (s < e)
3132 {
3133 overlay_strings_with_newlines = 1;
3134 break;
3135 }
3136 }
3137
3138 /* If position is within an overlay string, set up IT to the right
3139 overlay string. */
3140 if (pos->overlay_string_index >= 0)
3141 {
3142 int relative_index;
3143
3144 /* If the first overlay string happens to have a `display'
3145 property for an image, the iterator will be set up for that
3146 image, and we have to undo that setup first before we can
3147 correct the overlay string index. */
3148 if (it->method == GET_FROM_IMAGE)
3149 pop_it (it);
3150
3151 /* We already have the first chunk of overlay strings in
3152 IT->overlay_strings. Load more until the one for
3153 pos->overlay_string_index is in IT->overlay_strings. */
3154 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3155 {
3156 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3157 it->current.overlay_string_index = 0;
3158 while (n--)
3159 {
3160 load_overlay_strings (it, 0);
3161 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3162 }
3163 }
3164
3165 it->current.overlay_string_index = pos->overlay_string_index;
3166 relative_index = (it->current.overlay_string_index
3167 % OVERLAY_STRING_CHUNK_SIZE);
3168 it->string = it->overlay_strings[relative_index];
3169 eassert (STRINGP (it->string));
3170 it->current.string_pos = pos->string_pos;
3171 it->method = GET_FROM_STRING;
3172 it->end_charpos = SCHARS (it->string);
3173 /* Set up the bidi iterator for this overlay string. */
3174 if (it->bidi_p)
3175 {
3176 it->bidi_it.string.lstring = it->string;
3177 it->bidi_it.string.s = NULL;
3178 it->bidi_it.string.schars = SCHARS (it->string);
3179 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3180 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3181 it->bidi_it.string.unibyte = !it->multibyte_p;
3182 it->bidi_it.w = it->w;
3183 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3184 FRAME_WINDOW_P (it->f), &it->bidi_it);
3185
3186 /* Synchronize the state of the bidi iterator with
3187 pos->string_pos. For any string position other than
3188 zero, this will be done automagically when we resume
3189 iteration over the string and get_visually_first_element
3190 is called. But if string_pos is zero, and the string is
3191 to be reordered for display, we need to resync manually,
3192 since it could be that the iteration state recorded in
3193 pos ended at string_pos of 0 moving backwards in string. */
3194 if (CHARPOS (pos->string_pos) == 0)
3195 {
3196 get_visually_first_element (it);
3197 if (IT_STRING_CHARPOS (*it) != 0)
3198 do {
3199 /* Paranoia. */
3200 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3201 bidi_move_to_visually_next (&it->bidi_it);
3202 } while (it->bidi_it.charpos != 0);
3203 }
3204 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3205 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3206 }
3207 }
3208
3209 if (CHARPOS (pos->string_pos) >= 0)
3210 {
3211 /* Recorded position is not in an overlay string, but in another
3212 string. This can only be a string from a `display' property.
3213 IT should already be filled with that string. */
3214 it->current.string_pos = pos->string_pos;
3215 eassert (STRINGP (it->string));
3216 if (it->bidi_p)
3217 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3218 FRAME_WINDOW_P (it->f), &it->bidi_it);
3219 }
3220
3221 /* Restore position in display vector translations, control
3222 character translations or ellipses. */
3223 if (pos->dpvec_index >= 0)
3224 {
3225 if (it->dpvec == NULL)
3226 get_next_display_element (it);
3227 eassert (it->dpvec && it->current.dpvec_index == 0);
3228 it->current.dpvec_index = pos->dpvec_index;
3229 }
3230
3231 CHECK_IT (it);
3232 return !overlay_strings_with_newlines;
3233 }
3234
3235
3236 /* Initialize IT for stepping through current_buffer in window W
3237 starting at ROW->start. */
3238
3239 static void
3240 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3241 {
3242 init_from_display_pos (it, w, &row->start);
3243 it->start = row->start;
3244 it->continuation_lines_width = row->continuation_lines_width;
3245 CHECK_IT (it);
3246 }
3247
3248
3249 /* Initialize IT for stepping through current_buffer in window W
3250 starting in the line following ROW, i.e. starting at ROW->end.
3251 Value is zero if there are overlay strings with newlines at ROW's
3252 end position. */
3253
3254 static int
3255 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3256 {
3257 int success = 0;
3258
3259 if (init_from_display_pos (it, w, &row->end))
3260 {
3261 if (row->continued_p)
3262 it->continuation_lines_width
3263 = row->continuation_lines_width + row->pixel_width;
3264 CHECK_IT (it);
3265 success = 1;
3266 }
3267
3268 return success;
3269 }
3270
3271
3272
3273 \f
3274 /***********************************************************************
3275 Text properties
3276 ***********************************************************************/
3277
3278 /* Called when IT reaches IT->stop_charpos. Handle text property and
3279 overlay changes. Set IT->stop_charpos to the next position where
3280 to stop. */
3281
3282 static void
3283 handle_stop (struct it *it)
3284 {
3285 enum prop_handled handled;
3286 int handle_overlay_change_p;
3287 struct props *p;
3288
3289 it->dpvec = NULL;
3290 it->current.dpvec_index = -1;
3291 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3292 it->ignore_overlay_strings_at_pos_p = 0;
3293 it->ellipsis_p = 0;
3294
3295 /* Use face of preceding text for ellipsis (if invisible) */
3296 if (it->selective_display_ellipsis_p)
3297 it->saved_face_id = it->face_id;
3298
3299 do
3300 {
3301 handled = HANDLED_NORMALLY;
3302
3303 /* Call text property handlers. */
3304 for (p = it_props; p->handler; ++p)
3305 {
3306 handled = p->handler (it);
3307
3308 if (handled == HANDLED_RECOMPUTE_PROPS)
3309 break;
3310 else if (handled == HANDLED_RETURN)
3311 {
3312 /* We still want to show before and after strings from
3313 overlays even if the actual buffer text is replaced. */
3314 if (!handle_overlay_change_p
3315 || it->sp > 1
3316 /* Don't call get_overlay_strings_1 if we already
3317 have overlay strings loaded, because doing so
3318 will load them again and push the iterator state
3319 onto the stack one more time, which is not
3320 expected by the rest of the code that processes
3321 overlay strings. */
3322 || (it->current.overlay_string_index < 0
3323 ? !get_overlay_strings_1 (it, 0, 0)
3324 : 0))
3325 {
3326 if (it->ellipsis_p)
3327 setup_for_ellipsis (it, 0);
3328 /* When handling a display spec, we might load an
3329 empty string. In that case, discard it here. We
3330 used to discard it in handle_single_display_spec,
3331 but that causes get_overlay_strings_1, above, to
3332 ignore overlay strings that we must check. */
3333 if (STRINGP (it->string) && !SCHARS (it->string))
3334 pop_it (it);
3335 return;
3336 }
3337 else if (STRINGP (it->string) && !SCHARS (it->string))
3338 pop_it (it);
3339 else
3340 {
3341 it->ignore_overlay_strings_at_pos_p = 1;
3342 it->string_from_display_prop_p = 0;
3343 it->from_disp_prop_p = 0;
3344 handle_overlay_change_p = 0;
3345 }
3346 handled = HANDLED_RECOMPUTE_PROPS;
3347 break;
3348 }
3349 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3350 handle_overlay_change_p = 0;
3351 }
3352
3353 if (handled != HANDLED_RECOMPUTE_PROPS)
3354 {
3355 /* Don't check for overlay strings below when set to deliver
3356 characters from a display vector. */
3357 if (it->method == GET_FROM_DISPLAY_VECTOR)
3358 handle_overlay_change_p = 0;
3359
3360 /* Handle overlay changes.
3361 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3362 if it finds overlays. */
3363 if (handle_overlay_change_p)
3364 handled = handle_overlay_change (it);
3365 }
3366
3367 if (it->ellipsis_p)
3368 {
3369 setup_for_ellipsis (it, 0);
3370 break;
3371 }
3372 }
3373 while (handled == HANDLED_RECOMPUTE_PROPS);
3374
3375 /* Determine where to stop next. */
3376 if (handled == HANDLED_NORMALLY)
3377 compute_stop_pos (it);
3378 }
3379
3380
3381 /* Compute IT->stop_charpos from text property and overlay change
3382 information for IT's current position. */
3383
3384 static void
3385 compute_stop_pos (struct it *it)
3386 {
3387 register INTERVAL iv, next_iv;
3388 Lisp_Object object, limit, position;
3389 ptrdiff_t charpos, bytepos;
3390
3391 if (STRINGP (it->string))
3392 {
3393 /* Strings are usually short, so don't limit the search for
3394 properties. */
3395 it->stop_charpos = it->end_charpos;
3396 object = it->string;
3397 limit = Qnil;
3398 charpos = IT_STRING_CHARPOS (*it);
3399 bytepos = IT_STRING_BYTEPOS (*it);
3400 }
3401 else
3402 {
3403 ptrdiff_t pos;
3404
3405 /* If end_charpos is out of range for some reason, such as a
3406 misbehaving display function, rationalize it (Bug#5984). */
3407 if (it->end_charpos > ZV)
3408 it->end_charpos = ZV;
3409 it->stop_charpos = it->end_charpos;
3410
3411 /* If next overlay change is in front of the current stop pos
3412 (which is IT->end_charpos), stop there. Note: value of
3413 next_overlay_change is point-max if no overlay change
3414 follows. */
3415 charpos = IT_CHARPOS (*it);
3416 bytepos = IT_BYTEPOS (*it);
3417 pos = next_overlay_change (charpos);
3418 if (pos < it->stop_charpos)
3419 it->stop_charpos = pos;
3420
3421 /* If showing the region, we have to stop at the region
3422 start or end because the face might change there. */
3423 if (it->region_beg_charpos > 0)
3424 {
3425 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3426 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3427 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3428 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3429 }
3430
3431 /* Set up variables for computing the stop position from text
3432 property changes. */
3433 XSETBUFFER (object, current_buffer);
3434 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3435 }
3436
3437 /* Get the interval containing IT's position. Value is a null
3438 interval if there isn't such an interval. */
3439 position = make_number (charpos);
3440 iv = validate_interval_range (object, &position, &position, 0);
3441 if (iv)
3442 {
3443 Lisp_Object values_here[LAST_PROP_IDX];
3444 struct props *p;
3445
3446 /* Get properties here. */
3447 for (p = it_props; p->handler; ++p)
3448 values_here[p->idx] = textget (iv->plist, *p->name);
3449
3450 /* Look for an interval following iv that has different
3451 properties. */
3452 for (next_iv = next_interval (iv);
3453 (next_iv
3454 && (NILP (limit)
3455 || XFASTINT (limit) > next_iv->position));
3456 next_iv = next_interval (next_iv))
3457 {
3458 for (p = it_props; p->handler; ++p)
3459 {
3460 Lisp_Object new_value;
3461
3462 new_value = textget (next_iv->plist, *p->name);
3463 if (!EQ (values_here[p->idx], new_value))
3464 break;
3465 }
3466
3467 if (p->handler)
3468 break;
3469 }
3470
3471 if (next_iv)
3472 {
3473 if (INTEGERP (limit)
3474 && next_iv->position >= XFASTINT (limit))
3475 /* No text property change up to limit. */
3476 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3477 else
3478 /* Text properties change in next_iv. */
3479 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3480 }
3481 }
3482
3483 if (it->cmp_it.id < 0)
3484 {
3485 ptrdiff_t stoppos = it->end_charpos;
3486
3487 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3488 stoppos = -1;
3489 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3490 stoppos, it->string);
3491 }
3492
3493 eassert (STRINGP (it->string)
3494 || (it->stop_charpos >= BEGV
3495 && it->stop_charpos >= IT_CHARPOS (*it)));
3496 }
3497
3498
3499 /* Return the position of the next overlay change after POS in
3500 current_buffer. Value is point-max if no overlay change
3501 follows. This is like `next-overlay-change' but doesn't use
3502 xmalloc. */
3503
3504 static ptrdiff_t
3505 next_overlay_change (ptrdiff_t pos)
3506 {
3507 ptrdiff_t i, noverlays;
3508 ptrdiff_t endpos;
3509 Lisp_Object *overlays;
3510
3511 /* Get all overlays at the given position. */
3512 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3513
3514 /* If any of these overlays ends before endpos,
3515 use its ending point instead. */
3516 for (i = 0; i < noverlays; ++i)
3517 {
3518 Lisp_Object oend;
3519 ptrdiff_t oendpos;
3520
3521 oend = OVERLAY_END (overlays[i]);
3522 oendpos = OVERLAY_POSITION (oend);
3523 endpos = min (endpos, oendpos);
3524 }
3525
3526 return endpos;
3527 }
3528
3529 /* How many characters forward to search for a display property or
3530 display string. Searching too far forward makes the bidi display
3531 sluggish, especially in small windows. */
3532 #define MAX_DISP_SCAN 250
3533
3534 /* Return the character position of a display string at or after
3535 position specified by POSITION. If no display string exists at or
3536 after POSITION, return ZV. A display string is either an overlay
3537 with `display' property whose value is a string, or a `display'
3538 text property whose value is a string. STRING is data about the
3539 string to iterate; if STRING->lstring is nil, we are iterating a
3540 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3541 on a GUI frame. DISP_PROP is set to zero if we searched
3542 MAX_DISP_SCAN characters forward without finding any display
3543 strings, non-zero otherwise. It is set to 2 if the display string
3544 uses any kind of `(space ...)' spec that will produce a stretch of
3545 white space in the text area. */
3546 ptrdiff_t
3547 compute_display_string_pos (struct text_pos *position,
3548 struct bidi_string_data *string,
3549 struct window *w,
3550 int frame_window_p, int *disp_prop)
3551 {
3552 /* OBJECT = nil means current buffer. */
3553 Lisp_Object object, object1;
3554 Lisp_Object pos, spec, limpos;
3555 int string_p = (string && (STRINGP (string->lstring) || string->s));
3556 ptrdiff_t eob = string_p ? string->schars : ZV;
3557 ptrdiff_t begb = string_p ? 0 : BEGV;
3558 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3559 ptrdiff_t lim =
3560 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3561 struct text_pos tpos;
3562 int rv = 0;
3563
3564 if (string && STRINGP (string->lstring))
3565 object1 = object = string->lstring;
3566 else if (w && !string_p)
3567 {
3568 XSETWINDOW (object, w);
3569 object1 = Qnil;
3570 }
3571 else
3572 object1 = object = Qnil;
3573
3574 *disp_prop = 1;
3575
3576 if (charpos >= eob
3577 /* We don't support display properties whose values are strings
3578 that have display string properties. */
3579 || string->from_disp_str
3580 /* C strings cannot have display properties. */
3581 || (string->s && !STRINGP (object)))
3582 {
3583 *disp_prop = 0;
3584 return eob;
3585 }
3586
3587 /* If the character at CHARPOS is where the display string begins,
3588 return CHARPOS. */
3589 pos = make_number (charpos);
3590 if (STRINGP (object))
3591 bufpos = string->bufpos;
3592 else
3593 bufpos = charpos;
3594 tpos = *position;
3595 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3596 && (charpos <= begb
3597 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3598 object),
3599 spec))
3600 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3601 frame_window_p)))
3602 {
3603 if (rv == 2)
3604 *disp_prop = 2;
3605 return charpos;
3606 }
3607
3608 /* Look forward for the first character with a `display' property
3609 that will replace the underlying text when displayed. */
3610 limpos = make_number (lim);
3611 do {
3612 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3613 CHARPOS (tpos) = XFASTINT (pos);
3614 if (CHARPOS (tpos) >= lim)
3615 {
3616 *disp_prop = 0;
3617 break;
3618 }
3619 if (STRINGP (object))
3620 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3621 else
3622 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3623 spec = Fget_char_property (pos, Qdisplay, object);
3624 if (!STRINGP (object))
3625 bufpos = CHARPOS (tpos);
3626 } while (NILP (spec)
3627 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3628 bufpos, frame_window_p)));
3629 if (rv == 2)
3630 *disp_prop = 2;
3631
3632 return CHARPOS (tpos);
3633 }
3634
3635 /* Return the character position of the end of the display string that
3636 started at CHARPOS. If there's no display string at CHARPOS,
3637 return -1. A display string is either an overlay with `display'
3638 property whose value is a string or a `display' text property whose
3639 value is a string. */
3640 ptrdiff_t
3641 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3642 {
3643 /* OBJECT = nil means current buffer. */
3644 Lisp_Object object =
3645 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3646 Lisp_Object pos = make_number (charpos);
3647 ptrdiff_t eob =
3648 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3649
3650 if (charpos >= eob || (string->s && !STRINGP (object)))
3651 return eob;
3652
3653 /* It could happen that the display property or overlay was removed
3654 since we found it in compute_display_string_pos above. One way
3655 this can happen is if JIT font-lock was called (through
3656 handle_fontified_prop), and jit-lock-functions remove text
3657 properties or overlays from the portion of buffer that includes
3658 CHARPOS. Muse mode is known to do that, for example. In this
3659 case, we return -1 to the caller, to signal that no display
3660 string is actually present at CHARPOS. See bidi_fetch_char for
3661 how this is handled.
3662
3663 An alternative would be to never look for display properties past
3664 it->stop_charpos. But neither compute_display_string_pos nor
3665 bidi_fetch_char that calls it know or care where the next
3666 stop_charpos is. */
3667 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3668 return -1;
3669
3670 /* Look forward for the first character where the `display' property
3671 changes. */
3672 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3673
3674 return XFASTINT (pos);
3675 }
3676
3677
3678 \f
3679 /***********************************************************************
3680 Fontification
3681 ***********************************************************************/
3682
3683 /* Handle changes in the `fontified' property of the current buffer by
3684 calling hook functions from Qfontification_functions to fontify
3685 regions of text. */
3686
3687 static enum prop_handled
3688 handle_fontified_prop (struct it *it)
3689 {
3690 Lisp_Object prop, pos;
3691 enum prop_handled handled = HANDLED_NORMALLY;
3692
3693 if (!NILP (Vmemory_full))
3694 return handled;
3695
3696 /* Get the value of the `fontified' property at IT's current buffer
3697 position. (The `fontified' property doesn't have a special
3698 meaning in strings.) If the value is nil, call functions from
3699 Qfontification_functions. */
3700 if (!STRINGP (it->string)
3701 && it->s == NULL
3702 && !NILP (Vfontification_functions)
3703 && !NILP (Vrun_hooks)
3704 && (pos = make_number (IT_CHARPOS (*it)),
3705 prop = Fget_char_property (pos, Qfontified, Qnil),
3706 /* Ignore the special cased nil value always present at EOB since
3707 no amount of fontifying will be able to change it. */
3708 NILP (prop) && IT_CHARPOS (*it) < Z))
3709 {
3710 ptrdiff_t count = SPECPDL_INDEX ();
3711 Lisp_Object val;
3712 struct buffer *obuf = current_buffer;
3713 int begv = BEGV, zv = ZV;
3714 int old_clip_changed = current_buffer->clip_changed;
3715
3716 val = Vfontification_functions;
3717 specbind (Qfontification_functions, Qnil);
3718
3719 eassert (it->end_charpos == ZV);
3720
3721 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3722 safe_call1 (val, pos);
3723 else
3724 {
3725 Lisp_Object fns, fn;
3726 struct gcpro gcpro1, gcpro2;
3727
3728 fns = Qnil;
3729 GCPRO2 (val, fns);
3730
3731 for (; CONSP (val); val = XCDR (val))
3732 {
3733 fn = XCAR (val);
3734
3735 if (EQ (fn, Qt))
3736 {
3737 /* A value of t indicates this hook has a local
3738 binding; it means to run the global binding too.
3739 In a global value, t should not occur. If it
3740 does, we must ignore it to avoid an endless
3741 loop. */
3742 for (fns = Fdefault_value (Qfontification_functions);
3743 CONSP (fns);
3744 fns = XCDR (fns))
3745 {
3746 fn = XCAR (fns);
3747 if (!EQ (fn, Qt))
3748 safe_call1 (fn, pos);
3749 }
3750 }
3751 else
3752 safe_call1 (fn, pos);
3753 }
3754
3755 UNGCPRO;
3756 }
3757
3758 unbind_to (count, Qnil);
3759
3760 /* Fontification functions routinely call `save-restriction'.
3761 Normally, this tags clip_changed, which can confuse redisplay
3762 (see discussion in Bug#6671). Since we don't perform any
3763 special handling of fontification changes in the case where
3764 `save-restriction' isn't called, there's no point doing so in
3765 this case either. So, if the buffer's restrictions are
3766 actually left unchanged, reset clip_changed. */
3767 if (obuf == current_buffer)
3768 {
3769 if (begv == BEGV && zv == ZV)
3770 current_buffer->clip_changed = old_clip_changed;
3771 }
3772 /* There isn't much we can reasonably do to protect against
3773 misbehaving fontification, but here's a fig leaf. */
3774 else if (BUFFER_LIVE_P (obuf))
3775 set_buffer_internal_1 (obuf);
3776
3777 /* The fontification code may have added/removed text.
3778 It could do even a lot worse, but let's at least protect against
3779 the most obvious case where only the text past `pos' gets changed',
3780 as is/was done in grep.el where some escapes sequences are turned
3781 into face properties (bug#7876). */
3782 it->end_charpos = ZV;
3783
3784 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3785 something. This avoids an endless loop if they failed to
3786 fontify the text for which reason ever. */
3787 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3788 handled = HANDLED_RECOMPUTE_PROPS;
3789 }
3790
3791 return handled;
3792 }
3793
3794
3795 \f
3796 /***********************************************************************
3797 Faces
3798 ***********************************************************************/
3799
3800 /* Set up iterator IT from face properties at its current position.
3801 Called from handle_stop. */
3802
3803 static enum prop_handled
3804 handle_face_prop (struct it *it)
3805 {
3806 int new_face_id;
3807 ptrdiff_t next_stop;
3808
3809 if (!STRINGP (it->string))
3810 {
3811 new_face_id
3812 = face_at_buffer_position (it->w,
3813 IT_CHARPOS (*it),
3814 it->region_beg_charpos,
3815 it->region_end_charpos,
3816 &next_stop,
3817 (IT_CHARPOS (*it)
3818 + TEXT_PROP_DISTANCE_LIMIT),
3819 0, it->base_face_id);
3820
3821 /* Is this a start of a run of characters with box face?
3822 Caveat: this can be called for a freshly initialized
3823 iterator; face_id is -1 in this case. We know that the new
3824 face will not change until limit, i.e. if the new face has a
3825 box, all characters up to limit will have one. But, as
3826 usual, we don't know whether limit is really the end. */
3827 if (new_face_id != it->face_id)
3828 {
3829 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3830 /* If it->face_id is -1, old_face below will be NULL, see
3831 the definition of FACE_FROM_ID. This will happen if this
3832 is the initial call that gets the face. */
3833 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3834
3835 /* If the value of face_id of the iterator is -1, we have to
3836 look in front of IT's position and see whether there is a
3837 face there that's different from new_face_id. */
3838 if (!old_face && IT_CHARPOS (*it) > BEG)
3839 {
3840 int prev_face_id = face_before_it_pos (it);
3841
3842 old_face = FACE_FROM_ID (it->f, prev_face_id);
3843 }
3844
3845 /* If the new face has a box, but the old face does not,
3846 this is the start of a run of characters with box face,
3847 i.e. this character has a shadow on the left side. */
3848 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3849 && (old_face == NULL || !old_face->box));
3850 it->face_box_p = new_face->box != FACE_NO_BOX;
3851 }
3852 }
3853 else
3854 {
3855 int base_face_id;
3856 ptrdiff_t bufpos;
3857 int i;
3858 Lisp_Object from_overlay
3859 = (it->current.overlay_string_index >= 0
3860 ? it->string_overlays[it->current.overlay_string_index
3861 % OVERLAY_STRING_CHUNK_SIZE]
3862 : Qnil);
3863
3864 /* See if we got to this string directly or indirectly from
3865 an overlay property. That includes the before-string or
3866 after-string of an overlay, strings in display properties
3867 provided by an overlay, their text properties, etc.
3868
3869 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3870 if (! NILP (from_overlay))
3871 for (i = it->sp - 1; i >= 0; i--)
3872 {
3873 if (it->stack[i].current.overlay_string_index >= 0)
3874 from_overlay
3875 = it->string_overlays[it->stack[i].current.overlay_string_index
3876 % OVERLAY_STRING_CHUNK_SIZE];
3877 else if (! NILP (it->stack[i].from_overlay))
3878 from_overlay = it->stack[i].from_overlay;
3879
3880 if (!NILP (from_overlay))
3881 break;
3882 }
3883
3884 if (! NILP (from_overlay))
3885 {
3886 bufpos = IT_CHARPOS (*it);
3887 /* For a string from an overlay, the base face depends
3888 only on text properties and ignores overlays. */
3889 base_face_id
3890 = face_for_overlay_string (it->w,
3891 IT_CHARPOS (*it),
3892 it->region_beg_charpos,
3893 it->region_end_charpos,
3894 &next_stop,
3895 (IT_CHARPOS (*it)
3896 + TEXT_PROP_DISTANCE_LIMIT),
3897 0,
3898 from_overlay);
3899 }
3900 else
3901 {
3902 bufpos = 0;
3903
3904 /* For strings from a `display' property, use the face at
3905 IT's current buffer position as the base face to merge
3906 with, so that overlay strings appear in the same face as
3907 surrounding text, unless they specify their own
3908 faces. */
3909 base_face_id = it->string_from_prefix_prop_p
3910 ? DEFAULT_FACE_ID
3911 : underlying_face_id (it);
3912 }
3913
3914 new_face_id = face_at_string_position (it->w,
3915 it->string,
3916 IT_STRING_CHARPOS (*it),
3917 bufpos,
3918 it->region_beg_charpos,
3919 it->region_end_charpos,
3920 &next_stop,
3921 base_face_id, 0);
3922
3923 /* Is this a start of a run of characters with box? Caveat:
3924 this can be called for a freshly allocated iterator; face_id
3925 is -1 is this case. We know that the new face will not
3926 change until the next check pos, i.e. if the new face has a
3927 box, all characters up to that position will have a
3928 box. But, as usual, we don't know whether that position
3929 is really the end. */
3930 if (new_face_id != it->face_id)
3931 {
3932 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3933 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3934
3935 /* If new face has a box but old face hasn't, this is the
3936 start of a run of characters with box, i.e. it has a
3937 shadow on the left side. */
3938 it->start_of_box_run_p
3939 = new_face->box && (old_face == NULL || !old_face->box);
3940 it->face_box_p = new_face->box != FACE_NO_BOX;
3941 }
3942 }
3943
3944 it->face_id = new_face_id;
3945 return HANDLED_NORMALLY;
3946 }
3947
3948
3949 /* Return the ID of the face ``underlying'' IT's current position,
3950 which is in a string. If the iterator is associated with a
3951 buffer, return the face at IT's current buffer position.
3952 Otherwise, use the iterator's base_face_id. */
3953
3954 static int
3955 underlying_face_id (struct it *it)
3956 {
3957 int face_id = it->base_face_id, i;
3958
3959 eassert (STRINGP (it->string));
3960
3961 for (i = it->sp - 1; i >= 0; --i)
3962 if (NILP (it->stack[i].string))
3963 face_id = it->stack[i].face_id;
3964
3965 return face_id;
3966 }
3967
3968
3969 /* Compute the face one character before or after the current position
3970 of IT, in the visual order. BEFORE_P non-zero means get the face
3971 in front (to the left in L2R paragraphs, to the right in R2L
3972 paragraphs) of IT's screen position. Value is the ID of the face. */
3973
3974 static int
3975 face_before_or_after_it_pos (struct it *it, int before_p)
3976 {
3977 int face_id, limit;
3978 ptrdiff_t next_check_charpos;
3979 struct it it_copy;
3980 void *it_copy_data = NULL;
3981
3982 eassert (it->s == NULL);
3983
3984 if (STRINGP (it->string))
3985 {
3986 ptrdiff_t bufpos, charpos;
3987 int base_face_id;
3988
3989 /* No face change past the end of the string (for the case
3990 we are padding with spaces). No face change before the
3991 string start. */
3992 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3993 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3994 return it->face_id;
3995
3996 if (!it->bidi_p)
3997 {
3998 /* Set charpos to the position before or after IT's current
3999 position, in the logical order, which in the non-bidi
4000 case is the same as the visual order. */
4001 if (before_p)
4002 charpos = IT_STRING_CHARPOS (*it) - 1;
4003 else if (it->what == IT_COMPOSITION)
4004 /* For composition, we must check the character after the
4005 composition. */
4006 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4007 else
4008 charpos = IT_STRING_CHARPOS (*it) + 1;
4009 }
4010 else
4011 {
4012 if (before_p)
4013 {
4014 /* With bidi iteration, the character before the current
4015 in the visual order cannot be found by simple
4016 iteration, because "reverse" reordering is not
4017 supported. Instead, we need to use the move_it_*
4018 family of functions. */
4019 /* Ignore face changes before the first visible
4020 character on this display line. */
4021 if (it->current_x <= it->first_visible_x)
4022 return it->face_id;
4023 SAVE_IT (it_copy, *it, it_copy_data);
4024 /* Implementation note: Since move_it_in_display_line
4025 works in the iterator geometry, and thinks the first
4026 character is always the leftmost, even in R2L lines,
4027 we don't need to distinguish between the R2L and L2R
4028 cases here. */
4029 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4030 it_copy.current_x - 1, MOVE_TO_X);
4031 charpos = IT_STRING_CHARPOS (it_copy);
4032 RESTORE_IT (it, it, it_copy_data);
4033 }
4034 else
4035 {
4036 /* Set charpos to the string position of the character
4037 that comes after IT's current position in the visual
4038 order. */
4039 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4040
4041 it_copy = *it;
4042 while (n--)
4043 bidi_move_to_visually_next (&it_copy.bidi_it);
4044
4045 charpos = it_copy.bidi_it.charpos;
4046 }
4047 }
4048 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4049
4050 if (it->current.overlay_string_index >= 0)
4051 bufpos = IT_CHARPOS (*it);
4052 else
4053 bufpos = 0;
4054
4055 base_face_id = underlying_face_id (it);
4056
4057 /* Get the face for ASCII, or unibyte. */
4058 face_id = face_at_string_position (it->w,
4059 it->string,
4060 charpos,
4061 bufpos,
4062 it->region_beg_charpos,
4063 it->region_end_charpos,
4064 &next_check_charpos,
4065 base_face_id, 0);
4066
4067 /* Correct the face for charsets different from ASCII. Do it
4068 for the multibyte case only. The face returned above is
4069 suitable for unibyte text if IT->string is unibyte. */
4070 if (STRING_MULTIBYTE (it->string))
4071 {
4072 struct text_pos pos1 = string_pos (charpos, it->string);
4073 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4074 int c, len;
4075 struct face *face = FACE_FROM_ID (it->f, face_id);
4076
4077 c = string_char_and_length (p, &len);
4078 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4079 }
4080 }
4081 else
4082 {
4083 struct text_pos pos;
4084
4085 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4086 || (IT_CHARPOS (*it) <= BEGV && before_p))
4087 return it->face_id;
4088
4089 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4090 pos = it->current.pos;
4091
4092 if (!it->bidi_p)
4093 {
4094 if (before_p)
4095 DEC_TEXT_POS (pos, it->multibyte_p);
4096 else
4097 {
4098 if (it->what == IT_COMPOSITION)
4099 {
4100 /* For composition, we must check the position after
4101 the composition. */
4102 pos.charpos += it->cmp_it.nchars;
4103 pos.bytepos += it->len;
4104 }
4105 else
4106 INC_TEXT_POS (pos, it->multibyte_p);
4107 }
4108 }
4109 else
4110 {
4111 if (before_p)
4112 {
4113 /* With bidi iteration, the character before the current
4114 in the visual order cannot be found by simple
4115 iteration, because "reverse" reordering is not
4116 supported. Instead, we need to use the move_it_*
4117 family of functions. */
4118 /* Ignore face changes before the first visible
4119 character on this display line. */
4120 if (it->current_x <= it->first_visible_x)
4121 return it->face_id;
4122 SAVE_IT (it_copy, *it, it_copy_data);
4123 /* Implementation note: Since move_it_in_display_line
4124 works in the iterator geometry, and thinks the first
4125 character is always the leftmost, even in R2L lines,
4126 we don't need to distinguish between the R2L and L2R
4127 cases here. */
4128 move_it_in_display_line (&it_copy, ZV,
4129 it_copy.current_x - 1, MOVE_TO_X);
4130 pos = it_copy.current.pos;
4131 RESTORE_IT (it, it, it_copy_data);
4132 }
4133 else
4134 {
4135 /* Set charpos to the buffer position of the character
4136 that comes after IT's current position in the visual
4137 order. */
4138 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4139
4140 it_copy = *it;
4141 while (n--)
4142 bidi_move_to_visually_next (&it_copy.bidi_it);
4143
4144 SET_TEXT_POS (pos,
4145 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4146 }
4147 }
4148 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4149
4150 /* Determine face for CHARSET_ASCII, or unibyte. */
4151 face_id = face_at_buffer_position (it->w,
4152 CHARPOS (pos),
4153 it->region_beg_charpos,
4154 it->region_end_charpos,
4155 &next_check_charpos,
4156 limit, 0, -1);
4157
4158 /* Correct the face for charsets different from ASCII. Do it
4159 for the multibyte case only. The face returned above is
4160 suitable for unibyte text if current_buffer is unibyte. */
4161 if (it->multibyte_p)
4162 {
4163 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4164 struct face *face = FACE_FROM_ID (it->f, face_id);
4165 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4166 }
4167 }
4168
4169 return face_id;
4170 }
4171
4172
4173 \f
4174 /***********************************************************************
4175 Invisible text
4176 ***********************************************************************/
4177
4178 /* Set up iterator IT from invisible properties at its current
4179 position. Called from handle_stop. */
4180
4181 static enum prop_handled
4182 handle_invisible_prop (struct it *it)
4183 {
4184 enum prop_handled handled = HANDLED_NORMALLY;
4185 int invis_p;
4186 Lisp_Object prop;
4187
4188 if (STRINGP (it->string))
4189 {
4190 Lisp_Object end_charpos, limit, charpos;
4191
4192 /* Get the value of the invisible text property at the
4193 current position. Value will be nil if there is no such
4194 property. */
4195 charpos = make_number (IT_STRING_CHARPOS (*it));
4196 prop = Fget_text_property (charpos, Qinvisible, it->string);
4197 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4198
4199 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4200 {
4201 /* Record whether we have to display an ellipsis for the
4202 invisible text. */
4203 int display_ellipsis_p = (invis_p == 2);
4204 ptrdiff_t len, endpos;
4205
4206 handled = HANDLED_RECOMPUTE_PROPS;
4207
4208 /* Get the position at which the next visible text can be
4209 found in IT->string, if any. */
4210 endpos = len = SCHARS (it->string);
4211 XSETINT (limit, len);
4212 do
4213 {
4214 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4215 it->string, limit);
4216 if (INTEGERP (end_charpos))
4217 {
4218 endpos = XFASTINT (end_charpos);
4219 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4220 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4221 if (invis_p == 2)
4222 display_ellipsis_p = 1;
4223 }
4224 }
4225 while (invis_p && endpos < len);
4226
4227 if (display_ellipsis_p)
4228 it->ellipsis_p = 1;
4229
4230 if (endpos < len)
4231 {
4232 /* Text at END_CHARPOS is visible. Move IT there. */
4233 struct text_pos old;
4234 ptrdiff_t oldpos;
4235
4236 old = it->current.string_pos;
4237 oldpos = CHARPOS (old);
4238 if (it->bidi_p)
4239 {
4240 if (it->bidi_it.first_elt
4241 && it->bidi_it.charpos < SCHARS (it->string))
4242 bidi_paragraph_init (it->paragraph_embedding,
4243 &it->bidi_it, 1);
4244 /* Bidi-iterate out of the invisible text. */
4245 do
4246 {
4247 bidi_move_to_visually_next (&it->bidi_it);
4248 }
4249 while (oldpos <= it->bidi_it.charpos
4250 && it->bidi_it.charpos < endpos);
4251
4252 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4253 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4254 if (IT_CHARPOS (*it) >= endpos)
4255 it->prev_stop = endpos;
4256 }
4257 else
4258 {
4259 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4260 compute_string_pos (&it->current.string_pos, old, it->string);
4261 }
4262 }
4263 else
4264 {
4265 /* The rest of the string is invisible. If this is an
4266 overlay string, proceed with the next overlay string
4267 or whatever comes and return a character from there. */
4268 if (it->current.overlay_string_index >= 0
4269 && !display_ellipsis_p)
4270 {
4271 next_overlay_string (it);
4272 /* Don't check for overlay strings when we just
4273 finished processing them. */
4274 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4275 }
4276 else
4277 {
4278 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4279 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4280 }
4281 }
4282 }
4283 }
4284 else
4285 {
4286 ptrdiff_t newpos, next_stop, start_charpos, tem;
4287 Lisp_Object pos, overlay;
4288
4289 /* First of all, is there invisible text at this position? */
4290 tem = start_charpos = IT_CHARPOS (*it);
4291 pos = make_number (tem);
4292 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4293 &overlay);
4294 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4295
4296 /* If we are on invisible text, skip over it. */
4297 if (invis_p && start_charpos < it->end_charpos)
4298 {
4299 /* Record whether we have to display an ellipsis for the
4300 invisible text. */
4301 int display_ellipsis_p = invis_p == 2;
4302
4303 handled = HANDLED_RECOMPUTE_PROPS;
4304
4305 /* Loop skipping over invisible text. The loop is left at
4306 ZV or with IT on the first char being visible again. */
4307 do
4308 {
4309 /* Try to skip some invisible text. Return value is the
4310 position reached which can be equal to where we start
4311 if there is nothing invisible there. This skips both
4312 over invisible text properties and overlays with
4313 invisible property. */
4314 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4315
4316 /* If we skipped nothing at all we weren't at invisible
4317 text in the first place. If everything to the end of
4318 the buffer was skipped, end the loop. */
4319 if (newpos == tem || newpos >= ZV)
4320 invis_p = 0;
4321 else
4322 {
4323 /* We skipped some characters but not necessarily
4324 all there are. Check if we ended up on visible
4325 text. Fget_char_property returns the property of
4326 the char before the given position, i.e. if we
4327 get invis_p = 0, this means that the char at
4328 newpos is visible. */
4329 pos = make_number (newpos);
4330 prop = Fget_char_property (pos, Qinvisible, it->window);
4331 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4332 }
4333
4334 /* If we ended up on invisible text, proceed to
4335 skip starting with next_stop. */
4336 if (invis_p)
4337 tem = next_stop;
4338
4339 /* If there are adjacent invisible texts, don't lose the
4340 second one's ellipsis. */
4341 if (invis_p == 2)
4342 display_ellipsis_p = 1;
4343 }
4344 while (invis_p);
4345
4346 /* The position newpos is now either ZV or on visible text. */
4347 if (it->bidi_p)
4348 {
4349 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4350 int on_newline =
4351 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4352 int after_newline =
4353 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4354
4355 /* If the invisible text ends on a newline or on a
4356 character after a newline, we can avoid the costly,
4357 character by character, bidi iteration to NEWPOS, and
4358 instead simply reseat the iterator there. That's
4359 because all bidi reordering information is tossed at
4360 the newline. This is a big win for modes that hide
4361 complete lines, like Outline, Org, etc. */
4362 if (on_newline || after_newline)
4363 {
4364 struct text_pos tpos;
4365 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4366
4367 SET_TEXT_POS (tpos, newpos, bpos);
4368 reseat_1 (it, tpos, 0);
4369 /* If we reseat on a newline/ZV, we need to prep the
4370 bidi iterator for advancing to the next character
4371 after the newline/EOB, keeping the current paragraph
4372 direction (so that PRODUCE_GLYPHS does TRT wrt
4373 prepending/appending glyphs to a glyph row). */
4374 if (on_newline)
4375 {
4376 it->bidi_it.first_elt = 0;
4377 it->bidi_it.paragraph_dir = pdir;
4378 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4379 it->bidi_it.nchars = 1;
4380 it->bidi_it.ch_len = 1;
4381 }
4382 }
4383 else /* Must use the slow method. */
4384 {
4385 /* With bidi iteration, the region of invisible text
4386 could start and/or end in the middle of a
4387 non-base embedding level. Therefore, we need to
4388 skip invisible text using the bidi iterator,
4389 starting at IT's current position, until we find
4390 ourselves outside of the invisible text.
4391 Skipping invisible text _after_ bidi iteration
4392 avoids affecting the visual order of the
4393 displayed text when invisible properties are
4394 added or removed. */
4395 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4396 {
4397 /* If we were `reseat'ed to a new paragraph,
4398 determine the paragraph base direction. We
4399 need to do it now because
4400 next_element_from_buffer may not have a
4401 chance to do it, if we are going to skip any
4402 text at the beginning, which resets the
4403 FIRST_ELT flag. */
4404 bidi_paragraph_init (it->paragraph_embedding,
4405 &it->bidi_it, 1);
4406 }
4407 do
4408 {
4409 bidi_move_to_visually_next (&it->bidi_it);
4410 }
4411 while (it->stop_charpos <= it->bidi_it.charpos
4412 && it->bidi_it.charpos < newpos);
4413 IT_CHARPOS (*it) = it->bidi_it.charpos;
4414 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4415 /* If we overstepped NEWPOS, record its position in
4416 the iterator, so that we skip invisible text if
4417 later the bidi iteration lands us in the
4418 invisible region again. */
4419 if (IT_CHARPOS (*it) >= newpos)
4420 it->prev_stop = newpos;
4421 }
4422 }
4423 else
4424 {
4425 IT_CHARPOS (*it) = newpos;
4426 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4427 }
4428
4429 /* If there are before-strings at the start of invisible
4430 text, and the text is invisible because of a text
4431 property, arrange to show before-strings because 20.x did
4432 it that way. (If the text is invisible because of an
4433 overlay property instead of a text property, this is
4434 already handled in the overlay code.) */
4435 if (NILP (overlay)
4436 && get_overlay_strings (it, it->stop_charpos))
4437 {
4438 handled = HANDLED_RECOMPUTE_PROPS;
4439 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4440 }
4441 else if (display_ellipsis_p)
4442 {
4443 /* Make sure that the glyphs of the ellipsis will get
4444 correct `charpos' values. If we would not update
4445 it->position here, the glyphs would belong to the
4446 last visible character _before_ the invisible
4447 text, which confuses `set_cursor_from_row'.
4448
4449 We use the last invisible position instead of the
4450 first because this way the cursor is always drawn on
4451 the first "." of the ellipsis, whenever PT is inside
4452 the invisible text. Otherwise the cursor would be
4453 placed _after_ the ellipsis when the point is after the
4454 first invisible character. */
4455 if (!STRINGP (it->object))
4456 {
4457 it->position.charpos = newpos - 1;
4458 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4459 }
4460 it->ellipsis_p = 1;
4461 /* Let the ellipsis display before
4462 considering any properties of the following char.
4463 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4464 handled = HANDLED_RETURN;
4465 }
4466 }
4467 }
4468
4469 return handled;
4470 }
4471
4472
4473 /* Make iterator IT return `...' next.
4474 Replaces LEN characters from buffer. */
4475
4476 static void
4477 setup_for_ellipsis (struct it *it, int len)
4478 {
4479 /* Use the display table definition for `...'. Invalid glyphs
4480 will be handled by the method returning elements from dpvec. */
4481 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4482 {
4483 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4484 it->dpvec = v->contents;
4485 it->dpend = v->contents + v->header.size;
4486 }
4487 else
4488 {
4489 /* Default `...'. */
4490 it->dpvec = default_invis_vector;
4491 it->dpend = default_invis_vector + 3;
4492 }
4493
4494 it->dpvec_char_len = len;
4495 it->current.dpvec_index = 0;
4496 it->dpvec_face_id = -1;
4497
4498 /* Remember the current face id in case glyphs specify faces.
4499 IT's face is restored in set_iterator_to_next.
4500 saved_face_id was set to preceding char's face in handle_stop. */
4501 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4502 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4503
4504 it->method = GET_FROM_DISPLAY_VECTOR;
4505 it->ellipsis_p = 1;
4506 }
4507
4508
4509 \f
4510 /***********************************************************************
4511 'display' property
4512 ***********************************************************************/
4513
4514 /* Set up iterator IT from `display' property at its current position.
4515 Called from handle_stop.
4516 We return HANDLED_RETURN if some part of the display property
4517 overrides the display of the buffer text itself.
4518 Otherwise we return HANDLED_NORMALLY. */
4519
4520 static enum prop_handled
4521 handle_display_prop (struct it *it)
4522 {
4523 Lisp_Object propval, object, overlay;
4524 struct text_pos *position;
4525 ptrdiff_t bufpos;
4526 /* Nonzero if some property replaces the display of the text itself. */
4527 int display_replaced_p = 0;
4528
4529 if (STRINGP (it->string))
4530 {
4531 object = it->string;
4532 position = &it->current.string_pos;
4533 bufpos = CHARPOS (it->current.pos);
4534 }
4535 else
4536 {
4537 XSETWINDOW (object, it->w);
4538 position = &it->current.pos;
4539 bufpos = CHARPOS (*position);
4540 }
4541
4542 /* Reset those iterator values set from display property values. */
4543 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4544 it->space_width = Qnil;
4545 it->font_height = Qnil;
4546 it->voffset = 0;
4547
4548 /* We don't support recursive `display' properties, i.e. string
4549 values that have a string `display' property, that have a string
4550 `display' property etc. */
4551 if (!it->string_from_display_prop_p)
4552 it->area = TEXT_AREA;
4553
4554 propval = get_char_property_and_overlay (make_number (position->charpos),
4555 Qdisplay, object, &overlay);
4556 if (NILP (propval))
4557 return HANDLED_NORMALLY;
4558 /* Now OVERLAY is the overlay that gave us this property, or nil
4559 if it was a text property. */
4560
4561 if (!STRINGP (it->string))
4562 object = it->w->contents;
4563
4564 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4565 position, bufpos,
4566 FRAME_WINDOW_P (it->f));
4567
4568 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4569 }
4570
4571 /* Subroutine of handle_display_prop. Returns non-zero if the display
4572 specification in SPEC is a replacing specification, i.e. it would
4573 replace the text covered by `display' property with something else,
4574 such as an image or a display string. If SPEC includes any kind or
4575 `(space ...) specification, the value is 2; this is used by
4576 compute_display_string_pos, which see.
4577
4578 See handle_single_display_spec for documentation of arguments.
4579 frame_window_p is non-zero if the window being redisplayed is on a
4580 GUI frame; this argument is used only if IT is NULL, see below.
4581
4582 IT can be NULL, if this is called by the bidi reordering code
4583 through compute_display_string_pos, which see. In that case, this
4584 function only examines SPEC, but does not otherwise "handle" it, in
4585 the sense that it doesn't set up members of IT from the display
4586 spec. */
4587 static int
4588 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4589 Lisp_Object overlay, struct text_pos *position,
4590 ptrdiff_t bufpos, int frame_window_p)
4591 {
4592 int replacing_p = 0;
4593 int rv;
4594
4595 if (CONSP (spec)
4596 /* Simple specifications. */
4597 && !EQ (XCAR (spec), Qimage)
4598 && !EQ (XCAR (spec), Qspace)
4599 && !EQ (XCAR (spec), Qwhen)
4600 && !EQ (XCAR (spec), Qslice)
4601 && !EQ (XCAR (spec), Qspace_width)
4602 && !EQ (XCAR (spec), Qheight)
4603 && !EQ (XCAR (spec), Qraise)
4604 /* Marginal area specifications. */
4605 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4606 && !EQ (XCAR (spec), Qleft_fringe)
4607 && !EQ (XCAR (spec), Qright_fringe)
4608 && !NILP (XCAR (spec)))
4609 {
4610 for (; CONSP (spec); spec = XCDR (spec))
4611 {
4612 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4613 overlay, position, bufpos,
4614 replacing_p, frame_window_p)))
4615 {
4616 replacing_p = rv;
4617 /* If some text in a string is replaced, `position' no
4618 longer points to the position of `object'. */
4619 if (!it || STRINGP (object))
4620 break;
4621 }
4622 }
4623 }
4624 else if (VECTORP (spec))
4625 {
4626 ptrdiff_t i;
4627 for (i = 0; i < ASIZE (spec); ++i)
4628 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4629 overlay, position, bufpos,
4630 replacing_p, frame_window_p)))
4631 {
4632 replacing_p = rv;
4633 /* If some text in a string is replaced, `position' no
4634 longer points to the position of `object'. */
4635 if (!it || STRINGP (object))
4636 break;
4637 }
4638 }
4639 else
4640 {
4641 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4642 position, bufpos, 0,
4643 frame_window_p)))
4644 replacing_p = rv;
4645 }
4646
4647 return replacing_p;
4648 }
4649
4650 /* Value is the position of the end of the `display' property starting
4651 at START_POS in OBJECT. */
4652
4653 static struct text_pos
4654 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4655 {
4656 Lisp_Object end;
4657 struct text_pos end_pos;
4658
4659 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4660 Qdisplay, object, Qnil);
4661 CHARPOS (end_pos) = XFASTINT (end);
4662 if (STRINGP (object))
4663 compute_string_pos (&end_pos, start_pos, it->string);
4664 else
4665 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4666
4667 return end_pos;
4668 }
4669
4670
4671 /* Set up IT from a single `display' property specification SPEC. OBJECT
4672 is the object in which the `display' property was found. *POSITION
4673 is the position in OBJECT at which the `display' property was found.
4674 BUFPOS is the buffer position of OBJECT (different from POSITION if
4675 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4676 previously saw a display specification which already replaced text
4677 display with something else, for example an image; we ignore such
4678 properties after the first one has been processed.
4679
4680 OVERLAY is the overlay this `display' property came from,
4681 or nil if it was a text property.
4682
4683 If SPEC is a `space' or `image' specification, and in some other
4684 cases too, set *POSITION to the position where the `display'
4685 property ends.
4686
4687 If IT is NULL, only examine the property specification in SPEC, but
4688 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4689 is intended to be displayed in a window on a GUI frame.
4690
4691 Value is non-zero if something was found which replaces the display
4692 of buffer or string text. */
4693
4694 static int
4695 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4696 Lisp_Object overlay, struct text_pos *position,
4697 ptrdiff_t bufpos, int display_replaced_p,
4698 int frame_window_p)
4699 {
4700 Lisp_Object form;
4701 Lisp_Object location, value;
4702 struct text_pos start_pos = *position;
4703 int valid_p;
4704
4705 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4706 If the result is non-nil, use VALUE instead of SPEC. */
4707 form = Qt;
4708 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4709 {
4710 spec = XCDR (spec);
4711 if (!CONSP (spec))
4712 return 0;
4713 form = XCAR (spec);
4714 spec = XCDR (spec);
4715 }
4716
4717 if (!NILP (form) && !EQ (form, Qt))
4718 {
4719 ptrdiff_t count = SPECPDL_INDEX ();
4720 struct gcpro gcpro1;
4721
4722 /* Bind `object' to the object having the `display' property, a
4723 buffer or string. Bind `position' to the position in the
4724 object where the property was found, and `buffer-position'
4725 to the current position in the buffer. */
4726
4727 if (NILP (object))
4728 XSETBUFFER (object, current_buffer);
4729 specbind (Qobject, object);
4730 specbind (Qposition, make_number (CHARPOS (*position)));
4731 specbind (Qbuffer_position, make_number (bufpos));
4732 GCPRO1 (form);
4733 form = safe_eval (form);
4734 UNGCPRO;
4735 unbind_to (count, Qnil);
4736 }
4737
4738 if (NILP (form))
4739 return 0;
4740
4741 /* Handle `(height HEIGHT)' specifications. */
4742 if (CONSP (spec)
4743 && EQ (XCAR (spec), Qheight)
4744 && CONSP (XCDR (spec)))
4745 {
4746 if (it)
4747 {
4748 if (!FRAME_WINDOW_P (it->f))
4749 return 0;
4750
4751 it->font_height = XCAR (XCDR (spec));
4752 if (!NILP (it->font_height))
4753 {
4754 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4755 int new_height = -1;
4756
4757 if (CONSP (it->font_height)
4758 && (EQ (XCAR (it->font_height), Qplus)
4759 || EQ (XCAR (it->font_height), Qminus))
4760 && CONSP (XCDR (it->font_height))
4761 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4762 {
4763 /* `(+ N)' or `(- N)' where N is an integer. */
4764 int steps = XINT (XCAR (XCDR (it->font_height)));
4765 if (EQ (XCAR (it->font_height), Qplus))
4766 steps = - steps;
4767 it->face_id = smaller_face (it->f, it->face_id, steps);
4768 }
4769 else if (FUNCTIONP (it->font_height))
4770 {
4771 /* Call function with current height as argument.
4772 Value is the new height. */
4773 Lisp_Object height;
4774 height = safe_call1 (it->font_height,
4775 face->lface[LFACE_HEIGHT_INDEX]);
4776 if (NUMBERP (height))
4777 new_height = XFLOATINT (height);
4778 }
4779 else if (NUMBERP (it->font_height))
4780 {
4781 /* Value is a multiple of the canonical char height. */
4782 struct face *f;
4783
4784 f = FACE_FROM_ID (it->f,
4785 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4786 new_height = (XFLOATINT (it->font_height)
4787 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4788 }
4789 else
4790 {
4791 /* Evaluate IT->font_height with `height' bound to the
4792 current specified height to get the new height. */
4793 ptrdiff_t count = SPECPDL_INDEX ();
4794
4795 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4796 value = safe_eval (it->font_height);
4797 unbind_to (count, Qnil);
4798
4799 if (NUMBERP (value))
4800 new_height = XFLOATINT (value);
4801 }
4802
4803 if (new_height > 0)
4804 it->face_id = face_with_height (it->f, it->face_id, new_height);
4805 }
4806 }
4807
4808 return 0;
4809 }
4810
4811 /* Handle `(space-width WIDTH)'. */
4812 if (CONSP (spec)
4813 && EQ (XCAR (spec), Qspace_width)
4814 && CONSP (XCDR (spec)))
4815 {
4816 if (it)
4817 {
4818 if (!FRAME_WINDOW_P (it->f))
4819 return 0;
4820
4821 value = XCAR (XCDR (spec));
4822 if (NUMBERP (value) && XFLOATINT (value) > 0)
4823 it->space_width = value;
4824 }
4825
4826 return 0;
4827 }
4828
4829 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4830 if (CONSP (spec)
4831 && EQ (XCAR (spec), Qslice))
4832 {
4833 Lisp_Object tem;
4834
4835 if (it)
4836 {
4837 if (!FRAME_WINDOW_P (it->f))
4838 return 0;
4839
4840 if (tem = XCDR (spec), CONSP (tem))
4841 {
4842 it->slice.x = XCAR (tem);
4843 if (tem = XCDR (tem), CONSP (tem))
4844 {
4845 it->slice.y = XCAR (tem);
4846 if (tem = XCDR (tem), CONSP (tem))
4847 {
4848 it->slice.width = XCAR (tem);
4849 if (tem = XCDR (tem), CONSP (tem))
4850 it->slice.height = XCAR (tem);
4851 }
4852 }
4853 }
4854 }
4855
4856 return 0;
4857 }
4858
4859 /* Handle `(raise FACTOR)'. */
4860 if (CONSP (spec)
4861 && EQ (XCAR (spec), Qraise)
4862 && CONSP (XCDR (spec)))
4863 {
4864 if (it)
4865 {
4866 if (!FRAME_WINDOW_P (it->f))
4867 return 0;
4868
4869 #ifdef HAVE_WINDOW_SYSTEM
4870 value = XCAR (XCDR (spec));
4871 if (NUMBERP (value))
4872 {
4873 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4874 it->voffset = - (XFLOATINT (value)
4875 * (FONT_HEIGHT (face->font)));
4876 }
4877 #endif /* HAVE_WINDOW_SYSTEM */
4878 }
4879
4880 return 0;
4881 }
4882
4883 /* Don't handle the other kinds of display specifications
4884 inside a string that we got from a `display' property. */
4885 if (it && it->string_from_display_prop_p)
4886 return 0;
4887
4888 /* Characters having this form of property are not displayed, so
4889 we have to find the end of the property. */
4890 if (it)
4891 {
4892 start_pos = *position;
4893 *position = display_prop_end (it, object, start_pos);
4894 }
4895 value = Qnil;
4896
4897 /* Stop the scan at that end position--we assume that all
4898 text properties change there. */
4899 if (it)
4900 it->stop_charpos = position->charpos;
4901
4902 /* Handle `(left-fringe BITMAP [FACE])'
4903 and `(right-fringe BITMAP [FACE])'. */
4904 if (CONSP (spec)
4905 && (EQ (XCAR (spec), Qleft_fringe)
4906 || EQ (XCAR (spec), Qright_fringe))
4907 && CONSP (XCDR (spec)))
4908 {
4909 int fringe_bitmap;
4910
4911 if (it)
4912 {
4913 if (!FRAME_WINDOW_P (it->f))
4914 /* If we return here, POSITION has been advanced
4915 across the text with this property. */
4916 {
4917 /* Synchronize the bidi iterator with POSITION. This is
4918 needed because we are not going to push the iterator
4919 on behalf of this display property, so there will be
4920 no pop_it call to do this synchronization for us. */
4921 if (it->bidi_p)
4922 {
4923 it->position = *position;
4924 iterate_out_of_display_property (it);
4925 *position = it->position;
4926 }
4927 return 1;
4928 }
4929 }
4930 else if (!frame_window_p)
4931 return 1;
4932
4933 #ifdef HAVE_WINDOW_SYSTEM
4934 value = XCAR (XCDR (spec));
4935 if (!SYMBOLP (value)
4936 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4937 /* If we return here, POSITION has been advanced
4938 across the text with this property. */
4939 {
4940 if (it && it->bidi_p)
4941 {
4942 it->position = *position;
4943 iterate_out_of_display_property (it);
4944 *position = it->position;
4945 }
4946 return 1;
4947 }
4948
4949 if (it)
4950 {
4951 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4952
4953 if (CONSP (XCDR (XCDR (spec))))
4954 {
4955 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4956 int face_id2 = lookup_derived_face (it->f, face_name,
4957 FRINGE_FACE_ID, 0);
4958 if (face_id2 >= 0)
4959 face_id = face_id2;
4960 }
4961
4962 /* Save current settings of IT so that we can restore them
4963 when we are finished with the glyph property value. */
4964 push_it (it, position);
4965
4966 it->area = TEXT_AREA;
4967 it->what = IT_IMAGE;
4968 it->image_id = -1; /* no image */
4969 it->position = start_pos;
4970 it->object = NILP (object) ? it->w->contents : object;
4971 it->method = GET_FROM_IMAGE;
4972 it->from_overlay = Qnil;
4973 it->face_id = face_id;
4974 it->from_disp_prop_p = 1;
4975
4976 /* Say that we haven't consumed the characters with
4977 `display' property yet. The call to pop_it in
4978 set_iterator_to_next will clean this up. */
4979 *position = start_pos;
4980
4981 if (EQ (XCAR (spec), Qleft_fringe))
4982 {
4983 it->left_user_fringe_bitmap = fringe_bitmap;
4984 it->left_user_fringe_face_id = face_id;
4985 }
4986 else
4987 {
4988 it->right_user_fringe_bitmap = fringe_bitmap;
4989 it->right_user_fringe_face_id = face_id;
4990 }
4991 }
4992 #endif /* HAVE_WINDOW_SYSTEM */
4993 return 1;
4994 }
4995
4996 /* Prepare to handle `((margin left-margin) ...)',
4997 `((margin right-margin) ...)' and `((margin nil) ...)'
4998 prefixes for display specifications. */
4999 location = Qunbound;
5000 if (CONSP (spec) && CONSP (XCAR (spec)))
5001 {
5002 Lisp_Object tem;
5003
5004 value = XCDR (spec);
5005 if (CONSP (value))
5006 value = XCAR (value);
5007
5008 tem = XCAR (spec);
5009 if (EQ (XCAR (tem), Qmargin)
5010 && (tem = XCDR (tem),
5011 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5012 (NILP (tem)
5013 || EQ (tem, Qleft_margin)
5014 || EQ (tem, Qright_margin))))
5015 location = tem;
5016 }
5017
5018 if (EQ (location, Qunbound))
5019 {
5020 location = Qnil;
5021 value = spec;
5022 }
5023
5024 /* After this point, VALUE is the property after any
5025 margin prefix has been stripped. It must be a string,
5026 an image specification, or `(space ...)'.
5027
5028 LOCATION specifies where to display: `left-margin',
5029 `right-margin' or nil. */
5030
5031 valid_p = (STRINGP (value)
5032 #ifdef HAVE_WINDOW_SYSTEM
5033 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5034 && valid_image_p (value))
5035 #endif /* not HAVE_WINDOW_SYSTEM */
5036 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5037
5038 if (valid_p && !display_replaced_p)
5039 {
5040 int retval = 1;
5041
5042 if (!it)
5043 {
5044 /* Callers need to know whether the display spec is any kind
5045 of `(space ...)' spec that is about to affect text-area
5046 display. */
5047 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5048 retval = 2;
5049 return retval;
5050 }
5051
5052 /* Save current settings of IT so that we can restore them
5053 when we are finished with the glyph property value. */
5054 push_it (it, position);
5055 it->from_overlay = overlay;
5056 it->from_disp_prop_p = 1;
5057
5058 if (NILP (location))
5059 it->area = TEXT_AREA;
5060 else if (EQ (location, Qleft_margin))
5061 it->area = LEFT_MARGIN_AREA;
5062 else
5063 it->area = RIGHT_MARGIN_AREA;
5064
5065 if (STRINGP (value))
5066 {
5067 it->string = value;
5068 it->multibyte_p = STRING_MULTIBYTE (it->string);
5069 it->current.overlay_string_index = -1;
5070 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5071 it->end_charpos = it->string_nchars = SCHARS (it->string);
5072 it->method = GET_FROM_STRING;
5073 it->stop_charpos = 0;
5074 it->prev_stop = 0;
5075 it->base_level_stop = 0;
5076 it->string_from_display_prop_p = 1;
5077 /* Say that we haven't consumed the characters with
5078 `display' property yet. The call to pop_it in
5079 set_iterator_to_next will clean this up. */
5080 if (BUFFERP (object))
5081 *position = start_pos;
5082
5083 /* Force paragraph direction to be that of the parent
5084 object. If the parent object's paragraph direction is
5085 not yet determined, default to L2R. */
5086 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5087 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5088 else
5089 it->paragraph_embedding = L2R;
5090
5091 /* Set up the bidi iterator for this display string. */
5092 if (it->bidi_p)
5093 {
5094 it->bidi_it.string.lstring = it->string;
5095 it->bidi_it.string.s = NULL;
5096 it->bidi_it.string.schars = it->end_charpos;
5097 it->bidi_it.string.bufpos = bufpos;
5098 it->bidi_it.string.from_disp_str = 1;
5099 it->bidi_it.string.unibyte = !it->multibyte_p;
5100 it->bidi_it.w = it->w;
5101 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5102 }
5103 }
5104 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5105 {
5106 it->method = GET_FROM_STRETCH;
5107 it->object = value;
5108 *position = it->position = start_pos;
5109 retval = 1 + (it->area == TEXT_AREA);
5110 }
5111 #ifdef HAVE_WINDOW_SYSTEM
5112 else
5113 {
5114 it->what = IT_IMAGE;
5115 it->image_id = lookup_image (it->f, value);
5116 it->position = start_pos;
5117 it->object = NILP (object) ? it->w->contents : object;
5118 it->method = GET_FROM_IMAGE;
5119
5120 /* Say that we haven't consumed the characters with
5121 `display' property yet. The call to pop_it in
5122 set_iterator_to_next will clean this up. */
5123 *position = start_pos;
5124 }
5125 #endif /* HAVE_WINDOW_SYSTEM */
5126
5127 return retval;
5128 }
5129
5130 /* Invalid property or property not supported. Restore
5131 POSITION to what it was before. */
5132 *position = start_pos;
5133 return 0;
5134 }
5135
5136 /* Check if PROP is a display property value whose text should be
5137 treated as intangible. OVERLAY is the overlay from which PROP
5138 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5139 specify the buffer position covered by PROP. */
5140
5141 int
5142 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5143 ptrdiff_t charpos, ptrdiff_t bytepos)
5144 {
5145 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5146 struct text_pos position;
5147
5148 SET_TEXT_POS (position, charpos, bytepos);
5149 return handle_display_spec (NULL, prop, Qnil, overlay,
5150 &position, charpos, frame_window_p);
5151 }
5152
5153
5154 /* Return 1 if PROP is a display sub-property value containing STRING.
5155
5156 Implementation note: this and the following function are really
5157 special cases of handle_display_spec and
5158 handle_single_display_spec, and should ideally use the same code.
5159 Until they do, these two pairs must be consistent and must be
5160 modified in sync. */
5161
5162 static int
5163 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5164 {
5165 if (EQ (string, prop))
5166 return 1;
5167
5168 /* Skip over `when FORM'. */
5169 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5170 {
5171 prop = XCDR (prop);
5172 if (!CONSP (prop))
5173 return 0;
5174 /* Actually, the condition following `when' should be eval'ed,
5175 like handle_single_display_spec does, and we should return
5176 zero if it evaluates to nil. However, this function is
5177 called only when the buffer was already displayed and some
5178 glyph in the glyph matrix was found to come from a display
5179 string. Therefore, the condition was already evaluated, and
5180 the result was non-nil, otherwise the display string wouldn't
5181 have been displayed and we would have never been called for
5182 this property. Thus, we can skip the evaluation and assume
5183 its result is non-nil. */
5184 prop = XCDR (prop);
5185 }
5186
5187 if (CONSP (prop))
5188 /* Skip over `margin LOCATION'. */
5189 if (EQ (XCAR (prop), Qmargin))
5190 {
5191 prop = XCDR (prop);
5192 if (!CONSP (prop))
5193 return 0;
5194
5195 prop = XCDR (prop);
5196 if (!CONSP (prop))
5197 return 0;
5198 }
5199
5200 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5201 }
5202
5203
5204 /* Return 1 if STRING appears in the `display' property PROP. */
5205
5206 static int
5207 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5208 {
5209 if (CONSP (prop)
5210 && !EQ (XCAR (prop), Qwhen)
5211 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5212 {
5213 /* A list of sub-properties. */
5214 while (CONSP (prop))
5215 {
5216 if (single_display_spec_string_p (XCAR (prop), string))
5217 return 1;
5218 prop = XCDR (prop);
5219 }
5220 }
5221 else if (VECTORP (prop))
5222 {
5223 /* A vector of sub-properties. */
5224 ptrdiff_t i;
5225 for (i = 0; i < ASIZE (prop); ++i)
5226 if (single_display_spec_string_p (AREF (prop, i), string))
5227 return 1;
5228 }
5229 else
5230 return single_display_spec_string_p (prop, string);
5231
5232 return 0;
5233 }
5234
5235 /* Look for STRING in overlays and text properties in the current
5236 buffer, between character positions FROM and TO (excluding TO).
5237 BACK_P non-zero means look back (in this case, TO is supposed to be
5238 less than FROM).
5239 Value is the first character position where STRING was found, or
5240 zero if it wasn't found before hitting TO.
5241
5242 This function may only use code that doesn't eval because it is
5243 called asynchronously from note_mouse_highlight. */
5244
5245 static ptrdiff_t
5246 string_buffer_position_lim (Lisp_Object string,
5247 ptrdiff_t from, ptrdiff_t to, int back_p)
5248 {
5249 Lisp_Object limit, prop, pos;
5250 int found = 0;
5251
5252 pos = make_number (max (from, BEGV));
5253
5254 if (!back_p) /* looking forward */
5255 {
5256 limit = make_number (min (to, ZV));
5257 while (!found && !EQ (pos, limit))
5258 {
5259 prop = Fget_char_property (pos, Qdisplay, Qnil);
5260 if (!NILP (prop) && display_prop_string_p (prop, string))
5261 found = 1;
5262 else
5263 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5264 limit);
5265 }
5266 }
5267 else /* looking back */
5268 {
5269 limit = make_number (max (to, BEGV));
5270 while (!found && !EQ (pos, limit))
5271 {
5272 prop = Fget_char_property (pos, Qdisplay, Qnil);
5273 if (!NILP (prop) && display_prop_string_p (prop, string))
5274 found = 1;
5275 else
5276 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5277 limit);
5278 }
5279 }
5280
5281 return found ? XINT (pos) : 0;
5282 }
5283
5284 /* Determine which buffer position in current buffer STRING comes from.
5285 AROUND_CHARPOS is an approximate position where it could come from.
5286 Value is the buffer position or 0 if it couldn't be determined.
5287
5288 This function is necessary because we don't record buffer positions
5289 in glyphs generated from strings (to keep struct glyph small).
5290 This function may only use code that doesn't eval because it is
5291 called asynchronously from note_mouse_highlight. */
5292
5293 static ptrdiff_t
5294 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5295 {
5296 const int MAX_DISTANCE = 1000;
5297 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5298 around_charpos + MAX_DISTANCE,
5299 0);
5300
5301 if (!found)
5302 found = string_buffer_position_lim (string, around_charpos,
5303 around_charpos - MAX_DISTANCE, 1);
5304 return found;
5305 }
5306
5307
5308 \f
5309 /***********************************************************************
5310 `composition' property
5311 ***********************************************************************/
5312
5313 /* Set up iterator IT from `composition' property at its current
5314 position. Called from handle_stop. */
5315
5316 static enum prop_handled
5317 handle_composition_prop (struct it *it)
5318 {
5319 Lisp_Object prop, string;
5320 ptrdiff_t pos, pos_byte, start, end;
5321
5322 if (STRINGP (it->string))
5323 {
5324 unsigned char *s;
5325
5326 pos = IT_STRING_CHARPOS (*it);
5327 pos_byte = IT_STRING_BYTEPOS (*it);
5328 string = it->string;
5329 s = SDATA (string) + pos_byte;
5330 it->c = STRING_CHAR (s);
5331 }
5332 else
5333 {
5334 pos = IT_CHARPOS (*it);
5335 pos_byte = IT_BYTEPOS (*it);
5336 string = Qnil;
5337 it->c = FETCH_CHAR (pos_byte);
5338 }
5339
5340 /* If there's a valid composition and point is not inside of the
5341 composition (in the case that the composition is from the current
5342 buffer), draw a glyph composed from the composition components. */
5343 if (find_composition (pos, -1, &start, &end, &prop, string)
5344 && composition_valid_p (start, end, prop)
5345 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5346 {
5347 if (start < pos)
5348 /* As we can't handle this situation (perhaps font-lock added
5349 a new composition), we just return here hoping that next
5350 redisplay will detect this composition much earlier. */
5351 return HANDLED_NORMALLY;
5352 if (start != pos)
5353 {
5354 if (STRINGP (it->string))
5355 pos_byte = string_char_to_byte (it->string, start);
5356 else
5357 pos_byte = CHAR_TO_BYTE (start);
5358 }
5359 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5360 prop, string);
5361
5362 if (it->cmp_it.id >= 0)
5363 {
5364 it->cmp_it.ch = -1;
5365 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5366 it->cmp_it.nglyphs = -1;
5367 }
5368 }
5369
5370 return HANDLED_NORMALLY;
5371 }
5372
5373
5374 \f
5375 /***********************************************************************
5376 Overlay strings
5377 ***********************************************************************/
5378
5379 /* The following structure is used to record overlay strings for
5380 later sorting in load_overlay_strings. */
5381
5382 struct overlay_entry
5383 {
5384 Lisp_Object overlay;
5385 Lisp_Object string;
5386 EMACS_INT priority;
5387 int after_string_p;
5388 };
5389
5390
5391 /* Set up iterator IT from overlay strings at its current position.
5392 Called from handle_stop. */
5393
5394 static enum prop_handled
5395 handle_overlay_change (struct it *it)
5396 {
5397 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5398 return HANDLED_RECOMPUTE_PROPS;
5399 else
5400 return HANDLED_NORMALLY;
5401 }
5402
5403
5404 /* Set up the next overlay string for delivery by IT, if there is an
5405 overlay string to deliver. Called by set_iterator_to_next when the
5406 end of the current overlay string is reached. If there are more
5407 overlay strings to display, IT->string and
5408 IT->current.overlay_string_index are set appropriately here.
5409 Otherwise IT->string is set to nil. */
5410
5411 static void
5412 next_overlay_string (struct it *it)
5413 {
5414 ++it->current.overlay_string_index;
5415 if (it->current.overlay_string_index == it->n_overlay_strings)
5416 {
5417 /* No more overlay strings. Restore IT's settings to what
5418 they were before overlay strings were processed, and
5419 continue to deliver from current_buffer. */
5420
5421 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5422 pop_it (it);
5423 eassert (it->sp > 0
5424 || (NILP (it->string)
5425 && it->method == GET_FROM_BUFFER
5426 && it->stop_charpos >= BEGV
5427 && it->stop_charpos <= it->end_charpos));
5428 it->current.overlay_string_index = -1;
5429 it->n_overlay_strings = 0;
5430 it->overlay_strings_charpos = -1;
5431 /* If there's an empty display string on the stack, pop the
5432 stack, to resync the bidi iterator with IT's position. Such
5433 empty strings are pushed onto the stack in
5434 get_overlay_strings_1. */
5435 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5436 pop_it (it);
5437
5438 /* If we're at the end of the buffer, record that we have
5439 processed the overlay strings there already, so that
5440 next_element_from_buffer doesn't try it again. */
5441 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5442 it->overlay_strings_at_end_processed_p = 1;
5443 }
5444 else
5445 {
5446 /* There are more overlay strings to process. If
5447 IT->current.overlay_string_index has advanced to a position
5448 where we must load IT->overlay_strings with more strings, do
5449 it. We must load at the IT->overlay_strings_charpos where
5450 IT->n_overlay_strings was originally computed; when invisible
5451 text is present, this might not be IT_CHARPOS (Bug#7016). */
5452 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5453
5454 if (it->current.overlay_string_index && i == 0)
5455 load_overlay_strings (it, it->overlay_strings_charpos);
5456
5457 /* Initialize IT to deliver display elements from the overlay
5458 string. */
5459 it->string = it->overlay_strings[i];
5460 it->multibyte_p = STRING_MULTIBYTE (it->string);
5461 SET_TEXT_POS (it->current.string_pos, 0, 0);
5462 it->method = GET_FROM_STRING;
5463 it->stop_charpos = 0;
5464 it->end_charpos = SCHARS (it->string);
5465 if (it->cmp_it.stop_pos >= 0)
5466 it->cmp_it.stop_pos = 0;
5467 it->prev_stop = 0;
5468 it->base_level_stop = 0;
5469
5470 /* Set up the bidi iterator for this overlay string. */
5471 if (it->bidi_p)
5472 {
5473 it->bidi_it.string.lstring = it->string;
5474 it->bidi_it.string.s = NULL;
5475 it->bidi_it.string.schars = SCHARS (it->string);
5476 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5477 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5478 it->bidi_it.string.unibyte = !it->multibyte_p;
5479 it->bidi_it.w = it->w;
5480 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5481 }
5482 }
5483
5484 CHECK_IT (it);
5485 }
5486
5487
5488 /* Compare two overlay_entry structures E1 and E2. Used as a
5489 comparison function for qsort in load_overlay_strings. Overlay
5490 strings for the same position are sorted so that
5491
5492 1. All after-strings come in front of before-strings, except
5493 when they come from the same overlay.
5494
5495 2. Within after-strings, strings are sorted so that overlay strings
5496 from overlays with higher priorities come first.
5497
5498 2. Within before-strings, strings are sorted so that overlay
5499 strings from overlays with higher priorities come last.
5500
5501 Value is analogous to strcmp. */
5502
5503
5504 static int
5505 compare_overlay_entries (const void *e1, const void *e2)
5506 {
5507 struct overlay_entry const *entry1 = e1;
5508 struct overlay_entry const *entry2 = e2;
5509 int result;
5510
5511 if (entry1->after_string_p != entry2->after_string_p)
5512 {
5513 /* Let after-strings appear in front of before-strings if
5514 they come from different overlays. */
5515 if (EQ (entry1->overlay, entry2->overlay))
5516 result = entry1->after_string_p ? 1 : -1;
5517 else
5518 result = entry1->after_string_p ? -1 : 1;
5519 }
5520 else if (entry1->priority != entry2->priority)
5521 {
5522 if (entry1->after_string_p)
5523 /* After-strings sorted in order of decreasing priority. */
5524 result = entry2->priority < entry1->priority ? -1 : 1;
5525 else
5526 /* Before-strings sorted in order of increasing priority. */
5527 result = entry1->priority < entry2->priority ? -1 : 1;
5528 }
5529 else
5530 result = 0;
5531
5532 return result;
5533 }
5534
5535
5536 /* Load the vector IT->overlay_strings with overlay strings from IT's
5537 current buffer position, or from CHARPOS if that is > 0. Set
5538 IT->n_overlays to the total number of overlay strings found.
5539
5540 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5541 a time. On entry into load_overlay_strings,
5542 IT->current.overlay_string_index gives the number of overlay
5543 strings that have already been loaded by previous calls to this
5544 function.
5545
5546 IT->add_overlay_start contains an additional overlay start
5547 position to consider for taking overlay strings from, if non-zero.
5548 This position comes into play when the overlay has an `invisible'
5549 property, and both before and after-strings. When we've skipped to
5550 the end of the overlay, because of its `invisible' property, we
5551 nevertheless want its before-string to appear.
5552 IT->add_overlay_start will contain the overlay start position
5553 in this case.
5554
5555 Overlay strings are sorted so that after-string strings come in
5556 front of before-string strings. Within before and after-strings,
5557 strings are sorted by overlay priority. See also function
5558 compare_overlay_entries. */
5559
5560 static void
5561 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5562 {
5563 Lisp_Object overlay, window, str, invisible;
5564 struct Lisp_Overlay *ov;
5565 ptrdiff_t start, end;
5566 ptrdiff_t size = 20;
5567 ptrdiff_t n = 0, i, j;
5568 int invis_p;
5569 struct overlay_entry *entries = alloca (size * sizeof *entries);
5570 USE_SAFE_ALLOCA;
5571
5572 if (charpos <= 0)
5573 charpos = IT_CHARPOS (*it);
5574
5575 /* Append the overlay string STRING of overlay OVERLAY to vector
5576 `entries' which has size `size' and currently contains `n'
5577 elements. AFTER_P non-zero means STRING is an after-string of
5578 OVERLAY. */
5579 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5580 do \
5581 { \
5582 Lisp_Object priority; \
5583 \
5584 if (n == size) \
5585 { \
5586 struct overlay_entry *old = entries; \
5587 SAFE_NALLOCA (entries, 2, size); \
5588 memcpy (entries, old, size * sizeof *entries); \
5589 size *= 2; \
5590 } \
5591 \
5592 entries[n].string = (STRING); \
5593 entries[n].overlay = (OVERLAY); \
5594 priority = Foverlay_get ((OVERLAY), Qpriority); \
5595 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5596 entries[n].after_string_p = (AFTER_P); \
5597 ++n; \
5598 } \
5599 while (0)
5600
5601 /* Process overlay before the overlay center. */
5602 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5603 {
5604 XSETMISC (overlay, ov);
5605 eassert (OVERLAYP (overlay));
5606 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5607 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5608
5609 if (end < charpos)
5610 break;
5611
5612 /* Skip this overlay if it doesn't start or end at IT's current
5613 position. */
5614 if (end != charpos && start != charpos)
5615 continue;
5616
5617 /* Skip this overlay if it doesn't apply to IT->w. */
5618 window = Foverlay_get (overlay, Qwindow);
5619 if (WINDOWP (window) && XWINDOW (window) != it->w)
5620 continue;
5621
5622 /* If the text ``under'' the overlay is invisible, both before-
5623 and after-strings from this overlay are visible; start and
5624 end position are indistinguishable. */
5625 invisible = Foverlay_get (overlay, Qinvisible);
5626 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5627
5628 /* If overlay has a non-empty before-string, record it. */
5629 if ((start == charpos || (end == charpos && invis_p))
5630 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5631 && SCHARS (str))
5632 RECORD_OVERLAY_STRING (overlay, str, 0);
5633
5634 /* If overlay has a non-empty after-string, record it. */
5635 if ((end == charpos || (start == charpos && invis_p))
5636 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5637 && SCHARS (str))
5638 RECORD_OVERLAY_STRING (overlay, str, 1);
5639 }
5640
5641 /* Process overlays after the overlay center. */
5642 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5643 {
5644 XSETMISC (overlay, ov);
5645 eassert (OVERLAYP (overlay));
5646 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5647 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5648
5649 if (start > charpos)
5650 break;
5651
5652 /* Skip this overlay if it doesn't start or end at IT's current
5653 position. */
5654 if (end != charpos && start != charpos)
5655 continue;
5656
5657 /* Skip this overlay if it doesn't apply to IT->w. */
5658 window = Foverlay_get (overlay, Qwindow);
5659 if (WINDOWP (window) && XWINDOW (window) != it->w)
5660 continue;
5661
5662 /* If the text ``under'' the overlay is invisible, it has a zero
5663 dimension, and both before- and after-strings apply. */
5664 invisible = Foverlay_get (overlay, Qinvisible);
5665 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5666
5667 /* If overlay has a non-empty before-string, record it. */
5668 if ((start == charpos || (end == charpos && invis_p))
5669 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5670 && SCHARS (str))
5671 RECORD_OVERLAY_STRING (overlay, str, 0);
5672
5673 /* If overlay has a non-empty after-string, record it. */
5674 if ((end == charpos || (start == charpos && invis_p))
5675 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5676 && SCHARS (str))
5677 RECORD_OVERLAY_STRING (overlay, str, 1);
5678 }
5679
5680 #undef RECORD_OVERLAY_STRING
5681
5682 /* Sort entries. */
5683 if (n > 1)
5684 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5685
5686 /* Record number of overlay strings, and where we computed it. */
5687 it->n_overlay_strings = n;
5688 it->overlay_strings_charpos = charpos;
5689
5690 /* IT->current.overlay_string_index is the number of overlay strings
5691 that have already been consumed by IT. Copy some of the
5692 remaining overlay strings to IT->overlay_strings. */
5693 i = 0;
5694 j = it->current.overlay_string_index;
5695 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5696 {
5697 it->overlay_strings[i] = entries[j].string;
5698 it->string_overlays[i++] = entries[j++].overlay;
5699 }
5700
5701 CHECK_IT (it);
5702 SAFE_FREE ();
5703 }
5704
5705
5706 /* Get the first chunk of overlay strings at IT's current buffer
5707 position, or at CHARPOS if that is > 0. Value is non-zero if at
5708 least one overlay string was found. */
5709
5710 static int
5711 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5712 {
5713 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5714 process. This fills IT->overlay_strings with strings, and sets
5715 IT->n_overlay_strings to the total number of strings to process.
5716 IT->pos.overlay_string_index has to be set temporarily to zero
5717 because load_overlay_strings needs this; it must be set to -1
5718 when no overlay strings are found because a zero value would
5719 indicate a position in the first overlay string. */
5720 it->current.overlay_string_index = 0;
5721 load_overlay_strings (it, charpos);
5722
5723 /* If we found overlay strings, set up IT to deliver display
5724 elements from the first one. Otherwise set up IT to deliver
5725 from current_buffer. */
5726 if (it->n_overlay_strings)
5727 {
5728 /* Make sure we know settings in current_buffer, so that we can
5729 restore meaningful values when we're done with the overlay
5730 strings. */
5731 if (compute_stop_p)
5732 compute_stop_pos (it);
5733 eassert (it->face_id >= 0);
5734
5735 /* Save IT's settings. They are restored after all overlay
5736 strings have been processed. */
5737 eassert (!compute_stop_p || it->sp == 0);
5738
5739 /* When called from handle_stop, there might be an empty display
5740 string loaded. In that case, don't bother saving it. But
5741 don't use this optimization with the bidi iterator, since we
5742 need the corresponding pop_it call to resync the bidi
5743 iterator's position with IT's position, after we are done
5744 with the overlay strings. (The corresponding call to pop_it
5745 in case of an empty display string is in
5746 next_overlay_string.) */
5747 if (!(!it->bidi_p
5748 && STRINGP (it->string) && !SCHARS (it->string)))
5749 push_it (it, NULL);
5750
5751 /* Set up IT to deliver display elements from the first overlay
5752 string. */
5753 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5754 it->string = it->overlay_strings[0];
5755 it->from_overlay = Qnil;
5756 it->stop_charpos = 0;
5757 eassert (STRINGP (it->string));
5758 it->end_charpos = SCHARS (it->string);
5759 it->prev_stop = 0;
5760 it->base_level_stop = 0;
5761 it->multibyte_p = STRING_MULTIBYTE (it->string);
5762 it->method = GET_FROM_STRING;
5763 it->from_disp_prop_p = 0;
5764
5765 /* Force paragraph direction to be that of the parent
5766 buffer. */
5767 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5768 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5769 else
5770 it->paragraph_embedding = L2R;
5771
5772 /* Set up the bidi iterator for this overlay string. */
5773 if (it->bidi_p)
5774 {
5775 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5776
5777 it->bidi_it.string.lstring = it->string;
5778 it->bidi_it.string.s = NULL;
5779 it->bidi_it.string.schars = SCHARS (it->string);
5780 it->bidi_it.string.bufpos = pos;
5781 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5782 it->bidi_it.string.unibyte = !it->multibyte_p;
5783 it->bidi_it.w = it->w;
5784 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5785 }
5786 return 1;
5787 }
5788
5789 it->current.overlay_string_index = -1;
5790 return 0;
5791 }
5792
5793 static int
5794 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5795 {
5796 it->string = Qnil;
5797 it->method = GET_FROM_BUFFER;
5798
5799 (void) get_overlay_strings_1 (it, charpos, 1);
5800
5801 CHECK_IT (it);
5802
5803 /* Value is non-zero if we found at least one overlay string. */
5804 return STRINGP (it->string);
5805 }
5806
5807
5808 \f
5809 /***********************************************************************
5810 Saving and restoring state
5811 ***********************************************************************/
5812
5813 /* Save current settings of IT on IT->stack. Called, for example,
5814 before setting up IT for an overlay string, to be able to restore
5815 IT's settings to what they were after the overlay string has been
5816 processed. If POSITION is non-NULL, it is the position to save on
5817 the stack instead of IT->position. */
5818
5819 static void
5820 push_it (struct it *it, struct text_pos *position)
5821 {
5822 struct iterator_stack_entry *p;
5823
5824 eassert (it->sp < IT_STACK_SIZE);
5825 p = it->stack + it->sp;
5826
5827 p->stop_charpos = it->stop_charpos;
5828 p->prev_stop = it->prev_stop;
5829 p->base_level_stop = it->base_level_stop;
5830 p->cmp_it = it->cmp_it;
5831 eassert (it->face_id >= 0);
5832 p->face_id = it->face_id;
5833 p->string = it->string;
5834 p->method = it->method;
5835 p->from_overlay = it->from_overlay;
5836 switch (p->method)
5837 {
5838 case GET_FROM_IMAGE:
5839 p->u.image.object = it->object;
5840 p->u.image.image_id = it->image_id;
5841 p->u.image.slice = it->slice;
5842 break;
5843 case GET_FROM_STRETCH:
5844 p->u.stretch.object = it->object;
5845 break;
5846 }
5847 p->position = position ? *position : it->position;
5848 p->current = it->current;
5849 p->end_charpos = it->end_charpos;
5850 p->string_nchars = it->string_nchars;
5851 p->area = it->area;
5852 p->multibyte_p = it->multibyte_p;
5853 p->avoid_cursor_p = it->avoid_cursor_p;
5854 p->space_width = it->space_width;
5855 p->font_height = it->font_height;
5856 p->voffset = it->voffset;
5857 p->string_from_display_prop_p = it->string_from_display_prop_p;
5858 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5859 p->display_ellipsis_p = 0;
5860 p->line_wrap = it->line_wrap;
5861 p->bidi_p = it->bidi_p;
5862 p->paragraph_embedding = it->paragraph_embedding;
5863 p->from_disp_prop_p = it->from_disp_prop_p;
5864 ++it->sp;
5865
5866 /* Save the state of the bidi iterator as well. */
5867 if (it->bidi_p)
5868 bidi_push_it (&it->bidi_it);
5869 }
5870
5871 static void
5872 iterate_out_of_display_property (struct it *it)
5873 {
5874 int buffer_p = !STRINGP (it->string);
5875 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5876 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5877
5878 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5879
5880 /* Maybe initialize paragraph direction. If we are at the beginning
5881 of a new paragraph, next_element_from_buffer may not have a
5882 chance to do that. */
5883 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5884 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5885 /* prev_stop can be zero, so check against BEGV as well. */
5886 while (it->bidi_it.charpos >= bob
5887 && it->prev_stop <= it->bidi_it.charpos
5888 && it->bidi_it.charpos < CHARPOS (it->position)
5889 && it->bidi_it.charpos < eob)
5890 bidi_move_to_visually_next (&it->bidi_it);
5891 /* Record the stop_pos we just crossed, for when we cross it
5892 back, maybe. */
5893 if (it->bidi_it.charpos > CHARPOS (it->position))
5894 it->prev_stop = CHARPOS (it->position);
5895 /* If we ended up not where pop_it put us, resync IT's
5896 positional members with the bidi iterator. */
5897 if (it->bidi_it.charpos != CHARPOS (it->position))
5898 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5899 if (buffer_p)
5900 it->current.pos = it->position;
5901 else
5902 it->current.string_pos = it->position;
5903 }
5904
5905 /* Restore IT's settings from IT->stack. Called, for example, when no
5906 more overlay strings must be processed, and we return to delivering
5907 display elements from a buffer, or when the end of a string from a
5908 `display' property is reached and we return to delivering display
5909 elements from an overlay string, or from a buffer. */
5910
5911 static void
5912 pop_it (struct it *it)
5913 {
5914 struct iterator_stack_entry *p;
5915 int from_display_prop = it->from_disp_prop_p;
5916
5917 eassert (it->sp > 0);
5918 --it->sp;
5919 p = it->stack + it->sp;
5920 it->stop_charpos = p->stop_charpos;
5921 it->prev_stop = p->prev_stop;
5922 it->base_level_stop = p->base_level_stop;
5923 it->cmp_it = p->cmp_it;
5924 it->face_id = p->face_id;
5925 it->current = p->current;
5926 it->position = p->position;
5927 it->string = p->string;
5928 it->from_overlay = p->from_overlay;
5929 if (NILP (it->string))
5930 SET_TEXT_POS (it->current.string_pos, -1, -1);
5931 it->method = p->method;
5932 switch (it->method)
5933 {
5934 case GET_FROM_IMAGE:
5935 it->image_id = p->u.image.image_id;
5936 it->object = p->u.image.object;
5937 it->slice = p->u.image.slice;
5938 break;
5939 case GET_FROM_STRETCH:
5940 it->object = p->u.stretch.object;
5941 break;
5942 case GET_FROM_BUFFER:
5943 it->object = it->w->contents;
5944 break;
5945 case GET_FROM_STRING:
5946 it->object = it->string;
5947 break;
5948 case GET_FROM_DISPLAY_VECTOR:
5949 if (it->s)
5950 it->method = GET_FROM_C_STRING;
5951 else if (STRINGP (it->string))
5952 it->method = GET_FROM_STRING;
5953 else
5954 {
5955 it->method = GET_FROM_BUFFER;
5956 it->object = it->w->contents;
5957 }
5958 }
5959 it->end_charpos = p->end_charpos;
5960 it->string_nchars = p->string_nchars;
5961 it->area = p->area;
5962 it->multibyte_p = p->multibyte_p;
5963 it->avoid_cursor_p = p->avoid_cursor_p;
5964 it->space_width = p->space_width;
5965 it->font_height = p->font_height;
5966 it->voffset = p->voffset;
5967 it->string_from_display_prop_p = p->string_from_display_prop_p;
5968 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5969 it->line_wrap = p->line_wrap;
5970 it->bidi_p = p->bidi_p;
5971 it->paragraph_embedding = p->paragraph_embedding;
5972 it->from_disp_prop_p = p->from_disp_prop_p;
5973 if (it->bidi_p)
5974 {
5975 bidi_pop_it (&it->bidi_it);
5976 /* Bidi-iterate until we get out of the portion of text, if any,
5977 covered by a `display' text property or by an overlay with
5978 `display' property. (We cannot just jump there, because the
5979 internal coherency of the bidi iterator state can not be
5980 preserved across such jumps.) We also must determine the
5981 paragraph base direction if the overlay we just processed is
5982 at the beginning of a new paragraph. */
5983 if (from_display_prop
5984 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5985 iterate_out_of_display_property (it);
5986
5987 eassert ((BUFFERP (it->object)
5988 && IT_CHARPOS (*it) == it->bidi_it.charpos
5989 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5990 || (STRINGP (it->object)
5991 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5992 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5993 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5994 }
5995 }
5996
5997
5998 \f
5999 /***********************************************************************
6000 Moving over lines
6001 ***********************************************************************/
6002
6003 /* Set IT's current position to the previous line start. */
6004
6005 static void
6006 back_to_previous_line_start (struct it *it)
6007 {
6008 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6009
6010 DEC_BOTH (cp, bp);
6011 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6012 }
6013
6014
6015 /* Move IT to the next line start.
6016
6017 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6018 we skipped over part of the text (as opposed to moving the iterator
6019 continuously over the text). Otherwise, don't change the value
6020 of *SKIPPED_P.
6021
6022 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6023 iterator on the newline, if it was found.
6024
6025 Newlines may come from buffer text, overlay strings, or strings
6026 displayed via the `display' property. That's the reason we can't
6027 simply use find_newline_no_quit.
6028
6029 Note that this function may not skip over invisible text that is so
6030 because of text properties and immediately follows a newline. If
6031 it would, function reseat_at_next_visible_line_start, when called
6032 from set_iterator_to_next, would effectively make invisible
6033 characters following a newline part of the wrong glyph row, which
6034 leads to wrong cursor motion. */
6035
6036 static int
6037 forward_to_next_line_start (struct it *it, int *skipped_p,
6038 struct bidi_it *bidi_it_prev)
6039 {
6040 ptrdiff_t old_selective;
6041 int newline_found_p, n;
6042 const int MAX_NEWLINE_DISTANCE = 500;
6043
6044 /* If already on a newline, just consume it to avoid unintended
6045 skipping over invisible text below. */
6046 if (it->what == IT_CHARACTER
6047 && it->c == '\n'
6048 && CHARPOS (it->position) == IT_CHARPOS (*it))
6049 {
6050 if (it->bidi_p && bidi_it_prev)
6051 *bidi_it_prev = it->bidi_it;
6052 set_iterator_to_next (it, 0);
6053 it->c = 0;
6054 return 1;
6055 }
6056
6057 /* Don't handle selective display in the following. It's (a)
6058 unnecessary because it's done by the caller, and (b) leads to an
6059 infinite recursion because next_element_from_ellipsis indirectly
6060 calls this function. */
6061 old_selective = it->selective;
6062 it->selective = 0;
6063
6064 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6065 from buffer text. */
6066 for (n = newline_found_p = 0;
6067 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6068 n += STRINGP (it->string) ? 0 : 1)
6069 {
6070 if (!get_next_display_element (it))
6071 return 0;
6072 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6073 if (newline_found_p && it->bidi_p && bidi_it_prev)
6074 *bidi_it_prev = it->bidi_it;
6075 set_iterator_to_next (it, 0);
6076 }
6077
6078 /* If we didn't find a newline near enough, see if we can use a
6079 short-cut. */
6080 if (!newline_found_p)
6081 {
6082 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6083 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6084 1, &bytepos);
6085 Lisp_Object pos;
6086
6087 eassert (!STRINGP (it->string));
6088
6089 /* If there isn't any `display' property in sight, and no
6090 overlays, we can just use the position of the newline in
6091 buffer text. */
6092 if (it->stop_charpos >= limit
6093 || ((pos = Fnext_single_property_change (make_number (start),
6094 Qdisplay, Qnil,
6095 make_number (limit)),
6096 NILP (pos))
6097 && next_overlay_change (start) == ZV))
6098 {
6099 if (!it->bidi_p)
6100 {
6101 IT_CHARPOS (*it) = limit;
6102 IT_BYTEPOS (*it) = bytepos;
6103 }
6104 else
6105 {
6106 struct bidi_it bprev;
6107
6108 /* Help bidi.c avoid expensive searches for display
6109 properties and overlays, by telling it that there are
6110 none up to `limit'. */
6111 if (it->bidi_it.disp_pos < limit)
6112 {
6113 it->bidi_it.disp_pos = limit;
6114 it->bidi_it.disp_prop = 0;
6115 }
6116 do {
6117 bprev = it->bidi_it;
6118 bidi_move_to_visually_next (&it->bidi_it);
6119 } while (it->bidi_it.charpos != limit);
6120 IT_CHARPOS (*it) = limit;
6121 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6122 if (bidi_it_prev)
6123 *bidi_it_prev = bprev;
6124 }
6125 *skipped_p = newline_found_p = 1;
6126 }
6127 else
6128 {
6129 while (get_next_display_element (it)
6130 && !newline_found_p)
6131 {
6132 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6133 if (newline_found_p && it->bidi_p && bidi_it_prev)
6134 *bidi_it_prev = it->bidi_it;
6135 set_iterator_to_next (it, 0);
6136 }
6137 }
6138 }
6139
6140 it->selective = old_selective;
6141 return newline_found_p;
6142 }
6143
6144
6145 /* Set IT's current position to the previous visible line start. Skip
6146 invisible text that is so either due to text properties or due to
6147 selective display. Caution: this does not change IT->current_x and
6148 IT->hpos. */
6149
6150 static void
6151 back_to_previous_visible_line_start (struct it *it)
6152 {
6153 while (IT_CHARPOS (*it) > BEGV)
6154 {
6155 back_to_previous_line_start (it);
6156
6157 if (IT_CHARPOS (*it) <= BEGV)
6158 break;
6159
6160 /* If selective > 0, then lines indented more than its value are
6161 invisible. */
6162 if (it->selective > 0
6163 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6164 it->selective))
6165 continue;
6166
6167 /* Check the newline before point for invisibility. */
6168 {
6169 Lisp_Object prop;
6170 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6171 Qinvisible, it->window);
6172 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6173 continue;
6174 }
6175
6176 if (IT_CHARPOS (*it) <= BEGV)
6177 break;
6178
6179 {
6180 struct it it2;
6181 void *it2data = NULL;
6182 ptrdiff_t pos;
6183 ptrdiff_t beg, end;
6184 Lisp_Object val, overlay;
6185
6186 SAVE_IT (it2, *it, it2data);
6187
6188 /* If newline is part of a composition, continue from start of composition */
6189 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6190 && beg < IT_CHARPOS (*it))
6191 goto replaced;
6192
6193 /* If newline is replaced by a display property, find start of overlay
6194 or interval and continue search from that point. */
6195 pos = --IT_CHARPOS (it2);
6196 --IT_BYTEPOS (it2);
6197 it2.sp = 0;
6198 bidi_unshelve_cache (NULL, 0);
6199 it2.string_from_display_prop_p = 0;
6200 it2.from_disp_prop_p = 0;
6201 if (handle_display_prop (&it2) == HANDLED_RETURN
6202 && !NILP (val = get_char_property_and_overlay
6203 (make_number (pos), Qdisplay, Qnil, &overlay))
6204 && (OVERLAYP (overlay)
6205 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6206 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6207 {
6208 RESTORE_IT (it, it, it2data);
6209 goto replaced;
6210 }
6211
6212 /* Newline is not replaced by anything -- so we are done. */
6213 RESTORE_IT (it, it, it2data);
6214 break;
6215
6216 replaced:
6217 if (beg < BEGV)
6218 beg = BEGV;
6219 IT_CHARPOS (*it) = beg;
6220 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6221 }
6222 }
6223
6224 it->continuation_lines_width = 0;
6225
6226 eassert (IT_CHARPOS (*it) >= BEGV);
6227 eassert (IT_CHARPOS (*it) == BEGV
6228 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6229 CHECK_IT (it);
6230 }
6231
6232
6233 /* Reseat iterator IT at the previous visible line start. Skip
6234 invisible text that is so either due to text properties or due to
6235 selective display. At the end, update IT's overlay information,
6236 face information etc. */
6237
6238 void
6239 reseat_at_previous_visible_line_start (struct it *it)
6240 {
6241 back_to_previous_visible_line_start (it);
6242 reseat (it, it->current.pos, 1);
6243 CHECK_IT (it);
6244 }
6245
6246
6247 /* Reseat iterator IT on the next visible line start in the current
6248 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6249 preceding the line start. Skip over invisible text that is so
6250 because of selective display. Compute faces, overlays etc at the
6251 new position. Note that this function does not skip over text that
6252 is invisible because of text properties. */
6253
6254 static void
6255 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6256 {
6257 int newline_found_p, skipped_p = 0;
6258 struct bidi_it bidi_it_prev;
6259
6260 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6261
6262 /* Skip over lines that are invisible because they are indented
6263 more than the value of IT->selective. */
6264 if (it->selective > 0)
6265 while (IT_CHARPOS (*it) < ZV
6266 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6267 it->selective))
6268 {
6269 eassert (IT_BYTEPOS (*it) == BEGV
6270 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6271 newline_found_p =
6272 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6273 }
6274
6275 /* Position on the newline if that's what's requested. */
6276 if (on_newline_p && newline_found_p)
6277 {
6278 if (STRINGP (it->string))
6279 {
6280 if (IT_STRING_CHARPOS (*it) > 0)
6281 {
6282 if (!it->bidi_p)
6283 {
6284 --IT_STRING_CHARPOS (*it);
6285 --IT_STRING_BYTEPOS (*it);
6286 }
6287 else
6288 {
6289 /* We need to restore the bidi iterator to the state
6290 it had on the newline, and resync the IT's
6291 position with that. */
6292 it->bidi_it = bidi_it_prev;
6293 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6294 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6295 }
6296 }
6297 }
6298 else if (IT_CHARPOS (*it) > BEGV)
6299 {
6300 if (!it->bidi_p)
6301 {
6302 --IT_CHARPOS (*it);
6303 --IT_BYTEPOS (*it);
6304 }
6305 else
6306 {
6307 /* We need to restore the bidi iterator to the state it
6308 had on the newline and resync IT with that. */
6309 it->bidi_it = bidi_it_prev;
6310 IT_CHARPOS (*it) = it->bidi_it.charpos;
6311 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6312 }
6313 reseat (it, it->current.pos, 0);
6314 }
6315 }
6316 else if (skipped_p)
6317 reseat (it, it->current.pos, 0);
6318
6319 CHECK_IT (it);
6320 }
6321
6322
6323 \f
6324 /***********************************************************************
6325 Changing an iterator's position
6326 ***********************************************************************/
6327
6328 /* Change IT's current position to POS in current_buffer. If FORCE_P
6329 is non-zero, always check for text properties at the new position.
6330 Otherwise, text properties are only looked up if POS >=
6331 IT->check_charpos of a property. */
6332
6333 static void
6334 reseat (struct it *it, struct text_pos pos, int force_p)
6335 {
6336 ptrdiff_t original_pos = IT_CHARPOS (*it);
6337
6338 reseat_1 (it, pos, 0);
6339
6340 /* Determine where to check text properties. Avoid doing it
6341 where possible because text property lookup is very expensive. */
6342 if (force_p
6343 || CHARPOS (pos) > it->stop_charpos
6344 || CHARPOS (pos) < original_pos)
6345 {
6346 if (it->bidi_p)
6347 {
6348 /* For bidi iteration, we need to prime prev_stop and
6349 base_level_stop with our best estimations. */
6350 /* Implementation note: Of course, POS is not necessarily a
6351 stop position, so assigning prev_pos to it is a lie; we
6352 should have called compute_stop_backwards. However, if
6353 the current buffer does not include any R2L characters,
6354 that call would be a waste of cycles, because the
6355 iterator will never move back, and thus never cross this
6356 "fake" stop position. So we delay that backward search
6357 until the time we really need it, in next_element_from_buffer. */
6358 if (CHARPOS (pos) != it->prev_stop)
6359 it->prev_stop = CHARPOS (pos);
6360 if (CHARPOS (pos) < it->base_level_stop)
6361 it->base_level_stop = 0; /* meaning it's unknown */
6362 handle_stop (it);
6363 }
6364 else
6365 {
6366 handle_stop (it);
6367 it->prev_stop = it->base_level_stop = 0;
6368 }
6369
6370 }
6371
6372 CHECK_IT (it);
6373 }
6374
6375
6376 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6377 IT->stop_pos to POS, also. */
6378
6379 static void
6380 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6381 {
6382 /* Don't call this function when scanning a C string. */
6383 eassert (it->s == NULL);
6384
6385 /* POS must be a reasonable value. */
6386 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6387
6388 it->current.pos = it->position = pos;
6389 it->end_charpos = ZV;
6390 it->dpvec = NULL;
6391 it->current.dpvec_index = -1;
6392 it->current.overlay_string_index = -1;
6393 IT_STRING_CHARPOS (*it) = -1;
6394 IT_STRING_BYTEPOS (*it) = -1;
6395 it->string = Qnil;
6396 it->method = GET_FROM_BUFFER;
6397 it->object = it->w->contents;
6398 it->area = TEXT_AREA;
6399 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6400 it->sp = 0;
6401 it->string_from_display_prop_p = 0;
6402 it->string_from_prefix_prop_p = 0;
6403
6404 it->from_disp_prop_p = 0;
6405 it->face_before_selective_p = 0;
6406 if (it->bidi_p)
6407 {
6408 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6409 &it->bidi_it);
6410 bidi_unshelve_cache (NULL, 0);
6411 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6412 it->bidi_it.string.s = NULL;
6413 it->bidi_it.string.lstring = Qnil;
6414 it->bidi_it.string.bufpos = 0;
6415 it->bidi_it.string.unibyte = 0;
6416 it->bidi_it.w = it->w;
6417 }
6418
6419 if (set_stop_p)
6420 {
6421 it->stop_charpos = CHARPOS (pos);
6422 it->base_level_stop = CHARPOS (pos);
6423 }
6424 /* This make the information stored in it->cmp_it invalidate. */
6425 it->cmp_it.id = -1;
6426 }
6427
6428
6429 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6430 If S is non-null, it is a C string to iterate over. Otherwise,
6431 STRING gives a Lisp string to iterate over.
6432
6433 If PRECISION > 0, don't return more then PRECISION number of
6434 characters from the string.
6435
6436 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6437 characters have been returned. FIELD_WIDTH < 0 means an infinite
6438 field width.
6439
6440 MULTIBYTE = 0 means disable processing of multibyte characters,
6441 MULTIBYTE > 0 means enable it,
6442 MULTIBYTE < 0 means use IT->multibyte_p.
6443
6444 IT must be initialized via a prior call to init_iterator before
6445 calling this function. */
6446
6447 static void
6448 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6449 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6450 int multibyte)
6451 {
6452 /* No region in strings. */
6453 it->region_beg_charpos = it->region_end_charpos = -1;
6454
6455 /* No text property checks performed by default, but see below. */
6456 it->stop_charpos = -1;
6457
6458 /* Set iterator position and end position. */
6459 memset (&it->current, 0, sizeof it->current);
6460 it->current.overlay_string_index = -1;
6461 it->current.dpvec_index = -1;
6462 eassert (charpos >= 0);
6463
6464 /* If STRING is specified, use its multibyteness, otherwise use the
6465 setting of MULTIBYTE, if specified. */
6466 if (multibyte >= 0)
6467 it->multibyte_p = multibyte > 0;
6468
6469 /* Bidirectional reordering of strings is controlled by the default
6470 value of bidi-display-reordering. Don't try to reorder while
6471 loading loadup.el, as the necessary character property tables are
6472 not yet available. */
6473 it->bidi_p =
6474 NILP (Vpurify_flag)
6475 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6476
6477 if (s == NULL)
6478 {
6479 eassert (STRINGP (string));
6480 it->string = string;
6481 it->s = NULL;
6482 it->end_charpos = it->string_nchars = SCHARS (string);
6483 it->method = GET_FROM_STRING;
6484 it->current.string_pos = string_pos (charpos, string);
6485
6486 if (it->bidi_p)
6487 {
6488 it->bidi_it.string.lstring = string;
6489 it->bidi_it.string.s = NULL;
6490 it->bidi_it.string.schars = it->end_charpos;
6491 it->bidi_it.string.bufpos = 0;
6492 it->bidi_it.string.from_disp_str = 0;
6493 it->bidi_it.string.unibyte = !it->multibyte_p;
6494 it->bidi_it.w = it->w;
6495 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6496 FRAME_WINDOW_P (it->f), &it->bidi_it);
6497 }
6498 }
6499 else
6500 {
6501 it->s = (const unsigned char *) s;
6502 it->string = Qnil;
6503
6504 /* Note that we use IT->current.pos, not it->current.string_pos,
6505 for displaying C strings. */
6506 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6507 if (it->multibyte_p)
6508 {
6509 it->current.pos = c_string_pos (charpos, s, 1);
6510 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6511 }
6512 else
6513 {
6514 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6515 it->end_charpos = it->string_nchars = strlen (s);
6516 }
6517
6518 if (it->bidi_p)
6519 {
6520 it->bidi_it.string.lstring = Qnil;
6521 it->bidi_it.string.s = (const unsigned char *) s;
6522 it->bidi_it.string.schars = it->end_charpos;
6523 it->bidi_it.string.bufpos = 0;
6524 it->bidi_it.string.from_disp_str = 0;
6525 it->bidi_it.string.unibyte = !it->multibyte_p;
6526 it->bidi_it.w = it->w;
6527 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6528 &it->bidi_it);
6529 }
6530 it->method = GET_FROM_C_STRING;
6531 }
6532
6533 /* PRECISION > 0 means don't return more than PRECISION characters
6534 from the string. */
6535 if (precision > 0 && it->end_charpos - charpos > precision)
6536 {
6537 it->end_charpos = it->string_nchars = charpos + precision;
6538 if (it->bidi_p)
6539 it->bidi_it.string.schars = it->end_charpos;
6540 }
6541
6542 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6543 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6544 FIELD_WIDTH < 0 means infinite field width. This is useful for
6545 padding with `-' at the end of a mode line. */
6546 if (field_width < 0)
6547 field_width = INFINITY;
6548 /* Implementation note: We deliberately don't enlarge
6549 it->bidi_it.string.schars here to fit it->end_charpos, because
6550 the bidi iterator cannot produce characters out of thin air. */
6551 if (field_width > it->end_charpos - charpos)
6552 it->end_charpos = charpos + field_width;
6553
6554 /* Use the standard display table for displaying strings. */
6555 if (DISP_TABLE_P (Vstandard_display_table))
6556 it->dp = XCHAR_TABLE (Vstandard_display_table);
6557
6558 it->stop_charpos = charpos;
6559 it->prev_stop = charpos;
6560 it->base_level_stop = 0;
6561 if (it->bidi_p)
6562 {
6563 it->bidi_it.first_elt = 1;
6564 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6565 it->bidi_it.disp_pos = -1;
6566 }
6567 if (s == NULL && it->multibyte_p)
6568 {
6569 ptrdiff_t endpos = SCHARS (it->string);
6570 if (endpos > it->end_charpos)
6571 endpos = it->end_charpos;
6572 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6573 it->string);
6574 }
6575 CHECK_IT (it);
6576 }
6577
6578
6579 \f
6580 /***********************************************************************
6581 Iteration
6582 ***********************************************************************/
6583
6584 /* Map enum it_method value to corresponding next_element_from_* function. */
6585
6586 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6587 {
6588 next_element_from_buffer,
6589 next_element_from_display_vector,
6590 next_element_from_string,
6591 next_element_from_c_string,
6592 next_element_from_image,
6593 next_element_from_stretch
6594 };
6595
6596 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6597
6598
6599 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6600 (possibly with the following characters). */
6601
6602 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6603 ((IT)->cmp_it.id >= 0 \
6604 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6605 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6606 END_CHARPOS, (IT)->w, \
6607 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6608 (IT)->string)))
6609
6610
6611 /* Lookup the char-table Vglyphless_char_display for character C (-1
6612 if we want information for no-font case), and return the display
6613 method symbol. By side-effect, update it->what and
6614 it->glyphless_method. This function is called from
6615 get_next_display_element for each character element, and from
6616 x_produce_glyphs when no suitable font was found. */
6617
6618 Lisp_Object
6619 lookup_glyphless_char_display (int c, struct it *it)
6620 {
6621 Lisp_Object glyphless_method = Qnil;
6622
6623 if (CHAR_TABLE_P (Vglyphless_char_display)
6624 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6625 {
6626 if (c >= 0)
6627 {
6628 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6629 if (CONSP (glyphless_method))
6630 glyphless_method = FRAME_WINDOW_P (it->f)
6631 ? XCAR (glyphless_method)
6632 : XCDR (glyphless_method);
6633 }
6634 else
6635 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6636 }
6637
6638 retry:
6639 if (NILP (glyphless_method))
6640 {
6641 if (c >= 0)
6642 /* The default is to display the character by a proper font. */
6643 return Qnil;
6644 /* The default for the no-font case is to display an empty box. */
6645 glyphless_method = Qempty_box;
6646 }
6647 if (EQ (glyphless_method, Qzero_width))
6648 {
6649 if (c >= 0)
6650 return glyphless_method;
6651 /* This method can't be used for the no-font case. */
6652 glyphless_method = Qempty_box;
6653 }
6654 if (EQ (glyphless_method, Qthin_space))
6655 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6656 else if (EQ (glyphless_method, Qempty_box))
6657 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6658 else if (EQ (glyphless_method, Qhex_code))
6659 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6660 else if (STRINGP (glyphless_method))
6661 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6662 else
6663 {
6664 /* Invalid value. We use the default method. */
6665 glyphless_method = Qnil;
6666 goto retry;
6667 }
6668 it->what = IT_GLYPHLESS;
6669 return glyphless_method;
6670 }
6671
6672 /* Load IT's display element fields with information about the next
6673 display element from the current position of IT. Value is zero if
6674 end of buffer (or C string) is reached. */
6675
6676 static struct frame *last_escape_glyph_frame = NULL;
6677 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6678 static int last_escape_glyph_merged_face_id = 0;
6679
6680 struct frame *last_glyphless_glyph_frame = NULL;
6681 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6682 int last_glyphless_glyph_merged_face_id = 0;
6683
6684 static int
6685 get_next_display_element (struct it *it)
6686 {
6687 /* Non-zero means that we found a display element. Zero means that
6688 we hit the end of what we iterate over. Performance note: the
6689 function pointer `method' used here turns out to be faster than
6690 using a sequence of if-statements. */
6691 int success_p;
6692
6693 get_next:
6694 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6695
6696 if (it->what == IT_CHARACTER)
6697 {
6698 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6699 and only if (a) the resolved directionality of that character
6700 is R..." */
6701 /* FIXME: Do we need an exception for characters from display
6702 tables? */
6703 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6704 it->c = bidi_mirror_char (it->c);
6705 /* Map via display table or translate control characters.
6706 IT->c, IT->len etc. have been set to the next character by
6707 the function call above. If we have a display table, and it
6708 contains an entry for IT->c, translate it. Don't do this if
6709 IT->c itself comes from a display table, otherwise we could
6710 end up in an infinite recursion. (An alternative could be to
6711 count the recursion depth of this function and signal an
6712 error when a certain maximum depth is reached.) Is it worth
6713 it? */
6714 if (success_p && it->dpvec == NULL)
6715 {
6716 Lisp_Object dv;
6717 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6718 int nonascii_space_p = 0;
6719 int nonascii_hyphen_p = 0;
6720 int c = it->c; /* This is the character to display. */
6721
6722 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6723 {
6724 eassert (SINGLE_BYTE_CHAR_P (c));
6725 if (unibyte_display_via_language_environment)
6726 {
6727 c = DECODE_CHAR (unibyte, c);
6728 if (c < 0)
6729 c = BYTE8_TO_CHAR (it->c);
6730 }
6731 else
6732 c = BYTE8_TO_CHAR (it->c);
6733 }
6734
6735 if (it->dp
6736 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6737 VECTORP (dv)))
6738 {
6739 struct Lisp_Vector *v = XVECTOR (dv);
6740
6741 /* Return the first character from the display table
6742 entry, if not empty. If empty, don't display the
6743 current character. */
6744 if (v->header.size)
6745 {
6746 it->dpvec_char_len = it->len;
6747 it->dpvec = v->contents;
6748 it->dpend = v->contents + v->header.size;
6749 it->current.dpvec_index = 0;
6750 it->dpvec_face_id = -1;
6751 it->saved_face_id = it->face_id;
6752 it->method = GET_FROM_DISPLAY_VECTOR;
6753 it->ellipsis_p = 0;
6754 }
6755 else
6756 {
6757 set_iterator_to_next (it, 0);
6758 }
6759 goto get_next;
6760 }
6761
6762 if (! NILP (lookup_glyphless_char_display (c, it)))
6763 {
6764 if (it->what == IT_GLYPHLESS)
6765 goto done;
6766 /* Don't display this character. */
6767 set_iterator_to_next (it, 0);
6768 goto get_next;
6769 }
6770
6771 /* If `nobreak-char-display' is non-nil, we display
6772 non-ASCII spaces and hyphens specially. */
6773 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6774 {
6775 if (c == 0xA0)
6776 nonascii_space_p = 1;
6777 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6778 nonascii_hyphen_p = 1;
6779 }
6780
6781 /* Translate control characters into `\003' or `^C' form.
6782 Control characters coming from a display table entry are
6783 currently not translated because we use IT->dpvec to hold
6784 the translation. This could easily be changed but I
6785 don't believe that it is worth doing.
6786
6787 The characters handled by `nobreak-char-display' must be
6788 translated too.
6789
6790 Non-printable characters and raw-byte characters are also
6791 translated to octal form. */
6792 if (((c < ' ' || c == 127) /* ASCII control chars */
6793 ? (it->area != TEXT_AREA
6794 /* In mode line, treat \n, \t like other crl chars. */
6795 || (c != '\t'
6796 && it->glyph_row
6797 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6798 || (c != '\n' && c != '\t'))
6799 : (nonascii_space_p
6800 || nonascii_hyphen_p
6801 || CHAR_BYTE8_P (c)
6802 || ! CHAR_PRINTABLE_P (c))))
6803 {
6804 /* C is a control character, non-ASCII space/hyphen,
6805 raw-byte, or a non-printable character which must be
6806 displayed either as '\003' or as `^C' where the '\\'
6807 and '^' can be defined in the display table. Fill
6808 IT->ctl_chars with glyphs for what we have to
6809 display. Then, set IT->dpvec to these glyphs. */
6810 Lisp_Object gc;
6811 int ctl_len;
6812 int face_id;
6813 int lface_id = 0;
6814 int escape_glyph;
6815
6816 /* Handle control characters with ^. */
6817
6818 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6819 {
6820 int g;
6821
6822 g = '^'; /* default glyph for Control */
6823 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6824 if (it->dp
6825 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6826 {
6827 g = GLYPH_CODE_CHAR (gc);
6828 lface_id = GLYPH_CODE_FACE (gc);
6829 }
6830 if (lface_id)
6831 {
6832 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6833 }
6834 else if (it->f == last_escape_glyph_frame
6835 && it->face_id == last_escape_glyph_face_id)
6836 {
6837 face_id = last_escape_glyph_merged_face_id;
6838 }
6839 else
6840 {
6841 /* Merge the escape-glyph face into the current face. */
6842 face_id = merge_faces (it->f, Qescape_glyph, 0,
6843 it->face_id);
6844 last_escape_glyph_frame = it->f;
6845 last_escape_glyph_face_id = it->face_id;
6846 last_escape_glyph_merged_face_id = face_id;
6847 }
6848
6849 XSETINT (it->ctl_chars[0], g);
6850 XSETINT (it->ctl_chars[1], c ^ 0100);
6851 ctl_len = 2;
6852 goto display_control;
6853 }
6854
6855 /* Handle non-ascii space in the mode where it only gets
6856 highlighting. */
6857
6858 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6859 {
6860 /* Merge `nobreak-space' into the current face. */
6861 face_id = merge_faces (it->f, Qnobreak_space, 0,
6862 it->face_id);
6863 XSETINT (it->ctl_chars[0], ' ');
6864 ctl_len = 1;
6865 goto display_control;
6866 }
6867
6868 /* Handle sequences that start with the "escape glyph". */
6869
6870 /* the default escape glyph is \. */
6871 escape_glyph = '\\';
6872
6873 if (it->dp
6874 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6875 {
6876 escape_glyph = GLYPH_CODE_CHAR (gc);
6877 lface_id = GLYPH_CODE_FACE (gc);
6878 }
6879 if (lface_id)
6880 {
6881 /* The display table specified a face.
6882 Merge it into face_id and also into escape_glyph. */
6883 face_id = merge_faces (it->f, Qt, lface_id,
6884 it->face_id);
6885 }
6886 else if (it->f == last_escape_glyph_frame
6887 && it->face_id == last_escape_glyph_face_id)
6888 {
6889 face_id = last_escape_glyph_merged_face_id;
6890 }
6891 else
6892 {
6893 /* Merge the escape-glyph face into the current face. */
6894 face_id = merge_faces (it->f, Qescape_glyph, 0,
6895 it->face_id);
6896 last_escape_glyph_frame = it->f;
6897 last_escape_glyph_face_id = it->face_id;
6898 last_escape_glyph_merged_face_id = face_id;
6899 }
6900
6901 /* Draw non-ASCII hyphen with just highlighting: */
6902
6903 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6904 {
6905 XSETINT (it->ctl_chars[0], '-');
6906 ctl_len = 1;
6907 goto display_control;
6908 }
6909
6910 /* Draw non-ASCII space/hyphen with escape glyph: */
6911
6912 if (nonascii_space_p || nonascii_hyphen_p)
6913 {
6914 XSETINT (it->ctl_chars[0], escape_glyph);
6915 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6916 ctl_len = 2;
6917 goto display_control;
6918 }
6919
6920 {
6921 char str[10];
6922 int len, i;
6923
6924 if (CHAR_BYTE8_P (c))
6925 /* Display \200 instead of \17777600. */
6926 c = CHAR_TO_BYTE8 (c);
6927 len = sprintf (str, "%03o", c);
6928
6929 XSETINT (it->ctl_chars[0], escape_glyph);
6930 for (i = 0; i < len; i++)
6931 XSETINT (it->ctl_chars[i + 1], str[i]);
6932 ctl_len = len + 1;
6933 }
6934
6935 display_control:
6936 /* Set up IT->dpvec and return first character from it. */
6937 it->dpvec_char_len = it->len;
6938 it->dpvec = it->ctl_chars;
6939 it->dpend = it->dpvec + ctl_len;
6940 it->current.dpvec_index = 0;
6941 it->dpvec_face_id = face_id;
6942 it->saved_face_id = it->face_id;
6943 it->method = GET_FROM_DISPLAY_VECTOR;
6944 it->ellipsis_p = 0;
6945 goto get_next;
6946 }
6947 it->char_to_display = c;
6948 }
6949 else if (success_p)
6950 {
6951 it->char_to_display = it->c;
6952 }
6953 }
6954
6955 /* Adjust face id for a multibyte character. There are no multibyte
6956 character in unibyte text. */
6957 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6958 && it->multibyte_p
6959 && success_p
6960 && FRAME_WINDOW_P (it->f))
6961 {
6962 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6963
6964 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6965 {
6966 /* Automatic composition with glyph-string. */
6967 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6968
6969 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6970 }
6971 else
6972 {
6973 ptrdiff_t pos = (it->s ? -1
6974 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6975 : IT_CHARPOS (*it));
6976 int c;
6977
6978 if (it->what == IT_CHARACTER)
6979 c = it->char_to_display;
6980 else
6981 {
6982 struct composition *cmp = composition_table[it->cmp_it.id];
6983 int i;
6984
6985 c = ' ';
6986 for (i = 0; i < cmp->glyph_len; i++)
6987 /* TAB in a composition means display glyphs with
6988 padding space on the left or right. */
6989 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6990 break;
6991 }
6992 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6993 }
6994 }
6995
6996 done:
6997 /* Is this character the last one of a run of characters with
6998 box? If yes, set IT->end_of_box_run_p to 1. */
6999 if (it->face_box_p
7000 && it->s == NULL)
7001 {
7002 if (it->method == GET_FROM_STRING && it->sp)
7003 {
7004 int face_id = underlying_face_id (it);
7005 struct face *face = FACE_FROM_ID (it->f, face_id);
7006
7007 if (face)
7008 {
7009 if (face->box == FACE_NO_BOX)
7010 {
7011 /* If the box comes from face properties in a
7012 display string, check faces in that string. */
7013 int string_face_id = face_after_it_pos (it);
7014 it->end_of_box_run_p
7015 = (FACE_FROM_ID (it->f, string_face_id)->box
7016 == FACE_NO_BOX);
7017 }
7018 /* Otherwise, the box comes from the underlying face.
7019 If this is the last string character displayed, check
7020 the next buffer location. */
7021 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7022 && (it->current.overlay_string_index
7023 == it->n_overlay_strings - 1))
7024 {
7025 ptrdiff_t ignore;
7026 int next_face_id;
7027 struct text_pos pos = it->current.pos;
7028 INC_TEXT_POS (pos, it->multibyte_p);
7029
7030 next_face_id = face_at_buffer_position
7031 (it->w, CHARPOS (pos), it->region_beg_charpos,
7032 it->region_end_charpos, &ignore,
7033 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7034 -1);
7035 it->end_of_box_run_p
7036 = (FACE_FROM_ID (it->f, next_face_id)->box
7037 == FACE_NO_BOX);
7038 }
7039 }
7040 }
7041 else
7042 {
7043 int face_id = face_after_it_pos (it);
7044 it->end_of_box_run_p
7045 = (face_id != it->face_id
7046 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7047 }
7048 }
7049 /* If we reached the end of the object we've been iterating (e.g., a
7050 display string or an overlay string), and there's something on
7051 IT->stack, proceed with what's on the stack. It doesn't make
7052 sense to return zero if there's unprocessed stuff on the stack,
7053 because otherwise that stuff will never be displayed. */
7054 if (!success_p && it->sp > 0)
7055 {
7056 set_iterator_to_next (it, 0);
7057 success_p = get_next_display_element (it);
7058 }
7059
7060 /* Value is 0 if end of buffer or string reached. */
7061 return success_p;
7062 }
7063
7064
7065 /* Move IT to the next display element.
7066
7067 RESEAT_P non-zero means if called on a newline in buffer text,
7068 skip to the next visible line start.
7069
7070 Functions get_next_display_element and set_iterator_to_next are
7071 separate because I find this arrangement easier to handle than a
7072 get_next_display_element function that also increments IT's
7073 position. The way it is we can first look at an iterator's current
7074 display element, decide whether it fits on a line, and if it does,
7075 increment the iterator position. The other way around we probably
7076 would either need a flag indicating whether the iterator has to be
7077 incremented the next time, or we would have to implement a
7078 decrement position function which would not be easy to write. */
7079
7080 void
7081 set_iterator_to_next (struct it *it, int reseat_p)
7082 {
7083 /* Reset flags indicating start and end of a sequence of characters
7084 with box. Reset them at the start of this function because
7085 moving the iterator to a new position might set them. */
7086 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7087
7088 switch (it->method)
7089 {
7090 case GET_FROM_BUFFER:
7091 /* The current display element of IT is a character from
7092 current_buffer. Advance in the buffer, and maybe skip over
7093 invisible lines that are so because of selective display. */
7094 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7095 reseat_at_next_visible_line_start (it, 0);
7096 else if (it->cmp_it.id >= 0)
7097 {
7098 /* We are currently getting glyphs from a composition. */
7099 int i;
7100
7101 if (! it->bidi_p)
7102 {
7103 IT_CHARPOS (*it) += it->cmp_it.nchars;
7104 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7105 if (it->cmp_it.to < it->cmp_it.nglyphs)
7106 {
7107 it->cmp_it.from = it->cmp_it.to;
7108 }
7109 else
7110 {
7111 it->cmp_it.id = -1;
7112 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7113 IT_BYTEPOS (*it),
7114 it->end_charpos, Qnil);
7115 }
7116 }
7117 else if (! it->cmp_it.reversed_p)
7118 {
7119 /* Composition created while scanning forward. */
7120 /* Update IT's char/byte positions to point to the first
7121 character of the next grapheme cluster, or to the
7122 character visually after the current composition. */
7123 for (i = 0; i < it->cmp_it.nchars; i++)
7124 bidi_move_to_visually_next (&it->bidi_it);
7125 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7126 IT_CHARPOS (*it) = it->bidi_it.charpos;
7127
7128 if (it->cmp_it.to < it->cmp_it.nglyphs)
7129 {
7130 /* Proceed to the next grapheme cluster. */
7131 it->cmp_it.from = it->cmp_it.to;
7132 }
7133 else
7134 {
7135 /* No more grapheme clusters in this composition.
7136 Find the next stop position. */
7137 ptrdiff_t stop = it->end_charpos;
7138 if (it->bidi_it.scan_dir < 0)
7139 /* Now we are scanning backward and don't know
7140 where to stop. */
7141 stop = -1;
7142 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7143 IT_BYTEPOS (*it), stop, Qnil);
7144 }
7145 }
7146 else
7147 {
7148 /* Composition created while scanning backward. */
7149 /* Update IT's char/byte positions to point to the last
7150 character of the previous grapheme cluster, or the
7151 character visually after the current composition. */
7152 for (i = 0; i < it->cmp_it.nchars; i++)
7153 bidi_move_to_visually_next (&it->bidi_it);
7154 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7155 IT_CHARPOS (*it) = it->bidi_it.charpos;
7156 if (it->cmp_it.from > 0)
7157 {
7158 /* Proceed to the previous grapheme cluster. */
7159 it->cmp_it.to = it->cmp_it.from;
7160 }
7161 else
7162 {
7163 /* No more grapheme clusters in this composition.
7164 Find the next stop position. */
7165 ptrdiff_t stop = it->end_charpos;
7166 if (it->bidi_it.scan_dir < 0)
7167 /* Now we are scanning backward and don't know
7168 where to stop. */
7169 stop = -1;
7170 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7171 IT_BYTEPOS (*it), stop, Qnil);
7172 }
7173 }
7174 }
7175 else
7176 {
7177 eassert (it->len != 0);
7178
7179 if (!it->bidi_p)
7180 {
7181 IT_BYTEPOS (*it) += it->len;
7182 IT_CHARPOS (*it) += 1;
7183 }
7184 else
7185 {
7186 int prev_scan_dir = it->bidi_it.scan_dir;
7187 /* If this is a new paragraph, determine its base
7188 direction (a.k.a. its base embedding level). */
7189 if (it->bidi_it.new_paragraph)
7190 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7191 bidi_move_to_visually_next (&it->bidi_it);
7192 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7193 IT_CHARPOS (*it) = it->bidi_it.charpos;
7194 if (prev_scan_dir != it->bidi_it.scan_dir)
7195 {
7196 /* As the scan direction was changed, we must
7197 re-compute the stop position for composition. */
7198 ptrdiff_t stop = it->end_charpos;
7199 if (it->bidi_it.scan_dir < 0)
7200 stop = -1;
7201 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7202 IT_BYTEPOS (*it), stop, Qnil);
7203 }
7204 }
7205 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7206 }
7207 break;
7208
7209 case GET_FROM_C_STRING:
7210 /* Current display element of IT is from a C string. */
7211 if (!it->bidi_p
7212 /* If the string position is beyond string's end, it means
7213 next_element_from_c_string is padding the string with
7214 blanks, in which case we bypass the bidi iterator,
7215 because it cannot deal with such virtual characters. */
7216 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7217 {
7218 IT_BYTEPOS (*it) += it->len;
7219 IT_CHARPOS (*it) += 1;
7220 }
7221 else
7222 {
7223 bidi_move_to_visually_next (&it->bidi_it);
7224 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7225 IT_CHARPOS (*it) = it->bidi_it.charpos;
7226 }
7227 break;
7228
7229 case GET_FROM_DISPLAY_VECTOR:
7230 /* Current display element of IT is from a display table entry.
7231 Advance in the display table definition. Reset it to null if
7232 end reached, and continue with characters from buffers/
7233 strings. */
7234 ++it->current.dpvec_index;
7235
7236 /* Restore face of the iterator to what they were before the
7237 display vector entry (these entries may contain faces). */
7238 it->face_id = it->saved_face_id;
7239
7240 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7241 {
7242 int recheck_faces = it->ellipsis_p;
7243
7244 if (it->s)
7245 it->method = GET_FROM_C_STRING;
7246 else if (STRINGP (it->string))
7247 it->method = GET_FROM_STRING;
7248 else
7249 {
7250 it->method = GET_FROM_BUFFER;
7251 it->object = it->w->contents;
7252 }
7253
7254 it->dpvec = NULL;
7255 it->current.dpvec_index = -1;
7256
7257 /* Skip over characters which were displayed via IT->dpvec. */
7258 if (it->dpvec_char_len < 0)
7259 reseat_at_next_visible_line_start (it, 1);
7260 else if (it->dpvec_char_len > 0)
7261 {
7262 if (it->method == GET_FROM_STRING
7263 && it->current.overlay_string_index >= 0
7264 && it->n_overlay_strings > 0)
7265 it->ignore_overlay_strings_at_pos_p = 1;
7266 it->len = it->dpvec_char_len;
7267 set_iterator_to_next (it, reseat_p);
7268 }
7269
7270 /* Maybe recheck faces after display vector */
7271 if (recheck_faces)
7272 it->stop_charpos = IT_CHARPOS (*it);
7273 }
7274 break;
7275
7276 case GET_FROM_STRING:
7277 /* Current display element is a character from a Lisp string. */
7278 eassert (it->s == NULL && STRINGP (it->string));
7279 /* Don't advance past string end. These conditions are true
7280 when set_iterator_to_next is called at the end of
7281 get_next_display_element, in which case the Lisp string is
7282 already exhausted, and all we want is pop the iterator
7283 stack. */
7284 if (it->current.overlay_string_index >= 0)
7285 {
7286 /* This is an overlay string, so there's no padding with
7287 spaces, and the number of characters in the string is
7288 where the string ends. */
7289 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7290 goto consider_string_end;
7291 }
7292 else
7293 {
7294 /* Not an overlay string. There could be padding, so test
7295 against it->end_charpos . */
7296 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7297 goto consider_string_end;
7298 }
7299 if (it->cmp_it.id >= 0)
7300 {
7301 int i;
7302
7303 if (! it->bidi_p)
7304 {
7305 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7306 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7307 if (it->cmp_it.to < it->cmp_it.nglyphs)
7308 it->cmp_it.from = it->cmp_it.to;
7309 else
7310 {
7311 it->cmp_it.id = -1;
7312 composition_compute_stop_pos (&it->cmp_it,
7313 IT_STRING_CHARPOS (*it),
7314 IT_STRING_BYTEPOS (*it),
7315 it->end_charpos, it->string);
7316 }
7317 }
7318 else if (! it->cmp_it.reversed_p)
7319 {
7320 for (i = 0; i < it->cmp_it.nchars; i++)
7321 bidi_move_to_visually_next (&it->bidi_it);
7322 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7323 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7324
7325 if (it->cmp_it.to < it->cmp_it.nglyphs)
7326 it->cmp_it.from = it->cmp_it.to;
7327 else
7328 {
7329 ptrdiff_t stop = it->end_charpos;
7330 if (it->bidi_it.scan_dir < 0)
7331 stop = -1;
7332 composition_compute_stop_pos (&it->cmp_it,
7333 IT_STRING_CHARPOS (*it),
7334 IT_STRING_BYTEPOS (*it), stop,
7335 it->string);
7336 }
7337 }
7338 else
7339 {
7340 for (i = 0; i < it->cmp_it.nchars; i++)
7341 bidi_move_to_visually_next (&it->bidi_it);
7342 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7343 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7344 if (it->cmp_it.from > 0)
7345 it->cmp_it.to = it->cmp_it.from;
7346 else
7347 {
7348 ptrdiff_t stop = it->end_charpos;
7349 if (it->bidi_it.scan_dir < 0)
7350 stop = -1;
7351 composition_compute_stop_pos (&it->cmp_it,
7352 IT_STRING_CHARPOS (*it),
7353 IT_STRING_BYTEPOS (*it), stop,
7354 it->string);
7355 }
7356 }
7357 }
7358 else
7359 {
7360 if (!it->bidi_p
7361 /* If the string position is beyond string's end, it
7362 means next_element_from_string is padding the string
7363 with blanks, in which case we bypass the bidi
7364 iterator, because it cannot deal with such virtual
7365 characters. */
7366 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7367 {
7368 IT_STRING_BYTEPOS (*it) += it->len;
7369 IT_STRING_CHARPOS (*it) += 1;
7370 }
7371 else
7372 {
7373 int prev_scan_dir = it->bidi_it.scan_dir;
7374
7375 bidi_move_to_visually_next (&it->bidi_it);
7376 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7377 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7378 if (prev_scan_dir != it->bidi_it.scan_dir)
7379 {
7380 ptrdiff_t stop = it->end_charpos;
7381
7382 if (it->bidi_it.scan_dir < 0)
7383 stop = -1;
7384 composition_compute_stop_pos (&it->cmp_it,
7385 IT_STRING_CHARPOS (*it),
7386 IT_STRING_BYTEPOS (*it), stop,
7387 it->string);
7388 }
7389 }
7390 }
7391
7392 consider_string_end:
7393
7394 if (it->current.overlay_string_index >= 0)
7395 {
7396 /* IT->string is an overlay string. Advance to the
7397 next, if there is one. */
7398 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7399 {
7400 it->ellipsis_p = 0;
7401 next_overlay_string (it);
7402 if (it->ellipsis_p)
7403 setup_for_ellipsis (it, 0);
7404 }
7405 }
7406 else
7407 {
7408 /* IT->string is not an overlay string. If we reached
7409 its end, and there is something on IT->stack, proceed
7410 with what is on the stack. This can be either another
7411 string, this time an overlay string, or a buffer. */
7412 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7413 && it->sp > 0)
7414 {
7415 pop_it (it);
7416 if (it->method == GET_FROM_STRING)
7417 goto consider_string_end;
7418 }
7419 }
7420 break;
7421
7422 case GET_FROM_IMAGE:
7423 case GET_FROM_STRETCH:
7424 /* The position etc with which we have to proceed are on
7425 the stack. The position may be at the end of a string,
7426 if the `display' property takes up the whole string. */
7427 eassert (it->sp > 0);
7428 pop_it (it);
7429 if (it->method == GET_FROM_STRING)
7430 goto consider_string_end;
7431 break;
7432
7433 default:
7434 /* There are no other methods defined, so this should be a bug. */
7435 emacs_abort ();
7436 }
7437
7438 eassert (it->method != GET_FROM_STRING
7439 || (STRINGP (it->string)
7440 && IT_STRING_CHARPOS (*it) >= 0));
7441 }
7442
7443 /* Load IT's display element fields with information about the next
7444 display element which comes from a display table entry or from the
7445 result of translating a control character to one of the forms `^C'
7446 or `\003'.
7447
7448 IT->dpvec holds the glyphs to return as characters.
7449 IT->saved_face_id holds the face id before the display vector--it
7450 is restored into IT->face_id in set_iterator_to_next. */
7451
7452 static int
7453 next_element_from_display_vector (struct it *it)
7454 {
7455 Lisp_Object gc;
7456
7457 /* Precondition. */
7458 eassert (it->dpvec && it->current.dpvec_index >= 0);
7459
7460 it->face_id = it->saved_face_id;
7461
7462 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7463 That seemed totally bogus - so I changed it... */
7464 gc = it->dpvec[it->current.dpvec_index];
7465
7466 if (GLYPH_CODE_P (gc))
7467 {
7468 it->c = GLYPH_CODE_CHAR (gc);
7469 it->len = CHAR_BYTES (it->c);
7470
7471 /* The entry may contain a face id to use. Such a face id is
7472 the id of a Lisp face, not a realized face. A face id of
7473 zero means no face is specified. */
7474 if (it->dpvec_face_id >= 0)
7475 it->face_id = it->dpvec_face_id;
7476 else
7477 {
7478 int lface_id = GLYPH_CODE_FACE (gc);
7479 if (lface_id > 0)
7480 it->face_id = merge_faces (it->f, Qt, lface_id,
7481 it->saved_face_id);
7482 }
7483 }
7484 else
7485 /* Display table entry is invalid. Return a space. */
7486 it->c = ' ', it->len = 1;
7487
7488 /* Don't change position and object of the iterator here. They are
7489 still the values of the character that had this display table
7490 entry or was translated, and that's what we want. */
7491 it->what = IT_CHARACTER;
7492 return 1;
7493 }
7494
7495 /* Get the first element of string/buffer in the visual order, after
7496 being reseated to a new position in a string or a buffer. */
7497 static void
7498 get_visually_first_element (struct it *it)
7499 {
7500 int string_p = STRINGP (it->string) || it->s;
7501 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7502 ptrdiff_t bob = (string_p ? 0 : BEGV);
7503
7504 if (STRINGP (it->string))
7505 {
7506 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7507 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7508 }
7509 else
7510 {
7511 it->bidi_it.charpos = IT_CHARPOS (*it);
7512 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7513 }
7514
7515 if (it->bidi_it.charpos == eob)
7516 {
7517 /* Nothing to do, but reset the FIRST_ELT flag, like
7518 bidi_paragraph_init does, because we are not going to
7519 call it. */
7520 it->bidi_it.first_elt = 0;
7521 }
7522 else if (it->bidi_it.charpos == bob
7523 || (!string_p
7524 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7525 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7526 {
7527 /* If we are at the beginning of a line/string, we can produce
7528 the next element right away. */
7529 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7530 bidi_move_to_visually_next (&it->bidi_it);
7531 }
7532 else
7533 {
7534 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7535
7536 /* We need to prime the bidi iterator starting at the line's or
7537 string's beginning, before we will be able to produce the
7538 next element. */
7539 if (string_p)
7540 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7541 else
7542 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7543 IT_BYTEPOS (*it), -1,
7544 &it->bidi_it.bytepos);
7545 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7546 do
7547 {
7548 /* Now return to buffer/string position where we were asked
7549 to get the next display element, and produce that. */
7550 bidi_move_to_visually_next (&it->bidi_it);
7551 }
7552 while (it->bidi_it.bytepos != orig_bytepos
7553 && it->bidi_it.charpos < eob);
7554 }
7555
7556 /* Adjust IT's position information to where we ended up. */
7557 if (STRINGP (it->string))
7558 {
7559 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7560 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7561 }
7562 else
7563 {
7564 IT_CHARPOS (*it) = it->bidi_it.charpos;
7565 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7566 }
7567
7568 if (STRINGP (it->string) || !it->s)
7569 {
7570 ptrdiff_t stop, charpos, bytepos;
7571
7572 if (STRINGP (it->string))
7573 {
7574 eassert (!it->s);
7575 stop = SCHARS (it->string);
7576 if (stop > it->end_charpos)
7577 stop = it->end_charpos;
7578 charpos = IT_STRING_CHARPOS (*it);
7579 bytepos = IT_STRING_BYTEPOS (*it);
7580 }
7581 else
7582 {
7583 stop = it->end_charpos;
7584 charpos = IT_CHARPOS (*it);
7585 bytepos = IT_BYTEPOS (*it);
7586 }
7587 if (it->bidi_it.scan_dir < 0)
7588 stop = -1;
7589 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7590 it->string);
7591 }
7592 }
7593
7594 /* Load IT with the next display element from Lisp string IT->string.
7595 IT->current.string_pos is the current position within the string.
7596 If IT->current.overlay_string_index >= 0, the Lisp string is an
7597 overlay string. */
7598
7599 static int
7600 next_element_from_string (struct it *it)
7601 {
7602 struct text_pos position;
7603
7604 eassert (STRINGP (it->string));
7605 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7606 eassert (IT_STRING_CHARPOS (*it) >= 0);
7607 position = it->current.string_pos;
7608
7609 /* With bidi reordering, the character to display might not be the
7610 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7611 that we were reseat()ed to a new string, whose paragraph
7612 direction is not known. */
7613 if (it->bidi_p && it->bidi_it.first_elt)
7614 {
7615 get_visually_first_element (it);
7616 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7617 }
7618
7619 /* Time to check for invisible text? */
7620 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7621 {
7622 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7623 {
7624 if (!(!it->bidi_p
7625 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7626 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7627 {
7628 /* With bidi non-linear iteration, we could find
7629 ourselves far beyond the last computed stop_charpos,
7630 with several other stop positions in between that we
7631 missed. Scan them all now, in buffer's logical
7632 order, until we find and handle the last stop_charpos
7633 that precedes our current position. */
7634 handle_stop_backwards (it, it->stop_charpos);
7635 return GET_NEXT_DISPLAY_ELEMENT (it);
7636 }
7637 else
7638 {
7639 if (it->bidi_p)
7640 {
7641 /* Take note of the stop position we just moved
7642 across, for when we will move back across it. */
7643 it->prev_stop = it->stop_charpos;
7644 /* If we are at base paragraph embedding level, take
7645 note of the last stop position seen at this
7646 level. */
7647 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7648 it->base_level_stop = it->stop_charpos;
7649 }
7650 handle_stop (it);
7651
7652 /* Since a handler may have changed IT->method, we must
7653 recurse here. */
7654 return GET_NEXT_DISPLAY_ELEMENT (it);
7655 }
7656 }
7657 else if (it->bidi_p
7658 /* If we are before prev_stop, we may have overstepped
7659 on our way backwards a stop_pos, and if so, we need
7660 to handle that stop_pos. */
7661 && IT_STRING_CHARPOS (*it) < it->prev_stop
7662 /* We can sometimes back up for reasons that have nothing
7663 to do with bidi reordering. E.g., compositions. The
7664 code below is only needed when we are above the base
7665 embedding level, so test for that explicitly. */
7666 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7667 {
7668 /* If we lost track of base_level_stop, we have no better
7669 place for handle_stop_backwards to start from than string
7670 beginning. This happens, e.g., when we were reseated to
7671 the previous screenful of text by vertical-motion. */
7672 if (it->base_level_stop <= 0
7673 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7674 it->base_level_stop = 0;
7675 handle_stop_backwards (it, it->base_level_stop);
7676 return GET_NEXT_DISPLAY_ELEMENT (it);
7677 }
7678 }
7679
7680 if (it->current.overlay_string_index >= 0)
7681 {
7682 /* Get the next character from an overlay string. In overlay
7683 strings, there is no field width or padding with spaces to
7684 do. */
7685 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7686 {
7687 it->what = IT_EOB;
7688 return 0;
7689 }
7690 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7691 IT_STRING_BYTEPOS (*it),
7692 it->bidi_it.scan_dir < 0
7693 ? -1
7694 : SCHARS (it->string))
7695 && next_element_from_composition (it))
7696 {
7697 return 1;
7698 }
7699 else if (STRING_MULTIBYTE (it->string))
7700 {
7701 const unsigned char *s = (SDATA (it->string)
7702 + IT_STRING_BYTEPOS (*it));
7703 it->c = string_char_and_length (s, &it->len);
7704 }
7705 else
7706 {
7707 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7708 it->len = 1;
7709 }
7710 }
7711 else
7712 {
7713 /* Get the next character from a Lisp string that is not an
7714 overlay string. Such strings come from the mode line, for
7715 example. We may have to pad with spaces, or truncate the
7716 string. See also next_element_from_c_string. */
7717 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7718 {
7719 it->what = IT_EOB;
7720 return 0;
7721 }
7722 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7723 {
7724 /* Pad with spaces. */
7725 it->c = ' ', it->len = 1;
7726 CHARPOS (position) = BYTEPOS (position) = -1;
7727 }
7728 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7729 IT_STRING_BYTEPOS (*it),
7730 it->bidi_it.scan_dir < 0
7731 ? -1
7732 : it->string_nchars)
7733 && next_element_from_composition (it))
7734 {
7735 return 1;
7736 }
7737 else if (STRING_MULTIBYTE (it->string))
7738 {
7739 const unsigned char *s = (SDATA (it->string)
7740 + IT_STRING_BYTEPOS (*it));
7741 it->c = string_char_and_length (s, &it->len);
7742 }
7743 else
7744 {
7745 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7746 it->len = 1;
7747 }
7748 }
7749
7750 /* Record what we have and where it came from. */
7751 it->what = IT_CHARACTER;
7752 it->object = it->string;
7753 it->position = position;
7754 return 1;
7755 }
7756
7757
7758 /* Load IT with next display element from C string IT->s.
7759 IT->string_nchars is the maximum number of characters to return
7760 from the string. IT->end_charpos may be greater than
7761 IT->string_nchars when this function is called, in which case we
7762 may have to return padding spaces. Value is zero if end of string
7763 reached, including padding spaces. */
7764
7765 static int
7766 next_element_from_c_string (struct it *it)
7767 {
7768 int success_p = 1;
7769
7770 eassert (it->s);
7771 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7772 it->what = IT_CHARACTER;
7773 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7774 it->object = Qnil;
7775
7776 /* With bidi reordering, the character to display might not be the
7777 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7778 we were reseated to a new string, whose paragraph direction is
7779 not known. */
7780 if (it->bidi_p && it->bidi_it.first_elt)
7781 get_visually_first_element (it);
7782
7783 /* IT's position can be greater than IT->string_nchars in case a
7784 field width or precision has been specified when the iterator was
7785 initialized. */
7786 if (IT_CHARPOS (*it) >= it->end_charpos)
7787 {
7788 /* End of the game. */
7789 it->what = IT_EOB;
7790 success_p = 0;
7791 }
7792 else if (IT_CHARPOS (*it) >= it->string_nchars)
7793 {
7794 /* Pad with spaces. */
7795 it->c = ' ', it->len = 1;
7796 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7797 }
7798 else if (it->multibyte_p)
7799 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7800 else
7801 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7802
7803 return success_p;
7804 }
7805
7806
7807 /* Set up IT to return characters from an ellipsis, if appropriate.
7808 The definition of the ellipsis glyphs may come from a display table
7809 entry. This function fills IT with the first glyph from the
7810 ellipsis if an ellipsis is to be displayed. */
7811
7812 static int
7813 next_element_from_ellipsis (struct it *it)
7814 {
7815 if (it->selective_display_ellipsis_p)
7816 setup_for_ellipsis (it, it->len);
7817 else
7818 {
7819 /* The face at the current position may be different from the
7820 face we find after the invisible text. Remember what it
7821 was in IT->saved_face_id, and signal that it's there by
7822 setting face_before_selective_p. */
7823 it->saved_face_id = it->face_id;
7824 it->method = GET_FROM_BUFFER;
7825 it->object = it->w->contents;
7826 reseat_at_next_visible_line_start (it, 1);
7827 it->face_before_selective_p = 1;
7828 }
7829
7830 return GET_NEXT_DISPLAY_ELEMENT (it);
7831 }
7832
7833
7834 /* Deliver an image display element. The iterator IT is already
7835 filled with image information (done in handle_display_prop). Value
7836 is always 1. */
7837
7838
7839 static int
7840 next_element_from_image (struct it *it)
7841 {
7842 it->what = IT_IMAGE;
7843 it->ignore_overlay_strings_at_pos_p = 0;
7844 return 1;
7845 }
7846
7847
7848 /* Fill iterator IT with next display element from a stretch glyph
7849 property. IT->object is the value of the text property. Value is
7850 always 1. */
7851
7852 static int
7853 next_element_from_stretch (struct it *it)
7854 {
7855 it->what = IT_STRETCH;
7856 return 1;
7857 }
7858
7859 /* Scan backwards from IT's current position until we find a stop
7860 position, or until BEGV. This is called when we find ourself
7861 before both the last known prev_stop and base_level_stop while
7862 reordering bidirectional text. */
7863
7864 static void
7865 compute_stop_pos_backwards (struct it *it)
7866 {
7867 const int SCAN_BACK_LIMIT = 1000;
7868 struct text_pos pos;
7869 struct display_pos save_current = it->current;
7870 struct text_pos save_position = it->position;
7871 ptrdiff_t charpos = IT_CHARPOS (*it);
7872 ptrdiff_t where_we_are = charpos;
7873 ptrdiff_t save_stop_pos = it->stop_charpos;
7874 ptrdiff_t save_end_pos = it->end_charpos;
7875
7876 eassert (NILP (it->string) && !it->s);
7877 eassert (it->bidi_p);
7878 it->bidi_p = 0;
7879 do
7880 {
7881 it->end_charpos = min (charpos + 1, ZV);
7882 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7883 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7884 reseat_1 (it, pos, 0);
7885 compute_stop_pos (it);
7886 /* We must advance forward, right? */
7887 if (it->stop_charpos <= charpos)
7888 emacs_abort ();
7889 }
7890 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7891
7892 if (it->stop_charpos <= where_we_are)
7893 it->prev_stop = it->stop_charpos;
7894 else
7895 it->prev_stop = BEGV;
7896 it->bidi_p = 1;
7897 it->current = save_current;
7898 it->position = save_position;
7899 it->stop_charpos = save_stop_pos;
7900 it->end_charpos = save_end_pos;
7901 }
7902
7903 /* Scan forward from CHARPOS in the current buffer/string, until we
7904 find a stop position > current IT's position. Then handle the stop
7905 position before that. This is called when we bump into a stop
7906 position while reordering bidirectional text. CHARPOS should be
7907 the last previously processed stop_pos (or BEGV/0, if none were
7908 processed yet) whose position is less that IT's current
7909 position. */
7910
7911 static void
7912 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7913 {
7914 int bufp = !STRINGP (it->string);
7915 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7916 struct display_pos save_current = it->current;
7917 struct text_pos save_position = it->position;
7918 struct text_pos pos1;
7919 ptrdiff_t next_stop;
7920
7921 /* Scan in strict logical order. */
7922 eassert (it->bidi_p);
7923 it->bidi_p = 0;
7924 do
7925 {
7926 it->prev_stop = charpos;
7927 if (bufp)
7928 {
7929 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7930 reseat_1 (it, pos1, 0);
7931 }
7932 else
7933 it->current.string_pos = string_pos (charpos, it->string);
7934 compute_stop_pos (it);
7935 /* We must advance forward, right? */
7936 if (it->stop_charpos <= it->prev_stop)
7937 emacs_abort ();
7938 charpos = it->stop_charpos;
7939 }
7940 while (charpos <= where_we_are);
7941
7942 it->bidi_p = 1;
7943 it->current = save_current;
7944 it->position = save_position;
7945 next_stop = it->stop_charpos;
7946 it->stop_charpos = it->prev_stop;
7947 handle_stop (it);
7948 it->stop_charpos = next_stop;
7949 }
7950
7951 /* Load IT with the next display element from current_buffer. Value
7952 is zero if end of buffer reached. IT->stop_charpos is the next
7953 position at which to stop and check for text properties or buffer
7954 end. */
7955
7956 static int
7957 next_element_from_buffer (struct it *it)
7958 {
7959 int success_p = 1;
7960
7961 eassert (IT_CHARPOS (*it) >= BEGV);
7962 eassert (NILP (it->string) && !it->s);
7963 eassert (!it->bidi_p
7964 || (EQ (it->bidi_it.string.lstring, Qnil)
7965 && it->bidi_it.string.s == NULL));
7966
7967 /* With bidi reordering, the character to display might not be the
7968 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7969 we were reseat()ed to a new buffer position, which is potentially
7970 a different paragraph. */
7971 if (it->bidi_p && it->bidi_it.first_elt)
7972 {
7973 get_visually_first_element (it);
7974 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7975 }
7976
7977 if (IT_CHARPOS (*it) >= it->stop_charpos)
7978 {
7979 if (IT_CHARPOS (*it) >= it->end_charpos)
7980 {
7981 int overlay_strings_follow_p;
7982
7983 /* End of the game, except when overlay strings follow that
7984 haven't been returned yet. */
7985 if (it->overlay_strings_at_end_processed_p)
7986 overlay_strings_follow_p = 0;
7987 else
7988 {
7989 it->overlay_strings_at_end_processed_p = 1;
7990 overlay_strings_follow_p = get_overlay_strings (it, 0);
7991 }
7992
7993 if (overlay_strings_follow_p)
7994 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7995 else
7996 {
7997 it->what = IT_EOB;
7998 it->position = it->current.pos;
7999 success_p = 0;
8000 }
8001 }
8002 else if (!(!it->bidi_p
8003 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8004 || IT_CHARPOS (*it) == it->stop_charpos))
8005 {
8006 /* With bidi non-linear iteration, we could find ourselves
8007 far beyond the last computed stop_charpos, with several
8008 other stop positions in between that we missed. Scan
8009 them all now, in buffer's logical order, until we find
8010 and handle the last stop_charpos that precedes our
8011 current position. */
8012 handle_stop_backwards (it, it->stop_charpos);
8013 return GET_NEXT_DISPLAY_ELEMENT (it);
8014 }
8015 else
8016 {
8017 if (it->bidi_p)
8018 {
8019 /* Take note of the stop position we just moved across,
8020 for when we will move back across it. */
8021 it->prev_stop = it->stop_charpos;
8022 /* If we are at base paragraph embedding level, take
8023 note of the last stop position seen at this
8024 level. */
8025 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8026 it->base_level_stop = it->stop_charpos;
8027 }
8028 handle_stop (it);
8029 return GET_NEXT_DISPLAY_ELEMENT (it);
8030 }
8031 }
8032 else if (it->bidi_p
8033 /* If we are before prev_stop, we may have overstepped on
8034 our way backwards a stop_pos, and if so, we need to
8035 handle that stop_pos. */
8036 && IT_CHARPOS (*it) < it->prev_stop
8037 /* We can sometimes back up for reasons that have nothing
8038 to do with bidi reordering. E.g., compositions. The
8039 code below is only needed when we are above the base
8040 embedding level, so test for that explicitly. */
8041 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8042 {
8043 if (it->base_level_stop <= 0
8044 || IT_CHARPOS (*it) < it->base_level_stop)
8045 {
8046 /* If we lost track of base_level_stop, we need to find
8047 prev_stop by looking backwards. This happens, e.g., when
8048 we were reseated to the previous screenful of text by
8049 vertical-motion. */
8050 it->base_level_stop = BEGV;
8051 compute_stop_pos_backwards (it);
8052 handle_stop_backwards (it, it->prev_stop);
8053 }
8054 else
8055 handle_stop_backwards (it, it->base_level_stop);
8056 return GET_NEXT_DISPLAY_ELEMENT (it);
8057 }
8058 else
8059 {
8060 /* No face changes, overlays etc. in sight, so just return a
8061 character from current_buffer. */
8062 unsigned char *p;
8063 ptrdiff_t stop;
8064
8065 /* Maybe run the redisplay end trigger hook. Performance note:
8066 This doesn't seem to cost measurable time. */
8067 if (it->redisplay_end_trigger_charpos
8068 && it->glyph_row
8069 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8070 run_redisplay_end_trigger_hook (it);
8071
8072 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8073 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8074 stop)
8075 && next_element_from_composition (it))
8076 {
8077 return 1;
8078 }
8079
8080 /* Get the next character, maybe multibyte. */
8081 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8082 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8083 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8084 else
8085 it->c = *p, it->len = 1;
8086
8087 /* Record what we have and where it came from. */
8088 it->what = IT_CHARACTER;
8089 it->object = it->w->contents;
8090 it->position = it->current.pos;
8091
8092 /* Normally we return the character found above, except when we
8093 really want to return an ellipsis for selective display. */
8094 if (it->selective)
8095 {
8096 if (it->c == '\n')
8097 {
8098 /* A value of selective > 0 means hide lines indented more
8099 than that number of columns. */
8100 if (it->selective > 0
8101 && IT_CHARPOS (*it) + 1 < ZV
8102 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8103 IT_BYTEPOS (*it) + 1,
8104 it->selective))
8105 {
8106 success_p = next_element_from_ellipsis (it);
8107 it->dpvec_char_len = -1;
8108 }
8109 }
8110 else if (it->c == '\r' && it->selective == -1)
8111 {
8112 /* A value of selective == -1 means that everything from the
8113 CR to the end of the line is invisible, with maybe an
8114 ellipsis displayed for it. */
8115 success_p = next_element_from_ellipsis (it);
8116 it->dpvec_char_len = -1;
8117 }
8118 }
8119 }
8120
8121 /* Value is zero if end of buffer reached. */
8122 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8123 return success_p;
8124 }
8125
8126
8127 /* Run the redisplay end trigger hook for IT. */
8128
8129 static void
8130 run_redisplay_end_trigger_hook (struct it *it)
8131 {
8132 Lisp_Object args[3];
8133
8134 /* IT->glyph_row should be non-null, i.e. we should be actually
8135 displaying something, or otherwise we should not run the hook. */
8136 eassert (it->glyph_row);
8137
8138 /* Set up hook arguments. */
8139 args[0] = Qredisplay_end_trigger_functions;
8140 args[1] = it->window;
8141 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8142 it->redisplay_end_trigger_charpos = 0;
8143
8144 /* Since we are *trying* to run these functions, don't try to run
8145 them again, even if they get an error. */
8146 wset_redisplay_end_trigger (it->w, Qnil);
8147 Frun_hook_with_args (3, args);
8148
8149 /* Notice if it changed the face of the character we are on. */
8150 handle_face_prop (it);
8151 }
8152
8153
8154 /* Deliver a composition display element. Unlike the other
8155 next_element_from_XXX, this function is not registered in the array
8156 get_next_element[]. It is called from next_element_from_buffer and
8157 next_element_from_string when necessary. */
8158
8159 static int
8160 next_element_from_composition (struct it *it)
8161 {
8162 it->what = IT_COMPOSITION;
8163 it->len = it->cmp_it.nbytes;
8164 if (STRINGP (it->string))
8165 {
8166 if (it->c < 0)
8167 {
8168 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8169 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8170 return 0;
8171 }
8172 it->position = it->current.string_pos;
8173 it->object = it->string;
8174 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8175 IT_STRING_BYTEPOS (*it), it->string);
8176 }
8177 else
8178 {
8179 if (it->c < 0)
8180 {
8181 IT_CHARPOS (*it) += it->cmp_it.nchars;
8182 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8183 if (it->bidi_p)
8184 {
8185 if (it->bidi_it.new_paragraph)
8186 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8187 /* Resync the bidi iterator with IT's new position.
8188 FIXME: this doesn't support bidirectional text. */
8189 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8190 bidi_move_to_visually_next (&it->bidi_it);
8191 }
8192 return 0;
8193 }
8194 it->position = it->current.pos;
8195 it->object = it->w->contents;
8196 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8197 IT_BYTEPOS (*it), Qnil);
8198 }
8199 return 1;
8200 }
8201
8202
8203 \f
8204 /***********************************************************************
8205 Moving an iterator without producing glyphs
8206 ***********************************************************************/
8207
8208 /* Check if iterator is at a position corresponding to a valid buffer
8209 position after some move_it_ call. */
8210
8211 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8212 ((it)->method == GET_FROM_STRING \
8213 ? IT_STRING_CHARPOS (*it) == 0 \
8214 : 1)
8215
8216
8217 /* Move iterator IT to a specified buffer or X position within one
8218 line on the display without producing glyphs.
8219
8220 OP should be a bit mask including some or all of these bits:
8221 MOVE_TO_X: Stop upon reaching x-position TO_X.
8222 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8223 Regardless of OP's value, stop upon reaching the end of the display line.
8224
8225 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8226 This means, in particular, that TO_X includes window's horizontal
8227 scroll amount.
8228
8229 The return value has several possible values that
8230 say what condition caused the scan to stop:
8231
8232 MOVE_POS_MATCH_OR_ZV
8233 - when TO_POS or ZV was reached.
8234
8235 MOVE_X_REACHED
8236 -when TO_X was reached before TO_POS or ZV were reached.
8237
8238 MOVE_LINE_CONTINUED
8239 - when we reached the end of the display area and the line must
8240 be continued.
8241
8242 MOVE_LINE_TRUNCATED
8243 - when we reached the end of the display area and the line is
8244 truncated.
8245
8246 MOVE_NEWLINE_OR_CR
8247 - when we stopped at a line end, i.e. a newline or a CR and selective
8248 display is on. */
8249
8250 static enum move_it_result
8251 move_it_in_display_line_to (struct it *it,
8252 ptrdiff_t to_charpos, int to_x,
8253 enum move_operation_enum op)
8254 {
8255 enum move_it_result result = MOVE_UNDEFINED;
8256 struct glyph_row *saved_glyph_row;
8257 struct it wrap_it, atpos_it, atx_it, ppos_it;
8258 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8259 void *ppos_data = NULL;
8260 int may_wrap = 0;
8261 enum it_method prev_method = it->method;
8262 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8263 int saw_smaller_pos = prev_pos < to_charpos;
8264
8265 /* Don't produce glyphs in produce_glyphs. */
8266 saved_glyph_row = it->glyph_row;
8267 it->glyph_row = NULL;
8268
8269 /* Use wrap_it to save a copy of IT wherever a word wrap could
8270 occur. Use atpos_it to save a copy of IT at the desired buffer
8271 position, if found, so that we can scan ahead and check if the
8272 word later overshoots the window edge. Use atx_it similarly, for
8273 pixel positions. */
8274 wrap_it.sp = -1;
8275 atpos_it.sp = -1;
8276 atx_it.sp = -1;
8277
8278 /* Use ppos_it under bidi reordering to save a copy of IT for the
8279 position > CHARPOS that is the closest to CHARPOS. We restore
8280 that position in IT when we have scanned the entire display line
8281 without finding a match for CHARPOS and all the character
8282 positions are greater than CHARPOS. */
8283 if (it->bidi_p)
8284 {
8285 SAVE_IT (ppos_it, *it, ppos_data);
8286 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8287 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8288 SAVE_IT (ppos_it, *it, ppos_data);
8289 }
8290
8291 #define BUFFER_POS_REACHED_P() \
8292 ((op & MOVE_TO_POS) != 0 \
8293 && BUFFERP (it->object) \
8294 && (IT_CHARPOS (*it) == to_charpos \
8295 || ((!it->bidi_p \
8296 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8297 && IT_CHARPOS (*it) > to_charpos) \
8298 || (it->what == IT_COMPOSITION \
8299 && ((IT_CHARPOS (*it) > to_charpos \
8300 && to_charpos >= it->cmp_it.charpos) \
8301 || (IT_CHARPOS (*it) < to_charpos \
8302 && to_charpos <= it->cmp_it.charpos)))) \
8303 && (it->method == GET_FROM_BUFFER \
8304 || (it->method == GET_FROM_DISPLAY_VECTOR \
8305 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8306
8307 /* If there's a line-/wrap-prefix, handle it. */
8308 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8309 && it->current_y < it->last_visible_y)
8310 handle_line_prefix (it);
8311
8312 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8313 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8314
8315 while (1)
8316 {
8317 int x, i, ascent = 0, descent = 0;
8318
8319 /* Utility macro to reset an iterator with x, ascent, and descent. */
8320 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8321 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8322 (IT)->max_descent = descent)
8323
8324 /* Stop if we move beyond TO_CHARPOS (after an image or a
8325 display string or stretch glyph). */
8326 if ((op & MOVE_TO_POS) != 0
8327 && BUFFERP (it->object)
8328 && it->method == GET_FROM_BUFFER
8329 && (((!it->bidi_p
8330 /* When the iterator is at base embedding level, we
8331 are guaranteed that characters are delivered for
8332 display in strictly increasing order of their
8333 buffer positions. */
8334 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8335 && IT_CHARPOS (*it) > to_charpos)
8336 || (it->bidi_p
8337 && (prev_method == GET_FROM_IMAGE
8338 || prev_method == GET_FROM_STRETCH
8339 || prev_method == GET_FROM_STRING)
8340 /* Passed TO_CHARPOS from left to right. */
8341 && ((prev_pos < to_charpos
8342 && IT_CHARPOS (*it) > to_charpos)
8343 /* Passed TO_CHARPOS from right to left. */
8344 || (prev_pos > to_charpos
8345 && IT_CHARPOS (*it) < to_charpos)))))
8346 {
8347 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8348 {
8349 result = MOVE_POS_MATCH_OR_ZV;
8350 break;
8351 }
8352 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8353 /* If wrap_it is valid, the current position might be in a
8354 word that is wrapped. So, save the iterator in
8355 atpos_it and continue to see if wrapping happens. */
8356 SAVE_IT (atpos_it, *it, atpos_data);
8357 }
8358
8359 /* Stop when ZV reached.
8360 We used to stop here when TO_CHARPOS reached as well, but that is
8361 too soon if this glyph does not fit on this line. So we handle it
8362 explicitly below. */
8363 if (!get_next_display_element (it))
8364 {
8365 result = MOVE_POS_MATCH_OR_ZV;
8366 break;
8367 }
8368
8369 if (it->line_wrap == TRUNCATE)
8370 {
8371 if (BUFFER_POS_REACHED_P ())
8372 {
8373 result = MOVE_POS_MATCH_OR_ZV;
8374 break;
8375 }
8376 }
8377 else
8378 {
8379 if (it->line_wrap == WORD_WRAP)
8380 {
8381 if (IT_DISPLAYING_WHITESPACE (it))
8382 may_wrap = 1;
8383 else if (may_wrap)
8384 {
8385 /* We have reached a glyph that follows one or more
8386 whitespace characters. If the position is
8387 already found, we are done. */
8388 if (atpos_it.sp >= 0)
8389 {
8390 RESTORE_IT (it, &atpos_it, atpos_data);
8391 result = MOVE_POS_MATCH_OR_ZV;
8392 goto done;
8393 }
8394 if (atx_it.sp >= 0)
8395 {
8396 RESTORE_IT (it, &atx_it, atx_data);
8397 result = MOVE_X_REACHED;
8398 goto done;
8399 }
8400 /* Otherwise, we can wrap here. */
8401 SAVE_IT (wrap_it, *it, wrap_data);
8402 may_wrap = 0;
8403 }
8404 }
8405 }
8406
8407 /* Remember the line height for the current line, in case
8408 the next element doesn't fit on the line. */
8409 ascent = it->max_ascent;
8410 descent = it->max_descent;
8411
8412 /* The call to produce_glyphs will get the metrics of the
8413 display element IT is loaded with. Record the x-position
8414 before this display element, in case it doesn't fit on the
8415 line. */
8416 x = it->current_x;
8417
8418 PRODUCE_GLYPHS (it);
8419
8420 if (it->area != TEXT_AREA)
8421 {
8422 prev_method = it->method;
8423 if (it->method == GET_FROM_BUFFER)
8424 prev_pos = IT_CHARPOS (*it);
8425 set_iterator_to_next (it, 1);
8426 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8427 SET_TEXT_POS (this_line_min_pos,
8428 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8429 if (it->bidi_p
8430 && (op & MOVE_TO_POS)
8431 && IT_CHARPOS (*it) > to_charpos
8432 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8433 SAVE_IT (ppos_it, *it, ppos_data);
8434 continue;
8435 }
8436
8437 /* The number of glyphs we get back in IT->nglyphs will normally
8438 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8439 character on a terminal frame, or (iii) a line end. For the
8440 second case, IT->nglyphs - 1 padding glyphs will be present.
8441 (On X frames, there is only one glyph produced for a
8442 composite character.)
8443
8444 The behavior implemented below means, for continuation lines,
8445 that as many spaces of a TAB as fit on the current line are
8446 displayed there. For terminal frames, as many glyphs of a
8447 multi-glyph character are displayed in the current line, too.
8448 This is what the old redisplay code did, and we keep it that
8449 way. Under X, the whole shape of a complex character must
8450 fit on the line or it will be completely displayed in the
8451 next line.
8452
8453 Note that both for tabs and padding glyphs, all glyphs have
8454 the same width. */
8455 if (it->nglyphs)
8456 {
8457 /* More than one glyph or glyph doesn't fit on line. All
8458 glyphs have the same width. */
8459 int single_glyph_width = it->pixel_width / it->nglyphs;
8460 int new_x;
8461 int x_before_this_char = x;
8462 int hpos_before_this_char = it->hpos;
8463
8464 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8465 {
8466 new_x = x + single_glyph_width;
8467
8468 /* We want to leave anything reaching TO_X to the caller. */
8469 if ((op & MOVE_TO_X) && new_x > to_x)
8470 {
8471 if (BUFFER_POS_REACHED_P ())
8472 {
8473 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8474 goto buffer_pos_reached;
8475 if (atpos_it.sp < 0)
8476 {
8477 SAVE_IT (atpos_it, *it, atpos_data);
8478 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8479 }
8480 }
8481 else
8482 {
8483 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8484 {
8485 it->current_x = x;
8486 result = MOVE_X_REACHED;
8487 break;
8488 }
8489 if (atx_it.sp < 0)
8490 {
8491 SAVE_IT (atx_it, *it, atx_data);
8492 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8493 }
8494 }
8495 }
8496
8497 if (/* Lines are continued. */
8498 it->line_wrap != TRUNCATE
8499 && (/* And glyph doesn't fit on the line. */
8500 new_x > it->last_visible_x
8501 /* Or it fits exactly and we're on a window
8502 system frame. */
8503 || (new_x == it->last_visible_x
8504 && FRAME_WINDOW_P (it->f)
8505 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8506 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8507 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8508 {
8509 if (/* IT->hpos == 0 means the very first glyph
8510 doesn't fit on the line, e.g. a wide image. */
8511 it->hpos == 0
8512 || (new_x == it->last_visible_x
8513 && FRAME_WINDOW_P (it->f)))
8514 {
8515 ++it->hpos;
8516 it->current_x = new_x;
8517
8518 /* The character's last glyph just barely fits
8519 in this row. */
8520 if (i == it->nglyphs - 1)
8521 {
8522 /* If this is the destination position,
8523 return a position *before* it in this row,
8524 now that we know it fits in this row. */
8525 if (BUFFER_POS_REACHED_P ())
8526 {
8527 if (it->line_wrap != WORD_WRAP
8528 || wrap_it.sp < 0)
8529 {
8530 it->hpos = hpos_before_this_char;
8531 it->current_x = x_before_this_char;
8532 result = MOVE_POS_MATCH_OR_ZV;
8533 break;
8534 }
8535 if (it->line_wrap == WORD_WRAP
8536 && atpos_it.sp < 0)
8537 {
8538 SAVE_IT (atpos_it, *it, atpos_data);
8539 atpos_it.current_x = x_before_this_char;
8540 atpos_it.hpos = hpos_before_this_char;
8541 }
8542 }
8543
8544 prev_method = it->method;
8545 if (it->method == GET_FROM_BUFFER)
8546 prev_pos = IT_CHARPOS (*it);
8547 set_iterator_to_next (it, 1);
8548 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8549 SET_TEXT_POS (this_line_min_pos,
8550 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8551 /* On graphical terminals, newlines may
8552 "overflow" into the fringe if
8553 overflow-newline-into-fringe is non-nil.
8554 On text terminals, and on graphical
8555 terminals with no right margin, newlines
8556 may overflow into the last glyph on the
8557 display line.*/
8558 if (!FRAME_WINDOW_P (it->f)
8559 || ((it->bidi_p
8560 && it->bidi_it.paragraph_dir == R2L)
8561 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8562 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8563 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8564 {
8565 if (!get_next_display_element (it))
8566 {
8567 result = MOVE_POS_MATCH_OR_ZV;
8568 break;
8569 }
8570 if (BUFFER_POS_REACHED_P ())
8571 {
8572 if (ITERATOR_AT_END_OF_LINE_P (it))
8573 result = MOVE_POS_MATCH_OR_ZV;
8574 else
8575 result = MOVE_LINE_CONTINUED;
8576 break;
8577 }
8578 if (ITERATOR_AT_END_OF_LINE_P (it)
8579 && (it->line_wrap != WORD_WRAP
8580 || wrap_it.sp < 0))
8581 {
8582 result = MOVE_NEWLINE_OR_CR;
8583 break;
8584 }
8585 }
8586 }
8587 }
8588 else
8589 IT_RESET_X_ASCENT_DESCENT (it);
8590
8591 if (wrap_it.sp >= 0)
8592 {
8593 RESTORE_IT (it, &wrap_it, wrap_data);
8594 atpos_it.sp = -1;
8595 atx_it.sp = -1;
8596 }
8597
8598 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8599 IT_CHARPOS (*it)));
8600 result = MOVE_LINE_CONTINUED;
8601 break;
8602 }
8603
8604 if (BUFFER_POS_REACHED_P ())
8605 {
8606 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8607 goto buffer_pos_reached;
8608 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8609 {
8610 SAVE_IT (atpos_it, *it, atpos_data);
8611 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8612 }
8613 }
8614
8615 if (new_x > it->first_visible_x)
8616 {
8617 /* Glyph is visible. Increment number of glyphs that
8618 would be displayed. */
8619 ++it->hpos;
8620 }
8621 }
8622
8623 if (result != MOVE_UNDEFINED)
8624 break;
8625 }
8626 else if (BUFFER_POS_REACHED_P ())
8627 {
8628 buffer_pos_reached:
8629 IT_RESET_X_ASCENT_DESCENT (it);
8630 result = MOVE_POS_MATCH_OR_ZV;
8631 break;
8632 }
8633 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8634 {
8635 /* Stop when TO_X specified and reached. This check is
8636 necessary here because of lines consisting of a line end,
8637 only. The line end will not produce any glyphs and we
8638 would never get MOVE_X_REACHED. */
8639 eassert (it->nglyphs == 0);
8640 result = MOVE_X_REACHED;
8641 break;
8642 }
8643
8644 /* Is this a line end? If yes, we're done. */
8645 if (ITERATOR_AT_END_OF_LINE_P (it))
8646 {
8647 /* If we are past TO_CHARPOS, but never saw any character
8648 positions smaller than TO_CHARPOS, return
8649 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8650 did. */
8651 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8652 {
8653 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8654 {
8655 if (IT_CHARPOS (ppos_it) < ZV)
8656 {
8657 RESTORE_IT (it, &ppos_it, ppos_data);
8658 result = MOVE_POS_MATCH_OR_ZV;
8659 }
8660 else
8661 goto buffer_pos_reached;
8662 }
8663 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8664 && IT_CHARPOS (*it) > to_charpos)
8665 goto buffer_pos_reached;
8666 else
8667 result = MOVE_NEWLINE_OR_CR;
8668 }
8669 else
8670 result = MOVE_NEWLINE_OR_CR;
8671 break;
8672 }
8673
8674 prev_method = it->method;
8675 if (it->method == GET_FROM_BUFFER)
8676 prev_pos = IT_CHARPOS (*it);
8677 /* The current display element has been consumed. Advance
8678 to the next. */
8679 set_iterator_to_next (it, 1);
8680 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8681 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8682 if (IT_CHARPOS (*it) < to_charpos)
8683 saw_smaller_pos = 1;
8684 if (it->bidi_p
8685 && (op & MOVE_TO_POS)
8686 && IT_CHARPOS (*it) >= to_charpos
8687 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8688 SAVE_IT (ppos_it, *it, ppos_data);
8689
8690 /* Stop if lines are truncated and IT's current x-position is
8691 past the right edge of the window now. */
8692 if (it->line_wrap == TRUNCATE
8693 && it->current_x >= it->last_visible_x)
8694 {
8695 if (!FRAME_WINDOW_P (it->f)
8696 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8697 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8698 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8699 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8700 {
8701 int at_eob_p = 0;
8702
8703 if ((at_eob_p = !get_next_display_element (it))
8704 || BUFFER_POS_REACHED_P ()
8705 /* If we are past TO_CHARPOS, but never saw any
8706 character positions smaller than TO_CHARPOS,
8707 return MOVE_POS_MATCH_OR_ZV, like the
8708 unidirectional display did. */
8709 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8710 && !saw_smaller_pos
8711 && IT_CHARPOS (*it) > to_charpos))
8712 {
8713 if (it->bidi_p
8714 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8715 RESTORE_IT (it, &ppos_it, ppos_data);
8716 result = MOVE_POS_MATCH_OR_ZV;
8717 break;
8718 }
8719 if (ITERATOR_AT_END_OF_LINE_P (it))
8720 {
8721 result = MOVE_NEWLINE_OR_CR;
8722 break;
8723 }
8724 }
8725 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8726 && !saw_smaller_pos
8727 && IT_CHARPOS (*it) > to_charpos)
8728 {
8729 if (IT_CHARPOS (ppos_it) < ZV)
8730 RESTORE_IT (it, &ppos_it, ppos_data);
8731 result = MOVE_POS_MATCH_OR_ZV;
8732 break;
8733 }
8734 result = MOVE_LINE_TRUNCATED;
8735 break;
8736 }
8737 #undef IT_RESET_X_ASCENT_DESCENT
8738 }
8739
8740 #undef BUFFER_POS_REACHED_P
8741
8742 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8743 restore the saved iterator. */
8744 if (atpos_it.sp >= 0)
8745 RESTORE_IT (it, &atpos_it, atpos_data);
8746 else if (atx_it.sp >= 0)
8747 RESTORE_IT (it, &atx_it, atx_data);
8748
8749 done:
8750
8751 if (atpos_data)
8752 bidi_unshelve_cache (atpos_data, 1);
8753 if (atx_data)
8754 bidi_unshelve_cache (atx_data, 1);
8755 if (wrap_data)
8756 bidi_unshelve_cache (wrap_data, 1);
8757 if (ppos_data)
8758 bidi_unshelve_cache (ppos_data, 1);
8759
8760 /* Restore the iterator settings altered at the beginning of this
8761 function. */
8762 it->glyph_row = saved_glyph_row;
8763 return result;
8764 }
8765
8766 /* For external use. */
8767 void
8768 move_it_in_display_line (struct it *it,
8769 ptrdiff_t to_charpos, int to_x,
8770 enum move_operation_enum op)
8771 {
8772 if (it->line_wrap == WORD_WRAP
8773 && (op & MOVE_TO_X))
8774 {
8775 struct it save_it;
8776 void *save_data = NULL;
8777 int skip;
8778
8779 SAVE_IT (save_it, *it, save_data);
8780 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8781 /* When word-wrap is on, TO_X may lie past the end
8782 of a wrapped line. Then it->current is the
8783 character on the next line, so backtrack to the
8784 space before the wrap point. */
8785 if (skip == MOVE_LINE_CONTINUED)
8786 {
8787 int prev_x = max (it->current_x - 1, 0);
8788 RESTORE_IT (it, &save_it, save_data);
8789 move_it_in_display_line_to
8790 (it, -1, prev_x, MOVE_TO_X);
8791 }
8792 else
8793 bidi_unshelve_cache (save_data, 1);
8794 }
8795 else
8796 move_it_in_display_line_to (it, to_charpos, to_x, op);
8797 }
8798
8799
8800 /* Move IT forward until it satisfies one or more of the criteria in
8801 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8802
8803 OP is a bit-mask that specifies where to stop, and in particular,
8804 which of those four position arguments makes a difference. See the
8805 description of enum move_operation_enum.
8806
8807 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8808 screen line, this function will set IT to the next position that is
8809 displayed to the right of TO_CHARPOS on the screen. */
8810
8811 void
8812 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8813 {
8814 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8815 int line_height, line_start_x = 0, reached = 0;
8816 void *backup_data = NULL;
8817
8818 for (;;)
8819 {
8820 if (op & MOVE_TO_VPOS)
8821 {
8822 /* If no TO_CHARPOS and no TO_X specified, stop at the
8823 start of the line TO_VPOS. */
8824 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8825 {
8826 if (it->vpos == to_vpos)
8827 {
8828 reached = 1;
8829 break;
8830 }
8831 else
8832 skip = move_it_in_display_line_to (it, -1, -1, 0);
8833 }
8834 else
8835 {
8836 /* TO_VPOS >= 0 means stop at TO_X in the line at
8837 TO_VPOS, or at TO_POS, whichever comes first. */
8838 if (it->vpos == to_vpos)
8839 {
8840 reached = 2;
8841 break;
8842 }
8843
8844 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8845
8846 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8847 {
8848 reached = 3;
8849 break;
8850 }
8851 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8852 {
8853 /* We have reached TO_X but not in the line we want. */
8854 skip = move_it_in_display_line_to (it, to_charpos,
8855 -1, MOVE_TO_POS);
8856 if (skip == MOVE_POS_MATCH_OR_ZV)
8857 {
8858 reached = 4;
8859 break;
8860 }
8861 }
8862 }
8863 }
8864 else if (op & MOVE_TO_Y)
8865 {
8866 struct it it_backup;
8867
8868 if (it->line_wrap == WORD_WRAP)
8869 SAVE_IT (it_backup, *it, backup_data);
8870
8871 /* TO_Y specified means stop at TO_X in the line containing
8872 TO_Y---or at TO_CHARPOS if this is reached first. The
8873 problem is that we can't really tell whether the line
8874 contains TO_Y before we have completely scanned it, and
8875 this may skip past TO_X. What we do is to first scan to
8876 TO_X.
8877
8878 If TO_X is not specified, use a TO_X of zero. The reason
8879 is to make the outcome of this function more predictable.
8880 If we didn't use TO_X == 0, we would stop at the end of
8881 the line which is probably not what a caller would expect
8882 to happen. */
8883 skip = move_it_in_display_line_to
8884 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8885 (MOVE_TO_X | (op & MOVE_TO_POS)));
8886
8887 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8888 if (skip == MOVE_POS_MATCH_OR_ZV)
8889 reached = 5;
8890 else if (skip == MOVE_X_REACHED)
8891 {
8892 /* If TO_X was reached, we want to know whether TO_Y is
8893 in the line. We know this is the case if the already
8894 scanned glyphs make the line tall enough. Otherwise,
8895 we must check by scanning the rest of the line. */
8896 line_height = it->max_ascent + it->max_descent;
8897 if (to_y >= it->current_y
8898 && to_y < it->current_y + line_height)
8899 {
8900 reached = 6;
8901 break;
8902 }
8903 SAVE_IT (it_backup, *it, backup_data);
8904 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8905 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8906 op & MOVE_TO_POS);
8907 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8908 line_height = it->max_ascent + it->max_descent;
8909 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8910
8911 if (to_y >= it->current_y
8912 && to_y < it->current_y + line_height)
8913 {
8914 /* If TO_Y is in this line and TO_X was reached
8915 above, we scanned too far. We have to restore
8916 IT's settings to the ones before skipping. But
8917 keep the more accurate values of max_ascent and
8918 max_descent we've found while skipping the rest
8919 of the line, for the sake of callers, such as
8920 pos_visible_p, that need to know the line
8921 height. */
8922 int max_ascent = it->max_ascent;
8923 int max_descent = it->max_descent;
8924
8925 RESTORE_IT (it, &it_backup, backup_data);
8926 it->max_ascent = max_ascent;
8927 it->max_descent = max_descent;
8928 reached = 6;
8929 }
8930 else
8931 {
8932 skip = skip2;
8933 if (skip == MOVE_POS_MATCH_OR_ZV)
8934 reached = 7;
8935 }
8936 }
8937 else
8938 {
8939 /* Check whether TO_Y is in this line. */
8940 line_height = it->max_ascent + it->max_descent;
8941 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8942
8943 if (to_y >= it->current_y
8944 && to_y < it->current_y + line_height)
8945 {
8946 /* When word-wrap is on, TO_X may lie past the end
8947 of a wrapped line. Then it->current is the
8948 character on the next line, so backtrack to the
8949 space before the wrap point. */
8950 if (skip == MOVE_LINE_CONTINUED
8951 && it->line_wrap == WORD_WRAP)
8952 {
8953 int prev_x = max (it->current_x - 1, 0);
8954 RESTORE_IT (it, &it_backup, backup_data);
8955 skip = move_it_in_display_line_to
8956 (it, -1, prev_x, MOVE_TO_X);
8957 }
8958 reached = 6;
8959 }
8960 }
8961
8962 if (reached)
8963 break;
8964 }
8965 else if (BUFFERP (it->object)
8966 && (it->method == GET_FROM_BUFFER
8967 || it->method == GET_FROM_STRETCH)
8968 && IT_CHARPOS (*it) >= to_charpos
8969 /* Under bidi iteration, a call to set_iterator_to_next
8970 can scan far beyond to_charpos if the initial
8971 portion of the next line needs to be reordered. In
8972 that case, give move_it_in_display_line_to another
8973 chance below. */
8974 && !(it->bidi_p
8975 && it->bidi_it.scan_dir == -1))
8976 skip = MOVE_POS_MATCH_OR_ZV;
8977 else
8978 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8979
8980 switch (skip)
8981 {
8982 case MOVE_POS_MATCH_OR_ZV:
8983 reached = 8;
8984 goto out;
8985
8986 case MOVE_NEWLINE_OR_CR:
8987 set_iterator_to_next (it, 1);
8988 it->continuation_lines_width = 0;
8989 break;
8990
8991 case MOVE_LINE_TRUNCATED:
8992 it->continuation_lines_width = 0;
8993 reseat_at_next_visible_line_start (it, 0);
8994 if ((op & MOVE_TO_POS) != 0
8995 && IT_CHARPOS (*it) > to_charpos)
8996 {
8997 reached = 9;
8998 goto out;
8999 }
9000 break;
9001
9002 case MOVE_LINE_CONTINUED:
9003 /* For continued lines ending in a tab, some of the glyphs
9004 associated with the tab are displayed on the current
9005 line. Since it->current_x does not include these glyphs,
9006 we use it->last_visible_x instead. */
9007 if (it->c == '\t')
9008 {
9009 it->continuation_lines_width += it->last_visible_x;
9010 /* When moving by vpos, ensure that the iterator really
9011 advances to the next line (bug#847, bug#969). Fixme:
9012 do we need to do this in other circumstances? */
9013 if (it->current_x != it->last_visible_x
9014 && (op & MOVE_TO_VPOS)
9015 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9016 {
9017 line_start_x = it->current_x + it->pixel_width
9018 - it->last_visible_x;
9019 set_iterator_to_next (it, 0);
9020 }
9021 }
9022 else
9023 it->continuation_lines_width += it->current_x;
9024 break;
9025
9026 default:
9027 emacs_abort ();
9028 }
9029
9030 /* Reset/increment for the next run. */
9031 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9032 it->current_x = line_start_x;
9033 line_start_x = 0;
9034 it->hpos = 0;
9035 it->current_y += it->max_ascent + it->max_descent;
9036 ++it->vpos;
9037 last_height = it->max_ascent + it->max_descent;
9038 it->max_ascent = it->max_descent = 0;
9039 }
9040
9041 out:
9042
9043 /* On text terminals, we may stop at the end of a line in the middle
9044 of a multi-character glyph. If the glyph itself is continued,
9045 i.e. it is actually displayed on the next line, don't treat this
9046 stopping point as valid; move to the next line instead (unless
9047 that brings us offscreen). */
9048 if (!FRAME_WINDOW_P (it->f)
9049 && op & MOVE_TO_POS
9050 && IT_CHARPOS (*it) == to_charpos
9051 && it->what == IT_CHARACTER
9052 && it->nglyphs > 1
9053 && it->line_wrap == WINDOW_WRAP
9054 && it->current_x == it->last_visible_x - 1
9055 && it->c != '\n'
9056 && it->c != '\t'
9057 && it->vpos < XFASTINT (it->w->window_end_vpos))
9058 {
9059 it->continuation_lines_width += it->current_x;
9060 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9061 it->current_y += it->max_ascent + it->max_descent;
9062 ++it->vpos;
9063 last_height = it->max_ascent + it->max_descent;
9064 }
9065
9066 if (backup_data)
9067 bidi_unshelve_cache (backup_data, 1);
9068
9069 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9070 }
9071
9072
9073 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9074
9075 If DY > 0, move IT backward at least that many pixels. DY = 0
9076 means move IT backward to the preceding line start or BEGV. This
9077 function may move over more than DY pixels if IT->current_y - DY
9078 ends up in the middle of a line; in this case IT->current_y will be
9079 set to the top of the line moved to. */
9080
9081 void
9082 move_it_vertically_backward (struct it *it, int dy)
9083 {
9084 int nlines, h;
9085 struct it it2, it3;
9086 void *it2data = NULL, *it3data = NULL;
9087 ptrdiff_t start_pos;
9088 int nchars_per_row
9089 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9090 ptrdiff_t pos_limit;
9091
9092 move_further_back:
9093 eassert (dy >= 0);
9094
9095 start_pos = IT_CHARPOS (*it);
9096
9097 /* Estimate how many newlines we must move back. */
9098 nlines = max (1, dy / default_line_pixel_height (it->w));
9099 if (it->line_wrap == TRUNCATE)
9100 pos_limit = BEGV;
9101 else
9102 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9103
9104 /* Set the iterator's position that many lines back. But don't go
9105 back more than NLINES full screen lines -- this wins a day with
9106 buffers which have very long lines. */
9107 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9108 back_to_previous_visible_line_start (it);
9109
9110 /* Reseat the iterator here. When moving backward, we don't want
9111 reseat to skip forward over invisible text, set up the iterator
9112 to deliver from overlay strings at the new position etc. So,
9113 use reseat_1 here. */
9114 reseat_1 (it, it->current.pos, 1);
9115
9116 /* We are now surely at a line start. */
9117 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9118 reordering is in effect. */
9119 it->continuation_lines_width = 0;
9120
9121 /* Move forward and see what y-distance we moved. First move to the
9122 start of the next line so that we get its height. We need this
9123 height to be able to tell whether we reached the specified
9124 y-distance. */
9125 SAVE_IT (it2, *it, it2data);
9126 it2.max_ascent = it2.max_descent = 0;
9127 do
9128 {
9129 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9130 MOVE_TO_POS | MOVE_TO_VPOS);
9131 }
9132 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9133 /* If we are in a display string which starts at START_POS,
9134 and that display string includes a newline, and we are
9135 right after that newline (i.e. at the beginning of a
9136 display line), exit the loop, because otherwise we will
9137 infloop, since move_it_to will see that it is already at
9138 START_POS and will not move. */
9139 || (it2.method == GET_FROM_STRING
9140 && IT_CHARPOS (it2) == start_pos
9141 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9142 eassert (IT_CHARPOS (*it) >= BEGV);
9143 SAVE_IT (it3, it2, it3data);
9144
9145 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9146 eassert (IT_CHARPOS (*it) >= BEGV);
9147 /* H is the actual vertical distance from the position in *IT
9148 and the starting position. */
9149 h = it2.current_y - it->current_y;
9150 /* NLINES is the distance in number of lines. */
9151 nlines = it2.vpos - it->vpos;
9152
9153 /* Correct IT's y and vpos position
9154 so that they are relative to the starting point. */
9155 it->vpos -= nlines;
9156 it->current_y -= h;
9157
9158 if (dy == 0)
9159 {
9160 /* DY == 0 means move to the start of the screen line. The
9161 value of nlines is > 0 if continuation lines were involved,
9162 or if the original IT position was at start of a line. */
9163 RESTORE_IT (it, it, it2data);
9164 if (nlines > 0)
9165 move_it_by_lines (it, nlines);
9166 /* The above code moves us to some position NLINES down,
9167 usually to its first glyph (leftmost in an L2R line), but
9168 that's not necessarily the start of the line, under bidi
9169 reordering. We want to get to the character position
9170 that is immediately after the newline of the previous
9171 line. */
9172 if (it->bidi_p
9173 && !it->continuation_lines_width
9174 && !STRINGP (it->string)
9175 && IT_CHARPOS (*it) > BEGV
9176 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9177 {
9178 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9179
9180 DEC_BOTH (cp, bp);
9181 cp = find_newline_no_quit (cp, bp, -1, NULL);
9182 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9183 }
9184 bidi_unshelve_cache (it3data, 1);
9185 }
9186 else
9187 {
9188 /* The y-position we try to reach, relative to *IT.
9189 Note that H has been subtracted in front of the if-statement. */
9190 int target_y = it->current_y + h - dy;
9191 int y0 = it3.current_y;
9192 int y1;
9193 int line_height;
9194
9195 RESTORE_IT (&it3, &it3, it3data);
9196 y1 = line_bottom_y (&it3);
9197 line_height = y1 - y0;
9198 RESTORE_IT (it, it, it2data);
9199 /* If we did not reach target_y, try to move further backward if
9200 we can. If we moved too far backward, try to move forward. */
9201 if (target_y < it->current_y
9202 /* This is heuristic. In a window that's 3 lines high, with
9203 a line height of 13 pixels each, recentering with point
9204 on the bottom line will try to move -39/2 = 19 pixels
9205 backward. Try to avoid moving into the first line. */
9206 && (it->current_y - target_y
9207 > min (window_box_height (it->w), line_height * 2 / 3))
9208 && IT_CHARPOS (*it) > BEGV)
9209 {
9210 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9211 target_y - it->current_y));
9212 dy = it->current_y - target_y;
9213 goto move_further_back;
9214 }
9215 else if (target_y >= it->current_y + line_height
9216 && IT_CHARPOS (*it) < ZV)
9217 {
9218 /* Should move forward by at least one line, maybe more.
9219
9220 Note: Calling move_it_by_lines can be expensive on
9221 terminal frames, where compute_motion is used (via
9222 vmotion) to do the job, when there are very long lines
9223 and truncate-lines is nil. That's the reason for
9224 treating terminal frames specially here. */
9225
9226 if (!FRAME_WINDOW_P (it->f))
9227 move_it_vertically (it, target_y - (it->current_y + line_height));
9228 else
9229 {
9230 do
9231 {
9232 move_it_by_lines (it, 1);
9233 }
9234 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9235 }
9236 }
9237 }
9238 }
9239
9240
9241 /* Move IT by a specified amount of pixel lines DY. DY negative means
9242 move backwards. DY = 0 means move to start of screen line. At the
9243 end, IT will be on the start of a screen line. */
9244
9245 void
9246 move_it_vertically (struct it *it, int dy)
9247 {
9248 if (dy <= 0)
9249 move_it_vertically_backward (it, -dy);
9250 else
9251 {
9252 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9253 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9254 MOVE_TO_POS | MOVE_TO_Y);
9255 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9256
9257 /* If buffer ends in ZV without a newline, move to the start of
9258 the line to satisfy the post-condition. */
9259 if (IT_CHARPOS (*it) == ZV
9260 && ZV > BEGV
9261 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9262 move_it_by_lines (it, 0);
9263 }
9264 }
9265
9266
9267 /* Move iterator IT past the end of the text line it is in. */
9268
9269 void
9270 move_it_past_eol (struct it *it)
9271 {
9272 enum move_it_result rc;
9273
9274 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9275 if (rc == MOVE_NEWLINE_OR_CR)
9276 set_iterator_to_next (it, 0);
9277 }
9278
9279
9280 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9281 negative means move up. DVPOS == 0 means move to the start of the
9282 screen line.
9283
9284 Optimization idea: If we would know that IT->f doesn't use
9285 a face with proportional font, we could be faster for
9286 truncate-lines nil. */
9287
9288 void
9289 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9290 {
9291
9292 /* The commented-out optimization uses vmotion on terminals. This
9293 gives bad results, because elements like it->what, on which
9294 callers such as pos_visible_p rely, aren't updated. */
9295 /* struct position pos;
9296 if (!FRAME_WINDOW_P (it->f))
9297 {
9298 struct text_pos textpos;
9299
9300 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9301 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9302 reseat (it, textpos, 1);
9303 it->vpos += pos.vpos;
9304 it->current_y += pos.vpos;
9305 }
9306 else */
9307
9308 if (dvpos == 0)
9309 {
9310 /* DVPOS == 0 means move to the start of the screen line. */
9311 move_it_vertically_backward (it, 0);
9312 /* Let next call to line_bottom_y calculate real line height */
9313 last_height = 0;
9314 }
9315 else if (dvpos > 0)
9316 {
9317 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9318 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9319 {
9320 /* Only move to the next buffer position if we ended up in a
9321 string from display property, not in an overlay string
9322 (before-string or after-string). That is because the
9323 latter don't conceal the underlying buffer position, so
9324 we can ask to move the iterator to the exact position we
9325 are interested in. Note that, even if we are already at
9326 IT_CHARPOS (*it), the call below is not a no-op, as it
9327 will detect that we are at the end of the string, pop the
9328 iterator, and compute it->current_x and it->hpos
9329 correctly. */
9330 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9331 -1, -1, -1, MOVE_TO_POS);
9332 }
9333 }
9334 else
9335 {
9336 struct it it2;
9337 void *it2data = NULL;
9338 ptrdiff_t start_charpos, i;
9339 int nchars_per_row
9340 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9341 ptrdiff_t pos_limit;
9342
9343 /* Start at the beginning of the screen line containing IT's
9344 position. This may actually move vertically backwards,
9345 in case of overlays, so adjust dvpos accordingly. */
9346 dvpos += it->vpos;
9347 move_it_vertically_backward (it, 0);
9348 dvpos -= it->vpos;
9349
9350 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9351 screen lines, and reseat the iterator there. */
9352 start_charpos = IT_CHARPOS (*it);
9353 if (it->line_wrap == TRUNCATE)
9354 pos_limit = BEGV;
9355 else
9356 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9357 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9358 back_to_previous_visible_line_start (it);
9359 reseat (it, it->current.pos, 1);
9360
9361 /* Move further back if we end up in a string or an image. */
9362 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9363 {
9364 /* First try to move to start of display line. */
9365 dvpos += it->vpos;
9366 move_it_vertically_backward (it, 0);
9367 dvpos -= it->vpos;
9368 if (IT_POS_VALID_AFTER_MOVE_P (it))
9369 break;
9370 /* If start of line is still in string or image,
9371 move further back. */
9372 back_to_previous_visible_line_start (it);
9373 reseat (it, it->current.pos, 1);
9374 dvpos--;
9375 }
9376
9377 it->current_x = it->hpos = 0;
9378
9379 /* Above call may have moved too far if continuation lines
9380 are involved. Scan forward and see if it did. */
9381 SAVE_IT (it2, *it, it2data);
9382 it2.vpos = it2.current_y = 0;
9383 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9384 it->vpos -= it2.vpos;
9385 it->current_y -= it2.current_y;
9386 it->current_x = it->hpos = 0;
9387
9388 /* If we moved too far back, move IT some lines forward. */
9389 if (it2.vpos > -dvpos)
9390 {
9391 int delta = it2.vpos + dvpos;
9392
9393 RESTORE_IT (&it2, &it2, it2data);
9394 SAVE_IT (it2, *it, it2data);
9395 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9396 /* Move back again if we got too far ahead. */
9397 if (IT_CHARPOS (*it) >= start_charpos)
9398 RESTORE_IT (it, &it2, it2data);
9399 else
9400 bidi_unshelve_cache (it2data, 1);
9401 }
9402 else
9403 RESTORE_IT (it, it, it2data);
9404 }
9405 }
9406
9407 /* Return 1 if IT points into the middle of a display vector. */
9408
9409 int
9410 in_display_vector_p (struct it *it)
9411 {
9412 return (it->method == GET_FROM_DISPLAY_VECTOR
9413 && it->current.dpvec_index > 0
9414 && it->dpvec + it->current.dpvec_index != it->dpend);
9415 }
9416
9417 \f
9418 /***********************************************************************
9419 Messages
9420 ***********************************************************************/
9421
9422
9423 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9424 to *Messages*. */
9425
9426 void
9427 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9428 {
9429 Lisp_Object args[3];
9430 Lisp_Object msg, fmt;
9431 char *buffer;
9432 ptrdiff_t len;
9433 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9434 USE_SAFE_ALLOCA;
9435
9436 fmt = msg = Qnil;
9437 GCPRO4 (fmt, msg, arg1, arg2);
9438
9439 args[0] = fmt = build_string (format);
9440 args[1] = arg1;
9441 args[2] = arg2;
9442 msg = Fformat (3, args);
9443
9444 len = SBYTES (msg) + 1;
9445 buffer = SAFE_ALLOCA (len);
9446 memcpy (buffer, SDATA (msg), len);
9447
9448 message_dolog (buffer, len - 1, 1, 0);
9449 SAFE_FREE ();
9450
9451 UNGCPRO;
9452 }
9453
9454
9455 /* Output a newline in the *Messages* buffer if "needs" one. */
9456
9457 void
9458 message_log_maybe_newline (void)
9459 {
9460 if (message_log_need_newline)
9461 message_dolog ("", 0, 1, 0);
9462 }
9463
9464
9465 /* Add a string M of length NBYTES to the message log, optionally
9466 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9467 true, means interpret the contents of M as multibyte. This
9468 function calls low-level routines in order to bypass text property
9469 hooks, etc. which might not be safe to run.
9470
9471 This may GC (insert may run before/after change hooks),
9472 so the buffer M must NOT point to a Lisp string. */
9473
9474 void
9475 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9476 {
9477 const unsigned char *msg = (const unsigned char *) m;
9478
9479 if (!NILP (Vmemory_full))
9480 return;
9481
9482 if (!NILP (Vmessage_log_max))
9483 {
9484 struct buffer *oldbuf;
9485 Lisp_Object oldpoint, oldbegv, oldzv;
9486 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9487 ptrdiff_t point_at_end = 0;
9488 ptrdiff_t zv_at_end = 0;
9489 Lisp_Object old_deactivate_mark;
9490 bool shown;
9491 struct gcpro gcpro1;
9492
9493 old_deactivate_mark = Vdeactivate_mark;
9494 oldbuf = current_buffer;
9495 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9496 bset_undo_list (current_buffer, Qt);
9497
9498 oldpoint = message_dolog_marker1;
9499 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9500 oldbegv = message_dolog_marker2;
9501 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9502 oldzv = message_dolog_marker3;
9503 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9504 GCPRO1 (old_deactivate_mark);
9505
9506 if (PT == Z)
9507 point_at_end = 1;
9508 if (ZV == Z)
9509 zv_at_end = 1;
9510
9511 BEGV = BEG;
9512 BEGV_BYTE = BEG_BYTE;
9513 ZV = Z;
9514 ZV_BYTE = Z_BYTE;
9515 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9516
9517 /* Insert the string--maybe converting multibyte to single byte
9518 or vice versa, so that all the text fits the buffer. */
9519 if (multibyte
9520 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9521 {
9522 ptrdiff_t i;
9523 int c, char_bytes;
9524 char work[1];
9525
9526 /* Convert a multibyte string to single-byte
9527 for the *Message* buffer. */
9528 for (i = 0; i < nbytes; i += char_bytes)
9529 {
9530 c = string_char_and_length (msg + i, &char_bytes);
9531 work[0] = (ASCII_CHAR_P (c)
9532 ? c
9533 : multibyte_char_to_unibyte (c));
9534 insert_1_both (work, 1, 1, 1, 0, 0);
9535 }
9536 }
9537 else if (! multibyte
9538 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9539 {
9540 ptrdiff_t i;
9541 int c, char_bytes;
9542 unsigned char str[MAX_MULTIBYTE_LENGTH];
9543 /* Convert a single-byte string to multibyte
9544 for the *Message* buffer. */
9545 for (i = 0; i < nbytes; i++)
9546 {
9547 c = msg[i];
9548 MAKE_CHAR_MULTIBYTE (c);
9549 char_bytes = CHAR_STRING (c, str);
9550 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9551 }
9552 }
9553 else if (nbytes)
9554 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9555
9556 if (nlflag)
9557 {
9558 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9559 printmax_t dups;
9560
9561 insert_1_both ("\n", 1, 1, 1, 0, 0);
9562
9563 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9564 this_bol = PT;
9565 this_bol_byte = PT_BYTE;
9566
9567 /* See if this line duplicates the previous one.
9568 If so, combine duplicates. */
9569 if (this_bol > BEG)
9570 {
9571 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9572 prev_bol = PT;
9573 prev_bol_byte = PT_BYTE;
9574
9575 dups = message_log_check_duplicate (prev_bol_byte,
9576 this_bol_byte);
9577 if (dups)
9578 {
9579 del_range_both (prev_bol, prev_bol_byte,
9580 this_bol, this_bol_byte, 0);
9581 if (dups > 1)
9582 {
9583 char dupstr[sizeof " [ times]"
9584 + INT_STRLEN_BOUND (printmax_t)];
9585
9586 /* If you change this format, don't forget to also
9587 change message_log_check_duplicate. */
9588 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9589 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9590 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9591 }
9592 }
9593 }
9594
9595 /* If we have more than the desired maximum number of lines
9596 in the *Messages* buffer now, delete the oldest ones.
9597 This is safe because we don't have undo in this buffer. */
9598
9599 if (NATNUMP (Vmessage_log_max))
9600 {
9601 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9602 -XFASTINT (Vmessage_log_max) - 1, 0);
9603 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9604 }
9605 }
9606 BEGV = marker_position (oldbegv);
9607 BEGV_BYTE = marker_byte_position (oldbegv);
9608
9609 if (zv_at_end)
9610 {
9611 ZV = Z;
9612 ZV_BYTE = Z_BYTE;
9613 }
9614 else
9615 {
9616 ZV = marker_position (oldzv);
9617 ZV_BYTE = marker_byte_position (oldzv);
9618 }
9619
9620 if (point_at_end)
9621 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9622 else
9623 /* We can't do Fgoto_char (oldpoint) because it will run some
9624 Lisp code. */
9625 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9626 marker_byte_position (oldpoint));
9627
9628 UNGCPRO;
9629 unchain_marker (XMARKER (oldpoint));
9630 unchain_marker (XMARKER (oldbegv));
9631 unchain_marker (XMARKER (oldzv));
9632
9633 shown = buffer_window_count (current_buffer) > 0;
9634 set_buffer_internal (oldbuf);
9635 /* We called insert_1_both above with its 5th argument (PREPARE)
9636 zero, which prevents insert_1_both from calling
9637 prepare_to_modify_buffer, which in turns prevents us from
9638 incrementing windows_or_buffers_changed even if *Messages* is
9639 shown in some window. So we must manually incrementing
9640 windows_or_buffers_changed here to make up for that. */
9641 if (shown)
9642 windows_or_buffers_changed++;
9643 else
9644 windows_or_buffers_changed = old_windows_or_buffers_changed;
9645 message_log_need_newline = !nlflag;
9646 Vdeactivate_mark = old_deactivate_mark;
9647 }
9648 }
9649
9650
9651 /* We are at the end of the buffer after just having inserted a newline.
9652 (Note: We depend on the fact we won't be crossing the gap.)
9653 Check to see if the most recent message looks a lot like the previous one.
9654 Return 0 if different, 1 if the new one should just replace it, or a
9655 value N > 1 if we should also append " [N times]". */
9656
9657 static intmax_t
9658 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9659 {
9660 ptrdiff_t i;
9661 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9662 int seen_dots = 0;
9663 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9664 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9665
9666 for (i = 0; i < len; i++)
9667 {
9668 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9669 seen_dots = 1;
9670 if (p1[i] != p2[i])
9671 return seen_dots;
9672 }
9673 p1 += len;
9674 if (*p1 == '\n')
9675 return 2;
9676 if (*p1++ == ' ' && *p1++ == '[')
9677 {
9678 char *pend;
9679 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9680 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9681 return n + 1;
9682 }
9683 return 0;
9684 }
9685 \f
9686
9687 /* Display an echo area message M with a specified length of NBYTES
9688 bytes. The string may include null characters. If M is not a
9689 string, clear out any existing message, and let the mini-buffer
9690 text show through.
9691
9692 This function cancels echoing. */
9693
9694 void
9695 message3 (Lisp_Object m)
9696 {
9697 struct gcpro gcpro1;
9698
9699 GCPRO1 (m);
9700 clear_message (1,1);
9701 cancel_echoing ();
9702
9703 /* First flush out any partial line written with print. */
9704 message_log_maybe_newline ();
9705 if (STRINGP (m))
9706 {
9707 ptrdiff_t nbytes = SBYTES (m);
9708 bool multibyte = STRING_MULTIBYTE (m);
9709 USE_SAFE_ALLOCA;
9710 char *buffer = SAFE_ALLOCA (nbytes);
9711 memcpy (buffer, SDATA (m), nbytes);
9712 message_dolog (buffer, nbytes, 1, multibyte);
9713 SAFE_FREE ();
9714 }
9715 message3_nolog (m);
9716
9717 UNGCPRO;
9718 }
9719
9720
9721 /* The non-logging version of message3.
9722 This does not cancel echoing, because it is used for echoing.
9723 Perhaps we need to make a separate function for echoing
9724 and make this cancel echoing. */
9725
9726 void
9727 message3_nolog (Lisp_Object m)
9728 {
9729 struct frame *sf = SELECTED_FRAME ();
9730
9731 if (FRAME_INITIAL_P (sf))
9732 {
9733 if (noninteractive_need_newline)
9734 putc ('\n', stderr);
9735 noninteractive_need_newline = 0;
9736 if (STRINGP (m))
9737 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9738 if (cursor_in_echo_area == 0)
9739 fprintf (stderr, "\n");
9740 fflush (stderr);
9741 }
9742 /* Error messages get reported properly by cmd_error, so this must be just an
9743 informative message; if the frame hasn't really been initialized yet, just
9744 toss it. */
9745 else if (INTERACTIVE && sf->glyphs_initialized_p)
9746 {
9747 /* Get the frame containing the mini-buffer
9748 that the selected frame is using. */
9749 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9750 Lisp_Object frame = XWINDOW (mini_window)->frame;
9751 struct frame *f = XFRAME (frame);
9752
9753 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9754 Fmake_frame_visible (frame);
9755
9756 if (STRINGP (m) && SCHARS (m) > 0)
9757 {
9758 set_message (m);
9759 if (minibuffer_auto_raise)
9760 Fraise_frame (frame);
9761 /* Assume we are not echoing.
9762 (If we are, echo_now will override this.) */
9763 echo_message_buffer = Qnil;
9764 }
9765 else
9766 clear_message (1, 1);
9767
9768 do_pending_window_change (0);
9769 echo_area_display (1);
9770 do_pending_window_change (0);
9771 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9772 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9773 }
9774 }
9775
9776
9777 /* Display a null-terminated echo area message M. If M is 0, clear
9778 out any existing message, and let the mini-buffer text show through.
9779
9780 The buffer M must continue to exist until after the echo area gets
9781 cleared or some other message gets displayed there. Do not pass
9782 text that is stored in a Lisp string. Do not pass text in a buffer
9783 that was alloca'd. */
9784
9785 void
9786 message1 (const char *m)
9787 {
9788 message3 (m ? build_unibyte_string (m) : Qnil);
9789 }
9790
9791
9792 /* The non-logging counterpart of message1. */
9793
9794 void
9795 message1_nolog (const char *m)
9796 {
9797 message3_nolog (m ? build_unibyte_string (m) : Qnil);
9798 }
9799
9800 /* Display a message M which contains a single %s
9801 which gets replaced with STRING. */
9802
9803 void
9804 message_with_string (const char *m, Lisp_Object string, int log)
9805 {
9806 CHECK_STRING (string);
9807
9808 if (noninteractive)
9809 {
9810 if (m)
9811 {
9812 if (noninteractive_need_newline)
9813 putc ('\n', stderr);
9814 noninteractive_need_newline = 0;
9815 fprintf (stderr, m, SDATA (string));
9816 if (!cursor_in_echo_area)
9817 fprintf (stderr, "\n");
9818 fflush (stderr);
9819 }
9820 }
9821 else if (INTERACTIVE)
9822 {
9823 /* The frame whose minibuffer we're going to display the message on.
9824 It may be larger than the selected frame, so we need
9825 to use its buffer, not the selected frame's buffer. */
9826 Lisp_Object mini_window;
9827 struct frame *f, *sf = SELECTED_FRAME ();
9828
9829 /* Get the frame containing the minibuffer
9830 that the selected frame is using. */
9831 mini_window = FRAME_MINIBUF_WINDOW (sf);
9832 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9833
9834 /* Error messages get reported properly by cmd_error, so this must be
9835 just an informative message; if the frame hasn't really been
9836 initialized yet, just toss it. */
9837 if (f->glyphs_initialized_p)
9838 {
9839 Lisp_Object args[2], msg;
9840 struct gcpro gcpro1, gcpro2;
9841
9842 args[0] = build_string (m);
9843 args[1] = msg = string;
9844 GCPRO2 (args[0], msg);
9845 gcpro1.nvars = 2;
9846
9847 msg = Fformat (2, args);
9848
9849 if (log)
9850 message3 (msg);
9851 else
9852 message3_nolog (msg);
9853
9854 UNGCPRO;
9855
9856 /* Print should start at the beginning of the message
9857 buffer next time. */
9858 message_buf_print = 0;
9859 }
9860 }
9861 }
9862
9863
9864 /* Dump an informative message to the minibuf. If M is 0, clear out
9865 any existing message, and let the mini-buffer text show through. */
9866
9867 static void
9868 vmessage (const char *m, va_list ap)
9869 {
9870 if (noninteractive)
9871 {
9872 if (m)
9873 {
9874 if (noninteractive_need_newline)
9875 putc ('\n', stderr);
9876 noninteractive_need_newline = 0;
9877 vfprintf (stderr, m, ap);
9878 if (cursor_in_echo_area == 0)
9879 fprintf (stderr, "\n");
9880 fflush (stderr);
9881 }
9882 }
9883 else if (INTERACTIVE)
9884 {
9885 /* The frame whose mini-buffer we're going to display the message
9886 on. It may be larger than the selected frame, so we need to
9887 use its buffer, not the selected frame's buffer. */
9888 Lisp_Object mini_window;
9889 struct frame *f, *sf = SELECTED_FRAME ();
9890
9891 /* Get the frame containing the mini-buffer
9892 that the selected frame is using. */
9893 mini_window = FRAME_MINIBUF_WINDOW (sf);
9894 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9895
9896 /* Error messages get reported properly by cmd_error, so this must be
9897 just an informative message; if the frame hasn't really been
9898 initialized yet, just toss it. */
9899 if (f->glyphs_initialized_p)
9900 {
9901 if (m)
9902 {
9903 ptrdiff_t len;
9904 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9905 char *message_buf = alloca (maxsize + 1);
9906
9907 len = doprnt (message_buf, maxsize, m, 0, ap);
9908
9909 message3 (make_string (message_buf, len));
9910 }
9911 else
9912 message1 (0);
9913
9914 /* Print should start at the beginning of the message
9915 buffer next time. */
9916 message_buf_print = 0;
9917 }
9918 }
9919 }
9920
9921 void
9922 message (const char *m, ...)
9923 {
9924 va_list ap;
9925 va_start (ap, m);
9926 vmessage (m, ap);
9927 va_end (ap);
9928 }
9929
9930
9931 #if 0
9932 /* The non-logging version of message. */
9933
9934 void
9935 message_nolog (const char *m, ...)
9936 {
9937 Lisp_Object old_log_max;
9938 va_list ap;
9939 va_start (ap, m);
9940 old_log_max = Vmessage_log_max;
9941 Vmessage_log_max = Qnil;
9942 vmessage (m, ap);
9943 Vmessage_log_max = old_log_max;
9944 va_end (ap);
9945 }
9946 #endif
9947
9948
9949 /* Display the current message in the current mini-buffer. This is
9950 only called from error handlers in process.c, and is not time
9951 critical. */
9952
9953 void
9954 update_echo_area (void)
9955 {
9956 if (!NILP (echo_area_buffer[0]))
9957 {
9958 Lisp_Object string;
9959 string = Fcurrent_message ();
9960 message3 (string);
9961 }
9962 }
9963
9964
9965 /* Make sure echo area buffers in `echo_buffers' are live.
9966 If they aren't, make new ones. */
9967
9968 static void
9969 ensure_echo_area_buffers (void)
9970 {
9971 int i;
9972
9973 for (i = 0; i < 2; ++i)
9974 if (!BUFFERP (echo_buffer[i])
9975 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9976 {
9977 char name[30];
9978 Lisp_Object old_buffer;
9979 int j;
9980
9981 old_buffer = echo_buffer[i];
9982 echo_buffer[i] = Fget_buffer_create
9983 (make_formatted_string (name, " *Echo Area %d*", i));
9984 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9985 /* to force word wrap in echo area -
9986 it was decided to postpone this*/
9987 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9988
9989 for (j = 0; j < 2; ++j)
9990 if (EQ (old_buffer, echo_area_buffer[j]))
9991 echo_area_buffer[j] = echo_buffer[i];
9992 }
9993 }
9994
9995
9996 /* Call FN with args A1..A2 with either the current or last displayed
9997 echo_area_buffer as current buffer.
9998
9999 WHICH zero means use the current message buffer
10000 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10001 from echo_buffer[] and clear it.
10002
10003 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10004 suitable buffer from echo_buffer[] and clear it.
10005
10006 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10007 that the current message becomes the last displayed one, make
10008 choose a suitable buffer for echo_area_buffer[0], and clear it.
10009
10010 Value is what FN returns. */
10011
10012 static int
10013 with_echo_area_buffer (struct window *w, int which,
10014 int (*fn) (ptrdiff_t, Lisp_Object),
10015 ptrdiff_t a1, Lisp_Object a2)
10016 {
10017 Lisp_Object buffer;
10018 int this_one, the_other, clear_buffer_p, rc;
10019 ptrdiff_t count = SPECPDL_INDEX ();
10020
10021 /* If buffers aren't live, make new ones. */
10022 ensure_echo_area_buffers ();
10023
10024 clear_buffer_p = 0;
10025
10026 if (which == 0)
10027 this_one = 0, the_other = 1;
10028 else if (which > 0)
10029 this_one = 1, the_other = 0;
10030 else
10031 {
10032 this_one = 0, the_other = 1;
10033 clear_buffer_p = 1;
10034
10035 /* We need a fresh one in case the current echo buffer equals
10036 the one containing the last displayed echo area message. */
10037 if (!NILP (echo_area_buffer[this_one])
10038 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10039 echo_area_buffer[this_one] = Qnil;
10040 }
10041
10042 /* Choose a suitable buffer from echo_buffer[] is we don't
10043 have one. */
10044 if (NILP (echo_area_buffer[this_one]))
10045 {
10046 echo_area_buffer[this_one]
10047 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10048 ? echo_buffer[the_other]
10049 : echo_buffer[this_one]);
10050 clear_buffer_p = 1;
10051 }
10052
10053 buffer = echo_area_buffer[this_one];
10054
10055 /* Don't get confused by reusing the buffer used for echoing
10056 for a different purpose. */
10057 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10058 cancel_echoing ();
10059
10060 record_unwind_protect (unwind_with_echo_area_buffer,
10061 with_echo_area_buffer_unwind_data (w));
10062
10063 /* Make the echo area buffer current. Note that for display
10064 purposes, it is not necessary that the displayed window's buffer
10065 == current_buffer, except for text property lookup. So, let's
10066 only set that buffer temporarily here without doing a full
10067 Fset_window_buffer. We must also change w->pointm, though,
10068 because otherwise an assertions in unshow_buffer fails, and Emacs
10069 aborts. */
10070 set_buffer_internal_1 (XBUFFER (buffer));
10071 if (w)
10072 {
10073 wset_buffer (w, buffer);
10074 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10075 }
10076
10077 bset_undo_list (current_buffer, Qt);
10078 bset_read_only (current_buffer, Qnil);
10079 specbind (Qinhibit_read_only, Qt);
10080 specbind (Qinhibit_modification_hooks, Qt);
10081
10082 if (clear_buffer_p && Z > BEG)
10083 del_range (BEG, Z);
10084
10085 eassert (BEGV >= BEG);
10086 eassert (ZV <= Z && ZV >= BEGV);
10087
10088 rc = fn (a1, a2);
10089
10090 eassert (BEGV >= BEG);
10091 eassert (ZV <= Z && ZV >= BEGV);
10092
10093 unbind_to (count, Qnil);
10094 return rc;
10095 }
10096
10097
10098 /* Save state that should be preserved around the call to the function
10099 FN called in with_echo_area_buffer. */
10100
10101 static Lisp_Object
10102 with_echo_area_buffer_unwind_data (struct window *w)
10103 {
10104 int i = 0;
10105 Lisp_Object vector, tmp;
10106
10107 /* Reduce consing by keeping one vector in
10108 Vwith_echo_area_save_vector. */
10109 vector = Vwith_echo_area_save_vector;
10110 Vwith_echo_area_save_vector = Qnil;
10111
10112 if (NILP (vector))
10113 vector = Fmake_vector (make_number (9), Qnil);
10114
10115 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10116 ASET (vector, i, Vdeactivate_mark); ++i;
10117 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10118
10119 if (w)
10120 {
10121 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10122 ASET (vector, i, w->contents); ++i;
10123 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10124 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10125 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10126 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10127 }
10128 else
10129 {
10130 int end = i + 6;
10131 for (; i < end; ++i)
10132 ASET (vector, i, Qnil);
10133 }
10134
10135 eassert (i == ASIZE (vector));
10136 return vector;
10137 }
10138
10139
10140 /* Restore global state from VECTOR which was created by
10141 with_echo_area_buffer_unwind_data. */
10142
10143 static void
10144 unwind_with_echo_area_buffer (Lisp_Object vector)
10145 {
10146 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10147 Vdeactivate_mark = AREF (vector, 1);
10148 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10149
10150 if (WINDOWP (AREF (vector, 3)))
10151 {
10152 struct window *w;
10153 Lisp_Object buffer;
10154
10155 w = XWINDOW (AREF (vector, 3));
10156 buffer = AREF (vector, 4);
10157
10158 wset_buffer (w, buffer);
10159 set_marker_both (w->pointm, buffer,
10160 XFASTINT (AREF (vector, 5)),
10161 XFASTINT (AREF (vector, 6)));
10162 set_marker_both (w->start, buffer,
10163 XFASTINT (AREF (vector, 7)),
10164 XFASTINT (AREF (vector, 8)));
10165 }
10166
10167 Vwith_echo_area_save_vector = vector;
10168 }
10169
10170
10171 /* Set up the echo area for use by print functions. MULTIBYTE_P
10172 non-zero means we will print multibyte. */
10173
10174 void
10175 setup_echo_area_for_printing (int multibyte_p)
10176 {
10177 /* If we can't find an echo area any more, exit. */
10178 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10179 Fkill_emacs (Qnil);
10180
10181 ensure_echo_area_buffers ();
10182
10183 if (!message_buf_print)
10184 {
10185 /* A message has been output since the last time we printed.
10186 Choose a fresh echo area buffer. */
10187 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10188 echo_area_buffer[0] = echo_buffer[1];
10189 else
10190 echo_area_buffer[0] = echo_buffer[0];
10191
10192 /* Switch to that buffer and clear it. */
10193 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10194 bset_truncate_lines (current_buffer, Qnil);
10195
10196 if (Z > BEG)
10197 {
10198 ptrdiff_t count = SPECPDL_INDEX ();
10199 specbind (Qinhibit_read_only, Qt);
10200 /* Note that undo recording is always disabled. */
10201 del_range (BEG, Z);
10202 unbind_to (count, Qnil);
10203 }
10204 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10205
10206 /* Set up the buffer for the multibyteness we need. */
10207 if (multibyte_p
10208 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10209 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10210
10211 /* Raise the frame containing the echo area. */
10212 if (minibuffer_auto_raise)
10213 {
10214 struct frame *sf = SELECTED_FRAME ();
10215 Lisp_Object mini_window;
10216 mini_window = FRAME_MINIBUF_WINDOW (sf);
10217 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10218 }
10219
10220 message_log_maybe_newline ();
10221 message_buf_print = 1;
10222 }
10223 else
10224 {
10225 if (NILP (echo_area_buffer[0]))
10226 {
10227 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10228 echo_area_buffer[0] = echo_buffer[1];
10229 else
10230 echo_area_buffer[0] = echo_buffer[0];
10231 }
10232
10233 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10234 {
10235 /* Someone switched buffers between print requests. */
10236 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10237 bset_truncate_lines (current_buffer, Qnil);
10238 }
10239 }
10240 }
10241
10242
10243 /* Display an echo area message in window W. Value is non-zero if W's
10244 height is changed. If display_last_displayed_message_p is
10245 non-zero, display the message that was last displayed, otherwise
10246 display the current message. */
10247
10248 static int
10249 display_echo_area (struct window *w)
10250 {
10251 int i, no_message_p, window_height_changed_p;
10252
10253 /* Temporarily disable garbage collections while displaying the echo
10254 area. This is done because a GC can print a message itself.
10255 That message would modify the echo area buffer's contents while a
10256 redisplay of the buffer is going on, and seriously confuse
10257 redisplay. */
10258 ptrdiff_t count = inhibit_garbage_collection ();
10259
10260 /* If there is no message, we must call display_echo_area_1
10261 nevertheless because it resizes the window. But we will have to
10262 reset the echo_area_buffer in question to nil at the end because
10263 with_echo_area_buffer will sets it to an empty buffer. */
10264 i = display_last_displayed_message_p ? 1 : 0;
10265 no_message_p = NILP (echo_area_buffer[i]);
10266
10267 window_height_changed_p
10268 = with_echo_area_buffer (w, display_last_displayed_message_p,
10269 display_echo_area_1,
10270 (intptr_t) w, Qnil);
10271
10272 if (no_message_p)
10273 echo_area_buffer[i] = Qnil;
10274
10275 unbind_to (count, Qnil);
10276 return window_height_changed_p;
10277 }
10278
10279
10280 /* Helper for display_echo_area. Display the current buffer which
10281 contains the current echo area message in window W, a mini-window,
10282 a pointer to which is passed in A1. A2..A4 are currently not used.
10283 Change the height of W so that all of the message is displayed.
10284 Value is non-zero if height of W was changed. */
10285
10286 static int
10287 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10288 {
10289 intptr_t i1 = a1;
10290 struct window *w = (struct window *) i1;
10291 Lisp_Object window;
10292 struct text_pos start;
10293 int window_height_changed_p = 0;
10294
10295 /* Do this before displaying, so that we have a large enough glyph
10296 matrix for the display. If we can't get enough space for the
10297 whole text, display the last N lines. That works by setting w->start. */
10298 window_height_changed_p = resize_mini_window (w, 0);
10299
10300 /* Use the starting position chosen by resize_mini_window. */
10301 SET_TEXT_POS_FROM_MARKER (start, w->start);
10302
10303 /* Display. */
10304 clear_glyph_matrix (w->desired_matrix);
10305 XSETWINDOW (window, w);
10306 try_window (window, start, 0);
10307
10308 return window_height_changed_p;
10309 }
10310
10311
10312 /* Resize the echo area window to exactly the size needed for the
10313 currently displayed message, if there is one. If a mini-buffer
10314 is active, don't shrink it. */
10315
10316 void
10317 resize_echo_area_exactly (void)
10318 {
10319 if (BUFFERP (echo_area_buffer[0])
10320 && WINDOWP (echo_area_window))
10321 {
10322 struct window *w = XWINDOW (echo_area_window);
10323 int resized_p;
10324 Lisp_Object resize_exactly;
10325
10326 if (minibuf_level == 0)
10327 resize_exactly = Qt;
10328 else
10329 resize_exactly = Qnil;
10330
10331 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10332 (intptr_t) w, resize_exactly);
10333 if (resized_p)
10334 {
10335 ++windows_or_buffers_changed;
10336 ++update_mode_lines;
10337 redisplay_internal ();
10338 }
10339 }
10340 }
10341
10342
10343 /* Callback function for with_echo_area_buffer, when used from
10344 resize_echo_area_exactly. A1 contains a pointer to the window to
10345 resize, EXACTLY non-nil means resize the mini-window exactly to the
10346 size of the text displayed. A3 and A4 are not used. Value is what
10347 resize_mini_window returns. */
10348
10349 static int
10350 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10351 {
10352 intptr_t i1 = a1;
10353 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10354 }
10355
10356
10357 /* Resize mini-window W to fit the size of its contents. EXACT_P
10358 means size the window exactly to the size needed. Otherwise, it's
10359 only enlarged until W's buffer is empty.
10360
10361 Set W->start to the right place to begin display. If the whole
10362 contents fit, start at the beginning. Otherwise, start so as
10363 to make the end of the contents appear. This is particularly
10364 important for y-or-n-p, but seems desirable generally.
10365
10366 Value is non-zero if the window height has been changed. */
10367
10368 int
10369 resize_mini_window (struct window *w, int exact_p)
10370 {
10371 struct frame *f = XFRAME (w->frame);
10372 int window_height_changed_p = 0;
10373
10374 eassert (MINI_WINDOW_P (w));
10375
10376 /* By default, start display at the beginning. */
10377 set_marker_both (w->start, w->contents,
10378 BUF_BEGV (XBUFFER (w->contents)),
10379 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10380
10381 /* Don't resize windows while redisplaying a window; it would
10382 confuse redisplay functions when the size of the window they are
10383 displaying changes from under them. Such a resizing can happen,
10384 for instance, when which-func prints a long message while
10385 we are running fontification-functions. We're running these
10386 functions with safe_call which binds inhibit-redisplay to t. */
10387 if (!NILP (Vinhibit_redisplay))
10388 return 0;
10389
10390 /* Nil means don't try to resize. */
10391 if (NILP (Vresize_mini_windows)
10392 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10393 return 0;
10394
10395 if (!FRAME_MINIBUF_ONLY_P (f))
10396 {
10397 struct it it;
10398 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10399 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10400 int height;
10401 EMACS_INT max_height;
10402 int unit = FRAME_LINE_HEIGHT (f);
10403 struct text_pos start;
10404 struct buffer *old_current_buffer = NULL;
10405
10406 if (current_buffer != XBUFFER (w->contents))
10407 {
10408 old_current_buffer = current_buffer;
10409 set_buffer_internal (XBUFFER (w->contents));
10410 }
10411
10412 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10413
10414 /* Compute the max. number of lines specified by the user. */
10415 if (FLOATP (Vmax_mini_window_height))
10416 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10417 else if (INTEGERP (Vmax_mini_window_height))
10418 max_height = XINT (Vmax_mini_window_height);
10419 else
10420 max_height = total_height / 4;
10421
10422 /* Correct that max. height if it's bogus. */
10423 max_height = clip_to_bounds (1, max_height, total_height);
10424
10425 /* Find out the height of the text in the window. */
10426 if (it.line_wrap == TRUNCATE)
10427 height = 1;
10428 else
10429 {
10430 last_height = 0;
10431 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10432 if (it.max_ascent == 0 && it.max_descent == 0)
10433 height = it.current_y + last_height;
10434 else
10435 height = it.current_y + it.max_ascent + it.max_descent;
10436 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10437 height = (height + unit - 1) / unit;
10438 }
10439
10440 /* Compute a suitable window start. */
10441 if (height > max_height)
10442 {
10443 height = max_height;
10444 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10445 move_it_vertically_backward (&it, (height - 1) * unit);
10446 start = it.current.pos;
10447 }
10448 else
10449 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10450 SET_MARKER_FROM_TEXT_POS (w->start, start);
10451
10452 if (EQ (Vresize_mini_windows, Qgrow_only))
10453 {
10454 /* Let it grow only, until we display an empty message, in which
10455 case the window shrinks again. */
10456 if (height > WINDOW_TOTAL_LINES (w))
10457 {
10458 int old_height = WINDOW_TOTAL_LINES (w);
10459
10460 FRAME_WINDOWS_FROZEN (f) = 1;
10461 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10462 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10463 }
10464 else if (height < WINDOW_TOTAL_LINES (w)
10465 && (exact_p || BEGV == ZV))
10466 {
10467 int old_height = WINDOW_TOTAL_LINES (w);
10468
10469 FRAME_WINDOWS_FROZEN (f) = 0;
10470 shrink_mini_window (w);
10471 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10472 }
10473 }
10474 else
10475 {
10476 /* Always resize to exact size needed. */
10477 if (height > WINDOW_TOTAL_LINES (w))
10478 {
10479 int old_height = WINDOW_TOTAL_LINES (w);
10480
10481 FRAME_WINDOWS_FROZEN (f) = 1;
10482 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10483 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10484 }
10485 else if (height < WINDOW_TOTAL_LINES (w))
10486 {
10487 int old_height = WINDOW_TOTAL_LINES (w);
10488
10489 FRAME_WINDOWS_FROZEN (f) = 0;
10490 shrink_mini_window (w);
10491
10492 if (height)
10493 {
10494 FRAME_WINDOWS_FROZEN (f) = 1;
10495 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10496 }
10497
10498 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10499 }
10500 }
10501
10502 if (old_current_buffer)
10503 set_buffer_internal (old_current_buffer);
10504 }
10505
10506 return window_height_changed_p;
10507 }
10508
10509
10510 /* Value is the current message, a string, or nil if there is no
10511 current message. */
10512
10513 Lisp_Object
10514 current_message (void)
10515 {
10516 Lisp_Object msg;
10517
10518 if (!BUFFERP (echo_area_buffer[0]))
10519 msg = Qnil;
10520 else
10521 {
10522 with_echo_area_buffer (0, 0, current_message_1,
10523 (intptr_t) &msg, Qnil);
10524 if (NILP (msg))
10525 echo_area_buffer[0] = Qnil;
10526 }
10527
10528 return msg;
10529 }
10530
10531
10532 static int
10533 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10534 {
10535 intptr_t i1 = a1;
10536 Lisp_Object *msg = (Lisp_Object *) i1;
10537
10538 if (Z > BEG)
10539 *msg = make_buffer_string (BEG, Z, 1);
10540 else
10541 *msg = Qnil;
10542 return 0;
10543 }
10544
10545
10546 /* Push the current message on Vmessage_stack for later restoration
10547 by restore_message. Value is non-zero if the current message isn't
10548 empty. This is a relatively infrequent operation, so it's not
10549 worth optimizing. */
10550
10551 bool
10552 push_message (void)
10553 {
10554 Lisp_Object msg = current_message ();
10555 Vmessage_stack = Fcons (msg, Vmessage_stack);
10556 return STRINGP (msg);
10557 }
10558
10559
10560 /* Restore message display from the top of Vmessage_stack. */
10561
10562 void
10563 restore_message (void)
10564 {
10565 eassert (CONSP (Vmessage_stack));
10566 message3_nolog (XCAR (Vmessage_stack));
10567 }
10568
10569
10570 /* Handler for unwind-protect calling pop_message. */
10571
10572 void
10573 pop_message_unwind (void)
10574 {
10575 /* Pop the top-most entry off Vmessage_stack. */
10576 eassert (CONSP (Vmessage_stack));
10577 Vmessage_stack = XCDR (Vmessage_stack);
10578 }
10579
10580
10581 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10582 exits. If the stack is not empty, we have a missing pop_message
10583 somewhere. */
10584
10585 void
10586 check_message_stack (void)
10587 {
10588 if (!NILP (Vmessage_stack))
10589 emacs_abort ();
10590 }
10591
10592
10593 /* Truncate to NCHARS what will be displayed in the echo area the next
10594 time we display it---but don't redisplay it now. */
10595
10596 void
10597 truncate_echo_area (ptrdiff_t nchars)
10598 {
10599 if (nchars == 0)
10600 echo_area_buffer[0] = Qnil;
10601 else if (!noninteractive
10602 && INTERACTIVE
10603 && !NILP (echo_area_buffer[0]))
10604 {
10605 struct frame *sf = SELECTED_FRAME ();
10606 /* Error messages get reported properly by cmd_error, so this must be
10607 just an informative message; if the frame hasn't really been
10608 initialized yet, just toss it. */
10609 if (sf->glyphs_initialized_p)
10610 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10611 }
10612 }
10613
10614
10615 /* Helper function for truncate_echo_area. Truncate the current
10616 message to at most NCHARS characters. */
10617
10618 static int
10619 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10620 {
10621 if (BEG + nchars < Z)
10622 del_range (BEG + nchars, Z);
10623 if (Z == BEG)
10624 echo_area_buffer[0] = Qnil;
10625 return 0;
10626 }
10627
10628 /* Set the current message to STRING. */
10629
10630 static void
10631 set_message (Lisp_Object string)
10632 {
10633 eassert (STRINGP (string));
10634
10635 message_enable_multibyte = STRING_MULTIBYTE (string);
10636
10637 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10638 message_buf_print = 0;
10639 help_echo_showing_p = 0;
10640
10641 if (STRINGP (Vdebug_on_message)
10642 && STRINGP (string)
10643 && fast_string_match (Vdebug_on_message, string) >= 0)
10644 call_debugger (list2 (Qerror, string));
10645 }
10646
10647
10648 /* Helper function for set_message. First argument is ignored and second
10649 argument has the same meaning as for set_message.
10650 This function is called with the echo area buffer being current. */
10651
10652 static int
10653 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10654 {
10655 eassert (STRINGP (string));
10656
10657 /* Change multibyteness of the echo buffer appropriately. */
10658 if (message_enable_multibyte
10659 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10660 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10661
10662 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10663 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10664 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10665
10666 /* Insert new message at BEG. */
10667 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10668
10669 /* This function takes care of single/multibyte conversion.
10670 We just have to ensure that the echo area buffer has the right
10671 setting of enable_multibyte_characters. */
10672 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10673
10674 return 0;
10675 }
10676
10677
10678 /* Clear messages. CURRENT_P non-zero means clear the current
10679 message. LAST_DISPLAYED_P non-zero means clear the message
10680 last displayed. */
10681
10682 void
10683 clear_message (int current_p, int last_displayed_p)
10684 {
10685 if (current_p)
10686 {
10687 echo_area_buffer[0] = Qnil;
10688 message_cleared_p = 1;
10689 }
10690
10691 if (last_displayed_p)
10692 echo_area_buffer[1] = Qnil;
10693
10694 message_buf_print = 0;
10695 }
10696
10697 /* Clear garbaged frames.
10698
10699 This function is used where the old redisplay called
10700 redraw_garbaged_frames which in turn called redraw_frame which in
10701 turn called clear_frame. The call to clear_frame was a source of
10702 flickering. I believe a clear_frame is not necessary. It should
10703 suffice in the new redisplay to invalidate all current matrices,
10704 and ensure a complete redisplay of all windows. */
10705
10706 static void
10707 clear_garbaged_frames (void)
10708 {
10709 if (frame_garbaged)
10710 {
10711 Lisp_Object tail, frame;
10712 int changed_count = 0;
10713
10714 FOR_EACH_FRAME (tail, frame)
10715 {
10716 struct frame *f = XFRAME (frame);
10717
10718 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10719 {
10720 if (f->resized_p)
10721 {
10722 redraw_frame (f);
10723 f->force_flush_display_p = 1;
10724 }
10725 clear_current_matrices (f);
10726 changed_count++;
10727 f->garbaged = 0;
10728 f->resized_p = 0;
10729 }
10730 }
10731
10732 frame_garbaged = 0;
10733 if (changed_count)
10734 ++windows_or_buffers_changed;
10735 }
10736 }
10737
10738
10739 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10740 is non-zero update selected_frame. Value is non-zero if the
10741 mini-windows height has been changed. */
10742
10743 static int
10744 echo_area_display (int update_frame_p)
10745 {
10746 Lisp_Object mini_window;
10747 struct window *w;
10748 struct frame *f;
10749 int window_height_changed_p = 0;
10750 struct frame *sf = SELECTED_FRAME ();
10751
10752 mini_window = FRAME_MINIBUF_WINDOW (sf);
10753 w = XWINDOW (mini_window);
10754 f = XFRAME (WINDOW_FRAME (w));
10755
10756 /* Don't display if frame is invisible or not yet initialized. */
10757 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10758 return 0;
10759
10760 #ifdef HAVE_WINDOW_SYSTEM
10761 /* When Emacs starts, selected_frame may be the initial terminal
10762 frame. If we let this through, a message would be displayed on
10763 the terminal. */
10764 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10765 return 0;
10766 #endif /* HAVE_WINDOW_SYSTEM */
10767
10768 /* Redraw garbaged frames. */
10769 clear_garbaged_frames ();
10770
10771 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10772 {
10773 echo_area_window = mini_window;
10774 window_height_changed_p = display_echo_area (w);
10775 w->must_be_updated_p = 1;
10776
10777 /* Update the display, unless called from redisplay_internal.
10778 Also don't update the screen during redisplay itself. The
10779 update will happen at the end of redisplay, and an update
10780 here could cause confusion. */
10781 if (update_frame_p && !redisplaying_p)
10782 {
10783 int n = 0;
10784
10785 /* If the display update has been interrupted by pending
10786 input, update mode lines in the frame. Due to the
10787 pending input, it might have been that redisplay hasn't
10788 been called, so that mode lines above the echo area are
10789 garbaged. This looks odd, so we prevent it here. */
10790 if (!display_completed)
10791 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10792
10793 if (window_height_changed_p
10794 /* Don't do this if Emacs is shutting down. Redisplay
10795 needs to run hooks. */
10796 && !NILP (Vrun_hooks))
10797 {
10798 /* Must update other windows. Likewise as in other
10799 cases, don't let this update be interrupted by
10800 pending input. */
10801 ptrdiff_t count = SPECPDL_INDEX ();
10802 specbind (Qredisplay_dont_pause, Qt);
10803 windows_or_buffers_changed = 1;
10804 redisplay_internal ();
10805 unbind_to (count, Qnil);
10806 }
10807 else if (FRAME_WINDOW_P (f) && n == 0)
10808 {
10809 /* Window configuration is the same as before.
10810 Can do with a display update of the echo area,
10811 unless we displayed some mode lines. */
10812 update_single_window (w, 1);
10813 FRAME_RIF (f)->flush_display (f);
10814 }
10815 else
10816 update_frame (f, 1, 1);
10817
10818 /* If cursor is in the echo area, make sure that the next
10819 redisplay displays the minibuffer, so that the cursor will
10820 be replaced with what the minibuffer wants. */
10821 if (cursor_in_echo_area)
10822 ++windows_or_buffers_changed;
10823 }
10824 }
10825 else if (!EQ (mini_window, selected_window))
10826 windows_or_buffers_changed++;
10827
10828 /* Last displayed message is now the current message. */
10829 echo_area_buffer[1] = echo_area_buffer[0];
10830 /* Inform read_char that we're not echoing. */
10831 echo_message_buffer = Qnil;
10832
10833 /* Prevent redisplay optimization in redisplay_internal by resetting
10834 this_line_start_pos. This is done because the mini-buffer now
10835 displays the message instead of its buffer text. */
10836 if (EQ (mini_window, selected_window))
10837 CHARPOS (this_line_start_pos) = 0;
10838
10839 return window_height_changed_p;
10840 }
10841
10842 /* Nonzero if the current window's buffer is shown in more than one
10843 window and was modified since last redisplay. */
10844
10845 static int
10846 buffer_shared_and_changed (void)
10847 {
10848 return (buffer_window_count (current_buffer) > 1
10849 && UNCHANGED_MODIFIED < MODIFF);
10850 }
10851
10852 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10853 is enabled and mark of W's buffer was changed since last W's update. */
10854
10855 static int
10856 window_buffer_changed (struct window *w)
10857 {
10858 struct buffer *b = XBUFFER (w->contents);
10859
10860 eassert (BUFFER_LIVE_P (b));
10861
10862 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10863 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10864 != (w->region_showing != 0)));
10865 }
10866
10867 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10868
10869 static int
10870 mode_line_update_needed (struct window *w)
10871 {
10872 return (w->column_number_displayed != -1
10873 && !(PT == w->last_point && !window_outdated (w))
10874 && (w->column_number_displayed != current_column ()));
10875 }
10876
10877 /* Nonzero if window start of W is frozen and may not be changed during
10878 redisplay. */
10879
10880 static bool
10881 window_frozen_p (struct window *w)
10882 {
10883 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
10884 {
10885 Lisp_Object window;
10886
10887 XSETWINDOW (window, w);
10888 if (MINI_WINDOW_P (w))
10889 return 0;
10890 else if (EQ (window, selected_window))
10891 return 0;
10892 else if (MINI_WINDOW_P (XWINDOW (selected_window))
10893 && EQ (window, Vminibuf_scroll_window))
10894 /* This special window can't be frozen too. */
10895 return 0;
10896 else
10897 return 1;
10898 }
10899 return 0;
10900 }
10901
10902 /***********************************************************************
10903 Mode Lines and Frame Titles
10904 ***********************************************************************/
10905
10906 /* A buffer for constructing non-propertized mode-line strings and
10907 frame titles in it; allocated from the heap in init_xdisp and
10908 resized as needed in store_mode_line_noprop_char. */
10909
10910 static char *mode_line_noprop_buf;
10911
10912 /* The buffer's end, and a current output position in it. */
10913
10914 static char *mode_line_noprop_buf_end;
10915 static char *mode_line_noprop_ptr;
10916
10917 #define MODE_LINE_NOPROP_LEN(start) \
10918 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10919
10920 static enum {
10921 MODE_LINE_DISPLAY = 0,
10922 MODE_LINE_TITLE,
10923 MODE_LINE_NOPROP,
10924 MODE_LINE_STRING
10925 } mode_line_target;
10926
10927 /* Alist that caches the results of :propertize.
10928 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10929 static Lisp_Object mode_line_proptrans_alist;
10930
10931 /* List of strings making up the mode-line. */
10932 static Lisp_Object mode_line_string_list;
10933
10934 /* Base face property when building propertized mode line string. */
10935 static Lisp_Object mode_line_string_face;
10936 static Lisp_Object mode_line_string_face_prop;
10937
10938
10939 /* Unwind data for mode line strings */
10940
10941 static Lisp_Object Vmode_line_unwind_vector;
10942
10943 static Lisp_Object
10944 format_mode_line_unwind_data (struct frame *target_frame,
10945 struct buffer *obuf,
10946 Lisp_Object owin,
10947 int save_proptrans)
10948 {
10949 Lisp_Object vector, tmp;
10950
10951 /* Reduce consing by keeping one vector in
10952 Vwith_echo_area_save_vector. */
10953 vector = Vmode_line_unwind_vector;
10954 Vmode_line_unwind_vector = Qnil;
10955
10956 if (NILP (vector))
10957 vector = Fmake_vector (make_number (10), Qnil);
10958
10959 ASET (vector, 0, make_number (mode_line_target));
10960 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10961 ASET (vector, 2, mode_line_string_list);
10962 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10963 ASET (vector, 4, mode_line_string_face);
10964 ASET (vector, 5, mode_line_string_face_prop);
10965
10966 if (obuf)
10967 XSETBUFFER (tmp, obuf);
10968 else
10969 tmp = Qnil;
10970 ASET (vector, 6, tmp);
10971 ASET (vector, 7, owin);
10972 if (target_frame)
10973 {
10974 /* Similarly to `with-selected-window', if the operation selects
10975 a window on another frame, we must restore that frame's
10976 selected window, and (for a tty) the top-frame. */
10977 ASET (vector, 8, target_frame->selected_window);
10978 if (FRAME_TERMCAP_P (target_frame))
10979 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10980 }
10981
10982 return vector;
10983 }
10984
10985 static void
10986 unwind_format_mode_line (Lisp_Object vector)
10987 {
10988 Lisp_Object old_window = AREF (vector, 7);
10989 Lisp_Object target_frame_window = AREF (vector, 8);
10990 Lisp_Object old_top_frame = AREF (vector, 9);
10991
10992 mode_line_target = XINT (AREF (vector, 0));
10993 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10994 mode_line_string_list = AREF (vector, 2);
10995 if (! EQ (AREF (vector, 3), Qt))
10996 mode_line_proptrans_alist = AREF (vector, 3);
10997 mode_line_string_face = AREF (vector, 4);
10998 mode_line_string_face_prop = AREF (vector, 5);
10999
11000 /* Select window before buffer, since it may change the buffer. */
11001 if (!NILP (old_window))
11002 {
11003 /* If the operation that we are unwinding had selected a window
11004 on a different frame, reset its frame-selected-window. For a
11005 text terminal, reset its top-frame if necessary. */
11006 if (!NILP (target_frame_window))
11007 {
11008 Lisp_Object frame
11009 = WINDOW_FRAME (XWINDOW (target_frame_window));
11010
11011 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11012 Fselect_window (target_frame_window, Qt);
11013
11014 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11015 Fselect_frame (old_top_frame, Qt);
11016 }
11017
11018 Fselect_window (old_window, Qt);
11019 }
11020
11021 if (!NILP (AREF (vector, 6)))
11022 {
11023 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11024 ASET (vector, 6, Qnil);
11025 }
11026
11027 Vmode_line_unwind_vector = vector;
11028 }
11029
11030
11031 /* Store a single character C for the frame title in mode_line_noprop_buf.
11032 Re-allocate mode_line_noprop_buf if necessary. */
11033
11034 static void
11035 store_mode_line_noprop_char (char c)
11036 {
11037 /* If output position has reached the end of the allocated buffer,
11038 increase the buffer's size. */
11039 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11040 {
11041 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11042 ptrdiff_t size = len;
11043 mode_line_noprop_buf =
11044 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11045 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11046 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11047 }
11048
11049 *mode_line_noprop_ptr++ = c;
11050 }
11051
11052
11053 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11054 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11055 characters that yield more columns than PRECISION; PRECISION <= 0
11056 means copy the whole string. Pad with spaces until FIELD_WIDTH
11057 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11058 pad. Called from display_mode_element when it is used to build a
11059 frame title. */
11060
11061 static int
11062 store_mode_line_noprop (const char *string, int field_width, int precision)
11063 {
11064 const unsigned char *str = (const unsigned char *) string;
11065 int n = 0;
11066 ptrdiff_t dummy, nbytes;
11067
11068 /* Copy at most PRECISION chars from STR. */
11069 nbytes = strlen (string);
11070 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11071 while (nbytes--)
11072 store_mode_line_noprop_char (*str++);
11073
11074 /* Fill up with spaces until FIELD_WIDTH reached. */
11075 while (field_width > 0
11076 && n < field_width)
11077 {
11078 store_mode_line_noprop_char (' ');
11079 ++n;
11080 }
11081
11082 return n;
11083 }
11084
11085 /***********************************************************************
11086 Frame Titles
11087 ***********************************************************************/
11088
11089 #ifdef HAVE_WINDOW_SYSTEM
11090
11091 /* Set the title of FRAME, if it has changed. The title format is
11092 Vicon_title_format if FRAME is iconified, otherwise it is
11093 frame_title_format. */
11094
11095 static void
11096 x_consider_frame_title (Lisp_Object frame)
11097 {
11098 struct frame *f = XFRAME (frame);
11099
11100 if (FRAME_WINDOW_P (f)
11101 || FRAME_MINIBUF_ONLY_P (f)
11102 || f->explicit_name)
11103 {
11104 /* Do we have more than one visible frame on this X display? */
11105 Lisp_Object tail, other_frame, fmt;
11106 ptrdiff_t title_start;
11107 char *title;
11108 ptrdiff_t len;
11109 struct it it;
11110 ptrdiff_t count = SPECPDL_INDEX ();
11111
11112 FOR_EACH_FRAME (tail, other_frame)
11113 {
11114 struct frame *tf = XFRAME (other_frame);
11115
11116 if (tf != f
11117 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11118 && !FRAME_MINIBUF_ONLY_P (tf)
11119 && !EQ (other_frame, tip_frame)
11120 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11121 break;
11122 }
11123
11124 /* Set global variable indicating that multiple frames exist. */
11125 multiple_frames = CONSP (tail);
11126
11127 /* Switch to the buffer of selected window of the frame. Set up
11128 mode_line_target so that display_mode_element will output into
11129 mode_line_noprop_buf; then display the title. */
11130 record_unwind_protect (unwind_format_mode_line,
11131 format_mode_line_unwind_data
11132 (f, current_buffer, selected_window, 0));
11133
11134 Fselect_window (f->selected_window, Qt);
11135 set_buffer_internal_1
11136 (XBUFFER (XWINDOW (f->selected_window)->contents));
11137 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11138
11139 mode_line_target = MODE_LINE_TITLE;
11140 title_start = MODE_LINE_NOPROP_LEN (0);
11141 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11142 NULL, DEFAULT_FACE_ID);
11143 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11144 len = MODE_LINE_NOPROP_LEN (title_start);
11145 title = mode_line_noprop_buf + title_start;
11146 unbind_to (count, Qnil);
11147
11148 /* Set the title only if it's changed. This avoids consing in
11149 the common case where it hasn't. (If it turns out that we've
11150 already wasted too much time by walking through the list with
11151 display_mode_element, then we might need to optimize at a
11152 higher level than this.) */
11153 if (! STRINGP (f->name)
11154 || SBYTES (f->name) != len
11155 || memcmp (title, SDATA (f->name), len) != 0)
11156 x_implicitly_set_name (f, make_string (title, len), Qnil);
11157 }
11158 }
11159
11160 #endif /* not HAVE_WINDOW_SYSTEM */
11161
11162 \f
11163 /***********************************************************************
11164 Menu Bars
11165 ***********************************************************************/
11166
11167
11168 /* Prepare for redisplay by updating menu-bar item lists when
11169 appropriate. This can call eval. */
11170
11171 void
11172 prepare_menu_bars (void)
11173 {
11174 int all_windows;
11175 struct gcpro gcpro1, gcpro2;
11176 struct frame *f;
11177 Lisp_Object tooltip_frame;
11178
11179 #ifdef HAVE_WINDOW_SYSTEM
11180 tooltip_frame = tip_frame;
11181 #else
11182 tooltip_frame = Qnil;
11183 #endif
11184
11185 /* Update all frame titles based on their buffer names, etc. We do
11186 this before the menu bars so that the buffer-menu will show the
11187 up-to-date frame titles. */
11188 #ifdef HAVE_WINDOW_SYSTEM
11189 if (windows_or_buffers_changed || update_mode_lines)
11190 {
11191 Lisp_Object tail, frame;
11192
11193 FOR_EACH_FRAME (tail, frame)
11194 {
11195 f = XFRAME (frame);
11196 if (!EQ (frame, tooltip_frame)
11197 && (FRAME_ICONIFIED_P (f)
11198 || FRAME_VISIBLE_P (f) == 1
11199 /* Exclude TTY frames that are obscured because they
11200 are not the top frame on their console. This is
11201 because x_consider_frame_title actually switches
11202 to the frame, which for TTY frames means it is
11203 marked as garbaged, and will be completely
11204 redrawn on the next redisplay cycle. This causes
11205 TTY frames to be completely redrawn, when there
11206 are more than one of them, even though nothing
11207 should be changed on display. */
11208 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11209 x_consider_frame_title (frame);
11210 }
11211 }
11212 #endif /* HAVE_WINDOW_SYSTEM */
11213
11214 /* Update the menu bar item lists, if appropriate. This has to be
11215 done before any actual redisplay or generation of display lines. */
11216 all_windows = (update_mode_lines
11217 || buffer_shared_and_changed ()
11218 || windows_or_buffers_changed);
11219 if (all_windows)
11220 {
11221 Lisp_Object tail, frame;
11222 ptrdiff_t count = SPECPDL_INDEX ();
11223 /* 1 means that update_menu_bar has run its hooks
11224 so any further calls to update_menu_bar shouldn't do so again. */
11225 int menu_bar_hooks_run = 0;
11226
11227 record_unwind_save_match_data ();
11228
11229 FOR_EACH_FRAME (tail, frame)
11230 {
11231 f = XFRAME (frame);
11232
11233 /* Ignore tooltip frame. */
11234 if (EQ (frame, tooltip_frame))
11235 continue;
11236
11237 /* If a window on this frame changed size, report that to
11238 the user and clear the size-change flag. */
11239 if (FRAME_WINDOW_SIZES_CHANGED (f))
11240 {
11241 Lisp_Object functions;
11242
11243 /* Clear flag first in case we get an error below. */
11244 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11245 functions = Vwindow_size_change_functions;
11246 GCPRO2 (tail, functions);
11247
11248 while (CONSP (functions))
11249 {
11250 if (!EQ (XCAR (functions), Qt))
11251 call1 (XCAR (functions), frame);
11252 functions = XCDR (functions);
11253 }
11254 UNGCPRO;
11255 }
11256
11257 GCPRO1 (tail);
11258 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11259 #ifdef HAVE_WINDOW_SYSTEM
11260 update_tool_bar (f, 0);
11261 #endif
11262 #ifdef HAVE_NS
11263 if (windows_or_buffers_changed
11264 && FRAME_NS_P (f))
11265 ns_set_doc_edited
11266 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11267 #endif
11268 UNGCPRO;
11269 }
11270
11271 unbind_to (count, Qnil);
11272 }
11273 else
11274 {
11275 struct frame *sf = SELECTED_FRAME ();
11276 update_menu_bar (sf, 1, 0);
11277 #ifdef HAVE_WINDOW_SYSTEM
11278 update_tool_bar (sf, 1);
11279 #endif
11280 }
11281 }
11282
11283
11284 /* Update the menu bar item list for frame F. This has to be done
11285 before we start to fill in any display lines, because it can call
11286 eval.
11287
11288 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11289
11290 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11291 already ran the menu bar hooks for this redisplay, so there
11292 is no need to run them again. The return value is the
11293 updated value of this flag, to pass to the next call. */
11294
11295 static int
11296 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11297 {
11298 Lisp_Object window;
11299 register struct window *w;
11300
11301 /* If called recursively during a menu update, do nothing. This can
11302 happen when, for instance, an activate-menubar-hook causes a
11303 redisplay. */
11304 if (inhibit_menubar_update)
11305 return hooks_run;
11306
11307 window = FRAME_SELECTED_WINDOW (f);
11308 w = XWINDOW (window);
11309
11310 if (FRAME_WINDOW_P (f)
11311 ?
11312 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11313 || defined (HAVE_NS) || defined (USE_GTK)
11314 FRAME_EXTERNAL_MENU_BAR (f)
11315 #else
11316 FRAME_MENU_BAR_LINES (f) > 0
11317 #endif
11318 : FRAME_MENU_BAR_LINES (f) > 0)
11319 {
11320 /* If the user has switched buffers or windows, we need to
11321 recompute to reflect the new bindings. But we'll
11322 recompute when update_mode_lines is set too; that means
11323 that people can use force-mode-line-update to request
11324 that the menu bar be recomputed. The adverse effect on
11325 the rest of the redisplay algorithm is about the same as
11326 windows_or_buffers_changed anyway. */
11327 if (windows_or_buffers_changed
11328 /* This used to test w->update_mode_line, but we believe
11329 there is no need to recompute the menu in that case. */
11330 || update_mode_lines
11331 || window_buffer_changed (w))
11332 {
11333 struct buffer *prev = current_buffer;
11334 ptrdiff_t count = SPECPDL_INDEX ();
11335
11336 specbind (Qinhibit_menubar_update, Qt);
11337
11338 set_buffer_internal_1 (XBUFFER (w->contents));
11339 if (save_match_data)
11340 record_unwind_save_match_data ();
11341 if (NILP (Voverriding_local_map_menu_flag))
11342 {
11343 specbind (Qoverriding_terminal_local_map, Qnil);
11344 specbind (Qoverriding_local_map, Qnil);
11345 }
11346
11347 if (!hooks_run)
11348 {
11349 /* Run the Lucid hook. */
11350 safe_run_hooks (Qactivate_menubar_hook);
11351
11352 /* If it has changed current-menubar from previous value,
11353 really recompute the menu-bar from the value. */
11354 if (! NILP (Vlucid_menu_bar_dirty_flag))
11355 call0 (Qrecompute_lucid_menubar);
11356
11357 safe_run_hooks (Qmenu_bar_update_hook);
11358
11359 hooks_run = 1;
11360 }
11361
11362 XSETFRAME (Vmenu_updating_frame, f);
11363 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11364
11365 /* Redisplay the menu bar in case we changed it. */
11366 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11367 || defined (HAVE_NS) || defined (USE_GTK)
11368 if (FRAME_WINDOW_P (f))
11369 {
11370 #if defined (HAVE_NS)
11371 /* All frames on Mac OS share the same menubar. So only
11372 the selected frame should be allowed to set it. */
11373 if (f == SELECTED_FRAME ())
11374 #endif
11375 set_frame_menubar (f, 0, 0);
11376 }
11377 else
11378 /* On a terminal screen, the menu bar is an ordinary screen
11379 line, and this makes it get updated. */
11380 w->update_mode_line = 1;
11381 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11382 /* In the non-toolkit version, the menu bar is an ordinary screen
11383 line, and this makes it get updated. */
11384 w->update_mode_line = 1;
11385 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11386
11387 unbind_to (count, Qnil);
11388 set_buffer_internal_1 (prev);
11389 }
11390 }
11391
11392 return hooks_run;
11393 }
11394
11395
11396 \f
11397 /***********************************************************************
11398 Output Cursor
11399 ***********************************************************************/
11400
11401 #ifdef HAVE_WINDOW_SYSTEM
11402
11403 /* EXPORT:
11404 Nominal cursor position -- where to draw output.
11405 HPOS and VPOS are window relative glyph matrix coordinates.
11406 X and Y are window relative pixel coordinates. */
11407
11408 struct cursor_pos output_cursor;
11409
11410
11411 /* EXPORT:
11412 Set the global variable output_cursor to CURSOR. All cursor
11413 positions are relative to currently updated window. */
11414
11415 void
11416 set_output_cursor (struct cursor_pos *cursor)
11417 {
11418 output_cursor.hpos = cursor->hpos;
11419 output_cursor.vpos = cursor->vpos;
11420 output_cursor.x = cursor->x;
11421 output_cursor.y = cursor->y;
11422 }
11423
11424
11425 /* EXPORT for RIF:
11426 Set a nominal cursor position.
11427
11428 HPOS and VPOS are column/row positions in a window glyph matrix.
11429 X and Y are window text area relative pixel positions.
11430
11431 This is always done during window update, so the position is the
11432 future output cursor position for currently updated window W.
11433 NOTE: W is used only to check whether this function is called
11434 in a consistent manner via the redisplay interface. */
11435
11436 void
11437 x_cursor_to (struct window *w, int vpos, int hpos, int y, int x)
11438 {
11439 eassert (w);
11440
11441 /* Set the output cursor. */
11442 output_cursor.hpos = hpos;
11443 output_cursor.vpos = vpos;
11444 output_cursor.x = x;
11445 output_cursor.y = y;
11446 }
11447
11448 #endif /* HAVE_WINDOW_SYSTEM */
11449
11450 \f
11451 /***********************************************************************
11452 Tool-bars
11453 ***********************************************************************/
11454
11455 #ifdef HAVE_WINDOW_SYSTEM
11456
11457 /* Where the mouse was last time we reported a mouse event. */
11458
11459 struct frame *last_mouse_frame;
11460
11461 /* Tool-bar item index of the item on which a mouse button was pressed
11462 or -1. */
11463
11464 int last_tool_bar_item;
11465
11466 /* Select `frame' temporarily without running all the code in
11467 do_switch_frame.
11468 FIXME: Maybe do_switch_frame should be trimmed down similarly
11469 when `norecord' is set. */
11470 static void
11471 fast_set_selected_frame (Lisp_Object frame)
11472 {
11473 if (!EQ (selected_frame, frame))
11474 {
11475 selected_frame = frame;
11476 selected_window = XFRAME (frame)->selected_window;
11477 }
11478 }
11479
11480 /* Update the tool-bar item list for frame F. This has to be done
11481 before we start to fill in any display lines. Called from
11482 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11483 and restore it here. */
11484
11485 static void
11486 update_tool_bar (struct frame *f, int save_match_data)
11487 {
11488 #if defined (USE_GTK) || defined (HAVE_NS)
11489 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11490 #else
11491 int do_update = WINDOWP (f->tool_bar_window)
11492 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11493 #endif
11494
11495 if (do_update)
11496 {
11497 Lisp_Object window;
11498 struct window *w;
11499
11500 window = FRAME_SELECTED_WINDOW (f);
11501 w = XWINDOW (window);
11502
11503 /* If the user has switched buffers or windows, we need to
11504 recompute to reflect the new bindings. But we'll
11505 recompute when update_mode_lines is set too; that means
11506 that people can use force-mode-line-update to request
11507 that the menu bar be recomputed. The adverse effect on
11508 the rest of the redisplay algorithm is about the same as
11509 windows_or_buffers_changed anyway. */
11510 if (windows_or_buffers_changed
11511 || w->update_mode_line
11512 || update_mode_lines
11513 || window_buffer_changed (w))
11514 {
11515 struct buffer *prev = current_buffer;
11516 ptrdiff_t count = SPECPDL_INDEX ();
11517 Lisp_Object frame, new_tool_bar;
11518 int new_n_tool_bar;
11519 struct gcpro gcpro1;
11520
11521 /* Set current_buffer to the buffer of the selected
11522 window of the frame, so that we get the right local
11523 keymaps. */
11524 set_buffer_internal_1 (XBUFFER (w->contents));
11525
11526 /* Save match data, if we must. */
11527 if (save_match_data)
11528 record_unwind_save_match_data ();
11529
11530 /* Make sure that we don't accidentally use bogus keymaps. */
11531 if (NILP (Voverriding_local_map_menu_flag))
11532 {
11533 specbind (Qoverriding_terminal_local_map, Qnil);
11534 specbind (Qoverriding_local_map, Qnil);
11535 }
11536
11537 GCPRO1 (new_tool_bar);
11538
11539 /* We must temporarily set the selected frame to this frame
11540 before calling tool_bar_items, because the calculation of
11541 the tool-bar keymap uses the selected frame (see
11542 `tool-bar-make-keymap' in tool-bar.el). */
11543 eassert (EQ (selected_window,
11544 /* Since we only explicitly preserve selected_frame,
11545 check that selected_window would be redundant. */
11546 XFRAME (selected_frame)->selected_window));
11547 record_unwind_protect (fast_set_selected_frame, selected_frame);
11548 XSETFRAME (frame, f);
11549 fast_set_selected_frame (frame);
11550
11551 /* Build desired tool-bar items from keymaps. */
11552 new_tool_bar
11553 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11554 &new_n_tool_bar);
11555
11556 /* Redisplay the tool-bar if we changed it. */
11557 if (new_n_tool_bar != f->n_tool_bar_items
11558 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11559 {
11560 /* Redisplay that happens asynchronously due to an expose event
11561 may access f->tool_bar_items. Make sure we update both
11562 variables within BLOCK_INPUT so no such event interrupts. */
11563 block_input ();
11564 fset_tool_bar_items (f, new_tool_bar);
11565 f->n_tool_bar_items = new_n_tool_bar;
11566 w->update_mode_line = 1;
11567 unblock_input ();
11568 }
11569
11570 UNGCPRO;
11571
11572 unbind_to (count, Qnil);
11573 set_buffer_internal_1 (prev);
11574 }
11575 }
11576 }
11577
11578
11579 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11580 F's desired tool-bar contents. F->tool_bar_items must have
11581 been set up previously by calling prepare_menu_bars. */
11582
11583 static void
11584 build_desired_tool_bar_string (struct frame *f)
11585 {
11586 int i, size, size_needed;
11587 struct gcpro gcpro1, gcpro2, gcpro3;
11588 Lisp_Object image, plist, props;
11589
11590 image = plist = props = Qnil;
11591 GCPRO3 (image, plist, props);
11592
11593 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11594 Otherwise, make a new string. */
11595
11596 /* The size of the string we might be able to reuse. */
11597 size = (STRINGP (f->desired_tool_bar_string)
11598 ? SCHARS (f->desired_tool_bar_string)
11599 : 0);
11600
11601 /* We need one space in the string for each image. */
11602 size_needed = f->n_tool_bar_items;
11603
11604 /* Reuse f->desired_tool_bar_string, if possible. */
11605 if (size < size_needed || NILP (f->desired_tool_bar_string))
11606 fset_desired_tool_bar_string
11607 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11608 else
11609 {
11610 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11611 Fremove_text_properties (make_number (0), make_number (size),
11612 props, f->desired_tool_bar_string);
11613 }
11614
11615 /* Put a `display' property on the string for the images to display,
11616 put a `menu_item' property on tool-bar items with a value that
11617 is the index of the item in F's tool-bar item vector. */
11618 for (i = 0; i < f->n_tool_bar_items; ++i)
11619 {
11620 #define PROP(IDX) \
11621 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11622
11623 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11624 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11625 int hmargin, vmargin, relief, idx, end;
11626
11627 /* If image is a vector, choose the image according to the
11628 button state. */
11629 image = PROP (TOOL_BAR_ITEM_IMAGES);
11630 if (VECTORP (image))
11631 {
11632 if (enabled_p)
11633 idx = (selected_p
11634 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11635 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11636 else
11637 idx = (selected_p
11638 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11639 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11640
11641 eassert (ASIZE (image) >= idx);
11642 image = AREF (image, idx);
11643 }
11644 else
11645 idx = -1;
11646
11647 /* Ignore invalid image specifications. */
11648 if (!valid_image_p (image))
11649 continue;
11650
11651 /* Display the tool-bar button pressed, or depressed. */
11652 plist = Fcopy_sequence (XCDR (image));
11653
11654 /* Compute margin and relief to draw. */
11655 relief = (tool_bar_button_relief >= 0
11656 ? tool_bar_button_relief
11657 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11658 hmargin = vmargin = relief;
11659
11660 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11661 INT_MAX - max (hmargin, vmargin)))
11662 {
11663 hmargin += XFASTINT (Vtool_bar_button_margin);
11664 vmargin += XFASTINT (Vtool_bar_button_margin);
11665 }
11666 else if (CONSP (Vtool_bar_button_margin))
11667 {
11668 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11669 INT_MAX - hmargin))
11670 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11671
11672 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11673 INT_MAX - vmargin))
11674 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11675 }
11676
11677 if (auto_raise_tool_bar_buttons_p)
11678 {
11679 /* Add a `:relief' property to the image spec if the item is
11680 selected. */
11681 if (selected_p)
11682 {
11683 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11684 hmargin -= relief;
11685 vmargin -= relief;
11686 }
11687 }
11688 else
11689 {
11690 /* If image is selected, display it pressed, i.e. with a
11691 negative relief. If it's not selected, display it with a
11692 raised relief. */
11693 plist = Fplist_put (plist, QCrelief,
11694 (selected_p
11695 ? make_number (-relief)
11696 : make_number (relief)));
11697 hmargin -= relief;
11698 vmargin -= relief;
11699 }
11700
11701 /* Put a margin around the image. */
11702 if (hmargin || vmargin)
11703 {
11704 if (hmargin == vmargin)
11705 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11706 else
11707 plist = Fplist_put (plist, QCmargin,
11708 Fcons (make_number (hmargin),
11709 make_number (vmargin)));
11710 }
11711
11712 /* If button is not enabled, and we don't have special images
11713 for the disabled state, make the image appear disabled by
11714 applying an appropriate algorithm to it. */
11715 if (!enabled_p && idx < 0)
11716 plist = Fplist_put (plist, QCconversion, Qdisabled);
11717
11718 /* Put a `display' text property on the string for the image to
11719 display. Put a `menu-item' property on the string that gives
11720 the start of this item's properties in the tool-bar items
11721 vector. */
11722 image = Fcons (Qimage, plist);
11723 props = list4 (Qdisplay, image,
11724 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11725
11726 /* Let the last image hide all remaining spaces in the tool bar
11727 string. The string can be longer than needed when we reuse a
11728 previous string. */
11729 if (i + 1 == f->n_tool_bar_items)
11730 end = SCHARS (f->desired_tool_bar_string);
11731 else
11732 end = i + 1;
11733 Fadd_text_properties (make_number (i), make_number (end),
11734 props, f->desired_tool_bar_string);
11735 #undef PROP
11736 }
11737
11738 UNGCPRO;
11739 }
11740
11741
11742 /* Display one line of the tool-bar of frame IT->f.
11743
11744 HEIGHT specifies the desired height of the tool-bar line.
11745 If the actual height of the glyph row is less than HEIGHT, the
11746 row's height is increased to HEIGHT, and the icons are centered
11747 vertically in the new height.
11748
11749 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11750 count a final empty row in case the tool-bar width exactly matches
11751 the window width.
11752 */
11753
11754 static void
11755 display_tool_bar_line (struct it *it, int height)
11756 {
11757 struct glyph_row *row = it->glyph_row;
11758 int max_x = it->last_visible_x;
11759 struct glyph *last;
11760
11761 prepare_desired_row (row);
11762 row->y = it->current_y;
11763
11764 /* Note that this isn't made use of if the face hasn't a box,
11765 so there's no need to check the face here. */
11766 it->start_of_box_run_p = 1;
11767
11768 while (it->current_x < max_x)
11769 {
11770 int x, n_glyphs_before, i, nglyphs;
11771 struct it it_before;
11772
11773 /* Get the next display element. */
11774 if (!get_next_display_element (it))
11775 {
11776 /* Don't count empty row if we are counting needed tool-bar lines. */
11777 if (height < 0 && !it->hpos)
11778 return;
11779 break;
11780 }
11781
11782 /* Produce glyphs. */
11783 n_glyphs_before = row->used[TEXT_AREA];
11784 it_before = *it;
11785
11786 PRODUCE_GLYPHS (it);
11787
11788 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11789 i = 0;
11790 x = it_before.current_x;
11791 while (i < nglyphs)
11792 {
11793 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11794
11795 if (x + glyph->pixel_width > max_x)
11796 {
11797 /* Glyph doesn't fit on line. Backtrack. */
11798 row->used[TEXT_AREA] = n_glyphs_before;
11799 *it = it_before;
11800 /* If this is the only glyph on this line, it will never fit on the
11801 tool-bar, so skip it. But ensure there is at least one glyph,
11802 so we don't accidentally disable the tool-bar. */
11803 if (n_glyphs_before == 0
11804 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11805 break;
11806 goto out;
11807 }
11808
11809 ++it->hpos;
11810 x += glyph->pixel_width;
11811 ++i;
11812 }
11813
11814 /* Stop at line end. */
11815 if (ITERATOR_AT_END_OF_LINE_P (it))
11816 break;
11817
11818 set_iterator_to_next (it, 1);
11819 }
11820
11821 out:;
11822
11823 row->displays_text_p = row->used[TEXT_AREA] != 0;
11824
11825 /* Use default face for the border below the tool bar.
11826
11827 FIXME: When auto-resize-tool-bars is grow-only, there is
11828 no additional border below the possibly empty tool-bar lines.
11829 So to make the extra empty lines look "normal", we have to
11830 use the tool-bar face for the border too. */
11831 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11832 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11833 it->face_id = DEFAULT_FACE_ID;
11834
11835 extend_face_to_end_of_line (it);
11836 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11837 last->right_box_line_p = 1;
11838 if (last == row->glyphs[TEXT_AREA])
11839 last->left_box_line_p = 1;
11840
11841 /* Make line the desired height and center it vertically. */
11842 if ((height -= it->max_ascent + it->max_descent) > 0)
11843 {
11844 /* Don't add more than one line height. */
11845 height %= FRAME_LINE_HEIGHT (it->f);
11846 it->max_ascent += height / 2;
11847 it->max_descent += (height + 1) / 2;
11848 }
11849
11850 compute_line_metrics (it);
11851
11852 /* If line is empty, make it occupy the rest of the tool-bar. */
11853 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11854 {
11855 row->height = row->phys_height = it->last_visible_y - row->y;
11856 row->visible_height = row->height;
11857 row->ascent = row->phys_ascent = 0;
11858 row->extra_line_spacing = 0;
11859 }
11860
11861 row->full_width_p = 1;
11862 row->continued_p = 0;
11863 row->truncated_on_left_p = 0;
11864 row->truncated_on_right_p = 0;
11865
11866 it->current_x = it->hpos = 0;
11867 it->current_y += row->height;
11868 ++it->vpos;
11869 ++it->glyph_row;
11870 }
11871
11872
11873 /* Max tool-bar height. */
11874
11875 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11876 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11877
11878 /* Value is the number of screen lines needed to make all tool-bar
11879 items of frame F visible. The number of actual rows needed is
11880 returned in *N_ROWS if non-NULL. */
11881
11882 static int
11883 tool_bar_lines_needed (struct frame *f, int *n_rows)
11884 {
11885 struct window *w = XWINDOW (f->tool_bar_window);
11886 struct it it;
11887 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11888 the desired matrix, so use (unused) mode-line row as temporary row to
11889 avoid destroying the first tool-bar row. */
11890 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11891
11892 /* Initialize an iterator for iteration over
11893 F->desired_tool_bar_string in the tool-bar window of frame F. */
11894 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11895 it.first_visible_x = 0;
11896 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11897 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11898 it.paragraph_embedding = L2R;
11899
11900 while (!ITERATOR_AT_END_P (&it))
11901 {
11902 clear_glyph_row (temp_row);
11903 it.glyph_row = temp_row;
11904 display_tool_bar_line (&it, -1);
11905 }
11906 clear_glyph_row (temp_row);
11907
11908 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11909 if (n_rows)
11910 *n_rows = it.vpos > 0 ? it.vpos : -1;
11911
11912 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11913 }
11914
11915
11916 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11917 0, 1, 0,
11918 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11919 If FRAME is nil or omitted, use the selected frame. */)
11920 (Lisp_Object frame)
11921 {
11922 struct frame *f = decode_any_frame (frame);
11923 struct window *w;
11924 int nlines = 0;
11925
11926 if (WINDOWP (f->tool_bar_window)
11927 && (w = XWINDOW (f->tool_bar_window),
11928 WINDOW_TOTAL_LINES (w) > 0))
11929 {
11930 update_tool_bar (f, 1);
11931 if (f->n_tool_bar_items)
11932 {
11933 build_desired_tool_bar_string (f);
11934 nlines = tool_bar_lines_needed (f, NULL);
11935 }
11936 }
11937
11938 return make_number (nlines);
11939 }
11940
11941
11942 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11943 height should be changed. */
11944
11945 static int
11946 redisplay_tool_bar (struct frame *f)
11947 {
11948 struct window *w;
11949 struct it it;
11950 struct glyph_row *row;
11951
11952 #if defined (USE_GTK) || defined (HAVE_NS)
11953 if (FRAME_EXTERNAL_TOOL_BAR (f))
11954 update_frame_tool_bar (f);
11955 return 0;
11956 #endif
11957
11958 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11959 do anything. This means you must start with tool-bar-lines
11960 non-zero to get the auto-sizing effect. Or in other words, you
11961 can turn off tool-bars by specifying tool-bar-lines zero. */
11962 if (!WINDOWP (f->tool_bar_window)
11963 || (w = XWINDOW (f->tool_bar_window),
11964 WINDOW_TOTAL_LINES (w) == 0))
11965 return 0;
11966
11967 /* Set up an iterator for the tool-bar window. */
11968 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11969 it.first_visible_x = 0;
11970 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11971 row = it.glyph_row;
11972
11973 /* Build a string that represents the contents of the tool-bar. */
11974 build_desired_tool_bar_string (f);
11975 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11976 /* FIXME: This should be controlled by a user option. But it
11977 doesn't make sense to have an R2L tool bar if the menu bar cannot
11978 be drawn also R2L, and making the menu bar R2L is tricky due
11979 toolkit-specific code that implements it. If an R2L tool bar is
11980 ever supported, display_tool_bar_line should also be augmented to
11981 call unproduce_glyphs like display_line and display_string
11982 do. */
11983 it.paragraph_embedding = L2R;
11984
11985 if (f->n_tool_bar_rows == 0)
11986 {
11987 int nlines;
11988
11989 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11990 nlines != WINDOW_TOTAL_LINES (w)))
11991 {
11992 Lisp_Object frame;
11993 int old_height = WINDOW_TOTAL_LINES (w);
11994
11995 XSETFRAME (frame, f);
11996 Fmodify_frame_parameters (frame,
11997 list1 (Fcons (Qtool_bar_lines,
11998 make_number (nlines))));
11999 if (WINDOW_TOTAL_LINES (w) != old_height)
12000 {
12001 clear_glyph_matrix (w->desired_matrix);
12002 fonts_changed_p = 1;
12003 return 1;
12004 }
12005 }
12006 }
12007
12008 /* Display as many lines as needed to display all tool-bar items. */
12009
12010 if (f->n_tool_bar_rows > 0)
12011 {
12012 int border, rows, height, extra;
12013
12014 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12015 border = XINT (Vtool_bar_border);
12016 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12017 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12018 else if (EQ (Vtool_bar_border, Qborder_width))
12019 border = f->border_width;
12020 else
12021 border = 0;
12022 if (border < 0)
12023 border = 0;
12024
12025 rows = f->n_tool_bar_rows;
12026 height = max (1, (it.last_visible_y - border) / rows);
12027 extra = it.last_visible_y - border - height * rows;
12028
12029 while (it.current_y < it.last_visible_y)
12030 {
12031 int h = 0;
12032 if (extra > 0 && rows-- > 0)
12033 {
12034 h = (extra + rows - 1) / rows;
12035 extra -= h;
12036 }
12037 display_tool_bar_line (&it, height + h);
12038 }
12039 }
12040 else
12041 {
12042 while (it.current_y < it.last_visible_y)
12043 display_tool_bar_line (&it, 0);
12044 }
12045
12046 /* It doesn't make much sense to try scrolling in the tool-bar
12047 window, so don't do it. */
12048 w->desired_matrix->no_scrolling_p = 1;
12049 w->must_be_updated_p = 1;
12050
12051 if (!NILP (Vauto_resize_tool_bars))
12052 {
12053 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12054 int change_height_p = 0;
12055
12056 /* If we couldn't display everything, change the tool-bar's
12057 height if there is room for more. */
12058 if (IT_STRING_CHARPOS (it) < it.end_charpos
12059 && it.current_y < max_tool_bar_height)
12060 change_height_p = 1;
12061
12062 row = it.glyph_row - 1;
12063
12064 /* If there are blank lines at the end, except for a partially
12065 visible blank line at the end that is smaller than
12066 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12067 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12068 && row->height >= FRAME_LINE_HEIGHT (f))
12069 change_height_p = 1;
12070
12071 /* If row displays tool-bar items, but is partially visible,
12072 change the tool-bar's height. */
12073 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12074 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12075 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12076 change_height_p = 1;
12077
12078 /* Resize windows as needed by changing the `tool-bar-lines'
12079 frame parameter. */
12080 if (change_height_p)
12081 {
12082 Lisp_Object frame;
12083 int old_height = WINDOW_TOTAL_LINES (w);
12084 int nrows;
12085 int nlines = tool_bar_lines_needed (f, &nrows);
12086
12087 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12088 && !f->minimize_tool_bar_window_p)
12089 ? (nlines > old_height)
12090 : (nlines != old_height));
12091 f->minimize_tool_bar_window_p = 0;
12092
12093 if (change_height_p)
12094 {
12095 XSETFRAME (frame, f);
12096 Fmodify_frame_parameters (frame,
12097 list1 (Fcons (Qtool_bar_lines,
12098 make_number (nlines))));
12099 if (WINDOW_TOTAL_LINES (w) != old_height)
12100 {
12101 clear_glyph_matrix (w->desired_matrix);
12102 f->n_tool_bar_rows = nrows;
12103 fonts_changed_p = 1;
12104 return 1;
12105 }
12106 }
12107 }
12108 }
12109
12110 f->minimize_tool_bar_window_p = 0;
12111 return 0;
12112 }
12113
12114
12115 /* Get information about the tool-bar item which is displayed in GLYPH
12116 on frame F. Return in *PROP_IDX the index where tool-bar item
12117 properties start in F->tool_bar_items. Value is zero if
12118 GLYPH doesn't display a tool-bar item. */
12119
12120 static int
12121 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12122 {
12123 Lisp_Object prop;
12124 int success_p;
12125 int charpos;
12126
12127 /* This function can be called asynchronously, which means we must
12128 exclude any possibility that Fget_text_property signals an
12129 error. */
12130 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12131 charpos = max (0, charpos);
12132
12133 /* Get the text property `menu-item' at pos. The value of that
12134 property is the start index of this item's properties in
12135 F->tool_bar_items. */
12136 prop = Fget_text_property (make_number (charpos),
12137 Qmenu_item, f->current_tool_bar_string);
12138 if (INTEGERP (prop))
12139 {
12140 *prop_idx = XINT (prop);
12141 success_p = 1;
12142 }
12143 else
12144 success_p = 0;
12145
12146 return success_p;
12147 }
12148
12149 \f
12150 /* Get information about the tool-bar item at position X/Y on frame F.
12151 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12152 the current matrix of the tool-bar window of F, or NULL if not
12153 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12154 item in F->tool_bar_items. Value is
12155
12156 -1 if X/Y is not on a tool-bar item
12157 0 if X/Y is on the same item that was highlighted before.
12158 1 otherwise. */
12159
12160 static int
12161 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12162 int *hpos, int *vpos, int *prop_idx)
12163 {
12164 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12165 struct window *w = XWINDOW (f->tool_bar_window);
12166 int area;
12167
12168 /* Find the glyph under X/Y. */
12169 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12170 if (*glyph == NULL)
12171 return -1;
12172
12173 /* Get the start of this tool-bar item's properties in
12174 f->tool_bar_items. */
12175 if (!tool_bar_item_info (f, *glyph, prop_idx))
12176 return -1;
12177
12178 /* Is mouse on the highlighted item? */
12179 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12180 && *vpos >= hlinfo->mouse_face_beg_row
12181 && *vpos <= hlinfo->mouse_face_end_row
12182 && (*vpos > hlinfo->mouse_face_beg_row
12183 || *hpos >= hlinfo->mouse_face_beg_col)
12184 && (*vpos < hlinfo->mouse_face_end_row
12185 || *hpos < hlinfo->mouse_face_end_col
12186 || hlinfo->mouse_face_past_end))
12187 return 0;
12188
12189 return 1;
12190 }
12191
12192
12193 /* EXPORT:
12194 Handle mouse button event on the tool-bar of frame F, at
12195 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12196 0 for button release. MODIFIERS is event modifiers for button
12197 release. */
12198
12199 void
12200 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12201 int modifiers)
12202 {
12203 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12204 struct window *w = XWINDOW (f->tool_bar_window);
12205 int hpos, vpos, prop_idx;
12206 struct glyph *glyph;
12207 Lisp_Object enabled_p;
12208 int ts;
12209
12210 /* If not on the highlighted tool-bar item, and mouse-highlight is
12211 non-nil, return. This is so we generate the tool-bar button
12212 click only when the mouse button is released on the same item as
12213 where it was pressed. However, when mouse-highlight is disabled,
12214 generate the click when the button is released regardless of the
12215 highlight, since tool-bar items are not highlighted in that
12216 case. */
12217 frame_to_window_pixel_xy (w, &x, &y);
12218 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12219 if (ts == -1
12220 || (ts != 0 && !NILP (Vmouse_highlight)))
12221 return;
12222
12223 /* When mouse-highlight is off, generate the click for the item
12224 where the button was pressed, disregarding where it was
12225 released. */
12226 if (NILP (Vmouse_highlight) && !down_p)
12227 prop_idx = last_tool_bar_item;
12228
12229 /* If item is disabled, do nothing. */
12230 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12231 if (NILP (enabled_p))
12232 return;
12233
12234 if (down_p)
12235 {
12236 /* Show item in pressed state. */
12237 if (!NILP (Vmouse_highlight))
12238 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12239 last_tool_bar_item = prop_idx;
12240 }
12241 else
12242 {
12243 Lisp_Object key, frame;
12244 struct input_event event;
12245 EVENT_INIT (event);
12246
12247 /* Show item in released state. */
12248 if (!NILP (Vmouse_highlight))
12249 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12250
12251 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12252
12253 XSETFRAME (frame, f);
12254 event.kind = TOOL_BAR_EVENT;
12255 event.frame_or_window = frame;
12256 event.arg = frame;
12257 kbd_buffer_store_event (&event);
12258
12259 event.kind = TOOL_BAR_EVENT;
12260 event.frame_or_window = frame;
12261 event.arg = key;
12262 event.modifiers = modifiers;
12263 kbd_buffer_store_event (&event);
12264 last_tool_bar_item = -1;
12265 }
12266 }
12267
12268
12269 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12270 tool-bar window-relative coordinates X/Y. Called from
12271 note_mouse_highlight. */
12272
12273 static void
12274 note_tool_bar_highlight (struct frame *f, int x, int y)
12275 {
12276 Lisp_Object window = f->tool_bar_window;
12277 struct window *w = XWINDOW (window);
12278 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12279 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12280 int hpos, vpos;
12281 struct glyph *glyph;
12282 struct glyph_row *row;
12283 int i;
12284 Lisp_Object enabled_p;
12285 int prop_idx;
12286 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12287 int mouse_down_p, rc;
12288
12289 /* Function note_mouse_highlight is called with negative X/Y
12290 values when mouse moves outside of the frame. */
12291 if (x <= 0 || y <= 0)
12292 {
12293 clear_mouse_face (hlinfo);
12294 return;
12295 }
12296
12297 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12298 if (rc < 0)
12299 {
12300 /* Not on tool-bar item. */
12301 clear_mouse_face (hlinfo);
12302 return;
12303 }
12304 else if (rc == 0)
12305 /* On same tool-bar item as before. */
12306 goto set_help_echo;
12307
12308 clear_mouse_face (hlinfo);
12309
12310 /* Mouse is down, but on different tool-bar item? */
12311 mouse_down_p = (dpyinfo->grabbed
12312 && f == last_mouse_frame
12313 && FRAME_LIVE_P (f));
12314 if (mouse_down_p
12315 && last_tool_bar_item != prop_idx)
12316 return;
12317
12318 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12319
12320 /* If tool-bar item is not enabled, don't highlight it. */
12321 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12322 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12323 {
12324 /* Compute the x-position of the glyph. In front and past the
12325 image is a space. We include this in the highlighted area. */
12326 row = MATRIX_ROW (w->current_matrix, vpos);
12327 for (i = x = 0; i < hpos; ++i)
12328 x += row->glyphs[TEXT_AREA][i].pixel_width;
12329
12330 /* Record this as the current active region. */
12331 hlinfo->mouse_face_beg_col = hpos;
12332 hlinfo->mouse_face_beg_row = vpos;
12333 hlinfo->mouse_face_beg_x = x;
12334 hlinfo->mouse_face_beg_y = row->y;
12335 hlinfo->mouse_face_past_end = 0;
12336
12337 hlinfo->mouse_face_end_col = hpos + 1;
12338 hlinfo->mouse_face_end_row = vpos;
12339 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12340 hlinfo->mouse_face_end_y = row->y;
12341 hlinfo->mouse_face_window = window;
12342 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12343
12344 /* Display it as active. */
12345 show_mouse_face (hlinfo, draw);
12346 }
12347
12348 set_help_echo:
12349
12350 /* Set help_echo_string to a help string to display for this tool-bar item.
12351 XTread_socket does the rest. */
12352 help_echo_object = help_echo_window = Qnil;
12353 help_echo_pos = -1;
12354 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12355 if (NILP (help_echo_string))
12356 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12357 }
12358
12359 #endif /* HAVE_WINDOW_SYSTEM */
12360
12361
12362 \f
12363 /************************************************************************
12364 Horizontal scrolling
12365 ************************************************************************/
12366
12367 static int hscroll_window_tree (Lisp_Object);
12368 static int hscroll_windows (Lisp_Object);
12369
12370 /* For all leaf windows in the window tree rooted at WINDOW, set their
12371 hscroll value so that PT is (i) visible in the window, and (ii) so
12372 that it is not within a certain margin at the window's left and
12373 right border. Value is non-zero if any window's hscroll has been
12374 changed. */
12375
12376 static int
12377 hscroll_window_tree (Lisp_Object window)
12378 {
12379 int hscrolled_p = 0;
12380 int hscroll_relative_p = FLOATP (Vhscroll_step);
12381 int hscroll_step_abs = 0;
12382 double hscroll_step_rel = 0;
12383
12384 if (hscroll_relative_p)
12385 {
12386 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12387 if (hscroll_step_rel < 0)
12388 {
12389 hscroll_relative_p = 0;
12390 hscroll_step_abs = 0;
12391 }
12392 }
12393 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12394 {
12395 hscroll_step_abs = XINT (Vhscroll_step);
12396 if (hscroll_step_abs < 0)
12397 hscroll_step_abs = 0;
12398 }
12399 else
12400 hscroll_step_abs = 0;
12401
12402 while (WINDOWP (window))
12403 {
12404 struct window *w = XWINDOW (window);
12405
12406 if (WINDOWP (w->contents))
12407 hscrolled_p |= hscroll_window_tree (w->contents);
12408 else if (w->cursor.vpos >= 0)
12409 {
12410 int h_margin;
12411 int text_area_width;
12412 struct glyph_row *current_cursor_row
12413 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12414 struct glyph_row *desired_cursor_row
12415 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12416 struct glyph_row *cursor_row
12417 = (desired_cursor_row->enabled_p
12418 ? desired_cursor_row
12419 : current_cursor_row);
12420 int row_r2l_p = cursor_row->reversed_p;
12421
12422 text_area_width = window_box_width (w, TEXT_AREA);
12423
12424 /* Scroll when cursor is inside this scroll margin. */
12425 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12426
12427 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12428 /* For left-to-right rows, hscroll when cursor is either
12429 (i) inside the right hscroll margin, or (ii) if it is
12430 inside the left margin and the window is already
12431 hscrolled. */
12432 && ((!row_r2l_p
12433 && ((w->hscroll
12434 && w->cursor.x <= h_margin)
12435 || (cursor_row->enabled_p
12436 && cursor_row->truncated_on_right_p
12437 && (w->cursor.x >= text_area_width - h_margin))))
12438 /* For right-to-left rows, the logic is similar,
12439 except that rules for scrolling to left and right
12440 are reversed. E.g., if cursor.x <= h_margin, we
12441 need to hscroll "to the right" unconditionally,
12442 and that will scroll the screen to the left so as
12443 to reveal the next portion of the row. */
12444 || (row_r2l_p
12445 && ((cursor_row->enabled_p
12446 /* FIXME: It is confusing to set the
12447 truncated_on_right_p flag when R2L rows
12448 are actually truncated on the left. */
12449 && cursor_row->truncated_on_right_p
12450 && w->cursor.x <= h_margin)
12451 || (w->hscroll
12452 && (w->cursor.x >= text_area_width - h_margin))))))
12453 {
12454 struct it it;
12455 ptrdiff_t hscroll;
12456 struct buffer *saved_current_buffer;
12457 ptrdiff_t pt;
12458 int wanted_x;
12459
12460 /* Find point in a display of infinite width. */
12461 saved_current_buffer = current_buffer;
12462 current_buffer = XBUFFER (w->contents);
12463
12464 if (w == XWINDOW (selected_window))
12465 pt = PT;
12466 else
12467 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12468
12469 /* Move iterator to pt starting at cursor_row->start in
12470 a line with infinite width. */
12471 init_to_row_start (&it, w, cursor_row);
12472 it.last_visible_x = INFINITY;
12473 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12474 current_buffer = saved_current_buffer;
12475
12476 /* Position cursor in window. */
12477 if (!hscroll_relative_p && hscroll_step_abs == 0)
12478 hscroll = max (0, (it.current_x
12479 - (ITERATOR_AT_END_OF_LINE_P (&it)
12480 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12481 : (text_area_width / 2))))
12482 / FRAME_COLUMN_WIDTH (it.f);
12483 else if ((!row_r2l_p
12484 && w->cursor.x >= text_area_width - h_margin)
12485 || (row_r2l_p && w->cursor.x <= h_margin))
12486 {
12487 if (hscroll_relative_p)
12488 wanted_x = text_area_width * (1 - hscroll_step_rel)
12489 - h_margin;
12490 else
12491 wanted_x = text_area_width
12492 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12493 - h_margin;
12494 hscroll
12495 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12496 }
12497 else
12498 {
12499 if (hscroll_relative_p)
12500 wanted_x = text_area_width * hscroll_step_rel
12501 + h_margin;
12502 else
12503 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12504 + h_margin;
12505 hscroll
12506 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12507 }
12508 hscroll = max (hscroll, w->min_hscroll);
12509
12510 /* Don't prevent redisplay optimizations if hscroll
12511 hasn't changed, as it will unnecessarily slow down
12512 redisplay. */
12513 if (w->hscroll != hscroll)
12514 {
12515 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12516 w->hscroll = hscroll;
12517 hscrolled_p = 1;
12518 }
12519 }
12520 }
12521
12522 window = w->next;
12523 }
12524
12525 /* Value is non-zero if hscroll of any leaf window has been changed. */
12526 return hscrolled_p;
12527 }
12528
12529
12530 /* Set hscroll so that cursor is visible and not inside horizontal
12531 scroll margins for all windows in the tree rooted at WINDOW. See
12532 also hscroll_window_tree above. Value is non-zero if any window's
12533 hscroll has been changed. If it has, desired matrices on the frame
12534 of WINDOW are cleared. */
12535
12536 static int
12537 hscroll_windows (Lisp_Object window)
12538 {
12539 int hscrolled_p = hscroll_window_tree (window);
12540 if (hscrolled_p)
12541 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12542 return hscrolled_p;
12543 }
12544
12545
12546 \f
12547 /************************************************************************
12548 Redisplay
12549 ************************************************************************/
12550
12551 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12552 to a non-zero value. This is sometimes handy to have in a debugger
12553 session. */
12554
12555 #ifdef GLYPH_DEBUG
12556
12557 /* First and last unchanged row for try_window_id. */
12558
12559 static int debug_first_unchanged_at_end_vpos;
12560 static int debug_last_unchanged_at_beg_vpos;
12561
12562 /* Delta vpos and y. */
12563
12564 static int debug_dvpos, debug_dy;
12565
12566 /* Delta in characters and bytes for try_window_id. */
12567
12568 static ptrdiff_t debug_delta, debug_delta_bytes;
12569
12570 /* Values of window_end_pos and window_end_vpos at the end of
12571 try_window_id. */
12572
12573 static ptrdiff_t debug_end_vpos;
12574
12575 /* Append a string to W->desired_matrix->method. FMT is a printf
12576 format string. If trace_redisplay_p is non-zero also printf the
12577 resulting string to stderr. */
12578
12579 static void debug_method_add (struct window *, char const *, ...)
12580 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12581
12582 static void
12583 debug_method_add (struct window *w, char const *fmt, ...)
12584 {
12585 void *ptr = w;
12586 char *method = w->desired_matrix->method;
12587 int len = strlen (method);
12588 int size = sizeof w->desired_matrix->method;
12589 int remaining = size - len - 1;
12590 va_list ap;
12591
12592 if (len && remaining)
12593 {
12594 method[len] = '|';
12595 --remaining, ++len;
12596 }
12597
12598 va_start (ap, fmt);
12599 vsnprintf (method + len, remaining + 1, fmt, ap);
12600 va_end (ap);
12601
12602 if (trace_redisplay_p)
12603 fprintf (stderr, "%p (%s): %s\n",
12604 ptr,
12605 ((BUFFERP (w->contents)
12606 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12607 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12608 : "no buffer"),
12609 method + len);
12610 }
12611
12612 #endif /* GLYPH_DEBUG */
12613
12614
12615 /* Value is non-zero if all changes in window W, which displays
12616 current_buffer, are in the text between START and END. START is a
12617 buffer position, END is given as a distance from Z. Used in
12618 redisplay_internal for display optimization. */
12619
12620 static int
12621 text_outside_line_unchanged_p (struct window *w,
12622 ptrdiff_t start, ptrdiff_t end)
12623 {
12624 int unchanged_p = 1;
12625
12626 /* If text or overlays have changed, see where. */
12627 if (window_outdated (w))
12628 {
12629 /* Gap in the line? */
12630 if (GPT < start || Z - GPT < end)
12631 unchanged_p = 0;
12632
12633 /* Changes start in front of the line, or end after it? */
12634 if (unchanged_p
12635 && (BEG_UNCHANGED < start - 1
12636 || END_UNCHANGED < end))
12637 unchanged_p = 0;
12638
12639 /* If selective display, can't optimize if changes start at the
12640 beginning of the line. */
12641 if (unchanged_p
12642 && INTEGERP (BVAR (current_buffer, selective_display))
12643 && XINT (BVAR (current_buffer, selective_display)) > 0
12644 && (BEG_UNCHANGED < start || GPT <= start))
12645 unchanged_p = 0;
12646
12647 /* If there are overlays at the start or end of the line, these
12648 may have overlay strings with newlines in them. A change at
12649 START, for instance, may actually concern the display of such
12650 overlay strings as well, and they are displayed on different
12651 lines. So, quickly rule out this case. (For the future, it
12652 might be desirable to implement something more telling than
12653 just BEG/END_UNCHANGED.) */
12654 if (unchanged_p)
12655 {
12656 if (BEG + BEG_UNCHANGED == start
12657 && overlay_touches_p (start))
12658 unchanged_p = 0;
12659 if (END_UNCHANGED == end
12660 && overlay_touches_p (Z - end))
12661 unchanged_p = 0;
12662 }
12663
12664 /* Under bidi reordering, adding or deleting a character in the
12665 beginning of a paragraph, before the first strong directional
12666 character, can change the base direction of the paragraph (unless
12667 the buffer specifies a fixed paragraph direction), which will
12668 require to redisplay the whole paragraph. It might be worthwhile
12669 to find the paragraph limits and widen the range of redisplayed
12670 lines to that, but for now just give up this optimization. */
12671 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12672 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12673 unchanged_p = 0;
12674 }
12675
12676 return unchanged_p;
12677 }
12678
12679
12680 /* Do a frame update, taking possible shortcuts into account. This is
12681 the main external entry point for redisplay.
12682
12683 If the last redisplay displayed an echo area message and that message
12684 is no longer requested, we clear the echo area or bring back the
12685 mini-buffer if that is in use. */
12686
12687 void
12688 redisplay (void)
12689 {
12690 redisplay_internal ();
12691 }
12692
12693
12694 static Lisp_Object
12695 overlay_arrow_string_or_property (Lisp_Object var)
12696 {
12697 Lisp_Object val;
12698
12699 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12700 return val;
12701
12702 return Voverlay_arrow_string;
12703 }
12704
12705 /* Return 1 if there are any overlay-arrows in current_buffer. */
12706 static int
12707 overlay_arrow_in_current_buffer_p (void)
12708 {
12709 Lisp_Object vlist;
12710
12711 for (vlist = Voverlay_arrow_variable_list;
12712 CONSP (vlist);
12713 vlist = XCDR (vlist))
12714 {
12715 Lisp_Object var = XCAR (vlist);
12716 Lisp_Object val;
12717
12718 if (!SYMBOLP (var))
12719 continue;
12720 val = find_symbol_value (var);
12721 if (MARKERP (val)
12722 && current_buffer == XMARKER (val)->buffer)
12723 return 1;
12724 }
12725 return 0;
12726 }
12727
12728
12729 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12730 has changed. */
12731
12732 static int
12733 overlay_arrows_changed_p (void)
12734 {
12735 Lisp_Object vlist;
12736
12737 for (vlist = Voverlay_arrow_variable_list;
12738 CONSP (vlist);
12739 vlist = XCDR (vlist))
12740 {
12741 Lisp_Object var = XCAR (vlist);
12742 Lisp_Object val, pstr;
12743
12744 if (!SYMBOLP (var))
12745 continue;
12746 val = find_symbol_value (var);
12747 if (!MARKERP (val))
12748 continue;
12749 if (! EQ (COERCE_MARKER (val),
12750 Fget (var, Qlast_arrow_position))
12751 || ! (pstr = overlay_arrow_string_or_property (var),
12752 EQ (pstr, Fget (var, Qlast_arrow_string))))
12753 return 1;
12754 }
12755 return 0;
12756 }
12757
12758 /* Mark overlay arrows to be updated on next redisplay. */
12759
12760 static void
12761 update_overlay_arrows (int up_to_date)
12762 {
12763 Lisp_Object vlist;
12764
12765 for (vlist = Voverlay_arrow_variable_list;
12766 CONSP (vlist);
12767 vlist = XCDR (vlist))
12768 {
12769 Lisp_Object var = XCAR (vlist);
12770
12771 if (!SYMBOLP (var))
12772 continue;
12773
12774 if (up_to_date > 0)
12775 {
12776 Lisp_Object val = find_symbol_value (var);
12777 Fput (var, Qlast_arrow_position,
12778 COERCE_MARKER (val));
12779 Fput (var, Qlast_arrow_string,
12780 overlay_arrow_string_or_property (var));
12781 }
12782 else if (up_to_date < 0
12783 || !NILP (Fget (var, Qlast_arrow_position)))
12784 {
12785 Fput (var, Qlast_arrow_position, Qt);
12786 Fput (var, Qlast_arrow_string, Qt);
12787 }
12788 }
12789 }
12790
12791
12792 /* Return overlay arrow string to display at row.
12793 Return integer (bitmap number) for arrow bitmap in left fringe.
12794 Return nil if no overlay arrow. */
12795
12796 static Lisp_Object
12797 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12798 {
12799 Lisp_Object vlist;
12800
12801 for (vlist = Voverlay_arrow_variable_list;
12802 CONSP (vlist);
12803 vlist = XCDR (vlist))
12804 {
12805 Lisp_Object var = XCAR (vlist);
12806 Lisp_Object val;
12807
12808 if (!SYMBOLP (var))
12809 continue;
12810
12811 val = find_symbol_value (var);
12812
12813 if (MARKERP (val)
12814 && current_buffer == XMARKER (val)->buffer
12815 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12816 {
12817 if (FRAME_WINDOW_P (it->f)
12818 /* FIXME: if ROW->reversed_p is set, this should test
12819 the right fringe, not the left one. */
12820 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12821 {
12822 #ifdef HAVE_WINDOW_SYSTEM
12823 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12824 {
12825 int fringe_bitmap;
12826 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12827 return make_number (fringe_bitmap);
12828 }
12829 #endif
12830 return make_number (-1); /* Use default arrow bitmap. */
12831 }
12832 return overlay_arrow_string_or_property (var);
12833 }
12834 }
12835
12836 return Qnil;
12837 }
12838
12839 /* Return 1 if point moved out of or into a composition. Otherwise
12840 return 0. PREV_BUF and PREV_PT are the last point buffer and
12841 position. BUF and PT are the current point buffer and position. */
12842
12843 static int
12844 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12845 struct buffer *buf, ptrdiff_t pt)
12846 {
12847 ptrdiff_t start, end;
12848 Lisp_Object prop;
12849 Lisp_Object buffer;
12850
12851 XSETBUFFER (buffer, buf);
12852 /* Check a composition at the last point if point moved within the
12853 same buffer. */
12854 if (prev_buf == buf)
12855 {
12856 if (prev_pt == pt)
12857 /* Point didn't move. */
12858 return 0;
12859
12860 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12861 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12862 && composition_valid_p (start, end, prop)
12863 && start < prev_pt && end > prev_pt)
12864 /* The last point was within the composition. Return 1 iff
12865 point moved out of the composition. */
12866 return (pt <= start || pt >= end);
12867 }
12868
12869 /* Check a composition at the current point. */
12870 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12871 && find_composition (pt, -1, &start, &end, &prop, buffer)
12872 && composition_valid_p (start, end, prop)
12873 && start < pt && end > pt);
12874 }
12875
12876 /* Reconsider the clip changes of buffer which is displayed in W. */
12877
12878 static void
12879 reconsider_clip_changes (struct window *w)
12880 {
12881 struct buffer *b = XBUFFER (w->contents);
12882
12883 if (b->clip_changed
12884 && w->window_end_valid
12885 && w->current_matrix->buffer == b
12886 && w->current_matrix->zv == BUF_ZV (b)
12887 && w->current_matrix->begv == BUF_BEGV (b))
12888 b->clip_changed = 0;
12889
12890 /* If display wasn't paused, and W is not a tool bar window, see if
12891 point has been moved into or out of a composition. In that case,
12892 we set b->clip_changed to 1 to force updating the screen. If
12893 b->clip_changed has already been set to 1, we can skip this
12894 check. */
12895 if (!b->clip_changed && w->window_end_valid)
12896 {
12897 ptrdiff_t pt = (w == XWINDOW (selected_window)
12898 ? PT : marker_position (w->pointm));
12899
12900 if ((w->current_matrix->buffer != b || pt != w->last_point)
12901 && check_point_in_composition (w->current_matrix->buffer,
12902 w->last_point, b, pt))
12903 b->clip_changed = 1;
12904 }
12905 }
12906
12907 #define STOP_POLLING \
12908 do { if (! polling_stopped_here) stop_polling (); \
12909 polling_stopped_here = 1; } while (0)
12910
12911 #define RESUME_POLLING \
12912 do { if (polling_stopped_here) start_polling (); \
12913 polling_stopped_here = 0; } while (0)
12914
12915
12916 /* Perhaps in the future avoid recentering windows if it
12917 is not necessary; currently that causes some problems. */
12918
12919 static void
12920 redisplay_internal (void)
12921 {
12922 struct window *w = XWINDOW (selected_window);
12923 struct window *sw;
12924 struct frame *fr;
12925 int pending;
12926 bool must_finish = 0, match_p;
12927 struct text_pos tlbufpos, tlendpos;
12928 int number_of_visible_frames;
12929 ptrdiff_t count;
12930 struct frame *sf;
12931 int polling_stopped_here = 0;
12932 Lisp_Object tail, frame;
12933
12934 /* Non-zero means redisplay has to consider all windows on all
12935 frames. Zero means, only selected_window is considered. */
12936 int consider_all_windows_p;
12937
12938 /* Non-zero means redisplay has to redisplay the miniwindow. */
12939 int update_miniwindow_p = 0;
12940
12941 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12942
12943 /* No redisplay if running in batch mode or frame is not yet fully
12944 initialized, or redisplay is explicitly turned off by setting
12945 Vinhibit_redisplay. */
12946 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12947 || !NILP (Vinhibit_redisplay))
12948 return;
12949
12950 /* Don't examine these until after testing Vinhibit_redisplay.
12951 When Emacs is shutting down, perhaps because its connection to
12952 X has dropped, we should not look at them at all. */
12953 fr = XFRAME (w->frame);
12954 sf = SELECTED_FRAME ();
12955
12956 if (!fr->glyphs_initialized_p)
12957 return;
12958
12959 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12960 if (popup_activated ())
12961 return;
12962 #endif
12963
12964 /* I don't think this happens but let's be paranoid. */
12965 if (redisplaying_p)
12966 return;
12967
12968 /* Record a function that clears redisplaying_p
12969 when we leave this function. */
12970 count = SPECPDL_INDEX ();
12971 record_unwind_protect_void (unwind_redisplay);
12972 redisplaying_p = 1;
12973 specbind (Qinhibit_free_realized_faces, Qnil);
12974
12975 /* Record this function, so it appears on the profiler's backtraces. */
12976 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12977
12978 FOR_EACH_FRAME (tail, frame)
12979 XFRAME (frame)->already_hscrolled_p = 0;
12980
12981 retry:
12982 /* Remember the currently selected window. */
12983 sw = w;
12984
12985 pending = 0;
12986 last_escape_glyph_frame = NULL;
12987 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12988 last_glyphless_glyph_frame = NULL;
12989 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12990
12991 /* If new fonts have been loaded that make a glyph matrix adjustment
12992 necessary, do it. */
12993 if (fonts_changed_p)
12994 {
12995 adjust_glyphs (NULL);
12996 ++windows_or_buffers_changed;
12997 fonts_changed_p = 0;
12998 }
12999
13000 /* If face_change_count is non-zero, init_iterator will free all
13001 realized faces, which includes the faces referenced from current
13002 matrices. So, we can't reuse current matrices in this case. */
13003 if (face_change_count)
13004 ++windows_or_buffers_changed;
13005
13006 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13007 && FRAME_TTY (sf)->previous_frame != sf)
13008 {
13009 /* Since frames on a single ASCII terminal share the same
13010 display area, displaying a different frame means redisplay
13011 the whole thing. */
13012 windows_or_buffers_changed++;
13013 SET_FRAME_GARBAGED (sf);
13014 #ifndef DOS_NT
13015 set_tty_color_mode (FRAME_TTY (sf), sf);
13016 #endif
13017 FRAME_TTY (sf)->previous_frame = sf;
13018 }
13019
13020 /* Set the visible flags for all frames. Do this before checking for
13021 resized or garbaged frames; they want to know if their frames are
13022 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13023 number_of_visible_frames = 0;
13024
13025 FOR_EACH_FRAME (tail, frame)
13026 {
13027 struct frame *f = XFRAME (frame);
13028
13029 if (FRAME_VISIBLE_P (f))
13030 ++number_of_visible_frames;
13031 clear_desired_matrices (f);
13032 }
13033
13034 /* Notice any pending interrupt request to change frame size. */
13035 do_pending_window_change (1);
13036
13037 /* do_pending_window_change could change the selected_window due to
13038 frame resizing which makes the selected window too small. */
13039 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13040 sw = w;
13041
13042 /* Clear frames marked as garbaged. */
13043 clear_garbaged_frames ();
13044
13045 /* Build menubar and tool-bar items. */
13046 if (NILP (Vmemory_full))
13047 prepare_menu_bars ();
13048
13049 if (windows_or_buffers_changed)
13050 update_mode_lines++;
13051
13052 reconsider_clip_changes (w);
13053
13054 /* In most cases selected window displays current buffer. */
13055 match_p = XBUFFER (w->contents) == current_buffer;
13056 if (match_p)
13057 {
13058 ptrdiff_t count1;
13059
13060 /* Detect case that we need to write or remove a star in the mode line. */
13061 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13062 {
13063 w->update_mode_line = 1;
13064 if (buffer_shared_and_changed ())
13065 update_mode_lines++;
13066 }
13067
13068 /* Avoid invocation of point motion hooks by `current_column' below. */
13069 count1 = SPECPDL_INDEX ();
13070 specbind (Qinhibit_point_motion_hooks, Qt);
13071
13072 if (mode_line_update_needed (w))
13073 w->update_mode_line = 1;
13074
13075 unbind_to (count1, Qnil);
13076 }
13077
13078 consider_all_windows_p = (update_mode_lines
13079 || buffer_shared_and_changed ()
13080 || cursor_type_changed);
13081
13082 /* If specs for an arrow have changed, do thorough redisplay
13083 to ensure we remove any arrow that should no longer exist. */
13084 if (overlay_arrows_changed_p ())
13085 consider_all_windows_p = windows_or_buffers_changed = 1;
13086
13087 /* Normally the message* functions will have already displayed and
13088 updated the echo area, but the frame may have been trashed, or
13089 the update may have been preempted, so display the echo area
13090 again here. Checking message_cleared_p captures the case that
13091 the echo area should be cleared. */
13092 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13093 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13094 || (message_cleared_p
13095 && minibuf_level == 0
13096 /* If the mini-window is currently selected, this means the
13097 echo-area doesn't show through. */
13098 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13099 {
13100 int window_height_changed_p = echo_area_display (0);
13101
13102 if (message_cleared_p)
13103 update_miniwindow_p = 1;
13104
13105 must_finish = 1;
13106
13107 /* If we don't display the current message, don't clear the
13108 message_cleared_p flag, because, if we did, we wouldn't clear
13109 the echo area in the next redisplay which doesn't preserve
13110 the echo area. */
13111 if (!display_last_displayed_message_p)
13112 message_cleared_p = 0;
13113
13114 if (fonts_changed_p)
13115 goto retry;
13116 else if (window_height_changed_p)
13117 {
13118 consider_all_windows_p = 1;
13119 ++update_mode_lines;
13120 ++windows_or_buffers_changed;
13121
13122 /* If window configuration was changed, frames may have been
13123 marked garbaged. Clear them or we will experience
13124 surprises wrt scrolling. */
13125 clear_garbaged_frames ();
13126 }
13127 }
13128 else if (EQ (selected_window, minibuf_window)
13129 && (current_buffer->clip_changed || window_outdated (w))
13130 && resize_mini_window (w, 0))
13131 {
13132 /* Resized active mini-window to fit the size of what it is
13133 showing if its contents might have changed. */
13134 must_finish = 1;
13135 /* FIXME: this causes all frames to be updated, which seems unnecessary
13136 since only the current frame needs to be considered. This function
13137 needs to be rewritten with two variables, consider_all_windows and
13138 consider_all_frames. */
13139 consider_all_windows_p = 1;
13140 ++windows_or_buffers_changed;
13141 ++update_mode_lines;
13142
13143 /* If window configuration was changed, frames may have been
13144 marked garbaged. Clear them or we will experience
13145 surprises wrt scrolling. */
13146 clear_garbaged_frames ();
13147 }
13148
13149 /* If showing the region, and mark has changed, we must redisplay
13150 the whole window. The assignment to this_line_start_pos prevents
13151 the optimization directly below this if-statement. */
13152 if (((!NILP (Vtransient_mark_mode)
13153 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13154 != (w->region_showing > 0))
13155 || (w->region_showing
13156 && w->region_showing
13157 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13158 CHARPOS (this_line_start_pos) = 0;
13159
13160 /* Optimize the case that only the line containing the cursor in the
13161 selected window has changed. Variables starting with this_ are
13162 set in display_line and record information about the line
13163 containing the cursor. */
13164 tlbufpos = this_line_start_pos;
13165 tlendpos = this_line_end_pos;
13166 if (!consider_all_windows_p
13167 && CHARPOS (tlbufpos) > 0
13168 && !w->update_mode_line
13169 && !current_buffer->clip_changed
13170 && !current_buffer->prevent_redisplay_optimizations_p
13171 && FRAME_VISIBLE_P (XFRAME (w->frame))
13172 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13173 /* Make sure recorded data applies to current buffer, etc. */
13174 && this_line_buffer == current_buffer
13175 && match_p
13176 && !w->force_start
13177 && !w->optional_new_start
13178 /* Point must be on the line that we have info recorded about. */
13179 && PT >= CHARPOS (tlbufpos)
13180 && PT <= Z - CHARPOS (tlendpos)
13181 /* All text outside that line, including its final newline,
13182 must be unchanged. */
13183 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13184 CHARPOS (tlendpos)))
13185 {
13186 if (CHARPOS (tlbufpos) > BEGV
13187 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13188 && (CHARPOS (tlbufpos) == ZV
13189 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13190 /* Former continuation line has disappeared by becoming empty. */
13191 goto cancel;
13192 else if (window_outdated (w) || MINI_WINDOW_P (w))
13193 {
13194 /* We have to handle the case of continuation around a
13195 wide-column character (see the comment in indent.c around
13196 line 1340).
13197
13198 For instance, in the following case:
13199
13200 -------- Insert --------
13201 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13202 J_I_ ==> J_I_ `^^' are cursors.
13203 ^^ ^^
13204 -------- --------
13205
13206 As we have to redraw the line above, we cannot use this
13207 optimization. */
13208
13209 struct it it;
13210 int line_height_before = this_line_pixel_height;
13211
13212 /* Note that start_display will handle the case that the
13213 line starting at tlbufpos is a continuation line. */
13214 start_display (&it, w, tlbufpos);
13215
13216 /* Implementation note: It this still necessary? */
13217 if (it.current_x != this_line_start_x)
13218 goto cancel;
13219
13220 TRACE ((stderr, "trying display optimization 1\n"));
13221 w->cursor.vpos = -1;
13222 overlay_arrow_seen = 0;
13223 it.vpos = this_line_vpos;
13224 it.current_y = this_line_y;
13225 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13226 display_line (&it);
13227
13228 /* If line contains point, is not continued,
13229 and ends at same distance from eob as before, we win. */
13230 if (w->cursor.vpos >= 0
13231 /* Line is not continued, otherwise this_line_start_pos
13232 would have been set to 0 in display_line. */
13233 && CHARPOS (this_line_start_pos)
13234 /* Line ends as before. */
13235 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13236 /* Line has same height as before. Otherwise other lines
13237 would have to be shifted up or down. */
13238 && this_line_pixel_height == line_height_before)
13239 {
13240 /* If this is not the window's last line, we must adjust
13241 the charstarts of the lines below. */
13242 if (it.current_y < it.last_visible_y)
13243 {
13244 struct glyph_row *row
13245 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13246 ptrdiff_t delta, delta_bytes;
13247
13248 /* We used to distinguish between two cases here,
13249 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13250 when the line ends in a newline or the end of the
13251 buffer's accessible portion. But both cases did
13252 the same, so they were collapsed. */
13253 delta = (Z
13254 - CHARPOS (tlendpos)
13255 - MATRIX_ROW_START_CHARPOS (row));
13256 delta_bytes = (Z_BYTE
13257 - BYTEPOS (tlendpos)
13258 - MATRIX_ROW_START_BYTEPOS (row));
13259
13260 increment_matrix_positions (w->current_matrix,
13261 this_line_vpos + 1,
13262 w->current_matrix->nrows,
13263 delta, delta_bytes);
13264 }
13265
13266 /* If this row displays text now but previously didn't,
13267 or vice versa, w->window_end_vpos may have to be
13268 adjusted. */
13269 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13270 {
13271 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13272 wset_window_end_vpos (w, make_number (this_line_vpos));
13273 }
13274 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13275 && this_line_vpos > 0)
13276 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13277 w->window_end_valid = 0;
13278
13279 /* Update hint: No need to try to scroll in update_window. */
13280 w->desired_matrix->no_scrolling_p = 1;
13281
13282 #ifdef GLYPH_DEBUG
13283 *w->desired_matrix->method = 0;
13284 debug_method_add (w, "optimization 1");
13285 #endif
13286 #ifdef HAVE_WINDOW_SYSTEM
13287 update_window_fringes (w, 0);
13288 #endif
13289 goto update;
13290 }
13291 else
13292 goto cancel;
13293 }
13294 else if (/* Cursor position hasn't changed. */
13295 PT == w->last_point
13296 /* Make sure the cursor was last displayed
13297 in this window. Otherwise we have to reposition it. */
13298 && 0 <= w->cursor.vpos
13299 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13300 {
13301 if (!must_finish)
13302 {
13303 do_pending_window_change (1);
13304 /* If selected_window changed, redisplay again. */
13305 if (WINDOWP (selected_window)
13306 && (w = XWINDOW (selected_window)) != sw)
13307 goto retry;
13308
13309 /* We used to always goto end_of_redisplay here, but this
13310 isn't enough if we have a blinking cursor. */
13311 if (w->cursor_off_p == w->last_cursor_off_p)
13312 goto end_of_redisplay;
13313 }
13314 goto update;
13315 }
13316 /* If highlighting the region, or if the cursor is in the echo area,
13317 then we can't just move the cursor. */
13318 else if (! (!NILP (Vtransient_mark_mode)
13319 && !NILP (BVAR (current_buffer, mark_active)))
13320 && (EQ (selected_window,
13321 BVAR (current_buffer, last_selected_window))
13322 || highlight_nonselected_windows)
13323 && !w->region_showing
13324 && NILP (Vshow_trailing_whitespace)
13325 && !cursor_in_echo_area)
13326 {
13327 struct it it;
13328 struct glyph_row *row;
13329
13330 /* Skip from tlbufpos to PT and see where it is. Note that
13331 PT may be in invisible text. If so, we will end at the
13332 next visible position. */
13333 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13334 NULL, DEFAULT_FACE_ID);
13335 it.current_x = this_line_start_x;
13336 it.current_y = this_line_y;
13337 it.vpos = this_line_vpos;
13338
13339 /* The call to move_it_to stops in front of PT, but
13340 moves over before-strings. */
13341 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13342
13343 if (it.vpos == this_line_vpos
13344 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13345 row->enabled_p))
13346 {
13347 eassert (this_line_vpos == it.vpos);
13348 eassert (this_line_y == it.current_y);
13349 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13350 #ifdef GLYPH_DEBUG
13351 *w->desired_matrix->method = 0;
13352 debug_method_add (w, "optimization 3");
13353 #endif
13354 goto update;
13355 }
13356 else
13357 goto cancel;
13358 }
13359
13360 cancel:
13361 /* Text changed drastically or point moved off of line. */
13362 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13363 }
13364
13365 CHARPOS (this_line_start_pos) = 0;
13366 consider_all_windows_p |= buffer_shared_and_changed ();
13367 ++clear_face_cache_count;
13368 #ifdef HAVE_WINDOW_SYSTEM
13369 ++clear_image_cache_count;
13370 #endif
13371
13372 /* Build desired matrices, and update the display. If
13373 consider_all_windows_p is non-zero, do it for all windows on all
13374 frames. Otherwise do it for selected_window, only. */
13375
13376 if (consider_all_windows_p)
13377 {
13378 FOR_EACH_FRAME (tail, frame)
13379 XFRAME (frame)->updated_p = 0;
13380
13381 FOR_EACH_FRAME (tail, frame)
13382 {
13383 struct frame *f = XFRAME (frame);
13384
13385 /* We don't have to do anything for unselected terminal
13386 frames. */
13387 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13388 && !EQ (FRAME_TTY (f)->top_frame, frame))
13389 continue;
13390
13391 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13392 {
13393 /* Mark all the scroll bars to be removed; we'll redeem
13394 the ones we want when we redisplay their windows. */
13395 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13396 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13397
13398 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13399 redisplay_windows (FRAME_ROOT_WINDOW (f));
13400
13401 /* The X error handler may have deleted that frame. */
13402 if (!FRAME_LIVE_P (f))
13403 continue;
13404
13405 /* Any scroll bars which redisplay_windows should have
13406 nuked should now go away. */
13407 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13408 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13409
13410 /* If fonts changed, display again. */
13411 /* ??? rms: I suspect it is a mistake to jump all the way
13412 back to retry here. It should just retry this frame. */
13413 if (fonts_changed_p)
13414 goto retry;
13415
13416 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13417 {
13418 /* See if we have to hscroll. */
13419 if (!f->already_hscrolled_p)
13420 {
13421 f->already_hscrolled_p = 1;
13422 if (hscroll_windows (f->root_window))
13423 goto retry;
13424 }
13425
13426 /* Prevent various kinds of signals during display
13427 update. stdio is not robust about handling
13428 signals, which can cause an apparent I/O
13429 error. */
13430 if (interrupt_input)
13431 unrequest_sigio ();
13432 STOP_POLLING;
13433
13434 /* Update the display. */
13435 set_window_update_flags (XWINDOW (f->root_window), 1);
13436 pending |= update_frame (f, 0, 0);
13437 f->updated_p = 1;
13438 }
13439 }
13440 }
13441
13442 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13443
13444 if (!pending)
13445 {
13446 /* Do the mark_window_display_accurate after all windows have
13447 been redisplayed because this call resets flags in buffers
13448 which are needed for proper redisplay. */
13449 FOR_EACH_FRAME (tail, frame)
13450 {
13451 struct frame *f = XFRAME (frame);
13452 if (f->updated_p)
13453 {
13454 mark_window_display_accurate (f->root_window, 1);
13455 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13456 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13457 }
13458 }
13459 }
13460 }
13461 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13462 {
13463 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13464 struct frame *mini_frame;
13465
13466 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13467 /* Use list_of_error, not Qerror, so that
13468 we catch only errors and don't run the debugger. */
13469 internal_condition_case_1 (redisplay_window_1, selected_window,
13470 list_of_error,
13471 redisplay_window_error);
13472 if (update_miniwindow_p)
13473 internal_condition_case_1 (redisplay_window_1, mini_window,
13474 list_of_error,
13475 redisplay_window_error);
13476
13477 /* Compare desired and current matrices, perform output. */
13478
13479 update:
13480 /* If fonts changed, display again. */
13481 if (fonts_changed_p)
13482 goto retry;
13483
13484 /* Prevent various kinds of signals during display update.
13485 stdio is not robust about handling signals,
13486 which can cause an apparent I/O error. */
13487 if (interrupt_input)
13488 unrequest_sigio ();
13489 STOP_POLLING;
13490
13491 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13492 {
13493 if (hscroll_windows (selected_window))
13494 goto retry;
13495
13496 XWINDOW (selected_window)->must_be_updated_p = 1;
13497 pending = update_frame (sf, 0, 0);
13498 }
13499
13500 /* We may have called echo_area_display at the top of this
13501 function. If the echo area is on another frame, that may
13502 have put text on a frame other than the selected one, so the
13503 above call to update_frame would not have caught it. Catch
13504 it here. */
13505 mini_window = FRAME_MINIBUF_WINDOW (sf);
13506 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13507
13508 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13509 {
13510 XWINDOW (mini_window)->must_be_updated_p = 1;
13511 pending |= update_frame (mini_frame, 0, 0);
13512 if (!pending && hscroll_windows (mini_window))
13513 goto retry;
13514 }
13515 }
13516
13517 /* If display was paused because of pending input, make sure we do a
13518 thorough update the next time. */
13519 if (pending)
13520 {
13521 /* Prevent the optimization at the beginning of
13522 redisplay_internal that tries a single-line update of the
13523 line containing the cursor in the selected window. */
13524 CHARPOS (this_line_start_pos) = 0;
13525
13526 /* Let the overlay arrow be updated the next time. */
13527 update_overlay_arrows (0);
13528
13529 /* If we pause after scrolling, some rows in the current
13530 matrices of some windows are not valid. */
13531 if (!WINDOW_FULL_WIDTH_P (w)
13532 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13533 update_mode_lines = 1;
13534 }
13535 else
13536 {
13537 if (!consider_all_windows_p)
13538 {
13539 /* This has already been done above if
13540 consider_all_windows_p is set. */
13541 mark_window_display_accurate_1 (w, 1);
13542
13543 /* Say overlay arrows are up to date. */
13544 update_overlay_arrows (1);
13545
13546 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13547 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13548 }
13549
13550 update_mode_lines = 0;
13551 windows_or_buffers_changed = 0;
13552 cursor_type_changed = 0;
13553 }
13554
13555 /* Start SIGIO interrupts coming again. Having them off during the
13556 code above makes it less likely one will discard output, but not
13557 impossible, since there might be stuff in the system buffer here.
13558 But it is much hairier to try to do anything about that. */
13559 if (interrupt_input)
13560 request_sigio ();
13561 RESUME_POLLING;
13562
13563 /* If a frame has become visible which was not before, redisplay
13564 again, so that we display it. Expose events for such a frame
13565 (which it gets when becoming visible) don't call the parts of
13566 redisplay constructing glyphs, so simply exposing a frame won't
13567 display anything in this case. So, we have to display these
13568 frames here explicitly. */
13569 if (!pending)
13570 {
13571 int new_count = 0;
13572
13573 FOR_EACH_FRAME (tail, frame)
13574 {
13575 int this_is_visible = 0;
13576
13577 if (XFRAME (frame)->visible)
13578 this_is_visible = 1;
13579
13580 if (this_is_visible)
13581 new_count++;
13582 }
13583
13584 if (new_count != number_of_visible_frames)
13585 windows_or_buffers_changed++;
13586 }
13587
13588 /* Change frame size now if a change is pending. */
13589 do_pending_window_change (1);
13590
13591 /* If we just did a pending size change, or have additional
13592 visible frames, or selected_window changed, redisplay again. */
13593 if ((windows_or_buffers_changed && !pending)
13594 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13595 goto retry;
13596
13597 /* Clear the face and image caches.
13598
13599 We used to do this only if consider_all_windows_p. But the cache
13600 needs to be cleared if a timer creates images in the current
13601 buffer (e.g. the test case in Bug#6230). */
13602
13603 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13604 {
13605 clear_face_cache (0);
13606 clear_face_cache_count = 0;
13607 }
13608
13609 #ifdef HAVE_WINDOW_SYSTEM
13610 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13611 {
13612 clear_image_caches (Qnil);
13613 clear_image_cache_count = 0;
13614 }
13615 #endif /* HAVE_WINDOW_SYSTEM */
13616
13617 end_of_redisplay:
13618 unbind_to (count, Qnil);
13619 RESUME_POLLING;
13620 }
13621
13622
13623 /* Redisplay, but leave alone any recent echo area message unless
13624 another message has been requested in its place.
13625
13626 This is useful in situations where you need to redisplay but no
13627 user action has occurred, making it inappropriate for the message
13628 area to be cleared. See tracking_off and
13629 wait_reading_process_output for examples of these situations.
13630
13631 FROM_WHERE is an integer saying from where this function was
13632 called. This is useful for debugging. */
13633
13634 void
13635 redisplay_preserve_echo_area (int from_where)
13636 {
13637 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13638
13639 if (!NILP (echo_area_buffer[1]))
13640 {
13641 /* We have a previously displayed message, but no current
13642 message. Redisplay the previous message. */
13643 display_last_displayed_message_p = 1;
13644 redisplay_internal ();
13645 display_last_displayed_message_p = 0;
13646 }
13647 else
13648 redisplay_internal ();
13649
13650 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13651 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13652 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13653 }
13654
13655
13656 /* Function registered with record_unwind_protect in redisplay_internal. */
13657
13658 static void
13659 unwind_redisplay (void)
13660 {
13661 redisplaying_p = 0;
13662 }
13663
13664
13665 /* Mark the display of leaf window W as accurate or inaccurate.
13666 If ACCURATE_P is non-zero mark display of W as accurate. If
13667 ACCURATE_P is zero, arrange for W to be redisplayed the next
13668 time redisplay_internal is called. */
13669
13670 static void
13671 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13672 {
13673 struct buffer *b = XBUFFER (w->contents);
13674
13675 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13676 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13677 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13678
13679 if (accurate_p)
13680 {
13681 b->clip_changed = 0;
13682 b->prevent_redisplay_optimizations_p = 0;
13683
13684 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13685 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13686 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13687 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13688
13689 w->current_matrix->buffer = b;
13690 w->current_matrix->begv = BUF_BEGV (b);
13691 w->current_matrix->zv = BUF_ZV (b);
13692
13693 w->last_cursor = w->cursor;
13694 w->last_cursor_off_p = w->cursor_off_p;
13695
13696 if (w == XWINDOW (selected_window))
13697 w->last_point = BUF_PT (b);
13698 else
13699 w->last_point = marker_position (w->pointm);
13700
13701 w->window_end_valid = 1;
13702 w->update_mode_line = 0;
13703 }
13704 }
13705
13706
13707 /* Mark the display of windows in the window tree rooted at WINDOW as
13708 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13709 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13710 be redisplayed the next time redisplay_internal is called. */
13711
13712 void
13713 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13714 {
13715 struct window *w;
13716
13717 for (; !NILP (window); window = w->next)
13718 {
13719 w = XWINDOW (window);
13720 if (WINDOWP (w->contents))
13721 mark_window_display_accurate (w->contents, accurate_p);
13722 else
13723 mark_window_display_accurate_1 (w, accurate_p);
13724 }
13725
13726 if (accurate_p)
13727 update_overlay_arrows (1);
13728 else
13729 /* Force a thorough redisplay the next time by setting
13730 last_arrow_position and last_arrow_string to t, which is
13731 unequal to any useful value of Voverlay_arrow_... */
13732 update_overlay_arrows (-1);
13733 }
13734
13735
13736 /* Return value in display table DP (Lisp_Char_Table *) for character
13737 C. Since a display table doesn't have any parent, we don't have to
13738 follow parent. Do not call this function directly but use the
13739 macro DISP_CHAR_VECTOR. */
13740
13741 Lisp_Object
13742 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13743 {
13744 Lisp_Object val;
13745
13746 if (ASCII_CHAR_P (c))
13747 {
13748 val = dp->ascii;
13749 if (SUB_CHAR_TABLE_P (val))
13750 val = XSUB_CHAR_TABLE (val)->contents[c];
13751 }
13752 else
13753 {
13754 Lisp_Object table;
13755
13756 XSETCHAR_TABLE (table, dp);
13757 val = char_table_ref (table, c);
13758 }
13759 if (NILP (val))
13760 val = dp->defalt;
13761 return val;
13762 }
13763
13764
13765 \f
13766 /***********************************************************************
13767 Window Redisplay
13768 ***********************************************************************/
13769
13770 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13771
13772 static void
13773 redisplay_windows (Lisp_Object window)
13774 {
13775 while (!NILP (window))
13776 {
13777 struct window *w = XWINDOW (window);
13778
13779 if (WINDOWP (w->contents))
13780 redisplay_windows (w->contents);
13781 else if (BUFFERP (w->contents))
13782 {
13783 displayed_buffer = XBUFFER (w->contents);
13784 /* Use list_of_error, not Qerror, so that
13785 we catch only errors and don't run the debugger. */
13786 internal_condition_case_1 (redisplay_window_0, window,
13787 list_of_error,
13788 redisplay_window_error);
13789 }
13790
13791 window = w->next;
13792 }
13793 }
13794
13795 static Lisp_Object
13796 redisplay_window_error (Lisp_Object ignore)
13797 {
13798 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13799 return Qnil;
13800 }
13801
13802 static Lisp_Object
13803 redisplay_window_0 (Lisp_Object window)
13804 {
13805 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13806 redisplay_window (window, 0);
13807 return Qnil;
13808 }
13809
13810 static Lisp_Object
13811 redisplay_window_1 (Lisp_Object window)
13812 {
13813 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13814 redisplay_window (window, 1);
13815 return Qnil;
13816 }
13817 \f
13818
13819 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13820 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13821 which positions recorded in ROW differ from current buffer
13822 positions.
13823
13824 Return 0 if cursor is not on this row, 1 otherwise. */
13825
13826 static int
13827 set_cursor_from_row (struct window *w, struct glyph_row *row,
13828 struct glyph_matrix *matrix,
13829 ptrdiff_t delta, ptrdiff_t delta_bytes,
13830 int dy, int dvpos)
13831 {
13832 struct glyph *glyph = row->glyphs[TEXT_AREA];
13833 struct glyph *end = glyph + row->used[TEXT_AREA];
13834 struct glyph *cursor = NULL;
13835 /* The last known character position in row. */
13836 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13837 int x = row->x;
13838 ptrdiff_t pt_old = PT - delta;
13839 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13840 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13841 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13842 /* A glyph beyond the edge of TEXT_AREA which we should never
13843 touch. */
13844 struct glyph *glyphs_end = end;
13845 /* Non-zero means we've found a match for cursor position, but that
13846 glyph has the avoid_cursor_p flag set. */
13847 int match_with_avoid_cursor = 0;
13848 /* Non-zero means we've seen at least one glyph that came from a
13849 display string. */
13850 int string_seen = 0;
13851 /* Largest and smallest buffer positions seen so far during scan of
13852 glyph row. */
13853 ptrdiff_t bpos_max = pos_before;
13854 ptrdiff_t bpos_min = pos_after;
13855 /* Last buffer position covered by an overlay string with an integer
13856 `cursor' property. */
13857 ptrdiff_t bpos_covered = 0;
13858 /* Non-zero means the display string on which to display the cursor
13859 comes from a text property, not from an overlay. */
13860 int string_from_text_prop = 0;
13861
13862 /* Don't even try doing anything if called for a mode-line or
13863 header-line row, since the rest of the code isn't prepared to
13864 deal with such calamities. */
13865 eassert (!row->mode_line_p);
13866 if (row->mode_line_p)
13867 return 0;
13868
13869 /* Skip over glyphs not having an object at the start and the end of
13870 the row. These are special glyphs like truncation marks on
13871 terminal frames. */
13872 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13873 {
13874 if (!row->reversed_p)
13875 {
13876 while (glyph < end
13877 && INTEGERP (glyph->object)
13878 && glyph->charpos < 0)
13879 {
13880 x += glyph->pixel_width;
13881 ++glyph;
13882 }
13883 while (end > glyph
13884 && INTEGERP ((end - 1)->object)
13885 /* CHARPOS is zero for blanks and stretch glyphs
13886 inserted by extend_face_to_end_of_line. */
13887 && (end - 1)->charpos <= 0)
13888 --end;
13889 glyph_before = glyph - 1;
13890 glyph_after = end;
13891 }
13892 else
13893 {
13894 struct glyph *g;
13895
13896 /* If the glyph row is reversed, we need to process it from back
13897 to front, so swap the edge pointers. */
13898 glyphs_end = end = glyph - 1;
13899 glyph += row->used[TEXT_AREA] - 1;
13900
13901 while (glyph > end + 1
13902 && INTEGERP (glyph->object)
13903 && glyph->charpos < 0)
13904 {
13905 --glyph;
13906 x -= glyph->pixel_width;
13907 }
13908 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13909 --glyph;
13910 /* By default, in reversed rows we put the cursor on the
13911 rightmost (first in the reading order) glyph. */
13912 for (g = end + 1; g < glyph; g++)
13913 x += g->pixel_width;
13914 while (end < glyph
13915 && INTEGERP ((end + 1)->object)
13916 && (end + 1)->charpos <= 0)
13917 ++end;
13918 glyph_before = glyph + 1;
13919 glyph_after = end;
13920 }
13921 }
13922 else if (row->reversed_p)
13923 {
13924 /* In R2L rows that don't display text, put the cursor on the
13925 rightmost glyph. Case in point: an empty last line that is
13926 part of an R2L paragraph. */
13927 cursor = end - 1;
13928 /* Avoid placing the cursor on the last glyph of the row, where
13929 on terminal frames we hold the vertical border between
13930 adjacent windows. */
13931 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13932 && !WINDOW_RIGHTMOST_P (w)
13933 && cursor == row->glyphs[LAST_AREA] - 1)
13934 cursor--;
13935 x = -1; /* will be computed below, at label compute_x */
13936 }
13937
13938 /* Step 1: Try to find the glyph whose character position
13939 corresponds to point. If that's not possible, find 2 glyphs
13940 whose character positions are the closest to point, one before
13941 point, the other after it. */
13942 if (!row->reversed_p)
13943 while (/* not marched to end of glyph row */
13944 glyph < end
13945 /* glyph was not inserted by redisplay for internal purposes */
13946 && !INTEGERP (glyph->object))
13947 {
13948 if (BUFFERP (glyph->object))
13949 {
13950 ptrdiff_t dpos = glyph->charpos - pt_old;
13951
13952 if (glyph->charpos > bpos_max)
13953 bpos_max = glyph->charpos;
13954 if (glyph->charpos < bpos_min)
13955 bpos_min = glyph->charpos;
13956 if (!glyph->avoid_cursor_p)
13957 {
13958 /* If we hit point, we've found the glyph on which to
13959 display the cursor. */
13960 if (dpos == 0)
13961 {
13962 match_with_avoid_cursor = 0;
13963 break;
13964 }
13965 /* See if we've found a better approximation to
13966 POS_BEFORE or to POS_AFTER. */
13967 if (0 > dpos && dpos > pos_before - pt_old)
13968 {
13969 pos_before = glyph->charpos;
13970 glyph_before = glyph;
13971 }
13972 else if (0 < dpos && dpos < pos_after - pt_old)
13973 {
13974 pos_after = glyph->charpos;
13975 glyph_after = glyph;
13976 }
13977 }
13978 else if (dpos == 0)
13979 match_with_avoid_cursor = 1;
13980 }
13981 else if (STRINGP (glyph->object))
13982 {
13983 Lisp_Object chprop;
13984 ptrdiff_t glyph_pos = glyph->charpos;
13985
13986 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13987 glyph->object);
13988 if (!NILP (chprop))
13989 {
13990 /* If the string came from a `display' text property,
13991 look up the buffer position of that property and
13992 use that position to update bpos_max, as if we
13993 actually saw such a position in one of the row's
13994 glyphs. This helps with supporting integer values
13995 of `cursor' property on the display string in
13996 situations where most or all of the row's buffer
13997 text is completely covered by display properties,
13998 so that no glyph with valid buffer positions is
13999 ever seen in the row. */
14000 ptrdiff_t prop_pos =
14001 string_buffer_position_lim (glyph->object, pos_before,
14002 pos_after, 0);
14003
14004 if (prop_pos >= pos_before)
14005 bpos_max = prop_pos - 1;
14006 }
14007 if (INTEGERP (chprop))
14008 {
14009 bpos_covered = bpos_max + XINT (chprop);
14010 /* If the `cursor' property covers buffer positions up
14011 to and including point, we should display cursor on
14012 this glyph. Note that, if a `cursor' property on one
14013 of the string's characters has an integer value, we
14014 will break out of the loop below _before_ we get to
14015 the position match above. IOW, integer values of
14016 the `cursor' property override the "exact match for
14017 point" strategy of positioning the cursor. */
14018 /* Implementation note: bpos_max == pt_old when, e.g.,
14019 we are in an empty line, where bpos_max is set to
14020 MATRIX_ROW_START_CHARPOS, see above. */
14021 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14022 {
14023 cursor = glyph;
14024 break;
14025 }
14026 }
14027
14028 string_seen = 1;
14029 }
14030 x += glyph->pixel_width;
14031 ++glyph;
14032 }
14033 else if (glyph > end) /* row is reversed */
14034 while (!INTEGERP (glyph->object))
14035 {
14036 if (BUFFERP (glyph->object))
14037 {
14038 ptrdiff_t dpos = glyph->charpos - pt_old;
14039
14040 if (glyph->charpos > bpos_max)
14041 bpos_max = glyph->charpos;
14042 if (glyph->charpos < bpos_min)
14043 bpos_min = glyph->charpos;
14044 if (!glyph->avoid_cursor_p)
14045 {
14046 if (dpos == 0)
14047 {
14048 match_with_avoid_cursor = 0;
14049 break;
14050 }
14051 if (0 > dpos && dpos > pos_before - pt_old)
14052 {
14053 pos_before = glyph->charpos;
14054 glyph_before = glyph;
14055 }
14056 else if (0 < dpos && dpos < pos_after - pt_old)
14057 {
14058 pos_after = glyph->charpos;
14059 glyph_after = glyph;
14060 }
14061 }
14062 else if (dpos == 0)
14063 match_with_avoid_cursor = 1;
14064 }
14065 else if (STRINGP (glyph->object))
14066 {
14067 Lisp_Object chprop;
14068 ptrdiff_t glyph_pos = glyph->charpos;
14069
14070 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14071 glyph->object);
14072 if (!NILP (chprop))
14073 {
14074 ptrdiff_t prop_pos =
14075 string_buffer_position_lim (glyph->object, pos_before,
14076 pos_after, 0);
14077
14078 if (prop_pos >= pos_before)
14079 bpos_max = prop_pos - 1;
14080 }
14081 if (INTEGERP (chprop))
14082 {
14083 bpos_covered = bpos_max + XINT (chprop);
14084 /* If the `cursor' property covers buffer positions up
14085 to and including point, we should display cursor on
14086 this glyph. */
14087 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14088 {
14089 cursor = glyph;
14090 break;
14091 }
14092 }
14093 string_seen = 1;
14094 }
14095 --glyph;
14096 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14097 {
14098 x--; /* can't use any pixel_width */
14099 break;
14100 }
14101 x -= glyph->pixel_width;
14102 }
14103
14104 /* Step 2: If we didn't find an exact match for point, we need to
14105 look for a proper place to put the cursor among glyphs between
14106 GLYPH_BEFORE and GLYPH_AFTER. */
14107 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14108 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14109 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14110 {
14111 /* An empty line has a single glyph whose OBJECT is zero and
14112 whose CHARPOS is the position of a newline on that line.
14113 Note that on a TTY, there are more glyphs after that, which
14114 were produced by extend_face_to_end_of_line, but their
14115 CHARPOS is zero or negative. */
14116 int empty_line_p =
14117 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14118 && INTEGERP (glyph->object) && glyph->charpos > 0
14119 /* On a TTY, continued and truncated rows also have a glyph at
14120 their end whose OBJECT is zero and whose CHARPOS is
14121 positive (the continuation and truncation glyphs), but such
14122 rows are obviously not "empty". */
14123 && !(row->continued_p || row->truncated_on_right_p);
14124
14125 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14126 {
14127 ptrdiff_t ellipsis_pos;
14128
14129 /* Scan back over the ellipsis glyphs. */
14130 if (!row->reversed_p)
14131 {
14132 ellipsis_pos = (glyph - 1)->charpos;
14133 while (glyph > row->glyphs[TEXT_AREA]
14134 && (glyph - 1)->charpos == ellipsis_pos)
14135 glyph--, x -= glyph->pixel_width;
14136 /* That loop always goes one position too far, including
14137 the glyph before the ellipsis. So scan forward over
14138 that one. */
14139 x += glyph->pixel_width;
14140 glyph++;
14141 }
14142 else /* row is reversed */
14143 {
14144 ellipsis_pos = (glyph + 1)->charpos;
14145 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14146 && (glyph + 1)->charpos == ellipsis_pos)
14147 glyph++, x += glyph->pixel_width;
14148 x -= glyph->pixel_width;
14149 glyph--;
14150 }
14151 }
14152 else if (match_with_avoid_cursor)
14153 {
14154 cursor = glyph_after;
14155 x = -1;
14156 }
14157 else if (string_seen)
14158 {
14159 int incr = row->reversed_p ? -1 : +1;
14160
14161 /* Need to find the glyph that came out of a string which is
14162 present at point. That glyph is somewhere between
14163 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14164 positioned between POS_BEFORE and POS_AFTER in the
14165 buffer. */
14166 struct glyph *start, *stop;
14167 ptrdiff_t pos = pos_before;
14168
14169 x = -1;
14170
14171 /* If the row ends in a newline from a display string,
14172 reordering could have moved the glyphs belonging to the
14173 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14174 in this case we extend the search to the last glyph in
14175 the row that was not inserted by redisplay. */
14176 if (row->ends_in_newline_from_string_p)
14177 {
14178 glyph_after = end;
14179 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14180 }
14181
14182 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14183 correspond to POS_BEFORE and POS_AFTER, respectively. We
14184 need START and STOP in the order that corresponds to the
14185 row's direction as given by its reversed_p flag. If the
14186 directionality of characters between POS_BEFORE and
14187 POS_AFTER is the opposite of the row's base direction,
14188 these characters will have been reordered for display,
14189 and we need to reverse START and STOP. */
14190 if (!row->reversed_p)
14191 {
14192 start = min (glyph_before, glyph_after);
14193 stop = max (glyph_before, glyph_after);
14194 }
14195 else
14196 {
14197 start = max (glyph_before, glyph_after);
14198 stop = min (glyph_before, glyph_after);
14199 }
14200 for (glyph = start + incr;
14201 row->reversed_p ? glyph > stop : glyph < stop; )
14202 {
14203
14204 /* Any glyphs that come from the buffer are here because
14205 of bidi reordering. Skip them, and only pay
14206 attention to glyphs that came from some string. */
14207 if (STRINGP (glyph->object))
14208 {
14209 Lisp_Object str;
14210 ptrdiff_t tem;
14211 /* If the display property covers the newline, we
14212 need to search for it one position farther. */
14213 ptrdiff_t lim = pos_after
14214 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14215
14216 string_from_text_prop = 0;
14217 str = glyph->object;
14218 tem = string_buffer_position_lim (str, pos, lim, 0);
14219 if (tem == 0 /* from overlay */
14220 || pos <= tem)
14221 {
14222 /* If the string from which this glyph came is
14223 found in the buffer at point, or at position
14224 that is closer to point than pos_after, then
14225 we've found the glyph we've been looking for.
14226 If it comes from an overlay (tem == 0), and
14227 it has the `cursor' property on one of its
14228 glyphs, record that glyph as a candidate for
14229 displaying the cursor. (As in the
14230 unidirectional version, we will display the
14231 cursor on the last candidate we find.) */
14232 if (tem == 0
14233 || tem == pt_old
14234 || (tem - pt_old > 0 && tem < pos_after))
14235 {
14236 /* The glyphs from this string could have
14237 been reordered. Find the one with the
14238 smallest string position. Or there could
14239 be a character in the string with the
14240 `cursor' property, which means display
14241 cursor on that character's glyph. */
14242 ptrdiff_t strpos = glyph->charpos;
14243
14244 if (tem)
14245 {
14246 cursor = glyph;
14247 string_from_text_prop = 1;
14248 }
14249 for ( ;
14250 (row->reversed_p ? glyph > stop : glyph < stop)
14251 && EQ (glyph->object, str);
14252 glyph += incr)
14253 {
14254 Lisp_Object cprop;
14255 ptrdiff_t gpos = glyph->charpos;
14256
14257 cprop = Fget_char_property (make_number (gpos),
14258 Qcursor,
14259 glyph->object);
14260 if (!NILP (cprop))
14261 {
14262 cursor = glyph;
14263 break;
14264 }
14265 if (tem && glyph->charpos < strpos)
14266 {
14267 strpos = glyph->charpos;
14268 cursor = glyph;
14269 }
14270 }
14271
14272 if (tem == pt_old
14273 || (tem - pt_old > 0 && tem < pos_after))
14274 goto compute_x;
14275 }
14276 if (tem)
14277 pos = tem + 1; /* don't find previous instances */
14278 }
14279 /* This string is not what we want; skip all of the
14280 glyphs that came from it. */
14281 while ((row->reversed_p ? glyph > stop : glyph < stop)
14282 && EQ (glyph->object, str))
14283 glyph += incr;
14284 }
14285 else
14286 glyph += incr;
14287 }
14288
14289 /* If we reached the end of the line, and END was from a string,
14290 the cursor is not on this line. */
14291 if (cursor == NULL
14292 && (row->reversed_p ? glyph <= end : glyph >= end)
14293 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14294 && STRINGP (end->object)
14295 && row->continued_p)
14296 return 0;
14297 }
14298 /* A truncated row may not include PT among its character positions.
14299 Setting the cursor inside the scroll margin will trigger
14300 recalculation of hscroll in hscroll_window_tree. But if a
14301 display string covers point, defer to the string-handling
14302 code below to figure this out. */
14303 else if (row->truncated_on_left_p && pt_old < bpos_min)
14304 {
14305 cursor = glyph_before;
14306 x = -1;
14307 }
14308 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14309 /* Zero-width characters produce no glyphs. */
14310 || (!empty_line_p
14311 && (row->reversed_p
14312 ? glyph_after > glyphs_end
14313 : glyph_after < glyphs_end)))
14314 {
14315 cursor = glyph_after;
14316 x = -1;
14317 }
14318 }
14319
14320 compute_x:
14321 if (cursor != NULL)
14322 glyph = cursor;
14323 else if (glyph == glyphs_end
14324 && pos_before == pos_after
14325 && STRINGP ((row->reversed_p
14326 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14327 : row->glyphs[TEXT_AREA])->object))
14328 {
14329 /* If all the glyphs of this row came from strings, put the
14330 cursor on the first glyph of the row. This avoids having the
14331 cursor outside of the text area in this very rare and hard
14332 use case. */
14333 glyph =
14334 row->reversed_p
14335 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14336 : row->glyphs[TEXT_AREA];
14337 }
14338 if (x < 0)
14339 {
14340 struct glyph *g;
14341
14342 /* Need to compute x that corresponds to GLYPH. */
14343 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14344 {
14345 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14346 emacs_abort ();
14347 x += g->pixel_width;
14348 }
14349 }
14350
14351 /* ROW could be part of a continued line, which, under bidi
14352 reordering, might have other rows whose start and end charpos
14353 occlude point. Only set w->cursor if we found a better
14354 approximation to the cursor position than we have from previously
14355 examined candidate rows belonging to the same continued line. */
14356 if (/* we already have a candidate row */
14357 w->cursor.vpos >= 0
14358 /* that candidate is not the row we are processing */
14359 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14360 /* Make sure cursor.vpos specifies a row whose start and end
14361 charpos occlude point, and it is valid candidate for being a
14362 cursor-row. This is because some callers of this function
14363 leave cursor.vpos at the row where the cursor was displayed
14364 during the last redisplay cycle. */
14365 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14366 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14367 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14368 {
14369 struct glyph *g1 =
14370 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14371
14372 /* Don't consider glyphs that are outside TEXT_AREA. */
14373 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14374 return 0;
14375 /* Keep the candidate whose buffer position is the closest to
14376 point or has the `cursor' property. */
14377 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14378 w->cursor.hpos >= 0
14379 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14380 && ((BUFFERP (g1->object)
14381 && (g1->charpos == pt_old /* an exact match always wins */
14382 || (BUFFERP (glyph->object)
14383 && eabs (g1->charpos - pt_old)
14384 < eabs (glyph->charpos - pt_old))))
14385 /* previous candidate is a glyph from a string that has
14386 a non-nil `cursor' property */
14387 || (STRINGP (g1->object)
14388 && (!NILP (Fget_char_property (make_number (g1->charpos),
14389 Qcursor, g1->object))
14390 /* previous candidate is from the same display
14391 string as this one, and the display string
14392 came from a text property */
14393 || (EQ (g1->object, glyph->object)
14394 && string_from_text_prop)
14395 /* this candidate is from newline and its
14396 position is not an exact match */
14397 || (INTEGERP (glyph->object)
14398 && glyph->charpos != pt_old)))))
14399 return 0;
14400 /* If this candidate gives an exact match, use that. */
14401 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14402 /* If this candidate is a glyph created for the
14403 terminating newline of a line, and point is on that
14404 newline, it wins because it's an exact match. */
14405 || (!row->continued_p
14406 && INTEGERP (glyph->object)
14407 && glyph->charpos == 0
14408 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14409 /* Otherwise, keep the candidate that comes from a row
14410 spanning less buffer positions. This may win when one or
14411 both candidate positions are on glyphs that came from
14412 display strings, for which we cannot compare buffer
14413 positions. */
14414 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14415 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14416 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14417 return 0;
14418 }
14419 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14420 w->cursor.x = x;
14421 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14422 w->cursor.y = row->y + dy;
14423
14424 if (w == XWINDOW (selected_window))
14425 {
14426 if (!row->continued_p
14427 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14428 && row->x == 0)
14429 {
14430 this_line_buffer = XBUFFER (w->contents);
14431
14432 CHARPOS (this_line_start_pos)
14433 = MATRIX_ROW_START_CHARPOS (row) + delta;
14434 BYTEPOS (this_line_start_pos)
14435 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14436
14437 CHARPOS (this_line_end_pos)
14438 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14439 BYTEPOS (this_line_end_pos)
14440 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14441
14442 this_line_y = w->cursor.y;
14443 this_line_pixel_height = row->height;
14444 this_line_vpos = w->cursor.vpos;
14445 this_line_start_x = row->x;
14446 }
14447 else
14448 CHARPOS (this_line_start_pos) = 0;
14449 }
14450
14451 return 1;
14452 }
14453
14454
14455 /* Run window scroll functions, if any, for WINDOW with new window
14456 start STARTP. Sets the window start of WINDOW to that position.
14457
14458 We assume that the window's buffer is really current. */
14459
14460 static struct text_pos
14461 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14462 {
14463 struct window *w = XWINDOW (window);
14464 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14465
14466 eassert (current_buffer == XBUFFER (w->contents));
14467
14468 if (!NILP (Vwindow_scroll_functions))
14469 {
14470 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14471 make_number (CHARPOS (startp)));
14472 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14473 /* In case the hook functions switch buffers. */
14474 set_buffer_internal (XBUFFER (w->contents));
14475 }
14476
14477 return startp;
14478 }
14479
14480
14481 /* Make sure the line containing the cursor is fully visible.
14482 A value of 1 means there is nothing to be done.
14483 (Either the line is fully visible, or it cannot be made so,
14484 or we cannot tell.)
14485
14486 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14487 is higher than window.
14488
14489 A value of 0 means the caller should do scrolling
14490 as if point had gone off the screen. */
14491
14492 static int
14493 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14494 {
14495 struct glyph_matrix *matrix;
14496 struct glyph_row *row;
14497 int window_height;
14498
14499 if (!make_cursor_line_fully_visible_p)
14500 return 1;
14501
14502 /* It's not always possible to find the cursor, e.g, when a window
14503 is full of overlay strings. Don't do anything in that case. */
14504 if (w->cursor.vpos < 0)
14505 return 1;
14506
14507 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14508 row = MATRIX_ROW (matrix, w->cursor.vpos);
14509
14510 /* If the cursor row is not partially visible, there's nothing to do. */
14511 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14512 return 1;
14513
14514 /* If the row the cursor is in is taller than the window's height,
14515 it's not clear what to do, so do nothing. */
14516 window_height = window_box_height (w);
14517 if (row->height >= window_height)
14518 {
14519 if (!force_p || MINI_WINDOW_P (w)
14520 || w->vscroll || w->cursor.vpos == 0)
14521 return 1;
14522 }
14523 return 0;
14524 }
14525
14526
14527 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14528 non-zero means only WINDOW is redisplayed in redisplay_internal.
14529 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14530 in redisplay_window to bring a partially visible line into view in
14531 the case that only the cursor has moved.
14532
14533 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14534 last screen line's vertical height extends past the end of the screen.
14535
14536 Value is
14537
14538 1 if scrolling succeeded
14539
14540 0 if scrolling didn't find point.
14541
14542 -1 if new fonts have been loaded so that we must interrupt
14543 redisplay, adjust glyph matrices, and try again. */
14544
14545 enum
14546 {
14547 SCROLLING_SUCCESS,
14548 SCROLLING_FAILED,
14549 SCROLLING_NEED_LARGER_MATRICES
14550 };
14551
14552 /* If scroll-conservatively is more than this, never recenter.
14553
14554 If you change this, don't forget to update the doc string of
14555 `scroll-conservatively' and the Emacs manual. */
14556 #define SCROLL_LIMIT 100
14557
14558 static int
14559 try_scrolling (Lisp_Object window, int just_this_one_p,
14560 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14561 int temp_scroll_step, int last_line_misfit)
14562 {
14563 struct window *w = XWINDOW (window);
14564 struct frame *f = XFRAME (w->frame);
14565 struct text_pos pos, startp;
14566 struct it it;
14567 int this_scroll_margin, scroll_max, rc, height;
14568 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14569 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14570 Lisp_Object aggressive;
14571 /* We will never try scrolling more than this number of lines. */
14572 int scroll_limit = SCROLL_LIMIT;
14573 int frame_line_height = default_line_pixel_height (w);
14574 int window_total_lines
14575 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14576
14577 #ifdef GLYPH_DEBUG
14578 debug_method_add (w, "try_scrolling");
14579 #endif
14580
14581 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14582
14583 /* Compute scroll margin height in pixels. We scroll when point is
14584 within this distance from the top or bottom of the window. */
14585 if (scroll_margin > 0)
14586 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14587 * frame_line_height;
14588 else
14589 this_scroll_margin = 0;
14590
14591 /* Force arg_scroll_conservatively to have a reasonable value, to
14592 avoid scrolling too far away with slow move_it_* functions. Note
14593 that the user can supply scroll-conservatively equal to
14594 `most-positive-fixnum', which can be larger than INT_MAX. */
14595 if (arg_scroll_conservatively > scroll_limit)
14596 {
14597 arg_scroll_conservatively = scroll_limit + 1;
14598 scroll_max = scroll_limit * frame_line_height;
14599 }
14600 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14601 /* Compute how much we should try to scroll maximally to bring
14602 point into view. */
14603 scroll_max = (max (scroll_step,
14604 max (arg_scroll_conservatively, temp_scroll_step))
14605 * frame_line_height);
14606 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14607 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14608 /* We're trying to scroll because of aggressive scrolling but no
14609 scroll_step is set. Choose an arbitrary one. */
14610 scroll_max = 10 * frame_line_height;
14611 else
14612 scroll_max = 0;
14613
14614 too_near_end:
14615
14616 /* Decide whether to scroll down. */
14617 if (PT > CHARPOS (startp))
14618 {
14619 int scroll_margin_y;
14620
14621 /* Compute the pixel ypos of the scroll margin, then move IT to
14622 either that ypos or PT, whichever comes first. */
14623 start_display (&it, w, startp);
14624 scroll_margin_y = it.last_visible_y - this_scroll_margin
14625 - frame_line_height * extra_scroll_margin_lines;
14626 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14627 (MOVE_TO_POS | MOVE_TO_Y));
14628
14629 if (PT > CHARPOS (it.current.pos))
14630 {
14631 int y0 = line_bottom_y (&it);
14632 /* Compute how many pixels below window bottom to stop searching
14633 for PT. This avoids costly search for PT that is far away if
14634 the user limited scrolling by a small number of lines, but
14635 always finds PT if scroll_conservatively is set to a large
14636 number, such as most-positive-fixnum. */
14637 int slack = max (scroll_max, 10 * frame_line_height);
14638 int y_to_move = it.last_visible_y + slack;
14639
14640 /* Compute the distance from the scroll margin to PT or to
14641 the scroll limit, whichever comes first. This should
14642 include the height of the cursor line, to make that line
14643 fully visible. */
14644 move_it_to (&it, PT, -1, y_to_move,
14645 -1, MOVE_TO_POS | MOVE_TO_Y);
14646 dy = line_bottom_y (&it) - y0;
14647
14648 if (dy > scroll_max)
14649 return SCROLLING_FAILED;
14650
14651 if (dy > 0)
14652 scroll_down_p = 1;
14653 }
14654 }
14655
14656 if (scroll_down_p)
14657 {
14658 /* Point is in or below the bottom scroll margin, so move the
14659 window start down. If scrolling conservatively, move it just
14660 enough down to make point visible. If scroll_step is set,
14661 move it down by scroll_step. */
14662 if (arg_scroll_conservatively)
14663 amount_to_scroll
14664 = min (max (dy, frame_line_height),
14665 frame_line_height * arg_scroll_conservatively);
14666 else if (scroll_step || temp_scroll_step)
14667 amount_to_scroll = scroll_max;
14668 else
14669 {
14670 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14671 height = WINDOW_BOX_TEXT_HEIGHT (w);
14672 if (NUMBERP (aggressive))
14673 {
14674 double float_amount = XFLOATINT (aggressive) * height;
14675 int aggressive_scroll = float_amount;
14676 if (aggressive_scroll == 0 && float_amount > 0)
14677 aggressive_scroll = 1;
14678 /* Don't let point enter the scroll margin near top of
14679 the window. This could happen if the value of
14680 scroll_up_aggressively is too large and there are
14681 non-zero margins, because scroll_up_aggressively
14682 means put point that fraction of window height
14683 _from_the_bottom_margin_. */
14684 if (aggressive_scroll + 2*this_scroll_margin > height)
14685 aggressive_scroll = height - 2*this_scroll_margin;
14686 amount_to_scroll = dy + aggressive_scroll;
14687 }
14688 }
14689
14690 if (amount_to_scroll <= 0)
14691 return SCROLLING_FAILED;
14692
14693 start_display (&it, w, startp);
14694 if (arg_scroll_conservatively <= scroll_limit)
14695 move_it_vertically (&it, amount_to_scroll);
14696 else
14697 {
14698 /* Extra precision for users who set scroll-conservatively
14699 to a large number: make sure the amount we scroll
14700 the window start is never less than amount_to_scroll,
14701 which was computed as distance from window bottom to
14702 point. This matters when lines at window top and lines
14703 below window bottom have different height. */
14704 struct it it1;
14705 void *it1data = NULL;
14706 /* We use a temporary it1 because line_bottom_y can modify
14707 its argument, if it moves one line down; see there. */
14708 int start_y;
14709
14710 SAVE_IT (it1, it, it1data);
14711 start_y = line_bottom_y (&it1);
14712 do {
14713 RESTORE_IT (&it, &it, it1data);
14714 move_it_by_lines (&it, 1);
14715 SAVE_IT (it1, it, it1data);
14716 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14717 }
14718
14719 /* If STARTP is unchanged, move it down another screen line. */
14720 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14721 move_it_by_lines (&it, 1);
14722 startp = it.current.pos;
14723 }
14724 else
14725 {
14726 struct text_pos scroll_margin_pos = startp;
14727 int y_offset = 0;
14728
14729 /* See if point is inside the scroll margin at the top of the
14730 window. */
14731 if (this_scroll_margin)
14732 {
14733 int y_start;
14734
14735 start_display (&it, w, startp);
14736 y_start = it.current_y;
14737 move_it_vertically (&it, this_scroll_margin);
14738 scroll_margin_pos = it.current.pos;
14739 /* If we didn't move enough before hitting ZV, request
14740 additional amount of scroll, to move point out of the
14741 scroll margin. */
14742 if (IT_CHARPOS (it) == ZV
14743 && it.current_y - y_start < this_scroll_margin)
14744 y_offset = this_scroll_margin - (it.current_y - y_start);
14745 }
14746
14747 if (PT < CHARPOS (scroll_margin_pos))
14748 {
14749 /* Point is in the scroll margin at the top of the window or
14750 above what is displayed in the window. */
14751 int y0, y_to_move;
14752
14753 /* Compute the vertical distance from PT to the scroll
14754 margin position. Move as far as scroll_max allows, or
14755 one screenful, or 10 screen lines, whichever is largest.
14756 Give up if distance is greater than scroll_max or if we
14757 didn't reach the scroll margin position. */
14758 SET_TEXT_POS (pos, PT, PT_BYTE);
14759 start_display (&it, w, pos);
14760 y0 = it.current_y;
14761 y_to_move = max (it.last_visible_y,
14762 max (scroll_max, 10 * frame_line_height));
14763 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14764 y_to_move, -1,
14765 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14766 dy = it.current_y - y0;
14767 if (dy > scroll_max
14768 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14769 return SCROLLING_FAILED;
14770
14771 /* Additional scroll for when ZV was too close to point. */
14772 dy += y_offset;
14773
14774 /* Compute new window start. */
14775 start_display (&it, w, startp);
14776
14777 if (arg_scroll_conservatively)
14778 amount_to_scroll = max (dy, frame_line_height *
14779 max (scroll_step, temp_scroll_step));
14780 else if (scroll_step || temp_scroll_step)
14781 amount_to_scroll = scroll_max;
14782 else
14783 {
14784 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14785 height = WINDOW_BOX_TEXT_HEIGHT (w);
14786 if (NUMBERP (aggressive))
14787 {
14788 double float_amount = XFLOATINT (aggressive) * height;
14789 int aggressive_scroll = float_amount;
14790 if (aggressive_scroll == 0 && float_amount > 0)
14791 aggressive_scroll = 1;
14792 /* Don't let point enter the scroll margin near
14793 bottom of the window, if the value of
14794 scroll_down_aggressively happens to be too
14795 large. */
14796 if (aggressive_scroll + 2*this_scroll_margin > height)
14797 aggressive_scroll = height - 2*this_scroll_margin;
14798 amount_to_scroll = dy + aggressive_scroll;
14799 }
14800 }
14801
14802 if (amount_to_scroll <= 0)
14803 return SCROLLING_FAILED;
14804
14805 move_it_vertically_backward (&it, amount_to_scroll);
14806 startp = it.current.pos;
14807 }
14808 }
14809
14810 /* Run window scroll functions. */
14811 startp = run_window_scroll_functions (window, startp);
14812
14813 /* Display the window. Give up if new fonts are loaded, or if point
14814 doesn't appear. */
14815 if (!try_window (window, startp, 0))
14816 rc = SCROLLING_NEED_LARGER_MATRICES;
14817 else if (w->cursor.vpos < 0)
14818 {
14819 clear_glyph_matrix (w->desired_matrix);
14820 rc = SCROLLING_FAILED;
14821 }
14822 else
14823 {
14824 /* Maybe forget recorded base line for line number display. */
14825 if (!just_this_one_p
14826 || current_buffer->clip_changed
14827 || BEG_UNCHANGED < CHARPOS (startp))
14828 w->base_line_number = 0;
14829
14830 /* If cursor ends up on a partially visible line,
14831 treat that as being off the bottom of the screen. */
14832 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14833 /* It's possible that the cursor is on the first line of the
14834 buffer, which is partially obscured due to a vscroll
14835 (Bug#7537). In that case, avoid looping forever . */
14836 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14837 {
14838 clear_glyph_matrix (w->desired_matrix);
14839 ++extra_scroll_margin_lines;
14840 goto too_near_end;
14841 }
14842 rc = SCROLLING_SUCCESS;
14843 }
14844
14845 return rc;
14846 }
14847
14848
14849 /* Compute a suitable window start for window W if display of W starts
14850 on a continuation line. Value is non-zero if a new window start
14851 was computed.
14852
14853 The new window start will be computed, based on W's width, starting
14854 from the start of the continued line. It is the start of the
14855 screen line with the minimum distance from the old start W->start. */
14856
14857 static int
14858 compute_window_start_on_continuation_line (struct window *w)
14859 {
14860 struct text_pos pos, start_pos;
14861 int window_start_changed_p = 0;
14862
14863 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14864
14865 /* If window start is on a continuation line... Window start may be
14866 < BEGV in case there's invisible text at the start of the
14867 buffer (M-x rmail, for example). */
14868 if (CHARPOS (start_pos) > BEGV
14869 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14870 {
14871 struct it it;
14872 struct glyph_row *row;
14873
14874 /* Handle the case that the window start is out of range. */
14875 if (CHARPOS (start_pos) < BEGV)
14876 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14877 else if (CHARPOS (start_pos) > ZV)
14878 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14879
14880 /* Find the start of the continued line. This should be fast
14881 because find_newline is fast (newline cache). */
14882 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14883 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14884 row, DEFAULT_FACE_ID);
14885 reseat_at_previous_visible_line_start (&it);
14886
14887 /* If the line start is "too far" away from the window start,
14888 say it takes too much time to compute a new window start. */
14889 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14890 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14891 {
14892 int min_distance, distance;
14893
14894 /* Move forward by display lines to find the new window
14895 start. If window width was enlarged, the new start can
14896 be expected to be > the old start. If window width was
14897 decreased, the new window start will be < the old start.
14898 So, we're looking for the display line start with the
14899 minimum distance from the old window start. */
14900 pos = it.current.pos;
14901 min_distance = INFINITY;
14902 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14903 distance < min_distance)
14904 {
14905 min_distance = distance;
14906 pos = it.current.pos;
14907 move_it_by_lines (&it, 1);
14908 }
14909
14910 /* Set the window start there. */
14911 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14912 window_start_changed_p = 1;
14913 }
14914 }
14915
14916 return window_start_changed_p;
14917 }
14918
14919
14920 /* Try cursor movement in case text has not changed in window WINDOW,
14921 with window start STARTP. Value is
14922
14923 CURSOR_MOVEMENT_SUCCESS if successful
14924
14925 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14926
14927 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14928 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14929 we want to scroll as if scroll-step were set to 1. See the code.
14930
14931 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14932 which case we have to abort this redisplay, and adjust matrices
14933 first. */
14934
14935 enum
14936 {
14937 CURSOR_MOVEMENT_SUCCESS,
14938 CURSOR_MOVEMENT_CANNOT_BE_USED,
14939 CURSOR_MOVEMENT_MUST_SCROLL,
14940 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14941 };
14942
14943 static int
14944 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14945 {
14946 struct window *w = XWINDOW (window);
14947 struct frame *f = XFRAME (w->frame);
14948 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14949
14950 #ifdef GLYPH_DEBUG
14951 if (inhibit_try_cursor_movement)
14952 return rc;
14953 #endif
14954
14955 /* Previously, there was a check for Lisp integer in the
14956 if-statement below. Now, this field is converted to
14957 ptrdiff_t, thus zero means invalid position in a buffer. */
14958 eassert (w->last_point > 0);
14959
14960 /* Handle case where text has not changed, only point, and it has
14961 not moved off the frame. */
14962 if (/* Point may be in this window. */
14963 PT >= CHARPOS (startp)
14964 /* Selective display hasn't changed. */
14965 && !current_buffer->clip_changed
14966 /* Function force-mode-line-update is used to force a thorough
14967 redisplay. It sets either windows_or_buffers_changed or
14968 update_mode_lines. So don't take a shortcut here for these
14969 cases. */
14970 && !update_mode_lines
14971 && !windows_or_buffers_changed
14972 && !cursor_type_changed
14973 /* Can't use this case if highlighting a region. When a
14974 region exists, cursor movement has to do more than just
14975 set the cursor. */
14976 && markpos_of_region () < 0
14977 && !w->region_showing
14978 && NILP (Vshow_trailing_whitespace)
14979 /* This code is not used for mini-buffer for the sake of the case
14980 of redisplaying to replace an echo area message; since in
14981 that case the mini-buffer contents per se are usually
14982 unchanged. This code is of no real use in the mini-buffer
14983 since the handling of this_line_start_pos, etc., in redisplay
14984 handles the same cases. */
14985 && !EQ (window, minibuf_window)
14986 /* When splitting windows or for new windows, it happens that
14987 redisplay is called with a nil window_end_vpos or one being
14988 larger than the window. This should really be fixed in
14989 window.c. I don't have this on my list, now, so we do
14990 approximately the same as the old redisplay code. --gerd. */
14991 && INTEGERP (w->window_end_vpos)
14992 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14993 && (FRAME_WINDOW_P (f)
14994 || !overlay_arrow_in_current_buffer_p ()))
14995 {
14996 int this_scroll_margin, top_scroll_margin;
14997 struct glyph_row *row = NULL;
14998 int frame_line_height = default_line_pixel_height (w);
14999 int window_total_lines
15000 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15001
15002 #ifdef GLYPH_DEBUG
15003 debug_method_add (w, "cursor movement");
15004 #endif
15005
15006 /* Scroll if point within this distance from the top or bottom
15007 of the window. This is a pixel value. */
15008 if (scroll_margin > 0)
15009 {
15010 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15011 this_scroll_margin *= frame_line_height;
15012 }
15013 else
15014 this_scroll_margin = 0;
15015
15016 top_scroll_margin = this_scroll_margin;
15017 if (WINDOW_WANTS_HEADER_LINE_P (w))
15018 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15019
15020 /* Start with the row the cursor was displayed during the last
15021 not paused redisplay. Give up if that row is not valid. */
15022 if (w->last_cursor.vpos < 0
15023 || w->last_cursor.vpos >= w->current_matrix->nrows)
15024 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15025 else
15026 {
15027 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15028 if (row->mode_line_p)
15029 ++row;
15030 if (!row->enabled_p)
15031 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15032 }
15033
15034 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15035 {
15036 int scroll_p = 0, must_scroll = 0;
15037 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15038
15039 if (PT > w->last_point)
15040 {
15041 /* Point has moved forward. */
15042 while (MATRIX_ROW_END_CHARPOS (row) < PT
15043 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15044 {
15045 eassert (row->enabled_p);
15046 ++row;
15047 }
15048
15049 /* If the end position of a row equals the start
15050 position of the next row, and PT is at that position,
15051 we would rather display cursor in the next line. */
15052 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15053 && MATRIX_ROW_END_CHARPOS (row) == PT
15054 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15055 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15056 && !cursor_row_p (row))
15057 ++row;
15058
15059 /* If within the scroll margin, scroll. Note that
15060 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15061 the next line would be drawn, and that
15062 this_scroll_margin can be zero. */
15063 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15064 || PT > MATRIX_ROW_END_CHARPOS (row)
15065 /* Line is completely visible last line in window
15066 and PT is to be set in the next line. */
15067 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15068 && PT == MATRIX_ROW_END_CHARPOS (row)
15069 && !row->ends_at_zv_p
15070 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15071 scroll_p = 1;
15072 }
15073 else if (PT < w->last_point)
15074 {
15075 /* Cursor has to be moved backward. Note that PT >=
15076 CHARPOS (startp) because of the outer if-statement. */
15077 while (!row->mode_line_p
15078 && (MATRIX_ROW_START_CHARPOS (row) > PT
15079 || (MATRIX_ROW_START_CHARPOS (row) == PT
15080 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15081 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15082 row > w->current_matrix->rows
15083 && (row-1)->ends_in_newline_from_string_p))))
15084 && (row->y > top_scroll_margin
15085 || CHARPOS (startp) == BEGV))
15086 {
15087 eassert (row->enabled_p);
15088 --row;
15089 }
15090
15091 /* Consider the following case: Window starts at BEGV,
15092 there is invisible, intangible text at BEGV, so that
15093 display starts at some point START > BEGV. It can
15094 happen that we are called with PT somewhere between
15095 BEGV and START. Try to handle that case. */
15096 if (row < w->current_matrix->rows
15097 || row->mode_line_p)
15098 {
15099 row = w->current_matrix->rows;
15100 if (row->mode_line_p)
15101 ++row;
15102 }
15103
15104 /* Due to newlines in overlay strings, we may have to
15105 skip forward over overlay strings. */
15106 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15107 && MATRIX_ROW_END_CHARPOS (row) == PT
15108 && !cursor_row_p (row))
15109 ++row;
15110
15111 /* If within the scroll margin, scroll. */
15112 if (row->y < top_scroll_margin
15113 && CHARPOS (startp) != BEGV)
15114 scroll_p = 1;
15115 }
15116 else
15117 {
15118 /* Cursor did not move. So don't scroll even if cursor line
15119 is partially visible, as it was so before. */
15120 rc = CURSOR_MOVEMENT_SUCCESS;
15121 }
15122
15123 if (PT < MATRIX_ROW_START_CHARPOS (row)
15124 || PT > MATRIX_ROW_END_CHARPOS (row))
15125 {
15126 /* if PT is not in the glyph row, give up. */
15127 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15128 must_scroll = 1;
15129 }
15130 else if (rc != CURSOR_MOVEMENT_SUCCESS
15131 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15132 {
15133 struct glyph_row *row1;
15134
15135 /* If rows are bidi-reordered and point moved, back up
15136 until we find a row that does not belong to a
15137 continuation line. This is because we must consider
15138 all rows of a continued line as candidates for the
15139 new cursor positioning, since row start and end
15140 positions change non-linearly with vertical position
15141 in such rows. */
15142 /* FIXME: Revisit this when glyph ``spilling'' in
15143 continuation lines' rows is implemented for
15144 bidi-reordered rows. */
15145 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15146 MATRIX_ROW_CONTINUATION_LINE_P (row);
15147 --row)
15148 {
15149 /* If we hit the beginning of the displayed portion
15150 without finding the first row of a continued
15151 line, give up. */
15152 if (row <= row1)
15153 {
15154 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15155 break;
15156 }
15157 eassert (row->enabled_p);
15158 }
15159 }
15160 if (must_scroll)
15161 ;
15162 else if (rc != CURSOR_MOVEMENT_SUCCESS
15163 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15164 /* Make sure this isn't a header line by any chance, since
15165 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15166 && !row->mode_line_p
15167 && make_cursor_line_fully_visible_p)
15168 {
15169 if (PT == MATRIX_ROW_END_CHARPOS (row)
15170 && !row->ends_at_zv_p
15171 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15172 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15173 else if (row->height > window_box_height (w))
15174 {
15175 /* If we end up in a partially visible line, let's
15176 make it fully visible, except when it's taller
15177 than the window, in which case we can't do much
15178 about it. */
15179 *scroll_step = 1;
15180 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15181 }
15182 else
15183 {
15184 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15185 if (!cursor_row_fully_visible_p (w, 0, 1))
15186 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15187 else
15188 rc = CURSOR_MOVEMENT_SUCCESS;
15189 }
15190 }
15191 else if (scroll_p)
15192 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15193 else if (rc != CURSOR_MOVEMENT_SUCCESS
15194 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15195 {
15196 /* With bidi-reordered rows, there could be more than
15197 one candidate row whose start and end positions
15198 occlude point. We need to let set_cursor_from_row
15199 find the best candidate. */
15200 /* FIXME: Revisit this when glyph ``spilling'' in
15201 continuation lines' rows is implemented for
15202 bidi-reordered rows. */
15203 int rv = 0;
15204
15205 do
15206 {
15207 int at_zv_p = 0, exact_match_p = 0;
15208
15209 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15210 && PT <= MATRIX_ROW_END_CHARPOS (row)
15211 && cursor_row_p (row))
15212 rv |= set_cursor_from_row (w, row, w->current_matrix,
15213 0, 0, 0, 0);
15214 /* As soon as we've found the exact match for point,
15215 or the first suitable row whose ends_at_zv_p flag
15216 is set, we are done. */
15217 at_zv_p =
15218 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15219 if (rv && !at_zv_p
15220 && w->cursor.hpos >= 0
15221 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15222 w->cursor.vpos))
15223 {
15224 struct glyph_row *candidate =
15225 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15226 struct glyph *g =
15227 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15228 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15229
15230 exact_match_p =
15231 (BUFFERP (g->object) && g->charpos == PT)
15232 || (INTEGERP (g->object)
15233 && (g->charpos == PT
15234 || (g->charpos == 0 && endpos - 1 == PT)));
15235 }
15236 if (rv && (at_zv_p || exact_match_p))
15237 {
15238 rc = CURSOR_MOVEMENT_SUCCESS;
15239 break;
15240 }
15241 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15242 break;
15243 ++row;
15244 }
15245 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15246 || row->continued_p)
15247 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15248 || (MATRIX_ROW_START_CHARPOS (row) == PT
15249 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15250 /* If we didn't find any candidate rows, or exited the
15251 loop before all the candidates were examined, signal
15252 to the caller that this method failed. */
15253 if (rc != CURSOR_MOVEMENT_SUCCESS
15254 && !(rv
15255 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15256 && !row->continued_p))
15257 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15258 else if (rv)
15259 rc = CURSOR_MOVEMENT_SUCCESS;
15260 }
15261 else
15262 {
15263 do
15264 {
15265 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15266 {
15267 rc = CURSOR_MOVEMENT_SUCCESS;
15268 break;
15269 }
15270 ++row;
15271 }
15272 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15273 && MATRIX_ROW_START_CHARPOS (row) == PT
15274 && cursor_row_p (row));
15275 }
15276 }
15277 }
15278
15279 return rc;
15280 }
15281
15282 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15283 static
15284 #endif
15285 void
15286 set_vertical_scroll_bar (struct window *w)
15287 {
15288 ptrdiff_t start, end, whole;
15289
15290 /* Calculate the start and end positions for the current window.
15291 At some point, it would be nice to choose between scrollbars
15292 which reflect the whole buffer size, with special markers
15293 indicating narrowing, and scrollbars which reflect only the
15294 visible region.
15295
15296 Note that mini-buffers sometimes aren't displaying any text. */
15297 if (!MINI_WINDOW_P (w)
15298 || (w == XWINDOW (minibuf_window)
15299 && NILP (echo_area_buffer[0])))
15300 {
15301 struct buffer *buf = XBUFFER (w->contents);
15302 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15303 start = marker_position (w->start) - BUF_BEGV (buf);
15304 /* I don't think this is guaranteed to be right. For the
15305 moment, we'll pretend it is. */
15306 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15307
15308 if (end < start)
15309 end = start;
15310 if (whole < (end - start))
15311 whole = end - start;
15312 }
15313 else
15314 start = end = whole = 0;
15315
15316 /* Indicate what this scroll bar ought to be displaying now. */
15317 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15318 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15319 (w, end - start, whole, start);
15320 }
15321
15322
15323 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15324 selected_window is redisplayed.
15325
15326 We can return without actually redisplaying the window if
15327 fonts_changed_p. In that case, redisplay_internal will
15328 retry. */
15329
15330 static void
15331 redisplay_window (Lisp_Object window, int just_this_one_p)
15332 {
15333 struct window *w = XWINDOW (window);
15334 struct frame *f = XFRAME (w->frame);
15335 struct buffer *buffer = XBUFFER (w->contents);
15336 struct buffer *old = current_buffer;
15337 struct text_pos lpoint, opoint, startp;
15338 int update_mode_line;
15339 int tem;
15340 struct it it;
15341 /* Record it now because it's overwritten. */
15342 int current_matrix_up_to_date_p = 0;
15343 int used_current_matrix_p = 0;
15344 /* This is less strict than current_matrix_up_to_date_p.
15345 It indicates that the buffer contents and narrowing are unchanged. */
15346 int buffer_unchanged_p = 0;
15347 int temp_scroll_step = 0;
15348 ptrdiff_t count = SPECPDL_INDEX ();
15349 int rc;
15350 int centering_position = -1;
15351 int last_line_misfit = 0;
15352 ptrdiff_t beg_unchanged, end_unchanged;
15353 int frame_line_height;
15354
15355 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15356 opoint = lpoint;
15357
15358 #ifdef GLYPH_DEBUG
15359 *w->desired_matrix->method = 0;
15360 #endif
15361
15362 /* Make sure that both W's markers are valid. */
15363 eassert (XMARKER (w->start)->buffer == buffer);
15364 eassert (XMARKER (w->pointm)->buffer == buffer);
15365
15366 restart:
15367 reconsider_clip_changes (w);
15368 frame_line_height = default_line_pixel_height (w);
15369
15370 /* Has the mode line to be updated? */
15371 update_mode_line = (w->update_mode_line
15372 || update_mode_lines
15373 || buffer->clip_changed
15374 || buffer->prevent_redisplay_optimizations_p);
15375
15376 if (MINI_WINDOW_P (w))
15377 {
15378 if (w == XWINDOW (echo_area_window)
15379 && !NILP (echo_area_buffer[0]))
15380 {
15381 if (update_mode_line)
15382 /* We may have to update a tty frame's menu bar or a
15383 tool-bar. Example `M-x C-h C-h C-g'. */
15384 goto finish_menu_bars;
15385 else
15386 /* We've already displayed the echo area glyphs in this window. */
15387 goto finish_scroll_bars;
15388 }
15389 else if ((w != XWINDOW (minibuf_window)
15390 || minibuf_level == 0)
15391 /* When buffer is nonempty, redisplay window normally. */
15392 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15393 /* Quail displays non-mini buffers in minibuffer window.
15394 In that case, redisplay the window normally. */
15395 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15396 {
15397 /* W is a mini-buffer window, but it's not active, so clear
15398 it. */
15399 int yb = window_text_bottom_y (w);
15400 struct glyph_row *row;
15401 int y;
15402
15403 for (y = 0, row = w->desired_matrix->rows;
15404 y < yb;
15405 y += row->height, ++row)
15406 blank_row (w, row, y);
15407 goto finish_scroll_bars;
15408 }
15409
15410 clear_glyph_matrix (w->desired_matrix);
15411 }
15412
15413 /* Otherwise set up data on this window; select its buffer and point
15414 value. */
15415 /* Really select the buffer, for the sake of buffer-local
15416 variables. */
15417 set_buffer_internal_1 (XBUFFER (w->contents));
15418
15419 current_matrix_up_to_date_p
15420 = (w->window_end_valid
15421 && !current_buffer->clip_changed
15422 && !current_buffer->prevent_redisplay_optimizations_p
15423 && !window_outdated (w));
15424
15425 /* Run the window-bottom-change-functions
15426 if it is possible that the text on the screen has changed
15427 (either due to modification of the text, or any other reason). */
15428 if (!current_matrix_up_to_date_p
15429 && !NILP (Vwindow_text_change_functions))
15430 {
15431 safe_run_hooks (Qwindow_text_change_functions);
15432 goto restart;
15433 }
15434
15435 beg_unchanged = BEG_UNCHANGED;
15436 end_unchanged = END_UNCHANGED;
15437
15438 SET_TEXT_POS (opoint, PT, PT_BYTE);
15439
15440 specbind (Qinhibit_point_motion_hooks, Qt);
15441
15442 buffer_unchanged_p
15443 = (w->window_end_valid
15444 && !current_buffer->clip_changed
15445 && !window_outdated (w));
15446
15447 /* When windows_or_buffers_changed is non-zero, we can't rely on
15448 the window end being valid, so set it to nil there. */
15449 if (windows_or_buffers_changed)
15450 {
15451 /* If window starts on a continuation line, maybe adjust the
15452 window start in case the window's width changed. */
15453 if (XMARKER (w->start)->buffer == current_buffer)
15454 compute_window_start_on_continuation_line (w);
15455
15456 w->window_end_valid = 0;
15457 }
15458
15459 /* Some sanity checks. */
15460 CHECK_WINDOW_END (w);
15461 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15462 emacs_abort ();
15463 if (BYTEPOS (opoint) < CHARPOS (opoint))
15464 emacs_abort ();
15465
15466 if (mode_line_update_needed (w))
15467 update_mode_line = 1;
15468
15469 /* Point refers normally to the selected window. For any other
15470 window, set up appropriate value. */
15471 if (!EQ (window, selected_window))
15472 {
15473 ptrdiff_t new_pt = marker_position (w->pointm);
15474 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15475 if (new_pt < BEGV)
15476 {
15477 new_pt = BEGV;
15478 new_pt_byte = BEGV_BYTE;
15479 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15480 }
15481 else if (new_pt > (ZV - 1))
15482 {
15483 new_pt = ZV;
15484 new_pt_byte = ZV_BYTE;
15485 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15486 }
15487
15488 /* We don't use SET_PT so that the point-motion hooks don't run. */
15489 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15490 }
15491
15492 /* If any of the character widths specified in the display table
15493 have changed, invalidate the width run cache. It's true that
15494 this may be a bit late to catch such changes, but the rest of
15495 redisplay goes (non-fatally) haywire when the display table is
15496 changed, so why should we worry about doing any better? */
15497 if (current_buffer->width_run_cache)
15498 {
15499 struct Lisp_Char_Table *disptab = buffer_display_table ();
15500
15501 if (! disptab_matches_widthtab
15502 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15503 {
15504 invalidate_region_cache (current_buffer,
15505 current_buffer->width_run_cache,
15506 BEG, Z);
15507 recompute_width_table (current_buffer, disptab);
15508 }
15509 }
15510
15511 /* If window-start is screwed up, choose a new one. */
15512 if (XMARKER (w->start)->buffer != current_buffer)
15513 goto recenter;
15514
15515 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15516
15517 /* If someone specified a new starting point but did not insist,
15518 check whether it can be used. */
15519 if (w->optional_new_start
15520 && CHARPOS (startp) >= BEGV
15521 && CHARPOS (startp) <= ZV)
15522 {
15523 w->optional_new_start = 0;
15524 start_display (&it, w, startp);
15525 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15526 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15527 if (IT_CHARPOS (it) == PT)
15528 w->force_start = 1;
15529 /* IT may overshoot PT if text at PT is invisible. */
15530 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15531 w->force_start = 1;
15532 }
15533
15534 force_start:
15535
15536 /* Handle case where place to start displaying has been specified,
15537 unless the specified location is outside the accessible range. */
15538 if (w->force_start || window_frozen_p (w))
15539 {
15540 /* We set this later on if we have to adjust point. */
15541 int new_vpos = -1;
15542
15543 w->force_start = 0;
15544 w->vscroll = 0;
15545 w->window_end_valid = 0;
15546
15547 /* Forget any recorded base line for line number display. */
15548 if (!buffer_unchanged_p)
15549 w->base_line_number = 0;
15550
15551 /* Redisplay the mode line. Select the buffer properly for that.
15552 Also, run the hook window-scroll-functions
15553 because we have scrolled. */
15554 /* Note, we do this after clearing force_start because
15555 if there's an error, it is better to forget about force_start
15556 than to get into an infinite loop calling the hook functions
15557 and having them get more errors. */
15558 if (!update_mode_line
15559 || ! NILP (Vwindow_scroll_functions))
15560 {
15561 update_mode_line = 1;
15562 w->update_mode_line = 1;
15563 startp = run_window_scroll_functions (window, startp);
15564 }
15565
15566 if (CHARPOS (startp) < BEGV)
15567 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15568 else if (CHARPOS (startp) > ZV)
15569 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15570
15571 /* Redisplay, then check if cursor has been set during the
15572 redisplay. Give up if new fonts were loaded. */
15573 /* We used to issue a CHECK_MARGINS argument to try_window here,
15574 but this causes scrolling to fail when point begins inside
15575 the scroll margin (bug#148) -- cyd */
15576 if (!try_window (window, startp, 0))
15577 {
15578 w->force_start = 1;
15579 clear_glyph_matrix (w->desired_matrix);
15580 goto need_larger_matrices;
15581 }
15582
15583 if (w->cursor.vpos < 0 && !window_frozen_p (w))
15584 {
15585 /* If point does not appear, try to move point so it does
15586 appear. The desired matrix has been built above, so we
15587 can use it here. */
15588 new_vpos = window_box_height (w) / 2;
15589 }
15590
15591 if (!cursor_row_fully_visible_p (w, 0, 0))
15592 {
15593 /* Point does appear, but on a line partly visible at end of window.
15594 Move it back to a fully-visible line. */
15595 new_vpos = window_box_height (w);
15596 }
15597 else if (w->cursor.vpos >=0)
15598 {
15599 /* Some people insist on not letting point enter the scroll
15600 margin, even though this part handles windows that didn't
15601 scroll at all. */
15602 int window_total_lines
15603 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15604 int margin = min (scroll_margin, window_total_lines / 4);
15605 int pixel_margin = margin * frame_line_height;
15606 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15607
15608 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15609 below, which finds the row to move point to, advances by
15610 the Y coordinate of the _next_ row, see the definition of
15611 MATRIX_ROW_BOTTOM_Y. */
15612 if (w->cursor.vpos < margin + header_line)
15613 {
15614 w->cursor.vpos = -1;
15615 clear_glyph_matrix (w->desired_matrix);
15616 goto try_to_scroll;
15617 }
15618 else
15619 {
15620 int window_height = window_box_height (w);
15621
15622 if (header_line)
15623 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15624 if (w->cursor.y >= window_height - pixel_margin)
15625 {
15626 w->cursor.vpos = -1;
15627 clear_glyph_matrix (w->desired_matrix);
15628 goto try_to_scroll;
15629 }
15630 }
15631 }
15632
15633 /* If we need to move point for either of the above reasons,
15634 now actually do it. */
15635 if (new_vpos >= 0)
15636 {
15637 struct glyph_row *row;
15638
15639 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15640 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15641 ++row;
15642
15643 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15644 MATRIX_ROW_START_BYTEPOS (row));
15645
15646 if (w != XWINDOW (selected_window))
15647 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15648 else if (current_buffer == old)
15649 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15650
15651 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15652
15653 /* If we are highlighting the region, then we just changed
15654 the region, so redisplay to show it. */
15655 if (markpos_of_region () >= 0)
15656 {
15657 clear_glyph_matrix (w->desired_matrix);
15658 if (!try_window (window, startp, 0))
15659 goto need_larger_matrices;
15660 }
15661 }
15662
15663 #ifdef GLYPH_DEBUG
15664 debug_method_add (w, "forced window start");
15665 #endif
15666 goto done;
15667 }
15668
15669 /* Handle case where text has not changed, only point, and it has
15670 not moved off the frame, and we are not retrying after hscroll.
15671 (current_matrix_up_to_date_p is nonzero when retrying.) */
15672 if (current_matrix_up_to_date_p
15673 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15674 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15675 {
15676 switch (rc)
15677 {
15678 case CURSOR_MOVEMENT_SUCCESS:
15679 used_current_matrix_p = 1;
15680 goto done;
15681
15682 case CURSOR_MOVEMENT_MUST_SCROLL:
15683 goto try_to_scroll;
15684
15685 default:
15686 emacs_abort ();
15687 }
15688 }
15689 /* If current starting point was originally the beginning of a line
15690 but no longer is, find a new starting point. */
15691 else if (w->start_at_line_beg
15692 && !(CHARPOS (startp) <= BEGV
15693 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15694 {
15695 #ifdef GLYPH_DEBUG
15696 debug_method_add (w, "recenter 1");
15697 #endif
15698 goto recenter;
15699 }
15700
15701 /* Try scrolling with try_window_id. Value is > 0 if update has
15702 been done, it is -1 if we know that the same window start will
15703 not work. It is 0 if unsuccessful for some other reason. */
15704 else if ((tem = try_window_id (w)) != 0)
15705 {
15706 #ifdef GLYPH_DEBUG
15707 debug_method_add (w, "try_window_id %d", tem);
15708 #endif
15709
15710 if (fonts_changed_p)
15711 goto need_larger_matrices;
15712 if (tem > 0)
15713 goto done;
15714
15715 /* Otherwise try_window_id has returned -1 which means that we
15716 don't want the alternative below this comment to execute. */
15717 }
15718 else if (CHARPOS (startp) >= BEGV
15719 && CHARPOS (startp) <= ZV
15720 && PT >= CHARPOS (startp)
15721 && (CHARPOS (startp) < ZV
15722 /* Avoid starting at end of buffer. */
15723 || CHARPOS (startp) == BEGV
15724 || !window_outdated (w)))
15725 {
15726 int d1, d2, d3, d4, d5, d6;
15727
15728 /* If first window line is a continuation line, and window start
15729 is inside the modified region, but the first change is before
15730 current window start, we must select a new window start.
15731
15732 However, if this is the result of a down-mouse event (e.g. by
15733 extending the mouse-drag-overlay), we don't want to select a
15734 new window start, since that would change the position under
15735 the mouse, resulting in an unwanted mouse-movement rather
15736 than a simple mouse-click. */
15737 if (!w->start_at_line_beg
15738 && NILP (do_mouse_tracking)
15739 && CHARPOS (startp) > BEGV
15740 && CHARPOS (startp) > BEG + beg_unchanged
15741 && CHARPOS (startp) <= Z - end_unchanged
15742 /* Even if w->start_at_line_beg is nil, a new window may
15743 start at a line_beg, since that's how set_buffer_window
15744 sets it. So, we need to check the return value of
15745 compute_window_start_on_continuation_line. (See also
15746 bug#197). */
15747 && XMARKER (w->start)->buffer == current_buffer
15748 && compute_window_start_on_continuation_line (w)
15749 /* It doesn't make sense to force the window start like we
15750 do at label force_start if it is already known that point
15751 will not be visible in the resulting window, because
15752 doing so will move point from its correct position
15753 instead of scrolling the window to bring point into view.
15754 See bug#9324. */
15755 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15756 {
15757 w->force_start = 1;
15758 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15759 goto force_start;
15760 }
15761
15762 #ifdef GLYPH_DEBUG
15763 debug_method_add (w, "same window start");
15764 #endif
15765
15766 /* Try to redisplay starting at same place as before.
15767 If point has not moved off frame, accept the results. */
15768 if (!current_matrix_up_to_date_p
15769 /* Don't use try_window_reusing_current_matrix in this case
15770 because a window scroll function can have changed the
15771 buffer. */
15772 || !NILP (Vwindow_scroll_functions)
15773 || MINI_WINDOW_P (w)
15774 || !(used_current_matrix_p
15775 = try_window_reusing_current_matrix (w)))
15776 {
15777 IF_DEBUG (debug_method_add (w, "1"));
15778 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15779 /* -1 means we need to scroll.
15780 0 means we need new matrices, but fonts_changed_p
15781 is set in that case, so we will detect it below. */
15782 goto try_to_scroll;
15783 }
15784
15785 if (fonts_changed_p)
15786 goto need_larger_matrices;
15787
15788 if (w->cursor.vpos >= 0)
15789 {
15790 if (!just_this_one_p
15791 || current_buffer->clip_changed
15792 || BEG_UNCHANGED < CHARPOS (startp))
15793 /* Forget any recorded base line for line number display. */
15794 w->base_line_number = 0;
15795
15796 if (!cursor_row_fully_visible_p (w, 1, 0))
15797 {
15798 clear_glyph_matrix (w->desired_matrix);
15799 last_line_misfit = 1;
15800 }
15801 /* Drop through and scroll. */
15802 else
15803 goto done;
15804 }
15805 else
15806 clear_glyph_matrix (w->desired_matrix);
15807 }
15808
15809 try_to_scroll:
15810
15811 /* Redisplay the mode line. Select the buffer properly for that. */
15812 if (!update_mode_line)
15813 {
15814 update_mode_line = 1;
15815 w->update_mode_line = 1;
15816 }
15817
15818 /* Try to scroll by specified few lines. */
15819 if ((scroll_conservatively
15820 || emacs_scroll_step
15821 || temp_scroll_step
15822 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15823 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15824 && CHARPOS (startp) >= BEGV
15825 && CHARPOS (startp) <= ZV)
15826 {
15827 /* The function returns -1 if new fonts were loaded, 1 if
15828 successful, 0 if not successful. */
15829 int ss = try_scrolling (window, just_this_one_p,
15830 scroll_conservatively,
15831 emacs_scroll_step,
15832 temp_scroll_step, last_line_misfit);
15833 switch (ss)
15834 {
15835 case SCROLLING_SUCCESS:
15836 goto done;
15837
15838 case SCROLLING_NEED_LARGER_MATRICES:
15839 goto need_larger_matrices;
15840
15841 case SCROLLING_FAILED:
15842 break;
15843
15844 default:
15845 emacs_abort ();
15846 }
15847 }
15848
15849 /* Finally, just choose a place to start which positions point
15850 according to user preferences. */
15851
15852 recenter:
15853
15854 #ifdef GLYPH_DEBUG
15855 debug_method_add (w, "recenter");
15856 #endif
15857
15858 /* Forget any previously recorded base line for line number display. */
15859 if (!buffer_unchanged_p)
15860 w->base_line_number = 0;
15861
15862 /* Determine the window start relative to point. */
15863 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15864 it.current_y = it.last_visible_y;
15865 if (centering_position < 0)
15866 {
15867 int window_total_lines
15868 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15869 int margin =
15870 scroll_margin > 0
15871 ? min (scroll_margin, window_total_lines / 4)
15872 : 0;
15873 ptrdiff_t margin_pos = CHARPOS (startp);
15874 Lisp_Object aggressive;
15875 int scrolling_up;
15876
15877 /* If there is a scroll margin at the top of the window, find
15878 its character position. */
15879 if (margin
15880 /* Cannot call start_display if startp is not in the
15881 accessible region of the buffer. This can happen when we
15882 have just switched to a different buffer and/or changed
15883 its restriction. In that case, startp is initialized to
15884 the character position 1 (BEGV) because we did not yet
15885 have chance to display the buffer even once. */
15886 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15887 {
15888 struct it it1;
15889 void *it1data = NULL;
15890
15891 SAVE_IT (it1, it, it1data);
15892 start_display (&it1, w, startp);
15893 move_it_vertically (&it1, margin * frame_line_height);
15894 margin_pos = IT_CHARPOS (it1);
15895 RESTORE_IT (&it, &it, it1data);
15896 }
15897 scrolling_up = PT > margin_pos;
15898 aggressive =
15899 scrolling_up
15900 ? BVAR (current_buffer, scroll_up_aggressively)
15901 : BVAR (current_buffer, scroll_down_aggressively);
15902
15903 if (!MINI_WINDOW_P (w)
15904 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15905 {
15906 int pt_offset = 0;
15907
15908 /* Setting scroll-conservatively overrides
15909 scroll-*-aggressively. */
15910 if (!scroll_conservatively && NUMBERP (aggressive))
15911 {
15912 double float_amount = XFLOATINT (aggressive);
15913
15914 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15915 if (pt_offset == 0 && float_amount > 0)
15916 pt_offset = 1;
15917 if (pt_offset && margin > 0)
15918 margin -= 1;
15919 }
15920 /* Compute how much to move the window start backward from
15921 point so that point will be displayed where the user
15922 wants it. */
15923 if (scrolling_up)
15924 {
15925 centering_position = it.last_visible_y;
15926 if (pt_offset)
15927 centering_position -= pt_offset;
15928 centering_position -=
15929 frame_line_height * (1 + margin + (last_line_misfit != 0))
15930 + WINDOW_HEADER_LINE_HEIGHT (w);
15931 /* Don't let point enter the scroll margin near top of
15932 the window. */
15933 if (centering_position < margin * frame_line_height)
15934 centering_position = margin * frame_line_height;
15935 }
15936 else
15937 centering_position = margin * frame_line_height + pt_offset;
15938 }
15939 else
15940 /* Set the window start half the height of the window backward
15941 from point. */
15942 centering_position = window_box_height (w) / 2;
15943 }
15944 move_it_vertically_backward (&it, centering_position);
15945
15946 eassert (IT_CHARPOS (it) >= BEGV);
15947
15948 /* The function move_it_vertically_backward may move over more
15949 than the specified y-distance. If it->w is small, e.g. a
15950 mini-buffer window, we may end up in front of the window's
15951 display area. Start displaying at the start of the line
15952 containing PT in this case. */
15953 if (it.current_y <= 0)
15954 {
15955 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15956 move_it_vertically_backward (&it, 0);
15957 it.current_y = 0;
15958 }
15959
15960 it.current_x = it.hpos = 0;
15961
15962 /* Set the window start position here explicitly, to avoid an
15963 infinite loop in case the functions in window-scroll-functions
15964 get errors. */
15965 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15966
15967 /* Run scroll hooks. */
15968 startp = run_window_scroll_functions (window, it.current.pos);
15969
15970 /* Redisplay the window. */
15971 if (!current_matrix_up_to_date_p
15972 || windows_or_buffers_changed
15973 || cursor_type_changed
15974 /* Don't use try_window_reusing_current_matrix in this case
15975 because it can have changed the buffer. */
15976 || !NILP (Vwindow_scroll_functions)
15977 || !just_this_one_p
15978 || MINI_WINDOW_P (w)
15979 || !(used_current_matrix_p
15980 = try_window_reusing_current_matrix (w)))
15981 try_window (window, startp, 0);
15982
15983 /* If new fonts have been loaded (due to fontsets), give up. We
15984 have to start a new redisplay since we need to re-adjust glyph
15985 matrices. */
15986 if (fonts_changed_p)
15987 goto need_larger_matrices;
15988
15989 /* If cursor did not appear assume that the middle of the window is
15990 in the first line of the window. Do it again with the next line.
15991 (Imagine a window of height 100, displaying two lines of height
15992 60. Moving back 50 from it->last_visible_y will end in the first
15993 line.) */
15994 if (w->cursor.vpos < 0)
15995 {
15996 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15997 {
15998 clear_glyph_matrix (w->desired_matrix);
15999 move_it_by_lines (&it, 1);
16000 try_window (window, it.current.pos, 0);
16001 }
16002 else if (PT < IT_CHARPOS (it))
16003 {
16004 clear_glyph_matrix (w->desired_matrix);
16005 move_it_by_lines (&it, -1);
16006 try_window (window, it.current.pos, 0);
16007 }
16008 else
16009 {
16010 /* Not much we can do about it. */
16011 }
16012 }
16013
16014 /* Consider the following case: Window starts at BEGV, there is
16015 invisible, intangible text at BEGV, so that display starts at
16016 some point START > BEGV. It can happen that we are called with
16017 PT somewhere between BEGV and START. Try to handle that case. */
16018 if (w->cursor.vpos < 0)
16019 {
16020 struct glyph_row *row = w->current_matrix->rows;
16021 if (row->mode_line_p)
16022 ++row;
16023 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16024 }
16025
16026 if (!cursor_row_fully_visible_p (w, 0, 0))
16027 {
16028 /* If vscroll is enabled, disable it and try again. */
16029 if (w->vscroll)
16030 {
16031 w->vscroll = 0;
16032 clear_glyph_matrix (w->desired_matrix);
16033 goto recenter;
16034 }
16035
16036 /* Users who set scroll-conservatively to a large number want
16037 point just above/below the scroll margin. If we ended up
16038 with point's row partially visible, move the window start to
16039 make that row fully visible and out of the margin. */
16040 if (scroll_conservatively > SCROLL_LIMIT)
16041 {
16042 int window_total_lines
16043 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16044 int margin =
16045 scroll_margin > 0
16046 ? min (scroll_margin, window_total_lines / 4)
16047 : 0;
16048 int move_down = w->cursor.vpos >= window_total_lines / 2;
16049
16050 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16051 clear_glyph_matrix (w->desired_matrix);
16052 if (1 == try_window (window, it.current.pos,
16053 TRY_WINDOW_CHECK_MARGINS))
16054 goto done;
16055 }
16056
16057 /* If centering point failed to make the whole line visible,
16058 put point at the top instead. That has to make the whole line
16059 visible, if it can be done. */
16060 if (centering_position == 0)
16061 goto done;
16062
16063 clear_glyph_matrix (w->desired_matrix);
16064 centering_position = 0;
16065 goto recenter;
16066 }
16067
16068 done:
16069
16070 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16071 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16072 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16073
16074 /* Display the mode line, if we must. */
16075 if ((update_mode_line
16076 /* If window not full width, must redo its mode line
16077 if (a) the window to its side is being redone and
16078 (b) we do a frame-based redisplay. This is a consequence
16079 of how inverted lines are drawn in frame-based redisplay. */
16080 || (!just_this_one_p
16081 && !FRAME_WINDOW_P (f)
16082 && !WINDOW_FULL_WIDTH_P (w))
16083 /* Line number to display. */
16084 || w->base_line_pos > 0
16085 /* Column number is displayed and different from the one displayed. */
16086 || (w->column_number_displayed != -1
16087 && (w->column_number_displayed != current_column ())))
16088 /* This means that the window has a mode line. */
16089 && (WINDOW_WANTS_MODELINE_P (w)
16090 || WINDOW_WANTS_HEADER_LINE_P (w)))
16091 {
16092 display_mode_lines (w);
16093
16094 /* If mode line height has changed, arrange for a thorough
16095 immediate redisplay using the correct mode line height. */
16096 if (WINDOW_WANTS_MODELINE_P (w)
16097 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16098 {
16099 fonts_changed_p = 1;
16100 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16101 = DESIRED_MODE_LINE_HEIGHT (w);
16102 }
16103
16104 /* If header line height has changed, arrange for a thorough
16105 immediate redisplay using the correct header line height. */
16106 if (WINDOW_WANTS_HEADER_LINE_P (w)
16107 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16108 {
16109 fonts_changed_p = 1;
16110 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16111 = DESIRED_HEADER_LINE_HEIGHT (w);
16112 }
16113
16114 if (fonts_changed_p)
16115 goto need_larger_matrices;
16116 }
16117
16118 if (!line_number_displayed && w->base_line_pos != -1)
16119 {
16120 w->base_line_pos = 0;
16121 w->base_line_number = 0;
16122 }
16123
16124 finish_menu_bars:
16125
16126 /* When we reach a frame's selected window, redo the frame's menu bar. */
16127 if (update_mode_line
16128 && EQ (FRAME_SELECTED_WINDOW (f), window))
16129 {
16130 int redisplay_menu_p = 0;
16131
16132 if (FRAME_WINDOW_P (f))
16133 {
16134 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16135 || defined (HAVE_NS) || defined (USE_GTK)
16136 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16137 #else
16138 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16139 #endif
16140 }
16141 else
16142 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16143
16144 if (redisplay_menu_p)
16145 display_menu_bar (w);
16146
16147 #ifdef HAVE_WINDOW_SYSTEM
16148 if (FRAME_WINDOW_P (f))
16149 {
16150 #if defined (USE_GTK) || defined (HAVE_NS)
16151 if (FRAME_EXTERNAL_TOOL_BAR (f))
16152 redisplay_tool_bar (f);
16153 #else
16154 if (WINDOWP (f->tool_bar_window)
16155 && (FRAME_TOOL_BAR_LINES (f) > 0
16156 || !NILP (Vauto_resize_tool_bars))
16157 && redisplay_tool_bar (f))
16158 ignore_mouse_drag_p = 1;
16159 #endif
16160 }
16161 #endif
16162 }
16163
16164 #ifdef HAVE_WINDOW_SYSTEM
16165 if (FRAME_WINDOW_P (f)
16166 && update_window_fringes (w, (just_this_one_p
16167 || (!used_current_matrix_p && !overlay_arrow_seen)
16168 || w->pseudo_window_p)))
16169 {
16170 update_begin (f);
16171 block_input ();
16172 if (draw_window_fringes (w, 1))
16173 x_draw_vertical_border (w);
16174 unblock_input ();
16175 update_end (f);
16176 }
16177 #endif /* HAVE_WINDOW_SYSTEM */
16178
16179 /* We go to this label, with fonts_changed_p set,
16180 if it is necessary to try again using larger glyph matrices.
16181 We have to redeem the scroll bar even in this case,
16182 because the loop in redisplay_internal expects that. */
16183 need_larger_matrices:
16184 ;
16185 finish_scroll_bars:
16186
16187 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16188 {
16189 /* Set the thumb's position and size. */
16190 set_vertical_scroll_bar (w);
16191
16192 /* Note that we actually used the scroll bar attached to this
16193 window, so it shouldn't be deleted at the end of redisplay. */
16194 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16195 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16196 }
16197
16198 /* Restore current_buffer and value of point in it. The window
16199 update may have changed the buffer, so first make sure `opoint'
16200 is still valid (Bug#6177). */
16201 if (CHARPOS (opoint) < BEGV)
16202 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16203 else if (CHARPOS (opoint) > ZV)
16204 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16205 else
16206 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16207
16208 set_buffer_internal_1 (old);
16209 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16210 shorter. This can be caused by log truncation in *Messages*. */
16211 if (CHARPOS (lpoint) <= ZV)
16212 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16213
16214 unbind_to (count, Qnil);
16215 }
16216
16217
16218 /* Build the complete desired matrix of WINDOW with a window start
16219 buffer position POS.
16220
16221 Value is 1 if successful. It is zero if fonts were loaded during
16222 redisplay which makes re-adjusting glyph matrices necessary, and -1
16223 if point would appear in the scroll margins.
16224 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16225 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16226 set in FLAGS.) */
16227
16228 int
16229 try_window (Lisp_Object window, struct text_pos pos, int flags)
16230 {
16231 struct window *w = XWINDOW (window);
16232 struct it it;
16233 struct glyph_row *last_text_row = NULL;
16234 struct frame *f = XFRAME (w->frame);
16235 int frame_line_height = default_line_pixel_height (w);
16236
16237 /* Make POS the new window start. */
16238 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16239
16240 /* Mark cursor position as unknown. No overlay arrow seen. */
16241 w->cursor.vpos = -1;
16242 overlay_arrow_seen = 0;
16243
16244 /* Initialize iterator and info to start at POS. */
16245 start_display (&it, w, pos);
16246
16247 /* Display all lines of W. */
16248 while (it.current_y < it.last_visible_y)
16249 {
16250 if (display_line (&it))
16251 last_text_row = it.glyph_row - 1;
16252 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16253 return 0;
16254 }
16255
16256 /* Don't let the cursor end in the scroll margins. */
16257 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16258 && !MINI_WINDOW_P (w))
16259 {
16260 int this_scroll_margin;
16261 int window_total_lines
16262 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16263
16264 if (scroll_margin > 0)
16265 {
16266 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16267 this_scroll_margin *= frame_line_height;
16268 }
16269 else
16270 this_scroll_margin = 0;
16271
16272 if ((w->cursor.y >= 0 /* not vscrolled */
16273 && w->cursor.y < this_scroll_margin
16274 && CHARPOS (pos) > BEGV
16275 && IT_CHARPOS (it) < ZV)
16276 /* rms: considering make_cursor_line_fully_visible_p here
16277 seems to give wrong results. We don't want to recenter
16278 when the last line is partly visible, we want to allow
16279 that case to be handled in the usual way. */
16280 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16281 {
16282 w->cursor.vpos = -1;
16283 clear_glyph_matrix (w->desired_matrix);
16284 return -1;
16285 }
16286 }
16287
16288 /* If bottom moved off end of frame, change mode line percentage. */
16289 if (XFASTINT (w->window_end_pos) <= 0
16290 && Z != IT_CHARPOS (it))
16291 w->update_mode_line = 1;
16292
16293 /* Set window_end_pos to the offset of the last character displayed
16294 on the window from the end of current_buffer. Set
16295 window_end_vpos to its row number. */
16296 if (last_text_row)
16297 {
16298 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16299 w->window_end_bytepos
16300 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16301 wset_window_end_pos
16302 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16303 wset_window_end_vpos
16304 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16305 eassert
16306 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16307 XFASTINT (w->window_end_vpos))));
16308 }
16309 else
16310 {
16311 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16312 wset_window_end_pos (w, make_number (Z - ZV));
16313 wset_window_end_vpos (w, make_number (0));
16314 }
16315
16316 /* But that is not valid info until redisplay finishes. */
16317 w->window_end_valid = 0;
16318 return 1;
16319 }
16320
16321
16322 \f
16323 /************************************************************************
16324 Window redisplay reusing current matrix when buffer has not changed
16325 ************************************************************************/
16326
16327 /* Try redisplay of window W showing an unchanged buffer with a
16328 different window start than the last time it was displayed by
16329 reusing its current matrix. Value is non-zero if successful.
16330 W->start is the new window start. */
16331
16332 static int
16333 try_window_reusing_current_matrix (struct window *w)
16334 {
16335 struct frame *f = XFRAME (w->frame);
16336 struct glyph_row *bottom_row;
16337 struct it it;
16338 struct run run;
16339 struct text_pos start, new_start;
16340 int nrows_scrolled, i;
16341 struct glyph_row *last_text_row;
16342 struct glyph_row *last_reused_text_row;
16343 struct glyph_row *start_row;
16344 int start_vpos, min_y, max_y;
16345
16346 #ifdef GLYPH_DEBUG
16347 if (inhibit_try_window_reusing)
16348 return 0;
16349 #endif
16350
16351 if (/* This function doesn't handle terminal frames. */
16352 !FRAME_WINDOW_P (f)
16353 /* Don't try to reuse the display if windows have been split
16354 or such. */
16355 || windows_or_buffers_changed
16356 || cursor_type_changed)
16357 return 0;
16358
16359 /* Can't do this if region may have changed. */
16360 if (markpos_of_region () >= 0
16361 || w->region_showing
16362 || !NILP (Vshow_trailing_whitespace))
16363 return 0;
16364
16365 /* If top-line visibility has changed, give up. */
16366 if (WINDOW_WANTS_HEADER_LINE_P (w)
16367 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16368 return 0;
16369
16370 /* Give up if old or new display is scrolled vertically. We could
16371 make this function handle this, but right now it doesn't. */
16372 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16373 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16374 return 0;
16375
16376 /* The variable new_start now holds the new window start. The old
16377 start `start' can be determined from the current matrix. */
16378 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16379 start = start_row->minpos;
16380 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16381
16382 /* Clear the desired matrix for the display below. */
16383 clear_glyph_matrix (w->desired_matrix);
16384
16385 if (CHARPOS (new_start) <= CHARPOS (start))
16386 {
16387 /* Don't use this method if the display starts with an ellipsis
16388 displayed for invisible text. It's not easy to handle that case
16389 below, and it's certainly not worth the effort since this is
16390 not a frequent case. */
16391 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16392 return 0;
16393
16394 IF_DEBUG (debug_method_add (w, "twu1"));
16395
16396 /* Display up to a row that can be reused. The variable
16397 last_text_row is set to the last row displayed that displays
16398 text. Note that it.vpos == 0 if or if not there is a
16399 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16400 start_display (&it, w, new_start);
16401 w->cursor.vpos = -1;
16402 last_text_row = last_reused_text_row = NULL;
16403
16404 while (it.current_y < it.last_visible_y
16405 && !fonts_changed_p)
16406 {
16407 /* If we have reached into the characters in the START row,
16408 that means the line boundaries have changed. So we
16409 can't start copying with the row START. Maybe it will
16410 work to start copying with the following row. */
16411 while (IT_CHARPOS (it) > CHARPOS (start))
16412 {
16413 /* Advance to the next row as the "start". */
16414 start_row++;
16415 start = start_row->minpos;
16416 /* If there are no more rows to try, or just one, give up. */
16417 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16418 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16419 || CHARPOS (start) == ZV)
16420 {
16421 clear_glyph_matrix (w->desired_matrix);
16422 return 0;
16423 }
16424
16425 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16426 }
16427 /* If we have reached alignment, we can copy the rest of the
16428 rows. */
16429 if (IT_CHARPOS (it) == CHARPOS (start)
16430 /* Don't accept "alignment" inside a display vector,
16431 since start_row could have started in the middle of
16432 that same display vector (thus their character
16433 positions match), and we have no way of telling if
16434 that is the case. */
16435 && it.current.dpvec_index < 0)
16436 break;
16437
16438 if (display_line (&it))
16439 last_text_row = it.glyph_row - 1;
16440
16441 }
16442
16443 /* A value of current_y < last_visible_y means that we stopped
16444 at the previous window start, which in turn means that we
16445 have at least one reusable row. */
16446 if (it.current_y < it.last_visible_y)
16447 {
16448 struct glyph_row *row;
16449
16450 /* IT.vpos always starts from 0; it counts text lines. */
16451 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16452
16453 /* Find PT if not already found in the lines displayed. */
16454 if (w->cursor.vpos < 0)
16455 {
16456 int dy = it.current_y - start_row->y;
16457
16458 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16459 row = row_containing_pos (w, PT, row, NULL, dy);
16460 if (row)
16461 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16462 dy, nrows_scrolled);
16463 else
16464 {
16465 clear_glyph_matrix (w->desired_matrix);
16466 return 0;
16467 }
16468 }
16469
16470 /* Scroll the display. Do it before the current matrix is
16471 changed. The problem here is that update has not yet
16472 run, i.e. part of the current matrix is not up to date.
16473 scroll_run_hook will clear the cursor, and use the
16474 current matrix to get the height of the row the cursor is
16475 in. */
16476 run.current_y = start_row->y;
16477 run.desired_y = it.current_y;
16478 run.height = it.last_visible_y - it.current_y;
16479
16480 if (run.height > 0 && run.current_y != run.desired_y)
16481 {
16482 update_begin (f);
16483 FRAME_RIF (f)->update_window_begin_hook (w);
16484 FRAME_RIF (f)->clear_window_mouse_face (w);
16485 FRAME_RIF (f)->scroll_run_hook (w, &run);
16486 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16487 update_end (f);
16488 }
16489
16490 /* Shift current matrix down by nrows_scrolled lines. */
16491 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16492 rotate_matrix (w->current_matrix,
16493 start_vpos,
16494 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16495 nrows_scrolled);
16496
16497 /* Disable lines that must be updated. */
16498 for (i = 0; i < nrows_scrolled; ++i)
16499 (start_row + i)->enabled_p = 0;
16500
16501 /* Re-compute Y positions. */
16502 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16503 max_y = it.last_visible_y;
16504 for (row = start_row + nrows_scrolled;
16505 row < bottom_row;
16506 ++row)
16507 {
16508 row->y = it.current_y;
16509 row->visible_height = row->height;
16510
16511 if (row->y < min_y)
16512 row->visible_height -= min_y - row->y;
16513 if (row->y + row->height > max_y)
16514 row->visible_height -= row->y + row->height - max_y;
16515 if (row->fringe_bitmap_periodic_p)
16516 row->redraw_fringe_bitmaps_p = 1;
16517
16518 it.current_y += row->height;
16519
16520 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16521 last_reused_text_row = row;
16522 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16523 break;
16524 }
16525
16526 /* Disable lines in the current matrix which are now
16527 below the window. */
16528 for (++row; row < bottom_row; ++row)
16529 row->enabled_p = row->mode_line_p = 0;
16530 }
16531
16532 /* Update window_end_pos etc.; last_reused_text_row is the last
16533 reused row from the current matrix containing text, if any.
16534 The value of last_text_row is the last displayed line
16535 containing text. */
16536 if (last_reused_text_row)
16537 {
16538 w->window_end_bytepos
16539 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16540 wset_window_end_pos
16541 (w, make_number (Z
16542 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16543 wset_window_end_vpos
16544 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16545 w->current_matrix)));
16546 }
16547 else if (last_text_row)
16548 {
16549 w->window_end_bytepos
16550 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16551 wset_window_end_pos
16552 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16553 wset_window_end_vpos
16554 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16555 w->desired_matrix)));
16556 }
16557 else
16558 {
16559 /* This window must be completely empty. */
16560 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16561 wset_window_end_pos (w, make_number (Z - ZV));
16562 wset_window_end_vpos (w, make_number (0));
16563 }
16564 w->window_end_valid = 0;
16565
16566 /* Update hint: don't try scrolling again in update_window. */
16567 w->desired_matrix->no_scrolling_p = 1;
16568
16569 #ifdef GLYPH_DEBUG
16570 debug_method_add (w, "try_window_reusing_current_matrix 1");
16571 #endif
16572 return 1;
16573 }
16574 else if (CHARPOS (new_start) > CHARPOS (start))
16575 {
16576 struct glyph_row *pt_row, *row;
16577 struct glyph_row *first_reusable_row;
16578 struct glyph_row *first_row_to_display;
16579 int dy;
16580 int yb = window_text_bottom_y (w);
16581
16582 /* Find the row starting at new_start, if there is one. Don't
16583 reuse a partially visible line at the end. */
16584 first_reusable_row = start_row;
16585 while (first_reusable_row->enabled_p
16586 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16587 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16588 < CHARPOS (new_start)))
16589 ++first_reusable_row;
16590
16591 /* Give up if there is no row to reuse. */
16592 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16593 || !first_reusable_row->enabled_p
16594 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16595 != CHARPOS (new_start)))
16596 return 0;
16597
16598 /* We can reuse fully visible rows beginning with
16599 first_reusable_row to the end of the window. Set
16600 first_row_to_display to the first row that cannot be reused.
16601 Set pt_row to the row containing point, if there is any. */
16602 pt_row = NULL;
16603 for (first_row_to_display = first_reusable_row;
16604 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16605 ++first_row_to_display)
16606 {
16607 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16608 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16609 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16610 && first_row_to_display->ends_at_zv_p
16611 && pt_row == NULL)))
16612 pt_row = first_row_to_display;
16613 }
16614
16615 /* Start displaying at the start of first_row_to_display. */
16616 eassert (first_row_to_display->y < yb);
16617 init_to_row_start (&it, w, first_row_to_display);
16618
16619 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16620 - start_vpos);
16621 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16622 - nrows_scrolled);
16623 it.current_y = (first_row_to_display->y - first_reusable_row->y
16624 + WINDOW_HEADER_LINE_HEIGHT (w));
16625
16626 /* Display lines beginning with first_row_to_display in the
16627 desired matrix. Set last_text_row to the last row displayed
16628 that displays text. */
16629 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16630 if (pt_row == NULL)
16631 w->cursor.vpos = -1;
16632 last_text_row = NULL;
16633 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16634 if (display_line (&it))
16635 last_text_row = it.glyph_row - 1;
16636
16637 /* If point is in a reused row, adjust y and vpos of the cursor
16638 position. */
16639 if (pt_row)
16640 {
16641 w->cursor.vpos -= nrows_scrolled;
16642 w->cursor.y -= first_reusable_row->y - start_row->y;
16643 }
16644
16645 /* Give up if point isn't in a row displayed or reused. (This
16646 also handles the case where w->cursor.vpos < nrows_scrolled
16647 after the calls to display_line, which can happen with scroll
16648 margins. See bug#1295.) */
16649 if (w->cursor.vpos < 0)
16650 {
16651 clear_glyph_matrix (w->desired_matrix);
16652 return 0;
16653 }
16654
16655 /* Scroll the display. */
16656 run.current_y = first_reusable_row->y;
16657 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16658 run.height = it.last_visible_y - run.current_y;
16659 dy = run.current_y - run.desired_y;
16660
16661 if (run.height)
16662 {
16663 update_begin (f);
16664 FRAME_RIF (f)->update_window_begin_hook (w);
16665 FRAME_RIF (f)->clear_window_mouse_face (w);
16666 FRAME_RIF (f)->scroll_run_hook (w, &run);
16667 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16668 update_end (f);
16669 }
16670
16671 /* Adjust Y positions of reused rows. */
16672 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16673 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16674 max_y = it.last_visible_y;
16675 for (row = first_reusable_row; row < first_row_to_display; ++row)
16676 {
16677 row->y -= dy;
16678 row->visible_height = row->height;
16679 if (row->y < min_y)
16680 row->visible_height -= min_y - row->y;
16681 if (row->y + row->height > max_y)
16682 row->visible_height -= row->y + row->height - max_y;
16683 if (row->fringe_bitmap_periodic_p)
16684 row->redraw_fringe_bitmaps_p = 1;
16685 }
16686
16687 /* Scroll the current matrix. */
16688 eassert (nrows_scrolled > 0);
16689 rotate_matrix (w->current_matrix,
16690 start_vpos,
16691 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16692 -nrows_scrolled);
16693
16694 /* Disable rows not reused. */
16695 for (row -= nrows_scrolled; row < bottom_row; ++row)
16696 row->enabled_p = 0;
16697
16698 /* Point may have moved to a different line, so we cannot assume that
16699 the previous cursor position is valid; locate the correct row. */
16700 if (pt_row)
16701 {
16702 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16703 row < bottom_row
16704 && PT >= MATRIX_ROW_END_CHARPOS (row)
16705 && !row->ends_at_zv_p;
16706 row++)
16707 {
16708 w->cursor.vpos++;
16709 w->cursor.y = row->y;
16710 }
16711 if (row < bottom_row)
16712 {
16713 /* Can't simply scan the row for point with
16714 bidi-reordered glyph rows. Let set_cursor_from_row
16715 figure out where to put the cursor, and if it fails,
16716 give up. */
16717 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16718 {
16719 if (!set_cursor_from_row (w, row, w->current_matrix,
16720 0, 0, 0, 0))
16721 {
16722 clear_glyph_matrix (w->desired_matrix);
16723 return 0;
16724 }
16725 }
16726 else
16727 {
16728 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16729 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16730
16731 for (; glyph < end
16732 && (!BUFFERP (glyph->object)
16733 || glyph->charpos < PT);
16734 glyph++)
16735 {
16736 w->cursor.hpos++;
16737 w->cursor.x += glyph->pixel_width;
16738 }
16739 }
16740 }
16741 }
16742
16743 /* Adjust window end. A null value of last_text_row means that
16744 the window end is in reused rows which in turn means that
16745 only its vpos can have changed. */
16746 if (last_text_row)
16747 {
16748 w->window_end_bytepos
16749 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16750 wset_window_end_pos
16751 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16752 wset_window_end_vpos
16753 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16754 w->desired_matrix)));
16755 }
16756 else
16757 {
16758 wset_window_end_vpos
16759 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16760 }
16761
16762 w->window_end_valid = 0;
16763 w->desired_matrix->no_scrolling_p = 1;
16764
16765 #ifdef GLYPH_DEBUG
16766 debug_method_add (w, "try_window_reusing_current_matrix 2");
16767 #endif
16768 return 1;
16769 }
16770
16771 return 0;
16772 }
16773
16774
16775 \f
16776 /************************************************************************
16777 Window redisplay reusing current matrix when buffer has changed
16778 ************************************************************************/
16779
16780 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16781 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16782 ptrdiff_t *, ptrdiff_t *);
16783 static struct glyph_row *
16784 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16785 struct glyph_row *);
16786
16787
16788 /* Return the last row in MATRIX displaying text. If row START is
16789 non-null, start searching with that row. IT gives the dimensions
16790 of the display. Value is null if matrix is empty; otherwise it is
16791 a pointer to the row found. */
16792
16793 static struct glyph_row *
16794 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16795 struct glyph_row *start)
16796 {
16797 struct glyph_row *row, *row_found;
16798
16799 /* Set row_found to the last row in IT->w's current matrix
16800 displaying text. The loop looks funny but think of partially
16801 visible lines. */
16802 row_found = NULL;
16803 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16804 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16805 {
16806 eassert (row->enabled_p);
16807 row_found = row;
16808 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16809 break;
16810 ++row;
16811 }
16812
16813 return row_found;
16814 }
16815
16816
16817 /* Return the last row in the current matrix of W that is not affected
16818 by changes at the start of current_buffer that occurred since W's
16819 current matrix was built. Value is null if no such row exists.
16820
16821 BEG_UNCHANGED us the number of characters unchanged at the start of
16822 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16823 first changed character in current_buffer. Characters at positions <
16824 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16825 when the current matrix was built. */
16826
16827 static struct glyph_row *
16828 find_last_unchanged_at_beg_row (struct window *w)
16829 {
16830 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16831 struct glyph_row *row;
16832 struct glyph_row *row_found = NULL;
16833 int yb = window_text_bottom_y (w);
16834
16835 /* Find the last row displaying unchanged text. */
16836 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16837 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16838 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16839 ++row)
16840 {
16841 if (/* If row ends before first_changed_pos, it is unchanged,
16842 except in some case. */
16843 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16844 /* When row ends in ZV and we write at ZV it is not
16845 unchanged. */
16846 && !row->ends_at_zv_p
16847 /* When first_changed_pos is the end of a continued line,
16848 row is not unchanged because it may be no longer
16849 continued. */
16850 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16851 && (row->continued_p
16852 || row->exact_window_width_line_p))
16853 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16854 needs to be recomputed, so don't consider this row as
16855 unchanged. This happens when the last line was
16856 bidi-reordered and was killed immediately before this
16857 redisplay cycle. In that case, ROW->end stores the
16858 buffer position of the first visual-order character of
16859 the killed text, which is now beyond ZV. */
16860 && CHARPOS (row->end.pos) <= ZV)
16861 row_found = row;
16862
16863 /* Stop if last visible row. */
16864 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16865 break;
16866 }
16867
16868 return row_found;
16869 }
16870
16871
16872 /* Find the first glyph row in the current matrix of W that is not
16873 affected by changes at the end of current_buffer since the
16874 time W's current matrix was built.
16875
16876 Return in *DELTA the number of chars by which buffer positions in
16877 unchanged text at the end of current_buffer must be adjusted.
16878
16879 Return in *DELTA_BYTES the corresponding number of bytes.
16880
16881 Value is null if no such row exists, i.e. all rows are affected by
16882 changes. */
16883
16884 static struct glyph_row *
16885 find_first_unchanged_at_end_row (struct window *w,
16886 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16887 {
16888 struct glyph_row *row;
16889 struct glyph_row *row_found = NULL;
16890
16891 *delta = *delta_bytes = 0;
16892
16893 /* Display must not have been paused, otherwise the current matrix
16894 is not up to date. */
16895 eassert (w->window_end_valid);
16896
16897 /* A value of window_end_pos >= END_UNCHANGED means that the window
16898 end is in the range of changed text. If so, there is no
16899 unchanged row at the end of W's current matrix. */
16900 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16901 return NULL;
16902
16903 /* Set row to the last row in W's current matrix displaying text. */
16904 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16905
16906 /* If matrix is entirely empty, no unchanged row exists. */
16907 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16908 {
16909 /* The value of row is the last glyph row in the matrix having a
16910 meaningful buffer position in it. The end position of row
16911 corresponds to window_end_pos. This allows us to translate
16912 buffer positions in the current matrix to current buffer
16913 positions for characters not in changed text. */
16914 ptrdiff_t Z_old =
16915 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16916 ptrdiff_t Z_BYTE_old =
16917 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16918 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16919 struct glyph_row *first_text_row
16920 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16921
16922 *delta = Z - Z_old;
16923 *delta_bytes = Z_BYTE - Z_BYTE_old;
16924
16925 /* Set last_unchanged_pos to the buffer position of the last
16926 character in the buffer that has not been changed. Z is the
16927 index + 1 of the last character in current_buffer, i.e. by
16928 subtracting END_UNCHANGED we get the index of the last
16929 unchanged character, and we have to add BEG to get its buffer
16930 position. */
16931 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16932 last_unchanged_pos_old = last_unchanged_pos - *delta;
16933
16934 /* Search backward from ROW for a row displaying a line that
16935 starts at a minimum position >= last_unchanged_pos_old. */
16936 for (; row > first_text_row; --row)
16937 {
16938 /* This used to abort, but it can happen.
16939 It is ok to just stop the search instead here. KFS. */
16940 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16941 break;
16942
16943 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16944 row_found = row;
16945 }
16946 }
16947
16948 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16949
16950 return row_found;
16951 }
16952
16953
16954 /* Make sure that glyph rows in the current matrix of window W
16955 reference the same glyph memory as corresponding rows in the
16956 frame's frame matrix. This function is called after scrolling W's
16957 current matrix on a terminal frame in try_window_id and
16958 try_window_reusing_current_matrix. */
16959
16960 static void
16961 sync_frame_with_window_matrix_rows (struct window *w)
16962 {
16963 struct frame *f = XFRAME (w->frame);
16964 struct glyph_row *window_row, *window_row_end, *frame_row;
16965
16966 /* Preconditions: W must be a leaf window and full-width. Its frame
16967 must have a frame matrix. */
16968 eassert (BUFFERP (w->contents));
16969 eassert (WINDOW_FULL_WIDTH_P (w));
16970 eassert (!FRAME_WINDOW_P (f));
16971
16972 /* If W is a full-width window, glyph pointers in W's current matrix
16973 have, by definition, to be the same as glyph pointers in the
16974 corresponding frame matrix. Note that frame matrices have no
16975 marginal areas (see build_frame_matrix). */
16976 window_row = w->current_matrix->rows;
16977 window_row_end = window_row + w->current_matrix->nrows;
16978 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16979 while (window_row < window_row_end)
16980 {
16981 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16982 struct glyph *end = window_row->glyphs[LAST_AREA];
16983
16984 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16985 frame_row->glyphs[TEXT_AREA] = start;
16986 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16987 frame_row->glyphs[LAST_AREA] = end;
16988
16989 /* Disable frame rows whose corresponding window rows have
16990 been disabled in try_window_id. */
16991 if (!window_row->enabled_p)
16992 frame_row->enabled_p = 0;
16993
16994 ++window_row, ++frame_row;
16995 }
16996 }
16997
16998
16999 /* Find the glyph row in window W containing CHARPOS. Consider all
17000 rows between START and END (not inclusive). END null means search
17001 all rows to the end of the display area of W. Value is the row
17002 containing CHARPOS or null. */
17003
17004 struct glyph_row *
17005 row_containing_pos (struct window *w, ptrdiff_t charpos,
17006 struct glyph_row *start, struct glyph_row *end, int dy)
17007 {
17008 struct glyph_row *row = start;
17009 struct glyph_row *best_row = NULL;
17010 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17011 int last_y;
17012
17013 /* If we happen to start on a header-line, skip that. */
17014 if (row->mode_line_p)
17015 ++row;
17016
17017 if ((end && row >= end) || !row->enabled_p)
17018 return NULL;
17019
17020 last_y = window_text_bottom_y (w) - dy;
17021
17022 while (1)
17023 {
17024 /* Give up if we have gone too far. */
17025 if (end && row >= end)
17026 return NULL;
17027 /* This formerly returned if they were equal.
17028 I think that both quantities are of a "last plus one" type;
17029 if so, when they are equal, the row is within the screen. -- rms. */
17030 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17031 return NULL;
17032
17033 /* If it is in this row, return this row. */
17034 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17035 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17036 /* The end position of a row equals the start
17037 position of the next row. If CHARPOS is there, we
17038 would rather consider it displayed in the next
17039 line, except when this line ends in ZV. */
17040 && !row_for_charpos_p (row, charpos)))
17041 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17042 {
17043 struct glyph *g;
17044
17045 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17046 || (!best_row && !row->continued_p))
17047 return row;
17048 /* In bidi-reordered rows, there could be several rows whose
17049 edges surround CHARPOS, all of these rows belonging to
17050 the same continued line. We need to find the row which
17051 fits CHARPOS the best. */
17052 for (g = row->glyphs[TEXT_AREA];
17053 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17054 g++)
17055 {
17056 if (!STRINGP (g->object))
17057 {
17058 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17059 {
17060 mindif = eabs (g->charpos - charpos);
17061 best_row = row;
17062 /* Exact match always wins. */
17063 if (mindif == 0)
17064 return best_row;
17065 }
17066 }
17067 }
17068 }
17069 else if (best_row && !row->continued_p)
17070 return best_row;
17071 ++row;
17072 }
17073 }
17074
17075
17076 /* Try to redisplay window W by reusing its existing display. W's
17077 current matrix must be up to date when this function is called,
17078 i.e. window_end_valid must be nonzero.
17079
17080 Value is
17081
17082 1 if display has been updated
17083 0 if otherwise unsuccessful
17084 -1 if redisplay with same window start is known not to succeed
17085
17086 The following steps are performed:
17087
17088 1. Find the last row in the current matrix of W that is not
17089 affected by changes at the start of current_buffer. If no such row
17090 is found, give up.
17091
17092 2. Find the first row in W's current matrix that is not affected by
17093 changes at the end of current_buffer. Maybe there is no such row.
17094
17095 3. Display lines beginning with the row + 1 found in step 1 to the
17096 row found in step 2 or, if step 2 didn't find a row, to the end of
17097 the window.
17098
17099 4. If cursor is not known to appear on the window, give up.
17100
17101 5. If display stopped at the row found in step 2, scroll the
17102 display and current matrix as needed.
17103
17104 6. Maybe display some lines at the end of W, if we must. This can
17105 happen under various circumstances, like a partially visible line
17106 becoming fully visible, or because newly displayed lines are displayed
17107 in smaller font sizes.
17108
17109 7. Update W's window end information. */
17110
17111 static int
17112 try_window_id (struct window *w)
17113 {
17114 struct frame *f = XFRAME (w->frame);
17115 struct glyph_matrix *current_matrix = w->current_matrix;
17116 struct glyph_matrix *desired_matrix = w->desired_matrix;
17117 struct glyph_row *last_unchanged_at_beg_row;
17118 struct glyph_row *first_unchanged_at_end_row;
17119 struct glyph_row *row;
17120 struct glyph_row *bottom_row;
17121 int bottom_vpos;
17122 struct it it;
17123 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17124 int dvpos, dy;
17125 struct text_pos start_pos;
17126 struct run run;
17127 int first_unchanged_at_end_vpos = 0;
17128 struct glyph_row *last_text_row, *last_text_row_at_end;
17129 struct text_pos start;
17130 ptrdiff_t first_changed_charpos, last_changed_charpos;
17131
17132 #ifdef GLYPH_DEBUG
17133 if (inhibit_try_window_id)
17134 return 0;
17135 #endif
17136
17137 /* This is handy for debugging. */
17138 #if 0
17139 #define GIVE_UP(X) \
17140 do { \
17141 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17142 return 0; \
17143 } while (0)
17144 #else
17145 #define GIVE_UP(X) return 0
17146 #endif
17147
17148 SET_TEXT_POS_FROM_MARKER (start, w->start);
17149
17150 /* Don't use this for mini-windows because these can show
17151 messages and mini-buffers, and we don't handle that here. */
17152 if (MINI_WINDOW_P (w))
17153 GIVE_UP (1);
17154
17155 /* This flag is used to prevent redisplay optimizations. */
17156 if (windows_or_buffers_changed || cursor_type_changed)
17157 GIVE_UP (2);
17158
17159 /* Verify that narrowing has not changed.
17160 Also verify that we were not told to prevent redisplay optimizations.
17161 It would be nice to further
17162 reduce the number of cases where this prevents try_window_id. */
17163 if (current_buffer->clip_changed
17164 || current_buffer->prevent_redisplay_optimizations_p)
17165 GIVE_UP (3);
17166
17167 /* Window must either use window-based redisplay or be full width. */
17168 if (!FRAME_WINDOW_P (f)
17169 && (!FRAME_LINE_INS_DEL_OK (f)
17170 || !WINDOW_FULL_WIDTH_P (w)))
17171 GIVE_UP (4);
17172
17173 /* Give up if point is known NOT to appear in W. */
17174 if (PT < CHARPOS (start))
17175 GIVE_UP (5);
17176
17177 /* Another way to prevent redisplay optimizations. */
17178 if (w->last_modified == 0)
17179 GIVE_UP (6);
17180
17181 /* Verify that window is not hscrolled. */
17182 if (w->hscroll != 0)
17183 GIVE_UP (7);
17184
17185 /* Verify that display wasn't paused. */
17186 if (!w->window_end_valid)
17187 GIVE_UP (8);
17188
17189 /* Can't use this if highlighting a region because a cursor movement
17190 will do more than just set the cursor. */
17191 if (markpos_of_region () >= 0)
17192 GIVE_UP (9);
17193
17194 /* Likewise if highlighting trailing whitespace. */
17195 if (!NILP (Vshow_trailing_whitespace))
17196 GIVE_UP (11);
17197
17198 /* Likewise if showing a region. */
17199 if (w->region_showing)
17200 GIVE_UP (10);
17201
17202 /* Can't use this if overlay arrow position and/or string have
17203 changed. */
17204 if (overlay_arrows_changed_p ())
17205 GIVE_UP (12);
17206
17207 /* When word-wrap is on, adding a space to the first word of a
17208 wrapped line can change the wrap position, altering the line
17209 above it. It might be worthwhile to handle this more
17210 intelligently, but for now just redisplay from scratch. */
17211 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17212 GIVE_UP (21);
17213
17214 /* Under bidi reordering, adding or deleting a character in the
17215 beginning of a paragraph, before the first strong directional
17216 character, can change the base direction of the paragraph (unless
17217 the buffer specifies a fixed paragraph direction), which will
17218 require to redisplay the whole paragraph. It might be worthwhile
17219 to find the paragraph limits and widen the range of redisplayed
17220 lines to that, but for now just give up this optimization and
17221 redisplay from scratch. */
17222 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17223 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17224 GIVE_UP (22);
17225
17226 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17227 only if buffer has really changed. The reason is that the gap is
17228 initially at Z for freshly visited files. The code below would
17229 set end_unchanged to 0 in that case. */
17230 if (MODIFF > SAVE_MODIFF
17231 /* This seems to happen sometimes after saving a buffer. */
17232 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17233 {
17234 if (GPT - BEG < BEG_UNCHANGED)
17235 BEG_UNCHANGED = GPT - BEG;
17236 if (Z - GPT < END_UNCHANGED)
17237 END_UNCHANGED = Z - GPT;
17238 }
17239
17240 /* The position of the first and last character that has been changed. */
17241 first_changed_charpos = BEG + BEG_UNCHANGED;
17242 last_changed_charpos = Z - END_UNCHANGED;
17243
17244 /* If window starts after a line end, and the last change is in
17245 front of that newline, then changes don't affect the display.
17246 This case happens with stealth-fontification. Note that although
17247 the display is unchanged, glyph positions in the matrix have to
17248 be adjusted, of course. */
17249 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17250 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17251 && ((last_changed_charpos < CHARPOS (start)
17252 && CHARPOS (start) == BEGV)
17253 || (last_changed_charpos < CHARPOS (start) - 1
17254 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17255 {
17256 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17257 struct glyph_row *r0;
17258
17259 /* Compute how many chars/bytes have been added to or removed
17260 from the buffer. */
17261 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17262 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17263 Z_delta = Z - Z_old;
17264 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17265
17266 /* Give up if PT is not in the window. Note that it already has
17267 been checked at the start of try_window_id that PT is not in
17268 front of the window start. */
17269 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17270 GIVE_UP (13);
17271
17272 /* If window start is unchanged, we can reuse the whole matrix
17273 as is, after adjusting glyph positions. No need to compute
17274 the window end again, since its offset from Z hasn't changed. */
17275 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17276 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17277 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17278 /* PT must not be in a partially visible line. */
17279 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17280 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17281 {
17282 /* Adjust positions in the glyph matrix. */
17283 if (Z_delta || Z_delta_bytes)
17284 {
17285 struct glyph_row *r1
17286 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17287 increment_matrix_positions (w->current_matrix,
17288 MATRIX_ROW_VPOS (r0, current_matrix),
17289 MATRIX_ROW_VPOS (r1, current_matrix),
17290 Z_delta, Z_delta_bytes);
17291 }
17292
17293 /* Set the cursor. */
17294 row = row_containing_pos (w, PT, r0, NULL, 0);
17295 if (row)
17296 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17297 else
17298 emacs_abort ();
17299 return 1;
17300 }
17301 }
17302
17303 /* Handle the case that changes are all below what is displayed in
17304 the window, and that PT is in the window. This shortcut cannot
17305 be taken if ZV is visible in the window, and text has been added
17306 there that is visible in the window. */
17307 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17308 /* ZV is not visible in the window, or there are no
17309 changes at ZV, actually. */
17310 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17311 || first_changed_charpos == last_changed_charpos))
17312 {
17313 struct glyph_row *r0;
17314
17315 /* Give up if PT is not in the window. Note that it already has
17316 been checked at the start of try_window_id that PT is not in
17317 front of the window start. */
17318 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17319 GIVE_UP (14);
17320
17321 /* If window start is unchanged, we can reuse the whole matrix
17322 as is, without changing glyph positions since no text has
17323 been added/removed in front of the window end. */
17324 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17325 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17326 /* PT must not be in a partially visible line. */
17327 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17328 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17329 {
17330 /* We have to compute the window end anew since text
17331 could have been added/removed after it. */
17332 wset_window_end_pos
17333 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17334 w->window_end_bytepos
17335 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17336
17337 /* Set the cursor. */
17338 row = row_containing_pos (w, PT, r0, NULL, 0);
17339 if (row)
17340 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17341 else
17342 emacs_abort ();
17343 return 2;
17344 }
17345 }
17346
17347 /* Give up if window start is in the changed area.
17348
17349 The condition used to read
17350
17351 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17352
17353 but why that was tested escapes me at the moment. */
17354 if (CHARPOS (start) >= first_changed_charpos
17355 && CHARPOS (start) <= last_changed_charpos)
17356 GIVE_UP (15);
17357
17358 /* Check that window start agrees with the start of the first glyph
17359 row in its current matrix. Check this after we know the window
17360 start is not in changed text, otherwise positions would not be
17361 comparable. */
17362 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17363 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17364 GIVE_UP (16);
17365
17366 /* Give up if the window ends in strings. Overlay strings
17367 at the end are difficult to handle, so don't try. */
17368 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17369 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17370 GIVE_UP (20);
17371
17372 /* Compute the position at which we have to start displaying new
17373 lines. Some of the lines at the top of the window might be
17374 reusable because they are not displaying changed text. Find the
17375 last row in W's current matrix not affected by changes at the
17376 start of current_buffer. Value is null if changes start in the
17377 first line of window. */
17378 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17379 if (last_unchanged_at_beg_row)
17380 {
17381 /* Avoid starting to display in the middle of a character, a TAB
17382 for instance. This is easier than to set up the iterator
17383 exactly, and it's not a frequent case, so the additional
17384 effort wouldn't really pay off. */
17385 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17386 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17387 && last_unchanged_at_beg_row > w->current_matrix->rows)
17388 --last_unchanged_at_beg_row;
17389
17390 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17391 GIVE_UP (17);
17392
17393 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17394 GIVE_UP (18);
17395 start_pos = it.current.pos;
17396
17397 /* Start displaying new lines in the desired matrix at the same
17398 vpos we would use in the current matrix, i.e. below
17399 last_unchanged_at_beg_row. */
17400 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17401 current_matrix);
17402 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17403 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17404
17405 eassert (it.hpos == 0 && it.current_x == 0);
17406 }
17407 else
17408 {
17409 /* There are no reusable lines at the start of the window.
17410 Start displaying in the first text line. */
17411 start_display (&it, w, start);
17412 it.vpos = it.first_vpos;
17413 start_pos = it.current.pos;
17414 }
17415
17416 /* Find the first row that is not affected by changes at the end of
17417 the buffer. Value will be null if there is no unchanged row, in
17418 which case we must redisplay to the end of the window. delta
17419 will be set to the value by which buffer positions beginning with
17420 first_unchanged_at_end_row have to be adjusted due to text
17421 changes. */
17422 first_unchanged_at_end_row
17423 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17424 IF_DEBUG (debug_delta = delta);
17425 IF_DEBUG (debug_delta_bytes = delta_bytes);
17426
17427 /* Set stop_pos to the buffer position up to which we will have to
17428 display new lines. If first_unchanged_at_end_row != NULL, this
17429 is the buffer position of the start of the line displayed in that
17430 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17431 that we don't stop at a buffer position. */
17432 stop_pos = 0;
17433 if (first_unchanged_at_end_row)
17434 {
17435 eassert (last_unchanged_at_beg_row == NULL
17436 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17437
17438 /* If this is a continuation line, move forward to the next one
17439 that isn't. Changes in lines above affect this line.
17440 Caution: this may move first_unchanged_at_end_row to a row
17441 not displaying text. */
17442 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17443 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17444 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17445 < it.last_visible_y))
17446 ++first_unchanged_at_end_row;
17447
17448 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17449 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17450 >= it.last_visible_y))
17451 first_unchanged_at_end_row = NULL;
17452 else
17453 {
17454 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17455 + delta);
17456 first_unchanged_at_end_vpos
17457 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17458 eassert (stop_pos >= Z - END_UNCHANGED);
17459 }
17460 }
17461 else if (last_unchanged_at_beg_row == NULL)
17462 GIVE_UP (19);
17463
17464
17465 #ifdef GLYPH_DEBUG
17466
17467 /* Either there is no unchanged row at the end, or the one we have
17468 now displays text. This is a necessary condition for the window
17469 end pos calculation at the end of this function. */
17470 eassert (first_unchanged_at_end_row == NULL
17471 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17472
17473 debug_last_unchanged_at_beg_vpos
17474 = (last_unchanged_at_beg_row
17475 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17476 : -1);
17477 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17478
17479 #endif /* GLYPH_DEBUG */
17480
17481
17482 /* Display new lines. Set last_text_row to the last new line
17483 displayed which has text on it, i.e. might end up as being the
17484 line where the window_end_vpos is. */
17485 w->cursor.vpos = -1;
17486 last_text_row = NULL;
17487 overlay_arrow_seen = 0;
17488 while (it.current_y < it.last_visible_y
17489 && !fonts_changed_p
17490 && (first_unchanged_at_end_row == NULL
17491 || IT_CHARPOS (it) < stop_pos))
17492 {
17493 if (display_line (&it))
17494 last_text_row = it.glyph_row - 1;
17495 }
17496
17497 if (fonts_changed_p)
17498 return -1;
17499
17500
17501 /* Compute differences in buffer positions, y-positions etc. for
17502 lines reused at the bottom of the window. Compute what we can
17503 scroll. */
17504 if (first_unchanged_at_end_row
17505 /* No lines reused because we displayed everything up to the
17506 bottom of the window. */
17507 && it.current_y < it.last_visible_y)
17508 {
17509 dvpos = (it.vpos
17510 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17511 current_matrix));
17512 dy = it.current_y - first_unchanged_at_end_row->y;
17513 run.current_y = first_unchanged_at_end_row->y;
17514 run.desired_y = run.current_y + dy;
17515 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17516 }
17517 else
17518 {
17519 delta = delta_bytes = dvpos = dy
17520 = run.current_y = run.desired_y = run.height = 0;
17521 first_unchanged_at_end_row = NULL;
17522 }
17523 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17524
17525
17526 /* Find the cursor if not already found. We have to decide whether
17527 PT will appear on this window (it sometimes doesn't, but this is
17528 not a very frequent case.) This decision has to be made before
17529 the current matrix is altered. A value of cursor.vpos < 0 means
17530 that PT is either in one of the lines beginning at
17531 first_unchanged_at_end_row or below the window. Don't care for
17532 lines that might be displayed later at the window end; as
17533 mentioned, this is not a frequent case. */
17534 if (w->cursor.vpos < 0)
17535 {
17536 /* Cursor in unchanged rows at the top? */
17537 if (PT < CHARPOS (start_pos)
17538 && last_unchanged_at_beg_row)
17539 {
17540 row = row_containing_pos (w, PT,
17541 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17542 last_unchanged_at_beg_row + 1, 0);
17543 if (row)
17544 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17545 }
17546
17547 /* Start from first_unchanged_at_end_row looking for PT. */
17548 else if (first_unchanged_at_end_row)
17549 {
17550 row = row_containing_pos (w, PT - delta,
17551 first_unchanged_at_end_row, NULL, 0);
17552 if (row)
17553 set_cursor_from_row (w, row, w->current_matrix, delta,
17554 delta_bytes, dy, dvpos);
17555 }
17556
17557 /* Give up if cursor was not found. */
17558 if (w->cursor.vpos < 0)
17559 {
17560 clear_glyph_matrix (w->desired_matrix);
17561 return -1;
17562 }
17563 }
17564
17565 /* Don't let the cursor end in the scroll margins. */
17566 {
17567 int this_scroll_margin, cursor_height;
17568 int frame_line_height = default_line_pixel_height (w);
17569 int window_total_lines
17570 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17571
17572 this_scroll_margin =
17573 max (0, min (scroll_margin, window_total_lines / 4));
17574 this_scroll_margin *= frame_line_height;
17575 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17576
17577 if ((w->cursor.y < this_scroll_margin
17578 && CHARPOS (start) > BEGV)
17579 /* Old redisplay didn't take scroll margin into account at the bottom,
17580 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17581 || (w->cursor.y + (make_cursor_line_fully_visible_p
17582 ? cursor_height + this_scroll_margin
17583 : 1)) > it.last_visible_y)
17584 {
17585 w->cursor.vpos = -1;
17586 clear_glyph_matrix (w->desired_matrix);
17587 return -1;
17588 }
17589 }
17590
17591 /* Scroll the display. Do it before changing the current matrix so
17592 that xterm.c doesn't get confused about where the cursor glyph is
17593 found. */
17594 if (dy && run.height)
17595 {
17596 update_begin (f);
17597
17598 if (FRAME_WINDOW_P (f))
17599 {
17600 FRAME_RIF (f)->update_window_begin_hook (w);
17601 FRAME_RIF (f)->clear_window_mouse_face (w);
17602 FRAME_RIF (f)->scroll_run_hook (w, &run);
17603 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17604 }
17605 else
17606 {
17607 /* Terminal frame. In this case, dvpos gives the number of
17608 lines to scroll by; dvpos < 0 means scroll up. */
17609 int from_vpos
17610 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17611 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17612 int end = (WINDOW_TOP_EDGE_LINE (w)
17613 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17614 + window_internal_height (w));
17615
17616 #if defined (HAVE_GPM) || defined (MSDOS)
17617 x_clear_window_mouse_face (w);
17618 #endif
17619 /* Perform the operation on the screen. */
17620 if (dvpos > 0)
17621 {
17622 /* Scroll last_unchanged_at_beg_row to the end of the
17623 window down dvpos lines. */
17624 set_terminal_window (f, end);
17625
17626 /* On dumb terminals delete dvpos lines at the end
17627 before inserting dvpos empty lines. */
17628 if (!FRAME_SCROLL_REGION_OK (f))
17629 ins_del_lines (f, end - dvpos, -dvpos);
17630
17631 /* Insert dvpos empty lines in front of
17632 last_unchanged_at_beg_row. */
17633 ins_del_lines (f, from, dvpos);
17634 }
17635 else if (dvpos < 0)
17636 {
17637 /* Scroll up last_unchanged_at_beg_vpos to the end of
17638 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17639 set_terminal_window (f, end);
17640
17641 /* Delete dvpos lines in front of
17642 last_unchanged_at_beg_vpos. ins_del_lines will set
17643 the cursor to the given vpos and emit |dvpos| delete
17644 line sequences. */
17645 ins_del_lines (f, from + dvpos, dvpos);
17646
17647 /* On a dumb terminal insert dvpos empty lines at the
17648 end. */
17649 if (!FRAME_SCROLL_REGION_OK (f))
17650 ins_del_lines (f, end + dvpos, -dvpos);
17651 }
17652
17653 set_terminal_window (f, 0);
17654 }
17655
17656 update_end (f);
17657 }
17658
17659 /* Shift reused rows of the current matrix to the right position.
17660 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17661 text. */
17662 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17663 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17664 if (dvpos < 0)
17665 {
17666 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17667 bottom_vpos, dvpos);
17668 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17669 bottom_vpos);
17670 }
17671 else if (dvpos > 0)
17672 {
17673 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17674 bottom_vpos, dvpos);
17675 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17676 first_unchanged_at_end_vpos + dvpos);
17677 }
17678
17679 /* For frame-based redisplay, make sure that current frame and window
17680 matrix are in sync with respect to glyph memory. */
17681 if (!FRAME_WINDOW_P (f))
17682 sync_frame_with_window_matrix_rows (w);
17683
17684 /* Adjust buffer positions in reused rows. */
17685 if (delta || delta_bytes)
17686 increment_matrix_positions (current_matrix,
17687 first_unchanged_at_end_vpos + dvpos,
17688 bottom_vpos, delta, delta_bytes);
17689
17690 /* Adjust Y positions. */
17691 if (dy)
17692 shift_glyph_matrix (w, current_matrix,
17693 first_unchanged_at_end_vpos + dvpos,
17694 bottom_vpos, dy);
17695
17696 if (first_unchanged_at_end_row)
17697 {
17698 first_unchanged_at_end_row += dvpos;
17699 if (first_unchanged_at_end_row->y >= it.last_visible_y
17700 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17701 first_unchanged_at_end_row = NULL;
17702 }
17703
17704 /* If scrolling up, there may be some lines to display at the end of
17705 the window. */
17706 last_text_row_at_end = NULL;
17707 if (dy < 0)
17708 {
17709 /* Scrolling up can leave for example a partially visible line
17710 at the end of the window to be redisplayed. */
17711 /* Set last_row to the glyph row in the current matrix where the
17712 window end line is found. It has been moved up or down in
17713 the matrix by dvpos. */
17714 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17715 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17716
17717 /* If last_row is the window end line, it should display text. */
17718 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17719
17720 /* If window end line was partially visible before, begin
17721 displaying at that line. Otherwise begin displaying with the
17722 line following it. */
17723 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17724 {
17725 init_to_row_start (&it, w, last_row);
17726 it.vpos = last_vpos;
17727 it.current_y = last_row->y;
17728 }
17729 else
17730 {
17731 init_to_row_end (&it, w, last_row);
17732 it.vpos = 1 + last_vpos;
17733 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17734 ++last_row;
17735 }
17736
17737 /* We may start in a continuation line. If so, we have to
17738 get the right continuation_lines_width and current_x. */
17739 it.continuation_lines_width = last_row->continuation_lines_width;
17740 it.hpos = it.current_x = 0;
17741
17742 /* Display the rest of the lines at the window end. */
17743 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17744 while (it.current_y < it.last_visible_y
17745 && !fonts_changed_p)
17746 {
17747 /* Is it always sure that the display agrees with lines in
17748 the current matrix? I don't think so, so we mark rows
17749 displayed invalid in the current matrix by setting their
17750 enabled_p flag to zero. */
17751 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17752 if (display_line (&it))
17753 last_text_row_at_end = it.glyph_row - 1;
17754 }
17755 }
17756
17757 /* Update window_end_pos and window_end_vpos. */
17758 if (first_unchanged_at_end_row
17759 && !last_text_row_at_end)
17760 {
17761 /* Window end line if one of the preserved rows from the current
17762 matrix. Set row to the last row displaying text in current
17763 matrix starting at first_unchanged_at_end_row, after
17764 scrolling. */
17765 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17766 row = find_last_row_displaying_text (w->current_matrix, &it,
17767 first_unchanged_at_end_row);
17768 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17769
17770 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17771 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17772 wset_window_end_vpos
17773 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17774 eassert (w->window_end_bytepos >= 0);
17775 IF_DEBUG (debug_method_add (w, "A"));
17776 }
17777 else if (last_text_row_at_end)
17778 {
17779 wset_window_end_pos
17780 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17781 w->window_end_bytepos
17782 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17783 wset_window_end_vpos
17784 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17785 desired_matrix)));
17786 eassert (w->window_end_bytepos >= 0);
17787 IF_DEBUG (debug_method_add (w, "B"));
17788 }
17789 else if (last_text_row)
17790 {
17791 /* We have displayed either to the end of the window or at the
17792 end of the window, i.e. the last row with text is to be found
17793 in the desired matrix. */
17794 wset_window_end_pos
17795 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17796 w->window_end_bytepos
17797 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17798 wset_window_end_vpos
17799 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17800 eassert (w->window_end_bytepos >= 0);
17801 }
17802 else if (first_unchanged_at_end_row == NULL
17803 && last_text_row == NULL
17804 && last_text_row_at_end == NULL)
17805 {
17806 /* Displayed to end of window, but no line containing text was
17807 displayed. Lines were deleted at the end of the window. */
17808 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17809 int vpos = XFASTINT (w->window_end_vpos);
17810 struct glyph_row *current_row = current_matrix->rows + vpos;
17811 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17812
17813 for (row = NULL;
17814 row == NULL && vpos >= first_vpos;
17815 --vpos, --current_row, --desired_row)
17816 {
17817 if (desired_row->enabled_p)
17818 {
17819 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17820 row = desired_row;
17821 }
17822 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17823 row = current_row;
17824 }
17825
17826 eassert (row != NULL);
17827 wset_window_end_vpos (w, make_number (vpos + 1));
17828 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17829 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17830 eassert (w->window_end_bytepos >= 0);
17831 IF_DEBUG (debug_method_add (w, "C"));
17832 }
17833 else
17834 emacs_abort ();
17835
17836 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17837 debug_end_vpos = XFASTINT (w->window_end_vpos));
17838
17839 /* Record that display has not been completed. */
17840 w->window_end_valid = 0;
17841 w->desired_matrix->no_scrolling_p = 1;
17842 return 3;
17843
17844 #undef GIVE_UP
17845 }
17846
17847
17848 \f
17849 /***********************************************************************
17850 More debugging support
17851 ***********************************************************************/
17852
17853 #ifdef GLYPH_DEBUG
17854
17855 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17856 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17857 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17858
17859
17860 /* Dump the contents of glyph matrix MATRIX on stderr.
17861
17862 GLYPHS 0 means don't show glyph contents.
17863 GLYPHS 1 means show glyphs in short form
17864 GLYPHS > 1 means show glyphs in long form. */
17865
17866 void
17867 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17868 {
17869 int i;
17870 for (i = 0; i < matrix->nrows; ++i)
17871 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17872 }
17873
17874
17875 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17876 the glyph row and area where the glyph comes from. */
17877
17878 void
17879 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17880 {
17881 if (glyph->type == CHAR_GLYPH
17882 || glyph->type == GLYPHLESS_GLYPH)
17883 {
17884 fprintf (stderr,
17885 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17886 glyph - row->glyphs[TEXT_AREA],
17887 (glyph->type == CHAR_GLYPH
17888 ? 'C'
17889 : 'G'),
17890 glyph->charpos,
17891 (BUFFERP (glyph->object)
17892 ? 'B'
17893 : (STRINGP (glyph->object)
17894 ? 'S'
17895 : (INTEGERP (glyph->object)
17896 ? '0'
17897 : '-'))),
17898 glyph->pixel_width,
17899 glyph->u.ch,
17900 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17901 ? glyph->u.ch
17902 : '.'),
17903 glyph->face_id,
17904 glyph->left_box_line_p,
17905 glyph->right_box_line_p);
17906 }
17907 else if (glyph->type == STRETCH_GLYPH)
17908 {
17909 fprintf (stderr,
17910 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17911 glyph - row->glyphs[TEXT_AREA],
17912 'S',
17913 glyph->charpos,
17914 (BUFFERP (glyph->object)
17915 ? 'B'
17916 : (STRINGP (glyph->object)
17917 ? 'S'
17918 : (INTEGERP (glyph->object)
17919 ? '0'
17920 : '-'))),
17921 glyph->pixel_width,
17922 0,
17923 ' ',
17924 glyph->face_id,
17925 glyph->left_box_line_p,
17926 glyph->right_box_line_p);
17927 }
17928 else if (glyph->type == IMAGE_GLYPH)
17929 {
17930 fprintf (stderr,
17931 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17932 glyph - row->glyphs[TEXT_AREA],
17933 'I',
17934 glyph->charpos,
17935 (BUFFERP (glyph->object)
17936 ? 'B'
17937 : (STRINGP (glyph->object)
17938 ? 'S'
17939 : (INTEGERP (glyph->object)
17940 ? '0'
17941 : '-'))),
17942 glyph->pixel_width,
17943 glyph->u.img_id,
17944 '.',
17945 glyph->face_id,
17946 glyph->left_box_line_p,
17947 glyph->right_box_line_p);
17948 }
17949 else if (glyph->type == COMPOSITE_GLYPH)
17950 {
17951 fprintf (stderr,
17952 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17953 glyph - row->glyphs[TEXT_AREA],
17954 '+',
17955 glyph->charpos,
17956 (BUFFERP (glyph->object)
17957 ? 'B'
17958 : (STRINGP (glyph->object)
17959 ? 'S'
17960 : (INTEGERP (glyph->object)
17961 ? '0'
17962 : '-'))),
17963 glyph->pixel_width,
17964 glyph->u.cmp.id);
17965 if (glyph->u.cmp.automatic)
17966 fprintf (stderr,
17967 "[%d-%d]",
17968 glyph->slice.cmp.from, glyph->slice.cmp.to);
17969 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17970 glyph->face_id,
17971 glyph->left_box_line_p,
17972 glyph->right_box_line_p);
17973 }
17974 }
17975
17976
17977 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17978 GLYPHS 0 means don't show glyph contents.
17979 GLYPHS 1 means show glyphs in short form
17980 GLYPHS > 1 means show glyphs in long form. */
17981
17982 void
17983 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17984 {
17985 if (glyphs != 1)
17986 {
17987 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17988 fprintf (stderr, "==============================================================================\n");
17989
17990 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17991 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17992 vpos,
17993 MATRIX_ROW_START_CHARPOS (row),
17994 MATRIX_ROW_END_CHARPOS (row),
17995 row->used[TEXT_AREA],
17996 row->contains_overlapping_glyphs_p,
17997 row->enabled_p,
17998 row->truncated_on_left_p,
17999 row->truncated_on_right_p,
18000 row->continued_p,
18001 MATRIX_ROW_CONTINUATION_LINE_P (row),
18002 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18003 row->ends_at_zv_p,
18004 row->fill_line_p,
18005 row->ends_in_middle_of_char_p,
18006 row->starts_in_middle_of_char_p,
18007 row->mouse_face_p,
18008 row->x,
18009 row->y,
18010 row->pixel_width,
18011 row->height,
18012 row->visible_height,
18013 row->ascent,
18014 row->phys_ascent);
18015 /* The next 3 lines should align to "Start" in the header. */
18016 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18017 row->end.overlay_string_index,
18018 row->continuation_lines_width);
18019 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18020 CHARPOS (row->start.string_pos),
18021 CHARPOS (row->end.string_pos));
18022 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18023 row->end.dpvec_index);
18024 }
18025
18026 if (glyphs > 1)
18027 {
18028 int area;
18029
18030 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18031 {
18032 struct glyph *glyph = row->glyphs[area];
18033 struct glyph *glyph_end = glyph + row->used[area];
18034
18035 /* Glyph for a line end in text. */
18036 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18037 ++glyph_end;
18038
18039 if (glyph < glyph_end)
18040 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18041
18042 for (; glyph < glyph_end; ++glyph)
18043 dump_glyph (row, glyph, area);
18044 }
18045 }
18046 else if (glyphs == 1)
18047 {
18048 int area;
18049
18050 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18051 {
18052 char *s = alloca (row->used[area] + 4);
18053 int i;
18054
18055 for (i = 0; i < row->used[area]; ++i)
18056 {
18057 struct glyph *glyph = row->glyphs[area] + i;
18058 if (i == row->used[area] - 1
18059 && area == TEXT_AREA
18060 && INTEGERP (glyph->object)
18061 && glyph->type == CHAR_GLYPH
18062 && glyph->u.ch == ' ')
18063 {
18064 strcpy (&s[i], "[\\n]");
18065 i += 4;
18066 }
18067 else if (glyph->type == CHAR_GLYPH
18068 && glyph->u.ch < 0x80
18069 && glyph->u.ch >= ' ')
18070 s[i] = glyph->u.ch;
18071 else
18072 s[i] = '.';
18073 }
18074
18075 s[i] = '\0';
18076 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18077 }
18078 }
18079 }
18080
18081
18082 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18083 Sdump_glyph_matrix, 0, 1, "p",
18084 doc: /* Dump the current matrix of the selected window to stderr.
18085 Shows contents of glyph row structures. With non-nil
18086 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18087 glyphs in short form, otherwise show glyphs in long form. */)
18088 (Lisp_Object glyphs)
18089 {
18090 struct window *w = XWINDOW (selected_window);
18091 struct buffer *buffer = XBUFFER (w->contents);
18092
18093 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18094 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18095 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18096 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18097 fprintf (stderr, "=============================================\n");
18098 dump_glyph_matrix (w->current_matrix,
18099 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18100 return Qnil;
18101 }
18102
18103
18104 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18105 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18106 (void)
18107 {
18108 struct frame *f = XFRAME (selected_frame);
18109 dump_glyph_matrix (f->current_matrix, 1);
18110 return Qnil;
18111 }
18112
18113
18114 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18115 doc: /* Dump glyph row ROW to stderr.
18116 GLYPH 0 means don't dump glyphs.
18117 GLYPH 1 means dump glyphs in short form.
18118 GLYPH > 1 or omitted means dump glyphs in long form. */)
18119 (Lisp_Object row, Lisp_Object glyphs)
18120 {
18121 struct glyph_matrix *matrix;
18122 EMACS_INT vpos;
18123
18124 CHECK_NUMBER (row);
18125 matrix = XWINDOW (selected_window)->current_matrix;
18126 vpos = XINT (row);
18127 if (vpos >= 0 && vpos < matrix->nrows)
18128 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18129 vpos,
18130 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18131 return Qnil;
18132 }
18133
18134
18135 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18136 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18137 GLYPH 0 means don't dump glyphs.
18138 GLYPH 1 means dump glyphs in short form.
18139 GLYPH > 1 or omitted means dump glyphs in long form. */)
18140 (Lisp_Object row, Lisp_Object glyphs)
18141 {
18142 struct frame *sf = SELECTED_FRAME ();
18143 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18144 EMACS_INT vpos;
18145
18146 CHECK_NUMBER (row);
18147 vpos = XINT (row);
18148 if (vpos >= 0 && vpos < m->nrows)
18149 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18150 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18151 return Qnil;
18152 }
18153
18154
18155 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18156 doc: /* Toggle tracing of redisplay.
18157 With ARG, turn tracing on if and only if ARG is positive. */)
18158 (Lisp_Object arg)
18159 {
18160 if (NILP (arg))
18161 trace_redisplay_p = !trace_redisplay_p;
18162 else
18163 {
18164 arg = Fprefix_numeric_value (arg);
18165 trace_redisplay_p = XINT (arg) > 0;
18166 }
18167
18168 return Qnil;
18169 }
18170
18171
18172 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18173 doc: /* Like `format', but print result to stderr.
18174 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18175 (ptrdiff_t nargs, Lisp_Object *args)
18176 {
18177 Lisp_Object s = Fformat (nargs, args);
18178 fprintf (stderr, "%s", SDATA (s));
18179 return Qnil;
18180 }
18181
18182 #endif /* GLYPH_DEBUG */
18183
18184
18185 \f
18186 /***********************************************************************
18187 Building Desired Matrix Rows
18188 ***********************************************************************/
18189
18190 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18191 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18192
18193 static struct glyph_row *
18194 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18195 {
18196 struct frame *f = XFRAME (WINDOW_FRAME (w));
18197 struct buffer *buffer = XBUFFER (w->contents);
18198 struct buffer *old = current_buffer;
18199 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18200 int arrow_len = SCHARS (overlay_arrow_string);
18201 const unsigned char *arrow_end = arrow_string + arrow_len;
18202 const unsigned char *p;
18203 struct it it;
18204 bool multibyte_p;
18205 int n_glyphs_before;
18206
18207 set_buffer_temp (buffer);
18208 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18209 it.glyph_row->used[TEXT_AREA] = 0;
18210 SET_TEXT_POS (it.position, 0, 0);
18211
18212 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18213 p = arrow_string;
18214 while (p < arrow_end)
18215 {
18216 Lisp_Object face, ilisp;
18217
18218 /* Get the next character. */
18219 if (multibyte_p)
18220 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18221 else
18222 {
18223 it.c = it.char_to_display = *p, it.len = 1;
18224 if (! ASCII_CHAR_P (it.c))
18225 it.char_to_display = BYTE8_TO_CHAR (it.c);
18226 }
18227 p += it.len;
18228
18229 /* Get its face. */
18230 ilisp = make_number (p - arrow_string);
18231 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18232 it.face_id = compute_char_face (f, it.char_to_display, face);
18233
18234 /* Compute its width, get its glyphs. */
18235 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18236 SET_TEXT_POS (it.position, -1, -1);
18237 PRODUCE_GLYPHS (&it);
18238
18239 /* If this character doesn't fit any more in the line, we have
18240 to remove some glyphs. */
18241 if (it.current_x > it.last_visible_x)
18242 {
18243 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18244 break;
18245 }
18246 }
18247
18248 set_buffer_temp (old);
18249 return it.glyph_row;
18250 }
18251
18252
18253 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18254 glyphs to insert is determined by produce_special_glyphs. */
18255
18256 static void
18257 insert_left_trunc_glyphs (struct it *it)
18258 {
18259 struct it truncate_it;
18260 struct glyph *from, *end, *to, *toend;
18261
18262 eassert (!FRAME_WINDOW_P (it->f)
18263 || (!it->glyph_row->reversed_p
18264 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18265 || (it->glyph_row->reversed_p
18266 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18267
18268 /* Get the truncation glyphs. */
18269 truncate_it = *it;
18270 truncate_it.current_x = 0;
18271 truncate_it.face_id = DEFAULT_FACE_ID;
18272 truncate_it.glyph_row = &scratch_glyph_row;
18273 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18274 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18275 truncate_it.object = make_number (0);
18276 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18277
18278 /* Overwrite glyphs from IT with truncation glyphs. */
18279 if (!it->glyph_row->reversed_p)
18280 {
18281 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18282
18283 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18284 end = from + tused;
18285 to = it->glyph_row->glyphs[TEXT_AREA];
18286 toend = to + it->glyph_row->used[TEXT_AREA];
18287 if (FRAME_WINDOW_P (it->f))
18288 {
18289 /* On GUI frames, when variable-size fonts are displayed,
18290 the truncation glyphs may need more pixels than the row's
18291 glyphs they overwrite. We overwrite more glyphs to free
18292 enough screen real estate, and enlarge the stretch glyph
18293 on the right (see display_line), if there is one, to
18294 preserve the screen position of the truncation glyphs on
18295 the right. */
18296 int w = 0;
18297 struct glyph *g = to;
18298 short used;
18299
18300 /* The first glyph could be partially visible, in which case
18301 it->glyph_row->x will be negative. But we want the left
18302 truncation glyphs to be aligned at the left margin of the
18303 window, so we override the x coordinate at which the row
18304 will begin. */
18305 it->glyph_row->x = 0;
18306 while (g < toend && w < it->truncation_pixel_width)
18307 {
18308 w += g->pixel_width;
18309 ++g;
18310 }
18311 if (g - to - tused > 0)
18312 {
18313 memmove (to + tused, g, (toend - g) * sizeof(*g));
18314 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18315 }
18316 used = it->glyph_row->used[TEXT_AREA];
18317 if (it->glyph_row->truncated_on_right_p
18318 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18319 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18320 == STRETCH_GLYPH)
18321 {
18322 int extra = w - it->truncation_pixel_width;
18323
18324 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18325 }
18326 }
18327
18328 while (from < end)
18329 *to++ = *from++;
18330
18331 /* There may be padding glyphs left over. Overwrite them too. */
18332 if (!FRAME_WINDOW_P (it->f))
18333 {
18334 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18335 {
18336 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18337 while (from < end)
18338 *to++ = *from++;
18339 }
18340 }
18341
18342 if (to > toend)
18343 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18344 }
18345 else
18346 {
18347 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18348
18349 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18350 that back to front. */
18351 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18352 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18353 toend = it->glyph_row->glyphs[TEXT_AREA];
18354 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18355 if (FRAME_WINDOW_P (it->f))
18356 {
18357 int w = 0;
18358 struct glyph *g = to;
18359
18360 while (g >= toend && w < it->truncation_pixel_width)
18361 {
18362 w += g->pixel_width;
18363 --g;
18364 }
18365 if (to - g - tused > 0)
18366 to = g + tused;
18367 if (it->glyph_row->truncated_on_right_p
18368 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18369 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18370 {
18371 int extra = w - it->truncation_pixel_width;
18372
18373 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18374 }
18375 }
18376
18377 while (from >= end && to >= toend)
18378 *to-- = *from--;
18379 if (!FRAME_WINDOW_P (it->f))
18380 {
18381 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18382 {
18383 from =
18384 truncate_it.glyph_row->glyphs[TEXT_AREA]
18385 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18386 while (from >= end && to >= toend)
18387 *to-- = *from--;
18388 }
18389 }
18390 if (from >= end)
18391 {
18392 /* Need to free some room before prepending additional
18393 glyphs. */
18394 int move_by = from - end + 1;
18395 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18396 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18397
18398 for ( ; g >= g0; g--)
18399 g[move_by] = *g;
18400 while (from >= end)
18401 *to-- = *from--;
18402 it->glyph_row->used[TEXT_AREA] += move_by;
18403 }
18404 }
18405 }
18406
18407 /* Compute the hash code for ROW. */
18408 unsigned
18409 row_hash (struct glyph_row *row)
18410 {
18411 int area, k;
18412 unsigned hashval = 0;
18413
18414 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18415 for (k = 0; k < row->used[area]; ++k)
18416 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18417 + row->glyphs[area][k].u.val
18418 + row->glyphs[area][k].face_id
18419 + row->glyphs[area][k].padding_p
18420 + (row->glyphs[area][k].type << 2));
18421
18422 return hashval;
18423 }
18424
18425 /* Compute the pixel height and width of IT->glyph_row.
18426
18427 Most of the time, ascent and height of a display line will be equal
18428 to the max_ascent and max_height values of the display iterator
18429 structure. This is not the case if
18430
18431 1. We hit ZV without displaying anything. In this case, max_ascent
18432 and max_height will be zero.
18433
18434 2. We have some glyphs that don't contribute to the line height.
18435 (The glyph row flag contributes_to_line_height_p is for future
18436 pixmap extensions).
18437
18438 The first case is easily covered by using default values because in
18439 these cases, the line height does not really matter, except that it
18440 must not be zero. */
18441
18442 static void
18443 compute_line_metrics (struct it *it)
18444 {
18445 struct glyph_row *row = it->glyph_row;
18446
18447 if (FRAME_WINDOW_P (it->f))
18448 {
18449 int i, min_y, max_y;
18450
18451 /* The line may consist of one space only, that was added to
18452 place the cursor on it. If so, the row's height hasn't been
18453 computed yet. */
18454 if (row->height == 0)
18455 {
18456 if (it->max_ascent + it->max_descent == 0)
18457 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18458 row->ascent = it->max_ascent;
18459 row->height = it->max_ascent + it->max_descent;
18460 row->phys_ascent = it->max_phys_ascent;
18461 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18462 row->extra_line_spacing = it->max_extra_line_spacing;
18463 }
18464
18465 /* Compute the width of this line. */
18466 row->pixel_width = row->x;
18467 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18468 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18469
18470 eassert (row->pixel_width >= 0);
18471 eassert (row->ascent >= 0 && row->height > 0);
18472
18473 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18474 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18475
18476 /* If first line's physical ascent is larger than its logical
18477 ascent, use the physical ascent, and make the row taller.
18478 This makes accented characters fully visible. */
18479 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18480 && row->phys_ascent > row->ascent)
18481 {
18482 row->height += row->phys_ascent - row->ascent;
18483 row->ascent = row->phys_ascent;
18484 }
18485
18486 /* Compute how much of the line is visible. */
18487 row->visible_height = row->height;
18488
18489 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18490 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18491
18492 if (row->y < min_y)
18493 row->visible_height -= min_y - row->y;
18494 if (row->y + row->height > max_y)
18495 row->visible_height -= row->y + row->height - max_y;
18496 }
18497 else
18498 {
18499 row->pixel_width = row->used[TEXT_AREA];
18500 if (row->continued_p)
18501 row->pixel_width -= it->continuation_pixel_width;
18502 else if (row->truncated_on_right_p)
18503 row->pixel_width -= it->truncation_pixel_width;
18504 row->ascent = row->phys_ascent = 0;
18505 row->height = row->phys_height = row->visible_height = 1;
18506 row->extra_line_spacing = 0;
18507 }
18508
18509 /* Compute a hash code for this row. */
18510 row->hash = row_hash (row);
18511
18512 it->max_ascent = it->max_descent = 0;
18513 it->max_phys_ascent = it->max_phys_descent = 0;
18514 }
18515
18516
18517 /* Append one space to the glyph row of iterator IT if doing a
18518 window-based redisplay. The space has the same face as
18519 IT->face_id. Value is non-zero if a space was added.
18520
18521 This function is called to make sure that there is always one glyph
18522 at the end of a glyph row that the cursor can be set on under
18523 window-systems. (If there weren't such a glyph we would not know
18524 how wide and tall a box cursor should be displayed).
18525
18526 At the same time this space let's a nicely handle clearing to the
18527 end of the line if the row ends in italic text. */
18528
18529 static int
18530 append_space_for_newline (struct it *it, int default_face_p)
18531 {
18532 if (FRAME_WINDOW_P (it->f))
18533 {
18534 int n = it->glyph_row->used[TEXT_AREA];
18535
18536 if (it->glyph_row->glyphs[TEXT_AREA] + n
18537 < it->glyph_row->glyphs[1 + TEXT_AREA])
18538 {
18539 /* Save some values that must not be changed.
18540 Must save IT->c and IT->len because otherwise
18541 ITERATOR_AT_END_P wouldn't work anymore after
18542 append_space_for_newline has been called. */
18543 enum display_element_type saved_what = it->what;
18544 int saved_c = it->c, saved_len = it->len;
18545 int saved_char_to_display = it->char_to_display;
18546 int saved_x = it->current_x;
18547 int saved_face_id = it->face_id;
18548 int saved_box_end = it->end_of_box_run_p;
18549 struct text_pos saved_pos;
18550 Lisp_Object saved_object;
18551 struct face *face;
18552
18553 saved_object = it->object;
18554 saved_pos = it->position;
18555
18556 it->what = IT_CHARACTER;
18557 memset (&it->position, 0, sizeof it->position);
18558 it->object = make_number (0);
18559 it->c = it->char_to_display = ' ';
18560 it->len = 1;
18561
18562 /* If the default face was remapped, be sure to use the
18563 remapped face for the appended newline. */
18564 if (default_face_p)
18565 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18566 else if (it->face_before_selective_p)
18567 it->face_id = it->saved_face_id;
18568 face = FACE_FROM_ID (it->f, it->face_id);
18569 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18570 /* In R2L rows, we will prepend a stretch glyph that will
18571 have the end_of_box_run_p flag set for it, so there's no
18572 need for the appended newline glyph to have that flag
18573 set. */
18574 if (it->glyph_row->reversed_p
18575 /* But if the appended newline glyph goes all the way to
18576 the end of the row, there will be no stretch glyph,
18577 so leave the box flag set. */
18578 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18579 it->end_of_box_run_p = 0;
18580
18581 PRODUCE_GLYPHS (it);
18582
18583 it->override_ascent = -1;
18584 it->constrain_row_ascent_descent_p = 0;
18585 it->current_x = saved_x;
18586 it->object = saved_object;
18587 it->position = saved_pos;
18588 it->what = saved_what;
18589 it->face_id = saved_face_id;
18590 it->len = saved_len;
18591 it->c = saved_c;
18592 it->char_to_display = saved_char_to_display;
18593 it->end_of_box_run_p = saved_box_end;
18594 return 1;
18595 }
18596 }
18597
18598 return 0;
18599 }
18600
18601
18602 /* Extend the face of the last glyph in the text area of IT->glyph_row
18603 to the end of the display line. Called from display_line. If the
18604 glyph row is empty, add a space glyph to it so that we know the
18605 face to draw. Set the glyph row flag fill_line_p. If the glyph
18606 row is R2L, prepend a stretch glyph to cover the empty space to the
18607 left of the leftmost glyph. */
18608
18609 static void
18610 extend_face_to_end_of_line (struct it *it)
18611 {
18612 struct face *face, *default_face;
18613 struct frame *f = it->f;
18614
18615 /* If line is already filled, do nothing. Non window-system frames
18616 get a grace of one more ``pixel'' because their characters are
18617 1-``pixel'' wide, so they hit the equality too early. This grace
18618 is needed only for R2L rows that are not continued, to produce
18619 one extra blank where we could display the cursor. */
18620 if (it->current_x >= it->last_visible_x
18621 + (!FRAME_WINDOW_P (f)
18622 && it->glyph_row->reversed_p
18623 && !it->glyph_row->continued_p))
18624 return;
18625
18626 /* The default face, possibly remapped. */
18627 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18628
18629 /* Face extension extends the background and box of IT->face_id
18630 to the end of the line. If the background equals the background
18631 of the frame, we don't have to do anything. */
18632 if (it->face_before_selective_p)
18633 face = FACE_FROM_ID (f, it->saved_face_id);
18634 else
18635 face = FACE_FROM_ID (f, it->face_id);
18636
18637 if (FRAME_WINDOW_P (f)
18638 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18639 && face->box == FACE_NO_BOX
18640 && face->background == FRAME_BACKGROUND_PIXEL (f)
18641 && !face->stipple
18642 && !it->glyph_row->reversed_p)
18643 return;
18644
18645 /* Set the glyph row flag indicating that the face of the last glyph
18646 in the text area has to be drawn to the end of the text area. */
18647 it->glyph_row->fill_line_p = 1;
18648
18649 /* If current character of IT is not ASCII, make sure we have the
18650 ASCII face. This will be automatically undone the next time
18651 get_next_display_element returns a multibyte character. Note
18652 that the character will always be single byte in unibyte
18653 text. */
18654 if (!ASCII_CHAR_P (it->c))
18655 {
18656 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18657 }
18658
18659 if (FRAME_WINDOW_P (f))
18660 {
18661 /* If the row is empty, add a space with the current face of IT,
18662 so that we know which face to draw. */
18663 if (it->glyph_row->used[TEXT_AREA] == 0)
18664 {
18665 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18666 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18667 it->glyph_row->used[TEXT_AREA] = 1;
18668 }
18669 #ifdef HAVE_WINDOW_SYSTEM
18670 if (it->glyph_row->reversed_p)
18671 {
18672 /* Prepend a stretch glyph to the row, such that the
18673 rightmost glyph will be drawn flushed all the way to the
18674 right margin of the window. The stretch glyph that will
18675 occupy the empty space, if any, to the left of the
18676 glyphs. */
18677 struct font *font = face->font ? face->font : FRAME_FONT (f);
18678 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18679 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18680 struct glyph *g;
18681 int row_width, stretch_ascent, stretch_width;
18682 struct text_pos saved_pos;
18683 int saved_face_id, saved_avoid_cursor, saved_box_start;
18684
18685 for (row_width = 0, g = row_start; g < row_end; g++)
18686 row_width += g->pixel_width;
18687 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18688 if (stretch_width > 0)
18689 {
18690 stretch_ascent =
18691 (((it->ascent + it->descent)
18692 * FONT_BASE (font)) / FONT_HEIGHT (font));
18693 saved_pos = it->position;
18694 memset (&it->position, 0, sizeof it->position);
18695 saved_avoid_cursor = it->avoid_cursor_p;
18696 it->avoid_cursor_p = 1;
18697 saved_face_id = it->face_id;
18698 saved_box_start = it->start_of_box_run_p;
18699 /* The last row's stretch glyph should get the default
18700 face, to avoid painting the rest of the window with
18701 the region face, if the region ends at ZV. */
18702 if (it->glyph_row->ends_at_zv_p)
18703 it->face_id = default_face->id;
18704 else
18705 it->face_id = face->id;
18706 it->start_of_box_run_p = 0;
18707 append_stretch_glyph (it, make_number (0), stretch_width,
18708 it->ascent + it->descent, stretch_ascent);
18709 it->position = saved_pos;
18710 it->avoid_cursor_p = saved_avoid_cursor;
18711 it->face_id = saved_face_id;
18712 it->start_of_box_run_p = saved_box_start;
18713 }
18714 }
18715 #endif /* HAVE_WINDOW_SYSTEM */
18716 }
18717 else
18718 {
18719 /* Save some values that must not be changed. */
18720 int saved_x = it->current_x;
18721 struct text_pos saved_pos;
18722 Lisp_Object saved_object;
18723 enum display_element_type saved_what = it->what;
18724 int saved_face_id = it->face_id;
18725
18726 saved_object = it->object;
18727 saved_pos = it->position;
18728
18729 it->what = IT_CHARACTER;
18730 memset (&it->position, 0, sizeof it->position);
18731 it->object = make_number (0);
18732 it->c = it->char_to_display = ' ';
18733 it->len = 1;
18734 /* The last row's blank glyphs should get the default face, to
18735 avoid painting the rest of the window with the region face,
18736 if the region ends at ZV. */
18737 if (it->glyph_row->ends_at_zv_p)
18738 it->face_id = default_face->id;
18739 else
18740 it->face_id = face->id;
18741
18742 PRODUCE_GLYPHS (it);
18743
18744 while (it->current_x <= it->last_visible_x)
18745 PRODUCE_GLYPHS (it);
18746
18747 /* Don't count these blanks really. It would let us insert a left
18748 truncation glyph below and make us set the cursor on them, maybe. */
18749 it->current_x = saved_x;
18750 it->object = saved_object;
18751 it->position = saved_pos;
18752 it->what = saved_what;
18753 it->face_id = saved_face_id;
18754 }
18755 }
18756
18757
18758 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18759 trailing whitespace. */
18760
18761 static int
18762 trailing_whitespace_p (ptrdiff_t charpos)
18763 {
18764 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18765 int c = 0;
18766
18767 while (bytepos < ZV_BYTE
18768 && (c = FETCH_CHAR (bytepos),
18769 c == ' ' || c == '\t'))
18770 ++bytepos;
18771
18772 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18773 {
18774 if (bytepos != PT_BYTE)
18775 return 1;
18776 }
18777 return 0;
18778 }
18779
18780
18781 /* Highlight trailing whitespace, if any, in ROW. */
18782
18783 static void
18784 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18785 {
18786 int used = row->used[TEXT_AREA];
18787
18788 if (used)
18789 {
18790 struct glyph *start = row->glyphs[TEXT_AREA];
18791 struct glyph *glyph = start + used - 1;
18792
18793 if (row->reversed_p)
18794 {
18795 /* Right-to-left rows need to be processed in the opposite
18796 direction, so swap the edge pointers. */
18797 glyph = start;
18798 start = row->glyphs[TEXT_AREA] + used - 1;
18799 }
18800
18801 /* Skip over glyphs inserted to display the cursor at the
18802 end of a line, for extending the face of the last glyph
18803 to the end of the line on terminals, and for truncation
18804 and continuation glyphs. */
18805 if (!row->reversed_p)
18806 {
18807 while (glyph >= start
18808 && glyph->type == CHAR_GLYPH
18809 && INTEGERP (glyph->object))
18810 --glyph;
18811 }
18812 else
18813 {
18814 while (glyph <= start
18815 && glyph->type == CHAR_GLYPH
18816 && INTEGERP (glyph->object))
18817 ++glyph;
18818 }
18819
18820 /* If last glyph is a space or stretch, and it's trailing
18821 whitespace, set the face of all trailing whitespace glyphs in
18822 IT->glyph_row to `trailing-whitespace'. */
18823 if ((row->reversed_p ? glyph <= start : glyph >= start)
18824 && BUFFERP (glyph->object)
18825 && (glyph->type == STRETCH_GLYPH
18826 || (glyph->type == CHAR_GLYPH
18827 && glyph->u.ch == ' '))
18828 && trailing_whitespace_p (glyph->charpos))
18829 {
18830 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18831 if (face_id < 0)
18832 return;
18833
18834 if (!row->reversed_p)
18835 {
18836 while (glyph >= start
18837 && BUFFERP (glyph->object)
18838 && (glyph->type == STRETCH_GLYPH
18839 || (glyph->type == CHAR_GLYPH
18840 && glyph->u.ch == ' ')))
18841 (glyph--)->face_id = face_id;
18842 }
18843 else
18844 {
18845 while (glyph <= start
18846 && BUFFERP (glyph->object)
18847 && (glyph->type == STRETCH_GLYPH
18848 || (glyph->type == CHAR_GLYPH
18849 && glyph->u.ch == ' ')))
18850 (glyph++)->face_id = face_id;
18851 }
18852 }
18853 }
18854 }
18855
18856
18857 /* Value is non-zero if glyph row ROW should be
18858 considered to hold the buffer position CHARPOS. */
18859
18860 static int
18861 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18862 {
18863 int result = 1;
18864
18865 if (charpos == CHARPOS (row->end.pos)
18866 || charpos == MATRIX_ROW_END_CHARPOS (row))
18867 {
18868 /* Suppose the row ends on a string.
18869 Unless the row is continued, that means it ends on a newline
18870 in the string. If it's anything other than a display string
18871 (e.g., a before-string from an overlay), we don't want the
18872 cursor there. (This heuristic seems to give the optimal
18873 behavior for the various types of multi-line strings.)
18874 One exception: if the string has `cursor' property on one of
18875 its characters, we _do_ want the cursor there. */
18876 if (CHARPOS (row->end.string_pos) >= 0)
18877 {
18878 if (row->continued_p)
18879 result = 1;
18880 else
18881 {
18882 /* Check for `display' property. */
18883 struct glyph *beg = row->glyphs[TEXT_AREA];
18884 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18885 struct glyph *glyph;
18886
18887 result = 0;
18888 for (glyph = end; glyph >= beg; --glyph)
18889 if (STRINGP (glyph->object))
18890 {
18891 Lisp_Object prop
18892 = Fget_char_property (make_number (charpos),
18893 Qdisplay, Qnil);
18894 result =
18895 (!NILP (prop)
18896 && display_prop_string_p (prop, glyph->object));
18897 /* If there's a `cursor' property on one of the
18898 string's characters, this row is a cursor row,
18899 even though this is not a display string. */
18900 if (!result)
18901 {
18902 Lisp_Object s = glyph->object;
18903
18904 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18905 {
18906 ptrdiff_t gpos = glyph->charpos;
18907
18908 if (!NILP (Fget_char_property (make_number (gpos),
18909 Qcursor, s)))
18910 {
18911 result = 1;
18912 break;
18913 }
18914 }
18915 }
18916 break;
18917 }
18918 }
18919 }
18920 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18921 {
18922 /* If the row ends in middle of a real character,
18923 and the line is continued, we want the cursor here.
18924 That's because CHARPOS (ROW->end.pos) would equal
18925 PT if PT is before the character. */
18926 if (!row->ends_in_ellipsis_p)
18927 result = row->continued_p;
18928 else
18929 /* If the row ends in an ellipsis, then
18930 CHARPOS (ROW->end.pos) will equal point after the
18931 invisible text. We want that position to be displayed
18932 after the ellipsis. */
18933 result = 0;
18934 }
18935 /* If the row ends at ZV, display the cursor at the end of that
18936 row instead of at the start of the row below. */
18937 else if (row->ends_at_zv_p)
18938 result = 1;
18939 else
18940 result = 0;
18941 }
18942
18943 return result;
18944 }
18945
18946 /* Value is non-zero if glyph row ROW should be
18947 used to hold the cursor. */
18948
18949 static int
18950 cursor_row_p (struct glyph_row *row)
18951 {
18952 return row_for_charpos_p (row, PT);
18953 }
18954
18955 \f
18956
18957 /* Push the property PROP so that it will be rendered at the current
18958 position in IT. Return 1 if PROP was successfully pushed, 0
18959 otherwise. Called from handle_line_prefix to handle the
18960 `line-prefix' and `wrap-prefix' properties. */
18961
18962 static int
18963 push_prefix_prop (struct it *it, Lisp_Object prop)
18964 {
18965 struct text_pos pos =
18966 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18967
18968 eassert (it->method == GET_FROM_BUFFER
18969 || it->method == GET_FROM_DISPLAY_VECTOR
18970 || it->method == GET_FROM_STRING);
18971
18972 /* We need to save the current buffer/string position, so it will be
18973 restored by pop_it, because iterate_out_of_display_property
18974 depends on that being set correctly, but some situations leave
18975 it->position not yet set when this function is called. */
18976 push_it (it, &pos);
18977
18978 if (STRINGP (prop))
18979 {
18980 if (SCHARS (prop) == 0)
18981 {
18982 pop_it (it);
18983 return 0;
18984 }
18985
18986 it->string = prop;
18987 it->string_from_prefix_prop_p = 1;
18988 it->multibyte_p = STRING_MULTIBYTE (it->string);
18989 it->current.overlay_string_index = -1;
18990 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18991 it->end_charpos = it->string_nchars = SCHARS (it->string);
18992 it->method = GET_FROM_STRING;
18993 it->stop_charpos = 0;
18994 it->prev_stop = 0;
18995 it->base_level_stop = 0;
18996
18997 /* Force paragraph direction to be that of the parent
18998 buffer/string. */
18999 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19000 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19001 else
19002 it->paragraph_embedding = L2R;
19003
19004 /* Set up the bidi iterator for this display string. */
19005 if (it->bidi_p)
19006 {
19007 it->bidi_it.string.lstring = it->string;
19008 it->bidi_it.string.s = NULL;
19009 it->bidi_it.string.schars = it->end_charpos;
19010 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19011 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19012 it->bidi_it.string.unibyte = !it->multibyte_p;
19013 it->bidi_it.w = it->w;
19014 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19015 }
19016 }
19017 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19018 {
19019 it->method = GET_FROM_STRETCH;
19020 it->object = prop;
19021 }
19022 #ifdef HAVE_WINDOW_SYSTEM
19023 else if (IMAGEP (prop))
19024 {
19025 it->what = IT_IMAGE;
19026 it->image_id = lookup_image (it->f, prop);
19027 it->method = GET_FROM_IMAGE;
19028 }
19029 #endif /* HAVE_WINDOW_SYSTEM */
19030 else
19031 {
19032 pop_it (it); /* bogus display property, give up */
19033 return 0;
19034 }
19035
19036 return 1;
19037 }
19038
19039 /* Return the character-property PROP at the current position in IT. */
19040
19041 static Lisp_Object
19042 get_it_property (struct it *it, Lisp_Object prop)
19043 {
19044 Lisp_Object position, object = it->object;
19045
19046 if (STRINGP (object))
19047 position = make_number (IT_STRING_CHARPOS (*it));
19048 else if (BUFFERP (object))
19049 {
19050 position = make_number (IT_CHARPOS (*it));
19051 object = it->window;
19052 }
19053 else
19054 return Qnil;
19055
19056 return Fget_char_property (position, prop, object);
19057 }
19058
19059 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19060
19061 static void
19062 handle_line_prefix (struct it *it)
19063 {
19064 Lisp_Object prefix;
19065
19066 if (it->continuation_lines_width > 0)
19067 {
19068 prefix = get_it_property (it, Qwrap_prefix);
19069 if (NILP (prefix))
19070 prefix = Vwrap_prefix;
19071 }
19072 else
19073 {
19074 prefix = get_it_property (it, Qline_prefix);
19075 if (NILP (prefix))
19076 prefix = Vline_prefix;
19077 }
19078 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19079 {
19080 /* If the prefix is wider than the window, and we try to wrap
19081 it, it would acquire its own wrap prefix, and so on till the
19082 iterator stack overflows. So, don't wrap the prefix. */
19083 it->line_wrap = TRUNCATE;
19084 it->avoid_cursor_p = 1;
19085 }
19086 }
19087
19088 \f
19089
19090 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19091 only for R2L lines from display_line and display_string, when they
19092 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19093 the line/string needs to be continued on the next glyph row. */
19094 static void
19095 unproduce_glyphs (struct it *it, int n)
19096 {
19097 struct glyph *glyph, *end;
19098
19099 eassert (it->glyph_row);
19100 eassert (it->glyph_row->reversed_p);
19101 eassert (it->area == TEXT_AREA);
19102 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19103
19104 if (n > it->glyph_row->used[TEXT_AREA])
19105 n = it->glyph_row->used[TEXT_AREA];
19106 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19107 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19108 for ( ; glyph < end; glyph++)
19109 glyph[-n] = *glyph;
19110 }
19111
19112 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19113 and ROW->maxpos. */
19114 static void
19115 find_row_edges (struct it *it, struct glyph_row *row,
19116 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19117 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19118 {
19119 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19120 lines' rows is implemented for bidi-reordered rows. */
19121
19122 /* ROW->minpos is the value of min_pos, the minimal buffer position
19123 we have in ROW, or ROW->start.pos if that is smaller. */
19124 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19125 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19126 else
19127 /* We didn't find buffer positions smaller than ROW->start, or
19128 didn't find _any_ valid buffer positions in any of the glyphs,
19129 so we must trust the iterator's computed positions. */
19130 row->minpos = row->start.pos;
19131 if (max_pos <= 0)
19132 {
19133 max_pos = CHARPOS (it->current.pos);
19134 max_bpos = BYTEPOS (it->current.pos);
19135 }
19136
19137 /* Here are the various use-cases for ending the row, and the
19138 corresponding values for ROW->maxpos:
19139
19140 Line ends in a newline from buffer eol_pos + 1
19141 Line is continued from buffer max_pos + 1
19142 Line is truncated on right it->current.pos
19143 Line ends in a newline from string max_pos + 1(*)
19144 (*) + 1 only when line ends in a forward scan
19145 Line is continued from string max_pos
19146 Line is continued from display vector max_pos
19147 Line is entirely from a string min_pos == max_pos
19148 Line is entirely from a display vector min_pos == max_pos
19149 Line that ends at ZV ZV
19150
19151 If you discover other use-cases, please add them here as
19152 appropriate. */
19153 if (row->ends_at_zv_p)
19154 row->maxpos = it->current.pos;
19155 else if (row->used[TEXT_AREA])
19156 {
19157 int seen_this_string = 0;
19158 struct glyph_row *r1 = row - 1;
19159
19160 /* Did we see the same display string on the previous row? */
19161 if (STRINGP (it->object)
19162 /* this is not the first row */
19163 && row > it->w->desired_matrix->rows
19164 /* previous row is not the header line */
19165 && !r1->mode_line_p
19166 /* previous row also ends in a newline from a string */
19167 && r1->ends_in_newline_from_string_p)
19168 {
19169 struct glyph *start, *end;
19170
19171 /* Search for the last glyph of the previous row that came
19172 from buffer or string. Depending on whether the row is
19173 L2R or R2L, we need to process it front to back or the
19174 other way round. */
19175 if (!r1->reversed_p)
19176 {
19177 start = r1->glyphs[TEXT_AREA];
19178 end = start + r1->used[TEXT_AREA];
19179 /* Glyphs inserted by redisplay have an integer (zero)
19180 as their object. */
19181 while (end > start
19182 && INTEGERP ((end - 1)->object)
19183 && (end - 1)->charpos <= 0)
19184 --end;
19185 if (end > start)
19186 {
19187 if (EQ ((end - 1)->object, it->object))
19188 seen_this_string = 1;
19189 }
19190 else
19191 /* If all the glyphs of the previous row were inserted
19192 by redisplay, it means the previous row was
19193 produced from a single newline, which is only
19194 possible if that newline came from the same string
19195 as the one which produced this ROW. */
19196 seen_this_string = 1;
19197 }
19198 else
19199 {
19200 end = r1->glyphs[TEXT_AREA] - 1;
19201 start = end + r1->used[TEXT_AREA];
19202 while (end < start
19203 && INTEGERP ((end + 1)->object)
19204 && (end + 1)->charpos <= 0)
19205 ++end;
19206 if (end < start)
19207 {
19208 if (EQ ((end + 1)->object, it->object))
19209 seen_this_string = 1;
19210 }
19211 else
19212 seen_this_string = 1;
19213 }
19214 }
19215 /* Take note of each display string that covers a newline only
19216 once, the first time we see it. This is for when a display
19217 string includes more than one newline in it. */
19218 if (row->ends_in_newline_from_string_p && !seen_this_string)
19219 {
19220 /* If we were scanning the buffer forward when we displayed
19221 the string, we want to account for at least one buffer
19222 position that belongs to this row (position covered by
19223 the display string), so that cursor positioning will
19224 consider this row as a candidate when point is at the end
19225 of the visual line represented by this row. This is not
19226 required when scanning back, because max_pos will already
19227 have a much larger value. */
19228 if (CHARPOS (row->end.pos) > max_pos)
19229 INC_BOTH (max_pos, max_bpos);
19230 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19231 }
19232 else if (CHARPOS (it->eol_pos) > 0)
19233 SET_TEXT_POS (row->maxpos,
19234 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19235 else if (row->continued_p)
19236 {
19237 /* If max_pos is different from IT's current position, it
19238 means IT->method does not belong to the display element
19239 at max_pos. However, it also means that the display
19240 element at max_pos was displayed in its entirety on this
19241 line, which is equivalent to saying that the next line
19242 starts at the next buffer position. */
19243 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19244 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19245 else
19246 {
19247 INC_BOTH (max_pos, max_bpos);
19248 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19249 }
19250 }
19251 else if (row->truncated_on_right_p)
19252 /* display_line already called reseat_at_next_visible_line_start,
19253 which puts the iterator at the beginning of the next line, in
19254 the logical order. */
19255 row->maxpos = it->current.pos;
19256 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19257 /* A line that is entirely from a string/image/stretch... */
19258 row->maxpos = row->minpos;
19259 else
19260 emacs_abort ();
19261 }
19262 else
19263 row->maxpos = it->current.pos;
19264 }
19265
19266 /* Construct the glyph row IT->glyph_row in the desired matrix of
19267 IT->w from text at the current position of IT. See dispextern.h
19268 for an overview of struct it. Value is non-zero if
19269 IT->glyph_row displays text, as opposed to a line displaying ZV
19270 only. */
19271
19272 static int
19273 display_line (struct it *it)
19274 {
19275 struct glyph_row *row = it->glyph_row;
19276 Lisp_Object overlay_arrow_string;
19277 struct it wrap_it;
19278 void *wrap_data = NULL;
19279 int may_wrap = 0, wrap_x IF_LINT (= 0);
19280 int wrap_row_used = -1;
19281 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19282 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19283 int wrap_row_extra_line_spacing IF_LINT (= 0);
19284 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19285 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19286 int cvpos;
19287 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19288 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19289
19290 /* We always start displaying at hpos zero even if hscrolled. */
19291 eassert (it->hpos == 0 && it->current_x == 0);
19292
19293 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19294 >= it->w->desired_matrix->nrows)
19295 {
19296 it->w->nrows_scale_factor++;
19297 fonts_changed_p = 1;
19298 return 0;
19299 }
19300
19301 /* Is IT->w showing the region? */
19302 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19303
19304 /* Clear the result glyph row and enable it. */
19305 prepare_desired_row (row);
19306
19307 row->y = it->current_y;
19308 row->start = it->start;
19309 row->continuation_lines_width = it->continuation_lines_width;
19310 row->displays_text_p = 1;
19311 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19312 it->starts_in_middle_of_char_p = 0;
19313
19314 /* Arrange the overlays nicely for our purposes. Usually, we call
19315 display_line on only one line at a time, in which case this
19316 can't really hurt too much, or we call it on lines which appear
19317 one after another in the buffer, in which case all calls to
19318 recenter_overlay_lists but the first will be pretty cheap. */
19319 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19320
19321 /* Move over display elements that are not visible because we are
19322 hscrolled. This may stop at an x-position < IT->first_visible_x
19323 if the first glyph is partially visible or if we hit a line end. */
19324 if (it->current_x < it->first_visible_x)
19325 {
19326 enum move_it_result move_result;
19327
19328 this_line_min_pos = row->start.pos;
19329 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19330 MOVE_TO_POS | MOVE_TO_X);
19331 /* If we are under a large hscroll, move_it_in_display_line_to
19332 could hit the end of the line without reaching
19333 it->first_visible_x. Pretend that we did reach it. This is
19334 especially important on a TTY, where we will call
19335 extend_face_to_end_of_line, which needs to know how many
19336 blank glyphs to produce. */
19337 if (it->current_x < it->first_visible_x
19338 && (move_result == MOVE_NEWLINE_OR_CR
19339 || move_result == MOVE_POS_MATCH_OR_ZV))
19340 it->current_x = it->first_visible_x;
19341
19342 /* Record the smallest positions seen while we moved over
19343 display elements that are not visible. This is needed by
19344 redisplay_internal for optimizing the case where the cursor
19345 stays inside the same line. The rest of this function only
19346 considers positions that are actually displayed, so
19347 RECORD_MAX_MIN_POS will not otherwise record positions that
19348 are hscrolled to the left of the left edge of the window. */
19349 min_pos = CHARPOS (this_line_min_pos);
19350 min_bpos = BYTEPOS (this_line_min_pos);
19351 }
19352 else
19353 {
19354 /* We only do this when not calling `move_it_in_display_line_to'
19355 above, because move_it_in_display_line_to calls
19356 handle_line_prefix itself. */
19357 handle_line_prefix (it);
19358 }
19359
19360 /* Get the initial row height. This is either the height of the
19361 text hscrolled, if there is any, or zero. */
19362 row->ascent = it->max_ascent;
19363 row->height = it->max_ascent + it->max_descent;
19364 row->phys_ascent = it->max_phys_ascent;
19365 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19366 row->extra_line_spacing = it->max_extra_line_spacing;
19367
19368 /* Utility macro to record max and min buffer positions seen until now. */
19369 #define RECORD_MAX_MIN_POS(IT) \
19370 do \
19371 { \
19372 int composition_p = !STRINGP ((IT)->string) \
19373 && ((IT)->what == IT_COMPOSITION); \
19374 ptrdiff_t current_pos = \
19375 composition_p ? (IT)->cmp_it.charpos \
19376 : IT_CHARPOS (*(IT)); \
19377 ptrdiff_t current_bpos = \
19378 composition_p ? CHAR_TO_BYTE (current_pos) \
19379 : IT_BYTEPOS (*(IT)); \
19380 if (current_pos < min_pos) \
19381 { \
19382 min_pos = current_pos; \
19383 min_bpos = current_bpos; \
19384 } \
19385 if (IT_CHARPOS (*it) > max_pos) \
19386 { \
19387 max_pos = IT_CHARPOS (*it); \
19388 max_bpos = IT_BYTEPOS (*it); \
19389 } \
19390 } \
19391 while (0)
19392
19393 /* Loop generating characters. The loop is left with IT on the next
19394 character to display. */
19395 while (1)
19396 {
19397 int n_glyphs_before, hpos_before, x_before;
19398 int x, nglyphs;
19399 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19400
19401 /* Retrieve the next thing to display. Value is zero if end of
19402 buffer reached. */
19403 if (!get_next_display_element (it))
19404 {
19405 /* Maybe add a space at the end of this line that is used to
19406 display the cursor there under X. Set the charpos of the
19407 first glyph of blank lines not corresponding to any text
19408 to -1. */
19409 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19410 row->exact_window_width_line_p = 1;
19411 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19412 || row->used[TEXT_AREA] == 0)
19413 {
19414 row->glyphs[TEXT_AREA]->charpos = -1;
19415 row->displays_text_p = 0;
19416
19417 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19418 && (!MINI_WINDOW_P (it->w)
19419 || (minibuf_level && EQ (it->window, minibuf_window))))
19420 row->indicate_empty_line_p = 1;
19421 }
19422
19423 it->continuation_lines_width = 0;
19424 row->ends_at_zv_p = 1;
19425 /* A row that displays right-to-left text must always have
19426 its last face extended all the way to the end of line,
19427 even if this row ends in ZV, because we still write to
19428 the screen left to right. We also need to extend the
19429 last face if the default face is remapped to some
19430 different face, otherwise the functions that clear
19431 portions of the screen will clear with the default face's
19432 background color. */
19433 if (row->reversed_p
19434 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19435 extend_face_to_end_of_line (it);
19436 break;
19437 }
19438
19439 /* Now, get the metrics of what we want to display. This also
19440 generates glyphs in `row' (which is IT->glyph_row). */
19441 n_glyphs_before = row->used[TEXT_AREA];
19442 x = it->current_x;
19443
19444 /* Remember the line height so far in case the next element doesn't
19445 fit on the line. */
19446 if (it->line_wrap != TRUNCATE)
19447 {
19448 ascent = it->max_ascent;
19449 descent = it->max_descent;
19450 phys_ascent = it->max_phys_ascent;
19451 phys_descent = it->max_phys_descent;
19452
19453 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19454 {
19455 if (IT_DISPLAYING_WHITESPACE (it))
19456 may_wrap = 1;
19457 else if (may_wrap)
19458 {
19459 SAVE_IT (wrap_it, *it, wrap_data);
19460 wrap_x = x;
19461 wrap_row_used = row->used[TEXT_AREA];
19462 wrap_row_ascent = row->ascent;
19463 wrap_row_height = row->height;
19464 wrap_row_phys_ascent = row->phys_ascent;
19465 wrap_row_phys_height = row->phys_height;
19466 wrap_row_extra_line_spacing = row->extra_line_spacing;
19467 wrap_row_min_pos = min_pos;
19468 wrap_row_min_bpos = min_bpos;
19469 wrap_row_max_pos = max_pos;
19470 wrap_row_max_bpos = max_bpos;
19471 may_wrap = 0;
19472 }
19473 }
19474 }
19475
19476 PRODUCE_GLYPHS (it);
19477
19478 /* If this display element was in marginal areas, continue with
19479 the next one. */
19480 if (it->area != TEXT_AREA)
19481 {
19482 row->ascent = max (row->ascent, it->max_ascent);
19483 row->height = max (row->height, it->max_ascent + it->max_descent);
19484 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19485 row->phys_height = max (row->phys_height,
19486 it->max_phys_ascent + it->max_phys_descent);
19487 row->extra_line_spacing = max (row->extra_line_spacing,
19488 it->max_extra_line_spacing);
19489 set_iterator_to_next (it, 1);
19490 continue;
19491 }
19492
19493 /* Does the display element fit on the line? If we truncate
19494 lines, we should draw past the right edge of the window. If
19495 we don't truncate, we want to stop so that we can display the
19496 continuation glyph before the right margin. If lines are
19497 continued, there are two possible strategies for characters
19498 resulting in more than 1 glyph (e.g. tabs): Display as many
19499 glyphs as possible in this line and leave the rest for the
19500 continuation line, or display the whole element in the next
19501 line. Original redisplay did the former, so we do it also. */
19502 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19503 hpos_before = it->hpos;
19504 x_before = x;
19505
19506 if (/* Not a newline. */
19507 nglyphs > 0
19508 /* Glyphs produced fit entirely in the line. */
19509 && it->current_x < it->last_visible_x)
19510 {
19511 it->hpos += nglyphs;
19512 row->ascent = max (row->ascent, it->max_ascent);
19513 row->height = max (row->height, it->max_ascent + it->max_descent);
19514 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19515 row->phys_height = max (row->phys_height,
19516 it->max_phys_ascent + it->max_phys_descent);
19517 row->extra_line_spacing = max (row->extra_line_spacing,
19518 it->max_extra_line_spacing);
19519 if (it->current_x - it->pixel_width < it->first_visible_x)
19520 row->x = x - it->first_visible_x;
19521 /* Record the maximum and minimum buffer positions seen so
19522 far in glyphs that will be displayed by this row. */
19523 if (it->bidi_p)
19524 RECORD_MAX_MIN_POS (it);
19525 }
19526 else
19527 {
19528 int i, new_x;
19529 struct glyph *glyph;
19530
19531 for (i = 0; i < nglyphs; ++i, x = new_x)
19532 {
19533 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19534 new_x = x + glyph->pixel_width;
19535
19536 if (/* Lines are continued. */
19537 it->line_wrap != TRUNCATE
19538 && (/* Glyph doesn't fit on the line. */
19539 new_x > it->last_visible_x
19540 /* Or it fits exactly on a window system frame. */
19541 || (new_x == it->last_visible_x
19542 && FRAME_WINDOW_P (it->f)
19543 && (row->reversed_p
19544 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19545 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19546 {
19547 /* End of a continued line. */
19548
19549 if (it->hpos == 0
19550 || (new_x == it->last_visible_x
19551 && FRAME_WINDOW_P (it->f)
19552 && (row->reversed_p
19553 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19554 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19555 {
19556 /* Current glyph is the only one on the line or
19557 fits exactly on the line. We must continue
19558 the line because we can't draw the cursor
19559 after the glyph. */
19560 row->continued_p = 1;
19561 it->current_x = new_x;
19562 it->continuation_lines_width += new_x;
19563 ++it->hpos;
19564 if (i == nglyphs - 1)
19565 {
19566 /* If line-wrap is on, check if a previous
19567 wrap point was found. */
19568 if (wrap_row_used > 0
19569 /* Even if there is a previous wrap
19570 point, continue the line here as
19571 usual, if (i) the previous character
19572 was a space or tab AND (ii) the
19573 current character is not. */
19574 && (!may_wrap
19575 || IT_DISPLAYING_WHITESPACE (it)))
19576 goto back_to_wrap;
19577
19578 /* Record the maximum and minimum buffer
19579 positions seen so far in glyphs that will be
19580 displayed by this row. */
19581 if (it->bidi_p)
19582 RECORD_MAX_MIN_POS (it);
19583 set_iterator_to_next (it, 1);
19584 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19585 {
19586 if (!get_next_display_element (it))
19587 {
19588 row->exact_window_width_line_p = 1;
19589 it->continuation_lines_width = 0;
19590 row->continued_p = 0;
19591 row->ends_at_zv_p = 1;
19592 }
19593 else if (ITERATOR_AT_END_OF_LINE_P (it))
19594 {
19595 row->continued_p = 0;
19596 row->exact_window_width_line_p = 1;
19597 }
19598 }
19599 }
19600 else if (it->bidi_p)
19601 RECORD_MAX_MIN_POS (it);
19602 }
19603 else if (CHAR_GLYPH_PADDING_P (*glyph)
19604 && !FRAME_WINDOW_P (it->f))
19605 {
19606 /* A padding glyph that doesn't fit on this line.
19607 This means the whole character doesn't fit
19608 on the line. */
19609 if (row->reversed_p)
19610 unproduce_glyphs (it, row->used[TEXT_AREA]
19611 - n_glyphs_before);
19612 row->used[TEXT_AREA] = n_glyphs_before;
19613
19614 /* Fill the rest of the row with continuation
19615 glyphs like in 20.x. */
19616 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19617 < row->glyphs[1 + TEXT_AREA])
19618 produce_special_glyphs (it, IT_CONTINUATION);
19619
19620 row->continued_p = 1;
19621 it->current_x = x_before;
19622 it->continuation_lines_width += x_before;
19623
19624 /* Restore the height to what it was before the
19625 element not fitting on the line. */
19626 it->max_ascent = ascent;
19627 it->max_descent = descent;
19628 it->max_phys_ascent = phys_ascent;
19629 it->max_phys_descent = phys_descent;
19630 }
19631 else if (wrap_row_used > 0)
19632 {
19633 back_to_wrap:
19634 if (row->reversed_p)
19635 unproduce_glyphs (it,
19636 row->used[TEXT_AREA] - wrap_row_used);
19637 RESTORE_IT (it, &wrap_it, wrap_data);
19638 it->continuation_lines_width += wrap_x;
19639 row->used[TEXT_AREA] = wrap_row_used;
19640 row->ascent = wrap_row_ascent;
19641 row->height = wrap_row_height;
19642 row->phys_ascent = wrap_row_phys_ascent;
19643 row->phys_height = wrap_row_phys_height;
19644 row->extra_line_spacing = wrap_row_extra_line_spacing;
19645 min_pos = wrap_row_min_pos;
19646 min_bpos = wrap_row_min_bpos;
19647 max_pos = wrap_row_max_pos;
19648 max_bpos = wrap_row_max_bpos;
19649 row->continued_p = 1;
19650 row->ends_at_zv_p = 0;
19651 row->exact_window_width_line_p = 0;
19652 it->continuation_lines_width += x;
19653
19654 /* Make sure that a non-default face is extended
19655 up to the right margin of the window. */
19656 extend_face_to_end_of_line (it);
19657 }
19658 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19659 {
19660 /* A TAB that extends past the right edge of the
19661 window. This produces a single glyph on
19662 window system frames. We leave the glyph in
19663 this row and let it fill the row, but don't
19664 consume the TAB. */
19665 if ((row->reversed_p
19666 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19667 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19668 produce_special_glyphs (it, IT_CONTINUATION);
19669 it->continuation_lines_width += it->last_visible_x;
19670 row->ends_in_middle_of_char_p = 1;
19671 row->continued_p = 1;
19672 glyph->pixel_width = it->last_visible_x - x;
19673 it->starts_in_middle_of_char_p = 1;
19674 }
19675 else
19676 {
19677 /* Something other than a TAB that draws past
19678 the right edge of the window. Restore
19679 positions to values before the element. */
19680 if (row->reversed_p)
19681 unproduce_glyphs (it, row->used[TEXT_AREA]
19682 - (n_glyphs_before + i));
19683 row->used[TEXT_AREA] = n_glyphs_before + i;
19684
19685 /* Display continuation glyphs. */
19686 it->current_x = x_before;
19687 it->continuation_lines_width += x;
19688 if (!FRAME_WINDOW_P (it->f)
19689 || (row->reversed_p
19690 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19691 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19692 produce_special_glyphs (it, IT_CONTINUATION);
19693 row->continued_p = 1;
19694
19695 extend_face_to_end_of_line (it);
19696
19697 if (nglyphs > 1 && i > 0)
19698 {
19699 row->ends_in_middle_of_char_p = 1;
19700 it->starts_in_middle_of_char_p = 1;
19701 }
19702
19703 /* Restore the height to what it was before the
19704 element not fitting on the line. */
19705 it->max_ascent = ascent;
19706 it->max_descent = descent;
19707 it->max_phys_ascent = phys_ascent;
19708 it->max_phys_descent = phys_descent;
19709 }
19710
19711 break;
19712 }
19713 else if (new_x > it->first_visible_x)
19714 {
19715 /* Increment number of glyphs actually displayed. */
19716 ++it->hpos;
19717
19718 /* Record the maximum and minimum buffer positions
19719 seen so far in glyphs that will be displayed by
19720 this row. */
19721 if (it->bidi_p)
19722 RECORD_MAX_MIN_POS (it);
19723
19724 if (x < it->first_visible_x)
19725 /* Glyph is partially visible, i.e. row starts at
19726 negative X position. */
19727 row->x = x - it->first_visible_x;
19728 }
19729 else
19730 {
19731 /* Glyph is completely off the left margin of the
19732 window. This should not happen because of the
19733 move_it_in_display_line at the start of this
19734 function, unless the text display area of the
19735 window is empty. */
19736 eassert (it->first_visible_x <= it->last_visible_x);
19737 }
19738 }
19739 /* Even if this display element produced no glyphs at all,
19740 we want to record its position. */
19741 if (it->bidi_p && nglyphs == 0)
19742 RECORD_MAX_MIN_POS (it);
19743
19744 row->ascent = max (row->ascent, it->max_ascent);
19745 row->height = max (row->height, it->max_ascent + it->max_descent);
19746 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19747 row->phys_height = max (row->phys_height,
19748 it->max_phys_ascent + it->max_phys_descent);
19749 row->extra_line_spacing = max (row->extra_line_spacing,
19750 it->max_extra_line_spacing);
19751
19752 /* End of this display line if row is continued. */
19753 if (row->continued_p || row->ends_at_zv_p)
19754 break;
19755 }
19756
19757 at_end_of_line:
19758 /* Is this a line end? If yes, we're also done, after making
19759 sure that a non-default face is extended up to the right
19760 margin of the window. */
19761 if (ITERATOR_AT_END_OF_LINE_P (it))
19762 {
19763 int used_before = row->used[TEXT_AREA];
19764
19765 row->ends_in_newline_from_string_p = STRINGP (it->object);
19766
19767 /* Add a space at the end of the line that is used to
19768 display the cursor there. */
19769 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19770 append_space_for_newline (it, 0);
19771
19772 /* Extend the face to the end of the line. */
19773 extend_face_to_end_of_line (it);
19774
19775 /* Make sure we have the position. */
19776 if (used_before == 0)
19777 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19778
19779 /* Record the position of the newline, for use in
19780 find_row_edges. */
19781 it->eol_pos = it->current.pos;
19782
19783 /* Consume the line end. This skips over invisible lines. */
19784 set_iterator_to_next (it, 1);
19785 it->continuation_lines_width = 0;
19786 break;
19787 }
19788
19789 /* Proceed with next display element. Note that this skips
19790 over lines invisible because of selective display. */
19791 set_iterator_to_next (it, 1);
19792
19793 /* If we truncate lines, we are done when the last displayed
19794 glyphs reach past the right margin of the window. */
19795 if (it->line_wrap == TRUNCATE
19796 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19797 ? (it->current_x >= it->last_visible_x)
19798 : (it->current_x > it->last_visible_x)))
19799 {
19800 /* Maybe add truncation glyphs. */
19801 if (!FRAME_WINDOW_P (it->f)
19802 || (row->reversed_p
19803 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19804 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19805 {
19806 int i, n;
19807
19808 if (!row->reversed_p)
19809 {
19810 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19811 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19812 break;
19813 }
19814 else
19815 {
19816 for (i = 0; i < row->used[TEXT_AREA]; i++)
19817 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19818 break;
19819 /* Remove any padding glyphs at the front of ROW, to
19820 make room for the truncation glyphs we will be
19821 adding below. The loop below always inserts at
19822 least one truncation glyph, so also remove the
19823 last glyph added to ROW. */
19824 unproduce_glyphs (it, i + 1);
19825 /* Adjust i for the loop below. */
19826 i = row->used[TEXT_AREA] - (i + 1);
19827 }
19828
19829 it->current_x = x_before;
19830 if (!FRAME_WINDOW_P (it->f))
19831 {
19832 for (n = row->used[TEXT_AREA]; i < n; ++i)
19833 {
19834 row->used[TEXT_AREA] = i;
19835 produce_special_glyphs (it, IT_TRUNCATION);
19836 }
19837 }
19838 else
19839 {
19840 row->used[TEXT_AREA] = i;
19841 produce_special_glyphs (it, IT_TRUNCATION);
19842 }
19843 }
19844 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19845 {
19846 /* Don't truncate if we can overflow newline into fringe. */
19847 if (!get_next_display_element (it))
19848 {
19849 it->continuation_lines_width = 0;
19850 row->ends_at_zv_p = 1;
19851 row->exact_window_width_line_p = 1;
19852 break;
19853 }
19854 if (ITERATOR_AT_END_OF_LINE_P (it))
19855 {
19856 row->exact_window_width_line_p = 1;
19857 goto at_end_of_line;
19858 }
19859 it->current_x = x_before;
19860 }
19861
19862 row->truncated_on_right_p = 1;
19863 it->continuation_lines_width = 0;
19864 reseat_at_next_visible_line_start (it, 0);
19865 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19866 it->hpos = hpos_before;
19867 break;
19868 }
19869 }
19870
19871 if (wrap_data)
19872 bidi_unshelve_cache (wrap_data, 1);
19873
19874 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19875 at the left window margin. */
19876 if (it->first_visible_x
19877 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19878 {
19879 if (!FRAME_WINDOW_P (it->f)
19880 || (row->reversed_p
19881 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19882 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19883 insert_left_trunc_glyphs (it);
19884 row->truncated_on_left_p = 1;
19885 }
19886
19887 /* Remember the position at which this line ends.
19888
19889 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19890 cannot be before the call to find_row_edges below, since that is
19891 where these positions are determined. */
19892 row->end = it->current;
19893 if (!it->bidi_p)
19894 {
19895 row->minpos = row->start.pos;
19896 row->maxpos = row->end.pos;
19897 }
19898 else
19899 {
19900 /* ROW->minpos and ROW->maxpos must be the smallest and
19901 `1 + the largest' buffer positions in ROW. But if ROW was
19902 bidi-reordered, these two positions can be anywhere in the
19903 row, so we must determine them now. */
19904 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19905 }
19906
19907 /* If the start of this line is the overlay arrow-position, then
19908 mark this glyph row as the one containing the overlay arrow.
19909 This is clearly a mess with variable size fonts. It would be
19910 better to let it be displayed like cursors under X. */
19911 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19912 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19913 !NILP (overlay_arrow_string)))
19914 {
19915 /* Overlay arrow in window redisplay is a fringe bitmap. */
19916 if (STRINGP (overlay_arrow_string))
19917 {
19918 struct glyph_row *arrow_row
19919 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19920 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19921 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19922 struct glyph *p = row->glyphs[TEXT_AREA];
19923 struct glyph *p2, *end;
19924
19925 /* Copy the arrow glyphs. */
19926 while (glyph < arrow_end)
19927 *p++ = *glyph++;
19928
19929 /* Throw away padding glyphs. */
19930 p2 = p;
19931 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19932 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19933 ++p2;
19934 if (p2 > p)
19935 {
19936 while (p2 < end)
19937 *p++ = *p2++;
19938 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19939 }
19940 }
19941 else
19942 {
19943 eassert (INTEGERP (overlay_arrow_string));
19944 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19945 }
19946 overlay_arrow_seen = 1;
19947 }
19948
19949 /* Highlight trailing whitespace. */
19950 if (!NILP (Vshow_trailing_whitespace))
19951 highlight_trailing_whitespace (it->f, it->glyph_row);
19952
19953 /* Compute pixel dimensions of this line. */
19954 compute_line_metrics (it);
19955
19956 /* Implementation note: No changes in the glyphs of ROW or in their
19957 faces can be done past this point, because compute_line_metrics
19958 computes ROW's hash value and stores it within the glyph_row
19959 structure. */
19960
19961 /* Record whether this row ends inside an ellipsis. */
19962 row->ends_in_ellipsis_p
19963 = (it->method == GET_FROM_DISPLAY_VECTOR
19964 && it->ellipsis_p);
19965
19966 /* Save fringe bitmaps in this row. */
19967 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19968 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19969 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19970 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19971
19972 it->left_user_fringe_bitmap = 0;
19973 it->left_user_fringe_face_id = 0;
19974 it->right_user_fringe_bitmap = 0;
19975 it->right_user_fringe_face_id = 0;
19976
19977 /* Maybe set the cursor. */
19978 cvpos = it->w->cursor.vpos;
19979 if ((cvpos < 0
19980 /* In bidi-reordered rows, keep checking for proper cursor
19981 position even if one has been found already, because buffer
19982 positions in such rows change non-linearly with ROW->VPOS,
19983 when a line is continued. One exception: when we are at ZV,
19984 display cursor on the first suitable glyph row, since all
19985 the empty rows after that also have their position set to ZV. */
19986 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19987 lines' rows is implemented for bidi-reordered rows. */
19988 || (it->bidi_p
19989 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19990 && PT >= MATRIX_ROW_START_CHARPOS (row)
19991 && PT <= MATRIX_ROW_END_CHARPOS (row)
19992 && cursor_row_p (row))
19993 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19994
19995 /* Prepare for the next line. This line starts horizontally at (X
19996 HPOS) = (0 0). Vertical positions are incremented. As a
19997 convenience for the caller, IT->glyph_row is set to the next
19998 row to be used. */
19999 it->current_x = it->hpos = 0;
20000 it->current_y += row->height;
20001 SET_TEXT_POS (it->eol_pos, 0, 0);
20002 ++it->vpos;
20003 ++it->glyph_row;
20004 /* The next row should by default use the same value of the
20005 reversed_p flag as this one. set_iterator_to_next decides when
20006 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20007 the flag accordingly. */
20008 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20009 it->glyph_row->reversed_p = row->reversed_p;
20010 it->start = row->end;
20011 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20012
20013 #undef RECORD_MAX_MIN_POS
20014 }
20015
20016 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20017 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20018 doc: /* Return paragraph direction at point in BUFFER.
20019 Value is either `left-to-right' or `right-to-left'.
20020 If BUFFER is omitted or nil, it defaults to the current buffer.
20021
20022 Paragraph direction determines how the text in the paragraph is displayed.
20023 In left-to-right paragraphs, text begins at the left margin of the window
20024 and the reading direction is generally left to right. In right-to-left
20025 paragraphs, text begins at the right margin and is read from right to left.
20026
20027 See also `bidi-paragraph-direction'. */)
20028 (Lisp_Object buffer)
20029 {
20030 struct buffer *buf = current_buffer;
20031 struct buffer *old = buf;
20032
20033 if (! NILP (buffer))
20034 {
20035 CHECK_BUFFER (buffer);
20036 buf = XBUFFER (buffer);
20037 }
20038
20039 if (NILP (BVAR (buf, bidi_display_reordering))
20040 || NILP (BVAR (buf, enable_multibyte_characters))
20041 /* When we are loading loadup.el, the character property tables
20042 needed for bidi iteration are not yet available. */
20043 || !NILP (Vpurify_flag))
20044 return Qleft_to_right;
20045 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20046 return BVAR (buf, bidi_paragraph_direction);
20047 else
20048 {
20049 /* Determine the direction from buffer text. We could try to
20050 use current_matrix if it is up to date, but this seems fast
20051 enough as it is. */
20052 struct bidi_it itb;
20053 ptrdiff_t pos = BUF_PT (buf);
20054 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20055 int c;
20056 void *itb_data = bidi_shelve_cache ();
20057
20058 set_buffer_temp (buf);
20059 /* bidi_paragraph_init finds the base direction of the paragraph
20060 by searching forward from paragraph start. We need the base
20061 direction of the current or _previous_ paragraph, so we need
20062 to make sure we are within that paragraph. To that end, find
20063 the previous non-empty line. */
20064 if (pos >= ZV && pos > BEGV)
20065 DEC_BOTH (pos, bytepos);
20066 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20067 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20068 {
20069 while ((c = FETCH_BYTE (bytepos)) == '\n'
20070 || c == ' ' || c == '\t' || c == '\f')
20071 {
20072 if (bytepos <= BEGV_BYTE)
20073 break;
20074 bytepos--;
20075 pos--;
20076 }
20077 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20078 bytepos--;
20079 }
20080 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20081 itb.paragraph_dir = NEUTRAL_DIR;
20082 itb.string.s = NULL;
20083 itb.string.lstring = Qnil;
20084 itb.string.bufpos = 0;
20085 itb.string.unibyte = 0;
20086 /* We have no window to use here for ignoring window-specific
20087 overlays. Using NULL for window pointer will cause
20088 compute_display_string_pos to use the current buffer. */
20089 itb.w = NULL;
20090 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20091 bidi_unshelve_cache (itb_data, 0);
20092 set_buffer_temp (old);
20093 switch (itb.paragraph_dir)
20094 {
20095 case L2R:
20096 return Qleft_to_right;
20097 break;
20098 case R2L:
20099 return Qright_to_left;
20100 break;
20101 default:
20102 emacs_abort ();
20103 }
20104 }
20105 }
20106
20107 DEFUN ("move-point-visually", Fmove_point_visually,
20108 Smove_point_visually, 1, 1, 0,
20109 doc: /* Move point in the visual order in the specified DIRECTION.
20110 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20111 left.
20112
20113 Value is the new character position of point. */)
20114 (Lisp_Object direction)
20115 {
20116 struct window *w = XWINDOW (selected_window);
20117 struct buffer *b = XBUFFER (w->contents);
20118 struct glyph_row *row;
20119 int dir;
20120 Lisp_Object paragraph_dir;
20121
20122 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20123 (!(ROW)->continued_p \
20124 && INTEGERP ((GLYPH)->object) \
20125 && (GLYPH)->type == CHAR_GLYPH \
20126 && (GLYPH)->u.ch == ' ' \
20127 && (GLYPH)->charpos >= 0 \
20128 && !(GLYPH)->avoid_cursor_p)
20129
20130 CHECK_NUMBER (direction);
20131 dir = XINT (direction);
20132 if (dir > 0)
20133 dir = 1;
20134 else
20135 dir = -1;
20136
20137 /* If current matrix is up-to-date, we can use the information
20138 recorded in the glyphs, at least as long as the goal is on the
20139 screen. */
20140 if (w->window_end_valid
20141 && !windows_or_buffers_changed
20142 && b
20143 && !b->clip_changed
20144 && !b->prevent_redisplay_optimizations_p
20145 && !window_outdated (w)
20146 && w->cursor.vpos >= 0
20147 && w->cursor.vpos < w->current_matrix->nrows
20148 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20149 {
20150 struct glyph *g = row->glyphs[TEXT_AREA];
20151 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20152 struct glyph *gpt = g + w->cursor.hpos;
20153
20154 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20155 {
20156 if (BUFFERP (g->object) && g->charpos != PT)
20157 {
20158 SET_PT (g->charpos);
20159 w->cursor.vpos = -1;
20160 return make_number (PT);
20161 }
20162 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20163 {
20164 ptrdiff_t new_pos;
20165
20166 if (BUFFERP (gpt->object))
20167 {
20168 new_pos = PT;
20169 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20170 new_pos += (row->reversed_p ? -dir : dir);
20171 else
20172 new_pos -= (row->reversed_p ? -dir : dir);;
20173 }
20174 else if (BUFFERP (g->object))
20175 new_pos = g->charpos;
20176 else
20177 break;
20178 SET_PT (new_pos);
20179 w->cursor.vpos = -1;
20180 return make_number (PT);
20181 }
20182 else if (ROW_GLYPH_NEWLINE_P (row, g))
20183 {
20184 /* Glyphs inserted at the end of a non-empty line for
20185 positioning the cursor have zero charpos, so we must
20186 deduce the value of point by other means. */
20187 if (g->charpos > 0)
20188 SET_PT (g->charpos);
20189 else if (row->ends_at_zv_p && PT != ZV)
20190 SET_PT (ZV);
20191 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20192 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20193 else
20194 break;
20195 w->cursor.vpos = -1;
20196 return make_number (PT);
20197 }
20198 }
20199 if (g == e || INTEGERP (g->object))
20200 {
20201 if (row->truncated_on_left_p || row->truncated_on_right_p)
20202 goto simulate_display;
20203 if (!row->reversed_p)
20204 row += dir;
20205 else
20206 row -= dir;
20207 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20208 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20209 goto simulate_display;
20210
20211 if (dir > 0)
20212 {
20213 if (row->reversed_p && !row->continued_p)
20214 {
20215 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20216 w->cursor.vpos = -1;
20217 return make_number (PT);
20218 }
20219 g = row->glyphs[TEXT_AREA];
20220 e = g + row->used[TEXT_AREA];
20221 for ( ; g < e; g++)
20222 {
20223 if (BUFFERP (g->object)
20224 /* Empty lines have only one glyph, which stands
20225 for the newline, and whose charpos is the
20226 buffer position of the newline. */
20227 || ROW_GLYPH_NEWLINE_P (row, g)
20228 /* When the buffer ends in a newline, the line at
20229 EOB also has one glyph, but its charpos is -1. */
20230 || (row->ends_at_zv_p
20231 && !row->reversed_p
20232 && INTEGERP (g->object)
20233 && g->type == CHAR_GLYPH
20234 && g->u.ch == ' '))
20235 {
20236 if (g->charpos > 0)
20237 SET_PT (g->charpos);
20238 else if (!row->reversed_p
20239 && row->ends_at_zv_p
20240 && PT != ZV)
20241 SET_PT (ZV);
20242 else
20243 continue;
20244 w->cursor.vpos = -1;
20245 return make_number (PT);
20246 }
20247 }
20248 }
20249 else
20250 {
20251 if (!row->reversed_p && !row->continued_p)
20252 {
20253 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20254 w->cursor.vpos = -1;
20255 return make_number (PT);
20256 }
20257 e = row->glyphs[TEXT_AREA];
20258 g = e + row->used[TEXT_AREA] - 1;
20259 for ( ; g >= e; g--)
20260 {
20261 if (BUFFERP (g->object)
20262 || (ROW_GLYPH_NEWLINE_P (row, g)
20263 && g->charpos > 0)
20264 /* Empty R2L lines on GUI frames have the buffer
20265 position of the newline stored in the stretch
20266 glyph. */
20267 || g->type == STRETCH_GLYPH
20268 || (row->ends_at_zv_p
20269 && row->reversed_p
20270 && INTEGERP (g->object)
20271 && g->type == CHAR_GLYPH
20272 && g->u.ch == ' '))
20273 {
20274 if (g->charpos > 0)
20275 SET_PT (g->charpos);
20276 else if (row->reversed_p
20277 && row->ends_at_zv_p
20278 && PT != ZV)
20279 SET_PT (ZV);
20280 else
20281 continue;
20282 w->cursor.vpos = -1;
20283 return make_number (PT);
20284 }
20285 }
20286 }
20287 }
20288 }
20289
20290 simulate_display:
20291
20292 /* If we wind up here, we failed to move by using the glyphs, so we
20293 need to simulate display instead. */
20294
20295 if (b)
20296 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20297 else
20298 paragraph_dir = Qleft_to_right;
20299 if (EQ (paragraph_dir, Qright_to_left))
20300 dir = -dir;
20301 if (PT <= BEGV && dir < 0)
20302 xsignal0 (Qbeginning_of_buffer);
20303 else if (PT >= ZV && dir > 0)
20304 xsignal0 (Qend_of_buffer);
20305 else
20306 {
20307 struct text_pos pt;
20308 struct it it;
20309 int pt_x, target_x, pixel_width, pt_vpos;
20310 bool at_eol_p;
20311 bool overshoot_expected = false;
20312 bool target_is_eol_p = false;
20313
20314 /* Setup the arena. */
20315 SET_TEXT_POS (pt, PT, PT_BYTE);
20316 start_display (&it, w, pt);
20317
20318 if (it.cmp_it.id < 0
20319 && it.method == GET_FROM_STRING
20320 && it.area == TEXT_AREA
20321 && it.string_from_display_prop_p
20322 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20323 overshoot_expected = true;
20324
20325 /* Find the X coordinate of point. We start from the beginning
20326 of this or previous line to make sure we are before point in
20327 the logical order (since the move_it_* functions can only
20328 move forward). */
20329 reseat_at_previous_visible_line_start (&it);
20330 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20331 if (IT_CHARPOS (it) != PT)
20332 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20333 -1, -1, -1, MOVE_TO_POS);
20334 pt_x = it.current_x;
20335 pt_vpos = it.vpos;
20336 if (dir > 0 || overshoot_expected)
20337 {
20338 struct glyph_row *row = it.glyph_row;
20339
20340 /* When point is at beginning of line, we don't have
20341 information about the glyph there loaded into struct
20342 it. Calling get_next_display_element fixes that. */
20343 if (pt_x == 0)
20344 get_next_display_element (&it);
20345 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20346 it.glyph_row = NULL;
20347 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20348 it.glyph_row = row;
20349 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20350 it, lest it will become out of sync with it's buffer
20351 position. */
20352 it.current_x = pt_x;
20353 }
20354 else
20355 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20356 pixel_width = it.pixel_width;
20357 if (overshoot_expected && at_eol_p)
20358 pixel_width = 0;
20359 else if (pixel_width <= 0)
20360 pixel_width = 1;
20361
20362 /* If there's a display string at point, we are actually at the
20363 glyph to the left of point, so we need to correct the X
20364 coordinate. */
20365 if (overshoot_expected)
20366 pt_x += pixel_width;
20367
20368 /* Compute target X coordinate, either to the left or to the
20369 right of point. On TTY frames, all characters have the same
20370 pixel width of 1, so we can use that. On GUI frames we don't
20371 have an easy way of getting at the pixel width of the
20372 character to the left of point, so we use a different method
20373 of getting to that place. */
20374 if (dir > 0)
20375 target_x = pt_x + pixel_width;
20376 else
20377 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20378
20379 /* Target X coordinate could be one line above or below the line
20380 of point, in which case we need to adjust the target X
20381 coordinate. Also, if moving to the left, we need to begin at
20382 the left edge of the point's screen line. */
20383 if (dir < 0)
20384 {
20385 if (pt_x > 0)
20386 {
20387 start_display (&it, w, pt);
20388 reseat_at_previous_visible_line_start (&it);
20389 it.current_x = it.current_y = it.hpos = 0;
20390 if (pt_vpos != 0)
20391 move_it_by_lines (&it, pt_vpos);
20392 }
20393 else
20394 {
20395 move_it_by_lines (&it, -1);
20396 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20397 target_is_eol_p = true;
20398 }
20399 }
20400 else
20401 {
20402 if (at_eol_p
20403 || (target_x >= it.last_visible_x
20404 && it.line_wrap != TRUNCATE))
20405 {
20406 if (pt_x > 0)
20407 move_it_by_lines (&it, 0);
20408 move_it_by_lines (&it, 1);
20409 target_x = 0;
20410 }
20411 }
20412
20413 /* Move to the target X coordinate. */
20414 #ifdef HAVE_WINDOW_SYSTEM
20415 /* On GUI frames, as we don't know the X coordinate of the
20416 character to the left of point, moving point to the left
20417 requires walking, one grapheme cluster at a time, until we
20418 find ourself at a place immediately to the left of the
20419 character at point. */
20420 if (FRAME_WINDOW_P (it.f) && dir < 0)
20421 {
20422 struct text_pos new_pos = it.current.pos;
20423 enum move_it_result rc = MOVE_X_REACHED;
20424
20425 while (it.current_x + it.pixel_width <= target_x
20426 && rc == MOVE_X_REACHED)
20427 {
20428 int new_x = it.current_x + it.pixel_width;
20429
20430 new_pos = it.current.pos;
20431 if (new_x == it.current_x)
20432 new_x++;
20433 rc = move_it_in_display_line_to (&it, ZV, new_x,
20434 MOVE_TO_POS | MOVE_TO_X);
20435 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20436 break;
20437 }
20438 /* If we ended up on a composed character inside
20439 bidi-reordered text (e.g., Hebrew text with diacritics),
20440 the iterator gives us the buffer position of the last (in
20441 logical order) character of the composed grapheme cluster,
20442 which is not what we want. So we cheat: we compute the
20443 character position of the character that follows (in the
20444 logical order) the one where the above loop stopped. That
20445 character will appear on display to the left of point. */
20446 if (it.bidi_p
20447 && it.bidi_it.scan_dir == -1
20448 && new_pos.charpos - IT_CHARPOS (it) > 1)
20449 {
20450 new_pos.charpos = IT_CHARPOS (it) + 1;
20451 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20452 }
20453 it.current.pos = new_pos;
20454 }
20455 else
20456 #endif
20457 if (it.current_x != target_x)
20458 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20459
20460 /* When lines are truncated, the above loop will stop at the
20461 window edge. But we want to get to the end of line, even if
20462 it is beyond the window edge; automatic hscroll will then
20463 scroll the window to show point as appropriate. */
20464 if (target_is_eol_p && it.line_wrap == TRUNCATE
20465 && get_next_display_element (&it))
20466 {
20467 struct text_pos new_pos = it.current.pos;
20468
20469 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20470 {
20471 set_iterator_to_next (&it, 0);
20472 if (it.method == GET_FROM_BUFFER)
20473 new_pos = it.current.pos;
20474 if (!get_next_display_element (&it))
20475 break;
20476 }
20477
20478 it.current.pos = new_pos;
20479 }
20480
20481 /* If we ended up in a display string that covers point, move to
20482 buffer position to the right in the visual order. */
20483 if (dir > 0)
20484 {
20485 while (IT_CHARPOS (it) == PT)
20486 {
20487 set_iterator_to_next (&it, 0);
20488 if (!get_next_display_element (&it))
20489 break;
20490 }
20491 }
20492
20493 /* Move point to that position. */
20494 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20495 }
20496
20497 return make_number (PT);
20498
20499 #undef ROW_GLYPH_NEWLINE_P
20500 }
20501
20502 \f
20503 /***********************************************************************
20504 Menu Bar
20505 ***********************************************************************/
20506
20507 /* Redisplay the menu bar in the frame for window W.
20508
20509 The menu bar of X frames that don't have X toolkit support is
20510 displayed in a special window W->frame->menu_bar_window.
20511
20512 The menu bar of terminal frames is treated specially as far as
20513 glyph matrices are concerned. Menu bar lines are not part of
20514 windows, so the update is done directly on the frame matrix rows
20515 for the menu bar. */
20516
20517 static void
20518 display_menu_bar (struct window *w)
20519 {
20520 struct frame *f = XFRAME (WINDOW_FRAME (w));
20521 struct it it;
20522 Lisp_Object items;
20523 int i;
20524
20525 /* Don't do all this for graphical frames. */
20526 #ifdef HAVE_NTGUI
20527 if (FRAME_W32_P (f))
20528 return;
20529 #endif
20530 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20531 if (FRAME_X_P (f))
20532 return;
20533 #endif
20534
20535 #ifdef HAVE_NS
20536 if (FRAME_NS_P (f))
20537 return;
20538 #endif /* HAVE_NS */
20539
20540 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20541 eassert (!FRAME_WINDOW_P (f));
20542 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20543 it.first_visible_x = 0;
20544 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20545 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20546 if (FRAME_WINDOW_P (f))
20547 {
20548 /* Menu bar lines are displayed in the desired matrix of the
20549 dummy window menu_bar_window. */
20550 struct window *menu_w;
20551 menu_w = XWINDOW (f->menu_bar_window);
20552 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20553 MENU_FACE_ID);
20554 it.first_visible_x = 0;
20555 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20556 }
20557 else
20558 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20559 {
20560 /* This is a TTY frame, i.e. character hpos/vpos are used as
20561 pixel x/y. */
20562 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20563 MENU_FACE_ID);
20564 it.first_visible_x = 0;
20565 it.last_visible_x = FRAME_COLS (f);
20566 }
20567
20568 /* FIXME: This should be controlled by a user option. See the
20569 comments in redisplay_tool_bar and display_mode_line about
20570 this. */
20571 it.paragraph_embedding = L2R;
20572
20573 /* Clear all rows of the menu bar. */
20574 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20575 {
20576 struct glyph_row *row = it.glyph_row + i;
20577 clear_glyph_row (row);
20578 row->enabled_p = 1;
20579 row->full_width_p = 1;
20580 }
20581
20582 /* Display all items of the menu bar. */
20583 items = FRAME_MENU_BAR_ITEMS (it.f);
20584 for (i = 0; i < ASIZE (items); i += 4)
20585 {
20586 Lisp_Object string;
20587
20588 /* Stop at nil string. */
20589 string = AREF (items, i + 1);
20590 if (NILP (string))
20591 break;
20592
20593 /* Remember where item was displayed. */
20594 ASET (items, i + 3, make_number (it.hpos));
20595
20596 /* Display the item, pad with one space. */
20597 if (it.current_x < it.last_visible_x)
20598 display_string (NULL, string, Qnil, 0, 0, &it,
20599 SCHARS (string) + 1, 0, 0, -1);
20600 }
20601
20602 /* Fill out the line with spaces. */
20603 if (it.current_x < it.last_visible_x)
20604 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20605
20606 /* Compute the total height of the lines. */
20607 compute_line_metrics (&it);
20608 }
20609
20610
20611 \f
20612 /***********************************************************************
20613 Mode Line
20614 ***********************************************************************/
20615
20616 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20617 FORCE is non-zero, redisplay mode lines unconditionally.
20618 Otherwise, redisplay only mode lines that are garbaged. Value is
20619 the number of windows whose mode lines were redisplayed. */
20620
20621 static int
20622 redisplay_mode_lines (Lisp_Object window, int force)
20623 {
20624 int nwindows = 0;
20625
20626 while (!NILP (window))
20627 {
20628 struct window *w = XWINDOW (window);
20629
20630 if (WINDOWP (w->contents))
20631 nwindows += redisplay_mode_lines (w->contents, force);
20632 else if (force
20633 || FRAME_GARBAGED_P (XFRAME (w->frame))
20634 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20635 {
20636 struct text_pos lpoint;
20637 struct buffer *old = current_buffer;
20638
20639 /* Set the window's buffer for the mode line display. */
20640 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20641 set_buffer_internal_1 (XBUFFER (w->contents));
20642
20643 /* Point refers normally to the selected window. For any
20644 other window, set up appropriate value. */
20645 if (!EQ (window, selected_window))
20646 {
20647 struct text_pos pt;
20648
20649 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20650 if (CHARPOS (pt) < BEGV)
20651 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20652 else if (CHARPOS (pt) > (ZV - 1))
20653 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20654 else
20655 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20656 }
20657
20658 /* Display mode lines. */
20659 clear_glyph_matrix (w->desired_matrix);
20660 if (display_mode_lines (w))
20661 {
20662 ++nwindows;
20663 w->must_be_updated_p = 1;
20664 }
20665
20666 /* Restore old settings. */
20667 set_buffer_internal_1 (old);
20668 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20669 }
20670
20671 window = w->next;
20672 }
20673
20674 return nwindows;
20675 }
20676
20677
20678 /* Display the mode and/or header line of window W. Value is the
20679 sum number of mode lines and header lines displayed. */
20680
20681 static int
20682 display_mode_lines (struct window *w)
20683 {
20684 Lisp_Object old_selected_window = selected_window;
20685 Lisp_Object old_selected_frame = selected_frame;
20686 Lisp_Object new_frame = w->frame;
20687 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20688 int n = 0;
20689
20690 selected_frame = new_frame;
20691 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20692 or window's point, then we'd need select_window_1 here as well. */
20693 XSETWINDOW (selected_window, w);
20694 XFRAME (new_frame)->selected_window = selected_window;
20695
20696 /* These will be set while the mode line specs are processed. */
20697 line_number_displayed = 0;
20698 w->column_number_displayed = -1;
20699
20700 if (WINDOW_WANTS_MODELINE_P (w))
20701 {
20702 struct window *sel_w = XWINDOW (old_selected_window);
20703
20704 /* Select mode line face based on the real selected window. */
20705 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20706 BVAR (current_buffer, mode_line_format));
20707 ++n;
20708 }
20709
20710 if (WINDOW_WANTS_HEADER_LINE_P (w))
20711 {
20712 display_mode_line (w, HEADER_LINE_FACE_ID,
20713 BVAR (current_buffer, header_line_format));
20714 ++n;
20715 }
20716
20717 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20718 selected_frame = old_selected_frame;
20719 selected_window = old_selected_window;
20720 return n;
20721 }
20722
20723
20724 /* Display mode or header line of window W. FACE_ID specifies which
20725 line to display; it is either MODE_LINE_FACE_ID or
20726 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20727 display. Value is the pixel height of the mode/header line
20728 displayed. */
20729
20730 static int
20731 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20732 {
20733 struct it it;
20734 struct face *face;
20735 ptrdiff_t count = SPECPDL_INDEX ();
20736
20737 init_iterator (&it, w, -1, -1, NULL, face_id);
20738 /* Don't extend on a previously drawn mode-line.
20739 This may happen if called from pos_visible_p. */
20740 it.glyph_row->enabled_p = 0;
20741 prepare_desired_row (it.glyph_row);
20742
20743 it.glyph_row->mode_line_p = 1;
20744
20745 /* FIXME: This should be controlled by a user option. But
20746 supporting such an option is not trivial, since the mode line is
20747 made up of many separate strings. */
20748 it.paragraph_embedding = L2R;
20749
20750 record_unwind_protect (unwind_format_mode_line,
20751 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20752
20753 mode_line_target = MODE_LINE_DISPLAY;
20754
20755 /* Temporarily make frame's keyboard the current kboard so that
20756 kboard-local variables in the mode_line_format will get the right
20757 values. */
20758 push_kboard (FRAME_KBOARD (it.f));
20759 record_unwind_save_match_data ();
20760 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20761 pop_kboard ();
20762
20763 unbind_to (count, Qnil);
20764
20765 /* Fill up with spaces. */
20766 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20767
20768 compute_line_metrics (&it);
20769 it.glyph_row->full_width_p = 1;
20770 it.glyph_row->continued_p = 0;
20771 it.glyph_row->truncated_on_left_p = 0;
20772 it.glyph_row->truncated_on_right_p = 0;
20773
20774 /* Make a 3D mode-line have a shadow at its right end. */
20775 face = FACE_FROM_ID (it.f, face_id);
20776 extend_face_to_end_of_line (&it);
20777 if (face->box != FACE_NO_BOX)
20778 {
20779 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20780 + it.glyph_row->used[TEXT_AREA] - 1);
20781 last->right_box_line_p = 1;
20782 }
20783
20784 return it.glyph_row->height;
20785 }
20786
20787 /* Move element ELT in LIST to the front of LIST.
20788 Return the updated list. */
20789
20790 static Lisp_Object
20791 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20792 {
20793 register Lisp_Object tail, prev;
20794 register Lisp_Object tem;
20795
20796 tail = list;
20797 prev = Qnil;
20798 while (CONSP (tail))
20799 {
20800 tem = XCAR (tail);
20801
20802 if (EQ (elt, tem))
20803 {
20804 /* Splice out the link TAIL. */
20805 if (NILP (prev))
20806 list = XCDR (tail);
20807 else
20808 Fsetcdr (prev, XCDR (tail));
20809
20810 /* Now make it the first. */
20811 Fsetcdr (tail, list);
20812 return tail;
20813 }
20814 else
20815 prev = tail;
20816 tail = XCDR (tail);
20817 QUIT;
20818 }
20819
20820 /* Not found--return unchanged LIST. */
20821 return list;
20822 }
20823
20824 /* Contribute ELT to the mode line for window IT->w. How it
20825 translates into text depends on its data type.
20826
20827 IT describes the display environment in which we display, as usual.
20828
20829 DEPTH is the depth in recursion. It is used to prevent
20830 infinite recursion here.
20831
20832 FIELD_WIDTH is the number of characters the display of ELT should
20833 occupy in the mode line, and PRECISION is the maximum number of
20834 characters to display from ELT's representation. See
20835 display_string for details.
20836
20837 Returns the hpos of the end of the text generated by ELT.
20838
20839 PROPS is a property list to add to any string we encounter.
20840
20841 If RISKY is nonzero, remove (disregard) any properties in any string
20842 we encounter, and ignore :eval and :propertize.
20843
20844 The global variable `mode_line_target' determines whether the
20845 output is passed to `store_mode_line_noprop',
20846 `store_mode_line_string', or `display_string'. */
20847
20848 static int
20849 display_mode_element (struct it *it, int depth, int field_width, int precision,
20850 Lisp_Object elt, Lisp_Object props, int risky)
20851 {
20852 int n = 0, field, prec;
20853 int literal = 0;
20854
20855 tail_recurse:
20856 if (depth > 100)
20857 elt = build_string ("*too-deep*");
20858
20859 depth++;
20860
20861 switch (XTYPE (elt))
20862 {
20863 case Lisp_String:
20864 {
20865 /* A string: output it and check for %-constructs within it. */
20866 unsigned char c;
20867 ptrdiff_t offset = 0;
20868
20869 if (SCHARS (elt) > 0
20870 && (!NILP (props) || risky))
20871 {
20872 Lisp_Object oprops, aelt;
20873 oprops = Ftext_properties_at (make_number (0), elt);
20874
20875 /* If the starting string's properties are not what
20876 we want, translate the string. Also, if the string
20877 is risky, do that anyway. */
20878
20879 if (NILP (Fequal (props, oprops)) || risky)
20880 {
20881 /* If the starting string has properties,
20882 merge the specified ones onto the existing ones. */
20883 if (! NILP (oprops) && !risky)
20884 {
20885 Lisp_Object tem;
20886
20887 oprops = Fcopy_sequence (oprops);
20888 tem = props;
20889 while (CONSP (tem))
20890 {
20891 oprops = Fplist_put (oprops, XCAR (tem),
20892 XCAR (XCDR (tem)));
20893 tem = XCDR (XCDR (tem));
20894 }
20895 props = oprops;
20896 }
20897
20898 aelt = Fassoc (elt, mode_line_proptrans_alist);
20899 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20900 {
20901 /* AELT is what we want. Move it to the front
20902 without consing. */
20903 elt = XCAR (aelt);
20904 mode_line_proptrans_alist
20905 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20906 }
20907 else
20908 {
20909 Lisp_Object tem;
20910
20911 /* If AELT has the wrong props, it is useless.
20912 so get rid of it. */
20913 if (! NILP (aelt))
20914 mode_line_proptrans_alist
20915 = Fdelq (aelt, mode_line_proptrans_alist);
20916
20917 elt = Fcopy_sequence (elt);
20918 Fset_text_properties (make_number (0), Flength (elt),
20919 props, elt);
20920 /* Add this item to mode_line_proptrans_alist. */
20921 mode_line_proptrans_alist
20922 = Fcons (Fcons (elt, props),
20923 mode_line_proptrans_alist);
20924 /* Truncate mode_line_proptrans_alist
20925 to at most 50 elements. */
20926 tem = Fnthcdr (make_number (50),
20927 mode_line_proptrans_alist);
20928 if (! NILP (tem))
20929 XSETCDR (tem, Qnil);
20930 }
20931 }
20932 }
20933
20934 offset = 0;
20935
20936 if (literal)
20937 {
20938 prec = precision - n;
20939 switch (mode_line_target)
20940 {
20941 case MODE_LINE_NOPROP:
20942 case MODE_LINE_TITLE:
20943 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20944 break;
20945 case MODE_LINE_STRING:
20946 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20947 break;
20948 case MODE_LINE_DISPLAY:
20949 n += display_string (NULL, elt, Qnil, 0, 0, it,
20950 0, prec, 0, STRING_MULTIBYTE (elt));
20951 break;
20952 }
20953
20954 break;
20955 }
20956
20957 /* Handle the non-literal case. */
20958
20959 while ((precision <= 0 || n < precision)
20960 && SREF (elt, offset) != 0
20961 && (mode_line_target != MODE_LINE_DISPLAY
20962 || it->current_x < it->last_visible_x))
20963 {
20964 ptrdiff_t last_offset = offset;
20965
20966 /* Advance to end of string or next format specifier. */
20967 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20968 ;
20969
20970 if (offset - 1 != last_offset)
20971 {
20972 ptrdiff_t nchars, nbytes;
20973
20974 /* Output to end of string or up to '%'. Field width
20975 is length of string. Don't output more than
20976 PRECISION allows us. */
20977 offset--;
20978
20979 prec = c_string_width (SDATA (elt) + last_offset,
20980 offset - last_offset, precision - n,
20981 &nchars, &nbytes);
20982
20983 switch (mode_line_target)
20984 {
20985 case MODE_LINE_NOPROP:
20986 case MODE_LINE_TITLE:
20987 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20988 break;
20989 case MODE_LINE_STRING:
20990 {
20991 ptrdiff_t bytepos = last_offset;
20992 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20993 ptrdiff_t endpos = (precision <= 0
20994 ? string_byte_to_char (elt, offset)
20995 : charpos + nchars);
20996
20997 n += store_mode_line_string (NULL,
20998 Fsubstring (elt, make_number (charpos),
20999 make_number (endpos)),
21000 0, 0, 0, Qnil);
21001 }
21002 break;
21003 case MODE_LINE_DISPLAY:
21004 {
21005 ptrdiff_t bytepos = last_offset;
21006 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21007
21008 if (precision <= 0)
21009 nchars = string_byte_to_char (elt, offset) - charpos;
21010 n += display_string (NULL, elt, Qnil, 0, charpos,
21011 it, 0, nchars, 0,
21012 STRING_MULTIBYTE (elt));
21013 }
21014 break;
21015 }
21016 }
21017 else /* c == '%' */
21018 {
21019 ptrdiff_t percent_position = offset;
21020
21021 /* Get the specified minimum width. Zero means
21022 don't pad. */
21023 field = 0;
21024 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21025 field = field * 10 + c - '0';
21026
21027 /* Don't pad beyond the total padding allowed. */
21028 if (field_width - n > 0 && field > field_width - n)
21029 field = field_width - n;
21030
21031 /* Note that either PRECISION <= 0 or N < PRECISION. */
21032 prec = precision - n;
21033
21034 if (c == 'M')
21035 n += display_mode_element (it, depth, field, prec,
21036 Vglobal_mode_string, props,
21037 risky);
21038 else if (c != 0)
21039 {
21040 bool multibyte;
21041 ptrdiff_t bytepos, charpos;
21042 const char *spec;
21043 Lisp_Object string;
21044
21045 bytepos = percent_position;
21046 charpos = (STRING_MULTIBYTE (elt)
21047 ? string_byte_to_char (elt, bytepos)
21048 : bytepos);
21049 spec = decode_mode_spec (it->w, c, field, &string);
21050 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21051
21052 switch (mode_line_target)
21053 {
21054 case MODE_LINE_NOPROP:
21055 case MODE_LINE_TITLE:
21056 n += store_mode_line_noprop (spec, field, prec);
21057 break;
21058 case MODE_LINE_STRING:
21059 {
21060 Lisp_Object tem = build_string (spec);
21061 props = Ftext_properties_at (make_number (charpos), elt);
21062 /* Should only keep face property in props */
21063 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21064 }
21065 break;
21066 case MODE_LINE_DISPLAY:
21067 {
21068 int nglyphs_before, nwritten;
21069
21070 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21071 nwritten = display_string (spec, string, elt,
21072 charpos, 0, it,
21073 field, prec, 0,
21074 multibyte);
21075
21076 /* Assign to the glyphs written above the
21077 string where the `%x' came from, position
21078 of the `%'. */
21079 if (nwritten > 0)
21080 {
21081 struct glyph *glyph
21082 = (it->glyph_row->glyphs[TEXT_AREA]
21083 + nglyphs_before);
21084 int i;
21085
21086 for (i = 0; i < nwritten; ++i)
21087 {
21088 glyph[i].object = elt;
21089 glyph[i].charpos = charpos;
21090 }
21091
21092 n += nwritten;
21093 }
21094 }
21095 break;
21096 }
21097 }
21098 else /* c == 0 */
21099 break;
21100 }
21101 }
21102 }
21103 break;
21104
21105 case Lisp_Symbol:
21106 /* A symbol: process the value of the symbol recursively
21107 as if it appeared here directly. Avoid error if symbol void.
21108 Special case: if value of symbol is a string, output the string
21109 literally. */
21110 {
21111 register Lisp_Object tem;
21112
21113 /* If the variable is not marked as risky to set
21114 then its contents are risky to use. */
21115 if (NILP (Fget (elt, Qrisky_local_variable)))
21116 risky = 1;
21117
21118 tem = Fboundp (elt);
21119 if (!NILP (tem))
21120 {
21121 tem = Fsymbol_value (elt);
21122 /* If value is a string, output that string literally:
21123 don't check for % within it. */
21124 if (STRINGP (tem))
21125 literal = 1;
21126
21127 if (!EQ (tem, elt))
21128 {
21129 /* Give up right away for nil or t. */
21130 elt = tem;
21131 goto tail_recurse;
21132 }
21133 }
21134 }
21135 break;
21136
21137 case Lisp_Cons:
21138 {
21139 register Lisp_Object car, tem;
21140
21141 /* A cons cell: five distinct cases.
21142 If first element is :eval or :propertize, do something special.
21143 If first element is a string or a cons, process all the elements
21144 and effectively concatenate them.
21145 If first element is a negative number, truncate displaying cdr to
21146 at most that many characters. If positive, pad (with spaces)
21147 to at least that many characters.
21148 If first element is a symbol, process the cadr or caddr recursively
21149 according to whether the symbol's value is non-nil or nil. */
21150 car = XCAR (elt);
21151 if (EQ (car, QCeval))
21152 {
21153 /* An element of the form (:eval FORM) means evaluate FORM
21154 and use the result as mode line elements. */
21155
21156 if (risky)
21157 break;
21158
21159 if (CONSP (XCDR (elt)))
21160 {
21161 Lisp_Object spec;
21162 spec = safe_eval (XCAR (XCDR (elt)));
21163 n += display_mode_element (it, depth, field_width - n,
21164 precision - n, spec, props,
21165 risky);
21166 }
21167 }
21168 else if (EQ (car, QCpropertize))
21169 {
21170 /* An element of the form (:propertize ELT PROPS...)
21171 means display ELT but applying properties PROPS. */
21172
21173 if (risky)
21174 break;
21175
21176 if (CONSP (XCDR (elt)))
21177 n += display_mode_element (it, depth, field_width - n,
21178 precision - n, XCAR (XCDR (elt)),
21179 XCDR (XCDR (elt)), risky);
21180 }
21181 else if (SYMBOLP (car))
21182 {
21183 tem = Fboundp (car);
21184 elt = XCDR (elt);
21185 if (!CONSP (elt))
21186 goto invalid;
21187 /* elt is now the cdr, and we know it is a cons cell.
21188 Use its car if CAR has a non-nil value. */
21189 if (!NILP (tem))
21190 {
21191 tem = Fsymbol_value (car);
21192 if (!NILP (tem))
21193 {
21194 elt = XCAR (elt);
21195 goto tail_recurse;
21196 }
21197 }
21198 /* Symbol's value is nil (or symbol is unbound)
21199 Get the cddr of the original list
21200 and if possible find the caddr and use that. */
21201 elt = XCDR (elt);
21202 if (NILP (elt))
21203 break;
21204 else if (!CONSP (elt))
21205 goto invalid;
21206 elt = XCAR (elt);
21207 goto tail_recurse;
21208 }
21209 else if (INTEGERP (car))
21210 {
21211 register int lim = XINT (car);
21212 elt = XCDR (elt);
21213 if (lim < 0)
21214 {
21215 /* Negative int means reduce maximum width. */
21216 if (precision <= 0)
21217 precision = -lim;
21218 else
21219 precision = min (precision, -lim);
21220 }
21221 else if (lim > 0)
21222 {
21223 /* Padding specified. Don't let it be more than
21224 current maximum. */
21225 if (precision > 0)
21226 lim = min (precision, lim);
21227
21228 /* If that's more padding than already wanted, queue it.
21229 But don't reduce padding already specified even if
21230 that is beyond the current truncation point. */
21231 field_width = max (lim, field_width);
21232 }
21233 goto tail_recurse;
21234 }
21235 else if (STRINGP (car) || CONSP (car))
21236 {
21237 Lisp_Object halftail = elt;
21238 int len = 0;
21239
21240 while (CONSP (elt)
21241 && (precision <= 0 || n < precision))
21242 {
21243 n += display_mode_element (it, depth,
21244 /* Do padding only after the last
21245 element in the list. */
21246 (! CONSP (XCDR (elt))
21247 ? field_width - n
21248 : 0),
21249 precision - n, XCAR (elt),
21250 props, risky);
21251 elt = XCDR (elt);
21252 len++;
21253 if ((len & 1) == 0)
21254 halftail = XCDR (halftail);
21255 /* Check for cycle. */
21256 if (EQ (halftail, elt))
21257 break;
21258 }
21259 }
21260 }
21261 break;
21262
21263 default:
21264 invalid:
21265 elt = build_string ("*invalid*");
21266 goto tail_recurse;
21267 }
21268
21269 /* Pad to FIELD_WIDTH. */
21270 if (field_width > 0 && n < field_width)
21271 {
21272 switch (mode_line_target)
21273 {
21274 case MODE_LINE_NOPROP:
21275 case MODE_LINE_TITLE:
21276 n += store_mode_line_noprop ("", field_width - n, 0);
21277 break;
21278 case MODE_LINE_STRING:
21279 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21280 break;
21281 case MODE_LINE_DISPLAY:
21282 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21283 0, 0, 0);
21284 break;
21285 }
21286 }
21287
21288 return n;
21289 }
21290
21291 /* Store a mode-line string element in mode_line_string_list.
21292
21293 If STRING is non-null, display that C string. Otherwise, the Lisp
21294 string LISP_STRING is displayed.
21295
21296 FIELD_WIDTH is the minimum number of output glyphs to produce.
21297 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21298 with spaces. FIELD_WIDTH <= 0 means don't pad.
21299
21300 PRECISION is the maximum number of characters to output from
21301 STRING. PRECISION <= 0 means don't truncate the string.
21302
21303 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21304 properties to the string.
21305
21306 PROPS are the properties to add to the string.
21307 The mode_line_string_face face property is always added to the string.
21308 */
21309
21310 static int
21311 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21312 int field_width, int precision, Lisp_Object props)
21313 {
21314 ptrdiff_t len;
21315 int n = 0;
21316
21317 if (string != NULL)
21318 {
21319 len = strlen (string);
21320 if (precision > 0 && len > precision)
21321 len = precision;
21322 lisp_string = make_string (string, len);
21323 if (NILP (props))
21324 props = mode_line_string_face_prop;
21325 else if (!NILP (mode_line_string_face))
21326 {
21327 Lisp_Object face = Fplist_get (props, Qface);
21328 props = Fcopy_sequence (props);
21329 if (NILP (face))
21330 face = mode_line_string_face;
21331 else
21332 face = list2 (face, mode_line_string_face);
21333 props = Fplist_put (props, Qface, face);
21334 }
21335 Fadd_text_properties (make_number (0), make_number (len),
21336 props, lisp_string);
21337 }
21338 else
21339 {
21340 len = XFASTINT (Flength (lisp_string));
21341 if (precision > 0 && len > precision)
21342 {
21343 len = precision;
21344 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21345 precision = -1;
21346 }
21347 if (!NILP (mode_line_string_face))
21348 {
21349 Lisp_Object face;
21350 if (NILP (props))
21351 props = Ftext_properties_at (make_number (0), lisp_string);
21352 face = Fplist_get (props, Qface);
21353 if (NILP (face))
21354 face = mode_line_string_face;
21355 else
21356 face = list2 (face, mode_line_string_face);
21357 props = list2 (Qface, face);
21358 if (copy_string)
21359 lisp_string = Fcopy_sequence (lisp_string);
21360 }
21361 if (!NILP (props))
21362 Fadd_text_properties (make_number (0), make_number (len),
21363 props, lisp_string);
21364 }
21365
21366 if (len > 0)
21367 {
21368 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21369 n += len;
21370 }
21371
21372 if (field_width > len)
21373 {
21374 field_width -= len;
21375 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21376 if (!NILP (props))
21377 Fadd_text_properties (make_number (0), make_number (field_width),
21378 props, lisp_string);
21379 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21380 n += field_width;
21381 }
21382
21383 return n;
21384 }
21385
21386
21387 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21388 1, 4, 0,
21389 doc: /* Format a string out of a mode line format specification.
21390 First arg FORMAT specifies the mode line format (see `mode-line-format'
21391 for details) to use.
21392
21393 By default, the format is evaluated for the currently selected window.
21394
21395 Optional second arg FACE specifies the face property to put on all
21396 characters for which no face is specified. The value nil means the
21397 default face. The value t means whatever face the window's mode line
21398 currently uses (either `mode-line' or `mode-line-inactive',
21399 depending on whether the window is the selected window or not).
21400 An integer value means the value string has no text
21401 properties.
21402
21403 Optional third and fourth args WINDOW and BUFFER specify the window
21404 and buffer to use as the context for the formatting (defaults
21405 are the selected window and the WINDOW's buffer). */)
21406 (Lisp_Object format, Lisp_Object face,
21407 Lisp_Object window, Lisp_Object buffer)
21408 {
21409 struct it it;
21410 int len;
21411 struct window *w;
21412 struct buffer *old_buffer = NULL;
21413 int face_id;
21414 int no_props = INTEGERP (face);
21415 ptrdiff_t count = SPECPDL_INDEX ();
21416 Lisp_Object str;
21417 int string_start = 0;
21418
21419 w = decode_any_window (window);
21420 XSETWINDOW (window, w);
21421
21422 if (NILP (buffer))
21423 buffer = w->contents;
21424 CHECK_BUFFER (buffer);
21425
21426 /* Make formatting the modeline a non-op when noninteractive, otherwise
21427 there will be problems later caused by a partially initialized frame. */
21428 if (NILP (format) || noninteractive)
21429 return empty_unibyte_string;
21430
21431 if (no_props)
21432 face = Qnil;
21433
21434 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21435 : EQ (face, Qt) ? (EQ (window, selected_window)
21436 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21437 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21438 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21439 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21440 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21441 : DEFAULT_FACE_ID;
21442
21443 old_buffer = current_buffer;
21444
21445 /* Save things including mode_line_proptrans_alist,
21446 and set that to nil so that we don't alter the outer value. */
21447 record_unwind_protect (unwind_format_mode_line,
21448 format_mode_line_unwind_data
21449 (XFRAME (WINDOW_FRAME (w)),
21450 old_buffer, selected_window, 1));
21451 mode_line_proptrans_alist = Qnil;
21452
21453 Fselect_window (window, Qt);
21454 set_buffer_internal_1 (XBUFFER (buffer));
21455
21456 init_iterator (&it, w, -1, -1, NULL, face_id);
21457
21458 if (no_props)
21459 {
21460 mode_line_target = MODE_LINE_NOPROP;
21461 mode_line_string_face_prop = Qnil;
21462 mode_line_string_list = Qnil;
21463 string_start = MODE_LINE_NOPROP_LEN (0);
21464 }
21465 else
21466 {
21467 mode_line_target = MODE_LINE_STRING;
21468 mode_line_string_list = Qnil;
21469 mode_line_string_face = face;
21470 mode_line_string_face_prop
21471 = NILP (face) ? Qnil : list2 (Qface, face);
21472 }
21473
21474 push_kboard (FRAME_KBOARD (it.f));
21475 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21476 pop_kboard ();
21477
21478 if (no_props)
21479 {
21480 len = MODE_LINE_NOPROP_LEN (string_start);
21481 str = make_string (mode_line_noprop_buf + string_start, len);
21482 }
21483 else
21484 {
21485 mode_line_string_list = Fnreverse (mode_line_string_list);
21486 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21487 empty_unibyte_string);
21488 }
21489
21490 unbind_to (count, Qnil);
21491 return str;
21492 }
21493
21494 /* Write a null-terminated, right justified decimal representation of
21495 the positive integer D to BUF using a minimal field width WIDTH. */
21496
21497 static void
21498 pint2str (register char *buf, register int width, register ptrdiff_t d)
21499 {
21500 register char *p = buf;
21501
21502 if (d <= 0)
21503 *p++ = '0';
21504 else
21505 {
21506 while (d > 0)
21507 {
21508 *p++ = d % 10 + '0';
21509 d /= 10;
21510 }
21511 }
21512
21513 for (width -= (int) (p - buf); width > 0; --width)
21514 *p++ = ' ';
21515 *p-- = '\0';
21516 while (p > buf)
21517 {
21518 d = *buf;
21519 *buf++ = *p;
21520 *p-- = d;
21521 }
21522 }
21523
21524 /* Write a null-terminated, right justified decimal and "human
21525 readable" representation of the nonnegative integer D to BUF using
21526 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21527
21528 static const char power_letter[] =
21529 {
21530 0, /* no letter */
21531 'k', /* kilo */
21532 'M', /* mega */
21533 'G', /* giga */
21534 'T', /* tera */
21535 'P', /* peta */
21536 'E', /* exa */
21537 'Z', /* zetta */
21538 'Y' /* yotta */
21539 };
21540
21541 static void
21542 pint2hrstr (char *buf, int width, ptrdiff_t d)
21543 {
21544 /* We aim to represent the nonnegative integer D as
21545 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21546 ptrdiff_t quotient = d;
21547 int remainder = 0;
21548 /* -1 means: do not use TENTHS. */
21549 int tenths = -1;
21550 int exponent = 0;
21551
21552 /* Length of QUOTIENT.TENTHS as a string. */
21553 int length;
21554
21555 char * psuffix;
21556 char * p;
21557
21558 if (quotient >= 1000)
21559 {
21560 /* Scale to the appropriate EXPONENT. */
21561 do
21562 {
21563 remainder = quotient % 1000;
21564 quotient /= 1000;
21565 exponent++;
21566 }
21567 while (quotient >= 1000);
21568
21569 /* Round to nearest and decide whether to use TENTHS or not. */
21570 if (quotient <= 9)
21571 {
21572 tenths = remainder / 100;
21573 if (remainder % 100 >= 50)
21574 {
21575 if (tenths < 9)
21576 tenths++;
21577 else
21578 {
21579 quotient++;
21580 if (quotient == 10)
21581 tenths = -1;
21582 else
21583 tenths = 0;
21584 }
21585 }
21586 }
21587 else
21588 if (remainder >= 500)
21589 {
21590 if (quotient < 999)
21591 quotient++;
21592 else
21593 {
21594 quotient = 1;
21595 exponent++;
21596 tenths = 0;
21597 }
21598 }
21599 }
21600
21601 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21602 if (tenths == -1 && quotient <= 99)
21603 if (quotient <= 9)
21604 length = 1;
21605 else
21606 length = 2;
21607 else
21608 length = 3;
21609 p = psuffix = buf + max (width, length);
21610
21611 /* Print EXPONENT. */
21612 *psuffix++ = power_letter[exponent];
21613 *psuffix = '\0';
21614
21615 /* Print TENTHS. */
21616 if (tenths >= 0)
21617 {
21618 *--p = '0' + tenths;
21619 *--p = '.';
21620 }
21621
21622 /* Print QUOTIENT. */
21623 do
21624 {
21625 int digit = quotient % 10;
21626 *--p = '0' + digit;
21627 }
21628 while ((quotient /= 10) != 0);
21629
21630 /* Print leading spaces. */
21631 while (buf < p)
21632 *--p = ' ';
21633 }
21634
21635 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21636 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21637 type of CODING_SYSTEM. Return updated pointer into BUF. */
21638
21639 static unsigned char invalid_eol_type[] = "(*invalid*)";
21640
21641 static char *
21642 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21643 {
21644 Lisp_Object val;
21645 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21646 const unsigned char *eol_str;
21647 int eol_str_len;
21648 /* The EOL conversion we are using. */
21649 Lisp_Object eoltype;
21650
21651 val = CODING_SYSTEM_SPEC (coding_system);
21652 eoltype = Qnil;
21653
21654 if (!VECTORP (val)) /* Not yet decided. */
21655 {
21656 *buf++ = multibyte ? '-' : ' ';
21657 if (eol_flag)
21658 eoltype = eol_mnemonic_undecided;
21659 /* Don't mention EOL conversion if it isn't decided. */
21660 }
21661 else
21662 {
21663 Lisp_Object attrs;
21664 Lisp_Object eolvalue;
21665
21666 attrs = AREF (val, 0);
21667 eolvalue = AREF (val, 2);
21668
21669 *buf++ = multibyte
21670 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21671 : ' ';
21672
21673 if (eol_flag)
21674 {
21675 /* The EOL conversion that is normal on this system. */
21676
21677 if (NILP (eolvalue)) /* Not yet decided. */
21678 eoltype = eol_mnemonic_undecided;
21679 else if (VECTORP (eolvalue)) /* Not yet decided. */
21680 eoltype = eol_mnemonic_undecided;
21681 else /* eolvalue is Qunix, Qdos, or Qmac. */
21682 eoltype = (EQ (eolvalue, Qunix)
21683 ? eol_mnemonic_unix
21684 : (EQ (eolvalue, Qdos) == 1
21685 ? eol_mnemonic_dos : eol_mnemonic_mac));
21686 }
21687 }
21688
21689 if (eol_flag)
21690 {
21691 /* Mention the EOL conversion if it is not the usual one. */
21692 if (STRINGP (eoltype))
21693 {
21694 eol_str = SDATA (eoltype);
21695 eol_str_len = SBYTES (eoltype);
21696 }
21697 else if (CHARACTERP (eoltype))
21698 {
21699 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21700 int c = XFASTINT (eoltype);
21701 eol_str_len = CHAR_STRING (c, tmp);
21702 eol_str = tmp;
21703 }
21704 else
21705 {
21706 eol_str = invalid_eol_type;
21707 eol_str_len = sizeof (invalid_eol_type) - 1;
21708 }
21709 memcpy (buf, eol_str, eol_str_len);
21710 buf += eol_str_len;
21711 }
21712
21713 return buf;
21714 }
21715
21716 /* Return a string for the output of a mode line %-spec for window W,
21717 generated by character C. FIELD_WIDTH > 0 means pad the string
21718 returned with spaces to that value. Return a Lisp string in
21719 *STRING if the resulting string is taken from that Lisp string.
21720
21721 Note we operate on the current buffer for most purposes. */
21722
21723 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21724
21725 static const char *
21726 decode_mode_spec (struct window *w, register int c, int field_width,
21727 Lisp_Object *string)
21728 {
21729 Lisp_Object obj;
21730 struct frame *f = XFRAME (WINDOW_FRAME (w));
21731 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21732 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21733 produce strings from numerical values, so limit preposterously
21734 large values of FIELD_WIDTH to avoid overrunning the buffer's
21735 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21736 bytes plus the terminating null. */
21737 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21738 struct buffer *b = current_buffer;
21739
21740 obj = Qnil;
21741 *string = Qnil;
21742
21743 switch (c)
21744 {
21745 case '*':
21746 if (!NILP (BVAR (b, read_only)))
21747 return "%";
21748 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21749 return "*";
21750 return "-";
21751
21752 case '+':
21753 /* This differs from %* only for a modified read-only buffer. */
21754 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21755 return "*";
21756 if (!NILP (BVAR (b, read_only)))
21757 return "%";
21758 return "-";
21759
21760 case '&':
21761 /* This differs from %* in ignoring read-only-ness. */
21762 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21763 return "*";
21764 return "-";
21765
21766 case '%':
21767 return "%";
21768
21769 case '[':
21770 {
21771 int i;
21772 char *p;
21773
21774 if (command_loop_level > 5)
21775 return "[[[... ";
21776 p = decode_mode_spec_buf;
21777 for (i = 0; i < command_loop_level; i++)
21778 *p++ = '[';
21779 *p = 0;
21780 return decode_mode_spec_buf;
21781 }
21782
21783 case ']':
21784 {
21785 int i;
21786 char *p;
21787
21788 if (command_loop_level > 5)
21789 return " ...]]]";
21790 p = decode_mode_spec_buf;
21791 for (i = 0; i < command_loop_level; i++)
21792 *p++ = ']';
21793 *p = 0;
21794 return decode_mode_spec_buf;
21795 }
21796
21797 case '-':
21798 {
21799 register int i;
21800
21801 /* Let lots_of_dashes be a string of infinite length. */
21802 if (mode_line_target == MODE_LINE_NOPROP
21803 || mode_line_target == MODE_LINE_STRING)
21804 return "--";
21805 if (field_width <= 0
21806 || field_width > sizeof (lots_of_dashes))
21807 {
21808 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21809 decode_mode_spec_buf[i] = '-';
21810 decode_mode_spec_buf[i] = '\0';
21811 return decode_mode_spec_buf;
21812 }
21813 else
21814 return lots_of_dashes;
21815 }
21816
21817 case 'b':
21818 obj = BVAR (b, name);
21819 break;
21820
21821 case 'c':
21822 /* %c and %l are ignored in `frame-title-format'.
21823 (In redisplay_internal, the frame title is drawn _before_ the
21824 windows are updated, so the stuff which depends on actual
21825 window contents (such as %l) may fail to render properly, or
21826 even crash emacs.) */
21827 if (mode_line_target == MODE_LINE_TITLE)
21828 return "";
21829 else
21830 {
21831 ptrdiff_t col = current_column ();
21832 w->column_number_displayed = col;
21833 pint2str (decode_mode_spec_buf, width, col);
21834 return decode_mode_spec_buf;
21835 }
21836
21837 case 'e':
21838 #ifndef SYSTEM_MALLOC
21839 {
21840 if (NILP (Vmemory_full))
21841 return "";
21842 else
21843 return "!MEM FULL! ";
21844 }
21845 #else
21846 return "";
21847 #endif
21848
21849 case 'F':
21850 /* %F displays the frame name. */
21851 if (!NILP (f->title))
21852 return SSDATA (f->title);
21853 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21854 return SSDATA (f->name);
21855 return "Emacs";
21856
21857 case 'f':
21858 obj = BVAR (b, filename);
21859 break;
21860
21861 case 'i':
21862 {
21863 ptrdiff_t size = ZV - BEGV;
21864 pint2str (decode_mode_spec_buf, width, size);
21865 return decode_mode_spec_buf;
21866 }
21867
21868 case 'I':
21869 {
21870 ptrdiff_t size = ZV - BEGV;
21871 pint2hrstr (decode_mode_spec_buf, width, size);
21872 return decode_mode_spec_buf;
21873 }
21874
21875 case 'l':
21876 {
21877 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21878 ptrdiff_t topline, nlines, height;
21879 ptrdiff_t junk;
21880
21881 /* %c and %l are ignored in `frame-title-format'. */
21882 if (mode_line_target == MODE_LINE_TITLE)
21883 return "";
21884
21885 startpos = marker_position (w->start);
21886 startpos_byte = marker_byte_position (w->start);
21887 height = WINDOW_TOTAL_LINES (w);
21888
21889 /* If we decided that this buffer isn't suitable for line numbers,
21890 don't forget that too fast. */
21891 if (w->base_line_pos == -1)
21892 goto no_value;
21893
21894 /* If the buffer is very big, don't waste time. */
21895 if (INTEGERP (Vline_number_display_limit)
21896 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21897 {
21898 w->base_line_pos = 0;
21899 w->base_line_number = 0;
21900 goto no_value;
21901 }
21902
21903 if (w->base_line_number > 0
21904 && w->base_line_pos > 0
21905 && w->base_line_pos <= startpos)
21906 {
21907 line = w->base_line_number;
21908 linepos = w->base_line_pos;
21909 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21910 }
21911 else
21912 {
21913 line = 1;
21914 linepos = BUF_BEGV (b);
21915 linepos_byte = BUF_BEGV_BYTE (b);
21916 }
21917
21918 /* Count lines from base line to window start position. */
21919 nlines = display_count_lines (linepos_byte,
21920 startpos_byte,
21921 startpos, &junk);
21922
21923 topline = nlines + line;
21924
21925 /* Determine a new base line, if the old one is too close
21926 or too far away, or if we did not have one.
21927 "Too close" means it's plausible a scroll-down would
21928 go back past it. */
21929 if (startpos == BUF_BEGV (b))
21930 {
21931 w->base_line_number = topline;
21932 w->base_line_pos = BUF_BEGV (b);
21933 }
21934 else if (nlines < height + 25 || nlines > height * 3 + 50
21935 || linepos == BUF_BEGV (b))
21936 {
21937 ptrdiff_t limit = BUF_BEGV (b);
21938 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21939 ptrdiff_t position;
21940 ptrdiff_t distance =
21941 (height * 2 + 30) * line_number_display_limit_width;
21942
21943 if (startpos - distance > limit)
21944 {
21945 limit = startpos - distance;
21946 limit_byte = CHAR_TO_BYTE (limit);
21947 }
21948
21949 nlines = display_count_lines (startpos_byte,
21950 limit_byte,
21951 - (height * 2 + 30),
21952 &position);
21953 /* If we couldn't find the lines we wanted within
21954 line_number_display_limit_width chars per line,
21955 give up on line numbers for this window. */
21956 if (position == limit_byte && limit == startpos - distance)
21957 {
21958 w->base_line_pos = -1;
21959 w->base_line_number = 0;
21960 goto no_value;
21961 }
21962
21963 w->base_line_number = topline - nlines;
21964 w->base_line_pos = BYTE_TO_CHAR (position);
21965 }
21966
21967 /* Now count lines from the start pos to point. */
21968 nlines = display_count_lines (startpos_byte,
21969 PT_BYTE, PT, &junk);
21970
21971 /* Record that we did display the line number. */
21972 line_number_displayed = 1;
21973
21974 /* Make the string to show. */
21975 pint2str (decode_mode_spec_buf, width, topline + nlines);
21976 return decode_mode_spec_buf;
21977 no_value:
21978 {
21979 char* p = decode_mode_spec_buf;
21980 int pad = width - 2;
21981 while (pad-- > 0)
21982 *p++ = ' ';
21983 *p++ = '?';
21984 *p++ = '?';
21985 *p = '\0';
21986 return decode_mode_spec_buf;
21987 }
21988 }
21989 break;
21990
21991 case 'm':
21992 obj = BVAR (b, mode_name);
21993 break;
21994
21995 case 'n':
21996 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21997 return " Narrow";
21998 break;
21999
22000 case 'p':
22001 {
22002 ptrdiff_t pos = marker_position (w->start);
22003 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22004
22005 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
22006 {
22007 if (pos <= BUF_BEGV (b))
22008 return "All";
22009 else
22010 return "Bottom";
22011 }
22012 else if (pos <= BUF_BEGV (b))
22013 return "Top";
22014 else
22015 {
22016 if (total > 1000000)
22017 /* Do it differently for a large value, to avoid overflow. */
22018 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22019 else
22020 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22021 /* We can't normally display a 3-digit number,
22022 so get us a 2-digit number that is close. */
22023 if (total == 100)
22024 total = 99;
22025 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22026 return decode_mode_spec_buf;
22027 }
22028 }
22029
22030 /* Display percentage of size above the bottom of the screen. */
22031 case 'P':
22032 {
22033 ptrdiff_t toppos = marker_position (w->start);
22034 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
22035 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22036
22037 if (botpos >= BUF_ZV (b))
22038 {
22039 if (toppos <= BUF_BEGV (b))
22040 return "All";
22041 else
22042 return "Bottom";
22043 }
22044 else
22045 {
22046 if (total > 1000000)
22047 /* Do it differently for a large value, to avoid overflow. */
22048 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22049 else
22050 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22051 /* We can't normally display a 3-digit number,
22052 so get us a 2-digit number that is close. */
22053 if (total == 100)
22054 total = 99;
22055 if (toppos <= BUF_BEGV (b))
22056 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22057 else
22058 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22059 return decode_mode_spec_buf;
22060 }
22061 }
22062
22063 case 's':
22064 /* status of process */
22065 obj = Fget_buffer_process (Fcurrent_buffer ());
22066 if (NILP (obj))
22067 return "no process";
22068 #ifndef MSDOS
22069 obj = Fsymbol_name (Fprocess_status (obj));
22070 #endif
22071 break;
22072
22073 case '@':
22074 {
22075 ptrdiff_t count = inhibit_garbage_collection ();
22076 Lisp_Object val = call1 (intern ("file-remote-p"),
22077 BVAR (current_buffer, directory));
22078 unbind_to (count, Qnil);
22079
22080 if (NILP (val))
22081 return "-";
22082 else
22083 return "@";
22084 }
22085
22086 case 'z':
22087 /* coding-system (not including end-of-line format) */
22088 case 'Z':
22089 /* coding-system (including end-of-line type) */
22090 {
22091 int eol_flag = (c == 'Z');
22092 char *p = decode_mode_spec_buf;
22093
22094 if (! FRAME_WINDOW_P (f))
22095 {
22096 /* No need to mention EOL here--the terminal never needs
22097 to do EOL conversion. */
22098 p = decode_mode_spec_coding (CODING_ID_NAME
22099 (FRAME_KEYBOARD_CODING (f)->id),
22100 p, 0);
22101 p = decode_mode_spec_coding (CODING_ID_NAME
22102 (FRAME_TERMINAL_CODING (f)->id),
22103 p, 0);
22104 }
22105 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22106 p, eol_flag);
22107
22108 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22109 #ifdef subprocesses
22110 obj = Fget_buffer_process (Fcurrent_buffer ());
22111 if (PROCESSP (obj))
22112 {
22113 p = decode_mode_spec_coding
22114 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22115 p = decode_mode_spec_coding
22116 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22117 }
22118 #endif /* subprocesses */
22119 #endif /* 0 */
22120 *p = 0;
22121 return decode_mode_spec_buf;
22122 }
22123 }
22124
22125 if (STRINGP (obj))
22126 {
22127 *string = obj;
22128 return SSDATA (obj);
22129 }
22130 else
22131 return "";
22132 }
22133
22134
22135 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22136 means count lines back from START_BYTE. But don't go beyond
22137 LIMIT_BYTE. Return the number of lines thus found (always
22138 nonnegative).
22139
22140 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22141 either the position COUNT lines after/before START_BYTE, if we
22142 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22143 COUNT lines. */
22144
22145 static ptrdiff_t
22146 display_count_lines (ptrdiff_t start_byte,
22147 ptrdiff_t limit_byte, ptrdiff_t count,
22148 ptrdiff_t *byte_pos_ptr)
22149 {
22150 register unsigned char *cursor;
22151 unsigned char *base;
22152
22153 register ptrdiff_t ceiling;
22154 register unsigned char *ceiling_addr;
22155 ptrdiff_t orig_count = count;
22156
22157 /* If we are not in selective display mode,
22158 check only for newlines. */
22159 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22160 && !INTEGERP (BVAR (current_buffer, selective_display)));
22161
22162 if (count > 0)
22163 {
22164 while (start_byte < limit_byte)
22165 {
22166 ceiling = BUFFER_CEILING_OF (start_byte);
22167 ceiling = min (limit_byte - 1, ceiling);
22168 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22169 base = (cursor = BYTE_POS_ADDR (start_byte));
22170
22171 do
22172 {
22173 if (selective_display)
22174 {
22175 while (*cursor != '\n' && *cursor != 015
22176 && ++cursor != ceiling_addr)
22177 continue;
22178 if (cursor == ceiling_addr)
22179 break;
22180 }
22181 else
22182 {
22183 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22184 if (! cursor)
22185 break;
22186 }
22187
22188 cursor++;
22189
22190 if (--count == 0)
22191 {
22192 start_byte += cursor - base;
22193 *byte_pos_ptr = start_byte;
22194 return orig_count;
22195 }
22196 }
22197 while (cursor < ceiling_addr);
22198
22199 start_byte += ceiling_addr - base;
22200 }
22201 }
22202 else
22203 {
22204 while (start_byte > limit_byte)
22205 {
22206 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22207 ceiling = max (limit_byte, ceiling);
22208 ceiling_addr = BYTE_POS_ADDR (ceiling);
22209 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22210 while (1)
22211 {
22212 if (selective_display)
22213 {
22214 while (--cursor >= ceiling_addr
22215 && *cursor != '\n' && *cursor != 015)
22216 continue;
22217 if (cursor < ceiling_addr)
22218 break;
22219 }
22220 else
22221 {
22222 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22223 if (! cursor)
22224 break;
22225 }
22226
22227 if (++count == 0)
22228 {
22229 start_byte += cursor - base + 1;
22230 *byte_pos_ptr = start_byte;
22231 /* When scanning backwards, we should
22232 not count the newline posterior to which we stop. */
22233 return - orig_count - 1;
22234 }
22235 }
22236 start_byte += ceiling_addr - base;
22237 }
22238 }
22239
22240 *byte_pos_ptr = limit_byte;
22241
22242 if (count < 0)
22243 return - orig_count + count;
22244 return orig_count - count;
22245
22246 }
22247
22248
22249 \f
22250 /***********************************************************************
22251 Displaying strings
22252 ***********************************************************************/
22253
22254 /* Display a NUL-terminated string, starting with index START.
22255
22256 If STRING is non-null, display that C string. Otherwise, the Lisp
22257 string LISP_STRING is displayed. There's a case that STRING is
22258 non-null and LISP_STRING is not nil. It means STRING is a string
22259 data of LISP_STRING. In that case, we display LISP_STRING while
22260 ignoring its text properties.
22261
22262 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22263 FACE_STRING. Display STRING or LISP_STRING with the face at
22264 FACE_STRING_POS in FACE_STRING:
22265
22266 Display the string in the environment given by IT, but use the
22267 standard display table, temporarily.
22268
22269 FIELD_WIDTH is the minimum number of output glyphs to produce.
22270 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22271 with spaces. If STRING has more characters, more than FIELD_WIDTH
22272 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22273
22274 PRECISION is the maximum number of characters to output from
22275 STRING. PRECISION < 0 means don't truncate the string.
22276
22277 This is roughly equivalent to printf format specifiers:
22278
22279 FIELD_WIDTH PRECISION PRINTF
22280 ----------------------------------------
22281 -1 -1 %s
22282 -1 10 %.10s
22283 10 -1 %10s
22284 20 10 %20.10s
22285
22286 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22287 display them, and < 0 means obey the current buffer's value of
22288 enable_multibyte_characters.
22289
22290 Value is the number of columns displayed. */
22291
22292 static int
22293 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22294 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22295 int field_width, int precision, int max_x, int multibyte)
22296 {
22297 int hpos_at_start = it->hpos;
22298 int saved_face_id = it->face_id;
22299 struct glyph_row *row = it->glyph_row;
22300 ptrdiff_t it_charpos;
22301
22302 /* Initialize the iterator IT for iteration over STRING beginning
22303 with index START. */
22304 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22305 precision, field_width, multibyte);
22306 if (string && STRINGP (lisp_string))
22307 /* LISP_STRING is the one returned by decode_mode_spec. We should
22308 ignore its text properties. */
22309 it->stop_charpos = it->end_charpos;
22310
22311 /* If displaying STRING, set up the face of the iterator from
22312 FACE_STRING, if that's given. */
22313 if (STRINGP (face_string))
22314 {
22315 ptrdiff_t endptr;
22316 struct face *face;
22317
22318 it->face_id
22319 = face_at_string_position (it->w, face_string, face_string_pos,
22320 0, it->region_beg_charpos,
22321 it->region_end_charpos,
22322 &endptr, it->base_face_id, 0);
22323 face = FACE_FROM_ID (it->f, it->face_id);
22324 it->face_box_p = face->box != FACE_NO_BOX;
22325 }
22326
22327 /* Set max_x to the maximum allowed X position. Don't let it go
22328 beyond the right edge of the window. */
22329 if (max_x <= 0)
22330 max_x = it->last_visible_x;
22331 else
22332 max_x = min (max_x, it->last_visible_x);
22333
22334 /* Skip over display elements that are not visible. because IT->w is
22335 hscrolled. */
22336 if (it->current_x < it->first_visible_x)
22337 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22338 MOVE_TO_POS | MOVE_TO_X);
22339
22340 row->ascent = it->max_ascent;
22341 row->height = it->max_ascent + it->max_descent;
22342 row->phys_ascent = it->max_phys_ascent;
22343 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22344 row->extra_line_spacing = it->max_extra_line_spacing;
22345
22346 if (STRINGP (it->string))
22347 it_charpos = IT_STRING_CHARPOS (*it);
22348 else
22349 it_charpos = IT_CHARPOS (*it);
22350
22351 /* This condition is for the case that we are called with current_x
22352 past last_visible_x. */
22353 while (it->current_x < max_x)
22354 {
22355 int x_before, x, n_glyphs_before, i, nglyphs;
22356
22357 /* Get the next display element. */
22358 if (!get_next_display_element (it))
22359 break;
22360
22361 /* Produce glyphs. */
22362 x_before = it->current_x;
22363 n_glyphs_before = row->used[TEXT_AREA];
22364 PRODUCE_GLYPHS (it);
22365
22366 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22367 i = 0;
22368 x = x_before;
22369 while (i < nglyphs)
22370 {
22371 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22372
22373 if (it->line_wrap != TRUNCATE
22374 && x + glyph->pixel_width > max_x)
22375 {
22376 /* End of continued line or max_x reached. */
22377 if (CHAR_GLYPH_PADDING_P (*glyph))
22378 {
22379 /* A wide character is unbreakable. */
22380 if (row->reversed_p)
22381 unproduce_glyphs (it, row->used[TEXT_AREA]
22382 - n_glyphs_before);
22383 row->used[TEXT_AREA] = n_glyphs_before;
22384 it->current_x = x_before;
22385 }
22386 else
22387 {
22388 if (row->reversed_p)
22389 unproduce_glyphs (it, row->used[TEXT_AREA]
22390 - (n_glyphs_before + i));
22391 row->used[TEXT_AREA] = n_glyphs_before + i;
22392 it->current_x = x;
22393 }
22394 break;
22395 }
22396 else if (x + glyph->pixel_width >= it->first_visible_x)
22397 {
22398 /* Glyph is at least partially visible. */
22399 ++it->hpos;
22400 if (x < it->first_visible_x)
22401 row->x = x - it->first_visible_x;
22402 }
22403 else
22404 {
22405 /* Glyph is off the left margin of the display area.
22406 Should not happen. */
22407 emacs_abort ();
22408 }
22409
22410 row->ascent = max (row->ascent, it->max_ascent);
22411 row->height = max (row->height, it->max_ascent + it->max_descent);
22412 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22413 row->phys_height = max (row->phys_height,
22414 it->max_phys_ascent + it->max_phys_descent);
22415 row->extra_line_spacing = max (row->extra_line_spacing,
22416 it->max_extra_line_spacing);
22417 x += glyph->pixel_width;
22418 ++i;
22419 }
22420
22421 /* Stop if max_x reached. */
22422 if (i < nglyphs)
22423 break;
22424
22425 /* Stop at line ends. */
22426 if (ITERATOR_AT_END_OF_LINE_P (it))
22427 {
22428 it->continuation_lines_width = 0;
22429 break;
22430 }
22431
22432 set_iterator_to_next (it, 1);
22433 if (STRINGP (it->string))
22434 it_charpos = IT_STRING_CHARPOS (*it);
22435 else
22436 it_charpos = IT_CHARPOS (*it);
22437
22438 /* Stop if truncating at the right edge. */
22439 if (it->line_wrap == TRUNCATE
22440 && it->current_x >= it->last_visible_x)
22441 {
22442 /* Add truncation mark, but don't do it if the line is
22443 truncated at a padding space. */
22444 if (it_charpos < it->string_nchars)
22445 {
22446 if (!FRAME_WINDOW_P (it->f))
22447 {
22448 int ii, n;
22449
22450 if (it->current_x > it->last_visible_x)
22451 {
22452 if (!row->reversed_p)
22453 {
22454 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22455 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22456 break;
22457 }
22458 else
22459 {
22460 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22461 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22462 break;
22463 unproduce_glyphs (it, ii + 1);
22464 ii = row->used[TEXT_AREA] - (ii + 1);
22465 }
22466 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22467 {
22468 row->used[TEXT_AREA] = ii;
22469 produce_special_glyphs (it, IT_TRUNCATION);
22470 }
22471 }
22472 produce_special_glyphs (it, IT_TRUNCATION);
22473 }
22474 row->truncated_on_right_p = 1;
22475 }
22476 break;
22477 }
22478 }
22479
22480 /* Maybe insert a truncation at the left. */
22481 if (it->first_visible_x
22482 && it_charpos > 0)
22483 {
22484 if (!FRAME_WINDOW_P (it->f)
22485 || (row->reversed_p
22486 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22487 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22488 insert_left_trunc_glyphs (it);
22489 row->truncated_on_left_p = 1;
22490 }
22491
22492 it->face_id = saved_face_id;
22493
22494 /* Value is number of columns displayed. */
22495 return it->hpos - hpos_at_start;
22496 }
22497
22498
22499 \f
22500 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22501 appears as an element of LIST or as the car of an element of LIST.
22502 If PROPVAL is a list, compare each element against LIST in that
22503 way, and return 1/2 if any element of PROPVAL is found in LIST.
22504 Otherwise return 0. This function cannot quit.
22505 The return value is 2 if the text is invisible but with an ellipsis
22506 and 1 if it's invisible and without an ellipsis. */
22507
22508 int
22509 invisible_p (register Lisp_Object propval, Lisp_Object list)
22510 {
22511 register Lisp_Object tail, proptail;
22512
22513 for (tail = list; CONSP (tail); tail = XCDR (tail))
22514 {
22515 register Lisp_Object tem;
22516 tem = XCAR (tail);
22517 if (EQ (propval, tem))
22518 return 1;
22519 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22520 return NILP (XCDR (tem)) ? 1 : 2;
22521 }
22522
22523 if (CONSP (propval))
22524 {
22525 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22526 {
22527 Lisp_Object propelt;
22528 propelt = XCAR (proptail);
22529 for (tail = list; CONSP (tail); tail = XCDR (tail))
22530 {
22531 register Lisp_Object tem;
22532 tem = XCAR (tail);
22533 if (EQ (propelt, tem))
22534 return 1;
22535 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22536 return NILP (XCDR (tem)) ? 1 : 2;
22537 }
22538 }
22539 }
22540
22541 return 0;
22542 }
22543
22544 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22545 doc: /* Non-nil if the property makes the text invisible.
22546 POS-OR-PROP can be a marker or number, in which case it is taken to be
22547 a position in the current buffer and the value of the `invisible' property
22548 is checked; or it can be some other value, which is then presumed to be the
22549 value of the `invisible' property of the text of interest.
22550 The non-nil value returned can be t for truly invisible text or something
22551 else if the text is replaced by an ellipsis. */)
22552 (Lisp_Object pos_or_prop)
22553 {
22554 Lisp_Object prop
22555 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22556 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22557 : pos_or_prop);
22558 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22559 return (invis == 0 ? Qnil
22560 : invis == 1 ? Qt
22561 : make_number (invis));
22562 }
22563
22564 /* Calculate a width or height in pixels from a specification using
22565 the following elements:
22566
22567 SPEC ::=
22568 NUM - a (fractional) multiple of the default font width/height
22569 (NUM) - specifies exactly NUM pixels
22570 UNIT - a fixed number of pixels, see below.
22571 ELEMENT - size of a display element in pixels, see below.
22572 (NUM . SPEC) - equals NUM * SPEC
22573 (+ SPEC SPEC ...) - add pixel values
22574 (- SPEC SPEC ...) - subtract pixel values
22575 (- SPEC) - negate pixel value
22576
22577 NUM ::=
22578 INT or FLOAT - a number constant
22579 SYMBOL - use symbol's (buffer local) variable binding.
22580
22581 UNIT ::=
22582 in - pixels per inch *)
22583 mm - pixels per 1/1000 meter *)
22584 cm - pixels per 1/100 meter *)
22585 width - width of current font in pixels.
22586 height - height of current font in pixels.
22587
22588 *) using the ratio(s) defined in display-pixels-per-inch.
22589
22590 ELEMENT ::=
22591
22592 left-fringe - left fringe width in pixels
22593 right-fringe - right fringe width in pixels
22594
22595 left-margin - left margin width in pixels
22596 right-margin - right margin width in pixels
22597
22598 scroll-bar - scroll-bar area width in pixels
22599
22600 Examples:
22601
22602 Pixels corresponding to 5 inches:
22603 (5 . in)
22604
22605 Total width of non-text areas on left side of window (if scroll-bar is on left):
22606 '(space :width (+ left-fringe left-margin scroll-bar))
22607
22608 Align to first text column (in header line):
22609 '(space :align-to 0)
22610
22611 Align to middle of text area minus half the width of variable `my-image'
22612 containing a loaded image:
22613 '(space :align-to (0.5 . (- text my-image)))
22614
22615 Width of left margin minus width of 1 character in the default font:
22616 '(space :width (- left-margin 1))
22617
22618 Width of left margin minus width of 2 characters in the current font:
22619 '(space :width (- left-margin (2 . width)))
22620
22621 Center 1 character over left-margin (in header line):
22622 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22623
22624 Different ways to express width of left fringe plus left margin minus one pixel:
22625 '(space :width (- (+ left-fringe left-margin) (1)))
22626 '(space :width (+ left-fringe left-margin (- (1))))
22627 '(space :width (+ left-fringe left-margin (-1)))
22628
22629 */
22630
22631 static int
22632 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22633 struct font *font, int width_p, int *align_to)
22634 {
22635 double pixels;
22636
22637 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22638 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22639
22640 if (NILP (prop))
22641 return OK_PIXELS (0);
22642
22643 eassert (FRAME_LIVE_P (it->f));
22644
22645 if (SYMBOLP (prop))
22646 {
22647 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22648 {
22649 char *unit = SSDATA (SYMBOL_NAME (prop));
22650
22651 if (unit[0] == 'i' && unit[1] == 'n')
22652 pixels = 1.0;
22653 else if (unit[0] == 'm' && unit[1] == 'm')
22654 pixels = 25.4;
22655 else if (unit[0] == 'c' && unit[1] == 'm')
22656 pixels = 2.54;
22657 else
22658 pixels = 0;
22659 if (pixels > 0)
22660 {
22661 double ppi = (width_p ? FRAME_RES_X (it->f)
22662 : FRAME_RES_Y (it->f));
22663
22664 if (ppi > 0)
22665 return OK_PIXELS (ppi / pixels);
22666 return 0;
22667 }
22668 }
22669
22670 #ifdef HAVE_WINDOW_SYSTEM
22671 if (EQ (prop, Qheight))
22672 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22673 if (EQ (prop, Qwidth))
22674 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22675 #else
22676 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22677 return OK_PIXELS (1);
22678 #endif
22679
22680 if (EQ (prop, Qtext))
22681 return OK_PIXELS (width_p
22682 ? window_box_width (it->w, TEXT_AREA)
22683 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22684
22685 if (align_to && *align_to < 0)
22686 {
22687 *res = 0;
22688 if (EQ (prop, Qleft))
22689 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22690 if (EQ (prop, Qright))
22691 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22692 if (EQ (prop, Qcenter))
22693 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22694 + window_box_width (it->w, TEXT_AREA) / 2);
22695 if (EQ (prop, Qleft_fringe))
22696 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22697 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22698 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22699 if (EQ (prop, Qright_fringe))
22700 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22701 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22702 : window_box_right_offset (it->w, TEXT_AREA));
22703 if (EQ (prop, Qleft_margin))
22704 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22705 if (EQ (prop, Qright_margin))
22706 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22707 if (EQ (prop, Qscroll_bar))
22708 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22709 ? 0
22710 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22711 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22712 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22713 : 0)));
22714 }
22715 else
22716 {
22717 if (EQ (prop, Qleft_fringe))
22718 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22719 if (EQ (prop, Qright_fringe))
22720 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22721 if (EQ (prop, Qleft_margin))
22722 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22723 if (EQ (prop, Qright_margin))
22724 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22725 if (EQ (prop, Qscroll_bar))
22726 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22727 }
22728
22729 prop = buffer_local_value_1 (prop, it->w->contents);
22730 if (EQ (prop, Qunbound))
22731 prop = Qnil;
22732 }
22733
22734 if (INTEGERP (prop) || FLOATP (prop))
22735 {
22736 int base_unit = (width_p
22737 ? FRAME_COLUMN_WIDTH (it->f)
22738 : FRAME_LINE_HEIGHT (it->f));
22739 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22740 }
22741
22742 if (CONSP (prop))
22743 {
22744 Lisp_Object car = XCAR (prop);
22745 Lisp_Object cdr = XCDR (prop);
22746
22747 if (SYMBOLP (car))
22748 {
22749 #ifdef HAVE_WINDOW_SYSTEM
22750 if (FRAME_WINDOW_P (it->f)
22751 && valid_image_p (prop))
22752 {
22753 ptrdiff_t id = lookup_image (it->f, prop);
22754 struct image *img = IMAGE_FROM_ID (it->f, id);
22755
22756 return OK_PIXELS (width_p ? img->width : img->height);
22757 }
22758 #endif
22759 if (EQ (car, Qplus) || EQ (car, Qminus))
22760 {
22761 int first = 1;
22762 double px;
22763
22764 pixels = 0;
22765 while (CONSP (cdr))
22766 {
22767 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22768 font, width_p, align_to))
22769 return 0;
22770 if (first)
22771 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22772 else
22773 pixels += px;
22774 cdr = XCDR (cdr);
22775 }
22776 if (EQ (car, Qminus))
22777 pixels = -pixels;
22778 return OK_PIXELS (pixels);
22779 }
22780
22781 car = buffer_local_value_1 (car, it->w->contents);
22782 if (EQ (car, Qunbound))
22783 car = Qnil;
22784 }
22785
22786 if (INTEGERP (car) || FLOATP (car))
22787 {
22788 double fact;
22789 pixels = XFLOATINT (car);
22790 if (NILP (cdr))
22791 return OK_PIXELS (pixels);
22792 if (calc_pixel_width_or_height (&fact, it, cdr,
22793 font, width_p, align_to))
22794 return OK_PIXELS (pixels * fact);
22795 return 0;
22796 }
22797
22798 return 0;
22799 }
22800
22801 return 0;
22802 }
22803
22804 \f
22805 /***********************************************************************
22806 Glyph Display
22807 ***********************************************************************/
22808
22809 #ifdef HAVE_WINDOW_SYSTEM
22810
22811 #ifdef GLYPH_DEBUG
22812
22813 void
22814 dump_glyph_string (struct glyph_string *s)
22815 {
22816 fprintf (stderr, "glyph string\n");
22817 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22818 s->x, s->y, s->width, s->height);
22819 fprintf (stderr, " ybase = %d\n", s->ybase);
22820 fprintf (stderr, " hl = %d\n", s->hl);
22821 fprintf (stderr, " left overhang = %d, right = %d\n",
22822 s->left_overhang, s->right_overhang);
22823 fprintf (stderr, " nchars = %d\n", s->nchars);
22824 fprintf (stderr, " extends to end of line = %d\n",
22825 s->extends_to_end_of_line_p);
22826 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22827 fprintf (stderr, " bg width = %d\n", s->background_width);
22828 }
22829
22830 #endif /* GLYPH_DEBUG */
22831
22832 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22833 of XChar2b structures for S; it can't be allocated in
22834 init_glyph_string because it must be allocated via `alloca'. W
22835 is the window on which S is drawn. ROW and AREA are the glyph row
22836 and area within the row from which S is constructed. START is the
22837 index of the first glyph structure covered by S. HL is a
22838 face-override for drawing S. */
22839
22840 #ifdef HAVE_NTGUI
22841 #define OPTIONAL_HDC(hdc) HDC hdc,
22842 #define DECLARE_HDC(hdc) HDC hdc;
22843 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22844 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22845 #endif
22846
22847 #ifndef OPTIONAL_HDC
22848 #define OPTIONAL_HDC(hdc)
22849 #define DECLARE_HDC(hdc)
22850 #define ALLOCATE_HDC(hdc, f)
22851 #define RELEASE_HDC(hdc, f)
22852 #endif
22853
22854 static void
22855 init_glyph_string (struct glyph_string *s,
22856 OPTIONAL_HDC (hdc)
22857 XChar2b *char2b, struct window *w, struct glyph_row *row,
22858 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22859 {
22860 memset (s, 0, sizeof *s);
22861 s->w = w;
22862 s->f = XFRAME (w->frame);
22863 #ifdef HAVE_NTGUI
22864 s->hdc = hdc;
22865 #endif
22866 s->display = FRAME_X_DISPLAY (s->f);
22867 s->window = FRAME_X_WINDOW (s->f);
22868 s->char2b = char2b;
22869 s->hl = hl;
22870 s->row = row;
22871 s->area = area;
22872 s->first_glyph = row->glyphs[area] + start;
22873 s->height = row->height;
22874 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22875 s->ybase = s->y + row->ascent;
22876 }
22877
22878
22879 /* Append the list of glyph strings with head H and tail T to the list
22880 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22881
22882 static void
22883 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22884 struct glyph_string *h, struct glyph_string *t)
22885 {
22886 if (h)
22887 {
22888 if (*head)
22889 (*tail)->next = h;
22890 else
22891 *head = h;
22892 h->prev = *tail;
22893 *tail = t;
22894 }
22895 }
22896
22897
22898 /* Prepend the list of glyph strings with head H and tail T to the
22899 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22900 result. */
22901
22902 static void
22903 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22904 struct glyph_string *h, struct glyph_string *t)
22905 {
22906 if (h)
22907 {
22908 if (*head)
22909 (*head)->prev = t;
22910 else
22911 *tail = t;
22912 t->next = *head;
22913 *head = h;
22914 }
22915 }
22916
22917
22918 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22919 Set *HEAD and *TAIL to the resulting list. */
22920
22921 static void
22922 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22923 struct glyph_string *s)
22924 {
22925 s->next = s->prev = NULL;
22926 append_glyph_string_lists (head, tail, s, s);
22927 }
22928
22929
22930 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22931 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22932 make sure that X resources for the face returned are allocated.
22933 Value is a pointer to a realized face that is ready for display if
22934 DISPLAY_P is non-zero. */
22935
22936 static struct face *
22937 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22938 XChar2b *char2b, int display_p)
22939 {
22940 struct face *face = FACE_FROM_ID (f, face_id);
22941 unsigned code = 0;
22942
22943 if (face->font)
22944 {
22945 code = face->font->driver->encode_char (face->font, c);
22946
22947 if (code == FONT_INVALID_CODE)
22948 code = 0;
22949 }
22950 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22951
22952 /* Make sure X resources of the face are allocated. */
22953 #ifdef HAVE_X_WINDOWS
22954 if (display_p)
22955 #endif
22956 {
22957 eassert (face != NULL);
22958 PREPARE_FACE_FOR_DISPLAY (f, face);
22959 }
22960
22961 return face;
22962 }
22963
22964
22965 /* Get face and two-byte form of character glyph GLYPH on frame F.
22966 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22967 a pointer to a realized face that is ready for display. */
22968
22969 static struct face *
22970 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22971 XChar2b *char2b, int *two_byte_p)
22972 {
22973 struct face *face;
22974 unsigned code = 0;
22975
22976 eassert (glyph->type == CHAR_GLYPH);
22977 face = FACE_FROM_ID (f, glyph->face_id);
22978
22979 /* Make sure X resources of the face are allocated. */
22980 eassert (face != NULL);
22981 PREPARE_FACE_FOR_DISPLAY (f, face);
22982
22983 if (two_byte_p)
22984 *two_byte_p = 0;
22985
22986 if (face->font)
22987 {
22988 if (CHAR_BYTE8_P (glyph->u.ch))
22989 code = CHAR_TO_BYTE8 (glyph->u.ch);
22990 else
22991 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22992
22993 if (code == FONT_INVALID_CODE)
22994 code = 0;
22995 }
22996
22997 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22998 return face;
22999 }
23000
23001
23002 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23003 Return 1 if FONT has a glyph for C, otherwise return 0. */
23004
23005 static int
23006 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23007 {
23008 unsigned code;
23009
23010 if (CHAR_BYTE8_P (c))
23011 code = CHAR_TO_BYTE8 (c);
23012 else
23013 code = font->driver->encode_char (font, c);
23014
23015 if (code == FONT_INVALID_CODE)
23016 return 0;
23017 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23018 return 1;
23019 }
23020
23021
23022 /* Fill glyph string S with composition components specified by S->cmp.
23023
23024 BASE_FACE is the base face of the composition.
23025 S->cmp_from is the index of the first component for S.
23026
23027 OVERLAPS non-zero means S should draw the foreground only, and use
23028 its physical height for clipping. See also draw_glyphs.
23029
23030 Value is the index of a component not in S. */
23031
23032 static int
23033 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23034 int overlaps)
23035 {
23036 int i;
23037 /* For all glyphs of this composition, starting at the offset
23038 S->cmp_from, until we reach the end of the definition or encounter a
23039 glyph that requires the different face, add it to S. */
23040 struct face *face;
23041
23042 eassert (s);
23043
23044 s->for_overlaps = overlaps;
23045 s->face = NULL;
23046 s->font = NULL;
23047 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23048 {
23049 int c = COMPOSITION_GLYPH (s->cmp, i);
23050
23051 /* TAB in a composition means display glyphs with padding space
23052 on the left or right. */
23053 if (c != '\t')
23054 {
23055 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23056 -1, Qnil);
23057
23058 face = get_char_face_and_encoding (s->f, c, face_id,
23059 s->char2b + i, 1);
23060 if (face)
23061 {
23062 if (! s->face)
23063 {
23064 s->face = face;
23065 s->font = s->face->font;
23066 }
23067 else if (s->face != face)
23068 break;
23069 }
23070 }
23071 ++s->nchars;
23072 }
23073 s->cmp_to = i;
23074
23075 if (s->face == NULL)
23076 {
23077 s->face = base_face->ascii_face;
23078 s->font = s->face->font;
23079 }
23080
23081 /* All glyph strings for the same composition has the same width,
23082 i.e. the width set for the first component of the composition. */
23083 s->width = s->first_glyph->pixel_width;
23084
23085 /* If the specified font could not be loaded, use the frame's
23086 default font, but record the fact that we couldn't load it in
23087 the glyph string so that we can draw rectangles for the
23088 characters of the glyph string. */
23089 if (s->font == NULL)
23090 {
23091 s->font_not_found_p = 1;
23092 s->font = FRAME_FONT (s->f);
23093 }
23094
23095 /* Adjust base line for subscript/superscript text. */
23096 s->ybase += s->first_glyph->voffset;
23097
23098 /* This glyph string must always be drawn with 16-bit functions. */
23099 s->two_byte_p = 1;
23100
23101 return s->cmp_to;
23102 }
23103
23104 static int
23105 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23106 int start, int end, int overlaps)
23107 {
23108 struct glyph *glyph, *last;
23109 Lisp_Object lgstring;
23110 int i;
23111
23112 s->for_overlaps = overlaps;
23113 glyph = s->row->glyphs[s->area] + start;
23114 last = s->row->glyphs[s->area] + end;
23115 s->cmp_id = glyph->u.cmp.id;
23116 s->cmp_from = glyph->slice.cmp.from;
23117 s->cmp_to = glyph->slice.cmp.to + 1;
23118 s->face = FACE_FROM_ID (s->f, face_id);
23119 lgstring = composition_gstring_from_id (s->cmp_id);
23120 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23121 glyph++;
23122 while (glyph < last
23123 && glyph->u.cmp.automatic
23124 && glyph->u.cmp.id == s->cmp_id
23125 && s->cmp_to == glyph->slice.cmp.from)
23126 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23127
23128 for (i = s->cmp_from; i < s->cmp_to; i++)
23129 {
23130 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23131 unsigned code = LGLYPH_CODE (lglyph);
23132
23133 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23134 }
23135 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23136 return glyph - s->row->glyphs[s->area];
23137 }
23138
23139
23140 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23141 See the comment of fill_glyph_string for arguments.
23142 Value is the index of the first glyph not in S. */
23143
23144
23145 static int
23146 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23147 int start, int end, int overlaps)
23148 {
23149 struct glyph *glyph, *last;
23150 int voffset;
23151
23152 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23153 s->for_overlaps = overlaps;
23154 glyph = s->row->glyphs[s->area] + start;
23155 last = s->row->glyphs[s->area] + end;
23156 voffset = glyph->voffset;
23157 s->face = FACE_FROM_ID (s->f, face_id);
23158 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23159 s->nchars = 1;
23160 s->width = glyph->pixel_width;
23161 glyph++;
23162 while (glyph < last
23163 && glyph->type == GLYPHLESS_GLYPH
23164 && glyph->voffset == voffset
23165 && glyph->face_id == face_id)
23166 {
23167 s->nchars++;
23168 s->width += glyph->pixel_width;
23169 glyph++;
23170 }
23171 s->ybase += voffset;
23172 return glyph - s->row->glyphs[s->area];
23173 }
23174
23175
23176 /* Fill glyph string S from a sequence of character glyphs.
23177
23178 FACE_ID is the face id of the string. START is the index of the
23179 first glyph to consider, END is the index of the last + 1.
23180 OVERLAPS non-zero means S should draw the foreground only, and use
23181 its physical height for clipping. See also draw_glyphs.
23182
23183 Value is the index of the first glyph not in S. */
23184
23185 static int
23186 fill_glyph_string (struct glyph_string *s, int face_id,
23187 int start, int end, int overlaps)
23188 {
23189 struct glyph *glyph, *last;
23190 int voffset;
23191 int glyph_not_available_p;
23192
23193 eassert (s->f == XFRAME (s->w->frame));
23194 eassert (s->nchars == 0);
23195 eassert (start >= 0 && end > start);
23196
23197 s->for_overlaps = overlaps;
23198 glyph = s->row->glyphs[s->area] + start;
23199 last = s->row->glyphs[s->area] + end;
23200 voffset = glyph->voffset;
23201 s->padding_p = glyph->padding_p;
23202 glyph_not_available_p = glyph->glyph_not_available_p;
23203
23204 while (glyph < last
23205 && glyph->type == CHAR_GLYPH
23206 && glyph->voffset == voffset
23207 /* Same face id implies same font, nowadays. */
23208 && glyph->face_id == face_id
23209 && glyph->glyph_not_available_p == glyph_not_available_p)
23210 {
23211 int two_byte_p;
23212
23213 s->face = get_glyph_face_and_encoding (s->f, glyph,
23214 s->char2b + s->nchars,
23215 &two_byte_p);
23216 s->two_byte_p = two_byte_p;
23217 ++s->nchars;
23218 eassert (s->nchars <= end - start);
23219 s->width += glyph->pixel_width;
23220 if (glyph++->padding_p != s->padding_p)
23221 break;
23222 }
23223
23224 s->font = s->face->font;
23225
23226 /* If the specified font could not be loaded, use the frame's font,
23227 but record the fact that we couldn't load it in
23228 S->font_not_found_p so that we can draw rectangles for the
23229 characters of the glyph string. */
23230 if (s->font == NULL || glyph_not_available_p)
23231 {
23232 s->font_not_found_p = 1;
23233 s->font = FRAME_FONT (s->f);
23234 }
23235
23236 /* Adjust base line for subscript/superscript text. */
23237 s->ybase += voffset;
23238
23239 eassert (s->face && s->face->gc);
23240 return glyph - s->row->glyphs[s->area];
23241 }
23242
23243
23244 /* Fill glyph string S from image glyph S->first_glyph. */
23245
23246 static void
23247 fill_image_glyph_string (struct glyph_string *s)
23248 {
23249 eassert (s->first_glyph->type == IMAGE_GLYPH);
23250 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23251 eassert (s->img);
23252 s->slice = s->first_glyph->slice.img;
23253 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23254 s->font = s->face->font;
23255 s->width = s->first_glyph->pixel_width;
23256
23257 /* Adjust base line for subscript/superscript text. */
23258 s->ybase += s->first_glyph->voffset;
23259 }
23260
23261
23262 /* Fill glyph string S from a sequence of stretch glyphs.
23263
23264 START is the index of the first glyph to consider,
23265 END is the index of the last + 1.
23266
23267 Value is the index of the first glyph not in S. */
23268
23269 static int
23270 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23271 {
23272 struct glyph *glyph, *last;
23273 int voffset, face_id;
23274
23275 eassert (s->first_glyph->type == STRETCH_GLYPH);
23276
23277 glyph = s->row->glyphs[s->area] + start;
23278 last = s->row->glyphs[s->area] + end;
23279 face_id = glyph->face_id;
23280 s->face = FACE_FROM_ID (s->f, face_id);
23281 s->font = s->face->font;
23282 s->width = glyph->pixel_width;
23283 s->nchars = 1;
23284 voffset = glyph->voffset;
23285
23286 for (++glyph;
23287 (glyph < last
23288 && glyph->type == STRETCH_GLYPH
23289 && glyph->voffset == voffset
23290 && glyph->face_id == face_id);
23291 ++glyph)
23292 s->width += glyph->pixel_width;
23293
23294 /* Adjust base line for subscript/superscript text. */
23295 s->ybase += voffset;
23296
23297 /* The case that face->gc == 0 is handled when drawing the glyph
23298 string by calling PREPARE_FACE_FOR_DISPLAY. */
23299 eassert (s->face);
23300 return glyph - s->row->glyphs[s->area];
23301 }
23302
23303 static struct font_metrics *
23304 get_per_char_metric (struct font *font, XChar2b *char2b)
23305 {
23306 static struct font_metrics metrics;
23307 unsigned code;
23308
23309 if (! font)
23310 return NULL;
23311 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23312 if (code == FONT_INVALID_CODE)
23313 return NULL;
23314 font->driver->text_extents (font, &code, 1, &metrics);
23315 return &metrics;
23316 }
23317
23318 /* EXPORT for RIF:
23319 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23320 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23321 assumed to be zero. */
23322
23323 void
23324 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23325 {
23326 *left = *right = 0;
23327
23328 if (glyph->type == CHAR_GLYPH)
23329 {
23330 struct face *face;
23331 XChar2b char2b;
23332 struct font_metrics *pcm;
23333
23334 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23335 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23336 {
23337 if (pcm->rbearing > pcm->width)
23338 *right = pcm->rbearing - pcm->width;
23339 if (pcm->lbearing < 0)
23340 *left = -pcm->lbearing;
23341 }
23342 }
23343 else if (glyph->type == COMPOSITE_GLYPH)
23344 {
23345 if (! glyph->u.cmp.automatic)
23346 {
23347 struct composition *cmp = composition_table[glyph->u.cmp.id];
23348
23349 if (cmp->rbearing > cmp->pixel_width)
23350 *right = cmp->rbearing - cmp->pixel_width;
23351 if (cmp->lbearing < 0)
23352 *left = - cmp->lbearing;
23353 }
23354 else
23355 {
23356 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23357 struct font_metrics metrics;
23358
23359 composition_gstring_width (gstring, glyph->slice.cmp.from,
23360 glyph->slice.cmp.to + 1, &metrics);
23361 if (metrics.rbearing > metrics.width)
23362 *right = metrics.rbearing - metrics.width;
23363 if (metrics.lbearing < 0)
23364 *left = - metrics.lbearing;
23365 }
23366 }
23367 }
23368
23369
23370 /* Return the index of the first glyph preceding glyph string S that
23371 is overwritten by S because of S's left overhang. Value is -1
23372 if no glyphs are overwritten. */
23373
23374 static int
23375 left_overwritten (struct glyph_string *s)
23376 {
23377 int k;
23378
23379 if (s->left_overhang)
23380 {
23381 int x = 0, i;
23382 struct glyph *glyphs = s->row->glyphs[s->area];
23383 int first = s->first_glyph - glyphs;
23384
23385 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23386 x -= glyphs[i].pixel_width;
23387
23388 k = i + 1;
23389 }
23390 else
23391 k = -1;
23392
23393 return k;
23394 }
23395
23396
23397 /* Return the index of the first glyph preceding glyph string S that
23398 is overwriting S because of its right overhang. Value is -1 if no
23399 glyph in front of S overwrites S. */
23400
23401 static int
23402 left_overwriting (struct glyph_string *s)
23403 {
23404 int i, k, x;
23405 struct glyph *glyphs = s->row->glyphs[s->area];
23406 int first = s->first_glyph - glyphs;
23407
23408 k = -1;
23409 x = 0;
23410 for (i = first - 1; i >= 0; --i)
23411 {
23412 int left, right;
23413 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23414 if (x + right > 0)
23415 k = i;
23416 x -= glyphs[i].pixel_width;
23417 }
23418
23419 return k;
23420 }
23421
23422
23423 /* Return the index of the last glyph following glyph string S that is
23424 overwritten by S because of S's right overhang. Value is -1 if
23425 no such glyph is found. */
23426
23427 static int
23428 right_overwritten (struct glyph_string *s)
23429 {
23430 int k = -1;
23431
23432 if (s->right_overhang)
23433 {
23434 int x = 0, i;
23435 struct glyph *glyphs = s->row->glyphs[s->area];
23436 int first = (s->first_glyph - glyphs
23437 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23438 int end = s->row->used[s->area];
23439
23440 for (i = first; i < end && s->right_overhang > x; ++i)
23441 x += glyphs[i].pixel_width;
23442
23443 k = i;
23444 }
23445
23446 return k;
23447 }
23448
23449
23450 /* Return the index of the last glyph following glyph string S that
23451 overwrites S because of its left overhang. Value is negative
23452 if no such glyph is found. */
23453
23454 static int
23455 right_overwriting (struct glyph_string *s)
23456 {
23457 int i, k, x;
23458 int end = s->row->used[s->area];
23459 struct glyph *glyphs = s->row->glyphs[s->area];
23460 int first = (s->first_glyph - glyphs
23461 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23462
23463 k = -1;
23464 x = 0;
23465 for (i = first; i < end; ++i)
23466 {
23467 int left, right;
23468 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23469 if (x - left < 0)
23470 k = i;
23471 x += glyphs[i].pixel_width;
23472 }
23473
23474 return k;
23475 }
23476
23477
23478 /* Set background width of glyph string S. START is the index of the
23479 first glyph following S. LAST_X is the right-most x-position + 1
23480 in the drawing area. */
23481
23482 static void
23483 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23484 {
23485 /* If the face of this glyph string has to be drawn to the end of
23486 the drawing area, set S->extends_to_end_of_line_p. */
23487
23488 if (start == s->row->used[s->area]
23489 && s->area == TEXT_AREA
23490 && ((s->row->fill_line_p
23491 && (s->hl == DRAW_NORMAL_TEXT
23492 || s->hl == DRAW_IMAGE_RAISED
23493 || s->hl == DRAW_IMAGE_SUNKEN))
23494 || s->hl == DRAW_MOUSE_FACE))
23495 s->extends_to_end_of_line_p = 1;
23496
23497 /* If S extends its face to the end of the line, set its
23498 background_width to the distance to the right edge of the drawing
23499 area. */
23500 if (s->extends_to_end_of_line_p)
23501 s->background_width = last_x - s->x + 1;
23502 else
23503 s->background_width = s->width;
23504 }
23505
23506
23507 /* Compute overhangs and x-positions for glyph string S and its
23508 predecessors, or successors. X is the starting x-position for S.
23509 BACKWARD_P non-zero means process predecessors. */
23510
23511 static void
23512 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23513 {
23514 if (backward_p)
23515 {
23516 while (s)
23517 {
23518 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23519 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23520 x -= s->width;
23521 s->x = x;
23522 s = s->prev;
23523 }
23524 }
23525 else
23526 {
23527 while (s)
23528 {
23529 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23530 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23531 s->x = x;
23532 x += s->width;
23533 s = s->next;
23534 }
23535 }
23536 }
23537
23538
23539
23540 /* The following macros are only called from draw_glyphs below.
23541 They reference the following parameters of that function directly:
23542 `w', `row', `area', and `overlap_p'
23543 as well as the following local variables:
23544 `s', `f', and `hdc' (in W32) */
23545
23546 #ifdef HAVE_NTGUI
23547 /* On W32, silently add local `hdc' variable to argument list of
23548 init_glyph_string. */
23549 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23550 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23551 #else
23552 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23553 init_glyph_string (s, char2b, w, row, area, start, hl)
23554 #endif
23555
23556 /* Add a glyph string for a stretch glyph to the list of strings
23557 between HEAD and TAIL. START is the index of the stretch glyph in
23558 row area AREA of glyph row ROW. END is the index of the last glyph
23559 in that glyph row area. X is the current output position assigned
23560 to the new glyph string constructed. HL overrides that face of the
23561 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23562 is the right-most x-position of the drawing area. */
23563
23564 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23565 and below -- keep them on one line. */
23566 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23567 do \
23568 { \
23569 s = alloca (sizeof *s); \
23570 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23571 START = fill_stretch_glyph_string (s, START, END); \
23572 append_glyph_string (&HEAD, &TAIL, s); \
23573 s->x = (X); \
23574 } \
23575 while (0)
23576
23577
23578 /* Add a glyph string for an image glyph to the list of strings
23579 between HEAD and TAIL. START is the index of the image glyph in
23580 row area AREA of glyph row ROW. END is the index of the last glyph
23581 in that glyph row area. X is the current output position assigned
23582 to the new glyph string constructed. HL overrides that face of the
23583 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23584 is the right-most x-position of the drawing area. */
23585
23586 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23587 do \
23588 { \
23589 s = alloca (sizeof *s); \
23590 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23591 fill_image_glyph_string (s); \
23592 append_glyph_string (&HEAD, &TAIL, s); \
23593 ++START; \
23594 s->x = (X); \
23595 } \
23596 while (0)
23597
23598
23599 /* Add a glyph string for a sequence of character glyphs to the list
23600 of strings between HEAD and TAIL. START is the index of the first
23601 glyph in row area AREA of glyph row ROW that is part of the new
23602 glyph string. END is the index of the last glyph in that glyph row
23603 area. X is the current output position assigned to the new glyph
23604 string constructed. HL overrides that face of the glyph; e.g. it
23605 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23606 right-most x-position of the drawing area. */
23607
23608 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23609 do \
23610 { \
23611 int face_id; \
23612 XChar2b *char2b; \
23613 \
23614 face_id = (row)->glyphs[area][START].face_id; \
23615 \
23616 s = alloca (sizeof *s); \
23617 char2b = alloca ((END - START) * sizeof *char2b); \
23618 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23619 append_glyph_string (&HEAD, &TAIL, s); \
23620 s->x = (X); \
23621 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23622 } \
23623 while (0)
23624
23625
23626 /* Add a glyph string for a composite sequence to the list of strings
23627 between HEAD and TAIL. START is the index of the first glyph in
23628 row area AREA of glyph row ROW that is part of the new glyph
23629 string. END is the index of the last glyph in that glyph row area.
23630 X is the current output position assigned to the new glyph string
23631 constructed. HL overrides that face of the glyph; e.g. it is
23632 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23633 x-position of the drawing area. */
23634
23635 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23636 do { \
23637 int face_id = (row)->glyphs[area][START].face_id; \
23638 struct face *base_face = FACE_FROM_ID (f, face_id); \
23639 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23640 struct composition *cmp = composition_table[cmp_id]; \
23641 XChar2b *char2b; \
23642 struct glyph_string *first_s = NULL; \
23643 int n; \
23644 \
23645 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23646 \
23647 /* Make glyph_strings for each glyph sequence that is drawable by \
23648 the same face, and append them to HEAD/TAIL. */ \
23649 for (n = 0; n < cmp->glyph_len;) \
23650 { \
23651 s = alloca (sizeof *s); \
23652 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23653 append_glyph_string (&(HEAD), &(TAIL), s); \
23654 s->cmp = cmp; \
23655 s->cmp_from = n; \
23656 s->x = (X); \
23657 if (n == 0) \
23658 first_s = s; \
23659 n = fill_composite_glyph_string (s, base_face, overlaps); \
23660 } \
23661 \
23662 ++START; \
23663 s = first_s; \
23664 } while (0)
23665
23666
23667 /* Add a glyph string for a glyph-string sequence to the list of strings
23668 between HEAD and TAIL. */
23669
23670 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23671 do { \
23672 int face_id; \
23673 XChar2b *char2b; \
23674 Lisp_Object gstring; \
23675 \
23676 face_id = (row)->glyphs[area][START].face_id; \
23677 gstring = (composition_gstring_from_id \
23678 ((row)->glyphs[area][START].u.cmp.id)); \
23679 s = alloca (sizeof *s); \
23680 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23681 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23682 append_glyph_string (&(HEAD), &(TAIL), s); \
23683 s->x = (X); \
23684 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23685 } while (0)
23686
23687
23688 /* Add a glyph string for a sequence of glyphless character's glyphs
23689 to the list of strings between HEAD and TAIL. The meanings of
23690 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23691
23692 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23693 do \
23694 { \
23695 int face_id; \
23696 \
23697 face_id = (row)->glyphs[area][START].face_id; \
23698 \
23699 s = alloca (sizeof *s); \
23700 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23701 append_glyph_string (&HEAD, &TAIL, s); \
23702 s->x = (X); \
23703 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23704 overlaps); \
23705 } \
23706 while (0)
23707
23708
23709 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23710 of AREA of glyph row ROW on window W between indices START and END.
23711 HL overrides the face for drawing glyph strings, e.g. it is
23712 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23713 x-positions of the drawing area.
23714
23715 This is an ugly monster macro construct because we must use alloca
23716 to allocate glyph strings (because draw_glyphs can be called
23717 asynchronously). */
23718
23719 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23720 do \
23721 { \
23722 HEAD = TAIL = NULL; \
23723 while (START < END) \
23724 { \
23725 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23726 switch (first_glyph->type) \
23727 { \
23728 case CHAR_GLYPH: \
23729 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23730 HL, X, LAST_X); \
23731 break; \
23732 \
23733 case COMPOSITE_GLYPH: \
23734 if (first_glyph->u.cmp.automatic) \
23735 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23736 HL, X, LAST_X); \
23737 else \
23738 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23739 HL, X, LAST_X); \
23740 break; \
23741 \
23742 case STRETCH_GLYPH: \
23743 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23744 HL, X, LAST_X); \
23745 break; \
23746 \
23747 case IMAGE_GLYPH: \
23748 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23749 HL, X, LAST_X); \
23750 break; \
23751 \
23752 case GLYPHLESS_GLYPH: \
23753 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23754 HL, X, LAST_X); \
23755 break; \
23756 \
23757 default: \
23758 emacs_abort (); \
23759 } \
23760 \
23761 if (s) \
23762 { \
23763 set_glyph_string_background_width (s, START, LAST_X); \
23764 (X) += s->width; \
23765 } \
23766 } \
23767 } while (0)
23768
23769
23770 /* Draw glyphs between START and END in AREA of ROW on window W,
23771 starting at x-position X. X is relative to AREA in W. HL is a
23772 face-override with the following meaning:
23773
23774 DRAW_NORMAL_TEXT draw normally
23775 DRAW_CURSOR draw in cursor face
23776 DRAW_MOUSE_FACE draw in mouse face.
23777 DRAW_INVERSE_VIDEO draw in mode line face
23778 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23779 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23780
23781 If OVERLAPS is non-zero, draw only the foreground of characters and
23782 clip to the physical height of ROW. Non-zero value also defines
23783 the overlapping part to be drawn:
23784
23785 OVERLAPS_PRED overlap with preceding rows
23786 OVERLAPS_SUCC overlap with succeeding rows
23787 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23788 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23789
23790 Value is the x-position reached, relative to AREA of W. */
23791
23792 static int
23793 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23794 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23795 enum draw_glyphs_face hl, int overlaps)
23796 {
23797 struct glyph_string *head, *tail;
23798 struct glyph_string *s;
23799 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23800 int i, j, x_reached, last_x, area_left = 0;
23801 struct frame *f = XFRAME (WINDOW_FRAME (w));
23802 DECLARE_HDC (hdc);
23803
23804 ALLOCATE_HDC (hdc, f);
23805
23806 /* Let's rather be paranoid than getting a SEGV. */
23807 end = min (end, row->used[area]);
23808 start = clip_to_bounds (0, start, end);
23809
23810 /* Translate X to frame coordinates. Set last_x to the right
23811 end of the drawing area. */
23812 if (row->full_width_p)
23813 {
23814 /* X is relative to the left edge of W, without scroll bars
23815 or fringes. */
23816 area_left = WINDOW_LEFT_EDGE_X (w);
23817 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23818 }
23819 else
23820 {
23821 area_left = window_box_left (w, area);
23822 last_x = area_left + window_box_width (w, area);
23823 }
23824 x += area_left;
23825
23826 /* Build a doubly-linked list of glyph_string structures between
23827 head and tail from what we have to draw. Note that the macro
23828 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23829 the reason we use a separate variable `i'. */
23830 i = start;
23831 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23832 if (tail)
23833 x_reached = tail->x + tail->background_width;
23834 else
23835 x_reached = x;
23836
23837 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23838 the row, redraw some glyphs in front or following the glyph
23839 strings built above. */
23840 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23841 {
23842 struct glyph_string *h, *t;
23843 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23844 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23845 int check_mouse_face = 0;
23846 int dummy_x = 0;
23847
23848 /* If mouse highlighting is on, we may need to draw adjacent
23849 glyphs using mouse-face highlighting. */
23850 if (area == TEXT_AREA && row->mouse_face_p
23851 && hlinfo->mouse_face_beg_row >= 0
23852 && hlinfo->mouse_face_end_row >= 0)
23853 {
23854 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
23855
23856 if (row_vpos >= hlinfo->mouse_face_beg_row
23857 && row_vpos <= hlinfo->mouse_face_end_row)
23858 {
23859 check_mouse_face = 1;
23860 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
23861 ? hlinfo->mouse_face_beg_col : 0;
23862 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
23863 ? hlinfo->mouse_face_end_col
23864 : row->used[TEXT_AREA];
23865 }
23866 }
23867
23868 /* Compute overhangs for all glyph strings. */
23869 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23870 for (s = head; s; s = s->next)
23871 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23872
23873 /* Prepend glyph strings for glyphs in front of the first glyph
23874 string that are overwritten because of the first glyph
23875 string's left overhang. The background of all strings
23876 prepended must be drawn because the first glyph string
23877 draws over it. */
23878 i = left_overwritten (head);
23879 if (i >= 0)
23880 {
23881 enum draw_glyphs_face overlap_hl;
23882
23883 /* If this row contains mouse highlighting, attempt to draw
23884 the overlapped glyphs with the correct highlight. This
23885 code fails if the overlap encompasses more than one glyph
23886 and mouse-highlight spans only some of these glyphs.
23887 However, making it work perfectly involves a lot more
23888 code, and I don't know if the pathological case occurs in
23889 practice, so we'll stick to this for now. --- cyd */
23890 if (check_mouse_face
23891 && mouse_beg_col < start && mouse_end_col > i)
23892 overlap_hl = DRAW_MOUSE_FACE;
23893 else
23894 overlap_hl = DRAW_NORMAL_TEXT;
23895
23896 j = i;
23897 BUILD_GLYPH_STRINGS (j, start, h, t,
23898 overlap_hl, dummy_x, last_x);
23899 start = i;
23900 compute_overhangs_and_x (t, head->x, 1);
23901 prepend_glyph_string_lists (&head, &tail, h, t);
23902 clip_head = head;
23903 }
23904
23905 /* Prepend glyph strings for glyphs in front of the first glyph
23906 string that overwrite that glyph string because of their
23907 right overhang. For these strings, only the foreground must
23908 be drawn, because it draws over the glyph string at `head'.
23909 The background must not be drawn because this would overwrite
23910 right overhangs of preceding glyphs for which no glyph
23911 strings exist. */
23912 i = left_overwriting (head);
23913 if (i >= 0)
23914 {
23915 enum draw_glyphs_face overlap_hl;
23916
23917 if (check_mouse_face
23918 && mouse_beg_col < start && mouse_end_col > i)
23919 overlap_hl = DRAW_MOUSE_FACE;
23920 else
23921 overlap_hl = DRAW_NORMAL_TEXT;
23922
23923 clip_head = head;
23924 BUILD_GLYPH_STRINGS (i, start, h, t,
23925 overlap_hl, dummy_x, last_x);
23926 for (s = h; s; s = s->next)
23927 s->background_filled_p = 1;
23928 compute_overhangs_and_x (t, head->x, 1);
23929 prepend_glyph_string_lists (&head, &tail, h, t);
23930 }
23931
23932 /* Append glyphs strings for glyphs following the last glyph
23933 string tail that are overwritten by tail. The background of
23934 these strings has to be drawn because tail's foreground draws
23935 over it. */
23936 i = right_overwritten (tail);
23937 if (i >= 0)
23938 {
23939 enum draw_glyphs_face overlap_hl;
23940
23941 if (check_mouse_face
23942 && mouse_beg_col < i && mouse_end_col > end)
23943 overlap_hl = DRAW_MOUSE_FACE;
23944 else
23945 overlap_hl = DRAW_NORMAL_TEXT;
23946
23947 BUILD_GLYPH_STRINGS (end, i, h, t,
23948 overlap_hl, x, last_x);
23949 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23950 we don't have `end = i;' here. */
23951 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23952 append_glyph_string_lists (&head, &tail, h, t);
23953 clip_tail = tail;
23954 }
23955
23956 /* Append glyph strings for glyphs following the last glyph
23957 string tail that overwrite tail. The foreground of such
23958 glyphs has to be drawn because it writes into the background
23959 of tail. The background must not be drawn because it could
23960 paint over the foreground of following glyphs. */
23961 i = right_overwriting (tail);
23962 if (i >= 0)
23963 {
23964 enum draw_glyphs_face overlap_hl;
23965 if (check_mouse_face
23966 && mouse_beg_col < i && mouse_end_col > end)
23967 overlap_hl = DRAW_MOUSE_FACE;
23968 else
23969 overlap_hl = DRAW_NORMAL_TEXT;
23970
23971 clip_tail = tail;
23972 i++; /* We must include the Ith glyph. */
23973 BUILD_GLYPH_STRINGS (end, i, h, t,
23974 overlap_hl, x, last_x);
23975 for (s = h; s; s = s->next)
23976 s->background_filled_p = 1;
23977 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23978 append_glyph_string_lists (&head, &tail, h, t);
23979 }
23980 if (clip_head || clip_tail)
23981 for (s = head; s; s = s->next)
23982 {
23983 s->clip_head = clip_head;
23984 s->clip_tail = clip_tail;
23985 }
23986 }
23987
23988 /* Draw all strings. */
23989 for (s = head; s; s = s->next)
23990 FRAME_RIF (f)->draw_glyph_string (s);
23991
23992 #ifndef HAVE_NS
23993 /* When focus a sole frame and move horizontally, this sets on_p to 0
23994 causing a failure to erase prev cursor position. */
23995 if (area == TEXT_AREA
23996 && !row->full_width_p
23997 /* When drawing overlapping rows, only the glyph strings'
23998 foreground is drawn, which doesn't erase a cursor
23999 completely. */
24000 && !overlaps)
24001 {
24002 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24003 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24004 : (tail ? tail->x + tail->background_width : x));
24005 x0 -= area_left;
24006 x1 -= area_left;
24007
24008 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24009 row->y, MATRIX_ROW_BOTTOM_Y (row));
24010 }
24011 #endif
24012
24013 /* Value is the x-position up to which drawn, relative to AREA of W.
24014 This doesn't include parts drawn because of overhangs. */
24015 if (row->full_width_p)
24016 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24017 else
24018 x_reached -= area_left;
24019
24020 RELEASE_HDC (hdc, f);
24021
24022 return x_reached;
24023 }
24024
24025 /* Expand row matrix if too narrow. Don't expand if area
24026 is not present. */
24027
24028 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24029 { \
24030 if (!fonts_changed_p \
24031 && (it->glyph_row->glyphs[area] \
24032 < it->glyph_row->glyphs[area + 1])) \
24033 { \
24034 it->w->ncols_scale_factor++; \
24035 fonts_changed_p = 1; \
24036 } \
24037 }
24038
24039 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24040 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24041
24042 static void
24043 append_glyph (struct it *it)
24044 {
24045 struct glyph *glyph;
24046 enum glyph_row_area area = it->area;
24047
24048 eassert (it->glyph_row);
24049 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24050
24051 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24052 if (glyph < it->glyph_row->glyphs[area + 1])
24053 {
24054 /* If the glyph row is reversed, we need to prepend the glyph
24055 rather than append it. */
24056 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24057 {
24058 struct glyph *g;
24059
24060 /* Make room for the additional glyph. */
24061 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24062 g[1] = *g;
24063 glyph = it->glyph_row->glyphs[area];
24064 }
24065 glyph->charpos = CHARPOS (it->position);
24066 glyph->object = it->object;
24067 if (it->pixel_width > 0)
24068 {
24069 glyph->pixel_width = it->pixel_width;
24070 glyph->padding_p = 0;
24071 }
24072 else
24073 {
24074 /* Assure at least 1-pixel width. Otherwise, cursor can't
24075 be displayed correctly. */
24076 glyph->pixel_width = 1;
24077 glyph->padding_p = 1;
24078 }
24079 glyph->ascent = it->ascent;
24080 glyph->descent = it->descent;
24081 glyph->voffset = it->voffset;
24082 glyph->type = CHAR_GLYPH;
24083 glyph->avoid_cursor_p = it->avoid_cursor_p;
24084 glyph->multibyte_p = it->multibyte_p;
24085 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24086 {
24087 /* In R2L rows, the left and the right box edges need to be
24088 drawn in reverse direction. */
24089 glyph->right_box_line_p = it->start_of_box_run_p;
24090 glyph->left_box_line_p = it->end_of_box_run_p;
24091 }
24092 else
24093 {
24094 glyph->left_box_line_p = it->start_of_box_run_p;
24095 glyph->right_box_line_p = it->end_of_box_run_p;
24096 }
24097 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24098 || it->phys_descent > it->descent);
24099 glyph->glyph_not_available_p = it->glyph_not_available_p;
24100 glyph->face_id = it->face_id;
24101 glyph->u.ch = it->char_to_display;
24102 glyph->slice.img = null_glyph_slice;
24103 glyph->font_type = FONT_TYPE_UNKNOWN;
24104 if (it->bidi_p)
24105 {
24106 glyph->resolved_level = it->bidi_it.resolved_level;
24107 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24108 emacs_abort ();
24109 glyph->bidi_type = it->bidi_it.type;
24110 }
24111 else
24112 {
24113 glyph->resolved_level = 0;
24114 glyph->bidi_type = UNKNOWN_BT;
24115 }
24116 ++it->glyph_row->used[area];
24117 }
24118 else
24119 IT_EXPAND_MATRIX_WIDTH (it, area);
24120 }
24121
24122 /* Store one glyph for the composition IT->cmp_it.id in
24123 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24124 non-null. */
24125
24126 static void
24127 append_composite_glyph (struct it *it)
24128 {
24129 struct glyph *glyph;
24130 enum glyph_row_area area = it->area;
24131
24132 eassert (it->glyph_row);
24133
24134 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24135 if (glyph < it->glyph_row->glyphs[area + 1])
24136 {
24137 /* If the glyph row is reversed, we need to prepend the glyph
24138 rather than append it. */
24139 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24140 {
24141 struct glyph *g;
24142
24143 /* Make room for the new glyph. */
24144 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24145 g[1] = *g;
24146 glyph = it->glyph_row->glyphs[it->area];
24147 }
24148 glyph->charpos = it->cmp_it.charpos;
24149 glyph->object = it->object;
24150 glyph->pixel_width = it->pixel_width;
24151 glyph->ascent = it->ascent;
24152 glyph->descent = it->descent;
24153 glyph->voffset = it->voffset;
24154 glyph->type = COMPOSITE_GLYPH;
24155 if (it->cmp_it.ch < 0)
24156 {
24157 glyph->u.cmp.automatic = 0;
24158 glyph->u.cmp.id = it->cmp_it.id;
24159 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24160 }
24161 else
24162 {
24163 glyph->u.cmp.automatic = 1;
24164 glyph->u.cmp.id = it->cmp_it.id;
24165 glyph->slice.cmp.from = it->cmp_it.from;
24166 glyph->slice.cmp.to = it->cmp_it.to - 1;
24167 }
24168 glyph->avoid_cursor_p = it->avoid_cursor_p;
24169 glyph->multibyte_p = it->multibyte_p;
24170 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24171 {
24172 /* In R2L rows, the left and the right box edges need to be
24173 drawn in reverse direction. */
24174 glyph->right_box_line_p = it->start_of_box_run_p;
24175 glyph->left_box_line_p = it->end_of_box_run_p;
24176 }
24177 else
24178 {
24179 glyph->left_box_line_p = it->start_of_box_run_p;
24180 glyph->right_box_line_p = it->end_of_box_run_p;
24181 }
24182 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24183 || it->phys_descent > it->descent);
24184 glyph->padding_p = 0;
24185 glyph->glyph_not_available_p = 0;
24186 glyph->face_id = it->face_id;
24187 glyph->font_type = FONT_TYPE_UNKNOWN;
24188 if (it->bidi_p)
24189 {
24190 glyph->resolved_level = it->bidi_it.resolved_level;
24191 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24192 emacs_abort ();
24193 glyph->bidi_type = it->bidi_it.type;
24194 }
24195 ++it->glyph_row->used[area];
24196 }
24197 else
24198 IT_EXPAND_MATRIX_WIDTH (it, area);
24199 }
24200
24201
24202 /* Change IT->ascent and IT->height according to the setting of
24203 IT->voffset. */
24204
24205 static void
24206 take_vertical_position_into_account (struct it *it)
24207 {
24208 if (it->voffset)
24209 {
24210 if (it->voffset < 0)
24211 /* Increase the ascent so that we can display the text higher
24212 in the line. */
24213 it->ascent -= it->voffset;
24214 else
24215 /* Increase the descent so that we can display the text lower
24216 in the line. */
24217 it->descent += it->voffset;
24218 }
24219 }
24220
24221
24222 /* Produce glyphs/get display metrics for the image IT is loaded with.
24223 See the description of struct display_iterator in dispextern.h for
24224 an overview of struct display_iterator. */
24225
24226 static void
24227 produce_image_glyph (struct it *it)
24228 {
24229 struct image *img;
24230 struct face *face;
24231 int glyph_ascent, crop;
24232 struct glyph_slice slice;
24233
24234 eassert (it->what == IT_IMAGE);
24235
24236 face = FACE_FROM_ID (it->f, it->face_id);
24237 eassert (face);
24238 /* Make sure X resources of the face is loaded. */
24239 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24240
24241 if (it->image_id < 0)
24242 {
24243 /* Fringe bitmap. */
24244 it->ascent = it->phys_ascent = 0;
24245 it->descent = it->phys_descent = 0;
24246 it->pixel_width = 0;
24247 it->nglyphs = 0;
24248 return;
24249 }
24250
24251 img = IMAGE_FROM_ID (it->f, it->image_id);
24252 eassert (img);
24253 /* Make sure X resources of the image is loaded. */
24254 prepare_image_for_display (it->f, img);
24255
24256 slice.x = slice.y = 0;
24257 slice.width = img->width;
24258 slice.height = img->height;
24259
24260 if (INTEGERP (it->slice.x))
24261 slice.x = XINT (it->slice.x);
24262 else if (FLOATP (it->slice.x))
24263 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24264
24265 if (INTEGERP (it->slice.y))
24266 slice.y = XINT (it->slice.y);
24267 else if (FLOATP (it->slice.y))
24268 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24269
24270 if (INTEGERP (it->slice.width))
24271 slice.width = XINT (it->slice.width);
24272 else if (FLOATP (it->slice.width))
24273 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24274
24275 if (INTEGERP (it->slice.height))
24276 slice.height = XINT (it->slice.height);
24277 else if (FLOATP (it->slice.height))
24278 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24279
24280 if (slice.x >= img->width)
24281 slice.x = img->width;
24282 if (slice.y >= img->height)
24283 slice.y = img->height;
24284 if (slice.x + slice.width >= img->width)
24285 slice.width = img->width - slice.x;
24286 if (slice.y + slice.height > img->height)
24287 slice.height = img->height - slice.y;
24288
24289 if (slice.width == 0 || slice.height == 0)
24290 return;
24291
24292 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24293
24294 it->descent = slice.height - glyph_ascent;
24295 if (slice.y == 0)
24296 it->descent += img->vmargin;
24297 if (slice.y + slice.height == img->height)
24298 it->descent += img->vmargin;
24299 it->phys_descent = it->descent;
24300
24301 it->pixel_width = slice.width;
24302 if (slice.x == 0)
24303 it->pixel_width += img->hmargin;
24304 if (slice.x + slice.width == img->width)
24305 it->pixel_width += img->hmargin;
24306
24307 /* It's quite possible for images to have an ascent greater than
24308 their height, so don't get confused in that case. */
24309 if (it->descent < 0)
24310 it->descent = 0;
24311
24312 it->nglyphs = 1;
24313
24314 if (face->box != FACE_NO_BOX)
24315 {
24316 if (face->box_line_width > 0)
24317 {
24318 if (slice.y == 0)
24319 it->ascent += face->box_line_width;
24320 if (slice.y + slice.height == img->height)
24321 it->descent += face->box_line_width;
24322 }
24323
24324 if (it->start_of_box_run_p && slice.x == 0)
24325 it->pixel_width += eabs (face->box_line_width);
24326 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24327 it->pixel_width += eabs (face->box_line_width);
24328 }
24329
24330 take_vertical_position_into_account (it);
24331
24332 /* Automatically crop wide image glyphs at right edge so we can
24333 draw the cursor on same display row. */
24334 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24335 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24336 {
24337 it->pixel_width -= crop;
24338 slice.width -= crop;
24339 }
24340
24341 if (it->glyph_row)
24342 {
24343 struct glyph *glyph;
24344 enum glyph_row_area area = it->area;
24345
24346 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24347 if (glyph < it->glyph_row->glyphs[area + 1])
24348 {
24349 glyph->charpos = CHARPOS (it->position);
24350 glyph->object = it->object;
24351 glyph->pixel_width = it->pixel_width;
24352 glyph->ascent = glyph_ascent;
24353 glyph->descent = it->descent;
24354 glyph->voffset = it->voffset;
24355 glyph->type = IMAGE_GLYPH;
24356 glyph->avoid_cursor_p = it->avoid_cursor_p;
24357 glyph->multibyte_p = it->multibyte_p;
24358 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24359 {
24360 /* In R2L rows, the left and the right box edges need to be
24361 drawn in reverse direction. */
24362 glyph->right_box_line_p = it->start_of_box_run_p;
24363 glyph->left_box_line_p = it->end_of_box_run_p;
24364 }
24365 else
24366 {
24367 glyph->left_box_line_p = it->start_of_box_run_p;
24368 glyph->right_box_line_p = it->end_of_box_run_p;
24369 }
24370 glyph->overlaps_vertically_p = 0;
24371 glyph->padding_p = 0;
24372 glyph->glyph_not_available_p = 0;
24373 glyph->face_id = it->face_id;
24374 glyph->u.img_id = img->id;
24375 glyph->slice.img = slice;
24376 glyph->font_type = FONT_TYPE_UNKNOWN;
24377 if (it->bidi_p)
24378 {
24379 glyph->resolved_level = it->bidi_it.resolved_level;
24380 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24381 emacs_abort ();
24382 glyph->bidi_type = it->bidi_it.type;
24383 }
24384 ++it->glyph_row->used[area];
24385 }
24386 else
24387 IT_EXPAND_MATRIX_WIDTH (it, area);
24388 }
24389 }
24390
24391
24392 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24393 of the glyph, WIDTH and HEIGHT are the width and height of the
24394 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24395
24396 static void
24397 append_stretch_glyph (struct it *it, Lisp_Object object,
24398 int width, int height, int ascent)
24399 {
24400 struct glyph *glyph;
24401 enum glyph_row_area area = it->area;
24402
24403 eassert (ascent >= 0 && ascent <= height);
24404
24405 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24406 if (glyph < it->glyph_row->glyphs[area + 1])
24407 {
24408 /* If the glyph row is reversed, we need to prepend the glyph
24409 rather than append it. */
24410 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24411 {
24412 struct glyph *g;
24413
24414 /* Make room for the additional glyph. */
24415 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24416 g[1] = *g;
24417 glyph = it->glyph_row->glyphs[area];
24418 }
24419 glyph->charpos = CHARPOS (it->position);
24420 glyph->object = object;
24421 glyph->pixel_width = width;
24422 glyph->ascent = ascent;
24423 glyph->descent = height - ascent;
24424 glyph->voffset = it->voffset;
24425 glyph->type = STRETCH_GLYPH;
24426 glyph->avoid_cursor_p = it->avoid_cursor_p;
24427 glyph->multibyte_p = it->multibyte_p;
24428 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24429 {
24430 /* In R2L rows, the left and the right box edges need to be
24431 drawn in reverse direction. */
24432 glyph->right_box_line_p = it->start_of_box_run_p;
24433 glyph->left_box_line_p = it->end_of_box_run_p;
24434 }
24435 else
24436 {
24437 glyph->left_box_line_p = it->start_of_box_run_p;
24438 glyph->right_box_line_p = it->end_of_box_run_p;
24439 }
24440 glyph->overlaps_vertically_p = 0;
24441 glyph->padding_p = 0;
24442 glyph->glyph_not_available_p = 0;
24443 glyph->face_id = it->face_id;
24444 glyph->u.stretch.ascent = ascent;
24445 glyph->u.stretch.height = height;
24446 glyph->slice.img = null_glyph_slice;
24447 glyph->font_type = FONT_TYPE_UNKNOWN;
24448 if (it->bidi_p)
24449 {
24450 glyph->resolved_level = it->bidi_it.resolved_level;
24451 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24452 emacs_abort ();
24453 glyph->bidi_type = it->bidi_it.type;
24454 }
24455 else
24456 {
24457 glyph->resolved_level = 0;
24458 glyph->bidi_type = UNKNOWN_BT;
24459 }
24460 ++it->glyph_row->used[area];
24461 }
24462 else
24463 IT_EXPAND_MATRIX_WIDTH (it, area);
24464 }
24465
24466 #endif /* HAVE_WINDOW_SYSTEM */
24467
24468 /* Produce a stretch glyph for iterator IT. IT->object is the value
24469 of the glyph property displayed. The value must be a list
24470 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24471 being recognized:
24472
24473 1. `:width WIDTH' specifies that the space should be WIDTH *
24474 canonical char width wide. WIDTH may be an integer or floating
24475 point number.
24476
24477 2. `:relative-width FACTOR' specifies that the width of the stretch
24478 should be computed from the width of the first character having the
24479 `glyph' property, and should be FACTOR times that width.
24480
24481 3. `:align-to HPOS' specifies that the space should be wide enough
24482 to reach HPOS, a value in canonical character units.
24483
24484 Exactly one of the above pairs must be present.
24485
24486 4. `:height HEIGHT' specifies that the height of the stretch produced
24487 should be HEIGHT, measured in canonical character units.
24488
24489 5. `:relative-height FACTOR' specifies that the height of the
24490 stretch should be FACTOR times the height of the characters having
24491 the glyph property.
24492
24493 Either none or exactly one of 4 or 5 must be present.
24494
24495 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24496 of the stretch should be used for the ascent of the stretch.
24497 ASCENT must be in the range 0 <= ASCENT <= 100. */
24498
24499 void
24500 produce_stretch_glyph (struct it *it)
24501 {
24502 /* (space :width WIDTH :height HEIGHT ...) */
24503 Lisp_Object prop, plist;
24504 int width = 0, height = 0, align_to = -1;
24505 int zero_width_ok_p = 0;
24506 double tem;
24507 struct font *font = NULL;
24508
24509 #ifdef HAVE_WINDOW_SYSTEM
24510 int ascent = 0;
24511 int zero_height_ok_p = 0;
24512
24513 if (FRAME_WINDOW_P (it->f))
24514 {
24515 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24516 font = face->font ? face->font : FRAME_FONT (it->f);
24517 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24518 }
24519 #endif
24520
24521 /* List should start with `space'. */
24522 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24523 plist = XCDR (it->object);
24524
24525 /* Compute the width of the stretch. */
24526 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24527 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24528 {
24529 /* Absolute width `:width WIDTH' specified and valid. */
24530 zero_width_ok_p = 1;
24531 width = (int)tem;
24532 }
24533 #ifdef HAVE_WINDOW_SYSTEM
24534 else if (FRAME_WINDOW_P (it->f)
24535 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24536 {
24537 /* Relative width `:relative-width FACTOR' specified and valid.
24538 Compute the width of the characters having the `glyph'
24539 property. */
24540 struct it it2;
24541 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24542
24543 it2 = *it;
24544 if (it->multibyte_p)
24545 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24546 else
24547 {
24548 it2.c = it2.char_to_display = *p, it2.len = 1;
24549 if (! ASCII_CHAR_P (it2.c))
24550 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24551 }
24552
24553 it2.glyph_row = NULL;
24554 it2.what = IT_CHARACTER;
24555 x_produce_glyphs (&it2);
24556 width = NUMVAL (prop) * it2.pixel_width;
24557 }
24558 #endif /* HAVE_WINDOW_SYSTEM */
24559 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24560 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24561 {
24562 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24563 align_to = (align_to < 0
24564 ? 0
24565 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24566 else if (align_to < 0)
24567 align_to = window_box_left_offset (it->w, TEXT_AREA);
24568 width = max (0, (int)tem + align_to - it->current_x);
24569 zero_width_ok_p = 1;
24570 }
24571 else
24572 /* Nothing specified -> width defaults to canonical char width. */
24573 width = FRAME_COLUMN_WIDTH (it->f);
24574
24575 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24576 width = 1;
24577
24578 #ifdef HAVE_WINDOW_SYSTEM
24579 /* Compute height. */
24580 if (FRAME_WINDOW_P (it->f))
24581 {
24582 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24583 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24584 {
24585 height = (int)tem;
24586 zero_height_ok_p = 1;
24587 }
24588 else if (prop = Fplist_get (plist, QCrelative_height),
24589 NUMVAL (prop) > 0)
24590 height = FONT_HEIGHT (font) * NUMVAL (prop);
24591 else
24592 height = FONT_HEIGHT (font);
24593
24594 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24595 height = 1;
24596
24597 /* Compute percentage of height used for ascent. If
24598 `:ascent ASCENT' is present and valid, use that. Otherwise,
24599 derive the ascent from the font in use. */
24600 if (prop = Fplist_get (plist, QCascent),
24601 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24602 ascent = height * NUMVAL (prop) / 100.0;
24603 else if (!NILP (prop)
24604 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24605 ascent = min (max (0, (int)tem), height);
24606 else
24607 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24608 }
24609 else
24610 #endif /* HAVE_WINDOW_SYSTEM */
24611 height = 1;
24612
24613 if (width > 0 && it->line_wrap != TRUNCATE
24614 && it->current_x + width > it->last_visible_x)
24615 {
24616 width = it->last_visible_x - it->current_x;
24617 #ifdef HAVE_WINDOW_SYSTEM
24618 /* Subtract one more pixel from the stretch width, but only on
24619 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24620 width -= FRAME_WINDOW_P (it->f);
24621 #endif
24622 }
24623
24624 if (width > 0 && height > 0 && it->glyph_row)
24625 {
24626 Lisp_Object o_object = it->object;
24627 Lisp_Object object = it->stack[it->sp - 1].string;
24628 int n = width;
24629
24630 if (!STRINGP (object))
24631 object = it->w->contents;
24632 #ifdef HAVE_WINDOW_SYSTEM
24633 if (FRAME_WINDOW_P (it->f))
24634 append_stretch_glyph (it, object, width, height, ascent);
24635 else
24636 #endif
24637 {
24638 it->object = object;
24639 it->char_to_display = ' ';
24640 it->pixel_width = it->len = 1;
24641 while (n--)
24642 tty_append_glyph (it);
24643 it->object = o_object;
24644 }
24645 }
24646
24647 it->pixel_width = width;
24648 #ifdef HAVE_WINDOW_SYSTEM
24649 if (FRAME_WINDOW_P (it->f))
24650 {
24651 it->ascent = it->phys_ascent = ascent;
24652 it->descent = it->phys_descent = height - it->ascent;
24653 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24654 take_vertical_position_into_account (it);
24655 }
24656 else
24657 #endif
24658 it->nglyphs = width;
24659 }
24660
24661 /* Get information about special display element WHAT in an
24662 environment described by IT. WHAT is one of IT_TRUNCATION or
24663 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24664 non-null glyph_row member. This function ensures that fields like
24665 face_id, c, len of IT are left untouched. */
24666
24667 static void
24668 produce_special_glyphs (struct it *it, enum display_element_type what)
24669 {
24670 struct it temp_it;
24671 Lisp_Object gc;
24672 GLYPH glyph;
24673
24674 temp_it = *it;
24675 temp_it.object = make_number (0);
24676 memset (&temp_it.current, 0, sizeof temp_it.current);
24677
24678 if (what == IT_CONTINUATION)
24679 {
24680 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24681 if (it->bidi_it.paragraph_dir == R2L)
24682 SET_GLYPH_FROM_CHAR (glyph, '/');
24683 else
24684 SET_GLYPH_FROM_CHAR (glyph, '\\');
24685 if (it->dp
24686 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24687 {
24688 /* FIXME: Should we mirror GC for R2L lines? */
24689 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24690 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24691 }
24692 }
24693 else if (what == IT_TRUNCATION)
24694 {
24695 /* Truncation glyph. */
24696 SET_GLYPH_FROM_CHAR (glyph, '$');
24697 if (it->dp
24698 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24699 {
24700 /* FIXME: Should we mirror GC for R2L lines? */
24701 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24702 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24703 }
24704 }
24705 else
24706 emacs_abort ();
24707
24708 #ifdef HAVE_WINDOW_SYSTEM
24709 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24710 is turned off, we precede the truncation/continuation glyphs by a
24711 stretch glyph whose width is computed such that these special
24712 glyphs are aligned at the window margin, even when very different
24713 fonts are used in different glyph rows. */
24714 if (FRAME_WINDOW_P (temp_it.f)
24715 /* init_iterator calls this with it->glyph_row == NULL, and it
24716 wants only the pixel width of the truncation/continuation
24717 glyphs. */
24718 && temp_it.glyph_row
24719 /* insert_left_trunc_glyphs calls us at the beginning of the
24720 row, and it has its own calculation of the stretch glyph
24721 width. */
24722 && temp_it.glyph_row->used[TEXT_AREA] > 0
24723 && (temp_it.glyph_row->reversed_p
24724 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24725 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24726 {
24727 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24728
24729 if (stretch_width > 0)
24730 {
24731 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24732 struct font *font =
24733 face->font ? face->font : FRAME_FONT (temp_it.f);
24734 int stretch_ascent =
24735 (((temp_it.ascent + temp_it.descent)
24736 * FONT_BASE (font)) / FONT_HEIGHT (font));
24737
24738 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24739 temp_it.ascent + temp_it.descent,
24740 stretch_ascent);
24741 }
24742 }
24743 #endif
24744
24745 temp_it.dp = NULL;
24746 temp_it.what = IT_CHARACTER;
24747 temp_it.len = 1;
24748 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24749 temp_it.face_id = GLYPH_FACE (glyph);
24750 temp_it.len = CHAR_BYTES (temp_it.c);
24751
24752 PRODUCE_GLYPHS (&temp_it);
24753 it->pixel_width = temp_it.pixel_width;
24754 it->nglyphs = temp_it.pixel_width;
24755 }
24756
24757 #ifdef HAVE_WINDOW_SYSTEM
24758
24759 /* Calculate line-height and line-spacing properties.
24760 An integer value specifies explicit pixel value.
24761 A float value specifies relative value to current face height.
24762 A cons (float . face-name) specifies relative value to
24763 height of specified face font.
24764
24765 Returns height in pixels, or nil. */
24766
24767
24768 static Lisp_Object
24769 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24770 int boff, int override)
24771 {
24772 Lisp_Object face_name = Qnil;
24773 int ascent, descent, height;
24774
24775 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24776 return val;
24777
24778 if (CONSP (val))
24779 {
24780 face_name = XCAR (val);
24781 val = XCDR (val);
24782 if (!NUMBERP (val))
24783 val = make_number (1);
24784 if (NILP (face_name))
24785 {
24786 height = it->ascent + it->descent;
24787 goto scale;
24788 }
24789 }
24790
24791 if (NILP (face_name))
24792 {
24793 font = FRAME_FONT (it->f);
24794 boff = FRAME_BASELINE_OFFSET (it->f);
24795 }
24796 else if (EQ (face_name, Qt))
24797 {
24798 override = 0;
24799 }
24800 else
24801 {
24802 int face_id;
24803 struct face *face;
24804
24805 face_id = lookup_named_face (it->f, face_name, 0);
24806 if (face_id < 0)
24807 return make_number (-1);
24808
24809 face = FACE_FROM_ID (it->f, face_id);
24810 font = face->font;
24811 if (font == NULL)
24812 return make_number (-1);
24813 boff = font->baseline_offset;
24814 if (font->vertical_centering)
24815 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24816 }
24817
24818 ascent = FONT_BASE (font) + boff;
24819 descent = FONT_DESCENT (font) - boff;
24820
24821 if (override)
24822 {
24823 it->override_ascent = ascent;
24824 it->override_descent = descent;
24825 it->override_boff = boff;
24826 }
24827
24828 height = ascent + descent;
24829
24830 scale:
24831 if (FLOATP (val))
24832 height = (int)(XFLOAT_DATA (val) * height);
24833 else if (INTEGERP (val))
24834 height *= XINT (val);
24835
24836 return make_number (height);
24837 }
24838
24839
24840 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24841 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24842 and only if this is for a character for which no font was found.
24843
24844 If the display method (it->glyphless_method) is
24845 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24846 length of the acronym or the hexadecimal string, UPPER_XOFF and
24847 UPPER_YOFF are pixel offsets for the upper part of the string,
24848 LOWER_XOFF and LOWER_YOFF are for the lower part.
24849
24850 For the other display methods, LEN through LOWER_YOFF are zero. */
24851
24852 static void
24853 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24854 short upper_xoff, short upper_yoff,
24855 short lower_xoff, short lower_yoff)
24856 {
24857 struct glyph *glyph;
24858 enum glyph_row_area area = it->area;
24859
24860 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24861 if (glyph < it->glyph_row->glyphs[area + 1])
24862 {
24863 /* If the glyph row is reversed, we need to prepend the glyph
24864 rather than append it. */
24865 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24866 {
24867 struct glyph *g;
24868
24869 /* Make room for the additional glyph. */
24870 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24871 g[1] = *g;
24872 glyph = it->glyph_row->glyphs[area];
24873 }
24874 glyph->charpos = CHARPOS (it->position);
24875 glyph->object = it->object;
24876 glyph->pixel_width = it->pixel_width;
24877 glyph->ascent = it->ascent;
24878 glyph->descent = it->descent;
24879 glyph->voffset = it->voffset;
24880 glyph->type = GLYPHLESS_GLYPH;
24881 glyph->u.glyphless.method = it->glyphless_method;
24882 glyph->u.glyphless.for_no_font = for_no_font;
24883 glyph->u.glyphless.len = len;
24884 glyph->u.glyphless.ch = it->c;
24885 glyph->slice.glyphless.upper_xoff = upper_xoff;
24886 glyph->slice.glyphless.upper_yoff = upper_yoff;
24887 glyph->slice.glyphless.lower_xoff = lower_xoff;
24888 glyph->slice.glyphless.lower_yoff = lower_yoff;
24889 glyph->avoid_cursor_p = it->avoid_cursor_p;
24890 glyph->multibyte_p = it->multibyte_p;
24891 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24892 {
24893 /* In R2L rows, the left and the right box edges need to be
24894 drawn in reverse direction. */
24895 glyph->right_box_line_p = it->start_of_box_run_p;
24896 glyph->left_box_line_p = it->end_of_box_run_p;
24897 }
24898 else
24899 {
24900 glyph->left_box_line_p = it->start_of_box_run_p;
24901 glyph->right_box_line_p = it->end_of_box_run_p;
24902 }
24903 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24904 || it->phys_descent > it->descent);
24905 glyph->padding_p = 0;
24906 glyph->glyph_not_available_p = 0;
24907 glyph->face_id = face_id;
24908 glyph->font_type = FONT_TYPE_UNKNOWN;
24909 if (it->bidi_p)
24910 {
24911 glyph->resolved_level = it->bidi_it.resolved_level;
24912 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24913 emacs_abort ();
24914 glyph->bidi_type = it->bidi_it.type;
24915 }
24916 ++it->glyph_row->used[area];
24917 }
24918 else
24919 IT_EXPAND_MATRIX_WIDTH (it, area);
24920 }
24921
24922
24923 /* Produce a glyph for a glyphless character for iterator IT.
24924 IT->glyphless_method specifies which method to use for displaying
24925 the character. See the description of enum
24926 glyphless_display_method in dispextern.h for the detail.
24927
24928 FOR_NO_FONT is nonzero if and only if this is for a character for
24929 which no font was found. ACRONYM, if non-nil, is an acronym string
24930 for the character. */
24931
24932 static void
24933 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24934 {
24935 int face_id;
24936 struct face *face;
24937 struct font *font;
24938 int base_width, base_height, width, height;
24939 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24940 int len;
24941
24942 /* Get the metrics of the base font. We always refer to the current
24943 ASCII face. */
24944 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24945 font = face->font ? face->font : FRAME_FONT (it->f);
24946 it->ascent = FONT_BASE (font) + font->baseline_offset;
24947 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24948 base_height = it->ascent + it->descent;
24949 base_width = font->average_width;
24950
24951 /* Get a face ID for the glyph by utilizing a cache (the same way as
24952 done for `escape-glyph' in get_next_display_element). */
24953 if (it->f == last_glyphless_glyph_frame
24954 && it->face_id == last_glyphless_glyph_face_id)
24955 {
24956 face_id = last_glyphless_glyph_merged_face_id;
24957 }
24958 else
24959 {
24960 /* Merge the `glyphless-char' face into the current face. */
24961 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24962 last_glyphless_glyph_frame = it->f;
24963 last_glyphless_glyph_face_id = it->face_id;
24964 last_glyphless_glyph_merged_face_id = face_id;
24965 }
24966
24967 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24968 {
24969 it->pixel_width = THIN_SPACE_WIDTH;
24970 len = 0;
24971 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24972 }
24973 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24974 {
24975 width = CHAR_WIDTH (it->c);
24976 if (width == 0)
24977 width = 1;
24978 else if (width > 4)
24979 width = 4;
24980 it->pixel_width = base_width * width;
24981 len = 0;
24982 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24983 }
24984 else
24985 {
24986 char buf[7];
24987 const char *str;
24988 unsigned int code[6];
24989 int upper_len;
24990 int ascent, descent;
24991 struct font_metrics metrics_upper, metrics_lower;
24992
24993 face = FACE_FROM_ID (it->f, face_id);
24994 font = face->font ? face->font : FRAME_FONT (it->f);
24995 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24996
24997 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24998 {
24999 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25000 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25001 if (CONSP (acronym))
25002 acronym = XCAR (acronym);
25003 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25004 }
25005 else
25006 {
25007 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25008 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25009 str = buf;
25010 }
25011 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25012 code[len] = font->driver->encode_char (font, str[len]);
25013 upper_len = (len + 1) / 2;
25014 font->driver->text_extents (font, code, upper_len,
25015 &metrics_upper);
25016 font->driver->text_extents (font, code + upper_len, len - upper_len,
25017 &metrics_lower);
25018
25019
25020
25021 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25022 width = max (metrics_upper.width, metrics_lower.width) + 4;
25023 upper_xoff = upper_yoff = 2; /* the typical case */
25024 if (base_width >= width)
25025 {
25026 /* Align the upper to the left, the lower to the right. */
25027 it->pixel_width = base_width;
25028 lower_xoff = base_width - 2 - metrics_lower.width;
25029 }
25030 else
25031 {
25032 /* Center the shorter one. */
25033 it->pixel_width = width;
25034 if (metrics_upper.width >= metrics_lower.width)
25035 lower_xoff = (width - metrics_lower.width) / 2;
25036 else
25037 {
25038 /* FIXME: This code doesn't look right. It formerly was
25039 missing the "lower_xoff = 0;", which couldn't have
25040 been right since it left lower_xoff uninitialized. */
25041 lower_xoff = 0;
25042 upper_xoff = (width - metrics_upper.width) / 2;
25043 }
25044 }
25045
25046 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25047 top, bottom, and between upper and lower strings. */
25048 height = (metrics_upper.ascent + metrics_upper.descent
25049 + metrics_lower.ascent + metrics_lower.descent) + 5;
25050 /* Center vertically.
25051 H:base_height, D:base_descent
25052 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25053
25054 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25055 descent = D - H/2 + h/2;
25056 lower_yoff = descent - 2 - ld;
25057 upper_yoff = lower_yoff - la - 1 - ud; */
25058 ascent = - (it->descent - (base_height + height + 1) / 2);
25059 descent = it->descent - (base_height - height) / 2;
25060 lower_yoff = descent - 2 - metrics_lower.descent;
25061 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25062 - metrics_upper.descent);
25063 /* Don't make the height shorter than the base height. */
25064 if (height > base_height)
25065 {
25066 it->ascent = ascent;
25067 it->descent = descent;
25068 }
25069 }
25070
25071 it->phys_ascent = it->ascent;
25072 it->phys_descent = it->descent;
25073 if (it->glyph_row)
25074 append_glyphless_glyph (it, face_id, for_no_font, len,
25075 upper_xoff, upper_yoff,
25076 lower_xoff, lower_yoff);
25077 it->nglyphs = 1;
25078 take_vertical_position_into_account (it);
25079 }
25080
25081
25082 /* RIF:
25083 Produce glyphs/get display metrics for the display element IT is
25084 loaded with. See the description of struct it in dispextern.h
25085 for an overview of struct it. */
25086
25087 void
25088 x_produce_glyphs (struct it *it)
25089 {
25090 int extra_line_spacing = it->extra_line_spacing;
25091
25092 it->glyph_not_available_p = 0;
25093
25094 if (it->what == IT_CHARACTER)
25095 {
25096 XChar2b char2b;
25097 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25098 struct font *font = face->font;
25099 struct font_metrics *pcm = NULL;
25100 int boff; /* baseline offset */
25101
25102 if (font == NULL)
25103 {
25104 /* When no suitable font is found, display this character by
25105 the method specified in the first extra slot of
25106 Vglyphless_char_display. */
25107 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25108
25109 eassert (it->what == IT_GLYPHLESS);
25110 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25111 goto done;
25112 }
25113
25114 boff = font->baseline_offset;
25115 if (font->vertical_centering)
25116 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25117
25118 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25119 {
25120 int stretched_p;
25121
25122 it->nglyphs = 1;
25123
25124 if (it->override_ascent >= 0)
25125 {
25126 it->ascent = it->override_ascent;
25127 it->descent = it->override_descent;
25128 boff = it->override_boff;
25129 }
25130 else
25131 {
25132 it->ascent = FONT_BASE (font) + boff;
25133 it->descent = FONT_DESCENT (font) - boff;
25134 }
25135
25136 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25137 {
25138 pcm = get_per_char_metric (font, &char2b);
25139 if (pcm->width == 0
25140 && pcm->rbearing == 0 && pcm->lbearing == 0)
25141 pcm = NULL;
25142 }
25143
25144 if (pcm)
25145 {
25146 it->phys_ascent = pcm->ascent + boff;
25147 it->phys_descent = pcm->descent - boff;
25148 it->pixel_width = pcm->width;
25149 }
25150 else
25151 {
25152 it->glyph_not_available_p = 1;
25153 it->phys_ascent = it->ascent;
25154 it->phys_descent = it->descent;
25155 it->pixel_width = font->space_width;
25156 }
25157
25158 if (it->constrain_row_ascent_descent_p)
25159 {
25160 if (it->descent > it->max_descent)
25161 {
25162 it->ascent += it->descent - it->max_descent;
25163 it->descent = it->max_descent;
25164 }
25165 if (it->ascent > it->max_ascent)
25166 {
25167 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25168 it->ascent = it->max_ascent;
25169 }
25170 it->phys_ascent = min (it->phys_ascent, it->ascent);
25171 it->phys_descent = min (it->phys_descent, it->descent);
25172 extra_line_spacing = 0;
25173 }
25174
25175 /* If this is a space inside a region of text with
25176 `space-width' property, change its width. */
25177 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25178 if (stretched_p)
25179 it->pixel_width *= XFLOATINT (it->space_width);
25180
25181 /* If face has a box, add the box thickness to the character
25182 height. If character has a box line to the left and/or
25183 right, add the box line width to the character's width. */
25184 if (face->box != FACE_NO_BOX)
25185 {
25186 int thick = face->box_line_width;
25187
25188 if (thick > 0)
25189 {
25190 it->ascent += thick;
25191 it->descent += thick;
25192 }
25193 else
25194 thick = -thick;
25195
25196 if (it->start_of_box_run_p)
25197 it->pixel_width += thick;
25198 if (it->end_of_box_run_p)
25199 it->pixel_width += thick;
25200 }
25201
25202 /* If face has an overline, add the height of the overline
25203 (1 pixel) and a 1 pixel margin to the character height. */
25204 if (face->overline_p)
25205 it->ascent += overline_margin;
25206
25207 if (it->constrain_row_ascent_descent_p)
25208 {
25209 if (it->ascent > it->max_ascent)
25210 it->ascent = it->max_ascent;
25211 if (it->descent > it->max_descent)
25212 it->descent = it->max_descent;
25213 }
25214
25215 take_vertical_position_into_account (it);
25216
25217 /* If we have to actually produce glyphs, do it. */
25218 if (it->glyph_row)
25219 {
25220 if (stretched_p)
25221 {
25222 /* Translate a space with a `space-width' property
25223 into a stretch glyph. */
25224 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25225 / FONT_HEIGHT (font));
25226 append_stretch_glyph (it, it->object, it->pixel_width,
25227 it->ascent + it->descent, ascent);
25228 }
25229 else
25230 append_glyph (it);
25231
25232 /* If characters with lbearing or rbearing are displayed
25233 in this line, record that fact in a flag of the
25234 glyph row. This is used to optimize X output code. */
25235 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25236 it->glyph_row->contains_overlapping_glyphs_p = 1;
25237 }
25238 if (! stretched_p && it->pixel_width == 0)
25239 /* We assure that all visible glyphs have at least 1-pixel
25240 width. */
25241 it->pixel_width = 1;
25242 }
25243 else if (it->char_to_display == '\n')
25244 {
25245 /* A newline has no width, but we need the height of the
25246 line. But if previous part of the line sets a height,
25247 don't increase that height */
25248
25249 Lisp_Object height;
25250 Lisp_Object total_height = Qnil;
25251
25252 it->override_ascent = -1;
25253 it->pixel_width = 0;
25254 it->nglyphs = 0;
25255
25256 height = get_it_property (it, Qline_height);
25257 /* Split (line-height total-height) list */
25258 if (CONSP (height)
25259 && CONSP (XCDR (height))
25260 && NILP (XCDR (XCDR (height))))
25261 {
25262 total_height = XCAR (XCDR (height));
25263 height = XCAR (height);
25264 }
25265 height = calc_line_height_property (it, height, font, boff, 1);
25266
25267 if (it->override_ascent >= 0)
25268 {
25269 it->ascent = it->override_ascent;
25270 it->descent = it->override_descent;
25271 boff = it->override_boff;
25272 }
25273 else
25274 {
25275 it->ascent = FONT_BASE (font) + boff;
25276 it->descent = FONT_DESCENT (font) - boff;
25277 }
25278
25279 if (EQ (height, Qt))
25280 {
25281 if (it->descent > it->max_descent)
25282 {
25283 it->ascent += it->descent - it->max_descent;
25284 it->descent = it->max_descent;
25285 }
25286 if (it->ascent > it->max_ascent)
25287 {
25288 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25289 it->ascent = it->max_ascent;
25290 }
25291 it->phys_ascent = min (it->phys_ascent, it->ascent);
25292 it->phys_descent = min (it->phys_descent, it->descent);
25293 it->constrain_row_ascent_descent_p = 1;
25294 extra_line_spacing = 0;
25295 }
25296 else
25297 {
25298 Lisp_Object spacing;
25299
25300 it->phys_ascent = it->ascent;
25301 it->phys_descent = it->descent;
25302
25303 if ((it->max_ascent > 0 || it->max_descent > 0)
25304 && face->box != FACE_NO_BOX
25305 && face->box_line_width > 0)
25306 {
25307 it->ascent += face->box_line_width;
25308 it->descent += face->box_line_width;
25309 }
25310 if (!NILP (height)
25311 && XINT (height) > it->ascent + it->descent)
25312 it->ascent = XINT (height) - it->descent;
25313
25314 if (!NILP (total_height))
25315 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25316 else
25317 {
25318 spacing = get_it_property (it, Qline_spacing);
25319 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25320 }
25321 if (INTEGERP (spacing))
25322 {
25323 extra_line_spacing = XINT (spacing);
25324 if (!NILP (total_height))
25325 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25326 }
25327 }
25328 }
25329 else /* i.e. (it->char_to_display == '\t') */
25330 {
25331 if (font->space_width > 0)
25332 {
25333 int tab_width = it->tab_width * font->space_width;
25334 int x = it->current_x + it->continuation_lines_width;
25335 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25336
25337 /* If the distance from the current position to the next tab
25338 stop is less than a space character width, use the
25339 tab stop after that. */
25340 if (next_tab_x - x < font->space_width)
25341 next_tab_x += tab_width;
25342
25343 it->pixel_width = next_tab_x - x;
25344 it->nglyphs = 1;
25345 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25346 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25347
25348 if (it->glyph_row)
25349 {
25350 append_stretch_glyph (it, it->object, it->pixel_width,
25351 it->ascent + it->descent, it->ascent);
25352 }
25353 }
25354 else
25355 {
25356 it->pixel_width = 0;
25357 it->nglyphs = 1;
25358 }
25359 }
25360 }
25361 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25362 {
25363 /* A static composition.
25364
25365 Note: A composition is represented as one glyph in the
25366 glyph matrix. There are no padding glyphs.
25367
25368 Important note: pixel_width, ascent, and descent are the
25369 values of what is drawn by draw_glyphs (i.e. the values of
25370 the overall glyphs composed). */
25371 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25372 int boff; /* baseline offset */
25373 struct composition *cmp = composition_table[it->cmp_it.id];
25374 int glyph_len = cmp->glyph_len;
25375 struct font *font = face->font;
25376
25377 it->nglyphs = 1;
25378
25379 /* If we have not yet calculated pixel size data of glyphs of
25380 the composition for the current face font, calculate them
25381 now. Theoretically, we have to check all fonts for the
25382 glyphs, but that requires much time and memory space. So,
25383 here we check only the font of the first glyph. This may
25384 lead to incorrect display, but it's very rare, and C-l
25385 (recenter-top-bottom) can correct the display anyway. */
25386 if (! cmp->font || cmp->font != font)
25387 {
25388 /* Ascent and descent of the font of the first character
25389 of this composition (adjusted by baseline offset).
25390 Ascent and descent of overall glyphs should not be less
25391 than these, respectively. */
25392 int font_ascent, font_descent, font_height;
25393 /* Bounding box of the overall glyphs. */
25394 int leftmost, rightmost, lowest, highest;
25395 int lbearing, rbearing;
25396 int i, width, ascent, descent;
25397 int left_padded = 0, right_padded = 0;
25398 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25399 XChar2b char2b;
25400 struct font_metrics *pcm;
25401 int font_not_found_p;
25402 ptrdiff_t pos;
25403
25404 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25405 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25406 break;
25407 if (glyph_len < cmp->glyph_len)
25408 right_padded = 1;
25409 for (i = 0; i < glyph_len; i++)
25410 {
25411 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25412 break;
25413 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25414 }
25415 if (i > 0)
25416 left_padded = 1;
25417
25418 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25419 : IT_CHARPOS (*it));
25420 /* If no suitable font is found, use the default font. */
25421 font_not_found_p = font == NULL;
25422 if (font_not_found_p)
25423 {
25424 face = face->ascii_face;
25425 font = face->font;
25426 }
25427 boff = font->baseline_offset;
25428 if (font->vertical_centering)
25429 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25430 font_ascent = FONT_BASE (font) + boff;
25431 font_descent = FONT_DESCENT (font) - boff;
25432 font_height = FONT_HEIGHT (font);
25433
25434 cmp->font = font;
25435
25436 pcm = NULL;
25437 if (! font_not_found_p)
25438 {
25439 get_char_face_and_encoding (it->f, c, it->face_id,
25440 &char2b, 0);
25441 pcm = get_per_char_metric (font, &char2b);
25442 }
25443
25444 /* Initialize the bounding box. */
25445 if (pcm)
25446 {
25447 width = cmp->glyph_len > 0 ? pcm->width : 0;
25448 ascent = pcm->ascent;
25449 descent = pcm->descent;
25450 lbearing = pcm->lbearing;
25451 rbearing = pcm->rbearing;
25452 }
25453 else
25454 {
25455 width = cmp->glyph_len > 0 ? font->space_width : 0;
25456 ascent = FONT_BASE (font);
25457 descent = FONT_DESCENT (font);
25458 lbearing = 0;
25459 rbearing = width;
25460 }
25461
25462 rightmost = width;
25463 leftmost = 0;
25464 lowest = - descent + boff;
25465 highest = ascent + boff;
25466
25467 if (! font_not_found_p
25468 && font->default_ascent
25469 && CHAR_TABLE_P (Vuse_default_ascent)
25470 && !NILP (Faref (Vuse_default_ascent,
25471 make_number (it->char_to_display))))
25472 highest = font->default_ascent + boff;
25473
25474 /* Draw the first glyph at the normal position. It may be
25475 shifted to right later if some other glyphs are drawn
25476 at the left. */
25477 cmp->offsets[i * 2] = 0;
25478 cmp->offsets[i * 2 + 1] = boff;
25479 cmp->lbearing = lbearing;
25480 cmp->rbearing = rbearing;
25481
25482 /* Set cmp->offsets for the remaining glyphs. */
25483 for (i++; i < glyph_len; i++)
25484 {
25485 int left, right, btm, top;
25486 int ch = COMPOSITION_GLYPH (cmp, i);
25487 int face_id;
25488 struct face *this_face;
25489
25490 if (ch == '\t')
25491 ch = ' ';
25492 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25493 this_face = FACE_FROM_ID (it->f, face_id);
25494 font = this_face->font;
25495
25496 if (font == NULL)
25497 pcm = NULL;
25498 else
25499 {
25500 get_char_face_and_encoding (it->f, ch, face_id,
25501 &char2b, 0);
25502 pcm = get_per_char_metric (font, &char2b);
25503 }
25504 if (! pcm)
25505 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25506 else
25507 {
25508 width = pcm->width;
25509 ascent = pcm->ascent;
25510 descent = pcm->descent;
25511 lbearing = pcm->lbearing;
25512 rbearing = pcm->rbearing;
25513 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25514 {
25515 /* Relative composition with or without
25516 alternate chars. */
25517 left = (leftmost + rightmost - width) / 2;
25518 btm = - descent + boff;
25519 if (font->relative_compose
25520 && (! CHAR_TABLE_P (Vignore_relative_composition)
25521 || NILP (Faref (Vignore_relative_composition,
25522 make_number (ch)))))
25523 {
25524
25525 if (- descent >= font->relative_compose)
25526 /* One extra pixel between two glyphs. */
25527 btm = highest + 1;
25528 else if (ascent <= 0)
25529 /* One extra pixel between two glyphs. */
25530 btm = lowest - 1 - ascent - descent;
25531 }
25532 }
25533 else
25534 {
25535 /* A composition rule is specified by an integer
25536 value that encodes global and new reference
25537 points (GREF and NREF). GREF and NREF are
25538 specified by numbers as below:
25539
25540 0---1---2 -- ascent
25541 | |
25542 | |
25543 | |
25544 9--10--11 -- center
25545 | |
25546 ---3---4---5--- baseline
25547 | |
25548 6---7---8 -- descent
25549 */
25550 int rule = COMPOSITION_RULE (cmp, i);
25551 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25552
25553 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25554 grefx = gref % 3, nrefx = nref % 3;
25555 grefy = gref / 3, nrefy = nref / 3;
25556 if (xoff)
25557 xoff = font_height * (xoff - 128) / 256;
25558 if (yoff)
25559 yoff = font_height * (yoff - 128) / 256;
25560
25561 left = (leftmost
25562 + grefx * (rightmost - leftmost) / 2
25563 - nrefx * width / 2
25564 + xoff);
25565
25566 btm = ((grefy == 0 ? highest
25567 : grefy == 1 ? 0
25568 : grefy == 2 ? lowest
25569 : (highest + lowest) / 2)
25570 - (nrefy == 0 ? ascent + descent
25571 : nrefy == 1 ? descent - boff
25572 : nrefy == 2 ? 0
25573 : (ascent + descent) / 2)
25574 + yoff);
25575 }
25576
25577 cmp->offsets[i * 2] = left;
25578 cmp->offsets[i * 2 + 1] = btm + descent;
25579
25580 /* Update the bounding box of the overall glyphs. */
25581 if (width > 0)
25582 {
25583 right = left + width;
25584 if (left < leftmost)
25585 leftmost = left;
25586 if (right > rightmost)
25587 rightmost = right;
25588 }
25589 top = btm + descent + ascent;
25590 if (top > highest)
25591 highest = top;
25592 if (btm < lowest)
25593 lowest = btm;
25594
25595 if (cmp->lbearing > left + lbearing)
25596 cmp->lbearing = left + lbearing;
25597 if (cmp->rbearing < left + rbearing)
25598 cmp->rbearing = left + rbearing;
25599 }
25600 }
25601
25602 /* If there are glyphs whose x-offsets are negative,
25603 shift all glyphs to the right and make all x-offsets
25604 non-negative. */
25605 if (leftmost < 0)
25606 {
25607 for (i = 0; i < cmp->glyph_len; i++)
25608 cmp->offsets[i * 2] -= leftmost;
25609 rightmost -= leftmost;
25610 cmp->lbearing -= leftmost;
25611 cmp->rbearing -= leftmost;
25612 }
25613
25614 if (left_padded && cmp->lbearing < 0)
25615 {
25616 for (i = 0; i < cmp->glyph_len; i++)
25617 cmp->offsets[i * 2] -= cmp->lbearing;
25618 rightmost -= cmp->lbearing;
25619 cmp->rbearing -= cmp->lbearing;
25620 cmp->lbearing = 0;
25621 }
25622 if (right_padded && rightmost < cmp->rbearing)
25623 {
25624 rightmost = cmp->rbearing;
25625 }
25626
25627 cmp->pixel_width = rightmost;
25628 cmp->ascent = highest;
25629 cmp->descent = - lowest;
25630 if (cmp->ascent < font_ascent)
25631 cmp->ascent = font_ascent;
25632 if (cmp->descent < font_descent)
25633 cmp->descent = font_descent;
25634 }
25635
25636 if (it->glyph_row
25637 && (cmp->lbearing < 0
25638 || cmp->rbearing > cmp->pixel_width))
25639 it->glyph_row->contains_overlapping_glyphs_p = 1;
25640
25641 it->pixel_width = cmp->pixel_width;
25642 it->ascent = it->phys_ascent = cmp->ascent;
25643 it->descent = it->phys_descent = cmp->descent;
25644 if (face->box != FACE_NO_BOX)
25645 {
25646 int thick = face->box_line_width;
25647
25648 if (thick > 0)
25649 {
25650 it->ascent += thick;
25651 it->descent += thick;
25652 }
25653 else
25654 thick = - thick;
25655
25656 if (it->start_of_box_run_p)
25657 it->pixel_width += thick;
25658 if (it->end_of_box_run_p)
25659 it->pixel_width += thick;
25660 }
25661
25662 /* If face has an overline, add the height of the overline
25663 (1 pixel) and a 1 pixel margin to the character height. */
25664 if (face->overline_p)
25665 it->ascent += overline_margin;
25666
25667 take_vertical_position_into_account (it);
25668 if (it->ascent < 0)
25669 it->ascent = 0;
25670 if (it->descent < 0)
25671 it->descent = 0;
25672
25673 if (it->glyph_row && cmp->glyph_len > 0)
25674 append_composite_glyph (it);
25675 }
25676 else if (it->what == IT_COMPOSITION)
25677 {
25678 /* A dynamic (automatic) composition. */
25679 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25680 Lisp_Object gstring;
25681 struct font_metrics metrics;
25682
25683 it->nglyphs = 1;
25684
25685 gstring = composition_gstring_from_id (it->cmp_it.id);
25686 it->pixel_width
25687 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25688 &metrics);
25689 if (it->glyph_row
25690 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25691 it->glyph_row->contains_overlapping_glyphs_p = 1;
25692 it->ascent = it->phys_ascent = metrics.ascent;
25693 it->descent = it->phys_descent = metrics.descent;
25694 if (face->box != FACE_NO_BOX)
25695 {
25696 int thick = face->box_line_width;
25697
25698 if (thick > 0)
25699 {
25700 it->ascent += thick;
25701 it->descent += thick;
25702 }
25703 else
25704 thick = - thick;
25705
25706 if (it->start_of_box_run_p)
25707 it->pixel_width += thick;
25708 if (it->end_of_box_run_p)
25709 it->pixel_width += thick;
25710 }
25711 /* If face has an overline, add the height of the overline
25712 (1 pixel) and a 1 pixel margin to the character height. */
25713 if (face->overline_p)
25714 it->ascent += overline_margin;
25715 take_vertical_position_into_account (it);
25716 if (it->ascent < 0)
25717 it->ascent = 0;
25718 if (it->descent < 0)
25719 it->descent = 0;
25720
25721 if (it->glyph_row)
25722 append_composite_glyph (it);
25723 }
25724 else if (it->what == IT_GLYPHLESS)
25725 produce_glyphless_glyph (it, 0, Qnil);
25726 else if (it->what == IT_IMAGE)
25727 produce_image_glyph (it);
25728 else if (it->what == IT_STRETCH)
25729 produce_stretch_glyph (it);
25730
25731 done:
25732 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25733 because this isn't true for images with `:ascent 100'. */
25734 eassert (it->ascent >= 0 && it->descent >= 0);
25735 if (it->area == TEXT_AREA)
25736 it->current_x += it->pixel_width;
25737
25738 if (extra_line_spacing > 0)
25739 {
25740 it->descent += extra_line_spacing;
25741 if (extra_line_spacing > it->max_extra_line_spacing)
25742 it->max_extra_line_spacing = extra_line_spacing;
25743 }
25744
25745 it->max_ascent = max (it->max_ascent, it->ascent);
25746 it->max_descent = max (it->max_descent, it->descent);
25747 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25748 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25749 }
25750
25751 /* EXPORT for RIF:
25752 Output LEN glyphs starting at START at the nominal cursor position.
25753 Advance the nominal cursor over the text. The global variable
25754 updated_row is the glyph row being updated, and updated_area is the
25755 area of that row being updated. */
25756
25757 void
25758 x_write_glyphs (struct window *w, struct glyph *start, int len)
25759 {
25760 int x, hpos, chpos = w->phys_cursor.hpos;
25761
25762 eassert (updated_row);
25763 /* When the window is hscrolled, cursor hpos can legitimately be out
25764 of bounds, but we draw the cursor at the corresponding window
25765 margin in that case. */
25766 if (!updated_row->reversed_p && chpos < 0)
25767 chpos = 0;
25768 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25769 chpos = updated_row->used[TEXT_AREA] - 1;
25770
25771 block_input ();
25772
25773 /* Write glyphs. */
25774
25775 hpos = start - updated_row->glyphs[updated_area];
25776 x = draw_glyphs (w, output_cursor.x,
25777 updated_row, updated_area,
25778 hpos, hpos + len,
25779 DRAW_NORMAL_TEXT, 0);
25780
25781 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25782 if (updated_area == TEXT_AREA
25783 && w->phys_cursor_on_p
25784 && w->phys_cursor.vpos == output_cursor.vpos
25785 && chpos >= hpos
25786 && chpos < hpos + len)
25787 w->phys_cursor_on_p = 0;
25788
25789 unblock_input ();
25790
25791 /* Advance the output cursor. */
25792 output_cursor.hpos += len;
25793 output_cursor.x = x;
25794 }
25795
25796
25797 /* EXPORT for RIF:
25798 Insert LEN glyphs from START at the nominal cursor position. */
25799
25800 void
25801 x_insert_glyphs (struct window *w, struct glyph *start, int len)
25802 {
25803 struct frame *f;
25804 int line_height, shift_by_width, shifted_region_width;
25805 struct glyph_row *row;
25806 struct glyph *glyph;
25807 int frame_x, frame_y;
25808 ptrdiff_t hpos;
25809
25810 eassert (updated_row);
25811 block_input ();
25812 f = XFRAME (WINDOW_FRAME (w));
25813
25814 /* Get the height of the line we are in. */
25815 row = updated_row;
25816 line_height = row->height;
25817
25818 /* Get the width of the glyphs to insert. */
25819 shift_by_width = 0;
25820 for (glyph = start; glyph < start + len; ++glyph)
25821 shift_by_width += glyph->pixel_width;
25822
25823 /* Get the width of the region to shift right. */
25824 shifted_region_width = (window_box_width (w, updated_area)
25825 - output_cursor.x
25826 - shift_by_width);
25827
25828 /* Shift right. */
25829 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25830 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25831
25832 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25833 line_height, shift_by_width);
25834
25835 /* Write the glyphs. */
25836 hpos = start - row->glyphs[updated_area];
25837 draw_glyphs (w, output_cursor.x, row, updated_area,
25838 hpos, hpos + len,
25839 DRAW_NORMAL_TEXT, 0);
25840
25841 /* Advance the output cursor. */
25842 output_cursor.hpos += len;
25843 output_cursor.x += shift_by_width;
25844 unblock_input ();
25845 }
25846
25847
25848 /* EXPORT for RIF:
25849 Erase the current text line from the nominal cursor position
25850 (inclusive) to pixel column TO_X (exclusive). The idea is that
25851 everything from TO_X onward is already erased.
25852
25853 TO_X is a pixel position relative to updated_area of currently
25854 updated window W. TO_X == -1 means clear to the end of this area. */
25855
25856 void
25857 x_clear_end_of_line (struct window *w, int to_x)
25858 {
25859 struct frame *f;
25860 int max_x, min_y, max_y;
25861 int from_x, from_y, to_y;
25862
25863 eassert (updated_row);
25864 f = XFRAME (w->frame);
25865
25866 if (updated_row->full_width_p)
25867 max_x = WINDOW_TOTAL_WIDTH (w);
25868 else
25869 max_x = window_box_width (w, updated_area);
25870 max_y = window_text_bottom_y (w);
25871
25872 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25873 of window. For TO_X > 0, truncate to end of drawing area. */
25874 if (to_x == 0)
25875 return;
25876 else if (to_x < 0)
25877 to_x = max_x;
25878 else
25879 to_x = min (to_x, max_x);
25880
25881 to_y = min (max_y, output_cursor.y + updated_row->height);
25882
25883 /* Notice if the cursor will be cleared by this operation. */
25884 if (!updated_row->full_width_p)
25885 notice_overwritten_cursor (w, updated_area,
25886 output_cursor.x, -1,
25887 updated_row->y,
25888 MATRIX_ROW_BOTTOM_Y (updated_row));
25889
25890 from_x = output_cursor.x;
25891
25892 /* Translate to frame coordinates. */
25893 if (updated_row->full_width_p)
25894 {
25895 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25896 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25897 }
25898 else
25899 {
25900 int area_left = window_box_left (w, updated_area);
25901 from_x += area_left;
25902 to_x += area_left;
25903 }
25904
25905 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25906 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25907 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25908
25909 /* Prevent inadvertently clearing to end of the X window. */
25910 if (to_x > from_x && to_y > from_y)
25911 {
25912 block_input ();
25913 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25914 to_x - from_x, to_y - from_y);
25915 unblock_input ();
25916 }
25917 }
25918
25919 #endif /* HAVE_WINDOW_SYSTEM */
25920
25921
25922 \f
25923 /***********************************************************************
25924 Cursor types
25925 ***********************************************************************/
25926
25927 /* Value is the internal representation of the specified cursor type
25928 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25929 of the bar cursor. */
25930
25931 static enum text_cursor_kinds
25932 get_specified_cursor_type (Lisp_Object arg, int *width)
25933 {
25934 enum text_cursor_kinds type;
25935
25936 if (NILP (arg))
25937 return NO_CURSOR;
25938
25939 if (EQ (arg, Qbox))
25940 return FILLED_BOX_CURSOR;
25941
25942 if (EQ (arg, Qhollow))
25943 return HOLLOW_BOX_CURSOR;
25944
25945 if (EQ (arg, Qbar))
25946 {
25947 *width = 2;
25948 return BAR_CURSOR;
25949 }
25950
25951 if (CONSP (arg)
25952 && EQ (XCAR (arg), Qbar)
25953 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25954 {
25955 *width = XINT (XCDR (arg));
25956 return BAR_CURSOR;
25957 }
25958
25959 if (EQ (arg, Qhbar))
25960 {
25961 *width = 2;
25962 return HBAR_CURSOR;
25963 }
25964
25965 if (CONSP (arg)
25966 && EQ (XCAR (arg), Qhbar)
25967 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25968 {
25969 *width = XINT (XCDR (arg));
25970 return HBAR_CURSOR;
25971 }
25972
25973 /* Treat anything unknown as "hollow box cursor".
25974 It was bad to signal an error; people have trouble fixing
25975 .Xdefaults with Emacs, when it has something bad in it. */
25976 type = HOLLOW_BOX_CURSOR;
25977
25978 return type;
25979 }
25980
25981 /* Set the default cursor types for specified frame. */
25982 void
25983 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25984 {
25985 int width = 1;
25986 Lisp_Object tem;
25987
25988 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25989 FRAME_CURSOR_WIDTH (f) = width;
25990
25991 /* By default, set up the blink-off state depending on the on-state. */
25992
25993 tem = Fassoc (arg, Vblink_cursor_alist);
25994 if (!NILP (tem))
25995 {
25996 FRAME_BLINK_OFF_CURSOR (f)
25997 = get_specified_cursor_type (XCDR (tem), &width);
25998 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25999 }
26000 else
26001 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26002
26003 /* Make sure the cursor gets redrawn. */
26004 cursor_type_changed = 1;
26005 }
26006
26007
26008 #ifdef HAVE_WINDOW_SYSTEM
26009
26010 /* Return the cursor we want to be displayed in window W. Return
26011 width of bar/hbar cursor through WIDTH arg. Return with
26012 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26013 (i.e. if the `system caret' should track this cursor).
26014
26015 In a mini-buffer window, we want the cursor only to appear if we
26016 are reading input from this window. For the selected window, we
26017 want the cursor type given by the frame parameter or buffer local
26018 setting of cursor-type. If explicitly marked off, draw no cursor.
26019 In all other cases, we want a hollow box cursor. */
26020
26021 static enum text_cursor_kinds
26022 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26023 int *active_cursor)
26024 {
26025 struct frame *f = XFRAME (w->frame);
26026 struct buffer *b = XBUFFER (w->contents);
26027 int cursor_type = DEFAULT_CURSOR;
26028 Lisp_Object alt_cursor;
26029 int non_selected = 0;
26030
26031 *active_cursor = 1;
26032
26033 /* Echo area */
26034 if (cursor_in_echo_area
26035 && FRAME_HAS_MINIBUF_P (f)
26036 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26037 {
26038 if (w == XWINDOW (echo_area_window))
26039 {
26040 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26041 {
26042 *width = FRAME_CURSOR_WIDTH (f);
26043 return FRAME_DESIRED_CURSOR (f);
26044 }
26045 else
26046 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26047 }
26048
26049 *active_cursor = 0;
26050 non_selected = 1;
26051 }
26052
26053 /* Detect a nonselected window or nonselected frame. */
26054 else if (w != XWINDOW (f->selected_window)
26055 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26056 {
26057 *active_cursor = 0;
26058
26059 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26060 return NO_CURSOR;
26061
26062 non_selected = 1;
26063 }
26064
26065 /* Never display a cursor in a window in which cursor-type is nil. */
26066 if (NILP (BVAR (b, cursor_type)))
26067 return NO_CURSOR;
26068
26069 /* Get the normal cursor type for this window. */
26070 if (EQ (BVAR (b, cursor_type), Qt))
26071 {
26072 cursor_type = FRAME_DESIRED_CURSOR (f);
26073 *width = FRAME_CURSOR_WIDTH (f);
26074 }
26075 else
26076 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26077
26078 /* Use cursor-in-non-selected-windows instead
26079 for non-selected window or frame. */
26080 if (non_selected)
26081 {
26082 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26083 if (!EQ (Qt, alt_cursor))
26084 return get_specified_cursor_type (alt_cursor, width);
26085 /* t means modify the normal cursor type. */
26086 if (cursor_type == FILLED_BOX_CURSOR)
26087 cursor_type = HOLLOW_BOX_CURSOR;
26088 else if (cursor_type == BAR_CURSOR && *width > 1)
26089 --*width;
26090 return cursor_type;
26091 }
26092
26093 /* Use normal cursor if not blinked off. */
26094 if (!w->cursor_off_p)
26095 {
26096 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26097 {
26098 if (cursor_type == FILLED_BOX_CURSOR)
26099 {
26100 /* Using a block cursor on large images can be very annoying.
26101 So use a hollow cursor for "large" images.
26102 If image is not transparent (no mask), also use hollow cursor. */
26103 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26104 if (img != NULL && IMAGEP (img->spec))
26105 {
26106 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26107 where N = size of default frame font size.
26108 This should cover most of the "tiny" icons people may use. */
26109 if (!img->mask
26110 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26111 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26112 cursor_type = HOLLOW_BOX_CURSOR;
26113 }
26114 }
26115 else if (cursor_type != NO_CURSOR)
26116 {
26117 /* Display current only supports BOX and HOLLOW cursors for images.
26118 So for now, unconditionally use a HOLLOW cursor when cursor is
26119 not a solid box cursor. */
26120 cursor_type = HOLLOW_BOX_CURSOR;
26121 }
26122 }
26123 return cursor_type;
26124 }
26125
26126 /* Cursor is blinked off, so determine how to "toggle" it. */
26127
26128 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26129 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26130 return get_specified_cursor_type (XCDR (alt_cursor), width);
26131
26132 /* Then see if frame has specified a specific blink off cursor type. */
26133 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26134 {
26135 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26136 return FRAME_BLINK_OFF_CURSOR (f);
26137 }
26138
26139 #if 0
26140 /* Some people liked having a permanently visible blinking cursor,
26141 while others had very strong opinions against it. So it was
26142 decided to remove it. KFS 2003-09-03 */
26143
26144 /* Finally perform built-in cursor blinking:
26145 filled box <-> hollow box
26146 wide [h]bar <-> narrow [h]bar
26147 narrow [h]bar <-> no cursor
26148 other type <-> no cursor */
26149
26150 if (cursor_type == FILLED_BOX_CURSOR)
26151 return HOLLOW_BOX_CURSOR;
26152
26153 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26154 {
26155 *width = 1;
26156 return cursor_type;
26157 }
26158 #endif
26159
26160 return NO_CURSOR;
26161 }
26162
26163
26164 /* Notice when the text cursor of window W has been completely
26165 overwritten by a drawing operation that outputs glyphs in AREA
26166 starting at X0 and ending at X1 in the line starting at Y0 and
26167 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26168 the rest of the line after X0 has been written. Y coordinates
26169 are window-relative. */
26170
26171 static void
26172 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26173 int x0, int x1, int y0, int y1)
26174 {
26175 int cx0, cx1, cy0, cy1;
26176 struct glyph_row *row;
26177
26178 if (!w->phys_cursor_on_p)
26179 return;
26180 if (area != TEXT_AREA)
26181 return;
26182
26183 if (w->phys_cursor.vpos < 0
26184 || w->phys_cursor.vpos >= w->current_matrix->nrows
26185 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26186 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26187 return;
26188
26189 if (row->cursor_in_fringe_p)
26190 {
26191 row->cursor_in_fringe_p = 0;
26192 draw_fringe_bitmap (w, row, row->reversed_p);
26193 w->phys_cursor_on_p = 0;
26194 return;
26195 }
26196
26197 cx0 = w->phys_cursor.x;
26198 cx1 = cx0 + w->phys_cursor_width;
26199 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26200 return;
26201
26202 /* The cursor image will be completely removed from the
26203 screen if the output area intersects the cursor area in
26204 y-direction. When we draw in [y0 y1[, and some part of
26205 the cursor is at y < y0, that part must have been drawn
26206 before. When scrolling, the cursor is erased before
26207 actually scrolling, so we don't come here. When not
26208 scrolling, the rows above the old cursor row must have
26209 changed, and in this case these rows must have written
26210 over the cursor image.
26211
26212 Likewise if part of the cursor is below y1, with the
26213 exception of the cursor being in the first blank row at
26214 the buffer and window end because update_text_area
26215 doesn't draw that row. (Except when it does, but
26216 that's handled in update_text_area.) */
26217
26218 cy0 = w->phys_cursor.y;
26219 cy1 = cy0 + w->phys_cursor_height;
26220 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26221 return;
26222
26223 w->phys_cursor_on_p = 0;
26224 }
26225
26226 #endif /* HAVE_WINDOW_SYSTEM */
26227
26228 \f
26229 /************************************************************************
26230 Mouse Face
26231 ************************************************************************/
26232
26233 #ifdef HAVE_WINDOW_SYSTEM
26234
26235 /* EXPORT for RIF:
26236 Fix the display of area AREA of overlapping row ROW in window W
26237 with respect to the overlapping part OVERLAPS. */
26238
26239 void
26240 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26241 enum glyph_row_area area, int overlaps)
26242 {
26243 int i, x;
26244
26245 block_input ();
26246
26247 x = 0;
26248 for (i = 0; i < row->used[area];)
26249 {
26250 if (row->glyphs[area][i].overlaps_vertically_p)
26251 {
26252 int start = i, start_x = x;
26253
26254 do
26255 {
26256 x += row->glyphs[area][i].pixel_width;
26257 ++i;
26258 }
26259 while (i < row->used[area]
26260 && row->glyphs[area][i].overlaps_vertically_p);
26261
26262 draw_glyphs (w, start_x, row, area,
26263 start, i,
26264 DRAW_NORMAL_TEXT, overlaps);
26265 }
26266 else
26267 {
26268 x += row->glyphs[area][i].pixel_width;
26269 ++i;
26270 }
26271 }
26272
26273 unblock_input ();
26274 }
26275
26276
26277 /* EXPORT:
26278 Draw the cursor glyph of window W in glyph row ROW. See the
26279 comment of draw_glyphs for the meaning of HL. */
26280
26281 void
26282 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26283 enum draw_glyphs_face hl)
26284 {
26285 /* If cursor hpos is out of bounds, don't draw garbage. This can
26286 happen in mini-buffer windows when switching between echo area
26287 glyphs and mini-buffer. */
26288 if ((row->reversed_p
26289 ? (w->phys_cursor.hpos >= 0)
26290 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26291 {
26292 int on_p = w->phys_cursor_on_p;
26293 int x1;
26294 int hpos = w->phys_cursor.hpos;
26295
26296 /* When the window is hscrolled, cursor hpos can legitimately be
26297 out of bounds, but we draw the cursor at the corresponding
26298 window margin in that case. */
26299 if (!row->reversed_p && hpos < 0)
26300 hpos = 0;
26301 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26302 hpos = row->used[TEXT_AREA] - 1;
26303
26304 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26305 hl, 0);
26306 w->phys_cursor_on_p = on_p;
26307
26308 if (hl == DRAW_CURSOR)
26309 w->phys_cursor_width = x1 - w->phys_cursor.x;
26310 /* When we erase the cursor, and ROW is overlapped by other
26311 rows, make sure that these overlapping parts of other rows
26312 are redrawn. */
26313 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26314 {
26315 w->phys_cursor_width = x1 - w->phys_cursor.x;
26316
26317 if (row > w->current_matrix->rows
26318 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26319 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26320 OVERLAPS_ERASED_CURSOR);
26321
26322 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26323 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26324 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26325 OVERLAPS_ERASED_CURSOR);
26326 }
26327 }
26328 }
26329
26330
26331 /* EXPORT:
26332 Erase the image of a cursor of window W from the screen. */
26333
26334 void
26335 erase_phys_cursor (struct window *w)
26336 {
26337 struct frame *f = XFRAME (w->frame);
26338 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26339 int hpos = w->phys_cursor.hpos;
26340 int vpos = w->phys_cursor.vpos;
26341 int mouse_face_here_p = 0;
26342 struct glyph_matrix *active_glyphs = w->current_matrix;
26343 struct glyph_row *cursor_row;
26344 struct glyph *cursor_glyph;
26345 enum draw_glyphs_face hl;
26346
26347 /* No cursor displayed or row invalidated => nothing to do on the
26348 screen. */
26349 if (w->phys_cursor_type == NO_CURSOR)
26350 goto mark_cursor_off;
26351
26352 /* VPOS >= active_glyphs->nrows means that window has been resized.
26353 Don't bother to erase the cursor. */
26354 if (vpos >= active_glyphs->nrows)
26355 goto mark_cursor_off;
26356
26357 /* If row containing cursor is marked invalid, there is nothing we
26358 can do. */
26359 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26360 if (!cursor_row->enabled_p)
26361 goto mark_cursor_off;
26362
26363 /* If line spacing is > 0, old cursor may only be partially visible in
26364 window after split-window. So adjust visible height. */
26365 cursor_row->visible_height = min (cursor_row->visible_height,
26366 window_text_bottom_y (w) - cursor_row->y);
26367
26368 /* If row is completely invisible, don't attempt to delete a cursor which
26369 isn't there. This can happen if cursor is at top of a window, and
26370 we switch to a buffer with a header line in that window. */
26371 if (cursor_row->visible_height <= 0)
26372 goto mark_cursor_off;
26373
26374 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26375 if (cursor_row->cursor_in_fringe_p)
26376 {
26377 cursor_row->cursor_in_fringe_p = 0;
26378 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26379 goto mark_cursor_off;
26380 }
26381
26382 /* This can happen when the new row is shorter than the old one.
26383 In this case, either draw_glyphs or clear_end_of_line
26384 should have cleared the cursor. Note that we wouldn't be
26385 able to erase the cursor in this case because we don't have a
26386 cursor glyph at hand. */
26387 if ((cursor_row->reversed_p
26388 ? (w->phys_cursor.hpos < 0)
26389 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26390 goto mark_cursor_off;
26391
26392 /* When the window is hscrolled, cursor hpos can legitimately be out
26393 of bounds, but we draw the cursor at the corresponding window
26394 margin in that case. */
26395 if (!cursor_row->reversed_p && hpos < 0)
26396 hpos = 0;
26397 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26398 hpos = cursor_row->used[TEXT_AREA] - 1;
26399
26400 /* If the cursor is in the mouse face area, redisplay that when
26401 we clear the cursor. */
26402 if (! NILP (hlinfo->mouse_face_window)
26403 && coords_in_mouse_face_p (w, hpos, vpos)
26404 /* Don't redraw the cursor's spot in mouse face if it is at the
26405 end of a line (on a newline). The cursor appears there, but
26406 mouse highlighting does not. */
26407 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26408 mouse_face_here_p = 1;
26409
26410 /* Maybe clear the display under the cursor. */
26411 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26412 {
26413 int x, y, left_x;
26414 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26415 int width;
26416
26417 cursor_glyph = get_phys_cursor_glyph (w);
26418 if (cursor_glyph == NULL)
26419 goto mark_cursor_off;
26420
26421 width = cursor_glyph->pixel_width;
26422 left_x = window_box_left_offset (w, TEXT_AREA);
26423 x = w->phys_cursor.x;
26424 if (x < left_x)
26425 width -= left_x - x;
26426 width = min (width, window_box_width (w, TEXT_AREA) - x);
26427 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26428 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26429
26430 if (width > 0)
26431 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26432 }
26433
26434 /* Erase the cursor by redrawing the character underneath it. */
26435 if (mouse_face_here_p)
26436 hl = DRAW_MOUSE_FACE;
26437 else
26438 hl = DRAW_NORMAL_TEXT;
26439 draw_phys_cursor_glyph (w, cursor_row, hl);
26440
26441 mark_cursor_off:
26442 w->phys_cursor_on_p = 0;
26443 w->phys_cursor_type = NO_CURSOR;
26444 }
26445
26446
26447 /* EXPORT:
26448 Display or clear cursor of window W. If ON is zero, clear the
26449 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26450 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26451
26452 void
26453 display_and_set_cursor (struct window *w, int on,
26454 int hpos, int vpos, int x, int y)
26455 {
26456 struct frame *f = XFRAME (w->frame);
26457 int new_cursor_type;
26458 int new_cursor_width;
26459 int active_cursor;
26460 struct glyph_row *glyph_row;
26461 struct glyph *glyph;
26462
26463 /* This is pointless on invisible frames, and dangerous on garbaged
26464 windows and frames; in the latter case, the frame or window may
26465 be in the midst of changing its size, and x and y may be off the
26466 window. */
26467 if (! FRAME_VISIBLE_P (f)
26468 || FRAME_GARBAGED_P (f)
26469 || vpos >= w->current_matrix->nrows
26470 || hpos >= w->current_matrix->matrix_w)
26471 return;
26472
26473 /* If cursor is off and we want it off, return quickly. */
26474 if (!on && !w->phys_cursor_on_p)
26475 return;
26476
26477 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26478 /* If cursor row is not enabled, we don't really know where to
26479 display the cursor. */
26480 if (!glyph_row->enabled_p)
26481 {
26482 w->phys_cursor_on_p = 0;
26483 return;
26484 }
26485
26486 glyph = NULL;
26487 if (!glyph_row->exact_window_width_line_p
26488 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26489 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26490
26491 eassert (input_blocked_p ());
26492
26493 /* Set new_cursor_type to the cursor we want to be displayed. */
26494 new_cursor_type = get_window_cursor_type (w, glyph,
26495 &new_cursor_width, &active_cursor);
26496
26497 /* If cursor is currently being shown and we don't want it to be or
26498 it is in the wrong place, or the cursor type is not what we want,
26499 erase it. */
26500 if (w->phys_cursor_on_p
26501 && (!on
26502 || w->phys_cursor.x != x
26503 || w->phys_cursor.y != y
26504 || new_cursor_type != w->phys_cursor_type
26505 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26506 && new_cursor_width != w->phys_cursor_width)))
26507 erase_phys_cursor (w);
26508
26509 /* Don't check phys_cursor_on_p here because that flag is only set
26510 to zero in some cases where we know that the cursor has been
26511 completely erased, to avoid the extra work of erasing the cursor
26512 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26513 still not be visible, or it has only been partly erased. */
26514 if (on)
26515 {
26516 w->phys_cursor_ascent = glyph_row->ascent;
26517 w->phys_cursor_height = glyph_row->height;
26518
26519 /* Set phys_cursor_.* before x_draw_.* is called because some
26520 of them may need the information. */
26521 w->phys_cursor.x = x;
26522 w->phys_cursor.y = glyph_row->y;
26523 w->phys_cursor.hpos = hpos;
26524 w->phys_cursor.vpos = vpos;
26525 }
26526
26527 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26528 new_cursor_type, new_cursor_width,
26529 on, active_cursor);
26530 }
26531
26532
26533 /* Switch the display of W's cursor on or off, according to the value
26534 of ON. */
26535
26536 static void
26537 update_window_cursor (struct window *w, int on)
26538 {
26539 /* Don't update cursor in windows whose frame is in the process
26540 of being deleted. */
26541 if (w->current_matrix)
26542 {
26543 int hpos = w->phys_cursor.hpos;
26544 int vpos = w->phys_cursor.vpos;
26545 struct glyph_row *row;
26546
26547 if (vpos >= w->current_matrix->nrows
26548 || hpos >= w->current_matrix->matrix_w)
26549 return;
26550
26551 row = MATRIX_ROW (w->current_matrix, vpos);
26552
26553 /* When the window is hscrolled, cursor hpos can legitimately be
26554 out of bounds, but we draw the cursor at the corresponding
26555 window margin in that case. */
26556 if (!row->reversed_p && hpos < 0)
26557 hpos = 0;
26558 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26559 hpos = row->used[TEXT_AREA] - 1;
26560
26561 block_input ();
26562 display_and_set_cursor (w, on, hpos, vpos,
26563 w->phys_cursor.x, w->phys_cursor.y);
26564 unblock_input ();
26565 }
26566 }
26567
26568
26569 /* Call update_window_cursor with parameter ON_P on all leaf windows
26570 in the window tree rooted at W. */
26571
26572 static void
26573 update_cursor_in_window_tree (struct window *w, int on_p)
26574 {
26575 while (w)
26576 {
26577 if (WINDOWP (w->contents))
26578 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26579 else
26580 update_window_cursor (w, on_p);
26581
26582 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26583 }
26584 }
26585
26586
26587 /* EXPORT:
26588 Display the cursor on window W, or clear it, according to ON_P.
26589 Don't change the cursor's position. */
26590
26591 void
26592 x_update_cursor (struct frame *f, int on_p)
26593 {
26594 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26595 }
26596
26597
26598 /* EXPORT:
26599 Clear the cursor of window W to background color, and mark the
26600 cursor as not shown. This is used when the text where the cursor
26601 is about to be rewritten. */
26602
26603 void
26604 x_clear_cursor (struct window *w)
26605 {
26606 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26607 update_window_cursor (w, 0);
26608 }
26609
26610 #endif /* HAVE_WINDOW_SYSTEM */
26611
26612 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26613 and MSDOS. */
26614 static void
26615 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26616 int start_hpos, int end_hpos,
26617 enum draw_glyphs_face draw)
26618 {
26619 #ifdef HAVE_WINDOW_SYSTEM
26620 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26621 {
26622 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26623 return;
26624 }
26625 #endif
26626 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26627 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26628 #endif
26629 }
26630
26631 /* Display the active region described by mouse_face_* according to DRAW. */
26632
26633 static void
26634 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26635 {
26636 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26637 struct frame *f = XFRAME (WINDOW_FRAME (w));
26638
26639 if (/* If window is in the process of being destroyed, don't bother
26640 to do anything. */
26641 w->current_matrix != NULL
26642 /* Don't update mouse highlight if hidden */
26643 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26644 /* Recognize when we are called to operate on rows that don't exist
26645 anymore. This can happen when a window is split. */
26646 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26647 {
26648 int phys_cursor_on_p = w->phys_cursor_on_p;
26649 struct glyph_row *row, *first, *last;
26650
26651 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26652 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26653
26654 for (row = first; row <= last && row->enabled_p; ++row)
26655 {
26656 int start_hpos, end_hpos, start_x;
26657
26658 /* For all but the first row, the highlight starts at column 0. */
26659 if (row == first)
26660 {
26661 /* R2L rows have BEG and END in reversed order, but the
26662 screen drawing geometry is always left to right. So
26663 we need to mirror the beginning and end of the
26664 highlighted area in R2L rows. */
26665 if (!row->reversed_p)
26666 {
26667 start_hpos = hlinfo->mouse_face_beg_col;
26668 start_x = hlinfo->mouse_face_beg_x;
26669 }
26670 else if (row == last)
26671 {
26672 start_hpos = hlinfo->mouse_face_end_col;
26673 start_x = hlinfo->mouse_face_end_x;
26674 }
26675 else
26676 {
26677 start_hpos = 0;
26678 start_x = 0;
26679 }
26680 }
26681 else if (row->reversed_p && row == last)
26682 {
26683 start_hpos = hlinfo->mouse_face_end_col;
26684 start_x = hlinfo->mouse_face_end_x;
26685 }
26686 else
26687 {
26688 start_hpos = 0;
26689 start_x = 0;
26690 }
26691
26692 if (row == last)
26693 {
26694 if (!row->reversed_p)
26695 end_hpos = hlinfo->mouse_face_end_col;
26696 else if (row == first)
26697 end_hpos = hlinfo->mouse_face_beg_col;
26698 else
26699 {
26700 end_hpos = row->used[TEXT_AREA];
26701 if (draw == DRAW_NORMAL_TEXT)
26702 row->fill_line_p = 1; /* Clear to end of line */
26703 }
26704 }
26705 else if (row->reversed_p && row == first)
26706 end_hpos = hlinfo->mouse_face_beg_col;
26707 else
26708 {
26709 end_hpos = row->used[TEXT_AREA];
26710 if (draw == DRAW_NORMAL_TEXT)
26711 row->fill_line_p = 1; /* Clear to end of line */
26712 }
26713
26714 if (end_hpos > start_hpos)
26715 {
26716 draw_row_with_mouse_face (w, start_x, row,
26717 start_hpos, end_hpos, draw);
26718
26719 row->mouse_face_p
26720 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26721 }
26722 }
26723
26724 #ifdef HAVE_WINDOW_SYSTEM
26725 /* When we've written over the cursor, arrange for it to
26726 be displayed again. */
26727 if (FRAME_WINDOW_P (f)
26728 && phys_cursor_on_p && !w->phys_cursor_on_p)
26729 {
26730 int hpos = w->phys_cursor.hpos;
26731
26732 /* When the window is hscrolled, cursor hpos can legitimately be
26733 out of bounds, but we draw the cursor at the corresponding
26734 window margin in that case. */
26735 if (!row->reversed_p && hpos < 0)
26736 hpos = 0;
26737 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26738 hpos = row->used[TEXT_AREA] - 1;
26739
26740 block_input ();
26741 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26742 w->phys_cursor.x, w->phys_cursor.y);
26743 unblock_input ();
26744 }
26745 #endif /* HAVE_WINDOW_SYSTEM */
26746 }
26747
26748 #ifdef HAVE_WINDOW_SYSTEM
26749 /* Change the mouse cursor. */
26750 if (FRAME_WINDOW_P (f))
26751 {
26752 if (draw == DRAW_NORMAL_TEXT
26753 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26754 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26755 else if (draw == DRAW_MOUSE_FACE)
26756 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26757 else
26758 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26759 }
26760 #endif /* HAVE_WINDOW_SYSTEM */
26761 }
26762
26763 /* EXPORT:
26764 Clear out the mouse-highlighted active region.
26765 Redraw it un-highlighted first. Value is non-zero if mouse
26766 face was actually drawn unhighlighted. */
26767
26768 int
26769 clear_mouse_face (Mouse_HLInfo *hlinfo)
26770 {
26771 int cleared = 0;
26772
26773 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26774 {
26775 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26776 cleared = 1;
26777 }
26778
26779 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26780 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26781 hlinfo->mouse_face_window = Qnil;
26782 hlinfo->mouse_face_overlay = Qnil;
26783 return cleared;
26784 }
26785
26786 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26787 within the mouse face on that window. */
26788 static int
26789 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26790 {
26791 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26792
26793 /* Quickly resolve the easy cases. */
26794 if (!(WINDOWP (hlinfo->mouse_face_window)
26795 && XWINDOW (hlinfo->mouse_face_window) == w))
26796 return 0;
26797 if (vpos < hlinfo->mouse_face_beg_row
26798 || vpos > hlinfo->mouse_face_end_row)
26799 return 0;
26800 if (vpos > hlinfo->mouse_face_beg_row
26801 && vpos < hlinfo->mouse_face_end_row)
26802 return 1;
26803
26804 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26805 {
26806 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26807 {
26808 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26809 return 1;
26810 }
26811 else if ((vpos == hlinfo->mouse_face_beg_row
26812 && hpos >= hlinfo->mouse_face_beg_col)
26813 || (vpos == hlinfo->mouse_face_end_row
26814 && hpos < hlinfo->mouse_face_end_col))
26815 return 1;
26816 }
26817 else
26818 {
26819 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26820 {
26821 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26822 return 1;
26823 }
26824 else if ((vpos == hlinfo->mouse_face_beg_row
26825 && hpos <= hlinfo->mouse_face_beg_col)
26826 || (vpos == hlinfo->mouse_face_end_row
26827 && hpos > hlinfo->mouse_face_end_col))
26828 return 1;
26829 }
26830 return 0;
26831 }
26832
26833
26834 /* EXPORT:
26835 Non-zero if physical cursor of window W is within mouse face. */
26836
26837 int
26838 cursor_in_mouse_face_p (struct window *w)
26839 {
26840 int hpos = w->phys_cursor.hpos;
26841 int vpos = w->phys_cursor.vpos;
26842 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26843
26844 /* When the window is hscrolled, cursor hpos can legitimately be out
26845 of bounds, but we draw the cursor at the corresponding window
26846 margin in that case. */
26847 if (!row->reversed_p && hpos < 0)
26848 hpos = 0;
26849 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26850 hpos = row->used[TEXT_AREA] - 1;
26851
26852 return coords_in_mouse_face_p (w, hpos, vpos);
26853 }
26854
26855
26856 \f
26857 /* Find the glyph rows START_ROW and END_ROW of window W that display
26858 characters between buffer positions START_CHARPOS and END_CHARPOS
26859 (excluding END_CHARPOS). DISP_STRING is a display string that
26860 covers these buffer positions. This is similar to
26861 row_containing_pos, but is more accurate when bidi reordering makes
26862 buffer positions change non-linearly with glyph rows. */
26863 static void
26864 rows_from_pos_range (struct window *w,
26865 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26866 Lisp_Object disp_string,
26867 struct glyph_row **start, struct glyph_row **end)
26868 {
26869 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26870 int last_y = window_text_bottom_y (w);
26871 struct glyph_row *row;
26872
26873 *start = NULL;
26874 *end = NULL;
26875
26876 while (!first->enabled_p
26877 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26878 first++;
26879
26880 /* Find the START row. */
26881 for (row = first;
26882 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26883 row++)
26884 {
26885 /* A row can potentially be the START row if the range of the
26886 characters it displays intersects the range
26887 [START_CHARPOS..END_CHARPOS). */
26888 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26889 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26890 /* See the commentary in row_containing_pos, for the
26891 explanation of the complicated way to check whether
26892 some position is beyond the end of the characters
26893 displayed by a row. */
26894 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26895 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26896 && !row->ends_at_zv_p
26897 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26898 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26899 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26900 && !row->ends_at_zv_p
26901 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26902 {
26903 /* Found a candidate row. Now make sure at least one of the
26904 glyphs it displays has a charpos from the range
26905 [START_CHARPOS..END_CHARPOS).
26906
26907 This is not obvious because bidi reordering could make
26908 buffer positions of a row be 1,2,3,102,101,100, and if we
26909 want to highlight characters in [50..60), we don't want
26910 this row, even though [50..60) does intersect [1..103),
26911 the range of character positions given by the row's start
26912 and end positions. */
26913 struct glyph *g = row->glyphs[TEXT_AREA];
26914 struct glyph *e = g + row->used[TEXT_AREA];
26915
26916 while (g < e)
26917 {
26918 if (((BUFFERP (g->object) || INTEGERP (g->object))
26919 && start_charpos <= g->charpos && g->charpos < end_charpos)
26920 /* A glyph that comes from DISP_STRING is by
26921 definition to be highlighted. */
26922 || EQ (g->object, disp_string))
26923 *start = row;
26924 g++;
26925 }
26926 if (*start)
26927 break;
26928 }
26929 }
26930
26931 /* Find the END row. */
26932 if (!*start
26933 /* If the last row is partially visible, start looking for END
26934 from that row, instead of starting from FIRST. */
26935 && !(row->enabled_p
26936 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26937 row = first;
26938 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26939 {
26940 struct glyph_row *next = row + 1;
26941 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26942
26943 if (!next->enabled_p
26944 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26945 /* The first row >= START whose range of displayed characters
26946 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26947 is the row END + 1. */
26948 || (start_charpos < next_start
26949 && end_charpos < next_start)
26950 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26951 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26952 && !next->ends_at_zv_p
26953 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26954 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26955 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26956 && !next->ends_at_zv_p
26957 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26958 {
26959 *end = row;
26960 break;
26961 }
26962 else
26963 {
26964 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26965 but none of the characters it displays are in the range, it is
26966 also END + 1. */
26967 struct glyph *g = next->glyphs[TEXT_AREA];
26968 struct glyph *s = g;
26969 struct glyph *e = g + next->used[TEXT_AREA];
26970
26971 while (g < e)
26972 {
26973 if (((BUFFERP (g->object) || INTEGERP (g->object))
26974 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26975 /* If the buffer position of the first glyph in
26976 the row is equal to END_CHARPOS, it means
26977 the last character to be highlighted is the
26978 newline of ROW, and we must consider NEXT as
26979 END, not END+1. */
26980 || (((!next->reversed_p && g == s)
26981 || (next->reversed_p && g == e - 1))
26982 && (g->charpos == end_charpos
26983 /* Special case for when NEXT is an
26984 empty line at ZV. */
26985 || (g->charpos == -1
26986 && !row->ends_at_zv_p
26987 && next_start == end_charpos)))))
26988 /* A glyph that comes from DISP_STRING is by
26989 definition to be highlighted. */
26990 || EQ (g->object, disp_string))
26991 break;
26992 g++;
26993 }
26994 if (g == e)
26995 {
26996 *end = row;
26997 break;
26998 }
26999 /* The first row that ends at ZV must be the last to be
27000 highlighted. */
27001 else if (next->ends_at_zv_p)
27002 {
27003 *end = next;
27004 break;
27005 }
27006 }
27007 }
27008 }
27009
27010 /* This function sets the mouse_face_* elements of HLINFO, assuming
27011 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27012 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27013 for the overlay or run of text properties specifying the mouse
27014 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27015 before-string and after-string that must also be highlighted.
27016 DISP_STRING, if non-nil, is a display string that may cover some
27017 or all of the highlighted text. */
27018
27019 static void
27020 mouse_face_from_buffer_pos (Lisp_Object window,
27021 Mouse_HLInfo *hlinfo,
27022 ptrdiff_t mouse_charpos,
27023 ptrdiff_t start_charpos,
27024 ptrdiff_t end_charpos,
27025 Lisp_Object before_string,
27026 Lisp_Object after_string,
27027 Lisp_Object disp_string)
27028 {
27029 struct window *w = XWINDOW (window);
27030 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27031 struct glyph_row *r1, *r2;
27032 struct glyph *glyph, *end;
27033 ptrdiff_t ignore, pos;
27034 int x;
27035
27036 eassert (NILP (disp_string) || STRINGP (disp_string));
27037 eassert (NILP (before_string) || STRINGP (before_string));
27038 eassert (NILP (after_string) || STRINGP (after_string));
27039
27040 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27041 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27042 if (r1 == NULL)
27043 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27044 /* If the before-string or display-string contains newlines,
27045 rows_from_pos_range skips to its last row. Move back. */
27046 if (!NILP (before_string) || !NILP (disp_string))
27047 {
27048 struct glyph_row *prev;
27049 while ((prev = r1 - 1, prev >= first)
27050 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27051 && prev->used[TEXT_AREA] > 0)
27052 {
27053 struct glyph *beg = prev->glyphs[TEXT_AREA];
27054 glyph = beg + prev->used[TEXT_AREA];
27055 while (--glyph >= beg && INTEGERP (glyph->object));
27056 if (glyph < beg
27057 || !(EQ (glyph->object, before_string)
27058 || EQ (glyph->object, disp_string)))
27059 break;
27060 r1 = prev;
27061 }
27062 }
27063 if (r2 == NULL)
27064 {
27065 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27066 hlinfo->mouse_face_past_end = 1;
27067 }
27068 else if (!NILP (after_string))
27069 {
27070 /* If the after-string has newlines, advance to its last row. */
27071 struct glyph_row *next;
27072 struct glyph_row *last
27073 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27074
27075 for (next = r2 + 1;
27076 next <= last
27077 && next->used[TEXT_AREA] > 0
27078 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27079 ++next)
27080 r2 = next;
27081 }
27082 /* The rest of the display engine assumes that mouse_face_beg_row is
27083 either above mouse_face_end_row or identical to it. But with
27084 bidi-reordered continued lines, the row for START_CHARPOS could
27085 be below the row for END_CHARPOS. If so, swap the rows and store
27086 them in correct order. */
27087 if (r1->y > r2->y)
27088 {
27089 struct glyph_row *tem = r2;
27090
27091 r2 = r1;
27092 r1 = tem;
27093 }
27094
27095 hlinfo->mouse_face_beg_y = r1->y;
27096 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27097 hlinfo->mouse_face_end_y = r2->y;
27098 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27099
27100 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27101 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27102 could be anywhere in the row and in any order. The strategy
27103 below is to find the leftmost and the rightmost glyph that
27104 belongs to either of these 3 strings, or whose position is
27105 between START_CHARPOS and END_CHARPOS, and highlight all the
27106 glyphs between those two. This may cover more than just the text
27107 between START_CHARPOS and END_CHARPOS if the range of characters
27108 strides the bidi level boundary, e.g. if the beginning is in R2L
27109 text while the end is in L2R text or vice versa. */
27110 if (!r1->reversed_p)
27111 {
27112 /* This row is in a left to right paragraph. Scan it left to
27113 right. */
27114 glyph = r1->glyphs[TEXT_AREA];
27115 end = glyph + r1->used[TEXT_AREA];
27116 x = r1->x;
27117
27118 /* Skip truncation glyphs at the start of the glyph row. */
27119 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27120 for (; glyph < end
27121 && INTEGERP (glyph->object)
27122 && glyph->charpos < 0;
27123 ++glyph)
27124 x += glyph->pixel_width;
27125
27126 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27127 or DISP_STRING, and the first glyph from buffer whose
27128 position is between START_CHARPOS and END_CHARPOS. */
27129 for (; glyph < end
27130 && !INTEGERP (glyph->object)
27131 && !EQ (glyph->object, disp_string)
27132 && !(BUFFERP (glyph->object)
27133 && (glyph->charpos >= start_charpos
27134 && glyph->charpos < end_charpos));
27135 ++glyph)
27136 {
27137 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27138 are present at buffer positions between START_CHARPOS and
27139 END_CHARPOS, or if they come from an overlay. */
27140 if (EQ (glyph->object, before_string))
27141 {
27142 pos = string_buffer_position (before_string,
27143 start_charpos);
27144 /* If pos == 0, it means before_string came from an
27145 overlay, not from a buffer position. */
27146 if (!pos || (pos >= start_charpos && pos < end_charpos))
27147 break;
27148 }
27149 else if (EQ (glyph->object, after_string))
27150 {
27151 pos = string_buffer_position (after_string, end_charpos);
27152 if (!pos || (pos >= start_charpos && pos < end_charpos))
27153 break;
27154 }
27155 x += glyph->pixel_width;
27156 }
27157 hlinfo->mouse_face_beg_x = x;
27158 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27159 }
27160 else
27161 {
27162 /* This row is in a right to left paragraph. Scan it right to
27163 left. */
27164 struct glyph *g;
27165
27166 end = r1->glyphs[TEXT_AREA] - 1;
27167 glyph = end + r1->used[TEXT_AREA];
27168
27169 /* Skip truncation glyphs at the start of the glyph row. */
27170 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27171 for (; glyph > end
27172 && INTEGERP (glyph->object)
27173 && glyph->charpos < 0;
27174 --glyph)
27175 ;
27176
27177 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27178 or DISP_STRING, and the first glyph from buffer whose
27179 position is between START_CHARPOS and END_CHARPOS. */
27180 for (; glyph > end
27181 && !INTEGERP (glyph->object)
27182 && !EQ (glyph->object, disp_string)
27183 && !(BUFFERP (glyph->object)
27184 && (glyph->charpos >= start_charpos
27185 && glyph->charpos < end_charpos));
27186 --glyph)
27187 {
27188 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27189 are present at buffer positions between START_CHARPOS and
27190 END_CHARPOS, or if they come from an overlay. */
27191 if (EQ (glyph->object, before_string))
27192 {
27193 pos = string_buffer_position (before_string, start_charpos);
27194 /* If pos == 0, it means before_string came from an
27195 overlay, not from a buffer position. */
27196 if (!pos || (pos >= start_charpos && pos < end_charpos))
27197 break;
27198 }
27199 else if (EQ (glyph->object, after_string))
27200 {
27201 pos = string_buffer_position (after_string, end_charpos);
27202 if (!pos || (pos >= start_charpos && pos < end_charpos))
27203 break;
27204 }
27205 }
27206
27207 glyph++; /* first glyph to the right of the highlighted area */
27208 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27209 x += g->pixel_width;
27210 hlinfo->mouse_face_beg_x = x;
27211 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27212 }
27213
27214 /* If the highlight ends in a different row, compute GLYPH and END
27215 for the end row. Otherwise, reuse the values computed above for
27216 the row where the highlight begins. */
27217 if (r2 != r1)
27218 {
27219 if (!r2->reversed_p)
27220 {
27221 glyph = r2->glyphs[TEXT_AREA];
27222 end = glyph + r2->used[TEXT_AREA];
27223 x = r2->x;
27224 }
27225 else
27226 {
27227 end = r2->glyphs[TEXT_AREA] - 1;
27228 glyph = end + r2->used[TEXT_AREA];
27229 }
27230 }
27231
27232 if (!r2->reversed_p)
27233 {
27234 /* Skip truncation and continuation glyphs near the end of the
27235 row, and also blanks and stretch glyphs inserted by
27236 extend_face_to_end_of_line. */
27237 while (end > glyph
27238 && INTEGERP ((end - 1)->object))
27239 --end;
27240 /* Scan the rest of the glyph row from the end, looking for the
27241 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27242 DISP_STRING, or whose position is between START_CHARPOS
27243 and END_CHARPOS */
27244 for (--end;
27245 end > glyph
27246 && !INTEGERP (end->object)
27247 && !EQ (end->object, disp_string)
27248 && !(BUFFERP (end->object)
27249 && (end->charpos >= start_charpos
27250 && end->charpos < end_charpos));
27251 --end)
27252 {
27253 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27254 are present at buffer positions between START_CHARPOS and
27255 END_CHARPOS, or if they come from an overlay. */
27256 if (EQ (end->object, before_string))
27257 {
27258 pos = string_buffer_position (before_string, start_charpos);
27259 if (!pos || (pos >= start_charpos && pos < end_charpos))
27260 break;
27261 }
27262 else if (EQ (end->object, after_string))
27263 {
27264 pos = string_buffer_position (after_string, end_charpos);
27265 if (!pos || (pos >= start_charpos && pos < end_charpos))
27266 break;
27267 }
27268 }
27269 /* Find the X coordinate of the last glyph to be highlighted. */
27270 for (; glyph <= end; ++glyph)
27271 x += glyph->pixel_width;
27272
27273 hlinfo->mouse_face_end_x = x;
27274 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27275 }
27276 else
27277 {
27278 /* Skip truncation and continuation glyphs near the end of the
27279 row, and also blanks and stretch glyphs inserted by
27280 extend_face_to_end_of_line. */
27281 x = r2->x;
27282 end++;
27283 while (end < glyph
27284 && INTEGERP (end->object))
27285 {
27286 x += end->pixel_width;
27287 ++end;
27288 }
27289 /* Scan the rest of the glyph row from the end, looking for the
27290 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27291 DISP_STRING, or whose position is between START_CHARPOS
27292 and END_CHARPOS */
27293 for ( ;
27294 end < glyph
27295 && !INTEGERP (end->object)
27296 && !EQ (end->object, disp_string)
27297 && !(BUFFERP (end->object)
27298 && (end->charpos >= start_charpos
27299 && end->charpos < end_charpos));
27300 ++end)
27301 {
27302 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27303 are present at buffer positions between START_CHARPOS and
27304 END_CHARPOS, or if they come from an overlay. */
27305 if (EQ (end->object, before_string))
27306 {
27307 pos = string_buffer_position (before_string, start_charpos);
27308 if (!pos || (pos >= start_charpos && pos < end_charpos))
27309 break;
27310 }
27311 else if (EQ (end->object, after_string))
27312 {
27313 pos = string_buffer_position (after_string, end_charpos);
27314 if (!pos || (pos >= start_charpos && pos < end_charpos))
27315 break;
27316 }
27317 x += end->pixel_width;
27318 }
27319 /* If we exited the above loop because we arrived at the last
27320 glyph of the row, and its buffer position is still not in
27321 range, it means the last character in range is the preceding
27322 newline. Bump the end column and x values to get past the
27323 last glyph. */
27324 if (end == glyph
27325 && BUFFERP (end->object)
27326 && (end->charpos < start_charpos
27327 || end->charpos >= end_charpos))
27328 {
27329 x += end->pixel_width;
27330 ++end;
27331 }
27332 hlinfo->mouse_face_end_x = x;
27333 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27334 }
27335
27336 hlinfo->mouse_face_window = window;
27337 hlinfo->mouse_face_face_id
27338 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27339 mouse_charpos + 1,
27340 !hlinfo->mouse_face_hidden, -1);
27341 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27342 }
27343
27344 /* The following function is not used anymore (replaced with
27345 mouse_face_from_string_pos), but I leave it here for the time
27346 being, in case someone would. */
27347
27348 #if 0 /* not used */
27349
27350 /* Find the position of the glyph for position POS in OBJECT in
27351 window W's current matrix, and return in *X, *Y the pixel
27352 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27353
27354 RIGHT_P non-zero means return the position of the right edge of the
27355 glyph, RIGHT_P zero means return the left edge position.
27356
27357 If no glyph for POS exists in the matrix, return the position of
27358 the glyph with the next smaller position that is in the matrix, if
27359 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27360 exists in the matrix, return the position of the glyph with the
27361 next larger position in OBJECT.
27362
27363 Value is non-zero if a glyph was found. */
27364
27365 static int
27366 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27367 int *hpos, int *vpos, int *x, int *y, int right_p)
27368 {
27369 int yb = window_text_bottom_y (w);
27370 struct glyph_row *r;
27371 struct glyph *best_glyph = NULL;
27372 struct glyph_row *best_row = NULL;
27373 int best_x = 0;
27374
27375 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27376 r->enabled_p && r->y < yb;
27377 ++r)
27378 {
27379 struct glyph *g = r->glyphs[TEXT_AREA];
27380 struct glyph *e = g + r->used[TEXT_AREA];
27381 int gx;
27382
27383 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27384 if (EQ (g->object, object))
27385 {
27386 if (g->charpos == pos)
27387 {
27388 best_glyph = g;
27389 best_x = gx;
27390 best_row = r;
27391 goto found;
27392 }
27393 else if (best_glyph == NULL
27394 || ((eabs (g->charpos - pos)
27395 < eabs (best_glyph->charpos - pos))
27396 && (right_p
27397 ? g->charpos < pos
27398 : g->charpos > pos)))
27399 {
27400 best_glyph = g;
27401 best_x = gx;
27402 best_row = r;
27403 }
27404 }
27405 }
27406
27407 found:
27408
27409 if (best_glyph)
27410 {
27411 *x = best_x;
27412 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27413
27414 if (right_p)
27415 {
27416 *x += best_glyph->pixel_width;
27417 ++*hpos;
27418 }
27419
27420 *y = best_row->y;
27421 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27422 }
27423
27424 return best_glyph != NULL;
27425 }
27426 #endif /* not used */
27427
27428 /* Find the positions of the first and the last glyphs in window W's
27429 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27430 (assumed to be a string), and return in HLINFO's mouse_face_*
27431 members the pixel and column/row coordinates of those glyphs. */
27432
27433 static void
27434 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27435 Lisp_Object object,
27436 ptrdiff_t startpos, ptrdiff_t endpos)
27437 {
27438 int yb = window_text_bottom_y (w);
27439 struct glyph_row *r;
27440 struct glyph *g, *e;
27441 int gx;
27442 int found = 0;
27443
27444 /* Find the glyph row with at least one position in the range
27445 [STARTPOS..ENDPOS], and the first glyph in that row whose
27446 position belongs to that range. */
27447 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27448 r->enabled_p && r->y < yb;
27449 ++r)
27450 {
27451 if (!r->reversed_p)
27452 {
27453 g = r->glyphs[TEXT_AREA];
27454 e = g + r->used[TEXT_AREA];
27455 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27456 if (EQ (g->object, object)
27457 && startpos <= g->charpos && g->charpos <= endpos)
27458 {
27459 hlinfo->mouse_face_beg_row
27460 = MATRIX_ROW_VPOS (r, w->current_matrix);
27461 hlinfo->mouse_face_beg_y = r->y;
27462 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27463 hlinfo->mouse_face_beg_x = gx;
27464 found = 1;
27465 break;
27466 }
27467 }
27468 else
27469 {
27470 struct glyph *g1;
27471
27472 e = r->glyphs[TEXT_AREA];
27473 g = e + r->used[TEXT_AREA];
27474 for ( ; g > e; --g)
27475 if (EQ ((g-1)->object, object)
27476 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27477 {
27478 hlinfo->mouse_face_beg_row
27479 = MATRIX_ROW_VPOS (r, w->current_matrix);
27480 hlinfo->mouse_face_beg_y = r->y;
27481 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27482 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27483 gx += g1->pixel_width;
27484 hlinfo->mouse_face_beg_x = gx;
27485 found = 1;
27486 break;
27487 }
27488 }
27489 if (found)
27490 break;
27491 }
27492
27493 if (!found)
27494 return;
27495
27496 /* Starting with the next row, look for the first row which does NOT
27497 include any glyphs whose positions are in the range. */
27498 for (++r; r->enabled_p && r->y < yb; ++r)
27499 {
27500 g = r->glyphs[TEXT_AREA];
27501 e = g + r->used[TEXT_AREA];
27502 found = 0;
27503 for ( ; g < e; ++g)
27504 if (EQ (g->object, object)
27505 && startpos <= g->charpos && g->charpos <= endpos)
27506 {
27507 found = 1;
27508 break;
27509 }
27510 if (!found)
27511 break;
27512 }
27513
27514 /* The highlighted region ends on the previous row. */
27515 r--;
27516
27517 /* Set the end row and its vertical pixel coordinate. */
27518 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27519 hlinfo->mouse_face_end_y = r->y;
27520
27521 /* Compute and set the end column and the end column's horizontal
27522 pixel coordinate. */
27523 if (!r->reversed_p)
27524 {
27525 g = r->glyphs[TEXT_AREA];
27526 e = g + r->used[TEXT_AREA];
27527 for ( ; e > g; --e)
27528 if (EQ ((e-1)->object, object)
27529 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27530 break;
27531 hlinfo->mouse_face_end_col = e - g;
27532
27533 for (gx = r->x; g < e; ++g)
27534 gx += g->pixel_width;
27535 hlinfo->mouse_face_end_x = gx;
27536 }
27537 else
27538 {
27539 e = r->glyphs[TEXT_AREA];
27540 g = e + r->used[TEXT_AREA];
27541 for (gx = r->x ; e < g; ++e)
27542 {
27543 if (EQ (e->object, object)
27544 && startpos <= e->charpos && e->charpos <= endpos)
27545 break;
27546 gx += e->pixel_width;
27547 }
27548 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27549 hlinfo->mouse_face_end_x = gx;
27550 }
27551 }
27552
27553 #ifdef HAVE_WINDOW_SYSTEM
27554
27555 /* See if position X, Y is within a hot-spot of an image. */
27556
27557 static int
27558 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27559 {
27560 if (!CONSP (hot_spot))
27561 return 0;
27562
27563 if (EQ (XCAR (hot_spot), Qrect))
27564 {
27565 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27566 Lisp_Object rect = XCDR (hot_spot);
27567 Lisp_Object tem;
27568 if (!CONSP (rect))
27569 return 0;
27570 if (!CONSP (XCAR (rect)))
27571 return 0;
27572 if (!CONSP (XCDR (rect)))
27573 return 0;
27574 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27575 return 0;
27576 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27577 return 0;
27578 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27579 return 0;
27580 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27581 return 0;
27582 return 1;
27583 }
27584 else if (EQ (XCAR (hot_spot), Qcircle))
27585 {
27586 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27587 Lisp_Object circ = XCDR (hot_spot);
27588 Lisp_Object lr, lx0, ly0;
27589 if (CONSP (circ)
27590 && CONSP (XCAR (circ))
27591 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27592 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27593 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27594 {
27595 double r = XFLOATINT (lr);
27596 double dx = XINT (lx0) - x;
27597 double dy = XINT (ly0) - y;
27598 return (dx * dx + dy * dy <= r * r);
27599 }
27600 }
27601 else if (EQ (XCAR (hot_spot), Qpoly))
27602 {
27603 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27604 if (VECTORP (XCDR (hot_spot)))
27605 {
27606 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27607 Lisp_Object *poly = v->contents;
27608 ptrdiff_t n = v->header.size;
27609 ptrdiff_t i;
27610 int inside = 0;
27611 Lisp_Object lx, ly;
27612 int x0, y0;
27613
27614 /* Need an even number of coordinates, and at least 3 edges. */
27615 if (n < 6 || n & 1)
27616 return 0;
27617
27618 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27619 If count is odd, we are inside polygon. Pixels on edges
27620 may or may not be included depending on actual geometry of the
27621 polygon. */
27622 if ((lx = poly[n-2], !INTEGERP (lx))
27623 || (ly = poly[n-1], !INTEGERP (lx)))
27624 return 0;
27625 x0 = XINT (lx), y0 = XINT (ly);
27626 for (i = 0; i < n; i += 2)
27627 {
27628 int x1 = x0, y1 = y0;
27629 if ((lx = poly[i], !INTEGERP (lx))
27630 || (ly = poly[i+1], !INTEGERP (ly)))
27631 return 0;
27632 x0 = XINT (lx), y0 = XINT (ly);
27633
27634 /* Does this segment cross the X line? */
27635 if (x0 >= x)
27636 {
27637 if (x1 >= x)
27638 continue;
27639 }
27640 else if (x1 < x)
27641 continue;
27642 if (y > y0 && y > y1)
27643 continue;
27644 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27645 inside = !inside;
27646 }
27647 return inside;
27648 }
27649 }
27650 return 0;
27651 }
27652
27653 Lisp_Object
27654 find_hot_spot (Lisp_Object map, int x, int y)
27655 {
27656 while (CONSP (map))
27657 {
27658 if (CONSP (XCAR (map))
27659 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27660 return XCAR (map);
27661 map = XCDR (map);
27662 }
27663
27664 return Qnil;
27665 }
27666
27667 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27668 3, 3, 0,
27669 doc: /* Lookup in image map MAP coordinates X and Y.
27670 An image map is an alist where each element has the format (AREA ID PLIST).
27671 An AREA is specified as either a rectangle, a circle, or a polygon:
27672 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27673 pixel coordinates of the upper left and bottom right corners.
27674 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27675 and the radius of the circle; r may be a float or integer.
27676 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27677 vector describes one corner in the polygon.
27678 Returns the alist element for the first matching AREA in MAP. */)
27679 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27680 {
27681 if (NILP (map))
27682 return Qnil;
27683
27684 CHECK_NUMBER (x);
27685 CHECK_NUMBER (y);
27686
27687 return find_hot_spot (map,
27688 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27689 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27690 }
27691
27692
27693 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27694 static void
27695 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27696 {
27697 /* Do not change cursor shape while dragging mouse. */
27698 if (!NILP (do_mouse_tracking))
27699 return;
27700
27701 if (!NILP (pointer))
27702 {
27703 if (EQ (pointer, Qarrow))
27704 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27705 else if (EQ (pointer, Qhand))
27706 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27707 else if (EQ (pointer, Qtext))
27708 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27709 else if (EQ (pointer, intern ("hdrag")))
27710 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27711 #ifdef HAVE_X_WINDOWS
27712 else if (EQ (pointer, intern ("vdrag")))
27713 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27714 #endif
27715 else if (EQ (pointer, intern ("hourglass")))
27716 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27717 else if (EQ (pointer, Qmodeline))
27718 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27719 else
27720 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27721 }
27722
27723 if (cursor != No_Cursor)
27724 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27725 }
27726
27727 #endif /* HAVE_WINDOW_SYSTEM */
27728
27729 /* Take proper action when mouse has moved to the mode or header line
27730 or marginal area AREA of window W, x-position X and y-position Y.
27731 X is relative to the start of the text display area of W, so the
27732 width of bitmap areas and scroll bars must be subtracted to get a
27733 position relative to the start of the mode line. */
27734
27735 static void
27736 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27737 enum window_part area)
27738 {
27739 struct window *w = XWINDOW (window);
27740 struct frame *f = XFRAME (w->frame);
27741 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27742 #ifdef HAVE_WINDOW_SYSTEM
27743 Display_Info *dpyinfo;
27744 #endif
27745 Cursor cursor = No_Cursor;
27746 Lisp_Object pointer = Qnil;
27747 int dx, dy, width, height;
27748 ptrdiff_t charpos;
27749 Lisp_Object string, object = Qnil;
27750 Lisp_Object pos IF_LINT (= Qnil), help;
27751
27752 Lisp_Object mouse_face;
27753 int original_x_pixel = x;
27754 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27755 struct glyph_row *row IF_LINT (= 0);
27756
27757 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27758 {
27759 int x0;
27760 struct glyph *end;
27761
27762 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27763 returns them in row/column units! */
27764 string = mode_line_string (w, area, &x, &y, &charpos,
27765 &object, &dx, &dy, &width, &height);
27766
27767 row = (area == ON_MODE_LINE
27768 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27769 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27770
27771 /* Find the glyph under the mouse pointer. */
27772 if (row->mode_line_p && row->enabled_p)
27773 {
27774 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27775 end = glyph + row->used[TEXT_AREA];
27776
27777 for (x0 = original_x_pixel;
27778 glyph < end && x0 >= glyph->pixel_width;
27779 ++glyph)
27780 x0 -= glyph->pixel_width;
27781
27782 if (glyph >= end)
27783 glyph = NULL;
27784 }
27785 }
27786 else
27787 {
27788 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27789 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27790 returns them in row/column units! */
27791 string = marginal_area_string (w, area, &x, &y, &charpos,
27792 &object, &dx, &dy, &width, &height);
27793 }
27794
27795 help = Qnil;
27796
27797 #ifdef HAVE_WINDOW_SYSTEM
27798 if (IMAGEP (object))
27799 {
27800 Lisp_Object image_map, hotspot;
27801 if ((image_map = Fplist_get (XCDR (object), QCmap),
27802 !NILP (image_map))
27803 && (hotspot = find_hot_spot (image_map, dx, dy),
27804 CONSP (hotspot))
27805 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27806 {
27807 Lisp_Object plist;
27808
27809 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27810 If so, we could look for mouse-enter, mouse-leave
27811 properties in PLIST (and do something...). */
27812 hotspot = XCDR (hotspot);
27813 if (CONSP (hotspot)
27814 && (plist = XCAR (hotspot), CONSP (plist)))
27815 {
27816 pointer = Fplist_get (plist, Qpointer);
27817 if (NILP (pointer))
27818 pointer = Qhand;
27819 help = Fplist_get (plist, Qhelp_echo);
27820 if (!NILP (help))
27821 {
27822 help_echo_string = help;
27823 XSETWINDOW (help_echo_window, w);
27824 help_echo_object = w->contents;
27825 help_echo_pos = charpos;
27826 }
27827 }
27828 }
27829 if (NILP (pointer))
27830 pointer = Fplist_get (XCDR (object), QCpointer);
27831 }
27832 #endif /* HAVE_WINDOW_SYSTEM */
27833
27834 if (STRINGP (string))
27835 pos = make_number (charpos);
27836
27837 /* Set the help text and mouse pointer. If the mouse is on a part
27838 of the mode line without any text (e.g. past the right edge of
27839 the mode line text), use the default help text and pointer. */
27840 if (STRINGP (string) || area == ON_MODE_LINE)
27841 {
27842 /* Arrange to display the help by setting the global variables
27843 help_echo_string, help_echo_object, and help_echo_pos. */
27844 if (NILP (help))
27845 {
27846 if (STRINGP (string))
27847 help = Fget_text_property (pos, Qhelp_echo, string);
27848
27849 if (!NILP (help))
27850 {
27851 help_echo_string = help;
27852 XSETWINDOW (help_echo_window, w);
27853 help_echo_object = string;
27854 help_echo_pos = charpos;
27855 }
27856 else if (area == ON_MODE_LINE)
27857 {
27858 Lisp_Object default_help
27859 = buffer_local_value_1 (Qmode_line_default_help_echo,
27860 w->contents);
27861
27862 if (STRINGP (default_help))
27863 {
27864 help_echo_string = default_help;
27865 XSETWINDOW (help_echo_window, w);
27866 help_echo_object = Qnil;
27867 help_echo_pos = -1;
27868 }
27869 }
27870 }
27871
27872 #ifdef HAVE_WINDOW_SYSTEM
27873 /* Change the mouse pointer according to what is under it. */
27874 if (FRAME_WINDOW_P (f))
27875 {
27876 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27877 if (STRINGP (string))
27878 {
27879 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27880
27881 if (NILP (pointer))
27882 pointer = Fget_text_property (pos, Qpointer, string);
27883
27884 /* Change the mouse pointer according to what is under X/Y. */
27885 if (NILP (pointer)
27886 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27887 {
27888 Lisp_Object map;
27889 map = Fget_text_property (pos, Qlocal_map, string);
27890 if (!KEYMAPP (map))
27891 map = Fget_text_property (pos, Qkeymap, string);
27892 if (!KEYMAPP (map))
27893 cursor = dpyinfo->vertical_scroll_bar_cursor;
27894 }
27895 }
27896 else
27897 /* Default mode-line pointer. */
27898 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27899 }
27900 #endif
27901 }
27902
27903 /* Change the mouse face according to what is under X/Y. */
27904 if (STRINGP (string))
27905 {
27906 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27907 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27908 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27909 && glyph)
27910 {
27911 Lisp_Object b, e;
27912
27913 struct glyph * tmp_glyph;
27914
27915 int gpos;
27916 int gseq_length;
27917 int total_pixel_width;
27918 ptrdiff_t begpos, endpos, ignore;
27919
27920 int vpos, hpos;
27921
27922 b = Fprevious_single_property_change (make_number (charpos + 1),
27923 Qmouse_face, string, Qnil);
27924 if (NILP (b))
27925 begpos = 0;
27926 else
27927 begpos = XINT (b);
27928
27929 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27930 if (NILP (e))
27931 endpos = SCHARS (string);
27932 else
27933 endpos = XINT (e);
27934
27935 /* Calculate the glyph position GPOS of GLYPH in the
27936 displayed string, relative to the beginning of the
27937 highlighted part of the string.
27938
27939 Note: GPOS is different from CHARPOS. CHARPOS is the
27940 position of GLYPH in the internal string object. A mode
27941 line string format has structures which are converted to
27942 a flattened string by the Emacs Lisp interpreter. The
27943 internal string is an element of those structures. The
27944 displayed string is the flattened string. */
27945 tmp_glyph = row_start_glyph;
27946 while (tmp_glyph < glyph
27947 && (!(EQ (tmp_glyph->object, glyph->object)
27948 && begpos <= tmp_glyph->charpos
27949 && tmp_glyph->charpos < endpos)))
27950 tmp_glyph++;
27951 gpos = glyph - tmp_glyph;
27952
27953 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27954 the highlighted part of the displayed string to which
27955 GLYPH belongs. Note: GSEQ_LENGTH is different from
27956 SCHARS (STRING), because the latter returns the length of
27957 the internal string. */
27958 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27959 tmp_glyph > glyph
27960 && (!(EQ (tmp_glyph->object, glyph->object)
27961 && begpos <= tmp_glyph->charpos
27962 && tmp_glyph->charpos < endpos));
27963 tmp_glyph--)
27964 ;
27965 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27966
27967 /* Calculate the total pixel width of all the glyphs between
27968 the beginning of the highlighted area and GLYPH. */
27969 total_pixel_width = 0;
27970 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27971 total_pixel_width += tmp_glyph->pixel_width;
27972
27973 /* Pre calculation of re-rendering position. Note: X is in
27974 column units here, after the call to mode_line_string or
27975 marginal_area_string. */
27976 hpos = x - gpos;
27977 vpos = (area == ON_MODE_LINE
27978 ? (w->current_matrix)->nrows - 1
27979 : 0);
27980
27981 /* If GLYPH's position is included in the region that is
27982 already drawn in mouse face, we have nothing to do. */
27983 if ( EQ (window, hlinfo->mouse_face_window)
27984 && (!row->reversed_p
27985 ? (hlinfo->mouse_face_beg_col <= hpos
27986 && hpos < hlinfo->mouse_face_end_col)
27987 /* In R2L rows we swap BEG and END, see below. */
27988 : (hlinfo->mouse_face_end_col <= hpos
27989 && hpos < hlinfo->mouse_face_beg_col))
27990 && hlinfo->mouse_face_beg_row == vpos )
27991 return;
27992
27993 if (clear_mouse_face (hlinfo))
27994 cursor = No_Cursor;
27995
27996 if (!row->reversed_p)
27997 {
27998 hlinfo->mouse_face_beg_col = hpos;
27999 hlinfo->mouse_face_beg_x = original_x_pixel
28000 - (total_pixel_width + dx);
28001 hlinfo->mouse_face_end_col = hpos + gseq_length;
28002 hlinfo->mouse_face_end_x = 0;
28003 }
28004 else
28005 {
28006 /* In R2L rows, show_mouse_face expects BEG and END
28007 coordinates to be swapped. */
28008 hlinfo->mouse_face_end_col = hpos;
28009 hlinfo->mouse_face_end_x = original_x_pixel
28010 - (total_pixel_width + dx);
28011 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28012 hlinfo->mouse_face_beg_x = 0;
28013 }
28014
28015 hlinfo->mouse_face_beg_row = vpos;
28016 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28017 hlinfo->mouse_face_beg_y = 0;
28018 hlinfo->mouse_face_end_y = 0;
28019 hlinfo->mouse_face_past_end = 0;
28020 hlinfo->mouse_face_window = window;
28021
28022 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28023 charpos,
28024 0, 0, 0,
28025 &ignore,
28026 glyph->face_id,
28027 1);
28028 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28029
28030 if (NILP (pointer))
28031 pointer = Qhand;
28032 }
28033 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28034 clear_mouse_face (hlinfo);
28035 }
28036 #ifdef HAVE_WINDOW_SYSTEM
28037 if (FRAME_WINDOW_P (f))
28038 define_frame_cursor1 (f, cursor, pointer);
28039 #endif
28040 }
28041
28042
28043 /* EXPORT:
28044 Take proper action when the mouse has moved to position X, Y on
28045 frame F with regards to highlighting portions of display that have
28046 mouse-face properties. Also de-highlight portions of display where
28047 the mouse was before, set the mouse pointer shape as appropriate
28048 for the mouse coordinates, and activate help echo (tooltips).
28049 X and Y can be negative or out of range. */
28050
28051 void
28052 note_mouse_highlight (struct frame *f, int x, int y)
28053 {
28054 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28055 enum window_part part = ON_NOTHING;
28056 Lisp_Object window;
28057 struct window *w;
28058 Cursor cursor = No_Cursor;
28059 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28060 struct buffer *b;
28061
28062 /* When a menu is active, don't highlight because this looks odd. */
28063 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28064 if (popup_activated ())
28065 return;
28066 #endif
28067
28068 if (!f->glyphs_initialized_p
28069 || f->pointer_invisible)
28070 return;
28071
28072 hlinfo->mouse_face_mouse_x = x;
28073 hlinfo->mouse_face_mouse_y = y;
28074 hlinfo->mouse_face_mouse_frame = f;
28075
28076 if (hlinfo->mouse_face_defer)
28077 return;
28078
28079 /* Which window is that in? */
28080 window = window_from_coordinates (f, x, y, &part, 1);
28081
28082 /* If displaying active text in another window, clear that. */
28083 if (! EQ (window, hlinfo->mouse_face_window)
28084 /* Also clear if we move out of text area in same window. */
28085 || (!NILP (hlinfo->mouse_face_window)
28086 && !NILP (window)
28087 && part != ON_TEXT
28088 && part != ON_MODE_LINE
28089 && part != ON_HEADER_LINE))
28090 clear_mouse_face (hlinfo);
28091
28092 /* Not on a window -> return. */
28093 if (!WINDOWP (window))
28094 return;
28095
28096 /* Reset help_echo_string. It will get recomputed below. */
28097 help_echo_string = Qnil;
28098
28099 /* Convert to window-relative pixel coordinates. */
28100 w = XWINDOW (window);
28101 frame_to_window_pixel_xy (w, &x, &y);
28102
28103 #ifdef HAVE_WINDOW_SYSTEM
28104 /* Handle tool-bar window differently since it doesn't display a
28105 buffer. */
28106 if (EQ (window, f->tool_bar_window))
28107 {
28108 note_tool_bar_highlight (f, x, y);
28109 return;
28110 }
28111 #endif
28112
28113 /* Mouse is on the mode, header line or margin? */
28114 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28115 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28116 {
28117 note_mode_line_or_margin_highlight (window, x, y, part);
28118 return;
28119 }
28120
28121 #ifdef HAVE_WINDOW_SYSTEM
28122 if (part == ON_VERTICAL_BORDER)
28123 {
28124 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28125 help_echo_string = build_string ("drag-mouse-1: resize");
28126 }
28127 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28128 || part == ON_SCROLL_BAR)
28129 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28130 else
28131 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28132 #endif
28133
28134 /* Are we in a window whose display is up to date?
28135 And verify the buffer's text has not changed. */
28136 b = XBUFFER (w->contents);
28137 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
28138 {
28139 int hpos, vpos, dx, dy, area = LAST_AREA;
28140 ptrdiff_t pos;
28141 struct glyph *glyph;
28142 Lisp_Object object;
28143 Lisp_Object mouse_face = Qnil, position;
28144 Lisp_Object *overlay_vec = NULL;
28145 ptrdiff_t i, noverlays;
28146 struct buffer *obuf;
28147 ptrdiff_t obegv, ozv;
28148 int same_region;
28149
28150 /* Find the glyph under X/Y. */
28151 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28152
28153 #ifdef HAVE_WINDOW_SYSTEM
28154 /* Look for :pointer property on image. */
28155 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28156 {
28157 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28158 if (img != NULL && IMAGEP (img->spec))
28159 {
28160 Lisp_Object image_map, hotspot;
28161 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28162 !NILP (image_map))
28163 && (hotspot = find_hot_spot (image_map,
28164 glyph->slice.img.x + dx,
28165 glyph->slice.img.y + dy),
28166 CONSP (hotspot))
28167 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28168 {
28169 Lisp_Object plist;
28170
28171 /* Could check XCAR (hotspot) to see if we enter/leave
28172 this hot-spot.
28173 If so, we could look for mouse-enter, mouse-leave
28174 properties in PLIST (and do something...). */
28175 hotspot = XCDR (hotspot);
28176 if (CONSP (hotspot)
28177 && (plist = XCAR (hotspot), CONSP (plist)))
28178 {
28179 pointer = Fplist_get (plist, Qpointer);
28180 if (NILP (pointer))
28181 pointer = Qhand;
28182 help_echo_string = Fplist_get (plist, Qhelp_echo);
28183 if (!NILP (help_echo_string))
28184 {
28185 help_echo_window = window;
28186 help_echo_object = glyph->object;
28187 help_echo_pos = glyph->charpos;
28188 }
28189 }
28190 }
28191 if (NILP (pointer))
28192 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28193 }
28194 }
28195 #endif /* HAVE_WINDOW_SYSTEM */
28196
28197 /* Clear mouse face if X/Y not over text. */
28198 if (glyph == NULL
28199 || area != TEXT_AREA
28200 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28201 /* Glyph's OBJECT is an integer for glyphs inserted by the
28202 display engine for its internal purposes, like truncation
28203 and continuation glyphs and blanks beyond the end of
28204 line's text on text terminals. If we are over such a
28205 glyph, we are not over any text. */
28206 || INTEGERP (glyph->object)
28207 /* R2L rows have a stretch glyph at their front, which
28208 stands for no text, whereas L2R rows have no glyphs at
28209 all beyond the end of text. Treat such stretch glyphs
28210 like we do with NULL glyphs in L2R rows. */
28211 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28212 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28213 && glyph->type == STRETCH_GLYPH
28214 && glyph->avoid_cursor_p))
28215 {
28216 if (clear_mouse_face (hlinfo))
28217 cursor = No_Cursor;
28218 #ifdef HAVE_WINDOW_SYSTEM
28219 if (FRAME_WINDOW_P (f) && NILP (pointer))
28220 {
28221 if (area != TEXT_AREA)
28222 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28223 else
28224 pointer = Vvoid_text_area_pointer;
28225 }
28226 #endif
28227 goto set_cursor;
28228 }
28229
28230 pos = glyph->charpos;
28231 object = glyph->object;
28232 if (!STRINGP (object) && !BUFFERP (object))
28233 goto set_cursor;
28234
28235 /* If we get an out-of-range value, return now; avoid an error. */
28236 if (BUFFERP (object) && pos > BUF_Z (b))
28237 goto set_cursor;
28238
28239 /* Make the window's buffer temporarily current for
28240 overlays_at and compute_char_face. */
28241 obuf = current_buffer;
28242 current_buffer = b;
28243 obegv = BEGV;
28244 ozv = ZV;
28245 BEGV = BEG;
28246 ZV = Z;
28247
28248 /* Is this char mouse-active or does it have help-echo? */
28249 position = make_number (pos);
28250
28251 if (BUFFERP (object))
28252 {
28253 /* Put all the overlays we want in a vector in overlay_vec. */
28254 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28255 /* Sort overlays into increasing priority order. */
28256 noverlays = sort_overlays (overlay_vec, noverlays, w);
28257 }
28258 else
28259 noverlays = 0;
28260
28261 if (NILP (Vmouse_highlight))
28262 {
28263 clear_mouse_face (hlinfo);
28264 goto check_help_echo;
28265 }
28266
28267 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28268
28269 if (same_region)
28270 cursor = No_Cursor;
28271
28272 /* Check mouse-face highlighting. */
28273 if (! same_region
28274 /* If there exists an overlay with mouse-face overlapping
28275 the one we are currently highlighting, we have to
28276 check if we enter the overlapping overlay, and then
28277 highlight only that. */
28278 || (OVERLAYP (hlinfo->mouse_face_overlay)
28279 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28280 {
28281 /* Find the highest priority overlay with a mouse-face. */
28282 Lisp_Object overlay = Qnil;
28283 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28284 {
28285 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28286 if (!NILP (mouse_face))
28287 overlay = overlay_vec[i];
28288 }
28289
28290 /* If we're highlighting the same overlay as before, there's
28291 no need to do that again. */
28292 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28293 goto check_help_echo;
28294 hlinfo->mouse_face_overlay = overlay;
28295
28296 /* Clear the display of the old active region, if any. */
28297 if (clear_mouse_face (hlinfo))
28298 cursor = No_Cursor;
28299
28300 /* If no overlay applies, get a text property. */
28301 if (NILP (overlay))
28302 mouse_face = Fget_text_property (position, Qmouse_face, object);
28303
28304 /* Next, compute the bounds of the mouse highlighting and
28305 display it. */
28306 if (!NILP (mouse_face) && STRINGP (object))
28307 {
28308 /* The mouse-highlighting comes from a display string
28309 with a mouse-face. */
28310 Lisp_Object s, e;
28311 ptrdiff_t ignore;
28312
28313 s = Fprevious_single_property_change
28314 (make_number (pos + 1), Qmouse_face, object, Qnil);
28315 e = Fnext_single_property_change
28316 (position, Qmouse_face, object, Qnil);
28317 if (NILP (s))
28318 s = make_number (0);
28319 if (NILP (e))
28320 e = make_number (SCHARS (object) - 1);
28321 mouse_face_from_string_pos (w, hlinfo, object,
28322 XINT (s), XINT (e));
28323 hlinfo->mouse_face_past_end = 0;
28324 hlinfo->mouse_face_window = window;
28325 hlinfo->mouse_face_face_id
28326 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28327 glyph->face_id, 1);
28328 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28329 cursor = No_Cursor;
28330 }
28331 else
28332 {
28333 /* The mouse-highlighting, if any, comes from an overlay
28334 or text property in the buffer. */
28335 Lisp_Object buffer IF_LINT (= Qnil);
28336 Lisp_Object disp_string IF_LINT (= Qnil);
28337
28338 if (STRINGP (object))
28339 {
28340 /* If we are on a display string with no mouse-face,
28341 check if the text under it has one. */
28342 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28343 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28344 pos = string_buffer_position (object, start);
28345 if (pos > 0)
28346 {
28347 mouse_face = get_char_property_and_overlay
28348 (make_number (pos), Qmouse_face, w->contents, &overlay);
28349 buffer = w->contents;
28350 disp_string = object;
28351 }
28352 }
28353 else
28354 {
28355 buffer = object;
28356 disp_string = Qnil;
28357 }
28358
28359 if (!NILP (mouse_face))
28360 {
28361 Lisp_Object before, after;
28362 Lisp_Object before_string, after_string;
28363 /* To correctly find the limits of mouse highlight
28364 in a bidi-reordered buffer, we must not use the
28365 optimization of limiting the search in
28366 previous-single-property-change and
28367 next-single-property-change, because
28368 rows_from_pos_range needs the real start and end
28369 positions to DTRT in this case. That's because
28370 the first row visible in a window does not
28371 necessarily display the character whose position
28372 is the smallest. */
28373 Lisp_Object lim1 =
28374 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28375 ? Fmarker_position (w->start)
28376 : Qnil;
28377 Lisp_Object lim2 =
28378 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28379 ? make_number (BUF_Z (XBUFFER (buffer))
28380 - XFASTINT (w->window_end_pos))
28381 : Qnil;
28382
28383 if (NILP (overlay))
28384 {
28385 /* Handle the text property case. */
28386 before = Fprevious_single_property_change
28387 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28388 after = Fnext_single_property_change
28389 (make_number (pos), Qmouse_face, buffer, lim2);
28390 before_string = after_string = Qnil;
28391 }
28392 else
28393 {
28394 /* Handle the overlay case. */
28395 before = Foverlay_start (overlay);
28396 after = Foverlay_end (overlay);
28397 before_string = Foverlay_get (overlay, Qbefore_string);
28398 after_string = Foverlay_get (overlay, Qafter_string);
28399
28400 if (!STRINGP (before_string)) before_string = Qnil;
28401 if (!STRINGP (after_string)) after_string = Qnil;
28402 }
28403
28404 mouse_face_from_buffer_pos (window, hlinfo, pos,
28405 NILP (before)
28406 ? 1
28407 : XFASTINT (before),
28408 NILP (after)
28409 ? BUF_Z (XBUFFER (buffer))
28410 : XFASTINT (after),
28411 before_string, after_string,
28412 disp_string);
28413 cursor = No_Cursor;
28414 }
28415 }
28416 }
28417
28418 check_help_echo:
28419
28420 /* Look for a `help-echo' property. */
28421 if (NILP (help_echo_string)) {
28422 Lisp_Object help, overlay;
28423
28424 /* Check overlays first. */
28425 help = overlay = Qnil;
28426 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28427 {
28428 overlay = overlay_vec[i];
28429 help = Foverlay_get (overlay, Qhelp_echo);
28430 }
28431
28432 if (!NILP (help))
28433 {
28434 help_echo_string = help;
28435 help_echo_window = window;
28436 help_echo_object = overlay;
28437 help_echo_pos = pos;
28438 }
28439 else
28440 {
28441 Lisp_Object obj = glyph->object;
28442 ptrdiff_t charpos = glyph->charpos;
28443
28444 /* Try text properties. */
28445 if (STRINGP (obj)
28446 && charpos >= 0
28447 && charpos < SCHARS (obj))
28448 {
28449 help = Fget_text_property (make_number (charpos),
28450 Qhelp_echo, obj);
28451 if (NILP (help))
28452 {
28453 /* If the string itself doesn't specify a help-echo,
28454 see if the buffer text ``under'' it does. */
28455 struct glyph_row *r
28456 = MATRIX_ROW (w->current_matrix, vpos);
28457 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28458 ptrdiff_t p = string_buffer_position (obj, start);
28459 if (p > 0)
28460 {
28461 help = Fget_char_property (make_number (p),
28462 Qhelp_echo, w->contents);
28463 if (!NILP (help))
28464 {
28465 charpos = p;
28466 obj = w->contents;
28467 }
28468 }
28469 }
28470 }
28471 else if (BUFFERP (obj)
28472 && charpos >= BEGV
28473 && charpos < ZV)
28474 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28475 obj);
28476
28477 if (!NILP (help))
28478 {
28479 help_echo_string = help;
28480 help_echo_window = window;
28481 help_echo_object = obj;
28482 help_echo_pos = charpos;
28483 }
28484 }
28485 }
28486
28487 #ifdef HAVE_WINDOW_SYSTEM
28488 /* Look for a `pointer' property. */
28489 if (FRAME_WINDOW_P (f) && NILP (pointer))
28490 {
28491 /* Check overlays first. */
28492 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28493 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28494
28495 if (NILP (pointer))
28496 {
28497 Lisp_Object obj = glyph->object;
28498 ptrdiff_t charpos = glyph->charpos;
28499
28500 /* Try text properties. */
28501 if (STRINGP (obj)
28502 && charpos >= 0
28503 && charpos < SCHARS (obj))
28504 {
28505 pointer = Fget_text_property (make_number (charpos),
28506 Qpointer, obj);
28507 if (NILP (pointer))
28508 {
28509 /* If the string itself doesn't specify a pointer,
28510 see if the buffer text ``under'' it does. */
28511 struct glyph_row *r
28512 = MATRIX_ROW (w->current_matrix, vpos);
28513 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28514 ptrdiff_t p = string_buffer_position (obj, start);
28515 if (p > 0)
28516 pointer = Fget_char_property (make_number (p),
28517 Qpointer, w->contents);
28518 }
28519 }
28520 else if (BUFFERP (obj)
28521 && charpos >= BEGV
28522 && charpos < ZV)
28523 pointer = Fget_text_property (make_number (charpos),
28524 Qpointer, obj);
28525 }
28526 }
28527 #endif /* HAVE_WINDOW_SYSTEM */
28528
28529 BEGV = obegv;
28530 ZV = ozv;
28531 current_buffer = obuf;
28532 }
28533
28534 set_cursor:
28535
28536 #ifdef HAVE_WINDOW_SYSTEM
28537 if (FRAME_WINDOW_P (f))
28538 define_frame_cursor1 (f, cursor, pointer);
28539 #else
28540 /* This is here to prevent a compiler error, about "label at end of
28541 compound statement". */
28542 return;
28543 #endif
28544 }
28545
28546
28547 /* EXPORT for RIF:
28548 Clear any mouse-face on window W. This function is part of the
28549 redisplay interface, and is called from try_window_id and similar
28550 functions to ensure the mouse-highlight is off. */
28551
28552 void
28553 x_clear_window_mouse_face (struct window *w)
28554 {
28555 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28556 Lisp_Object window;
28557
28558 block_input ();
28559 XSETWINDOW (window, w);
28560 if (EQ (window, hlinfo->mouse_face_window))
28561 clear_mouse_face (hlinfo);
28562 unblock_input ();
28563 }
28564
28565
28566 /* EXPORT:
28567 Just discard the mouse face information for frame F, if any.
28568 This is used when the size of F is changed. */
28569
28570 void
28571 cancel_mouse_face (struct frame *f)
28572 {
28573 Lisp_Object window;
28574 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28575
28576 window = hlinfo->mouse_face_window;
28577 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28578 {
28579 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28580 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28581 hlinfo->mouse_face_window = Qnil;
28582 }
28583 }
28584
28585
28586 \f
28587 /***********************************************************************
28588 Exposure Events
28589 ***********************************************************************/
28590
28591 #ifdef HAVE_WINDOW_SYSTEM
28592
28593 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28594 which intersects rectangle R. R is in window-relative coordinates. */
28595
28596 static void
28597 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28598 enum glyph_row_area area)
28599 {
28600 struct glyph *first = row->glyphs[area];
28601 struct glyph *end = row->glyphs[area] + row->used[area];
28602 struct glyph *last;
28603 int first_x, start_x, x;
28604
28605 if (area == TEXT_AREA && row->fill_line_p)
28606 /* If row extends face to end of line write the whole line. */
28607 draw_glyphs (w, 0, row, area,
28608 0, row->used[area],
28609 DRAW_NORMAL_TEXT, 0);
28610 else
28611 {
28612 /* Set START_X to the window-relative start position for drawing glyphs of
28613 AREA. The first glyph of the text area can be partially visible.
28614 The first glyphs of other areas cannot. */
28615 start_x = window_box_left_offset (w, area);
28616 x = start_x;
28617 if (area == TEXT_AREA)
28618 x += row->x;
28619
28620 /* Find the first glyph that must be redrawn. */
28621 while (first < end
28622 && x + first->pixel_width < r->x)
28623 {
28624 x += first->pixel_width;
28625 ++first;
28626 }
28627
28628 /* Find the last one. */
28629 last = first;
28630 first_x = x;
28631 while (last < end
28632 && x < r->x + r->width)
28633 {
28634 x += last->pixel_width;
28635 ++last;
28636 }
28637
28638 /* Repaint. */
28639 if (last > first)
28640 draw_glyphs (w, first_x - start_x, row, area,
28641 first - row->glyphs[area], last - row->glyphs[area],
28642 DRAW_NORMAL_TEXT, 0);
28643 }
28644 }
28645
28646
28647 /* Redraw the parts of the glyph row ROW on window W intersecting
28648 rectangle R. R is in window-relative coordinates. Value is
28649 non-zero if mouse-face was overwritten. */
28650
28651 static int
28652 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28653 {
28654 eassert (row->enabled_p);
28655
28656 if (row->mode_line_p || w->pseudo_window_p)
28657 draw_glyphs (w, 0, row, TEXT_AREA,
28658 0, row->used[TEXT_AREA],
28659 DRAW_NORMAL_TEXT, 0);
28660 else
28661 {
28662 if (row->used[LEFT_MARGIN_AREA])
28663 expose_area (w, row, r, LEFT_MARGIN_AREA);
28664 if (row->used[TEXT_AREA])
28665 expose_area (w, row, r, TEXT_AREA);
28666 if (row->used[RIGHT_MARGIN_AREA])
28667 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28668 draw_row_fringe_bitmaps (w, row);
28669 }
28670
28671 return row->mouse_face_p;
28672 }
28673
28674
28675 /* Redraw those parts of glyphs rows during expose event handling that
28676 overlap other rows. Redrawing of an exposed line writes over parts
28677 of lines overlapping that exposed line; this function fixes that.
28678
28679 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28680 row in W's current matrix that is exposed and overlaps other rows.
28681 LAST_OVERLAPPING_ROW is the last such row. */
28682
28683 static void
28684 expose_overlaps (struct window *w,
28685 struct glyph_row *first_overlapping_row,
28686 struct glyph_row *last_overlapping_row,
28687 XRectangle *r)
28688 {
28689 struct glyph_row *row;
28690
28691 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28692 if (row->overlapping_p)
28693 {
28694 eassert (row->enabled_p && !row->mode_line_p);
28695
28696 row->clip = r;
28697 if (row->used[LEFT_MARGIN_AREA])
28698 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28699
28700 if (row->used[TEXT_AREA])
28701 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28702
28703 if (row->used[RIGHT_MARGIN_AREA])
28704 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28705 row->clip = NULL;
28706 }
28707 }
28708
28709
28710 /* Return non-zero if W's cursor intersects rectangle R. */
28711
28712 static int
28713 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28714 {
28715 XRectangle cr, result;
28716 struct glyph *cursor_glyph;
28717 struct glyph_row *row;
28718
28719 if (w->phys_cursor.vpos >= 0
28720 && w->phys_cursor.vpos < w->current_matrix->nrows
28721 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28722 row->enabled_p)
28723 && row->cursor_in_fringe_p)
28724 {
28725 /* Cursor is in the fringe. */
28726 cr.x = window_box_right_offset (w,
28727 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28728 ? RIGHT_MARGIN_AREA
28729 : TEXT_AREA));
28730 cr.y = row->y;
28731 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28732 cr.height = row->height;
28733 return x_intersect_rectangles (&cr, r, &result);
28734 }
28735
28736 cursor_glyph = get_phys_cursor_glyph (w);
28737 if (cursor_glyph)
28738 {
28739 /* r is relative to W's box, but w->phys_cursor.x is relative
28740 to left edge of W's TEXT area. Adjust it. */
28741 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28742 cr.y = w->phys_cursor.y;
28743 cr.width = cursor_glyph->pixel_width;
28744 cr.height = w->phys_cursor_height;
28745 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28746 I assume the effect is the same -- and this is portable. */
28747 return x_intersect_rectangles (&cr, r, &result);
28748 }
28749 /* If we don't understand the format, pretend we're not in the hot-spot. */
28750 return 0;
28751 }
28752
28753
28754 /* EXPORT:
28755 Draw a vertical window border to the right of window W if W doesn't
28756 have vertical scroll bars. */
28757
28758 void
28759 x_draw_vertical_border (struct window *w)
28760 {
28761 struct frame *f = XFRAME (WINDOW_FRAME (w));
28762
28763 /* We could do better, if we knew what type of scroll-bar the adjacent
28764 windows (on either side) have... But we don't :-(
28765 However, I think this works ok. ++KFS 2003-04-25 */
28766
28767 /* Redraw borders between horizontally adjacent windows. Don't
28768 do it for frames with vertical scroll bars because either the
28769 right scroll bar of a window, or the left scroll bar of its
28770 neighbor will suffice as a border. */
28771 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28772 return;
28773
28774 /* Note: It is necessary to redraw both the left and the right
28775 borders, for when only this single window W is being
28776 redisplayed. */
28777 if (!WINDOW_RIGHTMOST_P (w)
28778 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28779 {
28780 int x0, x1, y0, y1;
28781
28782 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28783 y1 -= 1;
28784
28785 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28786 x1 -= 1;
28787
28788 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28789 }
28790 if (!WINDOW_LEFTMOST_P (w)
28791 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28792 {
28793 int x0, x1, y0, y1;
28794
28795 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28796 y1 -= 1;
28797
28798 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28799 x0 -= 1;
28800
28801 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28802 }
28803 }
28804
28805
28806 /* Redraw the part of window W intersection rectangle FR. Pixel
28807 coordinates in FR are frame-relative. Call this function with
28808 input blocked. Value is non-zero if the exposure overwrites
28809 mouse-face. */
28810
28811 static int
28812 expose_window (struct window *w, XRectangle *fr)
28813 {
28814 struct frame *f = XFRAME (w->frame);
28815 XRectangle wr, r;
28816 int mouse_face_overwritten_p = 0;
28817
28818 /* If window is not yet fully initialized, do nothing. This can
28819 happen when toolkit scroll bars are used and a window is split.
28820 Reconfiguring the scroll bar will generate an expose for a newly
28821 created window. */
28822 if (w->current_matrix == NULL)
28823 return 0;
28824
28825 /* When we're currently updating the window, display and current
28826 matrix usually don't agree. Arrange for a thorough display
28827 later. */
28828 if (w->must_be_updated_p)
28829 {
28830 SET_FRAME_GARBAGED (f);
28831 return 0;
28832 }
28833
28834 /* Frame-relative pixel rectangle of W. */
28835 wr.x = WINDOW_LEFT_EDGE_X (w);
28836 wr.y = WINDOW_TOP_EDGE_Y (w);
28837 wr.width = WINDOW_TOTAL_WIDTH (w);
28838 wr.height = WINDOW_TOTAL_HEIGHT (w);
28839
28840 if (x_intersect_rectangles (fr, &wr, &r))
28841 {
28842 int yb = window_text_bottom_y (w);
28843 struct glyph_row *row;
28844 int cursor_cleared_p, phys_cursor_on_p;
28845 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28846
28847 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28848 r.x, r.y, r.width, r.height));
28849
28850 /* Convert to window coordinates. */
28851 r.x -= WINDOW_LEFT_EDGE_X (w);
28852 r.y -= WINDOW_TOP_EDGE_Y (w);
28853
28854 /* Turn off the cursor. */
28855 if (!w->pseudo_window_p
28856 && phys_cursor_in_rect_p (w, &r))
28857 {
28858 x_clear_cursor (w);
28859 cursor_cleared_p = 1;
28860 }
28861 else
28862 cursor_cleared_p = 0;
28863
28864 /* If the row containing the cursor extends face to end of line,
28865 then expose_area might overwrite the cursor outside the
28866 rectangle and thus notice_overwritten_cursor might clear
28867 w->phys_cursor_on_p. We remember the original value and
28868 check later if it is changed. */
28869 phys_cursor_on_p = w->phys_cursor_on_p;
28870
28871 /* Update lines intersecting rectangle R. */
28872 first_overlapping_row = last_overlapping_row = NULL;
28873 for (row = w->current_matrix->rows;
28874 row->enabled_p;
28875 ++row)
28876 {
28877 int y0 = row->y;
28878 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28879
28880 if ((y0 >= r.y && y0 < r.y + r.height)
28881 || (y1 > r.y && y1 < r.y + r.height)
28882 || (r.y >= y0 && r.y < y1)
28883 || (r.y + r.height > y0 && r.y + r.height < y1))
28884 {
28885 /* A header line may be overlapping, but there is no need
28886 to fix overlapping areas for them. KFS 2005-02-12 */
28887 if (row->overlapping_p && !row->mode_line_p)
28888 {
28889 if (first_overlapping_row == NULL)
28890 first_overlapping_row = row;
28891 last_overlapping_row = row;
28892 }
28893
28894 row->clip = fr;
28895 if (expose_line (w, row, &r))
28896 mouse_face_overwritten_p = 1;
28897 row->clip = NULL;
28898 }
28899 else if (row->overlapping_p)
28900 {
28901 /* We must redraw a row overlapping the exposed area. */
28902 if (y0 < r.y
28903 ? y0 + row->phys_height > r.y
28904 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28905 {
28906 if (first_overlapping_row == NULL)
28907 first_overlapping_row = row;
28908 last_overlapping_row = row;
28909 }
28910 }
28911
28912 if (y1 >= yb)
28913 break;
28914 }
28915
28916 /* Display the mode line if there is one. */
28917 if (WINDOW_WANTS_MODELINE_P (w)
28918 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28919 row->enabled_p)
28920 && row->y < r.y + r.height)
28921 {
28922 if (expose_line (w, row, &r))
28923 mouse_face_overwritten_p = 1;
28924 }
28925
28926 if (!w->pseudo_window_p)
28927 {
28928 /* Fix the display of overlapping rows. */
28929 if (first_overlapping_row)
28930 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28931 fr);
28932
28933 /* Draw border between windows. */
28934 x_draw_vertical_border (w);
28935
28936 /* Turn the cursor on again. */
28937 if (cursor_cleared_p
28938 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28939 update_window_cursor (w, 1);
28940 }
28941 }
28942
28943 return mouse_face_overwritten_p;
28944 }
28945
28946
28947
28948 /* Redraw (parts) of all windows in the window tree rooted at W that
28949 intersect R. R contains frame pixel coordinates. Value is
28950 non-zero if the exposure overwrites mouse-face. */
28951
28952 static int
28953 expose_window_tree (struct window *w, XRectangle *r)
28954 {
28955 struct frame *f = XFRAME (w->frame);
28956 int mouse_face_overwritten_p = 0;
28957
28958 while (w && !FRAME_GARBAGED_P (f))
28959 {
28960 if (WINDOWP (w->contents))
28961 mouse_face_overwritten_p
28962 |= expose_window_tree (XWINDOW (w->contents), r);
28963 else
28964 mouse_face_overwritten_p |= expose_window (w, r);
28965
28966 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28967 }
28968
28969 return mouse_face_overwritten_p;
28970 }
28971
28972
28973 /* EXPORT:
28974 Redisplay an exposed area of frame F. X and Y are the upper-left
28975 corner of the exposed rectangle. W and H are width and height of
28976 the exposed area. All are pixel values. W or H zero means redraw
28977 the entire frame. */
28978
28979 void
28980 expose_frame (struct frame *f, int x, int y, int w, int h)
28981 {
28982 XRectangle r;
28983 int mouse_face_overwritten_p = 0;
28984
28985 TRACE ((stderr, "expose_frame "));
28986
28987 /* No need to redraw if frame will be redrawn soon. */
28988 if (FRAME_GARBAGED_P (f))
28989 {
28990 TRACE ((stderr, " garbaged\n"));
28991 return;
28992 }
28993
28994 /* If basic faces haven't been realized yet, there is no point in
28995 trying to redraw anything. This can happen when we get an expose
28996 event while Emacs is starting, e.g. by moving another window. */
28997 if (FRAME_FACE_CACHE (f) == NULL
28998 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28999 {
29000 TRACE ((stderr, " no faces\n"));
29001 return;
29002 }
29003
29004 if (w == 0 || h == 0)
29005 {
29006 r.x = r.y = 0;
29007 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29008 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29009 }
29010 else
29011 {
29012 r.x = x;
29013 r.y = y;
29014 r.width = w;
29015 r.height = h;
29016 }
29017
29018 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29019 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29020
29021 if (WINDOWP (f->tool_bar_window))
29022 mouse_face_overwritten_p
29023 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29024
29025 #ifdef HAVE_X_WINDOWS
29026 #ifndef MSDOS
29027 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29028 if (WINDOWP (f->menu_bar_window))
29029 mouse_face_overwritten_p
29030 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29031 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29032 #endif
29033 #endif
29034
29035 /* Some window managers support a focus-follows-mouse style with
29036 delayed raising of frames. Imagine a partially obscured frame,
29037 and moving the mouse into partially obscured mouse-face on that
29038 frame. The visible part of the mouse-face will be highlighted,
29039 then the WM raises the obscured frame. With at least one WM, KDE
29040 2.1, Emacs is not getting any event for the raising of the frame
29041 (even tried with SubstructureRedirectMask), only Expose events.
29042 These expose events will draw text normally, i.e. not
29043 highlighted. Which means we must redo the highlight here.
29044 Subsume it under ``we love X''. --gerd 2001-08-15 */
29045 /* Included in Windows version because Windows most likely does not
29046 do the right thing if any third party tool offers
29047 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29048 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29049 {
29050 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29051 if (f == hlinfo->mouse_face_mouse_frame)
29052 {
29053 int mouse_x = hlinfo->mouse_face_mouse_x;
29054 int mouse_y = hlinfo->mouse_face_mouse_y;
29055 clear_mouse_face (hlinfo);
29056 note_mouse_highlight (f, mouse_x, mouse_y);
29057 }
29058 }
29059 }
29060
29061
29062 /* EXPORT:
29063 Determine the intersection of two rectangles R1 and R2. Return
29064 the intersection in *RESULT. Value is non-zero if RESULT is not
29065 empty. */
29066
29067 int
29068 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29069 {
29070 XRectangle *left, *right;
29071 XRectangle *upper, *lower;
29072 int intersection_p = 0;
29073
29074 /* Rearrange so that R1 is the left-most rectangle. */
29075 if (r1->x < r2->x)
29076 left = r1, right = r2;
29077 else
29078 left = r2, right = r1;
29079
29080 /* X0 of the intersection is right.x0, if this is inside R1,
29081 otherwise there is no intersection. */
29082 if (right->x <= left->x + left->width)
29083 {
29084 result->x = right->x;
29085
29086 /* The right end of the intersection is the minimum of
29087 the right ends of left and right. */
29088 result->width = (min (left->x + left->width, right->x + right->width)
29089 - result->x);
29090
29091 /* Same game for Y. */
29092 if (r1->y < r2->y)
29093 upper = r1, lower = r2;
29094 else
29095 upper = r2, lower = r1;
29096
29097 /* The upper end of the intersection is lower.y0, if this is inside
29098 of upper. Otherwise, there is no intersection. */
29099 if (lower->y <= upper->y + upper->height)
29100 {
29101 result->y = lower->y;
29102
29103 /* The lower end of the intersection is the minimum of the lower
29104 ends of upper and lower. */
29105 result->height = (min (lower->y + lower->height,
29106 upper->y + upper->height)
29107 - result->y);
29108 intersection_p = 1;
29109 }
29110 }
29111
29112 return intersection_p;
29113 }
29114
29115 #endif /* HAVE_WINDOW_SYSTEM */
29116
29117 \f
29118 /***********************************************************************
29119 Initialization
29120 ***********************************************************************/
29121
29122 void
29123 syms_of_xdisp (void)
29124 {
29125 Vwith_echo_area_save_vector = Qnil;
29126 staticpro (&Vwith_echo_area_save_vector);
29127
29128 Vmessage_stack = Qnil;
29129 staticpro (&Vmessage_stack);
29130
29131 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29132 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29133
29134 message_dolog_marker1 = Fmake_marker ();
29135 staticpro (&message_dolog_marker1);
29136 message_dolog_marker2 = Fmake_marker ();
29137 staticpro (&message_dolog_marker2);
29138 message_dolog_marker3 = Fmake_marker ();
29139 staticpro (&message_dolog_marker3);
29140
29141 #ifdef GLYPH_DEBUG
29142 defsubr (&Sdump_frame_glyph_matrix);
29143 defsubr (&Sdump_glyph_matrix);
29144 defsubr (&Sdump_glyph_row);
29145 defsubr (&Sdump_tool_bar_row);
29146 defsubr (&Strace_redisplay);
29147 defsubr (&Strace_to_stderr);
29148 #endif
29149 #ifdef HAVE_WINDOW_SYSTEM
29150 defsubr (&Stool_bar_lines_needed);
29151 defsubr (&Slookup_image_map);
29152 #endif
29153 defsubr (&Sline_pixel_height);
29154 defsubr (&Sformat_mode_line);
29155 defsubr (&Sinvisible_p);
29156 defsubr (&Scurrent_bidi_paragraph_direction);
29157 defsubr (&Smove_point_visually);
29158
29159 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29160 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29161 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29162 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29163 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29164 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29165 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29166 DEFSYM (Qeval, "eval");
29167 DEFSYM (QCdata, ":data");
29168 DEFSYM (Qdisplay, "display");
29169 DEFSYM (Qspace_width, "space-width");
29170 DEFSYM (Qraise, "raise");
29171 DEFSYM (Qslice, "slice");
29172 DEFSYM (Qspace, "space");
29173 DEFSYM (Qmargin, "margin");
29174 DEFSYM (Qpointer, "pointer");
29175 DEFSYM (Qleft_margin, "left-margin");
29176 DEFSYM (Qright_margin, "right-margin");
29177 DEFSYM (Qcenter, "center");
29178 DEFSYM (Qline_height, "line-height");
29179 DEFSYM (QCalign_to, ":align-to");
29180 DEFSYM (QCrelative_width, ":relative-width");
29181 DEFSYM (QCrelative_height, ":relative-height");
29182 DEFSYM (QCeval, ":eval");
29183 DEFSYM (QCpropertize, ":propertize");
29184 DEFSYM (QCfile, ":file");
29185 DEFSYM (Qfontified, "fontified");
29186 DEFSYM (Qfontification_functions, "fontification-functions");
29187 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29188 DEFSYM (Qescape_glyph, "escape-glyph");
29189 DEFSYM (Qnobreak_space, "nobreak-space");
29190 DEFSYM (Qimage, "image");
29191 DEFSYM (Qtext, "text");
29192 DEFSYM (Qboth, "both");
29193 DEFSYM (Qboth_horiz, "both-horiz");
29194 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29195 DEFSYM (QCmap, ":map");
29196 DEFSYM (QCpointer, ":pointer");
29197 DEFSYM (Qrect, "rect");
29198 DEFSYM (Qcircle, "circle");
29199 DEFSYM (Qpoly, "poly");
29200 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29201 DEFSYM (Qgrow_only, "grow-only");
29202 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29203 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29204 DEFSYM (Qposition, "position");
29205 DEFSYM (Qbuffer_position, "buffer-position");
29206 DEFSYM (Qobject, "object");
29207 DEFSYM (Qbar, "bar");
29208 DEFSYM (Qhbar, "hbar");
29209 DEFSYM (Qbox, "box");
29210 DEFSYM (Qhollow, "hollow");
29211 DEFSYM (Qhand, "hand");
29212 DEFSYM (Qarrow, "arrow");
29213 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29214
29215 list_of_error = list1 (list2 (intern_c_string ("error"),
29216 intern_c_string ("void-variable")));
29217 staticpro (&list_of_error);
29218
29219 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29220 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29221 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29222 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29223
29224 echo_buffer[0] = echo_buffer[1] = Qnil;
29225 staticpro (&echo_buffer[0]);
29226 staticpro (&echo_buffer[1]);
29227
29228 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29229 staticpro (&echo_area_buffer[0]);
29230 staticpro (&echo_area_buffer[1]);
29231
29232 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29233 staticpro (&Vmessages_buffer_name);
29234
29235 mode_line_proptrans_alist = Qnil;
29236 staticpro (&mode_line_proptrans_alist);
29237 mode_line_string_list = Qnil;
29238 staticpro (&mode_line_string_list);
29239 mode_line_string_face = Qnil;
29240 staticpro (&mode_line_string_face);
29241 mode_line_string_face_prop = Qnil;
29242 staticpro (&mode_line_string_face_prop);
29243 Vmode_line_unwind_vector = Qnil;
29244 staticpro (&Vmode_line_unwind_vector);
29245
29246 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29247
29248 help_echo_string = Qnil;
29249 staticpro (&help_echo_string);
29250 help_echo_object = Qnil;
29251 staticpro (&help_echo_object);
29252 help_echo_window = Qnil;
29253 staticpro (&help_echo_window);
29254 previous_help_echo_string = Qnil;
29255 staticpro (&previous_help_echo_string);
29256 help_echo_pos = -1;
29257
29258 DEFSYM (Qright_to_left, "right-to-left");
29259 DEFSYM (Qleft_to_right, "left-to-right");
29260
29261 #ifdef HAVE_WINDOW_SYSTEM
29262 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29263 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29264 For example, if a block cursor is over a tab, it will be drawn as
29265 wide as that tab on the display. */);
29266 x_stretch_cursor_p = 0;
29267 #endif
29268
29269 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29270 doc: /* Non-nil means highlight trailing whitespace.
29271 The face used for trailing whitespace is `trailing-whitespace'. */);
29272 Vshow_trailing_whitespace = Qnil;
29273
29274 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29275 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29276 If the value is t, Emacs highlights non-ASCII chars which have the
29277 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29278 or `escape-glyph' face respectively.
29279
29280 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29281 U+2011 (non-breaking hyphen) are affected.
29282
29283 Any other non-nil value means to display these characters as a escape
29284 glyph followed by an ordinary space or hyphen.
29285
29286 A value of nil means no special handling of these characters. */);
29287 Vnobreak_char_display = Qt;
29288
29289 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29290 doc: /* The pointer shape to show in void text areas.
29291 A value of nil means to show the text pointer. Other options are `arrow',
29292 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29293 Vvoid_text_area_pointer = Qarrow;
29294
29295 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29296 doc: /* Non-nil means don't actually do any redisplay.
29297 This is used for internal purposes. */);
29298 Vinhibit_redisplay = Qnil;
29299
29300 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29301 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29302 Vglobal_mode_string = Qnil;
29303
29304 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29305 doc: /* Marker for where to display an arrow on top of the buffer text.
29306 This must be the beginning of a line in order to work.
29307 See also `overlay-arrow-string'. */);
29308 Voverlay_arrow_position = Qnil;
29309
29310 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29311 doc: /* String to display as an arrow in non-window frames.
29312 See also `overlay-arrow-position'. */);
29313 Voverlay_arrow_string = build_pure_c_string ("=>");
29314
29315 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29316 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29317 The symbols on this list are examined during redisplay to determine
29318 where to display overlay arrows. */);
29319 Voverlay_arrow_variable_list
29320 = list1 (intern_c_string ("overlay-arrow-position"));
29321
29322 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29323 doc: /* The number of lines to try scrolling a window by when point moves out.
29324 If that fails to bring point back on frame, point is centered instead.
29325 If this is zero, point is always centered after it moves off frame.
29326 If you want scrolling to always be a line at a time, you should set
29327 `scroll-conservatively' to a large value rather than set this to 1. */);
29328
29329 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29330 doc: /* Scroll up to this many lines, to bring point back on screen.
29331 If point moves off-screen, redisplay will scroll by up to
29332 `scroll-conservatively' lines in order to bring point just barely
29333 onto the screen again. If that cannot be done, then redisplay
29334 recenters point as usual.
29335
29336 If the value is greater than 100, redisplay will never recenter point,
29337 but will always scroll just enough text to bring point into view, even
29338 if you move far away.
29339
29340 A value of zero means always recenter point if it moves off screen. */);
29341 scroll_conservatively = 0;
29342
29343 DEFVAR_INT ("scroll-margin", scroll_margin,
29344 doc: /* Number of lines of margin at the top and bottom of a window.
29345 Recenter the window whenever point gets within this many lines
29346 of the top or bottom of the window. */);
29347 scroll_margin = 0;
29348
29349 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29350 doc: /* Pixels per inch value for non-window system displays.
29351 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29352 Vdisplay_pixels_per_inch = make_float (72.0);
29353
29354 #ifdef GLYPH_DEBUG
29355 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29356 #endif
29357
29358 DEFVAR_LISP ("truncate-partial-width-windows",
29359 Vtruncate_partial_width_windows,
29360 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29361 For an integer value, truncate lines in each window narrower than the
29362 full frame width, provided the window width is less than that integer;
29363 otherwise, respect the value of `truncate-lines'.
29364
29365 For any other non-nil value, truncate lines in all windows that do
29366 not span the full frame width.
29367
29368 A value of nil means to respect the value of `truncate-lines'.
29369
29370 If `word-wrap' is enabled, you might want to reduce this. */);
29371 Vtruncate_partial_width_windows = make_number (50);
29372
29373 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29374 doc: /* Maximum buffer size for which line number should be displayed.
29375 If the buffer is bigger than this, the line number does not appear
29376 in the mode line. A value of nil means no limit. */);
29377 Vline_number_display_limit = Qnil;
29378
29379 DEFVAR_INT ("line-number-display-limit-width",
29380 line_number_display_limit_width,
29381 doc: /* Maximum line width (in characters) for line number display.
29382 If the average length of the lines near point is bigger than this, then the
29383 line number may be omitted from the mode line. */);
29384 line_number_display_limit_width = 200;
29385
29386 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29387 doc: /* Non-nil means highlight region even in nonselected windows. */);
29388 highlight_nonselected_windows = 0;
29389
29390 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29391 doc: /* Non-nil if more than one frame is visible on this display.
29392 Minibuffer-only frames don't count, but iconified frames do.
29393 This variable is not guaranteed to be accurate except while processing
29394 `frame-title-format' and `icon-title-format'. */);
29395
29396 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29397 doc: /* Template for displaying the title bar of visible frames.
29398 \(Assuming the window manager supports this feature.)
29399
29400 This variable has the same structure as `mode-line-format', except that
29401 the %c and %l constructs are ignored. It is used only on frames for
29402 which no explicit name has been set \(see `modify-frame-parameters'). */);
29403
29404 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29405 doc: /* Template for displaying the title bar of an iconified frame.
29406 \(Assuming the window manager supports this feature.)
29407 This variable has the same structure as `mode-line-format' (which see),
29408 and is used only on frames for which no explicit name has been set
29409 \(see `modify-frame-parameters'). */);
29410 Vicon_title_format
29411 = Vframe_title_format
29412 = listn (CONSTYPE_PURE, 3,
29413 intern_c_string ("multiple-frames"),
29414 build_pure_c_string ("%b"),
29415 listn (CONSTYPE_PURE, 4,
29416 empty_unibyte_string,
29417 intern_c_string ("invocation-name"),
29418 build_pure_c_string ("@"),
29419 intern_c_string ("system-name")));
29420
29421 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29422 doc: /* Maximum number of lines to keep in the message log buffer.
29423 If nil, disable message logging. If t, log messages but don't truncate
29424 the buffer when it becomes large. */);
29425 Vmessage_log_max = make_number (1000);
29426
29427 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29428 doc: /* Functions called before redisplay, if window sizes have changed.
29429 The value should be a list of functions that take one argument.
29430 Just before redisplay, for each frame, if any of its windows have changed
29431 size since the last redisplay, or have been split or deleted,
29432 all the functions in the list are called, with the frame as argument. */);
29433 Vwindow_size_change_functions = Qnil;
29434
29435 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29436 doc: /* List of functions to call before redisplaying a window with scrolling.
29437 Each function is called with two arguments, the window and its new
29438 display-start position. Note that these functions are also called by
29439 `set-window-buffer'. Also note that the value of `window-end' is not
29440 valid when these functions are called.
29441
29442 Warning: Do not use this feature to alter the way the window
29443 is scrolled. It is not designed for that, and such use probably won't
29444 work. */);
29445 Vwindow_scroll_functions = Qnil;
29446
29447 DEFVAR_LISP ("window-text-change-functions",
29448 Vwindow_text_change_functions,
29449 doc: /* Functions to call in redisplay when text in the window might change. */);
29450 Vwindow_text_change_functions = Qnil;
29451
29452 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29453 doc: /* Functions called when redisplay of a window reaches the end trigger.
29454 Each function is called with two arguments, the window and the end trigger value.
29455 See `set-window-redisplay-end-trigger'. */);
29456 Vredisplay_end_trigger_functions = Qnil;
29457
29458 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29459 doc: /* Non-nil means autoselect window with mouse pointer.
29460 If nil, do not autoselect windows.
29461 A positive number means delay autoselection by that many seconds: a
29462 window is autoselected only after the mouse has remained in that
29463 window for the duration of the delay.
29464 A negative number has a similar effect, but causes windows to be
29465 autoselected only after the mouse has stopped moving. \(Because of
29466 the way Emacs compares mouse events, you will occasionally wait twice
29467 that time before the window gets selected.\)
29468 Any other value means to autoselect window instantaneously when the
29469 mouse pointer enters it.
29470
29471 Autoselection selects the minibuffer only if it is active, and never
29472 unselects the minibuffer if it is active.
29473
29474 When customizing this variable make sure that the actual value of
29475 `focus-follows-mouse' matches the behavior of your window manager. */);
29476 Vmouse_autoselect_window = Qnil;
29477
29478 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29479 doc: /* Non-nil means automatically resize tool-bars.
29480 This dynamically changes the tool-bar's height to the minimum height
29481 that is needed to make all tool-bar items visible.
29482 If value is `grow-only', the tool-bar's height is only increased
29483 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29484 Vauto_resize_tool_bars = Qt;
29485
29486 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29487 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29488 auto_raise_tool_bar_buttons_p = 1;
29489
29490 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29491 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29492 make_cursor_line_fully_visible_p = 1;
29493
29494 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29495 doc: /* Border below tool-bar in pixels.
29496 If an integer, use it as the height of the border.
29497 If it is one of `internal-border-width' or `border-width', use the
29498 value of the corresponding frame parameter.
29499 Otherwise, no border is added below the tool-bar. */);
29500 Vtool_bar_border = Qinternal_border_width;
29501
29502 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29503 doc: /* Margin around tool-bar buttons in pixels.
29504 If an integer, use that for both horizontal and vertical margins.
29505 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29506 HORZ specifying the horizontal margin, and VERT specifying the
29507 vertical margin. */);
29508 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29509
29510 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29511 doc: /* Relief thickness of tool-bar buttons. */);
29512 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29513
29514 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29515 doc: /* Tool bar style to use.
29516 It can be one of
29517 image - show images only
29518 text - show text only
29519 both - show both, text below image
29520 both-horiz - show text to the right of the image
29521 text-image-horiz - show text to the left of the image
29522 any other - use system default or image if no system default.
29523
29524 This variable only affects the GTK+ toolkit version of Emacs. */);
29525 Vtool_bar_style = Qnil;
29526
29527 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29528 doc: /* Maximum number of characters a label can have to be shown.
29529 The tool bar style must also show labels for this to have any effect, see
29530 `tool-bar-style'. */);
29531 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29532
29533 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29534 doc: /* List of functions to call to fontify regions of text.
29535 Each function is called with one argument POS. Functions must
29536 fontify a region starting at POS in the current buffer, and give
29537 fontified regions the property `fontified'. */);
29538 Vfontification_functions = Qnil;
29539 Fmake_variable_buffer_local (Qfontification_functions);
29540
29541 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29542 unibyte_display_via_language_environment,
29543 doc: /* Non-nil means display unibyte text according to language environment.
29544 Specifically, this means that raw bytes in the range 160-255 decimal
29545 are displayed by converting them to the equivalent multibyte characters
29546 according to the current language environment. As a result, they are
29547 displayed according to the current fontset.
29548
29549 Note that this variable affects only how these bytes are displayed,
29550 but does not change the fact they are interpreted as raw bytes. */);
29551 unibyte_display_via_language_environment = 0;
29552
29553 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29554 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29555 If a float, it specifies a fraction of the mini-window frame's height.
29556 If an integer, it specifies a number of lines. */);
29557 Vmax_mini_window_height = make_float (0.25);
29558
29559 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29560 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29561 A value of nil means don't automatically resize mini-windows.
29562 A value of t means resize them to fit the text displayed in them.
29563 A value of `grow-only', the default, means let mini-windows grow only;
29564 they return to their normal size when the minibuffer is closed, or the
29565 echo area becomes empty. */);
29566 Vresize_mini_windows = Qgrow_only;
29567
29568 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29569 doc: /* Alist specifying how to blink the cursor off.
29570 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29571 `cursor-type' frame-parameter or variable equals ON-STATE,
29572 comparing using `equal', Emacs uses OFF-STATE to specify
29573 how to blink it off. ON-STATE and OFF-STATE are values for
29574 the `cursor-type' frame parameter.
29575
29576 If a frame's ON-STATE has no entry in this list,
29577 the frame's other specifications determine how to blink the cursor off. */);
29578 Vblink_cursor_alist = Qnil;
29579
29580 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29581 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29582 If non-nil, windows are automatically scrolled horizontally to make
29583 point visible. */);
29584 automatic_hscrolling_p = 1;
29585 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29586
29587 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29588 doc: /* How many columns away from the window edge point is allowed to get
29589 before automatic hscrolling will horizontally scroll the window. */);
29590 hscroll_margin = 5;
29591
29592 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29593 doc: /* How many columns to scroll the window when point gets too close to the edge.
29594 When point is less than `hscroll-margin' columns from the window
29595 edge, automatic hscrolling will scroll the window by the amount of columns
29596 determined by this variable. If its value is a positive integer, scroll that
29597 many columns. If it's a positive floating-point number, it specifies the
29598 fraction of the window's width to scroll. If it's nil or zero, point will be
29599 centered horizontally after the scroll. Any other value, including negative
29600 numbers, are treated as if the value were zero.
29601
29602 Automatic hscrolling always moves point outside the scroll margin, so if
29603 point was more than scroll step columns inside the margin, the window will
29604 scroll more than the value given by the scroll step.
29605
29606 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29607 and `scroll-right' overrides this variable's effect. */);
29608 Vhscroll_step = make_number (0);
29609
29610 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29611 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29612 Bind this around calls to `message' to let it take effect. */);
29613 message_truncate_lines = 0;
29614
29615 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29616 doc: /* Normal hook run to update the menu bar definitions.
29617 Redisplay runs this hook before it redisplays the menu bar.
29618 This is used to update submenus such as Buffers,
29619 whose contents depend on various data. */);
29620 Vmenu_bar_update_hook = Qnil;
29621
29622 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29623 doc: /* Frame for which we are updating a menu.
29624 The enable predicate for a menu binding should check this variable. */);
29625 Vmenu_updating_frame = Qnil;
29626
29627 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29628 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29629 inhibit_menubar_update = 0;
29630
29631 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29632 doc: /* Prefix prepended to all continuation lines at display time.
29633 The value may be a string, an image, or a stretch-glyph; it is
29634 interpreted in the same way as the value of a `display' text property.
29635
29636 This variable is overridden by any `wrap-prefix' text or overlay
29637 property.
29638
29639 To add a prefix to non-continuation lines, use `line-prefix'. */);
29640 Vwrap_prefix = Qnil;
29641 DEFSYM (Qwrap_prefix, "wrap-prefix");
29642 Fmake_variable_buffer_local (Qwrap_prefix);
29643
29644 DEFVAR_LISP ("line-prefix", Vline_prefix,
29645 doc: /* Prefix prepended to all non-continuation lines at display time.
29646 The value may be a string, an image, or a stretch-glyph; it is
29647 interpreted in the same way as the value of a `display' text property.
29648
29649 This variable is overridden by any `line-prefix' text or overlay
29650 property.
29651
29652 To add a prefix to continuation lines, use `wrap-prefix'. */);
29653 Vline_prefix = Qnil;
29654 DEFSYM (Qline_prefix, "line-prefix");
29655 Fmake_variable_buffer_local (Qline_prefix);
29656
29657 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29658 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29659 inhibit_eval_during_redisplay = 0;
29660
29661 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29662 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29663 inhibit_free_realized_faces = 0;
29664
29665 #ifdef GLYPH_DEBUG
29666 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29667 doc: /* Inhibit try_window_id display optimization. */);
29668 inhibit_try_window_id = 0;
29669
29670 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29671 doc: /* Inhibit try_window_reusing display optimization. */);
29672 inhibit_try_window_reusing = 0;
29673
29674 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29675 doc: /* Inhibit try_cursor_movement display optimization. */);
29676 inhibit_try_cursor_movement = 0;
29677 #endif /* GLYPH_DEBUG */
29678
29679 DEFVAR_INT ("overline-margin", overline_margin,
29680 doc: /* Space between overline and text, in pixels.
29681 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29682 margin to the character height. */);
29683 overline_margin = 2;
29684
29685 DEFVAR_INT ("underline-minimum-offset",
29686 underline_minimum_offset,
29687 doc: /* Minimum distance between baseline and underline.
29688 This can improve legibility of underlined text at small font sizes,
29689 particularly when using variable `x-use-underline-position-properties'
29690 with fonts that specify an UNDERLINE_POSITION relatively close to the
29691 baseline. The default value is 1. */);
29692 underline_minimum_offset = 1;
29693
29694 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29695 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29696 This feature only works when on a window system that can change
29697 cursor shapes. */);
29698 display_hourglass_p = 1;
29699
29700 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29701 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29702 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29703
29704 hourglass_atimer = NULL;
29705 hourglass_shown_p = 0;
29706
29707 DEFSYM (Qglyphless_char, "glyphless-char");
29708 DEFSYM (Qhex_code, "hex-code");
29709 DEFSYM (Qempty_box, "empty-box");
29710 DEFSYM (Qthin_space, "thin-space");
29711 DEFSYM (Qzero_width, "zero-width");
29712
29713 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29714 /* Intern this now in case it isn't already done.
29715 Setting this variable twice is harmless.
29716 But don't staticpro it here--that is done in alloc.c. */
29717 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29718 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29719
29720 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29721 doc: /* Char-table defining glyphless characters.
29722 Each element, if non-nil, should be one of the following:
29723 an ASCII acronym string: display this string in a box
29724 `hex-code': display the hexadecimal code of a character in a box
29725 `empty-box': display as an empty box
29726 `thin-space': display as 1-pixel width space
29727 `zero-width': don't display
29728 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29729 display method for graphical terminals and text terminals respectively.
29730 GRAPHICAL and TEXT should each have one of the values listed above.
29731
29732 The char-table has one extra slot to control the display of a character for
29733 which no font is found. This slot only takes effect on graphical terminals.
29734 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29735 `thin-space'. The default is `empty-box'. */);
29736 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29737 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29738 Qempty_box);
29739
29740 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29741 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29742 Vdebug_on_message = Qnil;
29743 }
29744
29745
29746 /* Initialize this module when Emacs starts. */
29747
29748 void
29749 init_xdisp (void)
29750 {
29751 current_header_line_height = current_mode_line_height = -1;
29752
29753 CHARPOS (this_line_start_pos) = 0;
29754
29755 if (!noninteractive)
29756 {
29757 struct window *m = XWINDOW (minibuf_window);
29758 Lisp_Object frame = m->frame;
29759 struct frame *f = XFRAME (frame);
29760 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29761 struct window *r = XWINDOW (root);
29762 int i;
29763
29764 echo_area_window = minibuf_window;
29765
29766 r->top_line = FRAME_TOP_MARGIN (f);
29767 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29768 r->total_cols = FRAME_COLS (f);
29769
29770 m->top_line = FRAME_LINES (f) - 1;
29771 m->total_lines = 1;
29772 m->total_cols = FRAME_COLS (f);
29773
29774 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29775 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29776 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29777
29778 /* The default ellipsis glyphs `...'. */
29779 for (i = 0; i < 3; ++i)
29780 default_invis_vector[i] = make_number ('.');
29781 }
29782
29783 {
29784 /* Allocate the buffer for frame titles.
29785 Also used for `format-mode-line'. */
29786 int size = 100;
29787 mode_line_noprop_buf = xmalloc (size);
29788 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29789 mode_line_noprop_ptr = mode_line_noprop_buf;
29790 mode_line_target = MODE_LINE_DISPLAY;
29791 }
29792
29793 help_echo_showing_p = 0;
29794 }
29795
29796 /* Platform-independent portion of hourglass implementation. */
29797
29798 /* Cancel a currently active hourglass timer, and start a new one. */
29799 void
29800 start_hourglass (void)
29801 {
29802 #if defined (HAVE_WINDOW_SYSTEM)
29803 EMACS_TIME delay;
29804
29805 cancel_hourglass ();
29806
29807 if (INTEGERP (Vhourglass_delay)
29808 && XINT (Vhourglass_delay) > 0)
29809 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29810 TYPE_MAXIMUM (time_t)),
29811 0);
29812 else if (FLOATP (Vhourglass_delay)
29813 && XFLOAT_DATA (Vhourglass_delay) > 0)
29814 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29815 else
29816 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29817
29818 #ifdef HAVE_NTGUI
29819 {
29820 extern void w32_note_current_window (void);
29821 w32_note_current_window ();
29822 }
29823 #endif /* HAVE_NTGUI */
29824
29825 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29826 show_hourglass, NULL);
29827 #endif
29828 }
29829
29830
29831 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29832 shown. */
29833 void
29834 cancel_hourglass (void)
29835 {
29836 #if defined (HAVE_WINDOW_SYSTEM)
29837 if (hourglass_atimer)
29838 {
29839 cancel_atimer (hourglass_atimer);
29840 hourglass_atimer = NULL;
29841 }
29842
29843 if (hourglass_shown_p)
29844 hide_hourglass ();
29845 #endif
29846 }