Fix bug #14771 with scroll-step = 1 and non-nil line-spacing.
[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 void reconsider_clip_changes (struct window *, struct buffer *);
808 static int text_outside_line_unchanged_p (struct window *,
809 ptrdiff_t, ptrdiff_t);
810 static void store_mode_line_noprop_char (char);
811 static int store_mode_line_noprop (const char *, int, int);
812 static void handle_stop (struct it *);
813 static void handle_stop_backwards (struct it *, ptrdiff_t);
814 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
815 static void ensure_echo_area_buffers (void);
816 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
817 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
818 static int with_echo_area_buffer (struct window *, int,
819 int (*) (ptrdiff_t, Lisp_Object),
820 ptrdiff_t, Lisp_Object);
821 static void clear_garbaged_frames (void);
822 static int current_message_1 (ptrdiff_t, Lisp_Object);
823 static void pop_message (void);
824 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
825 static void set_message (Lisp_Object);
826 static int set_message_1 (ptrdiff_t, Lisp_Object);
827 static int display_echo_area (struct window *);
828 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
829 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
830 static Lisp_Object unwind_redisplay (Lisp_Object);
831 static int string_char_and_length (const unsigned char *, int *);
832 static struct text_pos display_prop_end (struct it *, Lisp_Object,
833 struct text_pos);
834 static int compute_window_start_on_continuation_line (struct window *);
835 static void insert_left_trunc_glyphs (struct it *);
836 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
837 Lisp_Object);
838 static void extend_face_to_end_of_line (struct it *);
839 static int append_space_for_newline (struct it *, int);
840 static int cursor_row_fully_visible_p (struct window *, int, int);
841 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
842 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
843 static int trailing_whitespace_p (ptrdiff_t);
844 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
845 static void push_it (struct it *, struct text_pos *);
846 static void iterate_out_of_display_property (struct it *);
847 static void pop_it (struct it *);
848 static void sync_frame_with_window_matrix_rows (struct window *);
849 static void redisplay_internal (void);
850 static int echo_area_display (int);
851 static void redisplay_windows (Lisp_Object);
852 static void redisplay_window (Lisp_Object, int);
853 static Lisp_Object redisplay_window_error (Lisp_Object);
854 static Lisp_Object redisplay_window_0 (Lisp_Object);
855 static Lisp_Object redisplay_window_1 (Lisp_Object);
856 static int set_cursor_from_row (struct window *, struct glyph_row *,
857 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
858 int, int);
859 static int update_menu_bar (struct frame *, int, int);
860 static int try_window_reusing_current_matrix (struct window *);
861 static int try_window_id (struct window *);
862 static int display_line (struct it *);
863 static int display_mode_lines (struct window *);
864 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
865 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
866 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
867 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
868 static void display_menu_bar (struct window *);
869 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
870 ptrdiff_t *);
871 static int display_string (const char *, Lisp_Object, Lisp_Object,
872 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
873 static void compute_line_metrics (struct it *);
874 static void run_redisplay_end_trigger_hook (struct it *);
875 static int get_overlay_strings (struct it *, ptrdiff_t);
876 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
877 static void next_overlay_string (struct it *);
878 static void reseat (struct it *, struct text_pos, int);
879 static void reseat_1 (struct it *, struct text_pos, int);
880 static void back_to_previous_visible_line_start (struct it *);
881 static void reseat_at_next_visible_line_start (struct it *, int);
882 static int next_element_from_ellipsis (struct it *);
883 static int next_element_from_display_vector (struct it *);
884 static int next_element_from_string (struct it *);
885 static int next_element_from_c_string (struct it *);
886 static int next_element_from_buffer (struct it *);
887 static int next_element_from_composition (struct it *);
888 static int next_element_from_image (struct it *);
889 static int next_element_from_stretch (struct it *);
890 static void load_overlay_strings (struct it *, ptrdiff_t);
891 static int init_from_display_pos (struct it *, struct window *,
892 struct display_pos *);
893 static void reseat_to_string (struct it *, const char *,
894 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
895 static int get_next_display_element (struct it *);
896 static enum move_it_result
897 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
898 enum move_operation_enum);
899 static void get_visually_first_element (struct it *);
900 static void init_to_row_start (struct it *, struct window *,
901 struct glyph_row *);
902 static int init_to_row_end (struct it *, struct window *,
903 struct glyph_row *);
904 static void back_to_previous_line_start (struct it *);
905 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
906 static struct text_pos string_pos_nchars_ahead (struct text_pos,
907 Lisp_Object, ptrdiff_t);
908 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
909 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
910 static ptrdiff_t number_of_chars (const char *, bool);
911 static void compute_stop_pos (struct it *);
912 static void compute_string_pos (struct text_pos *, struct text_pos,
913 Lisp_Object);
914 static int face_before_or_after_it_pos (struct it *, int);
915 static ptrdiff_t next_overlay_change (ptrdiff_t);
916 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
917 Lisp_Object, struct text_pos *, ptrdiff_t, int);
918 static int handle_single_display_spec (struct it *, Lisp_Object,
919 Lisp_Object, Lisp_Object,
920 struct text_pos *, ptrdiff_t, int, int);
921 static int underlying_face_id (struct it *);
922 static int in_ellipses_for_invisible_text_p (struct display_pos *,
923 struct window *);
924
925 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
926 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
927
928 #ifdef HAVE_WINDOW_SYSTEM
929
930 static void x_consider_frame_title (Lisp_Object);
931 static int tool_bar_lines_needed (struct frame *, int *);
932 static void update_tool_bar (struct frame *, int);
933 static void build_desired_tool_bar_string (struct frame *f);
934 static int redisplay_tool_bar (struct frame *);
935 static void display_tool_bar_line (struct it *, int);
936 static void notice_overwritten_cursor (struct window *,
937 enum glyph_row_area,
938 int, int, int, int);
939 static void append_stretch_glyph (struct it *, Lisp_Object,
940 int, int, int);
941
942
943 #endif /* HAVE_WINDOW_SYSTEM */
944
945 static void produce_special_glyphs (struct it *, enum display_element_type);
946 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
947 static int coords_in_mouse_face_p (struct window *, int, int);
948
949
950 \f
951 /***********************************************************************
952 Window display dimensions
953 ***********************************************************************/
954
955 /* Return the bottom boundary y-position for text lines in window W.
956 This is the first y position at which a line cannot start.
957 It is relative to the top of the window.
958
959 This is the height of W minus the height of a mode line, if any. */
960
961 int
962 window_text_bottom_y (struct window *w)
963 {
964 int height = WINDOW_TOTAL_HEIGHT (w);
965
966 if (WINDOW_WANTS_MODELINE_P (w))
967 height -= CURRENT_MODE_LINE_HEIGHT (w);
968 return height;
969 }
970
971 /* Return the pixel width of display area AREA of window W. AREA < 0
972 means return the total width of W, not including fringes to
973 the left and right of the window. */
974
975 int
976 window_box_width (struct window *w, int area)
977 {
978 int cols = w->total_cols;
979 int pixels = 0;
980
981 if (!w->pseudo_window_p)
982 {
983 cols -= WINDOW_SCROLL_BAR_COLS (w);
984
985 if (area == TEXT_AREA)
986 {
987 if (INTEGERP (w->left_margin_cols))
988 cols -= XFASTINT (w->left_margin_cols);
989 if (INTEGERP (w->right_margin_cols))
990 cols -= XFASTINT (w->right_margin_cols);
991 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
992 }
993 else if (area == LEFT_MARGIN_AREA)
994 {
995 cols = (INTEGERP (w->left_margin_cols)
996 ? XFASTINT (w->left_margin_cols) : 0);
997 pixels = 0;
998 }
999 else if (area == RIGHT_MARGIN_AREA)
1000 {
1001 cols = (INTEGERP (w->right_margin_cols)
1002 ? XFASTINT (w->right_margin_cols) : 0);
1003 pixels = 0;
1004 }
1005 }
1006
1007 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1008 }
1009
1010
1011 /* Return the pixel height of the display area of window W, not
1012 including mode lines of W, if any. */
1013
1014 int
1015 window_box_height (struct window *w)
1016 {
1017 struct frame *f = XFRAME (w->frame);
1018 int height = WINDOW_TOTAL_HEIGHT (w);
1019
1020 eassert (height >= 0);
1021
1022 /* Note: the code below that determines the mode-line/header-line
1023 height is essentially the same as that contained in the macro
1024 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1025 the appropriate glyph row has its `mode_line_p' flag set,
1026 and if it doesn't, uses estimate_mode_line_height instead. */
1027
1028 if (WINDOW_WANTS_MODELINE_P (w))
1029 {
1030 struct glyph_row *ml_row
1031 = (w->current_matrix && w->current_matrix->rows
1032 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1033 : 0);
1034 if (ml_row && ml_row->mode_line_p)
1035 height -= ml_row->height;
1036 else
1037 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1038 }
1039
1040 if (WINDOW_WANTS_HEADER_LINE_P (w))
1041 {
1042 struct glyph_row *hl_row
1043 = (w->current_matrix && w->current_matrix->rows
1044 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1045 : 0);
1046 if (hl_row && hl_row->mode_line_p)
1047 height -= hl_row->height;
1048 else
1049 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1050 }
1051
1052 /* With a very small font and a mode-line that's taller than
1053 default, we might end up with a negative height. */
1054 return max (0, height);
1055 }
1056
1057 /* Return the window-relative coordinate of the left edge of display
1058 area AREA of window W. AREA < 0 means return the left edge of the
1059 whole window, to the right of the left fringe of W. */
1060
1061 int
1062 window_box_left_offset (struct window *w, int area)
1063 {
1064 int x;
1065
1066 if (w->pseudo_window_p)
1067 return 0;
1068
1069 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1070
1071 if (area == TEXT_AREA)
1072 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1073 + window_box_width (w, LEFT_MARGIN_AREA));
1074 else if (area == RIGHT_MARGIN_AREA)
1075 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1076 + window_box_width (w, LEFT_MARGIN_AREA)
1077 + window_box_width (w, TEXT_AREA)
1078 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1079 ? 0
1080 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1081 else if (area == LEFT_MARGIN_AREA
1082 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1083 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1084
1085 return x;
1086 }
1087
1088
1089 /* Return the window-relative coordinate of the right edge of display
1090 area AREA of window W. AREA < 0 means return the right edge of the
1091 whole window, to the left of the right fringe of W. */
1092
1093 int
1094 window_box_right_offset (struct window *w, int area)
1095 {
1096 return window_box_left_offset (w, area) + window_box_width (w, area);
1097 }
1098
1099 /* Return the frame-relative coordinate of the left edge of display
1100 area AREA of window W. AREA < 0 means return the left edge of the
1101 whole window, to the right of the left fringe of W. */
1102
1103 int
1104 window_box_left (struct window *w, int area)
1105 {
1106 struct frame *f = XFRAME (w->frame);
1107 int x;
1108
1109 if (w->pseudo_window_p)
1110 return FRAME_INTERNAL_BORDER_WIDTH (f);
1111
1112 x = (WINDOW_LEFT_EDGE_X (w)
1113 + window_box_left_offset (w, area));
1114
1115 return x;
1116 }
1117
1118
1119 /* Return the frame-relative coordinate of the right edge of display
1120 area AREA of window W. AREA < 0 means return the right edge of the
1121 whole window, to the left of the right fringe of W. */
1122
1123 int
1124 window_box_right (struct window *w, int area)
1125 {
1126 return window_box_left (w, area) + window_box_width (w, area);
1127 }
1128
1129 /* Get the bounding box of the display area AREA of window W, without
1130 mode lines, in frame-relative coordinates. AREA < 0 means the
1131 whole window, not including the left and right fringes of
1132 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1133 coordinates of the upper-left corner of the box. Return in
1134 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1135
1136 void
1137 window_box (struct window *w, int area, int *box_x, int *box_y,
1138 int *box_width, int *box_height)
1139 {
1140 if (box_width)
1141 *box_width = window_box_width (w, area);
1142 if (box_height)
1143 *box_height = window_box_height (w);
1144 if (box_x)
1145 *box_x = window_box_left (w, area);
1146 if (box_y)
1147 {
1148 *box_y = WINDOW_TOP_EDGE_Y (w);
1149 if (WINDOW_WANTS_HEADER_LINE_P (w))
1150 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1151 }
1152 }
1153
1154
1155 /* Get the bounding box of the display area AREA of window W, without
1156 mode lines. AREA < 0 means the whole window, not including the
1157 left and right fringe of the window. Return in *TOP_LEFT_X
1158 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1159 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1160 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1161 box. */
1162
1163 static void
1164 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1165 int *bottom_right_x, int *bottom_right_y)
1166 {
1167 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1168 bottom_right_y);
1169 *bottom_right_x += *top_left_x;
1170 *bottom_right_y += *top_left_y;
1171 }
1172
1173
1174 \f
1175 /***********************************************************************
1176 Utilities
1177 ***********************************************************************/
1178
1179 /* Return the bottom y-position of the line the iterator IT is in.
1180 This can modify IT's settings. */
1181
1182 int
1183 line_bottom_y (struct it *it)
1184 {
1185 int line_height = it->max_ascent + it->max_descent;
1186 int line_top_y = it->current_y;
1187
1188 if (line_height == 0)
1189 {
1190 if (last_height)
1191 line_height = last_height;
1192 else if (IT_CHARPOS (*it) < ZV)
1193 {
1194 move_it_by_lines (it, 1);
1195 line_height = (it->max_ascent || it->max_descent
1196 ? it->max_ascent + it->max_descent
1197 : last_height);
1198 }
1199 else
1200 {
1201 struct glyph_row *row = it->glyph_row;
1202
1203 /* Use the default character height. */
1204 it->glyph_row = NULL;
1205 it->what = IT_CHARACTER;
1206 it->c = ' ';
1207 it->len = 1;
1208 PRODUCE_GLYPHS (it);
1209 line_height = it->ascent + it->descent;
1210 it->glyph_row = row;
1211 }
1212 }
1213
1214 return line_top_y + line_height;
1215 }
1216
1217 DEFUN ("line-pixel-height", Fline_pixel_height,
1218 Sline_pixel_height, 0, 0, 0,
1219 doc: /* Return height in pixels of text line in the selected window.
1220
1221 Value is the height in pixels of the line at point. */)
1222 (void)
1223 {
1224 struct it it;
1225 struct text_pos pt;
1226 struct window *w = XWINDOW (selected_window);
1227
1228 SET_TEXT_POS (pt, PT, PT_BYTE);
1229 start_display (&it, w, pt);
1230 it.vpos = it.current_y = 0;
1231 last_height = 0;
1232 return make_number (line_bottom_y (&it));
1233 }
1234
1235 /* Return the default pixel height of text lines in window W. The
1236 value is the canonical height of the W frame's default font, plus
1237 any extra space required by the line-spacing variable or frame
1238 parameter.
1239
1240 Implementation note: this ignores any line-spacing text properties
1241 put on the newline characters. This is because those properties
1242 only affect the _screen_ line ending in the newline (i.e., in a
1243 continued line, only the last screen line will be affected), which
1244 means only a small number of lines in a buffer can ever use this
1245 feature. Since this function is used to compute the default pixel
1246 equivalent of text lines in a window, we can safely ignore those
1247 few lines. For the same reasons, we ignore the line-height
1248 properties. */
1249 int
1250 default_line_pixel_height (struct window *w)
1251 {
1252 struct frame *f = WINDOW_XFRAME (w);
1253 int height = FRAME_LINE_HEIGHT (f);
1254
1255 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1256 {
1257 struct buffer *b = XBUFFER (w->contents);
1258 Lisp_Object val = BVAR (b, extra_line_spacing);
1259
1260 if (NILP (val))
1261 val = BVAR (&buffer_defaults, extra_line_spacing);
1262 if (!NILP (val))
1263 {
1264 if (RANGED_INTEGERP (0, val, INT_MAX))
1265 height += XFASTINT (val);
1266 else if (FLOATP (val))
1267 {
1268 int addon = XFLOAT_DATA (val) * height + 0.5;
1269
1270 if (addon >= 0)
1271 height += addon;
1272 }
1273 }
1274 else
1275 height += f->extra_line_spacing;
1276 }
1277
1278 return height;
1279 }
1280
1281 /* Subroutine of pos_visible_p below. Extracts a display string, if
1282 any, from the display spec given as its argument. */
1283 static Lisp_Object
1284 string_from_display_spec (Lisp_Object spec)
1285 {
1286 if (CONSP (spec))
1287 {
1288 while (CONSP (spec))
1289 {
1290 if (STRINGP (XCAR (spec)))
1291 return XCAR (spec);
1292 spec = XCDR (spec);
1293 }
1294 }
1295 else if (VECTORP (spec))
1296 {
1297 ptrdiff_t i;
1298
1299 for (i = 0; i < ASIZE (spec); i++)
1300 {
1301 if (STRINGP (AREF (spec, i)))
1302 return AREF (spec, i);
1303 }
1304 return Qnil;
1305 }
1306
1307 return spec;
1308 }
1309
1310
1311 /* Limit insanely large values of W->hscroll on frame F to the largest
1312 value that will still prevent first_visible_x and last_visible_x of
1313 'struct it' from overflowing an int. */
1314 static int
1315 window_hscroll_limited (struct window *w, struct frame *f)
1316 {
1317 ptrdiff_t window_hscroll = w->hscroll;
1318 int window_text_width = window_box_width (w, TEXT_AREA);
1319 int colwidth = FRAME_COLUMN_WIDTH (f);
1320
1321 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1322 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1323
1324 return window_hscroll;
1325 }
1326
1327 /* Return 1 if position CHARPOS is visible in window W.
1328 CHARPOS < 0 means return info about WINDOW_END position.
1329 If visible, set *X and *Y to pixel coordinates of top left corner.
1330 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1331 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1332
1333 int
1334 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1335 int *rtop, int *rbot, int *rowh, int *vpos)
1336 {
1337 struct it it;
1338 void *itdata = bidi_shelve_cache ();
1339 struct text_pos top;
1340 int visible_p = 0;
1341 struct buffer *old_buffer = NULL;
1342
1343 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1344 return visible_p;
1345
1346 if (XBUFFER (w->contents) != current_buffer)
1347 {
1348 old_buffer = current_buffer;
1349 set_buffer_internal_1 (XBUFFER (w->contents));
1350 }
1351
1352 SET_TEXT_POS_FROM_MARKER (top, w->start);
1353 /* Scrolling a minibuffer window via scroll bar when the echo area
1354 shows long text sometimes resets the minibuffer contents behind
1355 our backs. */
1356 if (CHARPOS (top) > ZV)
1357 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1358
1359 /* Compute exact mode line heights. */
1360 if (WINDOW_WANTS_MODELINE_P (w))
1361 current_mode_line_height
1362 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1363 BVAR (current_buffer, mode_line_format));
1364
1365 if (WINDOW_WANTS_HEADER_LINE_P (w))
1366 current_header_line_height
1367 = display_mode_line (w, HEADER_LINE_FACE_ID,
1368 BVAR (current_buffer, header_line_format));
1369
1370 start_display (&it, w, top);
1371 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1372 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1373
1374 if (charpos >= 0
1375 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1376 && IT_CHARPOS (it) >= charpos)
1377 /* When scanning backwards under bidi iteration, move_it_to
1378 stops at or _before_ CHARPOS, because it stops at or to
1379 the _right_ of the character at CHARPOS. */
1380 || (it.bidi_p && it.bidi_it.scan_dir == -1
1381 && IT_CHARPOS (it) <= charpos)))
1382 {
1383 /* We have reached CHARPOS, or passed it. How the call to
1384 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1385 or covered by a display property, move_it_to stops at the end
1386 of the invisible text, to the right of CHARPOS. (ii) If
1387 CHARPOS is in a display vector, move_it_to stops on its last
1388 glyph. */
1389 int top_x = it.current_x;
1390 int top_y = it.current_y;
1391 /* Calling line_bottom_y may change it.method, it.position, etc. */
1392 enum it_method it_method = it.method;
1393 int bottom_y = (last_height = 0, line_bottom_y (&it));
1394 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1395
1396 if (top_y < window_top_y)
1397 visible_p = bottom_y > window_top_y;
1398 else if (top_y < it.last_visible_y)
1399 visible_p = 1;
1400 if (bottom_y >= it.last_visible_y
1401 && it.bidi_p && it.bidi_it.scan_dir == -1
1402 && IT_CHARPOS (it) < charpos)
1403 {
1404 /* When the last line of the window is scanned backwards
1405 under bidi iteration, we could be duped into thinking
1406 that we have passed CHARPOS, when in fact move_it_to
1407 simply stopped short of CHARPOS because it reached
1408 last_visible_y. To see if that's what happened, we call
1409 move_it_to again with a slightly larger vertical limit,
1410 and see if it actually moved vertically; if it did, we
1411 didn't really reach CHARPOS, which is beyond window end. */
1412 struct it save_it = it;
1413 /* Why 10? because we don't know how many canonical lines
1414 will the height of the next line(s) be. So we guess. */
1415 int ten_more_lines = 10 * default_line_pixel_height (w);
1416
1417 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1418 MOVE_TO_POS | MOVE_TO_Y);
1419 if (it.current_y > top_y)
1420 visible_p = 0;
1421
1422 it = save_it;
1423 }
1424 if (visible_p)
1425 {
1426 if (it_method == GET_FROM_DISPLAY_VECTOR)
1427 {
1428 /* We stopped on the last glyph of a display vector.
1429 Try and recompute. Hack alert! */
1430 if (charpos < 2 || top.charpos >= charpos)
1431 top_x = it.glyph_row->x;
1432 else
1433 {
1434 struct it it2, it2_prev;
1435 /* The idea is to get to the previous buffer
1436 position, consume the character there, and use
1437 the pixel coordinates we get after that. But if
1438 the previous buffer position is also displayed
1439 from a display vector, we need to consume all of
1440 the glyphs from that display vector. */
1441 start_display (&it2, w, top);
1442 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1443 /* If we didn't get to CHARPOS - 1, there's some
1444 replacing display property at that position, and
1445 we stopped after it. That is exactly the place
1446 whose coordinates we want. */
1447 if (IT_CHARPOS (it2) != charpos - 1)
1448 it2_prev = it2;
1449 else
1450 {
1451 /* Iterate until we get out of the display
1452 vector that displays the character at
1453 CHARPOS - 1. */
1454 do {
1455 get_next_display_element (&it2);
1456 PRODUCE_GLYPHS (&it2);
1457 it2_prev = it2;
1458 set_iterator_to_next (&it2, 1);
1459 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1460 && IT_CHARPOS (it2) < charpos);
1461 }
1462 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1463 || it2_prev.current_x > it2_prev.last_visible_x)
1464 top_x = it.glyph_row->x;
1465 else
1466 {
1467 top_x = it2_prev.current_x;
1468 top_y = it2_prev.current_y;
1469 }
1470 }
1471 }
1472 else if (IT_CHARPOS (it) != charpos)
1473 {
1474 Lisp_Object cpos = make_number (charpos);
1475 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1476 Lisp_Object string = string_from_display_spec (spec);
1477 struct text_pos tpos;
1478 int replacing_spec_p;
1479 bool newline_in_string
1480 = (STRINGP (string)
1481 && memchr (SDATA (string), '\n', SBYTES (string)));
1482
1483 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1484 replacing_spec_p
1485 = (!NILP (spec)
1486 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1487 charpos, FRAME_WINDOW_P (it.f)));
1488 /* The tricky code below is needed because there's a
1489 discrepancy between move_it_to and how we set cursor
1490 when PT is at the beginning of a portion of text
1491 covered by a display property or an overlay with a
1492 display property, or the display line ends in a
1493 newline from a display string. move_it_to will stop
1494 _after_ such display strings, whereas
1495 set_cursor_from_row conspires with cursor_row_p to
1496 place the cursor on the first glyph produced from the
1497 display string. */
1498
1499 /* We have overshoot PT because it is covered by a
1500 display property that replaces the text it covers.
1501 If the string includes embedded newlines, we are also
1502 in the wrong display line. Backtrack to the correct
1503 line, where the display property begins. */
1504 if (replacing_spec_p)
1505 {
1506 Lisp_Object startpos, endpos;
1507 EMACS_INT start, end;
1508 struct it it3;
1509 int it3_moved;
1510
1511 /* Find the first and the last buffer positions
1512 covered by the display string. */
1513 endpos =
1514 Fnext_single_char_property_change (cpos, Qdisplay,
1515 Qnil, Qnil);
1516 startpos =
1517 Fprevious_single_char_property_change (endpos, Qdisplay,
1518 Qnil, Qnil);
1519 start = XFASTINT (startpos);
1520 end = XFASTINT (endpos);
1521 /* Move to the last buffer position before the
1522 display property. */
1523 start_display (&it3, w, top);
1524 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1525 /* Move forward one more line if the position before
1526 the display string is a newline or if it is the
1527 rightmost character on a line that is
1528 continued or word-wrapped. */
1529 if (it3.method == GET_FROM_BUFFER
1530 && (it3.c == '\n'
1531 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1532 move_it_by_lines (&it3, 1);
1533 else if (move_it_in_display_line_to (&it3, -1,
1534 it3.current_x
1535 + it3.pixel_width,
1536 MOVE_TO_X)
1537 == MOVE_LINE_CONTINUED)
1538 {
1539 move_it_by_lines (&it3, 1);
1540 /* When we are under word-wrap, the #$@%!
1541 move_it_by_lines moves 2 lines, so we need to
1542 fix that up. */
1543 if (it3.line_wrap == WORD_WRAP)
1544 move_it_by_lines (&it3, -1);
1545 }
1546
1547 /* Record the vertical coordinate of the display
1548 line where we wound up. */
1549 top_y = it3.current_y;
1550 if (it3.bidi_p)
1551 {
1552 /* When characters are reordered for display,
1553 the character displayed to the left of the
1554 display string could be _after_ the display
1555 property in the logical order. Use the
1556 smallest vertical position of these two. */
1557 start_display (&it3, w, top);
1558 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1559 if (it3.current_y < top_y)
1560 top_y = it3.current_y;
1561 }
1562 /* Move from the top of the window to the beginning
1563 of the display line where the display string
1564 begins. */
1565 start_display (&it3, w, top);
1566 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1567 /* If it3_moved stays zero after the 'while' loop
1568 below, that means we already were at a newline
1569 before the loop (e.g., the display string begins
1570 with a newline), so we don't need to (and cannot)
1571 inspect the glyphs of it3.glyph_row, because
1572 PRODUCE_GLYPHS will not produce anything for a
1573 newline, and thus it3.glyph_row stays at its
1574 stale content it got at top of the window. */
1575 it3_moved = 0;
1576 /* Finally, advance the iterator until we hit the
1577 first display element whose character position is
1578 CHARPOS, or until the first newline from the
1579 display string, which signals the end of the
1580 display line. */
1581 while (get_next_display_element (&it3))
1582 {
1583 PRODUCE_GLYPHS (&it3);
1584 if (IT_CHARPOS (it3) == charpos
1585 || ITERATOR_AT_END_OF_LINE_P (&it3))
1586 break;
1587 it3_moved = 1;
1588 set_iterator_to_next (&it3, 0);
1589 }
1590 top_x = it3.current_x - it3.pixel_width;
1591 /* Normally, we would exit the above loop because we
1592 found the display element whose character
1593 position is CHARPOS. For the contingency that we
1594 didn't, and stopped at the first newline from the
1595 display string, move back over the glyphs
1596 produced from the string, until we find the
1597 rightmost glyph not from the string. */
1598 if (it3_moved
1599 && newline_in_string
1600 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1601 {
1602 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1603 + it3.glyph_row->used[TEXT_AREA];
1604
1605 while (EQ ((g - 1)->object, string))
1606 {
1607 --g;
1608 top_x -= g->pixel_width;
1609 }
1610 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1611 + it3.glyph_row->used[TEXT_AREA]);
1612 }
1613 }
1614 }
1615
1616 *x = top_x;
1617 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1618 *rtop = max (0, window_top_y - top_y);
1619 *rbot = max (0, bottom_y - it.last_visible_y);
1620 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1621 - max (top_y, window_top_y)));
1622 *vpos = it.vpos;
1623 }
1624 }
1625 else
1626 {
1627 /* We were asked to provide info about WINDOW_END. */
1628 struct it it2;
1629 void *it2data = NULL;
1630
1631 SAVE_IT (it2, it, it2data);
1632 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1633 move_it_by_lines (&it, 1);
1634 if (charpos < IT_CHARPOS (it)
1635 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1636 {
1637 visible_p = 1;
1638 RESTORE_IT (&it2, &it2, it2data);
1639 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1640 *x = it2.current_x;
1641 *y = it2.current_y + it2.max_ascent - it2.ascent;
1642 *rtop = max (0, -it2.current_y);
1643 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1644 - it.last_visible_y));
1645 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1646 it.last_visible_y)
1647 - max (it2.current_y,
1648 WINDOW_HEADER_LINE_HEIGHT (w))));
1649 *vpos = it2.vpos;
1650 }
1651 else
1652 bidi_unshelve_cache (it2data, 1);
1653 }
1654 bidi_unshelve_cache (itdata, 0);
1655
1656 if (old_buffer)
1657 set_buffer_internal_1 (old_buffer);
1658
1659 current_header_line_height = current_mode_line_height = -1;
1660
1661 if (visible_p && w->hscroll > 0)
1662 *x -=
1663 window_hscroll_limited (w, WINDOW_XFRAME (w))
1664 * WINDOW_FRAME_COLUMN_WIDTH (w);
1665
1666 #if 0
1667 /* Debugging code. */
1668 if (visible_p)
1669 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1670 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1671 else
1672 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1673 #endif
1674
1675 return visible_p;
1676 }
1677
1678
1679 /* Return the next character from STR. Return in *LEN the length of
1680 the character. This is like STRING_CHAR_AND_LENGTH but never
1681 returns an invalid character. If we find one, we return a `?', but
1682 with the length of the invalid character. */
1683
1684 static int
1685 string_char_and_length (const unsigned char *str, int *len)
1686 {
1687 int c;
1688
1689 c = STRING_CHAR_AND_LENGTH (str, *len);
1690 if (!CHAR_VALID_P (c))
1691 /* We may not change the length here because other places in Emacs
1692 don't use this function, i.e. they silently accept invalid
1693 characters. */
1694 c = '?';
1695
1696 return c;
1697 }
1698
1699
1700
1701 /* Given a position POS containing a valid character and byte position
1702 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1703
1704 static struct text_pos
1705 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1706 {
1707 eassert (STRINGP (string) && nchars >= 0);
1708
1709 if (STRING_MULTIBYTE (string))
1710 {
1711 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1712 int len;
1713
1714 while (nchars--)
1715 {
1716 string_char_and_length (p, &len);
1717 p += len;
1718 CHARPOS (pos) += 1;
1719 BYTEPOS (pos) += len;
1720 }
1721 }
1722 else
1723 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1724
1725 return pos;
1726 }
1727
1728
1729 /* Value is the text position, i.e. character and byte position,
1730 for character position CHARPOS in STRING. */
1731
1732 static struct text_pos
1733 string_pos (ptrdiff_t charpos, Lisp_Object string)
1734 {
1735 struct text_pos pos;
1736 eassert (STRINGP (string));
1737 eassert (charpos >= 0);
1738 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1739 return pos;
1740 }
1741
1742
1743 /* Value is a text position, i.e. character and byte position, for
1744 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1745 means recognize multibyte characters. */
1746
1747 static struct text_pos
1748 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1749 {
1750 struct text_pos pos;
1751
1752 eassert (s != NULL);
1753 eassert (charpos >= 0);
1754
1755 if (multibyte_p)
1756 {
1757 int len;
1758
1759 SET_TEXT_POS (pos, 0, 0);
1760 while (charpos--)
1761 {
1762 string_char_and_length ((const unsigned char *) s, &len);
1763 s += len;
1764 CHARPOS (pos) += 1;
1765 BYTEPOS (pos) += len;
1766 }
1767 }
1768 else
1769 SET_TEXT_POS (pos, charpos, charpos);
1770
1771 return pos;
1772 }
1773
1774
1775 /* Value is the number of characters in C string S. MULTIBYTE_P
1776 non-zero means recognize multibyte characters. */
1777
1778 static ptrdiff_t
1779 number_of_chars (const char *s, bool multibyte_p)
1780 {
1781 ptrdiff_t nchars;
1782
1783 if (multibyte_p)
1784 {
1785 ptrdiff_t rest = strlen (s);
1786 int len;
1787 const unsigned char *p = (const unsigned char *) s;
1788
1789 for (nchars = 0; rest > 0; ++nchars)
1790 {
1791 string_char_and_length (p, &len);
1792 rest -= len, p += len;
1793 }
1794 }
1795 else
1796 nchars = strlen (s);
1797
1798 return nchars;
1799 }
1800
1801
1802 /* Compute byte position NEWPOS->bytepos corresponding to
1803 NEWPOS->charpos. POS is a known position in string STRING.
1804 NEWPOS->charpos must be >= POS.charpos. */
1805
1806 static void
1807 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1808 {
1809 eassert (STRINGP (string));
1810 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1811
1812 if (STRING_MULTIBYTE (string))
1813 *newpos = string_pos_nchars_ahead (pos, string,
1814 CHARPOS (*newpos) - CHARPOS (pos));
1815 else
1816 BYTEPOS (*newpos) = CHARPOS (*newpos);
1817 }
1818
1819 /* EXPORT:
1820 Return an estimation of the pixel height of mode or header lines on
1821 frame F. FACE_ID specifies what line's height to estimate. */
1822
1823 int
1824 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1825 {
1826 #ifdef HAVE_WINDOW_SYSTEM
1827 if (FRAME_WINDOW_P (f))
1828 {
1829 int height = FONT_HEIGHT (FRAME_FONT (f));
1830
1831 /* This function is called so early when Emacs starts that the face
1832 cache and mode line face are not yet initialized. */
1833 if (FRAME_FACE_CACHE (f))
1834 {
1835 struct face *face = FACE_FROM_ID (f, face_id);
1836 if (face)
1837 {
1838 if (face->font)
1839 height = FONT_HEIGHT (face->font);
1840 if (face->box_line_width > 0)
1841 height += 2 * face->box_line_width;
1842 }
1843 }
1844
1845 return height;
1846 }
1847 #endif
1848
1849 return 1;
1850 }
1851
1852 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1853 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1854 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1855 not force the value into range. */
1856
1857 void
1858 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1859 int *x, int *y, NativeRectangle *bounds, int noclip)
1860 {
1861
1862 #ifdef HAVE_WINDOW_SYSTEM
1863 if (FRAME_WINDOW_P (f))
1864 {
1865 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1866 even for negative values. */
1867 if (pix_x < 0)
1868 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1869 if (pix_y < 0)
1870 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1871
1872 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1873 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1874
1875 if (bounds)
1876 STORE_NATIVE_RECT (*bounds,
1877 FRAME_COL_TO_PIXEL_X (f, pix_x),
1878 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1879 FRAME_COLUMN_WIDTH (f) - 1,
1880 FRAME_LINE_HEIGHT (f) - 1);
1881
1882 if (!noclip)
1883 {
1884 if (pix_x < 0)
1885 pix_x = 0;
1886 else if (pix_x > FRAME_TOTAL_COLS (f))
1887 pix_x = FRAME_TOTAL_COLS (f);
1888
1889 if (pix_y < 0)
1890 pix_y = 0;
1891 else if (pix_y > FRAME_LINES (f))
1892 pix_y = FRAME_LINES (f);
1893 }
1894 }
1895 #endif
1896
1897 *x = pix_x;
1898 *y = pix_y;
1899 }
1900
1901
1902 /* Find the glyph under window-relative coordinates X/Y in window W.
1903 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1904 strings. Return in *HPOS and *VPOS the row and column number of
1905 the glyph found. Return in *AREA the glyph area containing X.
1906 Value is a pointer to the glyph found or null if X/Y is not on
1907 text, or we can't tell because W's current matrix is not up to
1908 date. */
1909
1910 static
1911 struct glyph *
1912 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1913 int *dx, int *dy, int *area)
1914 {
1915 struct glyph *glyph, *end;
1916 struct glyph_row *row = NULL;
1917 int x0, i;
1918
1919 /* Find row containing Y. Give up if some row is not enabled. */
1920 for (i = 0; i < w->current_matrix->nrows; ++i)
1921 {
1922 row = MATRIX_ROW (w->current_matrix, i);
1923 if (!row->enabled_p)
1924 return NULL;
1925 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1926 break;
1927 }
1928
1929 *vpos = i;
1930 *hpos = 0;
1931
1932 /* Give up if Y is not in the window. */
1933 if (i == w->current_matrix->nrows)
1934 return NULL;
1935
1936 /* Get the glyph area containing X. */
1937 if (w->pseudo_window_p)
1938 {
1939 *area = TEXT_AREA;
1940 x0 = 0;
1941 }
1942 else
1943 {
1944 if (x < window_box_left_offset (w, TEXT_AREA))
1945 {
1946 *area = LEFT_MARGIN_AREA;
1947 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1948 }
1949 else if (x < window_box_right_offset (w, TEXT_AREA))
1950 {
1951 *area = TEXT_AREA;
1952 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1953 }
1954 else
1955 {
1956 *area = RIGHT_MARGIN_AREA;
1957 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1958 }
1959 }
1960
1961 /* Find glyph containing X. */
1962 glyph = row->glyphs[*area];
1963 end = glyph + row->used[*area];
1964 x -= x0;
1965 while (glyph < end && x >= glyph->pixel_width)
1966 {
1967 x -= glyph->pixel_width;
1968 ++glyph;
1969 }
1970
1971 if (glyph == end)
1972 return NULL;
1973
1974 if (dx)
1975 {
1976 *dx = x;
1977 *dy = y - (row->y + row->ascent - glyph->ascent);
1978 }
1979
1980 *hpos = glyph - row->glyphs[*area];
1981 return glyph;
1982 }
1983
1984 /* Convert frame-relative x/y to coordinates relative to window W.
1985 Takes pseudo-windows into account. */
1986
1987 static void
1988 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1989 {
1990 if (w->pseudo_window_p)
1991 {
1992 /* A pseudo-window is always full-width, and starts at the
1993 left edge of the frame, plus a frame border. */
1994 struct frame *f = XFRAME (w->frame);
1995 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1996 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1997 }
1998 else
1999 {
2000 *x -= WINDOW_LEFT_EDGE_X (w);
2001 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2002 }
2003 }
2004
2005 #ifdef HAVE_WINDOW_SYSTEM
2006
2007 /* EXPORT:
2008 Return in RECTS[] at most N clipping rectangles for glyph string S.
2009 Return the number of stored rectangles. */
2010
2011 int
2012 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2013 {
2014 XRectangle r;
2015
2016 if (n <= 0)
2017 return 0;
2018
2019 if (s->row->full_width_p)
2020 {
2021 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2022 r.x = WINDOW_LEFT_EDGE_X (s->w);
2023 r.width = WINDOW_TOTAL_WIDTH (s->w);
2024
2025 /* Unless displaying a mode or menu bar line, which are always
2026 fully visible, clip to the visible part of the row. */
2027 if (s->w->pseudo_window_p)
2028 r.height = s->row->visible_height;
2029 else
2030 r.height = s->height;
2031 }
2032 else
2033 {
2034 /* This is a text line that may be partially visible. */
2035 r.x = window_box_left (s->w, s->area);
2036 r.width = window_box_width (s->w, s->area);
2037 r.height = s->row->visible_height;
2038 }
2039
2040 if (s->clip_head)
2041 if (r.x < s->clip_head->x)
2042 {
2043 if (r.width >= s->clip_head->x - r.x)
2044 r.width -= s->clip_head->x - r.x;
2045 else
2046 r.width = 0;
2047 r.x = s->clip_head->x;
2048 }
2049 if (s->clip_tail)
2050 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2051 {
2052 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2053 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2054 else
2055 r.width = 0;
2056 }
2057
2058 /* If S draws overlapping rows, it's sufficient to use the top and
2059 bottom of the window for clipping because this glyph string
2060 intentionally draws over other lines. */
2061 if (s->for_overlaps)
2062 {
2063 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2064 r.height = window_text_bottom_y (s->w) - r.y;
2065
2066 /* Alas, the above simple strategy does not work for the
2067 environments with anti-aliased text: if the same text is
2068 drawn onto the same place multiple times, it gets thicker.
2069 If the overlap we are processing is for the erased cursor, we
2070 take the intersection with the rectangle of the cursor. */
2071 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2072 {
2073 XRectangle rc, r_save = r;
2074
2075 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2076 rc.y = s->w->phys_cursor.y;
2077 rc.width = s->w->phys_cursor_width;
2078 rc.height = s->w->phys_cursor_height;
2079
2080 x_intersect_rectangles (&r_save, &rc, &r);
2081 }
2082 }
2083 else
2084 {
2085 /* Don't use S->y for clipping because it doesn't take partially
2086 visible lines into account. For example, it can be negative for
2087 partially visible lines at the top of a window. */
2088 if (!s->row->full_width_p
2089 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2090 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2091 else
2092 r.y = max (0, s->row->y);
2093 }
2094
2095 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2096
2097 /* If drawing the cursor, don't let glyph draw outside its
2098 advertised boundaries. Cleartype does this under some circumstances. */
2099 if (s->hl == DRAW_CURSOR)
2100 {
2101 struct glyph *glyph = s->first_glyph;
2102 int height, max_y;
2103
2104 if (s->x > r.x)
2105 {
2106 r.width -= s->x - r.x;
2107 r.x = s->x;
2108 }
2109 r.width = min (r.width, glyph->pixel_width);
2110
2111 /* If r.y is below window bottom, ensure that we still see a cursor. */
2112 height = min (glyph->ascent + glyph->descent,
2113 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2114 max_y = window_text_bottom_y (s->w) - height;
2115 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2116 if (s->ybase - glyph->ascent > max_y)
2117 {
2118 r.y = max_y;
2119 r.height = height;
2120 }
2121 else
2122 {
2123 /* Don't draw cursor glyph taller than our actual glyph. */
2124 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2125 if (height < r.height)
2126 {
2127 max_y = r.y + r.height;
2128 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2129 r.height = min (max_y - r.y, height);
2130 }
2131 }
2132 }
2133
2134 if (s->row->clip)
2135 {
2136 XRectangle r_save = r;
2137
2138 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2139 r.width = 0;
2140 }
2141
2142 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2143 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2144 {
2145 #ifdef CONVERT_FROM_XRECT
2146 CONVERT_FROM_XRECT (r, *rects);
2147 #else
2148 *rects = r;
2149 #endif
2150 return 1;
2151 }
2152 else
2153 {
2154 /* If we are processing overlapping and allowed to return
2155 multiple clipping rectangles, we exclude the row of the glyph
2156 string from the clipping rectangle. This is to avoid drawing
2157 the same text on the environment with anti-aliasing. */
2158 #ifdef CONVERT_FROM_XRECT
2159 XRectangle rs[2];
2160 #else
2161 XRectangle *rs = rects;
2162 #endif
2163 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2164
2165 if (s->for_overlaps & OVERLAPS_PRED)
2166 {
2167 rs[i] = r;
2168 if (r.y + r.height > row_y)
2169 {
2170 if (r.y < row_y)
2171 rs[i].height = row_y - r.y;
2172 else
2173 rs[i].height = 0;
2174 }
2175 i++;
2176 }
2177 if (s->for_overlaps & OVERLAPS_SUCC)
2178 {
2179 rs[i] = r;
2180 if (r.y < row_y + s->row->visible_height)
2181 {
2182 if (r.y + r.height > row_y + s->row->visible_height)
2183 {
2184 rs[i].y = row_y + s->row->visible_height;
2185 rs[i].height = r.y + r.height - rs[i].y;
2186 }
2187 else
2188 rs[i].height = 0;
2189 }
2190 i++;
2191 }
2192
2193 n = i;
2194 #ifdef CONVERT_FROM_XRECT
2195 for (i = 0; i < n; i++)
2196 CONVERT_FROM_XRECT (rs[i], rects[i]);
2197 #endif
2198 return n;
2199 }
2200 }
2201
2202 /* EXPORT:
2203 Return in *NR the clipping rectangle for glyph string S. */
2204
2205 void
2206 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2207 {
2208 get_glyph_string_clip_rects (s, nr, 1);
2209 }
2210
2211
2212 /* EXPORT:
2213 Return the position and height of the phys cursor in window W.
2214 Set w->phys_cursor_width to width of phys cursor.
2215 */
2216
2217 void
2218 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2219 struct glyph *glyph, int *xp, int *yp, int *heightp)
2220 {
2221 struct frame *f = XFRAME (WINDOW_FRAME (w));
2222 int x, y, wd, h, h0, y0;
2223
2224 /* Compute the width of the rectangle to draw. If on a stretch
2225 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2226 rectangle as wide as the glyph, but use a canonical character
2227 width instead. */
2228 wd = glyph->pixel_width - 1;
2229 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2230 wd++; /* Why? */
2231 #endif
2232
2233 x = w->phys_cursor.x;
2234 if (x < 0)
2235 {
2236 wd += x;
2237 x = 0;
2238 }
2239
2240 if (glyph->type == STRETCH_GLYPH
2241 && !x_stretch_cursor_p)
2242 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2243 w->phys_cursor_width = wd;
2244
2245 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2246
2247 /* If y is below window bottom, ensure that we still see a cursor. */
2248 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2249
2250 h = max (h0, glyph->ascent + glyph->descent);
2251 h0 = min (h0, glyph->ascent + glyph->descent);
2252
2253 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2254 if (y < y0)
2255 {
2256 h = max (h - (y0 - y) + 1, h0);
2257 y = y0 - 1;
2258 }
2259 else
2260 {
2261 y0 = window_text_bottom_y (w) - h0;
2262 if (y > y0)
2263 {
2264 h += y - y0;
2265 y = y0;
2266 }
2267 }
2268
2269 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2270 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2271 *heightp = h;
2272 }
2273
2274 /*
2275 * Remember which glyph the mouse is over.
2276 */
2277
2278 void
2279 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2280 {
2281 Lisp_Object window;
2282 struct window *w;
2283 struct glyph_row *r, *gr, *end_row;
2284 enum window_part part;
2285 enum glyph_row_area area;
2286 int x, y, width, height;
2287
2288 /* Try to determine frame pixel position and size of the glyph under
2289 frame pixel coordinates X/Y on frame F. */
2290
2291 if (!f->glyphs_initialized_p
2292 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2293 NILP (window)))
2294 {
2295 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2296 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2297 goto virtual_glyph;
2298 }
2299
2300 w = XWINDOW (window);
2301 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2302 height = WINDOW_FRAME_LINE_HEIGHT (w);
2303
2304 x = window_relative_x_coord (w, part, gx);
2305 y = gy - WINDOW_TOP_EDGE_Y (w);
2306
2307 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2308 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2309
2310 if (w->pseudo_window_p)
2311 {
2312 area = TEXT_AREA;
2313 part = ON_MODE_LINE; /* Don't adjust margin. */
2314 goto text_glyph;
2315 }
2316
2317 switch (part)
2318 {
2319 case ON_LEFT_MARGIN:
2320 area = LEFT_MARGIN_AREA;
2321 goto text_glyph;
2322
2323 case ON_RIGHT_MARGIN:
2324 area = RIGHT_MARGIN_AREA;
2325 goto text_glyph;
2326
2327 case ON_HEADER_LINE:
2328 case ON_MODE_LINE:
2329 gr = (part == ON_HEADER_LINE
2330 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2331 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2332 gy = gr->y;
2333 area = TEXT_AREA;
2334 goto text_glyph_row_found;
2335
2336 case ON_TEXT:
2337 area = TEXT_AREA;
2338
2339 text_glyph:
2340 gr = 0; gy = 0;
2341 for (; r <= end_row && r->enabled_p; ++r)
2342 if (r->y + r->height > y)
2343 {
2344 gr = r; gy = r->y;
2345 break;
2346 }
2347
2348 text_glyph_row_found:
2349 if (gr && gy <= y)
2350 {
2351 struct glyph *g = gr->glyphs[area];
2352 struct glyph *end = g + gr->used[area];
2353
2354 height = gr->height;
2355 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2356 if (gx + g->pixel_width > x)
2357 break;
2358
2359 if (g < end)
2360 {
2361 if (g->type == IMAGE_GLYPH)
2362 {
2363 /* Don't remember when mouse is over image, as
2364 image may have hot-spots. */
2365 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2366 return;
2367 }
2368 width = g->pixel_width;
2369 }
2370 else
2371 {
2372 /* Use nominal char spacing at end of line. */
2373 x -= gx;
2374 gx += (x / width) * width;
2375 }
2376
2377 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2378 gx += window_box_left_offset (w, area);
2379 }
2380 else
2381 {
2382 /* Use nominal line height at end of window. */
2383 gx = (x / width) * width;
2384 y -= gy;
2385 gy += (y / height) * height;
2386 }
2387 break;
2388
2389 case ON_LEFT_FRINGE:
2390 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2391 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2392 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2393 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2394 goto row_glyph;
2395
2396 case ON_RIGHT_FRINGE:
2397 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2398 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2399 : window_box_right_offset (w, TEXT_AREA));
2400 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2401 goto row_glyph;
2402
2403 case ON_SCROLL_BAR:
2404 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2405 ? 0
2406 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2407 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2408 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2409 : 0)));
2410 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2411
2412 row_glyph:
2413 gr = 0, gy = 0;
2414 for (; r <= end_row && r->enabled_p; ++r)
2415 if (r->y + r->height > y)
2416 {
2417 gr = r; gy = r->y;
2418 break;
2419 }
2420
2421 if (gr && gy <= y)
2422 height = gr->height;
2423 else
2424 {
2425 /* Use nominal line height at end of window. */
2426 y -= gy;
2427 gy += (y / height) * height;
2428 }
2429 break;
2430
2431 default:
2432 ;
2433 virtual_glyph:
2434 /* If there is no glyph under the mouse, then we divide the screen
2435 into a grid of the smallest glyph in the frame, and use that
2436 as our "glyph". */
2437
2438 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2439 round down even for negative values. */
2440 if (gx < 0)
2441 gx -= width - 1;
2442 if (gy < 0)
2443 gy -= height - 1;
2444
2445 gx = (gx / width) * width;
2446 gy = (gy / height) * height;
2447
2448 goto store_rect;
2449 }
2450
2451 gx += WINDOW_LEFT_EDGE_X (w);
2452 gy += WINDOW_TOP_EDGE_Y (w);
2453
2454 store_rect:
2455 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2456
2457 /* Visible feedback for debugging. */
2458 #if 0
2459 #if HAVE_X_WINDOWS
2460 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2461 f->output_data.x->normal_gc,
2462 gx, gy, width, height);
2463 #endif
2464 #endif
2465 }
2466
2467
2468 #endif /* HAVE_WINDOW_SYSTEM */
2469
2470 \f
2471 /***********************************************************************
2472 Lisp form evaluation
2473 ***********************************************************************/
2474
2475 /* Error handler for safe_eval and safe_call. */
2476
2477 static Lisp_Object
2478 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2479 {
2480 add_to_log ("Error during redisplay: %S signaled %S",
2481 Flist (nargs, args), arg);
2482 return Qnil;
2483 }
2484
2485 /* Call function FUNC with the rest of NARGS - 1 arguments
2486 following. Return the result, or nil if something went
2487 wrong. Prevent redisplay during the evaluation. */
2488
2489 Lisp_Object
2490 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2491 {
2492 Lisp_Object val;
2493
2494 if (inhibit_eval_during_redisplay)
2495 val = Qnil;
2496 else
2497 {
2498 va_list ap;
2499 ptrdiff_t i;
2500 ptrdiff_t count = SPECPDL_INDEX ();
2501 struct gcpro gcpro1;
2502 Lisp_Object *args = alloca (nargs * word_size);
2503
2504 args[0] = func;
2505 va_start (ap, func);
2506 for (i = 1; i < nargs; i++)
2507 args[i] = va_arg (ap, Lisp_Object);
2508 va_end (ap);
2509
2510 GCPRO1 (args[0]);
2511 gcpro1.nvars = nargs;
2512 specbind (Qinhibit_redisplay, Qt);
2513 /* Use Qt to ensure debugger does not run,
2514 so there is no possibility of wanting to redisplay. */
2515 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2516 safe_eval_handler);
2517 UNGCPRO;
2518 val = unbind_to (count, val);
2519 }
2520
2521 return val;
2522 }
2523
2524
2525 /* Call function FN with one argument ARG.
2526 Return the result, or nil if something went wrong. */
2527
2528 Lisp_Object
2529 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2530 {
2531 return safe_call (2, fn, arg);
2532 }
2533
2534 static Lisp_Object Qeval;
2535
2536 Lisp_Object
2537 safe_eval (Lisp_Object sexpr)
2538 {
2539 return safe_call1 (Qeval, sexpr);
2540 }
2541
2542 /* Call function FN with two arguments ARG1 and ARG2.
2543 Return the result, or nil if something went wrong. */
2544
2545 Lisp_Object
2546 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2547 {
2548 return safe_call (3, fn, arg1, arg2);
2549 }
2550
2551
2552 \f
2553 /***********************************************************************
2554 Debugging
2555 ***********************************************************************/
2556
2557 #if 0
2558
2559 /* Define CHECK_IT to perform sanity checks on iterators.
2560 This is for debugging. It is too slow to do unconditionally. */
2561
2562 static void
2563 check_it (struct it *it)
2564 {
2565 if (it->method == GET_FROM_STRING)
2566 {
2567 eassert (STRINGP (it->string));
2568 eassert (IT_STRING_CHARPOS (*it) >= 0);
2569 }
2570 else
2571 {
2572 eassert (IT_STRING_CHARPOS (*it) < 0);
2573 if (it->method == GET_FROM_BUFFER)
2574 {
2575 /* Check that character and byte positions agree. */
2576 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2577 }
2578 }
2579
2580 if (it->dpvec)
2581 eassert (it->current.dpvec_index >= 0);
2582 else
2583 eassert (it->current.dpvec_index < 0);
2584 }
2585
2586 #define CHECK_IT(IT) check_it ((IT))
2587
2588 #else /* not 0 */
2589
2590 #define CHECK_IT(IT) (void) 0
2591
2592 #endif /* not 0 */
2593
2594
2595 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2596
2597 /* Check that the window end of window W is what we expect it
2598 to be---the last row in the current matrix displaying text. */
2599
2600 static void
2601 check_window_end (struct window *w)
2602 {
2603 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2604 {
2605 struct glyph_row *row;
2606 eassert ((row = MATRIX_ROW (w->current_matrix,
2607 XFASTINT (w->window_end_vpos)),
2608 !row->enabled_p
2609 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2610 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2611 }
2612 }
2613
2614 #define CHECK_WINDOW_END(W) check_window_end ((W))
2615
2616 #else
2617
2618 #define CHECK_WINDOW_END(W) (void) 0
2619
2620 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2621
2622 /* Return mark position if current buffer has the region of non-zero length,
2623 or -1 otherwise. */
2624
2625 static ptrdiff_t
2626 markpos_of_region (void)
2627 {
2628 if (!NILP (Vtransient_mark_mode)
2629 && !NILP (BVAR (current_buffer, mark_active))
2630 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2631 {
2632 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2633
2634 if (markpos != PT)
2635 return markpos;
2636 }
2637 return -1;
2638 }
2639
2640 /***********************************************************************
2641 Iterator initialization
2642 ***********************************************************************/
2643
2644 /* Initialize IT for displaying current_buffer in window W, starting
2645 at character position CHARPOS. CHARPOS < 0 means that no buffer
2646 position is specified which is useful when the iterator is assigned
2647 a position later. BYTEPOS is the byte position corresponding to
2648 CHARPOS.
2649
2650 If ROW is not null, calls to produce_glyphs with IT as parameter
2651 will produce glyphs in that row.
2652
2653 BASE_FACE_ID is the id of a base face to use. It must be one of
2654 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2655 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2656 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2657
2658 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2659 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2660 will be initialized to use the corresponding mode line glyph row of
2661 the desired matrix of W. */
2662
2663 void
2664 init_iterator (struct it *it, struct window *w,
2665 ptrdiff_t charpos, ptrdiff_t bytepos,
2666 struct glyph_row *row, enum face_id base_face_id)
2667 {
2668 ptrdiff_t markpos;
2669 enum face_id remapped_base_face_id = base_face_id;
2670
2671 /* Some precondition checks. */
2672 eassert (w != NULL && it != NULL);
2673 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2674 && charpos <= ZV));
2675
2676 /* If face attributes have been changed since the last redisplay,
2677 free realized faces now because they depend on face definitions
2678 that might have changed. Don't free faces while there might be
2679 desired matrices pending which reference these faces. */
2680 if (face_change_count && !inhibit_free_realized_faces)
2681 {
2682 face_change_count = 0;
2683 free_all_realized_faces (Qnil);
2684 }
2685
2686 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2687 if (! NILP (Vface_remapping_alist))
2688 remapped_base_face_id
2689 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2690
2691 /* Use one of the mode line rows of W's desired matrix if
2692 appropriate. */
2693 if (row == NULL)
2694 {
2695 if (base_face_id == MODE_LINE_FACE_ID
2696 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2697 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2698 else if (base_face_id == HEADER_LINE_FACE_ID)
2699 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2700 }
2701
2702 /* Clear IT. */
2703 memset (it, 0, sizeof *it);
2704 it->current.overlay_string_index = -1;
2705 it->current.dpvec_index = -1;
2706 it->base_face_id = remapped_base_face_id;
2707 it->string = Qnil;
2708 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2709 it->paragraph_embedding = L2R;
2710 it->bidi_it.string.lstring = Qnil;
2711 it->bidi_it.string.s = NULL;
2712 it->bidi_it.string.bufpos = 0;
2713 it->bidi_it.w = w;
2714
2715 /* The window in which we iterate over current_buffer: */
2716 XSETWINDOW (it->window, w);
2717 it->w = w;
2718 it->f = XFRAME (w->frame);
2719
2720 it->cmp_it.id = -1;
2721
2722 /* Extra space between lines (on window systems only). */
2723 if (base_face_id == DEFAULT_FACE_ID
2724 && FRAME_WINDOW_P (it->f))
2725 {
2726 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2727 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2728 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2729 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2730 * FRAME_LINE_HEIGHT (it->f));
2731 else if (it->f->extra_line_spacing > 0)
2732 it->extra_line_spacing = it->f->extra_line_spacing;
2733 it->max_extra_line_spacing = 0;
2734 }
2735
2736 /* If realized faces have been removed, e.g. because of face
2737 attribute changes of named faces, recompute them. When running
2738 in batch mode, the face cache of the initial frame is null. If
2739 we happen to get called, make a dummy face cache. */
2740 if (FRAME_FACE_CACHE (it->f) == NULL)
2741 init_frame_faces (it->f);
2742 if (FRAME_FACE_CACHE (it->f)->used == 0)
2743 recompute_basic_faces (it->f);
2744
2745 /* Current value of the `slice', `space-width', and 'height' properties. */
2746 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2747 it->space_width = Qnil;
2748 it->font_height = Qnil;
2749 it->override_ascent = -1;
2750
2751 /* Are control characters displayed as `^C'? */
2752 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2753
2754 /* -1 means everything between a CR and the following line end
2755 is invisible. >0 means lines indented more than this value are
2756 invisible. */
2757 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2758 ? (clip_to_bounds
2759 (-1, XINT (BVAR (current_buffer, selective_display)),
2760 PTRDIFF_MAX))
2761 : (!NILP (BVAR (current_buffer, selective_display))
2762 ? -1 : 0));
2763 it->selective_display_ellipsis_p
2764 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2765
2766 /* Display table to use. */
2767 it->dp = window_display_table (w);
2768
2769 /* Are multibyte characters enabled in current_buffer? */
2770 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2771
2772 /* If visible region is of non-zero length, set IT->region_beg_charpos
2773 and IT->region_end_charpos to the start and end of a visible region
2774 in window IT->w. Set both to -1 to indicate no region. */
2775 markpos = markpos_of_region ();
2776 if (markpos >= 0
2777 /* Maybe highlight only in selected window. */
2778 && (/* Either show region everywhere. */
2779 highlight_nonselected_windows
2780 /* Or show region in the selected window. */
2781 || w == XWINDOW (selected_window)
2782 /* Or show the region if we are in the mini-buffer and W is
2783 the window the mini-buffer refers to. */
2784 || (MINI_WINDOW_P (XWINDOW (selected_window))
2785 && WINDOWP (minibuf_selected_window)
2786 && w == XWINDOW (minibuf_selected_window))))
2787 {
2788 it->region_beg_charpos = min (PT, markpos);
2789 it->region_end_charpos = max (PT, markpos);
2790 }
2791 else
2792 it->region_beg_charpos = it->region_end_charpos = -1;
2793
2794 /* Get the position at which the redisplay_end_trigger hook should
2795 be run, if it is to be run at all. */
2796 if (MARKERP (w->redisplay_end_trigger)
2797 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2798 it->redisplay_end_trigger_charpos
2799 = marker_position (w->redisplay_end_trigger);
2800 else if (INTEGERP (w->redisplay_end_trigger))
2801 it->redisplay_end_trigger_charpos =
2802 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2803
2804 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2805
2806 /* Are lines in the display truncated? */
2807 if (base_face_id != DEFAULT_FACE_ID
2808 || it->w->hscroll
2809 || (! WINDOW_FULL_WIDTH_P (it->w)
2810 && ((!NILP (Vtruncate_partial_width_windows)
2811 && !INTEGERP (Vtruncate_partial_width_windows))
2812 || (INTEGERP (Vtruncate_partial_width_windows)
2813 && (WINDOW_TOTAL_COLS (it->w)
2814 < XINT (Vtruncate_partial_width_windows))))))
2815 it->line_wrap = TRUNCATE;
2816 else if (NILP (BVAR (current_buffer, truncate_lines)))
2817 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2818 ? WINDOW_WRAP : WORD_WRAP;
2819 else
2820 it->line_wrap = TRUNCATE;
2821
2822 /* Get dimensions of truncation and continuation glyphs. These are
2823 displayed as fringe bitmaps under X, but we need them for such
2824 frames when the fringes are turned off. But leave the dimensions
2825 zero for tooltip frames, as these glyphs look ugly there and also
2826 sabotage calculations of tooltip dimensions in x-show-tip. */
2827 #ifdef HAVE_WINDOW_SYSTEM
2828 if (!(FRAME_WINDOW_P (it->f)
2829 && FRAMEP (tip_frame)
2830 && it->f == XFRAME (tip_frame)))
2831 #endif
2832 {
2833 if (it->line_wrap == TRUNCATE)
2834 {
2835 /* We will need the truncation glyph. */
2836 eassert (it->glyph_row == NULL);
2837 produce_special_glyphs (it, IT_TRUNCATION);
2838 it->truncation_pixel_width = it->pixel_width;
2839 }
2840 else
2841 {
2842 /* We will need the continuation glyph. */
2843 eassert (it->glyph_row == NULL);
2844 produce_special_glyphs (it, IT_CONTINUATION);
2845 it->continuation_pixel_width = it->pixel_width;
2846 }
2847 }
2848
2849 /* Reset these values to zero because the produce_special_glyphs
2850 above has changed them. */
2851 it->pixel_width = it->ascent = it->descent = 0;
2852 it->phys_ascent = it->phys_descent = 0;
2853
2854 /* Set this after getting the dimensions of truncation and
2855 continuation glyphs, so that we don't produce glyphs when calling
2856 produce_special_glyphs, above. */
2857 it->glyph_row = row;
2858 it->area = TEXT_AREA;
2859
2860 /* Forget any previous info about this row being reversed. */
2861 if (it->glyph_row)
2862 it->glyph_row->reversed_p = 0;
2863
2864 /* Get the dimensions of the display area. The display area
2865 consists of the visible window area plus a horizontally scrolled
2866 part to the left of the window. All x-values are relative to the
2867 start of this total display area. */
2868 if (base_face_id != DEFAULT_FACE_ID)
2869 {
2870 /* Mode lines, menu bar in terminal frames. */
2871 it->first_visible_x = 0;
2872 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2873 }
2874 else
2875 {
2876 it->first_visible_x =
2877 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2878 it->last_visible_x = (it->first_visible_x
2879 + window_box_width (w, TEXT_AREA));
2880
2881 /* If we truncate lines, leave room for the truncation glyph(s) at
2882 the right margin. Otherwise, leave room for the continuation
2883 glyph(s). Done only if the window has no fringes. Since we
2884 don't know at this point whether there will be any R2L lines in
2885 the window, we reserve space for truncation/continuation glyphs
2886 even if only one of the fringes is absent. */
2887 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2888 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2889 {
2890 if (it->line_wrap == TRUNCATE)
2891 it->last_visible_x -= it->truncation_pixel_width;
2892 else
2893 it->last_visible_x -= it->continuation_pixel_width;
2894 }
2895
2896 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2897 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2898 }
2899
2900 /* Leave room for a border glyph. */
2901 if (!FRAME_WINDOW_P (it->f)
2902 && !WINDOW_RIGHTMOST_P (it->w))
2903 it->last_visible_x -= 1;
2904
2905 it->last_visible_y = window_text_bottom_y (w);
2906
2907 /* For mode lines and alike, arrange for the first glyph having a
2908 left box line if the face specifies a box. */
2909 if (base_face_id != DEFAULT_FACE_ID)
2910 {
2911 struct face *face;
2912
2913 it->face_id = remapped_base_face_id;
2914
2915 /* If we have a boxed mode line, make the first character appear
2916 with a left box line. */
2917 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2918 if (face->box != FACE_NO_BOX)
2919 it->start_of_box_run_p = 1;
2920 }
2921
2922 /* If a buffer position was specified, set the iterator there,
2923 getting overlays and face properties from that position. */
2924 if (charpos >= BUF_BEG (current_buffer))
2925 {
2926 it->end_charpos = ZV;
2927 eassert (charpos == BYTE_TO_CHAR (bytepos));
2928 IT_CHARPOS (*it) = charpos;
2929 IT_BYTEPOS (*it) = bytepos;
2930
2931 /* We will rely on `reseat' to set this up properly, via
2932 handle_face_prop. */
2933 it->face_id = it->base_face_id;
2934
2935 it->start = it->current;
2936 /* Do we need to reorder bidirectional text? Not if this is a
2937 unibyte buffer: by definition, none of the single-byte
2938 characters are strong R2L, so no reordering is needed. And
2939 bidi.c doesn't support unibyte buffers anyway. Also, don't
2940 reorder while we are loading loadup.el, since the tables of
2941 character properties needed for reordering are not yet
2942 available. */
2943 it->bidi_p =
2944 NILP (Vpurify_flag)
2945 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2946 && it->multibyte_p;
2947
2948 /* If we are to reorder bidirectional text, init the bidi
2949 iterator. */
2950 if (it->bidi_p)
2951 {
2952 /* Note the paragraph direction that this buffer wants to
2953 use. */
2954 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2955 Qleft_to_right))
2956 it->paragraph_embedding = L2R;
2957 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2958 Qright_to_left))
2959 it->paragraph_embedding = R2L;
2960 else
2961 it->paragraph_embedding = NEUTRAL_DIR;
2962 bidi_unshelve_cache (NULL, 0);
2963 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2964 &it->bidi_it);
2965 }
2966
2967 /* Compute faces etc. */
2968 reseat (it, it->current.pos, 1);
2969 }
2970
2971 CHECK_IT (it);
2972 }
2973
2974
2975 /* Initialize IT for the display of window W with window start POS. */
2976
2977 void
2978 start_display (struct it *it, struct window *w, struct text_pos pos)
2979 {
2980 struct glyph_row *row;
2981 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2982
2983 row = w->desired_matrix->rows + first_vpos;
2984 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2985 it->first_vpos = first_vpos;
2986
2987 /* Don't reseat to previous visible line start if current start
2988 position is in a string or image. */
2989 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2990 {
2991 int start_at_line_beg_p;
2992 int first_y = it->current_y;
2993
2994 /* If window start is not at a line start, skip forward to POS to
2995 get the correct continuation lines width. */
2996 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2997 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2998 if (!start_at_line_beg_p)
2999 {
3000 int new_x;
3001
3002 reseat_at_previous_visible_line_start (it);
3003 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3004
3005 new_x = it->current_x + it->pixel_width;
3006
3007 /* If lines are continued, this line may end in the middle
3008 of a multi-glyph character (e.g. a control character
3009 displayed as \003, or in the middle of an overlay
3010 string). In this case move_it_to above will not have
3011 taken us to the start of the continuation line but to the
3012 end of the continued line. */
3013 if (it->current_x > 0
3014 && it->line_wrap != TRUNCATE /* Lines are continued. */
3015 && (/* And glyph doesn't fit on the line. */
3016 new_x > it->last_visible_x
3017 /* Or it fits exactly and we're on a window
3018 system frame. */
3019 || (new_x == it->last_visible_x
3020 && FRAME_WINDOW_P (it->f)
3021 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3022 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3023 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3024 {
3025 if ((it->current.dpvec_index >= 0
3026 || it->current.overlay_string_index >= 0)
3027 /* If we are on a newline from a display vector or
3028 overlay string, then we are already at the end of
3029 a screen line; no need to go to the next line in
3030 that case, as this line is not really continued.
3031 (If we do go to the next line, C-e will not DTRT.) */
3032 && it->c != '\n')
3033 {
3034 set_iterator_to_next (it, 1);
3035 move_it_in_display_line_to (it, -1, -1, 0);
3036 }
3037
3038 it->continuation_lines_width += it->current_x;
3039 }
3040 /* If the character at POS is displayed via a display
3041 vector, move_it_to above stops at the final glyph of
3042 IT->dpvec. To make the caller redisplay that character
3043 again (a.k.a. start at POS), we need to reset the
3044 dpvec_index to the beginning of IT->dpvec. */
3045 else if (it->current.dpvec_index >= 0)
3046 it->current.dpvec_index = 0;
3047
3048 /* We're starting a new display line, not affected by the
3049 height of the continued line, so clear the appropriate
3050 fields in the iterator structure. */
3051 it->max_ascent = it->max_descent = 0;
3052 it->max_phys_ascent = it->max_phys_descent = 0;
3053
3054 it->current_y = first_y;
3055 it->vpos = 0;
3056 it->current_x = it->hpos = 0;
3057 }
3058 }
3059 }
3060
3061
3062 /* Return 1 if POS is a position in ellipses displayed for invisible
3063 text. W is the window we display, for text property lookup. */
3064
3065 static int
3066 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3067 {
3068 Lisp_Object prop, window;
3069 int ellipses_p = 0;
3070 ptrdiff_t charpos = CHARPOS (pos->pos);
3071
3072 /* If POS specifies a position in a display vector, this might
3073 be for an ellipsis displayed for invisible text. We won't
3074 get the iterator set up for delivering that ellipsis unless
3075 we make sure that it gets aware of the invisible text. */
3076 if (pos->dpvec_index >= 0
3077 && pos->overlay_string_index < 0
3078 && CHARPOS (pos->string_pos) < 0
3079 && charpos > BEGV
3080 && (XSETWINDOW (window, w),
3081 prop = Fget_char_property (make_number (charpos),
3082 Qinvisible, window),
3083 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3084 {
3085 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3086 window);
3087 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3088 }
3089
3090 return ellipses_p;
3091 }
3092
3093
3094 /* Initialize IT for stepping through current_buffer in window W,
3095 starting at position POS that includes overlay string and display
3096 vector/ control character translation position information. Value
3097 is zero if there are overlay strings with newlines at POS. */
3098
3099 static int
3100 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3101 {
3102 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3103 int i, overlay_strings_with_newlines = 0;
3104
3105 /* If POS specifies a position in a display vector, this might
3106 be for an ellipsis displayed for invisible text. We won't
3107 get the iterator set up for delivering that ellipsis unless
3108 we make sure that it gets aware of the invisible text. */
3109 if (in_ellipses_for_invisible_text_p (pos, w))
3110 {
3111 --charpos;
3112 bytepos = 0;
3113 }
3114
3115 /* Keep in mind: the call to reseat in init_iterator skips invisible
3116 text, so we might end up at a position different from POS. This
3117 is only a problem when POS is a row start after a newline and an
3118 overlay starts there with an after-string, and the overlay has an
3119 invisible property. Since we don't skip invisible text in
3120 display_line and elsewhere immediately after consuming the
3121 newline before the row start, such a POS will not be in a string,
3122 but the call to init_iterator below will move us to the
3123 after-string. */
3124 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3125
3126 /* This only scans the current chunk -- it should scan all chunks.
3127 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3128 to 16 in 22.1 to make this a lesser problem. */
3129 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3130 {
3131 const char *s = SSDATA (it->overlay_strings[i]);
3132 const char *e = s + SBYTES (it->overlay_strings[i]);
3133
3134 while (s < e && *s != '\n')
3135 ++s;
3136
3137 if (s < e)
3138 {
3139 overlay_strings_with_newlines = 1;
3140 break;
3141 }
3142 }
3143
3144 /* If position is within an overlay string, set up IT to the right
3145 overlay string. */
3146 if (pos->overlay_string_index >= 0)
3147 {
3148 int relative_index;
3149
3150 /* If the first overlay string happens to have a `display'
3151 property for an image, the iterator will be set up for that
3152 image, and we have to undo that setup first before we can
3153 correct the overlay string index. */
3154 if (it->method == GET_FROM_IMAGE)
3155 pop_it (it);
3156
3157 /* We already have the first chunk of overlay strings in
3158 IT->overlay_strings. Load more until the one for
3159 pos->overlay_string_index is in IT->overlay_strings. */
3160 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3161 {
3162 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3163 it->current.overlay_string_index = 0;
3164 while (n--)
3165 {
3166 load_overlay_strings (it, 0);
3167 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3168 }
3169 }
3170
3171 it->current.overlay_string_index = pos->overlay_string_index;
3172 relative_index = (it->current.overlay_string_index
3173 % OVERLAY_STRING_CHUNK_SIZE);
3174 it->string = it->overlay_strings[relative_index];
3175 eassert (STRINGP (it->string));
3176 it->current.string_pos = pos->string_pos;
3177 it->method = GET_FROM_STRING;
3178 it->end_charpos = SCHARS (it->string);
3179 /* Set up the bidi iterator for this overlay string. */
3180 if (it->bidi_p)
3181 {
3182 it->bidi_it.string.lstring = it->string;
3183 it->bidi_it.string.s = NULL;
3184 it->bidi_it.string.schars = SCHARS (it->string);
3185 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3186 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3187 it->bidi_it.string.unibyte = !it->multibyte_p;
3188 it->bidi_it.w = it->w;
3189 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3190 FRAME_WINDOW_P (it->f), &it->bidi_it);
3191
3192 /* Synchronize the state of the bidi iterator with
3193 pos->string_pos. For any string position other than
3194 zero, this will be done automagically when we resume
3195 iteration over the string and get_visually_first_element
3196 is called. But if string_pos is zero, and the string is
3197 to be reordered for display, we need to resync manually,
3198 since it could be that the iteration state recorded in
3199 pos ended at string_pos of 0 moving backwards in string. */
3200 if (CHARPOS (pos->string_pos) == 0)
3201 {
3202 get_visually_first_element (it);
3203 if (IT_STRING_CHARPOS (*it) != 0)
3204 do {
3205 /* Paranoia. */
3206 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3207 bidi_move_to_visually_next (&it->bidi_it);
3208 } while (it->bidi_it.charpos != 0);
3209 }
3210 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3211 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3212 }
3213 }
3214
3215 if (CHARPOS (pos->string_pos) >= 0)
3216 {
3217 /* Recorded position is not in an overlay string, but in another
3218 string. This can only be a string from a `display' property.
3219 IT should already be filled with that string. */
3220 it->current.string_pos = pos->string_pos;
3221 eassert (STRINGP (it->string));
3222 if (it->bidi_p)
3223 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3224 FRAME_WINDOW_P (it->f), &it->bidi_it);
3225 }
3226
3227 /* Restore position in display vector translations, control
3228 character translations or ellipses. */
3229 if (pos->dpvec_index >= 0)
3230 {
3231 if (it->dpvec == NULL)
3232 get_next_display_element (it);
3233 eassert (it->dpvec && it->current.dpvec_index == 0);
3234 it->current.dpvec_index = pos->dpvec_index;
3235 }
3236
3237 CHECK_IT (it);
3238 return !overlay_strings_with_newlines;
3239 }
3240
3241
3242 /* Initialize IT for stepping through current_buffer in window W
3243 starting at ROW->start. */
3244
3245 static void
3246 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3247 {
3248 init_from_display_pos (it, w, &row->start);
3249 it->start = row->start;
3250 it->continuation_lines_width = row->continuation_lines_width;
3251 CHECK_IT (it);
3252 }
3253
3254
3255 /* Initialize IT for stepping through current_buffer in window W
3256 starting in the line following ROW, i.e. starting at ROW->end.
3257 Value is zero if there are overlay strings with newlines at ROW's
3258 end position. */
3259
3260 static int
3261 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3262 {
3263 int success = 0;
3264
3265 if (init_from_display_pos (it, w, &row->end))
3266 {
3267 if (row->continued_p)
3268 it->continuation_lines_width
3269 = row->continuation_lines_width + row->pixel_width;
3270 CHECK_IT (it);
3271 success = 1;
3272 }
3273
3274 return success;
3275 }
3276
3277
3278
3279 \f
3280 /***********************************************************************
3281 Text properties
3282 ***********************************************************************/
3283
3284 /* Called when IT reaches IT->stop_charpos. Handle text property and
3285 overlay changes. Set IT->stop_charpos to the next position where
3286 to stop. */
3287
3288 static void
3289 handle_stop (struct it *it)
3290 {
3291 enum prop_handled handled;
3292 int handle_overlay_change_p;
3293 struct props *p;
3294
3295 it->dpvec = NULL;
3296 it->current.dpvec_index = -1;
3297 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3298 it->ignore_overlay_strings_at_pos_p = 0;
3299 it->ellipsis_p = 0;
3300
3301 /* Use face of preceding text for ellipsis (if invisible) */
3302 if (it->selective_display_ellipsis_p)
3303 it->saved_face_id = it->face_id;
3304
3305 do
3306 {
3307 handled = HANDLED_NORMALLY;
3308
3309 /* Call text property handlers. */
3310 for (p = it_props; p->handler; ++p)
3311 {
3312 handled = p->handler (it);
3313
3314 if (handled == HANDLED_RECOMPUTE_PROPS)
3315 break;
3316 else if (handled == HANDLED_RETURN)
3317 {
3318 /* We still want to show before and after strings from
3319 overlays even if the actual buffer text is replaced. */
3320 if (!handle_overlay_change_p
3321 || it->sp > 1
3322 /* Don't call get_overlay_strings_1 if we already
3323 have overlay strings loaded, because doing so
3324 will load them again and push the iterator state
3325 onto the stack one more time, which is not
3326 expected by the rest of the code that processes
3327 overlay strings. */
3328 || (it->current.overlay_string_index < 0
3329 ? !get_overlay_strings_1 (it, 0, 0)
3330 : 0))
3331 {
3332 if (it->ellipsis_p)
3333 setup_for_ellipsis (it, 0);
3334 /* When handling a display spec, we might load an
3335 empty string. In that case, discard it here. We
3336 used to discard it in handle_single_display_spec,
3337 but that causes get_overlay_strings_1, above, to
3338 ignore overlay strings that we must check. */
3339 if (STRINGP (it->string) && !SCHARS (it->string))
3340 pop_it (it);
3341 return;
3342 }
3343 else if (STRINGP (it->string) && !SCHARS (it->string))
3344 pop_it (it);
3345 else
3346 {
3347 it->ignore_overlay_strings_at_pos_p = 1;
3348 it->string_from_display_prop_p = 0;
3349 it->from_disp_prop_p = 0;
3350 handle_overlay_change_p = 0;
3351 }
3352 handled = HANDLED_RECOMPUTE_PROPS;
3353 break;
3354 }
3355 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3356 handle_overlay_change_p = 0;
3357 }
3358
3359 if (handled != HANDLED_RECOMPUTE_PROPS)
3360 {
3361 /* Don't check for overlay strings below when set to deliver
3362 characters from a display vector. */
3363 if (it->method == GET_FROM_DISPLAY_VECTOR)
3364 handle_overlay_change_p = 0;
3365
3366 /* Handle overlay changes.
3367 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3368 if it finds overlays. */
3369 if (handle_overlay_change_p)
3370 handled = handle_overlay_change (it);
3371 }
3372
3373 if (it->ellipsis_p)
3374 {
3375 setup_for_ellipsis (it, 0);
3376 break;
3377 }
3378 }
3379 while (handled == HANDLED_RECOMPUTE_PROPS);
3380
3381 /* Determine where to stop next. */
3382 if (handled == HANDLED_NORMALLY)
3383 compute_stop_pos (it);
3384 }
3385
3386
3387 /* Compute IT->stop_charpos from text property and overlay change
3388 information for IT's current position. */
3389
3390 static void
3391 compute_stop_pos (struct it *it)
3392 {
3393 register INTERVAL iv, next_iv;
3394 Lisp_Object object, limit, position;
3395 ptrdiff_t charpos, bytepos;
3396
3397 if (STRINGP (it->string))
3398 {
3399 /* Strings are usually short, so don't limit the search for
3400 properties. */
3401 it->stop_charpos = it->end_charpos;
3402 object = it->string;
3403 limit = Qnil;
3404 charpos = IT_STRING_CHARPOS (*it);
3405 bytepos = IT_STRING_BYTEPOS (*it);
3406 }
3407 else
3408 {
3409 ptrdiff_t pos;
3410
3411 /* If end_charpos is out of range for some reason, such as a
3412 misbehaving display function, rationalize it (Bug#5984). */
3413 if (it->end_charpos > ZV)
3414 it->end_charpos = ZV;
3415 it->stop_charpos = it->end_charpos;
3416
3417 /* If next overlay change is in front of the current stop pos
3418 (which is IT->end_charpos), stop there. Note: value of
3419 next_overlay_change is point-max if no overlay change
3420 follows. */
3421 charpos = IT_CHARPOS (*it);
3422 bytepos = IT_BYTEPOS (*it);
3423 pos = next_overlay_change (charpos);
3424 if (pos < it->stop_charpos)
3425 it->stop_charpos = pos;
3426
3427 /* If showing the region, we have to stop at the region
3428 start or end because the face might change there. */
3429 if (it->region_beg_charpos > 0)
3430 {
3431 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3432 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3433 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3434 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3435 }
3436
3437 /* Set up variables for computing the stop position from text
3438 property changes. */
3439 XSETBUFFER (object, current_buffer);
3440 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3441 }
3442
3443 /* Get the interval containing IT's position. Value is a null
3444 interval if there isn't such an interval. */
3445 position = make_number (charpos);
3446 iv = validate_interval_range (object, &position, &position, 0);
3447 if (iv)
3448 {
3449 Lisp_Object values_here[LAST_PROP_IDX];
3450 struct props *p;
3451
3452 /* Get properties here. */
3453 for (p = it_props; p->handler; ++p)
3454 values_here[p->idx] = textget (iv->plist, *p->name);
3455
3456 /* Look for an interval following iv that has different
3457 properties. */
3458 for (next_iv = next_interval (iv);
3459 (next_iv
3460 && (NILP (limit)
3461 || XFASTINT (limit) > next_iv->position));
3462 next_iv = next_interval (next_iv))
3463 {
3464 for (p = it_props; p->handler; ++p)
3465 {
3466 Lisp_Object new_value;
3467
3468 new_value = textget (next_iv->plist, *p->name);
3469 if (!EQ (values_here[p->idx], new_value))
3470 break;
3471 }
3472
3473 if (p->handler)
3474 break;
3475 }
3476
3477 if (next_iv)
3478 {
3479 if (INTEGERP (limit)
3480 && next_iv->position >= XFASTINT (limit))
3481 /* No text property change up to limit. */
3482 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3483 else
3484 /* Text properties change in next_iv. */
3485 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3486 }
3487 }
3488
3489 if (it->cmp_it.id < 0)
3490 {
3491 ptrdiff_t stoppos = it->end_charpos;
3492
3493 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3494 stoppos = -1;
3495 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3496 stoppos, it->string);
3497 }
3498
3499 eassert (STRINGP (it->string)
3500 || (it->stop_charpos >= BEGV
3501 && it->stop_charpos >= IT_CHARPOS (*it)));
3502 }
3503
3504
3505 /* Return the position of the next overlay change after POS in
3506 current_buffer. Value is point-max if no overlay change
3507 follows. This is like `next-overlay-change' but doesn't use
3508 xmalloc. */
3509
3510 static ptrdiff_t
3511 next_overlay_change (ptrdiff_t pos)
3512 {
3513 ptrdiff_t i, noverlays;
3514 ptrdiff_t endpos;
3515 Lisp_Object *overlays;
3516
3517 /* Get all overlays at the given position. */
3518 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3519
3520 /* If any of these overlays ends before endpos,
3521 use its ending point instead. */
3522 for (i = 0; i < noverlays; ++i)
3523 {
3524 Lisp_Object oend;
3525 ptrdiff_t oendpos;
3526
3527 oend = OVERLAY_END (overlays[i]);
3528 oendpos = OVERLAY_POSITION (oend);
3529 endpos = min (endpos, oendpos);
3530 }
3531
3532 return endpos;
3533 }
3534
3535 /* How many characters forward to search for a display property or
3536 display string. Searching too far forward makes the bidi display
3537 sluggish, especially in small windows. */
3538 #define MAX_DISP_SCAN 250
3539
3540 /* Return the character position of a display string at or after
3541 position specified by POSITION. If no display string exists at or
3542 after POSITION, return ZV. A display string is either an overlay
3543 with `display' property whose value is a string, or a `display'
3544 text property whose value is a string. STRING is data about the
3545 string to iterate; if STRING->lstring is nil, we are iterating a
3546 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3547 on a GUI frame. DISP_PROP is set to zero if we searched
3548 MAX_DISP_SCAN characters forward without finding any display
3549 strings, non-zero otherwise. It is set to 2 if the display string
3550 uses any kind of `(space ...)' spec that will produce a stretch of
3551 white space in the text area. */
3552 ptrdiff_t
3553 compute_display_string_pos (struct text_pos *position,
3554 struct bidi_string_data *string,
3555 struct window *w,
3556 int frame_window_p, int *disp_prop)
3557 {
3558 /* OBJECT = nil means current buffer. */
3559 Lisp_Object object, object1;
3560 Lisp_Object pos, spec, limpos;
3561 int string_p = (string && (STRINGP (string->lstring) || string->s));
3562 ptrdiff_t eob = string_p ? string->schars : ZV;
3563 ptrdiff_t begb = string_p ? 0 : BEGV;
3564 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3565 ptrdiff_t lim =
3566 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3567 struct text_pos tpos;
3568 int rv = 0;
3569
3570 if (string && STRINGP (string->lstring))
3571 object1 = object = string->lstring;
3572 else if (w && !string_p)
3573 {
3574 XSETWINDOW (object, w);
3575 object1 = Qnil;
3576 }
3577 else
3578 object1 = object = Qnil;
3579
3580 *disp_prop = 1;
3581
3582 if (charpos >= eob
3583 /* We don't support display properties whose values are strings
3584 that have display string properties. */
3585 || string->from_disp_str
3586 /* C strings cannot have display properties. */
3587 || (string->s && !STRINGP (object)))
3588 {
3589 *disp_prop = 0;
3590 return eob;
3591 }
3592
3593 /* If the character at CHARPOS is where the display string begins,
3594 return CHARPOS. */
3595 pos = make_number (charpos);
3596 if (STRINGP (object))
3597 bufpos = string->bufpos;
3598 else
3599 bufpos = charpos;
3600 tpos = *position;
3601 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3602 && (charpos <= begb
3603 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3604 object),
3605 spec))
3606 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3607 frame_window_p)))
3608 {
3609 if (rv == 2)
3610 *disp_prop = 2;
3611 return charpos;
3612 }
3613
3614 /* Look forward for the first character with a `display' property
3615 that will replace the underlying text when displayed. */
3616 limpos = make_number (lim);
3617 do {
3618 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3619 CHARPOS (tpos) = XFASTINT (pos);
3620 if (CHARPOS (tpos) >= lim)
3621 {
3622 *disp_prop = 0;
3623 break;
3624 }
3625 if (STRINGP (object))
3626 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3627 else
3628 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3629 spec = Fget_char_property (pos, Qdisplay, object);
3630 if (!STRINGP (object))
3631 bufpos = CHARPOS (tpos);
3632 } while (NILP (spec)
3633 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3634 bufpos, frame_window_p)));
3635 if (rv == 2)
3636 *disp_prop = 2;
3637
3638 return CHARPOS (tpos);
3639 }
3640
3641 /* Return the character position of the end of the display string that
3642 started at CHARPOS. If there's no display string at CHARPOS,
3643 return -1. A display string is either an overlay with `display'
3644 property whose value is a string or a `display' text property whose
3645 value is a string. */
3646 ptrdiff_t
3647 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3648 {
3649 /* OBJECT = nil means current buffer. */
3650 Lisp_Object object =
3651 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3652 Lisp_Object pos = make_number (charpos);
3653 ptrdiff_t eob =
3654 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3655
3656 if (charpos >= eob || (string->s && !STRINGP (object)))
3657 return eob;
3658
3659 /* It could happen that the display property or overlay was removed
3660 since we found it in compute_display_string_pos above. One way
3661 this can happen is if JIT font-lock was called (through
3662 handle_fontified_prop), and jit-lock-functions remove text
3663 properties or overlays from the portion of buffer that includes
3664 CHARPOS. Muse mode is known to do that, for example. In this
3665 case, we return -1 to the caller, to signal that no display
3666 string is actually present at CHARPOS. See bidi_fetch_char for
3667 how this is handled.
3668
3669 An alternative would be to never look for display properties past
3670 it->stop_charpos. But neither compute_display_string_pos nor
3671 bidi_fetch_char that calls it know or care where the next
3672 stop_charpos is. */
3673 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3674 return -1;
3675
3676 /* Look forward for the first character where the `display' property
3677 changes. */
3678 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3679
3680 return XFASTINT (pos);
3681 }
3682
3683
3684 \f
3685 /***********************************************************************
3686 Fontification
3687 ***********************************************************************/
3688
3689 /* Handle changes in the `fontified' property of the current buffer by
3690 calling hook functions from Qfontification_functions to fontify
3691 regions of text. */
3692
3693 static enum prop_handled
3694 handle_fontified_prop (struct it *it)
3695 {
3696 Lisp_Object prop, pos;
3697 enum prop_handled handled = HANDLED_NORMALLY;
3698
3699 if (!NILP (Vmemory_full))
3700 return handled;
3701
3702 /* Get the value of the `fontified' property at IT's current buffer
3703 position. (The `fontified' property doesn't have a special
3704 meaning in strings.) If the value is nil, call functions from
3705 Qfontification_functions. */
3706 if (!STRINGP (it->string)
3707 && it->s == NULL
3708 && !NILP (Vfontification_functions)
3709 && !NILP (Vrun_hooks)
3710 && (pos = make_number (IT_CHARPOS (*it)),
3711 prop = Fget_char_property (pos, Qfontified, Qnil),
3712 /* Ignore the special cased nil value always present at EOB since
3713 no amount of fontifying will be able to change it. */
3714 NILP (prop) && IT_CHARPOS (*it) < Z))
3715 {
3716 ptrdiff_t count = SPECPDL_INDEX ();
3717 Lisp_Object val;
3718 struct buffer *obuf = current_buffer;
3719 int begv = BEGV, zv = ZV;
3720 int old_clip_changed = current_buffer->clip_changed;
3721
3722 val = Vfontification_functions;
3723 specbind (Qfontification_functions, Qnil);
3724
3725 eassert (it->end_charpos == ZV);
3726
3727 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3728 safe_call1 (val, pos);
3729 else
3730 {
3731 Lisp_Object fns, fn;
3732 struct gcpro gcpro1, gcpro2;
3733
3734 fns = Qnil;
3735 GCPRO2 (val, fns);
3736
3737 for (; CONSP (val); val = XCDR (val))
3738 {
3739 fn = XCAR (val);
3740
3741 if (EQ (fn, Qt))
3742 {
3743 /* A value of t indicates this hook has a local
3744 binding; it means to run the global binding too.
3745 In a global value, t should not occur. If it
3746 does, we must ignore it to avoid an endless
3747 loop. */
3748 for (fns = Fdefault_value (Qfontification_functions);
3749 CONSP (fns);
3750 fns = XCDR (fns))
3751 {
3752 fn = XCAR (fns);
3753 if (!EQ (fn, Qt))
3754 safe_call1 (fn, pos);
3755 }
3756 }
3757 else
3758 safe_call1 (fn, pos);
3759 }
3760
3761 UNGCPRO;
3762 }
3763
3764 unbind_to (count, Qnil);
3765
3766 /* Fontification functions routinely call `save-restriction'.
3767 Normally, this tags clip_changed, which can confuse redisplay
3768 (see discussion in Bug#6671). Since we don't perform any
3769 special handling of fontification changes in the case where
3770 `save-restriction' isn't called, there's no point doing so in
3771 this case either. So, if the buffer's restrictions are
3772 actually left unchanged, reset clip_changed. */
3773 if (obuf == current_buffer)
3774 {
3775 if (begv == BEGV && zv == ZV)
3776 current_buffer->clip_changed = old_clip_changed;
3777 }
3778 /* There isn't much we can reasonably do to protect against
3779 misbehaving fontification, but here's a fig leaf. */
3780 else if (BUFFER_LIVE_P (obuf))
3781 set_buffer_internal_1 (obuf);
3782
3783 /* The fontification code may have added/removed text.
3784 It could do even a lot worse, but let's at least protect against
3785 the most obvious case where only the text past `pos' gets changed',
3786 as is/was done in grep.el where some escapes sequences are turned
3787 into face properties (bug#7876). */
3788 it->end_charpos = ZV;
3789
3790 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3791 something. This avoids an endless loop if they failed to
3792 fontify the text for which reason ever. */
3793 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3794 handled = HANDLED_RECOMPUTE_PROPS;
3795 }
3796
3797 return handled;
3798 }
3799
3800
3801 \f
3802 /***********************************************************************
3803 Faces
3804 ***********************************************************************/
3805
3806 /* Set up iterator IT from face properties at its current position.
3807 Called from handle_stop. */
3808
3809 static enum prop_handled
3810 handle_face_prop (struct it *it)
3811 {
3812 int new_face_id;
3813 ptrdiff_t next_stop;
3814
3815 if (!STRINGP (it->string))
3816 {
3817 new_face_id
3818 = face_at_buffer_position (it->w,
3819 IT_CHARPOS (*it),
3820 it->region_beg_charpos,
3821 it->region_end_charpos,
3822 &next_stop,
3823 (IT_CHARPOS (*it)
3824 + TEXT_PROP_DISTANCE_LIMIT),
3825 0, it->base_face_id);
3826
3827 /* Is this a start of a run of characters with box face?
3828 Caveat: this can be called for a freshly initialized
3829 iterator; face_id is -1 in this case. We know that the new
3830 face will not change until limit, i.e. if the new face has a
3831 box, all characters up to limit will have one. But, as
3832 usual, we don't know whether limit is really the end. */
3833 if (new_face_id != it->face_id)
3834 {
3835 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3836 /* If it->face_id is -1, old_face below will be NULL, see
3837 the definition of FACE_FROM_ID. This will happen if this
3838 is the initial call that gets the face. */
3839 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3840
3841 /* If the value of face_id of the iterator is -1, we have to
3842 look in front of IT's position and see whether there is a
3843 face there that's different from new_face_id. */
3844 if (!old_face && IT_CHARPOS (*it) > BEG)
3845 {
3846 int prev_face_id = face_before_it_pos (it);
3847
3848 old_face = FACE_FROM_ID (it->f, prev_face_id);
3849 }
3850
3851 /* If the new face has a box, but the old face does not,
3852 this is the start of a run of characters with box face,
3853 i.e. this character has a shadow on the left side. */
3854 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3855 && (old_face == NULL || !old_face->box));
3856 it->face_box_p = new_face->box != FACE_NO_BOX;
3857 }
3858 }
3859 else
3860 {
3861 int base_face_id;
3862 ptrdiff_t bufpos;
3863 int i;
3864 Lisp_Object from_overlay
3865 = (it->current.overlay_string_index >= 0
3866 ? it->string_overlays[it->current.overlay_string_index
3867 % OVERLAY_STRING_CHUNK_SIZE]
3868 : Qnil);
3869
3870 /* See if we got to this string directly or indirectly from
3871 an overlay property. That includes the before-string or
3872 after-string of an overlay, strings in display properties
3873 provided by an overlay, their text properties, etc.
3874
3875 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3876 if (! NILP (from_overlay))
3877 for (i = it->sp - 1; i >= 0; i--)
3878 {
3879 if (it->stack[i].current.overlay_string_index >= 0)
3880 from_overlay
3881 = it->string_overlays[it->stack[i].current.overlay_string_index
3882 % OVERLAY_STRING_CHUNK_SIZE];
3883 else if (! NILP (it->stack[i].from_overlay))
3884 from_overlay = it->stack[i].from_overlay;
3885
3886 if (!NILP (from_overlay))
3887 break;
3888 }
3889
3890 if (! NILP (from_overlay))
3891 {
3892 bufpos = IT_CHARPOS (*it);
3893 /* For a string from an overlay, the base face depends
3894 only on text properties and ignores overlays. */
3895 base_face_id
3896 = face_for_overlay_string (it->w,
3897 IT_CHARPOS (*it),
3898 it->region_beg_charpos,
3899 it->region_end_charpos,
3900 &next_stop,
3901 (IT_CHARPOS (*it)
3902 + TEXT_PROP_DISTANCE_LIMIT),
3903 0,
3904 from_overlay);
3905 }
3906 else
3907 {
3908 bufpos = 0;
3909
3910 /* For strings from a `display' property, use the face at
3911 IT's current buffer position as the base face to merge
3912 with, so that overlay strings appear in the same face as
3913 surrounding text, unless they specify their own
3914 faces. */
3915 base_face_id = it->string_from_prefix_prop_p
3916 ? DEFAULT_FACE_ID
3917 : underlying_face_id (it);
3918 }
3919
3920 new_face_id = face_at_string_position (it->w,
3921 it->string,
3922 IT_STRING_CHARPOS (*it),
3923 bufpos,
3924 it->region_beg_charpos,
3925 it->region_end_charpos,
3926 &next_stop,
3927 base_face_id, 0);
3928
3929 /* Is this a start of a run of characters with box? Caveat:
3930 this can be called for a freshly allocated iterator; face_id
3931 is -1 is this case. We know that the new face will not
3932 change until the next check pos, i.e. if the new face has a
3933 box, all characters up to that position will have a
3934 box. But, as usual, we don't know whether that position
3935 is really the end. */
3936 if (new_face_id != it->face_id)
3937 {
3938 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3939 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3940
3941 /* If new face has a box but old face hasn't, this is the
3942 start of a run of characters with box, i.e. it has a
3943 shadow on the left side. */
3944 it->start_of_box_run_p
3945 = new_face->box && (old_face == NULL || !old_face->box);
3946 it->face_box_p = new_face->box != FACE_NO_BOX;
3947 }
3948 }
3949
3950 it->face_id = new_face_id;
3951 return HANDLED_NORMALLY;
3952 }
3953
3954
3955 /* Return the ID of the face ``underlying'' IT's current position,
3956 which is in a string. If the iterator is associated with a
3957 buffer, return the face at IT's current buffer position.
3958 Otherwise, use the iterator's base_face_id. */
3959
3960 static int
3961 underlying_face_id (struct it *it)
3962 {
3963 int face_id = it->base_face_id, i;
3964
3965 eassert (STRINGP (it->string));
3966
3967 for (i = it->sp - 1; i >= 0; --i)
3968 if (NILP (it->stack[i].string))
3969 face_id = it->stack[i].face_id;
3970
3971 return face_id;
3972 }
3973
3974
3975 /* Compute the face one character before or after the current position
3976 of IT, in the visual order. BEFORE_P non-zero means get the face
3977 in front (to the left in L2R paragraphs, to the right in R2L
3978 paragraphs) of IT's screen position. Value is the ID of the face. */
3979
3980 static int
3981 face_before_or_after_it_pos (struct it *it, int before_p)
3982 {
3983 int face_id, limit;
3984 ptrdiff_t next_check_charpos;
3985 struct it it_copy;
3986 void *it_copy_data = NULL;
3987
3988 eassert (it->s == NULL);
3989
3990 if (STRINGP (it->string))
3991 {
3992 ptrdiff_t bufpos, charpos;
3993 int base_face_id;
3994
3995 /* No face change past the end of the string (for the case
3996 we are padding with spaces). No face change before the
3997 string start. */
3998 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3999 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4000 return it->face_id;
4001
4002 if (!it->bidi_p)
4003 {
4004 /* Set charpos to the position before or after IT's current
4005 position, in the logical order, which in the non-bidi
4006 case is the same as the visual order. */
4007 if (before_p)
4008 charpos = IT_STRING_CHARPOS (*it) - 1;
4009 else if (it->what == IT_COMPOSITION)
4010 /* For composition, we must check the character after the
4011 composition. */
4012 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4013 else
4014 charpos = IT_STRING_CHARPOS (*it) + 1;
4015 }
4016 else
4017 {
4018 if (before_p)
4019 {
4020 /* With bidi iteration, the character before the current
4021 in the visual order cannot be found by simple
4022 iteration, because "reverse" reordering is not
4023 supported. Instead, we need to use the move_it_*
4024 family of functions. */
4025 /* Ignore face changes before the first visible
4026 character on this display line. */
4027 if (it->current_x <= it->first_visible_x)
4028 return it->face_id;
4029 SAVE_IT (it_copy, *it, it_copy_data);
4030 /* Implementation note: Since move_it_in_display_line
4031 works in the iterator geometry, and thinks the first
4032 character is always the leftmost, even in R2L lines,
4033 we don't need to distinguish between the R2L and L2R
4034 cases here. */
4035 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4036 it_copy.current_x - 1, MOVE_TO_X);
4037 charpos = IT_STRING_CHARPOS (it_copy);
4038 RESTORE_IT (it, it, it_copy_data);
4039 }
4040 else
4041 {
4042 /* Set charpos to the string position of the character
4043 that comes after IT's current position in the visual
4044 order. */
4045 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4046
4047 it_copy = *it;
4048 while (n--)
4049 bidi_move_to_visually_next (&it_copy.bidi_it);
4050
4051 charpos = it_copy.bidi_it.charpos;
4052 }
4053 }
4054 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4055
4056 if (it->current.overlay_string_index >= 0)
4057 bufpos = IT_CHARPOS (*it);
4058 else
4059 bufpos = 0;
4060
4061 base_face_id = underlying_face_id (it);
4062
4063 /* Get the face for ASCII, or unibyte. */
4064 face_id = face_at_string_position (it->w,
4065 it->string,
4066 charpos,
4067 bufpos,
4068 it->region_beg_charpos,
4069 it->region_end_charpos,
4070 &next_check_charpos,
4071 base_face_id, 0);
4072
4073 /* Correct the face for charsets different from ASCII. Do it
4074 for the multibyte case only. The face returned above is
4075 suitable for unibyte text if IT->string is unibyte. */
4076 if (STRING_MULTIBYTE (it->string))
4077 {
4078 struct text_pos pos1 = string_pos (charpos, it->string);
4079 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4080 int c, len;
4081 struct face *face = FACE_FROM_ID (it->f, face_id);
4082
4083 c = string_char_and_length (p, &len);
4084 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4085 }
4086 }
4087 else
4088 {
4089 struct text_pos pos;
4090
4091 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4092 || (IT_CHARPOS (*it) <= BEGV && before_p))
4093 return it->face_id;
4094
4095 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4096 pos = it->current.pos;
4097
4098 if (!it->bidi_p)
4099 {
4100 if (before_p)
4101 DEC_TEXT_POS (pos, it->multibyte_p);
4102 else
4103 {
4104 if (it->what == IT_COMPOSITION)
4105 {
4106 /* For composition, we must check the position after
4107 the composition. */
4108 pos.charpos += it->cmp_it.nchars;
4109 pos.bytepos += it->len;
4110 }
4111 else
4112 INC_TEXT_POS (pos, it->multibyte_p);
4113 }
4114 }
4115 else
4116 {
4117 if (before_p)
4118 {
4119 /* With bidi iteration, the character before the current
4120 in the visual order cannot be found by simple
4121 iteration, because "reverse" reordering is not
4122 supported. Instead, we need to use the move_it_*
4123 family of functions. */
4124 /* Ignore face changes before the first visible
4125 character on this display line. */
4126 if (it->current_x <= it->first_visible_x)
4127 return it->face_id;
4128 SAVE_IT (it_copy, *it, it_copy_data);
4129 /* Implementation note: Since move_it_in_display_line
4130 works in the iterator geometry, and thinks the first
4131 character is always the leftmost, even in R2L lines,
4132 we don't need to distinguish between the R2L and L2R
4133 cases here. */
4134 move_it_in_display_line (&it_copy, ZV,
4135 it_copy.current_x - 1, MOVE_TO_X);
4136 pos = it_copy.current.pos;
4137 RESTORE_IT (it, it, it_copy_data);
4138 }
4139 else
4140 {
4141 /* Set charpos to the buffer position of the character
4142 that comes after IT's current position in the visual
4143 order. */
4144 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4145
4146 it_copy = *it;
4147 while (n--)
4148 bidi_move_to_visually_next (&it_copy.bidi_it);
4149
4150 SET_TEXT_POS (pos,
4151 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4152 }
4153 }
4154 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4155
4156 /* Determine face for CHARSET_ASCII, or unibyte. */
4157 face_id = face_at_buffer_position (it->w,
4158 CHARPOS (pos),
4159 it->region_beg_charpos,
4160 it->region_end_charpos,
4161 &next_check_charpos,
4162 limit, 0, -1);
4163
4164 /* Correct the face for charsets different from ASCII. Do it
4165 for the multibyte case only. The face returned above is
4166 suitable for unibyte text if current_buffer is unibyte. */
4167 if (it->multibyte_p)
4168 {
4169 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4170 struct face *face = FACE_FROM_ID (it->f, face_id);
4171 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4172 }
4173 }
4174
4175 return face_id;
4176 }
4177
4178
4179 \f
4180 /***********************************************************************
4181 Invisible text
4182 ***********************************************************************/
4183
4184 /* Set up iterator IT from invisible properties at its current
4185 position. Called from handle_stop. */
4186
4187 static enum prop_handled
4188 handle_invisible_prop (struct it *it)
4189 {
4190 enum prop_handled handled = HANDLED_NORMALLY;
4191 int invis_p;
4192 Lisp_Object prop;
4193
4194 if (STRINGP (it->string))
4195 {
4196 Lisp_Object end_charpos, limit, charpos;
4197
4198 /* Get the value of the invisible text property at the
4199 current position. Value will be nil if there is no such
4200 property. */
4201 charpos = make_number (IT_STRING_CHARPOS (*it));
4202 prop = Fget_text_property (charpos, Qinvisible, it->string);
4203 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4204
4205 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4206 {
4207 /* Record whether we have to display an ellipsis for the
4208 invisible text. */
4209 int display_ellipsis_p = (invis_p == 2);
4210 ptrdiff_t len, endpos;
4211
4212 handled = HANDLED_RECOMPUTE_PROPS;
4213
4214 /* Get the position at which the next visible text can be
4215 found in IT->string, if any. */
4216 endpos = len = SCHARS (it->string);
4217 XSETINT (limit, len);
4218 do
4219 {
4220 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4221 it->string, limit);
4222 if (INTEGERP (end_charpos))
4223 {
4224 endpos = XFASTINT (end_charpos);
4225 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4226 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4227 if (invis_p == 2)
4228 display_ellipsis_p = 1;
4229 }
4230 }
4231 while (invis_p && endpos < len);
4232
4233 if (display_ellipsis_p)
4234 it->ellipsis_p = 1;
4235
4236 if (endpos < len)
4237 {
4238 /* Text at END_CHARPOS is visible. Move IT there. */
4239 struct text_pos old;
4240 ptrdiff_t oldpos;
4241
4242 old = it->current.string_pos;
4243 oldpos = CHARPOS (old);
4244 if (it->bidi_p)
4245 {
4246 if (it->bidi_it.first_elt
4247 && it->bidi_it.charpos < SCHARS (it->string))
4248 bidi_paragraph_init (it->paragraph_embedding,
4249 &it->bidi_it, 1);
4250 /* Bidi-iterate out of the invisible text. */
4251 do
4252 {
4253 bidi_move_to_visually_next (&it->bidi_it);
4254 }
4255 while (oldpos <= it->bidi_it.charpos
4256 && it->bidi_it.charpos < endpos);
4257
4258 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4259 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4260 if (IT_CHARPOS (*it) >= endpos)
4261 it->prev_stop = endpos;
4262 }
4263 else
4264 {
4265 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4266 compute_string_pos (&it->current.string_pos, old, it->string);
4267 }
4268 }
4269 else
4270 {
4271 /* The rest of the string is invisible. If this is an
4272 overlay string, proceed with the next overlay string
4273 or whatever comes and return a character from there. */
4274 if (it->current.overlay_string_index >= 0
4275 && !display_ellipsis_p)
4276 {
4277 next_overlay_string (it);
4278 /* Don't check for overlay strings when we just
4279 finished processing them. */
4280 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4281 }
4282 else
4283 {
4284 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4285 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4286 }
4287 }
4288 }
4289 }
4290 else
4291 {
4292 ptrdiff_t newpos, next_stop, start_charpos, tem;
4293 Lisp_Object pos, overlay;
4294
4295 /* First of all, is there invisible text at this position? */
4296 tem = start_charpos = IT_CHARPOS (*it);
4297 pos = make_number (tem);
4298 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4299 &overlay);
4300 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4301
4302 /* If we are on invisible text, skip over it. */
4303 if (invis_p && start_charpos < it->end_charpos)
4304 {
4305 /* Record whether we have to display an ellipsis for the
4306 invisible text. */
4307 int display_ellipsis_p = invis_p == 2;
4308
4309 handled = HANDLED_RECOMPUTE_PROPS;
4310
4311 /* Loop skipping over invisible text. The loop is left at
4312 ZV or with IT on the first char being visible again. */
4313 do
4314 {
4315 /* Try to skip some invisible text. Return value is the
4316 position reached which can be equal to where we start
4317 if there is nothing invisible there. This skips both
4318 over invisible text properties and overlays with
4319 invisible property. */
4320 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4321
4322 /* If we skipped nothing at all we weren't at invisible
4323 text in the first place. If everything to the end of
4324 the buffer was skipped, end the loop. */
4325 if (newpos == tem || newpos >= ZV)
4326 invis_p = 0;
4327 else
4328 {
4329 /* We skipped some characters but not necessarily
4330 all there are. Check if we ended up on visible
4331 text. Fget_char_property returns the property of
4332 the char before the given position, i.e. if we
4333 get invis_p = 0, this means that the char at
4334 newpos is visible. */
4335 pos = make_number (newpos);
4336 prop = Fget_char_property (pos, Qinvisible, it->window);
4337 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4338 }
4339
4340 /* If we ended up on invisible text, proceed to
4341 skip starting with next_stop. */
4342 if (invis_p)
4343 tem = next_stop;
4344
4345 /* If there are adjacent invisible texts, don't lose the
4346 second one's ellipsis. */
4347 if (invis_p == 2)
4348 display_ellipsis_p = 1;
4349 }
4350 while (invis_p);
4351
4352 /* The position newpos is now either ZV or on visible text. */
4353 if (it->bidi_p)
4354 {
4355 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4356 int on_newline =
4357 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4358 int after_newline =
4359 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4360
4361 /* If the invisible text ends on a newline or on a
4362 character after a newline, we can avoid the costly,
4363 character by character, bidi iteration to NEWPOS, and
4364 instead simply reseat the iterator there. That's
4365 because all bidi reordering information is tossed at
4366 the newline. This is a big win for modes that hide
4367 complete lines, like Outline, Org, etc. */
4368 if (on_newline || after_newline)
4369 {
4370 struct text_pos tpos;
4371 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4372
4373 SET_TEXT_POS (tpos, newpos, bpos);
4374 reseat_1 (it, tpos, 0);
4375 /* If we reseat on a newline/ZV, we need to prep the
4376 bidi iterator for advancing to the next character
4377 after the newline/EOB, keeping the current paragraph
4378 direction (so that PRODUCE_GLYPHS does TRT wrt
4379 prepending/appending glyphs to a glyph row). */
4380 if (on_newline)
4381 {
4382 it->bidi_it.first_elt = 0;
4383 it->bidi_it.paragraph_dir = pdir;
4384 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4385 it->bidi_it.nchars = 1;
4386 it->bidi_it.ch_len = 1;
4387 }
4388 }
4389 else /* Must use the slow method. */
4390 {
4391 /* With bidi iteration, the region of invisible text
4392 could start and/or end in the middle of a
4393 non-base embedding level. Therefore, we need to
4394 skip invisible text using the bidi iterator,
4395 starting at IT's current position, until we find
4396 ourselves outside of the invisible text.
4397 Skipping invisible text _after_ bidi iteration
4398 avoids affecting the visual order of the
4399 displayed text when invisible properties are
4400 added or removed. */
4401 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4402 {
4403 /* If we were `reseat'ed to a new paragraph,
4404 determine the paragraph base direction. We
4405 need to do it now because
4406 next_element_from_buffer may not have a
4407 chance to do it, if we are going to skip any
4408 text at the beginning, which resets the
4409 FIRST_ELT flag. */
4410 bidi_paragraph_init (it->paragraph_embedding,
4411 &it->bidi_it, 1);
4412 }
4413 do
4414 {
4415 bidi_move_to_visually_next (&it->bidi_it);
4416 }
4417 while (it->stop_charpos <= it->bidi_it.charpos
4418 && it->bidi_it.charpos < newpos);
4419 IT_CHARPOS (*it) = it->bidi_it.charpos;
4420 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4421 /* If we overstepped NEWPOS, record its position in
4422 the iterator, so that we skip invisible text if
4423 later the bidi iteration lands us in the
4424 invisible region again. */
4425 if (IT_CHARPOS (*it) >= newpos)
4426 it->prev_stop = newpos;
4427 }
4428 }
4429 else
4430 {
4431 IT_CHARPOS (*it) = newpos;
4432 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4433 }
4434
4435 /* If there are before-strings at the start of invisible
4436 text, and the text is invisible because of a text
4437 property, arrange to show before-strings because 20.x did
4438 it that way. (If the text is invisible because of an
4439 overlay property instead of a text property, this is
4440 already handled in the overlay code.) */
4441 if (NILP (overlay)
4442 && get_overlay_strings (it, it->stop_charpos))
4443 {
4444 handled = HANDLED_RECOMPUTE_PROPS;
4445 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4446 }
4447 else if (display_ellipsis_p)
4448 {
4449 /* Make sure that the glyphs of the ellipsis will get
4450 correct `charpos' values. If we would not update
4451 it->position here, the glyphs would belong to the
4452 last visible character _before_ the invisible
4453 text, which confuses `set_cursor_from_row'.
4454
4455 We use the last invisible position instead of the
4456 first because this way the cursor is always drawn on
4457 the first "." of the ellipsis, whenever PT is inside
4458 the invisible text. Otherwise the cursor would be
4459 placed _after_ the ellipsis when the point is after the
4460 first invisible character. */
4461 if (!STRINGP (it->object))
4462 {
4463 it->position.charpos = newpos - 1;
4464 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4465 }
4466 it->ellipsis_p = 1;
4467 /* Let the ellipsis display before
4468 considering any properties of the following char.
4469 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4470 handled = HANDLED_RETURN;
4471 }
4472 }
4473 }
4474
4475 return handled;
4476 }
4477
4478
4479 /* Make iterator IT return `...' next.
4480 Replaces LEN characters from buffer. */
4481
4482 static void
4483 setup_for_ellipsis (struct it *it, int len)
4484 {
4485 /* Use the display table definition for `...'. Invalid glyphs
4486 will be handled by the method returning elements from dpvec. */
4487 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4488 {
4489 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4490 it->dpvec = v->contents;
4491 it->dpend = v->contents + v->header.size;
4492 }
4493 else
4494 {
4495 /* Default `...'. */
4496 it->dpvec = default_invis_vector;
4497 it->dpend = default_invis_vector + 3;
4498 }
4499
4500 it->dpvec_char_len = len;
4501 it->current.dpvec_index = 0;
4502 it->dpvec_face_id = -1;
4503
4504 /* Remember the current face id in case glyphs specify faces.
4505 IT's face is restored in set_iterator_to_next.
4506 saved_face_id was set to preceding char's face in handle_stop. */
4507 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4508 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4509
4510 it->method = GET_FROM_DISPLAY_VECTOR;
4511 it->ellipsis_p = 1;
4512 }
4513
4514
4515 \f
4516 /***********************************************************************
4517 'display' property
4518 ***********************************************************************/
4519
4520 /* Set up iterator IT from `display' property at its current position.
4521 Called from handle_stop.
4522 We return HANDLED_RETURN if some part of the display property
4523 overrides the display of the buffer text itself.
4524 Otherwise we return HANDLED_NORMALLY. */
4525
4526 static enum prop_handled
4527 handle_display_prop (struct it *it)
4528 {
4529 Lisp_Object propval, object, overlay;
4530 struct text_pos *position;
4531 ptrdiff_t bufpos;
4532 /* Nonzero if some property replaces the display of the text itself. */
4533 int display_replaced_p = 0;
4534
4535 if (STRINGP (it->string))
4536 {
4537 object = it->string;
4538 position = &it->current.string_pos;
4539 bufpos = CHARPOS (it->current.pos);
4540 }
4541 else
4542 {
4543 XSETWINDOW (object, it->w);
4544 position = &it->current.pos;
4545 bufpos = CHARPOS (*position);
4546 }
4547
4548 /* Reset those iterator values set from display property values. */
4549 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4550 it->space_width = Qnil;
4551 it->font_height = Qnil;
4552 it->voffset = 0;
4553
4554 /* We don't support recursive `display' properties, i.e. string
4555 values that have a string `display' property, that have a string
4556 `display' property etc. */
4557 if (!it->string_from_display_prop_p)
4558 it->area = TEXT_AREA;
4559
4560 propval = get_char_property_and_overlay (make_number (position->charpos),
4561 Qdisplay, object, &overlay);
4562 if (NILP (propval))
4563 return HANDLED_NORMALLY;
4564 /* Now OVERLAY is the overlay that gave us this property, or nil
4565 if it was a text property. */
4566
4567 if (!STRINGP (it->string))
4568 object = it->w->contents;
4569
4570 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4571 position, bufpos,
4572 FRAME_WINDOW_P (it->f));
4573
4574 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4575 }
4576
4577 /* Subroutine of handle_display_prop. Returns non-zero if the display
4578 specification in SPEC is a replacing specification, i.e. it would
4579 replace the text covered by `display' property with something else,
4580 such as an image or a display string. If SPEC includes any kind or
4581 `(space ...) specification, the value is 2; this is used by
4582 compute_display_string_pos, which see.
4583
4584 See handle_single_display_spec for documentation of arguments.
4585 frame_window_p is non-zero if the window being redisplayed is on a
4586 GUI frame; this argument is used only if IT is NULL, see below.
4587
4588 IT can be NULL, if this is called by the bidi reordering code
4589 through compute_display_string_pos, which see. In that case, this
4590 function only examines SPEC, but does not otherwise "handle" it, in
4591 the sense that it doesn't set up members of IT from the display
4592 spec. */
4593 static int
4594 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4595 Lisp_Object overlay, struct text_pos *position,
4596 ptrdiff_t bufpos, int frame_window_p)
4597 {
4598 int replacing_p = 0;
4599 int rv;
4600
4601 if (CONSP (spec)
4602 /* Simple specifications. */
4603 && !EQ (XCAR (spec), Qimage)
4604 && !EQ (XCAR (spec), Qspace)
4605 && !EQ (XCAR (spec), Qwhen)
4606 && !EQ (XCAR (spec), Qslice)
4607 && !EQ (XCAR (spec), Qspace_width)
4608 && !EQ (XCAR (spec), Qheight)
4609 && !EQ (XCAR (spec), Qraise)
4610 /* Marginal area specifications. */
4611 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4612 && !EQ (XCAR (spec), Qleft_fringe)
4613 && !EQ (XCAR (spec), Qright_fringe)
4614 && !NILP (XCAR (spec)))
4615 {
4616 for (; CONSP (spec); spec = XCDR (spec))
4617 {
4618 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4619 overlay, position, bufpos,
4620 replacing_p, frame_window_p)))
4621 {
4622 replacing_p = rv;
4623 /* If some text in a string is replaced, `position' no
4624 longer points to the position of `object'. */
4625 if (!it || STRINGP (object))
4626 break;
4627 }
4628 }
4629 }
4630 else if (VECTORP (spec))
4631 {
4632 ptrdiff_t i;
4633 for (i = 0; i < ASIZE (spec); ++i)
4634 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4635 overlay, position, bufpos,
4636 replacing_p, frame_window_p)))
4637 {
4638 replacing_p = rv;
4639 /* If some text in a string is replaced, `position' no
4640 longer points to the position of `object'. */
4641 if (!it || STRINGP (object))
4642 break;
4643 }
4644 }
4645 else
4646 {
4647 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4648 position, bufpos, 0,
4649 frame_window_p)))
4650 replacing_p = rv;
4651 }
4652
4653 return replacing_p;
4654 }
4655
4656 /* Value is the position of the end of the `display' property starting
4657 at START_POS in OBJECT. */
4658
4659 static struct text_pos
4660 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4661 {
4662 Lisp_Object end;
4663 struct text_pos end_pos;
4664
4665 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4666 Qdisplay, object, Qnil);
4667 CHARPOS (end_pos) = XFASTINT (end);
4668 if (STRINGP (object))
4669 compute_string_pos (&end_pos, start_pos, it->string);
4670 else
4671 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4672
4673 return end_pos;
4674 }
4675
4676
4677 /* Set up IT from a single `display' property specification SPEC. OBJECT
4678 is the object in which the `display' property was found. *POSITION
4679 is the position in OBJECT at which the `display' property was found.
4680 BUFPOS is the buffer position of OBJECT (different from POSITION if
4681 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4682 previously saw a display specification which already replaced text
4683 display with something else, for example an image; we ignore such
4684 properties after the first one has been processed.
4685
4686 OVERLAY is the overlay this `display' property came from,
4687 or nil if it was a text property.
4688
4689 If SPEC is a `space' or `image' specification, and in some other
4690 cases too, set *POSITION to the position where the `display'
4691 property ends.
4692
4693 If IT is NULL, only examine the property specification in SPEC, but
4694 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4695 is intended to be displayed in a window on a GUI frame.
4696
4697 Value is non-zero if something was found which replaces the display
4698 of buffer or string text. */
4699
4700 static int
4701 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4702 Lisp_Object overlay, struct text_pos *position,
4703 ptrdiff_t bufpos, int display_replaced_p,
4704 int frame_window_p)
4705 {
4706 Lisp_Object form;
4707 Lisp_Object location, value;
4708 struct text_pos start_pos = *position;
4709 int valid_p;
4710
4711 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4712 If the result is non-nil, use VALUE instead of SPEC. */
4713 form = Qt;
4714 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4715 {
4716 spec = XCDR (spec);
4717 if (!CONSP (spec))
4718 return 0;
4719 form = XCAR (spec);
4720 spec = XCDR (spec);
4721 }
4722
4723 if (!NILP (form) && !EQ (form, Qt))
4724 {
4725 ptrdiff_t count = SPECPDL_INDEX ();
4726 struct gcpro gcpro1;
4727
4728 /* Bind `object' to the object having the `display' property, a
4729 buffer or string. Bind `position' to the position in the
4730 object where the property was found, and `buffer-position'
4731 to the current position in the buffer. */
4732
4733 if (NILP (object))
4734 XSETBUFFER (object, current_buffer);
4735 specbind (Qobject, object);
4736 specbind (Qposition, make_number (CHARPOS (*position)));
4737 specbind (Qbuffer_position, make_number (bufpos));
4738 GCPRO1 (form);
4739 form = safe_eval (form);
4740 UNGCPRO;
4741 unbind_to (count, Qnil);
4742 }
4743
4744 if (NILP (form))
4745 return 0;
4746
4747 /* Handle `(height HEIGHT)' specifications. */
4748 if (CONSP (spec)
4749 && EQ (XCAR (spec), Qheight)
4750 && CONSP (XCDR (spec)))
4751 {
4752 if (it)
4753 {
4754 if (!FRAME_WINDOW_P (it->f))
4755 return 0;
4756
4757 it->font_height = XCAR (XCDR (spec));
4758 if (!NILP (it->font_height))
4759 {
4760 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4761 int new_height = -1;
4762
4763 if (CONSP (it->font_height)
4764 && (EQ (XCAR (it->font_height), Qplus)
4765 || EQ (XCAR (it->font_height), Qminus))
4766 && CONSP (XCDR (it->font_height))
4767 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4768 {
4769 /* `(+ N)' or `(- N)' where N is an integer. */
4770 int steps = XINT (XCAR (XCDR (it->font_height)));
4771 if (EQ (XCAR (it->font_height), Qplus))
4772 steps = - steps;
4773 it->face_id = smaller_face (it->f, it->face_id, steps);
4774 }
4775 else if (FUNCTIONP (it->font_height))
4776 {
4777 /* Call function with current height as argument.
4778 Value is the new height. */
4779 Lisp_Object height;
4780 height = safe_call1 (it->font_height,
4781 face->lface[LFACE_HEIGHT_INDEX]);
4782 if (NUMBERP (height))
4783 new_height = XFLOATINT (height);
4784 }
4785 else if (NUMBERP (it->font_height))
4786 {
4787 /* Value is a multiple of the canonical char height. */
4788 struct face *f;
4789
4790 f = FACE_FROM_ID (it->f,
4791 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4792 new_height = (XFLOATINT (it->font_height)
4793 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4794 }
4795 else
4796 {
4797 /* Evaluate IT->font_height with `height' bound to the
4798 current specified height to get the new height. */
4799 ptrdiff_t count = SPECPDL_INDEX ();
4800
4801 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4802 value = safe_eval (it->font_height);
4803 unbind_to (count, Qnil);
4804
4805 if (NUMBERP (value))
4806 new_height = XFLOATINT (value);
4807 }
4808
4809 if (new_height > 0)
4810 it->face_id = face_with_height (it->f, it->face_id, new_height);
4811 }
4812 }
4813
4814 return 0;
4815 }
4816
4817 /* Handle `(space-width WIDTH)'. */
4818 if (CONSP (spec)
4819 && EQ (XCAR (spec), Qspace_width)
4820 && CONSP (XCDR (spec)))
4821 {
4822 if (it)
4823 {
4824 if (!FRAME_WINDOW_P (it->f))
4825 return 0;
4826
4827 value = XCAR (XCDR (spec));
4828 if (NUMBERP (value) && XFLOATINT (value) > 0)
4829 it->space_width = value;
4830 }
4831
4832 return 0;
4833 }
4834
4835 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4836 if (CONSP (spec)
4837 && EQ (XCAR (spec), Qslice))
4838 {
4839 Lisp_Object tem;
4840
4841 if (it)
4842 {
4843 if (!FRAME_WINDOW_P (it->f))
4844 return 0;
4845
4846 if (tem = XCDR (spec), CONSP (tem))
4847 {
4848 it->slice.x = XCAR (tem);
4849 if (tem = XCDR (tem), CONSP (tem))
4850 {
4851 it->slice.y = XCAR (tem);
4852 if (tem = XCDR (tem), CONSP (tem))
4853 {
4854 it->slice.width = XCAR (tem);
4855 if (tem = XCDR (tem), CONSP (tem))
4856 it->slice.height = XCAR (tem);
4857 }
4858 }
4859 }
4860 }
4861
4862 return 0;
4863 }
4864
4865 /* Handle `(raise FACTOR)'. */
4866 if (CONSP (spec)
4867 && EQ (XCAR (spec), Qraise)
4868 && CONSP (XCDR (spec)))
4869 {
4870 if (it)
4871 {
4872 if (!FRAME_WINDOW_P (it->f))
4873 return 0;
4874
4875 #ifdef HAVE_WINDOW_SYSTEM
4876 value = XCAR (XCDR (spec));
4877 if (NUMBERP (value))
4878 {
4879 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4880 it->voffset = - (XFLOATINT (value)
4881 * (FONT_HEIGHT (face->font)));
4882 }
4883 #endif /* HAVE_WINDOW_SYSTEM */
4884 }
4885
4886 return 0;
4887 }
4888
4889 /* Don't handle the other kinds of display specifications
4890 inside a string that we got from a `display' property. */
4891 if (it && it->string_from_display_prop_p)
4892 return 0;
4893
4894 /* Characters having this form of property are not displayed, so
4895 we have to find the end of the property. */
4896 if (it)
4897 {
4898 start_pos = *position;
4899 *position = display_prop_end (it, object, start_pos);
4900 }
4901 value = Qnil;
4902
4903 /* Stop the scan at that end position--we assume that all
4904 text properties change there. */
4905 if (it)
4906 it->stop_charpos = position->charpos;
4907
4908 /* Handle `(left-fringe BITMAP [FACE])'
4909 and `(right-fringe BITMAP [FACE])'. */
4910 if (CONSP (spec)
4911 && (EQ (XCAR (spec), Qleft_fringe)
4912 || EQ (XCAR (spec), Qright_fringe))
4913 && CONSP (XCDR (spec)))
4914 {
4915 int fringe_bitmap;
4916
4917 if (it)
4918 {
4919 if (!FRAME_WINDOW_P (it->f))
4920 /* If we return here, POSITION has been advanced
4921 across the text with this property. */
4922 {
4923 /* Synchronize the bidi iterator with POSITION. This is
4924 needed because we are not going to push the iterator
4925 on behalf of this display property, so there will be
4926 no pop_it call to do this synchronization for us. */
4927 if (it->bidi_p)
4928 {
4929 it->position = *position;
4930 iterate_out_of_display_property (it);
4931 *position = it->position;
4932 }
4933 return 1;
4934 }
4935 }
4936 else if (!frame_window_p)
4937 return 1;
4938
4939 #ifdef HAVE_WINDOW_SYSTEM
4940 value = XCAR (XCDR (spec));
4941 if (!SYMBOLP (value)
4942 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4943 /* If we return here, POSITION has been advanced
4944 across the text with this property. */
4945 {
4946 if (it && it->bidi_p)
4947 {
4948 it->position = *position;
4949 iterate_out_of_display_property (it);
4950 *position = it->position;
4951 }
4952 return 1;
4953 }
4954
4955 if (it)
4956 {
4957 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4958
4959 if (CONSP (XCDR (XCDR (spec))))
4960 {
4961 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4962 int face_id2 = lookup_derived_face (it->f, face_name,
4963 FRINGE_FACE_ID, 0);
4964 if (face_id2 >= 0)
4965 face_id = face_id2;
4966 }
4967
4968 /* Save current settings of IT so that we can restore them
4969 when we are finished with the glyph property value. */
4970 push_it (it, position);
4971
4972 it->area = TEXT_AREA;
4973 it->what = IT_IMAGE;
4974 it->image_id = -1; /* no image */
4975 it->position = start_pos;
4976 it->object = NILP (object) ? it->w->contents : object;
4977 it->method = GET_FROM_IMAGE;
4978 it->from_overlay = Qnil;
4979 it->face_id = face_id;
4980 it->from_disp_prop_p = 1;
4981
4982 /* Say that we haven't consumed the characters with
4983 `display' property yet. The call to pop_it in
4984 set_iterator_to_next will clean this up. */
4985 *position = start_pos;
4986
4987 if (EQ (XCAR (spec), Qleft_fringe))
4988 {
4989 it->left_user_fringe_bitmap = fringe_bitmap;
4990 it->left_user_fringe_face_id = face_id;
4991 }
4992 else
4993 {
4994 it->right_user_fringe_bitmap = fringe_bitmap;
4995 it->right_user_fringe_face_id = face_id;
4996 }
4997 }
4998 #endif /* HAVE_WINDOW_SYSTEM */
4999 return 1;
5000 }
5001
5002 /* Prepare to handle `((margin left-margin) ...)',
5003 `((margin right-margin) ...)' and `((margin nil) ...)'
5004 prefixes for display specifications. */
5005 location = Qunbound;
5006 if (CONSP (spec) && CONSP (XCAR (spec)))
5007 {
5008 Lisp_Object tem;
5009
5010 value = XCDR (spec);
5011 if (CONSP (value))
5012 value = XCAR (value);
5013
5014 tem = XCAR (spec);
5015 if (EQ (XCAR (tem), Qmargin)
5016 && (tem = XCDR (tem),
5017 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5018 (NILP (tem)
5019 || EQ (tem, Qleft_margin)
5020 || EQ (tem, Qright_margin))))
5021 location = tem;
5022 }
5023
5024 if (EQ (location, Qunbound))
5025 {
5026 location = Qnil;
5027 value = spec;
5028 }
5029
5030 /* After this point, VALUE is the property after any
5031 margin prefix has been stripped. It must be a string,
5032 an image specification, or `(space ...)'.
5033
5034 LOCATION specifies where to display: `left-margin',
5035 `right-margin' or nil. */
5036
5037 valid_p = (STRINGP (value)
5038 #ifdef HAVE_WINDOW_SYSTEM
5039 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5040 && valid_image_p (value))
5041 #endif /* not HAVE_WINDOW_SYSTEM */
5042 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5043
5044 if (valid_p && !display_replaced_p)
5045 {
5046 int retval = 1;
5047
5048 if (!it)
5049 {
5050 /* Callers need to know whether the display spec is any kind
5051 of `(space ...)' spec that is about to affect text-area
5052 display. */
5053 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5054 retval = 2;
5055 return retval;
5056 }
5057
5058 /* Save current settings of IT so that we can restore them
5059 when we are finished with the glyph property value. */
5060 push_it (it, position);
5061 it->from_overlay = overlay;
5062 it->from_disp_prop_p = 1;
5063
5064 if (NILP (location))
5065 it->area = TEXT_AREA;
5066 else if (EQ (location, Qleft_margin))
5067 it->area = LEFT_MARGIN_AREA;
5068 else
5069 it->area = RIGHT_MARGIN_AREA;
5070
5071 if (STRINGP (value))
5072 {
5073 it->string = value;
5074 it->multibyte_p = STRING_MULTIBYTE (it->string);
5075 it->current.overlay_string_index = -1;
5076 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5077 it->end_charpos = it->string_nchars = SCHARS (it->string);
5078 it->method = GET_FROM_STRING;
5079 it->stop_charpos = 0;
5080 it->prev_stop = 0;
5081 it->base_level_stop = 0;
5082 it->string_from_display_prop_p = 1;
5083 /* Say that we haven't consumed the characters with
5084 `display' property yet. The call to pop_it in
5085 set_iterator_to_next will clean this up. */
5086 if (BUFFERP (object))
5087 *position = start_pos;
5088
5089 /* Force paragraph direction to be that of the parent
5090 object. If the parent object's paragraph direction is
5091 not yet determined, default to L2R. */
5092 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5093 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5094 else
5095 it->paragraph_embedding = L2R;
5096
5097 /* Set up the bidi iterator for this display string. */
5098 if (it->bidi_p)
5099 {
5100 it->bidi_it.string.lstring = it->string;
5101 it->bidi_it.string.s = NULL;
5102 it->bidi_it.string.schars = it->end_charpos;
5103 it->bidi_it.string.bufpos = bufpos;
5104 it->bidi_it.string.from_disp_str = 1;
5105 it->bidi_it.string.unibyte = !it->multibyte_p;
5106 it->bidi_it.w = it->w;
5107 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5108 }
5109 }
5110 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5111 {
5112 it->method = GET_FROM_STRETCH;
5113 it->object = value;
5114 *position = it->position = start_pos;
5115 retval = 1 + (it->area == TEXT_AREA);
5116 }
5117 #ifdef HAVE_WINDOW_SYSTEM
5118 else
5119 {
5120 it->what = IT_IMAGE;
5121 it->image_id = lookup_image (it->f, value);
5122 it->position = start_pos;
5123 it->object = NILP (object) ? it->w->contents : object;
5124 it->method = GET_FROM_IMAGE;
5125
5126 /* Say that we haven't consumed the characters with
5127 `display' property yet. The call to pop_it in
5128 set_iterator_to_next will clean this up. */
5129 *position = start_pos;
5130 }
5131 #endif /* HAVE_WINDOW_SYSTEM */
5132
5133 return retval;
5134 }
5135
5136 /* Invalid property or property not supported. Restore
5137 POSITION to what it was before. */
5138 *position = start_pos;
5139 return 0;
5140 }
5141
5142 /* Check if PROP is a display property value whose text should be
5143 treated as intangible. OVERLAY is the overlay from which PROP
5144 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5145 specify the buffer position covered by PROP. */
5146
5147 int
5148 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5149 ptrdiff_t charpos, ptrdiff_t bytepos)
5150 {
5151 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5152 struct text_pos position;
5153
5154 SET_TEXT_POS (position, charpos, bytepos);
5155 return handle_display_spec (NULL, prop, Qnil, overlay,
5156 &position, charpos, frame_window_p);
5157 }
5158
5159
5160 /* Return 1 if PROP is a display sub-property value containing STRING.
5161
5162 Implementation note: this and the following function are really
5163 special cases of handle_display_spec and
5164 handle_single_display_spec, and should ideally use the same code.
5165 Until they do, these two pairs must be consistent and must be
5166 modified in sync. */
5167
5168 static int
5169 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5170 {
5171 if (EQ (string, prop))
5172 return 1;
5173
5174 /* Skip over `when FORM'. */
5175 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5176 {
5177 prop = XCDR (prop);
5178 if (!CONSP (prop))
5179 return 0;
5180 /* Actually, the condition following `when' should be eval'ed,
5181 like handle_single_display_spec does, and we should return
5182 zero if it evaluates to nil. However, this function is
5183 called only when the buffer was already displayed and some
5184 glyph in the glyph matrix was found to come from a display
5185 string. Therefore, the condition was already evaluated, and
5186 the result was non-nil, otherwise the display string wouldn't
5187 have been displayed and we would have never been called for
5188 this property. Thus, we can skip the evaluation and assume
5189 its result is non-nil. */
5190 prop = XCDR (prop);
5191 }
5192
5193 if (CONSP (prop))
5194 /* Skip over `margin LOCATION'. */
5195 if (EQ (XCAR (prop), Qmargin))
5196 {
5197 prop = XCDR (prop);
5198 if (!CONSP (prop))
5199 return 0;
5200
5201 prop = XCDR (prop);
5202 if (!CONSP (prop))
5203 return 0;
5204 }
5205
5206 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5207 }
5208
5209
5210 /* Return 1 if STRING appears in the `display' property PROP. */
5211
5212 static int
5213 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5214 {
5215 if (CONSP (prop)
5216 && !EQ (XCAR (prop), Qwhen)
5217 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5218 {
5219 /* A list of sub-properties. */
5220 while (CONSP (prop))
5221 {
5222 if (single_display_spec_string_p (XCAR (prop), string))
5223 return 1;
5224 prop = XCDR (prop);
5225 }
5226 }
5227 else if (VECTORP (prop))
5228 {
5229 /* A vector of sub-properties. */
5230 ptrdiff_t i;
5231 for (i = 0; i < ASIZE (prop); ++i)
5232 if (single_display_spec_string_p (AREF (prop, i), string))
5233 return 1;
5234 }
5235 else
5236 return single_display_spec_string_p (prop, string);
5237
5238 return 0;
5239 }
5240
5241 /* Look for STRING in overlays and text properties in the current
5242 buffer, between character positions FROM and TO (excluding TO).
5243 BACK_P non-zero means look back (in this case, TO is supposed to be
5244 less than FROM).
5245 Value is the first character position where STRING was found, or
5246 zero if it wasn't found before hitting TO.
5247
5248 This function may only use code that doesn't eval because it is
5249 called asynchronously from note_mouse_highlight. */
5250
5251 static ptrdiff_t
5252 string_buffer_position_lim (Lisp_Object string,
5253 ptrdiff_t from, ptrdiff_t to, int back_p)
5254 {
5255 Lisp_Object limit, prop, pos;
5256 int found = 0;
5257
5258 pos = make_number (max (from, BEGV));
5259
5260 if (!back_p) /* looking forward */
5261 {
5262 limit = make_number (min (to, ZV));
5263 while (!found && !EQ (pos, limit))
5264 {
5265 prop = Fget_char_property (pos, Qdisplay, Qnil);
5266 if (!NILP (prop) && display_prop_string_p (prop, string))
5267 found = 1;
5268 else
5269 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5270 limit);
5271 }
5272 }
5273 else /* looking back */
5274 {
5275 limit = make_number (max (to, BEGV));
5276 while (!found && !EQ (pos, limit))
5277 {
5278 prop = Fget_char_property (pos, Qdisplay, Qnil);
5279 if (!NILP (prop) && display_prop_string_p (prop, string))
5280 found = 1;
5281 else
5282 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5283 limit);
5284 }
5285 }
5286
5287 return found ? XINT (pos) : 0;
5288 }
5289
5290 /* Determine which buffer position in current buffer STRING comes from.
5291 AROUND_CHARPOS is an approximate position where it could come from.
5292 Value is the buffer position or 0 if it couldn't be determined.
5293
5294 This function is necessary because we don't record buffer positions
5295 in glyphs generated from strings (to keep struct glyph small).
5296 This function may only use code that doesn't eval because it is
5297 called asynchronously from note_mouse_highlight. */
5298
5299 static ptrdiff_t
5300 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5301 {
5302 const int MAX_DISTANCE = 1000;
5303 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5304 around_charpos + MAX_DISTANCE,
5305 0);
5306
5307 if (!found)
5308 found = string_buffer_position_lim (string, around_charpos,
5309 around_charpos - MAX_DISTANCE, 1);
5310 return found;
5311 }
5312
5313
5314 \f
5315 /***********************************************************************
5316 `composition' property
5317 ***********************************************************************/
5318
5319 /* Set up iterator IT from `composition' property at its current
5320 position. Called from handle_stop. */
5321
5322 static enum prop_handled
5323 handle_composition_prop (struct it *it)
5324 {
5325 Lisp_Object prop, string;
5326 ptrdiff_t pos, pos_byte, start, end;
5327
5328 if (STRINGP (it->string))
5329 {
5330 unsigned char *s;
5331
5332 pos = IT_STRING_CHARPOS (*it);
5333 pos_byte = IT_STRING_BYTEPOS (*it);
5334 string = it->string;
5335 s = SDATA (string) + pos_byte;
5336 it->c = STRING_CHAR (s);
5337 }
5338 else
5339 {
5340 pos = IT_CHARPOS (*it);
5341 pos_byte = IT_BYTEPOS (*it);
5342 string = Qnil;
5343 it->c = FETCH_CHAR (pos_byte);
5344 }
5345
5346 /* If there's a valid composition and point is not inside of the
5347 composition (in the case that the composition is from the current
5348 buffer), draw a glyph composed from the composition components. */
5349 if (find_composition (pos, -1, &start, &end, &prop, string)
5350 && COMPOSITION_VALID_P (start, end, prop)
5351 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5352 {
5353 if (start < pos)
5354 /* As we can't handle this situation (perhaps font-lock added
5355 a new composition), we just return here hoping that next
5356 redisplay will detect this composition much earlier. */
5357 return HANDLED_NORMALLY;
5358 if (start != pos)
5359 {
5360 if (STRINGP (it->string))
5361 pos_byte = string_char_to_byte (it->string, start);
5362 else
5363 pos_byte = CHAR_TO_BYTE (start);
5364 }
5365 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5366 prop, string);
5367
5368 if (it->cmp_it.id >= 0)
5369 {
5370 it->cmp_it.ch = -1;
5371 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5372 it->cmp_it.nglyphs = -1;
5373 }
5374 }
5375
5376 return HANDLED_NORMALLY;
5377 }
5378
5379
5380 \f
5381 /***********************************************************************
5382 Overlay strings
5383 ***********************************************************************/
5384
5385 /* The following structure is used to record overlay strings for
5386 later sorting in load_overlay_strings. */
5387
5388 struct overlay_entry
5389 {
5390 Lisp_Object overlay;
5391 Lisp_Object string;
5392 EMACS_INT priority;
5393 int after_string_p;
5394 };
5395
5396
5397 /* Set up iterator IT from overlay strings at its current position.
5398 Called from handle_stop. */
5399
5400 static enum prop_handled
5401 handle_overlay_change (struct it *it)
5402 {
5403 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5404 return HANDLED_RECOMPUTE_PROPS;
5405 else
5406 return HANDLED_NORMALLY;
5407 }
5408
5409
5410 /* Set up the next overlay string for delivery by IT, if there is an
5411 overlay string to deliver. Called by set_iterator_to_next when the
5412 end of the current overlay string is reached. If there are more
5413 overlay strings to display, IT->string and
5414 IT->current.overlay_string_index are set appropriately here.
5415 Otherwise IT->string is set to nil. */
5416
5417 static void
5418 next_overlay_string (struct it *it)
5419 {
5420 ++it->current.overlay_string_index;
5421 if (it->current.overlay_string_index == it->n_overlay_strings)
5422 {
5423 /* No more overlay strings. Restore IT's settings to what
5424 they were before overlay strings were processed, and
5425 continue to deliver from current_buffer. */
5426
5427 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5428 pop_it (it);
5429 eassert (it->sp > 0
5430 || (NILP (it->string)
5431 && it->method == GET_FROM_BUFFER
5432 && it->stop_charpos >= BEGV
5433 && it->stop_charpos <= it->end_charpos));
5434 it->current.overlay_string_index = -1;
5435 it->n_overlay_strings = 0;
5436 it->overlay_strings_charpos = -1;
5437 /* If there's an empty display string on the stack, pop the
5438 stack, to resync the bidi iterator with IT's position. Such
5439 empty strings are pushed onto the stack in
5440 get_overlay_strings_1. */
5441 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5442 pop_it (it);
5443
5444 /* If we're at the end of the buffer, record that we have
5445 processed the overlay strings there already, so that
5446 next_element_from_buffer doesn't try it again. */
5447 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5448 it->overlay_strings_at_end_processed_p = 1;
5449 }
5450 else
5451 {
5452 /* There are more overlay strings to process. If
5453 IT->current.overlay_string_index has advanced to a position
5454 where we must load IT->overlay_strings with more strings, do
5455 it. We must load at the IT->overlay_strings_charpos where
5456 IT->n_overlay_strings was originally computed; when invisible
5457 text is present, this might not be IT_CHARPOS (Bug#7016). */
5458 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5459
5460 if (it->current.overlay_string_index && i == 0)
5461 load_overlay_strings (it, it->overlay_strings_charpos);
5462
5463 /* Initialize IT to deliver display elements from the overlay
5464 string. */
5465 it->string = it->overlay_strings[i];
5466 it->multibyte_p = STRING_MULTIBYTE (it->string);
5467 SET_TEXT_POS (it->current.string_pos, 0, 0);
5468 it->method = GET_FROM_STRING;
5469 it->stop_charpos = 0;
5470 it->end_charpos = SCHARS (it->string);
5471 if (it->cmp_it.stop_pos >= 0)
5472 it->cmp_it.stop_pos = 0;
5473 it->prev_stop = 0;
5474 it->base_level_stop = 0;
5475
5476 /* Set up the bidi iterator for this overlay string. */
5477 if (it->bidi_p)
5478 {
5479 it->bidi_it.string.lstring = it->string;
5480 it->bidi_it.string.s = NULL;
5481 it->bidi_it.string.schars = SCHARS (it->string);
5482 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5483 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5484 it->bidi_it.string.unibyte = !it->multibyte_p;
5485 it->bidi_it.w = it->w;
5486 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5487 }
5488 }
5489
5490 CHECK_IT (it);
5491 }
5492
5493
5494 /* Compare two overlay_entry structures E1 and E2. Used as a
5495 comparison function for qsort in load_overlay_strings. Overlay
5496 strings for the same position are sorted so that
5497
5498 1. All after-strings come in front of before-strings, except
5499 when they come from the same overlay.
5500
5501 2. Within after-strings, strings are sorted so that overlay strings
5502 from overlays with higher priorities come first.
5503
5504 2. Within before-strings, strings are sorted so that overlay
5505 strings from overlays with higher priorities come last.
5506
5507 Value is analogous to strcmp. */
5508
5509
5510 static int
5511 compare_overlay_entries (const void *e1, const void *e2)
5512 {
5513 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5514 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5515 int result;
5516
5517 if (entry1->after_string_p != entry2->after_string_p)
5518 {
5519 /* Let after-strings appear in front of before-strings if
5520 they come from different overlays. */
5521 if (EQ (entry1->overlay, entry2->overlay))
5522 result = entry1->after_string_p ? 1 : -1;
5523 else
5524 result = entry1->after_string_p ? -1 : 1;
5525 }
5526 else if (entry1->priority != entry2->priority)
5527 {
5528 if (entry1->after_string_p)
5529 /* After-strings sorted in order of decreasing priority. */
5530 result = entry2->priority < entry1->priority ? -1 : 1;
5531 else
5532 /* Before-strings sorted in order of increasing priority. */
5533 result = entry1->priority < entry2->priority ? -1 : 1;
5534 }
5535 else
5536 result = 0;
5537
5538 return result;
5539 }
5540
5541
5542 /* Load the vector IT->overlay_strings with overlay strings from IT's
5543 current buffer position, or from CHARPOS if that is > 0. Set
5544 IT->n_overlays to the total number of overlay strings found.
5545
5546 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5547 a time. On entry into load_overlay_strings,
5548 IT->current.overlay_string_index gives the number of overlay
5549 strings that have already been loaded by previous calls to this
5550 function.
5551
5552 IT->add_overlay_start contains an additional overlay start
5553 position to consider for taking overlay strings from, if non-zero.
5554 This position comes into play when the overlay has an `invisible'
5555 property, and both before and after-strings. When we've skipped to
5556 the end of the overlay, because of its `invisible' property, we
5557 nevertheless want its before-string to appear.
5558 IT->add_overlay_start will contain the overlay start position
5559 in this case.
5560
5561 Overlay strings are sorted so that after-string strings come in
5562 front of before-string strings. Within before and after-strings,
5563 strings are sorted by overlay priority. See also function
5564 compare_overlay_entries. */
5565
5566 static void
5567 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5568 {
5569 Lisp_Object overlay, window, str, invisible;
5570 struct Lisp_Overlay *ov;
5571 ptrdiff_t start, end;
5572 ptrdiff_t size = 20;
5573 ptrdiff_t n = 0, i, j;
5574 int invis_p;
5575 struct overlay_entry *entries = alloca (size * sizeof *entries);
5576 USE_SAFE_ALLOCA;
5577
5578 if (charpos <= 0)
5579 charpos = IT_CHARPOS (*it);
5580
5581 /* Append the overlay string STRING of overlay OVERLAY to vector
5582 `entries' which has size `size' and currently contains `n'
5583 elements. AFTER_P non-zero means STRING is an after-string of
5584 OVERLAY. */
5585 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5586 do \
5587 { \
5588 Lisp_Object priority; \
5589 \
5590 if (n == size) \
5591 { \
5592 struct overlay_entry *old = entries; \
5593 SAFE_NALLOCA (entries, 2, size); \
5594 memcpy (entries, old, size * sizeof *entries); \
5595 size *= 2; \
5596 } \
5597 \
5598 entries[n].string = (STRING); \
5599 entries[n].overlay = (OVERLAY); \
5600 priority = Foverlay_get ((OVERLAY), Qpriority); \
5601 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5602 entries[n].after_string_p = (AFTER_P); \
5603 ++n; \
5604 } \
5605 while (0)
5606
5607 /* Process overlay before the overlay center. */
5608 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5609 {
5610 XSETMISC (overlay, ov);
5611 eassert (OVERLAYP (overlay));
5612 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5613 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5614
5615 if (end < charpos)
5616 break;
5617
5618 /* Skip this overlay if it doesn't start or end at IT's current
5619 position. */
5620 if (end != charpos && start != charpos)
5621 continue;
5622
5623 /* Skip this overlay if it doesn't apply to IT->w. */
5624 window = Foverlay_get (overlay, Qwindow);
5625 if (WINDOWP (window) && XWINDOW (window) != it->w)
5626 continue;
5627
5628 /* If the text ``under'' the overlay is invisible, both before-
5629 and after-strings from this overlay are visible; start and
5630 end position are indistinguishable. */
5631 invisible = Foverlay_get (overlay, Qinvisible);
5632 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5633
5634 /* If overlay has a non-empty before-string, record it. */
5635 if ((start == charpos || (end == charpos && invis_p))
5636 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5637 && SCHARS (str))
5638 RECORD_OVERLAY_STRING (overlay, str, 0);
5639
5640 /* If overlay has a non-empty after-string, record it. */
5641 if ((end == charpos || (start == charpos && invis_p))
5642 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5643 && SCHARS (str))
5644 RECORD_OVERLAY_STRING (overlay, str, 1);
5645 }
5646
5647 /* Process overlays after the overlay center. */
5648 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5649 {
5650 XSETMISC (overlay, ov);
5651 eassert (OVERLAYP (overlay));
5652 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5653 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5654
5655 if (start > charpos)
5656 break;
5657
5658 /* Skip this overlay if it doesn't start or end at IT's current
5659 position. */
5660 if (end != charpos && start != charpos)
5661 continue;
5662
5663 /* Skip this overlay if it doesn't apply to IT->w. */
5664 window = Foverlay_get (overlay, Qwindow);
5665 if (WINDOWP (window) && XWINDOW (window) != it->w)
5666 continue;
5667
5668 /* If the text ``under'' the overlay is invisible, it has a zero
5669 dimension, and both before- and after-strings apply. */
5670 invisible = Foverlay_get (overlay, Qinvisible);
5671 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5672
5673 /* If overlay has a non-empty before-string, record it. */
5674 if ((start == charpos || (end == charpos && invis_p))
5675 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5676 && SCHARS (str))
5677 RECORD_OVERLAY_STRING (overlay, str, 0);
5678
5679 /* If overlay has a non-empty after-string, record it. */
5680 if ((end == charpos || (start == charpos && invis_p))
5681 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5682 && SCHARS (str))
5683 RECORD_OVERLAY_STRING (overlay, str, 1);
5684 }
5685
5686 #undef RECORD_OVERLAY_STRING
5687
5688 /* Sort entries. */
5689 if (n > 1)
5690 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5691
5692 /* Record number of overlay strings, and where we computed it. */
5693 it->n_overlay_strings = n;
5694 it->overlay_strings_charpos = charpos;
5695
5696 /* IT->current.overlay_string_index is the number of overlay strings
5697 that have already been consumed by IT. Copy some of the
5698 remaining overlay strings to IT->overlay_strings. */
5699 i = 0;
5700 j = it->current.overlay_string_index;
5701 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5702 {
5703 it->overlay_strings[i] = entries[j].string;
5704 it->string_overlays[i++] = entries[j++].overlay;
5705 }
5706
5707 CHECK_IT (it);
5708 SAFE_FREE ();
5709 }
5710
5711
5712 /* Get the first chunk of overlay strings at IT's current buffer
5713 position, or at CHARPOS if that is > 0. Value is non-zero if at
5714 least one overlay string was found. */
5715
5716 static int
5717 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5718 {
5719 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5720 process. This fills IT->overlay_strings with strings, and sets
5721 IT->n_overlay_strings to the total number of strings to process.
5722 IT->pos.overlay_string_index has to be set temporarily to zero
5723 because load_overlay_strings needs this; it must be set to -1
5724 when no overlay strings are found because a zero value would
5725 indicate a position in the first overlay string. */
5726 it->current.overlay_string_index = 0;
5727 load_overlay_strings (it, charpos);
5728
5729 /* If we found overlay strings, set up IT to deliver display
5730 elements from the first one. Otherwise set up IT to deliver
5731 from current_buffer. */
5732 if (it->n_overlay_strings)
5733 {
5734 /* Make sure we know settings in current_buffer, so that we can
5735 restore meaningful values when we're done with the overlay
5736 strings. */
5737 if (compute_stop_p)
5738 compute_stop_pos (it);
5739 eassert (it->face_id >= 0);
5740
5741 /* Save IT's settings. They are restored after all overlay
5742 strings have been processed. */
5743 eassert (!compute_stop_p || it->sp == 0);
5744
5745 /* When called from handle_stop, there might be an empty display
5746 string loaded. In that case, don't bother saving it. But
5747 don't use this optimization with the bidi iterator, since we
5748 need the corresponding pop_it call to resync the bidi
5749 iterator's position with IT's position, after we are done
5750 with the overlay strings. (The corresponding call to pop_it
5751 in case of an empty display string is in
5752 next_overlay_string.) */
5753 if (!(!it->bidi_p
5754 && STRINGP (it->string) && !SCHARS (it->string)))
5755 push_it (it, NULL);
5756
5757 /* Set up IT to deliver display elements from the first overlay
5758 string. */
5759 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5760 it->string = it->overlay_strings[0];
5761 it->from_overlay = Qnil;
5762 it->stop_charpos = 0;
5763 eassert (STRINGP (it->string));
5764 it->end_charpos = SCHARS (it->string);
5765 it->prev_stop = 0;
5766 it->base_level_stop = 0;
5767 it->multibyte_p = STRING_MULTIBYTE (it->string);
5768 it->method = GET_FROM_STRING;
5769 it->from_disp_prop_p = 0;
5770
5771 /* Force paragraph direction to be that of the parent
5772 buffer. */
5773 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5774 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5775 else
5776 it->paragraph_embedding = L2R;
5777
5778 /* Set up the bidi iterator for this overlay string. */
5779 if (it->bidi_p)
5780 {
5781 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5782
5783 it->bidi_it.string.lstring = it->string;
5784 it->bidi_it.string.s = NULL;
5785 it->bidi_it.string.schars = SCHARS (it->string);
5786 it->bidi_it.string.bufpos = pos;
5787 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5788 it->bidi_it.string.unibyte = !it->multibyte_p;
5789 it->bidi_it.w = it->w;
5790 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5791 }
5792 return 1;
5793 }
5794
5795 it->current.overlay_string_index = -1;
5796 return 0;
5797 }
5798
5799 static int
5800 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5801 {
5802 it->string = Qnil;
5803 it->method = GET_FROM_BUFFER;
5804
5805 (void) get_overlay_strings_1 (it, charpos, 1);
5806
5807 CHECK_IT (it);
5808
5809 /* Value is non-zero if we found at least one overlay string. */
5810 return STRINGP (it->string);
5811 }
5812
5813
5814 \f
5815 /***********************************************************************
5816 Saving and restoring state
5817 ***********************************************************************/
5818
5819 /* Save current settings of IT on IT->stack. Called, for example,
5820 before setting up IT for an overlay string, to be able to restore
5821 IT's settings to what they were after the overlay string has been
5822 processed. If POSITION is non-NULL, it is the position to save on
5823 the stack instead of IT->position. */
5824
5825 static void
5826 push_it (struct it *it, struct text_pos *position)
5827 {
5828 struct iterator_stack_entry *p;
5829
5830 eassert (it->sp < IT_STACK_SIZE);
5831 p = it->stack + it->sp;
5832
5833 p->stop_charpos = it->stop_charpos;
5834 p->prev_stop = it->prev_stop;
5835 p->base_level_stop = it->base_level_stop;
5836 p->cmp_it = it->cmp_it;
5837 eassert (it->face_id >= 0);
5838 p->face_id = it->face_id;
5839 p->string = it->string;
5840 p->method = it->method;
5841 p->from_overlay = it->from_overlay;
5842 switch (p->method)
5843 {
5844 case GET_FROM_IMAGE:
5845 p->u.image.object = it->object;
5846 p->u.image.image_id = it->image_id;
5847 p->u.image.slice = it->slice;
5848 break;
5849 case GET_FROM_STRETCH:
5850 p->u.stretch.object = it->object;
5851 break;
5852 }
5853 p->position = position ? *position : it->position;
5854 p->current = it->current;
5855 p->end_charpos = it->end_charpos;
5856 p->string_nchars = it->string_nchars;
5857 p->area = it->area;
5858 p->multibyte_p = it->multibyte_p;
5859 p->avoid_cursor_p = it->avoid_cursor_p;
5860 p->space_width = it->space_width;
5861 p->font_height = it->font_height;
5862 p->voffset = it->voffset;
5863 p->string_from_display_prop_p = it->string_from_display_prop_p;
5864 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5865 p->display_ellipsis_p = 0;
5866 p->line_wrap = it->line_wrap;
5867 p->bidi_p = it->bidi_p;
5868 p->paragraph_embedding = it->paragraph_embedding;
5869 p->from_disp_prop_p = it->from_disp_prop_p;
5870 ++it->sp;
5871
5872 /* Save the state of the bidi iterator as well. */
5873 if (it->bidi_p)
5874 bidi_push_it (&it->bidi_it);
5875 }
5876
5877 static void
5878 iterate_out_of_display_property (struct it *it)
5879 {
5880 int buffer_p = !STRINGP (it->string);
5881 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5882 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5883
5884 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5885
5886 /* Maybe initialize paragraph direction. If we are at the beginning
5887 of a new paragraph, next_element_from_buffer may not have a
5888 chance to do that. */
5889 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5890 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5891 /* prev_stop can be zero, so check against BEGV as well. */
5892 while (it->bidi_it.charpos >= bob
5893 && it->prev_stop <= it->bidi_it.charpos
5894 && it->bidi_it.charpos < CHARPOS (it->position)
5895 && it->bidi_it.charpos < eob)
5896 bidi_move_to_visually_next (&it->bidi_it);
5897 /* Record the stop_pos we just crossed, for when we cross it
5898 back, maybe. */
5899 if (it->bidi_it.charpos > CHARPOS (it->position))
5900 it->prev_stop = CHARPOS (it->position);
5901 /* If we ended up not where pop_it put us, resync IT's
5902 positional members with the bidi iterator. */
5903 if (it->bidi_it.charpos != CHARPOS (it->position))
5904 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5905 if (buffer_p)
5906 it->current.pos = it->position;
5907 else
5908 it->current.string_pos = it->position;
5909 }
5910
5911 /* Restore IT's settings from IT->stack. Called, for example, when no
5912 more overlay strings must be processed, and we return to delivering
5913 display elements from a buffer, or when the end of a string from a
5914 `display' property is reached and we return to delivering display
5915 elements from an overlay string, or from a buffer. */
5916
5917 static void
5918 pop_it (struct it *it)
5919 {
5920 struct iterator_stack_entry *p;
5921 int from_display_prop = it->from_disp_prop_p;
5922
5923 eassert (it->sp > 0);
5924 --it->sp;
5925 p = it->stack + it->sp;
5926 it->stop_charpos = p->stop_charpos;
5927 it->prev_stop = p->prev_stop;
5928 it->base_level_stop = p->base_level_stop;
5929 it->cmp_it = p->cmp_it;
5930 it->face_id = p->face_id;
5931 it->current = p->current;
5932 it->position = p->position;
5933 it->string = p->string;
5934 it->from_overlay = p->from_overlay;
5935 if (NILP (it->string))
5936 SET_TEXT_POS (it->current.string_pos, -1, -1);
5937 it->method = p->method;
5938 switch (it->method)
5939 {
5940 case GET_FROM_IMAGE:
5941 it->image_id = p->u.image.image_id;
5942 it->object = p->u.image.object;
5943 it->slice = p->u.image.slice;
5944 break;
5945 case GET_FROM_STRETCH:
5946 it->object = p->u.stretch.object;
5947 break;
5948 case GET_FROM_BUFFER:
5949 it->object = it->w->contents;
5950 break;
5951 case GET_FROM_STRING:
5952 it->object = it->string;
5953 break;
5954 case GET_FROM_DISPLAY_VECTOR:
5955 if (it->s)
5956 it->method = GET_FROM_C_STRING;
5957 else if (STRINGP (it->string))
5958 it->method = GET_FROM_STRING;
5959 else
5960 {
5961 it->method = GET_FROM_BUFFER;
5962 it->object = it->w->contents;
5963 }
5964 }
5965 it->end_charpos = p->end_charpos;
5966 it->string_nchars = p->string_nchars;
5967 it->area = p->area;
5968 it->multibyte_p = p->multibyte_p;
5969 it->avoid_cursor_p = p->avoid_cursor_p;
5970 it->space_width = p->space_width;
5971 it->font_height = p->font_height;
5972 it->voffset = p->voffset;
5973 it->string_from_display_prop_p = p->string_from_display_prop_p;
5974 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5975 it->line_wrap = p->line_wrap;
5976 it->bidi_p = p->bidi_p;
5977 it->paragraph_embedding = p->paragraph_embedding;
5978 it->from_disp_prop_p = p->from_disp_prop_p;
5979 if (it->bidi_p)
5980 {
5981 bidi_pop_it (&it->bidi_it);
5982 /* Bidi-iterate until we get out of the portion of text, if any,
5983 covered by a `display' text property or by an overlay with
5984 `display' property. (We cannot just jump there, because the
5985 internal coherency of the bidi iterator state can not be
5986 preserved across such jumps.) We also must determine the
5987 paragraph base direction if the overlay we just processed is
5988 at the beginning of a new paragraph. */
5989 if (from_display_prop
5990 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5991 iterate_out_of_display_property (it);
5992
5993 eassert ((BUFFERP (it->object)
5994 && IT_CHARPOS (*it) == it->bidi_it.charpos
5995 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5996 || (STRINGP (it->object)
5997 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5998 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5999 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6000 }
6001 }
6002
6003
6004 \f
6005 /***********************************************************************
6006 Moving over lines
6007 ***********************************************************************/
6008
6009 /* Set IT's current position to the previous line start. */
6010
6011 static void
6012 back_to_previous_line_start (struct it *it)
6013 {
6014 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6015
6016 DEC_BOTH (cp, bp);
6017 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6018 }
6019
6020
6021 /* Move IT to the next line start.
6022
6023 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6024 we skipped over part of the text (as opposed to moving the iterator
6025 continuously over the text). Otherwise, don't change the value
6026 of *SKIPPED_P.
6027
6028 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6029 iterator on the newline, if it was found.
6030
6031 Newlines may come from buffer text, overlay strings, or strings
6032 displayed via the `display' property. That's the reason we can't
6033 simply use find_newline_no_quit.
6034
6035 Note that this function may not skip over invisible text that is so
6036 because of text properties and immediately follows a newline. If
6037 it would, function reseat_at_next_visible_line_start, when called
6038 from set_iterator_to_next, would effectively make invisible
6039 characters following a newline part of the wrong glyph row, which
6040 leads to wrong cursor motion. */
6041
6042 static int
6043 forward_to_next_line_start (struct it *it, int *skipped_p,
6044 struct bidi_it *bidi_it_prev)
6045 {
6046 ptrdiff_t old_selective;
6047 int newline_found_p, n;
6048 const int MAX_NEWLINE_DISTANCE = 500;
6049
6050 /* If already on a newline, just consume it to avoid unintended
6051 skipping over invisible text below. */
6052 if (it->what == IT_CHARACTER
6053 && it->c == '\n'
6054 && CHARPOS (it->position) == IT_CHARPOS (*it))
6055 {
6056 if (it->bidi_p && bidi_it_prev)
6057 *bidi_it_prev = it->bidi_it;
6058 set_iterator_to_next (it, 0);
6059 it->c = 0;
6060 return 1;
6061 }
6062
6063 /* Don't handle selective display in the following. It's (a)
6064 unnecessary because it's done by the caller, and (b) leads to an
6065 infinite recursion because next_element_from_ellipsis indirectly
6066 calls this function. */
6067 old_selective = it->selective;
6068 it->selective = 0;
6069
6070 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6071 from buffer text. */
6072 for (n = newline_found_p = 0;
6073 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6074 n += STRINGP (it->string) ? 0 : 1)
6075 {
6076 if (!get_next_display_element (it))
6077 return 0;
6078 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6079 if (newline_found_p && it->bidi_p && bidi_it_prev)
6080 *bidi_it_prev = it->bidi_it;
6081 set_iterator_to_next (it, 0);
6082 }
6083
6084 /* If we didn't find a newline near enough, see if we can use a
6085 short-cut. */
6086 if (!newline_found_p)
6087 {
6088 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6089 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6090 1, &bytepos);
6091 Lisp_Object pos;
6092
6093 eassert (!STRINGP (it->string));
6094
6095 /* If there isn't any `display' property in sight, and no
6096 overlays, we can just use the position of the newline in
6097 buffer text. */
6098 if (it->stop_charpos >= limit
6099 || ((pos = Fnext_single_property_change (make_number (start),
6100 Qdisplay, Qnil,
6101 make_number (limit)),
6102 NILP (pos))
6103 && next_overlay_change (start) == ZV))
6104 {
6105 if (!it->bidi_p)
6106 {
6107 IT_CHARPOS (*it) = limit;
6108 IT_BYTEPOS (*it) = bytepos;
6109 }
6110 else
6111 {
6112 struct bidi_it bprev;
6113
6114 /* Help bidi.c avoid expensive searches for display
6115 properties and overlays, by telling it that there are
6116 none up to `limit'. */
6117 if (it->bidi_it.disp_pos < limit)
6118 {
6119 it->bidi_it.disp_pos = limit;
6120 it->bidi_it.disp_prop = 0;
6121 }
6122 do {
6123 bprev = it->bidi_it;
6124 bidi_move_to_visually_next (&it->bidi_it);
6125 } while (it->bidi_it.charpos != limit);
6126 IT_CHARPOS (*it) = limit;
6127 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6128 if (bidi_it_prev)
6129 *bidi_it_prev = bprev;
6130 }
6131 *skipped_p = newline_found_p = 1;
6132 }
6133 else
6134 {
6135 while (get_next_display_element (it)
6136 && !newline_found_p)
6137 {
6138 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6139 if (newline_found_p && it->bidi_p && bidi_it_prev)
6140 *bidi_it_prev = it->bidi_it;
6141 set_iterator_to_next (it, 0);
6142 }
6143 }
6144 }
6145
6146 it->selective = old_selective;
6147 return newline_found_p;
6148 }
6149
6150
6151 /* Set IT's current position to the previous visible line start. Skip
6152 invisible text that is so either due to text properties or due to
6153 selective display. Caution: this does not change IT->current_x and
6154 IT->hpos. */
6155
6156 static void
6157 back_to_previous_visible_line_start (struct it *it)
6158 {
6159 while (IT_CHARPOS (*it) > BEGV)
6160 {
6161 back_to_previous_line_start (it);
6162
6163 if (IT_CHARPOS (*it) <= BEGV)
6164 break;
6165
6166 /* If selective > 0, then lines indented more than its value are
6167 invisible. */
6168 if (it->selective > 0
6169 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6170 it->selective))
6171 continue;
6172
6173 /* Check the newline before point for invisibility. */
6174 {
6175 Lisp_Object prop;
6176 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6177 Qinvisible, it->window);
6178 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6179 continue;
6180 }
6181
6182 if (IT_CHARPOS (*it) <= BEGV)
6183 break;
6184
6185 {
6186 struct it it2;
6187 void *it2data = NULL;
6188 ptrdiff_t pos;
6189 ptrdiff_t beg, end;
6190 Lisp_Object val, overlay;
6191
6192 SAVE_IT (it2, *it, it2data);
6193
6194 /* If newline is part of a composition, continue from start of composition */
6195 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6196 && beg < IT_CHARPOS (*it))
6197 goto replaced;
6198
6199 /* If newline is replaced by a display property, find start of overlay
6200 or interval and continue search from that point. */
6201 pos = --IT_CHARPOS (it2);
6202 --IT_BYTEPOS (it2);
6203 it2.sp = 0;
6204 bidi_unshelve_cache (NULL, 0);
6205 it2.string_from_display_prop_p = 0;
6206 it2.from_disp_prop_p = 0;
6207 if (handle_display_prop (&it2) == HANDLED_RETURN
6208 && !NILP (val = get_char_property_and_overlay
6209 (make_number (pos), Qdisplay, Qnil, &overlay))
6210 && (OVERLAYP (overlay)
6211 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6212 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6213 {
6214 RESTORE_IT (it, it, it2data);
6215 goto replaced;
6216 }
6217
6218 /* Newline is not replaced by anything -- so we are done. */
6219 RESTORE_IT (it, it, it2data);
6220 break;
6221
6222 replaced:
6223 if (beg < BEGV)
6224 beg = BEGV;
6225 IT_CHARPOS (*it) = beg;
6226 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6227 }
6228 }
6229
6230 it->continuation_lines_width = 0;
6231
6232 eassert (IT_CHARPOS (*it) >= BEGV);
6233 eassert (IT_CHARPOS (*it) == BEGV
6234 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6235 CHECK_IT (it);
6236 }
6237
6238
6239 /* Reseat iterator IT at the previous visible line start. Skip
6240 invisible text that is so either due to text properties or due to
6241 selective display. At the end, update IT's overlay information,
6242 face information etc. */
6243
6244 void
6245 reseat_at_previous_visible_line_start (struct it *it)
6246 {
6247 back_to_previous_visible_line_start (it);
6248 reseat (it, it->current.pos, 1);
6249 CHECK_IT (it);
6250 }
6251
6252
6253 /* Reseat iterator IT on the next visible line start in the current
6254 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6255 preceding the line start. Skip over invisible text that is so
6256 because of selective display. Compute faces, overlays etc at the
6257 new position. Note that this function does not skip over text that
6258 is invisible because of text properties. */
6259
6260 static void
6261 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6262 {
6263 int newline_found_p, skipped_p = 0;
6264 struct bidi_it bidi_it_prev;
6265
6266 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6267
6268 /* Skip over lines that are invisible because they are indented
6269 more than the value of IT->selective. */
6270 if (it->selective > 0)
6271 while (IT_CHARPOS (*it) < ZV
6272 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6273 it->selective))
6274 {
6275 eassert (IT_BYTEPOS (*it) == BEGV
6276 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6277 newline_found_p =
6278 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6279 }
6280
6281 /* Position on the newline if that's what's requested. */
6282 if (on_newline_p && newline_found_p)
6283 {
6284 if (STRINGP (it->string))
6285 {
6286 if (IT_STRING_CHARPOS (*it) > 0)
6287 {
6288 if (!it->bidi_p)
6289 {
6290 --IT_STRING_CHARPOS (*it);
6291 --IT_STRING_BYTEPOS (*it);
6292 }
6293 else
6294 {
6295 /* We need to restore the bidi iterator to the state
6296 it had on the newline, and resync the IT's
6297 position with that. */
6298 it->bidi_it = bidi_it_prev;
6299 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6300 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6301 }
6302 }
6303 }
6304 else if (IT_CHARPOS (*it) > BEGV)
6305 {
6306 if (!it->bidi_p)
6307 {
6308 --IT_CHARPOS (*it);
6309 --IT_BYTEPOS (*it);
6310 }
6311 else
6312 {
6313 /* We need to restore the bidi iterator to the state it
6314 had on the newline and resync IT with that. */
6315 it->bidi_it = bidi_it_prev;
6316 IT_CHARPOS (*it) = it->bidi_it.charpos;
6317 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6318 }
6319 reseat (it, it->current.pos, 0);
6320 }
6321 }
6322 else if (skipped_p)
6323 reseat (it, it->current.pos, 0);
6324
6325 CHECK_IT (it);
6326 }
6327
6328
6329 \f
6330 /***********************************************************************
6331 Changing an iterator's position
6332 ***********************************************************************/
6333
6334 /* Change IT's current position to POS in current_buffer. If FORCE_P
6335 is non-zero, always check for text properties at the new position.
6336 Otherwise, text properties are only looked up if POS >=
6337 IT->check_charpos of a property. */
6338
6339 static void
6340 reseat (struct it *it, struct text_pos pos, int force_p)
6341 {
6342 ptrdiff_t original_pos = IT_CHARPOS (*it);
6343
6344 reseat_1 (it, pos, 0);
6345
6346 /* Determine where to check text properties. Avoid doing it
6347 where possible because text property lookup is very expensive. */
6348 if (force_p
6349 || CHARPOS (pos) > it->stop_charpos
6350 || CHARPOS (pos) < original_pos)
6351 {
6352 if (it->bidi_p)
6353 {
6354 /* For bidi iteration, we need to prime prev_stop and
6355 base_level_stop with our best estimations. */
6356 /* Implementation note: Of course, POS is not necessarily a
6357 stop position, so assigning prev_pos to it is a lie; we
6358 should have called compute_stop_backwards. However, if
6359 the current buffer does not include any R2L characters,
6360 that call would be a waste of cycles, because the
6361 iterator will never move back, and thus never cross this
6362 "fake" stop position. So we delay that backward search
6363 until the time we really need it, in next_element_from_buffer. */
6364 if (CHARPOS (pos) != it->prev_stop)
6365 it->prev_stop = CHARPOS (pos);
6366 if (CHARPOS (pos) < it->base_level_stop)
6367 it->base_level_stop = 0; /* meaning it's unknown */
6368 handle_stop (it);
6369 }
6370 else
6371 {
6372 handle_stop (it);
6373 it->prev_stop = it->base_level_stop = 0;
6374 }
6375
6376 }
6377
6378 CHECK_IT (it);
6379 }
6380
6381
6382 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6383 IT->stop_pos to POS, also. */
6384
6385 static void
6386 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6387 {
6388 /* Don't call this function when scanning a C string. */
6389 eassert (it->s == NULL);
6390
6391 /* POS must be a reasonable value. */
6392 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6393
6394 it->current.pos = it->position = pos;
6395 it->end_charpos = ZV;
6396 it->dpvec = NULL;
6397 it->current.dpvec_index = -1;
6398 it->current.overlay_string_index = -1;
6399 IT_STRING_CHARPOS (*it) = -1;
6400 IT_STRING_BYTEPOS (*it) = -1;
6401 it->string = Qnil;
6402 it->method = GET_FROM_BUFFER;
6403 it->object = it->w->contents;
6404 it->area = TEXT_AREA;
6405 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6406 it->sp = 0;
6407 it->string_from_display_prop_p = 0;
6408 it->string_from_prefix_prop_p = 0;
6409
6410 it->from_disp_prop_p = 0;
6411 it->face_before_selective_p = 0;
6412 if (it->bidi_p)
6413 {
6414 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6415 &it->bidi_it);
6416 bidi_unshelve_cache (NULL, 0);
6417 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6418 it->bidi_it.string.s = NULL;
6419 it->bidi_it.string.lstring = Qnil;
6420 it->bidi_it.string.bufpos = 0;
6421 it->bidi_it.string.unibyte = 0;
6422 it->bidi_it.w = it->w;
6423 }
6424
6425 if (set_stop_p)
6426 {
6427 it->stop_charpos = CHARPOS (pos);
6428 it->base_level_stop = CHARPOS (pos);
6429 }
6430 /* This make the information stored in it->cmp_it invalidate. */
6431 it->cmp_it.id = -1;
6432 }
6433
6434
6435 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6436 If S is non-null, it is a C string to iterate over. Otherwise,
6437 STRING gives a Lisp string to iterate over.
6438
6439 If PRECISION > 0, don't return more then PRECISION number of
6440 characters from the string.
6441
6442 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6443 characters have been returned. FIELD_WIDTH < 0 means an infinite
6444 field width.
6445
6446 MULTIBYTE = 0 means disable processing of multibyte characters,
6447 MULTIBYTE > 0 means enable it,
6448 MULTIBYTE < 0 means use IT->multibyte_p.
6449
6450 IT must be initialized via a prior call to init_iterator before
6451 calling this function. */
6452
6453 static void
6454 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6455 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6456 int multibyte)
6457 {
6458 /* No region in strings. */
6459 it->region_beg_charpos = it->region_end_charpos = -1;
6460
6461 /* No text property checks performed by default, but see below. */
6462 it->stop_charpos = -1;
6463
6464 /* Set iterator position and end position. */
6465 memset (&it->current, 0, sizeof it->current);
6466 it->current.overlay_string_index = -1;
6467 it->current.dpvec_index = -1;
6468 eassert (charpos >= 0);
6469
6470 /* If STRING is specified, use its multibyteness, otherwise use the
6471 setting of MULTIBYTE, if specified. */
6472 if (multibyte >= 0)
6473 it->multibyte_p = multibyte > 0;
6474
6475 /* Bidirectional reordering of strings is controlled by the default
6476 value of bidi-display-reordering. Don't try to reorder while
6477 loading loadup.el, as the necessary character property tables are
6478 not yet available. */
6479 it->bidi_p =
6480 NILP (Vpurify_flag)
6481 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6482
6483 if (s == NULL)
6484 {
6485 eassert (STRINGP (string));
6486 it->string = string;
6487 it->s = NULL;
6488 it->end_charpos = it->string_nchars = SCHARS (string);
6489 it->method = GET_FROM_STRING;
6490 it->current.string_pos = string_pos (charpos, string);
6491
6492 if (it->bidi_p)
6493 {
6494 it->bidi_it.string.lstring = string;
6495 it->bidi_it.string.s = NULL;
6496 it->bidi_it.string.schars = it->end_charpos;
6497 it->bidi_it.string.bufpos = 0;
6498 it->bidi_it.string.from_disp_str = 0;
6499 it->bidi_it.string.unibyte = !it->multibyte_p;
6500 it->bidi_it.w = it->w;
6501 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6502 FRAME_WINDOW_P (it->f), &it->bidi_it);
6503 }
6504 }
6505 else
6506 {
6507 it->s = (const unsigned char *) s;
6508 it->string = Qnil;
6509
6510 /* Note that we use IT->current.pos, not it->current.string_pos,
6511 for displaying C strings. */
6512 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6513 if (it->multibyte_p)
6514 {
6515 it->current.pos = c_string_pos (charpos, s, 1);
6516 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6517 }
6518 else
6519 {
6520 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6521 it->end_charpos = it->string_nchars = strlen (s);
6522 }
6523
6524 if (it->bidi_p)
6525 {
6526 it->bidi_it.string.lstring = Qnil;
6527 it->bidi_it.string.s = (const unsigned char *) s;
6528 it->bidi_it.string.schars = it->end_charpos;
6529 it->bidi_it.string.bufpos = 0;
6530 it->bidi_it.string.from_disp_str = 0;
6531 it->bidi_it.string.unibyte = !it->multibyte_p;
6532 it->bidi_it.w = it->w;
6533 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6534 &it->bidi_it);
6535 }
6536 it->method = GET_FROM_C_STRING;
6537 }
6538
6539 /* PRECISION > 0 means don't return more than PRECISION characters
6540 from the string. */
6541 if (precision > 0 && it->end_charpos - charpos > precision)
6542 {
6543 it->end_charpos = it->string_nchars = charpos + precision;
6544 if (it->bidi_p)
6545 it->bidi_it.string.schars = it->end_charpos;
6546 }
6547
6548 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6549 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6550 FIELD_WIDTH < 0 means infinite field width. This is useful for
6551 padding with `-' at the end of a mode line. */
6552 if (field_width < 0)
6553 field_width = INFINITY;
6554 /* Implementation note: We deliberately don't enlarge
6555 it->bidi_it.string.schars here to fit it->end_charpos, because
6556 the bidi iterator cannot produce characters out of thin air. */
6557 if (field_width > it->end_charpos - charpos)
6558 it->end_charpos = charpos + field_width;
6559
6560 /* Use the standard display table for displaying strings. */
6561 if (DISP_TABLE_P (Vstandard_display_table))
6562 it->dp = XCHAR_TABLE (Vstandard_display_table);
6563
6564 it->stop_charpos = charpos;
6565 it->prev_stop = charpos;
6566 it->base_level_stop = 0;
6567 if (it->bidi_p)
6568 {
6569 it->bidi_it.first_elt = 1;
6570 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6571 it->bidi_it.disp_pos = -1;
6572 }
6573 if (s == NULL && it->multibyte_p)
6574 {
6575 ptrdiff_t endpos = SCHARS (it->string);
6576 if (endpos > it->end_charpos)
6577 endpos = it->end_charpos;
6578 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6579 it->string);
6580 }
6581 CHECK_IT (it);
6582 }
6583
6584
6585 \f
6586 /***********************************************************************
6587 Iteration
6588 ***********************************************************************/
6589
6590 /* Map enum it_method value to corresponding next_element_from_* function. */
6591
6592 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6593 {
6594 next_element_from_buffer,
6595 next_element_from_display_vector,
6596 next_element_from_string,
6597 next_element_from_c_string,
6598 next_element_from_image,
6599 next_element_from_stretch
6600 };
6601
6602 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6603
6604
6605 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6606 (possibly with the following characters). */
6607
6608 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6609 ((IT)->cmp_it.id >= 0 \
6610 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6611 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6612 END_CHARPOS, (IT)->w, \
6613 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6614 (IT)->string)))
6615
6616
6617 /* Lookup the char-table Vglyphless_char_display for character C (-1
6618 if we want information for no-font case), and return the display
6619 method symbol. By side-effect, update it->what and
6620 it->glyphless_method. This function is called from
6621 get_next_display_element for each character element, and from
6622 x_produce_glyphs when no suitable font was found. */
6623
6624 Lisp_Object
6625 lookup_glyphless_char_display (int c, struct it *it)
6626 {
6627 Lisp_Object glyphless_method = Qnil;
6628
6629 if (CHAR_TABLE_P (Vglyphless_char_display)
6630 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6631 {
6632 if (c >= 0)
6633 {
6634 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6635 if (CONSP (glyphless_method))
6636 glyphless_method = FRAME_WINDOW_P (it->f)
6637 ? XCAR (glyphless_method)
6638 : XCDR (glyphless_method);
6639 }
6640 else
6641 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6642 }
6643
6644 retry:
6645 if (NILP (glyphless_method))
6646 {
6647 if (c >= 0)
6648 /* The default is to display the character by a proper font. */
6649 return Qnil;
6650 /* The default for the no-font case is to display an empty box. */
6651 glyphless_method = Qempty_box;
6652 }
6653 if (EQ (glyphless_method, Qzero_width))
6654 {
6655 if (c >= 0)
6656 return glyphless_method;
6657 /* This method can't be used for the no-font case. */
6658 glyphless_method = Qempty_box;
6659 }
6660 if (EQ (glyphless_method, Qthin_space))
6661 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6662 else if (EQ (glyphless_method, Qempty_box))
6663 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6664 else if (EQ (glyphless_method, Qhex_code))
6665 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6666 else if (STRINGP (glyphless_method))
6667 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6668 else
6669 {
6670 /* Invalid value. We use the default method. */
6671 glyphless_method = Qnil;
6672 goto retry;
6673 }
6674 it->what = IT_GLYPHLESS;
6675 return glyphless_method;
6676 }
6677
6678 /* Load IT's display element fields with information about the next
6679 display element from the current position of IT. Value is zero if
6680 end of buffer (or C string) is reached. */
6681
6682 static struct frame *last_escape_glyph_frame = NULL;
6683 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6684 static int last_escape_glyph_merged_face_id = 0;
6685
6686 struct frame *last_glyphless_glyph_frame = NULL;
6687 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6688 int last_glyphless_glyph_merged_face_id = 0;
6689
6690 static int
6691 get_next_display_element (struct it *it)
6692 {
6693 /* Non-zero means that we found a display element. Zero means that
6694 we hit the end of what we iterate over. Performance note: the
6695 function pointer `method' used here turns out to be faster than
6696 using a sequence of if-statements. */
6697 int success_p;
6698
6699 get_next:
6700 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6701
6702 if (it->what == IT_CHARACTER)
6703 {
6704 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6705 and only if (a) the resolved directionality of that character
6706 is R..." */
6707 /* FIXME: Do we need an exception for characters from display
6708 tables? */
6709 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6710 it->c = bidi_mirror_char (it->c);
6711 /* Map via display table or translate control characters.
6712 IT->c, IT->len etc. have been set to the next character by
6713 the function call above. If we have a display table, and it
6714 contains an entry for IT->c, translate it. Don't do this if
6715 IT->c itself comes from a display table, otherwise we could
6716 end up in an infinite recursion. (An alternative could be to
6717 count the recursion depth of this function and signal an
6718 error when a certain maximum depth is reached.) Is it worth
6719 it? */
6720 if (success_p && it->dpvec == NULL)
6721 {
6722 Lisp_Object dv;
6723 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6724 int nonascii_space_p = 0;
6725 int nonascii_hyphen_p = 0;
6726 int c = it->c; /* This is the character to display. */
6727
6728 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6729 {
6730 eassert (SINGLE_BYTE_CHAR_P (c));
6731 if (unibyte_display_via_language_environment)
6732 {
6733 c = DECODE_CHAR (unibyte, c);
6734 if (c < 0)
6735 c = BYTE8_TO_CHAR (it->c);
6736 }
6737 else
6738 c = BYTE8_TO_CHAR (it->c);
6739 }
6740
6741 if (it->dp
6742 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6743 VECTORP (dv)))
6744 {
6745 struct Lisp_Vector *v = XVECTOR (dv);
6746
6747 /* Return the first character from the display table
6748 entry, if not empty. If empty, don't display the
6749 current character. */
6750 if (v->header.size)
6751 {
6752 it->dpvec_char_len = it->len;
6753 it->dpvec = v->contents;
6754 it->dpend = v->contents + v->header.size;
6755 it->current.dpvec_index = 0;
6756 it->dpvec_face_id = -1;
6757 it->saved_face_id = it->face_id;
6758 it->method = GET_FROM_DISPLAY_VECTOR;
6759 it->ellipsis_p = 0;
6760 }
6761 else
6762 {
6763 set_iterator_to_next (it, 0);
6764 }
6765 goto get_next;
6766 }
6767
6768 if (! NILP (lookup_glyphless_char_display (c, it)))
6769 {
6770 if (it->what == IT_GLYPHLESS)
6771 goto done;
6772 /* Don't display this character. */
6773 set_iterator_to_next (it, 0);
6774 goto get_next;
6775 }
6776
6777 /* If `nobreak-char-display' is non-nil, we display
6778 non-ASCII spaces and hyphens specially. */
6779 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6780 {
6781 if (c == 0xA0)
6782 nonascii_space_p = 1;
6783 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6784 nonascii_hyphen_p = 1;
6785 }
6786
6787 /* Translate control characters into `\003' or `^C' form.
6788 Control characters coming from a display table entry are
6789 currently not translated because we use IT->dpvec to hold
6790 the translation. This could easily be changed but I
6791 don't believe that it is worth doing.
6792
6793 The characters handled by `nobreak-char-display' must be
6794 translated too.
6795
6796 Non-printable characters and raw-byte characters are also
6797 translated to octal form. */
6798 if (((c < ' ' || c == 127) /* ASCII control chars */
6799 ? (it->area != TEXT_AREA
6800 /* In mode line, treat \n, \t like other crl chars. */
6801 || (c != '\t'
6802 && it->glyph_row
6803 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6804 || (c != '\n' && c != '\t'))
6805 : (nonascii_space_p
6806 || nonascii_hyphen_p
6807 || CHAR_BYTE8_P (c)
6808 || ! CHAR_PRINTABLE_P (c))))
6809 {
6810 /* C is a control character, non-ASCII space/hyphen,
6811 raw-byte, or a non-printable character which must be
6812 displayed either as '\003' or as `^C' where the '\\'
6813 and '^' can be defined in the display table. Fill
6814 IT->ctl_chars with glyphs for what we have to
6815 display. Then, set IT->dpvec to these glyphs. */
6816 Lisp_Object gc;
6817 int ctl_len;
6818 int face_id;
6819 int lface_id = 0;
6820 int escape_glyph;
6821
6822 /* Handle control characters with ^. */
6823
6824 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6825 {
6826 int g;
6827
6828 g = '^'; /* default glyph for Control */
6829 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6830 if (it->dp
6831 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6832 {
6833 g = GLYPH_CODE_CHAR (gc);
6834 lface_id = GLYPH_CODE_FACE (gc);
6835 }
6836 if (lface_id)
6837 {
6838 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6839 }
6840 else if (it->f == last_escape_glyph_frame
6841 && it->face_id == last_escape_glyph_face_id)
6842 {
6843 face_id = last_escape_glyph_merged_face_id;
6844 }
6845 else
6846 {
6847 /* Merge the escape-glyph face into the current face. */
6848 face_id = merge_faces (it->f, Qescape_glyph, 0,
6849 it->face_id);
6850 last_escape_glyph_frame = it->f;
6851 last_escape_glyph_face_id = it->face_id;
6852 last_escape_glyph_merged_face_id = face_id;
6853 }
6854
6855 XSETINT (it->ctl_chars[0], g);
6856 XSETINT (it->ctl_chars[1], c ^ 0100);
6857 ctl_len = 2;
6858 goto display_control;
6859 }
6860
6861 /* Handle non-ascii space in the mode where it only gets
6862 highlighting. */
6863
6864 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6865 {
6866 /* Merge `nobreak-space' into the current face. */
6867 face_id = merge_faces (it->f, Qnobreak_space, 0,
6868 it->face_id);
6869 XSETINT (it->ctl_chars[0], ' ');
6870 ctl_len = 1;
6871 goto display_control;
6872 }
6873
6874 /* Handle sequences that start with the "escape glyph". */
6875
6876 /* the default escape glyph is \. */
6877 escape_glyph = '\\';
6878
6879 if (it->dp
6880 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6881 {
6882 escape_glyph = GLYPH_CODE_CHAR (gc);
6883 lface_id = GLYPH_CODE_FACE (gc);
6884 }
6885 if (lface_id)
6886 {
6887 /* The display table specified a face.
6888 Merge it into face_id and also into escape_glyph. */
6889 face_id = merge_faces (it->f, Qt, lface_id,
6890 it->face_id);
6891 }
6892 else if (it->f == last_escape_glyph_frame
6893 && it->face_id == last_escape_glyph_face_id)
6894 {
6895 face_id = last_escape_glyph_merged_face_id;
6896 }
6897 else
6898 {
6899 /* Merge the escape-glyph face into the current face. */
6900 face_id = merge_faces (it->f, Qescape_glyph, 0,
6901 it->face_id);
6902 last_escape_glyph_frame = it->f;
6903 last_escape_glyph_face_id = it->face_id;
6904 last_escape_glyph_merged_face_id = face_id;
6905 }
6906
6907 /* Draw non-ASCII hyphen with just highlighting: */
6908
6909 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6910 {
6911 XSETINT (it->ctl_chars[0], '-');
6912 ctl_len = 1;
6913 goto display_control;
6914 }
6915
6916 /* Draw non-ASCII space/hyphen with escape glyph: */
6917
6918 if (nonascii_space_p || nonascii_hyphen_p)
6919 {
6920 XSETINT (it->ctl_chars[0], escape_glyph);
6921 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6922 ctl_len = 2;
6923 goto display_control;
6924 }
6925
6926 {
6927 char str[10];
6928 int len, i;
6929
6930 if (CHAR_BYTE8_P (c))
6931 /* Display \200 instead of \17777600. */
6932 c = CHAR_TO_BYTE8 (c);
6933 len = sprintf (str, "%03o", c);
6934
6935 XSETINT (it->ctl_chars[0], escape_glyph);
6936 for (i = 0; i < len; i++)
6937 XSETINT (it->ctl_chars[i + 1], str[i]);
6938 ctl_len = len + 1;
6939 }
6940
6941 display_control:
6942 /* Set up IT->dpvec and return first character from it. */
6943 it->dpvec_char_len = it->len;
6944 it->dpvec = it->ctl_chars;
6945 it->dpend = it->dpvec + ctl_len;
6946 it->current.dpvec_index = 0;
6947 it->dpvec_face_id = face_id;
6948 it->saved_face_id = it->face_id;
6949 it->method = GET_FROM_DISPLAY_VECTOR;
6950 it->ellipsis_p = 0;
6951 goto get_next;
6952 }
6953 it->char_to_display = c;
6954 }
6955 else if (success_p)
6956 {
6957 it->char_to_display = it->c;
6958 }
6959 }
6960
6961 /* Adjust face id for a multibyte character. There are no multibyte
6962 character in unibyte text. */
6963 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6964 && it->multibyte_p
6965 && success_p
6966 && FRAME_WINDOW_P (it->f))
6967 {
6968 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6969
6970 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6971 {
6972 /* Automatic composition with glyph-string. */
6973 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6974
6975 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6976 }
6977 else
6978 {
6979 ptrdiff_t pos = (it->s ? -1
6980 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6981 : IT_CHARPOS (*it));
6982 int c;
6983
6984 if (it->what == IT_CHARACTER)
6985 c = it->char_to_display;
6986 else
6987 {
6988 struct composition *cmp = composition_table[it->cmp_it.id];
6989 int i;
6990
6991 c = ' ';
6992 for (i = 0; i < cmp->glyph_len; i++)
6993 /* TAB in a composition means display glyphs with
6994 padding space on the left or right. */
6995 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6996 break;
6997 }
6998 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6999 }
7000 }
7001
7002 done:
7003 /* Is this character the last one of a run of characters with
7004 box? If yes, set IT->end_of_box_run_p to 1. */
7005 if (it->face_box_p
7006 && it->s == NULL)
7007 {
7008 if (it->method == GET_FROM_STRING && it->sp)
7009 {
7010 int face_id = underlying_face_id (it);
7011 struct face *face = FACE_FROM_ID (it->f, face_id);
7012
7013 if (face)
7014 {
7015 if (face->box == FACE_NO_BOX)
7016 {
7017 /* If the box comes from face properties in a
7018 display string, check faces in that string. */
7019 int string_face_id = face_after_it_pos (it);
7020 it->end_of_box_run_p
7021 = (FACE_FROM_ID (it->f, string_face_id)->box
7022 == FACE_NO_BOX);
7023 }
7024 /* Otherwise, the box comes from the underlying face.
7025 If this is the last string character displayed, check
7026 the next buffer location. */
7027 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7028 && (it->current.overlay_string_index
7029 == it->n_overlay_strings - 1))
7030 {
7031 ptrdiff_t ignore;
7032 int next_face_id;
7033 struct text_pos pos = it->current.pos;
7034 INC_TEXT_POS (pos, it->multibyte_p);
7035
7036 next_face_id = face_at_buffer_position
7037 (it->w, CHARPOS (pos), it->region_beg_charpos,
7038 it->region_end_charpos, &ignore,
7039 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7040 -1);
7041 it->end_of_box_run_p
7042 = (FACE_FROM_ID (it->f, next_face_id)->box
7043 == FACE_NO_BOX);
7044 }
7045 }
7046 }
7047 else
7048 {
7049 int face_id = face_after_it_pos (it);
7050 it->end_of_box_run_p
7051 = (face_id != it->face_id
7052 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7053 }
7054 }
7055 /* If we reached the end of the object we've been iterating (e.g., a
7056 display string or an overlay string), and there's something on
7057 IT->stack, proceed with what's on the stack. It doesn't make
7058 sense to return zero if there's unprocessed stuff on the stack,
7059 because otherwise that stuff will never be displayed. */
7060 if (!success_p && it->sp > 0)
7061 {
7062 set_iterator_to_next (it, 0);
7063 success_p = get_next_display_element (it);
7064 }
7065
7066 /* Value is 0 if end of buffer or string reached. */
7067 return success_p;
7068 }
7069
7070
7071 /* Move IT to the next display element.
7072
7073 RESEAT_P non-zero means if called on a newline in buffer text,
7074 skip to the next visible line start.
7075
7076 Functions get_next_display_element and set_iterator_to_next are
7077 separate because I find this arrangement easier to handle than a
7078 get_next_display_element function that also increments IT's
7079 position. The way it is we can first look at an iterator's current
7080 display element, decide whether it fits on a line, and if it does,
7081 increment the iterator position. The other way around we probably
7082 would either need a flag indicating whether the iterator has to be
7083 incremented the next time, or we would have to implement a
7084 decrement position function which would not be easy to write. */
7085
7086 void
7087 set_iterator_to_next (struct it *it, int reseat_p)
7088 {
7089 /* Reset flags indicating start and end of a sequence of characters
7090 with box. Reset them at the start of this function because
7091 moving the iterator to a new position might set them. */
7092 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7093
7094 switch (it->method)
7095 {
7096 case GET_FROM_BUFFER:
7097 /* The current display element of IT is a character from
7098 current_buffer. Advance in the buffer, and maybe skip over
7099 invisible lines that are so because of selective display. */
7100 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7101 reseat_at_next_visible_line_start (it, 0);
7102 else if (it->cmp_it.id >= 0)
7103 {
7104 /* We are currently getting glyphs from a composition. */
7105 int i;
7106
7107 if (! it->bidi_p)
7108 {
7109 IT_CHARPOS (*it) += it->cmp_it.nchars;
7110 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7111 if (it->cmp_it.to < it->cmp_it.nglyphs)
7112 {
7113 it->cmp_it.from = it->cmp_it.to;
7114 }
7115 else
7116 {
7117 it->cmp_it.id = -1;
7118 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7119 IT_BYTEPOS (*it),
7120 it->end_charpos, Qnil);
7121 }
7122 }
7123 else if (! it->cmp_it.reversed_p)
7124 {
7125 /* Composition created while scanning forward. */
7126 /* Update IT's char/byte positions to point to the first
7127 character of the next grapheme cluster, or to the
7128 character visually after the current composition. */
7129 for (i = 0; i < it->cmp_it.nchars; i++)
7130 bidi_move_to_visually_next (&it->bidi_it);
7131 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7132 IT_CHARPOS (*it) = it->bidi_it.charpos;
7133
7134 if (it->cmp_it.to < it->cmp_it.nglyphs)
7135 {
7136 /* Proceed to the next grapheme cluster. */
7137 it->cmp_it.from = it->cmp_it.to;
7138 }
7139 else
7140 {
7141 /* No more grapheme clusters in this composition.
7142 Find the next stop position. */
7143 ptrdiff_t stop = it->end_charpos;
7144 if (it->bidi_it.scan_dir < 0)
7145 /* Now we are scanning backward and don't know
7146 where to stop. */
7147 stop = -1;
7148 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7149 IT_BYTEPOS (*it), stop, Qnil);
7150 }
7151 }
7152 else
7153 {
7154 /* Composition created while scanning backward. */
7155 /* Update IT's char/byte positions to point to the last
7156 character of the previous grapheme cluster, or the
7157 character visually after the current composition. */
7158 for (i = 0; i < it->cmp_it.nchars; i++)
7159 bidi_move_to_visually_next (&it->bidi_it);
7160 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7161 IT_CHARPOS (*it) = it->bidi_it.charpos;
7162 if (it->cmp_it.from > 0)
7163 {
7164 /* Proceed to the previous grapheme cluster. */
7165 it->cmp_it.to = it->cmp_it.from;
7166 }
7167 else
7168 {
7169 /* No more grapheme clusters in this composition.
7170 Find the next stop position. */
7171 ptrdiff_t stop = it->end_charpos;
7172 if (it->bidi_it.scan_dir < 0)
7173 /* Now we are scanning backward and don't know
7174 where to stop. */
7175 stop = -1;
7176 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7177 IT_BYTEPOS (*it), stop, Qnil);
7178 }
7179 }
7180 }
7181 else
7182 {
7183 eassert (it->len != 0);
7184
7185 if (!it->bidi_p)
7186 {
7187 IT_BYTEPOS (*it) += it->len;
7188 IT_CHARPOS (*it) += 1;
7189 }
7190 else
7191 {
7192 int prev_scan_dir = it->bidi_it.scan_dir;
7193 /* If this is a new paragraph, determine its base
7194 direction (a.k.a. its base embedding level). */
7195 if (it->bidi_it.new_paragraph)
7196 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7197 bidi_move_to_visually_next (&it->bidi_it);
7198 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7199 IT_CHARPOS (*it) = it->bidi_it.charpos;
7200 if (prev_scan_dir != it->bidi_it.scan_dir)
7201 {
7202 /* As the scan direction was changed, we must
7203 re-compute the stop position for composition. */
7204 ptrdiff_t stop = it->end_charpos;
7205 if (it->bidi_it.scan_dir < 0)
7206 stop = -1;
7207 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7208 IT_BYTEPOS (*it), stop, Qnil);
7209 }
7210 }
7211 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7212 }
7213 break;
7214
7215 case GET_FROM_C_STRING:
7216 /* Current display element of IT is from a C string. */
7217 if (!it->bidi_p
7218 /* If the string position is beyond string's end, it means
7219 next_element_from_c_string is padding the string with
7220 blanks, in which case we bypass the bidi iterator,
7221 because it cannot deal with such virtual characters. */
7222 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7223 {
7224 IT_BYTEPOS (*it) += it->len;
7225 IT_CHARPOS (*it) += 1;
7226 }
7227 else
7228 {
7229 bidi_move_to_visually_next (&it->bidi_it);
7230 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7231 IT_CHARPOS (*it) = it->bidi_it.charpos;
7232 }
7233 break;
7234
7235 case GET_FROM_DISPLAY_VECTOR:
7236 /* Current display element of IT is from a display table entry.
7237 Advance in the display table definition. Reset it to null if
7238 end reached, and continue with characters from buffers/
7239 strings. */
7240 ++it->current.dpvec_index;
7241
7242 /* Restore face of the iterator to what they were before the
7243 display vector entry (these entries may contain faces). */
7244 it->face_id = it->saved_face_id;
7245
7246 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7247 {
7248 int recheck_faces = it->ellipsis_p;
7249
7250 if (it->s)
7251 it->method = GET_FROM_C_STRING;
7252 else if (STRINGP (it->string))
7253 it->method = GET_FROM_STRING;
7254 else
7255 {
7256 it->method = GET_FROM_BUFFER;
7257 it->object = it->w->contents;
7258 }
7259
7260 it->dpvec = NULL;
7261 it->current.dpvec_index = -1;
7262
7263 /* Skip over characters which were displayed via IT->dpvec. */
7264 if (it->dpvec_char_len < 0)
7265 reseat_at_next_visible_line_start (it, 1);
7266 else if (it->dpvec_char_len > 0)
7267 {
7268 if (it->method == GET_FROM_STRING
7269 && it->current.overlay_string_index >= 0
7270 && it->n_overlay_strings > 0)
7271 it->ignore_overlay_strings_at_pos_p = 1;
7272 it->len = it->dpvec_char_len;
7273 set_iterator_to_next (it, reseat_p);
7274 }
7275
7276 /* Maybe recheck faces after display vector */
7277 if (recheck_faces)
7278 it->stop_charpos = IT_CHARPOS (*it);
7279 }
7280 break;
7281
7282 case GET_FROM_STRING:
7283 /* Current display element is a character from a Lisp string. */
7284 eassert (it->s == NULL && STRINGP (it->string));
7285 /* Don't advance past string end. These conditions are true
7286 when set_iterator_to_next is called at the end of
7287 get_next_display_element, in which case the Lisp string is
7288 already exhausted, and all we want is pop the iterator
7289 stack. */
7290 if (it->current.overlay_string_index >= 0)
7291 {
7292 /* This is an overlay string, so there's no padding with
7293 spaces, and the number of characters in the string is
7294 where the string ends. */
7295 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7296 goto consider_string_end;
7297 }
7298 else
7299 {
7300 /* Not an overlay string. There could be padding, so test
7301 against it->end_charpos . */
7302 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7303 goto consider_string_end;
7304 }
7305 if (it->cmp_it.id >= 0)
7306 {
7307 int i;
7308
7309 if (! it->bidi_p)
7310 {
7311 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7312 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7313 if (it->cmp_it.to < it->cmp_it.nglyphs)
7314 it->cmp_it.from = it->cmp_it.to;
7315 else
7316 {
7317 it->cmp_it.id = -1;
7318 composition_compute_stop_pos (&it->cmp_it,
7319 IT_STRING_CHARPOS (*it),
7320 IT_STRING_BYTEPOS (*it),
7321 it->end_charpos, it->string);
7322 }
7323 }
7324 else if (! it->cmp_it.reversed_p)
7325 {
7326 for (i = 0; i < it->cmp_it.nchars; i++)
7327 bidi_move_to_visually_next (&it->bidi_it);
7328 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7329 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7330
7331 if (it->cmp_it.to < it->cmp_it.nglyphs)
7332 it->cmp_it.from = it->cmp_it.to;
7333 else
7334 {
7335 ptrdiff_t stop = it->end_charpos;
7336 if (it->bidi_it.scan_dir < 0)
7337 stop = -1;
7338 composition_compute_stop_pos (&it->cmp_it,
7339 IT_STRING_CHARPOS (*it),
7340 IT_STRING_BYTEPOS (*it), stop,
7341 it->string);
7342 }
7343 }
7344 else
7345 {
7346 for (i = 0; i < it->cmp_it.nchars; i++)
7347 bidi_move_to_visually_next (&it->bidi_it);
7348 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7349 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7350 if (it->cmp_it.from > 0)
7351 it->cmp_it.to = it->cmp_it.from;
7352 else
7353 {
7354 ptrdiff_t stop = it->end_charpos;
7355 if (it->bidi_it.scan_dir < 0)
7356 stop = -1;
7357 composition_compute_stop_pos (&it->cmp_it,
7358 IT_STRING_CHARPOS (*it),
7359 IT_STRING_BYTEPOS (*it), stop,
7360 it->string);
7361 }
7362 }
7363 }
7364 else
7365 {
7366 if (!it->bidi_p
7367 /* If the string position is beyond string's end, it
7368 means next_element_from_string is padding the string
7369 with blanks, in which case we bypass the bidi
7370 iterator, because it cannot deal with such virtual
7371 characters. */
7372 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7373 {
7374 IT_STRING_BYTEPOS (*it) += it->len;
7375 IT_STRING_CHARPOS (*it) += 1;
7376 }
7377 else
7378 {
7379 int prev_scan_dir = it->bidi_it.scan_dir;
7380
7381 bidi_move_to_visually_next (&it->bidi_it);
7382 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7383 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7384 if (prev_scan_dir != it->bidi_it.scan_dir)
7385 {
7386 ptrdiff_t stop = it->end_charpos;
7387
7388 if (it->bidi_it.scan_dir < 0)
7389 stop = -1;
7390 composition_compute_stop_pos (&it->cmp_it,
7391 IT_STRING_CHARPOS (*it),
7392 IT_STRING_BYTEPOS (*it), stop,
7393 it->string);
7394 }
7395 }
7396 }
7397
7398 consider_string_end:
7399
7400 if (it->current.overlay_string_index >= 0)
7401 {
7402 /* IT->string is an overlay string. Advance to the
7403 next, if there is one. */
7404 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7405 {
7406 it->ellipsis_p = 0;
7407 next_overlay_string (it);
7408 if (it->ellipsis_p)
7409 setup_for_ellipsis (it, 0);
7410 }
7411 }
7412 else
7413 {
7414 /* IT->string is not an overlay string. If we reached
7415 its end, and there is something on IT->stack, proceed
7416 with what is on the stack. This can be either another
7417 string, this time an overlay string, or a buffer. */
7418 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7419 && it->sp > 0)
7420 {
7421 pop_it (it);
7422 if (it->method == GET_FROM_STRING)
7423 goto consider_string_end;
7424 }
7425 }
7426 break;
7427
7428 case GET_FROM_IMAGE:
7429 case GET_FROM_STRETCH:
7430 /* The position etc with which we have to proceed are on
7431 the stack. The position may be at the end of a string,
7432 if the `display' property takes up the whole string. */
7433 eassert (it->sp > 0);
7434 pop_it (it);
7435 if (it->method == GET_FROM_STRING)
7436 goto consider_string_end;
7437 break;
7438
7439 default:
7440 /* There are no other methods defined, so this should be a bug. */
7441 emacs_abort ();
7442 }
7443
7444 eassert (it->method != GET_FROM_STRING
7445 || (STRINGP (it->string)
7446 && IT_STRING_CHARPOS (*it) >= 0));
7447 }
7448
7449 /* Load IT's display element fields with information about the next
7450 display element which comes from a display table entry or from the
7451 result of translating a control character to one of the forms `^C'
7452 or `\003'.
7453
7454 IT->dpvec holds the glyphs to return as characters.
7455 IT->saved_face_id holds the face id before the display vector--it
7456 is restored into IT->face_id in set_iterator_to_next. */
7457
7458 static int
7459 next_element_from_display_vector (struct it *it)
7460 {
7461 Lisp_Object gc;
7462
7463 /* Precondition. */
7464 eassert (it->dpvec && it->current.dpvec_index >= 0);
7465
7466 it->face_id = it->saved_face_id;
7467
7468 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7469 That seemed totally bogus - so I changed it... */
7470 gc = it->dpvec[it->current.dpvec_index];
7471
7472 if (GLYPH_CODE_P (gc))
7473 {
7474 it->c = GLYPH_CODE_CHAR (gc);
7475 it->len = CHAR_BYTES (it->c);
7476
7477 /* The entry may contain a face id to use. Such a face id is
7478 the id of a Lisp face, not a realized face. A face id of
7479 zero means no face is specified. */
7480 if (it->dpvec_face_id >= 0)
7481 it->face_id = it->dpvec_face_id;
7482 else
7483 {
7484 int lface_id = GLYPH_CODE_FACE (gc);
7485 if (lface_id > 0)
7486 it->face_id = merge_faces (it->f, Qt, lface_id,
7487 it->saved_face_id);
7488 }
7489 }
7490 else
7491 /* Display table entry is invalid. Return a space. */
7492 it->c = ' ', it->len = 1;
7493
7494 /* Don't change position and object of the iterator here. They are
7495 still the values of the character that had this display table
7496 entry or was translated, and that's what we want. */
7497 it->what = IT_CHARACTER;
7498 return 1;
7499 }
7500
7501 /* Get the first element of string/buffer in the visual order, after
7502 being reseated to a new position in a string or a buffer. */
7503 static void
7504 get_visually_first_element (struct it *it)
7505 {
7506 int string_p = STRINGP (it->string) || it->s;
7507 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7508 ptrdiff_t bob = (string_p ? 0 : BEGV);
7509
7510 if (STRINGP (it->string))
7511 {
7512 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7513 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7514 }
7515 else
7516 {
7517 it->bidi_it.charpos = IT_CHARPOS (*it);
7518 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7519 }
7520
7521 if (it->bidi_it.charpos == eob)
7522 {
7523 /* Nothing to do, but reset the FIRST_ELT flag, like
7524 bidi_paragraph_init does, because we are not going to
7525 call it. */
7526 it->bidi_it.first_elt = 0;
7527 }
7528 else if (it->bidi_it.charpos == bob
7529 || (!string_p
7530 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7531 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7532 {
7533 /* If we are at the beginning of a line/string, we can produce
7534 the next element right away. */
7535 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7536 bidi_move_to_visually_next (&it->bidi_it);
7537 }
7538 else
7539 {
7540 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7541
7542 /* We need to prime the bidi iterator starting at the line's or
7543 string's beginning, before we will be able to produce the
7544 next element. */
7545 if (string_p)
7546 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7547 else
7548 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7549 IT_BYTEPOS (*it), -1,
7550 &it->bidi_it.bytepos);
7551 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7552 do
7553 {
7554 /* Now return to buffer/string position where we were asked
7555 to get the next display element, and produce that. */
7556 bidi_move_to_visually_next (&it->bidi_it);
7557 }
7558 while (it->bidi_it.bytepos != orig_bytepos
7559 && it->bidi_it.charpos < eob);
7560 }
7561
7562 /* Adjust IT's position information to where we ended up. */
7563 if (STRINGP (it->string))
7564 {
7565 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7566 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7567 }
7568 else
7569 {
7570 IT_CHARPOS (*it) = it->bidi_it.charpos;
7571 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7572 }
7573
7574 if (STRINGP (it->string) || !it->s)
7575 {
7576 ptrdiff_t stop, charpos, bytepos;
7577
7578 if (STRINGP (it->string))
7579 {
7580 eassert (!it->s);
7581 stop = SCHARS (it->string);
7582 if (stop > it->end_charpos)
7583 stop = it->end_charpos;
7584 charpos = IT_STRING_CHARPOS (*it);
7585 bytepos = IT_STRING_BYTEPOS (*it);
7586 }
7587 else
7588 {
7589 stop = it->end_charpos;
7590 charpos = IT_CHARPOS (*it);
7591 bytepos = IT_BYTEPOS (*it);
7592 }
7593 if (it->bidi_it.scan_dir < 0)
7594 stop = -1;
7595 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7596 it->string);
7597 }
7598 }
7599
7600 /* Load IT with the next display element from Lisp string IT->string.
7601 IT->current.string_pos is the current position within the string.
7602 If IT->current.overlay_string_index >= 0, the Lisp string is an
7603 overlay string. */
7604
7605 static int
7606 next_element_from_string (struct it *it)
7607 {
7608 struct text_pos position;
7609
7610 eassert (STRINGP (it->string));
7611 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7612 eassert (IT_STRING_CHARPOS (*it) >= 0);
7613 position = it->current.string_pos;
7614
7615 /* With bidi reordering, the character to display might not be the
7616 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7617 that we were reseat()ed to a new string, whose paragraph
7618 direction is not known. */
7619 if (it->bidi_p && it->bidi_it.first_elt)
7620 {
7621 get_visually_first_element (it);
7622 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7623 }
7624
7625 /* Time to check for invisible text? */
7626 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7627 {
7628 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7629 {
7630 if (!(!it->bidi_p
7631 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7632 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7633 {
7634 /* With bidi non-linear iteration, we could find
7635 ourselves far beyond the last computed stop_charpos,
7636 with several other stop positions in between that we
7637 missed. Scan them all now, in buffer's logical
7638 order, until we find and handle the last stop_charpos
7639 that precedes our current position. */
7640 handle_stop_backwards (it, it->stop_charpos);
7641 return GET_NEXT_DISPLAY_ELEMENT (it);
7642 }
7643 else
7644 {
7645 if (it->bidi_p)
7646 {
7647 /* Take note of the stop position we just moved
7648 across, for when we will move back across it. */
7649 it->prev_stop = it->stop_charpos;
7650 /* If we are at base paragraph embedding level, take
7651 note of the last stop position seen at this
7652 level. */
7653 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7654 it->base_level_stop = it->stop_charpos;
7655 }
7656 handle_stop (it);
7657
7658 /* Since a handler may have changed IT->method, we must
7659 recurse here. */
7660 return GET_NEXT_DISPLAY_ELEMENT (it);
7661 }
7662 }
7663 else if (it->bidi_p
7664 /* If we are before prev_stop, we may have overstepped
7665 on our way backwards a stop_pos, and if so, we need
7666 to handle that stop_pos. */
7667 && IT_STRING_CHARPOS (*it) < it->prev_stop
7668 /* We can sometimes back up for reasons that have nothing
7669 to do with bidi reordering. E.g., compositions. The
7670 code below is only needed when we are above the base
7671 embedding level, so test for that explicitly. */
7672 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7673 {
7674 /* If we lost track of base_level_stop, we have no better
7675 place for handle_stop_backwards to start from than string
7676 beginning. This happens, e.g., when we were reseated to
7677 the previous screenful of text by vertical-motion. */
7678 if (it->base_level_stop <= 0
7679 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7680 it->base_level_stop = 0;
7681 handle_stop_backwards (it, it->base_level_stop);
7682 return GET_NEXT_DISPLAY_ELEMENT (it);
7683 }
7684 }
7685
7686 if (it->current.overlay_string_index >= 0)
7687 {
7688 /* Get the next character from an overlay string. In overlay
7689 strings, there is no field width or padding with spaces to
7690 do. */
7691 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7692 {
7693 it->what = IT_EOB;
7694 return 0;
7695 }
7696 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7697 IT_STRING_BYTEPOS (*it),
7698 it->bidi_it.scan_dir < 0
7699 ? -1
7700 : SCHARS (it->string))
7701 && next_element_from_composition (it))
7702 {
7703 return 1;
7704 }
7705 else if (STRING_MULTIBYTE (it->string))
7706 {
7707 const unsigned char *s = (SDATA (it->string)
7708 + IT_STRING_BYTEPOS (*it));
7709 it->c = string_char_and_length (s, &it->len);
7710 }
7711 else
7712 {
7713 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7714 it->len = 1;
7715 }
7716 }
7717 else
7718 {
7719 /* Get the next character from a Lisp string that is not an
7720 overlay string. Such strings come from the mode line, for
7721 example. We may have to pad with spaces, or truncate the
7722 string. See also next_element_from_c_string. */
7723 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7724 {
7725 it->what = IT_EOB;
7726 return 0;
7727 }
7728 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7729 {
7730 /* Pad with spaces. */
7731 it->c = ' ', it->len = 1;
7732 CHARPOS (position) = BYTEPOS (position) = -1;
7733 }
7734 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7735 IT_STRING_BYTEPOS (*it),
7736 it->bidi_it.scan_dir < 0
7737 ? -1
7738 : it->string_nchars)
7739 && next_element_from_composition (it))
7740 {
7741 return 1;
7742 }
7743 else if (STRING_MULTIBYTE (it->string))
7744 {
7745 const unsigned char *s = (SDATA (it->string)
7746 + IT_STRING_BYTEPOS (*it));
7747 it->c = string_char_and_length (s, &it->len);
7748 }
7749 else
7750 {
7751 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7752 it->len = 1;
7753 }
7754 }
7755
7756 /* Record what we have and where it came from. */
7757 it->what = IT_CHARACTER;
7758 it->object = it->string;
7759 it->position = position;
7760 return 1;
7761 }
7762
7763
7764 /* Load IT with next display element from C string IT->s.
7765 IT->string_nchars is the maximum number of characters to return
7766 from the string. IT->end_charpos may be greater than
7767 IT->string_nchars when this function is called, in which case we
7768 may have to return padding spaces. Value is zero if end of string
7769 reached, including padding spaces. */
7770
7771 static int
7772 next_element_from_c_string (struct it *it)
7773 {
7774 int success_p = 1;
7775
7776 eassert (it->s);
7777 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7778 it->what = IT_CHARACTER;
7779 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7780 it->object = Qnil;
7781
7782 /* With bidi reordering, the character to display might not be the
7783 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7784 we were reseated to a new string, whose paragraph direction is
7785 not known. */
7786 if (it->bidi_p && it->bidi_it.first_elt)
7787 get_visually_first_element (it);
7788
7789 /* IT's position can be greater than IT->string_nchars in case a
7790 field width or precision has been specified when the iterator was
7791 initialized. */
7792 if (IT_CHARPOS (*it) >= it->end_charpos)
7793 {
7794 /* End of the game. */
7795 it->what = IT_EOB;
7796 success_p = 0;
7797 }
7798 else if (IT_CHARPOS (*it) >= it->string_nchars)
7799 {
7800 /* Pad with spaces. */
7801 it->c = ' ', it->len = 1;
7802 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7803 }
7804 else if (it->multibyte_p)
7805 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7806 else
7807 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7808
7809 return success_p;
7810 }
7811
7812
7813 /* Set up IT to return characters from an ellipsis, if appropriate.
7814 The definition of the ellipsis glyphs may come from a display table
7815 entry. This function fills IT with the first glyph from the
7816 ellipsis if an ellipsis is to be displayed. */
7817
7818 static int
7819 next_element_from_ellipsis (struct it *it)
7820 {
7821 if (it->selective_display_ellipsis_p)
7822 setup_for_ellipsis (it, it->len);
7823 else
7824 {
7825 /* The face at the current position may be different from the
7826 face we find after the invisible text. Remember what it
7827 was in IT->saved_face_id, and signal that it's there by
7828 setting face_before_selective_p. */
7829 it->saved_face_id = it->face_id;
7830 it->method = GET_FROM_BUFFER;
7831 it->object = it->w->contents;
7832 reseat_at_next_visible_line_start (it, 1);
7833 it->face_before_selective_p = 1;
7834 }
7835
7836 return GET_NEXT_DISPLAY_ELEMENT (it);
7837 }
7838
7839
7840 /* Deliver an image display element. The iterator IT is already
7841 filled with image information (done in handle_display_prop). Value
7842 is always 1. */
7843
7844
7845 static int
7846 next_element_from_image (struct it *it)
7847 {
7848 it->what = IT_IMAGE;
7849 it->ignore_overlay_strings_at_pos_p = 0;
7850 return 1;
7851 }
7852
7853
7854 /* Fill iterator IT with next display element from a stretch glyph
7855 property. IT->object is the value of the text property. Value is
7856 always 1. */
7857
7858 static int
7859 next_element_from_stretch (struct it *it)
7860 {
7861 it->what = IT_STRETCH;
7862 return 1;
7863 }
7864
7865 /* Scan backwards from IT's current position until we find a stop
7866 position, or until BEGV. This is called when we find ourself
7867 before both the last known prev_stop and base_level_stop while
7868 reordering bidirectional text. */
7869
7870 static void
7871 compute_stop_pos_backwards (struct it *it)
7872 {
7873 const int SCAN_BACK_LIMIT = 1000;
7874 struct text_pos pos;
7875 struct display_pos save_current = it->current;
7876 struct text_pos save_position = it->position;
7877 ptrdiff_t charpos = IT_CHARPOS (*it);
7878 ptrdiff_t where_we_are = charpos;
7879 ptrdiff_t save_stop_pos = it->stop_charpos;
7880 ptrdiff_t save_end_pos = it->end_charpos;
7881
7882 eassert (NILP (it->string) && !it->s);
7883 eassert (it->bidi_p);
7884 it->bidi_p = 0;
7885 do
7886 {
7887 it->end_charpos = min (charpos + 1, ZV);
7888 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7889 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7890 reseat_1 (it, pos, 0);
7891 compute_stop_pos (it);
7892 /* We must advance forward, right? */
7893 if (it->stop_charpos <= charpos)
7894 emacs_abort ();
7895 }
7896 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7897
7898 if (it->stop_charpos <= where_we_are)
7899 it->prev_stop = it->stop_charpos;
7900 else
7901 it->prev_stop = BEGV;
7902 it->bidi_p = 1;
7903 it->current = save_current;
7904 it->position = save_position;
7905 it->stop_charpos = save_stop_pos;
7906 it->end_charpos = save_end_pos;
7907 }
7908
7909 /* Scan forward from CHARPOS in the current buffer/string, until we
7910 find a stop position > current IT's position. Then handle the stop
7911 position before that. This is called when we bump into a stop
7912 position while reordering bidirectional text. CHARPOS should be
7913 the last previously processed stop_pos (or BEGV/0, if none were
7914 processed yet) whose position is less that IT's current
7915 position. */
7916
7917 static void
7918 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7919 {
7920 int bufp = !STRINGP (it->string);
7921 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7922 struct display_pos save_current = it->current;
7923 struct text_pos save_position = it->position;
7924 struct text_pos pos1;
7925 ptrdiff_t next_stop;
7926
7927 /* Scan in strict logical order. */
7928 eassert (it->bidi_p);
7929 it->bidi_p = 0;
7930 do
7931 {
7932 it->prev_stop = charpos;
7933 if (bufp)
7934 {
7935 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7936 reseat_1 (it, pos1, 0);
7937 }
7938 else
7939 it->current.string_pos = string_pos (charpos, it->string);
7940 compute_stop_pos (it);
7941 /* We must advance forward, right? */
7942 if (it->stop_charpos <= it->prev_stop)
7943 emacs_abort ();
7944 charpos = it->stop_charpos;
7945 }
7946 while (charpos <= where_we_are);
7947
7948 it->bidi_p = 1;
7949 it->current = save_current;
7950 it->position = save_position;
7951 next_stop = it->stop_charpos;
7952 it->stop_charpos = it->prev_stop;
7953 handle_stop (it);
7954 it->stop_charpos = next_stop;
7955 }
7956
7957 /* Load IT with the next display element from current_buffer. Value
7958 is zero if end of buffer reached. IT->stop_charpos is the next
7959 position at which to stop and check for text properties or buffer
7960 end. */
7961
7962 static int
7963 next_element_from_buffer (struct it *it)
7964 {
7965 int success_p = 1;
7966
7967 eassert (IT_CHARPOS (*it) >= BEGV);
7968 eassert (NILP (it->string) && !it->s);
7969 eassert (!it->bidi_p
7970 || (EQ (it->bidi_it.string.lstring, Qnil)
7971 && it->bidi_it.string.s == NULL));
7972
7973 /* With bidi reordering, the character to display might not be the
7974 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7975 we were reseat()ed to a new buffer position, which is potentially
7976 a different paragraph. */
7977 if (it->bidi_p && it->bidi_it.first_elt)
7978 {
7979 get_visually_first_element (it);
7980 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7981 }
7982
7983 if (IT_CHARPOS (*it) >= it->stop_charpos)
7984 {
7985 if (IT_CHARPOS (*it) >= it->end_charpos)
7986 {
7987 int overlay_strings_follow_p;
7988
7989 /* End of the game, except when overlay strings follow that
7990 haven't been returned yet. */
7991 if (it->overlay_strings_at_end_processed_p)
7992 overlay_strings_follow_p = 0;
7993 else
7994 {
7995 it->overlay_strings_at_end_processed_p = 1;
7996 overlay_strings_follow_p = get_overlay_strings (it, 0);
7997 }
7998
7999 if (overlay_strings_follow_p)
8000 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8001 else
8002 {
8003 it->what = IT_EOB;
8004 it->position = it->current.pos;
8005 success_p = 0;
8006 }
8007 }
8008 else if (!(!it->bidi_p
8009 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8010 || IT_CHARPOS (*it) == it->stop_charpos))
8011 {
8012 /* With bidi non-linear iteration, we could find ourselves
8013 far beyond the last computed stop_charpos, with several
8014 other stop positions in between that we missed. Scan
8015 them all now, in buffer's logical order, until we find
8016 and handle the last stop_charpos that precedes our
8017 current position. */
8018 handle_stop_backwards (it, it->stop_charpos);
8019 return GET_NEXT_DISPLAY_ELEMENT (it);
8020 }
8021 else
8022 {
8023 if (it->bidi_p)
8024 {
8025 /* Take note of the stop position we just moved across,
8026 for when we will move back across it. */
8027 it->prev_stop = it->stop_charpos;
8028 /* If we are at base paragraph embedding level, take
8029 note of the last stop position seen at this
8030 level. */
8031 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8032 it->base_level_stop = it->stop_charpos;
8033 }
8034 handle_stop (it);
8035 return GET_NEXT_DISPLAY_ELEMENT (it);
8036 }
8037 }
8038 else if (it->bidi_p
8039 /* If we are before prev_stop, we may have overstepped on
8040 our way backwards a stop_pos, and if so, we need to
8041 handle that stop_pos. */
8042 && IT_CHARPOS (*it) < it->prev_stop
8043 /* We can sometimes back up for reasons that have nothing
8044 to do with bidi reordering. E.g., compositions. The
8045 code below is only needed when we are above the base
8046 embedding level, so test for that explicitly. */
8047 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8048 {
8049 if (it->base_level_stop <= 0
8050 || IT_CHARPOS (*it) < it->base_level_stop)
8051 {
8052 /* If we lost track of base_level_stop, we need to find
8053 prev_stop by looking backwards. This happens, e.g., when
8054 we were reseated to the previous screenful of text by
8055 vertical-motion. */
8056 it->base_level_stop = BEGV;
8057 compute_stop_pos_backwards (it);
8058 handle_stop_backwards (it, it->prev_stop);
8059 }
8060 else
8061 handle_stop_backwards (it, it->base_level_stop);
8062 return GET_NEXT_DISPLAY_ELEMENT (it);
8063 }
8064 else
8065 {
8066 /* No face changes, overlays etc. in sight, so just return a
8067 character from current_buffer. */
8068 unsigned char *p;
8069 ptrdiff_t stop;
8070
8071 /* Maybe run the redisplay end trigger hook. Performance note:
8072 This doesn't seem to cost measurable time. */
8073 if (it->redisplay_end_trigger_charpos
8074 && it->glyph_row
8075 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8076 run_redisplay_end_trigger_hook (it);
8077
8078 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8079 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8080 stop)
8081 && next_element_from_composition (it))
8082 {
8083 return 1;
8084 }
8085
8086 /* Get the next character, maybe multibyte. */
8087 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8088 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8089 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8090 else
8091 it->c = *p, it->len = 1;
8092
8093 /* Record what we have and where it came from. */
8094 it->what = IT_CHARACTER;
8095 it->object = it->w->contents;
8096 it->position = it->current.pos;
8097
8098 /* Normally we return the character found above, except when we
8099 really want to return an ellipsis for selective display. */
8100 if (it->selective)
8101 {
8102 if (it->c == '\n')
8103 {
8104 /* A value of selective > 0 means hide lines indented more
8105 than that number of columns. */
8106 if (it->selective > 0
8107 && IT_CHARPOS (*it) + 1 < ZV
8108 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8109 IT_BYTEPOS (*it) + 1,
8110 it->selective))
8111 {
8112 success_p = next_element_from_ellipsis (it);
8113 it->dpvec_char_len = -1;
8114 }
8115 }
8116 else if (it->c == '\r' && it->selective == -1)
8117 {
8118 /* A value of selective == -1 means that everything from the
8119 CR to the end of the line is invisible, with maybe an
8120 ellipsis displayed for it. */
8121 success_p = next_element_from_ellipsis (it);
8122 it->dpvec_char_len = -1;
8123 }
8124 }
8125 }
8126
8127 /* Value is zero if end of buffer reached. */
8128 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8129 return success_p;
8130 }
8131
8132
8133 /* Run the redisplay end trigger hook for IT. */
8134
8135 static void
8136 run_redisplay_end_trigger_hook (struct it *it)
8137 {
8138 Lisp_Object args[3];
8139
8140 /* IT->glyph_row should be non-null, i.e. we should be actually
8141 displaying something, or otherwise we should not run the hook. */
8142 eassert (it->glyph_row);
8143
8144 /* Set up hook arguments. */
8145 args[0] = Qredisplay_end_trigger_functions;
8146 args[1] = it->window;
8147 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8148 it->redisplay_end_trigger_charpos = 0;
8149
8150 /* Since we are *trying* to run these functions, don't try to run
8151 them again, even if they get an error. */
8152 wset_redisplay_end_trigger (it->w, Qnil);
8153 Frun_hook_with_args (3, args);
8154
8155 /* Notice if it changed the face of the character we are on. */
8156 handle_face_prop (it);
8157 }
8158
8159
8160 /* Deliver a composition display element. Unlike the other
8161 next_element_from_XXX, this function is not registered in the array
8162 get_next_element[]. It is called from next_element_from_buffer and
8163 next_element_from_string when necessary. */
8164
8165 static int
8166 next_element_from_composition (struct it *it)
8167 {
8168 it->what = IT_COMPOSITION;
8169 it->len = it->cmp_it.nbytes;
8170 if (STRINGP (it->string))
8171 {
8172 if (it->c < 0)
8173 {
8174 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8175 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8176 return 0;
8177 }
8178 it->position = it->current.string_pos;
8179 it->object = it->string;
8180 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8181 IT_STRING_BYTEPOS (*it), it->string);
8182 }
8183 else
8184 {
8185 if (it->c < 0)
8186 {
8187 IT_CHARPOS (*it) += it->cmp_it.nchars;
8188 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8189 if (it->bidi_p)
8190 {
8191 if (it->bidi_it.new_paragraph)
8192 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8193 /* Resync the bidi iterator with IT's new position.
8194 FIXME: this doesn't support bidirectional text. */
8195 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8196 bidi_move_to_visually_next (&it->bidi_it);
8197 }
8198 return 0;
8199 }
8200 it->position = it->current.pos;
8201 it->object = it->w->contents;
8202 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8203 IT_BYTEPOS (*it), Qnil);
8204 }
8205 return 1;
8206 }
8207
8208
8209 \f
8210 /***********************************************************************
8211 Moving an iterator without producing glyphs
8212 ***********************************************************************/
8213
8214 /* Check if iterator is at a position corresponding to a valid buffer
8215 position after some move_it_ call. */
8216
8217 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8218 ((it)->method == GET_FROM_STRING \
8219 ? IT_STRING_CHARPOS (*it) == 0 \
8220 : 1)
8221
8222
8223 /* Move iterator IT to a specified buffer or X position within one
8224 line on the display without producing glyphs.
8225
8226 OP should be a bit mask including some or all of these bits:
8227 MOVE_TO_X: Stop upon reaching x-position TO_X.
8228 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8229 Regardless of OP's value, stop upon reaching the end of the display line.
8230
8231 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8232 This means, in particular, that TO_X includes window's horizontal
8233 scroll amount.
8234
8235 The return value has several possible values that
8236 say what condition caused the scan to stop:
8237
8238 MOVE_POS_MATCH_OR_ZV
8239 - when TO_POS or ZV was reached.
8240
8241 MOVE_X_REACHED
8242 -when TO_X was reached before TO_POS or ZV were reached.
8243
8244 MOVE_LINE_CONTINUED
8245 - when we reached the end of the display area and the line must
8246 be continued.
8247
8248 MOVE_LINE_TRUNCATED
8249 - when we reached the end of the display area and the line is
8250 truncated.
8251
8252 MOVE_NEWLINE_OR_CR
8253 - when we stopped at a line end, i.e. a newline or a CR and selective
8254 display is on. */
8255
8256 static enum move_it_result
8257 move_it_in_display_line_to (struct it *it,
8258 ptrdiff_t to_charpos, int to_x,
8259 enum move_operation_enum op)
8260 {
8261 enum move_it_result result = MOVE_UNDEFINED;
8262 struct glyph_row *saved_glyph_row;
8263 struct it wrap_it, atpos_it, atx_it, ppos_it;
8264 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8265 void *ppos_data = NULL;
8266 int may_wrap = 0;
8267 enum it_method prev_method = it->method;
8268 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8269 int saw_smaller_pos = prev_pos < to_charpos;
8270
8271 /* Don't produce glyphs in produce_glyphs. */
8272 saved_glyph_row = it->glyph_row;
8273 it->glyph_row = NULL;
8274
8275 /* Use wrap_it to save a copy of IT wherever a word wrap could
8276 occur. Use atpos_it to save a copy of IT at the desired buffer
8277 position, if found, so that we can scan ahead and check if the
8278 word later overshoots the window edge. Use atx_it similarly, for
8279 pixel positions. */
8280 wrap_it.sp = -1;
8281 atpos_it.sp = -1;
8282 atx_it.sp = -1;
8283
8284 /* Use ppos_it under bidi reordering to save a copy of IT for the
8285 position > CHARPOS that is the closest to CHARPOS. We restore
8286 that position in IT when we have scanned the entire display line
8287 without finding a match for CHARPOS and all the character
8288 positions are greater than CHARPOS. */
8289 if (it->bidi_p)
8290 {
8291 SAVE_IT (ppos_it, *it, ppos_data);
8292 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8293 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8294 SAVE_IT (ppos_it, *it, ppos_data);
8295 }
8296
8297 #define BUFFER_POS_REACHED_P() \
8298 ((op & MOVE_TO_POS) != 0 \
8299 && BUFFERP (it->object) \
8300 && (IT_CHARPOS (*it) == to_charpos \
8301 || ((!it->bidi_p \
8302 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8303 && IT_CHARPOS (*it) > to_charpos) \
8304 || (it->what == IT_COMPOSITION \
8305 && ((IT_CHARPOS (*it) > to_charpos \
8306 && to_charpos >= it->cmp_it.charpos) \
8307 || (IT_CHARPOS (*it) < to_charpos \
8308 && to_charpos <= it->cmp_it.charpos)))) \
8309 && (it->method == GET_FROM_BUFFER \
8310 || (it->method == GET_FROM_DISPLAY_VECTOR \
8311 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8312
8313 /* If there's a line-/wrap-prefix, handle it. */
8314 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8315 && it->current_y < it->last_visible_y)
8316 handle_line_prefix (it);
8317
8318 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8319 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8320
8321 while (1)
8322 {
8323 int x, i, ascent = 0, descent = 0;
8324
8325 /* Utility macro to reset an iterator with x, ascent, and descent. */
8326 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8327 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8328 (IT)->max_descent = descent)
8329
8330 /* Stop if we move beyond TO_CHARPOS (after an image or a
8331 display string or stretch glyph). */
8332 if ((op & MOVE_TO_POS) != 0
8333 && BUFFERP (it->object)
8334 && it->method == GET_FROM_BUFFER
8335 && (((!it->bidi_p
8336 /* When the iterator is at base embedding level, we
8337 are guaranteed that characters are delivered for
8338 display in strictly increasing order of their
8339 buffer positions. */
8340 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8341 && IT_CHARPOS (*it) > to_charpos)
8342 || (it->bidi_p
8343 && (prev_method == GET_FROM_IMAGE
8344 || prev_method == GET_FROM_STRETCH
8345 || prev_method == GET_FROM_STRING)
8346 /* Passed TO_CHARPOS from left to right. */
8347 && ((prev_pos < to_charpos
8348 && IT_CHARPOS (*it) > to_charpos)
8349 /* Passed TO_CHARPOS from right to left. */
8350 || (prev_pos > to_charpos
8351 && IT_CHARPOS (*it) < to_charpos)))))
8352 {
8353 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8354 {
8355 result = MOVE_POS_MATCH_OR_ZV;
8356 break;
8357 }
8358 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8359 /* If wrap_it is valid, the current position might be in a
8360 word that is wrapped. So, save the iterator in
8361 atpos_it and continue to see if wrapping happens. */
8362 SAVE_IT (atpos_it, *it, atpos_data);
8363 }
8364
8365 /* Stop when ZV reached.
8366 We used to stop here when TO_CHARPOS reached as well, but that is
8367 too soon if this glyph does not fit on this line. So we handle it
8368 explicitly below. */
8369 if (!get_next_display_element (it))
8370 {
8371 result = MOVE_POS_MATCH_OR_ZV;
8372 break;
8373 }
8374
8375 if (it->line_wrap == TRUNCATE)
8376 {
8377 if (BUFFER_POS_REACHED_P ())
8378 {
8379 result = MOVE_POS_MATCH_OR_ZV;
8380 break;
8381 }
8382 }
8383 else
8384 {
8385 if (it->line_wrap == WORD_WRAP)
8386 {
8387 if (IT_DISPLAYING_WHITESPACE (it))
8388 may_wrap = 1;
8389 else if (may_wrap)
8390 {
8391 /* We have reached a glyph that follows one or more
8392 whitespace characters. If the position is
8393 already found, we are done. */
8394 if (atpos_it.sp >= 0)
8395 {
8396 RESTORE_IT (it, &atpos_it, atpos_data);
8397 result = MOVE_POS_MATCH_OR_ZV;
8398 goto done;
8399 }
8400 if (atx_it.sp >= 0)
8401 {
8402 RESTORE_IT (it, &atx_it, atx_data);
8403 result = MOVE_X_REACHED;
8404 goto done;
8405 }
8406 /* Otherwise, we can wrap here. */
8407 SAVE_IT (wrap_it, *it, wrap_data);
8408 may_wrap = 0;
8409 }
8410 }
8411 }
8412
8413 /* Remember the line height for the current line, in case
8414 the next element doesn't fit on the line. */
8415 ascent = it->max_ascent;
8416 descent = it->max_descent;
8417
8418 /* The call to produce_glyphs will get the metrics of the
8419 display element IT is loaded with. Record the x-position
8420 before this display element, in case it doesn't fit on the
8421 line. */
8422 x = it->current_x;
8423
8424 PRODUCE_GLYPHS (it);
8425
8426 if (it->area != TEXT_AREA)
8427 {
8428 prev_method = it->method;
8429 if (it->method == GET_FROM_BUFFER)
8430 prev_pos = IT_CHARPOS (*it);
8431 set_iterator_to_next (it, 1);
8432 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8433 SET_TEXT_POS (this_line_min_pos,
8434 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8435 if (it->bidi_p
8436 && (op & MOVE_TO_POS)
8437 && IT_CHARPOS (*it) > to_charpos
8438 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8439 SAVE_IT (ppos_it, *it, ppos_data);
8440 continue;
8441 }
8442
8443 /* The number of glyphs we get back in IT->nglyphs will normally
8444 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8445 character on a terminal frame, or (iii) a line end. For the
8446 second case, IT->nglyphs - 1 padding glyphs will be present.
8447 (On X frames, there is only one glyph produced for a
8448 composite character.)
8449
8450 The behavior implemented below means, for continuation lines,
8451 that as many spaces of a TAB as fit on the current line are
8452 displayed there. For terminal frames, as many glyphs of a
8453 multi-glyph character are displayed in the current line, too.
8454 This is what the old redisplay code did, and we keep it that
8455 way. Under X, the whole shape of a complex character must
8456 fit on the line or it will be completely displayed in the
8457 next line.
8458
8459 Note that both for tabs and padding glyphs, all glyphs have
8460 the same width. */
8461 if (it->nglyphs)
8462 {
8463 /* More than one glyph or glyph doesn't fit on line. All
8464 glyphs have the same width. */
8465 int single_glyph_width = it->pixel_width / it->nglyphs;
8466 int new_x;
8467 int x_before_this_char = x;
8468 int hpos_before_this_char = it->hpos;
8469
8470 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8471 {
8472 new_x = x + single_glyph_width;
8473
8474 /* We want to leave anything reaching TO_X to the caller. */
8475 if ((op & MOVE_TO_X) && new_x > to_x)
8476 {
8477 if (BUFFER_POS_REACHED_P ())
8478 {
8479 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8480 goto buffer_pos_reached;
8481 if (atpos_it.sp < 0)
8482 {
8483 SAVE_IT (atpos_it, *it, atpos_data);
8484 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8485 }
8486 }
8487 else
8488 {
8489 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8490 {
8491 it->current_x = x;
8492 result = MOVE_X_REACHED;
8493 break;
8494 }
8495 if (atx_it.sp < 0)
8496 {
8497 SAVE_IT (atx_it, *it, atx_data);
8498 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8499 }
8500 }
8501 }
8502
8503 if (/* Lines are continued. */
8504 it->line_wrap != TRUNCATE
8505 && (/* And glyph doesn't fit on the line. */
8506 new_x > it->last_visible_x
8507 /* Or it fits exactly and we're on a window
8508 system frame. */
8509 || (new_x == it->last_visible_x
8510 && FRAME_WINDOW_P (it->f)
8511 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8512 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8513 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8514 {
8515 if (/* IT->hpos == 0 means the very first glyph
8516 doesn't fit on the line, e.g. a wide image. */
8517 it->hpos == 0
8518 || (new_x == it->last_visible_x
8519 && FRAME_WINDOW_P (it->f)))
8520 {
8521 ++it->hpos;
8522 it->current_x = new_x;
8523
8524 /* The character's last glyph just barely fits
8525 in this row. */
8526 if (i == it->nglyphs - 1)
8527 {
8528 /* If this is the destination position,
8529 return a position *before* it in this row,
8530 now that we know it fits in this row. */
8531 if (BUFFER_POS_REACHED_P ())
8532 {
8533 if (it->line_wrap != WORD_WRAP
8534 || wrap_it.sp < 0)
8535 {
8536 it->hpos = hpos_before_this_char;
8537 it->current_x = x_before_this_char;
8538 result = MOVE_POS_MATCH_OR_ZV;
8539 break;
8540 }
8541 if (it->line_wrap == WORD_WRAP
8542 && atpos_it.sp < 0)
8543 {
8544 SAVE_IT (atpos_it, *it, atpos_data);
8545 atpos_it.current_x = x_before_this_char;
8546 atpos_it.hpos = hpos_before_this_char;
8547 }
8548 }
8549
8550 prev_method = it->method;
8551 if (it->method == GET_FROM_BUFFER)
8552 prev_pos = IT_CHARPOS (*it);
8553 set_iterator_to_next (it, 1);
8554 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8555 SET_TEXT_POS (this_line_min_pos,
8556 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8557 /* On graphical terminals, newlines may
8558 "overflow" into the fringe if
8559 overflow-newline-into-fringe is non-nil.
8560 On text terminals, and on graphical
8561 terminals with no right margin, newlines
8562 may overflow into the last glyph on the
8563 display line.*/
8564 if (!FRAME_WINDOW_P (it->f)
8565 || ((it->bidi_p
8566 && it->bidi_it.paragraph_dir == R2L)
8567 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8568 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8569 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8570 {
8571 if (!get_next_display_element (it))
8572 {
8573 result = MOVE_POS_MATCH_OR_ZV;
8574 break;
8575 }
8576 if (BUFFER_POS_REACHED_P ())
8577 {
8578 if (ITERATOR_AT_END_OF_LINE_P (it))
8579 result = MOVE_POS_MATCH_OR_ZV;
8580 else
8581 result = MOVE_LINE_CONTINUED;
8582 break;
8583 }
8584 if (ITERATOR_AT_END_OF_LINE_P (it)
8585 && (it->line_wrap != WORD_WRAP
8586 || wrap_it.sp < 0))
8587 {
8588 result = MOVE_NEWLINE_OR_CR;
8589 break;
8590 }
8591 }
8592 }
8593 }
8594 else
8595 IT_RESET_X_ASCENT_DESCENT (it);
8596
8597 if (wrap_it.sp >= 0)
8598 {
8599 RESTORE_IT (it, &wrap_it, wrap_data);
8600 atpos_it.sp = -1;
8601 atx_it.sp = -1;
8602 }
8603
8604 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8605 IT_CHARPOS (*it)));
8606 result = MOVE_LINE_CONTINUED;
8607 break;
8608 }
8609
8610 if (BUFFER_POS_REACHED_P ())
8611 {
8612 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8613 goto buffer_pos_reached;
8614 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8615 {
8616 SAVE_IT (atpos_it, *it, atpos_data);
8617 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8618 }
8619 }
8620
8621 if (new_x > it->first_visible_x)
8622 {
8623 /* Glyph is visible. Increment number of glyphs that
8624 would be displayed. */
8625 ++it->hpos;
8626 }
8627 }
8628
8629 if (result != MOVE_UNDEFINED)
8630 break;
8631 }
8632 else if (BUFFER_POS_REACHED_P ())
8633 {
8634 buffer_pos_reached:
8635 IT_RESET_X_ASCENT_DESCENT (it);
8636 result = MOVE_POS_MATCH_OR_ZV;
8637 break;
8638 }
8639 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8640 {
8641 /* Stop when TO_X specified and reached. This check is
8642 necessary here because of lines consisting of a line end,
8643 only. The line end will not produce any glyphs and we
8644 would never get MOVE_X_REACHED. */
8645 eassert (it->nglyphs == 0);
8646 result = MOVE_X_REACHED;
8647 break;
8648 }
8649
8650 /* Is this a line end? If yes, we're done. */
8651 if (ITERATOR_AT_END_OF_LINE_P (it))
8652 {
8653 /* If we are past TO_CHARPOS, but never saw any character
8654 positions smaller than TO_CHARPOS, return
8655 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8656 did. */
8657 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8658 {
8659 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8660 {
8661 if (IT_CHARPOS (ppos_it) < ZV)
8662 {
8663 RESTORE_IT (it, &ppos_it, ppos_data);
8664 result = MOVE_POS_MATCH_OR_ZV;
8665 }
8666 else
8667 goto buffer_pos_reached;
8668 }
8669 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8670 && IT_CHARPOS (*it) > to_charpos)
8671 goto buffer_pos_reached;
8672 else
8673 result = MOVE_NEWLINE_OR_CR;
8674 }
8675 else
8676 result = MOVE_NEWLINE_OR_CR;
8677 break;
8678 }
8679
8680 prev_method = it->method;
8681 if (it->method == GET_FROM_BUFFER)
8682 prev_pos = IT_CHARPOS (*it);
8683 /* The current display element has been consumed. Advance
8684 to the next. */
8685 set_iterator_to_next (it, 1);
8686 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8687 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8688 if (IT_CHARPOS (*it) < to_charpos)
8689 saw_smaller_pos = 1;
8690 if (it->bidi_p
8691 && (op & MOVE_TO_POS)
8692 && IT_CHARPOS (*it) >= to_charpos
8693 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8694 SAVE_IT (ppos_it, *it, ppos_data);
8695
8696 /* Stop if lines are truncated and IT's current x-position is
8697 past the right edge of the window now. */
8698 if (it->line_wrap == TRUNCATE
8699 && it->current_x >= it->last_visible_x)
8700 {
8701 if (!FRAME_WINDOW_P (it->f)
8702 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8703 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8704 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8705 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8706 {
8707 int at_eob_p = 0;
8708
8709 if ((at_eob_p = !get_next_display_element (it))
8710 || BUFFER_POS_REACHED_P ()
8711 /* If we are past TO_CHARPOS, but never saw any
8712 character positions smaller than TO_CHARPOS,
8713 return MOVE_POS_MATCH_OR_ZV, like the
8714 unidirectional display did. */
8715 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8716 && !saw_smaller_pos
8717 && IT_CHARPOS (*it) > to_charpos))
8718 {
8719 if (it->bidi_p
8720 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8721 RESTORE_IT (it, &ppos_it, ppos_data);
8722 result = MOVE_POS_MATCH_OR_ZV;
8723 break;
8724 }
8725 if (ITERATOR_AT_END_OF_LINE_P (it))
8726 {
8727 result = MOVE_NEWLINE_OR_CR;
8728 break;
8729 }
8730 }
8731 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8732 && !saw_smaller_pos
8733 && IT_CHARPOS (*it) > to_charpos)
8734 {
8735 if (IT_CHARPOS (ppos_it) < ZV)
8736 RESTORE_IT (it, &ppos_it, ppos_data);
8737 result = MOVE_POS_MATCH_OR_ZV;
8738 break;
8739 }
8740 result = MOVE_LINE_TRUNCATED;
8741 break;
8742 }
8743 #undef IT_RESET_X_ASCENT_DESCENT
8744 }
8745
8746 #undef BUFFER_POS_REACHED_P
8747
8748 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8749 restore the saved iterator. */
8750 if (atpos_it.sp >= 0)
8751 RESTORE_IT (it, &atpos_it, atpos_data);
8752 else if (atx_it.sp >= 0)
8753 RESTORE_IT (it, &atx_it, atx_data);
8754
8755 done:
8756
8757 if (atpos_data)
8758 bidi_unshelve_cache (atpos_data, 1);
8759 if (atx_data)
8760 bidi_unshelve_cache (atx_data, 1);
8761 if (wrap_data)
8762 bidi_unshelve_cache (wrap_data, 1);
8763 if (ppos_data)
8764 bidi_unshelve_cache (ppos_data, 1);
8765
8766 /* Restore the iterator settings altered at the beginning of this
8767 function. */
8768 it->glyph_row = saved_glyph_row;
8769 return result;
8770 }
8771
8772 /* For external use. */
8773 void
8774 move_it_in_display_line (struct it *it,
8775 ptrdiff_t to_charpos, int to_x,
8776 enum move_operation_enum op)
8777 {
8778 if (it->line_wrap == WORD_WRAP
8779 && (op & MOVE_TO_X))
8780 {
8781 struct it save_it;
8782 void *save_data = NULL;
8783 int skip;
8784
8785 SAVE_IT (save_it, *it, save_data);
8786 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8787 /* When word-wrap is on, TO_X may lie past the end
8788 of a wrapped line. Then it->current is the
8789 character on the next line, so backtrack to the
8790 space before the wrap point. */
8791 if (skip == MOVE_LINE_CONTINUED)
8792 {
8793 int prev_x = max (it->current_x - 1, 0);
8794 RESTORE_IT (it, &save_it, save_data);
8795 move_it_in_display_line_to
8796 (it, -1, prev_x, MOVE_TO_X);
8797 }
8798 else
8799 bidi_unshelve_cache (save_data, 1);
8800 }
8801 else
8802 move_it_in_display_line_to (it, to_charpos, to_x, op);
8803 }
8804
8805
8806 /* Move IT forward until it satisfies one or more of the criteria in
8807 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8808
8809 OP is a bit-mask that specifies where to stop, and in particular,
8810 which of those four position arguments makes a difference. See the
8811 description of enum move_operation_enum.
8812
8813 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8814 screen line, this function will set IT to the next position that is
8815 displayed to the right of TO_CHARPOS on the screen. */
8816
8817 void
8818 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8819 {
8820 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8821 int line_height, line_start_x = 0, reached = 0;
8822 void *backup_data = NULL;
8823
8824 for (;;)
8825 {
8826 if (op & MOVE_TO_VPOS)
8827 {
8828 /* If no TO_CHARPOS and no TO_X specified, stop at the
8829 start of the line TO_VPOS. */
8830 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8831 {
8832 if (it->vpos == to_vpos)
8833 {
8834 reached = 1;
8835 break;
8836 }
8837 else
8838 skip = move_it_in_display_line_to (it, -1, -1, 0);
8839 }
8840 else
8841 {
8842 /* TO_VPOS >= 0 means stop at TO_X in the line at
8843 TO_VPOS, or at TO_POS, whichever comes first. */
8844 if (it->vpos == to_vpos)
8845 {
8846 reached = 2;
8847 break;
8848 }
8849
8850 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8851
8852 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8853 {
8854 reached = 3;
8855 break;
8856 }
8857 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8858 {
8859 /* We have reached TO_X but not in the line we want. */
8860 skip = move_it_in_display_line_to (it, to_charpos,
8861 -1, MOVE_TO_POS);
8862 if (skip == MOVE_POS_MATCH_OR_ZV)
8863 {
8864 reached = 4;
8865 break;
8866 }
8867 }
8868 }
8869 }
8870 else if (op & MOVE_TO_Y)
8871 {
8872 struct it it_backup;
8873
8874 if (it->line_wrap == WORD_WRAP)
8875 SAVE_IT (it_backup, *it, backup_data);
8876
8877 /* TO_Y specified means stop at TO_X in the line containing
8878 TO_Y---or at TO_CHARPOS if this is reached first. The
8879 problem is that we can't really tell whether the line
8880 contains TO_Y before we have completely scanned it, and
8881 this may skip past TO_X. What we do is to first scan to
8882 TO_X.
8883
8884 If TO_X is not specified, use a TO_X of zero. The reason
8885 is to make the outcome of this function more predictable.
8886 If we didn't use TO_X == 0, we would stop at the end of
8887 the line which is probably not what a caller would expect
8888 to happen. */
8889 skip = move_it_in_display_line_to
8890 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8891 (MOVE_TO_X | (op & MOVE_TO_POS)));
8892
8893 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8894 if (skip == MOVE_POS_MATCH_OR_ZV)
8895 reached = 5;
8896 else if (skip == MOVE_X_REACHED)
8897 {
8898 /* If TO_X was reached, we want to know whether TO_Y is
8899 in the line. We know this is the case if the already
8900 scanned glyphs make the line tall enough. Otherwise,
8901 we must check by scanning the rest of the line. */
8902 line_height = it->max_ascent + it->max_descent;
8903 if (to_y >= it->current_y
8904 && to_y < it->current_y + line_height)
8905 {
8906 reached = 6;
8907 break;
8908 }
8909 SAVE_IT (it_backup, *it, backup_data);
8910 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8911 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8912 op & MOVE_TO_POS);
8913 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8914 line_height = it->max_ascent + it->max_descent;
8915 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8916
8917 if (to_y >= it->current_y
8918 && to_y < it->current_y + line_height)
8919 {
8920 /* If TO_Y is in this line and TO_X was reached
8921 above, we scanned too far. We have to restore
8922 IT's settings to the ones before skipping. But
8923 keep the more accurate values of max_ascent and
8924 max_descent we've found while skipping the rest
8925 of the line, for the sake of callers, such as
8926 pos_visible_p, that need to know the line
8927 height. */
8928 int max_ascent = it->max_ascent;
8929 int max_descent = it->max_descent;
8930
8931 RESTORE_IT (it, &it_backup, backup_data);
8932 it->max_ascent = max_ascent;
8933 it->max_descent = max_descent;
8934 reached = 6;
8935 }
8936 else
8937 {
8938 skip = skip2;
8939 if (skip == MOVE_POS_MATCH_OR_ZV)
8940 reached = 7;
8941 }
8942 }
8943 else
8944 {
8945 /* Check whether TO_Y is in this line. */
8946 line_height = it->max_ascent + it->max_descent;
8947 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8948
8949 if (to_y >= it->current_y
8950 && to_y < it->current_y + line_height)
8951 {
8952 /* When word-wrap is on, TO_X may lie past the end
8953 of a wrapped line. Then it->current is the
8954 character on the next line, so backtrack to the
8955 space before the wrap point. */
8956 if (skip == MOVE_LINE_CONTINUED
8957 && it->line_wrap == WORD_WRAP)
8958 {
8959 int prev_x = max (it->current_x - 1, 0);
8960 RESTORE_IT (it, &it_backup, backup_data);
8961 skip = move_it_in_display_line_to
8962 (it, -1, prev_x, MOVE_TO_X);
8963 }
8964 reached = 6;
8965 }
8966 }
8967
8968 if (reached)
8969 break;
8970 }
8971 else if (BUFFERP (it->object)
8972 && (it->method == GET_FROM_BUFFER
8973 || it->method == GET_FROM_STRETCH)
8974 && IT_CHARPOS (*it) >= to_charpos
8975 /* Under bidi iteration, a call to set_iterator_to_next
8976 can scan far beyond to_charpos if the initial
8977 portion of the next line needs to be reordered. In
8978 that case, give move_it_in_display_line_to another
8979 chance below. */
8980 && !(it->bidi_p
8981 && it->bidi_it.scan_dir == -1))
8982 skip = MOVE_POS_MATCH_OR_ZV;
8983 else
8984 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8985
8986 switch (skip)
8987 {
8988 case MOVE_POS_MATCH_OR_ZV:
8989 reached = 8;
8990 goto out;
8991
8992 case MOVE_NEWLINE_OR_CR:
8993 set_iterator_to_next (it, 1);
8994 it->continuation_lines_width = 0;
8995 break;
8996
8997 case MOVE_LINE_TRUNCATED:
8998 it->continuation_lines_width = 0;
8999 reseat_at_next_visible_line_start (it, 0);
9000 if ((op & MOVE_TO_POS) != 0
9001 && IT_CHARPOS (*it) > to_charpos)
9002 {
9003 reached = 9;
9004 goto out;
9005 }
9006 break;
9007
9008 case MOVE_LINE_CONTINUED:
9009 /* For continued lines ending in a tab, some of the glyphs
9010 associated with the tab are displayed on the current
9011 line. Since it->current_x does not include these glyphs,
9012 we use it->last_visible_x instead. */
9013 if (it->c == '\t')
9014 {
9015 it->continuation_lines_width += it->last_visible_x;
9016 /* When moving by vpos, ensure that the iterator really
9017 advances to the next line (bug#847, bug#969). Fixme:
9018 do we need to do this in other circumstances? */
9019 if (it->current_x != it->last_visible_x
9020 && (op & MOVE_TO_VPOS)
9021 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9022 {
9023 line_start_x = it->current_x + it->pixel_width
9024 - it->last_visible_x;
9025 set_iterator_to_next (it, 0);
9026 }
9027 }
9028 else
9029 it->continuation_lines_width += it->current_x;
9030 break;
9031
9032 default:
9033 emacs_abort ();
9034 }
9035
9036 /* Reset/increment for the next run. */
9037 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9038 it->current_x = line_start_x;
9039 line_start_x = 0;
9040 it->hpos = 0;
9041 it->current_y += it->max_ascent + it->max_descent;
9042 ++it->vpos;
9043 last_height = it->max_ascent + it->max_descent;
9044 it->max_ascent = it->max_descent = 0;
9045 }
9046
9047 out:
9048
9049 /* On text terminals, we may stop at the end of a line in the middle
9050 of a multi-character glyph. If the glyph itself is continued,
9051 i.e. it is actually displayed on the next line, don't treat this
9052 stopping point as valid; move to the next line instead (unless
9053 that brings us offscreen). */
9054 if (!FRAME_WINDOW_P (it->f)
9055 && op & MOVE_TO_POS
9056 && IT_CHARPOS (*it) == to_charpos
9057 && it->what == IT_CHARACTER
9058 && it->nglyphs > 1
9059 && it->line_wrap == WINDOW_WRAP
9060 && it->current_x == it->last_visible_x - 1
9061 && it->c != '\n'
9062 && it->c != '\t'
9063 && it->vpos < XFASTINT (it->w->window_end_vpos))
9064 {
9065 it->continuation_lines_width += it->current_x;
9066 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9067 it->current_y += it->max_ascent + it->max_descent;
9068 ++it->vpos;
9069 last_height = it->max_ascent + it->max_descent;
9070 }
9071
9072 if (backup_data)
9073 bidi_unshelve_cache (backup_data, 1);
9074
9075 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9076 }
9077
9078
9079 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9080
9081 If DY > 0, move IT backward at least that many pixels. DY = 0
9082 means move IT backward to the preceding line start or BEGV. This
9083 function may move over more than DY pixels if IT->current_y - DY
9084 ends up in the middle of a line; in this case IT->current_y will be
9085 set to the top of the line moved to. */
9086
9087 void
9088 move_it_vertically_backward (struct it *it, int dy)
9089 {
9090 int nlines, h;
9091 struct it it2, it3;
9092 void *it2data = NULL, *it3data = NULL;
9093 ptrdiff_t start_pos;
9094 int nchars_per_row
9095 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9096 ptrdiff_t pos_limit;
9097
9098 move_further_back:
9099 eassert (dy >= 0);
9100
9101 start_pos = IT_CHARPOS (*it);
9102
9103 /* Estimate how many newlines we must move back. */
9104 nlines = max (1, dy / default_line_pixel_height (it->w));
9105 if (it->line_wrap == TRUNCATE)
9106 pos_limit = BEGV;
9107 else
9108 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9109
9110 /* Set the iterator's position that many lines back. But don't go
9111 back more than NLINES full screen lines -- this wins a day with
9112 buffers which have very long lines. */
9113 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9114 back_to_previous_visible_line_start (it);
9115
9116 /* Reseat the iterator here. When moving backward, we don't want
9117 reseat to skip forward over invisible text, set up the iterator
9118 to deliver from overlay strings at the new position etc. So,
9119 use reseat_1 here. */
9120 reseat_1 (it, it->current.pos, 1);
9121
9122 /* We are now surely at a line start. */
9123 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9124 reordering is in effect. */
9125 it->continuation_lines_width = 0;
9126
9127 /* Move forward and see what y-distance we moved. First move to the
9128 start of the next line so that we get its height. We need this
9129 height to be able to tell whether we reached the specified
9130 y-distance. */
9131 SAVE_IT (it2, *it, it2data);
9132 it2.max_ascent = it2.max_descent = 0;
9133 do
9134 {
9135 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9136 MOVE_TO_POS | MOVE_TO_VPOS);
9137 }
9138 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9139 /* If we are in a display string which starts at START_POS,
9140 and that display string includes a newline, and we are
9141 right after that newline (i.e. at the beginning of a
9142 display line), exit the loop, because otherwise we will
9143 infloop, since move_it_to will see that it is already at
9144 START_POS and will not move. */
9145 || (it2.method == GET_FROM_STRING
9146 && IT_CHARPOS (it2) == start_pos
9147 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9148 eassert (IT_CHARPOS (*it) >= BEGV);
9149 SAVE_IT (it3, it2, it3data);
9150
9151 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9152 eassert (IT_CHARPOS (*it) >= BEGV);
9153 /* H is the actual vertical distance from the position in *IT
9154 and the starting position. */
9155 h = it2.current_y - it->current_y;
9156 /* NLINES is the distance in number of lines. */
9157 nlines = it2.vpos - it->vpos;
9158
9159 /* Correct IT's y and vpos position
9160 so that they are relative to the starting point. */
9161 it->vpos -= nlines;
9162 it->current_y -= h;
9163
9164 if (dy == 0)
9165 {
9166 /* DY == 0 means move to the start of the screen line. The
9167 value of nlines is > 0 if continuation lines were involved,
9168 or if the original IT position was at start of a line. */
9169 RESTORE_IT (it, it, it2data);
9170 if (nlines > 0)
9171 move_it_by_lines (it, nlines);
9172 /* The above code moves us to some position NLINES down,
9173 usually to its first glyph (leftmost in an L2R line), but
9174 that's not necessarily the start of the line, under bidi
9175 reordering. We want to get to the character position
9176 that is immediately after the newline of the previous
9177 line. */
9178 if (it->bidi_p
9179 && !it->continuation_lines_width
9180 && !STRINGP (it->string)
9181 && IT_CHARPOS (*it) > BEGV
9182 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9183 {
9184 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9185
9186 DEC_BOTH (cp, bp);
9187 cp = find_newline_no_quit (cp, bp, -1, NULL);
9188 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9189 }
9190 bidi_unshelve_cache (it3data, 1);
9191 }
9192 else
9193 {
9194 /* The y-position we try to reach, relative to *IT.
9195 Note that H has been subtracted in front of the if-statement. */
9196 int target_y = it->current_y + h - dy;
9197 int y0 = it3.current_y;
9198 int y1;
9199 int line_height;
9200
9201 RESTORE_IT (&it3, &it3, it3data);
9202 y1 = line_bottom_y (&it3);
9203 line_height = y1 - y0;
9204 RESTORE_IT (it, it, it2data);
9205 /* If we did not reach target_y, try to move further backward if
9206 we can. If we moved too far backward, try to move forward. */
9207 if (target_y < it->current_y
9208 /* This is heuristic. In a window that's 3 lines high, with
9209 a line height of 13 pixels each, recentering with point
9210 on the bottom line will try to move -39/2 = 19 pixels
9211 backward. Try to avoid moving into the first line. */
9212 && (it->current_y - target_y
9213 > min (window_box_height (it->w), line_height * 2 / 3))
9214 && IT_CHARPOS (*it) > BEGV)
9215 {
9216 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9217 target_y - it->current_y));
9218 dy = it->current_y - target_y;
9219 goto move_further_back;
9220 }
9221 else if (target_y >= it->current_y + line_height
9222 && IT_CHARPOS (*it) < ZV)
9223 {
9224 /* Should move forward by at least one line, maybe more.
9225
9226 Note: Calling move_it_by_lines can be expensive on
9227 terminal frames, where compute_motion is used (via
9228 vmotion) to do the job, when there are very long lines
9229 and truncate-lines is nil. That's the reason for
9230 treating terminal frames specially here. */
9231
9232 if (!FRAME_WINDOW_P (it->f))
9233 move_it_vertically (it, target_y - (it->current_y + line_height));
9234 else
9235 {
9236 do
9237 {
9238 move_it_by_lines (it, 1);
9239 }
9240 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9241 }
9242 }
9243 }
9244 }
9245
9246
9247 /* Move IT by a specified amount of pixel lines DY. DY negative means
9248 move backwards. DY = 0 means move to start of screen line. At the
9249 end, IT will be on the start of a screen line. */
9250
9251 void
9252 move_it_vertically (struct it *it, int dy)
9253 {
9254 if (dy <= 0)
9255 move_it_vertically_backward (it, -dy);
9256 else
9257 {
9258 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9259 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9260 MOVE_TO_POS | MOVE_TO_Y);
9261 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9262
9263 /* If buffer ends in ZV without a newline, move to the start of
9264 the line to satisfy the post-condition. */
9265 if (IT_CHARPOS (*it) == ZV
9266 && ZV > BEGV
9267 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9268 move_it_by_lines (it, 0);
9269 }
9270 }
9271
9272
9273 /* Move iterator IT past the end of the text line it is in. */
9274
9275 void
9276 move_it_past_eol (struct it *it)
9277 {
9278 enum move_it_result rc;
9279
9280 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9281 if (rc == MOVE_NEWLINE_OR_CR)
9282 set_iterator_to_next (it, 0);
9283 }
9284
9285
9286 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9287 negative means move up. DVPOS == 0 means move to the start of the
9288 screen line.
9289
9290 Optimization idea: If we would know that IT->f doesn't use
9291 a face with proportional font, we could be faster for
9292 truncate-lines nil. */
9293
9294 void
9295 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9296 {
9297
9298 /* The commented-out optimization uses vmotion on terminals. This
9299 gives bad results, because elements like it->what, on which
9300 callers such as pos_visible_p rely, aren't updated. */
9301 /* struct position pos;
9302 if (!FRAME_WINDOW_P (it->f))
9303 {
9304 struct text_pos textpos;
9305
9306 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9307 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9308 reseat (it, textpos, 1);
9309 it->vpos += pos.vpos;
9310 it->current_y += pos.vpos;
9311 }
9312 else */
9313
9314 if (dvpos == 0)
9315 {
9316 /* DVPOS == 0 means move to the start of the screen line. */
9317 move_it_vertically_backward (it, 0);
9318 /* Let next call to line_bottom_y calculate real line height */
9319 last_height = 0;
9320 }
9321 else if (dvpos > 0)
9322 {
9323 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9324 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9325 {
9326 /* Only move to the next buffer position if we ended up in a
9327 string from display property, not in an overlay string
9328 (before-string or after-string). That is because the
9329 latter don't conceal the underlying buffer position, so
9330 we can ask to move the iterator to the exact position we
9331 are interested in. Note that, even if we are already at
9332 IT_CHARPOS (*it), the call below is not a no-op, as it
9333 will detect that we are at the end of the string, pop the
9334 iterator, and compute it->current_x and it->hpos
9335 correctly. */
9336 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9337 -1, -1, -1, MOVE_TO_POS);
9338 }
9339 }
9340 else
9341 {
9342 struct it it2;
9343 void *it2data = NULL;
9344 ptrdiff_t start_charpos, i;
9345 int nchars_per_row
9346 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9347 ptrdiff_t pos_limit;
9348
9349 /* Start at the beginning of the screen line containing IT's
9350 position. This may actually move vertically backwards,
9351 in case of overlays, so adjust dvpos accordingly. */
9352 dvpos += it->vpos;
9353 move_it_vertically_backward (it, 0);
9354 dvpos -= it->vpos;
9355
9356 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9357 screen lines, and reseat the iterator there. */
9358 start_charpos = IT_CHARPOS (*it);
9359 if (it->line_wrap == TRUNCATE)
9360 pos_limit = BEGV;
9361 else
9362 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9363 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9364 back_to_previous_visible_line_start (it);
9365 reseat (it, it->current.pos, 1);
9366
9367 /* Move further back if we end up in a string or an image. */
9368 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9369 {
9370 /* First try to move to start of display line. */
9371 dvpos += it->vpos;
9372 move_it_vertically_backward (it, 0);
9373 dvpos -= it->vpos;
9374 if (IT_POS_VALID_AFTER_MOVE_P (it))
9375 break;
9376 /* If start of line is still in string or image,
9377 move further back. */
9378 back_to_previous_visible_line_start (it);
9379 reseat (it, it->current.pos, 1);
9380 dvpos--;
9381 }
9382
9383 it->current_x = it->hpos = 0;
9384
9385 /* Above call may have moved too far if continuation lines
9386 are involved. Scan forward and see if it did. */
9387 SAVE_IT (it2, *it, it2data);
9388 it2.vpos = it2.current_y = 0;
9389 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9390 it->vpos -= it2.vpos;
9391 it->current_y -= it2.current_y;
9392 it->current_x = it->hpos = 0;
9393
9394 /* If we moved too far back, move IT some lines forward. */
9395 if (it2.vpos > -dvpos)
9396 {
9397 int delta = it2.vpos + dvpos;
9398
9399 RESTORE_IT (&it2, &it2, it2data);
9400 SAVE_IT (it2, *it, it2data);
9401 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9402 /* Move back again if we got too far ahead. */
9403 if (IT_CHARPOS (*it) >= start_charpos)
9404 RESTORE_IT (it, &it2, it2data);
9405 else
9406 bidi_unshelve_cache (it2data, 1);
9407 }
9408 else
9409 RESTORE_IT (it, it, it2data);
9410 }
9411 }
9412
9413 /* Return 1 if IT points into the middle of a display vector. */
9414
9415 int
9416 in_display_vector_p (struct it *it)
9417 {
9418 return (it->method == GET_FROM_DISPLAY_VECTOR
9419 && it->current.dpvec_index > 0
9420 && it->dpvec + it->current.dpvec_index != it->dpend);
9421 }
9422
9423 \f
9424 /***********************************************************************
9425 Messages
9426 ***********************************************************************/
9427
9428
9429 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9430 to *Messages*. */
9431
9432 void
9433 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9434 {
9435 Lisp_Object args[3];
9436 Lisp_Object msg, fmt;
9437 char *buffer;
9438 ptrdiff_t len;
9439 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9440 USE_SAFE_ALLOCA;
9441
9442 fmt = msg = Qnil;
9443 GCPRO4 (fmt, msg, arg1, arg2);
9444
9445 args[0] = fmt = build_string (format);
9446 args[1] = arg1;
9447 args[2] = arg2;
9448 msg = Fformat (3, args);
9449
9450 len = SBYTES (msg) + 1;
9451 buffer = SAFE_ALLOCA (len);
9452 memcpy (buffer, SDATA (msg), len);
9453
9454 message_dolog (buffer, len - 1, 1, 0);
9455 SAFE_FREE ();
9456
9457 UNGCPRO;
9458 }
9459
9460
9461 /* Output a newline in the *Messages* buffer if "needs" one. */
9462
9463 void
9464 message_log_maybe_newline (void)
9465 {
9466 if (message_log_need_newline)
9467 message_dolog ("", 0, 1, 0);
9468 }
9469
9470
9471 /* Add a string M of length NBYTES to the message log, optionally
9472 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9473 true, means interpret the contents of M as multibyte. This
9474 function calls low-level routines in order to bypass text property
9475 hooks, etc. which might not be safe to run.
9476
9477 This may GC (insert may run before/after change hooks),
9478 so the buffer M must NOT point to a Lisp string. */
9479
9480 void
9481 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9482 {
9483 const unsigned char *msg = (const unsigned char *) m;
9484
9485 if (!NILP (Vmemory_full))
9486 return;
9487
9488 if (!NILP (Vmessage_log_max))
9489 {
9490 struct buffer *oldbuf;
9491 Lisp_Object oldpoint, oldbegv, oldzv;
9492 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9493 ptrdiff_t point_at_end = 0;
9494 ptrdiff_t zv_at_end = 0;
9495 Lisp_Object old_deactivate_mark;
9496 bool shown;
9497 struct gcpro gcpro1;
9498
9499 old_deactivate_mark = Vdeactivate_mark;
9500 oldbuf = current_buffer;
9501 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9502 bset_undo_list (current_buffer, Qt);
9503
9504 oldpoint = message_dolog_marker1;
9505 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9506 oldbegv = message_dolog_marker2;
9507 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9508 oldzv = message_dolog_marker3;
9509 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9510 GCPRO1 (old_deactivate_mark);
9511
9512 if (PT == Z)
9513 point_at_end = 1;
9514 if (ZV == Z)
9515 zv_at_end = 1;
9516
9517 BEGV = BEG;
9518 BEGV_BYTE = BEG_BYTE;
9519 ZV = Z;
9520 ZV_BYTE = Z_BYTE;
9521 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9522
9523 /* Insert the string--maybe converting multibyte to single byte
9524 or vice versa, so that all the text fits the buffer. */
9525 if (multibyte
9526 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9527 {
9528 ptrdiff_t i;
9529 int c, char_bytes;
9530 char work[1];
9531
9532 /* Convert a multibyte string to single-byte
9533 for the *Message* buffer. */
9534 for (i = 0; i < nbytes; i += char_bytes)
9535 {
9536 c = string_char_and_length (msg + i, &char_bytes);
9537 work[0] = (ASCII_CHAR_P (c)
9538 ? c
9539 : multibyte_char_to_unibyte (c));
9540 insert_1_both (work, 1, 1, 1, 0, 0);
9541 }
9542 }
9543 else if (! multibyte
9544 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9545 {
9546 ptrdiff_t i;
9547 int c, char_bytes;
9548 unsigned char str[MAX_MULTIBYTE_LENGTH];
9549 /* Convert a single-byte string to multibyte
9550 for the *Message* buffer. */
9551 for (i = 0; i < nbytes; i++)
9552 {
9553 c = msg[i];
9554 MAKE_CHAR_MULTIBYTE (c);
9555 char_bytes = CHAR_STRING (c, str);
9556 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9557 }
9558 }
9559 else if (nbytes)
9560 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9561
9562 if (nlflag)
9563 {
9564 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9565 printmax_t dups;
9566
9567 insert_1_both ("\n", 1, 1, 1, 0, 0);
9568
9569 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9570 this_bol = PT;
9571 this_bol_byte = PT_BYTE;
9572
9573 /* See if this line duplicates the previous one.
9574 If so, combine duplicates. */
9575 if (this_bol > BEG)
9576 {
9577 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9578 prev_bol = PT;
9579 prev_bol_byte = PT_BYTE;
9580
9581 dups = message_log_check_duplicate (prev_bol_byte,
9582 this_bol_byte);
9583 if (dups)
9584 {
9585 del_range_both (prev_bol, prev_bol_byte,
9586 this_bol, this_bol_byte, 0);
9587 if (dups > 1)
9588 {
9589 char dupstr[sizeof " [ times]"
9590 + INT_STRLEN_BOUND (printmax_t)];
9591
9592 /* If you change this format, don't forget to also
9593 change message_log_check_duplicate. */
9594 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9595 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9596 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9597 }
9598 }
9599 }
9600
9601 /* If we have more than the desired maximum number of lines
9602 in the *Messages* buffer now, delete the oldest ones.
9603 This is safe because we don't have undo in this buffer. */
9604
9605 if (NATNUMP (Vmessage_log_max))
9606 {
9607 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9608 -XFASTINT (Vmessage_log_max) - 1, 0);
9609 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9610 }
9611 }
9612 BEGV = marker_position (oldbegv);
9613 BEGV_BYTE = marker_byte_position (oldbegv);
9614
9615 if (zv_at_end)
9616 {
9617 ZV = Z;
9618 ZV_BYTE = Z_BYTE;
9619 }
9620 else
9621 {
9622 ZV = marker_position (oldzv);
9623 ZV_BYTE = marker_byte_position (oldzv);
9624 }
9625
9626 if (point_at_end)
9627 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9628 else
9629 /* We can't do Fgoto_char (oldpoint) because it will run some
9630 Lisp code. */
9631 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9632 marker_byte_position (oldpoint));
9633
9634 UNGCPRO;
9635 unchain_marker (XMARKER (oldpoint));
9636 unchain_marker (XMARKER (oldbegv));
9637 unchain_marker (XMARKER (oldzv));
9638
9639 shown = buffer_window_count (current_buffer) > 0;
9640 set_buffer_internal (oldbuf);
9641 /* We called insert_1_both above with its 5th argument (PREPARE)
9642 zero, which prevents insert_1_both from calling
9643 prepare_to_modify_buffer, which in turns prevents us from
9644 incrementing windows_or_buffers_changed even if *Messages* is
9645 shown in some window. So we must manually incrementing
9646 windows_or_buffers_changed here to make up for that. */
9647 if (shown)
9648 windows_or_buffers_changed++;
9649 else
9650 windows_or_buffers_changed = old_windows_or_buffers_changed;
9651 message_log_need_newline = !nlflag;
9652 Vdeactivate_mark = old_deactivate_mark;
9653 }
9654 }
9655
9656
9657 /* We are at the end of the buffer after just having inserted a newline.
9658 (Note: We depend on the fact we won't be crossing the gap.)
9659 Check to see if the most recent message looks a lot like the previous one.
9660 Return 0 if different, 1 if the new one should just replace it, or a
9661 value N > 1 if we should also append " [N times]". */
9662
9663 static intmax_t
9664 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9665 {
9666 ptrdiff_t i;
9667 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9668 int seen_dots = 0;
9669 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9670 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9671
9672 for (i = 0; i < len; i++)
9673 {
9674 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9675 seen_dots = 1;
9676 if (p1[i] != p2[i])
9677 return seen_dots;
9678 }
9679 p1 += len;
9680 if (*p1 == '\n')
9681 return 2;
9682 if (*p1++ == ' ' && *p1++ == '[')
9683 {
9684 char *pend;
9685 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9686 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9687 return n + 1;
9688 }
9689 return 0;
9690 }
9691 \f
9692
9693 /* Display an echo area message M with a specified length of NBYTES
9694 bytes. The string may include null characters. If M is not a
9695 string, clear out any existing message, and let the mini-buffer
9696 text show through.
9697
9698 This function cancels echoing. */
9699
9700 void
9701 message3 (Lisp_Object m)
9702 {
9703 struct gcpro gcpro1;
9704
9705 GCPRO1 (m);
9706 clear_message (1,1);
9707 cancel_echoing ();
9708
9709 /* First flush out any partial line written with print. */
9710 message_log_maybe_newline ();
9711 if (STRINGP (m))
9712 {
9713 ptrdiff_t nbytes = SBYTES (m);
9714 bool multibyte = STRING_MULTIBYTE (m);
9715 USE_SAFE_ALLOCA;
9716 char *buffer = SAFE_ALLOCA (nbytes);
9717 memcpy (buffer, SDATA (m), nbytes);
9718 message_dolog (buffer, nbytes, 1, multibyte);
9719 SAFE_FREE ();
9720 }
9721 message3_nolog (m);
9722
9723 UNGCPRO;
9724 }
9725
9726
9727 /* The non-logging version of message3.
9728 This does not cancel echoing, because it is used for echoing.
9729 Perhaps we need to make a separate function for echoing
9730 and make this cancel echoing. */
9731
9732 void
9733 message3_nolog (Lisp_Object m)
9734 {
9735 struct frame *sf = SELECTED_FRAME ();
9736
9737 if (FRAME_INITIAL_P (sf))
9738 {
9739 if (noninteractive_need_newline)
9740 putc ('\n', stderr);
9741 noninteractive_need_newline = 0;
9742 if (STRINGP (m))
9743 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9744 if (cursor_in_echo_area == 0)
9745 fprintf (stderr, "\n");
9746 fflush (stderr);
9747 }
9748 /* Error messages get reported properly by cmd_error, so this must be just an
9749 informative message; if the frame hasn't really been initialized yet, just
9750 toss it. */
9751 else if (INTERACTIVE && sf->glyphs_initialized_p)
9752 {
9753 /* Get the frame containing the mini-buffer
9754 that the selected frame is using. */
9755 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9756 Lisp_Object frame = XWINDOW (mini_window)->frame;
9757 struct frame *f = XFRAME (frame);
9758
9759 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9760 Fmake_frame_visible (frame);
9761
9762 if (STRINGP (m) && SCHARS (m) > 0)
9763 {
9764 set_message (m);
9765 if (minibuffer_auto_raise)
9766 Fraise_frame (frame);
9767 /* Assume we are not echoing.
9768 (If we are, echo_now will override this.) */
9769 echo_message_buffer = Qnil;
9770 }
9771 else
9772 clear_message (1, 1);
9773
9774 do_pending_window_change (0);
9775 echo_area_display (1);
9776 do_pending_window_change (0);
9777 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9778 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9779 }
9780 }
9781
9782
9783 /* Display a null-terminated echo area message M. If M is 0, clear
9784 out any existing message, and let the mini-buffer text show through.
9785
9786 The buffer M must continue to exist until after the echo area gets
9787 cleared or some other message gets displayed there. Do not pass
9788 text that is stored in a Lisp string. Do not pass text in a buffer
9789 that was alloca'd. */
9790
9791 void
9792 message1 (const char *m)
9793 {
9794 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9795 }
9796
9797
9798 /* The non-logging counterpart of message1. */
9799
9800 void
9801 message1_nolog (const char *m)
9802 {
9803 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9804 }
9805
9806 /* Display a message M which contains a single %s
9807 which gets replaced with STRING. */
9808
9809 void
9810 message_with_string (const char *m, Lisp_Object string, int log)
9811 {
9812 CHECK_STRING (string);
9813
9814 if (noninteractive)
9815 {
9816 if (m)
9817 {
9818 if (noninteractive_need_newline)
9819 putc ('\n', stderr);
9820 noninteractive_need_newline = 0;
9821 fprintf (stderr, m, SDATA (string));
9822 if (!cursor_in_echo_area)
9823 fprintf (stderr, "\n");
9824 fflush (stderr);
9825 }
9826 }
9827 else if (INTERACTIVE)
9828 {
9829 /* The frame whose minibuffer we're going to display the message on.
9830 It may be larger than the selected frame, so we need
9831 to use its buffer, not the selected frame's buffer. */
9832 Lisp_Object mini_window;
9833 struct frame *f, *sf = SELECTED_FRAME ();
9834
9835 /* Get the frame containing the minibuffer
9836 that the selected frame is using. */
9837 mini_window = FRAME_MINIBUF_WINDOW (sf);
9838 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9839
9840 /* Error messages get reported properly by cmd_error, so this must be
9841 just an informative message; if the frame hasn't really been
9842 initialized yet, just toss it. */
9843 if (f->glyphs_initialized_p)
9844 {
9845 Lisp_Object args[2], msg;
9846 struct gcpro gcpro1, gcpro2;
9847
9848 args[0] = build_string (m);
9849 args[1] = msg = string;
9850 GCPRO2 (args[0], msg);
9851 gcpro1.nvars = 2;
9852
9853 msg = Fformat (2, args);
9854
9855 if (log)
9856 message3 (msg);
9857 else
9858 message3_nolog (msg);
9859
9860 UNGCPRO;
9861
9862 /* Print should start at the beginning of the message
9863 buffer next time. */
9864 message_buf_print = 0;
9865 }
9866 }
9867 }
9868
9869
9870 /* Dump an informative message to the minibuf. If M is 0, clear out
9871 any existing message, and let the mini-buffer text show through. */
9872
9873 static void
9874 vmessage (const char *m, va_list ap)
9875 {
9876 if (noninteractive)
9877 {
9878 if (m)
9879 {
9880 if (noninteractive_need_newline)
9881 putc ('\n', stderr);
9882 noninteractive_need_newline = 0;
9883 vfprintf (stderr, m, ap);
9884 if (cursor_in_echo_area == 0)
9885 fprintf (stderr, "\n");
9886 fflush (stderr);
9887 }
9888 }
9889 else if (INTERACTIVE)
9890 {
9891 /* The frame whose mini-buffer we're going to display the message
9892 on. It may be larger than the selected frame, so we need to
9893 use its buffer, not the selected frame's buffer. */
9894 Lisp_Object mini_window;
9895 struct frame *f, *sf = SELECTED_FRAME ();
9896
9897 /* Get the frame containing the mini-buffer
9898 that the selected frame is using. */
9899 mini_window = FRAME_MINIBUF_WINDOW (sf);
9900 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9901
9902 /* Error messages get reported properly by cmd_error, so this must be
9903 just an informative message; if the frame hasn't really been
9904 initialized yet, just toss it. */
9905 if (f->glyphs_initialized_p)
9906 {
9907 if (m)
9908 {
9909 ptrdiff_t len;
9910 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9911 char *message_buf = alloca (maxsize + 1);
9912
9913 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9914
9915 message3 (make_string (message_buf, len));
9916 }
9917 else
9918 message1 (0);
9919
9920 /* Print should start at the beginning of the message
9921 buffer next time. */
9922 message_buf_print = 0;
9923 }
9924 }
9925 }
9926
9927 void
9928 message (const char *m, ...)
9929 {
9930 va_list ap;
9931 va_start (ap, m);
9932 vmessage (m, ap);
9933 va_end (ap);
9934 }
9935
9936
9937 #if 0
9938 /* The non-logging version of message. */
9939
9940 void
9941 message_nolog (const char *m, ...)
9942 {
9943 Lisp_Object old_log_max;
9944 va_list ap;
9945 va_start (ap, m);
9946 old_log_max = Vmessage_log_max;
9947 Vmessage_log_max = Qnil;
9948 vmessage (m, ap);
9949 Vmessage_log_max = old_log_max;
9950 va_end (ap);
9951 }
9952 #endif
9953
9954
9955 /* Display the current message in the current mini-buffer. This is
9956 only called from error handlers in process.c, and is not time
9957 critical. */
9958
9959 void
9960 update_echo_area (void)
9961 {
9962 if (!NILP (echo_area_buffer[0]))
9963 {
9964 Lisp_Object string;
9965 string = Fcurrent_message ();
9966 message3 (string);
9967 }
9968 }
9969
9970
9971 /* Make sure echo area buffers in `echo_buffers' are live.
9972 If they aren't, make new ones. */
9973
9974 static void
9975 ensure_echo_area_buffers (void)
9976 {
9977 int i;
9978
9979 for (i = 0; i < 2; ++i)
9980 if (!BUFFERP (echo_buffer[i])
9981 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9982 {
9983 char name[30];
9984 Lisp_Object old_buffer;
9985 int j;
9986
9987 old_buffer = echo_buffer[i];
9988 echo_buffer[i] = Fget_buffer_create
9989 (make_formatted_string (name, " *Echo Area %d*", i));
9990 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9991 /* to force word wrap in echo area -
9992 it was decided to postpone this*/
9993 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9994
9995 for (j = 0; j < 2; ++j)
9996 if (EQ (old_buffer, echo_area_buffer[j]))
9997 echo_area_buffer[j] = echo_buffer[i];
9998 }
9999 }
10000
10001
10002 /* Call FN with args A1..A2 with either the current or last displayed
10003 echo_area_buffer as current buffer.
10004
10005 WHICH zero means use the current message buffer
10006 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10007 from echo_buffer[] and clear it.
10008
10009 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10010 suitable buffer from echo_buffer[] and clear it.
10011
10012 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10013 that the current message becomes the last displayed one, make
10014 choose a suitable buffer for echo_area_buffer[0], and clear it.
10015
10016 Value is what FN returns. */
10017
10018 static int
10019 with_echo_area_buffer (struct window *w, int which,
10020 int (*fn) (ptrdiff_t, Lisp_Object),
10021 ptrdiff_t a1, Lisp_Object a2)
10022 {
10023 Lisp_Object buffer;
10024 int this_one, the_other, clear_buffer_p, rc;
10025 ptrdiff_t count = SPECPDL_INDEX ();
10026
10027 /* If buffers aren't live, make new ones. */
10028 ensure_echo_area_buffers ();
10029
10030 clear_buffer_p = 0;
10031
10032 if (which == 0)
10033 this_one = 0, the_other = 1;
10034 else if (which > 0)
10035 this_one = 1, the_other = 0;
10036 else
10037 {
10038 this_one = 0, the_other = 1;
10039 clear_buffer_p = 1;
10040
10041 /* We need a fresh one in case the current echo buffer equals
10042 the one containing the last displayed echo area message. */
10043 if (!NILP (echo_area_buffer[this_one])
10044 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10045 echo_area_buffer[this_one] = Qnil;
10046 }
10047
10048 /* Choose a suitable buffer from echo_buffer[] is we don't
10049 have one. */
10050 if (NILP (echo_area_buffer[this_one]))
10051 {
10052 echo_area_buffer[this_one]
10053 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10054 ? echo_buffer[the_other]
10055 : echo_buffer[this_one]);
10056 clear_buffer_p = 1;
10057 }
10058
10059 buffer = echo_area_buffer[this_one];
10060
10061 /* Don't get confused by reusing the buffer used for echoing
10062 for a different purpose. */
10063 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10064 cancel_echoing ();
10065
10066 record_unwind_protect (unwind_with_echo_area_buffer,
10067 with_echo_area_buffer_unwind_data (w));
10068
10069 /* Make the echo area buffer current. Note that for display
10070 purposes, it is not necessary that the displayed window's buffer
10071 == current_buffer, except for text property lookup. So, let's
10072 only set that buffer temporarily here without doing a full
10073 Fset_window_buffer. We must also change w->pointm, though,
10074 because otherwise an assertions in unshow_buffer fails, and Emacs
10075 aborts. */
10076 set_buffer_internal_1 (XBUFFER (buffer));
10077 if (w)
10078 {
10079 wset_buffer (w, buffer);
10080 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10081 }
10082
10083 bset_undo_list (current_buffer, Qt);
10084 bset_read_only (current_buffer, Qnil);
10085 specbind (Qinhibit_read_only, Qt);
10086 specbind (Qinhibit_modification_hooks, Qt);
10087
10088 if (clear_buffer_p && Z > BEG)
10089 del_range (BEG, Z);
10090
10091 eassert (BEGV >= BEG);
10092 eassert (ZV <= Z && ZV >= BEGV);
10093
10094 rc = fn (a1, a2);
10095
10096 eassert (BEGV >= BEG);
10097 eassert (ZV <= Z && ZV >= BEGV);
10098
10099 unbind_to (count, Qnil);
10100 return rc;
10101 }
10102
10103
10104 /* Save state that should be preserved around the call to the function
10105 FN called in with_echo_area_buffer. */
10106
10107 static Lisp_Object
10108 with_echo_area_buffer_unwind_data (struct window *w)
10109 {
10110 int i = 0;
10111 Lisp_Object vector, tmp;
10112
10113 /* Reduce consing by keeping one vector in
10114 Vwith_echo_area_save_vector. */
10115 vector = Vwith_echo_area_save_vector;
10116 Vwith_echo_area_save_vector = Qnil;
10117
10118 if (NILP (vector))
10119 vector = Fmake_vector (make_number (9), Qnil);
10120
10121 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10122 ASET (vector, i, Vdeactivate_mark); ++i;
10123 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10124
10125 if (w)
10126 {
10127 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10128 ASET (vector, i, w->contents); ++i;
10129 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10130 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10131 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10132 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10133 }
10134 else
10135 {
10136 int end = i + 6;
10137 for (; i < end; ++i)
10138 ASET (vector, i, Qnil);
10139 }
10140
10141 eassert (i == ASIZE (vector));
10142 return vector;
10143 }
10144
10145
10146 /* Restore global state from VECTOR which was created by
10147 with_echo_area_buffer_unwind_data. */
10148
10149 static Lisp_Object
10150 unwind_with_echo_area_buffer (Lisp_Object vector)
10151 {
10152 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10153 Vdeactivate_mark = AREF (vector, 1);
10154 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10155
10156 if (WINDOWP (AREF (vector, 3)))
10157 {
10158 struct window *w;
10159 Lisp_Object buffer;
10160
10161 w = XWINDOW (AREF (vector, 3));
10162 buffer = AREF (vector, 4);
10163
10164 wset_buffer (w, buffer);
10165 set_marker_both (w->pointm, buffer,
10166 XFASTINT (AREF (vector, 5)),
10167 XFASTINT (AREF (vector, 6)));
10168 set_marker_both (w->start, buffer,
10169 XFASTINT (AREF (vector, 7)),
10170 XFASTINT (AREF (vector, 8)));
10171 }
10172
10173 Vwith_echo_area_save_vector = vector;
10174 return Qnil;
10175 }
10176
10177
10178 /* Set up the echo area for use by print functions. MULTIBYTE_P
10179 non-zero means we will print multibyte. */
10180
10181 void
10182 setup_echo_area_for_printing (int multibyte_p)
10183 {
10184 /* If we can't find an echo area any more, exit. */
10185 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10186 Fkill_emacs (Qnil);
10187
10188 ensure_echo_area_buffers ();
10189
10190 if (!message_buf_print)
10191 {
10192 /* A message has been output since the last time we printed.
10193 Choose a fresh echo area buffer. */
10194 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10195 echo_area_buffer[0] = echo_buffer[1];
10196 else
10197 echo_area_buffer[0] = echo_buffer[0];
10198
10199 /* Switch to that buffer and clear it. */
10200 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10201 bset_truncate_lines (current_buffer, Qnil);
10202
10203 if (Z > BEG)
10204 {
10205 ptrdiff_t count = SPECPDL_INDEX ();
10206 specbind (Qinhibit_read_only, Qt);
10207 /* Note that undo recording is always disabled. */
10208 del_range (BEG, Z);
10209 unbind_to (count, Qnil);
10210 }
10211 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10212
10213 /* Set up the buffer for the multibyteness we need. */
10214 if (multibyte_p
10215 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10216 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10217
10218 /* Raise the frame containing the echo area. */
10219 if (minibuffer_auto_raise)
10220 {
10221 struct frame *sf = SELECTED_FRAME ();
10222 Lisp_Object mini_window;
10223 mini_window = FRAME_MINIBUF_WINDOW (sf);
10224 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10225 }
10226
10227 message_log_maybe_newline ();
10228 message_buf_print = 1;
10229 }
10230 else
10231 {
10232 if (NILP (echo_area_buffer[0]))
10233 {
10234 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10235 echo_area_buffer[0] = echo_buffer[1];
10236 else
10237 echo_area_buffer[0] = echo_buffer[0];
10238 }
10239
10240 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10241 {
10242 /* Someone switched buffers between print requests. */
10243 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10244 bset_truncate_lines (current_buffer, Qnil);
10245 }
10246 }
10247 }
10248
10249
10250 /* Display an echo area message in window W. Value is non-zero if W's
10251 height is changed. If display_last_displayed_message_p is
10252 non-zero, display the message that was last displayed, otherwise
10253 display the current message. */
10254
10255 static int
10256 display_echo_area (struct window *w)
10257 {
10258 int i, no_message_p, window_height_changed_p;
10259
10260 /* Temporarily disable garbage collections while displaying the echo
10261 area. This is done because a GC can print a message itself.
10262 That message would modify the echo area buffer's contents while a
10263 redisplay of the buffer is going on, and seriously confuse
10264 redisplay. */
10265 ptrdiff_t count = inhibit_garbage_collection ();
10266
10267 /* If there is no message, we must call display_echo_area_1
10268 nevertheless because it resizes the window. But we will have to
10269 reset the echo_area_buffer in question to nil at the end because
10270 with_echo_area_buffer will sets it to an empty buffer. */
10271 i = display_last_displayed_message_p ? 1 : 0;
10272 no_message_p = NILP (echo_area_buffer[i]);
10273
10274 window_height_changed_p
10275 = with_echo_area_buffer (w, display_last_displayed_message_p,
10276 display_echo_area_1,
10277 (intptr_t) w, Qnil);
10278
10279 if (no_message_p)
10280 echo_area_buffer[i] = Qnil;
10281
10282 unbind_to (count, Qnil);
10283 return window_height_changed_p;
10284 }
10285
10286
10287 /* Helper for display_echo_area. Display the current buffer which
10288 contains the current echo area message in window W, a mini-window,
10289 a pointer to which is passed in A1. A2..A4 are currently not used.
10290 Change the height of W so that all of the message is displayed.
10291 Value is non-zero if height of W was changed. */
10292
10293 static int
10294 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10295 {
10296 intptr_t i1 = a1;
10297 struct window *w = (struct window *) i1;
10298 Lisp_Object window;
10299 struct text_pos start;
10300 int window_height_changed_p = 0;
10301
10302 /* Do this before displaying, so that we have a large enough glyph
10303 matrix for the display. If we can't get enough space for the
10304 whole text, display the last N lines. That works by setting w->start. */
10305 window_height_changed_p = resize_mini_window (w, 0);
10306
10307 /* Use the starting position chosen by resize_mini_window. */
10308 SET_TEXT_POS_FROM_MARKER (start, w->start);
10309
10310 /* Display. */
10311 clear_glyph_matrix (w->desired_matrix);
10312 XSETWINDOW (window, w);
10313 try_window (window, start, 0);
10314
10315 return window_height_changed_p;
10316 }
10317
10318
10319 /* Resize the echo area window to exactly the size needed for the
10320 currently displayed message, if there is one. If a mini-buffer
10321 is active, don't shrink it. */
10322
10323 void
10324 resize_echo_area_exactly (void)
10325 {
10326 if (BUFFERP (echo_area_buffer[0])
10327 && WINDOWP (echo_area_window))
10328 {
10329 struct window *w = XWINDOW (echo_area_window);
10330 int resized_p;
10331 Lisp_Object resize_exactly;
10332
10333 if (minibuf_level == 0)
10334 resize_exactly = Qt;
10335 else
10336 resize_exactly = Qnil;
10337
10338 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10339 (intptr_t) w, resize_exactly);
10340 if (resized_p)
10341 {
10342 ++windows_or_buffers_changed;
10343 ++update_mode_lines;
10344 redisplay_internal ();
10345 }
10346 }
10347 }
10348
10349
10350 /* Callback function for with_echo_area_buffer, when used from
10351 resize_echo_area_exactly. A1 contains a pointer to the window to
10352 resize, EXACTLY non-nil means resize the mini-window exactly to the
10353 size of the text displayed. A3 and A4 are not used. Value is what
10354 resize_mini_window returns. */
10355
10356 static int
10357 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10358 {
10359 intptr_t i1 = a1;
10360 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10361 }
10362
10363
10364 /* Resize mini-window W to fit the size of its contents. EXACT_P
10365 means size the window exactly to the size needed. Otherwise, it's
10366 only enlarged until W's buffer is empty.
10367
10368 Set W->start to the right place to begin display. If the whole
10369 contents fit, start at the beginning. Otherwise, start so as
10370 to make the end of the contents appear. This is particularly
10371 important for y-or-n-p, but seems desirable generally.
10372
10373 Value is non-zero if the window height has been changed. */
10374
10375 int
10376 resize_mini_window (struct window *w, int exact_p)
10377 {
10378 struct frame *f = XFRAME (w->frame);
10379 int window_height_changed_p = 0;
10380
10381 eassert (MINI_WINDOW_P (w));
10382
10383 /* By default, start display at the beginning. */
10384 set_marker_both (w->start, w->contents,
10385 BUF_BEGV (XBUFFER (w->contents)),
10386 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10387
10388 /* Don't resize windows while redisplaying a window; it would
10389 confuse redisplay functions when the size of the window they are
10390 displaying changes from under them. Such a resizing can happen,
10391 for instance, when which-func prints a long message while
10392 we are running fontification-functions. We're running these
10393 functions with safe_call which binds inhibit-redisplay to t. */
10394 if (!NILP (Vinhibit_redisplay))
10395 return 0;
10396
10397 /* Nil means don't try to resize. */
10398 if (NILP (Vresize_mini_windows)
10399 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10400 return 0;
10401
10402 if (!FRAME_MINIBUF_ONLY_P (f))
10403 {
10404 struct it it;
10405 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10406 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10407 int height;
10408 EMACS_INT max_height;
10409 int unit = FRAME_LINE_HEIGHT (f);
10410 struct text_pos start;
10411 struct buffer *old_current_buffer = NULL;
10412
10413 if (current_buffer != XBUFFER (w->contents))
10414 {
10415 old_current_buffer = current_buffer;
10416 set_buffer_internal (XBUFFER (w->contents));
10417 }
10418
10419 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10420
10421 /* Compute the max. number of lines specified by the user. */
10422 if (FLOATP (Vmax_mini_window_height))
10423 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10424 else if (INTEGERP (Vmax_mini_window_height))
10425 max_height = XINT (Vmax_mini_window_height);
10426 else
10427 max_height = total_height / 4;
10428
10429 /* Correct that max. height if it's bogus. */
10430 max_height = clip_to_bounds (1, max_height, total_height);
10431
10432 /* Find out the height of the text in the window. */
10433 if (it.line_wrap == TRUNCATE)
10434 height = 1;
10435 else
10436 {
10437 last_height = 0;
10438 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10439 if (it.max_ascent == 0 && it.max_descent == 0)
10440 height = it.current_y + last_height;
10441 else
10442 height = it.current_y + it.max_ascent + it.max_descent;
10443 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10444 height = (height + unit - 1) / unit;
10445 }
10446
10447 /* Compute a suitable window start. */
10448 if (height > max_height)
10449 {
10450 height = max_height;
10451 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10452 move_it_vertically_backward (&it, (height - 1) * unit);
10453 start = it.current.pos;
10454 }
10455 else
10456 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10457 SET_MARKER_FROM_TEXT_POS (w->start, start);
10458
10459 if (EQ (Vresize_mini_windows, Qgrow_only))
10460 {
10461 /* Let it grow only, until we display an empty message, in which
10462 case the window shrinks again. */
10463 if (height > WINDOW_TOTAL_LINES (w))
10464 {
10465 int old_height = WINDOW_TOTAL_LINES (w);
10466 freeze_window_starts (f, 1);
10467 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10468 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10469 }
10470 else if (height < WINDOW_TOTAL_LINES (w)
10471 && (exact_p || BEGV == ZV))
10472 {
10473 int old_height = WINDOW_TOTAL_LINES (w);
10474 freeze_window_starts (f, 0);
10475 shrink_mini_window (w);
10476 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10477 }
10478 }
10479 else
10480 {
10481 /* Always resize to exact size needed. */
10482 if (height > WINDOW_TOTAL_LINES (w))
10483 {
10484 int old_height = WINDOW_TOTAL_LINES (w);
10485 freeze_window_starts (f, 1);
10486 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10487 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10488 }
10489 else if (height < WINDOW_TOTAL_LINES (w))
10490 {
10491 int old_height = WINDOW_TOTAL_LINES (w);
10492 freeze_window_starts (f, 0);
10493 shrink_mini_window (w);
10494
10495 if (height)
10496 {
10497 freeze_window_starts (f, 1);
10498 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10499 }
10500
10501 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10502 }
10503 }
10504
10505 if (old_current_buffer)
10506 set_buffer_internal (old_current_buffer);
10507 }
10508
10509 return window_height_changed_p;
10510 }
10511
10512
10513 /* Value is the current message, a string, or nil if there is no
10514 current message. */
10515
10516 Lisp_Object
10517 current_message (void)
10518 {
10519 Lisp_Object msg;
10520
10521 if (!BUFFERP (echo_area_buffer[0]))
10522 msg = Qnil;
10523 else
10524 {
10525 with_echo_area_buffer (0, 0, current_message_1,
10526 (intptr_t) &msg, Qnil);
10527 if (NILP (msg))
10528 echo_area_buffer[0] = Qnil;
10529 }
10530
10531 return msg;
10532 }
10533
10534
10535 static int
10536 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10537 {
10538 intptr_t i1 = a1;
10539 Lisp_Object *msg = (Lisp_Object *) i1;
10540
10541 if (Z > BEG)
10542 *msg = make_buffer_string (BEG, Z, 1);
10543 else
10544 *msg = Qnil;
10545 return 0;
10546 }
10547
10548
10549 /* Push the current message on Vmessage_stack for later restoration
10550 by restore_message. Value is non-zero if the current message isn't
10551 empty. This is a relatively infrequent operation, so it's not
10552 worth optimizing. */
10553
10554 bool
10555 push_message (void)
10556 {
10557 Lisp_Object msg = current_message ();
10558 Vmessage_stack = Fcons (msg, Vmessage_stack);
10559 return STRINGP (msg);
10560 }
10561
10562
10563 /* Restore message display from the top of Vmessage_stack. */
10564
10565 void
10566 restore_message (void)
10567 {
10568 eassert (CONSP (Vmessage_stack));
10569 message3_nolog (XCAR (Vmessage_stack));
10570 }
10571
10572
10573 /* Handler for record_unwind_protect calling pop_message. */
10574
10575 Lisp_Object
10576 pop_message_unwind (Lisp_Object dummy)
10577 {
10578 pop_message ();
10579 return Qnil;
10580 }
10581
10582 /* Pop the top-most entry off Vmessage_stack. */
10583
10584 static void
10585 pop_message (void)
10586 {
10587 eassert (CONSP (Vmessage_stack));
10588 Vmessage_stack = XCDR (Vmessage_stack);
10589 }
10590
10591
10592 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10593 exits. If the stack is not empty, we have a missing pop_message
10594 somewhere. */
10595
10596 void
10597 check_message_stack (void)
10598 {
10599 if (!NILP (Vmessage_stack))
10600 emacs_abort ();
10601 }
10602
10603
10604 /* Truncate to NCHARS what will be displayed in the echo area the next
10605 time we display it---but don't redisplay it now. */
10606
10607 void
10608 truncate_echo_area (ptrdiff_t nchars)
10609 {
10610 if (nchars == 0)
10611 echo_area_buffer[0] = Qnil;
10612 else if (!noninteractive
10613 && INTERACTIVE
10614 && !NILP (echo_area_buffer[0]))
10615 {
10616 struct frame *sf = SELECTED_FRAME ();
10617 /* Error messages get reported properly by cmd_error, so this must be
10618 just an informative message; if the frame hasn't really been
10619 initialized yet, just toss it. */
10620 if (sf->glyphs_initialized_p)
10621 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10622 }
10623 }
10624
10625
10626 /* Helper function for truncate_echo_area. Truncate the current
10627 message to at most NCHARS characters. */
10628
10629 static int
10630 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10631 {
10632 if (BEG + nchars < Z)
10633 del_range (BEG + nchars, Z);
10634 if (Z == BEG)
10635 echo_area_buffer[0] = Qnil;
10636 return 0;
10637 }
10638
10639 /* Set the current message to STRING. */
10640
10641 static void
10642 set_message (Lisp_Object string)
10643 {
10644 eassert (STRINGP (string));
10645
10646 message_enable_multibyte = STRING_MULTIBYTE (string);
10647
10648 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10649 message_buf_print = 0;
10650 help_echo_showing_p = 0;
10651
10652 if (STRINGP (Vdebug_on_message)
10653 && STRINGP (string)
10654 && fast_string_match (Vdebug_on_message, string) >= 0)
10655 call_debugger (list2 (Qerror, string));
10656 }
10657
10658
10659 /* Helper function for set_message. First argument is ignored and second
10660 argument has the same meaning as for set_message.
10661 This function is called with the echo area buffer being current. */
10662
10663 static int
10664 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10665 {
10666 eassert (STRINGP (string));
10667
10668 /* Change multibyteness of the echo buffer appropriately. */
10669 if (message_enable_multibyte
10670 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10671 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10672
10673 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10674 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10675 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10676
10677 /* Insert new message at BEG. */
10678 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10679
10680 /* This function takes care of single/multibyte conversion.
10681 We just have to ensure that the echo area buffer has the right
10682 setting of enable_multibyte_characters. */
10683 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10684
10685 return 0;
10686 }
10687
10688
10689 /* Clear messages. CURRENT_P non-zero means clear the current
10690 message. LAST_DISPLAYED_P non-zero means clear the message
10691 last displayed. */
10692
10693 void
10694 clear_message (int current_p, int last_displayed_p)
10695 {
10696 if (current_p)
10697 {
10698 echo_area_buffer[0] = Qnil;
10699 message_cleared_p = 1;
10700 }
10701
10702 if (last_displayed_p)
10703 echo_area_buffer[1] = Qnil;
10704
10705 message_buf_print = 0;
10706 }
10707
10708 /* Clear garbaged frames.
10709
10710 This function is used where the old redisplay called
10711 redraw_garbaged_frames which in turn called redraw_frame which in
10712 turn called clear_frame. The call to clear_frame was a source of
10713 flickering. I believe a clear_frame is not necessary. It should
10714 suffice in the new redisplay to invalidate all current matrices,
10715 and ensure a complete redisplay of all windows. */
10716
10717 static void
10718 clear_garbaged_frames (void)
10719 {
10720 if (frame_garbaged)
10721 {
10722 Lisp_Object tail, frame;
10723 int changed_count = 0;
10724
10725 FOR_EACH_FRAME (tail, frame)
10726 {
10727 struct frame *f = XFRAME (frame);
10728
10729 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10730 {
10731 if (f->resized_p)
10732 {
10733 redraw_frame (f);
10734 f->force_flush_display_p = 1;
10735 }
10736 clear_current_matrices (f);
10737 changed_count++;
10738 f->garbaged = 0;
10739 f->resized_p = 0;
10740 }
10741 }
10742
10743 frame_garbaged = 0;
10744 if (changed_count)
10745 ++windows_or_buffers_changed;
10746 }
10747 }
10748
10749
10750 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10751 is non-zero update selected_frame. Value is non-zero if the
10752 mini-windows height has been changed. */
10753
10754 static int
10755 echo_area_display (int update_frame_p)
10756 {
10757 Lisp_Object mini_window;
10758 struct window *w;
10759 struct frame *f;
10760 int window_height_changed_p = 0;
10761 struct frame *sf = SELECTED_FRAME ();
10762
10763 mini_window = FRAME_MINIBUF_WINDOW (sf);
10764 w = XWINDOW (mini_window);
10765 f = XFRAME (WINDOW_FRAME (w));
10766
10767 /* Don't display if frame is invisible or not yet initialized. */
10768 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10769 return 0;
10770
10771 #ifdef HAVE_WINDOW_SYSTEM
10772 /* When Emacs starts, selected_frame may be the initial terminal
10773 frame. If we let this through, a message would be displayed on
10774 the terminal. */
10775 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10776 return 0;
10777 #endif /* HAVE_WINDOW_SYSTEM */
10778
10779 /* Redraw garbaged frames. */
10780 clear_garbaged_frames ();
10781
10782 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10783 {
10784 echo_area_window = mini_window;
10785 window_height_changed_p = display_echo_area (w);
10786 w->must_be_updated_p = 1;
10787
10788 /* Update the display, unless called from redisplay_internal.
10789 Also don't update the screen during redisplay itself. The
10790 update will happen at the end of redisplay, and an update
10791 here could cause confusion. */
10792 if (update_frame_p && !redisplaying_p)
10793 {
10794 int n = 0;
10795
10796 /* If the display update has been interrupted by pending
10797 input, update mode lines in the frame. Due to the
10798 pending input, it might have been that redisplay hasn't
10799 been called, so that mode lines above the echo area are
10800 garbaged. This looks odd, so we prevent it here. */
10801 if (!display_completed)
10802 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10803
10804 if (window_height_changed_p
10805 /* Don't do this if Emacs is shutting down. Redisplay
10806 needs to run hooks. */
10807 && !NILP (Vrun_hooks))
10808 {
10809 /* Must update other windows. Likewise as in other
10810 cases, don't let this update be interrupted by
10811 pending input. */
10812 ptrdiff_t count = SPECPDL_INDEX ();
10813 specbind (Qredisplay_dont_pause, Qt);
10814 windows_or_buffers_changed = 1;
10815 redisplay_internal ();
10816 unbind_to (count, Qnil);
10817 }
10818 else if (FRAME_WINDOW_P (f) && n == 0)
10819 {
10820 /* Window configuration is the same as before.
10821 Can do with a display update of the echo area,
10822 unless we displayed some mode lines. */
10823 update_single_window (w, 1);
10824 FRAME_RIF (f)->flush_display (f);
10825 }
10826 else
10827 update_frame (f, 1, 1);
10828
10829 /* If cursor is in the echo area, make sure that the next
10830 redisplay displays the minibuffer, so that the cursor will
10831 be replaced with what the minibuffer wants. */
10832 if (cursor_in_echo_area)
10833 ++windows_or_buffers_changed;
10834 }
10835 }
10836 else if (!EQ (mini_window, selected_window))
10837 windows_or_buffers_changed++;
10838
10839 /* Last displayed message is now the current message. */
10840 echo_area_buffer[1] = echo_area_buffer[0];
10841 /* Inform read_char that we're not echoing. */
10842 echo_message_buffer = Qnil;
10843
10844 /* Prevent redisplay optimization in redisplay_internal by resetting
10845 this_line_start_pos. This is done because the mini-buffer now
10846 displays the message instead of its buffer text. */
10847 if (EQ (mini_window, selected_window))
10848 CHARPOS (this_line_start_pos) = 0;
10849
10850 return window_height_changed_p;
10851 }
10852
10853 /* Nonzero if the current window's buffer is shown in more than one
10854 window and was modified since last redisplay. */
10855
10856 static int
10857 buffer_shared_and_changed (void)
10858 {
10859 return (buffer_window_count (current_buffer) > 1
10860 && UNCHANGED_MODIFIED < MODIFF);
10861 }
10862
10863 /* Nonzero if W doesn't reflect the actual state of current buffer due
10864 to its text or overlays change. FIXME: this may be called when
10865 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10866
10867 static int
10868 window_outdated (struct window *w)
10869 {
10870 return (w->last_modified < MODIFF
10871 || w->last_overlay_modified < OVERLAY_MODIFF);
10872 }
10873
10874 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10875 is enabled and mark of W's buffer was changed since last W's update. */
10876
10877 static int
10878 window_buffer_changed (struct window *w)
10879 {
10880 struct buffer *b = XBUFFER (w->contents);
10881
10882 eassert (BUFFER_LIVE_P (b));
10883
10884 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10885 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10886 != (w->region_showing != 0)));
10887 }
10888
10889 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10890
10891 static int
10892 mode_line_update_needed (struct window *w)
10893 {
10894 return (w->column_number_displayed != -1
10895 && !(PT == w->last_point && !window_outdated (w))
10896 && (w->column_number_displayed != current_column ()));
10897 }
10898
10899 /***********************************************************************
10900 Mode Lines and Frame Titles
10901 ***********************************************************************/
10902
10903 /* A buffer for constructing non-propertized mode-line strings and
10904 frame titles in it; allocated from the heap in init_xdisp and
10905 resized as needed in store_mode_line_noprop_char. */
10906
10907 static char *mode_line_noprop_buf;
10908
10909 /* The buffer's end, and a current output position in it. */
10910
10911 static char *mode_line_noprop_buf_end;
10912 static char *mode_line_noprop_ptr;
10913
10914 #define MODE_LINE_NOPROP_LEN(start) \
10915 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10916
10917 static enum {
10918 MODE_LINE_DISPLAY = 0,
10919 MODE_LINE_TITLE,
10920 MODE_LINE_NOPROP,
10921 MODE_LINE_STRING
10922 } mode_line_target;
10923
10924 /* Alist that caches the results of :propertize.
10925 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10926 static Lisp_Object mode_line_proptrans_alist;
10927
10928 /* List of strings making up the mode-line. */
10929 static Lisp_Object mode_line_string_list;
10930
10931 /* Base face property when building propertized mode line string. */
10932 static Lisp_Object mode_line_string_face;
10933 static Lisp_Object mode_line_string_face_prop;
10934
10935
10936 /* Unwind data for mode line strings */
10937
10938 static Lisp_Object Vmode_line_unwind_vector;
10939
10940 static Lisp_Object
10941 format_mode_line_unwind_data (struct frame *target_frame,
10942 struct buffer *obuf,
10943 Lisp_Object owin,
10944 int save_proptrans)
10945 {
10946 Lisp_Object vector, tmp;
10947
10948 /* Reduce consing by keeping one vector in
10949 Vwith_echo_area_save_vector. */
10950 vector = Vmode_line_unwind_vector;
10951 Vmode_line_unwind_vector = Qnil;
10952
10953 if (NILP (vector))
10954 vector = Fmake_vector (make_number (10), Qnil);
10955
10956 ASET (vector, 0, make_number (mode_line_target));
10957 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10958 ASET (vector, 2, mode_line_string_list);
10959 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10960 ASET (vector, 4, mode_line_string_face);
10961 ASET (vector, 5, mode_line_string_face_prop);
10962
10963 if (obuf)
10964 XSETBUFFER (tmp, obuf);
10965 else
10966 tmp = Qnil;
10967 ASET (vector, 6, tmp);
10968 ASET (vector, 7, owin);
10969 if (target_frame)
10970 {
10971 /* Similarly to `with-selected-window', if the operation selects
10972 a window on another frame, we must restore that frame's
10973 selected window, and (for a tty) the top-frame. */
10974 ASET (vector, 8, target_frame->selected_window);
10975 if (FRAME_TERMCAP_P (target_frame))
10976 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10977 }
10978
10979 return vector;
10980 }
10981
10982 static Lisp_Object
10983 unwind_format_mode_line (Lisp_Object vector)
10984 {
10985 Lisp_Object old_window = AREF (vector, 7);
10986 Lisp_Object target_frame_window = AREF (vector, 8);
10987 Lisp_Object old_top_frame = AREF (vector, 9);
10988
10989 mode_line_target = XINT (AREF (vector, 0));
10990 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10991 mode_line_string_list = AREF (vector, 2);
10992 if (! EQ (AREF (vector, 3), Qt))
10993 mode_line_proptrans_alist = AREF (vector, 3);
10994 mode_line_string_face = AREF (vector, 4);
10995 mode_line_string_face_prop = AREF (vector, 5);
10996
10997 /* Select window before buffer, since it may change the buffer. */
10998 if (!NILP (old_window))
10999 {
11000 /* If the operation that we are unwinding had selected a window
11001 on a different frame, reset its frame-selected-window. For a
11002 text terminal, reset its top-frame if necessary. */
11003 if (!NILP (target_frame_window))
11004 {
11005 Lisp_Object frame
11006 = WINDOW_FRAME (XWINDOW (target_frame_window));
11007
11008 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11009 Fselect_window (target_frame_window, Qt);
11010
11011 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11012 Fselect_frame (old_top_frame, Qt);
11013 }
11014
11015 Fselect_window (old_window, Qt);
11016 }
11017
11018 if (!NILP (AREF (vector, 6)))
11019 {
11020 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11021 ASET (vector, 6, Qnil);
11022 }
11023
11024 Vmode_line_unwind_vector = vector;
11025 return Qnil;
11026 }
11027
11028
11029 /* Store a single character C for the frame title in mode_line_noprop_buf.
11030 Re-allocate mode_line_noprop_buf if necessary. */
11031
11032 static void
11033 store_mode_line_noprop_char (char c)
11034 {
11035 /* If output position has reached the end of the allocated buffer,
11036 increase the buffer's size. */
11037 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11038 {
11039 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11040 ptrdiff_t size = len;
11041 mode_line_noprop_buf =
11042 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11043 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11044 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11045 }
11046
11047 *mode_line_noprop_ptr++ = c;
11048 }
11049
11050
11051 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11052 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11053 characters that yield more columns than PRECISION; PRECISION <= 0
11054 means copy the whole string. Pad with spaces until FIELD_WIDTH
11055 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11056 pad. Called from display_mode_element when it is used to build a
11057 frame title. */
11058
11059 static int
11060 store_mode_line_noprop (const char *string, int field_width, int precision)
11061 {
11062 const unsigned char *str = (const unsigned char *) string;
11063 int n = 0;
11064 ptrdiff_t dummy, nbytes;
11065
11066 /* Copy at most PRECISION chars from STR. */
11067 nbytes = strlen (string);
11068 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11069 while (nbytes--)
11070 store_mode_line_noprop_char (*str++);
11071
11072 /* Fill up with spaces until FIELD_WIDTH reached. */
11073 while (field_width > 0
11074 && n < field_width)
11075 {
11076 store_mode_line_noprop_char (' ');
11077 ++n;
11078 }
11079
11080 return n;
11081 }
11082
11083 /***********************************************************************
11084 Frame Titles
11085 ***********************************************************************/
11086
11087 #ifdef HAVE_WINDOW_SYSTEM
11088
11089 /* Set the title of FRAME, if it has changed. The title format is
11090 Vicon_title_format if FRAME is iconified, otherwise it is
11091 frame_title_format. */
11092
11093 static void
11094 x_consider_frame_title (Lisp_Object frame)
11095 {
11096 struct frame *f = XFRAME (frame);
11097
11098 if (FRAME_WINDOW_P (f)
11099 || FRAME_MINIBUF_ONLY_P (f)
11100 || f->explicit_name)
11101 {
11102 /* Do we have more than one visible frame on this X display? */
11103 Lisp_Object tail, other_frame, fmt;
11104 ptrdiff_t title_start;
11105 char *title;
11106 ptrdiff_t len;
11107 struct it it;
11108 ptrdiff_t count = SPECPDL_INDEX ();
11109
11110 FOR_EACH_FRAME (tail, other_frame)
11111 {
11112 struct frame *tf = XFRAME (other_frame);
11113
11114 if (tf != f
11115 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11116 && !FRAME_MINIBUF_ONLY_P (tf)
11117 && !EQ (other_frame, tip_frame)
11118 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11119 break;
11120 }
11121
11122 /* Set global variable indicating that multiple frames exist. */
11123 multiple_frames = CONSP (tail);
11124
11125 /* Switch to the buffer of selected window of the frame. Set up
11126 mode_line_target so that display_mode_element will output into
11127 mode_line_noprop_buf; then display the title. */
11128 record_unwind_protect (unwind_format_mode_line,
11129 format_mode_line_unwind_data
11130 (f, current_buffer, selected_window, 0));
11131
11132 Fselect_window (f->selected_window, Qt);
11133 set_buffer_internal_1
11134 (XBUFFER (XWINDOW (f->selected_window)->contents));
11135 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11136
11137 mode_line_target = MODE_LINE_TITLE;
11138 title_start = MODE_LINE_NOPROP_LEN (0);
11139 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11140 NULL, DEFAULT_FACE_ID);
11141 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11142 len = MODE_LINE_NOPROP_LEN (title_start);
11143 title = mode_line_noprop_buf + title_start;
11144 unbind_to (count, Qnil);
11145
11146 /* Set the title only if it's changed. This avoids consing in
11147 the common case where it hasn't. (If it turns out that we've
11148 already wasted too much time by walking through the list with
11149 display_mode_element, then we might need to optimize at a
11150 higher level than this.) */
11151 if (! STRINGP (f->name)
11152 || SBYTES (f->name) != len
11153 || memcmp (title, SDATA (f->name), len) != 0)
11154 x_implicitly_set_name (f, make_string (title, len), Qnil);
11155 }
11156 }
11157
11158 #endif /* not HAVE_WINDOW_SYSTEM */
11159
11160 \f
11161 /***********************************************************************
11162 Menu Bars
11163 ***********************************************************************/
11164
11165
11166 /* Prepare for redisplay by updating menu-bar item lists when
11167 appropriate. This can call eval. */
11168
11169 void
11170 prepare_menu_bars (void)
11171 {
11172 int all_windows;
11173 struct gcpro gcpro1, gcpro2;
11174 struct frame *f;
11175 Lisp_Object tooltip_frame;
11176
11177 #ifdef HAVE_WINDOW_SYSTEM
11178 tooltip_frame = tip_frame;
11179 #else
11180 tooltip_frame = Qnil;
11181 #endif
11182
11183 /* Update all frame titles based on their buffer names, etc. We do
11184 this before the menu bars so that the buffer-menu will show the
11185 up-to-date frame titles. */
11186 #ifdef HAVE_WINDOW_SYSTEM
11187 if (windows_or_buffers_changed || update_mode_lines)
11188 {
11189 Lisp_Object tail, frame;
11190
11191 FOR_EACH_FRAME (tail, frame)
11192 {
11193 f = XFRAME (frame);
11194 if (!EQ (frame, tooltip_frame)
11195 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11196 x_consider_frame_title (frame);
11197 }
11198 }
11199 #endif /* HAVE_WINDOW_SYSTEM */
11200
11201 /* Update the menu bar item lists, if appropriate. This has to be
11202 done before any actual redisplay or generation of display lines. */
11203 all_windows = (update_mode_lines
11204 || buffer_shared_and_changed ()
11205 || windows_or_buffers_changed);
11206 if (all_windows)
11207 {
11208 Lisp_Object tail, frame;
11209 ptrdiff_t count = SPECPDL_INDEX ();
11210 /* 1 means that update_menu_bar has run its hooks
11211 so any further calls to update_menu_bar shouldn't do so again. */
11212 int menu_bar_hooks_run = 0;
11213
11214 record_unwind_save_match_data ();
11215
11216 FOR_EACH_FRAME (tail, frame)
11217 {
11218 f = XFRAME (frame);
11219
11220 /* Ignore tooltip frame. */
11221 if (EQ (frame, tooltip_frame))
11222 continue;
11223
11224 /* If a window on this frame changed size, report that to
11225 the user and clear the size-change flag. */
11226 if (FRAME_WINDOW_SIZES_CHANGED (f))
11227 {
11228 Lisp_Object functions;
11229
11230 /* Clear flag first in case we get an error below. */
11231 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11232 functions = Vwindow_size_change_functions;
11233 GCPRO2 (tail, functions);
11234
11235 while (CONSP (functions))
11236 {
11237 if (!EQ (XCAR (functions), Qt))
11238 call1 (XCAR (functions), frame);
11239 functions = XCDR (functions);
11240 }
11241 UNGCPRO;
11242 }
11243
11244 GCPRO1 (tail);
11245 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11246 #ifdef HAVE_WINDOW_SYSTEM
11247 update_tool_bar (f, 0);
11248 #endif
11249 #ifdef HAVE_NS
11250 if (windows_or_buffers_changed
11251 && FRAME_NS_P (f))
11252 ns_set_doc_edited
11253 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11254 #endif
11255 UNGCPRO;
11256 }
11257
11258 unbind_to (count, Qnil);
11259 }
11260 else
11261 {
11262 struct frame *sf = SELECTED_FRAME ();
11263 update_menu_bar (sf, 1, 0);
11264 #ifdef HAVE_WINDOW_SYSTEM
11265 update_tool_bar (sf, 1);
11266 #endif
11267 }
11268 }
11269
11270
11271 /* Update the menu bar item list for frame F. This has to be done
11272 before we start to fill in any display lines, because it can call
11273 eval.
11274
11275 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11276
11277 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11278 already ran the menu bar hooks for this redisplay, so there
11279 is no need to run them again. The return value is the
11280 updated value of this flag, to pass to the next call. */
11281
11282 static int
11283 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11284 {
11285 Lisp_Object window;
11286 register struct window *w;
11287
11288 /* If called recursively during a menu update, do nothing. This can
11289 happen when, for instance, an activate-menubar-hook causes a
11290 redisplay. */
11291 if (inhibit_menubar_update)
11292 return hooks_run;
11293
11294 window = FRAME_SELECTED_WINDOW (f);
11295 w = XWINDOW (window);
11296
11297 if (FRAME_WINDOW_P (f)
11298 ?
11299 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11300 || defined (HAVE_NS) || defined (USE_GTK)
11301 FRAME_EXTERNAL_MENU_BAR (f)
11302 #else
11303 FRAME_MENU_BAR_LINES (f) > 0
11304 #endif
11305 : FRAME_MENU_BAR_LINES (f) > 0)
11306 {
11307 /* If the user has switched buffers or windows, we need to
11308 recompute to reflect the new bindings. But we'll
11309 recompute when update_mode_lines is set too; that means
11310 that people can use force-mode-line-update to request
11311 that the menu bar be recomputed. The adverse effect on
11312 the rest of the redisplay algorithm is about the same as
11313 windows_or_buffers_changed anyway. */
11314 if (windows_or_buffers_changed
11315 /* This used to test w->update_mode_line, but we believe
11316 there is no need to recompute the menu in that case. */
11317 || update_mode_lines
11318 || window_buffer_changed (w))
11319 {
11320 struct buffer *prev = current_buffer;
11321 ptrdiff_t count = SPECPDL_INDEX ();
11322
11323 specbind (Qinhibit_menubar_update, Qt);
11324
11325 set_buffer_internal_1 (XBUFFER (w->contents));
11326 if (save_match_data)
11327 record_unwind_save_match_data ();
11328 if (NILP (Voverriding_local_map_menu_flag))
11329 {
11330 specbind (Qoverriding_terminal_local_map, Qnil);
11331 specbind (Qoverriding_local_map, Qnil);
11332 }
11333
11334 if (!hooks_run)
11335 {
11336 /* Run the Lucid hook. */
11337 safe_run_hooks (Qactivate_menubar_hook);
11338
11339 /* If it has changed current-menubar from previous value,
11340 really recompute the menu-bar from the value. */
11341 if (! NILP (Vlucid_menu_bar_dirty_flag))
11342 call0 (Qrecompute_lucid_menubar);
11343
11344 safe_run_hooks (Qmenu_bar_update_hook);
11345
11346 hooks_run = 1;
11347 }
11348
11349 XSETFRAME (Vmenu_updating_frame, f);
11350 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11351
11352 /* Redisplay the menu bar in case we changed it. */
11353 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11354 || defined (HAVE_NS) || defined (USE_GTK)
11355 if (FRAME_WINDOW_P (f))
11356 {
11357 #if defined (HAVE_NS)
11358 /* All frames on Mac OS share the same menubar. So only
11359 the selected frame should be allowed to set it. */
11360 if (f == SELECTED_FRAME ())
11361 #endif
11362 set_frame_menubar (f, 0, 0);
11363 }
11364 else
11365 /* On a terminal screen, the menu bar is an ordinary screen
11366 line, and this makes it get updated. */
11367 w->update_mode_line = 1;
11368 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11369 /* In the non-toolkit version, the menu bar is an ordinary screen
11370 line, and this makes it get updated. */
11371 w->update_mode_line = 1;
11372 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11373
11374 unbind_to (count, Qnil);
11375 set_buffer_internal_1 (prev);
11376 }
11377 }
11378
11379 return hooks_run;
11380 }
11381
11382
11383 \f
11384 /***********************************************************************
11385 Output Cursor
11386 ***********************************************************************/
11387
11388 #ifdef HAVE_WINDOW_SYSTEM
11389
11390 /* EXPORT:
11391 Nominal cursor position -- where to draw output.
11392 HPOS and VPOS are window relative glyph matrix coordinates.
11393 X and Y are window relative pixel coordinates. */
11394
11395 struct cursor_pos output_cursor;
11396
11397
11398 /* EXPORT:
11399 Set the global variable output_cursor to CURSOR. All cursor
11400 positions are relative to updated_window. */
11401
11402 void
11403 set_output_cursor (struct cursor_pos *cursor)
11404 {
11405 output_cursor.hpos = cursor->hpos;
11406 output_cursor.vpos = cursor->vpos;
11407 output_cursor.x = cursor->x;
11408 output_cursor.y = cursor->y;
11409 }
11410
11411
11412 /* EXPORT for RIF:
11413 Set a nominal cursor position.
11414
11415 HPOS and VPOS are column/row positions in a window glyph matrix. X
11416 and Y are window text area relative pixel positions.
11417
11418 If this is done during an update, updated_window will contain the
11419 window that is being updated and the position is the future output
11420 cursor position for that window. If updated_window is null, use
11421 selected_window and display the cursor at the given position. */
11422
11423 void
11424 x_cursor_to (int vpos, int hpos, int y, int x)
11425 {
11426 struct window *w;
11427
11428 /* If updated_window is not set, work on selected_window. */
11429 if (updated_window)
11430 w = updated_window;
11431 else
11432 w = XWINDOW (selected_window);
11433
11434 /* Set the output cursor. */
11435 output_cursor.hpos = hpos;
11436 output_cursor.vpos = vpos;
11437 output_cursor.x = x;
11438 output_cursor.y = y;
11439
11440 /* If not called as part of an update, really display the cursor.
11441 This will also set the cursor position of W. */
11442 if (updated_window == NULL)
11443 {
11444 block_input ();
11445 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11446 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11447 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11448 unblock_input ();
11449 }
11450 }
11451
11452 #endif /* HAVE_WINDOW_SYSTEM */
11453
11454 \f
11455 /***********************************************************************
11456 Tool-bars
11457 ***********************************************************************/
11458
11459 #ifdef HAVE_WINDOW_SYSTEM
11460
11461 /* Where the mouse was last time we reported a mouse event. */
11462
11463 FRAME_PTR last_mouse_frame;
11464
11465 /* Tool-bar item index of the item on which a mouse button was pressed
11466 or -1. */
11467
11468 int last_tool_bar_item;
11469
11470 /* Select `frame' temporarily without running all the code in
11471 do_switch_frame.
11472 FIXME: Maybe do_switch_frame should be trimmed down similarly
11473 when `norecord' is set. */
11474 static Lisp_Object
11475 fast_set_selected_frame (Lisp_Object frame)
11476 {
11477 if (!EQ (selected_frame, frame))
11478 {
11479 selected_frame = frame;
11480 selected_window = XFRAME (frame)->selected_window;
11481 }
11482 return Qnil;
11483 }
11484
11485 /* Update the tool-bar item list for frame F. This has to be done
11486 before we start to fill in any display lines. Called from
11487 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11488 and restore it here. */
11489
11490 static void
11491 update_tool_bar (struct frame *f, int save_match_data)
11492 {
11493 #if defined (USE_GTK) || defined (HAVE_NS)
11494 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11495 #else
11496 int do_update = WINDOWP (f->tool_bar_window)
11497 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11498 #endif
11499
11500 if (do_update)
11501 {
11502 Lisp_Object window;
11503 struct window *w;
11504
11505 window = FRAME_SELECTED_WINDOW (f);
11506 w = XWINDOW (window);
11507
11508 /* If the user has switched buffers or windows, we need to
11509 recompute to reflect the new bindings. But we'll
11510 recompute when update_mode_lines is set too; that means
11511 that people can use force-mode-line-update to request
11512 that the menu bar be recomputed. The adverse effect on
11513 the rest of the redisplay algorithm is about the same as
11514 windows_or_buffers_changed anyway. */
11515 if (windows_or_buffers_changed
11516 || w->update_mode_line
11517 || update_mode_lines
11518 || window_buffer_changed (w))
11519 {
11520 struct buffer *prev = current_buffer;
11521 ptrdiff_t count = SPECPDL_INDEX ();
11522 Lisp_Object frame, new_tool_bar;
11523 int new_n_tool_bar;
11524 struct gcpro gcpro1;
11525
11526 /* Set current_buffer to the buffer of the selected
11527 window of the frame, so that we get the right local
11528 keymaps. */
11529 set_buffer_internal_1 (XBUFFER (w->contents));
11530
11531 /* Save match data, if we must. */
11532 if (save_match_data)
11533 record_unwind_save_match_data ();
11534
11535 /* Make sure that we don't accidentally use bogus keymaps. */
11536 if (NILP (Voverriding_local_map_menu_flag))
11537 {
11538 specbind (Qoverriding_terminal_local_map, Qnil);
11539 specbind (Qoverriding_local_map, Qnil);
11540 }
11541
11542 GCPRO1 (new_tool_bar);
11543
11544 /* We must temporarily set the selected frame to this frame
11545 before calling tool_bar_items, because the calculation of
11546 the tool-bar keymap uses the selected frame (see
11547 `tool-bar-make-keymap' in tool-bar.el). */
11548 eassert (EQ (selected_window,
11549 /* Since we only explicitly preserve selected_frame,
11550 check that selected_window would be redundant. */
11551 XFRAME (selected_frame)->selected_window));
11552 record_unwind_protect (fast_set_selected_frame, selected_frame);
11553 XSETFRAME (frame, f);
11554 fast_set_selected_frame (frame);
11555
11556 /* Build desired tool-bar items from keymaps. */
11557 new_tool_bar
11558 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11559 &new_n_tool_bar);
11560
11561 /* Redisplay the tool-bar if we changed it. */
11562 if (new_n_tool_bar != f->n_tool_bar_items
11563 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11564 {
11565 /* Redisplay that happens asynchronously due to an expose event
11566 may access f->tool_bar_items. Make sure we update both
11567 variables within BLOCK_INPUT so no such event interrupts. */
11568 block_input ();
11569 fset_tool_bar_items (f, new_tool_bar);
11570 f->n_tool_bar_items = new_n_tool_bar;
11571 w->update_mode_line = 1;
11572 unblock_input ();
11573 }
11574
11575 UNGCPRO;
11576
11577 unbind_to (count, Qnil);
11578 set_buffer_internal_1 (prev);
11579 }
11580 }
11581 }
11582
11583
11584 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11585 F's desired tool-bar contents. F->tool_bar_items must have
11586 been set up previously by calling prepare_menu_bars. */
11587
11588 static void
11589 build_desired_tool_bar_string (struct frame *f)
11590 {
11591 int i, size, size_needed;
11592 struct gcpro gcpro1, gcpro2, gcpro3;
11593 Lisp_Object image, plist, props;
11594
11595 image = plist = props = Qnil;
11596 GCPRO3 (image, plist, props);
11597
11598 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11599 Otherwise, make a new string. */
11600
11601 /* The size of the string we might be able to reuse. */
11602 size = (STRINGP (f->desired_tool_bar_string)
11603 ? SCHARS (f->desired_tool_bar_string)
11604 : 0);
11605
11606 /* We need one space in the string for each image. */
11607 size_needed = f->n_tool_bar_items;
11608
11609 /* Reuse f->desired_tool_bar_string, if possible. */
11610 if (size < size_needed || NILP (f->desired_tool_bar_string))
11611 fset_desired_tool_bar_string
11612 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11613 else
11614 {
11615 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11616 Fremove_text_properties (make_number (0), make_number (size),
11617 props, f->desired_tool_bar_string);
11618 }
11619
11620 /* Put a `display' property on the string for the images to display,
11621 put a `menu_item' property on tool-bar items with a value that
11622 is the index of the item in F's tool-bar item vector. */
11623 for (i = 0; i < f->n_tool_bar_items; ++i)
11624 {
11625 #define PROP(IDX) \
11626 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11627
11628 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11629 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11630 int hmargin, vmargin, relief, idx, end;
11631
11632 /* If image is a vector, choose the image according to the
11633 button state. */
11634 image = PROP (TOOL_BAR_ITEM_IMAGES);
11635 if (VECTORP (image))
11636 {
11637 if (enabled_p)
11638 idx = (selected_p
11639 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11640 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11641 else
11642 idx = (selected_p
11643 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11644 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11645
11646 eassert (ASIZE (image) >= idx);
11647 image = AREF (image, idx);
11648 }
11649 else
11650 idx = -1;
11651
11652 /* Ignore invalid image specifications. */
11653 if (!valid_image_p (image))
11654 continue;
11655
11656 /* Display the tool-bar button pressed, or depressed. */
11657 plist = Fcopy_sequence (XCDR (image));
11658
11659 /* Compute margin and relief to draw. */
11660 relief = (tool_bar_button_relief >= 0
11661 ? tool_bar_button_relief
11662 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11663 hmargin = vmargin = relief;
11664
11665 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11666 INT_MAX - max (hmargin, vmargin)))
11667 {
11668 hmargin += XFASTINT (Vtool_bar_button_margin);
11669 vmargin += XFASTINT (Vtool_bar_button_margin);
11670 }
11671 else if (CONSP (Vtool_bar_button_margin))
11672 {
11673 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11674 INT_MAX - hmargin))
11675 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11676
11677 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11678 INT_MAX - vmargin))
11679 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11680 }
11681
11682 if (auto_raise_tool_bar_buttons_p)
11683 {
11684 /* Add a `:relief' property to the image spec if the item is
11685 selected. */
11686 if (selected_p)
11687 {
11688 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11689 hmargin -= relief;
11690 vmargin -= relief;
11691 }
11692 }
11693 else
11694 {
11695 /* If image is selected, display it pressed, i.e. with a
11696 negative relief. If it's not selected, display it with a
11697 raised relief. */
11698 plist = Fplist_put (plist, QCrelief,
11699 (selected_p
11700 ? make_number (-relief)
11701 : make_number (relief)));
11702 hmargin -= relief;
11703 vmargin -= relief;
11704 }
11705
11706 /* Put a margin around the image. */
11707 if (hmargin || vmargin)
11708 {
11709 if (hmargin == vmargin)
11710 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11711 else
11712 plist = Fplist_put (plist, QCmargin,
11713 Fcons (make_number (hmargin),
11714 make_number (vmargin)));
11715 }
11716
11717 /* If button is not enabled, and we don't have special images
11718 for the disabled state, make the image appear disabled by
11719 applying an appropriate algorithm to it. */
11720 if (!enabled_p && idx < 0)
11721 plist = Fplist_put (plist, QCconversion, Qdisabled);
11722
11723 /* Put a `display' text property on the string for the image to
11724 display. Put a `menu-item' property on the string that gives
11725 the start of this item's properties in the tool-bar items
11726 vector. */
11727 image = Fcons (Qimage, plist);
11728 props = list4 (Qdisplay, image,
11729 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11730
11731 /* Let the last image hide all remaining spaces in the tool bar
11732 string. The string can be longer than needed when we reuse a
11733 previous string. */
11734 if (i + 1 == f->n_tool_bar_items)
11735 end = SCHARS (f->desired_tool_bar_string);
11736 else
11737 end = i + 1;
11738 Fadd_text_properties (make_number (i), make_number (end),
11739 props, f->desired_tool_bar_string);
11740 #undef PROP
11741 }
11742
11743 UNGCPRO;
11744 }
11745
11746
11747 /* Display one line of the tool-bar of frame IT->f.
11748
11749 HEIGHT specifies the desired height of the tool-bar line.
11750 If the actual height of the glyph row is less than HEIGHT, the
11751 row's height is increased to HEIGHT, and the icons are centered
11752 vertically in the new height.
11753
11754 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11755 count a final empty row in case the tool-bar width exactly matches
11756 the window width.
11757 */
11758
11759 static void
11760 display_tool_bar_line (struct it *it, int height)
11761 {
11762 struct glyph_row *row = it->glyph_row;
11763 int max_x = it->last_visible_x;
11764 struct glyph *last;
11765
11766 prepare_desired_row (row);
11767 row->y = it->current_y;
11768
11769 /* Note that this isn't made use of if the face hasn't a box,
11770 so there's no need to check the face here. */
11771 it->start_of_box_run_p = 1;
11772
11773 while (it->current_x < max_x)
11774 {
11775 int x, n_glyphs_before, i, nglyphs;
11776 struct it it_before;
11777
11778 /* Get the next display element. */
11779 if (!get_next_display_element (it))
11780 {
11781 /* Don't count empty row if we are counting needed tool-bar lines. */
11782 if (height < 0 && !it->hpos)
11783 return;
11784 break;
11785 }
11786
11787 /* Produce glyphs. */
11788 n_glyphs_before = row->used[TEXT_AREA];
11789 it_before = *it;
11790
11791 PRODUCE_GLYPHS (it);
11792
11793 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11794 i = 0;
11795 x = it_before.current_x;
11796 while (i < nglyphs)
11797 {
11798 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11799
11800 if (x + glyph->pixel_width > max_x)
11801 {
11802 /* Glyph doesn't fit on line. Backtrack. */
11803 row->used[TEXT_AREA] = n_glyphs_before;
11804 *it = it_before;
11805 /* If this is the only glyph on this line, it will never fit on the
11806 tool-bar, so skip it. But ensure there is at least one glyph,
11807 so we don't accidentally disable the tool-bar. */
11808 if (n_glyphs_before == 0
11809 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11810 break;
11811 goto out;
11812 }
11813
11814 ++it->hpos;
11815 x += glyph->pixel_width;
11816 ++i;
11817 }
11818
11819 /* Stop at line end. */
11820 if (ITERATOR_AT_END_OF_LINE_P (it))
11821 break;
11822
11823 set_iterator_to_next (it, 1);
11824 }
11825
11826 out:;
11827
11828 row->displays_text_p = row->used[TEXT_AREA] != 0;
11829
11830 /* Use default face for the border below the tool bar.
11831
11832 FIXME: When auto-resize-tool-bars is grow-only, there is
11833 no additional border below the possibly empty tool-bar lines.
11834 So to make the extra empty lines look "normal", we have to
11835 use the tool-bar face for the border too. */
11836 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11837 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11838 it->face_id = DEFAULT_FACE_ID;
11839
11840 extend_face_to_end_of_line (it);
11841 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11842 last->right_box_line_p = 1;
11843 if (last == row->glyphs[TEXT_AREA])
11844 last->left_box_line_p = 1;
11845
11846 /* Make line the desired height and center it vertically. */
11847 if ((height -= it->max_ascent + it->max_descent) > 0)
11848 {
11849 /* Don't add more than one line height. */
11850 height %= FRAME_LINE_HEIGHT (it->f);
11851 it->max_ascent += height / 2;
11852 it->max_descent += (height + 1) / 2;
11853 }
11854
11855 compute_line_metrics (it);
11856
11857 /* If line is empty, make it occupy the rest of the tool-bar. */
11858 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11859 {
11860 row->height = row->phys_height = it->last_visible_y - row->y;
11861 row->visible_height = row->height;
11862 row->ascent = row->phys_ascent = 0;
11863 row->extra_line_spacing = 0;
11864 }
11865
11866 row->full_width_p = 1;
11867 row->continued_p = 0;
11868 row->truncated_on_left_p = 0;
11869 row->truncated_on_right_p = 0;
11870
11871 it->current_x = it->hpos = 0;
11872 it->current_y += row->height;
11873 ++it->vpos;
11874 ++it->glyph_row;
11875 }
11876
11877
11878 /* Max tool-bar height. */
11879
11880 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11881 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11882
11883 /* Value is the number of screen lines needed to make all tool-bar
11884 items of frame F visible. The number of actual rows needed is
11885 returned in *N_ROWS if non-NULL. */
11886
11887 static int
11888 tool_bar_lines_needed (struct frame *f, int *n_rows)
11889 {
11890 struct window *w = XWINDOW (f->tool_bar_window);
11891 struct it it;
11892 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11893 the desired matrix, so use (unused) mode-line row as temporary row to
11894 avoid destroying the first tool-bar row. */
11895 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11896
11897 /* Initialize an iterator for iteration over
11898 F->desired_tool_bar_string in the tool-bar window of frame F. */
11899 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11900 it.first_visible_x = 0;
11901 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11902 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11903 it.paragraph_embedding = L2R;
11904
11905 while (!ITERATOR_AT_END_P (&it))
11906 {
11907 clear_glyph_row (temp_row);
11908 it.glyph_row = temp_row;
11909 display_tool_bar_line (&it, -1);
11910 }
11911 clear_glyph_row (temp_row);
11912
11913 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11914 if (n_rows)
11915 *n_rows = it.vpos > 0 ? it.vpos : -1;
11916
11917 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11918 }
11919
11920
11921 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11922 0, 1, 0,
11923 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11924 If FRAME is nil or omitted, use the selected frame. */)
11925 (Lisp_Object frame)
11926 {
11927 struct frame *f = decode_any_frame (frame);
11928 struct window *w;
11929 int nlines = 0;
11930
11931 if (WINDOWP (f->tool_bar_window)
11932 && (w = XWINDOW (f->tool_bar_window),
11933 WINDOW_TOTAL_LINES (w) > 0))
11934 {
11935 update_tool_bar (f, 1);
11936 if (f->n_tool_bar_items)
11937 {
11938 build_desired_tool_bar_string (f);
11939 nlines = tool_bar_lines_needed (f, NULL);
11940 }
11941 }
11942
11943 return make_number (nlines);
11944 }
11945
11946
11947 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11948 height should be changed. */
11949
11950 static int
11951 redisplay_tool_bar (struct frame *f)
11952 {
11953 struct window *w;
11954 struct it it;
11955 struct glyph_row *row;
11956
11957 #if defined (USE_GTK) || defined (HAVE_NS)
11958 if (FRAME_EXTERNAL_TOOL_BAR (f))
11959 update_frame_tool_bar (f);
11960 return 0;
11961 #endif
11962
11963 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11964 do anything. This means you must start with tool-bar-lines
11965 non-zero to get the auto-sizing effect. Or in other words, you
11966 can turn off tool-bars by specifying tool-bar-lines zero. */
11967 if (!WINDOWP (f->tool_bar_window)
11968 || (w = XWINDOW (f->tool_bar_window),
11969 WINDOW_TOTAL_LINES (w) == 0))
11970 return 0;
11971
11972 /* Set up an iterator for the tool-bar window. */
11973 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11974 it.first_visible_x = 0;
11975 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11976 row = it.glyph_row;
11977
11978 /* Build a string that represents the contents of the tool-bar. */
11979 build_desired_tool_bar_string (f);
11980 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11981 /* FIXME: This should be controlled by a user option. But it
11982 doesn't make sense to have an R2L tool bar if the menu bar cannot
11983 be drawn also R2L, and making the menu bar R2L is tricky due
11984 toolkit-specific code that implements it. If an R2L tool bar is
11985 ever supported, display_tool_bar_line should also be augmented to
11986 call unproduce_glyphs like display_line and display_string
11987 do. */
11988 it.paragraph_embedding = L2R;
11989
11990 if (f->n_tool_bar_rows == 0)
11991 {
11992 int nlines;
11993
11994 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11995 nlines != WINDOW_TOTAL_LINES (w)))
11996 {
11997 Lisp_Object frame;
11998 int old_height = WINDOW_TOTAL_LINES (w);
11999
12000 XSETFRAME (frame, f);
12001 Fmodify_frame_parameters (frame,
12002 Fcons (Fcons (Qtool_bar_lines,
12003 make_number (nlines)),
12004 Qnil));
12005 if (WINDOW_TOTAL_LINES (w) != old_height)
12006 {
12007 clear_glyph_matrix (w->desired_matrix);
12008 fonts_changed_p = 1;
12009 return 1;
12010 }
12011 }
12012 }
12013
12014 /* Display as many lines as needed to display all tool-bar items. */
12015
12016 if (f->n_tool_bar_rows > 0)
12017 {
12018 int border, rows, height, extra;
12019
12020 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12021 border = XINT (Vtool_bar_border);
12022 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12023 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12024 else if (EQ (Vtool_bar_border, Qborder_width))
12025 border = f->border_width;
12026 else
12027 border = 0;
12028 if (border < 0)
12029 border = 0;
12030
12031 rows = f->n_tool_bar_rows;
12032 height = max (1, (it.last_visible_y - border) / rows);
12033 extra = it.last_visible_y - border - height * rows;
12034
12035 while (it.current_y < it.last_visible_y)
12036 {
12037 int h = 0;
12038 if (extra > 0 && rows-- > 0)
12039 {
12040 h = (extra + rows - 1) / rows;
12041 extra -= h;
12042 }
12043 display_tool_bar_line (&it, height + h);
12044 }
12045 }
12046 else
12047 {
12048 while (it.current_y < it.last_visible_y)
12049 display_tool_bar_line (&it, 0);
12050 }
12051
12052 /* It doesn't make much sense to try scrolling in the tool-bar
12053 window, so don't do it. */
12054 w->desired_matrix->no_scrolling_p = 1;
12055 w->must_be_updated_p = 1;
12056
12057 if (!NILP (Vauto_resize_tool_bars))
12058 {
12059 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12060 int change_height_p = 0;
12061
12062 /* If we couldn't display everything, change the tool-bar's
12063 height if there is room for more. */
12064 if (IT_STRING_CHARPOS (it) < it.end_charpos
12065 && it.current_y < max_tool_bar_height)
12066 change_height_p = 1;
12067
12068 row = it.glyph_row - 1;
12069
12070 /* If there are blank lines at the end, except for a partially
12071 visible blank line at the end that is smaller than
12072 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12073 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12074 && row->height >= FRAME_LINE_HEIGHT (f))
12075 change_height_p = 1;
12076
12077 /* If row displays tool-bar items, but is partially visible,
12078 change the tool-bar's height. */
12079 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12080 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12081 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12082 change_height_p = 1;
12083
12084 /* Resize windows as needed by changing the `tool-bar-lines'
12085 frame parameter. */
12086 if (change_height_p)
12087 {
12088 Lisp_Object frame;
12089 int old_height = WINDOW_TOTAL_LINES (w);
12090 int nrows;
12091 int nlines = tool_bar_lines_needed (f, &nrows);
12092
12093 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12094 && !f->minimize_tool_bar_window_p)
12095 ? (nlines > old_height)
12096 : (nlines != old_height));
12097 f->minimize_tool_bar_window_p = 0;
12098
12099 if (change_height_p)
12100 {
12101 XSETFRAME (frame, f);
12102 Fmodify_frame_parameters (frame,
12103 Fcons (Fcons (Qtool_bar_lines,
12104 make_number (nlines)),
12105 Qnil));
12106 if (WINDOW_TOTAL_LINES (w) != old_height)
12107 {
12108 clear_glyph_matrix (w->desired_matrix);
12109 f->n_tool_bar_rows = nrows;
12110 fonts_changed_p = 1;
12111 return 1;
12112 }
12113 }
12114 }
12115 }
12116
12117 f->minimize_tool_bar_window_p = 0;
12118 return 0;
12119 }
12120
12121
12122 /* Get information about the tool-bar item which is displayed in GLYPH
12123 on frame F. Return in *PROP_IDX the index where tool-bar item
12124 properties start in F->tool_bar_items. Value is zero if
12125 GLYPH doesn't display a tool-bar item. */
12126
12127 static int
12128 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12129 {
12130 Lisp_Object prop;
12131 int success_p;
12132 int charpos;
12133
12134 /* This function can be called asynchronously, which means we must
12135 exclude any possibility that Fget_text_property signals an
12136 error. */
12137 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12138 charpos = max (0, charpos);
12139
12140 /* Get the text property `menu-item' at pos. The value of that
12141 property is the start index of this item's properties in
12142 F->tool_bar_items. */
12143 prop = Fget_text_property (make_number (charpos),
12144 Qmenu_item, f->current_tool_bar_string);
12145 if (INTEGERP (prop))
12146 {
12147 *prop_idx = XINT (prop);
12148 success_p = 1;
12149 }
12150 else
12151 success_p = 0;
12152
12153 return success_p;
12154 }
12155
12156 \f
12157 /* Get information about the tool-bar item at position X/Y on frame F.
12158 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12159 the current matrix of the tool-bar window of F, or NULL if not
12160 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12161 item in F->tool_bar_items. Value is
12162
12163 -1 if X/Y is not on a tool-bar item
12164 0 if X/Y is on the same item that was highlighted before.
12165 1 otherwise. */
12166
12167 static int
12168 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12169 int *hpos, int *vpos, int *prop_idx)
12170 {
12171 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12172 struct window *w = XWINDOW (f->tool_bar_window);
12173 int area;
12174
12175 /* Find the glyph under X/Y. */
12176 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12177 if (*glyph == NULL)
12178 return -1;
12179
12180 /* Get the start of this tool-bar item's properties in
12181 f->tool_bar_items. */
12182 if (!tool_bar_item_info (f, *glyph, prop_idx))
12183 return -1;
12184
12185 /* Is mouse on the highlighted item? */
12186 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12187 && *vpos >= hlinfo->mouse_face_beg_row
12188 && *vpos <= hlinfo->mouse_face_end_row
12189 && (*vpos > hlinfo->mouse_face_beg_row
12190 || *hpos >= hlinfo->mouse_face_beg_col)
12191 && (*vpos < hlinfo->mouse_face_end_row
12192 || *hpos < hlinfo->mouse_face_end_col
12193 || hlinfo->mouse_face_past_end))
12194 return 0;
12195
12196 return 1;
12197 }
12198
12199
12200 /* EXPORT:
12201 Handle mouse button event on the tool-bar of frame F, at
12202 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12203 0 for button release. MODIFIERS is event modifiers for button
12204 release. */
12205
12206 void
12207 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12208 int modifiers)
12209 {
12210 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12211 struct window *w = XWINDOW (f->tool_bar_window);
12212 int hpos, vpos, prop_idx;
12213 struct glyph *glyph;
12214 Lisp_Object enabled_p;
12215 int ts;
12216
12217 /* If not on the highlighted tool-bar item, and mouse-highlight is
12218 non-nil, return. This is so we generate the tool-bar button
12219 click only when the mouse button is released on the same item as
12220 where it was pressed. However, when mouse-highlight is disabled,
12221 generate the click when the button is released regardless of the
12222 highlight, since tool-bar items are not highlighted in that
12223 case. */
12224 frame_to_window_pixel_xy (w, &x, &y);
12225 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12226 if (ts == -1
12227 || (ts != 0 && !NILP (Vmouse_highlight)))
12228 return;
12229
12230 /* When mouse-highlight is off, generate the click for the item
12231 where the button was pressed, disregarding where it was
12232 released. */
12233 if (NILP (Vmouse_highlight) && !down_p)
12234 prop_idx = last_tool_bar_item;
12235
12236 /* If item is disabled, do nothing. */
12237 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12238 if (NILP (enabled_p))
12239 return;
12240
12241 if (down_p)
12242 {
12243 /* Show item in pressed state. */
12244 if (!NILP (Vmouse_highlight))
12245 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12246 last_tool_bar_item = prop_idx;
12247 }
12248 else
12249 {
12250 Lisp_Object key, frame;
12251 struct input_event event;
12252 EVENT_INIT (event);
12253
12254 /* Show item in released state. */
12255 if (!NILP (Vmouse_highlight))
12256 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12257
12258 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12259
12260 XSETFRAME (frame, f);
12261 event.kind = TOOL_BAR_EVENT;
12262 event.frame_or_window = frame;
12263 event.arg = frame;
12264 kbd_buffer_store_event (&event);
12265
12266 event.kind = TOOL_BAR_EVENT;
12267 event.frame_or_window = frame;
12268 event.arg = key;
12269 event.modifiers = modifiers;
12270 kbd_buffer_store_event (&event);
12271 last_tool_bar_item = -1;
12272 }
12273 }
12274
12275
12276 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12277 tool-bar window-relative coordinates X/Y. Called from
12278 note_mouse_highlight. */
12279
12280 static void
12281 note_tool_bar_highlight (struct frame *f, int x, int y)
12282 {
12283 Lisp_Object window = f->tool_bar_window;
12284 struct window *w = XWINDOW (window);
12285 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12286 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12287 int hpos, vpos;
12288 struct glyph *glyph;
12289 struct glyph_row *row;
12290 int i;
12291 Lisp_Object enabled_p;
12292 int prop_idx;
12293 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12294 int mouse_down_p, rc;
12295
12296 /* Function note_mouse_highlight is called with negative X/Y
12297 values when mouse moves outside of the frame. */
12298 if (x <= 0 || y <= 0)
12299 {
12300 clear_mouse_face (hlinfo);
12301 return;
12302 }
12303
12304 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12305 if (rc < 0)
12306 {
12307 /* Not on tool-bar item. */
12308 clear_mouse_face (hlinfo);
12309 return;
12310 }
12311 else if (rc == 0)
12312 /* On same tool-bar item as before. */
12313 goto set_help_echo;
12314
12315 clear_mouse_face (hlinfo);
12316
12317 /* Mouse is down, but on different tool-bar item? */
12318 mouse_down_p = (dpyinfo->grabbed
12319 && f == last_mouse_frame
12320 && FRAME_LIVE_P (f));
12321 if (mouse_down_p
12322 && last_tool_bar_item != prop_idx)
12323 return;
12324
12325 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12326
12327 /* If tool-bar item is not enabled, don't highlight it. */
12328 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12329 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12330 {
12331 /* Compute the x-position of the glyph. In front and past the
12332 image is a space. We include this in the highlighted area. */
12333 row = MATRIX_ROW (w->current_matrix, vpos);
12334 for (i = x = 0; i < hpos; ++i)
12335 x += row->glyphs[TEXT_AREA][i].pixel_width;
12336
12337 /* Record this as the current active region. */
12338 hlinfo->mouse_face_beg_col = hpos;
12339 hlinfo->mouse_face_beg_row = vpos;
12340 hlinfo->mouse_face_beg_x = x;
12341 hlinfo->mouse_face_beg_y = row->y;
12342 hlinfo->mouse_face_past_end = 0;
12343
12344 hlinfo->mouse_face_end_col = hpos + 1;
12345 hlinfo->mouse_face_end_row = vpos;
12346 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12347 hlinfo->mouse_face_end_y = row->y;
12348 hlinfo->mouse_face_window = window;
12349 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12350
12351 /* Display it as active. */
12352 show_mouse_face (hlinfo, draw);
12353 }
12354
12355 set_help_echo:
12356
12357 /* Set help_echo_string to a help string to display for this tool-bar item.
12358 XTread_socket does the rest. */
12359 help_echo_object = help_echo_window = Qnil;
12360 help_echo_pos = -1;
12361 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12362 if (NILP (help_echo_string))
12363 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12364 }
12365
12366 #endif /* HAVE_WINDOW_SYSTEM */
12367
12368
12369 \f
12370 /************************************************************************
12371 Horizontal scrolling
12372 ************************************************************************/
12373
12374 static int hscroll_window_tree (Lisp_Object);
12375 static int hscroll_windows (Lisp_Object);
12376
12377 /* For all leaf windows in the window tree rooted at WINDOW, set their
12378 hscroll value so that PT is (i) visible in the window, and (ii) so
12379 that it is not within a certain margin at the window's left and
12380 right border. Value is non-zero if any window's hscroll has been
12381 changed. */
12382
12383 static int
12384 hscroll_window_tree (Lisp_Object window)
12385 {
12386 int hscrolled_p = 0;
12387 int hscroll_relative_p = FLOATP (Vhscroll_step);
12388 int hscroll_step_abs = 0;
12389 double hscroll_step_rel = 0;
12390
12391 if (hscroll_relative_p)
12392 {
12393 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12394 if (hscroll_step_rel < 0)
12395 {
12396 hscroll_relative_p = 0;
12397 hscroll_step_abs = 0;
12398 }
12399 }
12400 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12401 {
12402 hscroll_step_abs = XINT (Vhscroll_step);
12403 if (hscroll_step_abs < 0)
12404 hscroll_step_abs = 0;
12405 }
12406 else
12407 hscroll_step_abs = 0;
12408
12409 while (WINDOWP (window))
12410 {
12411 struct window *w = XWINDOW (window);
12412
12413 if (WINDOWP (w->contents))
12414 hscrolled_p |= hscroll_window_tree (w->contents);
12415 else if (w->cursor.vpos >= 0)
12416 {
12417 int h_margin;
12418 int text_area_width;
12419 struct glyph_row *current_cursor_row
12420 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12421 struct glyph_row *desired_cursor_row
12422 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12423 struct glyph_row *cursor_row
12424 = (desired_cursor_row->enabled_p
12425 ? desired_cursor_row
12426 : current_cursor_row);
12427 int row_r2l_p = cursor_row->reversed_p;
12428
12429 text_area_width = window_box_width (w, TEXT_AREA);
12430
12431 /* Scroll when cursor is inside this scroll margin. */
12432 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12433
12434 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12435 /* For left-to-right rows, hscroll when cursor is either
12436 (i) inside the right hscroll margin, or (ii) if it is
12437 inside the left margin and the window is already
12438 hscrolled. */
12439 && ((!row_r2l_p
12440 && ((w->hscroll
12441 && w->cursor.x <= h_margin)
12442 || (cursor_row->enabled_p
12443 && cursor_row->truncated_on_right_p
12444 && (w->cursor.x >= text_area_width - h_margin))))
12445 /* For right-to-left rows, the logic is similar,
12446 except that rules for scrolling to left and right
12447 are reversed. E.g., if cursor.x <= h_margin, we
12448 need to hscroll "to the right" unconditionally,
12449 and that will scroll the screen to the left so as
12450 to reveal the next portion of the row. */
12451 || (row_r2l_p
12452 && ((cursor_row->enabled_p
12453 /* FIXME: It is confusing to set the
12454 truncated_on_right_p flag when R2L rows
12455 are actually truncated on the left. */
12456 && cursor_row->truncated_on_right_p
12457 && w->cursor.x <= h_margin)
12458 || (w->hscroll
12459 && (w->cursor.x >= text_area_width - h_margin))))))
12460 {
12461 struct it it;
12462 ptrdiff_t hscroll;
12463 struct buffer *saved_current_buffer;
12464 ptrdiff_t pt;
12465 int wanted_x;
12466
12467 /* Find point in a display of infinite width. */
12468 saved_current_buffer = current_buffer;
12469 current_buffer = XBUFFER (w->contents);
12470
12471 if (w == XWINDOW (selected_window))
12472 pt = PT;
12473 else
12474 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12475
12476 /* Move iterator to pt starting at cursor_row->start in
12477 a line with infinite width. */
12478 init_to_row_start (&it, w, cursor_row);
12479 it.last_visible_x = INFINITY;
12480 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12481 current_buffer = saved_current_buffer;
12482
12483 /* Position cursor in window. */
12484 if (!hscroll_relative_p && hscroll_step_abs == 0)
12485 hscroll = max (0, (it.current_x
12486 - (ITERATOR_AT_END_OF_LINE_P (&it)
12487 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12488 : (text_area_width / 2))))
12489 / FRAME_COLUMN_WIDTH (it.f);
12490 else if ((!row_r2l_p
12491 && w->cursor.x >= text_area_width - h_margin)
12492 || (row_r2l_p && w->cursor.x <= h_margin))
12493 {
12494 if (hscroll_relative_p)
12495 wanted_x = text_area_width * (1 - hscroll_step_rel)
12496 - h_margin;
12497 else
12498 wanted_x = text_area_width
12499 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12500 - h_margin;
12501 hscroll
12502 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12503 }
12504 else
12505 {
12506 if (hscroll_relative_p)
12507 wanted_x = text_area_width * hscroll_step_rel
12508 + h_margin;
12509 else
12510 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12511 + h_margin;
12512 hscroll
12513 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12514 }
12515 hscroll = max (hscroll, w->min_hscroll);
12516
12517 /* Don't prevent redisplay optimizations if hscroll
12518 hasn't changed, as it will unnecessarily slow down
12519 redisplay. */
12520 if (w->hscroll != hscroll)
12521 {
12522 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12523 w->hscroll = hscroll;
12524 hscrolled_p = 1;
12525 }
12526 }
12527 }
12528
12529 window = w->next;
12530 }
12531
12532 /* Value is non-zero if hscroll of any leaf window has been changed. */
12533 return hscrolled_p;
12534 }
12535
12536
12537 /* Set hscroll so that cursor is visible and not inside horizontal
12538 scroll margins for all windows in the tree rooted at WINDOW. See
12539 also hscroll_window_tree above. Value is non-zero if any window's
12540 hscroll has been changed. If it has, desired matrices on the frame
12541 of WINDOW are cleared. */
12542
12543 static int
12544 hscroll_windows (Lisp_Object window)
12545 {
12546 int hscrolled_p = hscroll_window_tree (window);
12547 if (hscrolled_p)
12548 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12549 return hscrolled_p;
12550 }
12551
12552
12553 \f
12554 /************************************************************************
12555 Redisplay
12556 ************************************************************************/
12557
12558 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12559 to a non-zero value. This is sometimes handy to have in a debugger
12560 session. */
12561
12562 #ifdef GLYPH_DEBUG
12563
12564 /* First and last unchanged row for try_window_id. */
12565
12566 static int debug_first_unchanged_at_end_vpos;
12567 static int debug_last_unchanged_at_beg_vpos;
12568
12569 /* Delta vpos and y. */
12570
12571 static int debug_dvpos, debug_dy;
12572
12573 /* Delta in characters and bytes for try_window_id. */
12574
12575 static ptrdiff_t debug_delta, debug_delta_bytes;
12576
12577 /* Values of window_end_pos and window_end_vpos at the end of
12578 try_window_id. */
12579
12580 static ptrdiff_t debug_end_vpos;
12581
12582 /* Append a string to W->desired_matrix->method. FMT is a printf
12583 format string. If trace_redisplay_p is non-zero also printf the
12584 resulting string to stderr. */
12585
12586 static void debug_method_add (struct window *, char const *, ...)
12587 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12588
12589 static void
12590 debug_method_add (struct window *w, char const *fmt, ...)
12591 {
12592 char *method = w->desired_matrix->method;
12593 int len = strlen (method);
12594 int size = sizeof w->desired_matrix->method;
12595 int remaining = size - len - 1;
12596 va_list ap;
12597
12598 if (len && remaining)
12599 {
12600 method[len] = '|';
12601 --remaining, ++len;
12602 }
12603
12604 va_start (ap, fmt);
12605 vsnprintf (method + len, remaining + 1, fmt, ap);
12606 va_end (ap);
12607
12608 if (trace_redisplay_p)
12609 fprintf (stderr, "%p (%s): %s\n",
12610 w,
12611 ((BUFFERP (w->contents)
12612 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12613 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12614 : "no buffer"),
12615 method + len);
12616 }
12617
12618 #endif /* GLYPH_DEBUG */
12619
12620
12621 /* Value is non-zero if all changes in window W, which displays
12622 current_buffer, are in the text between START and END. START is a
12623 buffer position, END is given as a distance from Z. Used in
12624 redisplay_internal for display optimization. */
12625
12626 static int
12627 text_outside_line_unchanged_p (struct window *w,
12628 ptrdiff_t start, ptrdiff_t end)
12629 {
12630 int unchanged_p = 1;
12631
12632 /* If text or overlays have changed, see where. */
12633 if (window_outdated (w))
12634 {
12635 /* Gap in the line? */
12636 if (GPT < start || Z - GPT < end)
12637 unchanged_p = 0;
12638
12639 /* Changes start in front of the line, or end after it? */
12640 if (unchanged_p
12641 && (BEG_UNCHANGED < start - 1
12642 || END_UNCHANGED < end))
12643 unchanged_p = 0;
12644
12645 /* If selective display, can't optimize if changes start at the
12646 beginning of the line. */
12647 if (unchanged_p
12648 && INTEGERP (BVAR (current_buffer, selective_display))
12649 && XINT (BVAR (current_buffer, selective_display)) > 0
12650 && (BEG_UNCHANGED < start || GPT <= start))
12651 unchanged_p = 0;
12652
12653 /* If there are overlays at the start or end of the line, these
12654 may have overlay strings with newlines in them. A change at
12655 START, for instance, may actually concern the display of such
12656 overlay strings as well, and they are displayed on different
12657 lines. So, quickly rule out this case. (For the future, it
12658 might be desirable to implement something more telling than
12659 just BEG/END_UNCHANGED.) */
12660 if (unchanged_p)
12661 {
12662 if (BEG + BEG_UNCHANGED == start
12663 && overlay_touches_p (start))
12664 unchanged_p = 0;
12665 if (END_UNCHANGED == end
12666 && overlay_touches_p (Z - end))
12667 unchanged_p = 0;
12668 }
12669
12670 /* Under bidi reordering, adding or deleting a character in the
12671 beginning of a paragraph, before the first strong directional
12672 character, can change the base direction of the paragraph (unless
12673 the buffer specifies a fixed paragraph direction), which will
12674 require to redisplay the whole paragraph. It might be worthwhile
12675 to find the paragraph limits and widen the range of redisplayed
12676 lines to that, but for now just give up this optimization. */
12677 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12678 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12679 unchanged_p = 0;
12680 }
12681
12682 return unchanged_p;
12683 }
12684
12685
12686 /* Do a frame update, taking possible shortcuts into account. This is
12687 the main external entry point for redisplay.
12688
12689 If the last redisplay displayed an echo area message and that message
12690 is no longer requested, we clear the echo area or bring back the
12691 mini-buffer if that is in use. */
12692
12693 void
12694 redisplay (void)
12695 {
12696 redisplay_internal ();
12697 }
12698
12699
12700 static Lisp_Object
12701 overlay_arrow_string_or_property (Lisp_Object var)
12702 {
12703 Lisp_Object val;
12704
12705 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12706 return val;
12707
12708 return Voverlay_arrow_string;
12709 }
12710
12711 /* Return 1 if there are any overlay-arrows in current_buffer. */
12712 static int
12713 overlay_arrow_in_current_buffer_p (void)
12714 {
12715 Lisp_Object vlist;
12716
12717 for (vlist = Voverlay_arrow_variable_list;
12718 CONSP (vlist);
12719 vlist = XCDR (vlist))
12720 {
12721 Lisp_Object var = XCAR (vlist);
12722 Lisp_Object val;
12723
12724 if (!SYMBOLP (var))
12725 continue;
12726 val = find_symbol_value (var);
12727 if (MARKERP (val)
12728 && current_buffer == XMARKER (val)->buffer)
12729 return 1;
12730 }
12731 return 0;
12732 }
12733
12734
12735 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12736 has changed. */
12737
12738 static int
12739 overlay_arrows_changed_p (void)
12740 {
12741 Lisp_Object vlist;
12742
12743 for (vlist = Voverlay_arrow_variable_list;
12744 CONSP (vlist);
12745 vlist = XCDR (vlist))
12746 {
12747 Lisp_Object var = XCAR (vlist);
12748 Lisp_Object val, pstr;
12749
12750 if (!SYMBOLP (var))
12751 continue;
12752 val = find_symbol_value (var);
12753 if (!MARKERP (val))
12754 continue;
12755 if (! EQ (COERCE_MARKER (val),
12756 Fget (var, Qlast_arrow_position))
12757 || ! (pstr = overlay_arrow_string_or_property (var),
12758 EQ (pstr, Fget (var, Qlast_arrow_string))))
12759 return 1;
12760 }
12761 return 0;
12762 }
12763
12764 /* Mark overlay arrows to be updated on next redisplay. */
12765
12766 static void
12767 update_overlay_arrows (int up_to_date)
12768 {
12769 Lisp_Object vlist;
12770
12771 for (vlist = Voverlay_arrow_variable_list;
12772 CONSP (vlist);
12773 vlist = XCDR (vlist))
12774 {
12775 Lisp_Object var = XCAR (vlist);
12776
12777 if (!SYMBOLP (var))
12778 continue;
12779
12780 if (up_to_date > 0)
12781 {
12782 Lisp_Object val = find_symbol_value (var);
12783 Fput (var, Qlast_arrow_position,
12784 COERCE_MARKER (val));
12785 Fput (var, Qlast_arrow_string,
12786 overlay_arrow_string_or_property (var));
12787 }
12788 else if (up_to_date < 0
12789 || !NILP (Fget (var, Qlast_arrow_position)))
12790 {
12791 Fput (var, Qlast_arrow_position, Qt);
12792 Fput (var, Qlast_arrow_string, Qt);
12793 }
12794 }
12795 }
12796
12797
12798 /* Return overlay arrow string to display at row.
12799 Return integer (bitmap number) for arrow bitmap in left fringe.
12800 Return nil if no overlay arrow. */
12801
12802 static Lisp_Object
12803 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12804 {
12805 Lisp_Object vlist;
12806
12807 for (vlist = Voverlay_arrow_variable_list;
12808 CONSP (vlist);
12809 vlist = XCDR (vlist))
12810 {
12811 Lisp_Object var = XCAR (vlist);
12812 Lisp_Object val;
12813
12814 if (!SYMBOLP (var))
12815 continue;
12816
12817 val = find_symbol_value (var);
12818
12819 if (MARKERP (val)
12820 && current_buffer == XMARKER (val)->buffer
12821 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12822 {
12823 if (FRAME_WINDOW_P (it->f)
12824 /* FIXME: if ROW->reversed_p is set, this should test
12825 the right fringe, not the left one. */
12826 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12827 {
12828 #ifdef HAVE_WINDOW_SYSTEM
12829 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12830 {
12831 int fringe_bitmap;
12832 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12833 return make_number (fringe_bitmap);
12834 }
12835 #endif
12836 return make_number (-1); /* Use default arrow bitmap. */
12837 }
12838 return overlay_arrow_string_or_property (var);
12839 }
12840 }
12841
12842 return Qnil;
12843 }
12844
12845 /* Return 1 if point moved out of or into a composition. Otherwise
12846 return 0. PREV_BUF and PREV_PT are the last point buffer and
12847 position. BUF and PT are the current point buffer and position. */
12848
12849 static int
12850 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12851 struct buffer *buf, ptrdiff_t pt)
12852 {
12853 ptrdiff_t start, end;
12854 Lisp_Object prop;
12855 Lisp_Object buffer;
12856
12857 XSETBUFFER (buffer, buf);
12858 /* Check a composition at the last point if point moved within the
12859 same buffer. */
12860 if (prev_buf == buf)
12861 {
12862 if (prev_pt == pt)
12863 /* Point didn't move. */
12864 return 0;
12865
12866 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12867 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12868 && COMPOSITION_VALID_P (start, end, prop)
12869 && start < prev_pt && end > prev_pt)
12870 /* The last point was within the composition. Return 1 iff
12871 point moved out of the composition. */
12872 return (pt <= start || pt >= end);
12873 }
12874
12875 /* Check a composition at the current point. */
12876 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12877 && find_composition (pt, -1, &start, &end, &prop, buffer)
12878 && COMPOSITION_VALID_P (start, end, prop)
12879 && start < pt && end > pt);
12880 }
12881
12882
12883 /* Reconsider the setting of B->clip_changed which is displayed
12884 in window W. */
12885
12886 static void
12887 reconsider_clip_changes (struct window *w, struct buffer *b)
12888 {
12889 if (b->clip_changed
12890 && w->window_end_valid
12891 && w->current_matrix->buffer == b
12892 && w->current_matrix->zv == BUF_ZV (b)
12893 && w->current_matrix->begv == BUF_BEGV (b))
12894 b->clip_changed = 0;
12895
12896 /* If display wasn't paused, and W is not a tool bar window, see if
12897 point has been moved into or out of a composition. In that case,
12898 we set b->clip_changed to 1 to force updating the screen. If
12899 b->clip_changed has already been set to 1, we can skip this
12900 check. */
12901 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12902 {
12903 ptrdiff_t pt;
12904
12905 if (w == XWINDOW (selected_window))
12906 pt = PT;
12907 else
12908 pt = marker_position (w->pointm);
12909
12910 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12911 || pt != w->last_point)
12912 && check_point_in_composition (w->current_matrix->buffer,
12913 w->last_point,
12914 XBUFFER (w->contents), pt))
12915 b->clip_changed = 1;
12916 }
12917 }
12918 \f
12919
12920 #define STOP_POLLING \
12921 do { if (! polling_stopped_here) stop_polling (); \
12922 polling_stopped_here = 1; } while (0)
12923
12924 #define RESUME_POLLING \
12925 do { if (polling_stopped_here) start_polling (); \
12926 polling_stopped_here = 0; } while (0)
12927
12928
12929 /* Perhaps in the future avoid recentering windows if it
12930 is not necessary; currently that causes some problems. */
12931
12932 static void
12933 redisplay_internal (void)
12934 {
12935 struct window *w = XWINDOW (selected_window);
12936 struct window *sw;
12937 struct frame *fr;
12938 int pending;
12939 int must_finish = 0;
12940 struct text_pos tlbufpos, tlendpos;
12941 int number_of_visible_frames;
12942 ptrdiff_t count, count1;
12943 struct frame *sf;
12944 int polling_stopped_here = 0;
12945 Lisp_Object tail, frame;
12946
12947 /* Non-zero means redisplay has to consider all windows on all
12948 frames. Zero means, only selected_window is considered. */
12949 int consider_all_windows_p;
12950
12951 /* Non-zero means redisplay has to redisplay the miniwindow. */
12952 int update_miniwindow_p = 0;
12953
12954 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12955
12956 /* No redisplay if running in batch mode or frame is not yet fully
12957 initialized, or redisplay is explicitly turned off by setting
12958 Vinhibit_redisplay. */
12959 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12960 || !NILP (Vinhibit_redisplay))
12961 return;
12962
12963 /* Don't examine these until after testing Vinhibit_redisplay.
12964 When Emacs is shutting down, perhaps because its connection to
12965 X has dropped, we should not look at them at all. */
12966 fr = XFRAME (w->frame);
12967 sf = SELECTED_FRAME ();
12968
12969 if (!fr->glyphs_initialized_p)
12970 return;
12971
12972 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12973 if (popup_activated ())
12974 return;
12975 #endif
12976
12977 /* I don't think this happens but let's be paranoid. */
12978 if (redisplaying_p)
12979 return;
12980
12981 /* Record a function that clears redisplaying_p
12982 when we leave this function. */
12983 count = SPECPDL_INDEX ();
12984 record_unwind_protect (unwind_redisplay, selected_frame);
12985 redisplaying_p = 1;
12986 specbind (Qinhibit_free_realized_faces, Qnil);
12987
12988 /* Record this function, so it appears on the profiler's backtraces. */
12989 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12990
12991 FOR_EACH_FRAME (tail, frame)
12992 XFRAME (frame)->already_hscrolled_p = 0;
12993
12994 retry:
12995 /* Remember the currently selected window. */
12996 sw = w;
12997
12998 pending = 0;
12999 reconsider_clip_changes (w, current_buffer);
13000 last_escape_glyph_frame = NULL;
13001 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13002 last_glyphless_glyph_frame = NULL;
13003 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13004
13005 /* If new fonts have been loaded that make a glyph matrix adjustment
13006 necessary, do it. */
13007 if (fonts_changed_p)
13008 {
13009 adjust_glyphs (NULL);
13010 ++windows_or_buffers_changed;
13011 fonts_changed_p = 0;
13012 }
13013
13014 /* If face_change_count is non-zero, init_iterator will free all
13015 realized faces, which includes the faces referenced from current
13016 matrices. So, we can't reuse current matrices in this case. */
13017 if (face_change_count)
13018 ++windows_or_buffers_changed;
13019
13020 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13021 && FRAME_TTY (sf)->previous_frame != sf)
13022 {
13023 /* Since frames on a single ASCII terminal share the same
13024 display area, displaying a different frame means redisplay
13025 the whole thing. */
13026 windows_or_buffers_changed++;
13027 SET_FRAME_GARBAGED (sf);
13028 #ifndef DOS_NT
13029 set_tty_color_mode (FRAME_TTY (sf), sf);
13030 #endif
13031 FRAME_TTY (sf)->previous_frame = sf;
13032 }
13033
13034 /* Set the visible flags for all frames. Do this before checking for
13035 resized or garbaged frames; they want to know if their frames are
13036 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13037 number_of_visible_frames = 0;
13038
13039 FOR_EACH_FRAME (tail, frame)
13040 {
13041 struct frame *f = XFRAME (frame);
13042
13043 if (FRAME_VISIBLE_P (f))
13044 ++number_of_visible_frames;
13045 clear_desired_matrices (f);
13046 }
13047
13048 /* Notice any pending interrupt request to change frame size. */
13049 do_pending_window_change (1);
13050
13051 /* do_pending_window_change could change the selected_window due to
13052 frame resizing which makes the selected window too small. */
13053 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13054 {
13055 sw = w;
13056 reconsider_clip_changes (w, current_buffer);
13057 }
13058
13059 /* Clear frames marked as garbaged. */
13060 clear_garbaged_frames ();
13061
13062 /* Build menubar and tool-bar items. */
13063 if (NILP (Vmemory_full))
13064 prepare_menu_bars ();
13065
13066 if (windows_or_buffers_changed)
13067 update_mode_lines++;
13068
13069 /* Detect case that we need to write or remove a star in the mode line. */
13070 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13071 {
13072 w->update_mode_line = 1;
13073 if (buffer_shared_and_changed ())
13074 update_mode_lines++;
13075 }
13076
13077 /* Avoid invocation of point motion hooks by `current_column' below. */
13078 count1 = SPECPDL_INDEX ();
13079 specbind (Qinhibit_point_motion_hooks, Qt);
13080
13081 if (mode_line_update_needed (w))
13082 w->update_mode_line = 1;
13083
13084 unbind_to (count1, Qnil);
13085
13086 consider_all_windows_p = (update_mode_lines
13087 || buffer_shared_and_changed ()
13088 || cursor_type_changed);
13089
13090 /* If specs for an arrow have changed, do thorough redisplay
13091 to ensure we remove any arrow that should no longer exist. */
13092 if (overlay_arrows_changed_p ())
13093 consider_all_windows_p = windows_or_buffers_changed = 1;
13094
13095 /* Normally the message* functions will have already displayed and
13096 updated the echo area, but the frame may have been trashed, or
13097 the update may have been preempted, so display the echo area
13098 again here. Checking message_cleared_p captures the case that
13099 the echo area should be cleared. */
13100 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13101 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13102 || (message_cleared_p
13103 && minibuf_level == 0
13104 /* If the mini-window is currently selected, this means the
13105 echo-area doesn't show through. */
13106 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13107 {
13108 int window_height_changed_p = echo_area_display (0);
13109
13110 if (message_cleared_p)
13111 update_miniwindow_p = 1;
13112
13113 must_finish = 1;
13114
13115 /* If we don't display the current message, don't clear the
13116 message_cleared_p flag, because, if we did, we wouldn't clear
13117 the echo area in the next redisplay which doesn't preserve
13118 the echo area. */
13119 if (!display_last_displayed_message_p)
13120 message_cleared_p = 0;
13121
13122 if (fonts_changed_p)
13123 goto retry;
13124 else if (window_height_changed_p)
13125 {
13126 consider_all_windows_p = 1;
13127 ++update_mode_lines;
13128 ++windows_or_buffers_changed;
13129
13130 /* If window configuration was changed, frames may have been
13131 marked garbaged. Clear them or we will experience
13132 surprises wrt scrolling. */
13133 clear_garbaged_frames ();
13134 }
13135 }
13136 else if (EQ (selected_window, minibuf_window)
13137 && (current_buffer->clip_changed || window_outdated (w))
13138 && resize_mini_window (w, 0))
13139 {
13140 /* Resized active mini-window to fit the size of what it is
13141 showing if its contents might have changed. */
13142 must_finish = 1;
13143 /* FIXME: this causes all frames to be updated, which seems unnecessary
13144 since only the current frame needs to be considered. This function
13145 needs to be rewritten with two variables, consider_all_windows and
13146 consider_all_frames. */
13147 consider_all_windows_p = 1;
13148 ++windows_or_buffers_changed;
13149 ++update_mode_lines;
13150
13151 /* If window configuration was changed, frames may have been
13152 marked garbaged. Clear them or we will experience
13153 surprises wrt scrolling. */
13154 clear_garbaged_frames ();
13155 }
13156
13157 /* If showing the region, and mark has changed, we must redisplay
13158 the whole window. The assignment to this_line_start_pos prevents
13159 the optimization directly below this if-statement. */
13160 if (((!NILP (Vtransient_mark_mode)
13161 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13162 != (w->region_showing > 0))
13163 || (w->region_showing
13164 && w->region_showing
13165 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13166 CHARPOS (this_line_start_pos) = 0;
13167
13168 /* Optimize the case that only the line containing the cursor in the
13169 selected window has changed. Variables starting with this_ are
13170 set in display_line and record information about the line
13171 containing the cursor. */
13172 tlbufpos = this_line_start_pos;
13173 tlendpos = this_line_end_pos;
13174 if (!consider_all_windows_p
13175 && CHARPOS (tlbufpos) > 0
13176 && !w->update_mode_line
13177 && !current_buffer->clip_changed
13178 && !current_buffer->prevent_redisplay_optimizations_p
13179 && FRAME_VISIBLE_P (XFRAME (w->frame))
13180 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13181 /* Make sure recorded data applies to current buffer, etc. */
13182 && this_line_buffer == current_buffer
13183 && current_buffer == XBUFFER (w->contents)
13184 && !w->force_start
13185 && !w->optional_new_start
13186 /* Point must be on the line that we have info recorded about. */
13187 && PT >= CHARPOS (tlbufpos)
13188 && PT <= Z - CHARPOS (tlendpos)
13189 /* All text outside that line, including its final newline,
13190 must be unchanged. */
13191 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13192 CHARPOS (tlendpos)))
13193 {
13194 if (CHARPOS (tlbufpos) > BEGV
13195 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13196 && (CHARPOS (tlbufpos) == ZV
13197 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13198 /* Former continuation line has disappeared by becoming empty. */
13199 goto cancel;
13200 else if (window_outdated (w) || MINI_WINDOW_P (w))
13201 {
13202 /* We have to handle the case of continuation around a
13203 wide-column character (see the comment in indent.c around
13204 line 1340).
13205
13206 For instance, in the following case:
13207
13208 -------- Insert --------
13209 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13210 J_I_ ==> J_I_ `^^' are cursors.
13211 ^^ ^^
13212 -------- --------
13213
13214 As we have to redraw the line above, we cannot use this
13215 optimization. */
13216
13217 struct it it;
13218 int line_height_before = this_line_pixel_height;
13219
13220 /* Note that start_display will handle the case that the
13221 line starting at tlbufpos is a continuation line. */
13222 start_display (&it, w, tlbufpos);
13223
13224 /* Implementation note: It this still necessary? */
13225 if (it.current_x != this_line_start_x)
13226 goto cancel;
13227
13228 TRACE ((stderr, "trying display optimization 1\n"));
13229 w->cursor.vpos = -1;
13230 overlay_arrow_seen = 0;
13231 it.vpos = this_line_vpos;
13232 it.current_y = this_line_y;
13233 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13234 display_line (&it);
13235
13236 /* If line contains point, is not continued,
13237 and ends at same distance from eob as before, we win. */
13238 if (w->cursor.vpos >= 0
13239 /* Line is not continued, otherwise this_line_start_pos
13240 would have been set to 0 in display_line. */
13241 && CHARPOS (this_line_start_pos)
13242 /* Line ends as before. */
13243 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13244 /* Line has same height as before. Otherwise other lines
13245 would have to be shifted up or down. */
13246 && this_line_pixel_height == line_height_before)
13247 {
13248 /* If this is not the window's last line, we must adjust
13249 the charstarts of the lines below. */
13250 if (it.current_y < it.last_visible_y)
13251 {
13252 struct glyph_row *row
13253 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13254 ptrdiff_t delta, delta_bytes;
13255
13256 /* We used to distinguish between two cases here,
13257 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13258 when the line ends in a newline or the end of the
13259 buffer's accessible portion. But both cases did
13260 the same, so they were collapsed. */
13261 delta = (Z
13262 - CHARPOS (tlendpos)
13263 - MATRIX_ROW_START_CHARPOS (row));
13264 delta_bytes = (Z_BYTE
13265 - BYTEPOS (tlendpos)
13266 - MATRIX_ROW_START_BYTEPOS (row));
13267
13268 increment_matrix_positions (w->current_matrix,
13269 this_line_vpos + 1,
13270 w->current_matrix->nrows,
13271 delta, delta_bytes);
13272 }
13273
13274 /* If this row displays text now but previously didn't,
13275 or vice versa, w->window_end_vpos may have to be
13276 adjusted. */
13277 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13278 {
13279 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13280 wset_window_end_vpos (w, make_number (this_line_vpos));
13281 }
13282 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13283 && this_line_vpos > 0)
13284 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13285 w->window_end_valid = 0;
13286
13287 /* Update hint: No need to try to scroll in update_window. */
13288 w->desired_matrix->no_scrolling_p = 1;
13289
13290 #ifdef GLYPH_DEBUG
13291 *w->desired_matrix->method = 0;
13292 debug_method_add (w, "optimization 1");
13293 #endif
13294 #ifdef HAVE_WINDOW_SYSTEM
13295 update_window_fringes (w, 0);
13296 #endif
13297 goto update;
13298 }
13299 else
13300 goto cancel;
13301 }
13302 else if (/* Cursor position hasn't changed. */
13303 PT == w->last_point
13304 /* Make sure the cursor was last displayed
13305 in this window. Otherwise we have to reposition it. */
13306 && 0 <= w->cursor.vpos
13307 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13308 {
13309 if (!must_finish)
13310 {
13311 do_pending_window_change (1);
13312 /* If selected_window changed, redisplay again. */
13313 if (WINDOWP (selected_window)
13314 && (w = XWINDOW (selected_window)) != sw)
13315 goto retry;
13316
13317 /* We used to always goto end_of_redisplay here, but this
13318 isn't enough if we have a blinking cursor. */
13319 if (w->cursor_off_p == w->last_cursor_off_p)
13320 goto end_of_redisplay;
13321 }
13322 goto update;
13323 }
13324 /* If highlighting the region, or if the cursor is in the echo area,
13325 then we can't just move the cursor. */
13326 else if (! (!NILP (Vtransient_mark_mode)
13327 && !NILP (BVAR (current_buffer, mark_active)))
13328 && (EQ (selected_window,
13329 BVAR (current_buffer, last_selected_window))
13330 || highlight_nonselected_windows)
13331 && !w->region_showing
13332 && NILP (Vshow_trailing_whitespace)
13333 && !cursor_in_echo_area)
13334 {
13335 struct it it;
13336 struct glyph_row *row;
13337
13338 /* Skip from tlbufpos to PT and see where it is. Note that
13339 PT may be in invisible text. If so, we will end at the
13340 next visible position. */
13341 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13342 NULL, DEFAULT_FACE_ID);
13343 it.current_x = this_line_start_x;
13344 it.current_y = this_line_y;
13345 it.vpos = this_line_vpos;
13346
13347 /* The call to move_it_to stops in front of PT, but
13348 moves over before-strings. */
13349 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13350
13351 if (it.vpos == this_line_vpos
13352 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13353 row->enabled_p))
13354 {
13355 eassert (this_line_vpos == it.vpos);
13356 eassert (this_line_y == it.current_y);
13357 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13358 #ifdef GLYPH_DEBUG
13359 *w->desired_matrix->method = 0;
13360 debug_method_add (w, "optimization 3");
13361 #endif
13362 goto update;
13363 }
13364 else
13365 goto cancel;
13366 }
13367
13368 cancel:
13369 /* Text changed drastically or point moved off of line. */
13370 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13371 }
13372
13373 CHARPOS (this_line_start_pos) = 0;
13374 consider_all_windows_p |= buffer_shared_and_changed ();
13375 ++clear_face_cache_count;
13376 #ifdef HAVE_WINDOW_SYSTEM
13377 ++clear_image_cache_count;
13378 #endif
13379
13380 /* Build desired matrices, and update the display. If
13381 consider_all_windows_p is non-zero, do it for all windows on all
13382 frames. Otherwise do it for selected_window, only. */
13383
13384 if (consider_all_windows_p)
13385 {
13386 FOR_EACH_FRAME (tail, frame)
13387 XFRAME (frame)->updated_p = 0;
13388
13389 FOR_EACH_FRAME (tail, frame)
13390 {
13391 struct frame *f = XFRAME (frame);
13392
13393 /* We don't have to do anything for unselected terminal
13394 frames. */
13395 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13396 && !EQ (FRAME_TTY (f)->top_frame, frame))
13397 continue;
13398
13399 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13400 {
13401 /* Mark all the scroll bars to be removed; we'll redeem
13402 the ones we want when we redisplay their windows. */
13403 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13404 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13405
13406 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13407 redisplay_windows (FRAME_ROOT_WINDOW (f));
13408
13409 /* The X error handler may have deleted that frame. */
13410 if (!FRAME_LIVE_P (f))
13411 continue;
13412
13413 /* Any scroll bars which redisplay_windows should have
13414 nuked should now go away. */
13415 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13416 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13417
13418 /* If fonts changed, display again. */
13419 /* ??? rms: I suspect it is a mistake to jump all the way
13420 back to retry here. It should just retry this frame. */
13421 if (fonts_changed_p)
13422 goto retry;
13423
13424 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13425 {
13426 /* See if we have to hscroll. */
13427 if (!f->already_hscrolled_p)
13428 {
13429 f->already_hscrolled_p = 1;
13430 if (hscroll_windows (f->root_window))
13431 goto retry;
13432 }
13433
13434 /* Prevent various kinds of signals during display
13435 update. stdio is not robust about handling
13436 signals, which can cause an apparent I/O
13437 error. */
13438 if (interrupt_input)
13439 unrequest_sigio ();
13440 STOP_POLLING;
13441
13442 /* Update the display. */
13443 set_window_update_flags (XWINDOW (f->root_window), 1);
13444 pending |= update_frame (f, 0, 0);
13445 f->updated_p = 1;
13446 }
13447 }
13448 }
13449
13450 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13451
13452 if (!pending)
13453 {
13454 /* Do the mark_window_display_accurate after all windows have
13455 been redisplayed because this call resets flags in buffers
13456 which are needed for proper redisplay. */
13457 FOR_EACH_FRAME (tail, frame)
13458 {
13459 struct frame *f = XFRAME (frame);
13460 if (f->updated_p)
13461 {
13462 mark_window_display_accurate (f->root_window, 1);
13463 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13464 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13465 }
13466 }
13467 }
13468 }
13469 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13470 {
13471 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13472 struct frame *mini_frame;
13473
13474 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13475 /* Use list_of_error, not Qerror, so that
13476 we catch only errors and don't run the debugger. */
13477 internal_condition_case_1 (redisplay_window_1, selected_window,
13478 list_of_error,
13479 redisplay_window_error);
13480 if (update_miniwindow_p)
13481 internal_condition_case_1 (redisplay_window_1, mini_window,
13482 list_of_error,
13483 redisplay_window_error);
13484
13485 /* Compare desired and current matrices, perform output. */
13486
13487 update:
13488 /* If fonts changed, display again. */
13489 if (fonts_changed_p)
13490 goto retry;
13491
13492 /* Prevent various kinds of signals during display update.
13493 stdio is not robust about handling signals,
13494 which can cause an apparent I/O error. */
13495 if (interrupt_input)
13496 unrequest_sigio ();
13497 STOP_POLLING;
13498
13499 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13500 {
13501 if (hscroll_windows (selected_window))
13502 goto retry;
13503
13504 XWINDOW (selected_window)->must_be_updated_p = 1;
13505 pending = update_frame (sf, 0, 0);
13506 }
13507
13508 /* We may have called echo_area_display at the top of this
13509 function. If the echo area is on another frame, that may
13510 have put text on a frame other than the selected one, so the
13511 above call to update_frame would not have caught it. Catch
13512 it here. */
13513 mini_window = FRAME_MINIBUF_WINDOW (sf);
13514 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13515
13516 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13517 {
13518 XWINDOW (mini_window)->must_be_updated_p = 1;
13519 pending |= update_frame (mini_frame, 0, 0);
13520 if (!pending && hscroll_windows (mini_window))
13521 goto retry;
13522 }
13523 }
13524
13525 /* If display was paused because of pending input, make sure we do a
13526 thorough update the next time. */
13527 if (pending)
13528 {
13529 /* Prevent the optimization at the beginning of
13530 redisplay_internal that tries a single-line update of the
13531 line containing the cursor in the selected window. */
13532 CHARPOS (this_line_start_pos) = 0;
13533
13534 /* Let the overlay arrow be updated the next time. */
13535 update_overlay_arrows (0);
13536
13537 /* If we pause after scrolling, some rows in the current
13538 matrices of some windows are not valid. */
13539 if (!WINDOW_FULL_WIDTH_P (w)
13540 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13541 update_mode_lines = 1;
13542 }
13543 else
13544 {
13545 if (!consider_all_windows_p)
13546 {
13547 /* This has already been done above if
13548 consider_all_windows_p is set. */
13549 mark_window_display_accurate_1 (w, 1);
13550
13551 /* Say overlay arrows are up to date. */
13552 update_overlay_arrows (1);
13553
13554 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13555 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13556 }
13557
13558 update_mode_lines = 0;
13559 windows_or_buffers_changed = 0;
13560 cursor_type_changed = 0;
13561 }
13562
13563 /* Start SIGIO interrupts coming again. Having them off during the
13564 code above makes it less likely one will discard output, but not
13565 impossible, since there might be stuff in the system buffer here.
13566 But it is much hairier to try to do anything about that. */
13567 if (interrupt_input)
13568 request_sigio ();
13569 RESUME_POLLING;
13570
13571 /* If a frame has become visible which was not before, redisplay
13572 again, so that we display it. Expose events for such a frame
13573 (which it gets when becoming visible) don't call the parts of
13574 redisplay constructing glyphs, so simply exposing a frame won't
13575 display anything in this case. So, we have to display these
13576 frames here explicitly. */
13577 if (!pending)
13578 {
13579 int new_count = 0;
13580
13581 FOR_EACH_FRAME (tail, frame)
13582 {
13583 int this_is_visible = 0;
13584
13585 if (XFRAME (frame)->visible)
13586 this_is_visible = 1;
13587
13588 if (this_is_visible)
13589 new_count++;
13590 }
13591
13592 if (new_count != number_of_visible_frames)
13593 windows_or_buffers_changed++;
13594 }
13595
13596 /* Change frame size now if a change is pending. */
13597 do_pending_window_change (1);
13598
13599 /* If we just did a pending size change, or have additional
13600 visible frames, or selected_window changed, redisplay again. */
13601 if ((windows_or_buffers_changed && !pending)
13602 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13603 goto retry;
13604
13605 /* Clear the face and image caches.
13606
13607 We used to do this only if consider_all_windows_p. But the cache
13608 needs to be cleared if a timer creates images in the current
13609 buffer (e.g. the test case in Bug#6230). */
13610
13611 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13612 {
13613 clear_face_cache (0);
13614 clear_face_cache_count = 0;
13615 }
13616
13617 #ifdef HAVE_WINDOW_SYSTEM
13618 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13619 {
13620 clear_image_caches (Qnil);
13621 clear_image_cache_count = 0;
13622 }
13623 #endif /* HAVE_WINDOW_SYSTEM */
13624
13625 end_of_redisplay:
13626 unbind_to (count, Qnil);
13627 RESUME_POLLING;
13628 }
13629
13630
13631 /* Redisplay, but leave alone any recent echo area message unless
13632 another message has been requested in its place.
13633
13634 This is useful in situations where you need to redisplay but no
13635 user action has occurred, making it inappropriate for the message
13636 area to be cleared. See tracking_off and
13637 wait_reading_process_output for examples of these situations.
13638
13639 FROM_WHERE is an integer saying from where this function was
13640 called. This is useful for debugging. */
13641
13642 void
13643 redisplay_preserve_echo_area (int from_where)
13644 {
13645 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13646
13647 if (!NILP (echo_area_buffer[1]))
13648 {
13649 /* We have a previously displayed message, but no current
13650 message. Redisplay the previous message. */
13651 display_last_displayed_message_p = 1;
13652 redisplay_internal ();
13653 display_last_displayed_message_p = 0;
13654 }
13655 else
13656 redisplay_internal ();
13657
13658 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13659 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13660 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13661 }
13662
13663
13664 /* Function registered with record_unwind_protect in redisplay_internal.
13665 Clear redisplaying_p. Also select the previously selected frame. */
13666
13667 static Lisp_Object
13668 unwind_redisplay (Lisp_Object old_frame)
13669 {
13670 redisplaying_p = 0;
13671 return Qnil;
13672 }
13673
13674
13675 /* Mark the display of leaf window W as accurate or inaccurate.
13676 If ACCURATE_P is non-zero mark display of W as accurate. If
13677 ACCURATE_P is zero, arrange for W to be redisplayed the next
13678 time redisplay_internal is called. */
13679
13680 static void
13681 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13682 {
13683 struct buffer *b = XBUFFER (w->contents);
13684
13685 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13686 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13687 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13688
13689 if (accurate_p)
13690 {
13691 b->clip_changed = 0;
13692 b->prevent_redisplay_optimizations_p = 0;
13693
13694 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13695 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13696 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13697 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13698
13699 w->current_matrix->buffer = b;
13700 w->current_matrix->begv = BUF_BEGV (b);
13701 w->current_matrix->zv = BUF_ZV (b);
13702
13703 w->last_cursor = w->cursor;
13704 w->last_cursor_off_p = w->cursor_off_p;
13705
13706 if (w == XWINDOW (selected_window))
13707 w->last_point = BUF_PT (b);
13708 else
13709 w->last_point = marker_position (w->pointm);
13710
13711 w->window_end_valid = 1;
13712 w->update_mode_line = 0;
13713 }
13714 }
13715
13716
13717 /* Mark the display of windows in the window tree rooted at WINDOW as
13718 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13719 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13720 be redisplayed the next time redisplay_internal is called. */
13721
13722 void
13723 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13724 {
13725 struct window *w;
13726
13727 for (; !NILP (window); window = w->next)
13728 {
13729 w = XWINDOW (window);
13730 if (WINDOWP (w->contents))
13731 mark_window_display_accurate (w->contents, accurate_p);
13732 else
13733 mark_window_display_accurate_1 (w, accurate_p);
13734 }
13735
13736 if (accurate_p)
13737 update_overlay_arrows (1);
13738 else
13739 /* Force a thorough redisplay the next time by setting
13740 last_arrow_position and last_arrow_string to t, which is
13741 unequal to any useful value of Voverlay_arrow_... */
13742 update_overlay_arrows (-1);
13743 }
13744
13745
13746 /* Return value in display table DP (Lisp_Char_Table *) for character
13747 C. Since a display table doesn't have any parent, we don't have to
13748 follow parent. Do not call this function directly but use the
13749 macro DISP_CHAR_VECTOR. */
13750
13751 Lisp_Object
13752 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13753 {
13754 Lisp_Object val;
13755
13756 if (ASCII_CHAR_P (c))
13757 {
13758 val = dp->ascii;
13759 if (SUB_CHAR_TABLE_P (val))
13760 val = XSUB_CHAR_TABLE (val)->contents[c];
13761 }
13762 else
13763 {
13764 Lisp_Object table;
13765
13766 XSETCHAR_TABLE (table, dp);
13767 val = char_table_ref (table, c);
13768 }
13769 if (NILP (val))
13770 val = dp->defalt;
13771 return val;
13772 }
13773
13774
13775 \f
13776 /***********************************************************************
13777 Window Redisplay
13778 ***********************************************************************/
13779
13780 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13781
13782 static void
13783 redisplay_windows (Lisp_Object window)
13784 {
13785 while (!NILP (window))
13786 {
13787 struct window *w = XWINDOW (window);
13788
13789 if (WINDOWP (w->contents))
13790 redisplay_windows (w->contents);
13791 else if (BUFFERP (w->contents))
13792 {
13793 displayed_buffer = XBUFFER (w->contents);
13794 /* Use list_of_error, not Qerror, so that
13795 we catch only errors and don't run the debugger. */
13796 internal_condition_case_1 (redisplay_window_0, window,
13797 list_of_error,
13798 redisplay_window_error);
13799 }
13800
13801 window = w->next;
13802 }
13803 }
13804
13805 static Lisp_Object
13806 redisplay_window_error (Lisp_Object ignore)
13807 {
13808 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13809 return Qnil;
13810 }
13811
13812 static Lisp_Object
13813 redisplay_window_0 (Lisp_Object window)
13814 {
13815 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13816 redisplay_window (window, 0);
13817 return Qnil;
13818 }
13819
13820 static Lisp_Object
13821 redisplay_window_1 (Lisp_Object window)
13822 {
13823 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13824 redisplay_window (window, 1);
13825 return Qnil;
13826 }
13827 \f
13828
13829 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13830 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13831 which positions recorded in ROW differ from current buffer
13832 positions.
13833
13834 Return 0 if cursor is not on this row, 1 otherwise. */
13835
13836 static int
13837 set_cursor_from_row (struct window *w, struct glyph_row *row,
13838 struct glyph_matrix *matrix,
13839 ptrdiff_t delta, ptrdiff_t delta_bytes,
13840 int dy, int dvpos)
13841 {
13842 struct glyph *glyph = row->glyphs[TEXT_AREA];
13843 struct glyph *end = glyph + row->used[TEXT_AREA];
13844 struct glyph *cursor = NULL;
13845 /* The last known character position in row. */
13846 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13847 int x = row->x;
13848 ptrdiff_t pt_old = PT - delta;
13849 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13850 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13851 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13852 /* A glyph beyond the edge of TEXT_AREA which we should never
13853 touch. */
13854 struct glyph *glyphs_end = end;
13855 /* Non-zero means we've found a match for cursor position, but that
13856 glyph has the avoid_cursor_p flag set. */
13857 int match_with_avoid_cursor = 0;
13858 /* Non-zero means we've seen at least one glyph that came from a
13859 display string. */
13860 int string_seen = 0;
13861 /* Largest and smallest buffer positions seen so far during scan of
13862 glyph row. */
13863 ptrdiff_t bpos_max = pos_before;
13864 ptrdiff_t bpos_min = pos_after;
13865 /* Last buffer position covered by an overlay string with an integer
13866 `cursor' property. */
13867 ptrdiff_t bpos_covered = 0;
13868 /* Non-zero means the display string on which to display the cursor
13869 comes from a text property, not from an overlay. */
13870 int string_from_text_prop = 0;
13871
13872 /* Don't even try doing anything if called for a mode-line or
13873 header-line row, since the rest of the code isn't prepared to
13874 deal with such calamities. */
13875 eassert (!row->mode_line_p);
13876 if (row->mode_line_p)
13877 return 0;
13878
13879 /* Skip over glyphs not having an object at the start and the end of
13880 the row. These are special glyphs like truncation marks on
13881 terminal frames. */
13882 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13883 {
13884 if (!row->reversed_p)
13885 {
13886 while (glyph < end
13887 && INTEGERP (glyph->object)
13888 && glyph->charpos < 0)
13889 {
13890 x += glyph->pixel_width;
13891 ++glyph;
13892 }
13893 while (end > glyph
13894 && INTEGERP ((end - 1)->object)
13895 /* CHARPOS is zero for blanks and stretch glyphs
13896 inserted by extend_face_to_end_of_line. */
13897 && (end - 1)->charpos <= 0)
13898 --end;
13899 glyph_before = glyph - 1;
13900 glyph_after = end;
13901 }
13902 else
13903 {
13904 struct glyph *g;
13905
13906 /* If the glyph row is reversed, we need to process it from back
13907 to front, so swap the edge pointers. */
13908 glyphs_end = end = glyph - 1;
13909 glyph += row->used[TEXT_AREA] - 1;
13910
13911 while (glyph > end + 1
13912 && INTEGERP (glyph->object)
13913 && glyph->charpos < 0)
13914 {
13915 --glyph;
13916 x -= glyph->pixel_width;
13917 }
13918 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13919 --glyph;
13920 /* By default, in reversed rows we put the cursor on the
13921 rightmost (first in the reading order) glyph. */
13922 for (g = end + 1; g < glyph; g++)
13923 x += g->pixel_width;
13924 while (end < glyph
13925 && INTEGERP ((end + 1)->object)
13926 && (end + 1)->charpos <= 0)
13927 ++end;
13928 glyph_before = glyph + 1;
13929 glyph_after = end;
13930 }
13931 }
13932 else if (row->reversed_p)
13933 {
13934 /* In R2L rows that don't display text, put the cursor on the
13935 rightmost glyph. Case in point: an empty last line that is
13936 part of an R2L paragraph. */
13937 cursor = end - 1;
13938 /* Avoid placing the cursor on the last glyph of the row, where
13939 on terminal frames we hold the vertical border between
13940 adjacent windows. */
13941 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13942 && !WINDOW_RIGHTMOST_P (w)
13943 && cursor == row->glyphs[LAST_AREA] - 1)
13944 cursor--;
13945 x = -1; /* will be computed below, at label compute_x */
13946 }
13947
13948 /* Step 1: Try to find the glyph whose character position
13949 corresponds to point. If that's not possible, find 2 glyphs
13950 whose character positions are the closest to point, one before
13951 point, the other after it. */
13952 if (!row->reversed_p)
13953 while (/* not marched to end of glyph row */
13954 glyph < end
13955 /* glyph was not inserted by redisplay for internal purposes */
13956 && !INTEGERP (glyph->object))
13957 {
13958 if (BUFFERP (glyph->object))
13959 {
13960 ptrdiff_t dpos = glyph->charpos - pt_old;
13961
13962 if (glyph->charpos > bpos_max)
13963 bpos_max = glyph->charpos;
13964 if (glyph->charpos < bpos_min)
13965 bpos_min = glyph->charpos;
13966 if (!glyph->avoid_cursor_p)
13967 {
13968 /* If we hit point, we've found the glyph on which to
13969 display the cursor. */
13970 if (dpos == 0)
13971 {
13972 match_with_avoid_cursor = 0;
13973 break;
13974 }
13975 /* See if we've found a better approximation to
13976 POS_BEFORE or to POS_AFTER. */
13977 if (0 > dpos && dpos > pos_before - pt_old)
13978 {
13979 pos_before = glyph->charpos;
13980 glyph_before = glyph;
13981 }
13982 else if (0 < dpos && dpos < pos_after - pt_old)
13983 {
13984 pos_after = glyph->charpos;
13985 glyph_after = glyph;
13986 }
13987 }
13988 else if (dpos == 0)
13989 match_with_avoid_cursor = 1;
13990 }
13991 else if (STRINGP (glyph->object))
13992 {
13993 Lisp_Object chprop;
13994 ptrdiff_t glyph_pos = glyph->charpos;
13995
13996 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13997 glyph->object);
13998 if (!NILP (chprop))
13999 {
14000 /* If the string came from a `display' text property,
14001 look up the buffer position of that property and
14002 use that position to update bpos_max, as if we
14003 actually saw such a position in one of the row's
14004 glyphs. This helps with supporting integer values
14005 of `cursor' property on the display string in
14006 situations where most or all of the row's buffer
14007 text is completely covered by display properties,
14008 so that no glyph with valid buffer positions is
14009 ever seen in the row. */
14010 ptrdiff_t prop_pos =
14011 string_buffer_position_lim (glyph->object, pos_before,
14012 pos_after, 0);
14013
14014 if (prop_pos >= pos_before)
14015 bpos_max = prop_pos - 1;
14016 }
14017 if (INTEGERP (chprop))
14018 {
14019 bpos_covered = bpos_max + XINT (chprop);
14020 /* If the `cursor' property covers buffer positions up
14021 to and including point, we should display cursor on
14022 this glyph. Note that, if a `cursor' property on one
14023 of the string's characters has an integer value, we
14024 will break out of the loop below _before_ we get to
14025 the position match above. IOW, integer values of
14026 the `cursor' property override the "exact match for
14027 point" strategy of positioning the cursor. */
14028 /* Implementation note: bpos_max == pt_old when, e.g.,
14029 we are in an empty line, where bpos_max is set to
14030 MATRIX_ROW_START_CHARPOS, see above. */
14031 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14032 {
14033 cursor = glyph;
14034 break;
14035 }
14036 }
14037
14038 string_seen = 1;
14039 }
14040 x += glyph->pixel_width;
14041 ++glyph;
14042 }
14043 else if (glyph > end) /* row is reversed */
14044 while (!INTEGERP (glyph->object))
14045 {
14046 if (BUFFERP (glyph->object))
14047 {
14048 ptrdiff_t dpos = glyph->charpos - pt_old;
14049
14050 if (glyph->charpos > bpos_max)
14051 bpos_max = glyph->charpos;
14052 if (glyph->charpos < bpos_min)
14053 bpos_min = glyph->charpos;
14054 if (!glyph->avoid_cursor_p)
14055 {
14056 if (dpos == 0)
14057 {
14058 match_with_avoid_cursor = 0;
14059 break;
14060 }
14061 if (0 > dpos && dpos > pos_before - pt_old)
14062 {
14063 pos_before = glyph->charpos;
14064 glyph_before = glyph;
14065 }
14066 else if (0 < dpos && dpos < pos_after - pt_old)
14067 {
14068 pos_after = glyph->charpos;
14069 glyph_after = glyph;
14070 }
14071 }
14072 else if (dpos == 0)
14073 match_with_avoid_cursor = 1;
14074 }
14075 else if (STRINGP (glyph->object))
14076 {
14077 Lisp_Object chprop;
14078 ptrdiff_t glyph_pos = glyph->charpos;
14079
14080 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14081 glyph->object);
14082 if (!NILP (chprop))
14083 {
14084 ptrdiff_t prop_pos =
14085 string_buffer_position_lim (glyph->object, pos_before,
14086 pos_after, 0);
14087
14088 if (prop_pos >= pos_before)
14089 bpos_max = prop_pos - 1;
14090 }
14091 if (INTEGERP (chprop))
14092 {
14093 bpos_covered = bpos_max + XINT (chprop);
14094 /* If the `cursor' property covers buffer positions up
14095 to and including point, we should display cursor on
14096 this glyph. */
14097 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14098 {
14099 cursor = glyph;
14100 break;
14101 }
14102 }
14103 string_seen = 1;
14104 }
14105 --glyph;
14106 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14107 {
14108 x--; /* can't use any pixel_width */
14109 break;
14110 }
14111 x -= glyph->pixel_width;
14112 }
14113
14114 /* Step 2: If we didn't find an exact match for point, we need to
14115 look for a proper place to put the cursor among glyphs between
14116 GLYPH_BEFORE and GLYPH_AFTER. */
14117 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14118 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14119 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14120 {
14121 /* An empty line has a single glyph whose OBJECT is zero and
14122 whose CHARPOS is the position of a newline on that line.
14123 Note that on a TTY, there are more glyphs after that, which
14124 were produced by extend_face_to_end_of_line, but their
14125 CHARPOS is zero or negative. */
14126 int empty_line_p =
14127 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14128 && INTEGERP (glyph->object) && glyph->charpos > 0
14129 /* On a TTY, continued and truncated rows also have a glyph at
14130 their end whose OBJECT is zero and whose CHARPOS is
14131 positive (the continuation and truncation glyphs), but such
14132 rows are obviously not "empty". */
14133 && !(row->continued_p || row->truncated_on_right_p);
14134
14135 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14136 {
14137 ptrdiff_t ellipsis_pos;
14138
14139 /* Scan back over the ellipsis glyphs. */
14140 if (!row->reversed_p)
14141 {
14142 ellipsis_pos = (glyph - 1)->charpos;
14143 while (glyph > row->glyphs[TEXT_AREA]
14144 && (glyph - 1)->charpos == ellipsis_pos)
14145 glyph--, x -= glyph->pixel_width;
14146 /* That loop always goes one position too far, including
14147 the glyph before the ellipsis. So scan forward over
14148 that one. */
14149 x += glyph->pixel_width;
14150 glyph++;
14151 }
14152 else /* row is reversed */
14153 {
14154 ellipsis_pos = (glyph + 1)->charpos;
14155 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14156 && (glyph + 1)->charpos == ellipsis_pos)
14157 glyph++, x += glyph->pixel_width;
14158 x -= glyph->pixel_width;
14159 glyph--;
14160 }
14161 }
14162 else if (match_with_avoid_cursor)
14163 {
14164 cursor = glyph_after;
14165 x = -1;
14166 }
14167 else if (string_seen)
14168 {
14169 int incr = row->reversed_p ? -1 : +1;
14170
14171 /* Need to find the glyph that came out of a string which is
14172 present at point. That glyph is somewhere between
14173 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14174 positioned between POS_BEFORE and POS_AFTER in the
14175 buffer. */
14176 struct glyph *start, *stop;
14177 ptrdiff_t pos = pos_before;
14178
14179 x = -1;
14180
14181 /* If the row ends in a newline from a display string,
14182 reordering could have moved the glyphs belonging to the
14183 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14184 in this case we extend the search to the last glyph in
14185 the row that was not inserted by redisplay. */
14186 if (row->ends_in_newline_from_string_p)
14187 {
14188 glyph_after = end;
14189 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14190 }
14191
14192 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14193 correspond to POS_BEFORE and POS_AFTER, respectively. We
14194 need START and STOP in the order that corresponds to the
14195 row's direction as given by its reversed_p flag. If the
14196 directionality of characters between POS_BEFORE and
14197 POS_AFTER is the opposite of the row's base direction,
14198 these characters will have been reordered for display,
14199 and we need to reverse START and STOP. */
14200 if (!row->reversed_p)
14201 {
14202 start = min (glyph_before, glyph_after);
14203 stop = max (glyph_before, glyph_after);
14204 }
14205 else
14206 {
14207 start = max (glyph_before, glyph_after);
14208 stop = min (glyph_before, glyph_after);
14209 }
14210 for (glyph = start + incr;
14211 row->reversed_p ? glyph > stop : glyph < stop; )
14212 {
14213
14214 /* Any glyphs that come from the buffer are here because
14215 of bidi reordering. Skip them, and only pay
14216 attention to glyphs that came from some string. */
14217 if (STRINGP (glyph->object))
14218 {
14219 Lisp_Object str;
14220 ptrdiff_t tem;
14221 /* If the display property covers the newline, we
14222 need to search for it one position farther. */
14223 ptrdiff_t lim = pos_after
14224 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14225
14226 string_from_text_prop = 0;
14227 str = glyph->object;
14228 tem = string_buffer_position_lim (str, pos, lim, 0);
14229 if (tem == 0 /* from overlay */
14230 || pos <= tem)
14231 {
14232 /* If the string from which this glyph came is
14233 found in the buffer at point, or at position
14234 that is closer to point than pos_after, then
14235 we've found the glyph we've been looking for.
14236 If it comes from an overlay (tem == 0), and
14237 it has the `cursor' property on one of its
14238 glyphs, record that glyph as a candidate for
14239 displaying the cursor. (As in the
14240 unidirectional version, we will display the
14241 cursor on the last candidate we find.) */
14242 if (tem == 0
14243 || tem == pt_old
14244 || (tem - pt_old > 0 && tem < pos_after))
14245 {
14246 /* The glyphs from this string could have
14247 been reordered. Find the one with the
14248 smallest string position. Or there could
14249 be a character in the string with the
14250 `cursor' property, which means display
14251 cursor on that character's glyph. */
14252 ptrdiff_t strpos = glyph->charpos;
14253
14254 if (tem)
14255 {
14256 cursor = glyph;
14257 string_from_text_prop = 1;
14258 }
14259 for ( ;
14260 (row->reversed_p ? glyph > stop : glyph < stop)
14261 && EQ (glyph->object, str);
14262 glyph += incr)
14263 {
14264 Lisp_Object cprop;
14265 ptrdiff_t gpos = glyph->charpos;
14266
14267 cprop = Fget_char_property (make_number (gpos),
14268 Qcursor,
14269 glyph->object);
14270 if (!NILP (cprop))
14271 {
14272 cursor = glyph;
14273 break;
14274 }
14275 if (tem && glyph->charpos < strpos)
14276 {
14277 strpos = glyph->charpos;
14278 cursor = glyph;
14279 }
14280 }
14281
14282 if (tem == pt_old
14283 || (tem - pt_old > 0 && tem < pos_after))
14284 goto compute_x;
14285 }
14286 if (tem)
14287 pos = tem + 1; /* don't find previous instances */
14288 }
14289 /* This string is not what we want; skip all of the
14290 glyphs that came from it. */
14291 while ((row->reversed_p ? glyph > stop : glyph < stop)
14292 && EQ (glyph->object, str))
14293 glyph += incr;
14294 }
14295 else
14296 glyph += incr;
14297 }
14298
14299 /* If we reached the end of the line, and END was from a string,
14300 the cursor is not on this line. */
14301 if (cursor == NULL
14302 && (row->reversed_p ? glyph <= end : glyph >= end)
14303 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14304 && STRINGP (end->object)
14305 && row->continued_p)
14306 return 0;
14307 }
14308 /* A truncated row may not include PT among its character positions.
14309 Setting the cursor inside the scroll margin will trigger
14310 recalculation of hscroll in hscroll_window_tree. But if a
14311 display string covers point, defer to the string-handling
14312 code below to figure this out. */
14313 else if (row->truncated_on_left_p && pt_old < bpos_min)
14314 {
14315 cursor = glyph_before;
14316 x = -1;
14317 }
14318 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14319 /* Zero-width characters produce no glyphs. */
14320 || (!empty_line_p
14321 && (row->reversed_p
14322 ? glyph_after > glyphs_end
14323 : glyph_after < glyphs_end)))
14324 {
14325 cursor = glyph_after;
14326 x = -1;
14327 }
14328 }
14329
14330 compute_x:
14331 if (cursor != NULL)
14332 glyph = cursor;
14333 else if (glyph == glyphs_end
14334 && pos_before == pos_after
14335 && STRINGP ((row->reversed_p
14336 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14337 : row->glyphs[TEXT_AREA])->object))
14338 {
14339 /* If all the glyphs of this row came from strings, put the
14340 cursor on the first glyph of the row. This avoids having the
14341 cursor outside of the text area in this very rare and hard
14342 use case. */
14343 glyph =
14344 row->reversed_p
14345 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14346 : row->glyphs[TEXT_AREA];
14347 }
14348 if (x < 0)
14349 {
14350 struct glyph *g;
14351
14352 /* Need to compute x that corresponds to GLYPH. */
14353 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14354 {
14355 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14356 emacs_abort ();
14357 x += g->pixel_width;
14358 }
14359 }
14360
14361 /* ROW could be part of a continued line, which, under bidi
14362 reordering, might have other rows whose start and end charpos
14363 occlude point. Only set w->cursor if we found a better
14364 approximation to the cursor position than we have from previously
14365 examined candidate rows belonging to the same continued line. */
14366 if (/* we already have a candidate row */
14367 w->cursor.vpos >= 0
14368 /* that candidate is not the row we are processing */
14369 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14370 /* Make sure cursor.vpos specifies a row whose start and end
14371 charpos occlude point, and it is valid candidate for being a
14372 cursor-row. This is because some callers of this function
14373 leave cursor.vpos at the row where the cursor was displayed
14374 during the last redisplay cycle. */
14375 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14376 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14377 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14378 {
14379 struct glyph *g1 =
14380 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14381
14382 /* Don't consider glyphs that are outside TEXT_AREA. */
14383 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14384 return 0;
14385 /* Keep the candidate whose buffer position is the closest to
14386 point or has the `cursor' property. */
14387 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14388 w->cursor.hpos >= 0
14389 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14390 && ((BUFFERP (g1->object)
14391 && (g1->charpos == pt_old /* an exact match always wins */
14392 || (BUFFERP (glyph->object)
14393 && eabs (g1->charpos - pt_old)
14394 < eabs (glyph->charpos - pt_old))))
14395 /* previous candidate is a glyph from a string that has
14396 a non-nil `cursor' property */
14397 || (STRINGP (g1->object)
14398 && (!NILP (Fget_char_property (make_number (g1->charpos),
14399 Qcursor, g1->object))
14400 /* previous candidate is from the same display
14401 string as this one, and the display string
14402 came from a text property */
14403 || (EQ (g1->object, glyph->object)
14404 && string_from_text_prop)
14405 /* this candidate is from newline and its
14406 position is not an exact match */
14407 || (INTEGERP (glyph->object)
14408 && glyph->charpos != pt_old)))))
14409 return 0;
14410 /* If this candidate gives an exact match, use that. */
14411 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14412 /* If this candidate is a glyph created for the
14413 terminating newline of a line, and point is on that
14414 newline, it wins because it's an exact match. */
14415 || (!row->continued_p
14416 && INTEGERP (glyph->object)
14417 && glyph->charpos == 0
14418 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14419 /* Otherwise, keep the candidate that comes from a row
14420 spanning less buffer positions. This may win when one or
14421 both candidate positions are on glyphs that came from
14422 display strings, for which we cannot compare buffer
14423 positions. */
14424 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14425 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14426 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14427 return 0;
14428 }
14429 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14430 w->cursor.x = x;
14431 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14432 w->cursor.y = row->y + dy;
14433
14434 if (w == XWINDOW (selected_window))
14435 {
14436 if (!row->continued_p
14437 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14438 && row->x == 0)
14439 {
14440 this_line_buffer = XBUFFER (w->contents);
14441
14442 CHARPOS (this_line_start_pos)
14443 = MATRIX_ROW_START_CHARPOS (row) + delta;
14444 BYTEPOS (this_line_start_pos)
14445 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14446
14447 CHARPOS (this_line_end_pos)
14448 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14449 BYTEPOS (this_line_end_pos)
14450 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14451
14452 this_line_y = w->cursor.y;
14453 this_line_pixel_height = row->height;
14454 this_line_vpos = w->cursor.vpos;
14455 this_line_start_x = row->x;
14456 }
14457 else
14458 CHARPOS (this_line_start_pos) = 0;
14459 }
14460
14461 return 1;
14462 }
14463
14464
14465 /* Run window scroll functions, if any, for WINDOW with new window
14466 start STARTP. Sets the window start of WINDOW to that position.
14467
14468 We assume that the window's buffer is really current. */
14469
14470 static struct text_pos
14471 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14472 {
14473 struct window *w = XWINDOW (window);
14474 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14475
14476 if (current_buffer != XBUFFER (w->contents))
14477 emacs_abort ();
14478
14479 if (!NILP (Vwindow_scroll_functions))
14480 {
14481 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14482 make_number (CHARPOS (startp)));
14483 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14484 /* In case the hook functions switch buffers. */
14485 set_buffer_internal (XBUFFER (w->contents));
14486 }
14487
14488 return startp;
14489 }
14490
14491
14492 /* Make sure the line containing the cursor is fully visible.
14493 A value of 1 means there is nothing to be done.
14494 (Either the line is fully visible, or it cannot be made so,
14495 or we cannot tell.)
14496
14497 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14498 is higher than window.
14499
14500 A value of 0 means the caller should do scrolling
14501 as if point had gone off the screen. */
14502
14503 static int
14504 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14505 {
14506 struct glyph_matrix *matrix;
14507 struct glyph_row *row;
14508 int window_height;
14509
14510 if (!make_cursor_line_fully_visible_p)
14511 return 1;
14512
14513 /* It's not always possible to find the cursor, e.g, when a window
14514 is full of overlay strings. Don't do anything in that case. */
14515 if (w->cursor.vpos < 0)
14516 return 1;
14517
14518 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14519 row = MATRIX_ROW (matrix, w->cursor.vpos);
14520
14521 /* If the cursor row is not partially visible, there's nothing to do. */
14522 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14523 return 1;
14524
14525 /* If the row the cursor is in is taller than the window's height,
14526 it's not clear what to do, so do nothing. */
14527 window_height = window_box_height (w);
14528 if (row->height >= window_height)
14529 {
14530 if (!force_p || MINI_WINDOW_P (w)
14531 || w->vscroll || w->cursor.vpos == 0)
14532 return 1;
14533 }
14534 return 0;
14535 }
14536
14537
14538 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14539 non-zero means only WINDOW is redisplayed in redisplay_internal.
14540 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14541 in redisplay_window to bring a partially visible line into view in
14542 the case that only the cursor has moved.
14543
14544 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14545 last screen line's vertical height extends past the end of the screen.
14546
14547 Value is
14548
14549 1 if scrolling succeeded
14550
14551 0 if scrolling didn't find point.
14552
14553 -1 if new fonts have been loaded so that we must interrupt
14554 redisplay, adjust glyph matrices, and try again. */
14555
14556 enum
14557 {
14558 SCROLLING_SUCCESS,
14559 SCROLLING_FAILED,
14560 SCROLLING_NEED_LARGER_MATRICES
14561 };
14562
14563 /* If scroll-conservatively is more than this, never recenter.
14564
14565 If you change this, don't forget to update the doc string of
14566 `scroll-conservatively' and the Emacs manual. */
14567 #define SCROLL_LIMIT 100
14568
14569 static int
14570 try_scrolling (Lisp_Object window, int just_this_one_p,
14571 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14572 int temp_scroll_step, int last_line_misfit)
14573 {
14574 struct window *w = XWINDOW (window);
14575 struct frame *f = XFRAME (w->frame);
14576 struct text_pos pos, startp;
14577 struct it it;
14578 int this_scroll_margin, scroll_max, rc, height;
14579 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14580 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14581 Lisp_Object aggressive;
14582 /* We will never try scrolling more than this number of lines. */
14583 int scroll_limit = SCROLL_LIMIT;
14584 int frame_line_height = default_line_pixel_height (w);
14585 int window_total_lines
14586 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14587
14588 #ifdef GLYPH_DEBUG
14589 debug_method_add (w, "try_scrolling");
14590 #endif
14591
14592 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14593
14594 /* Compute scroll margin height in pixels. We scroll when point is
14595 within this distance from the top or bottom of the window. */
14596 if (scroll_margin > 0)
14597 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14598 * frame_line_height;
14599 else
14600 this_scroll_margin = 0;
14601
14602 /* Force arg_scroll_conservatively to have a reasonable value, to
14603 avoid scrolling too far away with slow move_it_* functions. Note
14604 that the user can supply scroll-conservatively equal to
14605 `most-positive-fixnum', which can be larger than INT_MAX. */
14606 if (arg_scroll_conservatively > scroll_limit)
14607 {
14608 arg_scroll_conservatively = scroll_limit + 1;
14609 scroll_max = scroll_limit * frame_line_height;
14610 }
14611 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14612 /* Compute how much we should try to scroll maximally to bring
14613 point into view. */
14614 scroll_max = (max (scroll_step,
14615 max (arg_scroll_conservatively, temp_scroll_step))
14616 * frame_line_height);
14617 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14618 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14619 /* We're trying to scroll because of aggressive scrolling but no
14620 scroll_step is set. Choose an arbitrary one. */
14621 scroll_max = 10 * frame_line_height;
14622 else
14623 scroll_max = 0;
14624
14625 too_near_end:
14626
14627 /* Decide whether to scroll down. */
14628 if (PT > CHARPOS (startp))
14629 {
14630 int scroll_margin_y;
14631
14632 /* Compute the pixel ypos of the scroll margin, then move IT to
14633 either that ypos or PT, whichever comes first. */
14634 start_display (&it, w, startp);
14635 scroll_margin_y = it.last_visible_y - this_scroll_margin
14636 - frame_line_height * extra_scroll_margin_lines;
14637 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14638 (MOVE_TO_POS | MOVE_TO_Y));
14639
14640 if (PT > CHARPOS (it.current.pos))
14641 {
14642 int y0 = line_bottom_y (&it);
14643 /* Compute how many pixels below window bottom to stop searching
14644 for PT. This avoids costly search for PT that is far away if
14645 the user limited scrolling by a small number of lines, but
14646 always finds PT if scroll_conservatively is set to a large
14647 number, such as most-positive-fixnum. */
14648 int slack = max (scroll_max, 10 * frame_line_height);
14649 int y_to_move = it.last_visible_y + slack;
14650
14651 /* Compute the distance from the scroll margin to PT or to
14652 the scroll limit, whichever comes first. This should
14653 include the height of the cursor line, to make that line
14654 fully visible. */
14655 move_it_to (&it, PT, -1, y_to_move,
14656 -1, MOVE_TO_POS | MOVE_TO_Y);
14657 dy = line_bottom_y (&it) - y0;
14658
14659 if (dy > scroll_max)
14660 return SCROLLING_FAILED;
14661
14662 if (dy > 0)
14663 scroll_down_p = 1;
14664 }
14665 }
14666
14667 if (scroll_down_p)
14668 {
14669 /* Point is in or below the bottom scroll margin, so move the
14670 window start down. If scrolling conservatively, move it just
14671 enough down to make point visible. If scroll_step is set,
14672 move it down by scroll_step. */
14673 if (arg_scroll_conservatively)
14674 amount_to_scroll
14675 = min (max (dy, frame_line_height),
14676 frame_line_height * arg_scroll_conservatively);
14677 else if (scroll_step || temp_scroll_step)
14678 amount_to_scroll = scroll_max;
14679 else
14680 {
14681 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14682 height = WINDOW_BOX_TEXT_HEIGHT (w);
14683 if (NUMBERP (aggressive))
14684 {
14685 double float_amount = XFLOATINT (aggressive) * height;
14686 int aggressive_scroll = float_amount;
14687 if (aggressive_scroll == 0 && float_amount > 0)
14688 aggressive_scroll = 1;
14689 /* Don't let point enter the scroll margin near top of
14690 the window. This could happen if the value of
14691 scroll_up_aggressively is too large and there are
14692 non-zero margins, because scroll_up_aggressively
14693 means put point that fraction of window height
14694 _from_the_bottom_margin_. */
14695 if (aggressive_scroll + 2*this_scroll_margin > height)
14696 aggressive_scroll = height - 2*this_scroll_margin;
14697 amount_to_scroll = dy + aggressive_scroll;
14698 }
14699 }
14700
14701 if (amount_to_scroll <= 0)
14702 return SCROLLING_FAILED;
14703
14704 start_display (&it, w, startp);
14705 if (arg_scroll_conservatively <= scroll_limit)
14706 move_it_vertically (&it, amount_to_scroll);
14707 else
14708 {
14709 /* Extra precision for users who set scroll-conservatively
14710 to a large number: make sure the amount we scroll
14711 the window start is never less than amount_to_scroll,
14712 which was computed as distance from window bottom to
14713 point. This matters when lines at window top and lines
14714 below window bottom have different height. */
14715 struct it it1;
14716 void *it1data = NULL;
14717 /* We use a temporary it1 because line_bottom_y can modify
14718 its argument, if it moves one line down; see there. */
14719 int start_y;
14720
14721 SAVE_IT (it1, it, it1data);
14722 start_y = line_bottom_y (&it1);
14723 do {
14724 RESTORE_IT (&it, &it, it1data);
14725 move_it_by_lines (&it, 1);
14726 SAVE_IT (it1, it, it1data);
14727 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14728 }
14729
14730 /* If STARTP is unchanged, move it down another screen line. */
14731 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14732 move_it_by_lines (&it, 1);
14733 startp = it.current.pos;
14734 }
14735 else
14736 {
14737 struct text_pos scroll_margin_pos = startp;
14738 int y_offset = 0;
14739
14740 /* See if point is inside the scroll margin at the top of the
14741 window. */
14742 if (this_scroll_margin)
14743 {
14744 int y_start;
14745
14746 start_display (&it, w, startp);
14747 y_start = it.current_y;
14748 move_it_vertically (&it, this_scroll_margin);
14749 scroll_margin_pos = it.current.pos;
14750 /* If we didn't move enough before hitting ZV, request
14751 additional amount of scroll, to move point out of the
14752 scroll margin. */
14753 if (IT_CHARPOS (it) == ZV
14754 && it.current_y - y_start < this_scroll_margin)
14755 y_offset = this_scroll_margin - (it.current_y - y_start);
14756 }
14757
14758 if (PT < CHARPOS (scroll_margin_pos))
14759 {
14760 /* Point is in the scroll margin at the top of the window or
14761 above what is displayed in the window. */
14762 int y0, y_to_move;
14763
14764 /* Compute the vertical distance from PT to the scroll
14765 margin position. Move as far as scroll_max allows, or
14766 one screenful, or 10 screen lines, whichever is largest.
14767 Give up if distance is greater than scroll_max or if we
14768 didn't reach the scroll margin position. */
14769 SET_TEXT_POS (pos, PT, PT_BYTE);
14770 start_display (&it, w, pos);
14771 y0 = it.current_y;
14772 y_to_move = max (it.last_visible_y,
14773 max (scroll_max, 10 * frame_line_height));
14774 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14775 y_to_move, -1,
14776 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14777 dy = it.current_y - y0;
14778 if (dy > scroll_max
14779 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14780 return SCROLLING_FAILED;
14781
14782 /* Additional scroll for when ZV was too close to point. */
14783 dy += y_offset;
14784
14785 /* Compute new window start. */
14786 start_display (&it, w, startp);
14787
14788 if (arg_scroll_conservatively)
14789 amount_to_scroll = max (dy, frame_line_height *
14790 max (scroll_step, temp_scroll_step));
14791 else if (scroll_step || temp_scroll_step)
14792 amount_to_scroll = scroll_max;
14793 else
14794 {
14795 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14796 height = WINDOW_BOX_TEXT_HEIGHT (w);
14797 if (NUMBERP (aggressive))
14798 {
14799 double float_amount = XFLOATINT (aggressive) * height;
14800 int aggressive_scroll = float_amount;
14801 if (aggressive_scroll == 0 && float_amount > 0)
14802 aggressive_scroll = 1;
14803 /* Don't let point enter the scroll margin near
14804 bottom of the window, if the value of
14805 scroll_down_aggressively happens to be too
14806 large. */
14807 if (aggressive_scroll + 2*this_scroll_margin > height)
14808 aggressive_scroll = height - 2*this_scroll_margin;
14809 amount_to_scroll = dy + aggressive_scroll;
14810 }
14811 }
14812
14813 if (amount_to_scroll <= 0)
14814 return SCROLLING_FAILED;
14815
14816 move_it_vertically_backward (&it, amount_to_scroll);
14817 startp = it.current.pos;
14818 }
14819 }
14820
14821 /* Run window scroll functions. */
14822 startp = run_window_scroll_functions (window, startp);
14823
14824 /* Display the window. Give up if new fonts are loaded, or if point
14825 doesn't appear. */
14826 if (!try_window (window, startp, 0))
14827 rc = SCROLLING_NEED_LARGER_MATRICES;
14828 else if (w->cursor.vpos < 0)
14829 {
14830 clear_glyph_matrix (w->desired_matrix);
14831 rc = SCROLLING_FAILED;
14832 }
14833 else
14834 {
14835 /* Maybe forget recorded base line for line number display. */
14836 if (!just_this_one_p
14837 || current_buffer->clip_changed
14838 || BEG_UNCHANGED < CHARPOS (startp))
14839 w->base_line_number = 0;
14840
14841 /* If cursor ends up on a partially visible line,
14842 treat that as being off the bottom of the screen. */
14843 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14844 /* It's possible that the cursor is on the first line of the
14845 buffer, which is partially obscured due to a vscroll
14846 (Bug#7537). In that case, avoid looping forever . */
14847 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14848 {
14849 clear_glyph_matrix (w->desired_matrix);
14850 ++extra_scroll_margin_lines;
14851 goto too_near_end;
14852 }
14853 rc = SCROLLING_SUCCESS;
14854 }
14855
14856 return rc;
14857 }
14858
14859
14860 /* Compute a suitable window start for window W if display of W starts
14861 on a continuation line. Value is non-zero if a new window start
14862 was computed.
14863
14864 The new window start will be computed, based on W's width, starting
14865 from the start of the continued line. It is the start of the
14866 screen line with the minimum distance from the old start W->start. */
14867
14868 static int
14869 compute_window_start_on_continuation_line (struct window *w)
14870 {
14871 struct text_pos pos, start_pos;
14872 int window_start_changed_p = 0;
14873
14874 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14875
14876 /* If window start is on a continuation line... Window start may be
14877 < BEGV in case there's invisible text at the start of the
14878 buffer (M-x rmail, for example). */
14879 if (CHARPOS (start_pos) > BEGV
14880 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14881 {
14882 struct it it;
14883 struct glyph_row *row;
14884
14885 /* Handle the case that the window start is out of range. */
14886 if (CHARPOS (start_pos) < BEGV)
14887 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14888 else if (CHARPOS (start_pos) > ZV)
14889 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14890
14891 /* Find the start of the continued line. This should be fast
14892 because find_newline is fast (newline cache). */
14893 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14894 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14895 row, DEFAULT_FACE_ID);
14896 reseat_at_previous_visible_line_start (&it);
14897
14898 /* If the line start is "too far" away from the window start,
14899 say it takes too much time to compute a new window start. */
14900 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14901 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14902 {
14903 int min_distance, distance;
14904
14905 /* Move forward by display lines to find the new window
14906 start. If window width was enlarged, the new start can
14907 be expected to be > the old start. If window width was
14908 decreased, the new window start will be < the old start.
14909 So, we're looking for the display line start with the
14910 minimum distance from the old window start. */
14911 pos = it.current.pos;
14912 min_distance = INFINITY;
14913 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14914 distance < min_distance)
14915 {
14916 min_distance = distance;
14917 pos = it.current.pos;
14918 move_it_by_lines (&it, 1);
14919 }
14920
14921 /* Set the window start there. */
14922 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14923 window_start_changed_p = 1;
14924 }
14925 }
14926
14927 return window_start_changed_p;
14928 }
14929
14930
14931 /* Try cursor movement in case text has not changed in window WINDOW,
14932 with window start STARTP. Value is
14933
14934 CURSOR_MOVEMENT_SUCCESS if successful
14935
14936 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14937
14938 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14939 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14940 we want to scroll as if scroll-step were set to 1. See the code.
14941
14942 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14943 which case we have to abort this redisplay, and adjust matrices
14944 first. */
14945
14946 enum
14947 {
14948 CURSOR_MOVEMENT_SUCCESS,
14949 CURSOR_MOVEMENT_CANNOT_BE_USED,
14950 CURSOR_MOVEMENT_MUST_SCROLL,
14951 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14952 };
14953
14954 static int
14955 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14956 {
14957 struct window *w = XWINDOW (window);
14958 struct frame *f = XFRAME (w->frame);
14959 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14960
14961 #ifdef GLYPH_DEBUG
14962 if (inhibit_try_cursor_movement)
14963 return rc;
14964 #endif
14965
14966 /* Previously, there was a check for Lisp integer in the
14967 if-statement below. Now, this field is converted to
14968 ptrdiff_t, thus zero means invalid position in a buffer. */
14969 eassert (w->last_point > 0);
14970
14971 /* Handle case where text has not changed, only point, and it has
14972 not moved off the frame. */
14973 if (/* Point may be in this window. */
14974 PT >= CHARPOS (startp)
14975 /* Selective display hasn't changed. */
14976 && !current_buffer->clip_changed
14977 /* Function force-mode-line-update is used to force a thorough
14978 redisplay. It sets either windows_or_buffers_changed or
14979 update_mode_lines. So don't take a shortcut here for these
14980 cases. */
14981 && !update_mode_lines
14982 && !windows_or_buffers_changed
14983 && !cursor_type_changed
14984 /* Can't use this case if highlighting a region. When a
14985 region exists, cursor movement has to do more than just
14986 set the cursor. */
14987 && markpos_of_region () < 0
14988 && !w->region_showing
14989 && NILP (Vshow_trailing_whitespace)
14990 /* This code is not used for mini-buffer for the sake of the case
14991 of redisplaying to replace an echo area message; since in
14992 that case the mini-buffer contents per se are usually
14993 unchanged. This code is of no real use in the mini-buffer
14994 since the handling of this_line_start_pos, etc., in redisplay
14995 handles the same cases. */
14996 && !EQ (window, minibuf_window)
14997 /* When splitting windows or for new windows, it happens that
14998 redisplay is called with a nil window_end_vpos or one being
14999 larger than the window. This should really be fixed in
15000 window.c. I don't have this on my list, now, so we do
15001 approximately the same as the old redisplay code. --gerd. */
15002 && INTEGERP (w->window_end_vpos)
15003 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15004 && (FRAME_WINDOW_P (f)
15005 || !overlay_arrow_in_current_buffer_p ()))
15006 {
15007 int this_scroll_margin, top_scroll_margin;
15008 struct glyph_row *row = NULL;
15009 int frame_line_height = default_line_pixel_height (w);
15010 int window_total_lines
15011 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15012
15013 #ifdef GLYPH_DEBUG
15014 debug_method_add (w, "cursor movement");
15015 #endif
15016
15017 /* Scroll if point within this distance from the top or bottom
15018 of the window. This is a pixel value. */
15019 if (scroll_margin > 0)
15020 {
15021 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15022 this_scroll_margin *= frame_line_height;
15023 }
15024 else
15025 this_scroll_margin = 0;
15026
15027 top_scroll_margin = this_scroll_margin;
15028 if (WINDOW_WANTS_HEADER_LINE_P (w))
15029 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15030
15031 /* Start with the row the cursor was displayed during the last
15032 not paused redisplay. Give up if that row is not valid. */
15033 if (w->last_cursor.vpos < 0
15034 || w->last_cursor.vpos >= w->current_matrix->nrows)
15035 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15036 else
15037 {
15038 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15039 if (row->mode_line_p)
15040 ++row;
15041 if (!row->enabled_p)
15042 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15043 }
15044
15045 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15046 {
15047 int scroll_p = 0, must_scroll = 0;
15048 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15049
15050 if (PT > w->last_point)
15051 {
15052 /* Point has moved forward. */
15053 while (MATRIX_ROW_END_CHARPOS (row) < PT
15054 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15055 {
15056 eassert (row->enabled_p);
15057 ++row;
15058 }
15059
15060 /* If the end position of a row equals the start
15061 position of the next row, and PT is at that position,
15062 we would rather display cursor in the next line. */
15063 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15064 && MATRIX_ROW_END_CHARPOS (row) == PT
15065 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15066 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15067 && !cursor_row_p (row))
15068 ++row;
15069
15070 /* If within the scroll margin, scroll. Note that
15071 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15072 the next line would be drawn, and that
15073 this_scroll_margin can be zero. */
15074 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15075 || PT > MATRIX_ROW_END_CHARPOS (row)
15076 /* Line is completely visible last line in window
15077 and PT is to be set in the next line. */
15078 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15079 && PT == MATRIX_ROW_END_CHARPOS (row)
15080 && !row->ends_at_zv_p
15081 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15082 scroll_p = 1;
15083 }
15084 else if (PT < w->last_point)
15085 {
15086 /* Cursor has to be moved backward. Note that PT >=
15087 CHARPOS (startp) because of the outer if-statement. */
15088 while (!row->mode_line_p
15089 && (MATRIX_ROW_START_CHARPOS (row) > PT
15090 || (MATRIX_ROW_START_CHARPOS (row) == PT
15091 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15092 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15093 row > w->current_matrix->rows
15094 && (row-1)->ends_in_newline_from_string_p))))
15095 && (row->y > top_scroll_margin
15096 || CHARPOS (startp) == BEGV))
15097 {
15098 eassert (row->enabled_p);
15099 --row;
15100 }
15101
15102 /* Consider the following case: Window starts at BEGV,
15103 there is invisible, intangible text at BEGV, so that
15104 display starts at some point START > BEGV. It can
15105 happen that we are called with PT somewhere between
15106 BEGV and START. Try to handle that case. */
15107 if (row < w->current_matrix->rows
15108 || row->mode_line_p)
15109 {
15110 row = w->current_matrix->rows;
15111 if (row->mode_line_p)
15112 ++row;
15113 }
15114
15115 /* Due to newlines in overlay strings, we may have to
15116 skip forward over overlay strings. */
15117 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15118 && MATRIX_ROW_END_CHARPOS (row) == PT
15119 && !cursor_row_p (row))
15120 ++row;
15121
15122 /* If within the scroll margin, scroll. */
15123 if (row->y < top_scroll_margin
15124 && CHARPOS (startp) != BEGV)
15125 scroll_p = 1;
15126 }
15127 else
15128 {
15129 /* Cursor did not move. So don't scroll even if cursor line
15130 is partially visible, as it was so before. */
15131 rc = CURSOR_MOVEMENT_SUCCESS;
15132 }
15133
15134 if (PT < MATRIX_ROW_START_CHARPOS (row)
15135 || PT > MATRIX_ROW_END_CHARPOS (row))
15136 {
15137 /* if PT is not in the glyph row, give up. */
15138 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15139 must_scroll = 1;
15140 }
15141 else if (rc != CURSOR_MOVEMENT_SUCCESS
15142 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15143 {
15144 struct glyph_row *row1;
15145
15146 /* If rows are bidi-reordered and point moved, back up
15147 until we find a row that does not belong to a
15148 continuation line. This is because we must consider
15149 all rows of a continued line as candidates for the
15150 new cursor positioning, since row start and end
15151 positions change non-linearly with vertical position
15152 in such rows. */
15153 /* FIXME: Revisit this when glyph ``spilling'' in
15154 continuation lines' rows is implemented for
15155 bidi-reordered rows. */
15156 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15157 MATRIX_ROW_CONTINUATION_LINE_P (row);
15158 --row)
15159 {
15160 /* If we hit the beginning of the displayed portion
15161 without finding the first row of a continued
15162 line, give up. */
15163 if (row <= row1)
15164 {
15165 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15166 break;
15167 }
15168 eassert (row->enabled_p);
15169 }
15170 }
15171 if (must_scroll)
15172 ;
15173 else if (rc != CURSOR_MOVEMENT_SUCCESS
15174 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15175 /* Make sure this isn't a header line by any chance, since
15176 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15177 && !row->mode_line_p
15178 && make_cursor_line_fully_visible_p)
15179 {
15180 if (PT == MATRIX_ROW_END_CHARPOS (row)
15181 && !row->ends_at_zv_p
15182 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15183 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15184 else if (row->height > window_box_height (w))
15185 {
15186 /* If we end up in a partially visible line, let's
15187 make it fully visible, except when it's taller
15188 than the window, in which case we can't do much
15189 about it. */
15190 *scroll_step = 1;
15191 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15192 }
15193 else
15194 {
15195 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15196 if (!cursor_row_fully_visible_p (w, 0, 1))
15197 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15198 else
15199 rc = CURSOR_MOVEMENT_SUCCESS;
15200 }
15201 }
15202 else if (scroll_p)
15203 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15204 else if (rc != CURSOR_MOVEMENT_SUCCESS
15205 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15206 {
15207 /* With bidi-reordered rows, there could be more than
15208 one candidate row whose start and end positions
15209 occlude point. We need to let set_cursor_from_row
15210 find the best candidate. */
15211 /* FIXME: Revisit this when glyph ``spilling'' in
15212 continuation lines' rows is implemented for
15213 bidi-reordered rows. */
15214 int rv = 0;
15215
15216 do
15217 {
15218 int at_zv_p = 0, exact_match_p = 0;
15219
15220 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15221 && PT <= MATRIX_ROW_END_CHARPOS (row)
15222 && cursor_row_p (row))
15223 rv |= set_cursor_from_row (w, row, w->current_matrix,
15224 0, 0, 0, 0);
15225 /* As soon as we've found the exact match for point,
15226 or the first suitable row whose ends_at_zv_p flag
15227 is set, we are done. */
15228 at_zv_p =
15229 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15230 if (rv && !at_zv_p
15231 && w->cursor.hpos >= 0
15232 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15233 w->cursor.vpos))
15234 {
15235 struct glyph_row *candidate =
15236 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15237 struct glyph *g =
15238 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15239 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15240
15241 exact_match_p =
15242 (BUFFERP (g->object) && g->charpos == PT)
15243 || (INTEGERP (g->object)
15244 && (g->charpos == PT
15245 || (g->charpos == 0 && endpos - 1 == PT)));
15246 }
15247 if (rv && (at_zv_p || exact_match_p))
15248 {
15249 rc = CURSOR_MOVEMENT_SUCCESS;
15250 break;
15251 }
15252 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15253 break;
15254 ++row;
15255 }
15256 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15257 || row->continued_p)
15258 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15259 || (MATRIX_ROW_START_CHARPOS (row) == PT
15260 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15261 /* If we didn't find any candidate rows, or exited the
15262 loop before all the candidates were examined, signal
15263 to the caller that this method failed. */
15264 if (rc != CURSOR_MOVEMENT_SUCCESS
15265 && !(rv
15266 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15267 && !row->continued_p))
15268 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15269 else if (rv)
15270 rc = CURSOR_MOVEMENT_SUCCESS;
15271 }
15272 else
15273 {
15274 do
15275 {
15276 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15277 {
15278 rc = CURSOR_MOVEMENT_SUCCESS;
15279 break;
15280 }
15281 ++row;
15282 }
15283 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15284 && MATRIX_ROW_START_CHARPOS (row) == PT
15285 && cursor_row_p (row));
15286 }
15287 }
15288 }
15289
15290 return rc;
15291 }
15292
15293 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15294 static
15295 #endif
15296 void
15297 set_vertical_scroll_bar (struct window *w)
15298 {
15299 ptrdiff_t start, end, whole;
15300
15301 /* Calculate the start and end positions for the current window.
15302 At some point, it would be nice to choose between scrollbars
15303 which reflect the whole buffer size, with special markers
15304 indicating narrowing, and scrollbars which reflect only the
15305 visible region.
15306
15307 Note that mini-buffers sometimes aren't displaying any text. */
15308 if (!MINI_WINDOW_P (w)
15309 || (w == XWINDOW (minibuf_window)
15310 && NILP (echo_area_buffer[0])))
15311 {
15312 struct buffer *buf = XBUFFER (w->contents);
15313 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15314 start = marker_position (w->start) - BUF_BEGV (buf);
15315 /* I don't think this is guaranteed to be right. For the
15316 moment, we'll pretend it is. */
15317 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15318
15319 if (end < start)
15320 end = start;
15321 if (whole < (end - start))
15322 whole = end - start;
15323 }
15324 else
15325 start = end = whole = 0;
15326
15327 /* Indicate what this scroll bar ought to be displaying now. */
15328 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15329 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15330 (w, end - start, whole, start);
15331 }
15332
15333
15334 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15335 selected_window is redisplayed.
15336
15337 We can return without actually redisplaying the window if
15338 fonts_changed_p. In that case, redisplay_internal will
15339 retry. */
15340
15341 static void
15342 redisplay_window (Lisp_Object window, int just_this_one_p)
15343 {
15344 struct window *w = XWINDOW (window);
15345 struct frame *f = XFRAME (w->frame);
15346 struct buffer *buffer = XBUFFER (w->contents);
15347 struct buffer *old = current_buffer;
15348 struct text_pos lpoint, opoint, startp;
15349 int update_mode_line;
15350 int tem;
15351 struct it it;
15352 /* Record it now because it's overwritten. */
15353 int current_matrix_up_to_date_p = 0;
15354 int used_current_matrix_p = 0;
15355 /* This is less strict than current_matrix_up_to_date_p.
15356 It indicates that the buffer contents and narrowing are unchanged. */
15357 int buffer_unchanged_p = 0;
15358 int temp_scroll_step = 0;
15359 ptrdiff_t count = SPECPDL_INDEX ();
15360 int rc;
15361 int centering_position = -1;
15362 int last_line_misfit = 0;
15363 ptrdiff_t beg_unchanged, end_unchanged;
15364 int frame_line_height;
15365
15366 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15367 opoint = lpoint;
15368
15369 #ifdef GLYPH_DEBUG
15370 *w->desired_matrix->method = 0;
15371 #endif
15372
15373 /* Make sure that both W's markers are valid. */
15374 eassert (XMARKER (w->start)->buffer == buffer);
15375 eassert (XMARKER (w->pointm)->buffer == buffer);
15376
15377 restart:
15378 reconsider_clip_changes (w, buffer);
15379 frame_line_height = default_line_pixel_height (w);
15380
15381 /* Has the mode line to be updated? */
15382 update_mode_line = (w->update_mode_line
15383 || update_mode_lines
15384 || buffer->clip_changed
15385 || buffer->prevent_redisplay_optimizations_p);
15386
15387 if (MINI_WINDOW_P (w))
15388 {
15389 if (w == XWINDOW (echo_area_window)
15390 && !NILP (echo_area_buffer[0]))
15391 {
15392 if (update_mode_line)
15393 /* We may have to update a tty frame's menu bar or a
15394 tool-bar. Example `M-x C-h C-h C-g'. */
15395 goto finish_menu_bars;
15396 else
15397 /* We've already displayed the echo area glyphs in this window. */
15398 goto finish_scroll_bars;
15399 }
15400 else if ((w != XWINDOW (minibuf_window)
15401 || minibuf_level == 0)
15402 /* When buffer is nonempty, redisplay window normally. */
15403 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15404 /* Quail displays non-mini buffers in minibuffer window.
15405 In that case, redisplay the window normally. */
15406 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15407 {
15408 /* W is a mini-buffer window, but it's not active, so clear
15409 it. */
15410 int yb = window_text_bottom_y (w);
15411 struct glyph_row *row;
15412 int y;
15413
15414 for (y = 0, row = w->desired_matrix->rows;
15415 y < yb;
15416 y += row->height, ++row)
15417 blank_row (w, row, y);
15418 goto finish_scroll_bars;
15419 }
15420
15421 clear_glyph_matrix (w->desired_matrix);
15422 }
15423
15424 /* Otherwise set up data on this window; select its buffer and point
15425 value. */
15426 /* Really select the buffer, for the sake of buffer-local
15427 variables. */
15428 set_buffer_internal_1 (XBUFFER (w->contents));
15429
15430 current_matrix_up_to_date_p
15431 = (w->window_end_valid
15432 && !current_buffer->clip_changed
15433 && !current_buffer->prevent_redisplay_optimizations_p
15434 && !window_outdated (w));
15435
15436 /* Run the window-bottom-change-functions
15437 if it is possible that the text on the screen has changed
15438 (either due to modification of the text, or any other reason). */
15439 if (!current_matrix_up_to_date_p
15440 && !NILP (Vwindow_text_change_functions))
15441 {
15442 safe_run_hooks (Qwindow_text_change_functions);
15443 goto restart;
15444 }
15445
15446 beg_unchanged = BEG_UNCHANGED;
15447 end_unchanged = END_UNCHANGED;
15448
15449 SET_TEXT_POS (opoint, PT, PT_BYTE);
15450
15451 specbind (Qinhibit_point_motion_hooks, Qt);
15452
15453 buffer_unchanged_p
15454 = (w->window_end_valid
15455 && !current_buffer->clip_changed
15456 && !window_outdated (w));
15457
15458 /* When windows_or_buffers_changed is non-zero, we can't rely on
15459 the window end being valid, so set it to nil there. */
15460 if (windows_or_buffers_changed)
15461 {
15462 /* If window starts on a continuation line, maybe adjust the
15463 window start in case the window's width changed. */
15464 if (XMARKER (w->start)->buffer == current_buffer)
15465 compute_window_start_on_continuation_line (w);
15466
15467 w->window_end_valid = 0;
15468 }
15469
15470 /* Some sanity checks. */
15471 CHECK_WINDOW_END (w);
15472 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15473 emacs_abort ();
15474 if (BYTEPOS (opoint) < CHARPOS (opoint))
15475 emacs_abort ();
15476
15477 if (mode_line_update_needed (w))
15478 update_mode_line = 1;
15479
15480 /* Point refers normally to the selected window. For any other
15481 window, set up appropriate value. */
15482 if (!EQ (window, selected_window))
15483 {
15484 ptrdiff_t new_pt = marker_position (w->pointm);
15485 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15486 if (new_pt < BEGV)
15487 {
15488 new_pt = BEGV;
15489 new_pt_byte = BEGV_BYTE;
15490 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15491 }
15492 else if (new_pt > (ZV - 1))
15493 {
15494 new_pt = ZV;
15495 new_pt_byte = ZV_BYTE;
15496 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15497 }
15498
15499 /* We don't use SET_PT so that the point-motion hooks don't run. */
15500 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15501 }
15502
15503 /* If any of the character widths specified in the display table
15504 have changed, invalidate the width run cache. It's true that
15505 this may be a bit late to catch such changes, but the rest of
15506 redisplay goes (non-fatally) haywire when the display table is
15507 changed, so why should we worry about doing any better? */
15508 if (current_buffer->width_run_cache)
15509 {
15510 struct Lisp_Char_Table *disptab = buffer_display_table ();
15511
15512 if (! disptab_matches_widthtab
15513 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15514 {
15515 invalidate_region_cache (current_buffer,
15516 current_buffer->width_run_cache,
15517 BEG, Z);
15518 recompute_width_table (current_buffer, disptab);
15519 }
15520 }
15521
15522 /* If window-start is screwed up, choose a new one. */
15523 if (XMARKER (w->start)->buffer != current_buffer)
15524 goto recenter;
15525
15526 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15527
15528 /* If someone specified a new starting point but did not insist,
15529 check whether it can be used. */
15530 if (w->optional_new_start
15531 && CHARPOS (startp) >= BEGV
15532 && CHARPOS (startp) <= ZV)
15533 {
15534 w->optional_new_start = 0;
15535 start_display (&it, w, startp);
15536 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15537 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15538 if (IT_CHARPOS (it) == PT)
15539 w->force_start = 1;
15540 /* IT may overshoot PT if text at PT is invisible. */
15541 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15542 w->force_start = 1;
15543 }
15544
15545 force_start:
15546
15547 /* Handle case where place to start displaying has been specified,
15548 unless the specified location is outside the accessible range. */
15549 if (w->force_start || w->frozen_window_start_p)
15550 {
15551 /* We set this later on if we have to adjust point. */
15552 int new_vpos = -1;
15553
15554 w->force_start = 0;
15555 w->vscroll = 0;
15556 w->window_end_valid = 0;
15557
15558 /* Forget any recorded base line for line number display. */
15559 if (!buffer_unchanged_p)
15560 w->base_line_number = 0;
15561
15562 /* Redisplay the mode line. Select the buffer properly for that.
15563 Also, run the hook window-scroll-functions
15564 because we have scrolled. */
15565 /* Note, we do this after clearing force_start because
15566 if there's an error, it is better to forget about force_start
15567 than to get into an infinite loop calling the hook functions
15568 and having them get more errors. */
15569 if (!update_mode_line
15570 || ! NILP (Vwindow_scroll_functions))
15571 {
15572 update_mode_line = 1;
15573 w->update_mode_line = 1;
15574 startp = run_window_scroll_functions (window, startp);
15575 }
15576
15577 w->last_modified = 0;
15578 w->last_overlay_modified = 0;
15579 if (CHARPOS (startp) < BEGV)
15580 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15581 else if (CHARPOS (startp) > ZV)
15582 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15583
15584 /* Redisplay, then check if cursor has been set during the
15585 redisplay. Give up if new fonts were loaded. */
15586 /* We used to issue a CHECK_MARGINS argument to try_window here,
15587 but this causes scrolling to fail when point begins inside
15588 the scroll margin (bug#148) -- cyd */
15589 if (!try_window (window, startp, 0))
15590 {
15591 w->force_start = 1;
15592 clear_glyph_matrix (w->desired_matrix);
15593 goto need_larger_matrices;
15594 }
15595
15596 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15597 {
15598 /* If point does not appear, try to move point so it does
15599 appear. The desired matrix has been built above, so we
15600 can use it here. */
15601 new_vpos = window_box_height (w) / 2;
15602 }
15603
15604 if (!cursor_row_fully_visible_p (w, 0, 0))
15605 {
15606 /* Point does appear, but on a line partly visible at end of window.
15607 Move it back to a fully-visible line. */
15608 new_vpos = window_box_height (w);
15609 }
15610 else if (w->cursor.vpos >=0)
15611 {
15612 /* Some people insist on not letting point enter the scroll
15613 margin, even though this part handles windows that didn't
15614 scroll at all. */
15615 int window_total_lines
15616 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15617 int margin = min (scroll_margin, window_total_lines / 4);
15618 int pixel_margin = margin * frame_line_height;
15619 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15620
15621 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15622 below, which finds the row to move point to, advances by
15623 the Y coordinate of the _next_ row, see the definition of
15624 MATRIX_ROW_BOTTOM_Y. */
15625 if (w->cursor.vpos < margin + header_line)
15626 new_vpos
15627 = pixel_margin + (header_line
15628 ? CURRENT_HEADER_LINE_HEIGHT (w)
15629 : 0) + frame_line_height;
15630 else
15631 {
15632 int window_height = window_box_height (w);
15633
15634 if (header_line)
15635 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15636 if (w->cursor.y >= window_height - pixel_margin)
15637 new_vpos = window_height - pixel_margin;
15638 }
15639 }
15640
15641 /* If we need to move point for either of the above reasons,
15642 now actually do it. */
15643 if (new_vpos >= 0)
15644 {
15645 struct glyph_row *row;
15646
15647 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15648 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15649 ++row;
15650
15651 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15652 MATRIX_ROW_START_BYTEPOS (row));
15653
15654 if (w != XWINDOW (selected_window))
15655 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15656 else if (current_buffer == old)
15657 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15658
15659 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15660
15661 /* If we are highlighting the region, then we just changed
15662 the region, so redisplay to show it. */
15663 if (markpos_of_region () >= 0)
15664 {
15665 clear_glyph_matrix (w->desired_matrix);
15666 if (!try_window (window, startp, 0))
15667 goto need_larger_matrices;
15668 }
15669 }
15670
15671 #ifdef GLYPH_DEBUG
15672 debug_method_add (w, "forced window start");
15673 #endif
15674 goto done;
15675 }
15676
15677 /* Handle case where text has not changed, only point, and it has
15678 not moved off the frame, and we are not retrying after hscroll.
15679 (current_matrix_up_to_date_p is nonzero when retrying.) */
15680 if (current_matrix_up_to_date_p
15681 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15682 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15683 {
15684 switch (rc)
15685 {
15686 case CURSOR_MOVEMENT_SUCCESS:
15687 used_current_matrix_p = 1;
15688 goto done;
15689
15690 case CURSOR_MOVEMENT_MUST_SCROLL:
15691 goto try_to_scroll;
15692
15693 default:
15694 emacs_abort ();
15695 }
15696 }
15697 /* If current starting point was originally the beginning of a line
15698 but no longer is, find a new starting point. */
15699 else if (w->start_at_line_beg
15700 && !(CHARPOS (startp) <= BEGV
15701 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15702 {
15703 #ifdef GLYPH_DEBUG
15704 debug_method_add (w, "recenter 1");
15705 #endif
15706 goto recenter;
15707 }
15708
15709 /* Try scrolling with try_window_id. Value is > 0 if update has
15710 been done, it is -1 if we know that the same window start will
15711 not work. It is 0 if unsuccessful for some other reason. */
15712 else if ((tem = try_window_id (w)) != 0)
15713 {
15714 #ifdef GLYPH_DEBUG
15715 debug_method_add (w, "try_window_id %d", tem);
15716 #endif
15717
15718 if (fonts_changed_p)
15719 goto need_larger_matrices;
15720 if (tem > 0)
15721 goto done;
15722
15723 /* Otherwise try_window_id has returned -1 which means that we
15724 don't want the alternative below this comment to execute. */
15725 }
15726 else if (CHARPOS (startp) >= BEGV
15727 && CHARPOS (startp) <= ZV
15728 && PT >= CHARPOS (startp)
15729 && (CHARPOS (startp) < ZV
15730 /* Avoid starting at end of buffer. */
15731 || CHARPOS (startp) == BEGV
15732 || !window_outdated (w)))
15733 {
15734 int d1, d2, d3, d4, d5, d6;
15735
15736 /* If first window line is a continuation line, and window start
15737 is inside the modified region, but the first change is before
15738 current window start, we must select a new window start.
15739
15740 However, if this is the result of a down-mouse event (e.g. by
15741 extending the mouse-drag-overlay), we don't want to select a
15742 new window start, since that would change the position under
15743 the mouse, resulting in an unwanted mouse-movement rather
15744 than a simple mouse-click. */
15745 if (!w->start_at_line_beg
15746 && NILP (do_mouse_tracking)
15747 && CHARPOS (startp) > BEGV
15748 && CHARPOS (startp) > BEG + beg_unchanged
15749 && CHARPOS (startp) <= Z - end_unchanged
15750 /* Even if w->start_at_line_beg is nil, a new window may
15751 start at a line_beg, since that's how set_buffer_window
15752 sets it. So, we need to check the return value of
15753 compute_window_start_on_continuation_line. (See also
15754 bug#197). */
15755 && XMARKER (w->start)->buffer == current_buffer
15756 && compute_window_start_on_continuation_line (w)
15757 /* It doesn't make sense to force the window start like we
15758 do at label force_start if it is already known that point
15759 will not be visible in the resulting window, because
15760 doing so will move point from its correct position
15761 instead of scrolling the window to bring point into view.
15762 See bug#9324. */
15763 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15764 {
15765 w->force_start = 1;
15766 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15767 goto force_start;
15768 }
15769
15770 #ifdef GLYPH_DEBUG
15771 debug_method_add (w, "same window start");
15772 #endif
15773
15774 /* Try to redisplay starting at same place as before.
15775 If point has not moved off frame, accept the results. */
15776 if (!current_matrix_up_to_date_p
15777 /* Don't use try_window_reusing_current_matrix in this case
15778 because a window scroll function can have changed the
15779 buffer. */
15780 || !NILP (Vwindow_scroll_functions)
15781 || MINI_WINDOW_P (w)
15782 || !(used_current_matrix_p
15783 = try_window_reusing_current_matrix (w)))
15784 {
15785 IF_DEBUG (debug_method_add (w, "1"));
15786 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15787 /* -1 means we need to scroll.
15788 0 means we need new matrices, but fonts_changed_p
15789 is set in that case, so we will detect it below. */
15790 goto try_to_scroll;
15791 }
15792
15793 if (fonts_changed_p)
15794 goto need_larger_matrices;
15795
15796 if (w->cursor.vpos >= 0)
15797 {
15798 if (!just_this_one_p
15799 || current_buffer->clip_changed
15800 || BEG_UNCHANGED < CHARPOS (startp))
15801 /* Forget any recorded base line for line number display. */
15802 w->base_line_number = 0;
15803
15804 if (!cursor_row_fully_visible_p (w, 1, 0))
15805 {
15806 clear_glyph_matrix (w->desired_matrix);
15807 last_line_misfit = 1;
15808 }
15809 /* Drop through and scroll. */
15810 else
15811 goto done;
15812 }
15813 else
15814 clear_glyph_matrix (w->desired_matrix);
15815 }
15816
15817 try_to_scroll:
15818
15819 w->last_modified = 0;
15820 w->last_overlay_modified = 0;
15821
15822 /* Redisplay the mode line. Select the buffer properly for that. */
15823 if (!update_mode_line)
15824 {
15825 update_mode_line = 1;
15826 w->update_mode_line = 1;
15827 }
15828
15829 /* Try to scroll by specified few lines. */
15830 if ((scroll_conservatively
15831 || emacs_scroll_step
15832 || temp_scroll_step
15833 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15834 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15835 && CHARPOS (startp) >= BEGV
15836 && CHARPOS (startp) <= ZV)
15837 {
15838 /* The function returns -1 if new fonts were loaded, 1 if
15839 successful, 0 if not successful. */
15840 int ss = try_scrolling (window, just_this_one_p,
15841 scroll_conservatively,
15842 emacs_scroll_step,
15843 temp_scroll_step, last_line_misfit);
15844 switch (ss)
15845 {
15846 case SCROLLING_SUCCESS:
15847 goto done;
15848
15849 case SCROLLING_NEED_LARGER_MATRICES:
15850 goto need_larger_matrices;
15851
15852 case SCROLLING_FAILED:
15853 break;
15854
15855 default:
15856 emacs_abort ();
15857 }
15858 }
15859
15860 /* Finally, just choose a place to start which positions point
15861 according to user preferences. */
15862
15863 recenter:
15864
15865 #ifdef GLYPH_DEBUG
15866 debug_method_add (w, "recenter");
15867 #endif
15868
15869 /* Forget any previously recorded base line for line number display. */
15870 if (!buffer_unchanged_p)
15871 w->base_line_number = 0;
15872
15873 /* Determine the window start relative to point. */
15874 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15875 it.current_y = it.last_visible_y;
15876 if (centering_position < 0)
15877 {
15878 int window_total_lines
15879 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15880 int margin =
15881 scroll_margin > 0
15882 ? min (scroll_margin, window_total_lines / 4)
15883 : 0;
15884 ptrdiff_t margin_pos = CHARPOS (startp);
15885 Lisp_Object aggressive;
15886 int scrolling_up;
15887
15888 /* If there is a scroll margin at the top of the window, find
15889 its character position. */
15890 if (margin
15891 /* Cannot call start_display if startp is not in the
15892 accessible region of the buffer. This can happen when we
15893 have just switched to a different buffer and/or changed
15894 its restriction. In that case, startp is initialized to
15895 the character position 1 (BEGV) because we did not yet
15896 have chance to display the buffer even once. */
15897 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15898 {
15899 struct it it1;
15900 void *it1data = NULL;
15901
15902 SAVE_IT (it1, it, it1data);
15903 start_display (&it1, w, startp);
15904 move_it_vertically (&it1, margin * frame_line_height);
15905 margin_pos = IT_CHARPOS (it1);
15906 RESTORE_IT (&it, &it, it1data);
15907 }
15908 scrolling_up = PT > margin_pos;
15909 aggressive =
15910 scrolling_up
15911 ? BVAR (current_buffer, scroll_up_aggressively)
15912 : BVAR (current_buffer, scroll_down_aggressively);
15913
15914 if (!MINI_WINDOW_P (w)
15915 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15916 {
15917 int pt_offset = 0;
15918
15919 /* Setting scroll-conservatively overrides
15920 scroll-*-aggressively. */
15921 if (!scroll_conservatively && NUMBERP (aggressive))
15922 {
15923 double float_amount = XFLOATINT (aggressive);
15924
15925 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15926 if (pt_offset == 0 && float_amount > 0)
15927 pt_offset = 1;
15928 if (pt_offset && margin > 0)
15929 margin -= 1;
15930 }
15931 /* Compute how much to move the window start backward from
15932 point so that point will be displayed where the user
15933 wants it. */
15934 if (scrolling_up)
15935 {
15936 centering_position = it.last_visible_y;
15937 if (pt_offset)
15938 centering_position -= pt_offset;
15939 centering_position -=
15940 frame_line_height * (1 + margin + (last_line_misfit != 0))
15941 + WINDOW_HEADER_LINE_HEIGHT (w);
15942 /* Don't let point enter the scroll margin near top of
15943 the window. */
15944 if (centering_position < margin * frame_line_height)
15945 centering_position = margin * frame_line_height;
15946 }
15947 else
15948 centering_position = margin * frame_line_height + pt_offset;
15949 }
15950 else
15951 /* Set the window start half the height of the window backward
15952 from point. */
15953 centering_position = window_box_height (w) / 2;
15954 }
15955 move_it_vertically_backward (&it, centering_position);
15956
15957 eassert (IT_CHARPOS (it) >= BEGV);
15958
15959 /* The function move_it_vertically_backward may move over more
15960 than the specified y-distance. If it->w is small, e.g. a
15961 mini-buffer window, we may end up in front of the window's
15962 display area. Start displaying at the start of the line
15963 containing PT in this case. */
15964 if (it.current_y <= 0)
15965 {
15966 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15967 move_it_vertically_backward (&it, 0);
15968 it.current_y = 0;
15969 }
15970
15971 it.current_x = it.hpos = 0;
15972
15973 /* Set the window start position here explicitly, to avoid an
15974 infinite loop in case the functions in window-scroll-functions
15975 get errors. */
15976 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15977
15978 /* Run scroll hooks. */
15979 startp = run_window_scroll_functions (window, it.current.pos);
15980
15981 /* Redisplay the window. */
15982 if (!current_matrix_up_to_date_p
15983 || windows_or_buffers_changed
15984 || cursor_type_changed
15985 /* Don't use try_window_reusing_current_matrix in this case
15986 because it can have changed the buffer. */
15987 || !NILP (Vwindow_scroll_functions)
15988 || !just_this_one_p
15989 || MINI_WINDOW_P (w)
15990 || !(used_current_matrix_p
15991 = try_window_reusing_current_matrix (w)))
15992 try_window (window, startp, 0);
15993
15994 /* If new fonts have been loaded (due to fontsets), give up. We
15995 have to start a new redisplay since we need to re-adjust glyph
15996 matrices. */
15997 if (fonts_changed_p)
15998 goto need_larger_matrices;
15999
16000 /* If cursor did not appear assume that the middle of the window is
16001 in the first line of the window. Do it again with the next line.
16002 (Imagine a window of height 100, displaying two lines of height
16003 60. Moving back 50 from it->last_visible_y will end in the first
16004 line.) */
16005 if (w->cursor.vpos < 0)
16006 {
16007 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
16008 {
16009 clear_glyph_matrix (w->desired_matrix);
16010 move_it_by_lines (&it, 1);
16011 try_window (window, it.current.pos, 0);
16012 }
16013 else if (PT < IT_CHARPOS (it))
16014 {
16015 clear_glyph_matrix (w->desired_matrix);
16016 move_it_by_lines (&it, -1);
16017 try_window (window, it.current.pos, 0);
16018 }
16019 else
16020 {
16021 /* Not much we can do about it. */
16022 }
16023 }
16024
16025 /* Consider the following case: Window starts at BEGV, there is
16026 invisible, intangible text at BEGV, so that display starts at
16027 some point START > BEGV. It can happen that we are called with
16028 PT somewhere between BEGV and START. Try to handle that case. */
16029 if (w->cursor.vpos < 0)
16030 {
16031 struct glyph_row *row = w->current_matrix->rows;
16032 if (row->mode_line_p)
16033 ++row;
16034 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16035 }
16036
16037 if (!cursor_row_fully_visible_p (w, 0, 0))
16038 {
16039 /* If vscroll is enabled, disable it and try again. */
16040 if (w->vscroll)
16041 {
16042 w->vscroll = 0;
16043 clear_glyph_matrix (w->desired_matrix);
16044 goto recenter;
16045 }
16046
16047 /* Users who set scroll-conservatively to a large number want
16048 point just above/below the scroll margin. If we ended up
16049 with point's row partially visible, move the window start to
16050 make that row fully visible and out of the margin. */
16051 if (scroll_conservatively > SCROLL_LIMIT)
16052 {
16053 int window_total_lines
16054 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16055 int margin =
16056 scroll_margin > 0
16057 ? min (scroll_margin, window_total_lines / 4)
16058 : 0;
16059 int move_down = w->cursor.vpos >= window_total_lines / 2;
16060
16061 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16062 clear_glyph_matrix (w->desired_matrix);
16063 if (1 == try_window (window, it.current.pos,
16064 TRY_WINDOW_CHECK_MARGINS))
16065 goto done;
16066 }
16067
16068 /* If centering point failed to make the whole line visible,
16069 put point at the top instead. That has to make the whole line
16070 visible, if it can be done. */
16071 if (centering_position == 0)
16072 goto done;
16073
16074 clear_glyph_matrix (w->desired_matrix);
16075 centering_position = 0;
16076 goto recenter;
16077 }
16078
16079 done:
16080
16081 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16082 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16083 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16084
16085 /* Display the mode line, if we must. */
16086 if ((update_mode_line
16087 /* If window not full width, must redo its mode line
16088 if (a) the window to its side is being redone and
16089 (b) we do a frame-based redisplay. This is a consequence
16090 of how inverted lines are drawn in frame-based redisplay. */
16091 || (!just_this_one_p
16092 && !FRAME_WINDOW_P (f)
16093 && !WINDOW_FULL_WIDTH_P (w))
16094 /* Line number to display. */
16095 || w->base_line_pos > 0
16096 /* Column number is displayed and different from the one displayed. */
16097 || (w->column_number_displayed != -1
16098 && (w->column_number_displayed != current_column ())))
16099 /* This means that the window has a mode line. */
16100 && (WINDOW_WANTS_MODELINE_P (w)
16101 || WINDOW_WANTS_HEADER_LINE_P (w)))
16102 {
16103 display_mode_lines (w);
16104
16105 /* If mode line height has changed, arrange for a thorough
16106 immediate redisplay using the correct mode line height. */
16107 if (WINDOW_WANTS_MODELINE_P (w)
16108 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16109 {
16110 fonts_changed_p = 1;
16111 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16112 = DESIRED_MODE_LINE_HEIGHT (w);
16113 }
16114
16115 /* If header line height has changed, arrange for a thorough
16116 immediate redisplay using the correct header line height. */
16117 if (WINDOW_WANTS_HEADER_LINE_P (w)
16118 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16119 {
16120 fonts_changed_p = 1;
16121 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16122 = DESIRED_HEADER_LINE_HEIGHT (w);
16123 }
16124
16125 if (fonts_changed_p)
16126 goto need_larger_matrices;
16127 }
16128
16129 if (!line_number_displayed && w->base_line_pos != -1)
16130 {
16131 w->base_line_pos = 0;
16132 w->base_line_number = 0;
16133 }
16134
16135 finish_menu_bars:
16136
16137 /* When we reach a frame's selected window, redo the frame's menu bar. */
16138 if (update_mode_line
16139 && EQ (FRAME_SELECTED_WINDOW (f), window))
16140 {
16141 int redisplay_menu_p = 0;
16142
16143 if (FRAME_WINDOW_P (f))
16144 {
16145 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16146 || defined (HAVE_NS) || defined (USE_GTK)
16147 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16148 #else
16149 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16150 #endif
16151 }
16152 else
16153 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16154
16155 if (redisplay_menu_p)
16156 display_menu_bar (w);
16157
16158 #ifdef HAVE_WINDOW_SYSTEM
16159 if (FRAME_WINDOW_P (f))
16160 {
16161 #if defined (USE_GTK) || defined (HAVE_NS)
16162 if (FRAME_EXTERNAL_TOOL_BAR (f))
16163 redisplay_tool_bar (f);
16164 #else
16165 if (WINDOWP (f->tool_bar_window)
16166 && (FRAME_TOOL_BAR_LINES (f) > 0
16167 || !NILP (Vauto_resize_tool_bars))
16168 && redisplay_tool_bar (f))
16169 ignore_mouse_drag_p = 1;
16170 #endif
16171 }
16172 #endif
16173 }
16174
16175 #ifdef HAVE_WINDOW_SYSTEM
16176 if (FRAME_WINDOW_P (f)
16177 && update_window_fringes (w, (just_this_one_p
16178 || (!used_current_matrix_p && !overlay_arrow_seen)
16179 || w->pseudo_window_p)))
16180 {
16181 update_begin (f);
16182 block_input ();
16183 if (draw_window_fringes (w, 1))
16184 x_draw_vertical_border (w);
16185 unblock_input ();
16186 update_end (f);
16187 }
16188 #endif /* HAVE_WINDOW_SYSTEM */
16189
16190 /* We go to this label, with fonts_changed_p set,
16191 if it is necessary to try again using larger glyph matrices.
16192 We have to redeem the scroll bar even in this case,
16193 because the loop in redisplay_internal expects that. */
16194 need_larger_matrices:
16195 ;
16196 finish_scroll_bars:
16197
16198 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16199 {
16200 /* Set the thumb's position and size. */
16201 set_vertical_scroll_bar (w);
16202
16203 /* Note that we actually used the scroll bar attached to this
16204 window, so it shouldn't be deleted at the end of redisplay. */
16205 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16206 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16207 }
16208
16209 /* Restore current_buffer and value of point in it. The window
16210 update may have changed the buffer, so first make sure `opoint'
16211 is still valid (Bug#6177). */
16212 if (CHARPOS (opoint) < BEGV)
16213 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16214 else if (CHARPOS (opoint) > ZV)
16215 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16216 else
16217 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16218
16219 set_buffer_internal_1 (old);
16220 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16221 shorter. This can be caused by log truncation in *Messages*. */
16222 if (CHARPOS (lpoint) <= ZV)
16223 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16224
16225 unbind_to (count, Qnil);
16226 }
16227
16228
16229 /* Build the complete desired matrix of WINDOW with a window start
16230 buffer position POS.
16231
16232 Value is 1 if successful. It is zero if fonts were loaded during
16233 redisplay which makes re-adjusting glyph matrices necessary, and -1
16234 if point would appear in the scroll margins.
16235 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16236 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16237 set in FLAGS.) */
16238
16239 int
16240 try_window (Lisp_Object window, struct text_pos pos, int flags)
16241 {
16242 struct window *w = XWINDOW (window);
16243 struct it it;
16244 struct glyph_row *last_text_row = NULL;
16245 struct frame *f = XFRAME (w->frame);
16246 int frame_line_height = default_line_pixel_height (w);
16247
16248 /* Make POS the new window start. */
16249 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16250
16251 /* Mark cursor position as unknown. No overlay arrow seen. */
16252 w->cursor.vpos = -1;
16253 overlay_arrow_seen = 0;
16254
16255 /* Initialize iterator and info to start at POS. */
16256 start_display (&it, w, pos);
16257
16258 /* Display all lines of W. */
16259 while (it.current_y < it.last_visible_y)
16260 {
16261 if (display_line (&it))
16262 last_text_row = it.glyph_row - 1;
16263 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16264 return 0;
16265 }
16266
16267 /* Don't let the cursor end in the scroll margins. */
16268 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16269 && !MINI_WINDOW_P (w))
16270 {
16271 int this_scroll_margin;
16272 int window_total_lines
16273 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16274
16275 if (scroll_margin > 0)
16276 {
16277 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16278 this_scroll_margin *= frame_line_height;
16279 }
16280 else
16281 this_scroll_margin = 0;
16282
16283 if ((w->cursor.y >= 0 /* not vscrolled */
16284 && w->cursor.y < this_scroll_margin
16285 && CHARPOS (pos) > BEGV
16286 && IT_CHARPOS (it) < ZV)
16287 /* rms: considering make_cursor_line_fully_visible_p here
16288 seems to give wrong results. We don't want to recenter
16289 when the last line is partly visible, we want to allow
16290 that case to be handled in the usual way. */
16291 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16292 {
16293 w->cursor.vpos = -1;
16294 clear_glyph_matrix (w->desired_matrix);
16295 return -1;
16296 }
16297 }
16298
16299 /* If bottom moved off end of frame, change mode line percentage. */
16300 if (XFASTINT (w->window_end_pos) <= 0
16301 && Z != IT_CHARPOS (it))
16302 w->update_mode_line = 1;
16303
16304 /* Set window_end_pos to the offset of the last character displayed
16305 on the window from the end of current_buffer. Set
16306 window_end_vpos to its row number. */
16307 if (last_text_row)
16308 {
16309 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16310 w->window_end_bytepos
16311 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16312 wset_window_end_pos
16313 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16314 wset_window_end_vpos
16315 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16316 eassert
16317 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16318 XFASTINT (w->window_end_vpos))));
16319 }
16320 else
16321 {
16322 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16323 wset_window_end_pos (w, make_number (Z - ZV));
16324 wset_window_end_vpos (w, make_number (0));
16325 }
16326
16327 /* But that is not valid info until redisplay finishes. */
16328 w->window_end_valid = 0;
16329 return 1;
16330 }
16331
16332
16333 \f
16334 /************************************************************************
16335 Window redisplay reusing current matrix when buffer has not changed
16336 ************************************************************************/
16337
16338 /* Try redisplay of window W showing an unchanged buffer with a
16339 different window start than the last time it was displayed by
16340 reusing its current matrix. Value is non-zero if successful.
16341 W->start is the new window start. */
16342
16343 static int
16344 try_window_reusing_current_matrix (struct window *w)
16345 {
16346 struct frame *f = XFRAME (w->frame);
16347 struct glyph_row *bottom_row;
16348 struct it it;
16349 struct run run;
16350 struct text_pos start, new_start;
16351 int nrows_scrolled, i;
16352 struct glyph_row *last_text_row;
16353 struct glyph_row *last_reused_text_row;
16354 struct glyph_row *start_row;
16355 int start_vpos, min_y, max_y;
16356
16357 #ifdef GLYPH_DEBUG
16358 if (inhibit_try_window_reusing)
16359 return 0;
16360 #endif
16361
16362 if (/* This function doesn't handle terminal frames. */
16363 !FRAME_WINDOW_P (f)
16364 /* Don't try to reuse the display if windows have been split
16365 or such. */
16366 || windows_or_buffers_changed
16367 || cursor_type_changed)
16368 return 0;
16369
16370 /* Can't do this if region may have changed. */
16371 if (markpos_of_region () >= 0
16372 || w->region_showing
16373 || !NILP (Vshow_trailing_whitespace))
16374 return 0;
16375
16376 /* If top-line visibility has changed, give up. */
16377 if (WINDOW_WANTS_HEADER_LINE_P (w)
16378 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16379 return 0;
16380
16381 /* Give up if old or new display is scrolled vertically. We could
16382 make this function handle this, but right now it doesn't. */
16383 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16384 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16385 return 0;
16386
16387 /* The variable new_start now holds the new window start. The old
16388 start `start' can be determined from the current matrix. */
16389 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16390 start = start_row->minpos;
16391 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16392
16393 /* Clear the desired matrix for the display below. */
16394 clear_glyph_matrix (w->desired_matrix);
16395
16396 if (CHARPOS (new_start) <= CHARPOS (start))
16397 {
16398 /* Don't use this method if the display starts with an ellipsis
16399 displayed for invisible text. It's not easy to handle that case
16400 below, and it's certainly not worth the effort since this is
16401 not a frequent case. */
16402 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16403 return 0;
16404
16405 IF_DEBUG (debug_method_add (w, "twu1"));
16406
16407 /* Display up to a row that can be reused. The variable
16408 last_text_row is set to the last row displayed that displays
16409 text. Note that it.vpos == 0 if or if not there is a
16410 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16411 start_display (&it, w, new_start);
16412 w->cursor.vpos = -1;
16413 last_text_row = last_reused_text_row = NULL;
16414
16415 while (it.current_y < it.last_visible_y
16416 && !fonts_changed_p)
16417 {
16418 /* If we have reached into the characters in the START row,
16419 that means the line boundaries have changed. So we
16420 can't start copying with the row START. Maybe it will
16421 work to start copying with the following row. */
16422 while (IT_CHARPOS (it) > CHARPOS (start))
16423 {
16424 /* Advance to the next row as the "start". */
16425 start_row++;
16426 start = start_row->minpos;
16427 /* If there are no more rows to try, or just one, give up. */
16428 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16429 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16430 || CHARPOS (start) == ZV)
16431 {
16432 clear_glyph_matrix (w->desired_matrix);
16433 return 0;
16434 }
16435
16436 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16437 }
16438 /* If we have reached alignment, we can copy the rest of the
16439 rows. */
16440 if (IT_CHARPOS (it) == CHARPOS (start)
16441 /* Don't accept "alignment" inside a display vector,
16442 since start_row could have started in the middle of
16443 that same display vector (thus their character
16444 positions match), and we have no way of telling if
16445 that is the case. */
16446 && it.current.dpvec_index < 0)
16447 break;
16448
16449 if (display_line (&it))
16450 last_text_row = it.glyph_row - 1;
16451
16452 }
16453
16454 /* A value of current_y < last_visible_y means that we stopped
16455 at the previous window start, which in turn means that we
16456 have at least one reusable row. */
16457 if (it.current_y < it.last_visible_y)
16458 {
16459 struct glyph_row *row;
16460
16461 /* IT.vpos always starts from 0; it counts text lines. */
16462 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16463
16464 /* Find PT if not already found in the lines displayed. */
16465 if (w->cursor.vpos < 0)
16466 {
16467 int dy = it.current_y - start_row->y;
16468
16469 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16470 row = row_containing_pos (w, PT, row, NULL, dy);
16471 if (row)
16472 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16473 dy, nrows_scrolled);
16474 else
16475 {
16476 clear_glyph_matrix (w->desired_matrix);
16477 return 0;
16478 }
16479 }
16480
16481 /* Scroll the display. Do it before the current matrix is
16482 changed. The problem here is that update has not yet
16483 run, i.e. part of the current matrix is not up to date.
16484 scroll_run_hook will clear the cursor, and use the
16485 current matrix to get the height of the row the cursor is
16486 in. */
16487 run.current_y = start_row->y;
16488 run.desired_y = it.current_y;
16489 run.height = it.last_visible_y - it.current_y;
16490
16491 if (run.height > 0 && run.current_y != run.desired_y)
16492 {
16493 update_begin (f);
16494 FRAME_RIF (f)->update_window_begin_hook (w);
16495 FRAME_RIF (f)->clear_window_mouse_face (w);
16496 FRAME_RIF (f)->scroll_run_hook (w, &run);
16497 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16498 update_end (f);
16499 }
16500
16501 /* Shift current matrix down by nrows_scrolled lines. */
16502 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16503 rotate_matrix (w->current_matrix,
16504 start_vpos,
16505 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16506 nrows_scrolled);
16507
16508 /* Disable lines that must be updated. */
16509 for (i = 0; i < nrows_scrolled; ++i)
16510 (start_row + i)->enabled_p = 0;
16511
16512 /* Re-compute Y positions. */
16513 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16514 max_y = it.last_visible_y;
16515 for (row = start_row + nrows_scrolled;
16516 row < bottom_row;
16517 ++row)
16518 {
16519 row->y = it.current_y;
16520 row->visible_height = row->height;
16521
16522 if (row->y < min_y)
16523 row->visible_height -= min_y - row->y;
16524 if (row->y + row->height > max_y)
16525 row->visible_height -= row->y + row->height - max_y;
16526 if (row->fringe_bitmap_periodic_p)
16527 row->redraw_fringe_bitmaps_p = 1;
16528
16529 it.current_y += row->height;
16530
16531 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16532 last_reused_text_row = row;
16533 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16534 break;
16535 }
16536
16537 /* Disable lines in the current matrix which are now
16538 below the window. */
16539 for (++row; row < bottom_row; ++row)
16540 row->enabled_p = row->mode_line_p = 0;
16541 }
16542
16543 /* Update window_end_pos etc.; last_reused_text_row is the last
16544 reused row from the current matrix containing text, if any.
16545 The value of last_text_row is the last displayed line
16546 containing text. */
16547 if (last_reused_text_row)
16548 {
16549 w->window_end_bytepos
16550 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16551 wset_window_end_pos
16552 (w, make_number (Z
16553 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16554 wset_window_end_vpos
16555 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16556 w->current_matrix)));
16557 }
16558 else if (last_text_row)
16559 {
16560 w->window_end_bytepos
16561 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16562 wset_window_end_pos
16563 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16564 wset_window_end_vpos
16565 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16566 w->desired_matrix)));
16567 }
16568 else
16569 {
16570 /* This window must be completely empty. */
16571 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16572 wset_window_end_pos (w, make_number (Z - ZV));
16573 wset_window_end_vpos (w, make_number (0));
16574 }
16575 w->window_end_valid = 0;
16576
16577 /* Update hint: don't try scrolling again in update_window. */
16578 w->desired_matrix->no_scrolling_p = 1;
16579
16580 #ifdef GLYPH_DEBUG
16581 debug_method_add (w, "try_window_reusing_current_matrix 1");
16582 #endif
16583 return 1;
16584 }
16585 else if (CHARPOS (new_start) > CHARPOS (start))
16586 {
16587 struct glyph_row *pt_row, *row;
16588 struct glyph_row *first_reusable_row;
16589 struct glyph_row *first_row_to_display;
16590 int dy;
16591 int yb = window_text_bottom_y (w);
16592
16593 /* Find the row starting at new_start, if there is one. Don't
16594 reuse a partially visible line at the end. */
16595 first_reusable_row = start_row;
16596 while (first_reusable_row->enabled_p
16597 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16598 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16599 < CHARPOS (new_start)))
16600 ++first_reusable_row;
16601
16602 /* Give up if there is no row to reuse. */
16603 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16604 || !first_reusable_row->enabled_p
16605 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16606 != CHARPOS (new_start)))
16607 return 0;
16608
16609 /* We can reuse fully visible rows beginning with
16610 first_reusable_row to the end of the window. Set
16611 first_row_to_display to the first row that cannot be reused.
16612 Set pt_row to the row containing point, if there is any. */
16613 pt_row = NULL;
16614 for (first_row_to_display = first_reusable_row;
16615 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16616 ++first_row_to_display)
16617 {
16618 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16619 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16620 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16621 && first_row_to_display->ends_at_zv_p
16622 && pt_row == NULL)))
16623 pt_row = first_row_to_display;
16624 }
16625
16626 /* Start displaying at the start of first_row_to_display. */
16627 eassert (first_row_to_display->y < yb);
16628 init_to_row_start (&it, w, first_row_to_display);
16629
16630 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16631 - start_vpos);
16632 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16633 - nrows_scrolled);
16634 it.current_y = (first_row_to_display->y - first_reusable_row->y
16635 + WINDOW_HEADER_LINE_HEIGHT (w));
16636
16637 /* Display lines beginning with first_row_to_display in the
16638 desired matrix. Set last_text_row to the last row displayed
16639 that displays text. */
16640 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16641 if (pt_row == NULL)
16642 w->cursor.vpos = -1;
16643 last_text_row = NULL;
16644 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16645 if (display_line (&it))
16646 last_text_row = it.glyph_row - 1;
16647
16648 /* If point is in a reused row, adjust y and vpos of the cursor
16649 position. */
16650 if (pt_row)
16651 {
16652 w->cursor.vpos -= nrows_scrolled;
16653 w->cursor.y -= first_reusable_row->y - start_row->y;
16654 }
16655
16656 /* Give up if point isn't in a row displayed or reused. (This
16657 also handles the case where w->cursor.vpos < nrows_scrolled
16658 after the calls to display_line, which can happen with scroll
16659 margins. See bug#1295.) */
16660 if (w->cursor.vpos < 0)
16661 {
16662 clear_glyph_matrix (w->desired_matrix);
16663 return 0;
16664 }
16665
16666 /* Scroll the display. */
16667 run.current_y = first_reusable_row->y;
16668 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16669 run.height = it.last_visible_y - run.current_y;
16670 dy = run.current_y - run.desired_y;
16671
16672 if (run.height)
16673 {
16674 update_begin (f);
16675 FRAME_RIF (f)->update_window_begin_hook (w);
16676 FRAME_RIF (f)->clear_window_mouse_face (w);
16677 FRAME_RIF (f)->scroll_run_hook (w, &run);
16678 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16679 update_end (f);
16680 }
16681
16682 /* Adjust Y positions of reused rows. */
16683 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16684 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16685 max_y = it.last_visible_y;
16686 for (row = first_reusable_row; row < first_row_to_display; ++row)
16687 {
16688 row->y -= dy;
16689 row->visible_height = row->height;
16690 if (row->y < min_y)
16691 row->visible_height -= min_y - row->y;
16692 if (row->y + row->height > max_y)
16693 row->visible_height -= row->y + row->height - max_y;
16694 if (row->fringe_bitmap_periodic_p)
16695 row->redraw_fringe_bitmaps_p = 1;
16696 }
16697
16698 /* Scroll the current matrix. */
16699 eassert (nrows_scrolled > 0);
16700 rotate_matrix (w->current_matrix,
16701 start_vpos,
16702 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16703 -nrows_scrolled);
16704
16705 /* Disable rows not reused. */
16706 for (row -= nrows_scrolled; row < bottom_row; ++row)
16707 row->enabled_p = 0;
16708
16709 /* Point may have moved to a different line, so we cannot assume that
16710 the previous cursor position is valid; locate the correct row. */
16711 if (pt_row)
16712 {
16713 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16714 row < bottom_row
16715 && PT >= MATRIX_ROW_END_CHARPOS (row)
16716 && !row->ends_at_zv_p;
16717 row++)
16718 {
16719 w->cursor.vpos++;
16720 w->cursor.y = row->y;
16721 }
16722 if (row < bottom_row)
16723 {
16724 /* Can't simply scan the row for point with
16725 bidi-reordered glyph rows. Let set_cursor_from_row
16726 figure out where to put the cursor, and if it fails,
16727 give up. */
16728 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16729 {
16730 if (!set_cursor_from_row (w, row, w->current_matrix,
16731 0, 0, 0, 0))
16732 {
16733 clear_glyph_matrix (w->desired_matrix);
16734 return 0;
16735 }
16736 }
16737 else
16738 {
16739 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16740 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16741
16742 for (; glyph < end
16743 && (!BUFFERP (glyph->object)
16744 || glyph->charpos < PT);
16745 glyph++)
16746 {
16747 w->cursor.hpos++;
16748 w->cursor.x += glyph->pixel_width;
16749 }
16750 }
16751 }
16752 }
16753
16754 /* Adjust window end. A null value of last_text_row means that
16755 the window end is in reused rows which in turn means that
16756 only its vpos can have changed. */
16757 if (last_text_row)
16758 {
16759 w->window_end_bytepos
16760 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16761 wset_window_end_pos
16762 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16763 wset_window_end_vpos
16764 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16765 w->desired_matrix)));
16766 }
16767 else
16768 {
16769 wset_window_end_vpos
16770 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16771 }
16772
16773 w->window_end_valid = 0;
16774 w->desired_matrix->no_scrolling_p = 1;
16775
16776 #ifdef GLYPH_DEBUG
16777 debug_method_add (w, "try_window_reusing_current_matrix 2");
16778 #endif
16779 return 1;
16780 }
16781
16782 return 0;
16783 }
16784
16785
16786 \f
16787 /************************************************************************
16788 Window redisplay reusing current matrix when buffer has changed
16789 ************************************************************************/
16790
16791 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16792 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16793 ptrdiff_t *, ptrdiff_t *);
16794 static struct glyph_row *
16795 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16796 struct glyph_row *);
16797
16798
16799 /* Return the last row in MATRIX displaying text. If row START is
16800 non-null, start searching with that row. IT gives the dimensions
16801 of the display. Value is null if matrix is empty; otherwise it is
16802 a pointer to the row found. */
16803
16804 static struct glyph_row *
16805 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16806 struct glyph_row *start)
16807 {
16808 struct glyph_row *row, *row_found;
16809
16810 /* Set row_found to the last row in IT->w's current matrix
16811 displaying text. The loop looks funny but think of partially
16812 visible lines. */
16813 row_found = NULL;
16814 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16815 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16816 {
16817 eassert (row->enabled_p);
16818 row_found = row;
16819 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16820 break;
16821 ++row;
16822 }
16823
16824 return row_found;
16825 }
16826
16827
16828 /* Return the last row in the current matrix of W that is not affected
16829 by changes at the start of current_buffer that occurred since W's
16830 current matrix was built. Value is null if no such row exists.
16831
16832 BEG_UNCHANGED us the number of characters unchanged at the start of
16833 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16834 first changed character in current_buffer. Characters at positions <
16835 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16836 when the current matrix was built. */
16837
16838 static struct glyph_row *
16839 find_last_unchanged_at_beg_row (struct window *w)
16840 {
16841 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16842 struct glyph_row *row;
16843 struct glyph_row *row_found = NULL;
16844 int yb = window_text_bottom_y (w);
16845
16846 /* Find the last row displaying unchanged text. */
16847 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16848 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16849 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16850 ++row)
16851 {
16852 if (/* If row ends before first_changed_pos, it is unchanged,
16853 except in some case. */
16854 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16855 /* When row ends in ZV and we write at ZV it is not
16856 unchanged. */
16857 && !row->ends_at_zv_p
16858 /* When first_changed_pos is the end of a continued line,
16859 row is not unchanged because it may be no longer
16860 continued. */
16861 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16862 && (row->continued_p
16863 || row->exact_window_width_line_p))
16864 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16865 needs to be recomputed, so don't consider this row as
16866 unchanged. This happens when the last line was
16867 bidi-reordered and was killed immediately before this
16868 redisplay cycle. In that case, ROW->end stores the
16869 buffer position of the first visual-order character of
16870 the killed text, which is now beyond ZV. */
16871 && CHARPOS (row->end.pos) <= ZV)
16872 row_found = row;
16873
16874 /* Stop if last visible row. */
16875 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16876 break;
16877 }
16878
16879 return row_found;
16880 }
16881
16882
16883 /* Find the first glyph row in the current matrix of W that is not
16884 affected by changes at the end of current_buffer since the
16885 time W's current matrix was built.
16886
16887 Return in *DELTA the number of chars by which buffer positions in
16888 unchanged text at the end of current_buffer must be adjusted.
16889
16890 Return in *DELTA_BYTES the corresponding number of bytes.
16891
16892 Value is null if no such row exists, i.e. all rows are affected by
16893 changes. */
16894
16895 static struct glyph_row *
16896 find_first_unchanged_at_end_row (struct window *w,
16897 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16898 {
16899 struct glyph_row *row;
16900 struct glyph_row *row_found = NULL;
16901
16902 *delta = *delta_bytes = 0;
16903
16904 /* Display must not have been paused, otherwise the current matrix
16905 is not up to date. */
16906 eassert (w->window_end_valid);
16907
16908 /* A value of window_end_pos >= END_UNCHANGED means that the window
16909 end is in the range of changed text. If so, there is no
16910 unchanged row at the end of W's current matrix. */
16911 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16912 return NULL;
16913
16914 /* Set row to the last row in W's current matrix displaying text. */
16915 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16916
16917 /* If matrix is entirely empty, no unchanged row exists. */
16918 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16919 {
16920 /* The value of row is the last glyph row in the matrix having a
16921 meaningful buffer position in it. The end position of row
16922 corresponds to window_end_pos. This allows us to translate
16923 buffer positions in the current matrix to current buffer
16924 positions for characters not in changed text. */
16925 ptrdiff_t Z_old =
16926 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16927 ptrdiff_t Z_BYTE_old =
16928 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16929 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16930 struct glyph_row *first_text_row
16931 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16932
16933 *delta = Z - Z_old;
16934 *delta_bytes = Z_BYTE - Z_BYTE_old;
16935
16936 /* Set last_unchanged_pos to the buffer position of the last
16937 character in the buffer that has not been changed. Z is the
16938 index + 1 of the last character in current_buffer, i.e. by
16939 subtracting END_UNCHANGED we get the index of the last
16940 unchanged character, and we have to add BEG to get its buffer
16941 position. */
16942 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16943 last_unchanged_pos_old = last_unchanged_pos - *delta;
16944
16945 /* Search backward from ROW for a row displaying a line that
16946 starts at a minimum position >= last_unchanged_pos_old. */
16947 for (; row > first_text_row; --row)
16948 {
16949 /* This used to abort, but it can happen.
16950 It is ok to just stop the search instead here. KFS. */
16951 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16952 break;
16953
16954 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16955 row_found = row;
16956 }
16957 }
16958
16959 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16960
16961 return row_found;
16962 }
16963
16964
16965 /* Make sure that glyph rows in the current matrix of window W
16966 reference the same glyph memory as corresponding rows in the
16967 frame's frame matrix. This function is called after scrolling W's
16968 current matrix on a terminal frame in try_window_id and
16969 try_window_reusing_current_matrix. */
16970
16971 static void
16972 sync_frame_with_window_matrix_rows (struct window *w)
16973 {
16974 struct frame *f = XFRAME (w->frame);
16975 struct glyph_row *window_row, *window_row_end, *frame_row;
16976
16977 /* Preconditions: W must be a leaf window and full-width. Its frame
16978 must have a frame matrix. */
16979 eassert (BUFFERP (w->contents));
16980 eassert (WINDOW_FULL_WIDTH_P (w));
16981 eassert (!FRAME_WINDOW_P (f));
16982
16983 /* If W is a full-width window, glyph pointers in W's current matrix
16984 have, by definition, to be the same as glyph pointers in the
16985 corresponding frame matrix. Note that frame matrices have no
16986 marginal areas (see build_frame_matrix). */
16987 window_row = w->current_matrix->rows;
16988 window_row_end = window_row + w->current_matrix->nrows;
16989 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16990 while (window_row < window_row_end)
16991 {
16992 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16993 struct glyph *end = window_row->glyphs[LAST_AREA];
16994
16995 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16996 frame_row->glyphs[TEXT_AREA] = start;
16997 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16998 frame_row->glyphs[LAST_AREA] = end;
16999
17000 /* Disable frame rows whose corresponding window rows have
17001 been disabled in try_window_id. */
17002 if (!window_row->enabled_p)
17003 frame_row->enabled_p = 0;
17004
17005 ++window_row, ++frame_row;
17006 }
17007 }
17008
17009
17010 /* Find the glyph row in window W containing CHARPOS. Consider all
17011 rows between START and END (not inclusive). END null means search
17012 all rows to the end of the display area of W. Value is the row
17013 containing CHARPOS or null. */
17014
17015 struct glyph_row *
17016 row_containing_pos (struct window *w, ptrdiff_t charpos,
17017 struct glyph_row *start, struct glyph_row *end, int dy)
17018 {
17019 struct glyph_row *row = start;
17020 struct glyph_row *best_row = NULL;
17021 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17022 int last_y;
17023
17024 /* If we happen to start on a header-line, skip that. */
17025 if (row->mode_line_p)
17026 ++row;
17027
17028 if ((end && row >= end) || !row->enabled_p)
17029 return NULL;
17030
17031 last_y = window_text_bottom_y (w) - dy;
17032
17033 while (1)
17034 {
17035 /* Give up if we have gone too far. */
17036 if (end && row >= end)
17037 return NULL;
17038 /* This formerly returned if they were equal.
17039 I think that both quantities are of a "last plus one" type;
17040 if so, when they are equal, the row is within the screen. -- rms. */
17041 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17042 return NULL;
17043
17044 /* If it is in this row, return this row. */
17045 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17046 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17047 /* The end position of a row equals the start
17048 position of the next row. If CHARPOS is there, we
17049 would rather consider it displayed in the next
17050 line, except when this line ends in ZV. */
17051 && !row_for_charpos_p (row, charpos)))
17052 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17053 {
17054 struct glyph *g;
17055
17056 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17057 || (!best_row && !row->continued_p))
17058 return row;
17059 /* In bidi-reordered rows, there could be several rows whose
17060 edges surround CHARPOS, all of these rows belonging to
17061 the same continued line. We need to find the row which
17062 fits CHARPOS the best. */
17063 for (g = row->glyphs[TEXT_AREA];
17064 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17065 g++)
17066 {
17067 if (!STRINGP (g->object))
17068 {
17069 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17070 {
17071 mindif = eabs (g->charpos - charpos);
17072 best_row = row;
17073 /* Exact match always wins. */
17074 if (mindif == 0)
17075 return best_row;
17076 }
17077 }
17078 }
17079 }
17080 else if (best_row && !row->continued_p)
17081 return best_row;
17082 ++row;
17083 }
17084 }
17085
17086
17087 /* Try to redisplay window W by reusing its existing display. W's
17088 current matrix must be up to date when this function is called,
17089 i.e. window_end_valid must be nonzero.
17090
17091 Value is
17092
17093 1 if display has been updated
17094 0 if otherwise unsuccessful
17095 -1 if redisplay with same window start is known not to succeed
17096
17097 The following steps are performed:
17098
17099 1. Find the last row in the current matrix of W that is not
17100 affected by changes at the start of current_buffer. If no such row
17101 is found, give up.
17102
17103 2. Find the first row in W's current matrix that is not affected by
17104 changes at the end of current_buffer. Maybe there is no such row.
17105
17106 3. Display lines beginning with the row + 1 found in step 1 to the
17107 row found in step 2 or, if step 2 didn't find a row, to the end of
17108 the window.
17109
17110 4. If cursor is not known to appear on the window, give up.
17111
17112 5. If display stopped at the row found in step 2, scroll the
17113 display and current matrix as needed.
17114
17115 6. Maybe display some lines at the end of W, if we must. This can
17116 happen under various circumstances, like a partially visible line
17117 becoming fully visible, or because newly displayed lines are displayed
17118 in smaller font sizes.
17119
17120 7. Update W's window end information. */
17121
17122 static int
17123 try_window_id (struct window *w)
17124 {
17125 struct frame *f = XFRAME (w->frame);
17126 struct glyph_matrix *current_matrix = w->current_matrix;
17127 struct glyph_matrix *desired_matrix = w->desired_matrix;
17128 struct glyph_row *last_unchanged_at_beg_row;
17129 struct glyph_row *first_unchanged_at_end_row;
17130 struct glyph_row *row;
17131 struct glyph_row *bottom_row;
17132 int bottom_vpos;
17133 struct it it;
17134 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17135 int dvpos, dy;
17136 struct text_pos start_pos;
17137 struct run run;
17138 int first_unchanged_at_end_vpos = 0;
17139 struct glyph_row *last_text_row, *last_text_row_at_end;
17140 struct text_pos start;
17141 ptrdiff_t first_changed_charpos, last_changed_charpos;
17142
17143 #ifdef GLYPH_DEBUG
17144 if (inhibit_try_window_id)
17145 return 0;
17146 #endif
17147
17148 /* This is handy for debugging. */
17149 #if 0
17150 #define GIVE_UP(X) \
17151 do { \
17152 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17153 return 0; \
17154 } while (0)
17155 #else
17156 #define GIVE_UP(X) return 0
17157 #endif
17158
17159 SET_TEXT_POS_FROM_MARKER (start, w->start);
17160
17161 /* Don't use this for mini-windows because these can show
17162 messages and mini-buffers, and we don't handle that here. */
17163 if (MINI_WINDOW_P (w))
17164 GIVE_UP (1);
17165
17166 /* This flag is used to prevent redisplay optimizations. */
17167 if (windows_or_buffers_changed || cursor_type_changed)
17168 GIVE_UP (2);
17169
17170 /* Verify that narrowing has not changed.
17171 Also verify that we were not told to prevent redisplay optimizations.
17172 It would be nice to further
17173 reduce the number of cases where this prevents try_window_id. */
17174 if (current_buffer->clip_changed
17175 || current_buffer->prevent_redisplay_optimizations_p)
17176 GIVE_UP (3);
17177
17178 /* Window must either use window-based redisplay or be full width. */
17179 if (!FRAME_WINDOW_P (f)
17180 && (!FRAME_LINE_INS_DEL_OK (f)
17181 || !WINDOW_FULL_WIDTH_P (w)))
17182 GIVE_UP (4);
17183
17184 /* Give up if point is known NOT to appear in W. */
17185 if (PT < CHARPOS (start))
17186 GIVE_UP (5);
17187
17188 /* Another way to prevent redisplay optimizations. */
17189 if (w->last_modified == 0)
17190 GIVE_UP (6);
17191
17192 /* Verify that window is not hscrolled. */
17193 if (w->hscroll != 0)
17194 GIVE_UP (7);
17195
17196 /* Verify that display wasn't paused. */
17197 if (!w->window_end_valid)
17198 GIVE_UP (8);
17199
17200 /* Can't use this if highlighting a region because a cursor movement
17201 will do more than just set the cursor. */
17202 if (markpos_of_region () >= 0)
17203 GIVE_UP (9);
17204
17205 /* Likewise if highlighting trailing whitespace. */
17206 if (!NILP (Vshow_trailing_whitespace))
17207 GIVE_UP (11);
17208
17209 /* Likewise if showing a region. */
17210 if (w->region_showing)
17211 GIVE_UP (10);
17212
17213 /* Can't use this if overlay arrow position and/or string have
17214 changed. */
17215 if (overlay_arrows_changed_p ())
17216 GIVE_UP (12);
17217
17218 /* When word-wrap is on, adding a space to the first word of a
17219 wrapped line can change the wrap position, altering the line
17220 above it. It might be worthwhile to handle this more
17221 intelligently, but for now just redisplay from scratch. */
17222 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17223 GIVE_UP (21);
17224
17225 /* Under bidi reordering, adding or deleting a character in the
17226 beginning of a paragraph, before the first strong directional
17227 character, can change the base direction of the paragraph (unless
17228 the buffer specifies a fixed paragraph direction), which will
17229 require to redisplay the whole paragraph. It might be worthwhile
17230 to find the paragraph limits and widen the range of redisplayed
17231 lines to that, but for now just give up this optimization and
17232 redisplay from scratch. */
17233 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17234 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17235 GIVE_UP (22);
17236
17237 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17238 only if buffer has really changed. The reason is that the gap is
17239 initially at Z for freshly visited files. The code below would
17240 set end_unchanged to 0 in that case. */
17241 if (MODIFF > SAVE_MODIFF
17242 /* This seems to happen sometimes after saving a buffer. */
17243 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17244 {
17245 if (GPT - BEG < BEG_UNCHANGED)
17246 BEG_UNCHANGED = GPT - BEG;
17247 if (Z - GPT < END_UNCHANGED)
17248 END_UNCHANGED = Z - GPT;
17249 }
17250
17251 /* The position of the first and last character that has been changed. */
17252 first_changed_charpos = BEG + BEG_UNCHANGED;
17253 last_changed_charpos = Z - END_UNCHANGED;
17254
17255 /* If window starts after a line end, and the last change is in
17256 front of that newline, then changes don't affect the display.
17257 This case happens with stealth-fontification. Note that although
17258 the display is unchanged, glyph positions in the matrix have to
17259 be adjusted, of course. */
17260 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17261 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17262 && ((last_changed_charpos < CHARPOS (start)
17263 && CHARPOS (start) == BEGV)
17264 || (last_changed_charpos < CHARPOS (start) - 1
17265 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17266 {
17267 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17268 struct glyph_row *r0;
17269
17270 /* Compute how many chars/bytes have been added to or removed
17271 from the buffer. */
17272 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17273 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17274 Z_delta = Z - Z_old;
17275 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17276
17277 /* Give up if PT is not in the window. Note that it already has
17278 been checked at the start of try_window_id that PT is not in
17279 front of the window start. */
17280 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17281 GIVE_UP (13);
17282
17283 /* If window start is unchanged, we can reuse the whole matrix
17284 as is, after adjusting glyph positions. No need to compute
17285 the window end again, since its offset from Z hasn't changed. */
17286 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17287 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17288 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17289 /* PT must not be in a partially visible line. */
17290 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17291 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17292 {
17293 /* Adjust positions in the glyph matrix. */
17294 if (Z_delta || Z_delta_bytes)
17295 {
17296 struct glyph_row *r1
17297 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17298 increment_matrix_positions (w->current_matrix,
17299 MATRIX_ROW_VPOS (r0, current_matrix),
17300 MATRIX_ROW_VPOS (r1, current_matrix),
17301 Z_delta, Z_delta_bytes);
17302 }
17303
17304 /* Set the cursor. */
17305 row = row_containing_pos (w, PT, r0, NULL, 0);
17306 if (row)
17307 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17308 else
17309 emacs_abort ();
17310 return 1;
17311 }
17312 }
17313
17314 /* Handle the case that changes are all below what is displayed in
17315 the window, and that PT is in the window. This shortcut cannot
17316 be taken if ZV is visible in the window, and text has been added
17317 there that is visible in the window. */
17318 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17319 /* ZV is not visible in the window, or there are no
17320 changes at ZV, actually. */
17321 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17322 || first_changed_charpos == last_changed_charpos))
17323 {
17324 struct glyph_row *r0;
17325
17326 /* Give up if PT is not in the window. Note that it already has
17327 been checked at the start of try_window_id that PT is not in
17328 front of the window start. */
17329 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17330 GIVE_UP (14);
17331
17332 /* If window start is unchanged, we can reuse the whole matrix
17333 as is, without changing glyph positions since no text has
17334 been added/removed in front of the window end. */
17335 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17336 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17337 /* PT must not be in a partially visible line. */
17338 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17339 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17340 {
17341 /* We have to compute the window end anew since text
17342 could have been added/removed after it. */
17343 wset_window_end_pos
17344 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17345 w->window_end_bytepos
17346 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17347
17348 /* Set the cursor. */
17349 row = row_containing_pos (w, PT, r0, NULL, 0);
17350 if (row)
17351 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17352 else
17353 emacs_abort ();
17354 return 2;
17355 }
17356 }
17357
17358 /* Give up if window start is in the changed area.
17359
17360 The condition used to read
17361
17362 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17363
17364 but why that was tested escapes me at the moment. */
17365 if (CHARPOS (start) >= first_changed_charpos
17366 && CHARPOS (start) <= last_changed_charpos)
17367 GIVE_UP (15);
17368
17369 /* Check that window start agrees with the start of the first glyph
17370 row in its current matrix. Check this after we know the window
17371 start is not in changed text, otherwise positions would not be
17372 comparable. */
17373 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17374 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17375 GIVE_UP (16);
17376
17377 /* Give up if the window ends in strings. Overlay strings
17378 at the end are difficult to handle, so don't try. */
17379 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17380 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17381 GIVE_UP (20);
17382
17383 /* Compute the position at which we have to start displaying new
17384 lines. Some of the lines at the top of the window might be
17385 reusable because they are not displaying changed text. Find the
17386 last row in W's current matrix not affected by changes at the
17387 start of current_buffer. Value is null if changes start in the
17388 first line of window. */
17389 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17390 if (last_unchanged_at_beg_row)
17391 {
17392 /* Avoid starting to display in the middle of a character, a TAB
17393 for instance. This is easier than to set up the iterator
17394 exactly, and it's not a frequent case, so the additional
17395 effort wouldn't really pay off. */
17396 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17397 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17398 && last_unchanged_at_beg_row > w->current_matrix->rows)
17399 --last_unchanged_at_beg_row;
17400
17401 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17402 GIVE_UP (17);
17403
17404 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17405 GIVE_UP (18);
17406 start_pos = it.current.pos;
17407
17408 /* Start displaying new lines in the desired matrix at the same
17409 vpos we would use in the current matrix, i.e. below
17410 last_unchanged_at_beg_row. */
17411 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17412 current_matrix);
17413 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17414 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17415
17416 eassert (it.hpos == 0 && it.current_x == 0);
17417 }
17418 else
17419 {
17420 /* There are no reusable lines at the start of the window.
17421 Start displaying in the first text line. */
17422 start_display (&it, w, start);
17423 it.vpos = it.first_vpos;
17424 start_pos = it.current.pos;
17425 }
17426
17427 /* Find the first row that is not affected by changes at the end of
17428 the buffer. Value will be null if there is no unchanged row, in
17429 which case we must redisplay to the end of the window. delta
17430 will be set to the value by which buffer positions beginning with
17431 first_unchanged_at_end_row have to be adjusted due to text
17432 changes. */
17433 first_unchanged_at_end_row
17434 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17435 IF_DEBUG (debug_delta = delta);
17436 IF_DEBUG (debug_delta_bytes = delta_bytes);
17437
17438 /* Set stop_pos to the buffer position up to which we will have to
17439 display new lines. If first_unchanged_at_end_row != NULL, this
17440 is the buffer position of the start of the line displayed in that
17441 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17442 that we don't stop at a buffer position. */
17443 stop_pos = 0;
17444 if (first_unchanged_at_end_row)
17445 {
17446 eassert (last_unchanged_at_beg_row == NULL
17447 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17448
17449 /* If this is a continuation line, move forward to the next one
17450 that isn't. Changes in lines above affect this line.
17451 Caution: this may move first_unchanged_at_end_row to a row
17452 not displaying text. */
17453 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17454 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17455 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17456 < it.last_visible_y))
17457 ++first_unchanged_at_end_row;
17458
17459 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17460 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17461 >= it.last_visible_y))
17462 first_unchanged_at_end_row = NULL;
17463 else
17464 {
17465 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17466 + delta);
17467 first_unchanged_at_end_vpos
17468 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17469 eassert (stop_pos >= Z - END_UNCHANGED);
17470 }
17471 }
17472 else if (last_unchanged_at_beg_row == NULL)
17473 GIVE_UP (19);
17474
17475
17476 #ifdef GLYPH_DEBUG
17477
17478 /* Either there is no unchanged row at the end, or the one we have
17479 now displays text. This is a necessary condition for the window
17480 end pos calculation at the end of this function. */
17481 eassert (first_unchanged_at_end_row == NULL
17482 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17483
17484 debug_last_unchanged_at_beg_vpos
17485 = (last_unchanged_at_beg_row
17486 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17487 : -1);
17488 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17489
17490 #endif /* GLYPH_DEBUG */
17491
17492
17493 /* Display new lines. Set last_text_row to the last new line
17494 displayed which has text on it, i.e. might end up as being the
17495 line where the window_end_vpos is. */
17496 w->cursor.vpos = -1;
17497 last_text_row = NULL;
17498 overlay_arrow_seen = 0;
17499 while (it.current_y < it.last_visible_y
17500 && !fonts_changed_p
17501 && (first_unchanged_at_end_row == NULL
17502 || IT_CHARPOS (it) < stop_pos))
17503 {
17504 if (display_line (&it))
17505 last_text_row = it.glyph_row - 1;
17506 }
17507
17508 if (fonts_changed_p)
17509 return -1;
17510
17511
17512 /* Compute differences in buffer positions, y-positions etc. for
17513 lines reused at the bottom of the window. Compute what we can
17514 scroll. */
17515 if (first_unchanged_at_end_row
17516 /* No lines reused because we displayed everything up to the
17517 bottom of the window. */
17518 && it.current_y < it.last_visible_y)
17519 {
17520 dvpos = (it.vpos
17521 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17522 current_matrix));
17523 dy = it.current_y - first_unchanged_at_end_row->y;
17524 run.current_y = first_unchanged_at_end_row->y;
17525 run.desired_y = run.current_y + dy;
17526 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17527 }
17528 else
17529 {
17530 delta = delta_bytes = dvpos = dy
17531 = run.current_y = run.desired_y = run.height = 0;
17532 first_unchanged_at_end_row = NULL;
17533 }
17534 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17535
17536
17537 /* Find the cursor if not already found. We have to decide whether
17538 PT will appear on this window (it sometimes doesn't, but this is
17539 not a very frequent case.) This decision has to be made before
17540 the current matrix is altered. A value of cursor.vpos < 0 means
17541 that PT is either in one of the lines beginning at
17542 first_unchanged_at_end_row or below the window. Don't care for
17543 lines that might be displayed later at the window end; as
17544 mentioned, this is not a frequent case. */
17545 if (w->cursor.vpos < 0)
17546 {
17547 /* Cursor in unchanged rows at the top? */
17548 if (PT < CHARPOS (start_pos)
17549 && last_unchanged_at_beg_row)
17550 {
17551 row = row_containing_pos (w, PT,
17552 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17553 last_unchanged_at_beg_row + 1, 0);
17554 if (row)
17555 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17556 }
17557
17558 /* Start from first_unchanged_at_end_row looking for PT. */
17559 else if (first_unchanged_at_end_row)
17560 {
17561 row = row_containing_pos (w, PT - delta,
17562 first_unchanged_at_end_row, NULL, 0);
17563 if (row)
17564 set_cursor_from_row (w, row, w->current_matrix, delta,
17565 delta_bytes, dy, dvpos);
17566 }
17567
17568 /* Give up if cursor was not found. */
17569 if (w->cursor.vpos < 0)
17570 {
17571 clear_glyph_matrix (w->desired_matrix);
17572 return -1;
17573 }
17574 }
17575
17576 /* Don't let the cursor end in the scroll margins. */
17577 {
17578 int this_scroll_margin, cursor_height;
17579 int frame_line_height = default_line_pixel_height (w);
17580 int window_total_lines
17581 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17582
17583 this_scroll_margin =
17584 max (0, min (scroll_margin, window_total_lines / 4));
17585 this_scroll_margin *= frame_line_height;
17586 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17587
17588 if ((w->cursor.y < this_scroll_margin
17589 && CHARPOS (start) > BEGV)
17590 /* Old redisplay didn't take scroll margin into account at the bottom,
17591 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17592 || (w->cursor.y + (make_cursor_line_fully_visible_p
17593 ? cursor_height + this_scroll_margin
17594 : 1)) > it.last_visible_y)
17595 {
17596 w->cursor.vpos = -1;
17597 clear_glyph_matrix (w->desired_matrix);
17598 return -1;
17599 }
17600 }
17601
17602 /* Scroll the display. Do it before changing the current matrix so
17603 that xterm.c doesn't get confused about where the cursor glyph is
17604 found. */
17605 if (dy && run.height)
17606 {
17607 update_begin (f);
17608
17609 if (FRAME_WINDOW_P (f))
17610 {
17611 FRAME_RIF (f)->update_window_begin_hook (w);
17612 FRAME_RIF (f)->clear_window_mouse_face (w);
17613 FRAME_RIF (f)->scroll_run_hook (w, &run);
17614 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17615 }
17616 else
17617 {
17618 /* Terminal frame. In this case, dvpos gives the number of
17619 lines to scroll by; dvpos < 0 means scroll up. */
17620 int from_vpos
17621 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17622 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17623 int end = (WINDOW_TOP_EDGE_LINE (w)
17624 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17625 + window_internal_height (w));
17626
17627 #if defined (HAVE_GPM) || defined (MSDOS)
17628 x_clear_window_mouse_face (w);
17629 #endif
17630 /* Perform the operation on the screen. */
17631 if (dvpos > 0)
17632 {
17633 /* Scroll last_unchanged_at_beg_row to the end of the
17634 window down dvpos lines. */
17635 set_terminal_window (f, end);
17636
17637 /* On dumb terminals delete dvpos lines at the end
17638 before inserting dvpos empty lines. */
17639 if (!FRAME_SCROLL_REGION_OK (f))
17640 ins_del_lines (f, end - dvpos, -dvpos);
17641
17642 /* Insert dvpos empty lines in front of
17643 last_unchanged_at_beg_row. */
17644 ins_del_lines (f, from, dvpos);
17645 }
17646 else if (dvpos < 0)
17647 {
17648 /* Scroll up last_unchanged_at_beg_vpos to the end of
17649 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17650 set_terminal_window (f, end);
17651
17652 /* Delete dvpos lines in front of
17653 last_unchanged_at_beg_vpos. ins_del_lines will set
17654 the cursor to the given vpos and emit |dvpos| delete
17655 line sequences. */
17656 ins_del_lines (f, from + dvpos, dvpos);
17657
17658 /* On a dumb terminal insert dvpos empty lines at the
17659 end. */
17660 if (!FRAME_SCROLL_REGION_OK (f))
17661 ins_del_lines (f, end + dvpos, -dvpos);
17662 }
17663
17664 set_terminal_window (f, 0);
17665 }
17666
17667 update_end (f);
17668 }
17669
17670 /* Shift reused rows of the current matrix to the right position.
17671 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17672 text. */
17673 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17674 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17675 if (dvpos < 0)
17676 {
17677 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17678 bottom_vpos, dvpos);
17679 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17680 bottom_vpos);
17681 }
17682 else if (dvpos > 0)
17683 {
17684 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17685 bottom_vpos, dvpos);
17686 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17687 first_unchanged_at_end_vpos + dvpos);
17688 }
17689
17690 /* For frame-based redisplay, make sure that current frame and window
17691 matrix are in sync with respect to glyph memory. */
17692 if (!FRAME_WINDOW_P (f))
17693 sync_frame_with_window_matrix_rows (w);
17694
17695 /* Adjust buffer positions in reused rows. */
17696 if (delta || delta_bytes)
17697 increment_matrix_positions (current_matrix,
17698 first_unchanged_at_end_vpos + dvpos,
17699 bottom_vpos, delta, delta_bytes);
17700
17701 /* Adjust Y positions. */
17702 if (dy)
17703 shift_glyph_matrix (w, current_matrix,
17704 first_unchanged_at_end_vpos + dvpos,
17705 bottom_vpos, dy);
17706
17707 if (first_unchanged_at_end_row)
17708 {
17709 first_unchanged_at_end_row += dvpos;
17710 if (first_unchanged_at_end_row->y >= it.last_visible_y
17711 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17712 first_unchanged_at_end_row = NULL;
17713 }
17714
17715 /* If scrolling up, there may be some lines to display at the end of
17716 the window. */
17717 last_text_row_at_end = NULL;
17718 if (dy < 0)
17719 {
17720 /* Scrolling up can leave for example a partially visible line
17721 at the end of the window to be redisplayed. */
17722 /* Set last_row to the glyph row in the current matrix where the
17723 window end line is found. It has been moved up or down in
17724 the matrix by dvpos. */
17725 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17726 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17727
17728 /* If last_row is the window end line, it should display text. */
17729 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17730
17731 /* If window end line was partially visible before, begin
17732 displaying at that line. Otherwise begin displaying with the
17733 line following it. */
17734 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17735 {
17736 init_to_row_start (&it, w, last_row);
17737 it.vpos = last_vpos;
17738 it.current_y = last_row->y;
17739 }
17740 else
17741 {
17742 init_to_row_end (&it, w, last_row);
17743 it.vpos = 1 + last_vpos;
17744 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17745 ++last_row;
17746 }
17747
17748 /* We may start in a continuation line. If so, we have to
17749 get the right continuation_lines_width and current_x. */
17750 it.continuation_lines_width = last_row->continuation_lines_width;
17751 it.hpos = it.current_x = 0;
17752
17753 /* Display the rest of the lines at the window end. */
17754 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17755 while (it.current_y < it.last_visible_y
17756 && !fonts_changed_p)
17757 {
17758 /* Is it always sure that the display agrees with lines in
17759 the current matrix? I don't think so, so we mark rows
17760 displayed invalid in the current matrix by setting their
17761 enabled_p flag to zero. */
17762 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17763 if (display_line (&it))
17764 last_text_row_at_end = it.glyph_row - 1;
17765 }
17766 }
17767
17768 /* Update window_end_pos and window_end_vpos. */
17769 if (first_unchanged_at_end_row
17770 && !last_text_row_at_end)
17771 {
17772 /* Window end line if one of the preserved rows from the current
17773 matrix. Set row to the last row displaying text in current
17774 matrix starting at first_unchanged_at_end_row, after
17775 scrolling. */
17776 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17777 row = find_last_row_displaying_text (w->current_matrix, &it,
17778 first_unchanged_at_end_row);
17779 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17780
17781 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17782 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17783 wset_window_end_vpos
17784 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17785 eassert (w->window_end_bytepos >= 0);
17786 IF_DEBUG (debug_method_add (w, "A"));
17787 }
17788 else if (last_text_row_at_end)
17789 {
17790 wset_window_end_pos
17791 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17792 w->window_end_bytepos
17793 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17794 wset_window_end_vpos
17795 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17796 desired_matrix)));
17797 eassert (w->window_end_bytepos >= 0);
17798 IF_DEBUG (debug_method_add (w, "B"));
17799 }
17800 else if (last_text_row)
17801 {
17802 /* We have displayed either to the end of the window or at the
17803 end of the window, i.e. the last row with text is to be found
17804 in the desired matrix. */
17805 wset_window_end_pos
17806 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17807 w->window_end_bytepos
17808 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17809 wset_window_end_vpos
17810 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17811 eassert (w->window_end_bytepos >= 0);
17812 }
17813 else if (first_unchanged_at_end_row == NULL
17814 && last_text_row == NULL
17815 && last_text_row_at_end == NULL)
17816 {
17817 /* Displayed to end of window, but no line containing text was
17818 displayed. Lines were deleted at the end of the window. */
17819 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17820 int vpos = XFASTINT (w->window_end_vpos);
17821 struct glyph_row *current_row = current_matrix->rows + vpos;
17822 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17823
17824 for (row = NULL;
17825 row == NULL && vpos >= first_vpos;
17826 --vpos, --current_row, --desired_row)
17827 {
17828 if (desired_row->enabled_p)
17829 {
17830 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17831 row = desired_row;
17832 }
17833 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17834 row = current_row;
17835 }
17836
17837 eassert (row != NULL);
17838 wset_window_end_vpos (w, make_number (vpos + 1));
17839 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17840 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17841 eassert (w->window_end_bytepos >= 0);
17842 IF_DEBUG (debug_method_add (w, "C"));
17843 }
17844 else
17845 emacs_abort ();
17846
17847 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17848 debug_end_vpos = XFASTINT (w->window_end_vpos));
17849
17850 /* Record that display has not been completed. */
17851 w->window_end_valid = 0;
17852 w->desired_matrix->no_scrolling_p = 1;
17853 return 3;
17854
17855 #undef GIVE_UP
17856 }
17857
17858
17859 \f
17860 /***********************************************************************
17861 More debugging support
17862 ***********************************************************************/
17863
17864 #ifdef GLYPH_DEBUG
17865
17866 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17867 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17868 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17869
17870
17871 /* Dump the contents of glyph matrix MATRIX on stderr.
17872
17873 GLYPHS 0 means don't show glyph contents.
17874 GLYPHS 1 means show glyphs in short form
17875 GLYPHS > 1 means show glyphs in long form. */
17876
17877 void
17878 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17879 {
17880 int i;
17881 for (i = 0; i < matrix->nrows; ++i)
17882 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17883 }
17884
17885
17886 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17887 the glyph row and area where the glyph comes from. */
17888
17889 void
17890 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17891 {
17892 if (glyph->type == CHAR_GLYPH
17893 || glyph->type == GLYPHLESS_GLYPH)
17894 {
17895 fprintf (stderr,
17896 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17897 glyph - row->glyphs[TEXT_AREA],
17898 (glyph->type == CHAR_GLYPH
17899 ? 'C'
17900 : 'G'),
17901 glyph->charpos,
17902 (BUFFERP (glyph->object)
17903 ? 'B'
17904 : (STRINGP (glyph->object)
17905 ? 'S'
17906 : (INTEGERP (glyph->object)
17907 ? '0'
17908 : '-'))),
17909 glyph->pixel_width,
17910 glyph->u.ch,
17911 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17912 ? glyph->u.ch
17913 : '.'),
17914 glyph->face_id,
17915 glyph->left_box_line_p,
17916 glyph->right_box_line_p);
17917 }
17918 else if (glyph->type == STRETCH_GLYPH)
17919 {
17920 fprintf (stderr,
17921 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17922 glyph - row->glyphs[TEXT_AREA],
17923 'S',
17924 glyph->charpos,
17925 (BUFFERP (glyph->object)
17926 ? 'B'
17927 : (STRINGP (glyph->object)
17928 ? 'S'
17929 : (INTEGERP (glyph->object)
17930 ? '0'
17931 : '-'))),
17932 glyph->pixel_width,
17933 0,
17934 ' ',
17935 glyph->face_id,
17936 glyph->left_box_line_p,
17937 glyph->right_box_line_p);
17938 }
17939 else if (glyph->type == IMAGE_GLYPH)
17940 {
17941 fprintf (stderr,
17942 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17943 glyph - row->glyphs[TEXT_AREA],
17944 'I',
17945 glyph->charpos,
17946 (BUFFERP (glyph->object)
17947 ? 'B'
17948 : (STRINGP (glyph->object)
17949 ? 'S'
17950 : (INTEGERP (glyph->object)
17951 ? '0'
17952 : '-'))),
17953 glyph->pixel_width,
17954 glyph->u.img_id,
17955 '.',
17956 glyph->face_id,
17957 glyph->left_box_line_p,
17958 glyph->right_box_line_p);
17959 }
17960 else if (glyph->type == COMPOSITE_GLYPH)
17961 {
17962 fprintf (stderr,
17963 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17964 glyph - row->glyphs[TEXT_AREA],
17965 '+',
17966 glyph->charpos,
17967 (BUFFERP (glyph->object)
17968 ? 'B'
17969 : (STRINGP (glyph->object)
17970 ? 'S'
17971 : (INTEGERP (glyph->object)
17972 ? '0'
17973 : '-'))),
17974 glyph->pixel_width,
17975 glyph->u.cmp.id);
17976 if (glyph->u.cmp.automatic)
17977 fprintf (stderr,
17978 "[%d-%d]",
17979 glyph->slice.cmp.from, glyph->slice.cmp.to);
17980 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17981 glyph->face_id,
17982 glyph->left_box_line_p,
17983 glyph->right_box_line_p);
17984 }
17985 }
17986
17987
17988 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17989 GLYPHS 0 means don't show glyph contents.
17990 GLYPHS 1 means show glyphs in short form
17991 GLYPHS > 1 means show glyphs in long form. */
17992
17993 void
17994 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17995 {
17996 if (glyphs != 1)
17997 {
17998 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17999 fprintf (stderr, "==============================================================================\n");
18000
18001 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18002 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18003 vpos,
18004 MATRIX_ROW_START_CHARPOS (row),
18005 MATRIX_ROW_END_CHARPOS (row),
18006 row->used[TEXT_AREA],
18007 row->contains_overlapping_glyphs_p,
18008 row->enabled_p,
18009 row->truncated_on_left_p,
18010 row->truncated_on_right_p,
18011 row->continued_p,
18012 MATRIX_ROW_CONTINUATION_LINE_P (row),
18013 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18014 row->ends_at_zv_p,
18015 row->fill_line_p,
18016 row->ends_in_middle_of_char_p,
18017 row->starts_in_middle_of_char_p,
18018 row->mouse_face_p,
18019 row->x,
18020 row->y,
18021 row->pixel_width,
18022 row->height,
18023 row->visible_height,
18024 row->ascent,
18025 row->phys_ascent);
18026 /* The next 3 lines should align to "Start" in the header. */
18027 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18028 row->end.overlay_string_index,
18029 row->continuation_lines_width);
18030 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18031 CHARPOS (row->start.string_pos),
18032 CHARPOS (row->end.string_pos));
18033 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18034 row->end.dpvec_index);
18035 }
18036
18037 if (glyphs > 1)
18038 {
18039 int area;
18040
18041 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18042 {
18043 struct glyph *glyph = row->glyphs[area];
18044 struct glyph *glyph_end = glyph + row->used[area];
18045
18046 /* Glyph for a line end in text. */
18047 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18048 ++glyph_end;
18049
18050 if (glyph < glyph_end)
18051 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18052
18053 for (; glyph < glyph_end; ++glyph)
18054 dump_glyph (row, glyph, area);
18055 }
18056 }
18057 else if (glyphs == 1)
18058 {
18059 int area;
18060
18061 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18062 {
18063 char *s = alloca (row->used[area] + 4);
18064 int i;
18065
18066 for (i = 0; i < row->used[area]; ++i)
18067 {
18068 struct glyph *glyph = row->glyphs[area] + i;
18069 if (i == row->used[area] - 1
18070 && area == TEXT_AREA
18071 && INTEGERP (glyph->object)
18072 && glyph->type == CHAR_GLYPH
18073 && glyph->u.ch == ' ')
18074 {
18075 strcpy (&s[i], "[\\n]");
18076 i += 4;
18077 }
18078 else if (glyph->type == CHAR_GLYPH
18079 && glyph->u.ch < 0x80
18080 && glyph->u.ch >= ' ')
18081 s[i] = glyph->u.ch;
18082 else
18083 s[i] = '.';
18084 }
18085
18086 s[i] = '\0';
18087 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18088 }
18089 }
18090 }
18091
18092
18093 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18094 Sdump_glyph_matrix, 0, 1, "p",
18095 doc: /* Dump the current matrix of the selected window to stderr.
18096 Shows contents of glyph row structures. With non-nil
18097 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18098 glyphs in short form, otherwise show glyphs in long form. */)
18099 (Lisp_Object glyphs)
18100 {
18101 struct window *w = XWINDOW (selected_window);
18102 struct buffer *buffer = XBUFFER (w->contents);
18103
18104 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18105 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18106 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18107 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18108 fprintf (stderr, "=============================================\n");
18109 dump_glyph_matrix (w->current_matrix,
18110 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18111 return Qnil;
18112 }
18113
18114
18115 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18116 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18117 (void)
18118 {
18119 struct frame *f = XFRAME (selected_frame);
18120 dump_glyph_matrix (f->current_matrix, 1);
18121 return Qnil;
18122 }
18123
18124
18125 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18126 doc: /* Dump glyph row ROW to stderr.
18127 GLYPH 0 means don't dump glyphs.
18128 GLYPH 1 means dump glyphs in short form.
18129 GLYPH > 1 or omitted means dump glyphs in long form. */)
18130 (Lisp_Object row, Lisp_Object glyphs)
18131 {
18132 struct glyph_matrix *matrix;
18133 EMACS_INT vpos;
18134
18135 CHECK_NUMBER (row);
18136 matrix = XWINDOW (selected_window)->current_matrix;
18137 vpos = XINT (row);
18138 if (vpos >= 0 && vpos < matrix->nrows)
18139 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18140 vpos,
18141 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18142 return Qnil;
18143 }
18144
18145
18146 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18147 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18148 GLYPH 0 means don't dump glyphs.
18149 GLYPH 1 means dump glyphs in short form.
18150 GLYPH > 1 or omitted means dump glyphs in long form. */)
18151 (Lisp_Object row, Lisp_Object glyphs)
18152 {
18153 struct frame *sf = SELECTED_FRAME ();
18154 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18155 EMACS_INT vpos;
18156
18157 CHECK_NUMBER (row);
18158 vpos = XINT (row);
18159 if (vpos >= 0 && vpos < m->nrows)
18160 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18161 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18162 return Qnil;
18163 }
18164
18165
18166 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18167 doc: /* Toggle tracing of redisplay.
18168 With ARG, turn tracing on if and only if ARG is positive. */)
18169 (Lisp_Object arg)
18170 {
18171 if (NILP (arg))
18172 trace_redisplay_p = !trace_redisplay_p;
18173 else
18174 {
18175 arg = Fprefix_numeric_value (arg);
18176 trace_redisplay_p = XINT (arg) > 0;
18177 }
18178
18179 return Qnil;
18180 }
18181
18182
18183 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18184 doc: /* Like `format', but print result to stderr.
18185 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18186 (ptrdiff_t nargs, Lisp_Object *args)
18187 {
18188 Lisp_Object s = Fformat (nargs, args);
18189 fprintf (stderr, "%s", SDATA (s));
18190 return Qnil;
18191 }
18192
18193 #endif /* GLYPH_DEBUG */
18194
18195
18196 \f
18197 /***********************************************************************
18198 Building Desired Matrix Rows
18199 ***********************************************************************/
18200
18201 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18202 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18203
18204 static struct glyph_row *
18205 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18206 {
18207 struct frame *f = XFRAME (WINDOW_FRAME (w));
18208 struct buffer *buffer = XBUFFER (w->contents);
18209 struct buffer *old = current_buffer;
18210 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18211 int arrow_len = SCHARS (overlay_arrow_string);
18212 const unsigned char *arrow_end = arrow_string + arrow_len;
18213 const unsigned char *p;
18214 struct it it;
18215 bool multibyte_p;
18216 int n_glyphs_before;
18217
18218 set_buffer_temp (buffer);
18219 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18220 it.glyph_row->used[TEXT_AREA] = 0;
18221 SET_TEXT_POS (it.position, 0, 0);
18222
18223 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18224 p = arrow_string;
18225 while (p < arrow_end)
18226 {
18227 Lisp_Object face, ilisp;
18228
18229 /* Get the next character. */
18230 if (multibyte_p)
18231 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18232 else
18233 {
18234 it.c = it.char_to_display = *p, it.len = 1;
18235 if (! ASCII_CHAR_P (it.c))
18236 it.char_to_display = BYTE8_TO_CHAR (it.c);
18237 }
18238 p += it.len;
18239
18240 /* Get its face. */
18241 ilisp = make_number (p - arrow_string);
18242 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18243 it.face_id = compute_char_face (f, it.char_to_display, face);
18244
18245 /* Compute its width, get its glyphs. */
18246 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18247 SET_TEXT_POS (it.position, -1, -1);
18248 PRODUCE_GLYPHS (&it);
18249
18250 /* If this character doesn't fit any more in the line, we have
18251 to remove some glyphs. */
18252 if (it.current_x > it.last_visible_x)
18253 {
18254 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18255 break;
18256 }
18257 }
18258
18259 set_buffer_temp (old);
18260 return it.glyph_row;
18261 }
18262
18263
18264 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18265 glyphs to insert is determined by produce_special_glyphs. */
18266
18267 static void
18268 insert_left_trunc_glyphs (struct it *it)
18269 {
18270 struct it truncate_it;
18271 struct glyph *from, *end, *to, *toend;
18272
18273 eassert (!FRAME_WINDOW_P (it->f)
18274 || (!it->glyph_row->reversed_p
18275 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18276 || (it->glyph_row->reversed_p
18277 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18278
18279 /* Get the truncation glyphs. */
18280 truncate_it = *it;
18281 truncate_it.current_x = 0;
18282 truncate_it.face_id = DEFAULT_FACE_ID;
18283 truncate_it.glyph_row = &scratch_glyph_row;
18284 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18285 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18286 truncate_it.object = make_number (0);
18287 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18288
18289 /* Overwrite glyphs from IT with truncation glyphs. */
18290 if (!it->glyph_row->reversed_p)
18291 {
18292 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18293
18294 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18295 end = from + tused;
18296 to = it->glyph_row->glyphs[TEXT_AREA];
18297 toend = to + it->glyph_row->used[TEXT_AREA];
18298 if (FRAME_WINDOW_P (it->f))
18299 {
18300 /* On GUI frames, when variable-size fonts are displayed,
18301 the truncation glyphs may need more pixels than the row's
18302 glyphs they overwrite. We overwrite more glyphs to free
18303 enough screen real estate, and enlarge the stretch glyph
18304 on the right (see display_line), if there is one, to
18305 preserve the screen position of the truncation glyphs on
18306 the right. */
18307 int w = 0;
18308 struct glyph *g = to;
18309 short used;
18310
18311 /* The first glyph could be partially visible, in which case
18312 it->glyph_row->x will be negative. But we want the left
18313 truncation glyphs to be aligned at the left margin of the
18314 window, so we override the x coordinate at which the row
18315 will begin. */
18316 it->glyph_row->x = 0;
18317 while (g < toend && w < it->truncation_pixel_width)
18318 {
18319 w += g->pixel_width;
18320 ++g;
18321 }
18322 if (g - to - tused > 0)
18323 {
18324 memmove (to + tused, g, (toend - g) * sizeof(*g));
18325 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18326 }
18327 used = it->glyph_row->used[TEXT_AREA];
18328 if (it->glyph_row->truncated_on_right_p
18329 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18330 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18331 == STRETCH_GLYPH)
18332 {
18333 int extra = w - it->truncation_pixel_width;
18334
18335 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18336 }
18337 }
18338
18339 while (from < end)
18340 *to++ = *from++;
18341
18342 /* There may be padding glyphs left over. Overwrite them too. */
18343 if (!FRAME_WINDOW_P (it->f))
18344 {
18345 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18346 {
18347 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18348 while (from < end)
18349 *to++ = *from++;
18350 }
18351 }
18352
18353 if (to > toend)
18354 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18355 }
18356 else
18357 {
18358 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18359
18360 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18361 that back to front. */
18362 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18363 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18364 toend = it->glyph_row->glyphs[TEXT_AREA];
18365 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18366 if (FRAME_WINDOW_P (it->f))
18367 {
18368 int w = 0;
18369 struct glyph *g = to;
18370
18371 while (g >= toend && w < it->truncation_pixel_width)
18372 {
18373 w += g->pixel_width;
18374 --g;
18375 }
18376 if (to - g - tused > 0)
18377 to = g + tused;
18378 if (it->glyph_row->truncated_on_right_p
18379 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18380 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18381 {
18382 int extra = w - it->truncation_pixel_width;
18383
18384 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18385 }
18386 }
18387
18388 while (from >= end && to >= toend)
18389 *to-- = *from--;
18390 if (!FRAME_WINDOW_P (it->f))
18391 {
18392 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18393 {
18394 from =
18395 truncate_it.glyph_row->glyphs[TEXT_AREA]
18396 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18397 while (from >= end && to >= toend)
18398 *to-- = *from--;
18399 }
18400 }
18401 if (from >= end)
18402 {
18403 /* Need to free some room before prepending additional
18404 glyphs. */
18405 int move_by = from - end + 1;
18406 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18407 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18408
18409 for ( ; g >= g0; g--)
18410 g[move_by] = *g;
18411 while (from >= end)
18412 *to-- = *from--;
18413 it->glyph_row->used[TEXT_AREA] += move_by;
18414 }
18415 }
18416 }
18417
18418 /* Compute the hash code for ROW. */
18419 unsigned
18420 row_hash (struct glyph_row *row)
18421 {
18422 int area, k;
18423 unsigned hashval = 0;
18424
18425 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18426 for (k = 0; k < row->used[area]; ++k)
18427 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18428 + row->glyphs[area][k].u.val
18429 + row->glyphs[area][k].face_id
18430 + row->glyphs[area][k].padding_p
18431 + (row->glyphs[area][k].type << 2));
18432
18433 return hashval;
18434 }
18435
18436 /* Compute the pixel height and width of IT->glyph_row.
18437
18438 Most of the time, ascent and height of a display line will be equal
18439 to the max_ascent and max_height values of the display iterator
18440 structure. This is not the case if
18441
18442 1. We hit ZV without displaying anything. In this case, max_ascent
18443 and max_height will be zero.
18444
18445 2. We have some glyphs that don't contribute to the line height.
18446 (The glyph row flag contributes_to_line_height_p is for future
18447 pixmap extensions).
18448
18449 The first case is easily covered by using default values because in
18450 these cases, the line height does not really matter, except that it
18451 must not be zero. */
18452
18453 static void
18454 compute_line_metrics (struct it *it)
18455 {
18456 struct glyph_row *row = it->glyph_row;
18457
18458 if (FRAME_WINDOW_P (it->f))
18459 {
18460 int i, min_y, max_y;
18461
18462 /* The line may consist of one space only, that was added to
18463 place the cursor on it. If so, the row's height hasn't been
18464 computed yet. */
18465 if (row->height == 0)
18466 {
18467 if (it->max_ascent + it->max_descent == 0)
18468 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18469 row->ascent = it->max_ascent;
18470 row->height = it->max_ascent + it->max_descent;
18471 row->phys_ascent = it->max_phys_ascent;
18472 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18473 row->extra_line_spacing = it->max_extra_line_spacing;
18474 }
18475
18476 /* Compute the width of this line. */
18477 row->pixel_width = row->x;
18478 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18479 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18480
18481 eassert (row->pixel_width >= 0);
18482 eassert (row->ascent >= 0 && row->height > 0);
18483
18484 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18485 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18486
18487 /* If first line's physical ascent is larger than its logical
18488 ascent, use the physical ascent, and make the row taller.
18489 This makes accented characters fully visible. */
18490 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18491 && row->phys_ascent > row->ascent)
18492 {
18493 row->height += row->phys_ascent - row->ascent;
18494 row->ascent = row->phys_ascent;
18495 }
18496
18497 /* Compute how much of the line is visible. */
18498 row->visible_height = row->height;
18499
18500 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18501 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18502
18503 if (row->y < min_y)
18504 row->visible_height -= min_y - row->y;
18505 if (row->y + row->height > max_y)
18506 row->visible_height -= row->y + row->height - max_y;
18507 }
18508 else
18509 {
18510 row->pixel_width = row->used[TEXT_AREA];
18511 if (row->continued_p)
18512 row->pixel_width -= it->continuation_pixel_width;
18513 else if (row->truncated_on_right_p)
18514 row->pixel_width -= it->truncation_pixel_width;
18515 row->ascent = row->phys_ascent = 0;
18516 row->height = row->phys_height = row->visible_height = 1;
18517 row->extra_line_spacing = 0;
18518 }
18519
18520 /* Compute a hash code for this row. */
18521 row->hash = row_hash (row);
18522
18523 it->max_ascent = it->max_descent = 0;
18524 it->max_phys_ascent = it->max_phys_descent = 0;
18525 }
18526
18527
18528 /* Append one space to the glyph row of iterator IT if doing a
18529 window-based redisplay. The space has the same face as
18530 IT->face_id. Value is non-zero if a space was added.
18531
18532 This function is called to make sure that there is always one glyph
18533 at the end of a glyph row that the cursor can be set on under
18534 window-systems. (If there weren't such a glyph we would not know
18535 how wide and tall a box cursor should be displayed).
18536
18537 At the same time this space let's a nicely handle clearing to the
18538 end of the line if the row ends in italic text. */
18539
18540 static int
18541 append_space_for_newline (struct it *it, int default_face_p)
18542 {
18543 if (FRAME_WINDOW_P (it->f))
18544 {
18545 int n = it->glyph_row->used[TEXT_AREA];
18546
18547 if (it->glyph_row->glyphs[TEXT_AREA] + n
18548 < it->glyph_row->glyphs[1 + TEXT_AREA])
18549 {
18550 /* Save some values that must not be changed.
18551 Must save IT->c and IT->len because otherwise
18552 ITERATOR_AT_END_P wouldn't work anymore after
18553 append_space_for_newline has been called. */
18554 enum display_element_type saved_what = it->what;
18555 int saved_c = it->c, saved_len = it->len;
18556 int saved_char_to_display = it->char_to_display;
18557 int saved_x = it->current_x;
18558 int saved_face_id = it->face_id;
18559 int saved_box_end = it->end_of_box_run_p;
18560 struct text_pos saved_pos;
18561 Lisp_Object saved_object;
18562 struct face *face;
18563
18564 saved_object = it->object;
18565 saved_pos = it->position;
18566
18567 it->what = IT_CHARACTER;
18568 memset (&it->position, 0, sizeof it->position);
18569 it->object = make_number (0);
18570 it->c = it->char_to_display = ' ';
18571 it->len = 1;
18572
18573 /* If the default face was remapped, be sure to use the
18574 remapped face for the appended newline. */
18575 if (default_face_p)
18576 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18577 else if (it->face_before_selective_p)
18578 it->face_id = it->saved_face_id;
18579 face = FACE_FROM_ID (it->f, it->face_id);
18580 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18581 /* In R2L rows, we will prepend a stretch glyph that will
18582 have the end_of_box_run_p flag set for it, so there's no
18583 need for the appended newline glyph to have that flag
18584 set. */
18585 if (it->glyph_row->reversed_p
18586 /* But if the appended newline glyph goes all the way to
18587 the end of the row, there will be no stretch glyph,
18588 so leave the box flag set. */
18589 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18590 it->end_of_box_run_p = 0;
18591
18592 PRODUCE_GLYPHS (it);
18593
18594 it->override_ascent = -1;
18595 it->constrain_row_ascent_descent_p = 0;
18596 it->current_x = saved_x;
18597 it->object = saved_object;
18598 it->position = saved_pos;
18599 it->what = saved_what;
18600 it->face_id = saved_face_id;
18601 it->len = saved_len;
18602 it->c = saved_c;
18603 it->char_to_display = saved_char_to_display;
18604 it->end_of_box_run_p = saved_box_end;
18605 return 1;
18606 }
18607 }
18608
18609 return 0;
18610 }
18611
18612
18613 /* Extend the face of the last glyph in the text area of IT->glyph_row
18614 to the end of the display line. Called from display_line. If the
18615 glyph row is empty, add a space glyph to it so that we know the
18616 face to draw. Set the glyph row flag fill_line_p. If the glyph
18617 row is R2L, prepend a stretch glyph to cover the empty space to the
18618 left of the leftmost glyph. */
18619
18620 static void
18621 extend_face_to_end_of_line (struct it *it)
18622 {
18623 struct face *face, *default_face;
18624 struct frame *f = it->f;
18625
18626 /* If line is already filled, do nothing. Non window-system frames
18627 get a grace of one more ``pixel'' because their characters are
18628 1-``pixel'' wide, so they hit the equality too early. This grace
18629 is needed only for R2L rows that are not continued, to produce
18630 one extra blank where we could display the cursor. */
18631 if (it->current_x >= it->last_visible_x
18632 + (!FRAME_WINDOW_P (f)
18633 && it->glyph_row->reversed_p
18634 && !it->glyph_row->continued_p))
18635 return;
18636
18637 /* The default face, possibly remapped. */
18638 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18639
18640 /* Face extension extends the background and box of IT->face_id
18641 to the end of the line. If the background equals the background
18642 of the frame, we don't have to do anything. */
18643 if (it->face_before_selective_p)
18644 face = FACE_FROM_ID (f, it->saved_face_id);
18645 else
18646 face = FACE_FROM_ID (f, it->face_id);
18647
18648 if (FRAME_WINDOW_P (f)
18649 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18650 && face->box == FACE_NO_BOX
18651 && face->background == FRAME_BACKGROUND_PIXEL (f)
18652 && !face->stipple
18653 && !it->glyph_row->reversed_p)
18654 return;
18655
18656 /* Set the glyph row flag indicating that the face of the last glyph
18657 in the text area has to be drawn to the end of the text area. */
18658 it->glyph_row->fill_line_p = 1;
18659
18660 /* If current character of IT is not ASCII, make sure we have the
18661 ASCII face. This will be automatically undone the next time
18662 get_next_display_element returns a multibyte character. Note
18663 that the character will always be single byte in unibyte
18664 text. */
18665 if (!ASCII_CHAR_P (it->c))
18666 {
18667 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18668 }
18669
18670 if (FRAME_WINDOW_P (f))
18671 {
18672 /* If the row is empty, add a space with the current face of IT,
18673 so that we know which face to draw. */
18674 if (it->glyph_row->used[TEXT_AREA] == 0)
18675 {
18676 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18677 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18678 it->glyph_row->used[TEXT_AREA] = 1;
18679 }
18680 #ifdef HAVE_WINDOW_SYSTEM
18681 if (it->glyph_row->reversed_p)
18682 {
18683 /* Prepend a stretch glyph to the row, such that the
18684 rightmost glyph will be drawn flushed all the way to the
18685 right margin of the window. The stretch glyph that will
18686 occupy the empty space, if any, to the left of the
18687 glyphs. */
18688 struct font *font = face->font ? face->font : FRAME_FONT (f);
18689 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18690 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18691 struct glyph *g;
18692 int row_width, stretch_ascent, stretch_width;
18693 struct text_pos saved_pos;
18694 int saved_face_id, saved_avoid_cursor, saved_box_start;
18695
18696 for (row_width = 0, g = row_start; g < row_end; g++)
18697 row_width += g->pixel_width;
18698 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18699 if (stretch_width > 0)
18700 {
18701 stretch_ascent =
18702 (((it->ascent + it->descent)
18703 * FONT_BASE (font)) / FONT_HEIGHT (font));
18704 saved_pos = it->position;
18705 memset (&it->position, 0, sizeof it->position);
18706 saved_avoid_cursor = it->avoid_cursor_p;
18707 it->avoid_cursor_p = 1;
18708 saved_face_id = it->face_id;
18709 saved_box_start = it->start_of_box_run_p;
18710 /* The last row's stretch glyph should get the default
18711 face, to avoid painting the rest of the window with
18712 the region face, if the region ends at ZV. */
18713 if (it->glyph_row->ends_at_zv_p)
18714 it->face_id = default_face->id;
18715 else
18716 it->face_id = face->id;
18717 it->start_of_box_run_p = 0;
18718 append_stretch_glyph (it, make_number (0), stretch_width,
18719 it->ascent + it->descent, stretch_ascent);
18720 it->position = saved_pos;
18721 it->avoid_cursor_p = saved_avoid_cursor;
18722 it->face_id = saved_face_id;
18723 it->start_of_box_run_p = saved_box_start;
18724 }
18725 }
18726 #endif /* HAVE_WINDOW_SYSTEM */
18727 }
18728 else
18729 {
18730 /* Save some values that must not be changed. */
18731 int saved_x = it->current_x;
18732 struct text_pos saved_pos;
18733 Lisp_Object saved_object;
18734 enum display_element_type saved_what = it->what;
18735 int saved_face_id = it->face_id;
18736
18737 saved_object = it->object;
18738 saved_pos = it->position;
18739
18740 it->what = IT_CHARACTER;
18741 memset (&it->position, 0, sizeof it->position);
18742 it->object = make_number (0);
18743 it->c = it->char_to_display = ' ';
18744 it->len = 1;
18745 /* The last row's blank glyphs should get the default face, to
18746 avoid painting the rest of the window with the region face,
18747 if the region ends at ZV. */
18748 if (it->glyph_row->ends_at_zv_p)
18749 it->face_id = default_face->id;
18750 else
18751 it->face_id = face->id;
18752
18753 PRODUCE_GLYPHS (it);
18754
18755 while (it->current_x <= it->last_visible_x)
18756 PRODUCE_GLYPHS (it);
18757
18758 /* Don't count these blanks really. It would let us insert a left
18759 truncation glyph below and make us set the cursor on them, maybe. */
18760 it->current_x = saved_x;
18761 it->object = saved_object;
18762 it->position = saved_pos;
18763 it->what = saved_what;
18764 it->face_id = saved_face_id;
18765 }
18766 }
18767
18768
18769 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18770 trailing whitespace. */
18771
18772 static int
18773 trailing_whitespace_p (ptrdiff_t charpos)
18774 {
18775 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18776 int c = 0;
18777
18778 while (bytepos < ZV_BYTE
18779 && (c = FETCH_CHAR (bytepos),
18780 c == ' ' || c == '\t'))
18781 ++bytepos;
18782
18783 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18784 {
18785 if (bytepos != PT_BYTE)
18786 return 1;
18787 }
18788 return 0;
18789 }
18790
18791
18792 /* Highlight trailing whitespace, if any, in ROW. */
18793
18794 static void
18795 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18796 {
18797 int used = row->used[TEXT_AREA];
18798
18799 if (used)
18800 {
18801 struct glyph *start = row->glyphs[TEXT_AREA];
18802 struct glyph *glyph = start + used - 1;
18803
18804 if (row->reversed_p)
18805 {
18806 /* Right-to-left rows need to be processed in the opposite
18807 direction, so swap the edge pointers. */
18808 glyph = start;
18809 start = row->glyphs[TEXT_AREA] + used - 1;
18810 }
18811
18812 /* Skip over glyphs inserted to display the cursor at the
18813 end of a line, for extending the face of the last glyph
18814 to the end of the line on terminals, and for truncation
18815 and continuation glyphs. */
18816 if (!row->reversed_p)
18817 {
18818 while (glyph >= start
18819 && glyph->type == CHAR_GLYPH
18820 && INTEGERP (glyph->object))
18821 --glyph;
18822 }
18823 else
18824 {
18825 while (glyph <= start
18826 && glyph->type == CHAR_GLYPH
18827 && INTEGERP (glyph->object))
18828 ++glyph;
18829 }
18830
18831 /* If last glyph is a space or stretch, and it's trailing
18832 whitespace, set the face of all trailing whitespace glyphs in
18833 IT->glyph_row to `trailing-whitespace'. */
18834 if ((row->reversed_p ? glyph <= start : glyph >= start)
18835 && BUFFERP (glyph->object)
18836 && (glyph->type == STRETCH_GLYPH
18837 || (glyph->type == CHAR_GLYPH
18838 && glyph->u.ch == ' '))
18839 && trailing_whitespace_p (glyph->charpos))
18840 {
18841 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18842 if (face_id < 0)
18843 return;
18844
18845 if (!row->reversed_p)
18846 {
18847 while (glyph >= start
18848 && BUFFERP (glyph->object)
18849 && (glyph->type == STRETCH_GLYPH
18850 || (glyph->type == CHAR_GLYPH
18851 && glyph->u.ch == ' ')))
18852 (glyph--)->face_id = face_id;
18853 }
18854 else
18855 {
18856 while (glyph <= start
18857 && BUFFERP (glyph->object)
18858 && (glyph->type == STRETCH_GLYPH
18859 || (glyph->type == CHAR_GLYPH
18860 && glyph->u.ch == ' ')))
18861 (glyph++)->face_id = face_id;
18862 }
18863 }
18864 }
18865 }
18866
18867
18868 /* Value is non-zero if glyph row ROW should be
18869 considered to hold the buffer position CHARPOS. */
18870
18871 static int
18872 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18873 {
18874 int result = 1;
18875
18876 if (charpos == CHARPOS (row->end.pos)
18877 || charpos == MATRIX_ROW_END_CHARPOS (row))
18878 {
18879 /* Suppose the row ends on a string.
18880 Unless the row is continued, that means it ends on a newline
18881 in the string. If it's anything other than a display string
18882 (e.g., a before-string from an overlay), we don't want the
18883 cursor there. (This heuristic seems to give the optimal
18884 behavior for the various types of multi-line strings.)
18885 One exception: if the string has `cursor' property on one of
18886 its characters, we _do_ want the cursor there. */
18887 if (CHARPOS (row->end.string_pos) >= 0)
18888 {
18889 if (row->continued_p)
18890 result = 1;
18891 else
18892 {
18893 /* Check for `display' property. */
18894 struct glyph *beg = row->glyphs[TEXT_AREA];
18895 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18896 struct glyph *glyph;
18897
18898 result = 0;
18899 for (glyph = end; glyph >= beg; --glyph)
18900 if (STRINGP (glyph->object))
18901 {
18902 Lisp_Object prop
18903 = Fget_char_property (make_number (charpos),
18904 Qdisplay, Qnil);
18905 result =
18906 (!NILP (prop)
18907 && display_prop_string_p (prop, glyph->object));
18908 /* If there's a `cursor' property on one of the
18909 string's characters, this row is a cursor row,
18910 even though this is not a display string. */
18911 if (!result)
18912 {
18913 Lisp_Object s = glyph->object;
18914
18915 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18916 {
18917 ptrdiff_t gpos = glyph->charpos;
18918
18919 if (!NILP (Fget_char_property (make_number (gpos),
18920 Qcursor, s)))
18921 {
18922 result = 1;
18923 break;
18924 }
18925 }
18926 }
18927 break;
18928 }
18929 }
18930 }
18931 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18932 {
18933 /* If the row ends in middle of a real character,
18934 and the line is continued, we want the cursor here.
18935 That's because CHARPOS (ROW->end.pos) would equal
18936 PT if PT is before the character. */
18937 if (!row->ends_in_ellipsis_p)
18938 result = row->continued_p;
18939 else
18940 /* If the row ends in an ellipsis, then
18941 CHARPOS (ROW->end.pos) will equal point after the
18942 invisible text. We want that position to be displayed
18943 after the ellipsis. */
18944 result = 0;
18945 }
18946 /* If the row ends at ZV, display the cursor at the end of that
18947 row instead of at the start of the row below. */
18948 else if (row->ends_at_zv_p)
18949 result = 1;
18950 else
18951 result = 0;
18952 }
18953
18954 return result;
18955 }
18956
18957 /* Value is non-zero if glyph row ROW should be
18958 used to hold the cursor. */
18959
18960 static int
18961 cursor_row_p (struct glyph_row *row)
18962 {
18963 return row_for_charpos_p (row, PT);
18964 }
18965
18966 \f
18967
18968 /* Push the property PROP so that it will be rendered at the current
18969 position in IT. Return 1 if PROP was successfully pushed, 0
18970 otherwise. Called from handle_line_prefix to handle the
18971 `line-prefix' and `wrap-prefix' properties. */
18972
18973 static int
18974 push_prefix_prop (struct it *it, Lisp_Object prop)
18975 {
18976 struct text_pos pos =
18977 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18978
18979 eassert (it->method == GET_FROM_BUFFER
18980 || it->method == GET_FROM_DISPLAY_VECTOR
18981 || it->method == GET_FROM_STRING);
18982
18983 /* We need to save the current buffer/string position, so it will be
18984 restored by pop_it, because iterate_out_of_display_property
18985 depends on that being set correctly, but some situations leave
18986 it->position not yet set when this function is called. */
18987 push_it (it, &pos);
18988
18989 if (STRINGP (prop))
18990 {
18991 if (SCHARS (prop) == 0)
18992 {
18993 pop_it (it);
18994 return 0;
18995 }
18996
18997 it->string = prop;
18998 it->string_from_prefix_prop_p = 1;
18999 it->multibyte_p = STRING_MULTIBYTE (it->string);
19000 it->current.overlay_string_index = -1;
19001 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19002 it->end_charpos = it->string_nchars = SCHARS (it->string);
19003 it->method = GET_FROM_STRING;
19004 it->stop_charpos = 0;
19005 it->prev_stop = 0;
19006 it->base_level_stop = 0;
19007
19008 /* Force paragraph direction to be that of the parent
19009 buffer/string. */
19010 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19011 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19012 else
19013 it->paragraph_embedding = L2R;
19014
19015 /* Set up the bidi iterator for this display string. */
19016 if (it->bidi_p)
19017 {
19018 it->bidi_it.string.lstring = it->string;
19019 it->bidi_it.string.s = NULL;
19020 it->bidi_it.string.schars = it->end_charpos;
19021 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19022 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19023 it->bidi_it.string.unibyte = !it->multibyte_p;
19024 it->bidi_it.w = it->w;
19025 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19026 }
19027 }
19028 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19029 {
19030 it->method = GET_FROM_STRETCH;
19031 it->object = prop;
19032 }
19033 #ifdef HAVE_WINDOW_SYSTEM
19034 else if (IMAGEP (prop))
19035 {
19036 it->what = IT_IMAGE;
19037 it->image_id = lookup_image (it->f, prop);
19038 it->method = GET_FROM_IMAGE;
19039 }
19040 #endif /* HAVE_WINDOW_SYSTEM */
19041 else
19042 {
19043 pop_it (it); /* bogus display property, give up */
19044 return 0;
19045 }
19046
19047 return 1;
19048 }
19049
19050 /* Return the character-property PROP at the current position in IT. */
19051
19052 static Lisp_Object
19053 get_it_property (struct it *it, Lisp_Object prop)
19054 {
19055 Lisp_Object position, object = it->object;
19056
19057 if (STRINGP (object))
19058 position = make_number (IT_STRING_CHARPOS (*it));
19059 else if (BUFFERP (object))
19060 {
19061 position = make_number (IT_CHARPOS (*it));
19062 object = it->window;
19063 }
19064 else
19065 return Qnil;
19066
19067 return Fget_char_property (position, prop, object);
19068 }
19069
19070 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19071
19072 static void
19073 handle_line_prefix (struct it *it)
19074 {
19075 Lisp_Object prefix;
19076
19077 if (it->continuation_lines_width > 0)
19078 {
19079 prefix = get_it_property (it, Qwrap_prefix);
19080 if (NILP (prefix))
19081 prefix = Vwrap_prefix;
19082 }
19083 else
19084 {
19085 prefix = get_it_property (it, Qline_prefix);
19086 if (NILP (prefix))
19087 prefix = Vline_prefix;
19088 }
19089 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19090 {
19091 /* If the prefix is wider than the window, and we try to wrap
19092 it, it would acquire its own wrap prefix, and so on till the
19093 iterator stack overflows. So, don't wrap the prefix. */
19094 it->line_wrap = TRUNCATE;
19095 it->avoid_cursor_p = 1;
19096 }
19097 }
19098
19099 \f
19100
19101 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19102 only for R2L lines from display_line and display_string, when they
19103 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19104 the line/string needs to be continued on the next glyph row. */
19105 static void
19106 unproduce_glyphs (struct it *it, int n)
19107 {
19108 struct glyph *glyph, *end;
19109
19110 eassert (it->glyph_row);
19111 eassert (it->glyph_row->reversed_p);
19112 eassert (it->area == TEXT_AREA);
19113 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19114
19115 if (n > it->glyph_row->used[TEXT_AREA])
19116 n = it->glyph_row->used[TEXT_AREA];
19117 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19118 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19119 for ( ; glyph < end; glyph++)
19120 glyph[-n] = *glyph;
19121 }
19122
19123 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19124 and ROW->maxpos. */
19125 static void
19126 find_row_edges (struct it *it, struct glyph_row *row,
19127 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19128 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19129 {
19130 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19131 lines' rows is implemented for bidi-reordered rows. */
19132
19133 /* ROW->minpos is the value of min_pos, the minimal buffer position
19134 we have in ROW, or ROW->start.pos if that is smaller. */
19135 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19136 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19137 else
19138 /* We didn't find buffer positions smaller than ROW->start, or
19139 didn't find _any_ valid buffer positions in any of the glyphs,
19140 so we must trust the iterator's computed positions. */
19141 row->minpos = row->start.pos;
19142 if (max_pos <= 0)
19143 {
19144 max_pos = CHARPOS (it->current.pos);
19145 max_bpos = BYTEPOS (it->current.pos);
19146 }
19147
19148 /* Here are the various use-cases for ending the row, and the
19149 corresponding values for ROW->maxpos:
19150
19151 Line ends in a newline from buffer eol_pos + 1
19152 Line is continued from buffer max_pos + 1
19153 Line is truncated on right it->current.pos
19154 Line ends in a newline from string max_pos + 1(*)
19155 (*) + 1 only when line ends in a forward scan
19156 Line is continued from string max_pos
19157 Line is continued from display vector max_pos
19158 Line is entirely from a string min_pos == max_pos
19159 Line is entirely from a display vector min_pos == max_pos
19160 Line that ends at ZV ZV
19161
19162 If you discover other use-cases, please add them here as
19163 appropriate. */
19164 if (row->ends_at_zv_p)
19165 row->maxpos = it->current.pos;
19166 else if (row->used[TEXT_AREA])
19167 {
19168 int seen_this_string = 0;
19169 struct glyph_row *r1 = row - 1;
19170
19171 /* Did we see the same display string on the previous row? */
19172 if (STRINGP (it->object)
19173 /* this is not the first row */
19174 && row > it->w->desired_matrix->rows
19175 /* previous row is not the header line */
19176 && !r1->mode_line_p
19177 /* previous row also ends in a newline from a string */
19178 && r1->ends_in_newline_from_string_p)
19179 {
19180 struct glyph *start, *end;
19181
19182 /* Search for the last glyph of the previous row that came
19183 from buffer or string. Depending on whether the row is
19184 L2R or R2L, we need to process it front to back or the
19185 other way round. */
19186 if (!r1->reversed_p)
19187 {
19188 start = r1->glyphs[TEXT_AREA];
19189 end = start + r1->used[TEXT_AREA];
19190 /* Glyphs inserted by redisplay have an integer (zero)
19191 as their object. */
19192 while (end > start
19193 && INTEGERP ((end - 1)->object)
19194 && (end - 1)->charpos <= 0)
19195 --end;
19196 if (end > start)
19197 {
19198 if (EQ ((end - 1)->object, it->object))
19199 seen_this_string = 1;
19200 }
19201 else
19202 /* If all the glyphs of the previous row were inserted
19203 by redisplay, it means the previous row was
19204 produced from a single newline, which is only
19205 possible if that newline came from the same string
19206 as the one which produced this ROW. */
19207 seen_this_string = 1;
19208 }
19209 else
19210 {
19211 end = r1->glyphs[TEXT_AREA] - 1;
19212 start = end + r1->used[TEXT_AREA];
19213 while (end < start
19214 && INTEGERP ((end + 1)->object)
19215 && (end + 1)->charpos <= 0)
19216 ++end;
19217 if (end < start)
19218 {
19219 if (EQ ((end + 1)->object, it->object))
19220 seen_this_string = 1;
19221 }
19222 else
19223 seen_this_string = 1;
19224 }
19225 }
19226 /* Take note of each display string that covers a newline only
19227 once, the first time we see it. This is for when a display
19228 string includes more than one newline in it. */
19229 if (row->ends_in_newline_from_string_p && !seen_this_string)
19230 {
19231 /* If we were scanning the buffer forward when we displayed
19232 the string, we want to account for at least one buffer
19233 position that belongs to this row (position covered by
19234 the display string), so that cursor positioning will
19235 consider this row as a candidate when point is at the end
19236 of the visual line represented by this row. This is not
19237 required when scanning back, because max_pos will already
19238 have a much larger value. */
19239 if (CHARPOS (row->end.pos) > max_pos)
19240 INC_BOTH (max_pos, max_bpos);
19241 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19242 }
19243 else if (CHARPOS (it->eol_pos) > 0)
19244 SET_TEXT_POS (row->maxpos,
19245 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19246 else if (row->continued_p)
19247 {
19248 /* If max_pos is different from IT's current position, it
19249 means IT->method does not belong to the display element
19250 at max_pos. However, it also means that the display
19251 element at max_pos was displayed in its entirety on this
19252 line, which is equivalent to saying that the next line
19253 starts at the next buffer position. */
19254 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19255 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19256 else
19257 {
19258 INC_BOTH (max_pos, max_bpos);
19259 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19260 }
19261 }
19262 else if (row->truncated_on_right_p)
19263 /* display_line already called reseat_at_next_visible_line_start,
19264 which puts the iterator at the beginning of the next line, in
19265 the logical order. */
19266 row->maxpos = it->current.pos;
19267 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19268 /* A line that is entirely from a string/image/stretch... */
19269 row->maxpos = row->minpos;
19270 else
19271 emacs_abort ();
19272 }
19273 else
19274 row->maxpos = it->current.pos;
19275 }
19276
19277 /* Construct the glyph row IT->glyph_row in the desired matrix of
19278 IT->w from text at the current position of IT. See dispextern.h
19279 for an overview of struct it. Value is non-zero if
19280 IT->glyph_row displays text, as opposed to a line displaying ZV
19281 only. */
19282
19283 static int
19284 display_line (struct it *it)
19285 {
19286 struct glyph_row *row = it->glyph_row;
19287 Lisp_Object overlay_arrow_string;
19288 struct it wrap_it;
19289 void *wrap_data = NULL;
19290 int may_wrap = 0, wrap_x IF_LINT (= 0);
19291 int wrap_row_used = -1;
19292 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19293 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19294 int wrap_row_extra_line_spacing IF_LINT (= 0);
19295 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19296 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19297 int cvpos;
19298 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19299 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19300
19301 /* We always start displaying at hpos zero even if hscrolled. */
19302 eassert (it->hpos == 0 && it->current_x == 0);
19303
19304 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19305 >= it->w->desired_matrix->nrows)
19306 {
19307 it->w->nrows_scale_factor++;
19308 fonts_changed_p = 1;
19309 return 0;
19310 }
19311
19312 /* Is IT->w showing the region? */
19313 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19314
19315 /* Clear the result glyph row and enable it. */
19316 prepare_desired_row (row);
19317
19318 row->y = it->current_y;
19319 row->start = it->start;
19320 row->continuation_lines_width = it->continuation_lines_width;
19321 row->displays_text_p = 1;
19322 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19323 it->starts_in_middle_of_char_p = 0;
19324
19325 /* Arrange the overlays nicely for our purposes. Usually, we call
19326 display_line on only one line at a time, in which case this
19327 can't really hurt too much, or we call it on lines which appear
19328 one after another in the buffer, in which case all calls to
19329 recenter_overlay_lists but the first will be pretty cheap. */
19330 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19331
19332 /* Move over display elements that are not visible because we are
19333 hscrolled. This may stop at an x-position < IT->first_visible_x
19334 if the first glyph is partially visible or if we hit a line end. */
19335 if (it->current_x < it->first_visible_x)
19336 {
19337 enum move_it_result move_result;
19338
19339 this_line_min_pos = row->start.pos;
19340 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19341 MOVE_TO_POS | MOVE_TO_X);
19342 /* If we are under a large hscroll, move_it_in_display_line_to
19343 could hit the end of the line without reaching
19344 it->first_visible_x. Pretend that we did reach it. This is
19345 especially important on a TTY, where we will call
19346 extend_face_to_end_of_line, which needs to know how many
19347 blank glyphs to produce. */
19348 if (it->current_x < it->first_visible_x
19349 && (move_result == MOVE_NEWLINE_OR_CR
19350 || move_result == MOVE_POS_MATCH_OR_ZV))
19351 it->current_x = it->first_visible_x;
19352
19353 /* Record the smallest positions seen while we moved over
19354 display elements that are not visible. This is needed by
19355 redisplay_internal for optimizing the case where the cursor
19356 stays inside the same line. The rest of this function only
19357 considers positions that are actually displayed, so
19358 RECORD_MAX_MIN_POS will not otherwise record positions that
19359 are hscrolled to the left of the left edge of the window. */
19360 min_pos = CHARPOS (this_line_min_pos);
19361 min_bpos = BYTEPOS (this_line_min_pos);
19362 }
19363 else
19364 {
19365 /* We only do this when not calling `move_it_in_display_line_to'
19366 above, because move_it_in_display_line_to calls
19367 handle_line_prefix itself. */
19368 handle_line_prefix (it);
19369 }
19370
19371 /* Get the initial row height. This is either the height of the
19372 text hscrolled, if there is any, or zero. */
19373 row->ascent = it->max_ascent;
19374 row->height = it->max_ascent + it->max_descent;
19375 row->phys_ascent = it->max_phys_ascent;
19376 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19377 row->extra_line_spacing = it->max_extra_line_spacing;
19378
19379 /* Utility macro to record max and min buffer positions seen until now. */
19380 #define RECORD_MAX_MIN_POS(IT) \
19381 do \
19382 { \
19383 int composition_p = !STRINGP ((IT)->string) \
19384 && ((IT)->what == IT_COMPOSITION); \
19385 ptrdiff_t current_pos = \
19386 composition_p ? (IT)->cmp_it.charpos \
19387 : IT_CHARPOS (*(IT)); \
19388 ptrdiff_t current_bpos = \
19389 composition_p ? CHAR_TO_BYTE (current_pos) \
19390 : IT_BYTEPOS (*(IT)); \
19391 if (current_pos < min_pos) \
19392 { \
19393 min_pos = current_pos; \
19394 min_bpos = current_bpos; \
19395 } \
19396 if (IT_CHARPOS (*it) > max_pos) \
19397 { \
19398 max_pos = IT_CHARPOS (*it); \
19399 max_bpos = IT_BYTEPOS (*it); \
19400 } \
19401 } \
19402 while (0)
19403
19404 /* Loop generating characters. The loop is left with IT on the next
19405 character to display. */
19406 while (1)
19407 {
19408 int n_glyphs_before, hpos_before, x_before;
19409 int x, nglyphs;
19410 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19411
19412 /* Retrieve the next thing to display. Value is zero if end of
19413 buffer reached. */
19414 if (!get_next_display_element (it))
19415 {
19416 /* Maybe add a space at the end of this line that is used to
19417 display the cursor there under X. Set the charpos of the
19418 first glyph of blank lines not corresponding to any text
19419 to -1. */
19420 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19421 row->exact_window_width_line_p = 1;
19422 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19423 || row->used[TEXT_AREA] == 0)
19424 {
19425 row->glyphs[TEXT_AREA]->charpos = -1;
19426 row->displays_text_p = 0;
19427
19428 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19429 && (!MINI_WINDOW_P (it->w)
19430 || (minibuf_level && EQ (it->window, minibuf_window))))
19431 row->indicate_empty_line_p = 1;
19432 }
19433
19434 it->continuation_lines_width = 0;
19435 row->ends_at_zv_p = 1;
19436 /* A row that displays right-to-left text must always have
19437 its last face extended all the way to the end of line,
19438 even if this row ends in ZV, because we still write to
19439 the screen left to right. We also need to extend the
19440 last face if the default face is remapped to some
19441 different face, otherwise the functions that clear
19442 portions of the screen will clear with the default face's
19443 background color. */
19444 if (row->reversed_p
19445 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19446 extend_face_to_end_of_line (it);
19447 break;
19448 }
19449
19450 /* Now, get the metrics of what we want to display. This also
19451 generates glyphs in `row' (which is IT->glyph_row). */
19452 n_glyphs_before = row->used[TEXT_AREA];
19453 x = it->current_x;
19454
19455 /* Remember the line height so far in case the next element doesn't
19456 fit on the line. */
19457 if (it->line_wrap != TRUNCATE)
19458 {
19459 ascent = it->max_ascent;
19460 descent = it->max_descent;
19461 phys_ascent = it->max_phys_ascent;
19462 phys_descent = it->max_phys_descent;
19463
19464 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19465 {
19466 if (IT_DISPLAYING_WHITESPACE (it))
19467 may_wrap = 1;
19468 else if (may_wrap)
19469 {
19470 SAVE_IT (wrap_it, *it, wrap_data);
19471 wrap_x = x;
19472 wrap_row_used = row->used[TEXT_AREA];
19473 wrap_row_ascent = row->ascent;
19474 wrap_row_height = row->height;
19475 wrap_row_phys_ascent = row->phys_ascent;
19476 wrap_row_phys_height = row->phys_height;
19477 wrap_row_extra_line_spacing = row->extra_line_spacing;
19478 wrap_row_min_pos = min_pos;
19479 wrap_row_min_bpos = min_bpos;
19480 wrap_row_max_pos = max_pos;
19481 wrap_row_max_bpos = max_bpos;
19482 may_wrap = 0;
19483 }
19484 }
19485 }
19486
19487 PRODUCE_GLYPHS (it);
19488
19489 /* If this display element was in marginal areas, continue with
19490 the next one. */
19491 if (it->area != TEXT_AREA)
19492 {
19493 row->ascent = max (row->ascent, it->max_ascent);
19494 row->height = max (row->height, it->max_ascent + it->max_descent);
19495 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19496 row->phys_height = max (row->phys_height,
19497 it->max_phys_ascent + it->max_phys_descent);
19498 row->extra_line_spacing = max (row->extra_line_spacing,
19499 it->max_extra_line_spacing);
19500 set_iterator_to_next (it, 1);
19501 continue;
19502 }
19503
19504 /* Does the display element fit on the line? If we truncate
19505 lines, we should draw past the right edge of the window. If
19506 we don't truncate, we want to stop so that we can display the
19507 continuation glyph before the right margin. If lines are
19508 continued, there are two possible strategies for characters
19509 resulting in more than 1 glyph (e.g. tabs): Display as many
19510 glyphs as possible in this line and leave the rest for the
19511 continuation line, or display the whole element in the next
19512 line. Original redisplay did the former, so we do it also. */
19513 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19514 hpos_before = it->hpos;
19515 x_before = x;
19516
19517 if (/* Not a newline. */
19518 nglyphs > 0
19519 /* Glyphs produced fit entirely in the line. */
19520 && it->current_x < it->last_visible_x)
19521 {
19522 it->hpos += nglyphs;
19523 row->ascent = max (row->ascent, it->max_ascent);
19524 row->height = max (row->height, it->max_ascent + it->max_descent);
19525 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19526 row->phys_height = max (row->phys_height,
19527 it->max_phys_ascent + it->max_phys_descent);
19528 row->extra_line_spacing = max (row->extra_line_spacing,
19529 it->max_extra_line_spacing);
19530 if (it->current_x - it->pixel_width < it->first_visible_x)
19531 row->x = x - it->first_visible_x;
19532 /* Record the maximum and minimum buffer positions seen so
19533 far in glyphs that will be displayed by this row. */
19534 if (it->bidi_p)
19535 RECORD_MAX_MIN_POS (it);
19536 }
19537 else
19538 {
19539 int i, new_x;
19540 struct glyph *glyph;
19541
19542 for (i = 0; i < nglyphs; ++i, x = new_x)
19543 {
19544 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19545 new_x = x + glyph->pixel_width;
19546
19547 if (/* Lines are continued. */
19548 it->line_wrap != TRUNCATE
19549 && (/* Glyph doesn't fit on the line. */
19550 new_x > it->last_visible_x
19551 /* Or it fits exactly on a window system frame. */
19552 || (new_x == it->last_visible_x
19553 && FRAME_WINDOW_P (it->f)
19554 && (row->reversed_p
19555 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19556 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19557 {
19558 /* End of a continued line. */
19559
19560 if (it->hpos == 0
19561 || (new_x == it->last_visible_x
19562 && FRAME_WINDOW_P (it->f)
19563 && (row->reversed_p
19564 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19565 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19566 {
19567 /* Current glyph is the only one on the line or
19568 fits exactly on the line. We must continue
19569 the line because we can't draw the cursor
19570 after the glyph. */
19571 row->continued_p = 1;
19572 it->current_x = new_x;
19573 it->continuation_lines_width += new_x;
19574 ++it->hpos;
19575 if (i == nglyphs - 1)
19576 {
19577 /* If line-wrap is on, check if a previous
19578 wrap point was found. */
19579 if (wrap_row_used > 0
19580 /* Even if there is a previous wrap
19581 point, continue the line here as
19582 usual, if (i) the previous character
19583 was a space or tab AND (ii) the
19584 current character is not. */
19585 && (!may_wrap
19586 || IT_DISPLAYING_WHITESPACE (it)))
19587 goto back_to_wrap;
19588
19589 /* Record the maximum and minimum buffer
19590 positions seen so far in glyphs that will be
19591 displayed by this row. */
19592 if (it->bidi_p)
19593 RECORD_MAX_MIN_POS (it);
19594 set_iterator_to_next (it, 1);
19595 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19596 {
19597 if (!get_next_display_element (it))
19598 {
19599 row->exact_window_width_line_p = 1;
19600 it->continuation_lines_width = 0;
19601 row->continued_p = 0;
19602 row->ends_at_zv_p = 1;
19603 }
19604 else if (ITERATOR_AT_END_OF_LINE_P (it))
19605 {
19606 row->continued_p = 0;
19607 row->exact_window_width_line_p = 1;
19608 }
19609 }
19610 }
19611 else if (it->bidi_p)
19612 RECORD_MAX_MIN_POS (it);
19613 }
19614 else if (CHAR_GLYPH_PADDING_P (*glyph)
19615 && !FRAME_WINDOW_P (it->f))
19616 {
19617 /* A padding glyph that doesn't fit on this line.
19618 This means the whole character doesn't fit
19619 on the line. */
19620 if (row->reversed_p)
19621 unproduce_glyphs (it, row->used[TEXT_AREA]
19622 - n_glyphs_before);
19623 row->used[TEXT_AREA] = n_glyphs_before;
19624
19625 /* Fill the rest of the row with continuation
19626 glyphs like in 20.x. */
19627 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19628 < row->glyphs[1 + TEXT_AREA])
19629 produce_special_glyphs (it, IT_CONTINUATION);
19630
19631 row->continued_p = 1;
19632 it->current_x = x_before;
19633 it->continuation_lines_width += x_before;
19634
19635 /* Restore the height to what it was before the
19636 element not fitting on the line. */
19637 it->max_ascent = ascent;
19638 it->max_descent = descent;
19639 it->max_phys_ascent = phys_ascent;
19640 it->max_phys_descent = phys_descent;
19641 }
19642 else if (wrap_row_used > 0)
19643 {
19644 back_to_wrap:
19645 if (row->reversed_p)
19646 unproduce_glyphs (it,
19647 row->used[TEXT_AREA] - wrap_row_used);
19648 RESTORE_IT (it, &wrap_it, wrap_data);
19649 it->continuation_lines_width += wrap_x;
19650 row->used[TEXT_AREA] = wrap_row_used;
19651 row->ascent = wrap_row_ascent;
19652 row->height = wrap_row_height;
19653 row->phys_ascent = wrap_row_phys_ascent;
19654 row->phys_height = wrap_row_phys_height;
19655 row->extra_line_spacing = wrap_row_extra_line_spacing;
19656 min_pos = wrap_row_min_pos;
19657 min_bpos = wrap_row_min_bpos;
19658 max_pos = wrap_row_max_pos;
19659 max_bpos = wrap_row_max_bpos;
19660 row->continued_p = 1;
19661 row->ends_at_zv_p = 0;
19662 row->exact_window_width_line_p = 0;
19663 it->continuation_lines_width += x;
19664
19665 /* Make sure that a non-default face is extended
19666 up to the right margin of the window. */
19667 extend_face_to_end_of_line (it);
19668 }
19669 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19670 {
19671 /* A TAB that extends past the right edge of the
19672 window. This produces a single glyph on
19673 window system frames. We leave the glyph in
19674 this row and let it fill the row, but don't
19675 consume the TAB. */
19676 if ((row->reversed_p
19677 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19678 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19679 produce_special_glyphs (it, IT_CONTINUATION);
19680 it->continuation_lines_width += it->last_visible_x;
19681 row->ends_in_middle_of_char_p = 1;
19682 row->continued_p = 1;
19683 glyph->pixel_width = it->last_visible_x - x;
19684 it->starts_in_middle_of_char_p = 1;
19685 }
19686 else
19687 {
19688 /* Something other than a TAB that draws past
19689 the right edge of the window. Restore
19690 positions to values before the element. */
19691 if (row->reversed_p)
19692 unproduce_glyphs (it, row->used[TEXT_AREA]
19693 - (n_glyphs_before + i));
19694 row->used[TEXT_AREA] = n_glyphs_before + i;
19695
19696 /* Display continuation glyphs. */
19697 it->current_x = x_before;
19698 it->continuation_lines_width += x;
19699 if (!FRAME_WINDOW_P (it->f)
19700 || (row->reversed_p
19701 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19702 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19703 produce_special_glyphs (it, IT_CONTINUATION);
19704 row->continued_p = 1;
19705
19706 extend_face_to_end_of_line (it);
19707
19708 if (nglyphs > 1 && i > 0)
19709 {
19710 row->ends_in_middle_of_char_p = 1;
19711 it->starts_in_middle_of_char_p = 1;
19712 }
19713
19714 /* Restore the height to what it was before the
19715 element not fitting on the line. */
19716 it->max_ascent = ascent;
19717 it->max_descent = descent;
19718 it->max_phys_ascent = phys_ascent;
19719 it->max_phys_descent = phys_descent;
19720 }
19721
19722 break;
19723 }
19724 else if (new_x > it->first_visible_x)
19725 {
19726 /* Increment number of glyphs actually displayed. */
19727 ++it->hpos;
19728
19729 /* Record the maximum and minimum buffer positions
19730 seen so far in glyphs that will be displayed by
19731 this row. */
19732 if (it->bidi_p)
19733 RECORD_MAX_MIN_POS (it);
19734
19735 if (x < it->first_visible_x)
19736 /* Glyph is partially visible, i.e. row starts at
19737 negative X position. */
19738 row->x = x - it->first_visible_x;
19739 }
19740 else
19741 {
19742 /* Glyph is completely off the left margin of the
19743 window. This should not happen because of the
19744 move_it_in_display_line at the start of this
19745 function, unless the text display area of the
19746 window is empty. */
19747 eassert (it->first_visible_x <= it->last_visible_x);
19748 }
19749 }
19750 /* Even if this display element produced no glyphs at all,
19751 we want to record its position. */
19752 if (it->bidi_p && nglyphs == 0)
19753 RECORD_MAX_MIN_POS (it);
19754
19755 row->ascent = max (row->ascent, it->max_ascent);
19756 row->height = max (row->height, it->max_ascent + it->max_descent);
19757 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19758 row->phys_height = max (row->phys_height,
19759 it->max_phys_ascent + it->max_phys_descent);
19760 row->extra_line_spacing = max (row->extra_line_spacing,
19761 it->max_extra_line_spacing);
19762
19763 /* End of this display line if row is continued. */
19764 if (row->continued_p || row->ends_at_zv_p)
19765 break;
19766 }
19767
19768 at_end_of_line:
19769 /* Is this a line end? If yes, we're also done, after making
19770 sure that a non-default face is extended up to the right
19771 margin of the window. */
19772 if (ITERATOR_AT_END_OF_LINE_P (it))
19773 {
19774 int used_before = row->used[TEXT_AREA];
19775
19776 row->ends_in_newline_from_string_p = STRINGP (it->object);
19777
19778 /* Add a space at the end of the line that is used to
19779 display the cursor there. */
19780 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19781 append_space_for_newline (it, 0);
19782
19783 /* Extend the face to the end of the line. */
19784 extend_face_to_end_of_line (it);
19785
19786 /* Make sure we have the position. */
19787 if (used_before == 0)
19788 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19789
19790 /* Record the position of the newline, for use in
19791 find_row_edges. */
19792 it->eol_pos = it->current.pos;
19793
19794 /* Consume the line end. This skips over invisible lines. */
19795 set_iterator_to_next (it, 1);
19796 it->continuation_lines_width = 0;
19797 break;
19798 }
19799
19800 /* Proceed with next display element. Note that this skips
19801 over lines invisible because of selective display. */
19802 set_iterator_to_next (it, 1);
19803
19804 /* If we truncate lines, we are done when the last displayed
19805 glyphs reach past the right margin of the window. */
19806 if (it->line_wrap == TRUNCATE
19807 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19808 ? (it->current_x >= it->last_visible_x)
19809 : (it->current_x > it->last_visible_x)))
19810 {
19811 /* Maybe add truncation glyphs. */
19812 if (!FRAME_WINDOW_P (it->f)
19813 || (row->reversed_p
19814 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19815 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19816 {
19817 int i, n;
19818
19819 if (!row->reversed_p)
19820 {
19821 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19822 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19823 break;
19824 }
19825 else
19826 {
19827 for (i = 0; i < row->used[TEXT_AREA]; i++)
19828 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19829 break;
19830 /* Remove any padding glyphs at the front of ROW, to
19831 make room for the truncation glyphs we will be
19832 adding below. The loop below always inserts at
19833 least one truncation glyph, so also remove the
19834 last glyph added to ROW. */
19835 unproduce_glyphs (it, i + 1);
19836 /* Adjust i for the loop below. */
19837 i = row->used[TEXT_AREA] - (i + 1);
19838 }
19839
19840 it->current_x = x_before;
19841 if (!FRAME_WINDOW_P (it->f))
19842 {
19843 for (n = row->used[TEXT_AREA]; i < n; ++i)
19844 {
19845 row->used[TEXT_AREA] = i;
19846 produce_special_glyphs (it, IT_TRUNCATION);
19847 }
19848 }
19849 else
19850 {
19851 row->used[TEXT_AREA] = i;
19852 produce_special_glyphs (it, IT_TRUNCATION);
19853 }
19854 }
19855 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19856 {
19857 /* Don't truncate if we can overflow newline into fringe. */
19858 if (!get_next_display_element (it))
19859 {
19860 it->continuation_lines_width = 0;
19861 row->ends_at_zv_p = 1;
19862 row->exact_window_width_line_p = 1;
19863 break;
19864 }
19865 if (ITERATOR_AT_END_OF_LINE_P (it))
19866 {
19867 row->exact_window_width_line_p = 1;
19868 goto at_end_of_line;
19869 }
19870 it->current_x = x_before;
19871 }
19872
19873 row->truncated_on_right_p = 1;
19874 it->continuation_lines_width = 0;
19875 reseat_at_next_visible_line_start (it, 0);
19876 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19877 it->hpos = hpos_before;
19878 break;
19879 }
19880 }
19881
19882 if (wrap_data)
19883 bidi_unshelve_cache (wrap_data, 1);
19884
19885 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19886 at the left window margin. */
19887 if (it->first_visible_x
19888 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19889 {
19890 if (!FRAME_WINDOW_P (it->f)
19891 || (row->reversed_p
19892 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19893 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19894 insert_left_trunc_glyphs (it);
19895 row->truncated_on_left_p = 1;
19896 }
19897
19898 /* Remember the position at which this line ends.
19899
19900 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19901 cannot be before the call to find_row_edges below, since that is
19902 where these positions are determined. */
19903 row->end = it->current;
19904 if (!it->bidi_p)
19905 {
19906 row->minpos = row->start.pos;
19907 row->maxpos = row->end.pos;
19908 }
19909 else
19910 {
19911 /* ROW->minpos and ROW->maxpos must be the smallest and
19912 `1 + the largest' buffer positions in ROW. But if ROW was
19913 bidi-reordered, these two positions can be anywhere in the
19914 row, so we must determine them now. */
19915 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19916 }
19917
19918 /* If the start of this line is the overlay arrow-position, then
19919 mark this glyph row as the one containing the overlay arrow.
19920 This is clearly a mess with variable size fonts. It would be
19921 better to let it be displayed like cursors under X. */
19922 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19923 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19924 !NILP (overlay_arrow_string)))
19925 {
19926 /* Overlay arrow in window redisplay is a fringe bitmap. */
19927 if (STRINGP (overlay_arrow_string))
19928 {
19929 struct glyph_row *arrow_row
19930 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19931 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19932 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19933 struct glyph *p = row->glyphs[TEXT_AREA];
19934 struct glyph *p2, *end;
19935
19936 /* Copy the arrow glyphs. */
19937 while (glyph < arrow_end)
19938 *p++ = *glyph++;
19939
19940 /* Throw away padding glyphs. */
19941 p2 = p;
19942 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19943 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19944 ++p2;
19945 if (p2 > p)
19946 {
19947 while (p2 < end)
19948 *p++ = *p2++;
19949 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19950 }
19951 }
19952 else
19953 {
19954 eassert (INTEGERP (overlay_arrow_string));
19955 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19956 }
19957 overlay_arrow_seen = 1;
19958 }
19959
19960 /* Highlight trailing whitespace. */
19961 if (!NILP (Vshow_trailing_whitespace))
19962 highlight_trailing_whitespace (it->f, it->glyph_row);
19963
19964 /* Compute pixel dimensions of this line. */
19965 compute_line_metrics (it);
19966
19967 /* Implementation note: No changes in the glyphs of ROW or in their
19968 faces can be done past this point, because compute_line_metrics
19969 computes ROW's hash value and stores it within the glyph_row
19970 structure. */
19971
19972 /* Record whether this row ends inside an ellipsis. */
19973 row->ends_in_ellipsis_p
19974 = (it->method == GET_FROM_DISPLAY_VECTOR
19975 && it->ellipsis_p);
19976
19977 /* Save fringe bitmaps in this row. */
19978 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19979 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19980 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19981 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19982
19983 it->left_user_fringe_bitmap = 0;
19984 it->left_user_fringe_face_id = 0;
19985 it->right_user_fringe_bitmap = 0;
19986 it->right_user_fringe_face_id = 0;
19987
19988 /* Maybe set the cursor. */
19989 cvpos = it->w->cursor.vpos;
19990 if ((cvpos < 0
19991 /* In bidi-reordered rows, keep checking for proper cursor
19992 position even if one has been found already, because buffer
19993 positions in such rows change non-linearly with ROW->VPOS,
19994 when a line is continued. One exception: when we are at ZV,
19995 display cursor on the first suitable glyph row, since all
19996 the empty rows after that also have their position set to ZV. */
19997 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19998 lines' rows is implemented for bidi-reordered rows. */
19999 || (it->bidi_p
20000 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20001 && PT >= MATRIX_ROW_START_CHARPOS (row)
20002 && PT <= MATRIX_ROW_END_CHARPOS (row)
20003 && cursor_row_p (row))
20004 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20005
20006 /* Prepare for the next line. This line starts horizontally at (X
20007 HPOS) = (0 0). Vertical positions are incremented. As a
20008 convenience for the caller, IT->glyph_row is set to the next
20009 row to be used. */
20010 it->current_x = it->hpos = 0;
20011 it->current_y += row->height;
20012 SET_TEXT_POS (it->eol_pos, 0, 0);
20013 ++it->vpos;
20014 ++it->glyph_row;
20015 /* The next row should by default use the same value of the
20016 reversed_p flag as this one. set_iterator_to_next decides when
20017 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20018 the flag accordingly. */
20019 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20020 it->glyph_row->reversed_p = row->reversed_p;
20021 it->start = row->end;
20022 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20023
20024 #undef RECORD_MAX_MIN_POS
20025 }
20026
20027 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20028 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20029 doc: /* Return paragraph direction at point in BUFFER.
20030 Value is either `left-to-right' or `right-to-left'.
20031 If BUFFER is omitted or nil, it defaults to the current buffer.
20032
20033 Paragraph direction determines how the text in the paragraph is displayed.
20034 In left-to-right paragraphs, text begins at the left margin of the window
20035 and the reading direction is generally left to right. In right-to-left
20036 paragraphs, text begins at the right margin and is read from right to left.
20037
20038 See also `bidi-paragraph-direction'. */)
20039 (Lisp_Object buffer)
20040 {
20041 struct buffer *buf = current_buffer;
20042 struct buffer *old = buf;
20043
20044 if (! NILP (buffer))
20045 {
20046 CHECK_BUFFER (buffer);
20047 buf = XBUFFER (buffer);
20048 }
20049
20050 if (NILP (BVAR (buf, bidi_display_reordering))
20051 || NILP (BVAR (buf, enable_multibyte_characters))
20052 /* When we are loading loadup.el, the character property tables
20053 needed for bidi iteration are not yet available. */
20054 || !NILP (Vpurify_flag))
20055 return Qleft_to_right;
20056 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20057 return BVAR (buf, bidi_paragraph_direction);
20058 else
20059 {
20060 /* Determine the direction from buffer text. We could try to
20061 use current_matrix if it is up to date, but this seems fast
20062 enough as it is. */
20063 struct bidi_it itb;
20064 ptrdiff_t pos = BUF_PT (buf);
20065 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20066 int c;
20067 void *itb_data = bidi_shelve_cache ();
20068
20069 set_buffer_temp (buf);
20070 /* bidi_paragraph_init finds the base direction of the paragraph
20071 by searching forward from paragraph start. We need the base
20072 direction of the current or _previous_ paragraph, so we need
20073 to make sure we are within that paragraph. To that end, find
20074 the previous non-empty line. */
20075 if (pos >= ZV && pos > BEGV)
20076 DEC_BOTH (pos, bytepos);
20077 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20078 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20079 {
20080 while ((c = FETCH_BYTE (bytepos)) == '\n'
20081 || c == ' ' || c == '\t' || c == '\f')
20082 {
20083 if (bytepos <= BEGV_BYTE)
20084 break;
20085 bytepos--;
20086 pos--;
20087 }
20088 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20089 bytepos--;
20090 }
20091 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20092 itb.paragraph_dir = NEUTRAL_DIR;
20093 itb.string.s = NULL;
20094 itb.string.lstring = Qnil;
20095 itb.string.bufpos = 0;
20096 itb.string.unibyte = 0;
20097 /* We have no window to use here for ignoring window-specific
20098 overlays. Using NULL for window pointer will cause
20099 compute_display_string_pos to use the current buffer. */
20100 itb.w = NULL;
20101 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20102 bidi_unshelve_cache (itb_data, 0);
20103 set_buffer_temp (old);
20104 switch (itb.paragraph_dir)
20105 {
20106 case L2R:
20107 return Qleft_to_right;
20108 break;
20109 case R2L:
20110 return Qright_to_left;
20111 break;
20112 default:
20113 emacs_abort ();
20114 }
20115 }
20116 }
20117
20118 DEFUN ("move-point-visually", Fmove_point_visually,
20119 Smove_point_visually, 1, 1, 0,
20120 doc: /* Move point in the visual order in the specified DIRECTION.
20121 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20122 left.
20123
20124 Value is the new character position of point. */)
20125 (Lisp_Object direction)
20126 {
20127 struct window *w = XWINDOW (selected_window);
20128 struct buffer *b = NULL;
20129 struct glyph_row *row;
20130 int dir;
20131 Lisp_Object paragraph_dir;
20132
20133 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20134 (!(ROW)->continued_p \
20135 && INTEGERP ((GLYPH)->object) \
20136 && (GLYPH)->type == CHAR_GLYPH \
20137 && (GLYPH)->u.ch == ' ' \
20138 && (GLYPH)->charpos >= 0 \
20139 && !(GLYPH)->avoid_cursor_p)
20140
20141 CHECK_NUMBER (direction);
20142 dir = XINT (direction);
20143 if (dir > 0)
20144 dir = 1;
20145 else
20146 dir = -1;
20147
20148 if (BUFFERP (w->contents))
20149 b = XBUFFER (w->contents);
20150
20151 /* If current matrix is up-to-date, we can use the information
20152 recorded in the glyphs, at least as long as the goal is on the
20153 screen. */
20154 if (w->window_end_valid
20155 && !windows_or_buffers_changed
20156 && b
20157 && !b->clip_changed
20158 && !b->prevent_redisplay_optimizations_p
20159 && w->last_modified >= BUF_MODIFF (b)
20160 && w->last_overlay_modified >= BUF_OVERLAY_MODIFF (b)
20161 && w->cursor.vpos >= 0
20162 && w->cursor.vpos < w->current_matrix->nrows
20163 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20164 {
20165 struct glyph *g = row->glyphs[TEXT_AREA];
20166 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20167 struct glyph *gpt = g + w->cursor.hpos;
20168
20169 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20170 {
20171 if (BUFFERP (g->object) && g->charpos != PT)
20172 {
20173 SET_PT (g->charpos);
20174 w->cursor.vpos = -1;
20175 return make_number (PT);
20176 }
20177 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20178 {
20179 ptrdiff_t new_pos;
20180
20181 if (BUFFERP (gpt->object))
20182 {
20183 new_pos = PT;
20184 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20185 new_pos += (row->reversed_p ? -dir : dir);
20186 else
20187 new_pos -= (row->reversed_p ? -dir : dir);;
20188 }
20189 else if (BUFFERP (g->object))
20190 new_pos = g->charpos;
20191 else
20192 break;
20193 SET_PT (new_pos);
20194 w->cursor.vpos = -1;
20195 return make_number (PT);
20196 }
20197 else if (ROW_GLYPH_NEWLINE_P (row, g))
20198 {
20199 /* Glyphs inserted at the end of a non-empty line for
20200 positioning the cursor have zero charpos, so we must
20201 deduce the value of point by other means. */
20202 if (g->charpos > 0)
20203 SET_PT (g->charpos);
20204 else if (row->ends_at_zv_p && PT != ZV)
20205 SET_PT (ZV);
20206 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20207 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20208 else
20209 break;
20210 w->cursor.vpos = -1;
20211 return make_number (PT);
20212 }
20213 }
20214 if (g == e || INTEGERP (g->object))
20215 {
20216 if (row->truncated_on_left_p || row->truncated_on_right_p)
20217 goto simulate_display;
20218 if (!row->reversed_p)
20219 row += dir;
20220 else
20221 row -= dir;
20222 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20223 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20224 goto simulate_display;
20225
20226 if (dir > 0)
20227 {
20228 if (row->reversed_p && !row->continued_p)
20229 {
20230 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20231 w->cursor.vpos = -1;
20232 return make_number (PT);
20233 }
20234 g = row->glyphs[TEXT_AREA];
20235 e = g + row->used[TEXT_AREA];
20236 for ( ; g < e; g++)
20237 {
20238 if (BUFFERP (g->object)
20239 /* Empty lines have only one glyph, which stands
20240 for the newline, and whose charpos is the
20241 buffer position of the newline. */
20242 || ROW_GLYPH_NEWLINE_P (row, g)
20243 /* When the buffer ends in a newline, the line at
20244 EOB also has one glyph, but its charpos is -1. */
20245 || (row->ends_at_zv_p
20246 && !row->reversed_p
20247 && INTEGERP (g->object)
20248 && g->type == CHAR_GLYPH
20249 && g->u.ch == ' '))
20250 {
20251 if (g->charpos > 0)
20252 SET_PT (g->charpos);
20253 else if (!row->reversed_p
20254 && row->ends_at_zv_p
20255 && PT != ZV)
20256 SET_PT (ZV);
20257 else
20258 continue;
20259 w->cursor.vpos = -1;
20260 return make_number (PT);
20261 }
20262 }
20263 }
20264 else
20265 {
20266 if (!row->reversed_p && !row->continued_p)
20267 {
20268 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20269 w->cursor.vpos = -1;
20270 return make_number (PT);
20271 }
20272 e = row->glyphs[TEXT_AREA];
20273 g = e + row->used[TEXT_AREA] - 1;
20274 for ( ; g >= e; g--)
20275 {
20276 if (BUFFERP (g->object)
20277 || (ROW_GLYPH_NEWLINE_P (row, g)
20278 && g->charpos > 0)
20279 /* Empty R2L lines on GUI frames have the buffer
20280 position of the newline stored in the stretch
20281 glyph. */
20282 || g->type == STRETCH_GLYPH
20283 || (row->ends_at_zv_p
20284 && row->reversed_p
20285 && INTEGERP (g->object)
20286 && g->type == CHAR_GLYPH
20287 && g->u.ch == ' '))
20288 {
20289 if (g->charpos > 0)
20290 SET_PT (g->charpos);
20291 else if (row->reversed_p
20292 && row->ends_at_zv_p
20293 && PT != ZV)
20294 SET_PT (ZV);
20295 else
20296 continue;
20297 w->cursor.vpos = -1;
20298 return make_number (PT);
20299 }
20300 }
20301 }
20302 }
20303 }
20304
20305 simulate_display:
20306
20307 /* If we wind up here, we failed to move by using the glyphs, so we
20308 need to simulate display instead. */
20309
20310 if (b)
20311 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20312 else
20313 paragraph_dir = Qleft_to_right;
20314 if (EQ (paragraph_dir, Qright_to_left))
20315 dir = -dir;
20316 if (PT <= BEGV && dir < 0)
20317 xsignal0 (Qbeginning_of_buffer);
20318 else if (PT >= ZV && dir > 0)
20319 xsignal0 (Qend_of_buffer);
20320 else
20321 {
20322 struct text_pos pt;
20323 struct it it;
20324 int pt_x, target_x, pixel_width, pt_vpos;
20325 bool at_eol_p;
20326 bool overshoot_expected = false;
20327 bool target_is_eol_p = false;
20328
20329 /* Setup the arena. */
20330 SET_TEXT_POS (pt, PT, PT_BYTE);
20331 start_display (&it, w, pt);
20332
20333 if (it.cmp_it.id < 0
20334 && it.method == GET_FROM_STRING
20335 && it.area == TEXT_AREA
20336 && it.string_from_display_prop_p
20337 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20338 overshoot_expected = true;
20339
20340 /* Find the X coordinate of point. We start from the beginning
20341 of this or previous line to make sure we are before point in
20342 the logical order (since the move_it_* functions can only
20343 move forward). */
20344 reseat_at_previous_visible_line_start (&it);
20345 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20346 if (IT_CHARPOS (it) != PT)
20347 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20348 -1, -1, -1, MOVE_TO_POS);
20349 pt_x = it.current_x;
20350 pt_vpos = it.vpos;
20351 if (dir > 0 || overshoot_expected)
20352 {
20353 struct glyph_row *row = it.glyph_row;
20354
20355 /* When point is at beginning of line, we don't have
20356 information about the glyph there loaded into struct
20357 it. Calling get_next_display_element fixes that. */
20358 if (pt_x == 0)
20359 get_next_display_element (&it);
20360 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20361 it.glyph_row = NULL;
20362 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20363 it.glyph_row = row;
20364 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20365 it, lest it will become out of sync with it's buffer
20366 position. */
20367 it.current_x = pt_x;
20368 }
20369 else
20370 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20371 pixel_width = it.pixel_width;
20372 if (overshoot_expected && at_eol_p)
20373 pixel_width = 0;
20374 else if (pixel_width <= 0)
20375 pixel_width = 1;
20376
20377 /* If there's a display string at point, we are actually at the
20378 glyph to the left of point, so we need to correct the X
20379 coordinate. */
20380 if (overshoot_expected)
20381 pt_x += pixel_width;
20382
20383 /* Compute target X coordinate, either to the left or to the
20384 right of point. On TTY frames, all characters have the same
20385 pixel width of 1, so we can use that. On GUI frames we don't
20386 have an easy way of getting at the pixel width of the
20387 character to the left of point, so we use a different method
20388 of getting to that place. */
20389 if (dir > 0)
20390 target_x = pt_x + pixel_width;
20391 else
20392 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20393
20394 /* Target X coordinate could be one line above or below the line
20395 of point, in which case we need to adjust the target X
20396 coordinate. Also, if moving to the left, we need to begin at
20397 the left edge of the point's screen line. */
20398 if (dir < 0)
20399 {
20400 if (pt_x > 0)
20401 {
20402 start_display (&it, w, pt);
20403 reseat_at_previous_visible_line_start (&it);
20404 it.current_x = it.current_y = it.hpos = 0;
20405 if (pt_vpos != 0)
20406 move_it_by_lines (&it, pt_vpos);
20407 }
20408 else
20409 {
20410 move_it_by_lines (&it, -1);
20411 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20412 target_is_eol_p = true;
20413 }
20414 }
20415 else
20416 {
20417 if (at_eol_p
20418 || (target_x >= it.last_visible_x
20419 && it.line_wrap != TRUNCATE))
20420 {
20421 if (pt_x > 0)
20422 move_it_by_lines (&it, 0);
20423 move_it_by_lines (&it, 1);
20424 target_x = 0;
20425 }
20426 }
20427
20428 /* Move to the target X coordinate. */
20429 #ifdef HAVE_WINDOW_SYSTEM
20430 /* On GUI frames, as we don't know the X coordinate of the
20431 character to the left of point, moving point to the left
20432 requires walking, one grapheme cluster at a time, until we
20433 find ourself at a place immediately to the left of the
20434 character at point. */
20435 if (FRAME_WINDOW_P (it.f) && dir < 0)
20436 {
20437 struct text_pos new_pos = it.current.pos;
20438 enum move_it_result rc = MOVE_X_REACHED;
20439
20440 while (it.current_x + it.pixel_width <= target_x
20441 && rc == MOVE_X_REACHED)
20442 {
20443 int new_x = it.current_x + it.pixel_width;
20444
20445 new_pos = it.current.pos;
20446 if (new_x == it.current_x)
20447 new_x++;
20448 rc = move_it_in_display_line_to (&it, ZV, new_x,
20449 MOVE_TO_POS | MOVE_TO_X);
20450 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20451 break;
20452 }
20453 /* If we ended up on a composed character inside
20454 bidi-reordered text (e.g., Hebrew text with diacritics),
20455 the iterator gives us the buffer position of the last (in
20456 logical order) character of the composed grapheme cluster,
20457 which is not what we want. So we cheat: we compute the
20458 character position of the character that follows (in the
20459 logical order) the one where the above loop stopped. That
20460 character will appear on display to the left of point. */
20461 if (it.bidi_p
20462 && it.bidi_it.scan_dir == -1
20463 && new_pos.charpos - IT_CHARPOS (it) > 1)
20464 {
20465 new_pos.charpos = IT_CHARPOS (it) + 1;
20466 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20467 }
20468 it.current.pos = new_pos;
20469 }
20470 else
20471 #endif
20472 if (it.current_x != target_x)
20473 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20474
20475 /* When lines are truncated, the above loop will stop at the
20476 window edge. But we want to get to the end of line, even if
20477 it is beyond the window edge; automatic hscroll will then
20478 scroll the window to show point as appropriate. */
20479 if (target_is_eol_p && it.line_wrap == TRUNCATE
20480 && get_next_display_element (&it))
20481 {
20482 struct text_pos new_pos = it.current.pos;
20483
20484 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20485 {
20486 set_iterator_to_next (&it, 0);
20487 if (it.method == GET_FROM_BUFFER)
20488 new_pos = it.current.pos;
20489 if (!get_next_display_element (&it))
20490 break;
20491 }
20492
20493 it.current.pos = new_pos;
20494 }
20495
20496 /* If we ended up in a display string that covers point, move to
20497 buffer position to the right in the visual order. */
20498 if (dir > 0)
20499 {
20500 while (IT_CHARPOS (it) == PT)
20501 {
20502 set_iterator_to_next (&it, 0);
20503 if (!get_next_display_element (&it))
20504 break;
20505 }
20506 }
20507
20508 /* Move point to that position. */
20509 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20510 }
20511
20512 return make_number (PT);
20513
20514 #undef ROW_GLYPH_NEWLINE_P
20515 }
20516
20517 \f
20518 /***********************************************************************
20519 Menu Bar
20520 ***********************************************************************/
20521
20522 /* Redisplay the menu bar in the frame for window W.
20523
20524 The menu bar of X frames that don't have X toolkit support is
20525 displayed in a special window W->frame->menu_bar_window.
20526
20527 The menu bar of terminal frames is treated specially as far as
20528 glyph matrices are concerned. Menu bar lines are not part of
20529 windows, so the update is done directly on the frame matrix rows
20530 for the menu bar. */
20531
20532 static void
20533 display_menu_bar (struct window *w)
20534 {
20535 struct frame *f = XFRAME (WINDOW_FRAME (w));
20536 struct it it;
20537 Lisp_Object items;
20538 int i;
20539
20540 /* Don't do all this for graphical frames. */
20541 #ifdef HAVE_NTGUI
20542 if (FRAME_W32_P (f))
20543 return;
20544 #endif
20545 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20546 if (FRAME_X_P (f))
20547 return;
20548 #endif
20549
20550 #ifdef HAVE_NS
20551 if (FRAME_NS_P (f))
20552 return;
20553 #endif /* HAVE_NS */
20554
20555 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20556 eassert (!FRAME_WINDOW_P (f));
20557 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20558 it.first_visible_x = 0;
20559 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20560 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20561 if (FRAME_WINDOW_P (f))
20562 {
20563 /* Menu bar lines are displayed in the desired matrix of the
20564 dummy window menu_bar_window. */
20565 struct window *menu_w;
20566 menu_w = XWINDOW (f->menu_bar_window);
20567 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20568 MENU_FACE_ID);
20569 it.first_visible_x = 0;
20570 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20571 }
20572 else
20573 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20574 {
20575 /* This is a TTY frame, i.e. character hpos/vpos are used as
20576 pixel x/y. */
20577 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20578 MENU_FACE_ID);
20579 it.first_visible_x = 0;
20580 it.last_visible_x = FRAME_COLS (f);
20581 }
20582
20583 /* FIXME: This should be controlled by a user option. See the
20584 comments in redisplay_tool_bar and display_mode_line about
20585 this. */
20586 it.paragraph_embedding = L2R;
20587
20588 /* Clear all rows of the menu bar. */
20589 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20590 {
20591 struct glyph_row *row = it.glyph_row + i;
20592 clear_glyph_row (row);
20593 row->enabled_p = 1;
20594 row->full_width_p = 1;
20595 }
20596
20597 /* Display all items of the menu bar. */
20598 items = FRAME_MENU_BAR_ITEMS (it.f);
20599 for (i = 0; i < ASIZE (items); i += 4)
20600 {
20601 Lisp_Object string;
20602
20603 /* Stop at nil string. */
20604 string = AREF (items, i + 1);
20605 if (NILP (string))
20606 break;
20607
20608 /* Remember where item was displayed. */
20609 ASET (items, i + 3, make_number (it.hpos));
20610
20611 /* Display the item, pad with one space. */
20612 if (it.current_x < it.last_visible_x)
20613 display_string (NULL, string, Qnil, 0, 0, &it,
20614 SCHARS (string) + 1, 0, 0, -1);
20615 }
20616
20617 /* Fill out the line with spaces. */
20618 if (it.current_x < it.last_visible_x)
20619 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20620
20621 /* Compute the total height of the lines. */
20622 compute_line_metrics (&it);
20623 }
20624
20625
20626 \f
20627 /***********************************************************************
20628 Mode Line
20629 ***********************************************************************/
20630
20631 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20632 FORCE is non-zero, redisplay mode lines unconditionally.
20633 Otherwise, redisplay only mode lines that are garbaged. Value is
20634 the number of windows whose mode lines were redisplayed. */
20635
20636 static int
20637 redisplay_mode_lines (Lisp_Object window, int force)
20638 {
20639 int nwindows = 0;
20640
20641 while (!NILP (window))
20642 {
20643 struct window *w = XWINDOW (window);
20644
20645 if (WINDOWP (w->contents))
20646 nwindows += redisplay_mode_lines (w->contents, force);
20647 else if (force
20648 || FRAME_GARBAGED_P (XFRAME (w->frame))
20649 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20650 {
20651 struct text_pos lpoint;
20652 struct buffer *old = current_buffer;
20653
20654 /* Set the window's buffer for the mode line display. */
20655 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20656 set_buffer_internal_1 (XBUFFER (w->contents));
20657
20658 /* Point refers normally to the selected window. For any
20659 other window, set up appropriate value. */
20660 if (!EQ (window, selected_window))
20661 {
20662 struct text_pos pt;
20663
20664 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20665 if (CHARPOS (pt) < BEGV)
20666 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20667 else if (CHARPOS (pt) > (ZV - 1))
20668 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20669 else
20670 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20671 }
20672
20673 /* Display mode lines. */
20674 clear_glyph_matrix (w->desired_matrix);
20675 if (display_mode_lines (w))
20676 {
20677 ++nwindows;
20678 w->must_be_updated_p = 1;
20679 }
20680
20681 /* Restore old settings. */
20682 set_buffer_internal_1 (old);
20683 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20684 }
20685
20686 window = w->next;
20687 }
20688
20689 return nwindows;
20690 }
20691
20692
20693 /* Display the mode and/or header line of window W. Value is the
20694 sum number of mode lines and header lines displayed. */
20695
20696 static int
20697 display_mode_lines (struct window *w)
20698 {
20699 Lisp_Object old_selected_window = selected_window;
20700 Lisp_Object old_selected_frame = selected_frame;
20701 Lisp_Object new_frame = w->frame;
20702 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20703 int n = 0;
20704
20705 selected_frame = new_frame;
20706 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20707 or window's point, then we'd need select_window_1 here as well. */
20708 XSETWINDOW (selected_window, w);
20709 XFRAME (new_frame)->selected_window = selected_window;
20710
20711 /* These will be set while the mode line specs are processed. */
20712 line_number_displayed = 0;
20713 w->column_number_displayed = -1;
20714
20715 if (WINDOW_WANTS_MODELINE_P (w))
20716 {
20717 struct window *sel_w = XWINDOW (old_selected_window);
20718
20719 /* Select mode line face based on the real selected window. */
20720 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20721 BVAR (current_buffer, mode_line_format));
20722 ++n;
20723 }
20724
20725 if (WINDOW_WANTS_HEADER_LINE_P (w))
20726 {
20727 display_mode_line (w, HEADER_LINE_FACE_ID,
20728 BVAR (current_buffer, header_line_format));
20729 ++n;
20730 }
20731
20732 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20733 selected_frame = old_selected_frame;
20734 selected_window = old_selected_window;
20735 return n;
20736 }
20737
20738
20739 /* Display mode or header line of window W. FACE_ID specifies which
20740 line to display; it is either MODE_LINE_FACE_ID or
20741 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20742 display. Value is the pixel height of the mode/header line
20743 displayed. */
20744
20745 static int
20746 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20747 {
20748 struct it it;
20749 struct face *face;
20750 ptrdiff_t count = SPECPDL_INDEX ();
20751
20752 init_iterator (&it, w, -1, -1, NULL, face_id);
20753 /* Don't extend on a previously drawn mode-line.
20754 This may happen if called from pos_visible_p. */
20755 it.glyph_row->enabled_p = 0;
20756 prepare_desired_row (it.glyph_row);
20757
20758 it.glyph_row->mode_line_p = 1;
20759
20760 /* FIXME: This should be controlled by a user option. But
20761 supporting such an option is not trivial, since the mode line is
20762 made up of many separate strings. */
20763 it.paragraph_embedding = L2R;
20764
20765 record_unwind_protect (unwind_format_mode_line,
20766 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20767
20768 mode_line_target = MODE_LINE_DISPLAY;
20769
20770 /* Temporarily make frame's keyboard the current kboard so that
20771 kboard-local variables in the mode_line_format will get the right
20772 values. */
20773 push_kboard (FRAME_KBOARD (it.f));
20774 record_unwind_save_match_data ();
20775 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20776 pop_kboard ();
20777
20778 unbind_to (count, Qnil);
20779
20780 /* Fill up with spaces. */
20781 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20782
20783 compute_line_metrics (&it);
20784 it.glyph_row->full_width_p = 1;
20785 it.glyph_row->continued_p = 0;
20786 it.glyph_row->truncated_on_left_p = 0;
20787 it.glyph_row->truncated_on_right_p = 0;
20788
20789 /* Make a 3D mode-line have a shadow at its right end. */
20790 face = FACE_FROM_ID (it.f, face_id);
20791 extend_face_to_end_of_line (&it);
20792 if (face->box != FACE_NO_BOX)
20793 {
20794 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20795 + it.glyph_row->used[TEXT_AREA] - 1);
20796 last->right_box_line_p = 1;
20797 }
20798
20799 return it.glyph_row->height;
20800 }
20801
20802 /* Move element ELT in LIST to the front of LIST.
20803 Return the updated list. */
20804
20805 static Lisp_Object
20806 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20807 {
20808 register Lisp_Object tail, prev;
20809 register Lisp_Object tem;
20810
20811 tail = list;
20812 prev = Qnil;
20813 while (CONSP (tail))
20814 {
20815 tem = XCAR (tail);
20816
20817 if (EQ (elt, tem))
20818 {
20819 /* Splice out the link TAIL. */
20820 if (NILP (prev))
20821 list = XCDR (tail);
20822 else
20823 Fsetcdr (prev, XCDR (tail));
20824
20825 /* Now make it the first. */
20826 Fsetcdr (tail, list);
20827 return tail;
20828 }
20829 else
20830 prev = tail;
20831 tail = XCDR (tail);
20832 QUIT;
20833 }
20834
20835 /* Not found--return unchanged LIST. */
20836 return list;
20837 }
20838
20839 /* Contribute ELT to the mode line for window IT->w. How it
20840 translates into text depends on its data type.
20841
20842 IT describes the display environment in which we display, as usual.
20843
20844 DEPTH is the depth in recursion. It is used to prevent
20845 infinite recursion here.
20846
20847 FIELD_WIDTH is the number of characters the display of ELT should
20848 occupy in the mode line, and PRECISION is the maximum number of
20849 characters to display from ELT's representation. See
20850 display_string for details.
20851
20852 Returns the hpos of the end of the text generated by ELT.
20853
20854 PROPS is a property list to add to any string we encounter.
20855
20856 If RISKY is nonzero, remove (disregard) any properties in any string
20857 we encounter, and ignore :eval and :propertize.
20858
20859 The global variable `mode_line_target' determines whether the
20860 output is passed to `store_mode_line_noprop',
20861 `store_mode_line_string', or `display_string'. */
20862
20863 static int
20864 display_mode_element (struct it *it, int depth, int field_width, int precision,
20865 Lisp_Object elt, Lisp_Object props, int risky)
20866 {
20867 int n = 0, field, prec;
20868 int literal = 0;
20869
20870 tail_recurse:
20871 if (depth > 100)
20872 elt = build_string ("*too-deep*");
20873
20874 depth++;
20875
20876 switch (XTYPE (elt))
20877 {
20878 case Lisp_String:
20879 {
20880 /* A string: output it and check for %-constructs within it. */
20881 unsigned char c;
20882 ptrdiff_t offset = 0;
20883
20884 if (SCHARS (elt) > 0
20885 && (!NILP (props) || risky))
20886 {
20887 Lisp_Object oprops, aelt;
20888 oprops = Ftext_properties_at (make_number (0), elt);
20889
20890 /* If the starting string's properties are not what
20891 we want, translate the string. Also, if the string
20892 is risky, do that anyway. */
20893
20894 if (NILP (Fequal (props, oprops)) || risky)
20895 {
20896 /* If the starting string has properties,
20897 merge the specified ones onto the existing ones. */
20898 if (! NILP (oprops) && !risky)
20899 {
20900 Lisp_Object tem;
20901
20902 oprops = Fcopy_sequence (oprops);
20903 tem = props;
20904 while (CONSP (tem))
20905 {
20906 oprops = Fplist_put (oprops, XCAR (tem),
20907 XCAR (XCDR (tem)));
20908 tem = XCDR (XCDR (tem));
20909 }
20910 props = oprops;
20911 }
20912
20913 aelt = Fassoc (elt, mode_line_proptrans_alist);
20914 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20915 {
20916 /* AELT is what we want. Move it to the front
20917 without consing. */
20918 elt = XCAR (aelt);
20919 mode_line_proptrans_alist
20920 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20921 }
20922 else
20923 {
20924 Lisp_Object tem;
20925
20926 /* If AELT has the wrong props, it is useless.
20927 so get rid of it. */
20928 if (! NILP (aelt))
20929 mode_line_proptrans_alist
20930 = Fdelq (aelt, mode_line_proptrans_alist);
20931
20932 elt = Fcopy_sequence (elt);
20933 Fset_text_properties (make_number (0), Flength (elt),
20934 props, elt);
20935 /* Add this item to mode_line_proptrans_alist. */
20936 mode_line_proptrans_alist
20937 = Fcons (Fcons (elt, props),
20938 mode_line_proptrans_alist);
20939 /* Truncate mode_line_proptrans_alist
20940 to at most 50 elements. */
20941 tem = Fnthcdr (make_number (50),
20942 mode_line_proptrans_alist);
20943 if (! NILP (tem))
20944 XSETCDR (tem, Qnil);
20945 }
20946 }
20947 }
20948
20949 offset = 0;
20950
20951 if (literal)
20952 {
20953 prec = precision - n;
20954 switch (mode_line_target)
20955 {
20956 case MODE_LINE_NOPROP:
20957 case MODE_LINE_TITLE:
20958 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20959 break;
20960 case MODE_LINE_STRING:
20961 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20962 break;
20963 case MODE_LINE_DISPLAY:
20964 n += display_string (NULL, elt, Qnil, 0, 0, it,
20965 0, prec, 0, STRING_MULTIBYTE (elt));
20966 break;
20967 }
20968
20969 break;
20970 }
20971
20972 /* Handle the non-literal case. */
20973
20974 while ((precision <= 0 || n < precision)
20975 && SREF (elt, offset) != 0
20976 && (mode_line_target != MODE_LINE_DISPLAY
20977 || it->current_x < it->last_visible_x))
20978 {
20979 ptrdiff_t last_offset = offset;
20980
20981 /* Advance to end of string or next format specifier. */
20982 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20983 ;
20984
20985 if (offset - 1 != last_offset)
20986 {
20987 ptrdiff_t nchars, nbytes;
20988
20989 /* Output to end of string or up to '%'. Field width
20990 is length of string. Don't output more than
20991 PRECISION allows us. */
20992 offset--;
20993
20994 prec = c_string_width (SDATA (elt) + last_offset,
20995 offset - last_offset, precision - n,
20996 &nchars, &nbytes);
20997
20998 switch (mode_line_target)
20999 {
21000 case MODE_LINE_NOPROP:
21001 case MODE_LINE_TITLE:
21002 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21003 break;
21004 case MODE_LINE_STRING:
21005 {
21006 ptrdiff_t bytepos = last_offset;
21007 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21008 ptrdiff_t endpos = (precision <= 0
21009 ? string_byte_to_char (elt, offset)
21010 : charpos + nchars);
21011
21012 n += store_mode_line_string (NULL,
21013 Fsubstring (elt, make_number (charpos),
21014 make_number (endpos)),
21015 0, 0, 0, Qnil);
21016 }
21017 break;
21018 case MODE_LINE_DISPLAY:
21019 {
21020 ptrdiff_t bytepos = last_offset;
21021 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21022
21023 if (precision <= 0)
21024 nchars = string_byte_to_char (elt, offset) - charpos;
21025 n += display_string (NULL, elt, Qnil, 0, charpos,
21026 it, 0, nchars, 0,
21027 STRING_MULTIBYTE (elt));
21028 }
21029 break;
21030 }
21031 }
21032 else /* c == '%' */
21033 {
21034 ptrdiff_t percent_position = offset;
21035
21036 /* Get the specified minimum width. Zero means
21037 don't pad. */
21038 field = 0;
21039 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21040 field = field * 10 + c - '0';
21041
21042 /* Don't pad beyond the total padding allowed. */
21043 if (field_width - n > 0 && field > field_width - n)
21044 field = field_width - n;
21045
21046 /* Note that either PRECISION <= 0 or N < PRECISION. */
21047 prec = precision - n;
21048
21049 if (c == 'M')
21050 n += display_mode_element (it, depth, field, prec,
21051 Vglobal_mode_string, props,
21052 risky);
21053 else if (c != 0)
21054 {
21055 bool multibyte;
21056 ptrdiff_t bytepos, charpos;
21057 const char *spec;
21058 Lisp_Object string;
21059
21060 bytepos = percent_position;
21061 charpos = (STRING_MULTIBYTE (elt)
21062 ? string_byte_to_char (elt, bytepos)
21063 : bytepos);
21064 spec = decode_mode_spec (it->w, c, field, &string);
21065 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21066
21067 switch (mode_line_target)
21068 {
21069 case MODE_LINE_NOPROP:
21070 case MODE_LINE_TITLE:
21071 n += store_mode_line_noprop (spec, field, prec);
21072 break;
21073 case MODE_LINE_STRING:
21074 {
21075 Lisp_Object tem = build_string (spec);
21076 props = Ftext_properties_at (make_number (charpos), elt);
21077 /* Should only keep face property in props */
21078 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21079 }
21080 break;
21081 case MODE_LINE_DISPLAY:
21082 {
21083 int nglyphs_before, nwritten;
21084
21085 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21086 nwritten = display_string (spec, string, elt,
21087 charpos, 0, it,
21088 field, prec, 0,
21089 multibyte);
21090
21091 /* Assign to the glyphs written above the
21092 string where the `%x' came from, position
21093 of the `%'. */
21094 if (nwritten > 0)
21095 {
21096 struct glyph *glyph
21097 = (it->glyph_row->glyphs[TEXT_AREA]
21098 + nglyphs_before);
21099 int i;
21100
21101 for (i = 0; i < nwritten; ++i)
21102 {
21103 glyph[i].object = elt;
21104 glyph[i].charpos = charpos;
21105 }
21106
21107 n += nwritten;
21108 }
21109 }
21110 break;
21111 }
21112 }
21113 else /* c == 0 */
21114 break;
21115 }
21116 }
21117 }
21118 break;
21119
21120 case Lisp_Symbol:
21121 /* A symbol: process the value of the symbol recursively
21122 as if it appeared here directly. Avoid error if symbol void.
21123 Special case: if value of symbol is a string, output the string
21124 literally. */
21125 {
21126 register Lisp_Object tem;
21127
21128 /* If the variable is not marked as risky to set
21129 then its contents are risky to use. */
21130 if (NILP (Fget (elt, Qrisky_local_variable)))
21131 risky = 1;
21132
21133 tem = Fboundp (elt);
21134 if (!NILP (tem))
21135 {
21136 tem = Fsymbol_value (elt);
21137 /* If value is a string, output that string literally:
21138 don't check for % within it. */
21139 if (STRINGP (tem))
21140 literal = 1;
21141
21142 if (!EQ (tem, elt))
21143 {
21144 /* Give up right away for nil or t. */
21145 elt = tem;
21146 goto tail_recurse;
21147 }
21148 }
21149 }
21150 break;
21151
21152 case Lisp_Cons:
21153 {
21154 register Lisp_Object car, tem;
21155
21156 /* A cons cell: five distinct cases.
21157 If first element is :eval or :propertize, do something special.
21158 If first element is a string or a cons, process all the elements
21159 and effectively concatenate them.
21160 If first element is a negative number, truncate displaying cdr to
21161 at most that many characters. If positive, pad (with spaces)
21162 to at least that many characters.
21163 If first element is a symbol, process the cadr or caddr recursively
21164 according to whether the symbol's value is non-nil or nil. */
21165 car = XCAR (elt);
21166 if (EQ (car, QCeval))
21167 {
21168 /* An element of the form (:eval FORM) means evaluate FORM
21169 and use the result as mode line elements. */
21170
21171 if (risky)
21172 break;
21173
21174 if (CONSP (XCDR (elt)))
21175 {
21176 Lisp_Object spec;
21177 spec = safe_eval (XCAR (XCDR (elt)));
21178 n += display_mode_element (it, depth, field_width - n,
21179 precision - n, spec, props,
21180 risky);
21181 }
21182 }
21183 else if (EQ (car, QCpropertize))
21184 {
21185 /* An element of the form (:propertize ELT PROPS...)
21186 means display ELT but applying properties PROPS. */
21187
21188 if (risky)
21189 break;
21190
21191 if (CONSP (XCDR (elt)))
21192 n += display_mode_element (it, depth, field_width - n,
21193 precision - n, XCAR (XCDR (elt)),
21194 XCDR (XCDR (elt)), risky);
21195 }
21196 else if (SYMBOLP (car))
21197 {
21198 tem = Fboundp (car);
21199 elt = XCDR (elt);
21200 if (!CONSP (elt))
21201 goto invalid;
21202 /* elt is now the cdr, and we know it is a cons cell.
21203 Use its car if CAR has a non-nil value. */
21204 if (!NILP (tem))
21205 {
21206 tem = Fsymbol_value (car);
21207 if (!NILP (tem))
21208 {
21209 elt = XCAR (elt);
21210 goto tail_recurse;
21211 }
21212 }
21213 /* Symbol's value is nil (or symbol is unbound)
21214 Get the cddr of the original list
21215 and if possible find the caddr and use that. */
21216 elt = XCDR (elt);
21217 if (NILP (elt))
21218 break;
21219 else if (!CONSP (elt))
21220 goto invalid;
21221 elt = XCAR (elt);
21222 goto tail_recurse;
21223 }
21224 else if (INTEGERP (car))
21225 {
21226 register int lim = XINT (car);
21227 elt = XCDR (elt);
21228 if (lim < 0)
21229 {
21230 /* Negative int means reduce maximum width. */
21231 if (precision <= 0)
21232 precision = -lim;
21233 else
21234 precision = min (precision, -lim);
21235 }
21236 else if (lim > 0)
21237 {
21238 /* Padding specified. Don't let it be more than
21239 current maximum. */
21240 if (precision > 0)
21241 lim = min (precision, lim);
21242
21243 /* If that's more padding than already wanted, queue it.
21244 But don't reduce padding already specified even if
21245 that is beyond the current truncation point. */
21246 field_width = max (lim, field_width);
21247 }
21248 goto tail_recurse;
21249 }
21250 else if (STRINGP (car) || CONSP (car))
21251 {
21252 Lisp_Object halftail = elt;
21253 int len = 0;
21254
21255 while (CONSP (elt)
21256 && (precision <= 0 || n < precision))
21257 {
21258 n += display_mode_element (it, depth,
21259 /* Do padding only after the last
21260 element in the list. */
21261 (! CONSP (XCDR (elt))
21262 ? field_width - n
21263 : 0),
21264 precision - n, XCAR (elt),
21265 props, risky);
21266 elt = XCDR (elt);
21267 len++;
21268 if ((len & 1) == 0)
21269 halftail = XCDR (halftail);
21270 /* Check for cycle. */
21271 if (EQ (halftail, elt))
21272 break;
21273 }
21274 }
21275 }
21276 break;
21277
21278 default:
21279 invalid:
21280 elt = build_string ("*invalid*");
21281 goto tail_recurse;
21282 }
21283
21284 /* Pad to FIELD_WIDTH. */
21285 if (field_width > 0 && n < field_width)
21286 {
21287 switch (mode_line_target)
21288 {
21289 case MODE_LINE_NOPROP:
21290 case MODE_LINE_TITLE:
21291 n += store_mode_line_noprop ("", field_width - n, 0);
21292 break;
21293 case MODE_LINE_STRING:
21294 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21295 break;
21296 case MODE_LINE_DISPLAY:
21297 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21298 0, 0, 0);
21299 break;
21300 }
21301 }
21302
21303 return n;
21304 }
21305
21306 /* Store a mode-line string element in mode_line_string_list.
21307
21308 If STRING is non-null, display that C string. Otherwise, the Lisp
21309 string LISP_STRING is displayed.
21310
21311 FIELD_WIDTH is the minimum number of output glyphs to produce.
21312 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21313 with spaces. FIELD_WIDTH <= 0 means don't pad.
21314
21315 PRECISION is the maximum number of characters to output from
21316 STRING. PRECISION <= 0 means don't truncate the string.
21317
21318 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21319 properties to the string.
21320
21321 PROPS are the properties to add to the string.
21322 The mode_line_string_face face property is always added to the string.
21323 */
21324
21325 static int
21326 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21327 int field_width, int precision, Lisp_Object props)
21328 {
21329 ptrdiff_t len;
21330 int n = 0;
21331
21332 if (string != NULL)
21333 {
21334 len = strlen (string);
21335 if (precision > 0 && len > precision)
21336 len = precision;
21337 lisp_string = make_string (string, len);
21338 if (NILP (props))
21339 props = mode_line_string_face_prop;
21340 else if (!NILP (mode_line_string_face))
21341 {
21342 Lisp_Object face = Fplist_get (props, Qface);
21343 props = Fcopy_sequence (props);
21344 if (NILP (face))
21345 face = mode_line_string_face;
21346 else
21347 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21348 props = Fplist_put (props, Qface, face);
21349 }
21350 Fadd_text_properties (make_number (0), make_number (len),
21351 props, lisp_string);
21352 }
21353 else
21354 {
21355 len = XFASTINT (Flength (lisp_string));
21356 if (precision > 0 && len > precision)
21357 {
21358 len = precision;
21359 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21360 precision = -1;
21361 }
21362 if (!NILP (mode_line_string_face))
21363 {
21364 Lisp_Object face;
21365 if (NILP (props))
21366 props = Ftext_properties_at (make_number (0), lisp_string);
21367 face = Fplist_get (props, Qface);
21368 if (NILP (face))
21369 face = mode_line_string_face;
21370 else
21371 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21372 props = Fcons (Qface, Fcons (face, Qnil));
21373 if (copy_string)
21374 lisp_string = Fcopy_sequence (lisp_string);
21375 }
21376 if (!NILP (props))
21377 Fadd_text_properties (make_number (0), make_number (len),
21378 props, lisp_string);
21379 }
21380
21381 if (len > 0)
21382 {
21383 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21384 n += len;
21385 }
21386
21387 if (field_width > len)
21388 {
21389 field_width -= len;
21390 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21391 if (!NILP (props))
21392 Fadd_text_properties (make_number (0), make_number (field_width),
21393 props, lisp_string);
21394 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21395 n += field_width;
21396 }
21397
21398 return n;
21399 }
21400
21401
21402 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21403 1, 4, 0,
21404 doc: /* Format a string out of a mode line format specification.
21405 First arg FORMAT specifies the mode line format (see `mode-line-format'
21406 for details) to use.
21407
21408 By default, the format is evaluated for the currently selected window.
21409
21410 Optional second arg FACE specifies the face property to put on all
21411 characters for which no face is specified. The value nil means the
21412 default face. The value t means whatever face the window's mode line
21413 currently uses (either `mode-line' or `mode-line-inactive',
21414 depending on whether the window is the selected window or not).
21415 An integer value means the value string has no text
21416 properties.
21417
21418 Optional third and fourth args WINDOW and BUFFER specify the window
21419 and buffer to use as the context for the formatting (defaults
21420 are the selected window and the WINDOW's buffer). */)
21421 (Lisp_Object format, Lisp_Object face,
21422 Lisp_Object window, Lisp_Object buffer)
21423 {
21424 struct it it;
21425 int len;
21426 struct window *w;
21427 struct buffer *old_buffer = NULL;
21428 int face_id;
21429 int no_props = INTEGERP (face);
21430 ptrdiff_t count = SPECPDL_INDEX ();
21431 Lisp_Object str;
21432 int string_start = 0;
21433
21434 w = decode_any_window (window);
21435 XSETWINDOW (window, w);
21436
21437 if (NILP (buffer))
21438 buffer = w->contents;
21439 CHECK_BUFFER (buffer);
21440
21441 /* Make formatting the modeline a non-op when noninteractive, otherwise
21442 there will be problems later caused by a partially initialized frame. */
21443 if (NILP (format) || noninteractive)
21444 return empty_unibyte_string;
21445
21446 if (no_props)
21447 face = Qnil;
21448
21449 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21450 : EQ (face, Qt) ? (EQ (window, selected_window)
21451 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21452 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21453 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21454 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21455 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21456 : DEFAULT_FACE_ID;
21457
21458 old_buffer = current_buffer;
21459
21460 /* Save things including mode_line_proptrans_alist,
21461 and set that to nil so that we don't alter the outer value. */
21462 record_unwind_protect (unwind_format_mode_line,
21463 format_mode_line_unwind_data
21464 (XFRAME (WINDOW_FRAME (w)),
21465 old_buffer, selected_window, 1));
21466 mode_line_proptrans_alist = Qnil;
21467
21468 Fselect_window (window, Qt);
21469 set_buffer_internal_1 (XBUFFER (buffer));
21470
21471 init_iterator (&it, w, -1, -1, NULL, face_id);
21472
21473 if (no_props)
21474 {
21475 mode_line_target = MODE_LINE_NOPROP;
21476 mode_line_string_face_prop = Qnil;
21477 mode_line_string_list = Qnil;
21478 string_start = MODE_LINE_NOPROP_LEN (0);
21479 }
21480 else
21481 {
21482 mode_line_target = MODE_LINE_STRING;
21483 mode_line_string_list = Qnil;
21484 mode_line_string_face = face;
21485 mode_line_string_face_prop
21486 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21487 }
21488
21489 push_kboard (FRAME_KBOARD (it.f));
21490 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21491 pop_kboard ();
21492
21493 if (no_props)
21494 {
21495 len = MODE_LINE_NOPROP_LEN (string_start);
21496 str = make_string (mode_line_noprop_buf + string_start, len);
21497 }
21498 else
21499 {
21500 mode_line_string_list = Fnreverse (mode_line_string_list);
21501 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21502 empty_unibyte_string);
21503 }
21504
21505 unbind_to (count, Qnil);
21506 return str;
21507 }
21508
21509 /* Write a null-terminated, right justified decimal representation of
21510 the positive integer D to BUF using a minimal field width WIDTH. */
21511
21512 static void
21513 pint2str (register char *buf, register int width, register ptrdiff_t d)
21514 {
21515 register char *p = buf;
21516
21517 if (d <= 0)
21518 *p++ = '0';
21519 else
21520 {
21521 while (d > 0)
21522 {
21523 *p++ = d % 10 + '0';
21524 d /= 10;
21525 }
21526 }
21527
21528 for (width -= (int) (p - buf); width > 0; --width)
21529 *p++ = ' ';
21530 *p-- = '\0';
21531 while (p > buf)
21532 {
21533 d = *buf;
21534 *buf++ = *p;
21535 *p-- = d;
21536 }
21537 }
21538
21539 /* Write a null-terminated, right justified decimal and "human
21540 readable" representation of the nonnegative integer D to BUF using
21541 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21542
21543 static const char power_letter[] =
21544 {
21545 0, /* no letter */
21546 'k', /* kilo */
21547 'M', /* mega */
21548 'G', /* giga */
21549 'T', /* tera */
21550 'P', /* peta */
21551 'E', /* exa */
21552 'Z', /* zetta */
21553 'Y' /* yotta */
21554 };
21555
21556 static void
21557 pint2hrstr (char *buf, int width, ptrdiff_t d)
21558 {
21559 /* We aim to represent the nonnegative integer D as
21560 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21561 ptrdiff_t quotient = d;
21562 int remainder = 0;
21563 /* -1 means: do not use TENTHS. */
21564 int tenths = -1;
21565 int exponent = 0;
21566
21567 /* Length of QUOTIENT.TENTHS as a string. */
21568 int length;
21569
21570 char * psuffix;
21571 char * p;
21572
21573 if (quotient >= 1000)
21574 {
21575 /* Scale to the appropriate EXPONENT. */
21576 do
21577 {
21578 remainder = quotient % 1000;
21579 quotient /= 1000;
21580 exponent++;
21581 }
21582 while (quotient >= 1000);
21583
21584 /* Round to nearest and decide whether to use TENTHS or not. */
21585 if (quotient <= 9)
21586 {
21587 tenths = remainder / 100;
21588 if (remainder % 100 >= 50)
21589 {
21590 if (tenths < 9)
21591 tenths++;
21592 else
21593 {
21594 quotient++;
21595 if (quotient == 10)
21596 tenths = -1;
21597 else
21598 tenths = 0;
21599 }
21600 }
21601 }
21602 else
21603 if (remainder >= 500)
21604 {
21605 if (quotient < 999)
21606 quotient++;
21607 else
21608 {
21609 quotient = 1;
21610 exponent++;
21611 tenths = 0;
21612 }
21613 }
21614 }
21615
21616 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21617 if (tenths == -1 && quotient <= 99)
21618 if (quotient <= 9)
21619 length = 1;
21620 else
21621 length = 2;
21622 else
21623 length = 3;
21624 p = psuffix = buf + max (width, length);
21625
21626 /* Print EXPONENT. */
21627 *psuffix++ = power_letter[exponent];
21628 *psuffix = '\0';
21629
21630 /* Print TENTHS. */
21631 if (tenths >= 0)
21632 {
21633 *--p = '0' + tenths;
21634 *--p = '.';
21635 }
21636
21637 /* Print QUOTIENT. */
21638 do
21639 {
21640 int digit = quotient % 10;
21641 *--p = '0' + digit;
21642 }
21643 while ((quotient /= 10) != 0);
21644
21645 /* Print leading spaces. */
21646 while (buf < p)
21647 *--p = ' ';
21648 }
21649
21650 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21651 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21652 type of CODING_SYSTEM. Return updated pointer into BUF. */
21653
21654 static unsigned char invalid_eol_type[] = "(*invalid*)";
21655
21656 static char *
21657 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21658 {
21659 Lisp_Object val;
21660 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21661 const unsigned char *eol_str;
21662 int eol_str_len;
21663 /* The EOL conversion we are using. */
21664 Lisp_Object eoltype;
21665
21666 val = CODING_SYSTEM_SPEC (coding_system);
21667 eoltype = Qnil;
21668
21669 if (!VECTORP (val)) /* Not yet decided. */
21670 {
21671 *buf++ = multibyte ? '-' : ' ';
21672 if (eol_flag)
21673 eoltype = eol_mnemonic_undecided;
21674 /* Don't mention EOL conversion if it isn't decided. */
21675 }
21676 else
21677 {
21678 Lisp_Object attrs;
21679 Lisp_Object eolvalue;
21680
21681 attrs = AREF (val, 0);
21682 eolvalue = AREF (val, 2);
21683
21684 *buf++ = multibyte
21685 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21686 : ' ';
21687
21688 if (eol_flag)
21689 {
21690 /* The EOL conversion that is normal on this system. */
21691
21692 if (NILP (eolvalue)) /* Not yet decided. */
21693 eoltype = eol_mnemonic_undecided;
21694 else if (VECTORP (eolvalue)) /* Not yet decided. */
21695 eoltype = eol_mnemonic_undecided;
21696 else /* eolvalue is Qunix, Qdos, or Qmac. */
21697 eoltype = (EQ (eolvalue, Qunix)
21698 ? eol_mnemonic_unix
21699 : (EQ (eolvalue, Qdos) == 1
21700 ? eol_mnemonic_dos : eol_mnemonic_mac));
21701 }
21702 }
21703
21704 if (eol_flag)
21705 {
21706 /* Mention the EOL conversion if it is not the usual one. */
21707 if (STRINGP (eoltype))
21708 {
21709 eol_str = SDATA (eoltype);
21710 eol_str_len = SBYTES (eoltype);
21711 }
21712 else if (CHARACTERP (eoltype))
21713 {
21714 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21715 int c = XFASTINT (eoltype);
21716 eol_str_len = CHAR_STRING (c, tmp);
21717 eol_str = tmp;
21718 }
21719 else
21720 {
21721 eol_str = invalid_eol_type;
21722 eol_str_len = sizeof (invalid_eol_type) - 1;
21723 }
21724 memcpy (buf, eol_str, eol_str_len);
21725 buf += eol_str_len;
21726 }
21727
21728 return buf;
21729 }
21730
21731 /* Return a string for the output of a mode line %-spec for window W,
21732 generated by character C. FIELD_WIDTH > 0 means pad the string
21733 returned with spaces to that value. Return a Lisp string in
21734 *STRING if the resulting string is taken from that Lisp string.
21735
21736 Note we operate on the current buffer for most purposes. */
21737
21738 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21739
21740 static const char *
21741 decode_mode_spec (struct window *w, register int c, int field_width,
21742 Lisp_Object *string)
21743 {
21744 Lisp_Object obj;
21745 struct frame *f = XFRAME (WINDOW_FRAME (w));
21746 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21747 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21748 produce strings from numerical values, so limit preposterously
21749 large values of FIELD_WIDTH to avoid overrunning the buffer's
21750 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21751 bytes plus the terminating null. */
21752 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21753 struct buffer *b = current_buffer;
21754
21755 obj = Qnil;
21756 *string = Qnil;
21757
21758 switch (c)
21759 {
21760 case '*':
21761 if (!NILP (BVAR (b, read_only)))
21762 return "%";
21763 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21764 return "*";
21765 return "-";
21766
21767 case '+':
21768 /* This differs from %* only for a modified read-only buffer. */
21769 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21770 return "*";
21771 if (!NILP (BVAR (b, read_only)))
21772 return "%";
21773 return "-";
21774
21775 case '&':
21776 /* This differs from %* in ignoring read-only-ness. */
21777 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21778 return "*";
21779 return "-";
21780
21781 case '%':
21782 return "%";
21783
21784 case '[':
21785 {
21786 int i;
21787 char *p;
21788
21789 if (command_loop_level > 5)
21790 return "[[[... ";
21791 p = decode_mode_spec_buf;
21792 for (i = 0; i < command_loop_level; i++)
21793 *p++ = '[';
21794 *p = 0;
21795 return decode_mode_spec_buf;
21796 }
21797
21798 case ']':
21799 {
21800 int i;
21801 char *p;
21802
21803 if (command_loop_level > 5)
21804 return " ...]]]";
21805 p = decode_mode_spec_buf;
21806 for (i = 0; i < command_loop_level; i++)
21807 *p++ = ']';
21808 *p = 0;
21809 return decode_mode_spec_buf;
21810 }
21811
21812 case '-':
21813 {
21814 register int i;
21815
21816 /* Let lots_of_dashes be a string of infinite length. */
21817 if (mode_line_target == MODE_LINE_NOPROP
21818 || mode_line_target == MODE_LINE_STRING)
21819 return "--";
21820 if (field_width <= 0
21821 || field_width > sizeof (lots_of_dashes))
21822 {
21823 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21824 decode_mode_spec_buf[i] = '-';
21825 decode_mode_spec_buf[i] = '\0';
21826 return decode_mode_spec_buf;
21827 }
21828 else
21829 return lots_of_dashes;
21830 }
21831
21832 case 'b':
21833 obj = BVAR (b, name);
21834 break;
21835
21836 case 'c':
21837 /* %c and %l are ignored in `frame-title-format'.
21838 (In redisplay_internal, the frame title is drawn _before_ the
21839 windows are updated, so the stuff which depends on actual
21840 window contents (such as %l) may fail to render properly, or
21841 even crash emacs.) */
21842 if (mode_line_target == MODE_LINE_TITLE)
21843 return "";
21844 else
21845 {
21846 ptrdiff_t col = current_column ();
21847 w->column_number_displayed = col;
21848 pint2str (decode_mode_spec_buf, width, col);
21849 return decode_mode_spec_buf;
21850 }
21851
21852 case 'e':
21853 #ifndef SYSTEM_MALLOC
21854 {
21855 if (NILP (Vmemory_full))
21856 return "";
21857 else
21858 return "!MEM FULL! ";
21859 }
21860 #else
21861 return "";
21862 #endif
21863
21864 case 'F':
21865 /* %F displays the frame name. */
21866 if (!NILP (f->title))
21867 return SSDATA (f->title);
21868 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21869 return SSDATA (f->name);
21870 return "Emacs";
21871
21872 case 'f':
21873 obj = BVAR (b, filename);
21874 break;
21875
21876 case 'i':
21877 {
21878 ptrdiff_t size = ZV - BEGV;
21879 pint2str (decode_mode_spec_buf, width, size);
21880 return decode_mode_spec_buf;
21881 }
21882
21883 case 'I':
21884 {
21885 ptrdiff_t size = ZV - BEGV;
21886 pint2hrstr (decode_mode_spec_buf, width, size);
21887 return decode_mode_spec_buf;
21888 }
21889
21890 case 'l':
21891 {
21892 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21893 ptrdiff_t topline, nlines, height;
21894 ptrdiff_t junk;
21895
21896 /* %c and %l are ignored in `frame-title-format'. */
21897 if (mode_line_target == MODE_LINE_TITLE)
21898 return "";
21899
21900 startpos = marker_position (w->start);
21901 startpos_byte = marker_byte_position (w->start);
21902 height = WINDOW_TOTAL_LINES (w);
21903
21904 /* If we decided that this buffer isn't suitable for line numbers,
21905 don't forget that too fast. */
21906 if (w->base_line_pos == -1)
21907 goto no_value;
21908
21909 /* If the buffer is very big, don't waste time. */
21910 if (INTEGERP (Vline_number_display_limit)
21911 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21912 {
21913 w->base_line_pos = 0;
21914 w->base_line_number = 0;
21915 goto no_value;
21916 }
21917
21918 if (w->base_line_number > 0
21919 && w->base_line_pos > 0
21920 && w->base_line_pos <= startpos)
21921 {
21922 line = w->base_line_number;
21923 linepos = w->base_line_pos;
21924 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21925 }
21926 else
21927 {
21928 line = 1;
21929 linepos = BUF_BEGV (b);
21930 linepos_byte = BUF_BEGV_BYTE (b);
21931 }
21932
21933 /* Count lines from base line to window start position. */
21934 nlines = display_count_lines (linepos_byte,
21935 startpos_byte,
21936 startpos, &junk);
21937
21938 topline = nlines + line;
21939
21940 /* Determine a new base line, if the old one is too close
21941 or too far away, or if we did not have one.
21942 "Too close" means it's plausible a scroll-down would
21943 go back past it. */
21944 if (startpos == BUF_BEGV (b))
21945 {
21946 w->base_line_number = topline;
21947 w->base_line_pos = BUF_BEGV (b);
21948 }
21949 else if (nlines < height + 25 || nlines > height * 3 + 50
21950 || linepos == BUF_BEGV (b))
21951 {
21952 ptrdiff_t limit = BUF_BEGV (b);
21953 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21954 ptrdiff_t position;
21955 ptrdiff_t distance =
21956 (height * 2 + 30) * line_number_display_limit_width;
21957
21958 if (startpos - distance > limit)
21959 {
21960 limit = startpos - distance;
21961 limit_byte = CHAR_TO_BYTE (limit);
21962 }
21963
21964 nlines = display_count_lines (startpos_byte,
21965 limit_byte,
21966 - (height * 2 + 30),
21967 &position);
21968 /* If we couldn't find the lines we wanted within
21969 line_number_display_limit_width chars per line,
21970 give up on line numbers for this window. */
21971 if (position == limit_byte && limit == startpos - distance)
21972 {
21973 w->base_line_pos = -1;
21974 w->base_line_number = 0;
21975 goto no_value;
21976 }
21977
21978 w->base_line_number = topline - nlines;
21979 w->base_line_pos = BYTE_TO_CHAR (position);
21980 }
21981
21982 /* Now count lines from the start pos to point. */
21983 nlines = display_count_lines (startpos_byte,
21984 PT_BYTE, PT, &junk);
21985
21986 /* Record that we did display the line number. */
21987 line_number_displayed = 1;
21988
21989 /* Make the string to show. */
21990 pint2str (decode_mode_spec_buf, width, topline + nlines);
21991 return decode_mode_spec_buf;
21992 no_value:
21993 {
21994 char* p = decode_mode_spec_buf;
21995 int pad = width - 2;
21996 while (pad-- > 0)
21997 *p++ = ' ';
21998 *p++ = '?';
21999 *p++ = '?';
22000 *p = '\0';
22001 return decode_mode_spec_buf;
22002 }
22003 }
22004 break;
22005
22006 case 'm':
22007 obj = BVAR (b, mode_name);
22008 break;
22009
22010 case 'n':
22011 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22012 return " Narrow";
22013 break;
22014
22015 case 'p':
22016 {
22017 ptrdiff_t pos = marker_position (w->start);
22018 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22019
22020 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
22021 {
22022 if (pos <= BUF_BEGV (b))
22023 return "All";
22024 else
22025 return "Bottom";
22026 }
22027 else if (pos <= BUF_BEGV (b))
22028 return "Top";
22029 else
22030 {
22031 if (total > 1000000)
22032 /* Do it differently for a large value, to avoid overflow. */
22033 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22034 else
22035 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22036 /* We can't normally display a 3-digit number,
22037 so get us a 2-digit number that is close. */
22038 if (total == 100)
22039 total = 99;
22040 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22041 return decode_mode_spec_buf;
22042 }
22043 }
22044
22045 /* Display percentage of size above the bottom of the screen. */
22046 case 'P':
22047 {
22048 ptrdiff_t toppos = marker_position (w->start);
22049 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
22050 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22051
22052 if (botpos >= BUF_ZV (b))
22053 {
22054 if (toppos <= BUF_BEGV (b))
22055 return "All";
22056 else
22057 return "Bottom";
22058 }
22059 else
22060 {
22061 if (total > 1000000)
22062 /* Do it differently for a large value, to avoid overflow. */
22063 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22064 else
22065 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22066 /* We can't normally display a 3-digit number,
22067 so get us a 2-digit number that is close. */
22068 if (total == 100)
22069 total = 99;
22070 if (toppos <= BUF_BEGV (b))
22071 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22072 else
22073 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22074 return decode_mode_spec_buf;
22075 }
22076 }
22077
22078 case 's':
22079 /* status of process */
22080 obj = Fget_buffer_process (Fcurrent_buffer ());
22081 if (NILP (obj))
22082 return "no process";
22083 #ifndef MSDOS
22084 obj = Fsymbol_name (Fprocess_status (obj));
22085 #endif
22086 break;
22087
22088 case '@':
22089 {
22090 ptrdiff_t count = inhibit_garbage_collection ();
22091 Lisp_Object val = call1 (intern ("file-remote-p"),
22092 BVAR (current_buffer, directory));
22093 unbind_to (count, Qnil);
22094
22095 if (NILP (val))
22096 return "-";
22097 else
22098 return "@";
22099 }
22100
22101 case 'z':
22102 /* coding-system (not including end-of-line format) */
22103 case 'Z':
22104 /* coding-system (including end-of-line type) */
22105 {
22106 int eol_flag = (c == 'Z');
22107 char *p = decode_mode_spec_buf;
22108
22109 if (! FRAME_WINDOW_P (f))
22110 {
22111 /* No need to mention EOL here--the terminal never needs
22112 to do EOL conversion. */
22113 p = decode_mode_spec_coding (CODING_ID_NAME
22114 (FRAME_KEYBOARD_CODING (f)->id),
22115 p, 0);
22116 p = decode_mode_spec_coding (CODING_ID_NAME
22117 (FRAME_TERMINAL_CODING (f)->id),
22118 p, 0);
22119 }
22120 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22121 p, eol_flag);
22122
22123 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22124 #ifdef subprocesses
22125 obj = Fget_buffer_process (Fcurrent_buffer ());
22126 if (PROCESSP (obj))
22127 {
22128 p = decode_mode_spec_coding
22129 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22130 p = decode_mode_spec_coding
22131 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22132 }
22133 #endif /* subprocesses */
22134 #endif /* 0 */
22135 *p = 0;
22136 return decode_mode_spec_buf;
22137 }
22138 }
22139
22140 if (STRINGP (obj))
22141 {
22142 *string = obj;
22143 return SSDATA (obj);
22144 }
22145 else
22146 return "";
22147 }
22148
22149
22150 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22151 means count lines back from START_BYTE. But don't go beyond
22152 LIMIT_BYTE. Return the number of lines thus found (always
22153 nonnegative).
22154
22155 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22156 either the position COUNT lines after/before START_BYTE, if we
22157 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22158 COUNT lines. */
22159
22160 static ptrdiff_t
22161 display_count_lines (ptrdiff_t start_byte,
22162 ptrdiff_t limit_byte, ptrdiff_t count,
22163 ptrdiff_t *byte_pos_ptr)
22164 {
22165 register unsigned char *cursor;
22166 unsigned char *base;
22167
22168 register ptrdiff_t ceiling;
22169 register unsigned char *ceiling_addr;
22170 ptrdiff_t orig_count = count;
22171
22172 /* If we are not in selective display mode,
22173 check only for newlines. */
22174 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22175 && !INTEGERP (BVAR (current_buffer, selective_display)));
22176
22177 if (count > 0)
22178 {
22179 while (start_byte < limit_byte)
22180 {
22181 ceiling = BUFFER_CEILING_OF (start_byte);
22182 ceiling = min (limit_byte - 1, ceiling);
22183 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22184 base = (cursor = BYTE_POS_ADDR (start_byte));
22185
22186 do
22187 {
22188 if (selective_display)
22189 {
22190 while (*cursor != '\n' && *cursor != 015
22191 && ++cursor != ceiling_addr)
22192 continue;
22193 if (cursor == ceiling_addr)
22194 break;
22195 }
22196 else
22197 {
22198 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22199 if (! cursor)
22200 break;
22201 }
22202
22203 cursor++;
22204
22205 if (--count == 0)
22206 {
22207 start_byte += cursor - base;
22208 *byte_pos_ptr = start_byte;
22209 return orig_count;
22210 }
22211 }
22212 while (cursor < ceiling_addr);
22213
22214 start_byte += ceiling_addr - base;
22215 }
22216 }
22217 else
22218 {
22219 while (start_byte > limit_byte)
22220 {
22221 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22222 ceiling = max (limit_byte, ceiling);
22223 ceiling_addr = BYTE_POS_ADDR (ceiling);
22224 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22225 while (1)
22226 {
22227 if (selective_display)
22228 {
22229 while (--cursor >= ceiling_addr
22230 && *cursor != '\n' && *cursor != 015)
22231 continue;
22232 if (cursor < ceiling_addr)
22233 break;
22234 }
22235 else
22236 {
22237 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22238 if (! cursor)
22239 break;
22240 }
22241
22242 if (++count == 0)
22243 {
22244 start_byte += cursor - base + 1;
22245 *byte_pos_ptr = start_byte;
22246 /* When scanning backwards, we should
22247 not count the newline posterior to which we stop. */
22248 return - orig_count - 1;
22249 }
22250 }
22251 start_byte += ceiling_addr - base;
22252 }
22253 }
22254
22255 *byte_pos_ptr = limit_byte;
22256
22257 if (count < 0)
22258 return - orig_count + count;
22259 return orig_count - count;
22260
22261 }
22262
22263
22264 \f
22265 /***********************************************************************
22266 Displaying strings
22267 ***********************************************************************/
22268
22269 /* Display a NUL-terminated string, starting with index START.
22270
22271 If STRING is non-null, display that C string. Otherwise, the Lisp
22272 string LISP_STRING is displayed. There's a case that STRING is
22273 non-null and LISP_STRING is not nil. It means STRING is a string
22274 data of LISP_STRING. In that case, we display LISP_STRING while
22275 ignoring its text properties.
22276
22277 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22278 FACE_STRING. Display STRING or LISP_STRING with the face at
22279 FACE_STRING_POS in FACE_STRING:
22280
22281 Display the string in the environment given by IT, but use the
22282 standard display table, temporarily.
22283
22284 FIELD_WIDTH is the minimum number of output glyphs to produce.
22285 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22286 with spaces. If STRING has more characters, more than FIELD_WIDTH
22287 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22288
22289 PRECISION is the maximum number of characters to output from
22290 STRING. PRECISION < 0 means don't truncate the string.
22291
22292 This is roughly equivalent to printf format specifiers:
22293
22294 FIELD_WIDTH PRECISION PRINTF
22295 ----------------------------------------
22296 -1 -1 %s
22297 -1 10 %.10s
22298 10 -1 %10s
22299 20 10 %20.10s
22300
22301 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22302 display them, and < 0 means obey the current buffer's value of
22303 enable_multibyte_characters.
22304
22305 Value is the number of columns displayed. */
22306
22307 static int
22308 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22309 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22310 int field_width, int precision, int max_x, int multibyte)
22311 {
22312 int hpos_at_start = it->hpos;
22313 int saved_face_id = it->face_id;
22314 struct glyph_row *row = it->glyph_row;
22315 ptrdiff_t it_charpos;
22316
22317 /* Initialize the iterator IT for iteration over STRING beginning
22318 with index START. */
22319 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22320 precision, field_width, multibyte);
22321 if (string && STRINGP (lisp_string))
22322 /* LISP_STRING is the one returned by decode_mode_spec. We should
22323 ignore its text properties. */
22324 it->stop_charpos = it->end_charpos;
22325
22326 /* If displaying STRING, set up the face of the iterator from
22327 FACE_STRING, if that's given. */
22328 if (STRINGP (face_string))
22329 {
22330 ptrdiff_t endptr;
22331 struct face *face;
22332
22333 it->face_id
22334 = face_at_string_position (it->w, face_string, face_string_pos,
22335 0, it->region_beg_charpos,
22336 it->region_end_charpos,
22337 &endptr, it->base_face_id, 0);
22338 face = FACE_FROM_ID (it->f, it->face_id);
22339 it->face_box_p = face->box != FACE_NO_BOX;
22340 }
22341
22342 /* Set max_x to the maximum allowed X position. Don't let it go
22343 beyond the right edge of the window. */
22344 if (max_x <= 0)
22345 max_x = it->last_visible_x;
22346 else
22347 max_x = min (max_x, it->last_visible_x);
22348
22349 /* Skip over display elements that are not visible. because IT->w is
22350 hscrolled. */
22351 if (it->current_x < it->first_visible_x)
22352 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22353 MOVE_TO_POS | MOVE_TO_X);
22354
22355 row->ascent = it->max_ascent;
22356 row->height = it->max_ascent + it->max_descent;
22357 row->phys_ascent = it->max_phys_ascent;
22358 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22359 row->extra_line_spacing = it->max_extra_line_spacing;
22360
22361 if (STRINGP (it->string))
22362 it_charpos = IT_STRING_CHARPOS (*it);
22363 else
22364 it_charpos = IT_CHARPOS (*it);
22365
22366 /* This condition is for the case that we are called with current_x
22367 past last_visible_x. */
22368 while (it->current_x < max_x)
22369 {
22370 int x_before, x, n_glyphs_before, i, nglyphs;
22371
22372 /* Get the next display element. */
22373 if (!get_next_display_element (it))
22374 break;
22375
22376 /* Produce glyphs. */
22377 x_before = it->current_x;
22378 n_glyphs_before = row->used[TEXT_AREA];
22379 PRODUCE_GLYPHS (it);
22380
22381 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22382 i = 0;
22383 x = x_before;
22384 while (i < nglyphs)
22385 {
22386 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22387
22388 if (it->line_wrap != TRUNCATE
22389 && x + glyph->pixel_width > max_x)
22390 {
22391 /* End of continued line or max_x reached. */
22392 if (CHAR_GLYPH_PADDING_P (*glyph))
22393 {
22394 /* A wide character is unbreakable. */
22395 if (row->reversed_p)
22396 unproduce_glyphs (it, row->used[TEXT_AREA]
22397 - n_glyphs_before);
22398 row->used[TEXT_AREA] = n_glyphs_before;
22399 it->current_x = x_before;
22400 }
22401 else
22402 {
22403 if (row->reversed_p)
22404 unproduce_glyphs (it, row->used[TEXT_AREA]
22405 - (n_glyphs_before + i));
22406 row->used[TEXT_AREA] = n_glyphs_before + i;
22407 it->current_x = x;
22408 }
22409 break;
22410 }
22411 else if (x + glyph->pixel_width >= it->first_visible_x)
22412 {
22413 /* Glyph is at least partially visible. */
22414 ++it->hpos;
22415 if (x < it->first_visible_x)
22416 row->x = x - it->first_visible_x;
22417 }
22418 else
22419 {
22420 /* Glyph is off the left margin of the display area.
22421 Should not happen. */
22422 emacs_abort ();
22423 }
22424
22425 row->ascent = max (row->ascent, it->max_ascent);
22426 row->height = max (row->height, it->max_ascent + it->max_descent);
22427 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22428 row->phys_height = max (row->phys_height,
22429 it->max_phys_ascent + it->max_phys_descent);
22430 row->extra_line_spacing = max (row->extra_line_spacing,
22431 it->max_extra_line_spacing);
22432 x += glyph->pixel_width;
22433 ++i;
22434 }
22435
22436 /* Stop if max_x reached. */
22437 if (i < nglyphs)
22438 break;
22439
22440 /* Stop at line ends. */
22441 if (ITERATOR_AT_END_OF_LINE_P (it))
22442 {
22443 it->continuation_lines_width = 0;
22444 break;
22445 }
22446
22447 set_iterator_to_next (it, 1);
22448 if (STRINGP (it->string))
22449 it_charpos = IT_STRING_CHARPOS (*it);
22450 else
22451 it_charpos = IT_CHARPOS (*it);
22452
22453 /* Stop if truncating at the right edge. */
22454 if (it->line_wrap == TRUNCATE
22455 && it->current_x >= it->last_visible_x)
22456 {
22457 /* Add truncation mark, but don't do it if the line is
22458 truncated at a padding space. */
22459 if (it_charpos < it->string_nchars)
22460 {
22461 if (!FRAME_WINDOW_P (it->f))
22462 {
22463 int ii, n;
22464
22465 if (it->current_x > it->last_visible_x)
22466 {
22467 if (!row->reversed_p)
22468 {
22469 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22470 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22471 break;
22472 }
22473 else
22474 {
22475 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22476 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22477 break;
22478 unproduce_glyphs (it, ii + 1);
22479 ii = row->used[TEXT_AREA] - (ii + 1);
22480 }
22481 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22482 {
22483 row->used[TEXT_AREA] = ii;
22484 produce_special_glyphs (it, IT_TRUNCATION);
22485 }
22486 }
22487 produce_special_glyphs (it, IT_TRUNCATION);
22488 }
22489 row->truncated_on_right_p = 1;
22490 }
22491 break;
22492 }
22493 }
22494
22495 /* Maybe insert a truncation at the left. */
22496 if (it->first_visible_x
22497 && it_charpos > 0)
22498 {
22499 if (!FRAME_WINDOW_P (it->f)
22500 || (row->reversed_p
22501 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22502 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22503 insert_left_trunc_glyphs (it);
22504 row->truncated_on_left_p = 1;
22505 }
22506
22507 it->face_id = saved_face_id;
22508
22509 /* Value is number of columns displayed. */
22510 return it->hpos - hpos_at_start;
22511 }
22512
22513
22514 \f
22515 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22516 appears as an element of LIST or as the car of an element of LIST.
22517 If PROPVAL is a list, compare each element against LIST in that
22518 way, and return 1/2 if any element of PROPVAL is found in LIST.
22519 Otherwise return 0. This function cannot quit.
22520 The return value is 2 if the text is invisible but with an ellipsis
22521 and 1 if it's invisible and without an ellipsis. */
22522
22523 int
22524 invisible_p (register Lisp_Object propval, Lisp_Object list)
22525 {
22526 register Lisp_Object tail, proptail;
22527
22528 for (tail = list; CONSP (tail); tail = XCDR (tail))
22529 {
22530 register Lisp_Object tem;
22531 tem = XCAR (tail);
22532 if (EQ (propval, tem))
22533 return 1;
22534 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22535 return NILP (XCDR (tem)) ? 1 : 2;
22536 }
22537
22538 if (CONSP (propval))
22539 {
22540 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22541 {
22542 Lisp_Object propelt;
22543 propelt = XCAR (proptail);
22544 for (tail = list; CONSP (tail); tail = XCDR (tail))
22545 {
22546 register Lisp_Object tem;
22547 tem = XCAR (tail);
22548 if (EQ (propelt, tem))
22549 return 1;
22550 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22551 return NILP (XCDR (tem)) ? 1 : 2;
22552 }
22553 }
22554 }
22555
22556 return 0;
22557 }
22558
22559 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22560 doc: /* Non-nil if the property makes the text invisible.
22561 POS-OR-PROP can be a marker or number, in which case it is taken to be
22562 a position in the current buffer and the value of the `invisible' property
22563 is checked; or it can be some other value, which is then presumed to be the
22564 value of the `invisible' property of the text of interest.
22565 The non-nil value returned can be t for truly invisible text or something
22566 else if the text is replaced by an ellipsis. */)
22567 (Lisp_Object pos_or_prop)
22568 {
22569 Lisp_Object prop
22570 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22571 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22572 : pos_or_prop);
22573 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22574 return (invis == 0 ? Qnil
22575 : invis == 1 ? Qt
22576 : make_number (invis));
22577 }
22578
22579 /* Calculate a width or height in pixels from a specification using
22580 the following elements:
22581
22582 SPEC ::=
22583 NUM - a (fractional) multiple of the default font width/height
22584 (NUM) - specifies exactly NUM pixels
22585 UNIT - a fixed number of pixels, see below.
22586 ELEMENT - size of a display element in pixels, see below.
22587 (NUM . SPEC) - equals NUM * SPEC
22588 (+ SPEC SPEC ...) - add pixel values
22589 (- SPEC SPEC ...) - subtract pixel values
22590 (- SPEC) - negate pixel value
22591
22592 NUM ::=
22593 INT or FLOAT - a number constant
22594 SYMBOL - use symbol's (buffer local) variable binding.
22595
22596 UNIT ::=
22597 in - pixels per inch *)
22598 mm - pixels per 1/1000 meter *)
22599 cm - pixels per 1/100 meter *)
22600 width - width of current font in pixels.
22601 height - height of current font in pixels.
22602
22603 *) using the ratio(s) defined in display-pixels-per-inch.
22604
22605 ELEMENT ::=
22606
22607 left-fringe - left fringe width in pixels
22608 right-fringe - right fringe width in pixels
22609
22610 left-margin - left margin width in pixels
22611 right-margin - right margin width in pixels
22612
22613 scroll-bar - scroll-bar area width in pixels
22614
22615 Examples:
22616
22617 Pixels corresponding to 5 inches:
22618 (5 . in)
22619
22620 Total width of non-text areas on left side of window (if scroll-bar is on left):
22621 '(space :width (+ left-fringe left-margin scroll-bar))
22622
22623 Align to first text column (in header line):
22624 '(space :align-to 0)
22625
22626 Align to middle of text area minus half the width of variable `my-image'
22627 containing a loaded image:
22628 '(space :align-to (0.5 . (- text my-image)))
22629
22630 Width of left margin minus width of 1 character in the default font:
22631 '(space :width (- left-margin 1))
22632
22633 Width of left margin minus width of 2 characters in the current font:
22634 '(space :width (- left-margin (2 . width)))
22635
22636 Center 1 character over left-margin (in header line):
22637 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22638
22639 Different ways to express width of left fringe plus left margin minus one pixel:
22640 '(space :width (- (+ left-fringe left-margin) (1)))
22641 '(space :width (+ left-fringe left-margin (- (1))))
22642 '(space :width (+ left-fringe left-margin (-1)))
22643
22644 */
22645
22646 static int
22647 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22648 struct font *font, int width_p, int *align_to)
22649 {
22650 double pixels;
22651
22652 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22653 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22654
22655 if (NILP (prop))
22656 return OK_PIXELS (0);
22657
22658 eassert (FRAME_LIVE_P (it->f));
22659
22660 if (SYMBOLP (prop))
22661 {
22662 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22663 {
22664 char *unit = SSDATA (SYMBOL_NAME (prop));
22665
22666 if (unit[0] == 'i' && unit[1] == 'n')
22667 pixels = 1.0;
22668 else if (unit[0] == 'm' && unit[1] == 'm')
22669 pixels = 25.4;
22670 else if (unit[0] == 'c' && unit[1] == 'm')
22671 pixels = 2.54;
22672 else
22673 pixels = 0;
22674 if (pixels > 0)
22675 {
22676 double ppi = (width_p ? FRAME_RES_X (it->f)
22677 : FRAME_RES_Y (it->f));
22678
22679 if (ppi > 0)
22680 return OK_PIXELS (ppi / pixels);
22681 return 0;
22682 }
22683 }
22684
22685 #ifdef HAVE_WINDOW_SYSTEM
22686 if (EQ (prop, Qheight))
22687 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22688 if (EQ (prop, Qwidth))
22689 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22690 #else
22691 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22692 return OK_PIXELS (1);
22693 #endif
22694
22695 if (EQ (prop, Qtext))
22696 return OK_PIXELS (width_p
22697 ? window_box_width (it->w, TEXT_AREA)
22698 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22699
22700 if (align_to && *align_to < 0)
22701 {
22702 *res = 0;
22703 if (EQ (prop, Qleft))
22704 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22705 if (EQ (prop, Qright))
22706 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22707 if (EQ (prop, Qcenter))
22708 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22709 + window_box_width (it->w, TEXT_AREA) / 2);
22710 if (EQ (prop, Qleft_fringe))
22711 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22712 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22713 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22714 if (EQ (prop, Qright_fringe))
22715 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22716 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22717 : window_box_right_offset (it->w, TEXT_AREA));
22718 if (EQ (prop, Qleft_margin))
22719 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22720 if (EQ (prop, Qright_margin))
22721 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22722 if (EQ (prop, Qscroll_bar))
22723 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22724 ? 0
22725 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22726 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22727 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22728 : 0)));
22729 }
22730 else
22731 {
22732 if (EQ (prop, Qleft_fringe))
22733 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22734 if (EQ (prop, Qright_fringe))
22735 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22736 if (EQ (prop, Qleft_margin))
22737 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22738 if (EQ (prop, Qright_margin))
22739 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22740 if (EQ (prop, Qscroll_bar))
22741 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22742 }
22743
22744 prop = buffer_local_value_1 (prop, it->w->contents);
22745 if (EQ (prop, Qunbound))
22746 prop = Qnil;
22747 }
22748
22749 if (INTEGERP (prop) || FLOATP (prop))
22750 {
22751 int base_unit = (width_p
22752 ? FRAME_COLUMN_WIDTH (it->f)
22753 : FRAME_LINE_HEIGHT (it->f));
22754 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22755 }
22756
22757 if (CONSP (prop))
22758 {
22759 Lisp_Object car = XCAR (prop);
22760 Lisp_Object cdr = XCDR (prop);
22761
22762 if (SYMBOLP (car))
22763 {
22764 #ifdef HAVE_WINDOW_SYSTEM
22765 if (FRAME_WINDOW_P (it->f)
22766 && valid_image_p (prop))
22767 {
22768 ptrdiff_t id = lookup_image (it->f, prop);
22769 struct image *img = IMAGE_FROM_ID (it->f, id);
22770
22771 return OK_PIXELS (width_p ? img->width : img->height);
22772 }
22773 #endif
22774 if (EQ (car, Qplus) || EQ (car, Qminus))
22775 {
22776 int first = 1;
22777 double px;
22778
22779 pixels = 0;
22780 while (CONSP (cdr))
22781 {
22782 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22783 font, width_p, align_to))
22784 return 0;
22785 if (first)
22786 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22787 else
22788 pixels += px;
22789 cdr = XCDR (cdr);
22790 }
22791 if (EQ (car, Qminus))
22792 pixels = -pixels;
22793 return OK_PIXELS (pixels);
22794 }
22795
22796 car = buffer_local_value_1 (car, it->w->contents);
22797 if (EQ (car, Qunbound))
22798 car = Qnil;
22799 }
22800
22801 if (INTEGERP (car) || FLOATP (car))
22802 {
22803 double fact;
22804 pixels = XFLOATINT (car);
22805 if (NILP (cdr))
22806 return OK_PIXELS (pixels);
22807 if (calc_pixel_width_or_height (&fact, it, cdr,
22808 font, width_p, align_to))
22809 return OK_PIXELS (pixels * fact);
22810 return 0;
22811 }
22812
22813 return 0;
22814 }
22815
22816 return 0;
22817 }
22818
22819 \f
22820 /***********************************************************************
22821 Glyph Display
22822 ***********************************************************************/
22823
22824 #ifdef HAVE_WINDOW_SYSTEM
22825
22826 #ifdef GLYPH_DEBUG
22827
22828 void
22829 dump_glyph_string (struct glyph_string *s)
22830 {
22831 fprintf (stderr, "glyph string\n");
22832 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22833 s->x, s->y, s->width, s->height);
22834 fprintf (stderr, " ybase = %d\n", s->ybase);
22835 fprintf (stderr, " hl = %d\n", s->hl);
22836 fprintf (stderr, " left overhang = %d, right = %d\n",
22837 s->left_overhang, s->right_overhang);
22838 fprintf (stderr, " nchars = %d\n", s->nchars);
22839 fprintf (stderr, " extends to end of line = %d\n",
22840 s->extends_to_end_of_line_p);
22841 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22842 fprintf (stderr, " bg width = %d\n", s->background_width);
22843 }
22844
22845 #endif /* GLYPH_DEBUG */
22846
22847 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22848 of XChar2b structures for S; it can't be allocated in
22849 init_glyph_string because it must be allocated via `alloca'. W
22850 is the window on which S is drawn. ROW and AREA are the glyph row
22851 and area within the row from which S is constructed. START is the
22852 index of the first glyph structure covered by S. HL is a
22853 face-override for drawing S. */
22854
22855 #ifdef HAVE_NTGUI
22856 #define OPTIONAL_HDC(hdc) HDC hdc,
22857 #define DECLARE_HDC(hdc) HDC hdc;
22858 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22859 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22860 #endif
22861
22862 #ifndef OPTIONAL_HDC
22863 #define OPTIONAL_HDC(hdc)
22864 #define DECLARE_HDC(hdc)
22865 #define ALLOCATE_HDC(hdc, f)
22866 #define RELEASE_HDC(hdc, f)
22867 #endif
22868
22869 static void
22870 init_glyph_string (struct glyph_string *s,
22871 OPTIONAL_HDC (hdc)
22872 XChar2b *char2b, struct window *w, struct glyph_row *row,
22873 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22874 {
22875 memset (s, 0, sizeof *s);
22876 s->w = w;
22877 s->f = XFRAME (w->frame);
22878 #ifdef HAVE_NTGUI
22879 s->hdc = hdc;
22880 #endif
22881 s->display = FRAME_X_DISPLAY (s->f);
22882 s->window = FRAME_X_WINDOW (s->f);
22883 s->char2b = char2b;
22884 s->hl = hl;
22885 s->row = row;
22886 s->area = area;
22887 s->first_glyph = row->glyphs[area] + start;
22888 s->height = row->height;
22889 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22890 s->ybase = s->y + row->ascent;
22891 }
22892
22893
22894 /* Append the list of glyph strings with head H and tail T to the list
22895 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22896
22897 static void
22898 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22899 struct glyph_string *h, struct glyph_string *t)
22900 {
22901 if (h)
22902 {
22903 if (*head)
22904 (*tail)->next = h;
22905 else
22906 *head = h;
22907 h->prev = *tail;
22908 *tail = t;
22909 }
22910 }
22911
22912
22913 /* Prepend the list of glyph strings with head H and tail T to the
22914 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22915 result. */
22916
22917 static void
22918 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22919 struct glyph_string *h, struct glyph_string *t)
22920 {
22921 if (h)
22922 {
22923 if (*head)
22924 (*head)->prev = t;
22925 else
22926 *tail = t;
22927 t->next = *head;
22928 *head = h;
22929 }
22930 }
22931
22932
22933 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22934 Set *HEAD and *TAIL to the resulting list. */
22935
22936 static void
22937 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22938 struct glyph_string *s)
22939 {
22940 s->next = s->prev = NULL;
22941 append_glyph_string_lists (head, tail, s, s);
22942 }
22943
22944
22945 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22946 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22947 make sure that X resources for the face returned are allocated.
22948 Value is a pointer to a realized face that is ready for display if
22949 DISPLAY_P is non-zero. */
22950
22951 static struct face *
22952 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22953 XChar2b *char2b, int display_p)
22954 {
22955 struct face *face = FACE_FROM_ID (f, face_id);
22956 unsigned code = 0;
22957
22958 if (face->font)
22959 {
22960 code = face->font->driver->encode_char (face->font, c);
22961
22962 if (code == FONT_INVALID_CODE)
22963 code = 0;
22964 }
22965 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22966
22967 /* Make sure X resources of the face are allocated. */
22968 #ifdef HAVE_X_WINDOWS
22969 if (display_p)
22970 #endif
22971 {
22972 eassert (face != NULL);
22973 PREPARE_FACE_FOR_DISPLAY (f, face);
22974 }
22975
22976 return face;
22977 }
22978
22979
22980 /* Get face and two-byte form of character glyph GLYPH on frame F.
22981 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22982 a pointer to a realized face that is ready for display. */
22983
22984 static struct face *
22985 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22986 XChar2b *char2b, int *two_byte_p)
22987 {
22988 struct face *face;
22989 unsigned code = 0;
22990
22991 eassert (glyph->type == CHAR_GLYPH);
22992 face = FACE_FROM_ID (f, glyph->face_id);
22993
22994 /* Make sure X resources of the face are allocated. */
22995 eassert (face != NULL);
22996 PREPARE_FACE_FOR_DISPLAY (f, face);
22997
22998 if (two_byte_p)
22999 *two_byte_p = 0;
23000
23001 if (face->font)
23002 {
23003 if (CHAR_BYTE8_P (glyph->u.ch))
23004 code = CHAR_TO_BYTE8 (glyph->u.ch);
23005 else
23006 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23007
23008 if (code == FONT_INVALID_CODE)
23009 code = 0;
23010 }
23011
23012 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23013 return face;
23014 }
23015
23016
23017 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23018 Return 1 if FONT has a glyph for C, otherwise return 0. */
23019
23020 static int
23021 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23022 {
23023 unsigned code;
23024
23025 if (CHAR_BYTE8_P (c))
23026 code = CHAR_TO_BYTE8 (c);
23027 else
23028 code = font->driver->encode_char (font, c);
23029
23030 if (code == FONT_INVALID_CODE)
23031 return 0;
23032 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23033 return 1;
23034 }
23035
23036
23037 /* Fill glyph string S with composition components specified by S->cmp.
23038
23039 BASE_FACE is the base face of the composition.
23040 S->cmp_from is the index of the first component for S.
23041
23042 OVERLAPS non-zero means S should draw the foreground only, and use
23043 its physical height for clipping. See also draw_glyphs.
23044
23045 Value is the index of a component not in S. */
23046
23047 static int
23048 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23049 int overlaps)
23050 {
23051 int i;
23052 /* For all glyphs of this composition, starting at the offset
23053 S->cmp_from, until we reach the end of the definition or encounter a
23054 glyph that requires the different face, add it to S. */
23055 struct face *face;
23056
23057 eassert (s);
23058
23059 s->for_overlaps = overlaps;
23060 s->face = NULL;
23061 s->font = NULL;
23062 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23063 {
23064 int c = COMPOSITION_GLYPH (s->cmp, i);
23065
23066 /* TAB in a composition means display glyphs with padding space
23067 on the left or right. */
23068 if (c != '\t')
23069 {
23070 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23071 -1, Qnil);
23072
23073 face = get_char_face_and_encoding (s->f, c, face_id,
23074 s->char2b + i, 1);
23075 if (face)
23076 {
23077 if (! s->face)
23078 {
23079 s->face = face;
23080 s->font = s->face->font;
23081 }
23082 else if (s->face != face)
23083 break;
23084 }
23085 }
23086 ++s->nchars;
23087 }
23088 s->cmp_to = i;
23089
23090 if (s->face == NULL)
23091 {
23092 s->face = base_face->ascii_face;
23093 s->font = s->face->font;
23094 }
23095
23096 /* All glyph strings for the same composition has the same width,
23097 i.e. the width set for the first component of the composition. */
23098 s->width = s->first_glyph->pixel_width;
23099
23100 /* If the specified font could not be loaded, use the frame's
23101 default font, but record the fact that we couldn't load it in
23102 the glyph string so that we can draw rectangles for the
23103 characters of the glyph string. */
23104 if (s->font == NULL)
23105 {
23106 s->font_not_found_p = 1;
23107 s->font = FRAME_FONT (s->f);
23108 }
23109
23110 /* Adjust base line for subscript/superscript text. */
23111 s->ybase += s->first_glyph->voffset;
23112
23113 /* This glyph string must always be drawn with 16-bit functions. */
23114 s->two_byte_p = 1;
23115
23116 return s->cmp_to;
23117 }
23118
23119 static int
23120 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23121 int start, int end, int overlaps)
23122 {
23123 struct glyph *glyph, *last;
23124 Lisp_Object lgstring;
23125 int i;
23126
23127 s->for_overlaps = overlaps;
23128 glyph = s->row->glyphs[s->area] + start;
23129 last = s->row->glyphs[s->area] + end;
23130 s->cmp_id = glyph->u.cmp.id;
23131 s->cmp_from = glyph->slice.cmp.from;
23132 s->cmp_to = glyph->slice.cmp.to + 1;
23133 s->face = FACE_FROM_ID (s->f, face_id);
23134 lgstring = composition_gstring_from_id (s->cmp_id);
23135 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23136 glyph++;
23137 while (glyph < last
23138 && glyph->u.cmp.automatic
23139 && glyph->u.cmp.id == s->cmp_id
23140 && s->cmp_to == glyph->slice.cmp.from)
23141 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23142
23143 for (i = s->cmp_from; i < s->cmp_to; i++)
23144 {
23145 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23146 unsigned code = LGLYPH_CODE (lglyph);
23147
23148 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23149 }
23150 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23151 return glyph - s->row->glyphs[s->area];
23152 }
23153
23154
23155 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23156 See the comment of fill_glyph_string for arguments.
23157 Value is the index of the first glyph not in S. */
23158
23159
23160 static int
23161 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23162 int start, int end, int overlaps)
23163 {
23164 struct glyph *glyph, *last;
23165 int voffset;
23166
23167 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23168 s->for_overlaps = overlaps;
23169 glyph = s->row->glyphs[s->area] + start;
23170 last = s->row->glyphs[s->area] + end;
23171 voffset = glyph->voffset;
23172 s->face = FACE_FROM_ID (s->f, face_id);
23173 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23174 s->nchars = 1;
23175 s->width = glyph->pixel_width;
23176 glyph++;
23177 while (glyph < last
23178 && glyph->type == GLYPHLESS_GLYPH
23179 && glyph->voffset == voffset
23180 && glyph->face_id == face_id)
23181 {
23182 s->nchars++;
23183 s->width += glyph->pixel_width;
23184 glyph++;
23185 }
23186 s->ybase += voffset;
23187 return glyph - s->row->glyphs[s->area];
23188 }
23189
23190
23191 /* Fill glyph string S from a sequence of character glyphs.
23192
23193 FACE_ID is the face id of the string. START is the index of the
23194 first glyph to consider, END is the index of the last + 1.
23195 OVERLAPS non-zero means S should draw the foreground only, and use
23196 its physical height for clipping. See also draw_glyphs.
23197
23198 Value is the index of the first glyph not in S. */
23199
23200 static int
23201 fill_glyph_string (struct glyph_string *s, int face_id,
23202 int start, int end, int overlaps)
23203 {
23204 struct glyph *glyph, *last;
23205 int voffset;
23206 int glyph_not_available_p;
23207
23208 eassert (s->f == XFRAME (s->w->frame));
23209 eassert (s->nchars == 0);
23210 eassert (start >= 0 && end > start);
23211
23212 s->for_overlaps = overlaps;
23213 glyph = s->row->glyphs[s->area] + start;
23214 last = s->row->glyphs[s->area] + end;
23215 voffset = glyph->voffset;
23216 s->padding_p = glyph->padding_p;
23217 glyph_not_available_p = glyph->glyph_not_available_p;
23218
23219 while (glyph < last
23220 && glyph->type == CHAR_GLYPH
23221 && glyph->voffset == voffset
23222 /* Same face id implies same font, nowadays. */
23223 && glyph->face_id == face_id
23224 && glyph->glyph_not_available_p == glyph_not_available_p)
23225 {
23226 int two_byte_p;
23227
23228 s->face = get_glyph_face_and_encoding (s->f, glyph,
23229 s->char2b + s->nchars,
23230 &two_byte_p);
23231 s->two_byte_p = two_byte_p;
23232 ++s->nchars;
23233 eassert (s->nchars <= end - start);
23234 s->width += glyph->pixel_width;
23235 if (glyph++->padding_p != s->padding_p)
23236 break;
23237 }
23238
23239 s->font = s->face->font;
23240
23241 /* If the specified font could not be loaded, use the frame's font,
23242 but record the fact that we couldn't load it in
23243 S->font_not_found_p so that we can draw rectangles for the
23244 characters of the glyph string. */
23245 if (s->font == NULL || glyph_not_available_p)
23246 {
23247 s->font_not_found_p = 1;
23248 s->font = FRAME_FONT (s->f);
23249 }
23250
23251 /* Adjust base line for subscript/superscript text. */
23252 s->ybase += voffset;
23253
23254 eassert (s->face && s->face->gc);
23255 return glyph - s->row->glyphs[s->area];
23256 }
23257
23258
23259 /* Fill glyph string S from image glyph S->first_glyph. */
23260
23261 static void
23262 fill_image_glyph_string (struct glyph_string *s)
23263 {
23264 eassert (s->first_glyph->type == IMAGE_GLYPH);
23265 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23266 eassert (s->img);
23267 s->slice = s->first_glyph->slice.img;
23268 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23269 s->font = s->face->font;
23270 s->width = s->first_glyph->pixel_width;
23271
23272 /* Adjust base line for subscript/superscript text. */
23273 s->ybase += s->first_glyph->voffset;
23274 }
23275
23276
23277 /* Fill glyph string S from a sequence of stretch glyphs.
23278
23279 START is the index of the first glyph to consider,
23280 END is the index of the last + 1.
23281
23282 Value is the index of the first glyph not in S. */
23283
23284 static int
23285 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23286 {
23287 struct glyph *glyph, *last;
23288 int voffset, face_id;
23289
23290 eassert (s->first_glyph->type == STRETCH_GLYPH);
23291
23292 glyph = s->row->glyphs[s->area] + start;
23293 last = s->row->glyphs[s->area] + end;
23294 face_id = glyph->face_id;
23295 s->face = FACE_FROM_ID (s->f, face_id);
23296 s->font = s->face->font;
23297 s->width = glyph->pixel_width;
23298 s->nchars = 1;
23299 voffset = glyph->voffset;
23300
23301 for (++glyph;
23302 (glyph < last
23303 && glyph->type == STRETCH_GLYPH
23304 && glyph->voffset == voffset
23305 && glyph->face_id == face_id);
23306 ++glyph)
23307 s->width += glyph->pixel_width;
23308
23309 /* Adjust base line for subscript/superscript text. */
23310 s->ybase += voffset;
23311
23312 /* The case that face->gc == 0 is handled when drawing the glyph
23313 string by calling PREPARE_FACE_FOR_DISPLAY. */
23314 eassert (s->face);
23315 return glyph - s->row->glyphs[s->area];
23316 }
23317
23318 static struct font_metrics *
23319 get_per_char_metric (struct font *font, XChar2b *char2b)
23320 {
23321 static struct font_metrics metrics;
23322 unsigned code;
23323
23324 if (! font)
23325 return NULL;
23326 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23327 if (code == FONT_INVALID_CODE)
23328 return NULL;
23329 font->driver->text_extents (font, &code, 1, &metrics);
23330 return &metrics;
23331 }
23332
23333 /* EXPORT for RIF:
23334 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23335 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23336 assumed to be zero. */
23337
23338 void
23339 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23340 {
23341 *left = *right = 0;
23342
23343 if (glyph->type == CHAR_GLYPH)
23344 {
23345 struct face *face;
23346 XChar2b char2b;
23347 struct font_metrics *pcm;
23348
23349 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23350 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23351 {
23352 if (pcm->rbearing > pcm->width)
23353 *right = pcm->rbearing - pcm->width;
23354 if (pcm->lbearing < 0)
23355 *left = -pcm->lbearing;
23356 }
23357 }
23358 else if (glyph->type == COMPOSITE_GLYPH)
23359 {
23360 if (! glyph->u.cmp.automatic)
23361 {
23362 struct composition *cmp = composition_table[glyph->u.cmp.id];
23363
23364 if (cmp->rbearing > cmp->pixel_width)
23365 *right = cmp->rbearing - cmp->pixel_width;
23366 if (cmp->lbearing < 0)
23367 *left = - cmp->lbearing;
23368 }
23369 else
23370 {
23371 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23372 struct font_metrics metrics;
23373
23374 composition_gstring_width (gstring, glyph->slice.cmp.from,
23375 glyph->slice.cmp.to + 1, &metrics);
23376 if (metrics.rbearing > metrics.width)
23377 *right = metrics.rbearing - metrics.width;
23378 if (metrics.lbearing < 0)
23379 *left = - metrics.lbearing;
23380 }
23381 }
23382 }
23383
23384
23385 /* Return the index of the first glyph preceding glyph string S that
23386 is overwritten by S because of S's left overhang. Value is -1
23387 if no glyphs are overwritten. */
23388
23389 static int
23390 left_overwritten (struct glyph_string *s)
23391 {
23392 int k;
23393
23394 if (s->left_overhang)
23395 {
23396 int x = 0, i;
23397 struct glyph *glyphs = s->row->glyphs[s->area];
23398 int first = s->first_glyph - glyphs;
23399
23400 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23401 x -= glyphs[i].pixel_width;
23402
23403 k = i + 1;
23404 }
23405 else
23406 k = -1;
23407
23408 return k;
23409 }
23410
23411
23412 /* Return the index of the first glyph preceding glyph string S that
23413 is overwriting S because of its right overhang. Value is -1 if no
23414 glyph in front of S overwrites S. */
23415
23416 static int
23417 left_overwriting (struct glyph_string *s)
23418 {
23419 int i, k, x;
23420 struct glyph *glyphs = s->row->glyphs[s->area];
23421 int first = s->first_glyph - glyphs;
23422
23423 k = -1;
23424 x = 0;
23425 for (i = first - 1; i >= 0; --i)
23426 {
23427 int left, right;
23428 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23429 if (x + right > 0)
23430 k = i;
23431 x -= glyphs[i].pixel_width;
23432 }
23433
23434 return k;
23435 }
23436
23437
23438 /* Return the index of the last glyph following glyph string S that is
23439 overwritten by S because of S's right overhang. Value is -1 if
23440 no such glyph is found. */
23441
23442 static int
23443 right_overwritten (struct glyph_string *s)
23444 {
23445 int k = -1;
23446
23447 if (s->right_overhang)
23448 {
23449 int x = 0, i;
23450 struct glyph *glyphs = s->row->glyphs[s->area];
23451 int first = (s->first_glyph - glyphs
23452 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23453 int end = s->row->used[s->area];
23454
23455 for (i = first; i < end && s->right_overhang > x; ++i)
23456 x += glyphs[i].pixel_width;
23457
23458 k = i;
23459 }
23460
23461 return k;
23462 }
23463
23464
23465 /* Return the index of the last glyph following glyph string S that
23466 overwrites S because of its left overhang. Value is negative
23467 if no such glyph is found. */
23468
23469 static int
23470 right_overwriting (struct glyph_string *s)
23471 {
23472 int i, k, x;
23473 int end = s->row->used[s->area];
23474 struct glyph *glyphs = s->row->glyphs[s->area];
23475 int first = (s->first_glyph - glyphs
23476 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23477
23478 k = -1;
23479 x = 0;
23480 for (i = first; i < end; ++i)
23481 {
23482 int left, right;
23483 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23484 if (x - left < 0)
23485 k = i;
23486 x += glyphs[i].pixel_width;
23487 }
23488
23489 return k;
23490 }
23491
23492
23493 /* Set background width of glyph string S. START is the index of the
23494 first glyph following S. LAST_X is the right-most x-position + 1
23495 in the drawing area. */
23496
23497 static void
23498 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23499 {
23500 /* If the face of this glyph string has to be drawn to the end of
23501 the drawing area, set S->extends_to_end_of_line_p. */
23502
23503 if (start == s->row->used[s->area]
23504 && s->area == TEXT_AREA
23505 && ((s->row->fill_line_p
23506 && (s->hl == DRAW_NORMAL_TEXT
23507 || s->hl == DRAW_IMAGE_RAISED
23508 || s->hl == DRAW_IMAGE_SUNKEN))
23509 || s->hl == DRAW_MOUSE_FACE))
23510 s->extends_to_end_of_line_p = 1;
23511
23512 /* If S extends its face to the end of the line, set its
23513 background_width to the distance to the right edge of the drawing
23514 area. */
23515 if (s->extends_to_end_of_line_p)
23516 s->background_width = last_x - s->x + 1;
23517 else
23518 s->background_width = s->width;
23519 }
23520
23521
23522 /* Compute overhangs and x-positions for glyph string S and its
23523 predecessors, or successors. X is the starting x-position for S.
23524 BACKWARD_P non-zero means process predecessors. */
23525
23526 static void
23527 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23528 {
23529 if (backward_p)
23530 {
23531 while (s)
23532 {
23533 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23534 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23535 x -= s->width;
23536 s->x = x;
23537 s = s->prev;
23538 }
23539 }
23540 else
23541 {
23542 while (s)
23543 {
23544 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23545 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23546 s->x = x;
23547 x += s->width;
23548 s = s->next;
23549 }
23550 }
23551 }
23552
23553
23554
23555 /* The following macros are only called from draw_glyphs below.
23556 They reference the following parameters of that function directly:
23557 `w', `row', `area', and `overlap_p'
23558 as well as the following local variables:
23559 `s', `f', and `hdc' (in W32) */
23560
23561 #ifdef HAVE_NTGUI
23562 /* On W32, silently add local `hdc' variable to argument list of
23563 init_glyph_string. */
23564 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23565 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23566 #else
23567 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23568 init_glyph_string (s, char2b, w, row, area, start, hl)
23569 #endif
23570
23571 /* Add a glyph string for a stretch glyph to the list of strings
23572 between HEAD and TAIL. START is the index of the stretch glyph in
23573 row area AREA of glyph row ROW. END is the index of the last glyph
23574 in that glyph row area. X is the current output position assigned
23575 to the new glyph string constructed. HL overrides that face of the
23576 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23577 is the right-most x-position of the drawing area. */
23578
23579 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23580 and below -- keep them on one line. */
23581 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23582 do \
23583 { \
23584 s = alloca (sizeof *s); \
23585 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23586 START = fill_stretch_glyph_string (s, START, END); \
23587 append_glyph_string (&HEAD, &TAIL, s); \
23588 s->x = (X); \
23589 } \
23590 while (0)
23591
23592
23593 /* Add a glyph string for an image glyph to the list of strings
23594 between HEAD and TAIL. START is the index of the image glyph in
23595 row area AREA of glyph row ROW. END is the index of the last glyph
23596 in that glyph row area. X is the current output position assigned
23597 to the new glyph string constructed. HL overrides that face of the
23598 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23599 is the right-most x-position of the drawing area. */
23600
23601 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23602 do \
23603 { \
23604 s = alloca (sizeof *s); \
23605 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23606 fill_image_glyph_string (s); \
23607 append_glyph_string (&HEAD, &TAIL, s); \
23608 ++START; \
23609 s->x = (X); \
23610 } \
23611 while (0)
23612
23613
23614 /* Add a glyph string for a sequence of character glyphs to the list
23615 of strings between HEAD and TAIL. START is the index of the first
23616 glyph in row area AREA of glyph row ROW that is part of the new
23617 glyph string. END is the index of the last glyph in that glyph row
23618 area. X is the current output position assigned to the new glyph
23619 string constructed. HL overrides that face of the glyph; e.g. it
23620 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23621 right-most x-position of the drawing area. */
23622
23623 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23624 do \
23625 { \
23626 int face_id; \
23627 XChar2b *char2b; \
23628 \
23629 face_id = (row)->glyphs[area][START].face_id; \
23630 \
23631 s = alloca (sizeof *s); \
23632 char2b = alloca ((END - START) * sizeof *char2b); \
23633 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23634 append_glyph_string (&HEAD, &TAIL, s); \
23635 s->x = (X); \
23636 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23637 } \
23638 while (0)
23639
23640
23641 /* Add a glyph string for a composite sequence to the list of strings
23642 between HEAD and TAIL. START is the index of the first glyph in
23643 row area AREA of glyph row ROW that is part of the new glyph
23644 string. END is the index of the last glyph in that glyph row area.
23645 X is the current output position assigned to the new glyph string
23646 constructed. HL overrides that face of the glyph; e.g. it is
23647 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23648 x-position of the drawing area. */
23649
23650 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23651 do { \
23652 int face_id = (row)->glyphs[area][START].face_id; \
23653 struct face *base_face = FACE_FROM_ID (f, face_id); \
23654 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23655 struct composition *cmp = composition_table[cmp_id]; \
23656 XChar2b *char2b; \
23657 struct glyph_string *first_s = NULL; \
23658 int n; \
23659 \
23660 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23661 \
23662 /* Make glyph_strings for each glyph sequence that is drawable by \
23663 the same face, and append them to HEAD/TAIL. */ \
23664 for (n = 0; n < cmp->glyph_len;) \
23665 { \
23666 s = alloca (sizeof *s); \
23667 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23668 append_glyph_string (&(HEAD), &(TAIL), s); \
23669 s->cmp = cmp; \
23670 s->cmp_from = n; \
23671 s->x = (X); \
23672 if (n == 0) \
23673 first_s = s; \
23674 n = fill_composite_glyph_string (s, base_face, overlaps); \
23675 } \
23676 \
23677 ++START; \
23678 s = first_s; \
23679 } while (0)
23680
23681
23682 /* Add a glyph string for a glyph-string sequence to the list of strings
23683 between HEAD and TAIL. */
23684
23685 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23686 do { \
23687 int face_id; \
23688 XChar2b *char2b; \
23689 Lisp_Object gstring; \
23690 \
23691 face_id = (row)->glyphs[area][START].face_id; \
23692 gstring = (composition_gstring_from_id \
23693 ((row)->glyphs[area][START].u.cmp.id)); \
23694 s = alloca (sizeof *s); \
23695 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23696 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23697 append_glyph_string (&(HEAD), &(TAIL), s); \
23698 s->x = (X); \
23699 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23700 } while (0)
23701
23702
23703 /* Add a glyph string for a sequence of glyphless character's glyphs
23704 to the list of strings between HEAD and TAIL. The meanings of
23705 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23706
23707 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23708 do \
23709 { \
23710 int face_id; \
23711 \
23712 face_id = (row)->glyphs[area][START].face_id; \
23713 \
23714 s = alloca (sizeof *s); \
23715 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23716 append_glyph_string (&HEAD, &TAIL, s); \
23717 s->x = (X); \
23718 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23719 overlaps); \
23720 } \
23721 while (0)
23722
23723
23724 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23725 of AREA of glyph row ROW on window W between indices START and END.
23726 HL overrides the face for drawing glyph strings, e.g. it is
23727 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23728 x-positions of the drawing area.
23729
23730 This is an ugly monster macro construct because we must use alloca
23731 to allocate glyph strings (because draw_glyphs can be called
23732 asynchronously). */
23733
23734 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23735 do \
23736 { \
23737 HEAD = TAIL = NULL; \
23738 while (START < END) \
23739 { \
23740 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23741 switch (first_glyph->type) \
23742 { \
23743 case CHAR_GLYPH: \
23744 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23745 HL, X, LAST_X); \
23746 break; \
23747 \
23748 case COMPOSITE_GLYPH: \
23749 if (first_glyph->u.cmp.automatic) \
23750 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23751 HL, X, LAST_X); \
23752 else \
23753 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23754 HL, X, LAST_X); \
23755 break; \
23756 \
23757 case STRETCH_GLYPH: \
23758 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23759 HL, X, LAST_X); \
23760 break; \
23761 \
23762 case IMAGE_GLYPH: \
23763 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23764 HL, X, LAST_X); \
23765 break; \
23766 \
23767 case GLYPHLESS_GLYPH: \
23768 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23769 HL, X, LAST_X); \
23770 break; \
23771 \
23772 default: \
23773 emacs_abort (); \
23774 } \
23775 \
23776 if (s) \
23777 { \
23778 set_glyph_string_background_width (s, START, LAST_X); \
23779 (X) += s->width; \
23780 } \
23781 } \
23782 } while (0)
23783
23784
23785 /* Draw glyphs between START and END in AREA of ROW on window W,
23786 starting at x-position X. X is relative to AREA in W. HL is a
23787 face-override with the following meaning:
23788
23789 DRAW_NORMAL_TEXT draw normally
23790 DRAW_CURSOR draw in cursor face
23791 DRAW_MOUSE_FACE draw in mouse face.
23792 DRAW_INVERSE_VIDEO draw in mode line face
23793 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23794 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23795
23796 If OVERLAPS is non-zero, draw only the foreground of characters and
23797 clip to the physical height of ROW. Non-zero value also defines
23798 the overlapping part to be drawn:
23799
23800 OVERLAPS_PRED overlap with preceding rows
23801 OVERLAPS_SUCC overlap with succeeding rows
23802 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23803 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23804
23805 Value is the x-position reached, relative to AREA of W. */
23806
23807 static int
23808 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23809 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23810 enum draw_glyphs_face hl, int overlaps)
23811 {
23812 struct glyph_string *head, *tail;
23813 struct glyph_string *s;
23814 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23815 int i, j, x_reached, last_x, area_left = 0;
23816 struct frame *f = XFRAME (WINDOW_FRAME (w));
23817 DECLARE_HDC (hdc);
23818
23819 ALLOCATE_HDC (hdc, f);
23820
23821 /* Let's rather be paranoid than getting a SEGV. */
23822 end = min (end, row->used[area]);
23823 start = clip_to_bounds (0, start, end);
23824
23825 /* Translate X to frame coordinates. Set last_x to the right
23826 end of the drawing area. */
23827 if (row->full_width_p)
23828 {
23829 /* X is relative to the left edge of W, without scroll bars
23830 or fringes. */
23831 area_left = WINDOW_LEFT_EDGE_X (w);
23832 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23833 }
23834 else
23835 {
23836 area_left = window_box_left (w, area);
23837 last_x = area_left + window_box_width (w, area);
23838 }
23839 x += area_left;
23840
23841 /* Build a doubly-linked list of glyph_string structures between
23842 head and tail from what we have to draw. Note that the macro
23843 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23844 the reason we use a separate variable `i'. */
23845 i = start;
23846 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23847 if (tail)
23848 x_reached = tail->x + tail->background_width;
23849 else
23850 x_reached = x;
23851
23852 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23853 the row, redraw some glyphs in front or following the glyph
23854 strings built above. */
23855 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23856 {
23857 struct glyph_string *h, *t;
23858 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23859 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23860 int check_mouse_face = 0;
23861 int dummy_x = 0;
23862
23863 /* If mouse highlighting is on, we may need to draw adjacent
23864 glyphs using mouse-face highlighting. */
23865 if (area == TEXT_AREA && row->mouse_face_p
23866 && hlinfo->mouse_face_beg_row >= 0
23867 && hlinfo->mouse_face_end_row >= 0)
23868 {
23869 struct glyph_row *mouse_beg_row, *mouse_end_row;
23870
23871 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23872 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23873
23874 if (row >= mouse_beg_row && row <= mouse_end_row)
23875 {
23876 check_mouse_face = 1;
23877 mouse_beg_col = (row == mouse_beg_row)
23878 ? hlinfo->mouse_face_beg_col : 0;
23879 mouse_end_col = (row == mouse_end_row)
23880 ? hlinfo->mouse_face_end_col
23881 : row->used[TEXT_AREA];
23882 }
23883 }
23884
23885 /* Compute overhangs for all glyph strings. */
23886 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23887 for (s = head; s; s = s->next)
23888 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23889
23890 /* Prepend glyph strings for glyphs in front of the first glyph
23891 string that are overwritten because of the first glyph
23892 string's left overhang. The background of all strings
23893 prepended must be drawn because the first glyph string
23894 draws over it. */
23895 i = left_overwritten (head);
23896 if (i >= 0)
23897 {
23898 enum draw_glyphs_face overlap_hl;
23899
23900 /* If this row contains mouse highlighting, attempt to draw
23901 the overlapped glyphs with the correct highlight. This
23902 code fails if the overlap encompasses more than one glyph
23903 and mouse-highlight spans only some of these glyphs.
23904 However, making it work perfectly involves a lot more
23905 code, and I don't know if the pathological case occurs in
23906 practice, so we'll stick to this for now. --- cyd */
23907 if (check_mouse_face
23908 && mouse_beg_col < start && mouse_end_col > i)
23909 overlap_hl = DRAW_MOUSE_FACE;
23910 else
23911 overlap_hl = DRAW_NORMAL_TEXT;
23912
23913 j = i;
23914 BUILD_GLYPH_STRINGS (j, start, h, t,
23915 overlap_hl, dummy_x, last_x);
23916 start = i;
23917 compute_overhangs_and_x (t, head->x, 1);
23918 prepend_glyph_string_lists (&head, &tail, h, t);
23919 clip_head = head;
23920 }
23921
23922 /* Prepend glyph strings for glyphs in front of the first glyph
23923 string that overwrite that glyph string because of their
23924 right overhang. For these strings, only the foreground must
23925 be drawn, because it draws over the glyph string at `head'.
23926 The background must not be drawn because this would overwrite
23927 right overhangs of preceding glyphs for which no glyph
23928 strings exist. */
23929 i = left_overwriting (head);
23930 if (i >= 0)
23931 {
23932 enum draw_glyphs_face overlap_hl;
23933
23934 if (check_mouse_face
23935 && mouse_beg_col < start && mouse_end_col > i)
23936 overlap_hl = DRAW_MOUSE_FACE;
23937 else
23938 overlap_hl = DRAW_NORMAL_TEXT;
23939
23940 clip_head = head;
23941 BUILD_GLYPH_STRINGS (i, start, h, t,
23942 overlap_hl, dummy_x, last_x);
23943 for (s = h; s; s = s->next)
23944 s->background_filled_p = 1;
23945 compute_overhangs_and_x (t, head->x, 1);
23946 prepend_glyph_string_lists (&head, &tail, h, t);
23947 }
23948
23949 /* Append glyphs strings for glyphs following the last glyph
23950 string tail that are overwritten by tail. The background of
23951 these strings has to be drawn because tail's foreground draws
23952 over it. */
23953 i = right_overwritten (tail);
23954 if (i >= 0)
23955 {
23956 enum draw_glyphs_face overlap_hl;
23957
23958 if (check_mouse_face
23959 && mouse_beg_col < i && mouse_end_col > end)
23960 overlap_hl = DRAW_MOUSE_FACE;
23961 else
23962 overlap_hl = DRAW_NORMAL_TEXT;
23963
23964 BUILD_GLYPH_STRINGS (end, i, h, t,
23965 overlap_hl, x, last_x);
23966 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23967 we don't have `end = i;' here. */
23968 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23969 append_glyph_string_lists (&head, &tail, h, t);
23970 clip_tail = tail;
23971 }
23972
23973 /* Append glyph strings for glyphs following the last glyph
23974 string tail that overwrite tail. The foreground of such
23975 glyphs has to be drawn because it writes into the background
23976 of tail. The background must not be drawn because it could
23977 paint over the foreground of following glyphs. */
23978 i = right_overwriting (tail);
23979 if (i >= 0)
23980 {
23981 enum draw_glyphs_face overlap_hl;
23982 if (check_mouse_face
23983 && mouse_beg_col < i && mouse_end_col > end)
23984 overlap_hl = DRAW_MOUSE_FACE;
23985 else
23986 overlap_hl = DRAW_NORMAL_TEXT;
23987
23988 clip_tail = tail;
23989 i++; /* We must include the Ith glyph. */
23990 BUILD_GLYPH_STRINGS (end, i, h, t,
23991 overlap_hl, x, last_x);
23992 for (s = h; s; s = s->next)
23993 s->background_filled_p = 1;
23994 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23995 append_glyph_string_lists (&head, &tail, h, t);
23996 }
23997 if (clip_head || clip_tail)
23998 for (s = head; s; s = s->next)
23999 {
24000 s->clip_head = clip_head;
24001 s->clip_tail = clip_tail;
24002 }
24003 }
24004
24005 /* Draw all strings. */
24006 for (s = head; s; s = s->next)
24007 FRAME_RIF (f)->draw_glyph_string (s);
24008
24009 #ifndef HAVE_NS
24010 /* When focus a sole frame and move horizontally, this sets on_p to 0
24011 causing a failure to erase prev cursor position. */
24012 if (area == TEXT_AREA
24013 && !row->full_width_p
24014 /* When drawing overlapping rows, only the glyph strings'
24015 foreground is drawn, which doesn't erase a cursor
24016 completely. */
24017 && !overlaps)
24018 {
24019 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24020 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24021 : (tail ? tail->x + tail->background_width : x));
24022 x0 -= area_left;
24023 x1 -= area_left;
24024
24025 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24026 row->y, MATRIX_ROW_BOTTOM_Y (row));
24027 }
24028 #endif
24029
24030 /* Value is the x-position up to which drawn, relative to AREA of W.
24031 This doesn't include parts drawn because of overhangs. */
24032 if (row->full_width_p)
24033 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24034 else
24035 x_reached -= area_left;
24036
24037 RELEASE_HDC (hdc, f);
24038
24039 return x_reached;
24040 }
24041
24042 /* Expand row matrix if too narrow. Don't expand if area
24043 is not present. */
24044
24045 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24046 { \
24047 if (!fonts_changed_p \
24048 && (it->glyph_row->glyphs[area] \
24049 < it->glyph_row->glyphs[area + 1])) \
24050 { \
24051 it->w->ncols_scale_factor++; \
24052 fonts_changed_p = 1; \
24053 } \
24054 }
24055
24056 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24057 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24058
24059 static void
24060 append_glyph (struct it *it)
24061 {
24062 struct glyph *glyph;
24063 enum glyph_row_area area = it->area;
24064
24065 eassert (it->glyph_row);
24066 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24067
24068 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24069 if (glyph < it->glyph_row->glyphs[area + 1])
24070 {
24071 /* If the glyph row is reversed, we need to prepend the glyph
24072 rather than append it. */
24073 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24074 {
24075 struct glyph *g;
24076
24077 /* Make room for the additional glyph. */
24078 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24079 g[1] = *g;
24080 glyph = it->glyph_row->glyphs[area];
24081 }
24082 glyph->charpos = CHARPOS (it->position);
24083 glyph->object = it->object;
24084 if (it->pixel_width > 0)
24085 {
24086 glyph->pixel_width = it->pixel_width;
24087 glyph->padding_p = 0;
24088 }
24089 else
24090 {
24091 /* Assure at least 1-pixel width. Otherwise, cursor can't
24092 be displayed correctly. */
24093 glyph->pixel_width = 1;
24094 glyph->padding_p = 1;
24095 }
24096 glyph->ascent = it->ascent;
24097 glyph->descent = it->descent;
24098 glyph->voffset = it->voffset;
24099 glyph->type = CHAR_GLYPH;
24100 glyph->avoid_cursor_p = it->avoid_cursor_p;
24101 glyph->multibyte_p = it->multibyte_p;
24102 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24103 {
24104 /* In R2L rows, the left and the right box edges need to be
24105 drawn in reverse direction. */
24106 glyph->right_box_line_p = it->start_of_box_run_p;
24107 glyph->left_box_line_p = it->end_of_box_run_p;
24108 }
24109 else
24110 {
24111 glyph->left_box_line_p = it->start_of_box_run_p;
24112 glyph->right_box_line_p = it->end_of_box_run_p;
24113 }
24114 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24115 || it->phys_descent > it->descent);
24116 glyph->glyph_not_available_p = it->glyph_not_available_p;
24117 glyph->face_id = it->face_id;
24118 glyph->u.ch = it->char_to_display;
24119 glyph->slice.img = null_glyph_slice;
24120 glyph->font_type = FONT_TYPE_UNKNOWN;
24121 if (it->bidi_p)
24122 {
24123 glyph->resolved_level = it->bidi_it.resolved_level;
24124 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24125 emacs_abort ();
24126 glyph->bidi_type = it->bidi_it.type;
24127 }
24128 else
24129 {
24130 glyph->resolved_level = 0;
24131 glyph->bidi_type = UNKNOWN_BT;
24132 }
24133 ++it->glyph_row->used[area];
24134 }
24135 else
24136 IT_EXPAND_MATRIX_WIDTH (it, area);
24137 }
24138
24139 /* Store one glyph for the composition IT->cmp_it.id in
24140 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24141 non-null. */
24142
24143 static void
24144 append_composite_glyph (struct it *it)
24145 {
24146 struct glyph *glyph;
24147 enum glyph_row_area area = it->area;
24148
24149 eassert (it->glyph_row);
24150
24151 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24152 if (glyph < it->glyph_row->glyphs[area + 1])
24153 {
24154 /* If the glyph row is reversed, we need to prepend the glyph
24155 rather than append it. */
24156 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24157 {
24158 struct glyph *g;
24159
24160 /* Make room for the new glyph. */
24161 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24162 g[1] = *g;
24163 glyph = it->glyph_row->glyphs[it->area];
24164 }
24165 glyph->charpos = it->cmp_it.charpos;
24166 glyph->object = it->object;
24167 glyph->pixel_width = it->pixel_width;
24168 glyph->ascent = it->ascent;
24169 glyph->descent = it->descent;
24170 glyph->voffset = it->voffset;
24171 glyph->type = COMPOSITE_GLYPH;
24172 if (it->cmp_it.ch < 0)
24173 {
24174 glyph->u.cmp.automatic = 0;
24175 glyph->u.cmp.id = it->cmp_it.id;
24176 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24177 }
24178 else
24179 {
24180 glyph->u.cmp.automatic = 1;
24181 glyph->u.cmp.id = it->cmp_it.id;
24182 glyph->slice.cmp.from = it->cmp_it.from;
24183 glyph->slice.cmp.to = it->cmp_it.to - 1;
24184 }
24185 glyph->avoid_cursor_p = it->avoid_cursor_p;
24186 glyph->multibyte_p = it->multibyte_p;
24187 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24188 {
24189 /* In R2L rows, the left and the right box edges need to be
24190 drawn in reverse direction. */
24191 glyph->right_box_line_p = it->start_of_box_run_p;
24192 glyph->left_box_line_p = it->end_of_box_run_p;
24193 }
24194 else
24195 {
24196 glyph->left_box_line_p = it->start_of_box_run_p;
24197 glyph->right_box_line_p = it->end_of_box_run_p;
24198 }
24199 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24200 || it->phys_descent > it->descent);
24201 glyph->padding_p = 0;
24202 glyph->glyph_not_available_p = 0;
24203 glyph->face_id = it->face_id;
24204 glyph->font_type = FONT_TYPE_UNKNOWN;
24205 if (it->bidi_p)
24206 {
24207 glyph->resolved_level = it->bidi_it.resolved_level;
24208 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24209 emacs_abort ();
24210 glyph->bidi_type = it->bidi_it.type;
24211 }
24212 ++it->glyph_row->used[area];
24213 }
24214 else
24215 IT_EXPAND_MATRIX_WIDTH (it, area);
24216 }
24217
24218
24219 /* Change IT->ascent and IT->height according to the setting of
24220 IT->voffset. */
24221
24222 static void
24223 take_vertical_position_into_account (struct it *it)
24224 {
24225 if (it->voffset)
24226 {
24227 if (it->voffset < 0)
24228 /* Increase the ascent so that we can display the text higher
24229 in the line. */
24230 it->ascent -= it->voffset;
24231 else
24232 /* Increase the descent so that we can display the text lower
24233 in the line. */
24234 it->descent += it->voffset;
24235 }
24236 }
24237
24238
24239 /* Produce glyphs/get display metrics for the image IT is loaded with.
24240 See the description of struct display_iterator in dispextern.h for
24241 an overview of struct display_iterator. */
24242
24243 static void
24244 produce_image_glyph (struct it *it)
24245 {
24246 struct image *img;
24247 struct face *face;
24248 int glyph_ascent, crop;
24249 struct glyph_slice slice;
24250
24251 eassert (it->what == IT_IMAGE);
24252
24253 face = FACE_FROM_ID (it->f, it->face_id);
24254 eassert (face);
24255 /* Make sure X resources of the face is loaded. */
24256 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24257
24258 if (it->image_id < 0)
24259 {
24260 /* Fringe bitmap. */
24261 it->ascent = it->phys_ascent = 0;
24262 it->descent = it->phys_descent = 0;
24263 it->pixel_width = 0;
24264 it->nglyphs = 0;
24265 return;
24266 }
24267
24268 img = IMAGE_FROM_ID (it->f, it->image_id);
24269 eassert (img);
24270 /* Make sure X resources of the image is loaded. */
24271 prepare_image_for_display (it->f, img);
24272
24273 slice.x = slice.y = 0;
24274 slice.width = img->width;
24275 slice.height = img->height;
24276
24277 if (INTEGERP (it->slice.x))
24278 slice.x = XINT (it->slice.x);
24279 else if (FLOATP (it->slice.x))
24280 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24281
24282 if (INTEGERP (it->slice.y))
24283 slice.y = XINT (it->slice.y);
24284 else if (FLOATP (it->slice.y))
24285 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24286
24287 if (INTEGERP (it->slice.width))
24288 slice.width = XINT (it->slice.width);
24289 else if (FLOATP (it->slice.width))
24290 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24291
24292 if (INTEGERP (it->slice.height))
24293 slice.height = XINT (it->slice.height);
24294 else if (FLOATP (it->slice.height))
24295 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24296
24297 if (slice.x >= img->width)
24298 slice.x = img->width;
24299 if (slice.y >= img->height)
24300 slice.y = img->height;
24301 if (slice.x + slice.width >= img->width)
24302 slice.width = img->width - slice.x;
24303 if (slice.y + slice.height > img->height)
24304 slice.height = img->height - slice.y;
24305
24306 if (slice.width == 0 || slice.height == 0)
24307 return;
24308
24309 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24310
24311 it->descent = slice.height - glyph_ascent;
24312 if (slice.y == 0)
24313 it->descent += img->vmargin;
24314 if (slice.y + slice.height == img->height)
24315 it->descent += img->vmargin;
24316 it->phys_descent = it->descent;
24317
24318 it->pixel_width = slice.width;
24319 if (slice.x == 0)
24320 it->pixel_width += img->hmargin;
24321 if (slice.x + slice.width == img->width)
24322 it->pixel_width += img->hmargin;
24323
24324 /* It's quite possible for images to have an ascent greater than
24325 their height, so don't get confused in that case. */
24326 if (it->descent < 0)
24327 it->descent = 0;
24328
24329 it->nglyphs = 1;
24330
24331 if (face->box != FACE_NO_BOX)
24332 {
24333 if (face->box_line_width > 0)
24334 {
24335 if (slice.y == 0)
24336 it->ascent += face->box_line_width;
24337 if (slice.y + slice.height == img->height)
24338 it->descent += face->box_line_width;
24339 }
24340
24341 if (it->start_of_box_run_p && slice.x == 0)
24342 it->pixel_width += eabs (face->box_line_width);
24343 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24344 it->pixel_width += eabs (face->box_line_width);
24345 }
24346
24347 take_vertical_position_into_account (it);
24348
24349 /* Automatically crop wide image glyphs at right edge so we can
24350 draw the cursor on same display row. */
24351 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24352 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24353 {
24354 it->pixel_width -= crop;
24355 slice.width -= crop;
24356 }
24357
24358 if (it->glyph_row)
24359 {
24360 struct glyph *glyph;
24361 enum glyph_row_area area = it->area;
24362
24363 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24364 if (glyph < it->glyph_row->glyphs[area + 1])
24365 {
24366 glyph->charpos = CHARPOS (it->position);
24367 glyph->object = it->object;
24368 glyph->pixel_width = it->pixel_width;
24369 glyph->ascent = glyph_ascent;
24370 glyph->descent = it->descent;
24371 glyph->voffset = it->voffset;
24372 glyph->type = IMAGE_GLYPH;
24373 glyph->avoid_cursor_p = it->avoid_cursor_p;
24374 glyph->multibyte_p = it->multibyte_p;
24375 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24376 {
24377 /* In R2L rows, the left and the right box edges need to be
24378 drawn in reverse direction. */
24379 glyph->right_box_line_p = it->start_of_box_run_p;
24380 glyph->left_box_line_p = it->end_of_box_run_p;
24381 }
24382 else
24383 {
24384 glyph->left_box_line_p = it->start_of_box_run_p;
24385 glyph->right_box_line_p = it->end_of_box_run_p;
24386 }
24387 glyph->overlaps_vertically_p = 0;
24388 glyph->padding_p = 0;
24389 glyph->glyph_not_available_p = 0;
24390 glyph->face_id = it->face_id;
24391 glyph->u.img_id = img->id;
24392 glyph->slice.img = slice;
24393 glyph->font_type = FONT_TYPE_UNKNOWN;
24394 if (it->bidi_p)
24395 {
24396 glyph->resolved_level = it->bidi_it.resolved_level;
24397 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24398 emacs_abort ();
24399 glyph->bidi_type = it->bidi_it.type;
24400 }
24401 ++it->glyph_row->used[area];
24402 }
24403 else
24404 IT_EXPAND_MATRIX_WIDTH (it, area);
24405 }
24406 }
24407
24408
24409 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24410 of the glyph, WIDTH and HEIGHT are the width and height of the
24411 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24412
24413 static void
24414 append_stretch_glyph (struct it *it, Lisp_Object object,
24415 int width, int height, int ascent)
24416 {
24417 struct glyph *glyph;
24418 enum glyph_row_area area = it->area;
24419
24420 eassert (ascent >= 0 && ascent <= height);
24421
24422 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24423 if (glyph < it->glyph_row->glyphs[area + 1])
24424 {
24425 /* If the glyph row is reversed, we need to prepend the glyph
24426 rather than append it. */
24427 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24428 {
24429 struct glyph *g;
24430
24431 /* Make room for the additional glyph. */
24432 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24433 g[1] = *g;
24434 glyph = it->glyph_row->glyphs[area];
24435 }
24436 glyph->charpos = CHARPOS (it->position);
24437 glyph->object = object;
24438 glyph->pixel_width = width;
24439 glyph->ascent = ascent;
24440 glyph->descent = height - ascent;
24441 glyph->voffset = it->voffset;
24442 glyph->type = STRETCH_GLYPH;
24443 glyph->avoid_cursor_p = it->avoid_cursor_p;
24444 glyph->multibyte_p = it->multibyte_p;
24445 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24446 {
24447 /* In R2L rows, the left and the right box edges need to be
24448 drawn in reverse direction. */
24449 glyph->right_box_line_p = it->start_of_box_run_p;
24450 glyph->left_box_line_p = it->end_of_box_run_p;
24451 }
24452 else
24453 {
24454 glyph->left_box_line_p = it->start_of_box_run_p;
24455 glyph->right_box_line_p = it->end_of_box_run_p;
24456 }
24457 glyph->overlaps_vertically_p = 0;
24458 glyph->padding_p = 0;
24459 glyph->glyph_not_available_p = 0;
24460 glyph->face_id = it->face_id;
24461 glyph->u.stretch.ascent = ascent;
24462 glyph->u.stretch.height = height;
24463 glyph->slice.img = null_glyph_slice;
24464 glyph->font_type = FONT_TYPE_UNKNOWN;
24465 if (it->bidi_p)
24466 {
24467 glyph->resolved_level = it->bidi_it.resolved_level;
24468 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24469 emacs_abort ();
24470 glyph->bidi_type = it->bidi_it.type;
24471 }
24472 else
24473 {
24474 glyph->resolved_level = 0;
24475 glyph->bidi_type = UNKNOWN_BT;
24476 }
24477 ++it->glyph_row->used[area];
24478 }
24479 else
24480 IT_EXPAND_MATRIX_WIDTH (it, area);
24481 }
24482
24483 #endif /* HAVE_WINDOW_SYSTEM */
24484
24485 /* Produce a stretch glyph for iterator IT. IT->object is the value
24486 of the glyph property displayed. The value must be a list
24487 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24488 being recognized:
24489
24490 1. `:width WIDTH' specifies that the space should be WIDTH *
24491 canonical char width wide. WIDTH may be an integer or floating
24492 point number.
24493
24494 2. `:relative-width FACTOR' specifies that the width of the stretch
24495 should be computed from the width of the first character having the
24496 `glyph' property, and should be FACTOR times that width.
24497
24498 3. `:align-to HPOS' specifies that the space should be wide enough
24499 to reach HPOS, a value in canonical character units.
24500
24501 Exactly one of the above pairs must be present.
24502
24503 4. `:height HEIGHT' specifies that the height of the stretch produced
24504 should be HEIGHT, measured in canonical character units.
24505
24506 5. `:relative-height FACTOR' specifies that the height of the
24507 stretch should be FACTOR times the height of the characters having
24508 the glyph property.
24509
24510 Either none or exactly one of 4 or 5 must be present.
24511
24512 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24513 of the stretch should be used for the ascent of the stretch.
24514 ASCENT must be in the range 0 <= ASCENT <= 100. */
24515
24516 void
24517 produce_stretch_glyph (struct it *it)
24518 {
24519 /* (space :width WIDTH :height HEIGHT ...) */
24520 Lisp_Object prop, plist;
24521 int width = 0, height = 0, align_to = -1;
24522 int zero_width_ok_p = 0;
24523 double tem;
24524 struct font *font = NULL;
24525
24526 #ifdef HAVE_WINDOW_SYSTEM
24527 int ascent = 0;
24528 int zero_height_ok_p = 0;
24529
24530 if (FRAME_WINDOW_P (it->f))
24531 {
24532 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24533 font = face->font ? face->font : FRAME_FONT (it->f);
24534 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24535 }
24536 #endif
24537
24538 /* List should start with `space'. */
24539 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24540 plist = XCDR (it->object);
24541
24542 /* Compute the width of the stretch. */
24543 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24544 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24545 {
24546 /* Absolute width `:width WIDTH' specified and valid. */
24547 zero_width_ok_p = 1;
24548 width = (int)tem;
24549 }
24550 #ifdef HAVE_WINDOW_SYSTEM
24551 else if (FRAME_WINDOW_P (it->f)
24552 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24553 {
24554 /* Relative width `:relative-width FACTOR' specified and valid.
24555 Compute the width of the characters having the `glyph'
24556 property. */
24557 struct it it2;
24558 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24559
24560 it2 = *it;
24561 if (it->multibyte_p)
24562 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24563 else
24564 {
24565 it2.c = it2.char_to_display = *p, it2.len = 1;
24566 if (! ASCII_CHAR_P (it2.c))
24567 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24568 }
24569
24570 it2.glyph_row = NULL;
24571 it2.what = IT_CHARACTER;
24572 x_produce_glyphs (&it2);
24573 width = NUMVAL (prop) * it2.pixel_width;
24574 }
24575 #endif /* HAVE_WINDOW_SYSTEM */
24576 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24577 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24578 {
24579 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24580 align_to = (align_to < 0
24581 ? 0
24582 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24583 else if (align_to < 0)
24584 align_to = window_box_left_offset (it->w, TEXT_AREA);
24585 width = max (0, (int)tem + align_to - it->current_x);
24586 zero_width_ok_p = 1;
24587 }
24588 else
24589 /* Nothing specified -> width defaults to canonical char width. */
24590 width = FRAME_COLUMN_WIDTH (it->f);
24591
24592 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24593 width = 1;
24594
24595 #ifdef HAVE_WINDOW_SYSTEM
24596 /* Compute height. */
24597 if (FRAME_WINDOW_P (it->f))
24598 {
24599 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24600 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24601 {
24602 height = (int)tem;
24603 zero_height_ok_p = 1;
24604 }
24605 else if (prop = Fplist_get (plist, QCrelative_height),
24606 NUMVAL (prop) > 0)
24607 height = FONT_HEIGHT (font) * NUMVAL (prop);
24608 else
24609 height = FONT_HEIGHT (font);
24610
24611 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24612 height = 1;
24613
24614 /* Compute percentage of height used for ascent. If
24615 `:ascent ASCENT' is present and valid, use that. Otherwise,
24616 derive the ascent from the font in use. */
24617 if (prop = Fplist_get (plist, QCascent),
24618 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24619 ascent = height * NUMVAL (prop) / 100.0;
24620 else if (!NILP (prop)
24621 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24622 ascent = min (max (0, (int)tem), height);
24623 else
24624 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24625 }
24626 else
24627 #endif /* HAVE_WINDOW_SYSTEM */
24628 height = 1;
24629
24630 if (width > 0 && it->line_wrap != TRUNCATE
24631 && it->current_x + width > it->last_visible_x)
24632 {
24633 width = it->last_visible_x - it->current_x;
24634 #ifdef HAVE_WINDOW_SYSTEM
24635 /* Subtract one more pixel from the stretch width, but only on
24636 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24637 width -= FRAME_WINDOW_P (it->f);
24638 #endif
24639 }
24640
24641 if (width > 0 && height > 0 && it->glyph_row)
24642 {
24643 Lisp_Object o_object = it->object;
24644 Lisp_Object object = it->stack[it->sp - 1].string;
24645 int n = width;
24646
24647 if (!STRINGP (object))
24648 object = it->w->contents;
24649 #ifdef HAVE_WINDOW_SYSTEM
24650 if (FRAME_WINDOW_P (it->f))
24651 append_stretch_glyph (it, object, width, height, ascent);
24652 else
24653 #endif
24654 {
24655 it->object = object;
24656 it->char_to_display = ' ';
24657 it->pixel_width = it->len = 1;
24658 while (n--)
24659 tty_append_glyph (it);
24660 it->object = o_object;
24661 }
24662 }
24663
24664 it->pixel_width = width;
24665 #ifdef HAVE_WINDOW_SYSTEM
24666 if (FRAME_WINDOW_P (it->f))
24667 {
24668 it->ascent = it->phys_ascent = ascent;
24669 it->descent = it->phys_descent = height - it->ascent;
24670 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24671 take_vertical_position_into_account (it);
24672 }
24673 else
24674 #endif
24675 it->nglyphs = width;
24676 }
24677
24678 /* Get information about special display element WHAT in an
24679 environment described by IT. WHAT is one of IT_TRUNCATION or
24680 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24681 non-null glyph_row member. This function ensures that fields like
24682 face_id, c, len of IT are left untouched. */
24683
24684 static void
24685 produce_special_glyphs (struct it *it, enum display_element_type what)
24686 {
24687 struct it temp_it;
24688 Lisp_Object gc;
24689 GLYPH glyph;
24690
24691 temp_it = *it;
24692 temp_it.object = make_number (0);
24693 memset (&temp_it.current, 0, sizeof temp_it.current);
24694
24695 if (what == IT_CONTINUATION)
24696 {
24697 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24698 if (it->bidi_it.paragraph_dir == R2L)
24699 SET_GLYPH_FROM_CHAR (glyph, '/');
24700 else
24701 SET_GLYPH_FROM_CHAR (glyph, '\\');
24702 if (it->dp
24703 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24704 {
24705 /* FIXME: Should we mirror GC for R2L lines? */
24706 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24707 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24708 }
24709 }
24710 else if (what == IT_TRUNCATION)
24711 {
24712 /* Truncation glyph. */
24713 SET_GLYPH_FROM_CHAR (glyph, '$');
24714 if (it->dp
24715 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24716 {
24717 /* FIXME: Should we mirror GC for R2L lines? */
24718 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24719 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24720 }
24721 }
24722 else
24723 emacs_abort ();
24724
24725 #ifdef HAVE_WINDOW_SYSTEM
24726 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24727 is turned off, we precede the truncation/continuation glyphs by a
24728 stretch glyph whose width is computed such that these special
24729 glyphs are aligned at the window margin, even when very different
24730 fonts are used in different glyph rows. */
24731 if (FRAME_WINDOW_P (temp_it.f)
24732 /* init_iterator calls this with it->glyph_row == NULL, and it
24733 wants only the pixel width of the truncation/continuation
24734 glyphs. */
24735 && temp_it.glyph_row
24736 /* insert_left_trunc_glyphs calls us at the beginning of the
24737 row, and it has its own calculation of the stretch glyph
24738 width. */
24739 && temp_it.glyph_row->used[TEXT_AREA] > 0
24740 && (temp_it.glyph_row->reversed_p
24741 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24742 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24743 {
24744 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24745
24746 if (stretch_width > 0)
24747 {
24748 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24749 struct font *font =
24750 face->font ? face->font : FRAME_FONT (temp_it.f);
24751 int stretch_ascent =
24752 (((temp_it.ascent + temp_it.descent)
24753 * FONT_BASE (font)) / FONT_HEIGHT (font));
24754
24755 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24756 temp_it.ascent + temp_it.descent,
24757 stretch_ascent);
24758 }
24759 }
24760 #endif
24761
24762 temp_it.dp = NULL;
24763 temp_it.what = IT_CHARACTER;
24764 temp_it.len = 1;
24765 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24766 temp_it.face_id = GLYPH_FACE (glyph);
24767 temp_it.len = CHAR_BYTES (temp_it.c);
24768
24769 PRODUCE_GLYPHS (&temp_it);
24770 it->pixel_width = temp_it.pixel_width;
24771 it->nglyphs = temp_it.pixel_width;
24772 }
24773
24774 #ifdef HAVE_WINDOW_SYSTEM
24775
24776 /* Calculate line-height and line-spacing properties.
24777 An integer value specifies explicit pixel value.
24778 A float value specifies relative value to current face height.
24779 A cons (float . face-name) specifies relative value to
24780 height of specified face font.
24781
24782 Returns height in pixels, or nil. */
24783
24784
24785 static Lisp_Object
24786 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24787 int boff, int override)
24788 {
24789 Lisp_Object face_name = Qnil;
24790 int ascent, descent, height;
24791
24792 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24793 return val;
24794
24795 if (CONSP (val))
24796 {
24797 face_name = XCAR (val);
24798 val = XCDR (val);
24799 if (!NUMBERP (val))
24800 val = make_number (1);
24801 if (NILP (face_name))
24802 {
24803 height = it->ascent + it->descent;
24804 goto scale;
24805 }
24806 }
24807
24808 if (NILP (face_name))
24809 {
24810 font = FRAME_FONT (it->f);
24811 boff = FRAME_BASELINE_OFFSET (it->f);
24812 }
24813 else if (EQ (face_name, Qt))
24814 {
24815 override = 0;
24816 }
24817 else
24818 {
24819 int face_id;
24820 struct face *face;
24821
24822 face_id = lookup_named_face (it->f, face_name, 0);
24823 if (face_id < 0)
24824 return make_number (-1);
24825
24826 face = FACE_FROM_ID (it->f, face_id);
24827 font = face->font;
24828 if (font == NULL)
24829 return make_number (-1);
24830 boff = font->baseline_offset;
24831 if (font->vertical_centering)
24832 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24833 }
24834
24835 ascent = FONT_BASE (font) + boff;
24836 descent = FONT_DESCENT (font) - boff;
24837
24838 if (override)
24839 {
24840 it->override_ascent = ascent;
24841 it->override_descent = descent;
24842 it->override_boff = boff;
24843 }
24844
24845 height = ascent + descent;
24846
24847 scale:
24848 if (FLOATP (val))
24849 height = (int)(XFLOAT_DATA (val) * height);
24850 else if (INTEGERP (val))
24851 height *= XINT (val);
24852
24853 return make_number (height);
24854 }
24855
24856
24857 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24858 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24859 and only if this is for a character for which no font was found.
24860
24861 If the display method (it->glyphless_method) is
24862 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24863 length of the acronym or the hexadecimal string, UPPER_XOFF and
24864 UPPER_YOFF are pixel offsets for the upper part of the string,
24865 LOWER_XOFF and LOWER_YOFF are for the lower part.
24866
24867 For the other display methods, LEN through LOWER_YOFF are zero. */
24868
24869 static void
24870 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24871 short upper_xoff, short upper_yoff,
24872 short lower_xoff, short lower_yoff)
24873 {
24874 struct glyph *glyph;
24875 enum glyph_row_area area = it->area;
24876
24877 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24878 if (glyph < it->glyph_row->glyphs[area + 1])
24879 {
24880 /* If the glyph row is reversed, we need to prepend the glyph
24881 rather than append it. */
24882 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24883 {
24884 struct glyph *g;
24885
24886 /* Make room for the additional glyph. */
24887 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24888 g[1] = *g;
24889 glyph = it->glyph_row->glyphs[area];
24890 }
24891 glyph->charpos = CHARPOS (it->position);
24892 glyph->object = it->object;
24893 glyph->pixel_width = it->pixel_width;
24894 glyph->ascent = it->ascent;
24895 glyph->descent = it->descent;
24896 glyph->voffset = it->voffset;
24897 glyph->type = GLYPHLESS_GLYPH;
24898 glyph->u.glyphless.method = it->glyphless_method;
24899 glyph->u.glyphless.for_no_font = for_no_font;
24900 glyph->u.glyphless.len = len;
24901 glyph->u.glyphless.ch = it->c;
24902 glyph->slice.glyphless.upper_xoff = upper_xoff;
24903 glyph->slice.glyphless.upper_yoff = upper_yoff;
24904 glyph->slice.glyphless.lower_xoff = lower_xoff;
24905 glyph->slice.glyphless.lower_yoff = lower_yoff;
24906 glyph->avoid_cursor_p = it->avoid_cursor_p;
24907 glyph->multibyte_p = it->multibyte_p;
24908 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24909 {
24910 /* In R2L rows, the left and the right box edges need to be
24911 drawn in reverse direction. */
24912 glyph->right_box_line_p = it->start_of_box_run_p;
24913 glyph->left_box_line_p = it->end_of_box_run_p;
24914 }
24915 else
24916 {
24917 glyph->left_box_line_p = it->start_of_box_run_p;
24918 glyph->right_box_line_p = it->end_of_box_run_p;
24919 }
24920 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24921 || it->phys_descent > it->descent);
24922 glyph->padding_p = 0;
24923 glyph->glyph_not_available_p = 0;
24924 glyph->face_id = face_id;
24925 glyph->font_type = FONT_TYPE_UNKNOWN;
24926 if (it->bidi_p)
24927 {
24928 glyph->resolved_level = it->bidi_it.resolved_level;
24929 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24930 emacs_abort ();
24931 glyph->bidi_type = it->bidi_it.type;
24932 }
24933 ++it->glyph_row->used[area];
24934 }
24935 else
24936 IT_EXPAND_MATRIX_WIDTH (it, area);
24937 }
24938
24939
24940 /* Produce a glyph for a glyphless character for iterator IT.
24941 IT->glyphless_method specifies which method to use for displaying
24942 the character. See the description of enum
24943 glyphless_display_method in dispextern.h for the detail.
24944
24945 FOR_NO_FONT is nonzero if and only if this is for a character for
24946 which no font was found. ACRONYM, if non-nil, is an acronym string
24947 for the character. */
24948
24949 static void
24950 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24951 {
24952 int face_id;
24953 struct face *face;
24954 struct font *font;
24955 int base_width, base_height, width, height;
24956 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24957 int len;
24958
24959 /* Get the metrics of the base font. We always refer to the current
24960 ASCII face. */
24961 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24962 font = face->font ? face->font : FRAME_FONT (it->f);
24963 it->ascent = FONT_BASE (font) + font->baseline_offset;
24964 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24965 base_height = it->ascent + it->descent;
24966 base_width = font->average_width;
24967
24968 /* Get a face ID for the glyph by utilizing a cache (the same way as
24969 done for `escape-glyph' in get_next_display_element). */
24970 if (it->f == last_glyphless_glyph_frame
24971 && it->face_id == last_glyphless_glyph_face_id)
24972 {
24973 face_id = last_glyphless_glyph_merged_face_id;
24974 }
24975 else
24976 {
24977 /* Merge the `glyphless-char' face into the current face. */
24978 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24979 last_glyphless_glyph_frame = it->f;
24980 last_glyphless_glyph_face_id = it->face_id;
24981 last_glyphless_glyph_merged_face_id = face_id;
24982 }
24983
24984 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24985 {
24986 it->pixel_width = THIN_SPACE_WIDTH;
24987 len = 0;
24988 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24989 }
24990 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24991 {
24992 width = CHAR_WIDTH (it->c);
24993 if (width == 0)
24994 width = 1;
24995 else if (width > 4)
24996 width = 4;
24997 it->pixel_width = base_width * width;
24998 len = 0;
24999 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25000 }
25001 else
25002 {
25003 char buf[7];
25004 const char *str;
25005 unsigned int code[6];
25006 int upper_len;
25007 int ascent, descent;
25008 struct font_metrics metrics_upper, metrics_lower;
25009
25010 face = FACE_FROM_ID (it->f, face_id);
25011 font = face->font ? face->font : FRAME_FONT (it->f);
25012 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25013
25014 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25015 {
25016 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25017 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25018 if (CONSP (acronym))
25019 acronym = XCAR (acronym);
25020 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25021 }
25022 else
25023 {
25024 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25025 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25026 str = buf;
25027 }
25028 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25029 code[len] = font->driver->encode_char (font, str[len]);
25030 upper_len = (len + 1) / 2;
25031 font->driver->text_extents (font, code, upper_len,
25032 &metrics_upper);
25033 font->driver->text_extents (font, code + upper_len, len - upper_len,
25034 &metrics_lower);
25035
25036
25037
25038 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25039 width = max (metrics_upper.width, metrics_lower.width) + 4;
25040 upper_xoff = upper_yoff = 2; /* the typical case */
25041 if (base_width >= width)
25042 {
25043 /* Align the upper to the left, the lower to the right. */
25044 it->pixel_width = base_width;
25045 lower_xoff = base_width - 2 - metrics_lower.width;
25046 }
25047 else
25048 {
25049 /* Center the shorter one. */
25050 it->pixel_width = width;
25051 if (metrics_upper.width >= metrics_lower.width)
25052 lower_xoff = (width - metrics_lower.width) / 2;
25053 else
25054 {
25055 /* FIXME: This code doesn't look right. It formerly was
25056 missing the "lower_xoff = 0;", which couldn't have
25057 been right since it left lower_xoff uninitialized. */
25058 lower_xoff = 0;
25059 upper_xoff = (width - metrics_upper.width) / 2;
25060 }
25061 }
25062
25063 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25064 top, bottom, and between upper and lower strings. */
25065 height = (metrics_upper.ascent + metrics_upper.descent
25066 + metrics_lower.ascent + metrics_lower.descent) + 5;
25067 /* Center vertically.
25068 H:base_height, D:base_descent
25069 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25070
25071 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25072 descent = D - H/2 + h/2;
25073 lower_yoff = descent - 2 - ld;
25074 upper_yoff = lower_yoff - la - 1 - ud; */
25075 ascent = - (it->descent - (base_height + height + 1) / 2);
25076 descent = it->descent - (base_height - height) / 2;
25077 lower_yoff = descent - 2 - metrics_lower.descent;
25078 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25079 - metrics_upper.descent);
25080 /* Don't make the height shorter than the base height. */
25081 if (height > base_height)
25082 {
25083 it->ascent = ascent;
25084 it->descent = descent;
25085 }
25086 }
25087
25088 it->phys_ascent = it->ascent;
25089 it->phys_descent = it->descent;
25090 if (it->glyph_row)
25091 append_glyphless_glyph (it, face_id, for_no_font, len,
25092 upper_xoff, upper_yoff,
25093 lower_xoff, lower_yoff);
25094 it->nglyphs = 1;
25095 take_vertical_position_into_account (it);
25096 }
25097
25098
25099 /* RIF:
25100 Produce glyphs/get display metrics for the display element IT is
25101 loaded with. See the description of struct it in dispextern.h
25102 for an overview of struct it. */
25103
25104 void
25105 x_produce_glyphs (struct it *it)
25106 {
25107 int extra_line_spacing = it->extra_line_spacing;
25108
25109 it->glyph_not_available_p = 0;
25110
25111 if (it->what == IT_CHARACTER)
25112 {
25113 XChar2b char2b;
25114 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25115 struct font *font = face->font;
25116 struct font_metrics *pcm = NULL;
25117 int boff; /* baseline offset */
25118
25119 if (font == NULL)
25120 {
25121 /* When no suitable font is found, display this character by
25122 the method specified in the first extra slot of
25123 Vglyphless_char_display. */
25124 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25125
25126 eassert (it->what == IT_GLYPHLESS);
25127 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25128 goto done;
25129 }
25130
25131 boff = font->baseline_offset;
25132 if (font->vertical_centering)
25133 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25134
25135 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25136 {
25137 int stretched_p;
25138
25139 it->nglyphs = 1;
25140
25141 if (it->override_ascent >= 0)
25142 {
25143 it->ascent = it->override_ascent;
25144 it->descent = it->override_descent;
25145 boff = it->override_boff;
25146 }
25147 else
25148 {
25149 it->ascent = FONT_BASE (font) + boff;
25150 it->descent = FONT_DESCENT (font) - boff;
25151 }
25152
25153 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25154 {
25155 pcm = get_per_char_metric (font, &char2b);
25156 if (pcm->width == 0
25157 && pcm->rbearing == 0 && pcm->lbearing == 0)
25158 pcm = NULL;
25159 }
25160
25161 if (pcm)
25162 {
25163 it->phys_ascent = pcm->ascent + boff;
25164 it->phys_descent = pcm->descent - boff;
25165 it->pixel_width = pcm->width;
25166 }
25167 else
25168 {
25169 it->glyph_not_available_p = 1;
25170 it->phys_ascent = it->ascent;
25171 it->phys_descent = it->descent;
25172 it->pixel_width = font->space_width;
25173 }
25174
25175 if (it->constrain_row_ascent_descent_p)
25176 {
25177 if (it->descent > it->max_descent)
25178 {
25179 it->ascent += it->descent - it->max_descent;
25180 it->descent = it->max_descent;
25181 }
25182 if (it->ascent > it->max_ascent)
25183 {
25184 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25185 it->ascent = it->max_ascent;
25186 }
25187 it->phys_ascent = min (it->phys_ascent, it->ascent);
25188 it->phys_descent = min (it->phys_descent, it->descent);
25189 extra_line_spacing = 0;
25190 }
25191
25192 /* If this is a space inside a region of text with
25193 `space-width' property, change its width. */
25194 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25195 if (stretched_p)
25196 it->pixel_width *= XFLOATINT (it->space_width);
25197
25198 /* If face has a box, add the box thickness to the character
25199 height. If character has a box line to the left and/or
25200 right, add the box line width to the character's width. */
25201 if (face->box != FACE_NO_BOX)
25202 {
25203 int thick = face->box_line_width;
25204
25205 if (thick > 0)
25206 {
25207 it->ascent += thick;
25208 it->descent += thick;
25209 }
25210 else
25211 thick = -thick;
25212
25213 if (it->start_of_box_run_p)
25214 it->pixel_width += thick;
25215 if (it->end_of_box_run_p)
25216 it->pixel_width += thick;
25217 }
25218
25219 /* If face has an overline, add the height of the overline
25220 (1 pixel) and a 1 pixel margin to the character height. */
25221 if (face->overline_p)
25222 it->ascent += overline_margin;
25223
25224 if (it->constrain_row_ascent_descent_p)
25225 {
25226 if (it->ascent > it->max_ascent)
25227 it->ascent = it->max_ascent;
25228 if (it->descent > it->max_descent)
25229 it->descent = it->max_descent;
25230 }
25231
25232 take_vertical_position_into_account (it);
25233
25234 /* If we have to actually produce glyphs, do it. */
25235 if (it->glyph_row)
25236 {
25237 if (stretched_p)
25238 {
25239 /* Translate a space with a `space-width' property
25240 into a stretch glyph. */
25241 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25242 / FONT_HEIGHT (font));
25243 append_stretch_glyph (it, it->object, it->pixel_width,
25244 it->ascent + it->descent, ascent);
25245 }
25246 else
25247 append_glyph (it);
25248
25249 /* If characters with lbearing or rbearing are displayed
25250 in this line, record that fact in a flag of the
25251 glyph row. This is used to optimize X output code. */
25252 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25253 it->glyph_row->contains_overlapping_glyphs_p = 1;
25254 }
25255 if (! stretched_p && it->pixel_width == 0)
25256 /* We assure that all visible glyphs have at least 1-pixel
25257 width. */
25258 it->pixel_width = 1;
25259 }
25260 else if (it->char_to_display == '\n')
25261 {
25262 /* A newline has no width, but we need the height of the
25263 line. But if previous part of the line sets a height,
25264 don't increase that height */
25265
25266 Lisp_Object height;
25267 Lisp_Object total_height = Qnil;
25268
25269 it->override_ascent = -1;
25270 it->pixel_width = 0;
25271 it->nglyphs = 0;
25272
25273 height = get_it_property (it, Qline_height);
25274 /* Split (line-height total-height) list */
25275 if (CONSP (height)
25276 && CONSP (XCDR (height))
25277 && NILP (XCDR (XCDR (height))))
25278 {
25279 total_height = XCAR (XCDR (height));
25280 height = XCAR (height);
25281 }
25282 height = calc_line_height_property (it, height, font, boff, 1);
25283
25284 if (it->override_ascent >= 0)
25285 {
25286 it->ascent = it->override_ascent;
25287 it->descent = it->override_descent;
25288 boff = it->override_boff;
25289 }
25290 else
25291 {
25292 it->ascent = FONT_BASE (font) + boff;
25293 it->descent = FONT_DESCENT (font) - boff;
25294 }
25295
25296 if (EQ (height, Qt))
25297 {
25298 if (it->descent > it->max_descent)
25299 {
25300 it->ascent += it->descent - it->max_descent;
25301 it->descent = it->max_descent;
25302 }
25303 if (it->ascent > it->max_ascent)
25304 {
25305 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25306 it->ascent = it->max_ascent;
25307 }
25308 it->phys_ascent = min (it->phys_ascent, it->ascent);
25309 it->phys_descent = min (it->phys_descent, it->descent);
25310 it->constrain_row_ascent_descent_p = 1;
25311 extra_line_spacing = 0;
25312 }
25313 else
25314 {
25315 Lisp_Object spacing;
25316
25317 it->phys_ascent = it->ascent;
25318 it->phys_descent = it->descent;
25319
25320 if ((it->max_ascent > 0 || it->max_descent > 0)
25321 && face->box != FACE_NO_BOX
25322 && face->box_line_width > 0)
25323 {
25324 it->ascent += face->box_line_width;
25325 it->descent += face->box_line_width;
25326 }
25327 if (!NILP (height)
25328 && XINT (height) > it->ascent + it->descent)
25329 it->ascent = XINT (height) - it->descent;
25330
25331 if (!NILP (total_height))
25332 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25333 else
25334 {
25335 spacing = get_it_property (it, Qline_spacing);
25336 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25337 }
25338 if (INTEGERP (spacing))
25339 {
25340 extra_line_spacing = XINT (spacing);
25341 if (!NILP (total_height))
25342 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25343 }
25344 }
25345 }
25346 else /* i.e. (it->char_to_display == '\t') */
25347 {
25348 if (font->space_width > 0)
25349 {
25350 int tab_width = it->tab_width * font->space_width;
25351 int x = it->current_x + it->continuation_lines_width;
25352 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25353
25354 /* If the distance from the current position to the next tab
25355 stop is less than a space character width, use the
25356 tab stop after that. */
25357 if (next_tab_x - x < font->space_width)
25358 next_tab_x += tab_width;
25359
25360 it->pixel_width = next_tab_x - x;
25361 it->nglyphs = 1;
25362 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25363 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25364
25365 if (it->glyph_row)
25366 {
25367 append_stretch_glyph (it, it->object, it->pixel_width,
25368 it->ascent + it->descent, it->ascent);
25369 }
25370 }
25371 else
25372 {
25373 it->pixel_width = 0;
25374 it->nglyphs = 1;
25375 }
25376 }
25377 }
25378 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25379 {
25380 /* A static composition.
25381
25382 Note: A composition is represented as one glyph in the
25383 glyph matrix. There are no padding glyphs.
25384
25385 Important note: pixel_width, ascent, and descent are the
25386 values of what is drawn by draw_glyphs (i.e. the values of
25387 the overall glyphs composed). */
25388 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25389 int boff; /* baseline offset */
25390 struct composition *cmp = composition_table[it->cmp_it.id];
25391 int glyph_len = cmp->glyph_len;
25392 struct font *font = face->font;
25393
25394 it->nglyphs = 1;
25395
25396 /* If we have not yet calculated pixel size data of glyphs of
25397 the composition for the current face font, calculate them
25398 now. Theoretically, we have to check all fonts for the
25399 glyphs, but that requires much time and memory space. So,
25400 here we check only the font of the first glyph. This may
25401 lead to incorrect display, but it's very rare, and C-l
25402 (recenter-top-bottom) can correct the display anyway. */
25403 if (! cmp->font || cmp->font != font)
25404 {
25405 /* Ascent and descent of the font of the first character
25406 of this composition (adjusted by baseline offset).
25407 Ascent and descent of overall glyphs should not be less
25408 than these, respectively. */
25409 int font_ascent, font_descent, font_height;
25410 /* Bounding box of the overall glyphs. */
25411 int leftmost, rightmost, lowest, highest;
25412 int lbearing, rbearing;
25413 int i, width, ascent, descent;
25414 int left_padded = 0, right_padded = 0;
25415 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25416 XChar2b char2b;
25417 struct font_metrics *pcm;
25418 int font_not_found_p;
25419 ptrdiff_t pos;
25420
25421 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25422 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25423 break;
25424 if (glyph_len < cmp->glyph_len)
25425 right_padded = 1;
25426 for (i = 0; i < glyph_len; i++)
25427 {
25428 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25429 break;
25430 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25431 }
25432 if (i > 0)
25433 left_padded = 1;
25434
25435 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25436 : IT_CHARPOS (*it));
25437 /* If no suitable font is found, use the default font. */
25438 font_not_found_p = font == NULL;
25439 if (font_not_found_p)
25440 {
25441 face = face->ascii_face;
25442 font = face->font;
25443 }
25444 boff = font->baseline_offset;
25445 if (font->vertical_centering)
25446 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25447 font_ascent = FONT_BASE (font) + boff;
25448 font_descent = FONT_DESCENT (font) - boff;
25449 font_height = FONT_HEIGHT (font);
25450
25451 cmp->font = font;
25452
25453 pcm = NULL;
25454 if (! font_not_found_p)
25455 {
25456 get_char_face_and_encoding (it->f, c, it->face_id,
25457 &char2b, 0);
25458 pcm = get_per_char_metric (font, &char2b);
25459 }
25460
25461 /* Initialize the bounding box. */
25462 if (pcm)
25463 {
25464 width = cmp->glyph_len > 0 ? pcm->width : 0;
25465 ascent = pcm->ascent;
25466 descent = pcm->descent;
25467 lbearing = pcm->lbearing;
25468 rbearing = pcm->rbearing;
25469 }
25470 else
25471 {
25472 width = cmp->glyph_len > 0 ? font->space_width : 0;
25473 ascent = FONT_BASE (font);
25474 descent = FONT_DESCENT (font);
25475 lbearing = 0;
25476 rbearing = width;
25477 }
25478
25479 rightmost = width;
25480 leftmost = 0;
25481 lowest = - descent + boff;
25482 highest = ascent + boff;
25483
25484 if (! font_not_found_p
25485 && font->default_ascent
25486 && CHAR_TABLE_P (Vuse_default_ascent)
25487 && !NILP (Faref (Vuse_default_ascent,
25488 make_number (it->char_to_display))))
25489 highest = font->default_ascent + boff;
25490
25491 /* Draw the first glyph at the normal position. It may be
25492 shifted to right later if some other glyphs are drawn
25493 at the left. */
25494 cmp->offsets[i * 2] = 0;
25495 cmp->offsets[i * 2 + 1] = boff;
25496 cmp->lbearing = lbearing;
25497 cmp->rbearing = rbearing;
25498
25499 /* Set cmp->offsets for the remaining glyphs. */
25500 for (i++; i < glyph_len; i++)
25501 {
25502 int left, right, btm, top;
25503 int ch = COMPOSITION_GLYPH (cmp, i);
25504 int face_id;
25505 struct face *this_face;
25506
25507 if (ch == '\t')
25508 ch = ' ';
25509 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25510 this_face = FACE_FROM_ID (it->f, face_id);
25511 font = this_face->font;
25512
25513 if (font == NULL)
25514 pcm = NULL;
25515 else
25516 {
25517 get_char_face_and_encoding (it->f, ch, face_id,
25518 &char2b, 0);
25519 pcm = get_per_char_metric (font, &char2b);
25520 }
25521 if (! pcm)
25522 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25523 else
25524 {
25525 width = pcm->width;
25526 ascent = pcm->ascent;
25527 descent = pcm->descent;
25528 lbearing = pcm->lbearing;
25529 rbearing = pcm->rbearing;
25530 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25531 {
25532 /* Relative composition with or without
25533 alternate chars. */
25534 left = (leftmost + rightmost - width) / 2;
25535 btm = - descent + boff;
25536 if (font->relative_compose
25537 && (! CHAR_TABLE_P (Vignore_relative_composition)
25538 || NILP (Faref (Vignore_relative_composition,
25539 make_number (ch)))))
25540 {
25541
25542 if (- descent >= font->relative_compose)
25543 /* One extra pixel between two glyphs. */
25544 btm = highest + 1;
25545 else if (ascent <= 0)
25546 /* One extra pixel between two glyphs. */
25547 btm = lowest - 1 - ascent - descent;
25548 }
25549 }
25550 else
25551 {
25552 /* A composition rule is specified by an integer
25553 value that encodes global and new reference
25554 points (GREF and NREF). GREF and NREF are
25555 specified by numbers as below:
25556
25557 0---1---2 -- ascent
25558 | |
25559 | |
25560 | |
25561 9--10--11 -- center
25562 | |
25563 ---3---4---5--- baseline
25564 | |
25565 6---7---8 -- descent
25566 */
25567 int rule = COMPOSITION_RULE (cmp, i);
25568 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25569
25570 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25571 grefx = gref % 3, nrefx = nref % 3;
25572 grefy = gref / 3, nrefy = nref / 3;
25573 if (xoff)
25574 xoff = font_height * (xoff - 128) / 256;
25575 if (yoff)
25576 yoff = font_height * (yoff - 128) / 256;
25577
25578 left = (leftmost
25579 + grefx * (rightmost - leftmost) / 2
25580 - nrefx * width / 2
25581 + xoff);
25582
25583 btm = ((grefy == 0 ? highest
25584 : grefy == 1 ? 0
25585 : grefy == 2 ? lowest
25586 : (highest + lowest) / 2)
25587 - (nrefy == 0 ? ascent + descent
25588 : nrefy == 1 ? descent - boff
25589 : nrefy == 2 ? 0
25590 : (ascent + descent) / 2)
25591 + yoff);
25592 }
25593
25594 cmp->offsets[i * 2] = left;
25595 cmp->offsets[i * 2 + 1] = btm + descent;
25596
25597 /* Update the bounding box of the overall glyphs. */
25598 if (width > 0)
25599 {
25600 right = left + width;
25601 if (left < leftmost)
25602 leftmost = left;
25603 if (right > rightmost)
25604 rightmost = right;
25605 }
25606 top = btm + descent + ascent;
25607 if (top > highest)
25608 highest = top;
25609 if (btm < lowest)
25610 lowest = btm;
25611
25612 if (cmp->lbearing > left + lbearing)
25613 cmp->lbearing = left + lbearing;
25614 if (cmp->rbearing < left + rbearing)
25615 cmp->rbearing = left + rbearing;
25616 }
25617 }
25618
25619 /* If there are glyphs whose x-offsets are negative,
25620 shift all glyphs to the right and make all x-offsets
25621 non-negative. */
25622 if (leftmost < 0)
25623 {
25624 for (i = 0; i < cmp->glyph_len; i++)
25625 cmp->offsets[i * 2] -= leftmost;
25626 rightmost -= leftmost;
25627 cmp->lbearing -= leftmost;
25628 cmp->rbearing -= leftmost;
25629 }
25630
25631 if (left_padded && cmp->lbearing < 0)
25632 {
25633 for (i = 0; i < cmp->glyph_len; i++)
25634 cmp->offsets[i * 2] -= cmp->lbearing;
25635 rightmost -= cmp->lbearing;
25636 cmp->rbearing -= cmp->lbearing;
25637 cmp->lbearing = 0;
25638 }
25639 if (right_padded && rightmost < cmp->rbearing)
25640 {
25641 rightmost = cmp->rbearing;
25642 }
25643
25644 cmp->pixel_width = rightmost;
25645 cmp->ascent = highest;
25646 cmp->descent = - lowest;
25647 if (cmp->ascent < font_ascent)
25648 cmp->ascent = font_ascent;
25649 if (cmp->descent < font_descent)
25650 cmp->descent = font_descent;
25651 }
25652
25653 if (it->glyph_row
25654 && (cmp->lbearing < 0
25655 || cmp->rbearing > cmp->pixel_width))
25656 it->glyph_row->contains_overlapping_glyphs_p = 1;
25657
25658 it->pixel_width = cmp->pixel_width;
25659 it->ascent = it->phys_ascent = cmp->ascent;
25660 it->descent = it->phys_descent = cmp->descent;
25661 if (face->box != FACE_NO_BOX)
25662 {
25663 int thick = face->box_line_width;
25664
25665 if (thick > 0)
25666 {
25667 it->ascent += thick;
25668 it->descent += thick;
25669 }
25670 else
25671 thick = - thick;
25672
25673 if (it->start_of_box_run_p)
25674 it->pixel_width += thick;
25675 if (it->end_of_box_run_p)
25676 it->pixel_width += thick;
25677 }
25678
25679 /* If face has an overline, add the height of the overline
25680 (1 pixel) and a 1 pixel margin to the character height. */
25681 if (face->overline_p)
25682 it->ascent += overline_margin;
25683
25684 take_vertical_position_into_account (it);
25685 if (it->ascent < 0)
25686 it->ascent = 0;
25687 if (it->descent < 0)
25688 it->descent = 0;
25689
25690 if (it->glyph_row && cmp->glyph_len > 0)
25691 append_composite_glyph (it);
25692 }
25693 else if (it->what == IT_COMPOSITION)
25694 {
25695 /* A dynamic (automatic) composition. */
25696 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25697 Lisp_Object gstring;
25698 struct font_metrics metrics;
25699
25700 it->nglyphs = 1;
25701
25702 gstring = composition_gstring_from_id (it->cmp_it.id);
25703 it->pixel_width
25704 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25705 &metrics);
25706 if (it->glyph_row
25707 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25708 it->glyph_row->contains_overlapping_glyphs_p = 1;
25709 it->ascent = it->phys_ascent = metrics.ascent;
25710 it->descent = it->phys_descent = metrics.descent;
25711 if (face->box != FACE_NO_BOX)
25712 {
25713 int thick = face->box_line_width;
25714
25715 if (thick > 0)
25716 {
25717 it->ascent += thick;
25718 it->descent += thick;
25719 }
25720 else
25721 thick = - thick;
25722
25723 if (it->start_of_box_run_p)
25724 it->pixel_width += thick;
25725 if (it->end_of_box_run_p)
25726 it->pixel_width += thick;
25727 }
25728 /* If face has an overline, add the height of the overline
25729 (1 pixel) and a 1 pixel margin to the character height. */
25730 if (face->overline_p)
25731 it->ascent += overline_margin;
25732 take_vertical_position_into_account (it);
25733 if (it->ascent < 0)
25734 it->ascent = 0;
25735 if (it->descent < 0)
25736 it->descent = 0;
25737
25738 if (it->glyph_row)
25739 append_composite_glyph (it);
25740 }
25741 else if (it->what == IT_GLYPHLESS)
25742 produce_glyphless_glyph (it, 0, Qnil);
25743 else if (it->what == IT_IMAGE)
25744 produce_image_glyph (it);
25745 else if (it->what == IT_STRETCH)
25746 produce_stretch_glyph (it);
25747
25748 done:
25749 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25750 because this isn't true for images with `:ascent 100'. */
25751 eassert (it->ascent >= 0 && it->descent >= 0);
25752 if (it->area == TEXT_AREA)
25753 it->current_x += it->pixel_width;
25754
25755 if (extra_line_spacing > 0)
25756 {
25757 it->descent += extra_line_spacing;
25758 if (extra_line_spacing > it->max_extra_line_spacing)
25759 it->max_extra_line_spacing = extra_line_spacing;
25760 }
25761
25762 it->max_ascent = max (it->max_ascent, it->ascent);
25763 it->max_descent = max (it->max_descent, it->descent);
25764 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25765 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25766 }
25767
25768 /* EXPORT for RIF:
25769 Output LEN glyphs starting at START at the nominal cursor position.
25770 Advance the nominal cursor over the text. The global variable
25771 updated_window contains the window being updated, updated_row is
25772 the glyph row being updated, and updated_area is the area of that
25773 row being updated. */
25774
25775 void
25776 x_write_glyphs (struct glyph *start, int len)
25777 {
25778 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25779
25780 eassert (updated_window && updated_row);
25781 /* When the window is hscrolled, cursor hpos can legitimately be out
25782 of bounds, but we draw the cursor at the corresponding window
25783 margin in that case. */
25784 if (!updated_row->reversed_p && chpos < 0)
25785 chpos = 0;
25786 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25787 chpos = updated_row->used[TEXT_AREA] - 1;
25788
25789 block_input ();
25790
25791 /* Write glyphs. */
25792
25793 hpos = start - updated_row->glyphs[updated_area];
25794 x = draw_glyphs (updated_window, output_cursor.x,
25795 updated_row, updated_area,
25796 hpos, hpos + len,
25797 DRAW_NORMAL_TEXT, 0);
25798
25799 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25800 if (updated_area == TEXT_AREA
25801 && updated_window->phys_cursor_on_p
25802 && updated_window->phys_cursor.vpos == output_cursor.vpos
25803 && chpos >= hpos
25804 && chpos < hpos + len)
25805 updated_window->phys_cursor_on_p = 0;
25806
25807 unblock_input ();
25808
25809 /* Advance the output cursor. */
25810 output_cursor.hpos += len;
25811 output_cursor.x = x;
25812 }
25813
25814
25815 /* EXPORT for RIF:
25816 Insert LEN glyphs from START at the nominal cursor position. */
25817
25818 void
25819 x_insert_glyphs (struct glyph *start, int len)
25820 {
25821 struct frame *f;
25822 struct window *w;
25823 int line_height, shift_by_width, shifted_region_width;
25824 struct glyph_row *row;
25825 struct glyph *glyph;
25826 int frame_x, frame_y;
25827 ptrdiff_t hpos;
25828
25829 eassert (updated_window && updated_row);
25830 block_input ();
25831 w = updated_window;
25832 f = XFRAME (WINDOW_FRAME (w));
25833
25834 /* Get the height of the line we are in. */
25835 row = updated_row;
25836 line_height = row->height;
25837
25838 /* Get the width of the glyphs to insert. */
25839 shift_by_width = 0;
25840 for (glyph = start; glyph < start + len; ++glyph)
25841 shift_by_width += glyph->pixel_width;
25842
25843 /* Get the width of the region to shift right. */
25844 shifted_region_width = (window_box_width (w, updated_area)
25845 - output_cursor.x
25846 - shift_by_width);
25847
25848 /* Shift right. */
25849 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25850 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25851
25852 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25853 line_height, shift_by_width);
25854
25855 /* Write the glyphs. */
25856 hpos = start - row->glyphs[updated_area];
25857 draw_glyphs (w, output_cursor.x, row, updated_area,
25858 hpos, hpos + len,
25859 DRAW_NORMAL_TEXT, 0);
25860
25861 /* Advance the output cursor. */
25862 output_cursor.hpos += len;
25863 output_cursor.x += shift_by_width;
25864 unblock_input ();
25865 }
25866
25867
25868 /* EXPORT for RIF:
25869 Erase the current text line from the nominal cursor position
25870 (inclusive) to pixel column TO_X (exclusive). The idea is that
25871 everything from TO_X onward is already erased.
25872
25873 TO_X is a pixel position relative to updated_area of
25874 updated_window. TO_X == -1 means clear to the end of this area. */
25875
25876 void
25877 x_clear_end_of_line (int to_x)
25878 {
25879 struct frame *f;
25880 struct window *w = updated_window;
25881 int max_x, min_y, max_y;
25882 int from_x, from_y, to_y;
25883
25884 eassert (updated_window && updated_row);
25885 f = XFRAME (w->frame);
25886
25887 if (updated_row->full_width_p)
25888 max_x = WINDOW_TOTAL_WIDTH (w);
25889 else
25890 max_x = window_box_width (w, updated_area);
25891 max_y = window_text_bottom_y (w);
25892
25893 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25894 of window. For TO_X > 0, truncate to end of drawing area. */
25895 if (to_x == 0)
25896 return;
25897 else if (to_x < 0)
25898 to_x = max_x;
25899 else
25900 to_x = min (to_x, max_x);
25901
25902 to_y = min (max_y, output_cursor.y + updated_row->height);
25903
25904 /* Notice if the cursor will be cleared by this operation. */
25905 if (!updated_row->full_width_p)
25906 notice_overwritten_cursor (w, updated_area,
25907 output_cursor.x, -1,
25908 updated_row->y,
25909 MATRIX_ROW_BOTTOM_Y (updated_row));
25910
25911 from_x = output_cursor.x;
25912
25913 /* Translate to frame coordinates. */
25914 if (updated_row->full_width_p)
25915 {
25916 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25917 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25918 }
25919 else
25920 {
25921 int area_left = window_box_left (w, updated_area);
25922 from_x += area_left;
25923 to_x += area_left;
25924 }
25925
25926 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25927 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25928 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25929
25930 /* Prevent inadvertently clearing to end of the X window. */
25931 if (to_x > from_x && to_y > from_y)
25932 {
25933 block_input ();
25934 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25935 to_x - from_x, to_y - from_y);
25936 unblock_input ();
25937 }
25938 }
25939
25940 #endif /* HAVE_WINDOW_SYSTEM */
25941
25942
25943 \f
25944 /***********************************************************************
25945 Cursor types
25946 ***********************************************************************/
25947
25948 /* Value is the internal representation of the specified cursor type
25949 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25950 of the bar cursor. */
25951
25952 static enum text_cursor_kinds
25953 get_specified_cursor_type (Lisp_Object arg, int *width)
25954 {
25955 enum text_cursor_kinds type;
25956
25957 if (NILP (arg))
25958 return NO_CURSOR;
25959
25960 if (EQ (arg, Qbox))
25961 return FILLED_BOX_CURSOR;
25962
25963 if (EQ (arg, Qhollow))
25964 return HOLLOW_BOX_CURSOR;
25965
25966 if (EQ (arg, Qbar))
25967 {
25968 *width = 2;
25969 return BAR_CURSOR;
25970 }
25971
25972 if (CONSP (arg)
25973 && EQ (XCAR (arg), Qbar)
25974 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25975 {
25976 *width = XINT (XCDR (arg));
25977 return BAR_CURSOR;
25978 }
25979
25980 if (EQ (arg, Qhbar))
25981 {
25982 *width = 2;
25983 return HBAR_CURSOR;
25984 }
25985
25986 if (CONSP (arg)
25987 && EQ (XCAR (arg), Qhbar)
25988 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25989 {
25990 *width = XINT (XCDR (arg));
25991 return HBAR_CURSOR;
25992 }
25993
25994 /* Treat anything unknown as "hollow box cursor".
25995 It was bad to signal an error; people have trouble fixing
25996 .Xdefaults with Emacs, when it has something bad in it. */
25997 type = HOLLOW_BOX_CURSOR;
25998
25999 return type;
26000 }
26001
26002 /* Set the default cursor types for specified frame. */
26003 void
26004 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26005 {
26006 int width = 1;
26007 Lisp_Object tem;
26008
26009 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26010 FRAME_CURSOR_WIDTH (f) = width;
26011
26012 /* By default, set up the blink-off state depending on the on-state. */
26013
26014 tem = Fassoc (arg, Vblink_cursor_alist);
26015 if (!NILP (tem))
26016 {
26017 FRAME_BLINK_OFF_CURSOR (f)
26018 = get_specified_cursor_type (XCDR (tem), &width);
26019 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26020 }
26021 else
26022 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26023 }
26024
26025
26026 #ifdef HAVE_WINDOW_SYSTEM
26027
26028 /* Return the cursor we want to be displayed in window W. Return
26029 width of bar/hbar cursor through WIDTH arg. Return with
26030 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26031 (i.e. if the `system caret' should track this cursor).
26032
26033 In a mini-buffer window, we want the cursor only to appear if we
26034 are reading input from this window. For the selected window, we
26035 want the cursor type given by the frame parameter or buffer local
26036 setting of cursor-type. If explicitly marked off, draw no cursor.
26037 In all other cases, we want a hollow box cursor. */
26038
26039 static enum text_cursor_kinds
26040 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26041 int *active_cursor)
26042 {
26043 struct frame *f = XFRAME (w->frame);
26044 struct buffer *b = XBUFFER (w->contents);
26045 int cursor_type = DEFAULT_CURSOR;
26046 Lisp_Object alt_cursor;
26047 int non_selected = 0;
26048
26049 *active_cursor = 1;
26050
26051 /* Echo area */
26052 if (cursor_in_echo_area
26053 && FRAME_HAS_MINIBUF_P (f)
26054 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26055 {
26056 if (w == XWINDOW (echo_area_window))
26057 {
26058 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26059 {
26060 *width = FRAME_CURSOR_WIDTH (f);
26061 return FRAME_DESIRED_CURSOR (f);
26062 }
26063 else
26064 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26065 }
26066
26067 *active_cursor = 0;
26068 non_selected = 1;
26069 }
26070
26071 /* Detect a nonselected window or nonselected frame. */
26072 else if (w != XWINDOW (f->selected_window)
26073 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26074 {
26075 *active_cursor = 0;
26076
26077 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26078 return NO_CURSOR;
26079
26080 non_selected = 1;
26081 }
26082
26083 /* Never display a cursor in a window in which cursor-type is nil. */
26084 if (NILP (BVAR (b, cursor_type)))
26085 return NO_CURSOR;
26086
26087 /* Get the normal cursor type for this window. */
26088 if (EQ (BVAR (b, cursor_type), Qt))
26089 {
26090 cursor_type = FRAME_DESIRED_CURSOR (f);
26091 *width = FRAME_CURSOR_WIDTH (f);
26092 }
26093 else
26094 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26095
26096 /* Use cursor-in-non-selected-windows instead
26097 for non-selected window or frame. */
26098 if (non_selected)
26099 {
26100 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26101 if (!EQ (Qt, alt_cursor))
26102 return get_specified_cursor_type (alt_cursor, width);
26103 /* t means modify the normal cursor type. */
26104 if (cursor_type == FILLED_BOX_CURSOR)
26105 cursor_type = HOLLOW_BOX_CURSOR;
26106 else if (cursor_type == BAR_CURSOR && *width > 1)
26107 --*width;
26108 return cursor_type;
26109 }
26110
26111 /* Use normal cursor if not blinked off. */
26112 if (!w->cursor_off_p)
26113 {
26114 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26115 {
26116 if (cursor_type == FILLED_BOX_CURSOR)
26117 {
26118 /* Using a block cursor on large images can be very annoying.
26119 So use a hollow cursor for "large" images.
26120 If image is not transparent (no mask), also use hollow cursor. */
26121 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26122 if (img != NULL && IMAGEP (img->spec))
26123 {
26124 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26125 where N = size of default frame font size.
26126 This should cover most of the "tiny" icons people may use. */
26127 if (!img->mask
26128 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26129 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26130 cursor_type = HOLLOW_BOX_CURSOR;
26131 }
26132 }
26133 else if (cursor_type != NO_CURSOR)
26134 {
26135 /* Display current only supports BOX and HOLLOW cursors for images.
26136 So for now, unconditionally use a HOLLOW cursor when cursor is
26137 not a solid box cursor. */
26138 cursor_type = HOLLOW_BOX_CURSOR;
26139 }
26140 }
26141 return cursor_type;
26142 }
26143
26144 /* Cursor is blinked off, so determine how to "toggle" it. */
26145
26146 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26147 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26148 return get_specified_cursor_type (XCDR (alt_cursor), width);
26149
26150 /* Then see if frame has specified a specific blink off cursor type. */
26151 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26152 {
26153 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26154 return FRAME_BLINK_OFF_CURSOR (f);
26155 }
26156
26157 #if 0
26158 /* Some people liked having a permanently visible blinking cursor,
26159 while others had very strong opinions against it. So it was
26160 decided to remove it. KFS 2003-09-03 */
26161
26162 /* Finally perform built-in cursor blinking:
26163 filled box <-> hollow box
26164 wide [h]bar <-> narrow [h]bar
26165 narrow [h]bar <-> no cursor
26166 other type <-> no cursor */
26167
26168 if (cursor_type == FILLED_BOX_CURSOR)
26169 return HOLLOW_BOX_CURSOR;
26170
26171 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26172 {
26173 *width = 1;
26174 return cursor_type;
26175 }
26176 #endif
26177
26178 return NO_CURSOR;
26179 }
26180
26181
26182 /* Notice when the text cursor of window W has been completely
26183 overwritten by a drawing operation that outputs glyphs in AREA
26184 starting at X0 and ending at X1 in the line starting at Y0 and
26185 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26186 the rest of the line after X0 has been written. Y coordinates
26187 are window-relative. */
26188
26189 static void
26190 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26191 int x0, int x1, int y0, int y1)
26192 {
26193 int cx0, cx1, cy0, cy1;
26194 struct glyph_row *row;
26195
26196 if (!w->phys_cursor_on_p)
26197 return;
26198 if (area != TEXT_AREA)
26199 return;
26200
26201 if (w->phys_cursor.vpos < 0
26202 || w->phys_cursor.vpos >= w->current_matrix->nrows
26203 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26204 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26205 return;
26206
26207 if (row->cursor_in_fringe_p)
26208 {
26209 row->cursor_in_fringe_p = 0;
26210 draw_fringe_bitmap (w, row, row->reversed_p);
26211 w->phys_cursor_on_p = 0;
26212 return;
26213 }
26214
26215 cx0 = w->phys_cursor.x;
26216 cx1 = cx0 + w->phys_cursor_width;
26217 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26218 return;
26219
26220 /* The cursor image will be completely removed from the
26221 screen if the output area intersects the cursor area in
26222 y-direction. When we draw in [y0 y1[, and some part of
26223 the cursor is at y < y0, that part must have been drawn
26224 before. When scrolling, the cursor is erased before
26225 actually scrolling, so we don't come here. When not
26226 scrolling, the rows above the old cursor row must have
26227 changed, and in this case these rows must have written
26228 over the cursor image.
26229
26230 Likewise if part of the cursor is below y1, with the
26231 exception of the cursor being in the first blank row at
26232 the buffer and window end because update_text_area
26233 doesn't draw that row. (Except when it does, but
26234 that's handled in update_text_area.) */
26235
26236 cy0 = w->phys_cursor.y;
26237 cy1 = cy0 + w->phys_cursor_height;
26238 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26239 return;
26240
26241 w->phys_cursor_on_p = 0;
26242 }
26243
26244 #endif /* HAVE_WINDOW_SYSTEM */
26245
26246 \f
26247 /************************************************************************
26248 Mouse Face
26249 ************************************************************************/
26250
26251 #ifdef HAVE_WINDOW_SYSTEM
26252
26253 /* EXPORT for RIF:
26254 Fix the display of area AREA of overlapping row ROW in window W
26255 with respect to the overlapping part OVERLAPS. */
26256
26257 void
26258 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26259 enum glyph_row_area area, int overlaps)
26260 {
26261 int i, x;
26262
26263 block_input ();
26264
26265 x = 0;
26266 for (i = 0; i < row->used[area];)
26267 {
26268 if (row->glyphs[area][i].overlaps_vertically_p)
26269 {
26270 int start = i, start_x = x;
26271
26272 do
26273 {
26274 x += row->glyphs[area][i].pixel_width;
26275 ++i;
26276 }
26277 while (i < row->used[area]
26278 && row->glyphs[area][i].overlaps_vertically_p);
26279
26280 draw_glyphs (w, start_x, row, area,
26281 start, i,
26282 DRAW_NORMAL_TEXT, overlaps);
26283 }
26284 else
26285 {
26286 x += row->glyphs[area][i].pixel_width;
26287 ++i;
26288 }
26289 }
26290
26291 unblock_input ();
26292 }
26293
26294
26295 /* EXPORT:
26296 Draw the cursor glyph of window W in glyph row ROW. See the
26297 comment of draw_glyphs for the meaning of HL. */
26298
26299 void
26300 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26301 enum draw_glyphs_face hl)
26302 {
26303 /* If cursor hpos is out of bounds, don't draw garbage. This can
26304 happen in mini-buffer windows when switching between echo area
26305 glyphs and mini-buffer. */
26306 if ((row->reversed_p
26307 ? (w->phys_cursor.hpos >= 0)
26308 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26309 {
26310 int on_p = w->phys_cursor_on_p;
26311 int x1;
26312 int hpos = w->phys_cursor.hpos;
26313
26314 /* When the window is hscrolled, cursor hpos can legitimately be
26315 out of bounds, but we draw the cursor at the corresponding
26316 window margin in that case. */
26317 if (!row->reversed_p && hpos < 0)
26318 hpos = 0;
26319 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26320 hpos = row->used[TEXT_AREA] - 1;
26321
26322 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26323 hl, 0);
26324 w->phys_cursor_on_p = on_p;
26325
26326 if (hl == DRAW_CURSOR)
26327 w->phys_cursor_width = x1 - w->phys_cursor.x;
26328 /* When we erase the cursor, and ROW is overlapped by other
26329 rows, make sure that these overlapping parts of other rows
26330 are redrawn. */
26331 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26332 {
26333 w->phys_cursor_width = x1 - w->phys_cursor.x;
26334
26335 if (row > w->current_matrix->rows
26336 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26337 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26338 OVERLAPS_ERASED_CURSOR);
26339
26340 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26341 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26342 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26343 OVERLAPS_ERASED_CURSOR);
26344 }
26345 }
26346 }
26347
26348
26349 /* EXPORT:
26350 Erase the image of a cursor of window W from the screen. */
26351
26352 void
26353 erase_phys_cursor (struct window *w)
26354 {
26355 struct frame *f = XFRAME (w->frame);
26356 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26357 int hpos = w->phys_cursor.hpos;
26358 int vpos = w->phys_cursor.vpos;
26359 int mouse_face_here_p = 0;
26360 struct glyph_matrix *active_glyphs = w->current_matrix;
26361 struct glyph_row *cursor_row;
26362 struct glyph *cursor_glyph;
26363 enum draw_glyphs_face hl;
26364
26365 /* No cursor displayed or row invalidated => nothing to do on the
26366 screen. */
26367 if (w->phys_cursor_type == NO_CURSOR)
26368 goto mark_cursor_off;
26369
26370 /* VPOS >= active_glyphs->nrows means that window has been resized.
26371 Don't bother to erase the cursor. */
26372 if (vpos >= active_glyphs->nrows)
26373 goto mark_cursor_off;
26374
26375 /* If row containing cursor is marked invalid, there is nothing we
26376 can do. */
26377 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26378 if (!cursor_row->enabled_p)
26379 goto mark_cursor_off;
26380
26381 /* If line spacing is > 0, old cursor may only be partially visible in
26382 window after split-window. So adjust visible height. */
26383 cursor_row->visible_height = min (cursor_row->visible_height,
26384 window_text_bottom_y (w) - cursor_row->y);
26385
26386 /* If row is completely invisible, don't attempt to delete a cursor which
26387 isn't there. This can happen if cursor is at top of a window, and
26388 we switch to a buffer with a header line in that window. */
26389 if (cursor_row->visible_height <= 0)
26390 goto mark_cursor_off;
26391
26392 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26393 if (cursor_row->cursor_in_fringe_p)
26394 {
26395 cursor_row->cursor_in_fringe_p = 0;
26396 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26397 goto mark_cursor_off;
26398 }
26399
26400 /* This can happen when the new row is shorter than the old one.
26401 In this case, either draw_glyphs or clear_end_of_line
26402 should have cleared the cursor. Note that we wouldn't be
26403 able to erase the cursor in this case because we don't have a
26404 cursor glyph at hand. */
26405 if ((cursor_row->reversed_p
26406 ? (w->phys_cursor.hpos < 0)
26407 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26408 goto mark_cursor_off;
26409
26410 /* When the window is hscrolled, cursor hpos can legitimately be out
26411 of bounds, but we draw the cursor at the corresponding window
26412 margin in that case. */
26413 if (!cursor_row->reversed_p && hpos < 0)
26414 hpos = 0;
26415 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26416 hpos = cursor_row->used[TEXT_AREA] - 1;
26417
26418 /* If the cursor is in the mouse face area, redisplay that when
26419 we clear the cursor. */
26420 if (! NILP (hlinfo->mouse_face_window)
26421 && coords_in_mouse_face_p (w, hpos, vpos)
26422 /* Don't redraw the cursor's spot in mouse face if it is at the
26423 end of a line (on a newline). The cursor appears there, but
26424 mouse highlighting does not. */
26425 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26426 mouse_face_here_p = 1;
26427
26428 /* Maybe clear the display under the cursor. */
26429 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26430 {
26431 int x, y, left_x;
26432 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26433 int width;
26434
26435 cursor_glyph = get_phys_cursor_glyph (w);
26436 if (cursor_glyph == NULL)
26437 goto mark_cursor_off;
26438
26439 width = cursor_glyph->pixel_width;
26440 left_x = window_box_left_offset (w, TEXT_AREA);
26441 x = w->phys_cursor.x;
26442 if (x < left_x)
26443 width -= left_x - x;
26444 width = min (width, window_box_width (w, TEXT_AREA) - x);
26445 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26446 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26447
26448 if (width > 0)
26449 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26450 }
26451
26452 /* Erase the cursor by redrawing the character underneath it. */
26453 if (mouse_face_here_p)
26454 hl = DRAW_MOUSE_FACE;
26455 else
26456 hl = DRAW_NORMAL_TEXT;
26457 draw_phys_cursor_glyph (w, cursor_row, hl);
26458
26459 mark_cursor_off:
26460 w->phys_cursor_on_p = 0;
26461 w->phys_cursor_type = NO_CURSOR;
26462 }
26463
26464
26465 /* EXPORT:
26466 Display or clear cursor of window W. If ON is zero, clear the
26467 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26468 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26469
26470 void
26471 display_and_set_cursor (struct window *w, int on,
26472 int hpos, int vpos, int x, int y)
26473 {
26474 struct frame *f = XFRAME (w->frame);
26475 int new_cursor_type;
26476 int new_cursor_width;
26477 int active_cursor;
26478 struct glyph_row *glyph_row;
26479 struct glyph *glyph;
26480
26481 /* This is pointless on invisible frames, and dangerous on garbaged
26482 windows and frames; in the latter case, the frame or window may
26483 be in the midst of changing its size, and x and y may be off the
26484 window. */
26485 if (! FRAME_VISIBLE_P (f)
26486 || FRAME_GARBAGED_P (f)
26487 || vpos >= w->current_matrix->nrows
26488 || hpos >= w->current_matrix->matrix_w)
26489 return;
26490
26491 /* If cursor is off and we want it off, return quickly. */
26492 if (!on && !w->phys_cursor_on_p)
26493 return;
26494
26495 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26496 /* If cursor row is not enabled, we don't really know where to
26497 display the cursor. */
26498 if (!glyph_row->enabled_p)
26499 {
26500 w->phys_cursor_on_p = 0;
26501 return;
26502 }
26503
26504 glyph = NULL;
26505 if (!glyph_row->exact_window_width_line_p
26506 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26507 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26508
26509 eassert (input_blocked_p ());
26510
26511 /* Set new_cursor_type to the cursor we want to be displayed. */
26512 new_cursor_type = get_window_cursor_type (w, glyph,
26513 &new_cursor_width, &active_cursor);
26514
26515 /* If cursor is currently being shown and we don't want it to be or
26516 it is in the wrong place, or the cursor type is not what we want,
26517 erase it. */
26518 if (w->phys_cursor_on_p
26519 && (!on
26520 || w->phys_cursor.x != x
26521 || w->phys_cursor.y != y
26522 || new_cursor_type != w->phys_cursor_type
26523 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26524 && new_cursor_width != w->phys_cursor_width)))
26525 erase_phys_cursor (w);
26526
26527 /* Don't check phys_cursor_on_p here because that flag is only set
26528 to zero in some cases where we know that the cursor has been
26529 completely erased, to avoid the extra work of erasing the cursor
26530 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26531 still not be visible, or it has only been partly erased. */
26532 if (on)
26533 {
26534 w->phys_cursor_ascent = glyph_row->ascent;
26535 w->phys_cursor_height = glyph_row->height;
26536
26537 /* Set phys_cursor_.* before x_draw_.* is called because some
26538 of them may need the information. */
26539 w->phys_cursor.x = x;
26540 w->phys_cursor.y = glyph_row->y;
26541 w->phys_cursor.hpos = hpos;
26542 w->phys_cursor.vpos = vpos;
26543 }
26544
26545 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26546 new_cursor_type, new_cursor_width,
26547 on, active_cursor);
26548 }
26549
26550
26551 /* Switch the display of W's cursor on or off, according to the value
26552 of ON. */
26553
26554 static void
26555 update_window_cursor (struct window *w, int on)
26556 {
26557 /* Don't update cursor in windows whose frame is in the process
26558 of being deleted. */
26559 if (w->current_matrix)
26560 {
26561 int hpos = w->phys_cursor.hpos;
26562 int vpos = w->phys_cursor.vpos;
26563 struct glyph_row *row;
26564
26565 if (vpos >= w->current_matrix->nrows
26566 || hpos >= w->current_matrix->matrix_w)
26567 return;
26568
26569 row = MATRIX_ROW (w->current_matrix, vpos);
26570
26571 /* When the window is hscrolled, cursor hpos can legitimately be
26572 out of bounds, but we draw the cursor at the corresponding
26573 window margin in that case. */
26574 if (!row->reversed_p && hpos < 0)
26575 hpos = 0;
26576 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26577 hpos = row->used[TEXT_AREA] - 1;
26578
26579 block_input ();
26580 display_and_set_cursor (w, on, hpos, vpos,
26581 w->phys_cursor.x, w->phys_cursor.y);
26582 unblock_input ();
26583 }
26584 }
26585
26586
26587 /* Call update_window_cursor with parameter ON_P on all leaf windows
26588 in the window tree rooted at W. */
26589
26590 static void
26591 update_cursor_in_window_tree (struct window *w, int on_p)
26592 {
26593 while (w)
26594 {
26595 if (WINDOWP (w->contents))
26596 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26597 else
26598 update_window_cursor (w, on_p);
26599
26600 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26601 }
26602 }
26603
26604
26605 /* EXPORT:
26606 Display the cursor on window W, or clear it, according to ON_P.
26607 Don't change the cursor's position. */
26608
26609 void
26610 x_update_cursor (struct frame *f, int on_p)
26611 {
26612 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26613 }
26614
26615
26616 /* EXPORT:
26617 Clear the cursor of window W to background color, and mark the
26618 cursor as not shown. This is used when the text where the cursor
26619 is about to be rewritten. */
26620
26621 void
26622 x_clear_cursor (struct window *w)
26623 {
26624 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26625 update_window_cursor (w, 0);
26626 }
26627
26628 #endif /* HAVE_WINDOW_SYSTEM */
26629
26630 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26631 and MSDOS. */
26632 static void
26633 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26634 int start_hpos, int end_hpos,
26635 enum draw_glyphs_face draw)
26636 {
26637 #ifdef HAVE_WINDOW_SYSTEM
26638 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26639 {
26640 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26641 return;
26642 }
26643 #endif
26644 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26645 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26646 #endif
26647 }
26648
26649 /* Display the active region described by mouse_face_* according to DRAW. */
26650
26651 static void
26652 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26653 {
26654 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26655 struct frame *f = XFRAME (WINDOW_FRAME (w));
26656
26657 if (/* If window is in the process of being destroyed, don't bother
26658 to do anything. */
26659 w->current_matrix != NULL
26660 /* Don't update mouse highlight if hidden */
26661 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26662 /* Recognize when we are called to operate on rows that don't exist
26663 anymore. This can happen when a window is split. */
26664 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26665 {
26666 int phys_cursor_on_p = w->phys_cursor_on_p;
26667 struct glyph_row *row, *first, *last;
26668
26669 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26670 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26671
26672 for (row = first; row <= last && row->enabled_p; ++row)
26673 {
26674 int start_hpos, end_hpos, start_x;
26675
26676 /* For all but the first row, the highlight starts at column 0. */
26677 if (row == first)
26678 {
26679 /* R2L rows have BEG and END in reversed order, but the
26680 screen drawing geometry is always left to right. So
26681 we need to mirror the beginning and end of the
26682 highlighted area in R2L rows. */
26683 if (!row->reversed_p)
26684 {
26685 start_hpos = hlinfo->mouse_face_beg_col;
26686 start_x = hlinfo->mouse_face_beg_x;
26687 }
26688 else if (row == last)
26689 {
26690 start_hpos = hlinfo->mouse_face_end_col;
26691 start_x = hlinfo->mouse_face_end_x;
26692 }
26693 else
26694 {
26695 start_hpos = 0;
26696 start_x = 0;
26697 }
26698 }
26699 else if (row->reversed_p && row == last)
26700 {
26701 start_hpos = hlinfo->mouse_face_end_col;
26702 start_x = hlinfo->mouse_face_end_x;
26703 }
26704 else
26705 {
26706 start_hpos = 0;
26707 start_x = 0;
26708 }
26709
26710 if (row == last)
26711 {
26712 if (!row->reversed_p)
26713 end_hpos = hlinfo->mouse_face_end_col;
26714 else if (row == first)
26715 end_hpos = hlinfo->mouse_face_beg_col;
26716 else
26717 {
26718 end_hpos = row->used[TEXT_AREA];
26719 if (draw == DRAW_NORMAL_TEXT)
26720 row->fill_line_p = 1; /* Clear to end of line */
26721 }
26722 }
26723 else if (row->reversed_p && row == first)
26724 end_hpos = hlinfo->mouse_face_beg_col;
26725 else
26726 {
26727 end_hpos = row->used[TEXT_AREA];
26728 if (draw == DRAW_NORMAL_TEXT)
26729 row->fill_line_p = 1; /* Clear to end of line */
26730 }
26731
26732 if (end_hpos > start_hpos)
26733 {
26734 draw_row_with_mouse_face (w, start_x, row,
26735 start_hpos, end_hpos, draw);
26736
26737 row->mouse_face_p
26738 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26739 }
26740 }
26741
26742 #ifdef HAVE_WINDOW_SYSTEM
26743 /* When we've written over the cursor, arrange for it to
26744 be displayed again. */
26745 if (FRAME_WINDOW_P (f)
26746 && phys_cursor_on_p && !w->phys_cursor_on_p)
26747 {
26748 int hpos = w->phys_cursor.hpos;
26749
26750 /* When the window is hscrolled, cursor hpos can legitimately be
26751 out of bounds, but we draw the cursor at the corresponding
26752 window margin in that case. */
26753 if (!row->reversed_p && hpos < 0)
26754 hpos = 0;
26755 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26756 hpos = row->used[TEXT_AREA] - 1;
26757
26758 block_input ();
26759 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26760 w->phys_cursor.x, w->phys_cursor.y);
26761 unblock_input ();
26762 }
26763 #endif /* HAVE_WINDOW_SYSTEM */
26764 }
26765
26766 #ifdef HAVE_WINDOW_SYSTEM
26767 /* Change the mouse cursor. */
26768 if (FRAME_WINDOW_P (f))
26769 {
26770 if (draw == DRAW_NORMAL_TEXT
26771 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26772 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26773 else if (draw == DRAW_MOUSE_FACE)
26774 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26775 else
26776 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26777 }
26778 #endif /* HAVE_WINDOW_SYSTEM */
26779 }
26780
26781 /* EXPORT:
26782 Clear out the mouse-highlighted active region.
26783 Redraw it un-highlighted first. Value is non-zero if mouse
26784 face was actually drawn unhighlighted. */
26785
26786 int
26787 clear_mouse_face (Mouse_HLInfo *hlinfo)
26788 {
26789 int cleared = 0;
26790
26791 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26792 {
26793 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26794 cleared = 1;
26795 }
26796
26797 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26798 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26799 hlinfo->mouse_face_window = Qnil;
26800 hlinfo->mouse_face_overlay = Qnil;
26801 return cleared;
26802 }
26803
26804 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26805 within the mouse face on that window. */
26806 static int
26807 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26808 {
26809 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26810
26811 /* Quickly resolve the easy cases. */
26812 if (!(WINDOWP (hlinfo->mouse_face_window)
26813 && XWINDOW (hlinfo->mouse_face_window) == w))
26814 return 0;
26815 if (vpos < hlinfo->mouse_face_beg_row
26816 || vpos > hlinfo->mouse_face_end_row)
26817 return 0;
26818 if (vpos > hlinfo->mouse_face_beg_row
26819 && vpos < hlinfo->mouse_face_end_row)
26820 return 1;
26821
26822 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26823 {
26824 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26825 {
26826 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26827 return 1;
26828 }
26829 else if ((vpos == hlinfo->mouse_face_beg_row
26830 && hpos >= hlinfo->mouse_face_beg_col)
26831 || (vpos == hlinfo->mouse_face_end_row
26832 && hpos < hlinfo->mouse_face_end_col))
26833 return 1;
26834 }
26835 else
26836 {
26837 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26838 {
26839 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26840 return 1;
26841 }
26842 else if ((vpos == hlinfo->mouse_face_beg_row
26843 && hpos <= hlinfo->mouse_face_beg_col)
26844 || (vpos == hlinfo->mouse_face_end_row
26845 && hpos > hlinfo->mouse_face_end_col))
26846 return 1;
26847 }
26848 return 0;
26849 }
26850
26851
26852 /* EXPORT:
26853 Non-zero if physical cursor of window W is within mouse face. */
26854
26855 int
26856 cursor_in_mouse_face_p (struct window *w)
26857 {
26858 int hpos = w->phys_cursor.hpos;
26859 int vpos = w->phys_cursor.vpos;
26860 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26861
26862 /* When the window is hscrolled, cursor hpos can legitimately be out
26863 of bounds, but we draw the cursor at the corresponding window
26864 margin in that case. */
26865 if (!row->reversed_p && hpos < 0)
26866 hpos = 0;
26867 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26868 hpos = row->used[TEXT_AREA] - 1;
26869
26870 return coords_in_mouse_face_p (w, hpos, vpos);
26871 }
26872
26873
26874 \f
26875 /* Find the glyph rows START_ROW and END_ROW of window W that display
26876 characters between buffer positions START_CHARPOS and END_CHARPOS
26877 (excluding END_CHARPOS). DISP_STRING is a display string that
26878 covers these buffer positions. This is similar to
26879 row_containing_pos, but is more accurate when bidi reordering makes
26880 buffer positions change non-linearly with glyph rows. */
26881 static void
26882 rows_from_pos_range (struct window *w,
26883 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26884 Lisp_Object disp_string,
26885 struct glyph_row **start, struct glyph_row **end)
26886 {
26887 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26888 int last_y = window_text_bottom_y (w);
26889 struct glyph_row *row;
26890
26891 *start = NULL;
26892 *end = NULL;
26893
26894 while (!first->enabled_p
26895 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26896 first++;
26897
26898 /* Find the START row. */
26899 for (row = first;
26900 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26901 row++)
26902 {
26903 /* A row can potentially be the START row if the range of the
26904 characters it displays intersects the range
26905 [START_CHARPOS..END_CHARPOS). */
26906 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26907 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26908 /* See the commentary in row_containing_pos, for the
26909 explanation of the complicated way to check whether
26910 some position is beyond the end of the characters
26911 displayed by a row. */
26912 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26913 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26914 && !row->ends_at_zv_p
26915 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26916 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26917 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26918 && !row->ends_at_zv_p
26919 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26920 {
26921 /* Found a candidate row. Now make sure at least one of the
26922 glyphs it displays has a charpos from the range
26923 [START_CHARPOS..END_CHARPOS).
26924
26925 This is not obvious because bidi reordering could make
26926 buffer positions of a row be 1,2,3,102,101,100, and if we
26927 want to highlight characters in [50..60), we don't want
26928 this row, even though [50..60) does intersect [1..103),
26929 the range of character positions given by the row's start
26930 and end positions. */
26931 struct glyph *g = row->glyphs[TEXT_AREA];
26932 struct glyph *e = g + row->used[TEXT_AREA];
26933
26934 while (g < e)
26935 {
26936 if (((BUFFERP (g->object) || INTEGERP (g->object))
26937 && start_charpos <= g->charpos && g->charpos < end_charpos)
26938 /* A glyph that comes from DISP_STRING is by
26939 definition to be highlighted. */
26940 || EQ (g->object, disp_string))
26941 *start = row;
26942 g++;
26943 }
26944 if (*start)
26945 break;
26946 }
26947 }
26948
26949 /* Find the END row. */
26950 if (!*start
26951 /* If the last row is partially visible, start looking for END
26952 from that row, instead of starting from FIRST. */
26953 && !(row->enabled_p
26954 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26955 row = first;
26956 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26957 {
26958 struct glyph_row *next = row + 1;
26959 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26960
26961 if (!next->enabled_p
26962 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26963 /* The first row >= START whose range of displayed characters
26964 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26965 is the row END + 1. */
26966 || (start_charpos < next_start
26967 && end_charpos < next_start)
26968 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26969 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26970 && !next->ends_at_zv_p
26971 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26972 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26973 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26974 && !next->ends_at_zv_p
26975 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26976 {
26977 *end = row;
26978 break;
26979 }
26980 else
26981 {
26982 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26983 but none of the characters it displays are in the range, it is
26984 also END + 1. */
26985 struct glyph *g = next->glyphs[TEXT_AREA];
26986 struct glyph *s = g;
26987 struct glyph *e = g + next->used[TEXT_AREA];
26988
26989 while (g < e)
26990 {
26991 if (((BUFFERP (g->object) || INTEGERP (g->object))
26992 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26993 /* If the buffer position of the first glyph in
26994 the row is equal to END_CHARPOS, it means
26995 the last character to be highlighted is the
26996 newline of ROW, and we must consider NEXT as
26997 END, not END+1. */
26998 || (((!next->reversed_p && g == s)
26999 || (next->reversed_p && g == e - 1))
27000 && (g->charpos == end_charpos
27001 /* Special case for when NEXT is an
27002 empty line at ZV. */
27003 || (g->charpos == -1
27004 && !row->ends_at_zv_p
27005 && next_start == end_charpos)))))
27006 /* A glyph that comes from DISP_STRING is by
27007 definition to be highlighted. */
27008 || EQ (g->object, disp_string))
27009 break;
27010 g++;
27011 }
27012 if (g == e)
27013 {
27014 *end = row;
27015 break;
27016 }
27017 /* The first row that ends at ZV must be the last to be
27018 highlighted. */
27019 else if (next->ends_at_zv_p)
27020 {
27021 *end = next;
27022 break;
27023 }
27024 }
27025 }
27026 }
27027
27028 /* This function sets the mouse_face_* elements of HLINFO, assuming
27029 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27030 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27031 for the overlay or run of text properties specifying the mouse
27032 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27033 before-string and after-string that must also be highlighted.
27034 DISP_STRING, if non-nil, is a display string that may cover some
27035 or all of the highlighted text. */
27036
27037 static void
27038 mouse_face_from_buffer_pos (Lisp_Object window,
27039 Mouse_HLInfo *hlinfo,
27040 ptrdiff_t mouse_charpos,
27041 ptrdiff_t start_charpos,
27042 ptrdiff_t end_charpos,
27043 Lisp_Object before_string,
27044 Lisp_Object after_string,
27045 Lisp_Object disp_string)
27046 {
27047 struct window *w = XWINDOW (window);
27048 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27049 struct glyph_row *r1, *r2;
27050 struct glyph *glyph, *end;
27051 ptrdiff_t ignore, pos;
27052 int x;
27053
27054 eassert (NILP (disp_string) || STRINGP (disp_string));
27055 eassert (NILP (before_string) || STRINGP (before_string));
27056 eassert (NILP (after_string) || STRINGP (after_string));
27057
27058 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27059 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27060 if (r1 == NULL)
27061 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27062 /* If the before-string or display-string contains newlines,
27063 rows_from_pos_range skips to its last row. Move back. */
27064 if (!NILP (before_string) || !NILP (disp_string))
27065 {
27066 struct glyph_row *prev;
27067 while ((prev = r1 - 1, prev >= first)
27068 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27069 && prev->used[TEXT_AREA] > 0)
27070 {
27071 struct glyph *beg = prev->glyphs[TEXT_AREA];
27072 glyph = beg + prev->used[TEXT_AREA];
27073 while (--glyph >= beg && INTEGERP (glyph->object));
27074 if (glyph < beg
27075 || !(EQ (glyph->object, before_string)
27076 || EQ (glyph->object, disp_string)))
27077 break;
27078 r1 = prev;
27079 }
27080 }
27081 if (r2 == NULL)
27082 {
27083 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27084 hlinfo->mouse_face_past_end = 1;
27085 }
27086 else if (!NILP (after_string))
27087 {
27088 /* If the after-string has newlines, advance to its last row. */
27089 struct glyph_row *next;
27090 struct glyph_row *last
27091 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27092
27093 for (next = r2 + 1;
27094 next <= last
27095 && next->used[TEXT_AREA] > 0
27096 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27097 ++next)
27098 r2 = next;
27099 }
27100 /* The rest of the display engine assumes that mouse_face_beg_row is
27101 either above mouse_face_end_row or identical to it. But with
27102 bidi-reordered continued lines, the row for START_CHARPOS could
27103 be below the row for END_CHARPOS. If so, swap the rows and store
27104 them in correct order. */
27105 if (r1->y > r2->y)
27106 {
27107 struct glyph_row *tem = r2;
27108
27109 r2 = r1;
27110 r1 = tem;
27111 }
27112
27113 hlinfo->mouse_face_beg_y = r1->y;
27114 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27115 hlinfo->mouse_face_end_y = r2->y;
27116 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27117
27118 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27119 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27120 could be anywhere in the row and in any order. The strategy
27121 below is to find the leftmost and the rightmost glyph that
27122 belongs to either of these 3 strings, or whose position is
27123 between START_CHARPOS and END_CHARPOS, and highlight all the
27124 glyphs between those two. This may cover more than just the text
27125 between START_CHARPOS and END_CHARPOS if the range of characters
27126 strides the bidi level boundary, e.g. if the beginning is in R2L
27127 text while the end is in L2R text or vice versa. */
27128 if (!r1->reversed_p)
27129 {
27130 /* This row is in a left to right paragraph. Scan it left to
27131 right. */
27132 glyph = r1->glyphs[TEXT_AREA];
27133 end = glyph + r1->used[TEXT_AREA];
27134 x = r1->x;
27135
27136 /* Skip truncation glyphs at the start of the glyph row. */
27137 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27138 for (; glyph < end
27139 && INTEGERP (glyph->object)
27140 && glyph->charpos < 0;
27141 ++glyph)
27142 x += glyph->pixel_width;
27143
27144 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27145 or DISP_STRING, and the first glyph from buffer whose
27146 position is between START_CHARPOS and END_CHARPOS. */
27147 for (; glyph < end
27148 && !INTEGERP (glyph->object)
27149 && !EQ (glyph->object, disp_string)
27150 && !(BUFFERP (glyph->object)
27151 && (glyph->charpos >= start_charpos
27152 && glyph->charpos < end_charpos));
27153 ++glyph)
27154 {
27155 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27156 are present at buffer positions between START_CHARPOS and
27157 END_CHARPOS, or if they come from an overlay. */
27158 if (EQ (glyph->object, before_string))
27159 {
27160 pos = string_buffer_position (before_string,
27161 start_charpos);
27162 /* If pos == 0, it means before_string came from an
27163 overlay, not from a buffer position. */
27164 if (!pos || (pos >= start_charpos && pos < end_charpos))
27165 break;
27166 }
27167 else if (EQ (glyph->object, after_string))
27168 {
27169 pos = string_buffer_position (after_string, end_charpos);
27170 if (!pos || (pos >= start_charpos && pos < end_charpos))
27171 break;
27172 }
27173 x += glyph->pixel_width;
27174 }
27175 hlinfo->mouse_face_beg_x = x;
27176 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27177 }
27178 else
27179 {
27180 /* This row is in a right to left paragraph. Scan it right to
27181 left. */
27182 struct glyph *g;
27183
27184 end = r1->glyphs[TEXT_AREA] - 1;
27185 glyph = end + r1->used[TEXT_AREA];
27186
27187 /* Skip truncation glyphs at the start of the glyph row. */
27188 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27189 for (; glyph > end
27190 && INTEGERP (glyph->object)
27191 && glyph->charpos < 0;
27192 --glyph)
27193 ;
27194
27195 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27196 or DISP_STRING, and the first glyph from buffer whose
27197 position is between START_CHARPOS and END_CHARPOS. */
27198 for (; glyph > end
27199 && !INTEGERP (glyph->object)
27200 && !EQ (glyph->object, disp_string)
27201 && !(BUFFERP (glyph->object)
27202 && (glyph->charpos >= start_charpos
27203 && glyph->charpos < end_charpos));
27204 --glyph)
27205 {
27206 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27207 are present at buffer positions between START_CHARPOS and
27208 END_CHARPOS, or if they come from an overlay. */
27209 if (EQ (glyph->object, before_string))
27210 {
27211 pos = string_buffer_position (before_string, start_charpos);
27212 /* If pos == 0, it means before_string came from an
27213 overlay, not from a buffer position. */
27214 if (!pos || (pos >= start_charpos && pos < end_charpos))
27215 break;
27216 }
27217 else if (EQ (glyph->object, after_string))
27218 {
27219 pos = string_buffer_position (after_string, end_charpos);
27220 if (!pos || (pos >= start_charpos && pos < end_charpos))
27221 break;
27222 }
27223 }
27224
27225 glyph++; /* first glyph to the right of the highlighted area */
27226 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27227 x += g->pixel_width;
27228 hlinfo->mouse_face_beg_x = x;
27229 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27230 }
27231
27232 /* If the highlight ends in a different row, compute GLYPH and END
27233 for the end row. Otherwise, reuse the values computed above for
27234 the row where the highlight begins. */
27235 if (r2 != r1)
27236 {
27237 if (!r2->reversed_p)
27238 {
27239 glyph = r2->glyphs[TEXT_AREA];
27240 end = glyph + r2->used[TEXT_AREA];
27241 x = r2->x;
27242 }
27243 else
27244 {
27245 end = r2->glyphs[TEXT_AREA] - 1;
27246 glyph = end + r2->used[TEXT_AREA];
27247 }
27248 }
27249
27250 if (!r2->reversed_p)
27251 {
27252 /* Skip truncation and continuation glyphs near the end of the
27253 row, and also blanks and stretch glyphs inserted by
27254 extend_face_to_end_of_line. */
27255 while (end > glyph
27256 && INTEGERP ((end - 1)->object))
27257 --end;
27258 /* Scan the rest of the glyph row from the end, looking for the
27259 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27260 DISP_STRING, or whose position is between START_CHARPOS
27261 and END_CHARPOS */
27262 for (--end;
27263 end > glyph
27264 && !INTEGERP (end->object)
27265 && !EQ (end->object, disp_string)
27266 && !(BUFFERP (end->object)
27267 && (end->charpos >= start_charpos
27268 && end->charpos < end_charpos));
27269 --end)
27270 {
27271 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27272 are present at buffer positions between START_CHARPOS and
27273 END_CHARPOS, or if they come from an overlay. */
27274 if (EQ (end->object, before_string))
27275 {
27276 pos = string_buffer_position (before_string, start_charpos);
27277 if (!pos || (pos >= start_charpos && pos < end_charpos))
27278 break;
27279 }
27280 else if (EQ (end->object, after_string))
27281 {
27282 pos = string_buffer_position (after_string, end_charpos);
27283 if (!pos || (pos >= start_charpos && pos < end_charpos))
27284 break;
27285 }
27286 }
27287 /* Find the X coordinate of the last glyph to be highlighted. */
27288 for (; glyph <= end; ++glyph)
27289 x += glyph->pixel_width;
27290
27291 hlinfo->mouse_face_end_x = x;
27292 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27293 }
27294 else
27295 {
27296 /* Skip truncation and continuation glyphs near the end of the
27297 row, and also blanks and stretch glyphs inserted by
27298 extend_face_to_end_of_line. */
27299 x = r2->x;
27300 end++;
27301 while (end < glyph
27302 && INTEGERP (end->object))
27303 {
27304 x += end->pixel_width;
27305 ++end;
27306 }
27307 /* Scan the rest of the glyph row from the end, looking for the
27308 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27309 DISP_STRING, or whose position is between START_CHARPOS
27310 and END_CHARPOS */
27311 for ( ;
27312 end < glyph
27313 && !INTEGERP (end->object)
27314 && !EQ (end->object, disp_string)
27315 && !(BUFFERP (end->object)
27316 && (end->charpos >= start_charpos
27317 && end->charpos < end_charpos));
27318 ++end)
27319 {
27320 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27321 are present at buffer positions between START_CHARPOS and
27322 END_CHARPOS, or if they come from an overlay. */
27323 if (EQ (end->object, before_string))
27324 {
27325 pos = string_buffer_position (before_string, start_charpos);
27326 if (!pos || (pos >= start_charpos && pos < end_charpos))
27327 break;
27328 }
27329 else if (EQ (end->object, after_string))
27330 {
27331 pos = string_buffer_position (after_string, end_charpos);
27332 if (!pos || (pos >= start_charpos && pos < end_charpos))
27333 break;
27334 }
27335 x += end->pixel_width;
27336 }
27337 /* If we exited the above loop because we arrived at the last
27338 glyph of the row, and its buffer position is still not in
27339 range, it means the last character in range is the preceding
27340 newline. Bump the end column and x values to get past the
27341 last glyph. */
27342 if (end == glyph
27343 && BUFFERP (end->object)
27344 && (end->charpos < start_charpos
27345 || end->charpos >= end_charpos))
27346 {
27347 x += end->pixel_width;
27348 ++end;
27349 }
27350 hlinfo->mouse_face_end_x = x;
27351 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27352 }
27353
27354 hlinfo->mouse_face_window = window;
27355 hlinfo->mouse_face_face_id
27356 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27357 mouse_charpos + 1,
27358 !hlinfo->mouse_face_hidden, -1);
27359 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27360 }
27361
27362 /* The following function is not used anymore (replaced with
27363 mouse_face_from_string_pos), but I leave it here for the time
27364 being, in case someone would. */
27365
27366 #if 0 /* not used */
27367
27368 /* Find the position of the glyph for position POS in OBJECT in
27369 window W's current matrix, and return in *X, *Y the pixel
27370 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27371
27372 RIGHT_P non-zero means return the position of the right edge of the
27373 glyph, RIGHT_P zero means return the left edge position.
27374
27375 If no glyph for POS exists in the matrix, return the position of
27376 the glyph with the next smaller position that is in the matrix, if
27377 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27378 exists in the matrix, return the position of the glyph with the
27379 next larger position in OBJECT.
27380
27381 Value is non-zero if a glyph was found. */
27382
27383 static int
27384 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27385 int *hpos, int *vpos, int *x, int *y, int right_p)
27386 {
27387 int yb = window_text_bottom_y (w);
27388 struct glyph_row *r;
27389 struct glyph *best_glyph = NULL;
27390 struct glyph_row *best_row = NULL;
27391 int best_x = 0;
27392
27393 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27394 r->enabled_p && r->y < yb;
27395 ++r)
27396 {
27397 struct glyph *g = r->glyphs[TEXT_AREA];
27398 struct glyph *e = g + r->used[TEXT_AREA];
27399 int gx;
27400
27401 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27402 if (EQ (g->object, object))
27403 {
27404 if (g->charpos == pos)
27405 {
27406 best_glyph = g;
27407 best_x = gx;
27408 best_row = r;
27409 goto found;
27410 }
27411 else if (best_glyph == NULL
27412 || ((eabs (g->charpos - pos)
27413 < eabs (best_glyph->charpos - pos))
27414 && (right_p
27415 ? g->charpos < pos
27416 : g->charpos > pos)))
27417 {
27418 best_glyph = g;
27419 best_x = gx;
27420 best_row = r;
27421 }
27422 }
27423 }
27424
27425 found:
27426
27427 if (best_glyph)
27428 {
27429 *x = best_x;
27430 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27431
27432 if (right_p)
27433 {
27434 *x += best_glyph->pixel_width;
27435 ++*hpos;
27436 }
27437
27438 *y = best_row->y;
27439 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27440 }
27441
27442 return best_glyph != NULL;
27443 }
27444 #endif /* not used */
27445
27446 /* Find the positions of the first and the last glyphs in window W's
27447 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27448 (assumed to be a string), and return in HLINFO's mouse_face_*
27449 members the pixel and column/row coordinates of those glyphs. */
27450
27451 static void
27452 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27453 Lisp_Object object,
27454 ptrdiff_t startpos, ptrdiff_t endpos)
27455 {
27456 int yb = window_text_bottom_y (w);
27457 struct glyph_row *r;
27458 struct glyph *g, *e;
27459 int gx;
27460 int found = 0;
27461
27462 /* Find the glyph row with at least one position in the range
27463 [STARTPOS..ENDPOS], and the first glyph in that row whose
27464 position belongs to that range. */
27465 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27466 r->enabled_p && r->y < yb;
27467 ++r)
27468 {
27469 if (!r->reversed_p)
27470 {
27471 g = r->glyphs[TEXT_AREA];
27472 e = g + r->used[TEXT_AREA];
27473 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27474 if (EQ (g->object, object)
27475 && startpos <= g->charpos && g->charpos <= endpos)
27476 {
27477 hlinfo->mouse_face_beg_row
27478 = MATRIX_ROW_VPOS (r, w->current_matrix);
27479 hlinfo->mouse_face_beg_y = r->y;
27480 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27481 hlinfo->mouse_face_beg_x = gx;
27482 found = 1;
27483 break;
27484 }
27485 }
27486 else
27487 {
27488 struct glyph *g1;
27489
27490 e = r->glyphs[TEXT_AREA];
27491 g = e + r->used[TEXT_AREA];
27492 for ( ; g > e; --g)
27493 if (EQ ((g-1)->object, object)
27494 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27495 {
27496 hlinfo->mouse_face_beg_row
27497 = MATRIX_ROW_VPOS (r, w->current_matrix);
27498 hlinfo->mouse_face_beg_y = r->y;
27499 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27500 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27501 gx += g1->pixel_width;
27502 hlinfo->mouse_face_beg_x = gx;
27503 found = 1;
27504 break;
27505 }
27506 }
27507 if (found)
27508 break;
27509 }
27510
27511 if (!found)
27512 return;
27513
27514 /* Starting with the next row, look for the first row which does NOT
27515 include any glyphs whose positions are in the range. */
27516 for (++r; r->enabled_p && r->y < yb; ++r)
27517 {
27518 g = r->glyphs[TEXT_AREA];
27519 e = g + r->used[TEXT_AREA];
27520 found = 0;
27521 for ( ; g < e; ++g)
27522 if (EQ (g->object, object)
27523 && startpos <= g->charpos && g->charpos <= endpos)
27524 {
27525 found = 1;
27526 break;
27527 }
27528 if (!found)
27529 break;
27530 }
27531
27532 /* The highlighted region ends on the previous row. */
27533 r--;
27534
27535 /* Set the end row and its vertical pixel coordinate. */
27536 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27537 hlinfo->mouse_face_end_y = r->y;
27538
27539 /* Compute and set the end column and the end column's horizontal
27540 pixel coordinate. */
27541 if (!r->reversed_p)
27542 {
27543 g = r->glyphs[TEXT_AREA];
27544 e = g + r->used[TEXT_AREA];
27545 for ( ; e > g; --e)
27546 if (EQ ((e-1)->object, object)
27547 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27548 break;
27549 hlinfo->mouse_face_end_col = e - g;
27550
27551 for (gx = r->x; g < e; ++g)
27552 gx += g->pixel_width;
27553 hlinfo->mouse_face_end_x = gx;
27554 }
27555 else
27556 {
27557 e = r->glyphs[TEXT_AREA];
27558 g = e + r->used[TEXT_AREA];
27559 for (gx = r->x ; e < g; ++e)
27560 {
27561 if (EQ (e->object, object)
27562 && startpos <= e->charpos && e->charpos <= endpos)
27563 break;
27564 gx += e->pixel_width;
27565 }
27566 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27567 hlinfo->mouse_face_end_x = gx;
27568 }
27569 }
27570
27571 #ifdef HAVE_WINDOW_SYSTEM
27572
27573 /* See if position X, Y is within a hot-spot of an image. */
27574
27575 static int
27576 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27577 {
27578 if (!CONSP (hot_spot))
27579 return 0;
27580
27581 if (EQ (XCAR (hot_spot), Qrect))
27582 {
27583 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27584 Lisp_Object rect = XCDR (hot_spot);
27585 Lisp_Object tem;
27586 if (!CONSP (rect))
27587 return 0;
27588 if (!CONSP (XCAR (rect)))
27589 return 0;
27590 if (!CONSP (XCDR (rect)))
27591 return 0;
27592 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27593 return 0;
27594 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27595 return 0;
27596 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27597 return 0;
27598 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27599 return 0;
27600 return 1;
27601 }
27602 else if (EQ (XCAR (hot_spot), Qcircle))
27603 {
27604 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27605 Lisp_Object circ = XCDR (hot_spot);
27606 Lisp_Object lr, lx0, ly0;
27607 if (CONSP (circ)
27608 && CONSP (XCAR (circ))
27609 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27610 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27611 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27612 {
27613 double r = XFLOATINT (lr);
27614 double dx = XINT (lx0) - x;
27615 double dy = XINT (ly0) - y;
27616 return (dx * dx + dy * dy <= r * r);
27617 }
27618 }
27619 else if (EQ (XCAR (hot_spot), Qpoly))
27620 {
27621 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27622 if (VECTORP (XCDR (hot_spot)))
27623 {
27624 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27625 Lisp_Object *poly = v->contents;
27626 ptrdiff_t n = v->header.size;
27627 ptrdiff_t i;
27628 int inside = 0;
27629 Lisp_Object lx, ly;
27630 int x0, y0;
27631
27632 /* Need an even number of coordinates, and at least 3 edges. */
27633 if (n < 6 || n & 1)
27634 return 0;
27635
27636 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27637 If count is odd, we are inside polygon. Pixels on edges
27638 may or may not be included depending on actual geometry of the
27639 polygon. */
27640 if ((lx = poly[n-2], !INTEGERP (lx))
27641 || (ly = poly[n-1], !INTEGERP (lx)))
27642 return 0;
27643 x0 = XINT (lx), y0 = XINT (ly);
27644 for (i = 0; i < n; i += 2)
27645 {
27646 int x1 = x0, y1 = y0;
27647 if ((lx = poly[i], !INTEGERP (lx))
27648 || (ly = poly[i+1], !INTEGERP (ly)))
27649 return 0;
27650 x0 = XINT (lx), y0 = XINT (ly);
27651
27652 /* Does this segment cross the X line? */
27653 if (x0 >= x)
27654 {
27655 if (x1 >= x)
27656 continue;
27657 }
27658 else if (x1 < x)
27659 continue;
27660 if (y > y0 && y > y1)
27661 continue;
27662 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27663 inside = !inside;
27664 }
27665 return inside;
27666 }
27667 }
27668 return 0;
27669 }
27670
27671 Lisp_Object
27672 find_hot_spot (Lisp_Object map, int x, int y)
27673 {
27674 while (CONSP (map))
27675 {
27676 if (CONSP (XCAR (map))
27677 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27678 return XCAR (map);
27679 map = XCDR (map);
27680 }
27681
27682 return Qnil;
27683 }
27684
27685 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27686 3, 3, 0,
27687 doc: /* Lookup in image map MAP coordinates X and Y.
27688 An image map is an alist where each element has the format (AREA ID PLIST).
27689 An AREA is specified as either a rectangle, a circle, or a polygon:
27690 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27691 pixel coordinates of the upper left and bottom right corners.
27692 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27693 and the radius of the circle; r may be a float or integer.
27694 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27695 vector describes one corner in the polygon.
27696 Returns the alist element for the first matching AREA in MAP. */)
27697 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27698 {
27699 if (NILP (map))
27700 return Qnil;
27701
27702 CHECK_NUMBER (x);
27703 CHECK_NUMBER (y);
27704
27705 return find_hot_spot (map,
27706 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27707 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27708 }
27709
27710
27711 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27712 static void
27713 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27714 {
27715 /* Do not change cursor shape while dragging mouse. */
27716 if (!NILP (do_mouse_tracking))
27717 return;
27718
27719 if (!NILP (pointer))
27720 {
27721 if (EQ (pointer, Qarrow))
27722 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27723 else if (EQ (pointer, Qhand))
27724 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27725 else if (EQ (pointer, Qtext))
27726 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27727 else if (EQ (pointer, intern ("hdrag")))
27728 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27729 #ifdef HAVE_X_WINDOWS
27730 else if (EQ (pointer, intern ("vdrag")))
27731 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27732 #endif
27733 else if (EQ (pointer, intern ("hourglass")))
27734 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27735 else if (EQ (pointer, Qmodeline))
27736 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27737 else
27738 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27739 }
27740
27741 if (cursor != No_Cursor)
27742 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27743 }
27744
27745 #endif /* HAVE_WINDOW_SYSTEM */
27746
27747 /* Take proper action when mouse has moved to the mode or header line
27748 or marginal area AREA of window W, x-position X and y-position Y.
27749 X is relative to the start of the text display area of W, so the
27750 width of bitmap areas and scroll bars must be subtracted to get a
27751 position relative to the start of the mode line. */
27752
27753 static void
27754 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27755 enum window_part area)
27756 {
27757 struct window *w = XWINDOW (window);
27758 struct frame *f = XFRAME (w->frame);
27759 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27760 #ifdef HAVE_WINDOW_SYSTEM
27761 Display_Info *dpyinfo;
27762 #endif
27763 Cursor cursor = No_Cursor;
27764 Lisp_Object pointer = Qnil;
27765 int dx, dy, width, height;
27766 ptrdiff_t charpos;
27767 Lisp_Object string, object = Qnil;
27768 Lisp_Object pos IF_LINT (= Qnil), help;
27769
27770 Lisp_Object mouse_face;
27771 int original_x_pixel = x;
27772 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27773 struct glyph_row *row IF_LINT (= 0);
27774
27775 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27776 {
27777 int x0;
27778 struct glyph *end;
27779
27780 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27781 returns them in row/column units! */
27782 string = mode_line_string (w, area, &x, &y, &charpos,
27783 &object, &dx, &dy, &width, &height);
27784
27785 row = (area == ON_MODE_LINE
27786 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27787 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27788
27789 /* Find the glyph under the mouse pointer. */
27790 if (row->mode_line_p && row->enabled_p)
27791 {
27792 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27793 end = glyph + row->used[TEXT_AREA];
27794
27795 for (x0 = original_x_pixel;
27796 glyph < end && x0 >= glyph->pixel_width;
27797 ++glyph)
27798 x0 -= glyph->pixel_width;
27799
27800 if (glyph >= end)
27801 glyph = NULL;
27802 }
27803 }
27804 else
27805 {
27806 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27807 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27808 returns them in row/column units! */
27809 string = marginal_area_string (w, area, &x, &y, &charpos,
27810 &object, &dx, &dy, &width, &height);
27811 }
27812
27813 help = Qnil;
27814
27815 #ifdef HAVE_WINDOW_SYSTEM
27816 if (IMAGEP (object))
27817 {
27818 Lisp_Object image_map, hotspot;
27819 if ((image_map = Fplist_get (XCDR (object), QCmap),
27820 !NILP (image_map))
27821 && (hotspot = find_hot_spot (image_map, dx, dy),
27822 CONSP (hotspot))
27823 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27824 {
27825 Lisp_Object plist;
27826
27827 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27828 If so, we could look for mouse-enter, mouse-leave
27829 properties in PLIST (and do something...). */
27830 hotspot = XCDR (hotspot);
27831 if (CONSP (hotspot)
27832 && (plist = XCAR (hotspot), CONSP (plist)))
27833 {
27834 pointer = Fplist_get (plist, Qpointer);
27835 if (NILP (pointer))
27836 pointer = Qhand;
27837 help = Fplist_get (plist, Qhelp_echo);
27838 if (!NILP (help))
27839 {
27840 help_echo_string = help;
27841 XSETWINDOW (help_echo_window, w);
27842 help_echo_object = w->contents;
27843 help_echo_pos = charpos;
27844 }
27845 }
27846 }
27847 if (NILP (pointer))
27848 pointer = Fplist_get (XCDR (object), QCpointer);
27849 }
27850 #endif /* HAVE_WINDOW_SYSTEM */
27851
27852 if (STRINGP (string))
27853 pos = make_number (charpos);
27854
27855 /* Set the help text and mouse pointer. If the mouse is on a part
27856 of the mode line without any text (e.g. past the right edge of
27857 the mode line text), use the default help text and pointer. */
27858 if (STRINGP (string) || area == ON_MODE_LINE)
27859 {
27860 /* Arrange to display the help by setting the global variables
27861 help_echo_string, help_echo_object, and help_echo_pos. */
27862 if (NILP (help))
27863 {
27864 if (STRINGP (string))
27865 help = Fget_text_property (pos, Qhelp_echo, string);
27866
27867 if (!NILP (help))
27868 {
27869 help_echo_string = help;
27870 XSETWINDOW (help_echo_window, w);
27871 help_echo_object = string;
27872 help_echo_pos = charpos;
27873 }
27874 else if (area == ON_MODE_LINE)
27875 {
27876 Lisp_Object default_help
27877 = buffer_local_value_1 (Qmode_line_default_help_echo,
27878 w->contents);
27879
27880 if (STRINGP (default_help))
27881 {
27882 help_echo_string = default_help;
27883 XSETWINDOW (help_echo_window, w);
27884 help_echo_object = Qnil;
27885 help_echo_pos = -1;
27886 }
27887 }
27888 }
27889
27890 #ifdef HAVE_WINDOW_SYSTEM
27891 /* Change the mouse pointer according to what is under it. */
27892 if (FRAME_WINDOW_P (f))
27893 {
27894 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27895 if (STRINGP (string))
27896 {
27897 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27898
27899 if (NILP (pointer))
27900 pointer = Fget_text_property (pos, Qpointer, string);
27901
27902 /* Change the mouse pointer according to what is under X/Y. */
27903 if (NILP (pointer)
27904 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27905 {
27906 Lisp_Object map;
27907 map = Fget_text_property (pos, Qlocal_map, string);
27908 if (!KEYMAPP (map))
27909 map = Fget_text_property (pos, Qkeymap, string);
27910 if (!KEYMAPP (map))
27911 cursor = dpyinfo->vertical_scroll_bar_cursor;
27912 }
27913 }
27914 else
27915 /* Default mode-line pointer. */
27916 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27917 }
27918 #endif
27919 }
27920
27921 /* Change the mouse face according to what is under X/Y. */
27922 if (STRINGP (string))
27923 {
27924 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27925 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27926 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27927 && glyph)
27928 {
27929 Lisp_Object b, e;
27930
27931 struct glyph * tmp_glyph;
27932
27933 int gpos;
27934 int gseq_length;
27935 int total_pixel_width;
27936 ptrdiff_t begpos, endpos, ignore;
27937
27938 int vpos, hpos;
27939
27940 b = Fprevious_single_property_change (make_number (charpos + 1),
27941 Qmouse_face, string, Qnil);
27942 if (NILP (b))
27943 begpos = 0;
27944 else
27945 begpos = XINT (b);
27946
27947 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27948 if (NILP (e))
27949 endpos = SCHARS (string);
27950 else
27951 endpos = XINT (e);
27952
27953 /* Calculate the glyph position GPOS of GLYPH in the
27954 displayed string, relative to the beginning of the
27955 highlighted part of the string.
27956
27957 Note: GPOS is different from CHARPOS. CHARPOS is the
27958 position of GLYPH in the internal string object. A mode
27959 line string format has structures which are converted to
27960 a flattened string by the Emacs Lisp interpreter. The
27961 internal string is an element of those structures. The
27962 displayed string is the flattened string. */
27963 tmp_glyph = row_start_glyph;
27964 while (tmp_glyph < glyph
27965 && (!(EQ (tmp_glyph->object, glyph->object)
27966 && begpos <= tmp_glyph->charpos
27967 && tmp_glyph->charpos < endpos)))
27968 tmp_glyph++;
27969 gpos = glyph - tmp_glyph;
27970
27971 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27972 the highlighted part of the displayed string to which
27973 GLYPH belongs. Note: GSEQ_LENGTH is different from
27974 SCHARS (STRING), because the latter returns the length of
27975 the internal string. */
27976 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27977 tmp_glyph > glyph
27978 && (!(EQ (tmp_glyph->object, glyph->object)
27979 && begpos <= tmp_glyph->charpos
27980 && tmp_glyph->charpos < endpos));
27981 tmp_glyph--)
27982 ;
27983 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27984
27985 /* Calculate the total pixel width of all the glyphs between
27986 the beginning of the highlighted area and GLYPH. */
27987 total_pixel_width = 0;
27988 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27989 total_pixel_width += tmp_glyph->pixel_width;
27990
27991 /* Pre calculation of re-rendering position. Note: X is in
27992 column units here, after the call to mode_line_string or
27993 marginal_area_string. */
27994 hpos = x - gpos;
27995 vpos = (area == ON_MODE_LINE
27996 ? (w->current_matrix)->nrows - 1
27997 : 0);
27998
27999 /* If GLYPH's position is included in the region that is
28000 already drawn in mouse face, we have nothing to do. */
28001 if ( EQ (window, hlinfo->mouse_face_window)
28002 && (!row->reversed_p
28003 ? (hlinfo->mouse_face_beg_col <= hpos
28004 && hpos < hlinfo->mouse_face_end_col)
28005 /* In R2L rows we swap BEG and END, see below. */
28006 : (hlinfo->mouse_face_end_col <= hpos
28007 && hpos < hlinfo->mouse_face_beg_col))
28008 && hlinfo->mouse_face_beg_row == vpos )
28009 return;
28010
28011 if (clear_mouse_face (hlinfo))
28012 cursor = No_Cursor;
28013
28014 if (!row->reversed_p)
28015 {
28016 hlinfo->mouse_face_beg_col = hpos;
28017 hlinfo->mouse_face_beg_x = original_x_pixel
28018 - (total_pixel_width + dx);
28019 hlinfo->mouse_face_end_col = hpos + gseq_length;
28020 hlinfo->mouse_face_end_x = 0;
28021 }
28022 else
28023 {
28024 /* In R2L rows, show_mouse_face expects BEG and END
28025 coordinates to be swapped. */
28026 hlinfo->mouse_face_end_col = hpos;
28027 hlinfo->mouse_face_end_x = original_x_pixel
28028 - (total_pixel_width + dx);
28029 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28030 hlinfo->mouse_face_beg_x = 0;
28031 }
28032
28033 hlinfo->mouse_face_beg_row = vpos;
28034 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28035 hlinfo->mouse_face_beg_y = 0;
28036 hlinfo->mouse_face_end_y = 0;
28037 hlinfo->mouse_face_past_end = 0;
28038 hlinfo->mouse_face_window = window;
28039
28040 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28041 charpos,
28042 0, 0, 0,
28043 &ignore,
28044 glyph->face_id,
28045 1);
28046 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28047
28048 if (NILP (pointer))
28049 pointer = Qhand;
28050 }
28051 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28052 clear_mouse_face (hlinfo);
28053 }
28054 #ifdef HAVE_WINDOW_SYSTEM
28055 if (FRAME_WINDOW_P (f))
28056 define_frame_cursor1 (f, cursor, pointer);
28057 #endif
28058 }
28059
28060
28061 /* EXPORT:
28062 Take proper action when the mouse has moved to position X, Y on
28063 frame F with regards to highlighting portions of display that have
28064 mouse-face properties. Also de-highlight portions of display where
28065 the mouse was before, set the mouse pointer shape as appropriate
28066 for the mouse coordinates, and activate help echo (tooltips).
28067 X and Y can be negative or out of range. */
28068
28069 void
28070 note_mouse_highlight (struct frame *f, int x, int y)
28071 {
28072 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28073 enum window_part part = ON_NOTHING;
28074 Lisp_Object window;
28075 struct window *w;
28076 Cursor cursor = No_Cursor;
28077 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28078 struct buffer *b;
28079
28080 /* When a menu is active, don't highlight because this looks odd. */
28081 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28082 if (popup_activated ())
28083 return;
28084 #endif
28085
28086 if (!f->glyphs_initialized_p
28087 || f->pointer_invisible)
28088 return;
28089
28090 hlinfo->mouse_face_mouse_x = x;
28091 hlinfo->mouse_face_mouse_y = y;
28092 hlinfo->mouse_face_mouse_frame = f;
28093
28094 if (hlinfo->mouse_face_defer)
28095 return;
28096
28097 /* Which window is that in? */
28098 window = window_from_coordinates (f, x, y, &part, 1);
28099
28100 /* If displaying active text in another window, clear that. */
28101 if (! EQ (window, hlinfo->mouse_face_window)
28102 /* Also clear if we move out of text area in same window. */
28103 || (!NILP (hlinfo->mouse_face_window)
28104 && !NILP (window)
28105 && part != ON_TEXT
28106 && part != ON_MODE_LINE
28107 && part != ON_HEADER_LINE))
28108 clear_mouse_face (hlinfo);
28109
28110 /* Not on a window -> return. */
28111 if (!WINDOWP (window))
28112 return;
28113
28114 /* Reset help_echo_string. It will get recomputed below. */
28115 help_echo_string = Qnil;
28116
28117 /* Convert to window-relative pixel coordinates. */
28118 w = XWINDOW (window);
28119 frame_to_window_pixel_xy (w, &x, &y);
28120
28121 #ifdef HAVE_WINDOW_SYSTEM
28122 /* Handle tool-bar window differently since it doesn't display a
28123 buffer. */
28124 if (EQ (window, f->tool_bar_window))
28125 {
28126 note_tool_bar_highlight (f, x, y);
28127 return;
28128 }
28129 #endif
28130
28131 /* Mouse is on the mode, header line or margin? */
28132 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28133 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28134 {
28135 note_mode_line_or_margin_highlight (window, x, y, part);
28136 return;
28137 }
28138
28139 #ifdef HAVE_WINDOW_SYSTEM
28140 if (part == ON_VERTICAL_BORDER)
28141 {
28142 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28143 help_echo_string = build_string ("drag-mouse-1: resize");
28144 }
28145 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28146 || part == ON_SCROLL_BAR)
28147 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28148 else
28149 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28150 #endif
28151
28152 /* Are we in a window whose display is up to date?
28153 And verify the buffer's text has not changed. */
28154 b = XBUFFER (w->contents);
28155 if (part == ON_TEXT
28156 && w->window_end_valid
28157 && w->last_modified == BUF_MODIFF (b)
28158 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
28159 {
28160 int hpos, vpos, dx, dy, area = LAST_AREA;
28161 ptrdiff_t pos;
28162 struct glyph *glyph;
28163 Lisp_Object object;
28164 Lisp_Object mouse_face = Qnil, position;
28165 Lisp_Object *overlay_vec = NULL;
28166 ptrdiff_t i, noverlays;
28167 struct buffer *obuf;
28168 ptrdiff_t obegv, ozv;
28169 int same_region;
28170
28171 /* Find the glyph under X/Y. */
28172 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28173
28174 #ifdef HAVE_WINDOW_SYSTEM
28175 /* Look for :pointer property on image. */
28176 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28177 {
28178 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28179 if (img != NULL && IMAGEP (img->spec))
28180 {
28181 Lisp_Object image_map, hotspot;
28182 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28183 !NILP (image_map))
28184 && (hotspot = find_hot_spot (image_map,
28185 glyph->slice.img.x + dx,
28186 glyph->slice.img.y + dy),
28187 CONSP (hotspot))
28188 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28189 {
28190 Lisp_Object plist;
28191
28192 /* Could check XCAR (hotspot) to see if we enter/leave
28193 this hot-spot.
28194 If so, we could look for mouse-enter, mouse-leave
28195 properties in PLIST (and do something...). */
28196 hotspot = XCDR (hotspot);
28197 if (CONSP (hotspot)
28198 && (plist = XCAR (hotspot), CONSP (plist)))
28199 {
28200 pointer = Fplist_get (plist, Qpointer);
28201 if (NILP (pointer))
28202 pointer = Qhand;
28203 help_echo_string = Fplist_get (plist, Qhelp_echo);
28204 if (!NILP (help_echo_string))
28205 {
28206 help_echo_window = window;
28207 help_echo_object = glyph->object;
28208 help_echo_pos = glyph->charpos;
28209 }
28210 }
28211 }
28212 if (NILP (pointer))
28213 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28214 }
28215 }
28216 #endif /* HAVE_WINDOW_SYSTEM */
28217
28218 /* Clear mouse face if X/Y not over text. */
28219 if (glyph == NULL
28220 || area != TEXT_AREA
28221 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28222 /* Glyph's OBJECT is an integer for glyphs inserted by the
28223 display engine for its internal purposes, like truncation
28224 and continuation glyphs and blanks beyond the end of
28225 line's text on text terminals. If we are over such a
28226 glyph, we are not over any text. */
28227 || INTEGERP (glyph->object)
28228 /* R2L rows have a stretch glyph at their front, which
28229 stands for no text, whereas L2R rows have no glyphs at
28230 all beyond the end of text. Treat such stretch glyphs
28231 like we do with NULL glyphs in L2R rows. */
28232 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28233 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28234 && glyph->type == STRETCH_GLYPH
28235 && glyph->avoid_cursor_p))
28236 {
28237 if (clear_mouse_face (hlinfo))
28238 cursor = No_Cursor;
28239 #ifdef HAVE_WINDOW_SYSTEM
28240 if (FRAME_WINDOW_P (f) && NILP (pointer))
28241 {
28242 if (area != TEXT_AREA)
28243 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28244 else
28245 pointer = Vvoid_text_area_pointer;
28246 }
28247 #endif
28248 goto set_cursor;
28249 }
28250
28251 pos = glyph->charpos;
28252 object = glyph->object;
28253 if (!STRINGP (object) && !BUFFERP (object))
28254 goto set_cursor;
28255
28256 /* If we get an out-of-range value, return now; avoid an error. */
28257 if (BUFFERP (object) && pos > BUF_Z (b))
28258 goto set_cursor;
28259
28260 /* Make the window's buffer temporarily current for
28261 overlays_at and compute_char_face. */
28262 obuf = current_buffer;
28263 current_buffer = b;
28264 obegv = BEGV;
28265 ozv = ZV;
28266 BEGV = BEG;
28267 ZV = Z;
28268
28269 /* Is this char mouse-active or does it have help-echo? */
28270 position = make_number (pos);
28271
28272 if (BUFFERP (object))
28273 {
28274 /* Put all the overlays we want in a vector in overlay_vec. */
28275 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28276 /* Sort overlays into increasing priority order. */
28277 noverlays = sort_overlays (overlay_vec, noverlays, w);
28278 }
28279 else
28280 noverlays = 0;
28281
28282 if (NILP (Vmouse_highlight))
28283 {
28284 clear_mouse_face (hlinfo);
28285 goto check_help_echo;
28286 }
28287
28288 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28289
28290 if (same_region)
28291 cursor = No_Cursor;
28292
28293 /* Check mouse-face highlighting. */
28294 if (! same_region
28295 /* If there exists an overlay with mouse-face overlapping
28296 the one we are currently highlighting, we have to
28297 check if we enter the overlapping overlay, and then
28298 highlight only that. */
28299 || (OVERLAYP (hlinfo->mouse_face_overlay)
28300 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28301 {
28302 /* Find the highest priority overlay with a mouse-face. */
28303 Lisp_Object overlay = Qnil;
28304 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28305 {
28306 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28307 if (!NILP (mouse_face))
28308 overlay = overlay_vec[i];
28309 }
28310
28311 /* If we're highlighting the same overlay as before, there's
28312 no need to do that again. */
28313 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28314 goto check_help_echo;
28315 hlinfo->mouse_face_overlay = overlay;
28316
28317 /* Clear the display of the old active region, if any. */
28318 if (clear_mouse_face (hlinfo))
28319 cursor = No_Cursor;
28320
28321 /* If no overlay applies, get a text property. */
28322 if (NILP (overlay))
28323 mouse_face = Fget_text_property (position, Qmouse_face, object);
28324
28325 /* Next, compute the bounds of the mouse highlighting and
28326 display it. */
28327 if (!NILP (mouse_face) && STRINGP (object))
28328 {
28329 /* The mouse-highlighting comes from a display string
28330 with a mouse-face. */
28331 Lisp_Object s, e;
28332 ptrdiff_t ignore;
28333
28334 s = Fprevious_single_property_change
28335 (make_number (pos + 1), Qmouse_face, object, Qnil);
28336 e = Fnext_single_property_change
28337 (position, Qmouse_face, object, Qnil);
28338 if (NILP (s))
28339 s = make_number (0);
28340 if (NILP (e))
28341 e = make_number (SCHARS (object) - 1);
28342 mouse_face_from_string_pos (w, hlinfo, object,
28343 XINT (s), XINT (e));
28344 hlinfo->mouse_face_past_end = 0;
28345 hlinfo->mouse_face_window = window;
28346 hlinfo->mouse_face_face_id
28347 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28348 glyph->face_id, 1);
28349 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28350 cursor = No_Cursor;
28351 }
28352 else
28353 {
28354 /* The mouse-highlighting, if any, comes from an overlay
28355 or text property in the buffer. */
28356 Lisp_Object buffer IF_LINT (= Qnil);
28357 Lisp_Object disp_string IF_LINT (= Qnil);
28358
28359 if (STRINGP (object))
28360 {
28361 /* If we are on a display string with no mouse-face,
28362 check if the text under it has one. */
28363 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28364 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28365 pos = string_buffer_position (object, start);
28366 if (pos > 0)
28367 {
28368 mouse_face = get_char_property_and_overlay
28369 (make_number (pos), Qmouse_face, w->contents, &overlay);
28370 buffer = w->contents;
28371 disp_string = object;
28372 }
28373 }
28374 else
28375 {
28376 buffer = object;
28377 disp_string = Qnil;
28378 }
28379
28380 if (!NILP (mouse_face))
28381 {
28382 Lisp_Object before, after;
28383 Lisp_Object before_string, after_string;
28384 /* To correctly find the limits of mouse highlight
28385 in a bidi-reordered buffer, we must not use the
28386 optimization of limiting the search in
28387 previous-single-property-change and
28388 next-single-property-change, because
28389 rows_from_pos_range needs the real start and end
28390 positions to DTRT in this case. That's because
28391 the first row visible in a window does not
28392 necessarily display the character whose position
28393 is the smallest. */
28394 Lisp_Object lim1 =
28395 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28396 ? Fmarker_position (w->start)
28397 : Qnil;
28398 Lisp_Object lim2 =
28399 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28400 ? make_number (BUF_Z (XBUFFER (buffer))
28401 - XFASTINT (w->window_end_pos))
28402 : Qnil;
28403
28404 if (NILP (overlay))
28405 {
28406 /* Handle the text property case. */
28407 before = Fprevious_single_property_change
28408 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28409 after = Fnext_single_property_change
28410 (make_number (pos), Qmouse_face, buffer, lim2);
28411 before_string = after_string = Qnil;
28412 }
28413 else
28414 {
28415 /* Handle the overlay case. */
28416 before = Foverlay_start (overlay);
28417 after = Foverlay_end (overlay);
28418 before_string = Foverlay_get (overlay, Qbefore_string);
28419 after_string = Foverlay_get (overlay, Qafter_string);
28420
28421 if (!STRINGP (before_string)) before_string = Qnil;
28422 if (!STRINGP (after_string)) after_string = Qnil;
28423 }
28424
28425 mouse_face_from_buffer_pos (window, hlinfo, pos,
28426 NILP (before)
28427 ? 1
28428 : XFASTINT (before),
28429 NILP (after)
28430 ? BUF_Z (XBUFFER (buffer))
28431 : XFASTINT (after),
28432 before_string, after_string,
28433 disp_string);
28434 cursor = No_Cursor;
28435 }
28436 }
28437 }
28438
28439 check_help_echo:
28440
28441 /* Look for a `help-echo' property. */
28442 if (NILP (help_echo_string)) {
28443 Lisp_Object help, overlay;
28444
28445 /* Check overlays first. */
28446 help = overlay = Qnil;
28447 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28448 {
28449 overlay = overlay_vec[i];
28450 help = Foverlay_get (overlay, Qhelp_echo);
28451 }
28452
28453 if (!NILP (help))
28454 {
28455 help_echo_string = help;
28456 help_echo_window = window;
28457 help_echo_object = overlay;
28458 help_echo_pos = pos;
28459 }
28460 else
28461 {
28462 Lisp_Object obj = glyph->object;
28463 ptrdiff_t charpos = glyph->charpos;
28464
28465 /* Try text properties. */
28466 if (STRINGP (obj)
28467 && charpos >= 0
28468 && charpos < SCHARS (obj))
28469 {
28470 help = Fget_text_property (make_number (charpos),
28471 Qhelp_echo, obj);
28472 if (NILP (help))
28473 {
28474 /* If the string itself doesn't specify a help-echo,
28475 see if the buffer text ``under'' it does. */
28476 struct glyph_row *r
28477 = MATRIX_ROW (w->current_matrix, vpos);
28478 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28479 ptrdiff_t p = string_buffer_position (obj, start);
28480 if (p > 0)
28481 {
28482 help = Fget_char_property (make_number (p),
28483 Qhelp_echo, w->contents);
28484 if (!NILP (help))
28485 {
28486 charpos = p;
28487 obj = w->contents;
28488 }
28489 }
28490 }
28491 }
28492 else if (BUFFERP (obj)
28493 && charpos >= BEGV
28494 && charpos < ZV)
28495 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28496 obj);
28497
28498 if (!NILP (help))
28499 {
28500 help_echo_string = help;
28501 help_echo_window = window;
28502 help_echo_object = obj;
28503 help_echo_pos = charpos;
28504 }
28505 }
28506 }
28507
28508 #ifdef HAVE_WINDOW_SYSTEM
28509 /* Look for a `pointer' property. */
28510 if (FRAME_WINDOW_P (f) && NILP (pointer))
28511 {
28512 /* Check overlays first. */
28513 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28514 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28515
28516 if (NILP (pointer))
28517 {
28518 Lisp_Object obj = glyph->object;
28519 ptrdiff_t charpos = glyph->charpos;
28520
28521 /* Try text properties. */
28522 if (STRINGP (obj)
28523 && charpos >= 0
28524 && charpos < SCHARS (obj))
28525 {
28526 pointer = Fget_text_property (make_number (charpos),
28527 Qpointer, obj);
28528 if (NILP (pointer))
28529 {
28530 /* If the string itself doesn't specify a pointer,
28531 see if the buffer text ``under'' it does. */
28532 struct glyph_row *r
28533 = MATRIX_ROW (w->current_matrix, vpos);
28534 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28535 ptrdiff_t p = string_buffer_position (obj, start);
28536 if (p > 0)
28537 pointer = Fget_char_property (make_number (p),
28538 Qpointer, w->contents);
28539 }
28540 }
28541 else if (BUFFERP (obj)
28542 && charpos >= BEGV
28543 && charpos < ZV)
28544 pointer = Fget_text_property (make_number (charpos),
28545 Qpointer, obj);
28546 }
28547 }
28548 #endif /* HAVE_WINDOW_SYSTEM */
28549
28550 BEGV = obegv;
28551 ZV = ozv;
28552 current_buffer = obuf;
28553 }
28554
28555 set_cursor:
28556
28557 #ifdef HAVE_WINDOW_SYSTEM
28558 if (FRAME_WINDOW_P (f))
28559 define_frame_cursor1 (f, cursor, pointer);
28560 #else
28561 /* This is here to prevent a compiler error, about "label at end of
28562 compound statement". */
28563 return;
28564 #endif
28565 }
28566
28567
28568 /* EXPORT for RIF:
28569 Clear any mouse-face on window W. This function is part of the
28570 redisplay interface, and is called from try_window_id and similar
28571 functions to ensure the mouse-highlight is off. */
28572
28573 void
28574 x_clear_window_mouse_face (struct window *w)
28575 {
28576 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28577 Lisp_Object window;
28578
28579 block_input ();
28580 XSETWINDOW (window, w);
28581 if (EQ (window, hlinfo->mouse_face_window))
28582 clear_mouse_face (hlinfo);
28583 unblock_input ();
28584 }
28585
28586
28587 /* EXPORT:
28588 Just discard the mouse face information for frame F, if any.
28589 This is used when the size of F is changed. */
28590
28591 void
28592 cancel_mouse_face (struct frame *f)
28593 {
28594 Lisp_Object window;
28595 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28596
28597 window = hlinfo->mouse_face_window;
28598 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28599 {
28600 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28601 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28602 hlinfo->mouse_face_window = Qnil;
28603 }
28604 }
28605
28606
28607 \f
28608 /***********************************************************************
28609 Exposure Events
28610 ***********************************************************************/
28611
28612 #ifdef HAVE_WINDOW_SYSTEM
28613
28614 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28615 which intersects rectangle R. R is in window-relative coordinates. */
28616
28617 static void
28618 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28619 enum glyph_row_area area)
28620 {
28621 struct glyph *first = row->glyphs[area];
28622 struct glyph *end = row->glyphs[area] + row->used[area];
28623 struct glyph *last;
28624 int first_x, start_x, x;
28625
28626 if (area == TEXT_AREA && row->fill_line_p)
28627 /* If row extends face to end of line write the whole line. */
28628 draw_glyphs (w, 0, row, area,
28629 0, row->used[area],
28630 DRAW_NORMAL_TEXT, 0);
28631 else
28632 {
28633 /* Set START_X to the window-relative start position for drawing glyphs of
28634 AREA. The first glyph of the text area can be partially visible.
28635 The first glyphs of other areas cannot. */
28636 start_x = window_box_left_offset (w, area);
28637 x = start_x;
28638 if (area == TEXT_AREA)
28639 x += row->x;
28640
28641 /* Find the first glyph that must be redrawn. */
28642 while (first < end
28643 && x + first->pixel_width < r->x)
28644 {
28645 x += first->pixel_width;
28646 ++first;
28647 }
28648
28649 /* Find the last one. */
28650 last = first;
28651 first_x = x;
28652 while (last < end
28653 && x < r->x + r->width)
28654 {
28655 x += last->pixel_width;
28656 ++last;
28657 }
28658
28659 /* Repaint. */
28660 if (last > first)
28661 draw_glyphs (w, first_x - start_x, row, area,
28662 first - row->glyphs[area], last - row->glyphs[area],
28663 DRAW_NORMAL_TEXT, 0);
28664 }
28665 }
28666
28667
28668 /* Redraw the parts of the glyph row ROW on window W intersecting
28669 rectangle R. R is in window-relative coordinates. Value is
28670 non-zero if mouse-face was overwritten. */
28671
28672 static int
28673 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28674 {
28675 eassert (row->enabled_p);
28676
28677 if (row->mode_line_p || w->pseudo_window_p)
28678 draw_glyphs (w, 0, row, TEXT_AREA,
28679 0, row->used[TEXT_AREA],
28680 DRAW_NORMAL_TEXT, 0);
28681 else
28682 {
28683 if (row->used[LEFT_MARGIN_AREA])
28684 expose_area (w, row, r, LEFT_MARGIN_AREA);
28685 if (row->used[TEXT_AREA])
28686 expose_area (w, row, r, TEXT_AREA);
28687 if (row->used[RIGHT_MARGIN_AREA])
28688 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28689 draw_row_fringe_bitmaps (w, row);
28690 }
28691
28692 return row->mouse_face_p;
28693 }
28694
28695
28696 /* Redraw those parts of glyphs rows during expose event handling that
28697 overlap other rows. Redrawing of an exposed line writes over parts
28698 of lines overlapping that exposed line; this function fixes that.
28699
28700 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28701 row in W's current matrix that is exposed and overlaps other rows.
28702 LAST_OVERLAPPING_ROW is the last such row. */
28703
28704 static void
28705 expose_overlaps (struct window *w,
28706 struct glyph_row *first_overlapping_row,
28707 struct glyph_row *last_overlapping_row,
28708 XRectangle *r)
28709 {
28710 struct glyph_row *row;
28711
28712 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28713 if (row->overlapping_p)
28714 {
28715 eassert (row->enabled_p && !row->mode_line_p);
28716
28717 row->clip = r;
28718 if (row->used[LEFT_MARGIN_AREA])
28719 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28720
28721 if (row->used[TEXT_AREA])
28722 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28723
28724 if (row->used[RIGHT_MARGIN_AREA])
28725 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28726 row->clip = NULL;
28727 }
28728 }
28729
28730
28731 /* Return non-zero if W's cursor intersects rectangle R. */
28732
28733 static int
28734 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28735 {
28736 XRectangle cr, result;
28737 struct glyph *cursor_glyph;
28738 struct glyph_row *row;
28739
28740 if (w->phys_cursor.vpos >= 0
28741 && w->phys_cursor.vpos < w->current_matrix->nrows
28742 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28743 row->enabled_p)
28744 && row->cursor_in_fringe_p)
28745 {
28746 /* Cursor is in the fringe. */
28747 cr.x = window_box_right_offset (w,
28748 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28749 ? RIGHT_MARGIN_AREA
28750 : TEXT_AREA));
28751 cr.y = row->y;
28752 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28753 cr.height = row->height;
28754 return x_intersect_rectangles (&cr, r, &result);
28755 }
28756
28757 cursor_glyph = get_phys_cursor_glyph (w);
28758 if (cursor_glyph)
28759 {
28760 /* r is relative to W's box, but w->phys_cursor.x is relative
28761 to left edge of W's TEXT area. Adjust it. */
28762 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28763 cr.y = w->phys_cursor.y;
28764 cr.width = cursor_glyph->pixel_width;
28765 cr.height = w->phys_cursor_height;
28766 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28767 I assume the effect is the same -- and this is portable. */
28768 return x_intersect_rectangles (&cr, r, &result);
28769 }
28770 /* If we don't understand the format, pretend we're not in the hot-spot. */
28771 return 0;
28772 }
28773
28774
28775 /* EXPORT:
28776 Draw a vertical window border to the right of window W if W doesn't
28777 have vertical scroll bars. */
28778
28779 void
28780 x_draw_vertical_border (struct window *w)
28781 {
28782 struct frame *f = XFRAME (WINDOW_FRAME (w));
28783
28784 /* We could do better, if we knew what type of scroll-bar the adjacent
28785 windows (on either side) have... But we don't :-(
28786 However, I think this works ok. ++KFS 2003-04-25 */
28787
28788 /* Redraw borders between horizontally adjacent windows. Don't
28789 do it for frames with vertical scroll bars because either the
28790 right scroll bar of a window, or the left scroll bar of its
28791 neighbor will suffice as a border. */
28792 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28793 return;
28794
28795 /* Note: It is necessary to redraw both the left and the right
28796 borders, for when only this single window W is being
28797 redisplayed. */
28798 if (!WINDOW_RIGHTMOST_P (w)
28799 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28800 {
28801 int x0, x1, y0, y1;
28802
28803 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28804 y1 -= 1;
28805
28806 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28807 x1 -= 1;
28808
28809 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28810 }
28811 if (!WINDOW_LEFTMOST_P (w)
28812 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28813 {
28814 int x0, x1, y0, y1;
28815
28816 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28817 y1 -= 1;
28818
28819 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28820 x0 -= 1;
28821
28822 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28823 }
28824 }
28825
28826
28827 /* Redraw the part of window W intersection rectangle FR. Pixel
28828 coordinates in FR are frame-relative. Call this function with
28829 input blocked. Value is non-zero if the exposure overwrites
28830 mouse-face. */
28831
28832 static int
28833 expose_window (struct window *w, XRectangle *fr)
28834 {
28835 struct frame *f = XFRAME (w->frame);
28836 XRectangle wr, r;
28837 int mouse_face_overwritten_p = 0;
28838
28839 /* If window is not yet fully initialized, do nothing. This can
28840 happen when toolkit scroll bars are used and a window is split.
28841 Reconfiguring the scroll bar will generate an expose for a newly
28842 created window. */
28843 if (w->current_matrix == NULL)
28844 return 0;
28845
28846 /* When we're currently updating the window, display and current
28847 matrix usually don't agree. Arrange for a thorough display
28848 later. */
28849 if (w == updated_window)
28850 {
28851 SET_FRAME_GARBAGED (f);
28852 return 0;
28853 }
28854
28855 /* Frame-relative pixel rectangle of W. */
28856 wr.x = WINDOW_LEFT_EDGE_X (w);
28857 wr.y = WINDOW_TOP_EDGE_Y (w);
28858 wr.width = WINDOW_TOTAL_WIDTH (w);
28859 wr.height = WINDOW_TOTAL_HEIGHT (w);
28860
28861 if (x_intersect_rectangles (fr, &wr, &r))
28862 {
28863 int yb = window_text_bottom_y (w);
28864 struct glyph_row *row;
28865 int cursor_cleared_p, phys_cursor_on_p;
28866 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28867
28868 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28869 r.x, r.y, r.width, r.height));
28870
28871 /* Convert to window coordinates. */
28872 r.x -= WINDOW_LEFT_EDGE_X (w);
28873 r.y -= WINDOW_TOP_EDGE_Y (w);
28874
28875 /* Turn off the cursor. */
28876 if (!w->pseudo_window_p
28877 && phys_cursor_in_rect_p (w, &r))
28878 {
28879 x_clear_cursor (w);
28880 cursor_cleared_p = 1;
28881 }
28882 else
28883 cursor_cleared_p = 0;
28884
28885 /* If the row containing the cursor extends face to end of line,
28886 then expose_area might overwrite the cursor outside the
28887 rectangle and thus notice_overwritten_cursor might clear
28888 w->phys_cursor_on_p. We remember the original value and
28889 check later if it is changed. */
28890 phys_cursor_on_p = w->phys_cursor_on_p;
28891
28892 /* Update lines intersecting rectangle R. */
28893 first_overlapping_row = last_overlapping_row = NULL;
28894 for (row = w->current_matrix->rows;
28895 row->enabled_p;
28896 ++row)
28897 {
28898 int y0 = row->y;
28899 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28900
28901 if ((y0 >= r.y && y0 < r.y + r.height)
28902 || (y1 > r.y && y1 < r.y + r.height)
28903 || (r.y >= y0 && r.y < y1)
28904 || (r.y + r.height > y0 && r.y + r.height < y1))
28905 {
28906 /* A header line may be overlapping, but there is no need
28907 to fix overlapping areas for them. KFS 2005-02-12 */
28908 if (row->overlapping_p && !row->mode_line_p)
28909 {
28910 if (first_overlapping_row == NULL)
28911 first_overlapping_row = row;
28912 last_overlapping_row = row;
28913 }
28914
28915 row->clip = fr;
28916 if (expose_line (w, row, &r))
28917 mouse_face_overwritten_p = 1;
28918 row->clip = NULL;
28919 }
28920 else if (row->overlapping_p)
28921 {
28922 /* We must redraw a row overlapping the exposed area. */
28923 if (y0 < r.y
28924 ? y0 + row->phys_height > r.y
28925 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28926 {
28927 if (first_overlapping_row == NULL)
28928 first_overlapping_row = row;
28929 last_overlapping_row = row;
28930 }
28931 }
28932
28933 if (y1 >= yb)
28934 break;
28935 }
28936
28937 /* Display the mode line if there is one. */
28938 if (WINDOW_WANTS_MODELINE_P (w)
28939 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28940 row->enabled_p)
28941 && row->y < r.y + r.height)
28942 {
28943 if (expose_line (w, row, &r))
28944 mouse_face_overwritten_p = 1;
28945 }
28946
28947 if (!w->pseudo_window_p)
28948 {
28949 /* Fix the display of overlapping rows. */
28950 if (first_overlapping_row)
28951 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28952 fr);
28953
28954 /* Draw border between windows. */
28955 x_draw_vertical_border (w);
28956
28957 /* Turn the cursor on again. */
28958 if (cursor_cleared_p
28959 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28960 update_window_cursor (w, 1);
28961 }
28962 }
28963
28964 return mouse_face_overwritten_p;
28965 }
28966
28967
28968
28969 /* Redraw (parts) of all windows in the window tree rooted at W that
28970 intersect R. R contains frame pixel coordinates. Value is
28971 non-zero if the exposure overwrites mouse-face. */
28972
28973 static int
28974 expose_window_tree (struct window *w, XRectangle *r)
28975 {
28976 struct frame *f = XFRAME (w->frame);
28977 int mouse_face_overwritten_p = 0;
28978
28979 while (w && !FRAME_GARBAGED_P (f))
28980 {
28981 if (WINDOWP (w->contents))
28982 mouse_face_overwritten_p
28983 |= expose_window_tree (XWINDOW (w->contents), r);
28984 else
28985 mouse_face_overwritten_p |= expose_window (w, r);
28986
28987 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28988 }
28989
28990 return mouse_face_overwritten_p;
28991 }
28992
28993
28994 /* EXPORT:
28995 Redisplay an exposed area of frame F. X and Y are the upper-left
28996 corner of the exposed rectangle. W and H are width and height of
28997 the exposed area. All are pixel values. W or H zero means redraw
28998 the entire frame. */
28999
29000 void
29001 expose_frame (struct frame *f, int x, int y, int w, int h)
29002 {
29003 XRectangle r;
29004 int mouse_face_overwritten_p = 0;
29005
29006 TRACE ((stderr, "expose_frame "));
29007
29008 /* No need to redraw if frame will be redrawn soon. */
29009 if (FRAME_GARBAGED_P (f))
29010 {
29011 TRACE ((stderr, " garbaged\n"));
29012 return;
29013 }
29014
29015 /* If basic faces haven't been realized yet, there is no point in
29016 trying to redraw anything. This can happen when we get an expose
29017 event while Emacs is starting, e.g. by moving another window. */
29018 if (FRAME_FACE_CACHE (f) == NULL
29019 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29020 {
29021 TRACE ((stderr, " no faces\n"));
29022 return;
29023 }
29024
29025 if (w == 0 || h == 0)
29026 {
29027 r.x = r.y = 0;
29028 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29029 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29030 }
29031 else
29032 {
29033 r.x = x;
29034 r.y = y;
29035 r.width = w;
29036 r.height = h;
29037 }
29038
29039 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29040 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29041
29042 if (WINDOWP (f->tool_bar_window))
29043 mouse_face_overwritten_p
29044 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29045
29046 #ifdef HAVE_X_WINDOWS
29047 #ifndef MSDOS
29048 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29049 if (WINDOWP (f->menu_bar_window))
29050 mouse_face_overwritten_p
29051 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29052 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29053 #endif
29054 #endif
29055
29056 /* Some window managers support a focus-follows-mouse style with
29057 delayed raising of frames. Imagine a partially obscured frame,
29058 and moving the mouse into partially obscured mouse-face on that
29059 frame. The visible part of the mouse-face will be highlighted,
29060 then the WM raises the obscured frame. With at least one WM, KDE
29061 2.1, Emacs is not getting any event for the raising of the frame
29062 (even tried with SubstructureRedirectMask), only Expose events.
29063 These expose events will draw text normally, i.e. not
29064 highlighted. Which means we must redo the highlight here.
29065 Subsume it under ``we love X''. --gerd 2001-08-15 */
29066 /* Included in Windows version because Windows most likely does not
29067 do the right thing if any third party tool offers
29068 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29069 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29070 {
29071 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29072 if (f == hlinfo->mouse_face_mouse_frame)
29073 {
29074 int mouse_x = hlinfo->mouse_face_mouse_x;
29075 int mouse_y = hlinfo->mouse_face_mouse_y;
29076 clear_mouse_face (hlinfo);
29077 note_mouse_highlight (f, mouse_x, mouse_y);
29078 }
29079 }
29080 }
29081
29082
29083 /* EXPORT:
29084 Determine the intersection of two rectangles R1 and R2. Return
29085 the intersection in *RESULT. Value is non-zero if RESULT is not
29086 empty. */
29087
29088 int
29089 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29090 {
29091 XRectangle *left, *right;
29092 XRectangle *upper, *lower;
29093 int intersection_p = 0;
29094
29095 /* Rearrange so that R1 is the left-most rectangle. */
29096 if (r1->x < r2->x)
29097 left = r1, right = r2;
29098 else
29099 left = r2, right = r1;
29100
29101 /* X0 of the intersection is right.x0, if this is inside R1,
29102 otherwise there is no intersection. */
29103 if (right->x <= left->x + left->width)
29104 {
29105 result->x = right->x;
29106
29107 /* The right end of the intersection is the minimum of
29108 the right ends of left and right. */
29109 result->width = (min (left->x + left->width, right->x + right->width)
29110 - result->x);
29111
29112 /* Same game for Y. */
29113 if (r1->y < r2->y)
29114 upper = r1, lower = r2;
29115 else
29116 upper = r2, lower = r1;
29117
29118 /* The upper end of the intersection is lower.y0, if this is inside
29119 of upper. Otherwise, there is no intersection. */
29120 if (lower->y <= upper->y + upper->height)
29121 {
29122 result->y = lower->y;
29123
29124 /* The lower end of the intersection is the minimum of the lower
29125 ends of upper and lower. */
29126 result->height = (min (lower->y + lower->height,
29127 upper->y + upper->height)
29128 - result->y);
29129 intersection_p = 1;
29130 }
29131 }
29132
29133 return intersection_p;
29134 }
29135
29136 #endif /* HAVE_WINDOW_SYSTEM */
29137
29138 \f
29139 /***********************************************************************
29140 Initialization
29141 ***********************************************************************/
29142
29143 void
29144 syms_of_xdisp (void)
29145 {
29146 Vwith_echo_area_save_vector = Qnil;
29147 staticpro (&Vwith_echo_area_save_vector);
29148
29149 Vmessage_stack = Qnil;
29150 staticpro (&Vmessage_stack);
29151
29152 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29153 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29154
29155 message_dolog_marker1 = Fmake_marker ();
29156 staticpro (&message_dolog_marker1);
29157 message_dolog_marker2 = Fmake_marker ();
29158 staticpro (&message_dolog_marker2);
29159 message_dolog_marker3 = Fmake_marker ();
29160 staticpro (&message_dolog_marker3);
29161
29162 #ifdef GLYPH_DEBUG
29163 defsubr (&Sdump_frame_glyph_matrix);
29164 defsubr (&Sdump_glyph_matrix);
29165 defsubr (&Sdump_glyph_row);
29166 defsubr (&Sdump_tool_bar_row);
29167 defsubr (&Strace_redisplay);
29168 defsubr (&Strace_to_stderr);
29169 #endif
29170 #ifdef HAVE_WINDOW_SYSTEM
29171 defsubr (&Stool_bar_lines_needed);
29172 defsubr (&Slookup_image_map);
29173 #endif
29174 defsubr (&Sline_pixel_height);
29175 defsubr (&Sformat_mode_line);
29176 defsubr (&Sinvisible_p);
29177 defsubr (&Scurrent_bidi_paragraph_direction);
29178 defsubr (&Smove_point_visually);
29179
29180 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29181 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29182 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29183 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29184 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29185 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29186 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29187 DEFSYM (Qeval, "eval");
29188 DEFSYM (QCdata, ":data");
29189 DEFSYM (Qdisplay, "display");
29190 DEFSYM (Qspace_width, "space-width");
29191 DEFSYM (Qraise, "raise");
29192 DEFSYM (Qslice, "slice");
29193 DEFSYM (Qspace, "space");
29194 DEFSYM (Qmargin, "margin");
29195 DEFSYM (Qpointer, "pointer");
29196 DEFSYM (Qleft_margin, "left-margin");
29197 DEFSYM (Qright_margin, "right-margin");
29198 DEFSYM (Qcenter, "center");
29199 DEFSYM (Qline_height, "line-height");
29200 DEFSYM (QCalign_to, ":align-to");
29201 DEFSYM (QCrelative_width, ":relative-width");
29202 DEFSYM (QCrelative_height, ":relative-height");
29203 DEFSYM (QCeval, ":eval");
29204 DEFSYM (QCpropertize, ":propertize");
29205 DEFSYM (QCfile, ":file");
29206 DEFSYM (Qfontified, "fontified");
29207 DEFSYM (Qfontification_functions, "fontification-functions");
29208 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29209 DEFSYM (Qescape_glyph, "escape-glyph");
29210 DEFSYM (Qnobreak_space, "nobreak-space");
29211 DEFSYM (Qimage, "image");
29212 DEFSYM (Qtext, "text");
29213 DEFSYM (Qboth, "both");
29214 DEFSYM (Qboth_horiz, "both-horiz");
29215 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29216 DEFSYM (QCmap, ":map");
29217 DEFSYM (QCpointer, ":pointer");
29218 DEFSYM (Qrect, "rect");
29219 DEFSYM (Qcircle, "circle");
29220 DEFSYM (Qpoly, "poly");
29221 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29222 DEFSYM (Qgrow_only, "grow-only");
29223 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29224 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29225 DEFSYM (Qposition, "position");
29226 DEFSYM (Qbuffer_position, "buffer-position");
29227 DEFSYM (Qobject, "object");
29228 DEFSYM (Qbar, "bar");
29229 DEFSYM (Qhbar, "hbar");
29230 DEFSYM (Qbox, "box");
29231 DEFSYM (Qhollow, "hollow");
29232 DEFSYM (Qhand, "hand");
29233 DEFSYM (Qarrow, "arrow");
29234 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29235
29236 list_of_error = Fcons (Fcons (intern_c_string ("error"),
29237 Fcons (intern_c_string ("void-variable"), Qnil)),
29238 Qnil);
29239 staticpro (&list_of_error);
29240
29241 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29242 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29243 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29244 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29245
29246 echo_buffer[0] = echo_buffer[1] = Qnil;
29247 staticpro (&echo_buffer[0]);
29248 staticpro (&echo_buffer[1]);
29249
29250 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29251 staticpro (&echo_area_buffer[0]);
29252 staticpro (&echo_area_buffer[1]);
29253
29254 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29255 staticpro (&Vmessages_buffer_name);
29256
29257 mode_line_proptrans_alist = Qnil;
29258 staticpro (&mode_line_proptrans_alist);
29259 mode_line_string_list = Qnil;
29260 staticpro (&mode_line_string_list);
29261 mode_line_string_face = Qnil;
29262 staticpro (&mode_line_string_face);
29263 mode_line_string_face_prop = Qnil;
29264 staticpro (&mode_line_string_face_prop);
29265 Vmode_line_unwind_vector = Qnil;
29266 staticpro (&Vmode_line_unwind_vector);
29267
29268 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29269
29270 help_echo_string = Qnil;
29271 staticpro (&help_echo_string);
29272 help_echo_object = Qnil;
29273 staticpro (&help_echo_object);
29274 help_echo_window = Qnil;
29275 staticpro (&help_echo_window);
29276 previous_help_echo_string = Qnil;
29277 staticpro (&previous_help_echo_string);
29278 help_echo_pos = -1;
29279
29280 DEFSYM (Qright_to_left, "right-to-left");
29281 DEFSYM (Qleft_to_right, "left-to-right");
29282
29283 #ifdef HAVE_WINDOW_SYSTEM
29284 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29285 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29286 For example, if a block cursor is over a tab, it will be drawn as
29287 wide as that tab on the display. */);
29288 x_stretch_cursor_p = 0;
29289 #endif
29290
29291 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29292 doc: /* Non-nil means highlight trailing whitespace.
29293 The face used for trailing whitespace is `trailing-whitespace'. */);
29294 Vshow_trailing_whitespace = Qnil;
29295
29296 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29297 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29298 If the value is t, Emacs highlights non-ASCII chars which have the
29299 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29300 or `escape-glyph' face respectively.
29301
29302 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29303 U+2011 (non-breaking hyphen) are affected.
29304
29305 Any other non-nil value means to display these characters as a escape
29306 glyph followed by an ordinary space or hyphen.
29307
29308 A value of nil means no special handling of these characters. */);
29309 Vnobreak_char_display = Qt;
29310
29311 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29312 doc: /* The pointer shape to show in void text areas.
29313 A value of nil means to show the text pointer. Other options are `arrow',
29314 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29315 Vvoid_text_area_pointer = Qarrow;
29316
29317 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29318 doc: /* Non-nil means don't actually do any redisplay.
29319 This is used for internal purposes. */);
29320 Vinhibit_redisplay = Qnil;
29321
29322 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29323 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29324 Vglobal_mode_string = Qnil;
29325
29326 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29327 doc: /* Marker for where to display an arrow on top of the buffer text.
29328 This must be the beginning of a line in order to work.
29329 See also `overlay-arrow-string'. */);
29330 Voverlay_arrow_position = Qnil;
29331
29332 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29333 doc: /* String to display as an arrow in non-window frames.
29334 See also `overlay-arrow-position'. */);
29335 Voverlay_arrow_string = build_pure_c_string ("=>");
29336
29337 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29338 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29339 The symbols on this list are examined during redisplay to determine
29340 where to display overlay arrows. */);
29341 Voverlay_arrow_variable_list
29342 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
29343
29344 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29345 doc: /* The number of lines to try scrolling a window by when point moves out.
29346 If that fails to bring point back on frame, point is centered instead.
29347 If this is zero, point is always centered after it moves off frame.
29348 If you want scrolling to always be a line at a time, you should set
29349 `scroll-conservatively' to a large value rather than set this to 1. */);
29350
29351 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29352 doc: /* Scroll up to this many lines, to bring point back on screen.
29353 If point moves off-screen, redisplay will scroll by up to
29354 `scroll-conservatively' lines in order to bring point just barely
29355 onto the screen again. If that cannot be done, then redisplay
29356 recenters point as usual.
29357
29358 If the value is greater than 100, redisplay will never recenter point,
29359 but will always scroll just enough text to bring point into view, even
29360 if you move far away.
29361
29362 A value of zero means always recenter point if it moves off screen. */);
29363 scroll_conservatively = 0;
29364
29365 DEFVAR_INT ("scroll-margin", scroll_margin,
29366 doc: /* Number of lines of margin at the top and bottom of a window.
29367 Recenter the window whenever point gets within this many lines
29368 of the top or bottom of the window. */);
29369 scroll_margin = 0;
29370
29371 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29372 doc: /* Pixels per inch value for non-window system displays.
29373 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29374 Vdisplay_pixels_per_inch = make_float (72.0);
29375
29376 #ifdef GLYPH_DEBUG
29377 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29378 #endif
29379
29380 DEFVAR_LISP ("truncate-partial-width-windows",
29381 Vtruncate_partial_width_windows,
29382 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29383 For an integer value, truncate lines in each window narrower than the
29384 full frame width, provided the window width is less than that integer;
29385 otherwise, respect the value of `truncate-lines'.
29386
29387 For any other non-nil value, truncate lines in all windows that do
29388 not span the full frame width.
29389
29390 A value of nil means to respect the value of `truncate-lines'.
29391
29392 If `word-wrap' is enabled, you might want to reduce this. */);
29393 Vtruncate_partial_width_windows = make_number (50);
29394
29395 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29396 doc: /* Maximum buffer size for which line number should be displayed.
29397 If the buffer is bigger than this, the line number does not appear
29398 in the mode line. A value of nil means no limit. */);
29399 Vline_number_display_limit = Qnil;
29400
29401 DEFVAR_INT ("line-number-display-limit-width",
29402 line_number_display_limit_width,
29403 doc: /* Maximum line width (in characters) for line number display.
29404 If the average length of the lines near point is bigger than this, then the
29405 line number may be omitted from the mode line. */);
29406 line_number_display_limit_width = 200;
29407
29408 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29409 doc: /* Non-nil means highlight region even in nonselected windows. */);
29410 highlight_nonselected_windows = 0;
29411
29412 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29413 doc: /* Non-nil if more than one frame is visible on this display.
29414 Minibuffer-only frames don't count, but iconified frames do.
29415 This variable is not guaranteed to be accurate except while processing
29416 `frame-title-format' and `icon-title-format'. */);
29417
29418 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29419 doc: /* Template for displaying the title bar of visible frames.
29420 \(Assuming the window manager supports this feature.)
29421
29422 This variable has the same structure as `mode-line-format', except that
29423 the %c and %l constructs are ignored. It is used only on frames for
29424 which no explicit name has been set \(see `modify-frame-parameters'). */);
29425
29426 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29427 doc: /* Template for displaying the title bar of an iconified frame.
29428 \(Assuming the window manager supports this feature.)
29429 This variable has the same structure as `mode-line-format' (which see),
29430 and is used only on frames for which no explicit name has been set
29431 \(see `modify-frame-parameters'). */);
29432 Vicon_title_format
29433 = Vframe_title_format
29434 = listn (CONSTYPE_PURE, 3,
29435 intern_c_string ("multiple-frames"),
29436 build_pure_c_string ("%b"),
29437 listn (CONSTYPE_PURE, 4,
29438 empty_unibyte_string,
29439 intern_c_string ("invocation-name"),
29440 build_pure_c_string ("@"),
29441 intern_c_string ("system-name")));
29442
29443 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29444 doc: /* Maximum number of lines to keep in the message log buffer.
29445 If nil, disable message logging. If t, log messages but don't truncate
29446 the buffer when it becomes large. */);
29447 Vmessage_log_max = make_number (1000);
29448
29449 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29450 doc: /* Functions called before redisplay, if window sizes have changed.
29451 The value should be a list of functions that take one argument.
29452 Just before redisplay, for each frame, if any of its windows have changed
29453 size since the last redisplay, or have been split or deleted,
29454 all the functions in the list are called, with the frame as argument. */);
29455 Vwindow_size_change_functions = Qnil;
29456
29457 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29458 doc: /* List of functions to call before redisplaying a window with scrolling.
29459 Each function is called with two arguments, the window and its new
29460 display-start position. Note that these functions are also called by
29461 `set-window-buffer'. Also note that the value of `window-end' is not
29462 valid when these functions are called.
29463
29464 Warning: Do not use this feature to alter the way the window
29465 is scrolled. It is not designed for that, and such use probably won't
29466 work. */);
29467 Vwindow_scroll_functions = Qnil;
29468
29469 DEFVAR_LISP ("window-text-change-functions",
29470 Vwindow_text_change_functions,
29471 doc: /* Functions to call in redisplay when text in the window might change. */);
29472 Vwindow_text_change_functions = Qnil;
29473
29474 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29475 doc: /* Functions called when redisplay of a window reaches the end trigger.
29476 Each function is called with two arguments, the window and the end trigger value.
29477 See `set-window-redisplay-end-trigger'. */);
29478 Vredisplay_end_trigger_functions = Qnil;
29479
29480 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29481 doc: /* Non-nil means autoselect window with mouse pointer.
29482 If nil, do not autoselect windows.
29483 A positive number means delay autoselection by that many seconds: a
29484 window is autoselected only after the mouse has remained in that
29485 window for the duration of the delay.
29486 A negative number has a similar effect, but causes windows to be
29487 autoselected only after the mouse has stopped moving. \(Because of
29488 the way Emacs compares mouse events, you will occasionally wait twice
29489 that time before the window gets selected.\)
29490 Any other value means to autoselect window instantaneously when the
29491 mouse pointer enters it.
29492
29493 Autoselection selects the minibuffer only if it is active, and never
29494 unselects the minibuffer if it is active.
29495
29496 When customizing this variable make sure that the actual value of
29497 `focus-follows-mouse' matches the behavior of your window manager. */);
29498 Vmouse_autoselect_window = Qnil;
29499
29500 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29501 doc: /* Non-nil means automatically resize tool-bars.
29502 This dynamically changes the tool-bar's height to the minimum height
29503 that is needed to make all tool-bar items visible.
29504 If value is `grow-only', the tool-bar's height is only increased
29505 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29506 Vauto_resize_tool_bars = Qt;
29507
29508 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29509 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29510 auto_raise_tool_bar_buttons_p = 1;
29511
29512 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29513 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29514 make_cursor_line_fully_visible_p = 1;
29515
29516 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29517 doc: /* Border below tool-bar in pixels.
29518 If an integer, use it as the height of the border.
29519 If it is one of `internal-border-width' or `border-width', use the
29520 value of the corresponding frame parameter.
29521 Otherwise, no border is added below the tool-bar. */);
29522 Vtool_bar_border = Qinternal_border_width;
29523
29524 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29525 doc: /* Margin around tool-bar buttons in pixels.
29526 If an integer, use that for both horizontal and vertical margins.
29527 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29528 HORZ specifying the horizontal margin, and VERT specifying the
29529 vertical margin. */);
29530 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29531
29532 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29533 doc: /* Relief thickness of tool-bar buttons. */);
29534 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29535
29536 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29537 doc: /* Tool bar style to use.
29538 It can be one of
29539 image - show images only
29540 text - show text only
29541 both - show both, text below image
29542 both-horiz - show text to the right of the image
29543 text-image-horiz - show text to the left of the image
29544 any other - use system default or image if no system default.
29545
29546 This variable only affects the GTK+ toolkit version of Emacs. */);
29547 Vtool_bar_style = Qnil;
29548
29549 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29550 doc: /* Maximum number of characters a label can have to be shown.
29551 The tool bar style must also show labels for this to have any effect, see
29552 `tool-bar-style'. */);
29553 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29554
29555 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29556 doc: /* List of functions to call to fontify regions of text.
29557 Each function is called with one argument POS. Functions must
29558 fontify a region starting at POS in the current buffer, and give
29559 fontified regions the property `fontified'. */);
29560 Vfontification_functions = Qnil;
29561 Fmake_variable_buffer_local (Qfontification_functions);
29562
29563 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29564 unibyte_display_via_language_environment,
29565 doc: /* Non-nil means display unibyte text according to language environment.
29566 Specifically, this means that raw bytes in the range 160-255 decimal
29567 are displayed by converting them to the equivalent multibyte characters
29568 according to the current language environment. As a result, they are
29569 displayed according to the current fontset.
29570
29571 Note that this variable affects only how these bytes are displayed,
29572 but does not change the fact they are interpreted as raw bytes. */);
29573 unibyte_display_via_language_environment = 0;
29574
29575 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29576 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29577 If a float, it specifies a fraction of the mini-window frame's height.
29578 If an integer, it specifies a number of lines. */);
29579 Vmax_mini_window_height = make_float (0.25);
29580
29581 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29582 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29583 A value of nil means don't automatically resize mini-windows.
29584 A value of t means resize them to fit the text displayed in them.
29585 A value of `grow-only', the default, means let mini-windows grow only;
29586 they return to their normal size when the minibuffer is closed, or the
29587 echo area becomes empty. */);
29588 Vresize_mini_windows = Qgrow_only;
29589
29590 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29591 doc: /* Alist specifying how to blink the cursor off.
29592 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29593 `cursor-type' frame-parameter or variable equals ON-STATE,
29594 comparing using `equal', Emacs uses OFF-STATE to specify
29595 how to blink it off. ON-STATE and OFF-STATE are values for
29596 the `cursor-type' frame parameter.
29597
29598 If a frame's ON-STATE has no entry in this list,
29599 the frame's other specifications determine how to blink the cursor off. */);
29600 Vblink_cursor_alist = Qnil;
29601
29602 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29603 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29604 If non-nil, windows are automatically scrolled horizontally to make
29605 point visible. */);
29606 automatic_hscrolling_p = 1;
29607 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29608
29609 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29610 doc: /* How many columns away from the window edge point is allowed to get
29611 before automatic hscrolling will horizontally scroll the window. */);
29612 hscroll_margin = 5;
29613
29614 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29615 doc: /* How many columns to scroll the window when point gets too close to the edge.
29616 When point is less than `hscroll-margin' columns from the window
29617 edge, automatic hscrolling will scroll the window by the amount of columns
29618 determined by this variable. If its value is a positive integer, scroll that
29619 many columns. If it's a positive floating-point number, it specifies the
29620 fraction of the window's width to scroll. If it's nil or zero, point will be
29621 centered horizontally after the scroll. Any other value, including negative
29622 numbers, are treated as if the value were zero.
29623
29624 Automatic hscrolling always moves point outside the scroll margin, so if
29625 point was more than scroll step columns inside the margin, the window will
29626 scroll more than the value given by the scroll step.
29627
29628 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29629 and `scroll-right' overrides this variable's effect. */);
29630 Vhscroll_step = make_number (0);
29631
29632 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29633 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29634 Bind this around calls to `message' to let it take effect. */);
29635 message_truncate_lines = 0;
29636
29637 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29638 doc: /* Normal hook run to update the menu bar definitions.
29639 Redisplay runs this hook before it redisplays the menu bar.
29640 This is used to update submenus such as Buffers,
29641 whose contents depend on various data. */);
29642 Vmenu_bar_update_hook = Qnil;
29643
29644 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29645 doc: /* Frame for which we are updating a menu.
29646 The enable predicate for a menu binding should check this variable. */);
29647 Vmenu_updating_frame = Qnil;
29648
29649 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29650 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29651 inhibit_menubar_update = 0;
29652
29653 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29654 doc: /* Prefix prepended to all continuation lines at display time.
29655 The value may be a string, an image, or a stretch-glyph; it is
29656 interpreted in the same way as the value of a `display' text property.
29657
29658 This variable is overridden by any `wrap-prefix' text or overlay
29659 property.
29660
29661 To add a prefix to non-continuation lines, use `line-prefix'. */);
29662 Vwrap_prefix = Qnil;
29663 DEFSYM (Qwrap_prefix, "wrap-prefix");
29664 Fmake_variable_buffer_local (Qwrap_prefix);
29665
29666 DEFVAR_LISP ("line-prefix", Vline_prefix,
29667 doc: /* Prefix prepended to all non-continuation lines at display time.
29668 The value may be a string, an image, or a stretch-glyph; it is
29669 interpreted in the same way as the value of a `display' text property.
29670
29671 This variable is overridden by any `line-prefix' text or overlay
29672 property.
29673
29674 To add a prefix to continuation lines, use `wrap-prefix'. */);
29675 Vline_prefix = Qnil;
29676 DEFSYM (Qline_prefix, "line-prefix");
29677 Fmake_variable_buffer_local (Qline_prefix);
29678
29679 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29680 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29681 inhibit_eval_during_redisplay = 0;
29682
29683 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29684 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29685 inhibit_free_realized_faces = 0;
29686
29687 #ifdef GLYPH_DEBUG
29688 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29689 doc: /* Inhibit try_window_id display optimization. */);
29690 inhibit_try_window_id = 0;
29691
29692 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29693 doc: /* Inhibit try_window_reusing display optimization. */);
29694 inhibit_try_window_reusing = 0;
29695
29696 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29697 doc: /* Inhibit try_cursor_movement display optimization. */);
29698 inhibit_try_cursor_movement = 0;
29699 #endif /* GLYPH_DEBUG */
29700
29701 DEFVAR_INT ("overline-margin", overline_margin,
29702 doc: /* Space between overline and text, in pixels.
29703 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29704 margin to the character height. */);
29705 overline_margin = 2;
29706
29707 DEFVAR_INT ("underline-minimum-offset",
29708 underline_minimum_offset,
29709 doc: /* Minimum distance between baseline and underline.
29710 This can improve legibility of underlined text at small font sizes,
29711 particularly when using variable `x-use-underline-position-properties'
29712 with fonts that specify an UNDERLINE_POSITION relatively close to the
29713 baseline. The default value is 1. */);
29714 underline_minimum_offset = 1;
29715
29716 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29717 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29718 This feature only works when on a window system that can change
29719 cursor shapes. */);
29720 display_hourglass_p = 1;
29721
29722 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29723 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29724 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29725
29726 hourglass_atimer = NULL;
29727 hourglass_shown_p = 0;
29728
29729 DEFSYM (Qglyphless_char, "glyphless-char");
29730 DEFSYM (Qhex_code, "hex-code");
29731 DEFSYM (Qempty_box, "empty-box");
29732 DEFSYM (Qthin_space, "thin-space");
29733 DEFSYM (Qzero_width, "zero-width");
29734
29735 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29736 /* Intern this now in case it isn't already done.
29737 Setting this variable twice is harmless.
29738 But don't staticpro it here--that is done in alloc.c. */
29739 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29740 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29741
29742 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29743 doc: /* Char-table defining glyphless characters.
29744 Each element, if non-nil, should be one of the following:
29745 an ASCII acronym string: display this string in a box
29746 `hex-code': display the hexadecimal code of a character in a box
29747 `empty-box': display as an empty box
29748 `thin-space': display as 1-pixel width space
29749 `zero-width': don't display
29750 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29751 display method for graphical terminals and text terminals respectively.
29752 GRAPHICAL and TEXT should each have one of the values listed above.
29753
29754 The char-table has one extra slot to control the display of a character for
29755 which no font is found. This slot only takes effect on graphical terminals.
29756 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29757 `thin-space'. The default is `empty-box'. */);
29758 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29759 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29760 Qempty_box);
29761
29762 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29763 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29764 Vdebug_on_message = Qnil;
29765 }
29766
29767
29768 /* Initialize this module when Emacs starts. */
29769
29770 void
29771 init_xdisp (void)
29772 {
29773 current_header_line_height = current_mode_line_height = -1;
29774
29775 CHARPOS (this_line_start_pos) = 0;
29776
29777 if (!noninteractive)
29778 {
29779 struct window *m = XWINDOW (minibuf_window);
29780 Lisp_Object frame = m->frame;
29781 struct frame *f = XFRAME (frame);
29782 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29783 struct window *r = XWINDOW (root);
29784 int i;
29785
29786 echo_area_window = minibuf_window;
29787
29788 r->top_line = FRAME_TOP_MARGIN (f);
29789 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29790 r->total_cols = FRAME_COLS (f);
29791
29792 m->top_line = FRAME_LINES (f) - 1;
29793 m->total_lines = 1;
29794 m->total_cols = FRAME_COLS (f);
29795
29796 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29797 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29798 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29799
29800 /* The default ellipsis glyphs `...'. */
29801 for (i = 0; i < 3; ++i)
29802 default_invis_vector[i] = make_number ('.');
29803 }
29804
29805 {
29806 /* Allocate the buffer for frame titles.
29807 Also used for `format-mode-line'. */
29808 int size = 100;
29809 mode_line_noprop_buf = xmalloc (size);
29810 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29811 mode_line_noprop_ptr = mode_line_noprop_buf;
29812 mode_line_target = MODE_LINE_DISPLAY;
29813 }
29814
29815 help_echo_showing_p = 0;
29816 }
29817
29818 /* Platform-independent portion of hourglass implementation. */
29819
29820 /* Cancel a currently active hourglass timer, and start a new one. */
29821 void
29822 start_hourglass (void)
29823 {
29824 #if defined (HAVE_WINDOW_SYSTEM)
29825 EMACS_TIME delay;
29826
29827 cancel_hourglass ();
29828
29829 if (INTEGERP (Vhourglass_delay)
29830 && XINT (Vhourglass_delay) > 0)
29831 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29832 TYPE_MAXIMUM (time_t)),
29833 0);
29834 else if (FLOATP (Vhourglass_delay)
29835 && XFLOAT_DATA (Vhourglass_delay) > 0)
29836 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29837 else
29838 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29839
29840 #ifdef HAVE_NTGUI
29841 {
29842 extern void w32_note_current_window (void);
29843 w32_note_current_window ();
29844 }
29845 #endif /* HAVE_NTGUI */
29846
29847 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29848 show_hourglass, NULL);
29849 #endif
29850 }
29851
29852
29853 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29854 shown. */
29855 void
29856 cancel_hourglass (void)
29857 {
29858 #if defined (HAVE_WINDOW_SYSTEM)
29859 if (hourglass_atimer)
29860 {
29861 cancel_atimer (hourglass_atimer);
29862 hourglass_atimer = NULL;
29863 }
29864
29865 if (hourglass_shown_p)
29866 hide_hourglass ();
29867 #endif
29868 }