Generalize INTERNAL_FIELD between buffers, keyboards and frames.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
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 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT, or the underlying buffer
387 or string character, is a space or a TAB character. This is used
388 to determine where word wrapping can occur. */
389
390 #define IT_DISPLAYING_WHITESPACE(it) \
391 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
392 || ((STRINGP (it->string) \
393 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
394 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
395 || (it->s \
396 && (it->s[IT_BYTEPOS (*it)] == ' ' \
397 || it->s[IT_BYTEPOS (*it)] == '\t')) \
398 || (IT_BYTEPOS (*it) < ZV_BYTE \
399 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
400 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
401
402 /* Name of the face used to highlight trailing whitespace. */
403
404 static Lisp_Object Qtrailing_whitespace;
405
406 /* Name and number of the face used to highlight escape glyphs. */
407
408 static Lisp_Object Qescape_glyph;
409
410 /* Name and number of the face used to highlight non-breaking spaces. */
411
412 static Lisp_Object Qnobreak_space;
413
414 /* The symbol `image' which is the car of the lists used to represent
415 images in Lisp. Also a tool bar style. */
416
417 Lisp_Object Qimage;
418
419 /* The image map types. */
420 Lisp_Object QCmap;
421 static Lisp_Object QCpointer;
422 static Lisp_Object Qrect, Qcircle, Qpoly;
423
424 /* Tool bar styles */
425 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
426
427 /* Non-zero means print newline to stdout before next mini-buffer
428 message. */
429
430 int noninteractive_need_newline;
431
432 /* Non-zero means print newline to message log before next message. */
433
434 static int message_log_need_newline;
435
436 /* Three markers that message_dolog uses.
437 It could allocate them itself, but that causes trouble
438 in handling memory-full errors. */
439 static Lisp_Object message_dolog_marker1;
440 static Lisp_Object message_dolog_marker2;
441 static Lisp_Object message_dolog_marker3;
442 \f
443 /* The buffer position of the first character appearing entirely or
444 partially on the line of the selected window which contains the
445 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
446 redisplay optimization in redisplay_internal. */
447
448 static struct text_pos this_line_start_pos;
449
450 /* Number of characters past the end of the line above, including the
451 terminating newline. */
452
453 static struct text_pos this_line_end_pos;
454
455 /* The vertical positions and the height of this line. */
456
457 static int this_line_vpos;
458 static int this_line_y;
459 static int this_line_pixel_height;
460
461 /* X position at which this display line starts. Usually zero;
462 negative if first character is partially visible. */
463
464 static int this_line_start_x;
465
466 /* The smallest character position seen by move_it_* functions as they
467 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
468 hscrolled lines, see display_line. */
469
470 static struct text_pos this_line_min_pos;
471
472 /* Buffer that this_line_.* variables are referring to. */
473
474 static struct buffer *this_line_buffer;
475
476
477 /* Values of those variables at last redisplay are stored as
478 properties on `overlay-arrow-position' symbol. However, if
479 Voverlay_arrow_position is a marker, last-arrow-position is its
480 numerical position. */
481
482 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
483
484 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
485 properties on a symbol in overlay-arrow-variable-list. */
486
487 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
488
489 Lisp_Object Qmenu_bar_update_hook;
490
491 /* Nonzero if an overlay arrow has been displayed in this window. */
492
493 static int overlay_arrow_seen;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
508
509 Lisp_Object echo_area_window;
510
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
515
516 static Lisp_Object Vmessage_stack;
517
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
520
521 static int message_enable_multibyte;
522
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
524
525 int update_mode_lines;
526
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
529
530 int windows_or_buffers_changed;
531
532 /* Nonzero means a frame's cursor type has been changed. */
533
534 int cursor_type_changed;
535
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
538
539 static int line_number_displayed;
540
541 /* The name of the *Messages* buffer, a string. */
542
543 static Lisp_Object Vmessages_buffer_name;
544
545 /* Current, index 0, and last displayed echo area message. Either
546 buffers from echo_buffers, or nil to indicate no message. */
547
548 Lisp_Object echo_area_buffer[2];
549
550 /* The buffers referenced from echo_area_buffer. */
551
552 static Lisp_Object echo_buffer[2];
553
554 /* A vector saved used in with_area_buffer to reduce consing. */
555
556 static Lisp_Object Vwith_echo_area_save_vector;
557
558 /* Non-zero means display_echo_area should display the last echo area
559 message again. Set by redisplay_preserve_echo_area. */
560
561 static int display_last_displayed_message_p;
562
563 /* Nonzero if echo area is being used by print; zero if being used by
564 message. */
565
566 static int message_buf_print;
567
568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
569
570 static Lisp_Object Qinhibit_menubar_update;
571 static Lisp_Object Qmessage_truncate_lines;
572
573 /* Set to 1 in clear_message to make redisplay_internal aware
574 of an emptied echo area. */
575
576 static int message_cleared_p;
577
578 /* A scratch glyph row with contents used for generating truncation
579 glyphs. Also used in direct_output_for_insert. */
580
581 #define MAX_SCRATCH_GLYPHS 100
582 static struct glyph_row scratch_glyph_row;
583 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
584
585 /* Ascent and height of the last line processed by move_it_to. */
586
587 static int last_max_ascent, last_height;
588
589 /* Non-zero if there's a help-echo in the echo area. */
590
591 int help_echo_showing_p;
592
593 /* If >= 0, computed, exact values of mode-line and header-line height
594 to use in the macros CURRENT_MODE_LINE_HEIGHT and
595 CURRENT_HEADER_LINE_HEIGHT. */
596
597 int current_mode_line_height, current_header_line_height;
598
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
604
605 #define TEXT_PROP_DISTANCE_LIMIT 100
606
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
621
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
629
630 #ifdef GLYPH_DEBUG
631
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG defined. */
634
635 int trace_redisplay_p;
636
637 #endif /* GLYPH_DEBUG */
638
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
642
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
647
648 static Lisp_Object Qauto_hscroll_mode;
649
650 /* Buffer being redisplayed -- for redisplay_window_error. */
651
652 static struct buffer *displayed_buffer;
653
654 /* Value returned from text property handlers (see below). */
655
656 enum prop_handled
657 {
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
662 };
663
664 /* A description of text properties that redisplay is interested
665 in. */
666
667 struct props
668 {
669 /* The name of the property. */
670 Lisp_Object *name;
671
672 /* A unique index for the property. */
673 enum prop_idx idx;
674
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) (struct it *it);
678 };
679
680 static enum prop_handled handle_face_prop (struct it *);
681 static enum prop_handled handle_invisible_prop (struct it *);
682 static enum prop_handled handle_display_prop (struct it *);
683 static enum prop_handled handle_composition_prop (struct it *);
684 static enum prop_handled handle_overlay_change (struct it *);
685 static enum prop_handled handle_fontified_prop (struct it *);
686
687 /* Properties handled by iterators. */
688
689 static struct props it_props[] =
690 {
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
698 {NULL, 0, NULL}
699 };
700
701 /* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
703
704 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
705
706 /* Enumeration returned by some move_it_.* functions internally. */
707
708 enum move_it_result
709 {
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
712
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
715
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
718
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
722
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
726
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
729 };
730
731 /* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
735
736 #define CLEAR_FACE_CACHE_COUNT 500
737 static int clear_face_cache_count;
738
739 /* Similarly for the image cache. */
740
741 #ifdef HAVE_WINDOW_SYSTEM
742 #define CLEAR_IMAGE_CACHE_COUNT 101
743 static int clear_image_cache_count;
744
745 /* Null glyph slice */
746 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
747 #endif
748
749 /* Non-zero while redisplay_internal is in progress. */
750
751 int redisplaying_p;
752
753 static Lisp_Object Qinhibit_free_realized_faces;
754 static Lisp_Object Qmode_line_default_help_echo;
755
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
758
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
763
764 /* Temporary variable for XTread_socket. */
765
766 Lisp_Object previous_help_echo_string;
767
768 /* Platform-independent portion of hourglass implementation. */
769
770 /* Non-zero means an hourglass cursor is currently shown. */
771 int hourglass_shown_p;
772
773 /* If non-null, an asynchronous timer that, when it expires, displays
774 an hourglass cursor on all frames. */
775 struct atimer *hourglass_atimer;
776
777 /* Name of the face used to display glyphless characters. */
778 Lisp_Object Qglyphless_char;
779
780 /* Symbol for the purpose of Vglyphless_char_display. */
781 static Lisp_Object Qglyphless_char_display;
782
783 /* Method symbols for Vglyphless_char_display. */
784 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
785
786 /* Default pixel width of `thin-space' display method. */
787 #define THIN_SPACE_WIDTH 1
788
789 /* Default number of seconds to wait before displaying an hourglass
790 cursor. */
791 #define DEFAULT_HOURGLASS_DELAY 1
792
793 \f
794 /* Function prototypes. */
795
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, int);
798 static void mark_window_display_accurate_1 (struct window *, int);
799 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
800 static int display_prop_string_p (Lisp_Object, Lisp_Object);
801 static int cursor_row_p (struct glyph_row *);
802 static int redisplay_mode_lines (Lisp_Object, int);
803 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
804
805 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
806
807 static void handle_line_prefix (struct it *);
808
809 static void pint2str (char *, int, ptrdiff_t);
810 static void pint2hrstr (char *, int, ptrdiff_t);
811 static struct text_pos run_window_scroll_functions (Lisp_Object,
812 struct text_pos);
813 static void reconsider_clip_changes (struct window *, struct buffer *);
814 static int text_outside_line_unchanged_p (struct window *,
815 ptrdiff_t, ptrdiff_t);
816 static void store_mode_line_noprop_char (char);
817 static int store_mode_line_noprop (const char *, int, int);
818 static void handle_stop (struct it *);
819 static void handle_stop_backwards (struct it *, ptrdiff_t);
820 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
821 static void ensure_echo_area_buffers (void);
822 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
823 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
824 static int with_echo_area_buffer (struct window *, int,
825 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
826 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
827 static void clear_garbaged_frames (void);
828 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
829 static void pop_message (void);
830 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
831 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
832 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
833 static int display_echo_area (struct window *);
834 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
835 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
836 static Lisp_Object unwind_redisplay (Lisp_Object);
837 static int string_char_and_length (const unsigned char *, int *);
838 static struct text_pos display_prop_end (struct it *, Lisp_Object,
839 struct text_pos);
840 static int compute_window_start_on_continuation_line (struct window *);
841 static Lisp_Object safe_eval_handler (Lisp_Object);
842 static void insert_left_trunc_glyphs (struct it *);
843 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
844 Lisp_Object);
845 static void extend_face_to_end_of_line (struct it *);
846 static int append_space_for_newline (struct it *, int);
847 static int cursor_row_fully_visible_p (struct window *, int, int);
848 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
849 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
850 static int trailing_whitespace_p (ptrdiff_t);
851 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
852 static void push_it (struct it *, struct text_pos *);
853 static void iterate_out_of_display_property (struct it *);
854 static void pop_it (struct it *);
855 static void sync_frame_with_window_matrix_rows (struct window *);
856 static void select_frame_for_redisplay (Lisp_Object);
857 static void redisplay_internal (void);
858 static int echo_area_display (int);
859 static void redisplay_windows (Lisp_Object);
860 static void redisplay_window (Lisp_Object, int);
861 static Lisp_Object redisplay_window_error (Lisp_Object);
862 static Lisp_Object redisplay_window_0 (Lisp_Object);
863 static Lisp_Object redisplay_window_1 (Lisp_Object);
864 static int set_cursor_from_row (struct window *, struct glyph_row *,
865 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
866 int, int);
867 static int update_menu_bar (struct frame *, int, int);
868 static int try_window_reusing_current_matrix (struct window *);
869 static int try_window_id (struct window *);
870 static int display_line (struct it *);
871 static int display_mode_lines (struct window *);
872 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
873 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
874 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
875 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
876 static void display_menu_bar (struct window *);
877 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
878 ptrdiff_t *);
879 static int display_string (const char *, Lisp_Object, Lisp_Object,
880 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
881 static void compute_line_metrics (struct it *);
882 static void run_redisplay_end_trigger_hook (struct it *);
883 static int get_overlay_strings (struct it *, ptrdiff_t);
884 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
885 static void next_overlay_string (struct it *);
886 static void reseat (struct it *, struct text_pos, int);
887 static void reseat_1 (struct it *, struct text_pos, int);
888 static void back_to_previous_visible_line_start (struct it *);
889 void reseat_at_previous_visible_line_start (struct it *);
890 static void reseat_at_next_visible_line_start (struct it *, int);
891 static int next_element_from_ellipsis (struct it *);
892 static int next_element_from_display_vector (struct it *);
893 static int next_element_from_string (struct it *);
894 static int next_element_from_c_string (struct it *);
895 static int next_element_from_buffer (struct it *);
896 static int next_element_from_composition (struct it *);
897 static int next_element_from_image (struct it *);
898 static int next_element_from_stretch (struct it *);
899 static void load_overlay_strings (struct it *, ptrdiff_t);
900 static int init_from_display_pos (struct it *, struct window *,
901 struct display_pos *);
902 static void reseat_to_string (struct it *, const char *,
903 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
904 static int get_next_display_element (struct it *);
905 static enum move_it_result
906 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
907 enum move_operation_enum);
908 void move_it_vertically_backward (struct it *, int);
909 static void init_to_row_start (struct it *, struct window *,
910 struct glyph_row *);
911 static int init_to_row_end (struct it *, struct window *,
912 struct glyph_row *);
913 static void back_to_previous_line_start (struct it *);
914 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
915 static struct text_pos string_pos_nchars_ahead (struct text_pos,
916 Lisp_Object, ptrdiff_t);
917 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
918 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
919 static ptrdiff_t number_of_chars (const char *, int);
920 static void compute_stop_pos (struct it *);
921 static void compute_string_pos (struct text_pos *, struct text_pos,
922 Lisp_Object);
923 static int face_before_or_after_it_pos (struct it *, int);
924 static ptrdiff_t next_overlay_change (ptrdiff_t);
925 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
926 Lisp_Object, struct text_pos *, ptrdiff_t, int);
927 static int handle_single_display_spec (struct it *, Lisp_Object,
928 Lisp_Object, Lisp_Object,
929 struct text_pos *, ptrdiff_t, int, int);
930 static int underlying_face_id (struct it *);
931 static int in_ellipses_for_invisible_text_p (struct display_pos *,
932 struct window *);
933
934 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
935 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
936
937 #ifdef HAVE_WINDOW_SYSTEM
938
939 static void x_consider_frame_title (Lisp_Object);
940 static int tool_bar_lines_needed (struct frame *, int *);
941 static void update_tool_bar (struct frame *, int);
942 static void build_desired_tool_bar_string (struct frame *f);
943 static int redisplay_tool_bar (struct frame *);
944 static void display_tool_bar_line (struct it *, int);
945 static void notice_overwritten_cursor (struct window *,
946 enum glyph_row_area,
947 int, int, int, int);
948 static void append_stretch_glyph (struct it *, Lisp_Object,
949 int, int, int);
950
951
952 #endif /* HAVE_WINDOW_SYSTEM */
953
954 static void produce_special_glyphs (struct it *, enum display_element_type);
955 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
956 static int coords_in_mouse_face_p (struct window *, int, int);
957
958
959 \f
960 /***********************************************************************
961 Window display dimensions
962 ***********************************************************************/
963
964 /* Return the bottom boundary y-position for text lines in window W.
965 This is the first y position at which a line cannot start.
966 It is relative to the top of the window.
967
968 This is the height of W minus the height of a mode line, if any. */
969
970 int
971 window_text_bottom_y (struct window *w)
972 {
973 int height = WINDOW_TOTAL_HEIGHT (w);
974
975 if (WINDOW_WANTS_MODELINE_P (w))
976 height -= CURRENT_MODE_LINE_HEIGHT (w);
977 return height;
978 }
979
980 /* Return the pixel width of display area AREA of window W. AREA < 0
981 means return the total width of W, not including fringes to
982 the left and right of the window. */
983
984 int
985 window_box_width (struct window *w, int area)
986 {
987 int cols = XFASTINT (w->total_cols);
988 int pixels = 0;
989
990 if (!w->pseudo_window_p)
991 {
992 cols -= WINDOW_SCROLL_BAR_COLS (w);
993
994 if (area == TEXT_AREA)
995 {
996 if (INTEGERP (w->left_margin_cols))
997 cols -= XFASTINT (w->left_margin_cols);
998 if (INTEGERP (w->right_margin_cols))
999 cols -= XFASTINT (w->right_margin_cols);
1000 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1001 }
1002 else if (area == LEFT_MARGIN_AREA)
1003 {
1004 cols = (INTEGERP (w->left_margin_cols)
1005 ? XFASTINT (w->left_margin_cols) : 0);
1006 pixels = 0;
1007 }
1008 else if (area == RIGHT_MARGIN_AREA)
1009 {
1010 cols = (INTEGERP (w->right_margin_cols)
1011 ? XFASTINT (w->right_margin_cols) : 0);
1012 pixels = 0;
1013 }
1014 }
1015
1016 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1017 }
1018
1019
1020 /* Return the pixel height of the display area of window W, not
1021 including mode lines of W, if any. */
1022
1023 int
1024 window_box_height (struct window *w)
1025 {
1026 struct frame *f = XFRAME (w->frame);
1027 int height = WINDOW_TOTAL_HEIGHT (w);
1028
1029 eassert (height >= 0);
1030
1031 /* Note: the code below that determines the mode-line/header-line
1032 height is essentially the same as that contained in the macro
1033 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1034 the appropriate glyph row has its `mode_line_p' flag set,
1035 and if it doesn't, uses estimate_mode_line_height instead. */
1036
1037 if (WINDOW_WANTS_MODELINE_P (w))
1038 {
1039 struct glyph_row *ml_row
1040 = (w->current_matrix && w->current_matrix->rows
1041 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1042 : 0);
1043 if (ml_row && ml_row->mode_line_p)
1044 height -= ml_row->height;
1045 else
1046 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1047 }
1048
1049 if (WINDOW_WANTS_HEADER_LINE_P (w))
1050 {
1051 struct glyph_row *hl_row
1052 = (w->current_matrix && w->current_matrix->rows
1053 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1054 : 0);
1055 if (hl_row && hl_row->mode_line_p)
1056 height -= hl_row->height;
1057 else
1058 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1059 }
1060
1061 /* With a very small font and a mode-line that's taller than
1062 default, we might end up with a negative height. */
1063 return max (0, height);
1064 }
1065
1066 /* Return the window-relative coordinate of the left edge of display
1067 area AREA of window W. AREA < 0 means return the left edge of the
1068 whole window, to the right of the left fringe of W. */
1069
1070 int
1071 window_box_left_offset (struct window *w, int area)
1072 {
1073 int x;
1074
1075 if (w->pseudo_window_p)
1076 return 0;
1077
1078 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1079
1080 if (area == TEXT_AREA)
1081 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1082 + window_box_width (w, LEFT_MARGIN_AREA));
1083 else if (area == RIGHT_MARGIN_AREA)
1084 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1085 + window_box_width (w, LEFT_MARGIN_AREA)
1086 + window_box_width (w, TEXT_AREA)
1087 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1088 ? 0
1089 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1090 else if (area == LEFT_MARGIN_AREA
1091 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1092 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1093
1094 return x;
1095 }
1096
1097
1098 /* Return the window-relative coordinate of the right edge of display
1099 area AREA of window W. AREA < 0 means return the right edge of the
1100 whole window, to the left of the right fringe of W. */
1101
1102 int
1103 window_box_right_offset (struct window *w, int area)
1104 {
1105 return window_box_left_offset (w, area) + window_box_width (w, area);
1106 }
1107
1108 /* Return the frame-relative coordinate of the left edge of display
1109 area AREA of window W. AREA < 0 means return the left edge of the
1110 whole window, to the right of the left fringe of W. */
1111
1112 int
1113 window_box_left (struct window *w, int area)
1114 {
1115 struct frame *f = XFRAME (w->frame);
1116 int x;
1117
1118 if (w->pseudo_window_p)
1119 return FRAME_INTERNAL_BORDER_WIDTH (f);
1120
1121 x = (WINDOW_LEFT_EDGE_X (w)
1122 + window_box_left_offset (w, area));
1123
1124 return x;
1125 }
1126
1127
1128 /* Return the frame-relative coordinate of the right edge of display
1129 area AREA of window W. AREA < 0 means return the right edge of the
1130 whole window, to the left of the right fringe of W. */
1131
1132 int
1133 window_box_right (struct window *w, int area)
1134 {
1135 return window_box_left (w, area) + window_box_width (w, area);
1136 }
1137
1138 /* Get the bounding box of the display area AREA of window W, without
1139 mode lines, in frame-relative coordinates. AREA < 0 means the
1140 whole window, not including the left and right fringes of
1141 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1142 coordinates of the upper-left corner of the box. Return in
1143 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1144
1145 void
1146 window_box (struct window *w, int area, int *box_x, int *box_y,
1147 int *box_width, int *box_height)
1148 {
1149 if (box_width)
1150 *box_width = window_box_width (w, area);
1151 if (box_height)
1152 *box_height = window_box_height (w);
1153 if (box_x)
1154 *box_x = window_box_left (w, area);
1155 if (box_y)
1156 {
1157 *box_y = WINDOW_TOP_EDGE_Y (w);
1158 if (WINDOW_WANTS_HEADER_LINE_P (w))
1159 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1160 }
1161 }
1162
1163
1164 /* Get the bounding box of the display area AREA of window W, without
1165 mode lines. AREA < 0 means the whole window, not including the
1166 left and right fringe of the window. Return in *TOP_LEFT_X
1167 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1168 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1169 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1170 box. */
1171
1172 static inline void
1173 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1174 int *bottom_right_x, int *bottom_right_y)
1175 {
1176 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1177 bottom_right_y);
1178 *bottom_right_x += *top_left_x;
1179 *bottom_right_y += *top_left_y;
1180 }
1181
1182
1183 \f
1184 /***********************************************************************
1185 Utilities
1186 ***********************************************************************/
1187
1188 /* Return the bottom y-position of the line the iterator IT is in.
1189 This can modify IT's settings. */
1190
1191 int
1192 line_bottom_y (struct it *it)
1193 {
1194 int line_height = it->max_ascent + it->max_descent;
1195 int line_top_y = it->current_y;
1196
1197 if (line_height == 0)
1198 {
1199 if (last_height)
1200 line_height = last_height;
1201 else if (IT_CHARPOS (*it) < ZV)
1202 {
1203 move_it_by_lines (it, 1);
1204 line_height = (it->max_ascent || it->max_descent
1205 ? it->max_ascent + it->max_descent
1206 : last_height);
1207 }
1208 else
1209 {
1210 struct glyph_row *row = it->glyph_row;
1211
1212 /* Use the default character height. */
1213 it->glyph_row = NULL;
1214 it->what = IT_CHARACTER;
1215 it->c = ' ';
1216 it->len = 1;
1217 PRODUCE_GLYPHS (it);
1218 line_height = it->ascent + it->descent;
1219 it->glyph_row = row;
1220 }
1221 }
1222
1223 return line_top_y + line_height;
1224 }
1225
1226 /* Subroutine of pos_visible_p below. Extracts a display string, if
1227 any, from the display spec given as its argument. */
1228 static Lisp_Object
1229 string_from_display_spec (Lisp_Object spec)
1230 {
1231 if (CONSP (spec))
1232 {
1233 while (CONSP (spec))
1234 {
1235 if (STRINGP (XCAR (spec)))
1236 return XCAR (spec);
1237 spec = XCDR (spec);
1238 }
1239 }
1240 else if (VECTORP (spec))
1241 {
1242 ptrdiff_t i;
1243
1244 for (i = 0; i < ASIZE (spec); i++)
1245 {
1246 if (STRINGP (AREF (spec, i)))
1247 return AREF (spec, i);
1248 }
1249 return Qnil;
1250 }
1251
1252 return spec;
1253 }
1254
1255
1256 /* Limit insanely large values of W->hscroll on frame F to the largest
1257 value that will still prevent first_visible_x and last_visible_x of
1258 'struct it' from overflowing an int. */
1259 static inline int
1260 window_hscroll_limited (struct window *w, struct frame *f)
1261 {
1262 ptrdiff_t window_hscroll = w->hscroll;
1263 int window_text_width = window_box_width (w, TEXT_AREA);
1264 int colwidth = FRAME_COLUMN_WIDTH (f);
1265
1266 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1267 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1268
1269 return window_hscroll;
1270 }
1271
1272 /* Return 1 if position CHARPOS is visible in window W.
1273 CHARPOS < 0 means return info about WINDOW_END position.
1274 If visible, set *X and *Y to pixel coordinates of top left corner.
1275 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1276 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1277
1278 int
1279 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1280 int *rtop, int *rbot, int *rowh, int *vpos)
1281 {
1282 struct it it;
1283 void *itdata = bidi_shelve_cache ();
1284 struct text_pos top;
1285 int visible_p = 0;
1286 struct buffer *old_buffer = NULL;
1287
1288 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1289 return visible_p;
1290
1291 if (XBUFFER (w->buffer) != current_buffer)
1292 {
1293 old_buffer = current_buffer;
1294 set_buffer_internal_1 (XBUFFER (w->buffer));
1295 }
1296
1297 SET_TEXT_POS_FROM_MARKER (top, w->start);
1298 /* Scrolling a minibuffer window via scroll bar when the echo area
1299 shows long text sometimes resets the minibuffer contents behind
1300 our backs. */
1301 if (CHARPOS (top) > ZV)
1302 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1303
1304 /* Compute exact mode line heights. */
1305 if (WINDOW_WANTS_MODELINE_P (w))
1306 current_mode_line_height
1307 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1308 BVAR (current_buffer, mode_line_format));
1309
1310 if (WINDOW_WANTS_HEADER_LINE_P (w))
1311 current_header_line_height
1312 = display_mode_line (w, HEADER_LINE_FACE_ID,
1313 BVAR (current_buffer, header_line_format));
1314
1315 start_display (&it, w, top);
1316 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1317 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1318
1319 if (charpos >= 0
1320 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1321 && IT_CHARPOS (it) >= charpos)
1322 /* When scanning backwards under bidi iteration, move_it_to
1323 stops at or _before_ CHARPOS, because it stops at or to
1324 the _right_ of the character at CHARPOS. */
1325 || (it.bidi_p && it.bidi_it.scan_dir == -1
1326 && IT_CHARPOS (it) <= charpos)))
1327 {
1328 /* We have reached CHARPOS, or passed it. How the call to
1329 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1330 or covered by a display property, move_it_to stops at the end
1331 of the invisible text, to the right of CHARPOS. (ii) If
1332 CHARPOS is in a display vector, move_it_to stops on its last
1333 glyph. */
1334 int top_x = it.current_x;
1335 int top_y = it.current_y;
1336 /* Calling line_bottom_y may change it.method, it.position, etc. */
1337 enum it_method it_method = it.method;
1338 int bottom_y = (last_height = 0, line_bottom_y (&it));
1339 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1340
1341 if (top_y < window_top_y)
1342 visible_p = bottom_y > window_top_y;
1343 else if (top_y < it.last_visible_y)
1344 visible_p = 1;
1345 if (bottom_y >= it.last_visible_y
1346 && it.bidi_p && it.bidi_it.scan_dir == -1
1347 && IT_CHARPOS (it) < charpos)
1348 {
1349 /* When the last line of the window is scanned backwards
1350 under bidi iteration, we could be duped into thinking
1351 that we have passed CHARPOS, when in fact move_it_to
1352 simply stopped short of CHARPOS because it reached
1353 last_visible_y. To see if that's what happened, we call
1354 move_it_to again with a slightly larger vertical limit,
1355 and see if it actually moved vertically; if it did, we
1356 didn't really reach CHARPOS, which is beyond window end. */
1357 struct it save_it = it;
1358 /* Why 10? because we don't know how many canonical lines
1359 will the height of the next line(s) be. So we guess. */
1360 int ten_more_lines =
1361 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1362
1363 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1364 MOVE_TO_POS | MOVE_TO_Y);
1365 if (it.current_y > top_y)
1366 visible_p = 0;
1367
1368 it = save_it;
1369 }
1370 if (visible_p)
1371 {
1372 if (it_method == GET_FROM_DISPLAY_VECTOR)
1373 {
1374 /* We stopped on the last glyph of a display vector.
1375 Try and recompute. Hack alert! */
1376 if (charpos < 2 || top.charpos >= charpos)
1377 top_x = it.glyph_row->x;
1378 else
1379 {
1380 struct it it2;
1381 start_display (&it2, w, top);
1382 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1383 get_next_display_element (&it2);
1384 PRODUCE_GLYPHS (&it2);
1385 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1386 || it2.current_x > it2.last_visible_x)
1387 top_x = it.glyph_row->x;
1388 else
1389 {
1390 top_x = it2.current_x;
1391 top_y = it2.current_y;
1392 }
1393 }
1394 }
1395 else if (IT_CHARPOS (it) != charpos)
1396 {
1397 Lisp_Object cpos = make_number (charpos);
1398 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1399 Lisp_Object string = string_from_display_spec (spec);
1400 int newline_in_string = 0;
1401
1402 if (STRINGP (string))
1403 {
1404 const char *s = SSDATA (string);
1405 const char *e = s + SBYTES (string);
1406 while (s < e)
1407 {
1408 if (*s++ == '\n')
1409 {
1410 newline_in_string = 1;
1411 break;
1412 }
1413 }
1414 }
1415 /* The tricky code below is needed because there's a
1416 discrepancy between move_it_to and how we set cursor
1417 when the display line ends in a newline from a
1418 display string. move_it_to will stop _after_ such
1419 display strings, whereas set_cursor_from_row
1420 conspires with cursor_row_p to place the cursor on
1421 the first glyph produced from the display string. */
1422
1423 /* We have overshoot PT because it is covered by a
1424 display property whose value is a string. If the
1425 string includes embedded newlines, we are also in the
1426 wrong display line. Backtrack to the correct line,
1427 where the display string begins. */
1428 if (newline_in_string)
1429 {
1430 Lisp_Object startpos, endpos;
1431 EMACS_INT start, end;
1432 struct it it3;
1433 int it3_moved;
1434
1435 /* Find the first and the last buffer positions
1436 covered by the display string. */
1437 endpos =
1438 Fnext_single_char_property_change (cpos, Qdisplay,
1439 Qnil, Qnil);
1440 startpos =
1441 Fprevious_single_char_property_change (endpos, Qdisplay,
1442 Qnil, Qnil);
1443 start = XFASTINT (startpos);
1444 end = XFASTINT (endpos);
1445 /* Move to the last buffer position before the
1446 display property. */
1447 start_display (&it3, w, top);
1448 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1449 /* Move forward one more line if the position before
1450 the display string is a newline or if it is the
1451 rightmost character on a line that is
1452 continued or word-wrapped. */
1453 if (it3.method == GET_FROM_BUFFER
1454 && it3.c == '\n')
1455 move_it_by_lines (&it3, 1);
1456 else if (move_it_in_display_line_to (&it3, -1,
1457 it3.current_x
1458 + it3.pixel_width,
1459 MOVE_TO_X)
1460 == MOVE_LINE_CONTINUED)
1461 {
1462 move_it_by_lines (&it3, 1);
1463 /* When we are under word-wrap, the #$@%!
1464 move_it_by_lines moves 2 lines, so we need to
1465 fix that up. */
1466 if (it3.line_wrap == WORD_WRAP)
1467 move_it_by_lines (&it3, -1);
1468 }
1469
1470 /* Record the vertical coordinate of the display
1471 line where we wound up. */
1472 top_y = it3.current_y;
1473 if (it3.bidi_p)
1474 {
1475 /* When characters are reordered for display,
1476 the character displayed to the left of the
1477 display string could be _after_ the display
1478 property in the logical order. Use the
1479 smallest vertical position of these two. */
1480 start_display (&it3, w, top);
1481 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1482 if (it3.current_y < top_y)
1483 top_y = it3.current_y;
1484 }
1485 /* Move from the top of the window to the beginning
1486 of the display line where the display string
1487 begins. */
1488 start_display (&it3, w, top);
1489 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1490 /* If it3_moved stays zero after the 'while' loop
1491 below, that means we already were at a newline
1492 before the loop (e.g., the display string begins
1493 with a newline), so we don't need to (and cannot)
1494 inspect the glyphs of it3.glyph_row, because
1495 PRODUCE_GLYPHS will not produce anything for a
1496 newline, and thus it3.glyph_row stays at its
1497 stale content it got at top of the window. */
1498 it3_moved = 0;
1499 /* Finally, advance the iterator until we hit the
1500 first display element whose character position is
1501 CHARPOS, or until the first newline from the
1502 display string, which signals the end of the
1503 display line. */
1504 while (get_next_display_element (&it3))
1505 {
1506 PRODUCE_GLYPHS (&it3);
1507 if (IT_CHARPOS (it3) == charpos
1508 || ITERATOR_AT_END_OF_LINE_P (&it3))
1509 break;
1510 it3_moved = 1;
1511 set_iterator_to_next (&it3, 0);
1512 }
1513 top_x = it3.current_x - it3.pixel_width;
1514 /* Normally, we would exit the above loop because we
1515 found the display element whose character
1516 position is CHARPOS. For the contingency that we
1517 didn't, and stopped at the first newline from the
1518 display string, move back over the glyphs
1519 produced from the string, until we find the
1520 rightmost glyph not from the string. */
1521 if (it3_moved
1522 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1523 {
1524 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1525 + it3.glyph_row->used[TEXT_AREA];
1526
1527 while (EQ ((g - 1)->object, string))
1528 {
1529 --g;
1530 top_x -= g->pixel_width;
1531 }
1532 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1533 + it3.glyph_row->used[TEXT_AREA]);
1534 }
1535 }
1536 }
1537
1538 *x = top_x;
1539 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1540 *rtop = max (0, window_top_y - top_y);
1541 *rbot = max (0, bottom_y - it.last_visible_y);
1542 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1543 - max (top_y, window_top_y)));
1544 *vpos = it.vpos;
1545 }
1546 }
1547 else
1548 {
1549 /* We were asked to provide info about WINDOW_END. */
1550 struct it it2;
1551 void *it2data = NULL;
1552
1553 SAVE_IT (it2, it, it2data);
1554 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1555 move_it_by_lines (&it, 1);
1556 if (charpos < IT_CHARPOS (it)
1557 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1558 {
1559 visible_p = 1;
1560 RESTORE_IT (&it2, &it2, it2data);
1561 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1562 *x = it2.current_x;
1563 *y = it2.current_y + it2.max_ascent - it2.ascent;
1564 *rtop = max (0, -it2.current_y);
1565 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1566 - it.last_visible_y));
1567 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1568 it.last_visible_y)
1569 - max (it2.current_y,
1570 WINDOW_HEADER_LINE_HEIGHT (w))));
1571 *vpos = it2.vpos;
1572 }
1573 else
1574 bidi_unshelve_cache (it2data, 1);
1575 }
1576 bidi_unshelve_cache (itdata, 0);
1577
1578 if (old_buffer)
1579 set_buffer_internal_1 (old_buffer);
1580
1581 current_header_line_height = current_mode_line_height = -1;
1582
1583 if (visible_p && w->hscroll > 0)
1584 *x -=
1585 window_hscroll_limited (w, WINDOW_XFRAME (w))
1586 * WINDOW_FRAME_COLUMN_WIDTH (w);
1587
1588 #if 0
1589 /* Debugging code. */
1590 if (visible_p)
1591 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1592 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1593 else
1594 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1595 #endif
1596
1597 return visible_p;
1598 }
1599
1600
1601 /* Return the next character from STR. Return in *LEN the length of
1602 the character. This is like STRING_CHAR_AND_LENGTH but never
1603 returns an invalid character. If we find one, we return a `?', but
1604 with the length of the invalid character. */
1605
1606 static inline int
1607 string_char_and_length (const unsigned char *str, int *len)
1608 {
1609 int c;
1610
1611 c = STRING_CHAR_AND_LENGTH (str, *len);
1612 if (!CHAR_VALID_P (c))
1613 /* We may not change the length here because other places in Emacs
1614 don't use this function, i.e. they silently accept invalid
1615 characters. */
1616 c = '?';
1617
1618 return c;
1619 }
1620
1621
1622
1623 /* Given a position POS containing a valid character and byte position
1624 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1625
1626 static struct text_pos
1627 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1628 {
1629 eassert (STRINGP (string) && nchars >= 0);
1630
1631 if (STRING_MULTIBYTE (string))
1632 {
1633 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1634 int len;
1635
1636 while (nchars--)
1637 {
1638 string_char_and_length (p, &len);
1639 p += len;
1640 CHARPOS (pos) += 1;
1641 BYTEPOS (pos) += len;
1642 }
1643 }
1644 else
1645 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1646
1647 return pos;
1648 }
1649
1650
1651 /* Value is the text position, i.e. character and byte position,
1652 for character position CHARPOS in STRING. */
1653
1654 static inline struct text_pos
1655 string_pos (ptrdiff_t charpos, Lisp_Object string)
1656 {
1657 struct text_pos pos;
1658 eassert (STRINGP (string));
1659 eassert (charpos >= 0);
1660 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1661 return pos;
1662 }
1663
1664
1665 /* Value is a text position, i.e. character and byte position, for
1666 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1667 means recognize multibyte characters. */
1668
1669 static struct text_pos
1670 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1671 {
1672 struct text_pos pos;
1673
1674 eassert (s != NULL);
1675 eassert (charpos >= 0);
1676
1677 if (multibyte_p)
1678 {
1679 int len;
1680
1681 SET_TEXT_POS (pos, 0, 0);
1682 while (charpos--)
1683 {
1684 string_char_and_length ((const unsigned char *) s, &len);
1685 s += len;
1686 CHARPOS (pos) += 1;
1687 BYTEPOS (pos) += len;
1688 }
1689 }
1690 else
1691 SET_TEXT_POS (pos, charpos, charpos);
1692
1693 return pos;
1694 }
1695
1696
1697 /* Value is the number of characters in C string S. MULTIBYTE_P
1698 non-zero means recognize multibyte characters. */
1699
1700 static ptrdiff_t
1701 number_of_chars (const char *s, int multibyte_p)
1702 {
1703 ptrdiff_t nchars;
1704
1705 if (multibyte_p)
1706 {
1707 ptrdiff_t rest = strlen (s);
1708 int len;
1709 const unsigned char *p = (const unsigned char *) s;
1710
1711 for (nchars = 0; rest > 0; ++nchars)
1712 {
1713 string_char_and_length (p, &len);
1714 rest -= len, p += len;
1715 }
1716 }
1717 else
1718 nchars = strlen (s);
1719
1720 return nchars;
1721 }
1722
1723
1724 /* Compute byte position NEWPOS->bytepos corresponding to
1725 NEWPOS->charpos. POS is a known position in string STRING.
1726 NEWPOS->charpos must be >= POS.charpos. */
1727
1728 static void
1729 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1730 {
1731 eassert (STRINGP (string));
1732 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1733
1734 if (STRING_MULTIBYTE (string))
1735 *newpos = string_pos_nchars_ahead (pos, string,
1736 CHARPOS (*newpos) - CHARPOS (pos));
1737 else
1738 BYTEPOS (*newpos) = CHARPOS (*newpos);
1739 }
1740
1741 /* EXPORT:
1742 Return an estimation of the pixel height of mode or header lines on
1743 frame F. FACE_ID specifies what line's height to estimate. */
1744
1745 int
1746 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1747 {
1748 #ifdef HAVE_WINDOW_SYSTEM
1749 if (FRAME_WINDOW_P (f))
1750 {
1751 int height = FONT_HEIGHT (FRAME_FONT (f));
1752
1753 /* This function is called so early when Emacs starts that the face
1754 cache and mode line face are not yet initialized. */
1755 if (FRAME_FACE_CACHE (f))
1756 {
1757 struct face *face = FACE_FROM_ID (f, face_id);
1758 if (face)
1759 {
1760 if (face->font)
1761 height = FONT_HEIGHT (face->font);
1762 if (face->box_line_width > 0)
1763 height += 2 * face->box_line_width;
1764 }
1765 }
1766
1767 return height;
1768 }
1769 #endif
1770
1771 return 1;
1772 }
1773
1774 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1775 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1776 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1777 not force the value into range. */
1778
1779 void
1780 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1781 int *x, int *y, NativeRectangle *bounds, int noclip)
1782 {
1783
1784 #ifdef HAVE_WINDOW_SYSTEM
1785 if (FRAME_WINDOW_P (f))
1786 {
1787 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1788 even for negative values. */
1789 if (pix_x < 0)
1790 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1791 if (pix_y < 0)
1792 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1793
1794 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1795 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1796
1797 if (bounds)
1798 STORE_NATIVE_RECT (*bounds,
1799 FRAME_COL_TO_PIXEL_X (f, pix_x),
1800 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1801 FRAME_COLUMN_WIDTH (f) - 1,
1802 FRAME_LINE_HEIGHT (f) - 1);
1803
1804 if (!noclip)
1805 {
1806 if (pix_x < 0)
1807 pix_x = 0;
1808 else if (pix_x > FRAME_TOTAL_COLS (f))
1809 pix_x = FRAME_TOTAL_COLS (f);
1810
1811 if (pix_y < 0)
1812 pix_y = 0;
1813 else if (pix_y > FRAME_LINES (f))
1814 pix_y = FRAME_LINES (f);
1815 }
1816 }
1817 #endif
1818
1819 *x = pix_x;
1820 *y = pix_y;
1821 }
1822
1823
1824 /* Find the glyph under window-relative coordinates X/Y in window W.
1825 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1826 strings. Return in *HPOS and *VPOS the row and column number of
1827 the glyph found. Return in *AREA the glyph area containing X.
1828 Value is a pointer to the glyph found or null if X/Y is not on
1829 text, or we can't tell because W's current matrix is not up to
1830 date. */
1831
1832 static
1833 struct glyph *
1834 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1835 int *dx, int *dy, int *area)
1836 {
1837 struct glyph *glyph, *end;
1838 struct glyph_row *row = NULL;
1839 int x0, i;
1840
1841 /* Find row containing Y. Give up if some row is not enabled. */
1842 for (i = 0; i < w->current_matrix->nrows; ++i)
1843 {
1844 row = MATRIX_ROW (w->current_matrix, i);
1845 if (!row->enabled_p)
1846 return NULL;
1847 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1848 break;
1849 }
1850
1851 *vpos = i;
1852 *hpos = 0;
1853
1854 /* Give up if Y is not in the window. */
1855 if (i == w->current_matrix->nrows)
1856 return NULL;
1857
1858 /* Get the glyph area containing X. */
1859 if (w->pseudo_window_p)
1860 {
1861 *area = TEXT_AREA;
1862 x0 = 0;
1863 }
1864 else
1865 {
1866 if (x < window_box_left_offset (w, TEXT_AREA))
1867 {
1868 *area = LEFT_MARGIN_AREA;
1869 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1870 }
1871 else if (x < window_box_right_offset (w, TEXT_AREA))
1872 {
1873 *area = TEXT_AREA;
1874 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1875 }
1876 else
1877 {
1878 *area = RIGHT_MARGIN_AREA;
1879 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1880 }
1881 }
1882
1883 /* Find glyph containing X. */
1884 glyph = row->glyphs[*area];
1885 end = glyph + row->used[*area];
1886 x -= x0;
1887 while (glyph < end && x >= glyph->pixel_width)
1888 {
1889 x -= glyph->pixel_width;
1890 ++glyph;
1891 }
1892
1893 if (glyph == end)
1894 return NULL;
1895
1896 if (dx)
1897 {
1898 *dx = x;
1899 *dy = y - (row->y + row->ascent - glyph->ascent);
1900 }
1901
1902 *hpos = glyph - row->glyphs[*area];
1903 return glyph;
1904 }
1905
1906 /* Convert frame-relative x/y to coordinates relative to window W.
1907 Takes pseudo-windows into account. */
1908
1909 static void
1910 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1911 {
1912 if (w->pseudo_window_p)
1913 {
1914 /* A pseudo-window is always full-width, and starts at the
1915 left edge of the frame, plus a frame border. */
1916 struct frame *f = XFRAME (w->frame);
1917 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1918 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1919 }
1920 else
1921 {
1922 *x -= WINDOW_LEFT_EDGE_X (w);
1923 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1924 }
1925 }
1926
1927 #ifdef HAVE_WINDOW_SYSTEM
1928
1929 /* EXPORT:
1930 Return in RECTS[] at most N clipping rectangles for glyph string S.
1931 Return the number of stored rectangles. */
1932
1933 int
1934 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1935 {
1936 XRectangle r;
1937
1938 if (n <= 0)
1939 return 0;
1940
1941 if (s->row->full_width_p)
1942 {
1943 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1944 r.x = WINDOW_LEFT_EDGE_X (s->w);
1945 r.width = WINDOW_TOTAL_WIDTH (s->w);
1946
1947 /* Unless displaying a mode or menu bar line, which are always
1948 fully visible, clip to the visible part of the row. */
1949 if (s->w->pseudo_window_p)
1950 r.height = s->row->visible_height;
1951 else
1952 r.height = s->height;
1953 }
1954 else
1955 {
1956 /* This is a text line that may be partially visible. */
1957 r.x = window_box_left (s->w, s->area);
1958 r.width = window_box_width (s->w, s->area);
1959 r.height = s->row->visible_height;
1960 }
1961
1962 if (s->clip_head)
1963 if (r.x < s->clip_head->x)
1964 {
1965 if (r.width >= s->clip_head->x - r.x)
1966 r.width -= s->clip_head->x - r.x;
1967 else
1968 r.width = 0;
1969 r.x = s->clip_head->x;
1970 }
1971 if (s->clip_tail)
1972 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1973 {
1974 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1975 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1976 else
1977 r.width = 0;
1978 }
1979
1980 /* If S draws overlapping rows, it's sufficient to use the top and
1981 bottom of the window for clipping because this glyph string
1982 intentionally draws over other lines. */
1983 if (s->for_overlaps)
1984 {
1985 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1986 r.height = window_text_bottom_y (s->w) - r.y;
1987
1988 /* Alas, the above simple strategy does not work for the
1989 environments with anti-aliased text: if the same text is
1990 drawn onto the same place multiple times, it gets thicker.
1991 If the overlap we are processing is for the erased cursor, we
1992 take the intersection with the rectangle of the cursor. */
1993 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1994 {
1995 XRectangle rc, r_save = r;
1996
1997 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1998 rc.y = s->w->phys_cursor.y;
1999 rc.width = s->w->phys_cursor_width;
2000 rc.height = s->w->phys_cursor_height;
2001
2002 x_intersect_rectangles (&r_save, &rc, &r);
2003 }
2004 }
2005 else
2006 {
2007 /* Don't use S->y for clipping because it doesn't take partially
2008 visible lines into account. For example, it can be negative for
2009 partially visible lines at the top of a window. */
2010 if (!s->row->full_width_p
2011 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2012 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2013 else
2014 r.y = max (0, s->row->y);
2015 }
2016
2017 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2018
2019 /* If drawing the cursor, don't let glyph draw outside its
2020 advertised boundaries. Cleartype does this under some circumstances. */
2021 if (s->hl == DRAW_CURSOR)
2022 {
2023 struct glyph *glyph = s->first_glyph;
2024 int height, max_y;
2025
2026 if (s->x > r.x)
2027 {
2028 r.width -= s->x - r.x;
2029 r.x = s->x;
2030 }
2031 r.width = min (r.width, glyph->pixel_width);
2032
2033 /* If r.y is below window bottom, ensure that we still see a cursor. */
2034 height = min (glyph->ascent + glyph->descent,
2035 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2036 max_y = window_text_bottom_y (s->w) - height;
2037 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2038 if (s->ybase - glyph->ascent > max_y)
2039 {
2040 r.y = max_y;
2041 r.height = height;
2042 }
2043 else
2044 {
2045 /* Don't draw cursor glyph taller than our actual glyph. */
2046 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2047 if (height < r.height)
2048 {
2049 max_y = r.y + r.height;
2050 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2051 r.height = min (max_y - r.y, height);
2052 }
2053 }
2054 }
2055
2056 if (s->row->clip)
2057 {
2058 XRectangle r_save = r;
2059
2060 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2061 r.width = 0;
2062 }
2063
2064 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2065 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2066 {
2067 #ifdef CONVERT_FROM_XRECT
2068 CONVERT_FROM_XRECT (r, *rects);
2069 #else
2070 *rects = r;
2071 #endif
2072 return 1;
2073 }
2074 else
2075 {
2076 /* If we are processing overlapping and allowed to return
2077 multiple clipping rectangles, we exclude the row of the glyph
2078 string from the clipping rectangle. This is to avoid drawing
2079 the same text on the environment with anti-aliasing. */
2080 #ifdef CONVERT_FROM_XRECT
2081 XRectangle rs[2];
2082 #else
2083 XRectangle *rs = rects;
2084 #endif
2085 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2086
2087 if (s->for_overlaps & OVERLAPS_PRED)
2088 {
2089 rs[i] = r;
2090 if (r.y + r.height > row_y)
2091 {
2092 if (r.y < row_y)
2093 rs[i].height = row_y - r.y;
2094 else
2095 rs[i].height = 0;
2096 }
2097 i++;
2098 }
2099 if (s->for_overlaps & OVERLAPS_SUCC)
2100 {
2101 rs[i] = r;
2102 if (r.y < row_y + s->row->visible_height)
2103 {
2104 if (r.y + r.height > row_y + s->row->visible_height)
2105 {
2106 rs[i].y = row_y + s->row->visible_height;
2107 rs[i].height = r.y + r.height - rs[i].y;
2108 }
2109 else
2110 rs[i].height = 0;
2111 }
2112 i++;
2113 }
2114
2115 n = i;
2116 #ifdef CONVERT_FROM_XRECT
2117 for (i = 0; i < n; i++)
2118 CONVERT_FROM_XRECT (rs[i], rects[i]);
2119 #endif
2120 return n;
2121 }
2122 }
2123
2124 /* EXPORT:
2125 Return in *NR the clipping rectangle for glyph string S. */
2126
2127 void
2128 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2129 {
2130 get_glyph_string_clip_rects (s, nr, 1);
2131 }
2132
2133
2134 /* EXPORT:
2135 Return the position and height of the phys cursor in window W.
2136 Set w->phys_cursor_width to width of phys cursor.
2137 */
2138
2139 void
2140 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2141 struct glyph *glyph, int *xp, int *yp, int *heightp)
2142 {
2143 struct frame *f = XFRAME (WINDOW_FRAME (w));
2144 int x, y, wd, h, h0, y0;
2145
2146 /* Compute the width of the rectangle to draw. If on a stretch
2147 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2148 rectangle as wide as the glyph, but use a canonical character
2149 width instead. */
2150 wd = glyph->pixel_width - 1;
2151 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2152 wd++; /* Why? */
2153 #endif
2154
2155 x = w->phys_cursor.x;
2156 if (x < 0)
2157 {
2158 wd += x;
2159 x = 0;
2160 }
2161
2162 if (glyph->type == STRETCH_GLYPH
2163 && !x_stretch_cursor_p)
2164 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2165 w->phys_cursor_width = wd;
2166
2167 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2168
2169 /* If y is below window bottom, ensure that we still see a cursor. */
2170 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2171
2172 h = max (h0, glyph->ascent + glyph->descent);
2173 h0 = min (h0, glyph->ascent + glyph->descent);
2174
2175 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2176 if (y < y0)
2177 {
2178 h = max (h - (y0 - y) + 1, h0);
2179 y = y0 - 1;
2180 }
2181 else
2182 {
2183 y0 = window_text_bottom_y (w) - h0;
2184 if (y > y0)
2185 {
2186 h += y - y0;
2187 y = y0;
2188 }
2189 }
2190
2191 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2192 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2193 *heightp = h;
2194 }
2195
2196 /*
2197 * Remember which glyph the mouse is over.
2198 */
2199
2200 void
2201 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2202 {
2203 Lisp_Object window;
2204 struct window *w;
2205 struct glyph_row *r, *gr, *end_row;
2206 enum window_part part;
2207 enum glyph_row_area area;
2208 int x, y, width, height;
2209
2210 /* Try to determine frame pixel position and size of the glyph under
2211 frame pixel coordinates X/Y on frame F. */
2212
2213 if (!f->glyphs_initialized_p
2214 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2215 NILP (window)))
2216 {
2217 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2218 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2219 goto virtual_glyph;
2220 }
2221
2222 w = XWINDOW (window);
2223 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2224 height = WINDOW_FRAME_LINE_HEIGHT (w);
2225
2226 x = window_relative_x_coord (w, part, gx);
2227 y = gy - WINDOW_TOP_EDGE_Y (w);
2228
2229 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2230 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2231
2232 if (w->pseudo_window_p)
2233 {
2234 area = TEXT_AREA;
2235 part = ON_MODE_LINE; /* Don't adjust margin. */
2236 goto text_glyph;
2237 }
2238
2239 switch (part)
2240 {
2241 case ON_LEFT_MARGIN:
2242 area = LEFT_MARGIN_AREA;
2243 goto text_glyph;
2244
2245 case ON_RIGHT_MARGIN:
2246 area = RIGHT_MARGIN_AREA;
2247 goto text_glyph;
2248
2249 case ON_HEADER_LINE:
2250 case ON_MODE_LINE:
2251 gr = (part == ON_HEADER_LINE
2252 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2253 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2254 gy = gr->y;
2255 area = TEXT_AREA;
2256 goto text_glyph_row_found;
2257
2258 case ON_TEXT:
2259 area = TEXT_AREA;
2260
2261 text_glyph:
2262 gr = 0; gy = 0;
2263 for (; r <= end_row && r->enabled_p; ++r)
2264 if (r->y + r->height > y)
2265 {
2266 gr = r; gy = r->y;
2267 break;
2268 }
2269
2270 text_glyph_row_found:
2271 if (gr && gy <= y)
2272 {
2273 struct glyph *g = gr->glyphs[area];
2274 struct glyph *end = g + gr->used[area];
2275
2276 height = gr->height;
2277 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2278 if (gx + g->pixel_width > x)
2279 break;
2280
2281 if (g < end)
2282 {
2283 if (g->type == IMAGE_GLYPH)
2284 {
2285 /* Don't remember when mouse is over image, as
2286 image may have hot-spots. */
2287 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2288 return;
2289 }
2290 width = g->pixel_width;
2291 }
2292 else
2293 {
2294 /* Use nominal char spacing at end of line. */
2295 x -= gx;
2296 gx += (x / width) * width;
2297 }
2298
2299 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2300 gx += window_box_left_offset (w, area);
2301 }
2302 else
2303 {
2304 /* Use nominal line height at end of window. */
2305 gx = (x / width) * width;
2306 y -= gy;
2307 gy += (y / height) * height;
2308 }
2309 break;
2310
2311 case ON_LEFT_FRINGE:
2312 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2313 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2314 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2315 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2316 goto row_glyph;
2317
2318 case ON_RIGHT_FRINGE:
2319 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2320 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2321 : window_box_right_offset (w, TEXT_AREA));
2322 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2323 goto row_glyph;
2324
2325 case ON_SCROLL_BAR:
2326 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2327 ? 0
2328 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2329 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2330 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2331 : 0)));
2332 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2333
2334 row_glyph:
2335 gr = 0, gy = 0;
2336 for (; r <= end_row && r->enabled_p; ++r)
2337 if (r->y + r->height > y)
2338 {
2339 gr = r; gy = r->y;
2340 break;
2341 }
2342
2343 if (gr && gy <= y)
2344 height = gr->height;
2345 else
2346 {
2347 /* Use nominal line height at end of window. */
2348 y -= gy;
2349 gy += (y / height) * height;
2350 }
2351 break;
2352
2353 default:
2354 ;
2355 virtual_glyph:
2356 /* If there is no glyph under the mouse, then we divide the screen
2357 into a grid of the smallest glyph in the frame, and use that
2358 as our "glyph". */
2359
2360 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2361 round down even for negative values. */
2362 if (gx < 0)
2363 gx -= width - 1;
2364 if (gy < 0)
2365 gy -= height - 1;
2366
2367 gx = (gx / width) * width;
2368 gy = (gy / height) * height;
2369
2370 goto store_rect;
2371 }
2372
2373 gx += WINDOW_LEFT_EDGE_X (w);
2374 gy += WINDOW_TOP_EDGE_Y (w);
2375
2376 store_rect:
2377 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2378
2379 /* Visible feedback for debugging. */
2380 #if 0
2381 #if HAVE_X_WINDOWS
2382 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2383 f->output_data.x->normal_gc,
2384 gx, gy, width, height);
2385 #endif
2386 #endif
2387 }
2388
2389
2390 #endif /* HAVE_WINDOW_SYSTEM */
2391
2392 \f
2393 /***********************************************************************
2394 Lisp form evaluation
2395 ***********************************************************************/
2396
2397 /* Error handler for safe_eval and safe_call. */
2398
2399 static Lisp_Object
2400 safe_eval_handler (Lisp_Object arg)
2401 {
2402 add_to_log ("Error during redisplay: %S", arg, Qnil);
2403 return Qnil;
2404 }
2405
2406 /* Call function FUNC with the rest of NARGS - 1 arguments
2407 following. Return the result, or nil if something went
2408 wrong. Prevent redisplay during the evaluation. */
2409
2410 Lisp_Object
2411 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2412 {
2413 Lisp_Object val;
2414
2415 if (inhibit_eval_during_redisplay)
2416 val = Qnil;
2417 else
2418 {
2419 va_list ap;
2420 ptrdiff_t i;
2421 ptrdiff_t count = SPECPDL_INDEX ();
2422 struct gcpro gcpro1;
2423 Lisp_Object *args = alloca (nargs * sizeof (Lisp_Object));
2424
2425 args[0] = func;
2426 va_start (ap, func);
2427 for (i = 1; i < nargs; i++)
2428 args[i] = va_arg (ap, Lisp_Object);
2429 va_end (ap);
2430
2431 GCPRO1 (args[0]);
2432 gcpro1.nvars = nargs;
2433 specbind (Qinhibit_redisplay, Qt);
2434 /* Use Qt to ensure debugger does not run,
2435 so there is no possibility of wanting to redisplay. */
2436 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2437 safe_eval_handler);
2438 UNGCPRO;
2439 val = unbind_to (count, val);
2440 }
2441
2442 return val;
2443 }
2444
2445
2446 /* Call function FN with one argument ARG.
2447 Return the result, or nil if something went wrong. */
2448
2449 Lisp_Object
2450 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2451 {
2452 return safe_call (2, fn, arg);
2453 }
2454
2455 static Lisp_Object Qeval;
2456
2457 Lisp_Object
2458 safe_eval (Lisp_Object sexpr)
2459 {
2460 return safe_call1 (Qeval, sexpr);
2461 }
2462
2463 /* Call function FN with two arguments ARG1 and ARG2.
2464 Return the result, or nil if something went wrong. */
2465
2466 Lisp_Object
2467 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2468 {
2469 return safe_call (3, fn, arg1, arg2);
2470 }
2471
2472
2473 \f
2474 /***********************************************************************
2475 Debugging
2476 ***********************************************************************/
2477
2478 #if 0
2479
2480 /* Define CHECK_IT to perform sanity checks on iterators.
2481 This is for debugging. It is too slow to do unconditionally. */
2482
2483 static void
2484 check_it (struct it *it)
2485 {
2486 if (it->method == GET_FROM_STRING)
2487 {
2488 eassert (STRINGP (it->string));
2489 eassert (IT_STRING_CHARPOS (*it) >= 0);
2490 }
2491 else
2492 {
2493 eassert (IT_STRING_CHARPOS (*it) < 0);
2494 if (it->method == GET_FROM_BUFFER)
2495 {
2496 /* Check that character and byte positions agree. */
2497 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2498 }
2499 }
2500
2501 if (it->dpvec)
2502 eassert (it->current.dpvec_index >= 0);
2503 else
2504 eassert (it->current.dpvec_index < 0);
2505 }
2506
2507 #define CHECK_IT(IT) check_it ((IT))
2508
2509 #else /* not 0 */
2510
2511 #define CHECK_IT(IT) (void) 0
2512
2513 #endif /* not 0 */
2514
2515
2516 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2517
2518 /* Check that the window end of window W is what we expect it
2519 to be---the last row in the current matrix displaying text. */
2520
2521 static void
2522 check_window_end (struct window *w)
2523 {
2524 if (!MINI_WINDOW_P (w)
2525 && !NILP (w->window_end_valid))
2526 {
2527 struct glyph_row *row;
2528 eassert ((row = MATRIX_ROW (w->current_matrix,
2529 XFASTINT (w->window_end_vpos)),
2530 !row->enabled_p
2531 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2532 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2533 }
2534 }
2535
2536 #define CHECK_WINDOW_END(W) check_window_end ((W))
2537
2538 #else
2539
2540 #define CHECK_WINDOW_END(W) (void) 0
2541
2542 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2543
2544
2545 \f
2546 /***********************************************************************
2547 Iterator initialization
2548 ***********************************************************************/
2549
2550 /* Initialize IT for displaying current_buffer in window W, starting
2551 at character position CHARPOS. CHARPOS < 0 means that no buffer
2552 position is specified which is useful when the iterator is assigned
2553 a position later. BYTEPOS is the byte position corresponding to
2554 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2555
2556 If ROW is not null, calls to produce_glyphs with IT as parameter
2557 will produce glyphs in that row.
2558
2559 BASE_FACE_ID is the id of a base face to use. It must be one of
2560 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2561 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2562 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2563
2564 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2565 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2566 will be initialized to use the corresponding mode line glyph row of
2567 the desired matrix of W. */
2568
2569 void
2570 init_iterator (struct it *it, struct window *w,
2571 ptrdiff_t charpos, ptrdiff_t bytepos,
2572 struct glyph_row *row, enum face_id base_face_id)
2573 {
2574 int highlight_region_p;
2575 enum face_id remapped_base_face_id = base_face_id;
2576
2577 /* Some precondition checks. */
2578 eassert (w != NULL && it != NULL);
2579 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2580 && charpos <= ZV));
2581
2582 /* If face attributes have been changed since the last redisplay,
2583 free realized faces now because they depend on face definitions
2584 that might have changed. Don't free faces while there might be
2585 desired matrices pending which reference these faces. */
2586 if (face_change_count && !inhibit_free_realized_faces)
2587 {
2588 face_change_count = 0;
2589 free_all_realized_faces (Qnil);
2590 }
2591
2592 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2593 if (! NILP (Vface_remapping_alist))
2594 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2595
2596 /* Use one of the mode line rows of W's desired matrix if
2597 appropriate. */
2598 if (row == NULL)
2599 {
2600 if (base_face_id == MODE_LINE_FACE_ID
2601 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2602 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2603 else if (base_face_id == HEADER_LINE_FACE_ID)
2604 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2605 }
2606
2607 /* Clear IT. */
2608 memset (it, 0, sizeof *it);
2609 it->current.overlay_string_index = -1;
2610 it->current.dpvec_index = -1;
2611 it->base_face_id = remapped_base_face_id;
2612 it->string = Qnil;
2613 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2614 it->paragraph_embedding = L2R;
2615 it->bidi_it.string.lstring = Qnil;
2616 it->bidi_it.string.s = NULL;
2617 it->bidi_it.string.bufpos = 0;
2618
2619 /* The window in which we iterate over current_buffer: */
2620 XSETWINDOW (it->window, w);
2621 it->w = w;
2622 it->f = XFRAME (w->frame);
2623
2624 it->cmp_it.id = -1;
2625
2626 /* Extra space between lines (on window systems only). */
2627 if (base_face_id == DEFAULT_FACE_ID
2628 && FRAME_WINDOW_P (it->f))
2629 {
2630 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2631 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2632 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2633 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2634 * FRAME_LINE_HEIGHT (it->f));
2635 else if (it->f->extra_line_spacing > 0)
2636 it->extra_line_spacing = it->f->extra_line_spacing;
2637 it->max_extra_line_spacing = 0;
2638 }
2639
2640 /* If realized faces have been removed, e.g. because of face
2641 attribute changes of named faces, recompute them. When running
2642 in batch mode, the face cache of the initial frame is null. If
2643 we happen to get called, make a dummy face cache. */
2644 if (FRAME_FACE_CACHE (it->f) == NULL)
2645 init_frame_faces (it->f);
2646 if (FRAME_FACE_CACHE (it->f)->used == 0)
2647 recompute_basic_faces (it->f);
2648
2649 /* Current value of the `slice', `space-width', and 'height' properties. */
2650 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2651 it->space_width = Qnil;
2652 it->font_height = Qnil;
2653 it->override_ascent = -1;
2654
2655 /* Are control characters displayed as `^C'? */
2656 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2657
2658 /* -1 means everything between a CR and the following line end
2659 is invisible. >0 means lines indented more than this value are
2660 invisible. */
2661 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2662 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2663 selective_display)),
2664 PTRDIFF_MAX)
2665 : (!NILP (BVAR (current_buffer, selective_display))
2666 ? -1 : 0));
2667 it->selective_display_ellipsis_p
2668 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2669
2670 /* Display table to use. */
2671 it->dp = window_display_table (w);
2672
2673 /* Are multibyte characters enabled in current_buffer? */
2674 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2675
2676 /* Non-zero if we should highlight the region. */
2677 highlight_region_p
2678 = (!NILP (Vtransient_mark_mode)
2679 && !NILP (BVAR (current_buffer, mark_active))
2680 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2681
2682 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2683 start and end of a visible region in window IT->w. Set both to
2684 -1 to indicate no region. */
2685 if (highlight_region_p
2686 /* Maybe highlight only in selected window. */
2687 && (/* Either show region everywhere. */
2688 highlight_nonselected_windows
2689 /* Or show region in the selected window. */
2690 || w == XWINDOW (selected_window)
2691 /* Or show the region if we are in the mini-buffer and W is
2692 the window the mini-buffer refers to. */
2693 || (MINI_WINDOW_P (XWINDOW (selected_window))
2694 && WINDOWP (minibuf_selected_window)
2695 && w == XWINDOW (minibuf_selected_window))))
2696 {
2697 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2698 it->region_beg_charpos = min (PT, markpos);
2699 it->region_end_charpos = max (PT, markpos);
2700 }
2701 else
2702 it->region_beg_charpos = it->region_end_charpos = -1;
2703
2704 /* Get the position at which the redisplay_end_trigger hook should
2705 be run, if it is to be run at all. */
2706 if (MARKERP (w->redisplay_end_trigger)
2707 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2708 it->redisplay_end_trigger_charpos
2709 = marker_position (w->redisplay_end_trigger);
2710 else if (INTEGERP (w->redisplay_end_trigger))
2711 it->redisplay_end_trigger_charpos =
2712 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2713
2714 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2715
2716 /* Are lines in the display truncated? */
2717 if (base_face_id != DEFAULT_FACE_ID
2718 || it->w->hscroll
2719 || (! WINDOW_FULL_WIDTH_P (it->w)
2720 && ((!NILP (Vtruncate_partial_width_windows)
2721 && !INTEGERP (Vtruncate_partial_width_windows))
2722 || (INTEGERP (Vtruncate_partial_width_windows)
2723 && (WINDOW_TOTAL_COLS (it->w)
2724 < XINT (Vtruncate_partial_width_windows))))))
2725 it->line_wrap = TRUNCATE;
2726 else if (NILP (BVAR (current_buffer, truncate_lines)))
2727 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2728 ? WINDOW_WRAP : WORD_WRAP;
2729 else
2730 it->line_wrap = TRUNCATE;
2731
2732 /* Get dimensions of truncation and continuation glyphs. These are
2733 displayed as fringe bitmaps under X, but we need them for such
2734 frames when the fringes are turned off. But leave the dimensions
2735 zero for tooltip frames, as these glyphs look ugly there and also
2736 sabotage calculations of tooltip dimensions in x-show-tip. */
2737 #ifdef HAVE_WINDOW_SYSTEM
2738 if (!(FRAME_WINDOW_P (it->f)
2739 && FRAMEP (tip_frame)
2740 && it->f == XFRAME (tip_frame)))
2741 #endif
2742 {
2743 if (it->line_wrap == TRUNCATE)
2744 {
2745 /* We will need the truncation glyph. */
2746 eassert (it->glyph_row == NULL);
2747 produce_special_glyphs (it, IT_TRUNCATION);
2748 it->truncation_pixel_width = it->pixel_width;
2749 }
2750 else
2751 {
2752 /* We will need the continuation glyph. */
2753 eassert (it->glyph_row == NULL);
2754 produce_special_glyphs (it, IT_CONTINUATION);
2755 it->continuation_pixel_width = it->pixel_width;
2756 }
2757 }
2758
2759 /* Reset these values to zero because the produce_special_glyphs
2760 above has changed them. */
2761 it->pixel_width = it->ascent = it->descent = 0;
2762 it->phys_ascent = it->phys_descent = 0;
2763
2764 /* Set this after getting the dimensions of truncation and
2765 continuation glyphs, so that we don't produce glyphs when calling
2766 produce_special_glyphs, above. */
2767 it->glyph_row = row;
2768 it->area = TEXT_AREA;
2769
2770 /* Forget any previous info about this row being reversed. */
2771 if (it->glyph_row)
2772 it->glyph_row->reversed_p = 0;
2773
2774 /* Get the dimensions of the display area. The display area
2775 consists of the visible window area plus a horizontally scrolled
2776 part to the left of the window. All x-values are relative to the
2777 start of this total display area. */
2778 if (base_face_id != DEFAULT_FACE_ID)
2779 {
2780 /* Mode lines, menu bar in terminal frames. */
2781 it->first_visible_x = 0;
2782 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2783 }
2784 else
2785 {
2786 it->first_visible_x =
2787 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2788 it->last_visible_x = (it->first_visible_x
2789 + window_box_width (w, TEXT_AREA));
2790
2791 /* If we truncate lines, leave room for the truncation glyph(s) at
2792 the right margin. Otherwise, leave room for the continuation
2793 glyph(s). Done only if the window has no fringes. Since we
2794 don't know at this point whether there will be any R2L lines in
2795 the window, we reserve space for truncation/continuation glyphs
2796 even if only one of the fringes is absent. */
2797 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2798 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2799 {
2800 if (it->line_wrap == TRUNCATE)
2801 it->last_visible_x -= it->truncation_pixel_width;
2802 else
2803 it->last_visible_x -= it->continuation_pixel_width;
2804 }
2805
2806 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2807 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2808 }
2809
2810 /* Leave room for a border glyph. */
2811 if (!FRAME_WINDOW_P (it->f)
2812 && !WINDOW_RIGHTMOST_P (it->w))
2813 it->last_visible_x -= 1;
2814
2815 it->last_visible_y = window_text_bottom_y (w);
2816
2817 /* For mode lines and alike, arrange for the first glyph having a
2818 left box line if the face specifies a box. */
2819 if (base_face_id != DEFAULT_FACE_ID)
2820 {
2821 struct face *face;
2822
2823 it->face_id = remapped_base_face_id;
2824
2825 /* If we have a boxed mode line, make the first character appear
2826 with a left box line. */
2827 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2828 if (face->box != FACE_NO_BOX)
2829 it->start_of_box_run_p = 1;
2830 }
2831
2832 /* If a buffer position was specified, set the iterator there,
2833 getting overlays and face properties from that position. */
2834 if (charpos >= BUF_BEG (current_buffer))
2835 {
2836 it->end_charpos = ZV;
2837 IT_CHARPOS (*it) = charpos;
2838
2839 /* We will rely on `reseat' to set this up properly, via
2840 handle_face_prop. */
2841 it->face_id = it->base_face_id;
2842
2843 /* Compute byte position if not specified. */
2844 if (bytepos < charpos)
2845 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2846 else
2847 IT_BYTEPOS (*it) = bytepos;
2848
2849 it->start = it->current;
2850 /* Do we need to reorder bidirectional text? Not if this is a
2851 unibyte buffer: by definition, none of the single-byte
2852 characters are strong R2L, so no reordering is needed. And
2853 bidi.c doesn't support unibyte buffers anyway. Also, don't
2854 reorder while we are loading loadup.el, since the tables of
2855 character properties needed for reordering are not yet
2856 available. */
2857 it->bidi_p =
2858 NILP (Vpurify_flag)
2859 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2860 && it->multibyte_p;
2861
2862 /* If we are to reorder bidirectional text, init the bidi
2863 iterator. */
2864 if (it->bidi_p)
2865 {
2866 /* Note the paragraph direction that this buffer wants to
2867 use. */
2868 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2869 Qleft_to_right))
2870 it->paragraph_embedding = L2R;
2871 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2872 Qright_to_left))
2873 it->paragraph_embedding = R2L;
2874 else
2875 it->paragraph_embedding = NEUTRAL_DIR;
2876 bidi_unshelve_cache (NULL, 0);
2877 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2878 &it->bidi_it);
2879 }
2880
2881 /* Compute faces etc. */
2882 reseat (it, it->current.pos, 1);
2883 }
2884
2885 CHECK_IT (it);
2886 }
2887
2888
2889 /* Initialize IT for the display of window W with window start POS. */
2890
2891 void
2892 start_display (struct it *it, struct window *w, struct text_pos pos)
2893 {
2894 struct glyph_row *row;
2895 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2896
2897 row = w->desired_matrix->rows + first_vpos;
2898 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2899 it->first_vpos = first_vpos;
2900
2901 /* Don't reseat to previous visible line start if current start
2902 position is in a string or image. */
2903 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2904 {
2905 int start_at_line_beg_p;
2906 int first_y = it->current_y;
2907
2908 /* If window start is not at a line start, skip forward to POS to
2909 get the correct continuation lines width. */
2910 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2911 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2912 if (!start_at_line_beg_p)
2913 {
2914 int new_x;
2915
2916 reseat_at_previous_visible_line_start (it);
2917 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2918
2919 new_x = it->current_x + it->pixel_width;
2920
2921 /* If lines are continued, this line may end in the middle
2922 of a multi-glyph character (e.g. a control character
2923 displayed as \003, or in the middle of an overlay
2924 string). In this case move_it_to above will not have
2925 taken us to the start of the continuation line but to the
2926 end of the continued line. */
2927 if (it->current_x > 0
2928 && it->line_wrap != TRUNCATE /* Lines are continued. */
2929 && (/* And glyph doesn't fit on the line. */
2930 new_x > it->last_visible_x
2931 /* Or it fits exactly and we're on a window
2932 system frame. */
2933 || (new_x == it->last_visible_x
2934 && FRAME_WINDOW_P (it->f)
2935 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2936 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2937 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2938 {
2939 if ((it->current.dpvec_index >= 0
2940 || it->current.overlay_string_index >= 0)
2941 /* If we are on a newline from a display vector or
2942 overlay string, then we are already at the end of
2943 a screen line; no need to go to the next line in
2944 that case, as this line is not really continued.
2945 (If we do go to the next line, C-e will not DTRT.) */
2946 && it->c != '\n')
2947 {
2948 set_iterator_to_next (it, 1);
2949 move_it_in_display_line_to (it, -1, -1, 0);
2950 }
2951
2952 it->continuation_lines_width += it->current_x;
2953 }
2954 /* If the character at POS is displayed via a display
2955 vector, move_it_to above stops at the final glyph of
2956 IT->dpvec. To make the caller redisplay that character
2957 again (a.k.a. start at POS), we need to reset the
2958 dpvec_index to the beginning of IT->dpvec. */
2959 else if (it->current.dpvec_index >= 0)
2960 it->current.dpvec_index = 0;
2961
2962 /* We're starting a new display line, not affected by the
2963 height of the continued line, so clear the appropriate
2964 fields in the iterator structure. */
2965 it->max_ascent = it->max_descent = 0;
2966 it->max_phys_ascent = it->max_phys_descent = 0;
2967
2968 it->current_y = first_y;
2969 it->vpos = 0;
2970 it->current_x = it->hpos = 0;
2971 }
2972 }
2973 }
2974
2975
2976 /* Return 1 if POS is a position in ellipses displayed for invisible
2977 text. W is the window we display, for text property lookup. */
2978
2979 static int
2980 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2981 {
2982 Lisp_Object prop, window;
2983 int ellipses_p = 0;
2984 ptrdiff_t charpos = CHARPOS (pos->pos);
2985
2986 /* If POS specifies a position in a display vector, this might
2987 be for an ellipsis displayed for invisible text. We won't
2988 get the iterator set up for delivering that ellipsis unless
2989 we make sure that it gets aware of the invisible text. */
2990 if (pos->dpvec_index >= 0
2991 && pos->overlay_string_index < 0
2992 && CHARPOS (pos->string_pos) < 0
2993 && charpos > BEGV
2994 && (XSETWINDOW (window, w),
2995 prop = Fget_char_property (make_number (charpos),
2996 Qinvisible, window),
2997 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2998 {
2999 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3000 window);
3001 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3002 }
3003
3004 return ellipses_p;
3005 }
3006
3007
3008 /* Initialize IT for stepping through current_buffer in window W,
3009 starting at position POS that includes overlay string and display
3010 vector/ control character translation position information. Value
3011 is zero if there are overlay strings with newlines at POS. */
3012
3013 static int
3014 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3015 {
3016 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3017 int i, overlay_strings_with_newlines = 0;
3018
3019 /* If POS specifies a position in a display vector, this might
3020 be for an ellipsis displayed for invisible text. We won't
3021 get the iterator set up for delivering that ellipsis unless
3022 we make sure that it gets aware of the invisible text. */
3023 if (in_ellipses_for_invisible_text_p (pos, w))
3024 {
3025 --charpos;
3026 bytepos = 0;
3027 }
3028
3029 /* Keep in mind: the call to reseat in init_iterator skips invisible
3030 text, so we might end up at a position different from POS. This
3031 is only a problem when POS is a row start after a newline and an
3032 overlay starts there with an after-string, and the overlay has an
3033 invisible property. Since we don't skip invisible text in
3034 display_line and elsewhere immediately after consuming the
3035 newline before the row start, such a POS will not be in a string,
3036 but the call to init_iterator below will move us to the
3037 after-string. */
3038 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3039
3040 /* This only scans the current chunk -- it should scan all chunks.
3041 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3042 to 16 in 22.1 to make this a lesser problem. */
3043 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3044 {
3045 const char *s = SSDATA (it->overlay_strings[i]);
3046 const char *e = s + SBYTES (it->overlay_strings[i]);
3047
3048 while (s < e && *s != '\n')
3049 ++s;
3050
3051 if (s < e)
3052 {
3053 overlay_strings_with_newlines = 1;
3054 break;
3055 }
3056 }
3057
3058 /* If position is within an overlay string, set up IT to the right
3059 overlay string. */
3060 if (pos->overlay_string_index >= 0)
3061 {
3062 int relative_index;
3063
3064 /* If the first overlay string happens to have a `display'
3065 property for an image, the iterator will be set up for that
3066 image, and we have to undo that setup first before we can
3067 correct the overlay string index. */
3068 if (it->method == GET_FROM_IMAGE)
3069 pop_it (it);
3070
3071 /* We already have the first chunk of overlay strings in
3072 IT->overlay_strings. Load more until the one for
3073 pos->overlay_string_index is in IT->overlay_strings. */
3074 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3075 {
3076 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3077 it->current.overlay_string_index = 0;
3078 while (n--)
3079 {
3080 load_overlay_strings (it, 0);
3081 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3082 }
3083 }
3084
3085 it->current.overlay_string_index = pos->overlay_string_index;
3086 relative_index = (it->current.overlay_string_index
3087 % OVERLAY_STRING_CHUNK_SIZE);
3088 it->string = it->overlay_strings[relative_index];
3089 eassert (STRINGP (it->string));
3090 it->current.string_pos = pos->string_pos;
3091 it->method = GET_FROM_STRING;
3092 }
3093
3094 if (CHARPOS (pos->string_pos) >= 0)
3095 {
3096 /* Recorded position is not in an overlay string, but in another
3097 string. This can only be a string from a `display' property.
3098 IT should already be filled with that string. */
3099 it->current.string_pos = pos->string_pos;
3100 eassert (STRINGP (it->string));
3101 }
3102
3103 /* Restore position in display vector translations, control
3104 character translations or ellipses. */
3105 if (pos->dpvec_index >= 0)
3106 {
3107 if (it->dpvec == NULL)
3108 get_next_display_element (it);
3109 eassert (it->dpvec && it->current.dpvec_index == 0);
3110 it->current.dpvec_index = pos->dpvec_index;
3111 }
3112
3113 CHECK_IT (it);
3114 return !overlay_strings_with_newlines;
3115 }
3116
3117
3118 /* Initialize IT for stepping through current_buffer in window W
3119 starting at ROW->start. */
3120
3121 static void
3122 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3123 {
3124 init_from_display_pos (it, w, &row->start);
3125 it->start = row->start;
3126 it->continuation_lines_width = row->continuation_lines_width;
3127 CHECK_IT (it);
3128 }
3129
3130
3131 /* Initialize IT for stepping through current_buffer in window W
3132 starting in the line following ROW, i.e. starting at ROW->end.
3133 Value is zero if there are overlay strings with newlines at ROW's
3134 end position. */
3135
3136 static int
3137 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3138 {
3139 int success = 0;
3140
3141 if (init_from_display_pos (it, w, &row->end))
3142 {
3143 if (row->continued_p)
3144 it->continuation_lines_width
3145 = row->continuation_lines_width + row->pixel_width;
3146 CHECK_IT (it);
3147 success = 1;
3148 }
3149
3150 return success;
3151 }
3152
3153
3154
3155 \f
3156 /***********************************************************************
3157 Text properties
3158 ***********************************************************************/
3159
3160 /* Called when IT reaches IT->stop_charpos. Handle text property and
3161 overlay changes. Set IT->stop_charpos to the next position where
3162 to stop. */
3163
3164 static void
3165 handle_stop (struct it *it)
3166 {
3167 enum prop_handled handled;
3168 int handle_overlay_change_p;
3169 struct props *p;
3170
3171 it->dpvec = NULL;
3172 it->current.dpvec_index = -1;
3173 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3174 it->ignore_overlay_strings_at_pos_p = 0;
3175 it->ellipsis_p = 0;
3176
3177 /* Use face of preceding text for ellipsis (if invisible) */
3178 if (it->selective_display_ellipsis_p)
3179 it->saved_face_id = it->face_id;
3180
3181 do
3182 {
3183 handled = HANDLED_NORMALLY;
3184
3185 /* Call text property handlers. */
3186 for (p = it_props; p->handler; ++p)
3187 {
3188 handled = p->handler (it);
3189
3190 if (handled == HANDLED_RECOMPUTE_PROPS)
3191 break;
3192 else if (handled == HANDLED_RETURN)
3193 {
3194 /* We still want to show before and after strings from
3195 overlays even if the actual buffer text is replaced. */
3196 if (!handle_overlay_change_p
3197 || it->sp > 1
3198 /* Don't call get_overlay_strings_1 if we already
3199 have overlay strings loaded, because doing so
3200 will load them again and push the iterator state
3201 onto the stack one more time, which is not
3202 expected by the rest of the code that processes
3203 overlay strings. */
3204 || (it->current.overlay_string_index < 0
3205 ? !get_overlay_strings_1 (it, 0, 0)
3206 : 0))
3207 {
3208 if (it->ellipsis_p)
3209 setup_for_ellipsis (it, 0);
3210 /* When handling a display spec, we might load an
3211 empty string. In that case, discard it here. We
3212 used to discard it in handle_single_display_spec,
3213 but that causes get_overlay_strings_1, above, to
3214 ignore overlay strings that we must check. */
3215 if (STRINGP (it->string) && !SCHARS (it->string))
3216 pop_it (it);
3217 return;
3218 }
3219 else if (STRINGP (it->string) && !SCHARS (it->string))
3220 pop_it (it);
3221 else
3222 {
3223 it->ignore_overlay_strings_at_pos_p = 1;
3224 it->string_from_display_prop_p = 0;
3225 it->from_disp_prop_p = 0;
3226 handle_overlay_change_p = 0;
3227 }
3228 handled = HANDLED_RECOMPUTE_PROPS;
3229 break;
3230 }
3231 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3232 handle_overlay_change_p = 0;
3233 }
3234
3235 if (handled != HANDLED_RECOMPUTE_PROPS)
3236 {
3237 /* Don't check for overlay strings below when set to deliver
3238 characters from a display vector. */
3239 if (it->method == GET_FROM_DISPLAY_VECTOR)
3240 handle_overlay_change_p = 0;
3241
3242 /* Handle overlay changes.
3243 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3244 if it finds overlays. */
3245 if (handle_overlay_change_p)
3246 handled = handle_overlay_change (it);
3247 }
3248
3249 if (it->ellipsis_p)
3250 {
3251 setup_for_ellipsis (it, 0);
3252 break;
3253 }
3254 }
3255 while (handled == HANDLED_RECOMPUTE_PROPS);
3256
3257 /* Determine where to stop next. */
3258 if (handled == HANDLED_NORMALLY)
3259 compute_stop_pos (it);
3260 }
3261
3262
3263 /* Compute IT->stop_charpos from text property and overlay change
3264 information for IT's current position. */
3265
3266 static void
3267 compute_stop_pos (struct it *it)
3268 {
3269 register INTERVAL iv, next_iv;
3270 Lisp_Object object, limit, position;
3271 ptrdiff_t charpos, bytepos;
3272
3273 if (STRINGP (it->string))
3274 {
3275 /* Strings are usually short, so don't limit the search for
3276 properties. */
3277 it->stop_charpos = it->end_charpos;
3278 object = it->string;
3279 limit = Qnil;
3280 charpos = IT_STRING_CHARPOS (*it);
3281 bytepos = IT_STRING_BYTEPOS (*it);
3282 }
3283 else
3284 {
3285 ptrdiff_t pos;
3286
3287 /* If end_charpos is out of range for some reason, such as a
3288 misbehaving display function, rationalize it (Bug#5984). */
3289 if (it->end_charpos > ZV)
3290 it->end_charpos = ZV;
3291 it->stop_charpos = it->end_charpos;
3292
3293 /* If next overlay change is in front of the current stop pos
3294 (which is IT->end_charpos), stop there. Note: value of
3295 next_overlay_change is point-max if no overlay change
3296 follows. */
3297 charpos = IT_CHARPOS (*it);
3298 bytepos = IT_BYTEPOS (*it);
3299 pos = next_overlay_change (charpos);
3300 if (pos < it->stop_charpos)
3301 it->stop_charpos = pos;
3302
3303 /* If showing the region, we have to stop at the region
3304 start or end because the face might change there. */
3305 if (it->region_beg_charpos > 0)
3306 {
3307 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3308 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3309 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3310 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3311 }
3312
3313 /* Set up variables for computing the stop position from text
3314 property changes. */
3315 XSETBUFFER (object, current_buffer);
3316 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3317 }
3318
3319 /* Get the interval containing IT's position. Value is a null
3320 interval if there isn't such an interval. */
3321 position = make_number (charpos);
3322 iv = validate_interval_range (object, &position, &position, 0);
3323 if (!NULL_INTERVAL_P (iv))
3324 {
3325 Lisp_Object values_here[LAST_PROP_IDX];
3326 struct props *p;
3327
3328 /* Get properties here. */
3329 for (p = it_props; p->handler; ++p)
3330 values_here[p->idx] = textget (iv->plist, *p->name);
3331
3332 /* Look for an interval following iv that has different
3333 properties. */
3334 for (next_iv = next_interval (iv);
3335 (!NULL_INTERVAL_P (next_iv)
3336 && (NILP (limit)
3337 || XFASTINT (limit) > next_iv->position));
3338 next_iv = next_interval (next_iv))
3339 {
3340 for (p = it_props; p->handler; ++p)
3341 {
3342 Lisp_Object new_value;
3343
3344 new_value = textget (next_iv->plist, *p->name);
3345 if (!EQ (values_here[p->idx], new_value))
3346 break;
3347 }
3348
3349 if (p->handler)
3350 break;
3351 }
3352
3353 if (!NULL_INTERVAL_P (next_iv))
3354 {
3355 if (INTEGERP (limit)
3356 && next_iv->position >= XFASTINT (limit))
3357 /* No text property change up to limit. */
3358 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3359 else
3360 /* Text properties change in next_iv. */
3361 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3362 }
3363 }
3364
3365 if (it->cmp_it.id < 0)
3366 {
3367 ptrdiff_t stoppos = it->end_charpos;
3368
3369 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3370 stoppos = -1;
3371 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3372 stoppos, it->string);
3373 }
3374
3375 eassert (STRINGP (it->string)
3376 || (it->stop_charpos >= BEGV
3377 && it->stop_charpos >= IT_CHARPOS (*it)));
3378 }
3379
3380
3381 /* Return the position of the next overlay change after POS in
3382 current_buffer. Value is point-max if no overlay change
3383 follows. This is like `next-overlay-change' but doesn't use
3384 xmalloc. */
3385
3386 static ptrdiff_t
3387 next_overlay_change (ptrdiff_t pos)
3388 {
3389 ptrdiff_t i, noverlays;
3390 ptrdiff_t endpos;
3391 Lisp_Object *overlays;
3392
3393 /* Get all overlays at the given position. */
3394 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3395
3396 /* If any of these overlays ends before endpos,
3397 use its ending point instead. */
3398 for (i = 0; i < noverlays; ++i)
3399 {
3400 Lisp_Object oend;
3401 ptrdiff_t oendpos;
3402
3403 oend = OVERLAY_END (overlays[i]);
3404 oendpos = OVERLAY_POSITION (oend);
3405 endpos = min (endpos, oendpos);
3406 }
3407
3408 return endpos;
3409 }
3410
3411 /* How many characters forward to search for a display property or
3412 display string. Searching too far forward makes the bidi display
3413 sluggish, especially in small windows. */
3414 #define MAX_DISP_SCAN 250
3415
3416 /* Return the character position of a display string at or after
3417 position specified by POSITION. If no display string exists at or
3418 after POSITION, return ZV. A display string is either an overlay
3419 with `display' property whose value is a string, or a `display'
3420 text property whose value is a string. STRING is data about the
3421 string to iterate; if STRING->lstring is nil, we are iterating a
3422 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3423 on a GUI frame. DISP_PROP is set to zero if we searched
3424 MAX_DISP_SCAN characters forward without finding any display
3425 strings, non-zero otherwise. It is set to 2 if the display string
3426 uses any kind of `(space ...)' spec that will produce a stretch of
3427 white space in the text area. */
3428 ptrdiff_t
3429 compute_display_string_pos (struct text_pos *position,
3430 struct bidi_string_data *string,
3431 int frame_window_p, int *disp_prop)
3432 {
3433 /* OBJECT = nil means current buffer. */
3434 Lisp_Object object =
3435 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3436 Lisp_Object pos, spec, limpos;
3437 int string_p = (string && (STRINGP (string->lstring) || string->s));
3438 ptrdiff_t eob = string_p ? string->schars : ZV;
3439 ptrdiff_t begb = string_p ? 0 : BEGV;
3440 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3441 ptrdiff_t lim =
3442 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3443 struct text_pos tpos;
3444 int rv = 0;
3445
3446 *disp_prop = 1;
3447
3448 if (charpos >= eob
3449 /* We don't support display properties whose values are strings
3450 that have display string properties. */
3451 || string->from_disp_str
3452 /* C strings cannot have display properties. */
3453 || (string->s && !STRINGP (object)))
3454 {
3455 *disp_prop = 0;
3456 return eob;
3457 }
3458
3459 /* If the character at CHARPOS is where the display string begins,
3460 return CHARPOS. */
3461 pos = make_number (charpos);
3462 if (STRINGP (object))
3463 bufpos = string->bufpos;
3464 else
3465 bufpos = charpos;
3466 tpos = *position;
3467 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3468 && (charpos <= begb
3469 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3470 object),
3471 spec))
3472 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3473 frame_window_p)))
3474 {
3475 if (rv == 2)
3476 *disp_prop = 2;
3477 return charpos;
3478 }
3479
3480 /* Look forward for the first character with a `display' property
3481 that will replace the underlying text when displayed. */
3482 limpos = make_number (lim);
3483 do {
3484 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3485 CHARPOS (tpos) = XFASTINT (pos);
3486 if (CHARPOS (tpos) >= lim)
3487 {
3488 *disp_prop = 0;
3489 break;
3490 }
3491 if (STRINGP (object))
3492 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3493 else
3494 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3495 spec = Fget_char_property (pos, Qdisplay, object);
3496 if (!STRINGP (object))
3497 bufpos = CHARPOS (tpos);
3498 } while (NILP (spec)
3499 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3500 bufpos, frame_window_p)));
3501 if (rv == 2)
3502 *disp_prop = 2;
3503
3504 return CHARPOS (tpos);
3505 }
3506
3507 /* Return the character position of the end of the display string that
3508 started at CHARPOS. If there's no display string at CHARPOS,
3509 return -1. A display string is either an overlay with `display'
3510 property whose value is a string or a `display' text property whose
3511 value is a string. */
3512 ptrdiff_t
3513 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3514 {
3515 /* OBJECT = nil means current buffer. */
3516 Lisp_Object object =
3517 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3518 Lisp_Object pos = make_number (charpos);
3519 ptrdiff_t eob =
3520 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3521
3522 if (charpos >= eob || (string->s && !STRINGP (object)))
3523 return eob;
3524
3525 /* It could happen that the display property or overlay was removed
3526 since we found it in compute_display_string_pos above. One way
3527 this can happen is if JIT font-lock was called (through
3528 handle_fontified_prop), and jit-lock-functions remove text
3529 properties or overlays from the portion of buffer that includes
3530 CHARPOS. Muse mode is known to do that, for example. In this
3531 case, we return -1 to the caller, to signal that no display
3532 string is actually present at CHARPOS. See bidi_fetch_char for
3533 how this is handled.
3534
3535 An alternative would be to never look for display properties past
3536 it->stop_charpos. But neither compute_display_string_pos nor
3537 bidi_fetch_char that calls it know or care where the next
3538 stop_charpos is. */
3539 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3540 return -1;
3541
3542 /* Look forward for the first character where the `display' property
3543 changes. */
3544 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3545
3546 return XFASTINT (pos);
3547 }
3548
3549
3550 \f
3551 /***********************************************************************
3552 Fontification
3553 ***********************************************************************/
3554
3555 /* Handle changes in the `fontified' property of the current buffer by
3556 calling hook functions from Qfontification_functions to fontify
3557 regions of text. */
3558
3559 static enum prop_handled
3560 handle_fontified_prop (struct it *it)
3561 {
3562 Lisp_Object prop, pos;
3563 enum prop_handled handled = HANDLED_NORMALLY;
3564
3565 if (!NILP (Vmemory_full))
3566 return handled;
3567
3568 /* Get the value of the `fontified' property at IT's current buffer
3569 position. (The `fontified' property doesn't have a special
3570 meaning in strings.) If the value is nil, call functions from
3571 Qfontification_functions. */
3572 if (!STRINGP (it->string)
3573 && it->s == NULL
3574 && !NILP (Vfontification_functions)
3575 && !NILP (Vrun_hooks)
3576 && (pos = make_number (IT_CHARPOS (*it)),
3577 prop = Fget_char_property (pos, Qfontified, Qnil),
3578 /* Ignore the special cased nil value always present at EOB since
3579 no amount of fontifying will be able to change it. */
3580 NILP (prop) && IT_CHARPOS (*it) < Z))
3581 {
3582 ptrdiff_t count = SPECPDL_INDEX ();
3583 Lisp_Object val;
3584 struct buffer *obuf = current_buffer;
3585 int begv = BEGV, zv = ZV;
3586 int old_clip_changed = current_buffer->clip_changed;
3587
3588 val = Vfontification_functions;
3589 specbind (Qfontification_functions, Qnil);
3590
3591 eassert (it->end_charpos == ZV);
3592
3593 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3594 safe_call1 (val, pos);
3595 else
3596 {
3597 Lisp_Object fns, fn;
3598 struct gcpro gcpro1, gcpro2;
3599
3600 fns = Qnil;
3601 GCPRO2 (val, fns);
3602
3603 for (; CONSP (val); val = XCDR (val))
3604 {
3605 fn = XCAR (val);
3606
3607 if (EQ (fn, Qt))
3608 {
3609 /* A value of t indicates this hook has a local
3610 binding; it means to run the global binding too.
3611 In a global value, t should not occur. If it
3612 does, we must ignore it to avoid an endless
3613 loop. */
3614 for (fns = Fdefault_value (Qfontification_functions);
3615 CONSP (fns);
3616 fns = XCDR (fns))
3617 {
3618 fn = XCAR (fns);
3619 if (!EQ (fn, Qt))
3620 safe_call1 (fn, pos);
3621 }
3622 }
3623 else
3624 safe_call1 (fn, pos);
3625 }
3626
3627 UNGCPRO;
3628 }
3629
3630 unbind_to (count, Qnil);
3631
3632 /* Fontification functions routinely call `save-restriction'.
3633 Normally, this tags clip_changed, which can confuse redisplay
3634 (see discussion in Bug#6671). Since we don't perform any
3635 special handling of fontification changes in the case where
3636 `save-restriction' isn't called, there's no point doing so in
3637 this case either. So, if the buffer's restrictions are
3638 actually left unchanged, reset clip_changed. */
3639 if (obuf == current_buffer)
3640 {
3641 if (begv == BEGV && zv == ZV)
3642 current_buffer->clip_changed = old_clip_changed;
3643 }
3644 /* There isn't much we can reasonably do to protect against
3645 misbehaving fontification, but here's a fig leaf. */
3646 else if (!NILP (BVAR (obuf, name)))
3647 set_buffer_internal_1 (obuf);
3648
3649 /* The fontification code may have added/removed text.
3650 It could do even a lot worse, but let's at least protect against
3651 the most obvious case where only the text past `pos' gets changed',
3652 as is/was done in grep.el where some escapes sequences are turned
3653 into face properties (bug#7876). */
3654 it->end_charpos = ZV;
3655
3656 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3657 something. This avoids an endless loop if they failed to
3658 fontify the text for which reason ever. */
3659 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3660 handled = HANDLED_RECOMPUTE_PROPS;
3661 }
3662
3663 return handled;
3664 }
3665
3666
3667 \f
3668 /***********************************************************************
3669 Faces
3670 ***********************************************************************/
3671
3672 /* Set up iterator IT from face properties at its current position.
3673 Called from handle_stop. */
3674
3675 static enum prop_handled
3676 handle_face_prop (struct it *it)
3677 {
3678 int new_face_id;
3679 ptrdiff_t next_stop;
3680
3681 if (!STRINGP (it->string))
3682 {
3683 new_face_id
3684 = face_at_buffer_position (it->w,
3685 IT_CHARPOS (*it),
3686 it->region_beg_charpos,
3687 it->region_end_charpos,
3688 &next_stop,
3689 (IT_CHARPOS (*it)
3690 + TEXT_PROP_DISTANCE_LIMIT),
3691 0, it->base_face_id);
3692
3693 /* Is this a start of a run of characters with box face?
3694 Caveat: this can be called for a freshly initialized
3695 iterator; face_id is -1 in this case. We know that the new
3696 face will not change until limit, i.e. if the new face has a
3697 box, all characters up to limit will have one. But, as
3698 usual, we don't know whether limit is really the end. */
3699 if (new_face_id != it->face_id)
3700 {
3701 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3702
3703 /* If new face has a box but old face has not, this is
3704 the start of a run of characters with box, i.e. it has
3705 a shadow on the left side. The value of face_id of the
3706 iterator will be -1 if this is the initial call that gets
3707 the face. In this case, we have to look in front of IT's
3708 position and see whether there is a face != new_face_id. */
3709 it->start_of_box_run_p
3710 = (new_face->box != FACE_NO_BOX
3711 && (it->face_id >= 0
3712 || IT_CHARPOS (*it) == BEG
3713 || new_face_id != face_before_it_pos (it)));
3714 it->face_box_p = new_face->box != FACE_NO_BOX;
3715 }
3716 }
3717 else
3718 {
3719 int base_face_id;
3720 ptrdiff_t bufpos;
3721 int i;
3722 Lisp_Object from_overlay
3723 = (it->current.overlay_string_index >= 0
3724 ? it->string_overlays[it->current.overlay_string_index
3725 % OVERLAY_STRING_CHUNK_SIZE]
3726 : Qnil);
3727
3728 /* See if we got to this string directly or indirectly from
3729 an overlay property. That includes the before-string or
3730 after-string of an overlay, strings in display properties
3731 provided by an overlay, their text properties, etc.
3732
3733 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3734 if (! NILP (from_overlay))
3735 for (i = it->sp - 1; i >= 0; i--)
3736 {
3737 if (it->stack[i].current.overlay_string_index >= 0)
3738 from_overlay
3739 = it->string_overlays[it->stack[i].current.overlay_string_index
3740 % OVERLAY_STRING_CHUNK_SIZE];
3741 else if (! NILP (it->stack[i].from_overlay))
3742 from_overlay = it->stack[i].from_overlay;
3743
3744 if (!NILP (from_overlay))
3745 break;
3746 }
3747
3748 if (! NILP (from_overlay))
3749 {
3750 bufpos = IT_CHARPOS (*it);
3751 /* For a string from an overlay, the base face depends
3752 only on text properties and ignores overlays. */
3753 base_face_id
3754 = face_for_overlay_string (it->w,
3755 IT_CHARPOS (*it),
3756 it->region_beg_charpos,
3757 it->region_end_charpos,
3758 &next_stop,
3759 (IT_CHARPOS (*it)
3760 + TEXT_PROP_DISTANCE_LIMIT),
3761 0,
3762 from_overlay);
3763 }
3764 else
3765 {
3766 bufpos = 0;
3767
3768 /* For strings from a `display' property, use the face at
3769 IT's current buffer position as the base face to merge
3770 with, so that overlay strings appear in the same face as
3771 surrounding text, unless they specify their own
3772 faces. */
3773 base_face_id = it->string_from_prefix_prop_p
3774 ? DEFAULT_FACE_ID
3775 : underlying_face_id (it);
3776 }
3777
3778 new_face_id = face_at_string_position (it->w,
3779 it->string,
3780 IT_STRING_CHARPOS (*it),
3781 bufpos,
3782 it->region_beg_charpos,
3783 it->region_end_charpos,
3784 &next_stop,
3785 base_face_id, 0);
3786
3787 /* Is this a start of a run of characters with box? Caveat:
3788 this can be called for a freshly allocated iterator; face_id
3789 is -1 is this case. We know that the new face will not
3790 change until the next check pos, i.e. if the new face has a
3791 box, all characters up to that position will have a
3792 box. But, as usual, we don't know whether that position
3793 is really the end. */
3794 if (new_face_id != it->face_id)
3795 {
3796 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3797 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3798
3799 /* If new face has a box but old face hasn't, this is the
3800 start of a run of characters with box, i.e. it has a
3801 shadow on the left side. */
3802 it->start_of_box_run_p
3803 = new_face->box && (old_face == NULL || !old_face->box);
3804 it->face_box_p = new_face->box != FACE_NO_BOX;
3805 }
3806 }
3807
3808 it->face_id = new_face_id;
3809 return HANDLED_NORMALLY;
3810 }
3811
3812
3813 /* Return the ID of the face ``underlying'' IT's current position,
3814 which is in a string. If the iterator is associated with a
3815 buffer, return the face at IT's current buffer position.
3816 Otherwise, use the iterator's base_face_id. */
3817
3818 static int
3819 underlying_face_id (struct it *it)
3820 {
3821 int face_id = it->base_face_id, i;
3822
3823 eassert (STRINGP (it->string));
3824
3825 for (i = it->sp - 1; i >= 0; --i)
3826 if (NILP (it->stack[i].string))
3827 face_id = it->stack[i].face_id;
3828
3829 return face_id;
3830 }
3831
3832
3833 /* Compute the face one character before or after the current position
3834 of IT, in the visual order. BEFORE_P non-zero means get the face
3835 in front (to the left in L2R paragraphs, to the right in R2L
3836 paragraphs) of IT's screen position. Value is the ID of the face. */
3837
3838 static int
3839 face_before_or_after_it_pos (struct it *it, int before_p)
3840 {
3841 int face_id, limit;
3842 ptrdiff_t next_check_charpos;
3843 struct it it_copy;
3844 void *it_copy_data = NULL;
3845
3846 eassert (it->s == NULL);
3847
3848 if (STRINGP (it->string))
3849 {
3850 ptrdiff_t bufpos, charpos;
3851 int base_face_id;
3852
3853 /* No face change past the end of the string (for the case
3854 we are padding with spaces). No face change before the
3855 string start. */
3856 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3857 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3858 return it->face_id;
3859
3860 if (!it->bidi_p)
3861 {
3862 /* Set charpos to the position before or after IT's current
3863 position, in the logical order, which in the non-bidi
3864 case is the same as the visual order. */
3865 if (before_p)
3866 charpos = IT_STRING_CHARPOS (*it) - 1;
3867 else if (it->what == IT_COMPOSITION)
3868 /* For composition, we must check the character after the
3869 composition. */
3870 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3871 else
3872 charpos = IT_STRING_CHARPOS (*it) + 1;
3873 }
3874 else
3875 {
3876 if (before_p)
3877 {
3878 /* With bidi iteration, the character before the current
3879 in the visual order cannot be found by simple
3880 iteration, because "reverse" reordering is not
3881 supported. Instead, we need to use the move_it_*
3882 family of functions. */
3883 /* Ignore face changes before the first visible
3884 character on this display line. */
3885 if (it->current_x <= it->first_visible_x)
3886 return it->face_id;
3887 SAVE_IT (it_copy, *it, it_copy_data);
3888 /* Implementation note: Since move_it_in_display_line
3889 works in the iterator geometry, and thinks the first
3890 character is always the leftmost, even in R2L lines,
3891 we don't need to distinguish between the R2L and L2R
3892 cases here. */
3893 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3894 it_copy.current_x - 1, MOVE_TO_X);
3895 charpos = IT_STRING_CHARPOS (it_copy);
3896 RESTORE_IT (it, it, it_copy_data);
3897 }
3898 else
3899 {
3900 /* Set charpos to the string position of the character
3901 that comes after IT's current position in the visual
3902 order. */
3903 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3904
3905 it_copy = *it;
3906 while (n--)
3907 bidi_move_to_visually_next (&it_copy.bidi_it);
3908
3909 charpos = it_copy.bidi_it.charpos;
3910 }
3911 }
3912 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3913
3914 if (it->current.overlay_string_index >= 0)
3915 bufpos = IT_CHARPOS (*it);
3916 else
3917 bufpos = 0;
3918
3919 base_face_id = underlying_face_id (it);
3920
3921 /* Get the face for ASCII, or unibyte. */
3922 face_id = face_at_string_position (it->w,
3923 it->string,
3924 charpos,
3925 bufpos,
3926 it->region_beg_charpos,
3927 it->region_end_charpos,
3928 &next_check_charpos,
3929 base_face_id, 0);
3930
3931 /* Correct the face for charsets different from ASCII. Do it
3932 for the multibyte case only. The face returned above is
3933 suitable for unibyte text if IT->string is unibyte. */
3934 if (STRING_MULTIBYTE (it->string))
3935 {
3936 struct text_pos pos1 = string_pos (charpos, it->string);
3937 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3938 int c, len;
3939 struct face *face = FACE_FROM_ID (it->f, face_id);
3940
3941 c = string_char_and_length (p, &len);
3942 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3943 }
3944 }
3945 else
3946 {
3947 struct text_pos pos;
3948
3949 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3950 || (IT_CHARPOS (*it) <= BEGV && before_p))
3951 return it->face_id;
3952
3953 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3954 pos = it->current.pos;
3955
3956 if (!it->bidi_p)
3957 {
3958 if (before_p)
3959 DEC_TEXT_POS (pos, it->multibyte_p);
3960 else
3961 {
3962 if (it->what == IT_COMPOSITION)
3963 {
3964 /* For composition, we must check the position after
3965 the composition. */
3966 pos.charpos += it->cmp_it.nchars;
3967 pos.bytepos += it->len;
3968 }
3969 else
3970 INC_TEXT_POS (pos, it->multibyte_p);
3971 }
3972 }
3973 else
3974 {
3975 if (before_p)
3976 {
3977 /* With bidi iteration, the character before the current
3978 in the visual order cannot be found by simple
3979 iteration, because "reverse" reordering is not
3980 supported. Instead, we need to use the move_it_*
3981 family of functions. */
3982 /* Ignore face changes before the first visible
3983 character on this display line. */
3984 if (it->current_x <= it->first_visible_x)
3985 return it->face_id;
3986 SAVE_IT (it_copy, *it, it_copy_data);
3987 /* Implementation note: Since move_it_in_display_line
3988 works in the iterator geometry, and thinks the first
3989 character is always the leftmost, even in R2L lines,
3990 we don't need to distinguish between the R2L and L2R
3991 cases here. */
3992 move_it_in_display_line (&it_copy, ZV,
3993 it_copy.current_x - 1, MOVE_TO_X);
3994 pos = it_copy.current.pos;
3995 RESTORE_IT (it, it, it_copy_data);
3996 }
3997 else
3998 {
3999 /* Set charpos to the buffer position of the character
4000 that comes after IT's current position in the visual
4001 order. */
4002 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4003
4004 it_copy = *it;
4005 while (n--)
4006 bidi_move_to_visually_next (&it_copy.bidi_it);
4007
4008 SET_TEXT_POS (pos,
4009 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4010 }
4011 }
4012 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4013
4014 /* Determine face for CHARSET_ASCII, or unibyte. */
4015 face_id = face_at_buffer_position (it->w,
4016 CHARPOS (pos),
4017 it->region_beg_charpos,
4018 it->region_end_charpos,
4019 &next_check_charpos,
4020 limit, 0, -1);
4021
4022 /* Correct the face for charsets different from ASCII. Do it
4023 for the multibyte case only. The face returned above is
4024 suitable for unibyte text if current_buffer is unibyte. */
4025 if (it->multibyte_p)
4026 {
4027 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4028 struct face *face = FACE_FROM_ID (it->f, face_id);
4029 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4030 }
4031 }
4032
4033 return face_id;
4034 }
4035
4036
4037 \f
4038 /***********************************************************************
4039 Invisible text
4040 ***********************************************************************/
4041
4042 /* Set up iterator IT from invisible properties at its current
4043 position. Called from handle_stop. */
4044
4045 static enum prop_handled
4046 handle_invisible_prop (struct it *it)
4047 {
4048 enum prop_handled handled = HANDLED_NORMALLY;
4049
4050 if (STRINGP (it->string))
4051 {
4052 Lisp_Object prop, end_charpos, limit, charpos;
4053
4054 /* Get the value of the invisible text property at the
4055 current position. Value will be nil if there is no such
4056 property. */
4057 charpos = make_number (IT_STRING_CHARPOS (*it));
4058 prop = Fget_text_property (charpos, Qinvisible, it->string);
4059
4060 if (!NILP (prop)
4061 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4062 {
4063 ptrdiff_t endpos;
4064
4065 handled = HANDLED_RECOMPUTE_PROPS;
4066
4067 /* Get the position at which the next change of the
4068 invisible text property can be found in IT->string.
4069 Value will be nil if the property value is the same for
4070 all the rest of IT->string. */
4071 XSETINT (limit, SCHARS (it->string));
4072 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4073 it->string, limit);
4074
4075 /* Text at current position is invisible. The next
4076 change in the property is at position end_charpos.
4077 Move IT's current position to that position. */
4078 if (INTEGERP (end_charpos)
4079 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4080 {
4081 struct text_pos old;
4082 ptrdiff_t oldpos;
4083
4084 old = it->current.string_pos;
4085 oldpos = CHARPOS (old);
4086 if (it->bidi_p)
4087 {
4088 if (it->bidi_it.first_elt
4089 && it->bidi_it.charpos < SCHARS (it->string))
4090 bidi_paragraph_init (it->paragraph_embedding,
4091 &it->bidi_it, 1);
4092 /* Bidi-iterate out of the invisible text. */
4093 do
4094 {
4095 bidi_move_to_visually_next (&it->bidi_it);
4096 }
4097 while (oldpos <= it->bidi_it.charpos
4098 && it->bidi_it.charpos < endpos);
4099
4100 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4101 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4102 if (IT_CHARPOS (*it) >= endpos)
4103 it->prev_stop = endpos;
4104 }
4105 else
4106 {
4107 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4108 compute_string_pos (&it->current.string_pos, old, it->string);
4109 }
4110 }
4111 else
4112 {
4113 /* The rest of the string is invisible. If this is an
4114 overlay string, proceed with the next overlay string
4115 or whatever comes and return a character from there. */
4116 if (it->current.overlay_string_index >= 0)
4117 {
4118 next_overlay_string (it);
4119 /* Don't check for overlay strings when we just
4120 finished processing them. */
4121 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4122 }
4123 else
4124 {
4125 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4126 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4127 }
4128 }
4129 }
4130 }
4131 else
4132 {
4133 int invis_p;
4134 ptrdiff_t newpos, next_stop, start_charpos, tem;
4135 Lisp_Object pos, prop, overlay;
4136
4137 /* First of all, is there invisible text at this position? */
4138 tem = start_charpos = IT_CHARPOS (*it);
4139 pos = make_number (tem);
4140 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4141 &overlay);
4142 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4143
4144 /* If we are on invisible text, skip over it. */
4145 if (invis_p && start_charpos < it->end_charpos)
4146 {
4147 /* Record whether we have to display an ellipsis for the
4148 invisible text. */
4149 int display_ellipsis_p = invis_p == 2;
4150
4151 handled = HANDLED_RECOMPUTE_PROPS;
4152
4153 /* Loop skipping over invisible text. The loop is left at
4154 ZV or with IT on the first char being visible again. */
4155 do
4156 {
4157 /* Try to skip some invisible text. Return value is the
4158 position reached which can be equal to where we start
4159 if there is nothing invisible there. This skips both
4160 over invisible text properties and overlays with
4161 invisible property. */
4162 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4163
4164 /* If we skipped nothing at all we weren't at invisible
4165 text in the first place. If everything to the end of
4166 the buffer was skipped, end the loop. */
4167 if (newpos == tem || newpos >= ZV)
4168 invis_p = 0;
4169 else
4170 {
4171 /* We skipped some characters but not necessarily
4172 all there are. Check if we ended up on visible
4173 text. Fget_char_property returns the property of
4174 the char before the given position, i.e. if we
4175 get invis_p = 0, this means that the char at
4176 newpos is visible. */
4177 pos = make_number (newpos);
4178 prop = Fget_char_property (pos, Qinvisible, it->window);
4179 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4180 }
4181
4182 /* If we ended up on invisible text, proceed to
4183 skip starting with next_stop. */
4184 if (invis_p)
4185 tem = next_stop;
4186
4187 /* If there are adjacent invisible texts, don't lose the
4188 second one's ellipsis. */
4189 if (invis_p == 2)
4190 display_ellipsis_p = 1;
4191 }
4192 while (invis_p);
4193
4194 /* The position newpos is now either ZV or on visible text. */
4195 if (it->bidi_p)
4196 {
4197 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4198 int on_newline =
4199 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4200 int after_newline =
4201 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4202
4203 /* If the invisible text ends on a newline or on a
4204 character after a newline, we can avoid the costly,
4205 character by character, bidi iteration to NEWPOS, and
4206 instead simply reseat the iterator there. That's
4207 because all bidi reordering information is tossed at
4208 the newline. This is a big win for modes that hide
4209 complete lines, like Outline, Org, etc. */
4210 if (on_newline || after_newline)
4211 {
4212 struct text_pos tpos;
4213 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4214
4215 SET_TEXT_POS (tpos, newpos, bpos);
4216 reseat_1 (it, tpos, 0);
4217 /* If we reseat on a newline/ZV, we need to prep the
4218 bidi iterator for advancing to the next character
4219 after the newline/EOB, keeping the current paragraph
4220 direction (so that PRODUCE_GLYPHS does TRT wrt
4221 prepending/appending glyphs to a glyph row). */
4222 if (on_newline)
4223 {
4224 it->bidi_it.first_elt = 0;
4225 it->bidi_it.paragraph_dir = pdir;
4226 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4227 it->bidi_it.nchars = 1;
4228 it->bidi_it.ch_len = 1;
4229 }
4230 }
4231 else /* Must use the slow method. */
4232 {
4233 /* With bidi iteration, the region of invisible text
4234 could start and/or end in the middle of a
4235 non-base embedding level. Therefore, we need to
4236 skip invisible text using the bidi iterator,
4237 starting at IT's current position, until we find
4238 ourselves outside of the invisible text.
4239 Skipping invisible text _after_ bidi iteration
4240 avoids affecting the visual order of the
4241 displayed text when invisible properties are
4242 added or removed. */
4243 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4244 {
4245 /* If we were `reseat'ed to a new paragraph,
4246 determine the paragraph base direction. We
4247 need to do it now because
4248 next_element_from_buffer may not have a
4249 chance to do it, if we are going to skip any
4250 text at the beginning, which resets the
4251 FIRST_ELT flag. */
4252 bidi_paragraph_init (it->paragraph_embedding,
4253 &it->bidi_it, 1);
4254 }
4255 do
4256 {
4257 bidi_move_to_visually_next (&it->bidi_it);
4258 }
4259 while (it->stop_charpos <= it->bidi_it.charpos
4260 && it->bidi_it.charpos < newpos);
4261 IT_CHARPOS (*it) = it->bidi_it.charpos;
4262 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4263 /* If we overstepped NEWPOS, record its position in
4264 the iterator, so that we skip invisible text if
4265 later the bidi iteration lands us in the
4266 invisible region again. */
4267 if (IT_CHARPOS (*it) >= newpos)
4268 it->prev_stop = newpos;
4269 }
4270 }
4271 else
4272 {
4273 IT_CHARPOS (*it) = newpos;
4274 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4275 }
4276
4277 /* If there are before-strings at the start of invisible
4278 text, and the text is invisible because of a text
4279 property, arrange to show before-strings because 20.x did
4280 it that way. (If the text is invisible because of an
4281 overlay property instead of a text property, this is
4282 already handled in the overlay code.) */
4283 if (NILP (overlay)
4284 && get_overlay_strings (it, it->stop_charpos))
4285 {
4286 handled = HANDLED_RECOMPUTE_PROPS;
4287 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4288 }
4289 else if (display_ellipsis_p)
4290 {
4291 /* Make sure that the glyphs of the ellipsis will get
4292 correct `charpos' values. If we would not update
4293 it->position here, the glyphs would belong to the
4294 last visible character _before_ the invisible
4295 text, which confuses `set_cursor_from_row'.
4296
4297 We use the last invisible position instead of the
4298 first because this way the cursor is always drawn on
4299 the first "." of the ellipsis, whenever PT is inside
4300 the invisible text. Otherwise the cursor would be
4301 placed _after_ the ellipsis when the point is after the
4302 first invisible character. */
4303 if (!STRINGP (it->object))
4304 {
4305 it->position.charpos = newpos - 1;
4306 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4307 }
4308 it->ellipsis_p = 1;
4309 /* Let the ellipsis display before
4310 considering any properties of the following char.
4311 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4312 handled = HANDLED_RETURN;
4313 }
4314 }
4315 }
4316
4317 return handled;
4318 }
4319
4320
4321 /* Make iterator IT return `...' next.
4322 Replaces LEN characters from buffer. */
4323
4324 static void
4325 setup_for_ellipsis (struct it *it, int len)
4326 {
4327 /* Use the display table definition for `...'. Invalid glyphs
4328 will be handled by the method returning elements from dpvec. */
4329 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4330 {
4331 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4332 it->dpvec = v->contents;
4333 it->dpend = v->contents + v->header.size;
4334 }
4335 else
4336 {
4337 /* Default `...'. */
4338 it->dpvec = default_invis_vector;
4339 it->dpend = default_invis_vector + 3;
4340 }
4341
4342 it->dpvec_char_len = len;
4343 it->current.dpvec_index = 0;
4344 it->dpvec_face_id = -1;
4345
4346 /* Remember the current face id in case glyphs specify faces.
4347 IT's face is restored in set_iterator_to_next.
4348 saved_face_id was set to preceding char's face in handle_stop. */
4349 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4350 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4351
4352 it->method = GET_FROM_DISPLAY_VECTOR;
4353 it->ellipsis_p = 1;
4354 }
4355
4356
4357 \f
4358 /***********************************************************************
4359 'display' property
4360 ***********************************************************************/
4361
4362 /* Set up iterator IT from `display' property at its current position.
4363 Called from handle_stop.
4364 We return HANDLED_RETURN if some part of the display property
4365 overrides the display of the buffer text itself.
4366 Otherwise we return HANDLED_NORMALLY. */
4367
4368 static enum prop_handled
4369 handle_display_prop (struct it *it)
4370 {
4371 Lisp_Object propval, object, overlay;
4372 struct text_pos *position;
4373 ptrdiff_t bufpos;
4374 /* Nonzero if some property replaces the display of the text itself. */
4375 int display_replaced_p = 0;
4376
4377 if (STRINGP (it->string))
4378 {
4379 object = it->string;
4380 position = &it->current.string_pos;
4381 bufpos = CHARPOS (it->current.pos);
4382 }
4383 else
4384 {
4385 XSETWINDOW (object, it->w);
4386 position = &it->current.pos;
4387 bufpos = CHARPOS (*position);
4388 }
4389
4390 /* Reset those iterator values set from display property values. */
4391 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4392 it->space_width = Qnil;
4393 it->font_height = Qnil;
4394 it->voffset = 0;
4395
4396 /* We don't support recursive `display' properties, i.e. string
4397 values that have a string `display' property, that have a string
4398 `display' property etc. */
4399 if (!it->string_from_display_prop_p)
4400 it->area = TEXT_AREA;
4401
4402 propval = get_char_property_and_overlay (make_number (position->charpos),
4403 Qdisplay, object, &overlay);
4404 if (NILP (propval))
4405 return HANDLED_NORMALLY;
4406 /* Now OVERLAY is the overlay that gave us this property, or nil
4407 if it was a text property. */
4408
4409 if (!STRINGP (it->string))
4410 object = it->w->buffer;
4411
4412 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4413 position, bufpos,
4414 FRAME_WINDOW_P (it->f));
4415
4416 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4417 }
4418
4419 /* Subroutine of handle_display_prop. Returns non-zero if the display
4420 specification in SPEC is a replacing specification, i.e. it would
4421 replace the text covered by `display' property with something else,
4422 such as an image or a display string. If SPEC includes any kind or
4423 `(space ...) specification, the value is 2; this is used by
4424 compute_display_string_pos, which see.
4425
4426 See handle_single_display_spec for documentation of arguments.
4427 frame_window_p is non-zero if the window being redisplayed is on a
4428 GUI frame; this argument is used only if IT is NULL, see below.
4429
4430 IT can be NULL, if this is called by the bidi reordering code
4431 through compute_display_string_pos, which see. In that case, this
4432 function only examines SPEC, but does not otherwise "handle" it, in
4433 the sense that it doesn't set up members of IT from the display
4434 spec. */
4435 static int
4436 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4437 Lisp_Object overlay, struct text_pos *position,
4438 ptrdiff_t bufpos, int frame_window_p)
4439 {
4440 int replacing_p = 0;
4441 int rv;
4442
4443 if (CONSP (spec)
4444 /* Simple specifications. */
4445 && !EQ (XCAR (spec), Qimage)
4446 && !EQ (XCAR (spec), Qspace)
4447 && !EQ (XCAR (spec), Qwhen)
4448 && !EQ (XCAR (spec), Qslice)
4449 && !EQ (XCAR (spec), Qspace_width)
4450 && !EQ (XCAR (spec), Qheight)
4451 && !EQ (XCAR (spec), Qraise)
4452 /* Marginal area specifications. */
4453 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4454 && !EQ (XCAR (spec), Qleft_fringe)
4455 && !EQ (XCAR (spec), Qright_fringe)
4456 && !NILP (XCAR (spec)))
4457 {
4458 for (; CONSP (spec); spec = XCDR (spec))
4459 {
4460 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4461 overlay, position, bufpos,
4462 replacing_p, frame_window_p)))
4463 {
4464 replacing_p = rv;
4465 /* If some text in a string is replaced, `position' no
4466 longer points to the position of `object'. */
4467 if (!it || STRINGP (object))
4468 break;
4469 }
4470 }
4471 }
4472 else if (VECTORP (spec))
4473 {
4474 ptrdiff_t i;
4475 for (i = 0; i < ASIZE (spec); ++i)
4476 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4477 overlay, position, bufpos,
4478 replacing_p, frame_window_p)))
4479 {
4480 replacing_p = rv;
4481 /* If some text in a string is replaced, `position' no
4482 longer points to the position of `object'. */
4483 if (!it || STRINGP (object))
4484 break;
4485 }
4486 }
4487 else
4488 {
4489 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4490 position, bufpos, 0,
4491 frame_window_p)))
4492 replacing_p = rv;
4493 }
4494
4495 return replacing_p;
4496 }
4497
4498 /* Value is the position of the end of the `display' property starting
4499 at START_POS in OBJECT. */
4500
4501 static struct text_pos
4502 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4503 {
4504 Lisp_Object end;
4505 struct text_pos end_pos;
4506
4507 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4508 Qdisplay, object, Qnil);
4509 CHARPOS (end_pos) = XFASTINT (end);
4510 if (STRINGP (object))
4511 compute_string_pos (&end_pos, start_pos, it->string);
4512 else
4513 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4514
4515 return end_pos;
4516 }
4517
4518
4519 /* Set up IT from a single `display' property specification SPEC. OBJECT
4520 is the object in which the `display' property was found. *POSITION
4521 is the position in OBJECT at which the `display' property was found.
4522 BUFPOS is the buffer position of OBJECT (different from POSITION if
4523 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4524 previously saw a display specification which already replaced text
4525 display with something else, for example an image; we ignore such
4526 properties after the first one has been processed.
4527
4528 OVERLAY is the overlay this `display' property came from,
4529 or nil if it was a text property.
4530
4531 If SPEC is a `space' or `image' specification, and in some other
4532 cases too, set *POSITION to the position where the `display'
4533 property ends.
4534
4535 If IT is NULL, only examine the property specification in SPEC, but
4536 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4537 is intended to be displayed in a window on a GUI frame.
4538
4539 Value is non-zero if something was found which replaces the display
4540 of buffer or string text. */
4541
4542 static int
4543 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4544 Lisp_Object overlay, struct text_pos *position,
4545 ptrdiff_t bufpos, int display_replaced_p,
4546 int frame_window_p)
4547 {
4548 Lisp_Object form;
4549 Lisp_Object location, value;
4550 struct text_pos start_pos = *position;
4551 int valid_p;
4552
4553 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4554 If the result is non-nil, use VALUE instead of SPEC. */
4555 form = Qt;
4556 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4557 {
4558 spec = XCDR (spec);
4559 if (!CONSP (spec))
4560 return 0;
4561 form = XCAR (spec);
4562 spec = XCDR (spec);
4563 }
4564
4565 if (!NILP (form) && !EQ (form, Qt))
4566 {
4567 ptrdiff_t count = SPECPDL_INDEX ();
4568 struct gcpro gcpro1;
4569
4570 /* Bind `object' to the object having the `display' property, a
4571 buffer or string. Bind `position' to the position in the
4572 object where the property was found, and `buffer-position'
4573 to the current position in the buffer. */
4574
4575 if (NILP (object))
4576 XSETBUFFER (object, current_buffer);
4577 specbind (Qobject, object);
4578 specbind (Qposition, make_number (CHARPOS (*position)));
4579 specbind (Qbuffer_position, make_number (bufpos));
4580 GCPRO1 (form);
4581 form = safe_eval (form);
4582 UNGCPRO;
4583 unbind_to (count, Qnil);
4584 }
4585
4586 if (NILP (form))
4587 return 0;
4588
4589 /* Handle `(height HEIGHT)' specifications. */
4590 if (CONSP (spec)
4591 && EQ (XCAR (spec), Qheight)
4592 && CONSP (XCDR (spec)))
4593 {
4594 if (it)
4595 {
4596 if (!FRAME_WINDOW_P (it->f))
4597 return 0;
4598
4599 it->font_height = XCAR (XCDR (spec));
4600 if (!NILP (it->font_height))
4601 {
4602 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4603 int new_height = -1;
4604
4605 if (CONSP (it->font_height)
4606 && (EQ (XCAR (it->font_height), Qplus)
4607 || EQ (XCAR (it->font_height), Qminus))
4608 && CONSP (XCDR (it->font_height))
4609 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4610 {
4611 /* `(+ N)' or `(- N)' where N is an integer. */
4612 int steps = XINT (XCAR (XCDR (it->font_height)));
4613 if (EQ (XCAR (it->font_height), Qplus))
4614 steps = - steps;
4615 it->face_id = smaller_face (it->f, it->face_id, steps);
4616 }
4617 else if (FUNCTIONP (it->font_height))
4618 {
4619 /* Call function with current height as argument.
4620 Value is the new height. */
4621 Lisp_Object height;
4622 height = safe_call1 (it->font_height,
4623 face->lface[LFACE_HEIGHT_INDEX]);
4624 if (NUMBERP (height))
4625 new_height = XFLOATINT (height);
4626 }
4627 else if (NUMBERP (it->font_height))
4628 {
4629 /* Value is a multiple of the canonical char height. */
4630 struct face *f;
4631
4632 f = FACE_FROM_ID (it->f,
4633 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4634 new_height = (XFLOATINT (it->font_height)
4635 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4636 }
4637 else
4638 {
4639 /* Evaluate IT->font_height with `height' bound to the
4640 current specified height to get the new height. */
4641 ptrdiff_t count = SPECPDL_INDEX ();
4642
4643 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4644 value = safe_eval (it->font_height);
4645 unbind_to (count, Qnil);
4646
4647 if (NUMBERP (value))
4648 new_height = XFLOATINT (value);
4649 }
4650
4651 if (new_height > 0)
4652 it->face_id = face_with_height (it->f, it->face_id, new_height);
4653 }
4654 }
4655
4656 return 0;
4657 }
4658
4659 /* Handle `(space-width WIDTH)'. */
4660 if (CONSP (spec)
4661 && EQ (XCAR (spec), Qspace_width)
4662 && CONSP (XCDR (spec)))
4663 {
4664 if (it)
4665 {
4666 if (!FRAME_WINDOW_P (it->f))
4667 return 0;
4668
4669 value = XCAR (XCDR (spec));
4670 if (NUMBERP (value) && XFLOATINT (value) > 0)
4671 it->space_width = value;
4672 }
4673
4674 return 0;
4675 }
4676
4677 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4678 if (CONSP (spec)
4679 && EQ (XCAR (spec), Qslice))
4680 {
4681 Lisp_Object tem;
4682
4683 if (it)
4684 {
4685 if (!FRAME_WINDOW_P (it->f))
4686 return 0;
4687
4688 if (tem = XCDR (spec), CONSP (tem))
4689 {
4690 it->slice.x = XCAR (tem);
4691 if (tem = XCDR (tem), CONSP (tem))
4692 {
4693 it->slice.y = XCAR (tem);
4694 if (tem = XCDR (tem), CONSP (tem))
4695 {
4696 it->slice.width = XCAR (tem);
4697 if (tem = XCDR (tem), CONSP (tem))
4698 it->slice.height = XCAR (tem);
4699 }
4700 }
4701 }
4702 }
4703
4704 return 0;
4705 }
4706
4707 /* Handle `(raise FACTOR)'. */
4708 if (CONSP (spec)
4709 && EQ (XCAR (spec), Qraise)
4710 && CONSP (XCDR (spec)))
4711 {
4712 if (it)
4713 {
4714 if (!FRAME_WINDOW_P (it->f))
4715 return 0;
4716
4717 #ifdef HAVE_WINDOW_SYSTEM
4718 value = XCAR (XCDR (spec));
4719 if (NUMBERP (value))
4720 {
4721 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4722 it->voffset = - (XFLOATINT (value)
4723 * (FONT_HEIGHT (face->font)));
4724 }
4725 #endif /* HAVE_WINDOW_SYSTEM */
4726 }
4727
4728 return 0;
4729 }
4730
4731 /* Don't handle the other kinds of display specifications
4732 inside a string that we got from a `display' property. */
4733 if (it && it->string_from_display_prop_p)
4734 return 0;
4735
4736 /* Characters having this form of property are not displayed, so
4737 we have to find the end of the property. */
4738 if (it)
4739 {
4740 start_pos = *position;
4741 *position = display_prop_end (it, object, start_pos);
4742 }
4743 value = Qnil;
4744
4745 /* Stop the scan at that end position--we assume that all
4746 text properties change there. */
4747 if (it)
4748 it->stop_charpos = position->charpos;
4749
4750 /* Handle `(left-fringe BITMAP [FACE])'
4751 and `(right-fringe BITMAP [FACE])'. */
4752 if (CONSP (spec)
4753 && (EQ (XCAR (spec), Qleft_fringe)
4754 || EQ (XCAR (spec), Qright_fringe))
4755 && CONSP (XCDR (spec)))
4756 {
4757 int fringe_bitmap;
4758
4759 if (it)
4760 {
4761 if (!FRAME_WINDOW_P (it->f))
4762 /* If we return here, POSITION has been advanced
4763 across the text with this property. */
4764 {
4765 /* Synchronize the bidi iterator with POSITION. This is
4766 needed because we are not going to push the iterator
4767 on behalf of this display property, so there will be
4768 no pop_it call to do this synchronization for us. */
4769 if (it->bidi_p)
4770 {
4771 it->position = *position;
4772 iterate_out_of_display_property (it);
4773 *position = it->position;
4774 }
4775 return 1;
4776 }
4777 }
4778 else if (!frame_window_p)
4779 return 1;
4780
4781 #ifdef HAVE_WINDOW_SYSTEM
4782 value = XCAR (XCDR (spec));
4783 if (!SYMBOLP (value)
4784 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4785 /* If we return here, POSITION has been advanced
4786 across the text with this property. */
4787 {
4788 if (it && it->bidi_p)
4789 {
4790 it->position = *position;
4791 iterate_out_of_display_property (it);
4792 *position = it->position;
4793 }
4794 return 1;
4795 }
4796
4797 if (it)
4798 {
4799 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4800
4801 if (CONSP (XCDR (XCDR (spec))))
4802 {
4803 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4804 int face_id2 = lookup_derived_face (it->f, face_name,
4805 FRINGE_FACE_ID, 0);
4806 if (face_id2 >= 0)
4807 face_id = face_id2;
4808 }
4809
4810 /* Save current settings of IT so that we can restore them
4811 when we are finished with the glyph property value. */
4812 push_it (it, position);
4813
4814 it->area = TEXT_AREA;
4815 it->what = IT_IMAGE;
4816 it->image_id = -1; /* no image */
4817 it->position = start_pos;
4818 it->object = NILP (object) ? it->w->buffer : object;
4819 it->method = GET_FROM_IMAGE;
4820 it->from_overlay = Qnil;
4821 it->face_id = face_id;
4822 it->from_disp_prop_p = 1;
4823
4824 /* Say that we haven't consumed the characters with
4825 `display' property yet. The call to pop_it in
4826 set_iterator_to_next will clean this up. */
4827 *position = start_pos;
4828
4829 if (EQ (XCAR (spec), Qleft_fringe))
4830 {
4831 it->left_user_fringe_bitmap = fringe_bitmap;
4832 it->left_user_fringe_face_id = face_id;
4833 }
4834 else
4835 {
4836 it->right_user_fringe_bitmap = fringe_bitmap;
4837 it->right_user_fringe_face_id = face_id;
4838 }
4839 }
4840 #endif /* HAVE_WINDOW_SYSTEM */
4841 return 1;
4842 }
4843
4844 /* Prepare to handle `((margin left-margin) ...)',
4845 `((margin right-margin) ...)' and `((margin nil) ...)'
4846 prefixes for display specifications. */
4847 location = Qunbound;
4848 if (CONSP (spec) && CONSP (XCAR (spec)))
4849 {
4850 Lisp_Object tem;
4851
4852 value = XCDR (spec);
4853 if (CONSP (value))
4854 value = XCAR (value);
4855
4856 tem = XCAR (spec);
4857 if (EQ (XCAR (tem), Qmargin)
4858 && (tem = XCDR (tem),
4859 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4860 (NILP (tem)
4861 || EQ (tem, Qleft_margin)
4862 || EQ (tem, Qright_margin))))
4863 location = tem;
4864 }
4865
4866 if (EQ (location, Qunbound))
4867 {
4868 location = Qnil;
4869 value = spec;
4870 }
4871
4872 /* After this point, VALUE is the property after any
4873 margin prefix has been stripped. It must be a string,
4874 an image specification, or `(space ...)'.
4875
4876 LOCATION specifies where to display: `left-margin',
4877 `right-margin' or nil. */
4878
4879 valid_p = (STRINGP (value)
4880 #ifdef HAVE_WINDOW_SYSTEM
4881 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4882 && valid_image_p (value))
4883 #endif /* not HAVE_WINDOW_SYSTEM */
4884 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4885
4886 if (valid_p && !display_replaced_p)
4887 {
4888 int retval = 1;
4889
4890 if (!it)
4891 {
4892 /* Callers need to know whether the display spec is any kind
4893 of `(space ...)' spec that is about to affect text-area
4894 display. */
4895 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4896 retval = 2;
4897 return retval;
4898 }
4899
4900 /* Save current settings of IT so that we can restore them
4901 when we are finished with the glyph property value. */
4902 push_it (it, position);
4903 it->from_overlay = overlay;
4904 it->from_disp_prop_p = 1;
4905
4906 if (NILP (location))
4907 it->area = TEXT_AREA;
4908 else if (EQ (location, Qleft_margin))
4909 it->area = LEFT_MARGIN_AREA;
4910 else
4911 it->area = RIGHT_MARGIN_AREA;
4912
4913 if (STRINGP (value))
4914 {
4915 it->string = value;
4916 it->multibyte_p = STRING_MULTIBYTE (it->string);
4917 it->current.overlay_string_index = -1;
4918 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4919 it->end_charpos = it->string_nchars = SCHARS (it->string);
4920 it->method = GET_FROM_STRING;
4921 it->stop_charpos = 0;
4922 it->prev_stop = 0;
4923 it->base_level_stop = 0;
4924 it->string_from_display_prop_p = 1;
4925 /* Say that we haven't consumed the characters with
4926 `display' property yet. The call to pop_it in
4927 set_iterator_to_next will clean this up. */
4928 if (BUFFERP (object))
4929 *position = start_pos;
4930
4931 /* Force paragraph direction to be that of the parent
4932 object. If the parent object's paragraph direction is
4933 not yet determined, default to L2R. */
4934 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4935 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4936 else
4937 it->paragraph_embedding = L2R;
4938
4939 /* Set up the bidi iterator for this display string. */
4940 if (it->bidi_p)
4941 {
4942 it->bidi_it.string.lstring = it->string;
4943 it->bidi_it.string.s = NULL;
4944 it->bidi_it.string.schars = it->end_charpos;
4945 it->bidi_it.string.bufpos = bufpos;
4946 it->bidi_it.string.from_disp_str = 1;
4947 it->bidi_it.string.unibyte = !it->multibyte_p;
4948 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4949 }
4950 }
4951 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4952 {
4953 it->method = GET_FROM_STRETCH;
4954 it->object = value;
4955 *position = it->position = start_pos;
4956 retval = 1 + (it->area == TEXT_AREA);
4957 }
4958 #ifdef HAVE_WINDOW_SYSTEM
4959 else
4960 {
4961 it->what = IT_IMAGE;
4962 it->image_id = lookup_image (it->f, value);
4963 it->position = start_pos;
4964 it->object = NILP (object) ? it->w->buffer : object;
4965 it->method = GET_FROM_IMAGE;
4966
4967 /* Say that we haven't consumed the characters with
4968 `display' property yet. The call to pop_it in
4969 set_iterator_to_next will clean this up. */
4970 *position = start_pos;
4971 }
4972 #endif /* HAVE_WINDOW_SYSTEM */
4973
4974 return retval;
4975 }
4976
4977 /* Invalid property or property not supported. Restore
4978 POSITION to what it was before. */
4979 *position = start_pos;
4980 return 0;
4981 }
4982
4983 /* Check if PROP is a display property value whose text should be
4984 treated as intangible. OVERLAY is the overlay from which PROP
4985 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4986 specify the buffer position covered by PROP. */
4987
4988 int
4989 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4990 ptrdiff_t charpos, ptrdiff_t bytepos)
4991 {
4992 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4993 struct text_pos position;
4994
4995 SET_TEXT_POS (position, charpos, bytepos);
4996 return handle_display_spec (NULL, prop, Qnil, overlay,
4997 &position, charpos, frame_window_p);
4998 }
4999
5000
5001 /* Return 1 if PROP is a display sub-property value containing STRING.
5002
5003 Implementation note: this and the following function are really
5004 special cases of handle_display_spec and
5005 handle_single_display_spec, and should ideally use the same code.
5006 Until they do, these two pairs must be consistent and must be
5007 modified in sync. */
5008
5009 static int
5010 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5011 {
5012 if (EQ (string, prop))
5013 return 1;
5014
5015 /* Skip over `when FORM'. */
5016 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5017 {
5018 prop = XCDR (prop);
5019 if (!CONSP (prop))
5020 return 0;
5021 /* Actually, the condition following `when' should be eval'ed,
5022 like handle_single_display_spec does, and we should return
5023 zero if it evaluates to nil. However, this function is
5024 called only when the buffer was already displayed and some
5025 glyph in the glyph matrix was found to come from a display
5026 string. Therefore, the condition was already evaluated, and
5027 the result was non-nil, otherwise the display string wouldn't
5028 have been displayed and we would have never been called for
5029 this property. Thus, we can skip the evaluation and assume
5030 its result is non-nil. */
5031 prop = XCDR (prop);
5032 }
5033
5034 if (CONSP (prop))
5035 /* Skip over `margin LOCATION'. */
5036 if (EQ (XCAR (prop), Qmargin))
5037 {
5038 prop = XCDR (prop);
5039 if (!CONSP (prop))
5040 return 0;
5041
5042 prop = XCDR (prop);
5043 if (!CONSP (prop))
5044 return 0;
5045 }
5046
5047 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5048 }
5049
5050
5051 /* Return 1 if STRING appears in the `display' property PROP. */
5052
5053 static int
5054 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5055 {
5056 if (CONSP (prop)
5057 && !EQ (XCAR (prop), Qwhen)
5058 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5059 {
5060 /* A list of sub-properties. */
5061 while (CONSP (prop))
5062 {
5063 if (single_display_spec_string_p (XCAR (prop), string))
5064 return 1;
5065 prop = XCDR (prop);
5066 }
5067 }
5068 else if (VECTORP (prop))
5069 {
5070 /* A vector of sub-properties. */
5071 ptrdiff_t i;
5072 for (i = 0; i < ASIZE (prop); ++i)
5073 if (single_display_spec_string_p (AREF (prop, i), string))
5074 return 1;
5075 }
5076 else
5077 return single_display_spec_string_p (prop, string);
5078
5079 return 0;
5080 }
5081
5082 /* Look for STRING in overlays and text properties in the current
5083 buffer, between character positions FROM and TO (excluding TO).
5084 BACK_P non-zero means look back (in this case, TO is supposed to be
5085 less than FROM).
5086 Value is the first character position where STRING was found, or
5087 zero if it wasn't found before hitting TO.
5088
5089 This function may only use code that doesn't eval because it is
5090 called asynchronously from note_mouse_highlight. */
5091
5092 static ptrdiff_t
5093 string_buffer_position_lim (Lisp_Object string,
5094 ptrdiff_t from, ptrdiff_t to, int back_p)
5095 {
5096 Lisp_Object limit, prop, pos;
5097 int found = 0;
5098
5099 pos = make_number (max (from, BEGV));
5100
5101 if (!back_p) /* looking forward */
5102 {
5103 limit = make_number (min (to, ZV));
5104 while (!found && !EQ (pos, limit))
5105 {
5106 prop = Fget_char_property (pos, Qdisplay, Qnil);
5107 if (!NILP (prop) && display_prop_string_p (prop, string))
5108 found = 1;
5109 else
5110 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5111 limit);
5112 }
5113 }
5114 else /* looking back */
5115 {
5116 limit = make_number (max (to, BEGV));
5117 while (!found && !EQ (pos, limit))
5118 {
5119 prop = Fget_char_property (pos, Qdisplay, Qnil);
5120 if (!NILP (prop) && display_prop_string_p (prop, string))
5121 found = 1;
5122 else
5123 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5124 limit);
5125 }
5126 }
5127
5128 return found ? XINT (pos) : 0;
5129 }
5130
5131 /* Determine which buffer position in current buffer STRING comes from.
5132 AROUND_CHARPOS is an approximate position where it could come from.
5133 Value is the buffer position or 0 if it couldn't be determined.
5134
5135 This function is necessary because we don't record buffer positions
5136 in glyphs generated from strings (to keep struct glyph small).
5137 This function may only use code that doesn't eval because it is
5138 called asynchronously from note_mouse_highlight. */
5139
5140 static ptrdiff_t
5141 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5142 {
5143 const int MAX_DISTANCE = 1000;
5144 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5145 around_charpos + MAX_DISTANCE,
5146 0);
5147
5148 if (!found)
5149 found = string_buffer_position_lim (string, around_charpos,
5150 around_charpos - MAX_DISTANCE, 1);
5151 return found;
5152 }
5153
5154
5155 \f
5156 /***********************************************************************
5157 `composition' property
5158 ***********************************************************************/
5159
5160 /* Set up iterator IT from `composition' property at its current
5161 position. Called from handle_stop. */
5162
5163 static enum prop_handled
5164 handle_composition_prop (struct it *it)
5165 {
5166 Lisp_Object prop, string;
5167 ptrdiff_t pos, pos_byte, start, end;
5168
5169 if (STRINGP (it->string))
5170 {
5171 unsigned char *s;
5172
5173 pos = IT_STRING_CHARPOS (*it);
5174 pos_byte = IT_STRING_BYTEPOS (*it);
5175 string = it->string;
5176 s = SDATA (string) + pos_byte;
5177 it->c = STRING_CHAR (s);
5178 }
5179 else
5180 {
5181 pos = IT_CHARPOS (*it);
5182 pos_byte = IT_BYTEPOS (*it);
5183 string = Qnil;
5184 it->c = FETCH_CHAR (pos_byte);
5185 }
5186
5187 /* If there's a valid composition and point is not inside of the
5188 composition (in the case that the composition is from the current
5189 buffer), draw a glyph composed from the composition components. */
5190 if (find_composition (pos, -1, &start, &end, &prop, string)
5191 && COMPOSITION_VALID_P (start, end, prop)
5192 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5193 {
5194 if (start < pos)
5195 /* As we can't handle this situation (perhaps font-lock added
5196 a new composition), we just return here hoping that next
5197 redisplay will detect this composition much earlier. */
5198 return HANDLED_NORMALLY;
5199 if (start != pos)
5200 {
5201 if (STRINGP (it->string))
5202 pos_byte = string_char_to_byte (it->string, start);
5203 else
5204 pos_byte = CHAR_TO_BYTE (start);
5205 }
5206 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5207 prop, string);
5208
5209 if (it->cmp_it.id >= 0)
5210 {
5211 it->cmp_it.ch = -1;
5212 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5213 it->cmp_it.nglyphs = -1;
5214 }
5215 }
5216
5217 return HANDLED_NORMALLY;
5218 }
5219
5220
5221 \f
5222 /***********************************************************************
5223 Overlay strings
5224 ***********************************************************************/
5225
5226 /* The following structure is used to record overlay strings for
5227 later sorting in load_overlay_strings. */
5228
5229 struct overlay_entry
5230 {
5231 Lisp_Object overlay;
5232 Lisp_Object string;
5233 EMACS_INT priority;
5234 int after_string_p;
5235 };
5236
5237
5238 /* Set up iterator IT from overlay strings at its current position.
5239 Called from handle_stop. */
5240
5241 static enum prop_handled
5242 handle_overlay_change (struct it *it)
5243 {
5244 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5245 return HANDLED_RECOMPUTE_PROPS;
5246 else
5247 return HANDLED_NORMALLY;
5248 }
5249
5250
5251 /* Set up the next overlay string for delivery by IT, if there is an
5252 overlay string to deliver. Called by set_iterator_to_next when the
5253 end of the current overlay string is reached. If there are more
5254 overlay strings to display, IT->string and
5255 IT->current.overlay_string_index are set appropriately here.
5256 Otherwise IT->string is set to nil. */
5257
5258 static void
5259 next_overlay_string (struct it *it)
5260 {
5261 ++it->current.overlay_string_index;
5262 if (it->current.overlay_string_index == it->n_overlay_strings)
5263 {
5264 /* No more overlay strings. Restore IT's settings to what
5265 they were before overlay strings were processed, and
5266 continue to deliver from current_buffer. */
5267
5268 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5269 pop_it (it);
5270 eassert (it->sp > 0
5271 || (NILP (it->string)
5272 && it->method == GET_FROM_BUFFER
5273 && it->stop_charpos >= BEGV
5274 && it->stop_charpos <= it->end_charpos));
5275 it->current.overlay_string_index = -1;
5276 it->n_overlay_strings = 0;
5277 it->overlay_strings_charpos = -1;
5278 /* If there's an empty display string on the stack, pop the
5279 stack, to resync the bidi iterator with IT's position. Such
5280 empty strings are pushed onto the stack in
5281 get_overlay_strings_1. */
5282 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5283 pop_it (it);
5284
5285 /* If we're at the end of the buffer, record that we have
5286 processed the overlay strings there already, so that
5287 next_element_from_buffer doesn't try it again. */
5288 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5289 it->overlay_strings_at_end_processed_p = 1;
5290 }
5291 else
5292 {
5293 /* There are more overlay strings to process. If
5294 IT->current.overlay_string_index has advanced to a position
5295 where we must load IT->overlay_strings with more strings, do
5296 it. We must load at the IT->overlay_strings_charpos where
5297 IT->n_overlay_strings was originally computed; when invisible
5298 text is present, this might not be IT_CHARPOS (Bug#7016). */
5299 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5300
5301 if (it->current.overlay_string_index && i == 0)
5302 load_overlay_strings (it, it->overlay_strings_charpos);
5303
5304 /* Initialize IT to deliver display elements from the overlay
5305 string. */
5306 it->string = it->overlay_strings[i];
5307 it->multibyte_p = STRING_MULTIBYTE (it->string);
5308 SET_TEXT_POS (it->current.string_pos, 0, 0);
5309 it->method = GET_FROM_STRING;
5310 it->stop_charpos = 0;
5311 if (it->cmp_it.stop_pos >= 0)
5312 it->cmp_it.stop_pos = 0;
5313 it->prev_stop = 0;
5314 it->base_level_stop = 0;
5315
5316 /* Set up the bidi iterator for this overlay string. */
5317 if (it->bidi_p)
5318 {
5319 it->bidi_it.string.lstring = it->string;
5320 it->bidi_it.string.s = NULL;
5321 it->bidi_it.string.schars = SCHARS (it->string);
5322 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5323 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5324 it->bidi_it.string.unibyte = !it->multibyte_p;
5325 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5326 }
5327 }
5328
5329 CHECK_IT (it);
5330 }
5331
5332
5333 /* Compare two overlay_entry structures E1 and E2. Used as a
5334 comparison function for qsort in load_overlay_strings. Overlay
5335 strings for the same position are sorted so that
5336
5337 1. All after-strings come in front of before-strings, except
5338 when they come from the same overlay.
5339
5340 2. Within after-strings, strings are sorted so that overlay strings
5341 from overlays with higher priorities come first.
5342
5343 2. Within before-strings, strings are sorted so that overlay
5344 strings from overlays with higher priorities come last.
5345
5346 Value is analogous to strcmp. */
5347
5348
5349 static int
5350 compare_overlay_entries (const void *e1, const void *e2)
5351 {
5352 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5353 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5354 int result;
5355
5356 if (entry1->after_string_p != entry2->after_string_p)
5357 {
5358 /* Let after-strings appear in front of before-strings if
5359 they come from different overlays. */
5360 if (EQ (entry1->overlay, entry2->overlay))
5361 result = entry1->after_string_p ? 1 : -1;
5362 else
5363 result = entry1->after_string_p ? -1 : 1;
5364 }
5365 else if (entry1->priority != entry2->priority)
5366 {
5367 if (entry1->after_string_p)
5368 /* After-strings sorted in order of decreasing priority. */
5369 result = entry2->priority < entry1->priority ? -1 : 1;
5370 else
5371 /* Before-strings sorted in order of increasing priority. */
5372 result = entry1->priority < entry2->priority ? -1 : 1;
5373 }
5374 else
5375 result = 0;
5376
5377 return result;
5378 }
5379
5380
5381 /* Load the vector IT->overlay_strings with overlay strings from IT's
5382 current buffer position, or from CHARPOS if that is > 0. Set
5383 IT->n_overlays to the total number of overlay strings found.
5384
5385 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5386 a time. On entry into load_overlay_strings,
5387 IT->current.overlay_string_index gives the number of overlay
5388 strings that have already been loaded by previous calls to this
5389 function.
5390
5391 IT->add_overlay_start contains an additional overlay start
5392 position to consider for taking overlay strings from, if non-zero.
5393 This position comes into play when the overlay has an `invisible'
5394 property, and both before and after-strings. When we've skipped to
5395 the end of the overlay, because of its `invisible' property, we
5396 nevertheless want its before-string to appear.
5397 IT->add_overlay_start will contain the overlay start position
5398 in this case.
5399
5400 Overlay strings are sorted so that after-string strings come in
5401 front of before-string strings. Within before and after-strings,
5402 strings are sorted by overlay priority. See also function
5403 compare_overlay_entries. */
5404
5405 static void
5406 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5407 {
5408 Lisp_Object overlay, window, str, invisible;
5409 struct Lisp_Overlay *ov;
5410 ptrdiff_t start, end;
5411 ptrdiff_t size = 20;
5412 ptrdiff_t n = 0, i, j;
5413 int invis_p;
5414 struct overlay_entry *entries = alloca (size * sizeof *entries);
5415 USE_SAFE_ALLOCA;
5416
5417 if (charpos <= 0)
5418 charpos = IT_CHARPOS (*it);
5419
5420 /* Append the overlay string STRING of overlay OVERLAY to vector
5421 `entries' which has size `size' and currently contains `n'
5422 elements. AFTER_P non-zero means STRING is an after-string of
5423 OVERLAY. */
5424 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5425 do \
5426 { \
5427 Lisp_Object priority; \
5428 \
5429 if (n == size) \
5430 { \
5431 struct overlay_entry *old = entries; \
5432 SAFE_NALLOCA (entries, 2, size); \
5433 memcpy (entries, old, size * sizeof *entries); \
5434 size *= 2; \
5435 } \
5436 \
5437 entries[n].string = (STRING); \
5438 entries[n].overlay = (OVERLAY); \
5439 priority = Foverlay_get ((OVERLAY), Qpriority); \
5440 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5441 entries[n].after_string_p = (AFTER_P); \
5442 ++n; \
5443 } \
5444 while (0)
5445
5446 /* Process overlay before the overlay center. */
5447 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5448 {
5449 XSETMISC (overlay, ov);
5450 eassert (OVERLAYP (overlay));
5451 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5452 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5453
5454 if (end < charpos)
5455 break;
5456
5457 /* Skip this overlay if it doesn't start or end at IT's current
5458 position. */
5459 if (end != charpos && start != charpos)
5460 continue;
5461
5462 /* Skip this overlay if it doesn't apply to IT->w. */
5463 window = Foverlay_get (overlay, Qwindow);
5464 if (WINDOWP (window) && XWINDOW (window) != it->w)
5465 continue;
5466
5467 /* If the text ``under'' the overlay is invisible, both before-
5468 and after-strings from this overlay are visible; start and
5469 end position are indistinguishable. */
5470 invisible = Foverlay_get (overlay, Qinvisible);
5471 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5472
5473 /* If overlay has a non-empty before-string, record it. */
5474 if ((start == charpos || (end == charpos && invis_p))
5475 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5476 && SCHARS (str))
5477 RECORD_OVERLAY_STRING (overlay, str, 0);
5478
5479 /* If overlay has a non-empty after-string, record it. */
5480 if ((end == charpos || (start == charpos && invis_p))
5481 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5482 && SCHARS (str))
5483 RECORD_OVERLAY_STRING (overlay, str, 1);
5484 }
5485
5486 /* Process overlays after the overlay center. */
5487 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5488 {
5489 XSETMISC (overlay, ov);
5490 eassert (OVERLAYP (overlay));
5491 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5492 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5493
5494 if (start > charpos)
5495 break;
5496
5497 /* Skip this overlay if it doesn't start or end at IT's current
5498 position. */
5499 if (end != charpos && start != charpos)
5500 continue;
5501
5502 /* Skip this overlay if it doesn't apply to IT->w. */
5503 window = Foverlay_get (overlay, Qwindow);
5504 if (WINDOWP (window) && XWINDOW (window) != it->w)
5505 continue;
5506
5507 /* If the text ``under'' the overlay is invisible, it has a zero
5508 dimension, and both before- and after-strings apply. */
5509 invisible = Foverlay_get (overlay, Qinvisible);
5510 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5511
5512 /* If overlay has a non-empty before-string, record it. */
5513 if ((start == charpos || (end == charpos && invis_p))
5514 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5515 && SCHARS (str))
5516 RECORD_OVERLAY_STRING (overlay, str, 0);
5517
5518 /* If overlay has a non-empty after-string, record it. */
5519 if ((end == charpos || (start == charpos && invis_p))
5520 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5521 && SCHARS (str))
5522 RECORD_OVERLAY_STRING (overlay, str, 1);
5523 }
5524
5525 #undef RECORD_OVERLAY_STRING
5526
5527 /* Sort entries. */
5528 if (n > 1)
5529 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5530
5531 /* Record number of overlay strings, and where we computed it. */
5532 it->n_overlay_strings = n;
5533 it->overlay_strings_charpos = charpos;
5534
5535 /* IT->current.overlay_string_index is the number of overlay strings
5536 that have already been consumed by IT. Copy some of the
5537 remaining overlay strings to IT->overlay_strings. */
5538 i = 0;
5539 j = it->current.overlay_string_index;
5540 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5541 {
5542 it->overlay_strings[i] = entries[j].string;
5543 it->string_overlays[i++] = entries[j++].overlay;
5544 }
5545
5546 CHECK_IT (it);
5547 SAFE_FREE ();
5548 }
5549
5550
5551 /* Get the first chunk of overlay strings at IT's current buffer
5552 position, or at CHARPOS if that is > 0. Value is non-zero if at
5553 least one overlay string was found. */
5554
5555 static int
5556 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5557 {
5558 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5559 process. This fills IT->overlay_strings with strings, and sets
5560 IT->n_overlay_strings to the total number of strings to process.
5561 IT->pos.overlay_string_index has to be set temporarily to zero
5562 because load_overlay_strings needs this; it must be set to -1
5563 when no overlay strings are found because a zero value would
5564 indicate a position in the first overlay string. */
5565 it->current.overlay_string_index = 0;
5566 load_overlay_strings (it, charpos);
5567
5568 /* If we found overlay strings, set up IT to deliver display
5569 elements from the first one. Otherwise set up IT to deliver
5570 from current_buffer. */
5571 if (it->n_overlay_strings)
5572 {
5573 /* Make sure we know settings in current_buffer, so that we can
5574 restore meaningful values when we're done with the overlay
5575 strings. */
5576 if (compute_stop_p)
5577 compute_stop_pos (it);
5578 eassert (it->face_id >= 0);
5579
5580 /* Save IT's settings. They are restored after all overlay
5581 strings have been processed. */
5582 eassert (!compute_stop_p || it->sp == 0);
5583
5584 /* When called from handle_stop, there might be an empty display
5585 string loaded. In that case, don't bother saving it. But
5586 don't use this optimization with the bidi iterator, since we
5587 need the corresponding pop_it call to resync the bidi
5588 iterator's position with IT's position, after we are done
5589 with the overlay strings. (The corresponding call to pop_it
5590 in case of an empty display string is in
5591 next_overlay_string.) */
5592 if (!(!it->bidi_p
5593 && STRINGP (it->string) && !SCHARS (it->string)))
5594 push_it (it, NULL);
5595
5596 /* Set up IT to deliver display elements from the first overlay
5597 string. */
5598 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5599 it->string = it->overlay_strings[0];
5600 it->from_overlay = Qnil;
5601 it->stop_charpos = 0;
5602 eassert (STRINGP (it->string));
5603 it->end_charpos = SCHARS (it->string);
5604 it->prev_stop = 0;
5605 it->base_level_stop = 0;
5606 it->multibyte_p = STRING_MULTIBYTE (it->string);
5607 it->method = GET_FROM_STRING;
5608 it->from_disp_prop_p = 0;
5609
5610 /* Force paragraph direction to be that of the parent
5611 buffer. */
5612 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5613 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5614 else
5615 it->paragraph_embedding = L2R;
5616
5617 /* Set up the bidi iterator for this overlay string. */
5618 if (it->bidi_p)
5619 {
5620 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5621
5622 it->bidi_it.string.lstring = it->string;
5623 it->bidi_it.string.s = NULL;
5624 it->bidi_it.string.schars = SCHARS (it->string);
5625 it->bidi_it.string.bufpos = pos;
5626 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5627 it->bidi_it.string.unibyte = !it->multibyte_p;
5628 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5629 }
5630 return 1;
5631 }
5632
5633 it->current.overlay_string_index = -1;
5634 return 0;
5635 }
5636
5637 static int
5638 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5639 {
5640 it->string = Qnil;
5641 it->method = GET_FROM_BUFFER;
5642
5643 (void) get_overlay_strings_1 (it, charpos, 1);
5644
5645 CHECK_IT (it);
5646
5647 /* Value is non-zero if we found at least one overlay string. */
5648 return STRINGP (it->string);
5649 }
5650
5651
5652 \f
5653 /***********************************************************************
5654 Saving and restoring state
5655 ***********************************************************************/
5656
5657 /* Save current settings of IT on IT->stack. Called, for example,
5658 before setting up IT for an overlay string, to be able to restore
5659 IT's settings to what they were after the overlay string has been
5660 processed. If POSITION is non-NULL, it is the position to save on
5661 the stack instead of IT->position. */
5662
5663 static void
5664 push_it (struct it *it, struct text_pos *position)
5665 {
5666 struct iterator_stack_entry *p;
5667
5668 eassert (it->sp < IT_STACK_SIZE);
5669 p = it->stack + it->sp;
5670
5671 p->stop_charpos = it->stop_charpos;
5672 p->prev_stop = it->prev_stop;
5673 p->base_level_stop = it->base_level_stop;
5674 p->cmp_it = it->cmp_it;
5675 eassert (it->face_id >= 0);
5676 p->face_id = it->face_id;
5677 p->string = it->string;
5678 p->method = it->method;
5679 p->from_overlay = it->from_overlay;
5680 switch (p->method)
5681 {
5682 case GET_FROM_IMAGE:
5683 p->u.image.object = it->object;
5684 p->u.image.image_id = it->image_id;
5685 p->u.image.slice = it->slice;
5686 break;
5687 case GET_FROM_STRETCH:
5688 p->u.stretch.object = it->object;
5689 break;
5690 }
5691 p->position = position ? *position : it->position;
5692 p->current = it->current;
5693 p->end_charpos = it->end_charpos;
5694 p->string_nchars = it->string_nchars;
5695 p->area = it->area;
5696 p->multibyte_p = it->multibyte_p;
5697 p->avoid_cursor_p = it->avoid_cursor_p;
5698 p->space_width = it->space_width;
5699 p->font_height = it->font_height;
5700 p->voffset = it->voffset;
5701 p->string_from_display_prop_p = it->string_from_display_prop_p;
5702 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5703 p->display_ellipsis_p = 0;
5704 p->line_wrap = it->line_wrap;
5705 p->bidi_p = it->bidi_p;
5706 p->paragraph_embedding = it->paragraph_embedding;
5707 p->from_disp_prop_p = it->from_disp_prop_p;
5708 ++it->sp;
5709
5710 /* Save the state of the bidi iterator as well. */
5711 if (it->bidi_p)
5712 bidi_push_it (&it->bidi_it);
5713 }
5714
5715 static void
5716 iterate_out_of_display_property (struct it *it)
5717 {
5718 int buffer_p = !STRINGP (it->string);
5719 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5720 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5721
5722 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5723
5724 /* Maybe initialize paragraph direction. If we are at the beginning
5725 of a new paragraph, next_element_from_buffer may not have a
5726 chance to do that. */
5727 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5728 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5729 /* prev_stop can be zero, so check against BEGV as well. */
5730 while (it->bidi_it.charpos >= bob
5731 && it->prev_stop <= it->bidi_it.charpos
5732 && it->bidi_it.charpos < CHARPOS (it->position)
5733 && it->bidi_it.charpos < eob)
5734 bidi_move_to_visually_next (&it->bidi_it);
5735 /* Record the stop_pos we just crossed, for when we cross it
5736 back, maybe. */
5737 if (it->bidi_it.charpos > CHARPOS (it->position))
5738 it->prev_stop = CHARPOS (it->position);
5739 /* If we ended up not where pop_it put us, resync IT's
5740 positional members with the bidi iterator. */
5741 if (it->bidi_it.charpos != CHARPOS (it->position))
5742 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5743 if (buffer_p)
5744 it->current.pos = it->position;
5745 else
5746 it->current.string_pos = it->position;
5747 }
5748
5749 /* Restore IT's settings from IT->stack. Called, for example, when no
5750 more overlay strings must be processed, and we return to delivering
5751 display elements from a buffer, or when the end of a string from a
5752 `display' property is reached and we return to delivering display
5753 elements from an overlay string, or from a buffer. */
5754
5755 static void
5756 pop_it (struct it *it)
5757 {
5758 struct iterator_stack_entry *p;
5759 int from_display_prop = it->from_disp_prop_p;
5760
5761 eassert (it->sp > 0);
5762 --it->sp;
5763 p = it->stack + it->sp;
5764 it->stop_charpos = p->stop_charpos;
5765 it->prev_stop = p->prev_stop;
5766 it->base_level_stop = p->base_level_stop;
5767 it->cmp_it = p->cmp_it;
5768 it->face_id = p->face_id;
5769 it->current = p->current;
5770 it->position = p->position;
5771 it->string = p->string;
5772 it->from_overlay = p->from_overlay;
5773 if (NILP (it->string))
5774 SET_TEXT_POS (it->current.string_pos, -1, -1);
5775 it->method = p->method;
5776 switch (it->method)
5777 {
5778 case GET_FROM_IMAGE:
5779 it->image_id = p->u.image.image_id;
5780 it->object = p->u.image.object;
5781 it->slice = p->u.image.slice;
5782 break;
5783 case GET_FROM_STRETCH:
5784 it->object = p->u.stretch.object;
5785 break;
5786 case GET_FROM_BUFFER:
5787 it->object = it->w->buffer;
5788 break;
5789 case GET_FROM_STRING:
5790 it->object = it->string;
5791 break;
5792 case GET_FROM_DISPLAY_VECTOR:
5793 if (it->s)
5794 it->method = GET_FROM_C_STRING;
5795 else if (STRINGP (it->string))
5796 it->method = GET_FROM_STRING;
5797 else
5798 {
5799 it->method = GET_FROM_BUFFER;
5800 it->object = it->w->buffer;
5801 }
5802 }
5803 it->end_charpos = p->end_charpos;
5804 it->string_nchars = p->string_nchars;
5805 it->area = p->area;
5806 it->multibyte_p = p->multibyte_p;
5807 it->avoid_cursor_p = p->avoid_cursor_p;
5808 it->space_width = p->space_width;
5809 it->font_height = p->font_height;
5810 it->voffset = p->voffset;
5811 it->string_from_display_prop_p = p->string_from_display_prop_p;
5812 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5813 it->line_wrap = p->line_wrap;
5814 it->bidi_p = p->bidi_p;
5815 it->paragraph_embedding = p->paragraph_embedding;
5816 it->from_disp_prop_p = p->from_disp_prop_p;
5817 if (it->bidi_p)
5818 {
5819 bidi_pop_it (&it->bidi_it);
5820 /* Bidi-iterate until we get out of the portion of text, if any,
5821 covered by a `display' text property or by an overlay with
5822 `display' property. (We cannot just jump there, because the
5823 internal coherency of the bidi iterator state can not be
5824 preserved across such jumps.) We also must determine the
5825 paragraph base direction if the overlay we just processed is
5826 at the beginning of a new paragraph. */
5827 if (from_display_prop
5828 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5829 iterate_out_of_display_property (it);
5830
5831 eassert ((BUFFERP (it->object)
5832 && IT_CHARPOS (*it) == it->bidi_it.charpos
5833 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5834 || (STRINGP (it->object)
5835 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5836 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5837 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5838 }
5839 }
5840
5841
5842 \f
5843 /***********************************************************************
5844 Moving over lines
5845 ***********************************************************************/
5846
5847 /* Set IT's current position to the previous line start. */
5848
5849 static void
5850 back_to_previous_line_start (struct it *it)
5851 {
5852 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5853 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5854 }
5855
5856
5857 /* Move IT to the next line start.
5858
5859 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5860 we skipped over part of the text (as opposed to moving the iterator
5861 continuously over the text). Otherwise, don't change the value
5862 of *SKIPPED_P.
5863
5864 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5865 iterator on the newline, if it was found.
5866
5867 Newlines may come from buffer text, overlay strings, or strings
5868 displayed via the `display' property. That's the reason we can't
5869 simply use find_next_newline_no_quit.
5870
5871 Note that this function may not skip over invisible text that is so
5872 because of text properties and immediately follows a newline. If
5873 it would, function reseat_at_next_visible_line_start, when called
5874 from set_iterator_to_next, would effectively make invisible
5875 characters following a newline part of the wrong glyph row, which
5876 leads to wrong cursor motion. */
5877
5878 static int
5879 forward_to_next_line_start (struct it *it, int *skipped_p,
5880 struct bidi_it *bidi_it_prev)
5881 {
5882 ptrdiff_t old_selective;
5883 int newline_found_p, n;
5884 const int MAX_NEWLINE_DISTANCE = 500;
5885
5886 /* If already on a newline, just consume it to avoid unintended
5887 skipping over invisible text below. */
5888 if (it->what == IT_CHARACTER
5889 && it->c == '\n'
5890 && CHARPOS (it->position) == IT_CHARPOS (*it))
5891 {
5892 if (it->bidi_p && bidi_it_prev)
5893 *bidi_it_prev = it->bidi_it;
5894 set_iterator_to_next (it, 0);
5895 it->c = 0;
5896 return 1;
5897 }
5898
5899 /* Don't handle selective display in the following. It's (a)
5900 unnecessary because it's done by the caller, and (b) leads to an
5901 infinite recursion because next_element_from_ellipsis indirectly
5902 calls this function. */
5903 old_selective = it->selective;
5904 it->selective = 0;
5905
5906 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5907 from buffer text. */
5908 for (n = newline_found_p = 0;
5909 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5910 n += STRINGP (it->string) ? 0 : 1)
5911 {
5912 if (!get_next_display_element (it))
5913 return 0;
5914 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5915 if (newline_found_p && it->bidi_p && bidi_it_prev)
5916 *bidi_it_prev = it->bidi_it;
5917 set_iterator_to_next (it, 0);
5918 }
5919
5920 /* If we didn't find a newline near enough, see if we can use a
5921 short-cut. */
5922 if (!newline_found_p)
5923 {
5924 ptrdiff_t start = IT_CHARPOS (*it);
5925 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5926 Lisp_Object pos;
5927
5928 eassert (!STRINGP (it->string));
5929
5930 /* If there isn't any `display' property in sight, and no
5931 overlays, we can just use the position of the newline in
5932 buffer text. */
5933 if (it->stop_charpos >= limit
5934 || ((pos = Fnext_single_property_change (make_number (start),
5935 Qdisplay, Qnil,
5936 make_number (limit)),
5937 NILP (pos))
5938 && next_overlay_change (start) == ZV))
5939 {
5940 if (!it->bidi_p)
5941 {
5942 IT_CHARPOS (*it) = limit;
5943 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5944 }
5945 else
5946 {
5947 struct bidi_it bprev;
5948
5949 /* Help bidi.c avoid expensive searches for display
5950 properties and overlays, by telling it that there are
5951 none up to `limit'. */
5952 if (it->bidi_it.disp_pos < limit)
5953 {
5954 it->bidi_it.disp_pos = limit;
5955 it->bidi_it.disp_prop = 0;
5956 }
5957 do {
5958 bprev = it->bidi_it;
5959 bidi_move_to_visually_next (&it->bidi_it);
5960 } while (it->bidi_it.charpos != limit);
5961 IT_CHARPOS (*it) = limit;
5962 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5963 if (bidi_it_prev)
5964 *bidi_it_prev = bprev;
5965 }
5966 *skipped_p = newline_found_p = 1;
5967 }
5968 else
5969 {
5970 while (get_next_display_element (it)
5971 && !newline_found_p)
5972 {
5973 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5974 if (newline_found_p && it->bidi_p && bidi_it_prev)
5975 *bidi_it_prev = it->bidi_it;
5976 set_iterator_to_next (it, 0);
5977 }
5978 }
5979 }
5980
5981 it->selective = old_selective;
5982 return newline_found_p;
5983 }
5984
5985
5986 /* Set IT's current position to the previous visible line start. Skip
5987 invisible text that is so either due to text properties or due to
5988 selective display. Caution: this does not change IT->current_x and
5989 IT->hpos. */
5990
5991 static void
5992 back_to_previous_visible_line_start (struct it *it)
5993 {
5994 while (IT_CHARPOS (*it) > BEGV)
5995 {
5996 back_to_previous_line_start (it);
5997
5998 if (IT_CHARPOS (*it) <= BEGV)
5999 break;
6000
6001 /* If selective > 0, then lines indented more than its value are
6002 invisible. */
6003 if (it->selective > 0
6004 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6005 it->selective))
6006 continue;
6007
6008 /* Check the newline before point for invisibility. */
6009 {
6010 Lisp_Object prop;
6011 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6012 Qinvisible, it->window);
6013 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6014 continue;
6015 }
6016
6017 if (IT_CHARPOS (*it) <= BEGV)
6018 break;
6019
6020 {
6021 struct it it2;
6022 void *it2data = NULL;
6023 ptrdiff_t pos;
6024 ptrdiff_t beg, end;
6025 Lisp_Object val, overlay;
6026
6027 SAVE_IT (it2, *it, it2data);
6028
6029 /* If newline is part of a composition, continue from start of composition */
6030 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6031 && beg < IT_CHARPOS (*it))
6032 goto replaced;
6033
6034 /* If newline is replaced by a display property, find start of overlay
6035 or interval and continue search from that point. */
6036 pos = --IT_CHARPOS (it2);
6037 --IT_BYTEPOS (it2);
6038 it2.sp = 0;
6039 bidi_unshelve_cache (NULL, 0);
6040 it2.string_from_display_prop_p = 0;
6041 it2.from_disp_prop_p = 0;
6042 if (handle_display_prop (&it2) == HANDLED_RETURN
6043 && !NILP (val = get_char_property_and_overlay
6044 (make_number (pos), Qdisplay, Qnil, &overlay))
6045 && (OVERLAYP (overlay)
6046 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6047 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6048 {
6049 RESTORE_IT (it, it, it2data);
6050 goto replaced;
6051 }
6052
6053 /* Newline is not replaced by anything -- so we are done. */
6054 RESTORE_IT (it, it, it2data);
6055 break;
6056
6057 replaced:
6058 if (beg < BEGV)
6059 beg = BEGV;
6060 IT_CHARPOS (*it) = beg;
6061 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6062 }
6063 }
6064
6065 it->continuation_lines_width = 0;
6066
6067 eassert (IT_CHARPOS (*it) >= BEGV);
6068 eassert (IT_CHARPOS (*it) == BEGV
6069 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6070 CHECK_IT (it);
6071 }
6072
6073
6074 /* Reseat iterator IT at the previous visible line start. Skip
6075 invisible text that is so either due to text properties or due to
6076 selective display. At the end, update IT's overlay information,
6077 face information etc. */
6078
6079 void
6080 reseat_at_previous_visible_line_start (struct it *it)
6081 {
6082 back_to_previous_visible_line_start (it);
6083 reseat (it, it->current.pos, 1);
6084 CHECK_IT (it);
6085 }
6086
6087
6088 /* Reseat iterator IT on the next visible line start in the current
6089 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6090 preceding the line start. Skip over invisible text that is so
6091 because of selective display. Compute faces, overlays etc at the
6092 new position. Note that this function does not skip over text that
6093 is invisible because of text properties. */
6094
6095 static void
6096 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6097 {
6098 int newline_found_p, skipped_p = 0;
6099 struct bidi_it bidi_it_prev;
6100
6101 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6102
6103 /* Skip over lines that are invisible because they are indented
6104 more than the value of IT->selective. */
6105 if (it->selective > 0)
6106 while (IT_CHARPOS (*it) < ZV
6107 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6108 it->selective))
6109 {
6110 eassert (IT_BYTEPOS (*it) == BEGV
6111 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6112 newline_found_p =
6113 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6114 }
6115
6116 /* Position on the newline if that's what's requested. */
6117 if (on_newline_p && newline_found_p)
6118 {
6119 if (STRINGP (it->string))
6120 {
6121 if (IT_STRING_CHARPOS (*it) > 0)
6122 {
6123 if (!it->bidi_p)
6124 {
6125 --IT_STRING_CHARPOS (*it);
6126 --IT_STRING_BYTEPOS (*it);
6127 }
6128 else
6129 {
6130 /* We need to restore the bidi iterator to the state
6131 it had on the newline, and resync the IT's
6132 position with that. */
6133 it->bidi_it = bidi_it_prev;
6134 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6135 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6136 }
6137 }
6138 }
6139 else if (IT_CHARPOS (*it) > BEGV)
6140 {
6141 if (!it->bidi_p)
6142 {
6143 --IT_CHARPOS (*it);
6144 --IT_BYTEPOS (*it);
6145 }
6146 else
6147 {
6148 /* We need to restore the bidi iterator to the state it
6149 had on the newline and resync IT with that. */
6150 it->bidi_it = bidi_it_prev;
6151 IT_CHARPOS (*it) = it->bidi_it.charpos;
6152 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6153 }
6154 reseat (it, it->current.pos, 0);
6155 }
6156 }
6157 else if (skipped_p)
6158 reseat (it, it->current.pos, 0);
6159
6160 CHECK_IT (it);
6161 }
6162
6163
6164 \f
6165 /***********************************************************************
6166 Changing an iterator's position
6167 ***********************************************************************/
6168
6169 /* Change IT's current position to POS in current_buffer. If FORCE_P
6170 is non-zero, always check for text properties at the new position.
6171 Otherwise, text properties are only looked up if POS >=
6172 IT->check_charpos of a property. */
6173
6174 static void
6175 reseat (struct it *it, struct text_pos pos, int force_p)
6176 {
6177 ptrdiff_t original_pos = IT_CHARPOS (*it);
6178
6179 reseat_1 (it, pos, 0);
6180
6181 /* Determine where to check text properties. Avoid doing it
6182 where possible because text property lookup is very expensive. */
6183 if (force_p
6184 || CHARPOS (pos) > it->stop_charpos
6185 || CHARPOS (pos) < original_pos)
6186 {
6187 if (it->bidi_p)
6188 {
6189 /* For bidi iteration, we need to prime prev_stop and
6190 base_level_stop with our best estimations. */
6191 /* Implementation note: Of course, POS is not necessarily a
6192 stop position, so assigning prev_pos to it is a lie; we
6193 should have called compute_stop_backwards. However, if
6194 the current buffer does not include any R2L characters,
6195 that call would be a waste of cycles, because the
6196 iterator will never move back, and thus never cross this
6197 "fake" stop position. So we delay that backward search
6198 until the time we really need it, in next_element_from_buffer. */
6199 if (CHARPOS (pos) != it->prev_stop)
6200 it->prev_stop = CHARPOS (pos);
6201 if (CHARPOS (pos) < it->base_level_stop)
6202 it->base_level_stop = 0; /* meaning it's unknown */
6203 handle_stop (it);
6204 }
6205 else
6206 {
6207 handle_stop (it);
6208 it->prev_stop = it->base_level_stop = 0;
6209 }
6210
6211 }
6212
6213 CHECK_IT (it);
6214 }
6215
6216
6217 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6218 IT->stop_pos to POS, also. */
6219
6220 static void
6221 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6222 {
6223 /* Don't call this function when scanning a C string. */
6224 eassert (it->s == NULL);
6225
6226 /* POS must be a reasonable value. */
6227 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6228
6229 it->current.pos = it->position = pos;
6230 it->end_charpos = ZV;
6231 it->dpvec = NULL;
6232 it->current.dpvec_index = -1;
6233 it->current.overlay_string_index = -1;
6234 IT_STRING_CHARPOS (*it) = -1;
6235 IT_STRING_BYTEPOS (*it) = -1;
6236 it->string = Qnil;
6237 it->method = GET_FROM_BUFFER;
6238 it->object = it->w->buffer;
6239 it->area = TEXT_AREA;
6240 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6241 it->sp = 0;
6242 it->string_from_display_prop_p = 0;
6243 it->string_from_prefix_prop_p = 0;
6244
6245 it->from_disp_prop_p = 0;
6246 it->face_before_selective_p = 0;
6247 if (it->bidi_p)
6248 {
6249 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6250 &it->bidi_it);
6251 bidi_unshelve_cache (NULL, 0);
6252 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6253 it->bidi_it.string.s = NULL;
6254 it->bidi_it.string.lstring = Qnil;
6255 it->bidi_it.string.bufpos = 0;
6256 it->bidi_it.string.unibyte = 0;
6257 }
6258
6259 if (set_stop_p)
6260 {
6261 it->stop_charpos = CHARPOS (pos);
6262 it->base_level_stop = CHARPOS (pos);
6263 }
6264 }
6265
6266
6267 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6268 If S is non-null, it is a C string to iterate over. Otherwise,
6269 STRING gives a Lisp string to iterate over.
6270
6271 If PRECISION > 0, don't return more then PRECISION number of
6272 characters from the string.
6273
6274 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6275 characters have been returned. FIELD_WIDTH < 0 means an infinite
6276 field width.
6277
6278 MULTIBYTE = 0 means disable processing of multibyte characters,
6279 MULTIBYTE > 0 means enable it,
6280 MULTIBYTE < 0 means use IT->multibyte_p.
6281
6282 IT must be initialized via a prior call to init_iterator before
6283 calling this function. */
6284
6285 static void
6286 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6287 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6288 int multibyte)
6289 {
6290 /* No region in strings. */
6291 it->region_beg_charpos = it->region_end_charpos = -1;
6292
6293 /* No text property checks performed by default, but see below. */
6294 it->stop_charpos = -1;
6295
6296 /* Set iterator position and end position. */
6297 memset (&it->current, 0, sizeof it->current);
6298 it->current.overlay_string_index = -1;
6299 it->current.dpvec_index = -1;
6300 eassert (charpos >= 0);
6301
6302 /* If STRING is specified, use its multibyteness, otherwise use the
6303 setting of MULTIBYTE, if specified. */
6304 if (multibyte >= 0)
6305 it->multibyte_p = multibyte > 0;
6306
6307 /* Bidirectional reordering of strings is controlled by the default
6308 value of bidi-display-reordering. Don't try to reorder while
6309 loading loadup.el, as the necessary character property tables are
6310 not yet available. */
6311 it->bidi_p =
6312 NILP (Vpurify_flag)
6313 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6314
6315 if (s == NULL)
6316 {
6317 eassert (STRINGP (string));
6318 it->string = string;
6319 it->s = NULL;
6320 it->end_charpos = it->string_nchars = SCHARS (string);
6321 it->method = GET_FROM_STRING;
6322 it->current.string_pos = string_pos (charpos, string);
6323
6324 if (it->bidi_p)
6325 {
6326 it->bidi_it.string.lstring = string;
6327 it->bidi_it.string.s = NULL;
6328 it->bidi_it.string.schars = it->end_charpos;
6329 it->bidi_it.string.bufpos = 0;
6330 it->bidi_it.string.from_disp_str = 0;
6331 it->bidi_it.string.unibyte = !it->multibyte_p;
6332 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6333 FRAME_WINDOW_P (it->f), &it->bidi_it);
6334 }
6335 }
6336 else
6337 {
6338 it->s = (const unsigned char *) s;
6339 it->string = Qnil;
6340
6341 /* Note that we use IT->current.pos, not it->current.string_pos,
6342 for displaying C strings. */
6343 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6344 if (it->multibyte_p)
6345 {
6346 it->current.pos = c_string_pos (charpos, s, 1);
6347 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6348 }
6349 else
6350 {
6351 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6352 it->end_charpos = it->string_nchars = strlen (s);
6353 }
6354
6355 if (it->bidi_p)
6356 {
6357 it->bidi_it.string.lstring = Qnil;
6358 it->bidi_it.string.s = (const unsigned char *) s;
6359 it->bidi_it.string.schars = it->end_charpos;
6360 it->bidi_it.string.bufpos = 0;
6361 it->bidi_it.string.from_disp_str = 0;
6362 it->bidi_it.string.unibyte = !it->multibyte_p;
6363 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6364 &it->bidi_it);
6365 }
6366 it->method = GET_FROM_C_STRING;
6367 }
6368
6369 /* PRECISION > 0 means don't return more than PRECISION characters
6370 from the string. */
6371 if (precision > 0 && it->end_charpos - charpos > precision)
6372 {
6373 it->end_charpos = it->string_nchars = charpos + precision;
6374 if (it->bidi_p)
6375 it->bidi_it.string.schars = it->end_charpos;
6376 }
6377
6378 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6379 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6380 FIELD_WIDTH < 0 means infinite field width. This is useful for
6381 padding with `-' at the end of a mode line. */
6382 if (field_width < 0)
6383 field_width = INFINITY;
6384 /* Implementation note: We deliberately don't enlarge
6385 it->bidi_it.string.schars here to fit it->end_charpos, because
6386 the bidi iterator cannot produce characters out of thin air. */
6387 if (field_width > it->end_charpos - charpos)
6388 it->end_charpos = charpos + field_width;
6389
6390 /* Use the standard display table for displaying strings. */
6391 if (DISP_TABLE_P (Vstandard_display_table))
6392 it->dp = XCHAR_TABLE (Vstandard_display_table);
6393
6394 it->stop_charpos = charpos;
6395 it->prev_stop = charpos;
6396 it->base_level_stop = 0;
6397 if (it->bidi_p)
6398 {
6399 it->bidi_it.first_elt = 1;
6400 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6401 it->bidi_it.disp_pos = -1;
6402 }
6403 if (s == NULL && it->multibyte_p)
6404 {
6405 ptrdiff_t endpos = SCHARS (it->string);
6406 if (endpos > it->end_charpos)
6407 endpos = it->end_charpos;
6408 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6409 it->string);
6410 }
6411 CHECK_IT (it);
6412 }
6413
6414
6415 \f
6416 /***********************************************************************
6417 Iteration
6418 ***********************************************************************/
6419
6420 /* Map enum it_method value to corresponding next_element_from_* function. */
6421
6422 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6423 {
6424 next_element_from_buffer,
6425 next_element_from_display_vector,
6426 next_element_from_string,
6427 next_element_from_c_string,
6428 next_element_from_image,
6429 next_element_from_stretch
6430 };
6431
6432 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6433
6434
6435 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6436 (possibly with the following characters). */
6437
6438 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6439 ((IT)->cmp_it.id >= 0 \
6440 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6441 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6442 END_CHARPOS, (IT)->w, \
6443 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6444 (IT)->string)))
6445
6446
6447 /* Lookup the char-table Vglyphless_char_display for character C (-1
6448 if we want information for no-font case), and return the display
6449 method symbol. By side-effect, update it->what and
6450 it->glyphless_method. This function is called from
6451 get_next_display_element for each character element, and from
6452 x_produce_glyphs when no suitable font was found. */
6453
6454 Lisp_Object
6455 lookup_glyphless_char_display (int c, struct it *it)
6456 {
6457 Lisp_Object glyphless_method = Qnil;
6458
6459 if (CHAR_TABLE_P (Vglyphless_char_display)
6460 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6461 {
6462 if (c >= 0)
6463 {
6464 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6465 if (CONSP (glyphless_method))
6466 glyphless_method = FRAME_WINDOW_P (it->f)
6467 ? XCAR (glyphless_method)
6468 : XCDR (glyphless_method);
6469 }
6470 else
6471 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6472 }
6473
6474 retry:
6475 if (NILP (glyphless_method))
6476 {
6477 if (c >= 0)
6478 /* The default is to display the character by a proper font. */
6479 return Qnil;
6480 /* The default for the no-font case is to display an empty box. */
6481 glyphless_method = Qempty_box;
6482 }
6483 if (EQ (glyphless_method, Qzero_width))
6484 {
6485 if (c >= 0)
6486 return glyphless_method;
6487 /* This method can't be used for the no-font case. */
6488 glyphless_method = Qempty_box;
6489 }
6490 if (EQ (glyphless_method, Qthin_space))
6491 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6492 else if (EQ (glyphless_method, Qempty_box))
6493 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6494 else if (EQ (glyphless_method, Qhex_code))
6495 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6496 else if (STRINGP (glyphless_method))
6497 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6498 else
6499 {
6500 /* Invalid value. We use the default method. */
6501 glyphless_method = Qnil;
6502 goto retry;
6503 }
6504 it->what = IT_GLYPHLESS;
6505 return glyphless_method;
6506 }
6507
6508 /* Load IT's display element fields with information about the next
6509 display element from the current position of IT. Value is zero if
6510 end of buffer (or C string) is reached. */
6511
6512 static struct frame *last_escape_glyph_frame = NULL;
6513 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6514 static int last_escape_glyph_merged_face_id = 0;
6515
6516 struct frame *last_glyphless_glyph_frame = NULL;
6517 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6518 int last_glyphless_glyph_merged_face_id = 0;
6519
6520 static int
6521 get_next_display_element (struct it *it)
6522 {
6523 /* Non-zero means that we found a display element. Zero means that
6524 we hit the end of what we iterate over. Performance note: the
6525 function pointer `method' used here turns out to be faster than
6526 using a sequence of if-statements. */
6527 int success_p;
6528
6529 get_next:
6530 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6531
6532 if (it->what == IT_CHARACTER)
6533 {
6534 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6535 and only if (a) the resolved directionality of that character
6536 is R..." */
6537 /* FIXME: Do we need an exception for characters from display
6538 tables? */
6539 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6540 it->c = bidi_mirror_char (it->c);
6541 /* Map via display table or translate control characters.
6542 IT->c, IT->len etc. have been set to the next character by
6543 the function call above. If we have a display table, and it
6544 contains an entry for IT->c, translate it. Don't do this if
6545 IT->c itself comes from a display table, otherwise we could
6546 end up in an infinite recursion. (An alternative could be to
6547 count the recursion depth of this function and signal an
6548 error when a certain maximum depth is reached.) Is it worth
6549 it? */
6550 if (success_p && it->dpvec == NULL)
6551 {
6552 Lisp_Object dv;
6553 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6554 int nonascii_space_p = 0;
6555 int nonascii_hyphen_p = 0;
6556 int c = it->c; /* This is the character to display. */
6557
6558 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6559 {
6560 eassert (SINGLE_BYTE_CHAR_P (c));
6561 if (unibyte_display_via_language_environment)
6562 {
6563 c = DECODE_CHAR (unibyte, c);
6564 if (c < 0)
6565 c = BYTE8_TO_CHAR (it->c);
6566 }
6567 else
6568 c = BYTE8_TO_CHAR (it->c);
6569 }
6570
6571 if (it->dp
6572 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6573 VECTORP (dv)))
6574 {
6575 struct Lisp_Vector *v = XVECTOR (dv);
6576
6577 /* Return the first character from the display table
6578 entry, if not empty. If empty, don't display the
6579 current character. */
6580 if (v->header.size)
6581 {
6582 it->dpvec_char_len = it->len;
6583 it->dpvec = v->contents;
6584 it->dpend = v->contents + v->header.size;
6585 it->current.dpvec_index = 0;
6586 it->dpvec_face_id = -1;
6587 it->saved_face_id = it->face_id;
6588 it->method = GET_FROM_DISPLAY_VECTOR;
6589 it->ellipsis_p = 0;
6590 }
6591 else
6592 {
6593 set_iterator_to_next (it, 0);
6594 }
6595 goto get_next;
6596 }
6597
6598 if (! NILP (lookup_glyphless_char_display (c, it)))
6599 {
6600 if (it->what == IT_GLYPHLESS)
6601 goto done;
6602 /* Don't display this character. */
6603 set_iterator_to_next (it, 0);
6604 goto get_next;
6605 }
6606
6607 /* If `nobreak-char-display' is non-nil, we display
6608 non-ASCII spaces and hyphens specially. */
6609 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6610 {
6611 if (c == 0xA0)
6612 nonascii_space_p = 1;
6613 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6614 nonascii_hyphen_p = 1;
6615 }
6616
6617 /* Translate control characters into `\003' or `^C' form.
6618 Control characters coming from a display table entry are
6619 currently not translated because we use IT->dpvec to hold
6620 the translation. This could easily be changed but I
6621 don't believe that it is worth doing.
6622
6623 The characters handled by `nobreak-char-display' must be
6624 translated too.
6625
6626 Non-printable characters and raw-byte characters are also
6627 translated to octal form. */
6628 if (((c < ' ' || c == 127) /* ASCII control chars */
6629 ? (it->area != TEXT_AREA
6630 /* In mode line, treat \n, \t like other crl chars. */
6631 || (c != '\t'
6632 && it->glyph_row
6633 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6634 || (c != '\n' && c != '\t'))
6635 : (nonascii_space_p
6636 || nonascii_hyphen_p
6637 || CHAR_BYTE8_P (c)
6638 || ! CHAR_PRINTABLE_P (c))))
6639 {
6640 /* C is a control character, non-ASCII space/hyphen,
6641 raw-byte, or a non-printable character which must be
6642 displayed either as '\003' or as `^C' where the '\\'
6643 and '^' can be defined in the display table. Fill
6644 IT->ctl_chars with glyphs for what we have to
6645 display. Then, set IT->dpvec to these glyphs. */
6646 Lisp_Object gc;
6647 int ctl_len;
6648 int face_id;
6649 int lface_id = 0;
6650 int escape_glyph;
6651
6652 /* Handle control characters with ^. */
6653
6654 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6655 {
6656 int g;
6657
6658 g = '^'; /* default glyph for Control */
6659 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6660 if (it->dp
6661 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6662 {
6663 g = GLYPH_CODE_CHAR (gc);
6664 lface_id = GLYPH_CODE_FACE (gc);
6665 }
6666 if (lface_id)
6667 {
6668 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6669 }
6670 else if (it->f == last_escape_glyph_frame
6671 && it->face_id == last_escape_glyph_face_id)
6672 {
6673 face_id = last_escape_glyph_merged_face_id;
6674 }
6675 else
6676 {
6677 /* Merge the escape-glyph face into the current face. */
6678 face_id = merge_faces (it->f, Qescape_glyph, 0,
6679 it->face_id);
6680 last_escape_glyph_frame = it->f;
6681 last_escape_glyph_face_id = it->face_id;
6682 last_escape_glyph_merged_face_id = face_id;
6683 }
6684
6685 XSETINT (it->ctl_chars[0], g);
6686 XSETINT (it->ctl_chars[1], c ^ 0100);
6687 ctl_len = 2;
6688 goto display_control;
6689 }
6690
6691 /* Handle non-ascii space in the mode where it only gets
6692 highlighting. */
6693
6694 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6695 {
6696 /* Merge `nobreak-space' into the current face. */
6697 face_id = merge_faces (it->f, Qnobreak_space, 0,
6698 it->face_id);
6699 XSETINT (it->ctl_chars[0], ' ');
6700 ctl_len = 1;
6701 goto display_control;
6702 }
6703
6704 /* Handle sequences that start with the "escape glyph". */
6705
6706 /* the default escape glyph is \. */
6707 escape_glyph = '\\';
6708
6709 if (it->dp
6710 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6711 {
6712 escape_glyph = GLYPH_CODE_CHAR (gc);
6713 lface_id = GLYPH_CODE_FACE (gc);
6714 }
6715 if (lface_id)
6716 {
6717 /* The display table specified a face.
6718 Merge it into face_id and also into escape_glyph. */
6719 face_id = merge_faces (it->f, Qt, lface_id,
6720 it->face_id);
6721 }
6722 else if (it->f == last_escape_glyph_frame
6723 && it->face_id == last_escape_glyph_face_id)
6724 {
6725 face_id = last_escape_glyph_merged_face_id;
6726 }
6727 else
6728 {
6729 /* Merge the escape-glyph face into the current face. */
6730 face_id = merge_faces (it->f, Qescape_glyph, 0,
6731 it->face_id);
6732 last_escape_glyph_frame = it->f;
6733 last_escape_glyph_face_id = it->face_id;
6734 last_escape_glyph_merged_face_id = face_id;
6735 }
6736
6737 /* Draw non-ASCII hyphen with just highlighting: */
6738
6739 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6740 {
6741 XSETINT (it->ctl_chars[0], '-');
6742 ctl_len = 1;
6743 goto display_control;
6744 }
6745
6746 /* Draw non-ASCII space/hyphen with escape glyph: */
6747
6748 if (nonascii_space_p || nonascii_hyphen_p)
6749 {
6750 XSETINT (it->ctl_chars[0], escape_glyph);
6751 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6752 ctl_len = 2;
6753 goto display_control;
6754 }
6755
6756 {
6757 char str[10];
6758 int len, i;
6759
6760 if (CHAR_BYTE8_P (c))
6761 /* Display \200 instead of \17777600. */
6762 c = CHAR_TO_BYTE8 (c);
6763 len = sprintf (str, "%03o", c);
6764
6765 XSETINT (it->ctl_chars[0], escape_glyph);
6766 for (i = 0; i < len; i++)
6767 XSETINT (it->ctl_chars[i + 1], str[i]);
6768 ctl_len = len + 1;
6769 }
6770
6771 display_control:
6772 /* Set up IT->dpvec and return first character from it. */
6773 it->dpvec_char_len = it->len;
6774 it->dpvec = it->ctl_chars;
6775 it->dpend = it->dpvec + ctl_len;
6776 it->current.dpvec_index = 0;
6777 it->dpvec_face_id = face_id;
6778 it->saved_face_id = it->face_id;
6779 it->method = GET_FROM_DISPLAY_VECTOR;
6780 it->ellipsis_p = 0;
6781 goto get_next;
6782 }
6783 it->char_to_display = c;
6784 }
6785 else if (success_p)
6786 {
6787 it->char_to_display = it->c;
6788 }
6789 }
6790
6791 /* Adjust face id for a multibyte character. There are no multibyte
6792 character in unibyte text. */
6793 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6794 && it->multibyte_p
6795 && success_p
6796 && FRAME_WINDOW_P (it->f))
6797 {
6798 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6799
6800 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6801 {
6802 /* Automatic composition with glyph-string. */
6803 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6804
6805 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6806 }
6807 else
6808 {
6809 ptrdiff_t pos = (it->s ? -1
6810 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6811 : IT_CHARPOS (*it));
6812 int c;
6813
6814 if (it->what == IT_CHARACTER)
6815 c = it->char_to_display;
6816 else
6817 {
6818 struct composition *cmp = composition_table[it->cmp_it.id];
6819 int i;
6820
6821 c = ' ';
6822 for (i = 0; i < cmp->glyph_len; i++)
6823 /* TAB in a composition means display glyphs with
6824 padding space on the left or right. */
6825 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6826 break;
6827 }
6828 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6829 }
6830 }
6831
6832 done:
6833 /* Is this character the last one of a run of characters with
6834 box? If yes, set IT->end_of_box_run_p to 1. */
6835 if (it->face_box_p
6836 && it->s == NULL)
6837 {
6838 if (it->method == GET_FROM_STRING && it->sp)
6839 {
6840 int face_id = underlying_face_id (it);
6841 struct face *face = FACE_FROM_ID (it->f, face_id);
6842
6843 if (face)
6844 {
6845 if (face->box == FACE_NO_BOX)
6846 {
6847 /* If the box comes from face properties in a
6848 display string, check faces in that string. */
6849 int string_face_id = face_after_it_pos (it);
6850 it->end_of_box_run_p
6851 = (FACE_FROM_ID (it->f, string_face_id)->box
6852 == FACE_NO_BOX);
6853 }
6854 /* Otherwise, the box comes from the underlying face.
6855 If this is the last string character displayed, check
6856 the next buffer location. */
6857 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6858 && (it->current.overlay_string_index
6859 == it->n_overlay_strings - 1))
6860 {
6861 ptrdiff_t ignore;
6862 int next_face_id;
6863 struct text_pos pos = it->current.pos;
6864 INC_TEXT_POS (pos, it->multibyte_p);
6865
6866 next_face_id = face_at_buffer_position
6867 (it->w, CHARPOS (pos), it->region_beg_charpos,
6868 it->region_end_charpos, &ignore,
6869 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6870 -1);
6871 it->end_of_box_run_p
6872 = (FACE_FROM_ID (it->f, next_face_id)->box
6873 == FACE_NO_BOX);
6874 }
6875 }
6876 }
6877 else
6878 {
6879 int face_id = face_after_it_pos (it);
6880 it->end_of_box_run_p
6881 = (face_id != it->face_id
6882 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6883 }
6884 }
6885 /* If we reached the end of the object we've been iterating (e.g., a
6886 display string or an overlay string), and there's something on
6887 IT->stack, proceed with what's on the stack. It doesn't make
6888 sense to return zero if there's unprocessed stuff on the stack,
6889 because otherwise that stuff will never be displayed. */
6890 if (!success_p && it->sp > 0)
6891 {
6892 set_iterator_to_next (it, 0);
6893 success_p = get_next_display_element (it);
6894 }
6895
6896 /* Value is 0 if end of buffer or string reached. */
6897 return success_p;
6898 }
6899
6900
6901 /* Move IT to the next display element.
6902
6903 RESEAT_P non-zero means if called on a newline in buffer text,
6904 skip to the next visible line start.
6905
6906 Functions get_next_display_element and set_iterator_to_next are
6907 separate because I find this arrangement easier to handle than a
6908 get_next_display_element function that also increments IT's
6909 position. The way it is we can first look at an iterator's current
6910 display element, decide whether it fits on a line, and if it does,
6911 increment the iterator position. The other way around we probably
6912 would either need a flag indicating whether the iterator has to be
6913 incremented the next time, or we would have to implement a
6914 decrement position function which would not be easy to write. */
6915
6916 void
6917 set_iterator_to_next (struct it *it, int reseat_p)
6918 {
6919 /* Reset flags indicating start and end of a sequence of characters
6920 with box. Reset them at the start of this function because
6921 moving the iterator to a new position might set them. */
6922 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6923
6924 switch (it->method)
6925 {
6926 case GET_FROM_BUFFER:
6927 /* The current display element of IT is a character from
6928 current_buffer. Advance in the buffer, and maybe skip over
6929 invisible lines that are so because of selective display. */
6930 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6931 reseat_at_next_visible_line_start (it, 0);
6932 else if (it->cmp_it.id >= 0)
6933 {
6934 /* We are currently getting glyphs from a composition. */
6935 int i;
6936
6937 if (! it->bidi_p)
6938 {
6939 IT_CHARPOS (*it) += it->cmp_it.nchars;
6940 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6941 if (it->cmp_it.to < it->cmp_it.nglyphs)
6942 {
6943 it->cmp_it.from = it->cmp_it.to;
6944 }
6945 else
6946 {
6947 it->cmp_it.id = -1;
6948 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6949 IT_BYTEPOS (*it),
6950 it->end_charpos, Qnil);
6951 }
6952 }
6953 else if (! it->cmp_it.reversed_p)
6954 {
6955 /* Composition created while scanning forward. */
6956 /* Update IT's char/byte positions to point to the first
6957 character of the next grapheme cluster, or to the
6958 character visually after the current composition. */
6959 for (i = 0; i < it->cmp_it.nchars; i++)
6960 bidi_move_to_visually_next (&it->bidi_it);
6961 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6962 IT_CHARPOS (*it) = it->bidi_it.charpos;
6963
6964 if (it->cmp_it.to < it->cmp_it.nglyphs)
6965 {
6966 /* Proceed to the next grapheme cluster. */
6967 it->cmp_it.from = it->cmp_it.to;
6968 }
6969 else
6970 {
6971 /* No more grapheme clusters in this composition.
6972 Find the next stop position. */
6973 ptrdiff_t stop = it->end_charpos;
6974 if (it->bidi_it.scan_dir < 0)
6975 /* Now we are scanning backward and don't know
6976 where to stop. */
6977 stop = -1;
6978 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6979 IT_BYTEPOS (*it), stop, Qnil);
6980 }
6981 }
6982 else
6983 {
6984 /* Composition created while scanning backward. */
6985 /* Update IT's char/byte positions to point to the last
6986 character of the previous grapheme cluster, or the
6987 character visually after the current composition. */
6988 for (i = 0; i < it->cmp_it.nchars; i++)
6989 bidi_move_to_visually_next (&it->bidi_it);
6990 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6991 IT_CHARPOS (*it) = it->bidi_it.charpos;
6992 if (it->cmp_it.from > 0)
6993 {
6994 /* Proceed to the previous grapheme cluster. */
6995 it->cmp_it.to = it->cmp_it.from;
6996 }
6997 else
6998 {
6999 /* No more grapheme clusters in this composition.
7000 Find the next stop position. */
7001 ptrdiff_t stop = it->end_charpos;
7002 if (it->bidi_it.scan_dir < 0)
7003 /* Now we are scanning backward and don't know
7004 where to stop. */
7005 stop = -1;
7006 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7007 IT_BYTEPOS (*it), stop, Qnil);
7008 }
7009 }
7010 }
7011 else
7012 {
7013 eassert (it->len != 0);
7014
7015 if (!it->bidi_p)
7016 {
7017 IT_BYTEPOS (*it) += it->len;
7018 IT_CHARPOS (*it) += 1;
7019 }
7020 else
7021 {
7022 int prev_scan_dir = it->bidi_it.scan_dir;
7023 /* If this is a new paragraph, determine its base
7024 direction (a.k.a. its base embedding level). */
7025 if (it->bidi_it.new_paragraph)
7026 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7027 bidi_move_to_visually_next (&it->bidi_it);
7028 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7029 IT_CHARPOS (*it) = it->bidi_it.charpos;
7030 if (prev_scan_dir != it->bidi_it.scan_dir)
7031 {
7032 /* As the scan direction was changed, we must
7033 re-compute the stop position for composition. */
7034 ptrdiff_t stop = it->end_charpos;
7035 if (it->bidi_it.scan_dir < 0)
7036 stop = -1;
7037 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7038 IT_BYTEPOS (*it), stop, Qnil);
7039 }
7040 }
7041 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7042 }
7043 break;
7044
7045 case GET_FROM_C_STRING:
7046 /* Current display element of IT is from a C string. */
7047 if (!it->bidi_p
7048 /* If the string position is beyond string's end, it means
7049 next_element_from_c_string is padding the string with
7050 blanks, in which case we bypass the bidi iterator,
7051 because it cannot deal with such virtual characters. */
7052 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7053 {
7054 IT_BYTEPOS (*it) += it->len;
7055 IT_CHARPOS (*it) += 1;
7056 }
7057 else
7058 {
7059 bidi_move_to_visually_next (&it->bidi_it);
7060 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7061 IT_CHARPOS (*it) = it->bidi_it.charpos;
7062 }
7063 break;
7064
7065 case GET_FROM_DISPLAY_VECTOR:
7066 /* Current display element of IT is from a display table entry.
7067 Advance in the display table definition. Reset it to null if
7068 end reached, and continue with characters from buffers/
7069 strings. */
7070 ++it->current.dpvec_index;
7071
7072 /* Restore face of the iterator to what they were before the
7073 display vector entry (these entries may contain faces). */
7074 it->face_id = it->saved_face_id;
7075
7076 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7077 {
7078 int recheck_faces = it->ellipsis_p;
7079
7080 if (it->s)
7081 it->method = GET_FROM_C_STRING;
7082 else if (STRINGP (it->string))
7083 it->method = GET_FROM_STRING;
7084 else
7085 {
7086 it->method = GET_FROM_BUFFER;
7087 it->object = it->w->buffer;
7088 }
7089
7090 it->dpvec = NULL;
7091 it->current.dpvec_index = -1;
7092
7093 /* Skip over characters which were displayed via IT->dpvec. */
7094 if (it->dpvec_char_len < 0)
7095 reseat_at_next_visible_line_start (it, 1);
7096 else if (it->dpvec_char_len > 0)
7097 {
7098 if (it->method == GET_FROM_STRING
7099 && it->n_overlay_strings > 0)
7100 it->ignore_overlay_strings_at_pos_p = 1;
7101 it->len = it->dpvec_char_len;
7102 set_iterator_to_next (it, reseat_p);
7103 }
7104
7105 /* Maybe recheck faces after display vector */
7106 if (recheck_faces)
7107 it->stop_charpos = IT_CHARPOS (*it);
7108 }
7109 break;
7110
7111 case GET_FROM_STRING:
7112 /* Current display element is a character from a Lisp string. */
7113 eassert (it->s == NULL && STRINGP (it->string));
7114 /* Don't advance past string end. These conditions are true
7115 when set_iterator_to_next is called at the end of
7116 get_next_display_element, in which case the Lisp string is
7117 already exhausted, and all we want is pop the iterator
7118 stack. */
7119 if (it->current.overlay_string_index >= 0)
7120 {
7121 /* This is an overlay string, so there's no padding with
7122 spaces, and the number of characters in the string is
7123 where the string ends. */
7124 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7125 goto consider_string_end;
7126 }
7127 else
7128 {
7129 /* Not an overlay string. There could be padding, so test
7130 against it->end_charpos . */
7131 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7132 goto consider_string_end;
7133 }
7134 if (it->cmp_it.id >= 0)
7135 {
7136 int i;
7137
7138 if (! it->bidi_p)
7139 {
7140 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7141 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7142 if (it->cmp_it.to < it->cmp_it.nglyphs)
7143 it->cmp_it.from = it->cmp_it.to;
7144 else
7145 {
7146 it->cmp_it.id = -1;
7147 composition_compute_stop_pos (&it->cmp_it,
7148 IT_STRING_CHARPOS (*it),
7149 IT_STRING_BYTEPOS (*it),
7150 it->end_charpos, it->string);
7151 }
7152 }
7153 else if (! it->cmp_it.reversed_p)
7154 {
7155 for (i = 0; i < it->cmp_it.nchars; i++)
7156 bidi_move_to_visually_next (&it->bidi_it);
7157 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7158 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7159
7160 if (it->cmp_it.to < it->cmp_it.nglyphs)
7161 it->cmp_it.from = it->cmp_it.to;
7162 else
7163 {
7164 ptrdiff_t stop = it->end_charpos;
7165 if (it->bidi_it.scan_dir < 0)
7166 stop = -1;
7167 composition_compute_stop_pos (&it->cmp_it,
7168 IT_STRING_CHARPOS (*it),
7169 IT_STRING_BYTEPOS (*it), stop,
7170 it->string);
7171 }
7172 }
7173 else
7174 {
7175 for (i = 0; i < it->cmp_it.nchars; i++)
7176 bidi_move_to_visually_next (&it->bidi_it);
7177 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7178 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7179 if (it->cmp_it.from > 0)
7180 it->cmp_it.to = it->cmp_it.from;
7181 else
7182 {
7183 ptrdiff_t stop = it->end_charpos;
7184 if (it->bidi_it.scan_dir < 0)
7185 stop = -1;
7186 composition_compute_stop_pos (&it->cmp_it,
7187 IT_STRING_CHARPOS (*it),
7188 IT_STRING_BYTEPOS (*it), stop,
7189 it->string);
7190 }
7191 }
7192 }
7193 else
7194 {
7195 if (!it->bidi_p
7196 /* If the string position is beyond string's end, it
7197 means next_element_from_string is padding the string
7198 with blanks, in which case we bypass the bidi
7199 iterator, because it cannot deal with such virtual
7200 characters. */
7201 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7202 {
7203 IT_STRING_BYTEPOS (*it) += it->len;
7204 IT_STRING_CHARPOS (*it) += 1;
7205 }
7206 else
7207 {
7208 int prev_scan_dir = it->bidi_it.scan_dir;
7209
7210 bidi_move_to_visually_next (&it->bidi_it);
7211 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7212 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7213 if (prev_scan_dir != it->bidi_it.scan_dir)
7214 {
7215 ptrdiff_t stop = it->end_charpos;
7216
7217 if (it->bidi_it.scan_dir < 0)
7218 stop = -1;
7219 composition_compute_stop_pos (&it->cmp_it,
7220 IT_STRING_CHARPOS (*it),
7221 IT_STRING_BYTEPOS (*it), stop,
7222 it->string);
7223 }
7224 }
7225 }
7226
7227 consider_string_end:
7228
7229 if (it->current.overlay_string_index >= 0)
7230 {
7231 /* IT->string is an overlay string. Advance to the
7232 next, if there is one. */
7233 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7234 {
7235 it->ellipsis_p = 0;
7236 next_overlay_string (it);
7237 if (it->ellipsis_p)
7238 setup_for_ellipsis (it, 0);
7239 }
7240 }
7241 else
7242 {
7243 /* IT->string is not an overlay string. If we reached
7244 its end, and there is something on IT->stack, proceed
7245 with what is on the stack. This can be either another
7246 string, this time an overlay string, or a buffer. */
7247 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7248 && it->sp > 0)
7249 {
7250 pop_it (it);
7251 if (it->method == GET_FROM_STRING)
7252 goto consider_string_end;
7253 }
7254 }
7255 break;
7256
7257 case GET_FROM_IMAGE:
7258 case GET_FROM_STRETCH:
7259 /* The position etc with which we have to proceed are on
7260 the stack. The position may be at the end of a string,
7261 if the `display' property takes up the whole string. */
7262 eassert (it->sp > 0);
7263 pop_it (it);
7264 if (it->method == GET_FROM_STRING)
7265 goto consider_string_end;
7266 break;
7267
7268 default:
7269 /* There are no other methods defined, so this should be a bug. */
7270 abort ();
7271 }
7272
7273 eassert (it->method != GET_FROM_STRING
7274 || (STRINGP (it->string)
7275 && IT_STRING_CHARPOS (*it) >= 0));
7276 }
7277
7278 /* Load IT's display element fields with information about the next
7279 display element which comes from a display table entry or from the
7280 result of translating a control character to one of the forms `^C'
7281 or `\003'.
7282
7283 IT->dpvec holds the glyphs to return as characters.
7284 IT->saved_face_id holds the face id before the display vector--it
7285 is restored into IT->face_id in set_iterator_to_next. */
7286
7287 static int
7288 next_element_from_display_vector (struct it *it)
7289 {
7290 Lisp_Object gc;
7291
7292 /* Precondition. */
7293 eassert (it->dpvec && it->current.dpvec_index >= 0);
7294
7295 it->face_id = it->saved_face_id;
7296
7297 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7298 That seemed totally bogus - so I changed it... */
7299 gc = it->dpvec[it->current.dpvec_index];
7300
7301 if (GLYPH_CODE_P (gc))
7302 {
7303 it->c = GLYPH_CODE_CHAR (gc);
7304 it->len = CHAR_BYTES (it->c);
7305
7306 /* The entry may contain a face id to use. Such a face id is
7307 the id of a Lisp face, not a realized face. A face id of
7308 zero means no face is specified. */
7309 if (it->dpvec_face_id >= 0)
7310 it->face_id = it->dpvec_face_id;
7311 else
7312 {
7313 int lface_id = GLYPH_CODE_FACE (gc);
7314 if (lface_id > 0)
7315 it->face_id = merge_faces (it->f, Qt, lface_id,
7316 it->saved_face_id);
7317 }
7318 }
7319 else
7320 /* Display table entry is invalid. Return a space. */
7321 it->c = ' ', it->len = 1;
7322
7323 /* Don't change position and object of the iterator here. They are
7324 still the values of the character that had this display table
7325 entry or was translated, and that's what we want. */
7326 it->what = IT_CHARACTER;
7327 return 1;
7328 }
7329
7330 /* Get the first element of string/buffer in the visual order, after
7331 being reseated to a new position in a string or a buffer. */
7332 static void
7333 get_visually_first_element (struct it *it)
7334 {
7335 int string_p = STRINGP (it->string) || it->s;
7336 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7337 ptrdiff_t bob = (string_p ? 0 : BEGV);
7338
7339 if (STRINGP (it->string))
7340 {
7341 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7342 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7343 }
7344 else
7345 {
7346 it->bidi_it.charpos = IT_CHARPOS (*it);
7347 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7348 }
7349
7350 if (it->bidi_it.charpos == eob)
7351 {
7352 /* Nothing to do, but reset the FIRST_ELT flag, like
7353 bidi_paragraph_init does, because we are not going to
7354 call it. */
7355 it->bidi_it.first_elt = 0;
7356 }
7357 else if (it->bidi_it.charpos == bob
7358 || (!string_p
7359 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7360 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7361 {
7362 /* If we are at the beginning of a line/string, we can produce
7363 the next element right away. */
7364 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7365 bidi_move_to_visually_next (&it->bidi_it);
7366 }
7367 else
7368 {
7369 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7370
7371 /* We need to prime the bidi iterator starting at the line's or
7372 string's beginning, before we will be able to produce the
7373 next element. */
7374 if (string_p)
7375 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7376 else
7377 {
7378 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7379 -1);
7380 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7381 }
7382 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7383 do
7384 {
7385 /* Now return to buffer/string position where we were asked
7386 to get the next display element, and produce that. */
7387 bidi_move_to_visually_next (&it->bidi_it);
7388 }
7389 while (it->bidi_it.bytepos != orig_bytepos
7390 && it->bidi_it.charpos < eob);
7391 }
7392
7393 /* Adjust IT's position information to where we ended up. */
7394 if (STRINGP (it->string))
7395 {
7396 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7397 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7398 }
7399 else
7400 {
7401 IT_CHARPOS (*it) = it->bidi_it.charpos;
7402 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7403 }
7404
7405 if (STRINGP (it->string) || !it->s)
7406 {
7407 ptrdiff_t stop, charpos, bytepos;
7408
7409 if (STRINGP (it->string))
7410 {
7411 eassert (!it->s);
7412 stop = SCHARS (it->string);
7413 if (stop > it->end_charpos)
7414 stop = it->end_charpos;
7415 charpos = IT_STRING_CHARPOS (*it);
7416 bytepos = IT_STRING_BYTEPOS (*it);
7417 }
7418 else
7419 {
7420 stop = it->end_charpos;
7421 charpos = IT_CHARPOS (*it);
7422 bytepos = IT_BYTEPOS (*it);
7423 }
7424 if (it->bidi_it.scan_dir < 0)
7425 stop = -1;
7426 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7427 it->string);
7428 }
7429 }
7430
7431 /* Load IT with the next display element from Lisp string IT->string.
7432 IT->current.string_pos is the current position within the string.
7433 If IT->current.overlay_string_index >= 0, the Lisp string is an
7434 overlay string. */
7435
7436 static int
7437 next_element_from_string (struct it *it)
7438 {
7439 struct text_pos position;
7440
7441 eassert (STRINGP (it->string));
7442 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7443 eassert (IT_STRING_CHARPOS (*it) >= 0);
7444 position = it->current.string_pos;
7445
7446 /* With bidi reordering, the character to display might not be the
7447 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7448 that we were reseat()ed to a new string, whose paragraph
7449 direction is not known. */
7450 if (it->bidi_p && it->bidi_it.first_elt)
7451 {
7452 get_visually_first_element (it);
7453 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7454 }
7455
7456 /* Time to check for invisible text? */
7457 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7458 {
7459 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7460 {
7461 if (!(!it->bidi_p
7462 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7463 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7464 {
7465 /* With bidi non-linear iteration, we could find
7466 ourselves far beyond the last computed stop_charpos,
7467 with several other stop positions in between that we
7468 missed. Scan them all now, in buffer's logical
7469 order, until we find and handle the last stop_charpos
7470 that precedes our current position. */
7471 handle_stop_backwards (it, it->stop_charpos);
7472 return GET_NEXT_DISPLAY_ELEMENT (it);
7473 }
7474 else
7475 {
7476 if (it->bidi_p)
7477 {
7478 /* Take note of the stop position we just moved
7479 across, for when we will move back across it. */
7480 it->prev_stop = it->stop_charpos;
7481 /* If we are at base paragraph embedding level, take
7482 note of the last stop position seen at this
7483 level. */
7484 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7485 it->base_level_stop = it->stop_charpos;
7486 }
7487 handle_stop (it);
7488
7489 /* Since a handler may have changed IT->method, we must
7490 recurse here. */
7491 return GET_NEXT_DISPLAY_ELEMENT (it);
7492 }
7493 }
7494 else if (it->bidi_p
7495 /* If we are before prev_stop, we may have overstepped
7496 on our way backwards a stop_pos, and if so, we need
7497 to handle that stop_pos. */
7498 && IT_STRING_CHARPOS (*it) < it->prev_stop
7499 /* We can sometimes back up for reasons that have nothing
7500 to do with bidi reordering. E.g., compositions. The
7501 code below is only needed when we are above the base
7502 embedding level, so test for that explicitly. */
7503 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7504 {
7505 /* If we lost track of base_level_stop, we have no better
7506 place for handle_stop_backwards to start from than string
7507 beginning. This happens, e.g., when we were reseated to
7508 the previous screenful of text by vertical-motion. */
7509 if (it->base_level_stop <= 0
7510 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7511 it->base_level_stop = 0;
7512 handle_stop_backwards (it, it->base_level_stop);
7513 return GET_NEXT_DISPLAY_ELEMENT (it);
7514 }
7515 }
7516
7517 if (it->current.overlay_string_index >= 0)
7518 {
7519 /* Get the next character from an overlay string. In overlay
7520 strings, there is no field width or padding with spaces to
7521 do. */
7522 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7523 {
7524 it->what = IT_EOB;
7525 return 0;
7526 }
7527 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7528 IT_STRING_BYTEPOS (*it),
7529 it->bidi_it.scan_dir < 0
7530 ? -1
7531 : SCHARS (it->string))
7532 && next_element_from_composition (it))
7533 {
7534 return 1;
7535 }
7536 else if (STRING_MULTIBYTE (it->string))
7537 {
7538 const unsigned char *s = (SDATA (it->string)
7539 + IT_STRING_BYTEPOS (*it));
7540 it->c = string_char_and_length (s, &it->len);
7541 }
7542 else
7543 {
7544 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7545 it->len = 1;
7546 }
7547 }
7548 else
7549 {
7550 /* Get the next character from a Lisp string that is not an
7551 overlay string. Such strings come from the mode line, for
7552 example. We may have to pad with spaces, or truncate the
7553 string. See also next_element_from_c_string. */
7554 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7555 {
7556 it->what = IT_EOB;
7557 return 0;
7558 }
7559 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7560 {
7561 /* Pad with spaces. */
7562 it->c = ' ', it->len = 1;
7563 CHARPOS (position) = BYTEPOS (position) = -1;
7564 }
7565 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7566 IT_STRING_BYTEPOS (*it),
7567 it->bidi_it.scan_dir < 0
7568 ? -1
7569 : it->string_nchars)
7570 && next_element_from_composition (it))
7571 {
7572 return 1;
7573 }
7574 else if (STRING_MULTIBYTE (it->string))
7575 {
7576 const unsigned char *s = (SDATA (it->string)
7577 + IT_STRING_BYTEPOS (*it));
7578 it->c = string_char_and_length (s, &it->len);
7579 }
7580 else
7581 {
7582 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7583 it->len = 1;
7584 }
7585 }
7586
7587 /* Record what we have and where it came from. */
7588 it->what = IT_CHARACTER;
7589 it->object = it->string;
7590 it->position = position;
7591 return 1;
7592 }
7593
7594
7595 /* Load IT with next display element from C string IT->s.
7596 IT->string_nchars is the maximum number of characters to return
7597 from the string. IT->end_charpos may be greater than
7598 IT->string_nchars when this function is called, in which case we
7599 may have to return padding spaces. Value is zero if end of string
7600 reached, including padding spaces. */
7601
7602 static int
7603 next_element_from_c_string (struct it *it)
7604 {
7605 int success_p = 1;
7606
7607 eassert (it->s);
7608 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7609 it->what = IT_CHARACTER;
7610 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7611 it->object = Qnil;
7612
7613 /* With bidi reordering, the character to display might not be the
7614 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7615 we were reseated to a new string, whose paragraph direction is
7616 not known. */
7617 if (it->bidi_p && it->bidi_it.first_elt)
7618 get_visually_first_element (it);
7619
7620 /* IT's position can be greater than IT->string_nchars in case a
7621 field width or precision has been specified when the iterator was
7622 initialized. */
7623 if (IT_CHARPOS (*it) >= it->end_charpos)
7624 {
7625 /* End of the game. */
7626 it->what = IT_EOB;
7627 success_p = 0;
7628 }
7629 else if (IT_CHARPOS (*it) >= it->string_nchars)
7630 {
7631 /* Pad with spaces. */
7632 it->c = ' ', it->len = 1;
7633 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7634 }
7635 else if (it->multibyte_p)
7636 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7637 else
7638 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7639
7640 return success_p;
7641 }
7642
7643
7644 /* Set up IT to return characters from an ellipsis, if appropriate.
7645 The definition of the ellipsis glyphs may come from a display table
7646 entry. This function fills IT with the first glyph from the
7647 ellipsis if an ellipsis is to be displayed. */
7648
7649 static int
7650 next_element_from_ellipsis (struct it *it)
7651 {
7652 if (it->selective_display_ellipsis_p)
7653 setup_for_ellipsis (it, it->len);
7654 else
7655 {
7656 /* The face at the current position may be different from the
7657 face we find after the invisible text. Remember what it
7658 was in IT->saved_face_id, and signal that it's there by
7659 setting face_before_selective_p. */
7660 it->saved_face_id = it->face_id;
7661 it->method = GET_FROM_BUFFER;
7662 it->object = it->w->buffer;
7663 reseat_at_next_visible_line_start (it, 1);
7664 it->face_before_selective_p = 1;
7665 }
7666
7667 return GET_NEXT_DISPLAY_ELEMENT (it);
7668 }
7669
7670
7671 /* Deliver an image display element. The iterator IT is already
7672 filled with image information (done in handle_display_prop). Value
7673 is always 1. */
7674
7675
7676 static int
7677 next_element_from_image (struct it *it)
7678 {
7679 it->what = IT_IMAGE;
7680 it->ignore_overlay_strings_at_pos_p = 0;
7681 return 1;
7682 }
7683
7684
7685 /* Fill iterator IT with next display element from a stretch glyph
7686 property. IT->object is the value of the text property. Value is
7687 always 1. */
7688
7689 static int
7690 next_element_from_stretch (struct it *it)
7691 {
7692 it->what = IT_STRETCH;
7693 return 1;
7694 }
7695
7696 /* Scan backwards from IT's current position until we find a stop
7697 position, or until BEGV. This is called when we find ourself
7698 before both the last known prev_stop and base_level_stop while
7699 reordering bidirectional text. */
7700
7701 static void
7702 compute_stop_pos_backwards (struct it *it)
7703 {
7704 const int SCAN_BACK_LIMIT = 1000;
7705 struct text_pos pos;
7706 struct display_pos save_current = it->current;
7707 struct text_pos save_position = it->position;
7708 ptrdiff_t charpos = IT_CHARPOS (*it);
7709 ptrdiff_t where_we_are = charpos;
7710 ptrdiff_t save_stop_pos = it->stop_charpos;
7711 ptrdiff_t save_end_pos = it->end_charpos;
7712
7713 eassert (NILP (it->string) && !it->s);
7714 eassert (it->bidi_p);
7715 it->bidi_p = 0;
7716 do
7717 {
7718 it->end_charpos = min (charpos + 1, ZV);
7719 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7720 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7721 reseat_1 (it, pos, 0);
7722 compute_stop_pos (it);
7723 /* We must advance forward, right? */
7724 if (it->stop_charpos <= charpos)
7725 abort ();
7726 }
7727 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7728
7729 if (it->stop_charpos <= where_we_are)
7730 it->prev_stop = it->stop_charpos;
7731 else
7732 it->prev_stop = BEGV;
7733 it->bidi_p = 1;
7734 it->current = save_current;
7735 it->position = save_position;
7736 it->stop_charpos = save_stop_pos;
7737 it->end_charpos = save_end_pos;
7738 }
7739
7740 /* Scan forward from CHARPOS in the current buffer/string, until we
7741 find a stop position > current IT's position. Then handle the stop
7742 position before that. This is called when we bump into a stop
7743 position while reordering bidirectional text. CHARPOS should be
7744 the last previously processed stop_pos (or BEGV/0, if none were
7745 processed yet) whose position is less that IT's current
7746 position. */
7747
7748 static void
7749 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7750 {
7751 int bufp = !STRINGP (it->string);
7752 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7753 struct display_pos save_current = it->current;
7754 struct text_pos save_position = it->position;
7755 struct text_pos pos1;
7756 ptrdiff_t next_stop;
7757
7758 /* Scan in strict logical order. */
7759 eassert (it->bidi_p);
7760 it->bidi_p = 0;
7761 do
7762 {
7763 it->prev_stop = charpos;
7764 if (bufp)
7765 {
7766 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7767 reseat_1 (it, pos1, 0);
7768 }
7769 else
7770 it->current.string_pos = string_pos (charpos, it->string);
7771 compute_stop_pos (it);
7772 /* We must advance forward, right? */
7773 if (it->stop_charpos <= it->prev_stop)
7774 abort ();
7775 charpos = it->stop_charpos;
7776 }
7777 while (charpos <= where_we_are);
7778
7779 it->bidi_p = 1;
7780 it->current = save_current;
7781 it->position = save_position;
7782 next_stop = it->stop_charpos;
7783 it->stop_charpos = it->prev_stop;
7784 handle_stop (it);
7785 it->stop_charpos = next_stop;
7786 }
7787
7788 /* Load IT with the next display element from current_buffer. Value
7789 is zero if end of buffer reached. IT->stop_charpos is the next
7790 position at which to stop and check for text properties or buffer
7791 end. */
7792
7793 static int
7794 next_element_from_buffer (struct it *it)
7795 {
7796 int success_p = 1;
7797
7798 eassert (IT_CHARPOS (*it) >= BEGV);
7799 eassert (NILP (it->string) && !it->s);
7800 eassert (!it->bidi_p
7801 || (EQ (it->bidi_it.string.lstring, Qnil)
7802 && it->bidi_it.string.s == NULL));
7803
7804 /* With bidi reordering, the character to display might not be the
7805 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7806 we were reseat()ed to a new buffer position, which is potentially
7807 a different paragraph. */
7808 if (it->bidi_p && it->bidi_it.first_elt)
7809 {
7810 get_visually_first_element (it);
7811 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7812 }
7813
7814 if (IT_CHARPOS (*it) >= it->stop_charpos)
7815 {
7816 if (IT_CHARPOS (*it) >= it->end_charpos)
7817 {
7818 int overlay_strings_follow_p;
7819
7820 /* End of the game, except when overlay strings follow that
7821 haven't been returned yet. */
7822 if (it->overlay_strings_at_end_processed_p)
7823 overlay_strings_follow_p = 0;
7824 else
7825 {
7826 it->overlay_strings_at_end_processed_p = 1;
7827 overlay_strings_follow_p = get_overlay_strings (it, 0);
7828 }
7829
7830 if (overlay_strings_follow_p)
7831 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7832 else
7833 {
7834 it->what = IT_EOB;
7835 it->position = it->current.pos;
7836 success_p = 0;
7837 }
7838 }
7839 else if (!(!it->bidi_p
7840 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7841 || IT_CHARPOS (*it) == it->stop_charpos))
7842 {
7843 /* With bidi non-linear iteration, we could find ourselves
7844 far beyond the last computed stop_charpos, with several
7845 other stop positions in between that we missed. Scan
7846 them all now, in buffer's logical order, until we find
7847 and handle the last stop_charpos that precedes our
7848 current position. */
7849 handle_stop_backwards (it, it->stop_charpos);
7850 return GET_NEXT_DISPLAY_ELEMENT (it);
7851 }
7852 else
7853 {
7854 if (it->bidi_p)
7855 {
7856 /* Take note of the stop position we just moved across,
7857 for when we will move back across it. */
7858 it->prev_stop = it->stop_charpos;
7859 /* If we are at base paragraph embedding level, take
7860 note of the last stop position seen at this
7861 level. */
7862 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7863 it->base_level_stop = it->stop_charpos;
7864 }
7865 handle_stop (it);
7866 return GET_NEXT_DISPLAY_ELEMENT (it);
7867 }
7868 }
7869 else if (it->bidi_p
7870 /* If we are before prev_stop, we may have overstepped on
7871 our way backwards a stop_pos, and if so, we need to
7872 handle that stop_pos. */
7873 && IT_CHARPOS (*it) < it->prev_stop
7874 /* We can sometimes back up for reasons that have nothing
7875 to do with bidi reordering. E.g., compositions. The
7876 code below is only needed when we are above the base
7877 embedding level, so test for that explicitly. */
7878 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7879 {
7880 if (it->base_level_stop <= 0
7881 || IT_CHARPOS (*it) < it->base_level_stop)
7882 {
7883 /* If we lost track of base_level_stop, we need to find
7884 prev_stop by looking backwards. This happens, e.g., when
7885 we were reseated to the previous screenful of text by
7886 vertical-motion. */
7887 it->base_level_stop = BEGV;
7888 compute_stop_pos_backwards (it);
7889 handle_stop_backwards (it, it->prev_stop);
7890 }
7891 else
7892 handle_stop_backwards (it, it->base_level_stop);
7893 return GET_NEXT_DISPLAY_ELEMENT (it);
7894 }
7895 else
7896 {
7897 /* No face changes, overlays etc. in sight, so just return a
7898 character from current_buffer. */
7899 unsigned char *p;
7900 ptrdiff_t stop;
7901
7902 /* Maybe run the redisplay end trigger hook. Performance note:
7903 This doesn't seem to cost measurable time. */
7904 if (it->redisplay_end_trigger_charpos
7905 && it->glyph_row
7906 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7907 run_redisplay_end_trigger_hook (it);
7908
7909 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7910 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7911 stop)
7912 && next_element_from_composition (it))
7913 {
7914 return 1;
7915 }
7916
7917 /* Get the next character, maybe multibyte. */
7918 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7919 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7920 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7921 else
7922 it->c = *p, it->len = 1;
7923
7924 /* Record what we have and where it came from. */
7925 it->what = IT_CHARACTER;
7926 it->object = it->w->buffer;
7927 it->position = it->current.pos;
7928
7929 /* Normally we return the character found above, except when we
7930 really want to return an ellipsis for selective display. */
7931 if (it->selective)
7932 {
7933 if (it->c == '\n')
7934 {
7935 /* A value of selective > 0 means hide lines indented more
7936 than that number of columns. */
7937 if (it->selective > 0
7938 && IT_CHARPOS (*it) + 1 < ZV
7939 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7940 IT_BYTEPOS (*it) + 1,
7941 it->selective))
7942 {
7943 success_p = next_element_from_ellipsis (it);
7944 it->dpvec_char_len = -1;
7945 }
7946 }
7947 else if (it->c == '\r' && it->selective == -1)
7948 {
7949 /* A value of selective == -1 means that everything from the
7950 CR to the end of the line is invisible, with maybe an
7951 ellipsis displayed for it. */
7952 success_p = next_element_from_ellipsis (it);
7953 it->dpvec_char_len = -1;
7954 }
7955 }
7956 }
7957
7958 /* Value is zero if end of buffer reached. */
7959 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7960 return success_p;
7961 }
7962
7963
7964 /* Run the redisplay end trigger hook for IT. */
7965
7966 static void
7967 run_redisplay_end_trigger_hook (struct it *it)
7968 {
7969 Lisp_Object args[3];
7970
7971 /* IT->glyph_row should be non-null, i.e. we should be actually
7972 displaying something, or otherwise we should not run the hook. */
7973 eassert (it->glyph_row);
7974
7975 /* Set up hook arguments. */
7976 args[0] = Qredisplay_end_trigger_functions;
7977 args[1] = it->window;
7978 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7979 it->redisplay_end_trigger_charpos = 0;
7980
7981 /* Since we are *trying* to run these functions, don't try to run
7982 them again, even if they get an error. */
7983 it->w->redisplay_end_trigger = Qnil;
7984 Frun_hook_with_args (3, args);
7985
7986 /* Notice if it changed the face of the character we are on. */
7987 handle_face_prop (it);
7988 }
7989
7990
7991 /* Deliver a composition display element. Unlike the other
7992 next_element_from_XXX, this function is not registered in the array
7993 get_next_element[]. It is called from next_element_from_buffer and
7994 next_element_from_string when necessary. */
7995
7996 static int
7997 next_element_from_composition (struct it *it)
7998 {
7999 it->what = IT_COMPOSITION;
8000 it->len = it->cmp_it.nbytes;
8001 if (STRINGP (it->string))
8002 {
8003 if (it->c < 0)
8004 {
8005 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8006 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8007 return 0;
8008 }
8009 it->position = it->current.string_pos;
8010 it->object = it->string;
8011 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8012 IT_STRING_BYTEPOS (*it), it->string);
8013 }
8014 else
8015 {
8016 if (it->c < 0)
8017 {
8018 IT_CHARPOS (*it) += it->cmp_it.nchars;
8019 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8020 if (it->bidi_p)
8021 {
8022 if (it->bidi_it.new_paragraph)
8023 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8024 /* Resync the bidi iterator with IT's new position.
8025 FIXME: this doesn't support bidirectional text. */
8026 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8027 bidi_move_to_visually_next (&it->bidi_it);
8028 }
8029 return 0;
8030 }
8031 it->position = it->current.pos;
8032 it->object = it->w->buffer;
8033 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8034 IT_BYTEPOS (*it), Qnil);
8035 }
8036 return 1;
8037 }
8038
8039
8040 \f
8041 /***********************************************************************
8042 Moving an iterator without producing glyphs
8043 ***********************************************************************/
8044
8045 /* Check if iterator is at a position corresponding to a valid buffer
8046 position after some move_it_ call. */
8047
8048 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8049 ((it)->method == GET_FROM_STRING \
8050 ? IT_STRING_CHARPOS (*it) == 0 \
8051 : 1)
8052
8053
8054 /* Move iterator IT to a specified buffer or X position within one
8055 line on the display without producing glyphs.
8056
8057 OP should be a bit mask including some or all of these bits:
8058 MOVE_TO_X: Stop upon reaching x-position TO_X.
8059 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8060 Regardless of OP's value, stop upon reaching the end of the display line.
8061
8062 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8063 This means, in particular, that TO_X includes window's horizontal
8064 scroll amount.
8065
8066 The return value has several possible values that
8067 say what condition caused the scan to stop:
8068
8069 MOVE_POS_MATCH_OR_ZV
8070 - when TO_POS or ZV was reached.
8071
8072 MOVE_X_REACHED
8073 -when TO_X was reached before TO_POS or ZV were reached.
8074
8075 MOVE_LINE_CONTINUED
8076 - when we reached the end of the display area and the line must
8077 be continued.
8078
8079 MOVE_LINE_TRUNCATED
8080 - when we reached the end of the display area and the line is
8081 truncated.
8082
8083 MOVE_NEWLINE_OR_CR
8084 - when we stopped at a line end, i.e. a newline or a CR and selective
8085 display is on. */
8086
8087 static enum move_it_result
8088 move_it_in_display_line_to (struct it *it,
8089 ptrdiff_t to_charpos, int to_x,
8090 enum move_operation_enum op)
8091 {
8092 enum move_it_result result = MOVE_UNDEFINED;
8093 struct glyph_row *saved_glyph_row;
8094 struct it wrap_it, atpos_it, atx_it, ppos_it;
8095 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8096 void *ppos_data = NULL;
8097 int may_wrap = 0;
8098 enum it_method prev_method = it->method;
8099 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8100 int saw_smaller_pos = prev_pos < to_charpos;
8101
8102 /* Don't produce glyphs in produce_glyphs. */
8103 saved_glyph_row = it->glyph_row;
8104 it->glyph_row = NULL;
8105
8106 /* Use wrap_it to save a copy of IT wherever a word wrap could
8107 occur. Use atpos_it to save a copy of IT at the desired buffer
8108 position, if found, so that we can scan ahead and check if the
8109 word later overshoots the window edge. Use atx_it similarly, for
8110 pixel positions. */
8111 wrap_it.sp = -1;
8112 atpos_it.sp = -1;
8113 atx_it.sp = -1;
8114
8115 /* Use ppos_it under bidi reordering to save a copy of IT for the
8116 position > CHARPOS that is the closest to CHARPOS. We restore
8117 that position in IT when we have scanned the entire display line
8118 without finding a match for CHARPOS and all the character
8119 positions are greater than CHARPOS. */
8120 if (it->bidi_p)
8121 {
8122 SAVE_IT (ppos_it, *it, ppos_data);
8123 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8124 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8125 SAVE_IT (ppos_it, *it, ppos_data);
8126 }
8127
8128 #define BUFFER_POS_REACHED_P() \
8129 ((op & MOVE_TO_POS) != 0 \
8130 && BUFFERP (it->object) \
8131 && (IT_CHARPOS (*it) == to_charpos \
8132 || ((!it->bidi_p \
8133 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8134 && IT_CHARPOS (*it) > to_charpos) \
8135 || (it->what == IT_COMPOSITION \
8136 && ((IT_CHARPOS (*it) > to_charpos \
8137 && to_charpos >= it->cmp_it.charpos) \
8138 || (IT_CHARPOS (*it) < to_charpos \
8139 && to_charpos <= it->cmp_it.charpos)))) \
8140 && (it->method == GET_FROM_BUFFER \
8141 || (it->method == GET_FROM_DISPLAY_VECTOR \
8142 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8143
8144 /* If there's a line-/wrap-prefix, handle it. */
8145 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8146 && it->current_y < it->last_visible_y)
8147 handle_line_prefix (it);
8148
8149 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8150 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8151
8152 while (1)
8153 {
8154 int x, i, ascent = 0, descent = 0;
8155
8156 /* Utility macro to reset an iterator with x, ascent, and descent. */
8157 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8158 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8159 (IT)->max_descent = descent)
8160
8161 /* Stop if we move beyond TO_CHARPOS (after an image or a
8162 display string or stretch glyph). */
8163 if ((op & MOVE_TO_POS) != 0
8164 && BUFFERP (it->object)
8165 && it->method == GET_FROM_BUFFER
8166 && (((!it->bidi_p
8167 /* When the iterator is at base embedding level, we
8168 are guaranteed that characters are delivered for
8169 display in strictly increasing order of their
8170 buffer positions. */
8171 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8172 && IT_CHARPOS (*it) > to_charpos)
8173 || (it->bidi_p
8174 && (prev_method == GET_FROM_IMAGE
8175 || prev_method == GET_FROM_STRETCH
8176 || prev_method == GET_FROM_STRING)
8177 /* Passed TO_CHARPOS from left to right. */
8178 && ((prev_pos < to_charpos
8179 && IT_CHARPOS (*it) > to_charpos)
8180 /* Passed TO_CHARPOS from right to left. */
8181 || (prev_pos > to_charpos
8182 && IT_CHARPOS (*it) < to_charpos)))))
8183 {
8184 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8185 {
8186 result = MOVE_POS_MATCH_OR_ZV;
8187 break;
8188 }
8189 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8190 /* If wrap_it is valid, the current position might be in a
8191 word that is wrapped. So, save the iterator in
8192 atpos_it and continue to see if wrapping happens. */
8193 SAVE_IT (atpos_it, *it, atpos_data);
8194 }
8195
8196 /* Stop when ZV reached.
8197 We used to stop here when TO_CHARPOS reached as well, but that is
8198 too soon if this glyph does not fit on this line. So we handle it
8199 explicitly below. */
8200 if (!get_next_display_element (it))
8201 {
8202 result = MOVE_POS_MATCH_OR_ZV;
8203 break;
8204 }
8205
8206 if (it->line_wrap == TRUNCATE)
8207 {
8208 if (BUFFER_POS_REACHED_P ())
8209 {
8210 result = MOVE_POS_MATCH_OR_ZV;
8211 break;
8212 }
8213 }
8214 else
8215 {
8216 if (it->line_wrap == WORD_WRAP)
8217 {
8218 if (IT_DISPLAYING_WHITESPACE (it))
8219 may_wrap = 1;
8220 else if (may_wrap)
8221 {
8222 /* We have reached a glyph that follows one or more
8223 whitespace characters. If the position is
8224 already found, we are done. */
8225 if (atpos_it.sp >= 0)
8226 {
8227 RESTORE_IT (it, &atpos_it, atpos_data);
8228 result = MOVE_POS_MATCH_OR_ZV;
8229 goto done;
8230 }
8231 if (atx_it.sp >= 0)
8232 {
8233 RESTORE_IT (it, &atx_it, atx_data);
8234 result = MOVE_X_REACHED;
8235 goto done;
8236 }
8237 /* Otherwise, we can wrap here. */
8238 SAVE_IT (wrap_it, *it, wrap_data);
8239 may_wrap = 0;
8240 }
8241 }
8242 }
8243
8244 /* Remember the line height for the current line, in case
8245 the next element doesn't fit on the line. */
8246 ascent = it->max_ascent;
8247 descent = it->max_descent;
8248
8249 /* The call to produce_glyphs will get the metrics of the
8250 display element IT is loaded with. Record the x-position
8251 before this display element, in case it doesn't fit on the
8252 line. */
8253 x = it->current_x;
8254
8255 PRODUCE_GLYPHS (it);
8256
8257 if (it->area != TEXT_AREA)
8258 {
8259 prev_method = it->method;
8260 if (it->method == GET_FROM_BUFFER)
8261 prev_pos = IT_CHARPOS (*it);
8262 set_iterator_to_next (it, 1);
8263 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8264 SET_TEXT_POS (this_line_min_pos,
8265 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8266 if (it->bidi_p
8267 && (op & MOVE_TO_POS)
8268 && IT_CHARPOS (*it) > to_charpos
8269 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8270 SAVE_IT (ppos_it, *it, ppos_data);
8271 continue;
8272 }
8273
8274 /* The number of glyphs we get back in IT->nglyphs will normally
8275 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8276 character on a terminal frame, or (iii) a line end. For the
8277 second case, IT->nglyphs - 1 padding glyphs will be present.
8278 (On X frames, there is only one glyph produced for a
8279 composite character.)
8280
8281 The behavior implemented below means, for continuation lines,
8282 that as many spaces of a TAB as fit on the current line are
8283 displayed there. For terminal frames, as many glyphs of a
8284 multi-glyph character are displayed in the current line, too.
8285 This is what the old redisplay code did, and we keep it that
8286 way. Under X, the whole shape of a complex character must
8287 fit on the line or it will be completely displayed in the
8288 next line.
8289
8290 Note that both for tabs and padding glyphs, all glyphs have
8291 the same width. */
8292 if (it->nglyphs)
8293 {
8294 /* More than one glyph or glyph doesn't fit on line. All
8295 glyphs have the same width. */
8296 int single_glyph_width = it->pixel_width / it->nglyphs;
8297 int new_x;
8298 int x_before_this_char = x;
8299 int hpos_before_this_char = it->hpos;
8300
8301 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8302 {
8303 new_x = x + single_glyph_width;
8304
8305 /* We want to leave anything reaching TO_X to the caller. */
8306 if ((op & MOVE_TO_X) && new_x > to_x)
8307 {
8308 if (BUFFER_POS_REACHED_P ())
8309 {
8310 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8311 goto buffer_pos_reached;
8312 if (atpos_it.sp < 0)
8313 {
8314 SAVE_IT (atpos_it, *it, atpos_data);
8315 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8316 }
8317 }
8318 else
8319 {
8320 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8321 {
8322 it->current_x = x;
8323 result = MOVE_X_REACHED;
8324 break;
8325 }
8326 if (atx_it.sp < 0)
8327 {
8328 SAVE_IT (atx_it, *it, atx_data);
8329 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8330 }
8331 }
8332 }
8333
8334 if (/* Lines are continued. */
8335 it->line_wrap != TRUNCATE
8336 && (/* And glyph doesn't fit on the line. */
8337 new_x > it->last_visible_x
8338 /* Or it fits exactly and we're on a window
8339 system frame. */
8340 || (new_x == it->last_visible_x
8341 && FRAME_WINDOW_P (it->f)
8342 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8343 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8344 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8345 {
8346 if (/* IT->hpos == 0 means the very first glyph
8347 doesn't fit on the line, e.g. a wide image. */
8348 it->hpos == 0
8349 || (new_x == it->last_visible_x
8350 && FRAME_WINDOW_P (it->f)))
8351 {
8352 ++it->hpos;
8353 it->current_x = new_x;
8354
8355 /* The character's last glyph just barely fits
8356 in this row. */
8357 if (i == it->nglyphs - 1)
8358 {
8359 /* If this is the destination position,
8360 return a position *before* it in this row,
8361 now that we know it fits in this row. */
8362 if (BUFFER_POS_REACHED_P ())
8363 {
8364 if (it->line_wrap != WORD_WRAP
8365 || wrap_it.sp < 0)
8366 {
8367 it->hpos = hpos_before_this_char;
8368 it->current_x = x_before_this_char;
8369 result = MOVE_POS_MATCH_OR_ZV;
8370 break;
8371 }
8372 if (it->line_wrap == WORD_WRAP
8373 && atpos_it.sp < 0)
8374 {
8375 SAVE_IT (atpos_it, *it, atpos_data);
8376 atpos_it.current_x = x_before_this_char;
8377 atpos_it.hpos = hpos_before_this_char;
8378 }
8379 }
8380
8381 prev_method = it->method;
8382 if (it->method == GET_FROM_BUFFER)
8383 prev_pos = IT_CHARPOS (*it);
8384 set_iterator_to_next (it, 1);
8385 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8386 SET_TEXT_POS (this_line_min_pos,
8387 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8388 /* On graphical terminals, newlines may
8389 "overflow" into the fringe if
8390 overflow-newline-into-fringe is non-nil.
8391 On text terminals, and on graphical
8392 terminals with no right margin, newlines
8393 may overflow into the last glyph on the
8394 display line.*/
8395 if (!FRAME_WINDOW_P (it->f)
8396 || ((it->bidi_p
8397 && it->bidi_it.paragraph_dir == R2L)
8398 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8399 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8400 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8401 {
8402 if (!get_next_display_element (it))
8403 {
8404 result = MOVE_POS_MATCH_OR_ZV;
8405 break;
8406 }
8407 if (BUFFER_POS_REACHED_P ())
8408 {
8409 if (ITERATOR_AT_END_OF_LINE_P (it))
8410 result = MOVE_POS_MATCH_OR_ZV;
8411 else
8412 result = MOVE_LINE_CONTINUED;
8413 break;
8414 }
8415 if (ITERATOR_AT_END_OF_LINE_P (it))
8416 {
8417 result = MOVE_NEWLINE_OR_CR;
8418 break;
8419 }
8420 }
8421 }
8422 }
8423 else
8424 IT_RESET_X_ASCENT_DESCENT (it);
8425
8426 if (wrap_it.sp >= 0)
8427 {
8428 RESTORE_IT (it, &wrap_it, wrap_data);
8429 atpos_it.sp = -1;
8430 atx_it.sp = -1;
8431 }
8432
8433 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8434 IT_CHARPOS (*it)));
8435 result = MOVE_LINE_CONTINUED;
8436 break;
8437 }
8438
8439 if (BUFFER_POS_REACHED_P ())
8440 {
8441 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8442 goto buffer_pos_reached;
8443 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8444 {
8445 SAVE_IT (atpos_it, *it, atpos_data);
8446 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8447 }
8448 }
8449
8450 if (new_x > it->first_visible_x)
8451 {
8452 /* Glyph is visible. Increment number of glyphs that
8453 would be displayed. */
8454 ++it->hpos;
8455 }
8456 }
8457
8458 if (result != MOVE_UNDEFINED)
8459 break;
8460 }
8461 else if (BUFFER_POS_REACHED_P ())
8462 {
8463 buffer_pos_reached:
8464 IT_RESET_X_ASCENT_DESCENT (it);
8465 result = MOVE_POS_MATCH_OR_ZV;
8466 break;
8467 }
8468 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8469 {
8470 /* Stop when TO_X specified and reached. This check is
8471 necessary here because of lines consisting of a line end,
8472 only. The line end will not produce any glyphs and we
8473 would never get MOVE_X_REACHED. */
8474 eassert (it->nglyphs == 0);
8475 result = MOVE_X_REACHED;
8476 break;
8477 }
8478
8479 /* Is this a line end? If yes, we're done. */
8480 if (ITERATOR_AT_END_OF_LINE_P (it))
8481 {
8482 /* If we are past TO_CHARPOS, but never saw any character
8483 positions smaller than TO_CHARPOS, return
8484 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8485 did. */
8486 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8487 {
8488 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8489 {
8490 if (IT_CHARPOS (ppos_it) < ZV)
8491 {
8492 RESTORE_IT (it, &ppos_it, ppos_data);
8493 result = MOVE_POS_MATCH_OR_ZV;
8494 }
8495 else
8496 goto buffer_pos_reached;
8497 }
8498 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8499 && IT_CHARPOS (*it) > to_charpos)
8500 goto buffer_pos_reached;
8501 else
8502 result = MOVE_NEWLINE_OR_CR;
8503 }
8504 else
8505 result = MOVE_NEWLINE_OR_CR;
8506 break;
8507 }
8508
8509 prev_method = it->method;
8510 if (it->method == GET_FROM_BUFFER)
8511 prev_pos = IT_CHARPOS (*it);
8512 /* The current display element has been consumed. Advance
8513 to the next. */
8514 set_iterator_to_next (it, 1);
8515 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8516 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8517 if (IT_CHARPOS (*it) < to_charpos)
8518 saw_smaller_pos = 1;
8519 if (it->bidi_p
8520 && (op & MOVE_TO_POS)
8521 && IT_CHARPOS (*it) >= to_charpos
8522 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8523 SAVE_IT (ppos_it, *it, ppos_data);
8524
8525 /* Stop if lines are truncated and IT's current x-position is
8526 past the right edge of the window now. */
8527 if (it->line_wrap == TRUNCATE
8528 && it->current_x >= it->last_visible_x)
8529 {
8530 if (!FRAME_WINDOW_P (it->f)
8531 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8532 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8533 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8534 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8535 {
8536 int at_eob_p = 0;
8537
8538 if ((at_eob_p = !get_next_display_element (it))
8539 || BUFFER_POS_REACHED_P ()
8540 /* If we are past TO_CHARPOS, but never saw any
8541 character positions smaller than TO_CHARPOS,
8542 return MOVE_POS_MATCH_OR_ZV, like the
8543 unidirectional display did. */
8544 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8545 && !saw_smaller_pos
8546 && IT_CHARPOS (*it) > to_charpos))
8547 {
8548 if (it->bidi_p
8549 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8550 RESTORE_IT (it, &ppos_it, ppos_data);
8551 result = MOVE_POS_MATCH_OR_ZV;
8552 break;
8553 }
8554 if (ITERATOR_AT_END_OF_LINE_P (it))
8555 {
8556 result = MOVE_NEWLINE_OR_CR;
8557 break;
8558 }
8559 }
8560 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8561 && !saw_smaller_pos
8562 && IT_CHARPOS (*it) > to_charpos)
8563 {
8564 if (IT_CHARPOS (ppos_it) < ZV)
8565 RESTORE_IT (it, &ppos_it, ppos_data);
8566 result = MOVE_POS_MATCH_OR_ZV;
8567 break;
8568 }
8569 result = MOVE_LINE_TRUNCATED;
8570 break;
8571 }
8572 #undef IT_RESET_X_ASCENT_DESCENT
8573 }
8574
8575 #undef BUFFER_POS_REACHED_P
8576
8577 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8578 restore the saved iterator. */
8579 if (atpos_it.sp >= 0)
8580 RESTORE_IT (it, &atpos_it, atpos_data);
8581 else if (atx_it.sp >= 0)
8582 RESTORE_IT (it, &atx_it, atx_data);
8583
8584 done:
8585
8586 if (atpos_data)
8587 bidi_unshelve_cache (atpos_data, 1);
8588 if (atx_data)
8589 bidi_unshelve_cache (atx_data, 1);
8590 if (wrap_data)
8591 bidi_unshelve_cache (wrap_data, 1);
8592 if (ppos_data)
8593 bidi_unshelve_cache (ppos_data, 1);
8594
8595 /* Restore the iterator settings altered at the beginning of this
8596 function. */
8597 it->glyph_row = saved_glyph_row;
8598 return result;
8599 }
8600
8601 /* For external use. */
8602 void
8603 move_it_in_display_line (struct it *it,
8604 ptrdiff_t to_charpos, int to_x,
8605 enum move_operation_enum op)
8606 {
8607 if (it->line_wrap == WORD_WRAP
8608 && (op & MOVE_TO_X))
8609 {
8610 struct it save_it;
8611 void *save_data = NULL;
8612 int skip;
8613
8614 SAVE_IT (save_it, *it, save_data);
8615 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8616 /* When word-wrap is on, TO_X may lie past the end
8617 of a wrapped line. Then it->current is the
8618 character on the next line, so backtrack to the
8619 space before the wrap point. */
8620 if (skip == MOVE_LINE_CONTINUED)
8621 {
8622 int prev_x = max (it->current_x - 1, 0);
8623 RESTORE_IT (it, &save_it, save_data);
8624 move_it_in_display_line_to
8625 (it, -1, prev_x, MOVE_TO_X);
8626 }
8627 else
8628 bidi_unshelve_cache (save_data, 1);
8629 }
8630 else
8631 move_it_in_display_line_to (it, to_charpos, to_x, op);
8632 }
8633
8634
8635 /* Move IT forward until it satisfies one or more of the criteria in
8636 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8637
8638 OP is a bit-mask that specifies where to stop, and in particular,
8639 which of those four position arguments makes a difference. See the
8640 description of enum move_operation_enum.
8641
8642 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8643 screen line, this function will set IT to the next position that is
8644 displayed to the right of TO_CHARPOS on the screen. */
8645
8646 void
8647 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8648 {
8649 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8650 int line_height, line_start_x = 0, reached = 0;
8651 void *backup_data = NULL;
8652
8653 for (;;)
8654 {
8655 if (op & MOVE_TO_VPOS)
8656 {
8657 /* If no TO_CHARPOS and no TO_X specified, stop at the
8658 start of the line TO_VPOS. */
8659 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8660 {
8661 if (it->vpos == to_vpos)
8662 {
8663 reached = 1;
8664 break;
8665 }
8666 else
8667 skip = move_it_in_display_line_to (it, -1, -1, 0);
8668 }
8669 else
8670 {
8671 /* TO_VPOS >= 0 means stop at TO_X in the line at
8672 TO_VPOS, or at TO_POS, whichever comes first. */
8673 if (it->vpos == to_vpos)
8674 {
8675 reached = 2;
8676 break;
8677 }
8678
8679 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8680
8681 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8682 {
8683 reached = 3;
8684 break;
8685 }
8686 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8687 {
8688 /* We have reached TO_X but not in the line we want. */
8689 skip = move_it_in_display_line_to (it, to_charpos,
8690 -1, MOVE_TO_POS);
8691 if (skip == MOVE_POS_MATCH_OR_ZV)
8692 {
8693 reached = 4;
8694 break;
8695 }
8696 }
8697 }
8698 }
8699 else if (op & MOVE_TO_Y)
8700 {
8701 struct it it_backup;
8702
8703 if (it->line_wrap == WORD_WRAP)
8704 SAVE_IT (it_backup, *it, backup_data);
8705
8706 /* TO_Y specified means stop at TO_X in the line containing
8707 TO_Y---or at TO_CHARPOS if this is reached first. The
8708 problem is that we can't really tell whether the line
8709 contains TO_Y before we have completely scanned it, and
8710 this may skip past TO_X. What we do is to first scan to
8711 TO_X.
8712
8713 If TO_X is not specified, use a TO_X of zero. The reason
8714 is to make the outcome of this function more predictable.
8715 If we didn't use TO_X == 0, we would stop at the end of
8716 the line which is probably not what a caller would expect
8717 to happen. */
8718 skip = move_it_in_display_line_to
8719 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8720 (MOVE_TO_X | (op & MOVE_TO_POS)));
8721
8722 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8723 if (skip == MOVE_POS_MATCH_OR_ZV)
8724 reached = 5;
8725 else if (skip == MOVE_X_REACHED)
8726 {
8727 /* If TO_X was reached, we want to know whether TO_Y is
8728 in the line. We know this is the case if the already
8729 scanned glyphs make the line tall enough. Otherwise,
8730 we must check by scanning the rest of the line. */
8731 line_height = it->max_ascent + it->max_descent;
8732 if (to_y >= it->current_y
8733 && to_y < it->current_y + line_height)
8734 {
8735 reached = 6;
8736 break;
8737 }
8738 SAVE_IT (it_backup, *it, backup_data);
8739 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8740 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8741 op & MOVE_TO_POS);
8742 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8743 line_height = it->max_ascent + it->max_descent;
8744 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8745
8746 if (to_y >= it->current_y
8747 && to_y < it->current_y + line_height)
8748 {
8749 /* If TO_Y is in this line and TO_X was reached
8750 above, we scanned too far. We have to restore
8751 IT's settings to the ones before skipping. But
8752 keep the more accurate values of max_ascent and
8753 max_descent we've found while skipping the rest
8754 of the line, for the sake of callers, such as
8755 pos_visible_p, that need to know the line
8756 height. */
8757 int max_ascent = it->max_ascent;
8758 int max_descent = it->max_descent;
8759
8760 RESTORE_IT (it, &it_backup, backup_data);
8761 it->max_ascent = max_ascent;
8762 it->max_descent = max_descent;
8763 reached = 6;
8764 }
8765 else
8766 {
8767 skip = skip2;
8768 if (skip == MOVE_POS_MATCH_OR_ZV)
8769 reached = 7;
8770 }
8771 }
8772 else
8773 {
8774 /* Check whether TO_Y is in this line. */
8775 line_height = it->max_ascent + it->max_descent;
8776 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8777
8778 if (to_y >= it->current_y
8779 && to_y < it->current_y + line_height)
8780 {
8781 /* When word-wrap is on, TO_X may lie past the end
8782 of a wrapped line. Then it->current is the
8783 character on the next line, so backtrack to the
8784 space before the wrap point. */
8785 if (skip == MOVE_LINE_CONTINUED
8786 && it->line_wrap == WORD_WRAP)
8787 {
8788 int prev_x = max (it->current_x - 1, 0);
8789 RESTORE_IT (it, &it_backup, backup_data);
8790 skip = move_it_in_display_line_to
8791 (it, -1, prev_x, MOVE_TO_X);
8792 }
8793 reached = 6;
8794 }
8795 }
8796
8797 if (reached)
8798 break;
8799 }
8800 else if (BUFFERP (it->object)
8801 && (it->method == GET_FROM_BUFFER
8802 || it->method == GET_FROM_STRETCH)
8803 && IT_CHARPOS (*it) >= to_charpos
8804 /* Under bidi iteration, a call to set_iterator_to_next
8805 can scan far beyond to_charpos if the initial
8806 portion of the next line needs to be reordered. In
8807 that case, give move_it_in_display_line_to another
8808 chance below. */
8809 && !(it->bidi_p
8810 && it->bidi_it.scan_dir == -1))
8811 skip = MOVE_POS_MATCH_OR_ZV;
8812 else
8813 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8814
8815 switch (skip)
8816 {
8817 case MOVE_POS_MATCH_OR_ZV:
8818 reached = 8;
8819 goto out;
8820
8821 case MOVE_NEWLINE_OR_CR:
8822 set_iterator_to_next (it, 1);
8823 it->continuation_lines_width = 0;
8824 break;
8825
8826 case MOVE_LINE_TRUNCATED:
8827 it->continuation_lines_width = 0;
8828 reseat_at_next_visible_line_start (it, 0);
8829 if ((op & MOVE_TO_POS) != 0
8830 && IT_CHARPOS (*it) > to_charpos)
8831 {
8832 reached = 9;
8833 goto out;
8834 }
8835 break;
8836
8837 case MOVE_LINE_CONTINUED:
8838 /* For continued lines ending in a tab, some of the glyphs
8839 associated with the tab are displayed on the current
8840 line. Since it->current_x does not include these glyphs,
8841 we use it->last_visible_x instead. */
8842 if (it->c == '\t')
8843 {
8844 it->continuation_lines_width += it->last_visible_x;
8845 /* When moving by vpos, ensure that the iterator really
8846 advances to the next line (bug#847, bug#969). Fixme:
8847 do we need to do this in other circumstances? */
8848 if (it->current_x != it->last_visible_x
8849 && (op & MOVE_TO_VPOS)
8850 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8851 {
8852 line_start_x = it->current_x + it->pixel_width
8853 - it->last_visible_x;
8854 set_iterator_to_next (it, 0);
8855 }
8856 }
8857 else
8858 it->continuation_lines_width += it->current_x;
8859 break;
8860
8861 default:
8862 abort ();
8863 }
8864
8865 /* Reset/increment for the next run. */
8866 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8867 it->current_x = line_start_x;
8868 line_start_x = 0;
8869 it->hpos = 0;
8870 it->current_y += it->max_ascent + it->max_descent;
8871 ++it->vpos;
8872 last_height = it->max_ascent + it->max_descent;
8873 last_max_ascent = it->max_ascent;
8874 it->max_ascent = it->max_descent = 0;
8875 }
8876
8877 out:
8878
8879 /* On text terminals, we may stop at the end of a line in the middle
8880 of a multi-character glyph. If the glyph itself is continued,
8881 i.e. it is actually displayed on the next line, don't treat this
8882 stopping point as valid; move to the next line instead (unless
8883 that brings us offscreen). */
8884 if (!FRAME_WINDOW_P (it->f)
8885 && op & MOVE_TO_POS
8886 && IT_CHARPOS (*it) == to_charpos
8887 && it->what == IT_CHARACTER
8888 && it->nglyphs > 1
8889 && it->line_wrap == WINDOW_WRAP
8890 && it->current_x == it->last_visible_x - 1
8891 && it->c != '\n'
8892 && it->c != '\t'
8893 && it->vpos < XFASTINT (it->w->window_end_vpos))
8894 {
8895 it->continuation_lines_width += it->current_x;
8896 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8897 it->current_y += it->max_ascent + it->max_descent;
8898 ++it->vpos;
8899 last_height = it->max_ascent + it->max_descent;
8900 last_max_ascent = it->max_ascent;
8901 }
8902
8903 if (backup_data)
8904 bidi_unshelve_cache (backup_data, 1);
8905
8906 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8907 }
8908
8909
8910 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8911
8912 If DY > 0, move IT backward at least that many pixels. DY = 0
8913 means move IT backward to the preceding line start or BEGV. This
8914 function may move over more than DY pixels if IT->current_y - DY
8915 ends up in the middle of a line; in this case IT->current_y will be
8916 set to the top of the line moved to. */
8917
8918 void
8919 move_it_vertically_backward (struct it *it, int dy)
8920 {
8921 int nlines, h;
8922 struct it it2, it3;
8923 void *it2data = NULL, *it3data = NULL;
8924 ptrdiff_t start_pos;
8925
8926 move_further_back:
8927 eassert (dy >= 0);
8928
8929 start_pos = IT_CHARPOS (*it);
8930
8931 /* Estimate how many newlines we must move back. */
8932 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8933
8934 /* Set the iterator's position that many lines back. */
8935 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8936 back_to_previous_visible_line_start (it);
8937
8938 /* Reseat the iterator here. When moving backward, we don't want
8939 reseat to skip forward over invisible text, set up the iterator
8940 to deliver from overlay strings at the new position etc. So,
8941 use reseat_1 here. */
8942 reseat_1 (it, it->current.pos, 1);
8943
8944 /* We are now surely at a line start. */
8945 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8946 reordering is in effect. */
8947 it->continuation_lines_width = 0;
8948
8949 /* Move forward and see what y-distance we moved. First move to the
8950 start of the next line so that we get its height. We need this
8951 height to be able to tell whether we reached the specified
8952 y-distance. */
8953 SAVE_IT (it2, *it, it2data);
8954 it2.max_ascent = it2.max_descent = 0;
8955 do
8956 {
8957 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8958 MOVE_TO_POS | MOVE_TO_VPOS);
8959 }
8960 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8961 /* If we are in a display string which starts at START_POS,
8962 and that display string includes a newline, and we are
8963 right after that newline (i.e. at the beginning of a
8964 display line), exit the loop, because otherwise we will
8965 infloop, since move_it_to will see that it is already at
8966 START_POS and will not move. */
8967 || (it2.method == GET_FROM_STRING
8968 && IT_CHARPOS (it2) == start_pos
8969 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8970 eassert (IT_CHARPOS (*it) >= BEGV);
8971 SAVE_IT (it3, it2, it3data);
8972
8973 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8974 eassert (IT_CHARPOS (*it) >= BEGV);
8975 /* H is the actual vertical distance from the position in *IT
8976 and the starting position. */
8977 h = it2.current_y - it->current_y;
8978 /* NLINES is the distance in number of lines. */
8979 nlines = it2.vpos - it->vpos;
8980
8981 /* Correct IT's y and vpos position
8982 so that they are relative to the starting point. */
8983 it->vpos -= nlines;
8984 it->current_y -= h;
8985
8986 if (dy == 0)
8987 {
8988 /* DY == 0 means move to the start of the screen line. The
8989 value of nlines is > 0 if continuation lines were involved,
8990 or if the original IT position was at start of a line. */
8991 RESTORE_IT (it, it, it2data);
8992 if (nlines > 0)
8993 move_it_by_lines (it, nlines);
8994 /* The above code moves us to some position NLINES down,
8995 usually to its first glyph (leftmost in an L2R line), but
8996 that's not necessarily the start of the line, under bidi
8997 reordering. We want to get to the character position
8998 that is immediately after the newline of the previous
8999 line. */
9000 if (it->bidi_p
9001 && !it->continuation_lines_width
9002 && !STRINGP (it->string)
9003 && IT_CHARPOS (*it) > BEGV
9004 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9005 {
9006 ptrdiff_t nl_pos =
9007 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9008
9009 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9010 }
9011 bidi_unshelve_cache (it3data, 1);
9012 }
9013 else
9014 {
9015 /* The y-position we try to reach, relative to *IT.
9016 Note that H has been subtracted in front of the if-statement. */
9017 int target_y = it->current_y + h - dy;
9018 int y0 = it3.current_y;
9019 int y1;
9020 int line_height;
9021
9022 RESTORE_IT (&it3, &it3, it3data);
9023 y1 = line_bottom_y (&it3);
9024 line_height = y1 - y0;
9025 RESTORE_IT (it, it, it2data);
9026 /* If we did not reach target_y, try to move further backward if
9027 we can. If we moved too far backward, try to move forward. */
9028 if (target_y < it->current_y
9029 /* This is heuristic. In a window that's 3 lines high, with
9030 a line height of 13 pixels each, recentering with point
9031 on the bottom line will try to move -39/2 = 19 pixels
9032 backward. Try to avoid moving into the first line. */
9033 && (it->current_y - target_y
9034 > min (window_box_height (it->w), line_height * 2 / 3))
9035 && IT_CHARPOS (*it) > BEGV)
9036 {
9037 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9038 target_y - it->current_y));
9039 dy = it->current_y - target_y;
9040 goto move_further_back;
9041 }
9042 else if (target_y >= it->current_y + line_height
9043 && IT_CHARPOS (*it) < ZV)
9044 {
9045 /* Should move forward by at least one line, maybe more.
9046
9047 Note: Calling move_it_by_lines can be expensive on
9048 terminal frames, where compute_motion is used (via
9049 vmotion) to do the job, when there are very long lines
9050 and truncate-lines is nil. That's the reason for
9051 treating terminal frames specially here. */
9052
9053 if (!FRAME_WINDOW_P (it->f))
9054 move_it_vertically (it, target_y - (it->current_y + line_height));
9055 else
9056 {
9057 do
9058 {
9059 move_it_by_lines (it, 1);
9060 }
9061 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9062 }
9063 }
9064 }
9065 }
9066
9067
9068 /* Move IT by a specified amount of pixel lines DY. DY negative means
9069 move backwards. DY = 0 means move to start of screen line. At the
9070 end, IT will be on the start of a screen line. */
9071
9072 void
9073 move_it_vertically (struct it *it, int dy)
9074 {
9075 if (dy <= 0)
9076 move_it_vertically_backward (it, -dy);
9077 else
9078 {
9079 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9080 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9081 MOVE_TO_POS | MOVE_TO_Y);
9082 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9083
9084 /* If buffer ends in ZV without a newline, move to the start of
9085 the line to satisfy the post-condition. */
9086 if (IT_CHARPOS (*it) == ZV
9087 && ZV > BEGV
9088 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9089 move_it_by_lines (it, 0);
9090 }
9091 }
9092
9093
9094 /* Move iterator IT past the end of the text line it is in. */
9095
9096 void
9097 move_it_past_eol (struct it *it)
9098 {
9099 enum move_it_result rc;
9100
9101 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9102 if (rc == MOVE_NEWLINE_OR_CR)
9103 set_iterator_to_next (it, 0);
9104 }
9105
9106
9107 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9108 negative means move up. DVPOS == 0 means move to the start of the
9109 screen line.
9110
9111 Optimization idea: If we would know that IT->f doesn't use
9112 a face with proportional font, we could be faster for
9113 truncate-lines nil. */
9114
9115 void
9116 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9117 {
9118
9119 /* The commented-out optimization uses vmotion on terminals. This
9120 gives bad results, because elements like it->what, on which
9121 callers such as pos_visible_p rely, aren't updated. */
9122 /* struct position pos;
9123 if (!FRAME_WINDOW_P (it->f))
9124 {
9125 struct text_pos textpos;
9126
9127 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9128 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9129 reseat (it, textpos, 1);
9130 it->vpos += pos.vpos;
9131 it->current_y += pos.vpos;
9132 }
9133 else */
9134
9135 if (dvpos == 0)
9136 {
9137 /* DVPOS == 0 means move to the start of the screen line. */
9138 move_it_vertically_backward (it, 0);
9139 /* Let next call to line_bottom_y calculate real line height */
9140 last_height = 0;
9141 }
9142 else if (dvpos > 0)
9143 {
9144 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9145 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9146 {
9147 /* Only move to the next buffer position if we ended up in a
9148 string from display property, not in an overlay string
9149 (before-string or after-string). That is because the
9150 latter don't conceal the underlying buffer position, so
9151 we can ask to move the iterator to the exact position we
9152 are interested in. Note that, even if we are already at
9153 IT_CHARPOS (*it), the call below is not a no-op, as it
9154 will detect that we are at the end of the string, pop the
9155 iterator, and compute it->current_x and it->hpos
9156 correctly. */
9157 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9158 -1, -1, -1, MOVE_TO_POS);
9159 }
9160 }
9161 else
9162 {
9163 struct it it2;
9164 void *it2data = NULL;
9165 ptrdiff_t start_charpos, i;
9166
9167 /* Start at the beginning of the screen line containing IT's
9168 position. This may actually move vertically backwards,
9169 in case of overlays, so adjust dvpos accordingly. */
9170 dvpos += it->vpos;
9171 move_it_vertically_backward (it, 0);
9172 dvpos -= it->vpos;
9173
9174 /* Go back -DVPOS visible lines and reseat the iterator there. */
9175 start_charpos = IT_CHARPOS (*it);
9176 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9177 back_to_previous_visible_line_start (it);
9178 reseat (it, it->current.pos, 1);
9179
9180 /* Move further back if we end up in a string or an image. */
9181 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9182 {
9183 /* First try to move to start of display line. */
9184 dvpos += it->vpos;
9185 move_it_vertically_backward (it, 0);
9186 dvpos -= it->vpos;
9187 if (IT_POS_VALID_AFTER_MOVE_P (it))
9188 break;
9189 /* If start of line is still in string or image,
9190 move further back. */
9191 back_to_previous_visible_line_start (it);
9192 reseat (it, it->current.pos, 1);
9193 dvpos--;
9194 }
9195
9196 it->current_x = it->hpos = 0;
9197
9198 /* Above call may have moved too far if continuation lines
9199 are involved. Scan forward and see if it did. */
9200 SAVE_IT (it2, *it, it2data);
9201 it2.vpos = it2.current_y = 0;
9202 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9203 it->vpos -= it2.vpos;
9204 it->current_y -= it2.current_y;
9205 it->current_x = it->hpos = 0;
9206
9207 /* If we moved too far back, move IT some lines forward. */
9208 if (it2.vpos > -dvpos)
9209 {
9210 int delta = it2.vpos + dvpos;
9211
9212 RESTORE_IT (&it2, &it2, it2data);
9213 SAVE_IT (it2, *it, it2data);
9214 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9215 /* Move back again if we got too far ahead. */
9216 if (IT_CHARPOS (*it) >= start_charpos)
9217 RESTORE_IT (it, &it2, it2data);
9218 else
9219 bidi_unshelve_cache (it2data, 1);
9220 }
9221 else
9222 RESTORE_IT (it, it, it2data);
9223 }
9224 }
9225
9226 /* Return 1 if IT points into the middle of a display vector. */
9227
9228 int
9229 in_display_vector_p (struct it *it)
9230 {
9231 return (it->method == GET_FROM_DISPLAY_VECTOR
9232 && it->current.dpvec_index > 0
9233 && it->dpvec + it->current.dpvec_index != it->dpend);
9234 }
9235
9236 \f
9237 /***********************************************************************
9238 Messages
9239 ***********************************************************************/
9240
9241
9242 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9243 to *Messages*. */
9244
9245 void
9246 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9247 {
9248 Lisp_Object args[3];
9249 Lisp_Object msg, fmt;
9250 char *buffer;
9251 ptrdiff_t len;
9252 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9253 USE_SAFE_ALLOCA;
9254
9255 /* Do nothing if called asynchronously. Inserting text into
9256 a buffer may call after-change-functions and alike and
9257 that would means running Lisp asynchronously. */
9258 if (handling_signal)
9259 return;
9260
9261 fmt = msg = Qnil;
9262 GCPRO4 (fmt, msg, arg1, arg2);
9263
9264 args[0] = fmt = build_string (format);
9265 args[1] = arg1;
9266 args[2] = arg2;
9267 msg = Fformat (3, args);
9268
9269 len = SBYTES (msg) + 1;
9270 SAFE_ALLOCA (buffer, char *, len);
9271 memcpy (buffer, SDATA (msg), len);
9272
9273 message_dolog (buffer, len - 1, 1, 0);
9274 SAFE_FREE ();
9275
9276 UNGCPRO;
9277 }
9278
9279
9280 /* Output a newline in the *Messages* buffer if "needs" one. */
9281
9282 void
9283 message_log_maybe_newline (void)
9284 {
9285 if (message_log_need_newline)
9286 message_dolog ("", 0, 1, 0);
9287 }
9288
9289
9290 /* Add a string M of length NBYTES to the message log, optionally
9291 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9292 nonzero, means interpret the contents of M as multibyte. This
9293 function calls low-level routines in order to bypass text property
9294 hooks, etc. which might not be safe to run.
9295
9296 This may GC (insert may run before/after change hooks),
9297 so the buffer M must NOT point to a Lisp string. */
9298
9299 void
9300 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9301 {
9302 const unsigned char *msg = (const unsigned char *) m;
9303
9304 if (!NILP (Vmemory_full))
9305 return;
9306
9307 if (!NILP (Vmessage_log_max))
9308 {
9309 struct buffer *oldbuf;
9310 Lisp_Object oldpoint, oldbegv, oldzv;
9311 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9312 ptrdiff_t point_at_end = 0;
9313 ptrdiff_t zv_at_end = 0;
9314 Lisp_Object old_deactivate_mark, tem;
9315 struct gcpro gcpro1;
9316
9317 old_deactivate_mark = Vdeactivate_mark;
9318 oldbuf = current_buffer;
9319 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9320 BVAR (current_buffer, undo_list) = Qt;
9321
9322 oldpoint = message_dolog_marker1;
9323 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9324 oldbegv = message_dolog_marker2;
9325 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9326 oldzv = message_dolog_marker3;
9327 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9328 GCPRO1 (old_deactivate_mark);
9329
9330 if (PT == Z)
9331 point_at_end = 1;
9332 if (ZV == Z)
9333 zv_at_end = 1;
9334
9335 BEGV = BEG;
9336 BEGV_BYTE = BEG_BYTE;
9337 ZV = Z;
9338 ZV_BYTE = Z_BYTE;
9339 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9340
9341 /* Insert the string--maybe converting multibyte to single byte
9342 or vice versa, so that all the text fits the buffer. */
9343 if (multibyte
9344 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9345 {
9346 ptrdiff_t i;
9347 int c, char_bytes;
9348 char work[1];
9349
9350 /* Convert a multibyte string to single-byte
9351 for the *Message* buffer. */
9352 for (i = 0; i < nbytes; i += char_bytes)
9353 {
9354 c = string_char_and_length (msg + i, &char_bytes);
9355 work[0] = (ASCII_CHAR_P (c)
9356 ? c
9357 : multibyte_char_to_unibyte (c));
9358 insert_1_both (work, 1, 1, 1, 0, 0);
9359 }
9360 }
9361 else if (! multibyte
9362 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9363 {
9364 ptrdiff_t i;
9365 int c, char_bytes;
9366 unsigned char str[MAX_MULTIBYTE_LENGTH];
9367 /* Convert a single-byte string to multibyte
9368 for the *Message* buffer. */
9369 for (i = 0; i < nbytes; i++)
9370 {
9371 c = msg[i];
9372 MAKE_CHAR_MULTIBYTE (c);
9373 char_bytes = CHAR_STRING (c, str);
9374 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9375 }
9376 }
9377 else if (nbytes)
9378 insert_1 (m, nbytes, 1, 0, 0);
9379
9380 if (nlflag)
9381 {
9382 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9383 printmax_t dups;
9384 insert_1 ("\n", 1, 1, 0, 0);
9385
9386 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9387 this_bol = PT;
9388 this_bol_byte = PT_BYTE;
9389
9390 /* See if this line duplicates the previous one.
9391 If so, combine duplicates. */
9392 if (this_bol > BEG)
9393 {
9394 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9395 prev_bol = PT;
9396 prev_bol_byte = PT_BYTE;
9397
9398 dups = message_log_check_duplicate (prev_bol_byte,
9399 this_bol_byte);
9400 if (dups)
9401 {
9402 del_range_both (prev_bol, prev_bol_byte,
9403 this_bol, this_bol_byte, 0);
9404 if (dups > 1)
9405 {
9406 char dupstr[sizeof " [ times]"
9407 + INT_STRLEN_BOUND (printmax_t)];
9408
9409 /* If you change this format, don't forget to also
9410 change message_log_check_duplicate. */
9411 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9412 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9413 insert_1 (dupstr, duplen, 1, 0, 1);
9414 }
9415 }
9416 }
9417
9418 /* If we have more than the desired maximum number of lines
9419 in the *Messages* buffer now, delete the oldest ones.
9420 This is safe because we don't have undo in this buffer. */
9421
9422 if (NATNUMP (Vmessage_log_max))
9423 {
9424 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9425 -XFASTINT (Vmessage_log_max) - 1, 0);
9426 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9427 }
9428 }
9429 BEGV = XMARKER (oldbegv)->charpos;
9430 BEGV_BYTE = marker_byte_position (oldbegv);
9431
9432 if (zv_at_end)
9433 {
9434 ZV = Z;
9435 ZV_BYTE = Z_BYTE;
9436 }
9437 else
9438 {
9439 ZV = XMARKER (oldzv)->charpos;
9440 ZV_BYTE = marker_byte_position (oldzv);
9441 }
9442
9443 if (point_at_end)
9444 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9445 else
9446 /* We can't do Fgoto_char (oldpoint) because it will run some
9447 Lisp code. */
9448 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9449 XMARKER (oldpoint)->bytepos);
9450
9451 UNGCPRO;
9452 unchain_marker (XMARKER (oldpoint));
9453 unchain_marker (XMARKER (oldbegv));
9454 unchain_marker (XMARKER (oldzv));
9455
9456 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9457 set_buffer_internal (oldbuf);
9458 if (NILP (tem))
9459 windows_or_buffers_changed = old_windows_or_buffers_changed;
9460 message_log_need_newline = !nlflag;
9461 Vdeactivate_mark = old_deactivate_mark;
9462 }
9463 }
9464
9465
9466 /* We are at the end of the buffer after just having inserted a newline.
9467 (Note: We depend on the fact we won't be crossing the gap.)
9468 Check to see if the most recent message looks a lot like the previous one.
9469 Return 0 if different, 1 if the new one should just replace it, or a
9470 value N > 1 if we should also append " [N times]". */
9471
9472 static intmax_t
9473 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9474 {
9475 ptrdiff_t i;
9476 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9477 int seen_dots = 0;
9478 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9479 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9480
9481 for (i = 0; i < len; i++)
9482 {
9483 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9484 seen_dots = 1;
9485 if (p1[i] != p2[i])
9486 return seen_dots;
9487 }
9488 p1 += len;
9489 if (*p1 == '\n')
9490 return 2;
9491 if (*p1++ == ' ' && *p1++ == '[')
9492 {
9493 char *pend;
9494 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9495 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9496 return n+1;
9497 }
9498 return 0;
9499 }
9500 \f
9501
9502 /* Display an echo area message M with a specified length of NBYTES
9503 bytes. The string may include null characters. If M is 0, clear
9504 out any existing message, and let the mini-buffer text show
9505 through.
9506
9507 This may GC, so the buffer M must NOT point to a Lisp string. */
9508
9509 void
9510 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9511 {
9512 /* First flush out any partial line written with print. */
9513 message_log_maybe_newline ();
9514 if (m)
9515 message_dolog (m, nbytes, 1, multibyte);
9516 message2_nolog (m, nbytes, multibyte);
9517 }
9518
9519
9520 /* The non-logging counterpart of message2. */
9521
9522 void
9523 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9524 {
9525 struct frame *sf = SELECTED_FRAME ();
9526 message_enable_multibyte = multibyte;
9527
9528 if (FRAME_INITIAL_P (sf))
9529 {
9530 if (noninteractive_need_newline)
9531 putc ('\n', stderr);
9532 noninteractive_need_newline = 0;
9533 if (m)
9534 fwrite (m, nbytes, 1, stderr);
9535 if (cursor_in_echo_area == 0)
9536 fprintf (stderr, "\n");
9537 fflush (stderr);
9538 }
9539 /* A null message buffer means that the frame hasn't really been
9540 initialized yet. Error messages get reported properly by
9541 cmd_error, so this must be just an informative message; toss it. */
9542 else if (INTERACTIVE
9543 && sf->glyphs_initialized_p
9544 && FRAME_MESSAGE_BUF (sf))
9545 {
9546 Lisp_Object mini_window;
9547 struct frame *f;
9548
9549 /* Get the frame containing the mini-buffer
9550 that the selected frame is using. */
9551 mini_window = FRAME_MINIBUF_WINDOW (sf);
9552 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9553
9554 FRAME_SAMPLE_VISIBILITY (f);
9555 if (FRAME_VISIBLE_P (sf)
9556 && ! FRAME_VISIBLE_P (f))
9557 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9558
9559 if (m)
9560 {
9561 set_message (m, Qnil, nbytes, multibyte);
9562 if (minibuffer_auto_raise)
9563 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9564 }
9565 else
9566 clear_message (1, 1);
9567
9568 do_pending_window_change (0);
9569 echo_area_display (1);
9570 do_pending_window_change (0);
9571 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9572 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9573 }
9574 }
9575
9576
9577 /* Display an echo area message M with a specified length of NBYTES
9578 bytes. The string may include null characters. If M is not a
9579 string, clear out any existing message, and let the mini-buffer
9580 text show through.
9581
9582 This function cancels echoing. */
9583
9584 void
9585 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9586 {
9587 struct gcpro gcpro1;
9588
9589 GCPRO1 (m);
9590 clear_message (1,1);
9591 cancel_echoing ();
9592
9593 /* First flush out any partial line written with print. */
9594 message_log_maybe_newline ();
9595 if (STRINGP (m))
9596 {
9597 char *buffer;
9598 USE_SAFE_ALLOCA;
9599
9600 SAFE_ALLOCA (buffer, char *, nbytes);
9601 memcpy (buffer, SDATA (m), nbytes);
9602 message_dolog (buffer, nbytes, 1, multibyte);
9603 SAFE_FREE ();
9604 }
9605 message3_nolog (m, nbytes, multibyte);
9606
9607 UNGCPRO;
9608 }
9609
9610
9611 /* The non-logging version of message3.
9612 This does not cancel echoing, because it is used for echoing.
9613 Perhaps we need to make a separate function for echoing
9614 and make this cancel echoing. */
9615
9616 void
9617 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9618 {
9619 struct frame *sf = SELECTED_FRAME ();
9620 message_enable_multibyte = multibyte;
9621
9622 if (FRAME_INITIAL_P (sf))
9623 {
9624 if (noninteractive_need_newline)
9625 putc ('\n', stderr);
9626 noninteractive_need_newline = 0;
9627 if (STRINGP (m))
9628 fwrite (SDATA (m), nbytes, 1, stderr);
9629 if (cursor_in_echo_area == 0)
9630 fprintf (stderr, "\n");
9631 fflush (stderr);
9632 }
9633 /* A null message buffer means that the frame hasn't really been
9634 initialized yet. Error messages get reported properly by
9635 cmd_error, so this must be just an informative message; toss it. */
9636 else if (INTERACTIVE
9637 && sf->glyphs_initialized_p
9638 && FRAME_MESSAGE_BUF (sf))
9639 {
9640 Lisp_Object mini_window;
9641 Lisp_Object frame;
9642 struct frame *f;
9643
9644 /* Get the frame containing the mini-buffer
9645 that the selected frame is using. */
9646 mini_window = FRAME_MINIBUF_WINDOW (sf);
9647 frame = XWINDOW (mini_window)->frame;
9648 f = XFRAME (frame);
9649
9650 FRAME_SAMPLE_VISIBILITY (f);
9651 if (FRAME_VISIBLE_P (sf)
9652 && !FRAME_VISIBLE_P (f))
9653 Fmake_frame_visible (frame);
9654
9655 if (STRINGP (m) && SCHARS (m) > 0)
9656 {
9657 set_message (NULL, m, nbytes, multibyte);
9658 if (minibuffer_auto_raise)
9659 Fraise_frame (frame);
9660 /* Assume we are not echoing.
9661 (If we are, echo_now will override this.) */
9662 echo_message_buffer = Qnil;
9663 }
9664 else
9665 clear_message (1, 1);
9666
9667 do_pending_window_change (0);
9668 echo_area_display (1);
9669 do_pending_window_change (0);
9670 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9671 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9672 }
9673 }
9674
9675
9676 /* Display a null-terminated echo area message M. If M is 0, clear
9677 out any existing message, and let the mini-buffer text show through.
9678
9679 The buffer M must continue to exist until after the echo area gets
9680 cleared or some other message gets displayed there. Do not pass
9681 text that is stored in a Lisp string. Do not pass text in a buffer
9682 that was alloca'd. */
9683
9684 void
9685 message1 (const char *m)
9686 {
9687 message2 (m, (m ? strlen (m) : 0), 0);
9688 }
9689
9690
9691 /* The non-logging counterpart of message1. */
9692
9693 void
9694 message1_nolog (const char *m)
9695 {
9696 message2_nolog (m, (m ? strlen (m) : 0), 0);
9697 }
9698
9699 /* Display a message M which contains a single %s
9700 which gets replaced with STRING. */
9701
9702 void
9703 message_with_string (const char *m, Lisp_Object string, int log)
9704 {
9705 CHECK_STRING (string);
9706
9707 if (noninteractive)
9708 {
9709 if (m)
9710 {
9711 if (noninteractive_need_newline)
9712 putc ('\n', stderr);
9713 noninteractive_need_newline = 0;
9714 fprintf (stderr, m, SDATA (string));
9715 if (!cursor_in_echo_area)
9716 fprintf (stderr, "\n");
9717 fflush (stderr);
9718 }
9719 }
9720 else if (INTERACTIVE)
9721 {
9722 /* The frame whose minibuffer we're going to display the message on.
9723 It may be larger than the selected frame, so we need
9724 to use its buffer, not the selected frame's buffer. */
9725 Lisp_Object mini_window;
9726 struct frame *f, *sf = SELECTED_FRAME ();
9727
9728 /* Get the frame containing the minibuffer
9729 that the selected frame is using. */
9730 mini_window = FRAME_MINIBUF_WINDOW (sf);
9731 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9732
9733 /* A null message buffer means that the frame hasn't really been
9734 initialized yet. Error messages get reported properly by
9735 cmd_error, so this must be just an informative message; toss it. */
9736 if (FRAME_MESSAGE_BUF (f))
9737 {
9738 Lisp_Object args[2], msg;
9739 struct gcpro gcpro1, gcpro2;
9740
9741 args[0] = build_string (m);
9742 args[1] = msg = string;
9743 GCPRO2 (args[0], msg);
9744 gcpro1.nvars = 2;
9745
9746 msg = Fformat (2, args);
9747
9748 if (log)
9749 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9750 else
9751 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9752
9753 UNGCPRO;
9754
9755 /* Print should start at the beginning of the message
9756 buffer next time. */
9757 message_buf_print = 0;
9758 }
9759 }
9760 }
9761
9762
9763 /* Dump an informative message to the minibuf. If M is 0, clear out
9764 any existing message, and let the mini-buffer text show through. */
9765
9766 static void
9767 vmessage (const char *m, va_list ap)
9768 {
9769 if (noninteractive)
9770 {
9771 if (m)
9772 {
9773 if (noninteractive_need_newline)
9774 putc ('\n', stderr);
9775 noninteractive_need_newline = 0;
9776 vfprintf (stderr, m, ap);
9777 if (cursor_in_echo_area == 0)
9778 fprintf (stderr, "\n");
9779 fflush (stderr);
9780 }
9781 }
9782 else if (INTERACTIVE)
9783 {
9784 /* The frame whose mini-buffer we're going to display the message
9785 on. It may be larger than the selected frame, so we need to
9786 use its buffer, not the selected frame's buffer. */
9787 Lisp_Object mini_window;
9788 struct frame *f, *sf = SELECTED_FRAME ();
9789
9790 /* Get the frame containing the mini-buffer
9791 that the selected frame is using. */
9792 mini_window = FRAME_MINIBUF_WINDOW (sf);
9793 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9794
9795 /* A null message buffer means that the frame hasn't really been
9796 initialized yet. Error messages get reported properly by
9797 cmd_error, so this must be just an informative message; toss
9798 it. */
9799 if (FRAME_MESSAGE_BUF (f))
9800 {
9801 if (m)
9802 {
9803 ptrdiff_t len;
9804
9805 len = doprnt (FRAME_MESSAGE_BUF (f),
9806 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9807
9808 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9809 }
9810 else
9811 message1 (0);
9812
9813 /* Print should start at the beginning of the message
9814 buffer next time. */
9815 message_buf_print = 0;
9816 }
9817 }
9818 }
9819
9820 void
9821 message (const char *m, ...)
9822 {
9823 va_list ap;
9824 va_start (ap, m);
9825 vmessage (m, ap);
9826 va_end (ap);
9827 }
9828
9829
9830 #if 0
9831 /* The non-logging version of message. */
9832
9833 void
9834 message_nolog (const char *m, ...)
9835 {
9836 Lisp_Object old_log_max;
9837 va_list ap;
9838 va_start (ap, m);
9839 old_log_max = Vmessage_log_max;
9840 Vmessage_log_max = Qnil;
9841 vmessage (m, ap);
9842 Vmessage_log_max = old_log_max;
9843 va_end (ap);
9844 }
9845 #endif
9846
9847
9848 /* Display the current message in the current mini-buffer. This is
9849 only called from error handlers in process.c, and is not time
9850 critical. */
9851
9852 void
9853 update_echo_area (void)
9854 {
9855 if (!NILP (echo_area_buffer[0]))
9856 {
9857 Lisp_Object string;
9858 string = Fcurrent_message ();
9859 message3 (string, SBYTES (string),
9860 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9861 }
9862 }
9863
9864
9865 /* Make sure echo area buffers in `echo_buffers' are live.
9866 If they aren't, make new ones. */
9867
9868 static void
9869 ensure_echo_area_buffers (void)
9870 {
9871 int i;
9872
9873 for (i = 0; i < 2; ++i)
9874 if (!BUFFERP (echo_buffer[i])
9875 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9876 {
9877 char name[30];
9878 Lisp_Object old_buffer;
9879 int j;
9880
9881 old_buffer = echo_buffer[i];
9882 echo_buffer[i] = Fget_buffer_create
9883 (make_formatted_string (name, " *Echo Area %d*", i));
9884 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9885 /* to force word wrap in echo area -
9886 it was decided to postpone this*/
9887 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9888
9889 for (j = 0; j < 2; ++j)
9890 if (EQ (old_buffer, echo_area_buffer[j]))
9891 echo_area_buffer[j] = echo_buffer[i];
9892 }
9893 }
9894
9895
9896 /* Call FN with args A1..A4 with either the current or last displayed
9897 echo_area_buffer as current buffer.
9898
9899 WHICH zero means use the current message buffer
9900 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9901 from echo_buffer[] and clear it.
9902
9903 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9904 suitable buffer from echo_buffer[] and clear it.
9905
9906 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9907 that the current message becomes the last displayed one, make
9908 choose a suitable buffer for echo_area_buffer[0], and clear it.
9909
9910 Value is what FN returns. */
9911
9912 static int
9913 with_echo_area_buffer (struct window *w, int which,
9914 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9915 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9916 {
9917 Lisp_Object buffer;
9918 int this_one, the_other, clear_buffer_p, rc;
9919 ptrdiff_t count = SPECPDL_INDEX ();
9920
9921 /* If buffers aren't live, make new ones. */
9922 ensure_echo_area_buffers ();
9923
9924 clear_buffer_p = 0;
9925
9926 if (which == 0)
9927 this_one = 0, the_other = 1;
9928 else if (which > 0)
9929 this_one = 1, the_other = 0;
9930 else
9931 {
9932 this_one = 0, the_other = 1;
9933 clear_buffer_p = 1;
9934
9935 /* We need a fresh one in case the current echo buffer equals
9936 the one containing the last displayed echo area message. */
9937 if (!NILP (echo_area_buffer[this_one])
9938 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9939 echo_area_buffer[this_one] = Qnil;
9940 }
9941
9942 /* Choose a suitable buffer from echo_buffer[] is we don't
9943 have one. */
9944 if (NILP (echo_area_buffer[this_one]))
9945 {
9946 echo_area_buffer[this_one]
9947 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9948 ? echo_buffer[the_other]
9949 : echo_buffer[this_one]);
9950 clear_buffer_p = 1;
9951 }
9952
9953 buffer = echo_area_buffer[this_one];
9954
9955 /* Don't get confused by reusing the buffer used for echoing
9956 for a different purpose. */
9957 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9958 cancel_echoing ();
9959
9960 record_unwind_protect (unwind_with_echo_area_buffer,
9961 with_echo_area_buffer_unwind_data (w));
9962
9963 /* Make the echo area buffer current. Note that for display
9964 purposes, it is not necessary that the displayed window's buffer
9965 == current_buffer, except for text property lookup. So, let's
9966 only set that buffer temporarily here without doing a full
9967 Fset_window_buffer. We must also change w->pointm, though,
9968 because otherwise an assertions in unshow_buffer fails, and Emacs
9969 aborts. */
9970 set_buffer_internal_1 (XBUFFER (buffer));
9971 if (w)
9972 {
9973 w->buffer = buffer;
9974 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9975 }
9976
9977 BVAR (current_buffer, undo_list) = Qt;
9978 BVAR (current_buffer, read_only) = Qnil;
9979 specbind (Qinhibit_read_only, Qt);
9980 specbind (Qinhibit_modification_hooks, Qt);
9981
9982 if (clear_buffer_p && Z > BEG)
9983 del_range (BEG, Z);
9984
9985 eassert (BEGV >= BEG);
9986 eassert (ZV <= Z && ZV >= BEGV);
9987
9988 rc = fn (a1, a2, a3, a4);
9989
9990 eassert (BEGV >= BEG);
9991 eassert (ZV <= Z && ZV >= BEGV);
9992
9993 unbind_to (count, Qnil);
9994 return rc;
9995 }
9996
9997
9998 /* Save state that should be preserved around the call to the function
9999 FN called in with_echo_area_buffer. */
10000
10001 static Lisp_Object
10002 with_echo_area_buffer_unwind_data (struct window *w)
10003 {
10004 int i = 0;
10005 Lisp_Object vector, tmp;
10006
10007 /* Reduce consing by keeping one vector in
10008 Vwith_echo_area_save_vector. */
10009 vector = Vwith_echo_area_save_vector;
10010 Vwith_echo_area_save_vector = Qnil;
10011
10012 if (NILP (vector))
10013 vector = Fmake_vector (make_number (7), Qnil);
10014
10015 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10016 ASET (vector, i, Vdeactivate_mark); ++i;
10017 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10018
10019 if (w)
10020 {
10021 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10022 ASET (vector, i, w->buffer); ++i;
10023 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10024 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10025 }
10026 else
10027 {
10028 int end = i + 4;
10029 for (; i < end; ++i)
10030 ASET (vector, i, Qnil);
10031 }
10032
10033 eassert (i == ASIZE (vector));
10034 return vector;
10035 }
10036
10037
10038 /* Restore global state from VECTOR which was created by
10039 with_echo_area_buffer_unwind_data. */
10040
10041 static Lisp_Object
10042 unwind_with_echo_area_buffer (Lisp_Object vector)
10043 {
10044 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10045 Vdeactivate_mark = AREF (vector, 1);
10046 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10047
10048 if (WINDOWP (AREF (vector, 3)))
10049 {
10050 struct window *w;
10051 Lisp_Object buffer, charpos, bytepos;
10052
10053 w = XWINDOW (AREF (vector, 3));
10054 buffer = AREF (vector, 4);
10055 charpos = AREF (vector, 5);
10056 bytepos = AREF (vector, 6);
10057
10058 w->buffer = buffer;
10059 set_marker_both (w->pointm, buffer,
10060 XFASTINT (charpos), XFASTINT (bytepos));
10061 }
10062
10063 Vwith_echo_area_save_vector = vector;
10064 return Qnil;
10065 }
10066
10067
10068 /* Set up the echo area for use by print functions. MULTIBYTE_P
10069 non-zero means we will print multibyte. */
10070
10071 void
10072 setup_echo_area_for_printing (int multibyte_p)
10073 {
10074 /* If we can't find an echo area any more, exit. */
10075 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10076 Fkill_emacs (Qnil);
10077
10078 ensure_echo_area_buffers ();
10079
10080 if (!message_buf_print)
10081 {
10082 /* A message has been output since the last time we printed.
10083 Choose a fresh echo area buffer. */
10084 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10085 echo_area_buffer[0] = echo_buffer[1];
10086 else
10087 echo_area_buffer[0] = echo_buffer[0];
10088
10089 /* Switch to that buffer and clear it. */
10090 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10091 BVAR (current_buffer, truncate_lines) = Qnil;
10092
10093 if (Z > BEG)
10094 {
10095 ptrdiff_t count = SPECPDL_INDEX ();
10096 specbind (Qinhibit_read_only, Qt);
10097 /* Note that undo recording is always disabled. */
10098 del_range (BEG, Z);
10099 unbind_to (count, Qnil);
10100 }
10101 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10102
10103 /* Set up the buffer for the multibyteness we need. */
10104 if (multibyte_p
10105 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10106 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10107
10108 /* Raise the frame containing the echo area. */
10109 if (minibuffer_auto_raise)
10110 {
10111 struct frame *sf = SELECTED_FRAME ();
10112 Lisp_Object mini_window;
10113 mini_window = FRAME_MINIBUF_WINDOW (sf);
10114 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10115 }
10116
10117 message_log_maybe_newline ();
10118 message_buf_print = 1;
10119 }
10120 else
10121 {
10122 if (NILP (echo_area_buffer[0]))
10123 {
10124 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10125 echo_area_buffer[0] = echo_buffer[1];
10126 else
10127 echo_area_buffer[0] = echo_buffer[0];
10128 }
10129
10130 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10131 {
10132 /* Someone switched buffers between print requests. */
10133 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10134 BVAR (current_buffer, truncate_lines) = Qnil;
10135 }
10136 }
10137 }
10138
10139
10140 /* Display an echo area message in window W. Value is non-zero if W's
10141 height is changed. If display_last_displayed_message_p is
10142 non-zero, display the message that was last displayed, otherwise
10143 display the current message. */
10144
10145 static int
10146 display_echo_area (struct window *w)
10147 {
10148 int i, no_message_p, window_height_changed_p;
10149
10150 /* Temporarily disable garbage collections while displaying the echo
10151 area. This is done because a GC can print a message itself.
10152 That message would modify the echo area buffer's contents while a
10153 redisplay of the buffer is going on, and seriously confuse
10154 redisplay. */
10155 ptrdiff_t count = inhibit_garbage_collection ();
10156
10157 /* If there is no message, we must call display_echo_area_1
10158 nevertheless because it resizes the window. But we will have to
10159 reset the echo_area_buffer in question to nil at the end because
10160 with_echo_area_buffer will sets it to an empty buffer. */
10161 i = display_last_displayed_message_p ? 1 : 0;
10162 no_message_p = NILP (echo_area_buffer[i]);
10163
10164 window_height_changed_p
10165 = with_echo_area_buffer (w, display_last_displayed_message_p,
10166 display_echo_area_1,
10167 (intptr_t) w, Qnil, 0, 0);
10168
10169 if (no_message_p)
10170 echo_area_buffer[i] = Qnil;
10171
10172 unbind_to (count, Qnil);
10173 return window_height_changed_p;
10174 }
10175
10176
10177 /* Helper for display_echo_area. Display the current buffer which
10178 contains the current echo area message in window W, a mini-window,
10179 a pointer to which is passed in A1. A2..A4 are currently not used.
10180 Change the height of W so that all of the message is displayed.
10181 Value is non-zero if height of W was changed. */
10182
10183 static int
10184 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10185 {
10186 intptr_t i1 = a1;
10187 struct window *w = (struct window *) i1;
10188 Lisp_Object window;
10189 struct text_pos start;
10190 int window_height_changed_p = 0;
10191
10192 /* Do this before displaying, so that we have a large enough glyph
10193 matrix for the display. If we can't get enough space for the
10194 whole text, display the last N lines. That works by setting w->start. */
10195 window_height_changed_p = resize_mini_window (w, 0);
10196
10197 /* Use the starting position chosen by resize_mini_window. */
10198 SET_TEXT_POS_FROM_MARKER (start, w->start);
10199
10200 /* Display. */
10201 clear_glyph_matrix (w->desired_matrix);
10202 XSETWINDOW (window, w);
10203 try_window (window, start, 0);
10204
10205 return window_height_changed_p;
10206 }
10207
10208
10209 /* Resize the echo area window to exactly the size needed for the
10210 currently displayed message, if there is one. If a mini-buffer
10211 is active, don't shrink it. */
10212
10213 void
10214 resize_echo_area_exactly (void)
10215 {
10216 if (BUFFERP (echo_area_buffer[0])
10217 && WINDOWP (echo_area_window))
10218 {
10219 struct window *w = XWINDOW (echo_area_window);
10220 int resized_p;
10221 Lisp_Object resize_exactly;
10222
10223 if (minibuf_level == 0)
10224 resize_exactly = Qt;
10225 else
10226 resize_exactly = Qnil;
10227
10228 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10229 (intptr_t) w, resize_exactly,
10230 0, 0);
10231 if (resized_p)
10232 {
10233 ++windows_or_buffers_changed;
10234 ++update_mode_lines;
10235 redisplay_internal ();
10236 }
10237 }
10238 }
10239
10240
10241 /* Callback function for with_echo_area_buffer, when used from
10242 resize_echo_area_exactly. A1 contains a pointer to the window to
10243 resize, EXACTLY non-nil means resize the mini-window exactly to the
10244 size of the text displayed. A3 and A4 are not used. Value is what
10245 resize_mini_window returns. */
10246
10247 static int
10248 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10249 {
10250 intptr_t i1 = a1;
10251 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10252 }
10253
10254
10255 /* Resize mini-window W to fit the size of its contents. EXACT_P
10256 means size the window exactly to the size needed. Otherwise, it's
10257 only enlarged until W's buffer is empty.
10258
10259 Set W->start to the right place to begin display. If the whole
10260 contents fit, start at the beginning. Otherwise, start so as
10261 to make the end of the contents appear. This is particularly
10262 important for y-or-n-p, but seems desirable generally.
10263
10264 Value is non-zero if the window height has been changed. */
10265
10266 int
10267 resize_mini_window (struct window *w, int exact_p)
10268 {
10269 struct frame *f = XFRAME (w->frame);
10270 int window_height_changed_p = 0;
10271
10272 eassert (MINI_WINDOW_P (w));
10273
10274 /* By default, start display at the beginning. */
10275 set_marker_both (w->start, w->buffer,
10276 BUF_BEGV (XBUFFER (w->buffer)),
10277 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10278
10279 /* Don't resize windows while redisplaying a window; it would
10280 confuse redisplay functions when the size of the window they are
10281 displaying changes from under them. Such a resizing can happen,
10282 for instance, when which-func prints a long message while
10283 we are running fontification-functions. We're running these
10284 functions with safe_call which binds inhibit-redisplay to t. */
10285 if (!NILP (Vinhibit_redisplay))
10286 return 0;
10287
10288 /* Nil means don't try to resize. */
10289 if (NILP (Vresize_mini_windows)
10290 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10291 return 0;
10292
10293 if (!FRAME_MINIBUF_ONLY_P (f))
10294 {
10295 struct it it;
10296 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10297 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10298 int height;
10299 EMACS_INT max_height;
10300 int unit = FRAME_LINE_HEIGHT (f);
10301 struct text_pos start;
10302 struct buffer *old_current_buffer = NULL;
10303
10304 if (current_buffer != XBUFFER (w->buffer))
10305 {
10306 old_current_buffer = current_buffer;
10307 set_buffer_internal (XBUFFER (w->buffer));
10308 }
10309
10310 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10311
10312 /* Compute the max. number of lines specified by the user. */
10313 if (FLOATP (Vmax_mini_window_height))
10314 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10315 else if (INTEGERP (Vmax_mini_window_height))
10316 max_height = XINT (Vmax_mini_window_height);
10317 else
10318 max_height = total_height / 4;
10319
10320 /* Correct that max. height if it's bogus. */
10321 max_height = max (1, max_height);
10322 max_height = min (total_height, max_height);
10323
10324 /* Find out the height of the text in the window. */
10325 if (it.line_wrap == TRUNCATE)
10326 height = 1;
10327 else
10328 {
10329 last_height = 0;
10330 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10331 if (it.max_ascent == 0 && it.max_descent == 0)
10332 height = it.current_y + last_height;
10333 else
10334 height = it.current_y + it.max_ascent + it.max_descent;
10335 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10336 height = (height + unit - 1) / unit;
10337 }
10338
10339 /* Compute a suitable window start. */
10340 if (height > max_height)
10341 {
10342 height = max_height;
10343 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10344 move_it_vertically_backward (&it, (height - 1) * unit);
10345 start = it.current.pos;
10346 }
10347 else
10348 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10349 SET_MARKER_FROM_TEXT_POS (w->start, start);
10350
10351 if (EQ (Vresize_mini_windows, Qgrow_only))
10352 {
10353 /* Let it grow only, until we display an empty message, in which
10354 case the window shrinks again. */
10355 if (height > WINDOW_TOTAL_LINES (w))
10356 {
10357 int old_height = WINDOW_TOTAL_LINES (w);
10358 freeze_window_starts (f, 1);
10359 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10360 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10361 }
10362 else if (height < WINDOW_TOTAL_LINES (w)
10363 && (exact_p || BEGV == ZV))
10364 {
10365 int old_height = WINDOW_TOTAL_LINES (w);
10366 freeze_window_starts (f, 0);
10367 shrink_mini_window (w);
10368 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10369 }
10370 }
10371 else
10372 {
10373 /* Always resize to exact size needed. */
10374 if (height > WINDOW_TOTAL_LINES (w))
10375 {
10376 int old_height = WINDOW_TOTAL_LINES (w);
10377 freeze_window_starts (f, 1);
10378 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10379 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10380 }
10381 else if (height < WINDOW_TOTAL_LINES (w))
10382 {
10383 int old_height = WINDOW_TOTAL_LINES (w);
10384 freeze_window_starts (f, 0);
10385 shrink_mini_window (w);
10386
10387 if (height)
10388 {
10389 freeze_window_starts (f, 1);
10390 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10391 }
10392
10393 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10394 }
10395 }
10396
10397 if (old_current_buffer)
10398 set_buffer_internal (old_current_buffer);
10399 }
10400
10401 return window_height_changed_p;
10402 }
10403
10404
10405 /* Value is the current message, a string, or nil if there is no
10406 current message. */
10407
10408 Lisp_Object
10409 current_message (void)
10410 {
10411 Lisp_Object msg;
10412
10413 if (!BUFFERP (echo_area_buffer[0]))
10414 msg = Qnil;
10415 else
10416 {
10417 with_echo_area_buffer (0, 0, current_message_1,
10418 (intptr_t) &msg, Qnil, 0, 0);
10419 if (NILP (msg))
10420 echo_area_buffer[0] = Qnil;
10421 }
10422
10423 return msg;
10424 }
10425
10426
10427 static int
10428 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10429 {
10430 intptr_t i1 = a1;
10431 Lisp_Object *msg = (Lisp_Object *) i1;
10432
10433 if (Z > BEG)
10434 *msg = make_buffer_string (BEG, Z, 1);
10435 else
10436 *msg = Qnil;
10437 return 0;
10438 }
10439
10440
10441 /* Push the current message on Vmessage_stack for later restoration
10442 by restore_message. Value is non-zero if the current message isn't
10443 empty. This is a relatively infrequent operation, so it's not
10444 worth optimizing. */
10445
10446 int
10447 push_message (void)
10448 {
10449 Lisp_Object msg;
10450 msg = current_message ();
10451 Vmessage_stack = Fcons (msg, Vmessage_stack);
10452 return STRINGP (msg);
10453 }
10454
10455
10456 /* Restore message display from the top of Vmessage_stack. */
10457
10458 void
10459 restore_message (void)
10460 {
10461 Lisp_Object msg;
10462
10463 eassert (CONSP (Vmessage_stack));
10464 msg = XCAR (Vmessage_stack);
10465 if (STRINGP (msg))
10466 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10467 else
10468 message3_nolog (msg, 0, 0);
10469 }
10470
10471
10472 /* Handler for record_unwind_protect calling pop_message. */
10473
10474 Lisp_Object
10475 pop_message_unwind (Lisp_Object dummy)
10476 {
10477 pop_message ();
10478 return Qnil;
10479 }
10480
10481 /* Pop the top-most entry off Vmessage_stack. */
10482
10483 static void
10484 pop_message (void)
10485 {
10486 eassert (CONSP (Vmessage_stack));
10487 Vmessage_stack = XCDR (Vmessage_stack);
10488 }
10489
10490
10491 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10492 exits. If the stack is not empty, we have a missing pop_message
10493 somewhere. */
10494
10495 void
10496 check_message_stack (void)
10497 {
10498 if (!NILP (Vmessage_stack))
10499 abort ();
10500 }
10501
10502
10503 /* Truncate to NCHARS what will be displayed in the echo area the next
10504 time we display it---but don't redisplay it now. */
10505
10506 void
10507 truncate_echo_area (ptrdiff_t nchars)
10508 {
10509 if (nchars == 0)
10510 echo_area_buffer[0] = Qnil;
10511 /* A null message buffer means that the frame hasn't really been
10512 initialized yet. Error messages get reported properly by
10513 cmd_error, so this must be just an informative message; toss it. */
10514 else if (!noninteractive
10515 && INTERACTIVE
10516 && !NILP (echo_area_buffer[0]))
10517 {
10518 struct frame *sf = SELECTED_FRAME ();
10519 if (FRAME_MESSAGE_BUF (sf))
10520 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10521 }
10522 }
10523
10524
10525 /* Helper function for truncate_echo_area. Truncate the current
10526 message to at most NCHARS characters. */
10527
10528 static int
10529 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10530 {
10531 if (BEG + nchars < Z)
10532 del_range (BEG + nchars, Z);
10533 if (Z == BEG)
10534 echo_area_buffer[0] = Qnil;
10535 return 0;
10536 }
10537
10538
10539 /* Set the current message to a substring of S or STRING.
10540
10541 If STRING is a Lisp string, set the message to the first NBYTES
10542 bytes from STRING. NBYTES zero means use the whole string. If
10543 STRING is multibyte, the message will be displayed multibyte.
10544
10545 If S is not null, set the message to the first LEN bytes of S. LEN
10546 zero means use the whole string. MULTIBYTE_P non-zero means S is
10547 multibyte. Display the message multibyte in that case.
10548
10549 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10550 to t before calling set_message_1 (which calls insert).
10551 */
10552
10553 static void
10554 set_message (const char *s, Lisp_Object string,
10555 ptrdiff_t nbytes, int multibyte_p)
10556 {
10557 message_enable_multibyte
10558 = ((s && multibyte_p)
10559 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10560
10561 with_echo_area_buffer (0, -1, set_message_1,
10562 (intptr_t) s, string, nbytes, multibyte_p);
10563 message_buf_print = 0;
10564 help_echo_showing_p = 0;
10565 }
10566
10567
10568 /* Helper function for set_message. Arguments have the same meaning
10569 as there, with A1 corresponding to S and A2 corresponding to STRING
10570 This function is called with the echo area buffer being
10571 current. */
10572
10573 static int
10574 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10575 {
10576 intptr_t i1 = a1;
10577 const char *s = (const char *) i1;
10578 const unsigned char *msg = (const unsigned char *) s;
10579 Lisp_Object string = a2;
10580
10581 /* Change multibyteness of the echo buffer appropriately. */
10582 if (message_enable_multibyte
10583 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10584 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10585
10586 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10587 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10588 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10589
10590 /* Insert new message at BEG. */
10591 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10592
10593 if (STRINGP (string))
10594 {
10595 ptrdiff_t nchars;
10596
10597 if (nbytes == 0)
10598 nbytes = SBYTES (string);
10599 nchars = string_byte_to_char (string, nbytes);
10600
10601 /* This function takes care of single/multibyte conversion. We
10602 just have to ensure that the echo area buffer has the right
10603 setting of enable_multibyte_characters. */
10604 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10605 }
10606 else if (s)
10607 {
10608 if (nbytes == 0)
10609 nbytes = strlen (s);
10610
10611 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10612 {
10613 /* Convert from multi-byte to single-byte. */
10614 ptrdiff_t i;
10615 int c, n;
10616 char work[1];
10617
10618 /* Convert a multibyte string to single-byte. */
10619 for (i = 0; i < nbytes; i += n)
10620 {
10621 c = string_char_and_length (msg + i, &n);
10622 work[0] = (ASCII_CHAR_P (c)
10623 ? c
10624 : multibyte_char_to_unibyte (c));
10625 insert_1_both (work, 1, 1, 1, 0, 0);
10626 }
10627 }
10628 else if (!multibyte_p
10629 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10630 {
10631 /* Convert from single-byte to multi-byte. */
10632 ptrdiff_t i;
10633 int c, n;
10634 unsigned char str[MAX_MULTIBYTE_LENGTH];
10635
10636 /* Convert a single-byte string to multibyte. */
10637 for (i = 0; i < nbytes; i++)
10638 {
10639 c = msg[i];
10640 MAKE_CHAR_MULTIBYTE (c);
10641 n = CHAR_STRING (c, str);
10642 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10643 }
10644 }
10645 else
10646 insert_1 (s, nbytes, 1, 0, 0);
10647 }
10648
10649 return 0;
10650 }
10651
10652
10653 /* Clear messages. CURRENT_P non-zero means clear the current
10654 message. LAST_DISPLAYED_P non-zero means clear the message
10655 last displayed. */
10656
10657 void
10658 clear_message (int current_p, int last_displayed_p)
10659 {
10660 if (current_p)
10661 {
10662 echo_area_buffer[0] = Qnil;
10663 message_cleared_p = 1;
10664 }
10665
10666 if (last_displayed_p)
10667 echo_area_buffer[1] = Qnil;
10668
10669 message_buf_print = 0;
10670 }
10671
10672 /* Clear garbaged frames.
10673
10674 This function is used where the old redisplay called
10675 redraw_garbaged_frames which in turn called redraw_frame which in
10676 turn called clear_frame. The call to clear_frame was a source of
10677 flickering. I believe a clear_frame is not necessary. It should
10678 suffice in the new redisplay to invalidate all current matrices,
10679 and ensure a complete redisplay of all windows. */
10680
10681 static void
10682 clear_garbaged_frames (void)
10683 {
10684 if (frame_garbaged)
10685 {
10686 Lisp_Object tail, frame;
10687 int changed_count = 0;
10688
10689 FOR_EACH_FRAME (tail, frame)
10690 {
10691 struct frame *f = XFRAME (frame);
10692
10693 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10694 {
10695 if (f->resized_p)
10696 {
10697 Fredraw_frame (frame);
10698 f->force_flush_display_p = 1;
10699 }
10700 clear_current_matrices (f);
10701 changed_count++;
10702 f->garbaged = 0;
10703 f->resized_p = 0;
10704 }
10705 }
10706
10707 frame_garbaged = 0;
10708 if (changed_count)
10709 ++windows_or_buffers_changed;
10710 }
10711 }
10712
10713
10714 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10715 is non-zero update selected_frame. Value is non-zero if the
10716 mini-windows height has been changed. */
10717
10718 static int
10719 echo_area_display (int update_frame_p)
10720 {
10721 Lisp_Object mini_window;
10722 struct window *w;
10723 struct frame *f;
10724 int window_height_changed_p = 0;
10725 struct frame *sf = SELECTED_FRAME ();
10726
10727 mini_window = FRAME_MINIBUF_WINDOW (sf);
10728 w = XWINDOW (mini_window);
10729 f = XFRAME (WINDOW_FRAME (w));
10730
10731 /* Don't display if frame is invisible or not yet initialized. */
10732 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10733 return 0;
10734
10735 #ifdef HAVE_WINDOW_SYSTEM
10736 /* When Emacs starts, selected_frame may be the initial terminal
10737 frame. If we let this through, a message would be displayed on
10738 the terminal. */
10739 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10740 return 0;
10741 #endif /* HAVE_WINDOW_SYSTEM */
10742
10743 /* Redraw garbaged frames. */
10744 if (frame_garbaged)
10745 clear_garbaged_frames ();
10746
10747 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10748 {
10749 echo_area_window = mini_window;
10750 window_height_changed_p = display_echo_area (w);
10751 w->must_be_updated_p = 1;
10752
10753 /* Update the display, unless called from redisplay_internal.
10754 Also don't update the screen during redisplay itself. The
10755 update will happen at the end of redisplay, and an update
10756 here could cause confusion. */
10757 if (update_frame_p && !redisplaying_p)
10758 {
10759 int n = 0;
10760
10761 /* If the display update has been interrupted by pending
10762 input, update mode lines in the frame. Due to the
10763 pending input, it might have been that redisplay hasn't
10764 been called, so that mode lines above the echo area are
10765 garbaged. This looks odd, so we prevent it here. */
10766 if (!display_completed)
10767 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10768
10769 if (window_height_changed_p
10770 /* Don't do this if Emacs is shutting down. Redisplay
10771 needs to run hooks. */
10772 && !NILP (Vrun_hooks))
10773 {
10774 /* Must update other windows. Likewise as in other
10775 cases, don't let this update be interrupted by
10776 pending input. */
10777 ptrdiff_t count = SPECPDL_INDEX ();
10778 specbind (Qredisplay_dont_pause, Qt);
10779 windows_or_buffers_changed = 1;
10780 redisplay_internal ();
10781 unbind_to (count, Qnil);
10782 }
10783 else if (FRAME_WINDOW_P (f) && n == 0)
10784 {
10785 /* Window configuration is the same as before.
10786 Can do with a display update of the echo area,
10787 unless we displayed some mode lines. */
10788 update_single_window (w, 1);
10789 FRAME_RIF (f)->flush_display (f);
10790 }
10791 else
10792 update_frame (f, 1, 1);
10793
10794 /* If cursor is in the echo area, make sure that the next
10795 redisplay displays the minibuffer, so that the cursor will
10796 be replaced with what the minibuffer wants. */
10797 if (cursor_in_echo_area)
10798 ++windows_or_buffers_changed;
10799 }
10800 }
10801 else if (!EQ (mini_window, selected_window))
10802 windows_or_buffers_changed++;
10803
10804 /* Last displayed message is now the current message. */
10805 echo_area_buffer[1] = echo_area_buffer[0];
10806 /* Inform read_char that we're not echoing. */
10807 echo_message_buffer = Qnil;
10808
10809 /* Prevent redisplay optimization in redisplay_internal by resetting
10810 this_line_start_pos. This is done because the mini-buffer now
10811 displays the message instead of its buffer text. */
10812 if (EQ (mini_window, selected_window))
10813 CHARPOS (this_line_start_pos) = 0;
10814
10815 return window_height_changed_p;
10816 }
10817
10818
10819 \f
10820 /***********************************************************************
10821 Mode Lines and Frame Titles
10822 ***********************************************************************/
10823
10824 /* A buffer for constructing non-propertized mode-line strings and
10825 frame titles in it; allocated from the heap in init_xdisp and
10826 resized as needed in store_mode_line_noprop_char. */
10827
10828 static char *mode_line_noprop_buf;
10829
10830 /* The buffer's end, and a current output position in it. */
10831
10832 static char *mode_line_noprop_buf_end;
10833 static char *mode_line_noprop_ptr;
10834
10835 #define MODE_LINE_NOPROP_LEN(start) \
10836 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10837
10838 static enum {
10839 MODE_LINE_DISPLAY = 0,
10840 MODE_LINE_TITLE,
10841 MODE_LINE_NOPROP,
10842 MODE_LINE_STRING
10843 } mode_line_target;
10844
10845 /* Alist that caches the results of :propertize.
10846 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10847 static Lisp_Object mode_line_proptrans_alist;
10848
10849 /* List of strings making up the mode-line. */
10850 static Lisp_Object mode_line_string_list;
10851
10852 /* Base face property when building propertized mode line string. */
10853 static Lisp_Object mode_line_string_face;
10854 static Lisp_Object mode_line_string_face_prop;
10855
10856
10857 /* Unwind data for mode line strings */
10858
10859 static Lisp_Object Vmode_line_unwind_vector;
10860
10861 static Lisp_Object
10862 format_mode_line_unwind_data (struct frame *target_frame,
10863 struct buffer *obuf,
10864 Lisp_Object owin,
10865 int save_proptrans)
10866 {
10867 Lisp_Object vector, tmp;
10868
10869 /* Reduce consing by keeping one vector in
10870 Vwith_echo_area_save_vector. */
10871 vector = Vmode_line_unwind_vector;
10872 Vmode_line_unwind_vector = Qnil;
10873
10874 if (NILP (vector))
10875 vector = Fmake_vector (make_number (10), Qnil);
10876
10877 ASET (vector, 0, make_number (mode_line_target));
10878 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10879 ASET (vector, 2, mode_line_string_list);
10880 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10881 ASET (vector, 4, mode_line_string_face);
10882 ASET (vector, 5, mode_line_string_face_prop);
10883
10884 if (obuf)
10885 XSETBUFFER (tmp, obuf);
10886 else
10887 tmp = Qnil;
10888 ASET (vector, 6, tmp);
10889 ASET (vector, 7, owin);
10890 if (target_frame)
10891 {
10892 /* Similarly to `with-selected-window', if the operation selects
10893 a window on another frame, we must restore that frame's
10894 selected window, and (for a tty) the top-frame. */
10895 ASET (vector, 8, FVAR (target_frame, selected_window));
10896 if (FRAME_TERMCAP_P (target_frame))
10897 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10898 }
10899
10900 return vector;
10901 }
10902
10903 static Lisp_Object
10904 unwind_format_mode_line (Lisp_Object vector)
10905 {
10906 Lisp_Object old_window = AREF (vector, 7);
10907 Lisp_Object target_frame_window = AREF (vector, 8);
10908 Lisp_Object old_top_frame = AREF (vector, 9);
10909
10910 mode_line_target = XINT (AREF (vector, 0));
10911 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10912 mode_line_string_list = AREF (vector, 2);
10913 if (! EQ (AREF (vector, 3), Qt))
10914 mode_line_proptrans_alist = AREF (vector, 3);
10915 mode_line_string_face = AREF (vector, 4);
10916 mode_line_string_face_prop = AREF (vector, 5);
10917
10918 /* Select window before buffer, since it may change the buffer. */
10919 if (!NILP (old_window))
10920 {
10921 /* If the operation that we are unwinding had selected a window
10922 on a different frame, reset its frame-selected-window. For a
10923 text terminal, reset its top-frame if necessary. */
10924 if (!NILP (target_frame_window))
10925 {
10926 Lisp_Object frame
10927 = WINDOW_FRAME (XWINDOW (target_frame_window));
10928
10929 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10930 Fselect_window (target_frame_window, Qt);
10931
10932 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10933 Fselect_frame (old_top_frame, Qt);
10934 }
10935
10936 Fselect_window (old_window, Qt);
10937 }
10938
10939 if (!NILP (AREF (vector, 6)))
10940 {
10941 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10942 ASET (vector, 6, Qnil);
10943 }
10944
10945 Vmode_line_unwind_vector = vector;
10946 return Qnil;
10947 }
10948
10949
10950 /* Store a single character C for the frame title in mode_line_noprop_buf.
10951 Re-allocate mode_line_noprop_buf if necessary. */
10952
10953 static void
10954 store_mode_line_noprop_char (char c)
10955 {
10956 /* If output position has reached the end of the allocated buffer,
10957 increase the buffer's size. */
10958 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10959 {
10960 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10961 ptrdiff_t size = len;
10962 mode_line_noprop_buf =
10963 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10964 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10965 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10966 }
10967
10968 *mode_line_noprop_ptr++ = c;
10969 }
10970
10971
10972 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10973 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10974 characters that yield more columns than PRECISION; PRECISION <= 0
10975 means copy the whole string. Pad with spaces until FIELD_WIDTH
10976 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10977 pad. Called from display_mode_element when it is used to build a
10978 frame title. */
10979
10980 static int
10981 store_mode_line_noprop (const char *string, int field_width, int precision)
10982 {
10983 const unsigned char *str = (const unsigned char *) string;
10984 int n = 0;
10985 ptrdiff_t dummy, nbytes;
10986
10987 /* Copy at most PRECISION chars from STR. */
10988 nbytes = strlen (string);
10989 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10990 while (nbytes--)
10991 store_mode_line_noprop_char (*str++);
10992
10993 /* Fill up with spaces until FIELD_WIDTH reached. */
10994 while (field_width > 0
10995 && n < field_width)
10996 {
10997 store_mode_line_noprop_char (' ');
10998 ++n;
10999 }
11000
11001 return n;
11002 }
11003
11004 /***********************************************************************
11005 Frame Titles
11006 ***********************************************************************/
11007
11008 #ifdef HAVE_WINDOW_SYSTEM
11009
11010 /* Set the title of FRAME, if it has changed. The title format is
11011 Vicon_title_format if FRAME is iconified, otherwise it is
11012 frame_title_format. */
11013
11014 static void
11015 x_consider_frame_title (Lisp_Object frame)
11016 {
11017 struct frame *f = XFRAME (frame);
11018
11019 if (FRAME_WINDOW_P (f)
11020 || FRAME_MINIBUF_ONLY_P (f)
11021 || f->explicit_name)
11022 {
11023 /* Do we have more than one visible frame on this X display? */
11024 Lisp_Object tail;
11025 Lisp_Object fmt;
11026 ptrdiff_t title_start;
11027 char *title;
11028 ptrdiff_t len;
11029 struct it it;
11030 ptrdiff_t count = SPECPDL_INDEX ();
11031
11032 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11033 {
11034 Lisp_Object other_frame = XCAR (tail);
11035 struct frame *tf = XFRAME (other_frame);
11036
11037 if (tf != f
11038 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11039 && !FRAME_MINIBUF_ONLY_P (tf)
11040 && !EQ (other_frame, tip_frame)
11041 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11042 break;
11043 }
11044
11045 /* Set global variable indicating that multiple frames exist. */
11046 multiple_frames = CONSP (tail);
11047
11048 /* Switch to the buffer of selected window of the frame. Set up
11049 mode_line_target so that display_mode_element will output into
11050 mode_line_noprop_buf; then display the title. */
11051 record_unwind_protect (unwind_format_mode_line,
11052 format_mode_line_unwind_data
11053 (f, current_buffer, selected_window, 0));
11054
11055 Fselect_window (FVAR (f, selected_window), Qt);
11056 set_buffer_internal_1 (XBUFFER (XWINDOW (FVAR (f, selected_window))->buffer));
11057 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11058
11059 mode_line_target = MODE_LINE_TITLE;
11060 title_start = MODE_LINE_NOPROP_LEN (0);
11061 init_iterator (&it, XWINDOW (FVAR (f, selected_window)), -1, -1,
11062 NULL, DEFAULT_FACE_ID);
11063 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11064 len = MODE_LINE_NOPROP_LEN (title_start);
11065 title = mode_line_noprop_buf + title_start;
11066 unbind_to (count, Qnil);
11067
11068 /* Set the title only if it's changed. This avoids consing in
11069 the common case where it hasn't. (If it turns out that we've
11070 already wasted too much time by walking through the list with
11071 display_mode_element, then we might need to optimize at a
11072 higher level than this.) */
11073 if (! STRINGP (FVAR (f, name))
11074 || SBYTES (FVAR (f, name)) != len
11075 || memcmp (title, SDATA (FVAR (f, name)), len) != 0)
11076 x_implicitly_set_name (f, make_string (title, len), Qnil);
11077 }
11078 }
11079
11080 #endif /* not HAVE_WINDOW_SYSTEM */
11081
11082 \f
11083 /***********************************************************************
11084 Menu Bars
11085 ***********************************************************************/
11086
11087
11088 /* Prepare for redisplay by updating menu-bar item lists when
11089 appropriate. This can call eval. */
11090
11091 void
11092 prepare_menu_bars (void)
11093 {
11094 int all_windows;
11095 struct gcpro gcpro1, gcpro2;
11096 struct frame *f;
11097 Lisp_Object tooltip_frame;
11098
11099 #ifdef HAVE_WINDOW_SYSTEM
11100 tooltip_frame = tip_frame;
11101 #else
11102 tooltip_frame = Qnil;
11103 #endif
11104
11105 /* Update all frame titles based on their buffer names, etc. We do
11106 this before the menu bars so that the buffer-menu will show the
11107 up-to-date frame titles. */
11108 #ifdef HAVE_WINDOW_SYSTEM
11109 if (windows_or_buffers_changed || update_mode_lines)
11110 {
11111 Lisp_Object tail, frame;
11112
11113 FOR_EACH_FRAME (tail, frame)
11114 {
11115 f = XFRAME (frame);
11116 if (!EQ (frame, tooltip_frame)
11117 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11118 x_consider_frame_title (frame);
11119 }
11120 }
11121 #endif /* HAVE_WINDOW_SYSTEM */
11122
11123 /* Update the menu bar item lists, if appropriate. This has to be
11124 done before any actual redisplay or generation of display lines. */
11125 all_windows = (update_mode_lines
11126 || buffer_shared > 1
11127 || windows_or_buffers_changed);
11128 if (all_windows)
11129 {
11130 Lisp_Object tail, frame;
11131 ptrdiff_t count = SPECPDL_INDEX ();
11132 /* 1 means that update_menu_bar has run its hooks
11133 so any further calls to update_menu_bar shouldn't do so again. */
11134 int menu_bar_hooks_run = 0;
11135
11136 record_unwind_save_match_data ();
11137
11138 FOR_EACH_FRAME (tail, frame)
11139 {
11140 f = XFRAME (frame);
11141
11142 /* Ignore tooltip frame. */
11143 if (EQ (frame, tooltip_frame))
11144 continue;
11145
11146 /* If a window on this frame changed size, report that to
11147 the user and clear the size-change flag. */
11148 if (FRAME_WINDOW_SIZES_CHANGED (f))
11149 {
11150 Lisp_Object functions;
11151
11152 /* Clear flag first in case we get an error below. */
11153 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11154 functions = Vwindow_size_change_functions;
11155 GCPRO2 (tail, functions);
11156
11157 while (CONSP (functions))
11158 {
11159 if (!EQ (XCAR (functions), Qt))
11160 call1 (XCAR (functions), frame);
11161 functions = XCDR (functions);
11162 }
11163 UNGCPRO;
11164 }
11165
11166 GCPRO1 (tail);
11167 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11168 #ifdef HAVE_WINDOW_SYSTEM
11169 update_tool_bar (f, 0);
11170 #endif
11171 #ifdef HAVE_NS
11172 if (windows_or_buffers_changed
11173 && FRAME_NS_P (f))
11174 ns_set_doc_edited (f, Fbuffer_modified_p
11175 (XWINDOW (FVAR (f, selected_window))->buffer));
11176 #endif
11177 UNGCPRO;
11178 }
11179
11180 unbind_to (count, Qnil);
11181 }
11182 else
11183 {
11184 struct frame *sf = SELECTED_FRAME ();
11185 update_menu_bar (sf, 1, 0);
11186 #ifdef HAVE_WINDOW_SYSTEM
11187 update_tool_bar (sf, 1);
11188 #endif
11189 }
11190 }
11191
11192
11193 /* Update the menu bar item list for frame F. This has to be done
11194 before we start to fill in any display lines, because it can call
11195 eval.
11196
11197 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11198
11199 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11200 already ran the menu bar hooks for this redisplay, so there
11201 is no need to run them again. The return value is the
11202 updated value of this flag, to pass to the next call. */
11203
11204 static int
11205 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11206 {
11207 Lisp_Object window;
11208 register struct window *w;
11209
11210 /* If called recursively during a menu update, do nothing. This can
11211 happen when, for instance, an activate-menubar-hook causes a
11212 redisplay. */
11213 if (inhibit_menubar_update)
11214 return hooks_run;
11215
11216 window = FRAME_SELECTED_WINDOW (f);
11217 w = XWINDOW (window);
11218
11219 if (FRAME_WINDOW_P (f)
11220 ?
11221 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11222 || defined (HAVE_NS) || defined (USE_GTK)
11223 FRAME_EXTERNAL_MENU_BAR (f)
11224 #else
11225 FRAME_MENU_BAR_LINES (f) > 0
11226 #endif
11227 : FRAME_MENU_BAR_LINES (f) > 0)
11228 {
11229 /* If the user has switched buffers or windows, we need to
11230 recompute to reflect the new bindings. But we'll
11231 recompute when update_mode_lines is set too; that means
11232 that people can use force-mode-line-update to request
11233 that the menu bar be recomputed. The adverse effect on
11234 the rest of the redisplay algorithm is about the same as
11235 windows_or_buffers_changed anyway. */
11236 if (windows_or_buffers_changed
11237 /* This used to test w->update_mode_line, but we believe
11238 there is no need to recompute the menu in that case. */
11239 || update_mode_lines
11240 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11241 < BUF_MODIFF (XBUFFER (w->buffer)))
11242 != w->last_had_star)
11243 || ((!NILP (Vtransient_mark_mode)
11244 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11245 != !NILP (w->region_showing)))
11246 {
11247 struct buffer *prev = current_buffer;
11248 ptrdiff_t count = SPECPDL_INDEX ();
11249
11250 specbind (Qinhibit_menubar_update, Qt);
11251
11252 set_buffer_internal_1 (XBUFFER (w->buffer));
11253 if (save_match_data)
11254 record_unwind_save_match_data ();
11255 if (NILP (Voverriding_local_map_menu_flag))
11256 {
11257 specbind (Qoverriding_terminal_local_map, Qnil);
11258 specbind (Qoverriding_local_map, Qnil);
11259 }
11260
11261 if (!hooks_run)
11262 {
11263 /* Run the Lucid hook. */
11264 safe_run_hooks (Qactivate_menubar_hook);
11265
11266 /* If it has changed current-menubar from previous value,
11267 really recompute the menu-bar from the value. */
11268 if (! NILP (Vlucid_menu_bar_dirty_flag))
11269 call0 (Qrecompute_lucid_menubar);
11270
11271 safe_run_hooks (Qmenu_bar_update_hook);
11272
11273 hooks_run = 1;
11274 }
11275
11276 XSETFRAME (Vmenu_updating_frame, f);
11277 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11278
11279 /* Redisplay the menu bar in case we changed it. */
11280 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11281 || defined (HAVE_NS) || defined (USE_GTK)
11282 if (FRAME_WINDOW_P (f))
11283 {
11284 #if defined (HAVE_NS)
11285 /* All frames on Mac OS share the same menubar. So only
11286 the selected frame should be allowed to set it. */
11287 if (f == SELECTED_FRAME ())
11288 #endif
11289 set_frame_menubar (f, 0, 0);
11290 }
11291 else
11292 /* On a terminal screen, the menu bar is an ordinary screen
11293 line, and this makes it get updated. */
11294 w->update_mode_line = 1;
11295 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11296 /* In the non-toolkit version, the menu bar is an ordinary screen
11297 line, and this makes it get updated. */
11298 w->update_mode_line = 1;
11299 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11300
11301 unbind_to (count, Qnil);
11302 set_buffer_internal_1 (prev);
11303 }
11304 }
11305
11306 return hooks_run;
11307 }
11308
11309
11310 \f
11311 /***********************************************************************
11312 Output Cursor
11313 ***********************************************************************/
11314
11315 #ifdef HAVE_WINDOW_SYSTEM
11316
11317 /* EXPORT:
11318 Nominal cursor position -- where to draw output.
11319 HPOS and VPOS are window relative glyph matrix coordinates.
11320 X and Y are window relative pixel coordinates. */
11321
11322 struct cursor_pos output_cursor;
11323
11324
11325 /* EXPORT:
11326 Set the global variable output_cursor to CURSOR. All cursor
11327 positions are relative to updated_window. */
11328
11329 void
11330 set_output_cursor (struct cursor_pos *cursor)
11331 {
11332 output_cursor.hpos = cursor->hpos;
11333 output_cursor.vpos = cursor->vpos;
11334 output_cursor.x = cursor->x;
11335 output_cursor.y = cursor->y;
11336 }
11337
11338
11339 /* EXPORT for RIF:
11340 Set a nominal cursor position.
11341
11342 HPOS and VPOS are column/row positions in a window glyph matrix. X
11343 and Y are window text area relative pixel positions.
11344
11345 If this is done during an update, updated_window will contain the
11346 window that is being updated and the position is the future output
11347 cursor position for that window. If updated_window is null, use
11348 selected_window and display the cursor at the given position. */
11349
11350 void
11351 x_cursor_to (int vpos, int hpos, int y, int x)
11352 {
11353 struct window *w;
11354
11355 /* If updated_window is not set, work on selected_window. */
11356 if (updated_window)
11357 w = updated_window;
11358 else
11359 w = XWINDOW (selected_window);
11360
11361 /* Set the output cursor. */
11362 output_cursor.hpos = hpos;
11363 output_cursor.vpos = vpos;
11364 output_cursor.x = x;
11365 output_cursor.y = y;
11366
11367 /* If not called as part of an update, really display the cursor.
11368 This will also set the cursor position of W. */
11369 if (updated_window == NULL)
11370 {
11371 BLOCK_INPUT;
11372 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11373 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11374 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11375 UNBLOCK_INPUT;
11376 }
11377 }
11378
11379 #endif /* HAVE_WINDOW_SYSTEM */
11380
11381 \f
11382 /***********************************************************************
11383 Tool-bars
11384 ***********************************************************************/
11385
11386 #ifdef HAVE_WINDOW_SYSTEM
11387
11388 /* Where the mouse was last time we reported a mouse event. */
11389
11390 FRAME_PTR last_mouse_frame;
11391
11392 /* Tool-bar item index of the item on which a mouse button was pressed
11393 or -1. */
11394
11395 int last_tool_bar_item;
11396
11397
11398 static Lisp_Object
11399 update_tool_bar_unwind (Lisp_Object frame)
11400 {
11401 selected_frame = frame;
11402 return Qnil;
11403 }
11404
11405 /* Update the tool-bar item list for frame F. This has to be done
11406 before we start to fill in any display lines. Called from
11407 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11408 and restore it here. */
11409
11410 static void
11411 update_tool_bar (struct frame *f, int save_match_data)
11412 {
11413 #if defined (USE_GTK) || defined (HAVE_NS)
11414 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11415 #else
11416 int do_update = WINDOWP (FVAR (f, tool_bar_window))
11417 && WINDOW_TOTAL_LINES (XWINDOW (FVAR (f, tool_bar_window))) > 0;
11418 #endif
11419
11420 if (do_update)
11421 {
11422 Lisp_Object window;
11423 struct window *w;
11424
11425 window = FRAME_SELECTED_WINDOW (f);
11426 w = XWINDOW (window);
11427
11428 /* If the user has switched buffers or windows, we need to
11429 recompute to reflect the new bindings. But we'll
11430 recompute when update_mode_lines is set too; that means
11431 that people can use force-mode-line-update to request
11432 that the menu bar be recomputed. The adverse effect on
11433 the rest of the redisplay algorithm is about the same as
11434 windows_or_buffers_changed anyway. */
11435 if (windows_or_buffers_changed
11436 || w->update_mode_line
11437 || update_mode_lines
11438 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11439 < BUF_MODIFF (XBUFFER (w->buffer)))
11440 != w->last_had_star)
11441 || ((!NILP (Vtransient_mark_mode)
11442 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11443 != !NILP (w->region_showing)))
11444 {
11445 struct buffer *prev = current_buffer;
11446 ptrdiff_t count = SPECPDL_INDEX ();
11447 Lisp_Object frame, new_tool_bar;
11448 int new_n_tool_bar;
11449 struct gcpro gcpro1;
11450
11451 /* Set current_buffer to the buffer of the selected
11452 window of the frame, so that we get the right local
11453 keymaps. */
11454 set_buffer_internal_1 (XBUFFER (w->buffer));
11455
11456 /* Save match data, if we must. */
11457 if (save_match_data)
11458 record_unwind_save_match_data ();
11459
11460 /* Make sure that we don't accidentally use bogus keymaps. */
11461 if (NILP (Voverriding_local_map_menu_flag))
11462 {
11463 specbind (Qoverriding_terminal_local_map, Qnil);
11464 specbind (Qoverriding_local_map, Qnil);
11465 }
11466
11467 GCPRO1 (new_tool_bar);
11468
11469 /* We must temporarily set the selected frame to this frame
11470 before calling tool_bar_items, because the calculation of
11471 the tool-bar keymap uses the selected frame (see
11472 `tool-bar-make-keymap' in tool-bar.el). */
11473 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11474 XSETFRAME (frame, f);
11475 selected_frame = frame;
11476
11477 /* Build desired tool-bar items from keymaps. */
11478 new_tool_bar = tool_bar_items
11479 (Fcopy_sequence (FVAR (f, tool_bar_items)), &new_n_tool_bar);
11480
11481 /* Redisplay the tool-bar if we changed it. */
11482 if (new_n_tool_bar != f->n_tool_bar_items
11483 || NILP (Fequal (new_tool_bar, FVAR (f, tool_bar_items))))
11484 {
11485 /* Redisplay that happens asynchronously due to an expose event
11486 may access f->tool_bar_items. Make sure we update both
11487 variables within BLOCK_INPUT so no such event interrupts. */
11488 BLOCK_INPUT;
11489 FVAR (f, tool_bar_items) = new_tool_bar;
11490 f->n_tool_bar_items = new_n_tool_bar;
11491 w->update_mode_line = 1;
11492 UNBLOCK_INPUT;
11493 }
11494
11495 UNGCPRO;
11496
11497 unbind_to (count, Qnil);
11498 set_buffer_internal_1 (prev);
11499 }
11500 }
11501 }
11502
11503
11504 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11505 F's desired tool-bar contents. F->tool_bar_items must have
11506 been set up previously by calling prepare_menu_bars. */
11507
11508 static void
11509 build_desired_tool_bar_string (struct frame *f)
11510 {
11511 int i, size, size_needed;
11512 struct gcpro gcpro1, gcpro2, gcpro3;
11513 Lisp_Object image, plist, props;
11514
11515 image = plist = props = Qnil;
11516 GCPRO3 (image, plist, props);
11517
11518 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11519 Otherwise, make a new string. */
11520
11521 /* The size of the string we might be able to reuse. */
11522 size = (STRINGP (FVAR (f, desired_tool_bar_string))
11523 ? SCHARS (FVAR (f, desired_tool_bar_string))
11524 : 0);
11525
11526 /* We need one space in the string for each image. */
11527 size_needed = f->n_tool_bar_items;
11528
11529 /* Reuse f->desired_tool_bar_string, if possible. */
11530 if (size < size_needed || NILP (FVAR (f, desired_tool_bar_string)))
11531 FVAR (f, desired_tool_bar_string)
11532 = Fmake_string (make_number (size_needed), make_number (' '));
11533 else
11534 {
11535 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11536 Fremove_text_properties (make_number (0), make_number (size),
11537 props, FVAR (f, desired_tool_bar_string));
11538 }
11539
11540 /* Put a `display' property on the string for the images to display,
11541 put a `menu_item' property on tool-bar items with a value that
11542 is the index of the item in F's tool-bar item vector. */
11543 for (i = 0; i < f->n_tool_bar_items; ++i)
11544 {
11545 #define PROP(IDX) \
11546 AREF (FVAR (f, tool_bar_items), i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11547
11548 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11549 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11550 int hmargin, vmargin, relief, idx, end;
11551
11552 /* If image is a vector, choose the image according to the
11553 button state. */
11554 image = PROP (TOOL_BAR_ITEM_IMAGES);
11555 if (VECTORP (image))
11556 {
11557 if (enabled_p)
11558 idx = (selected_p
11559 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11560 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11561 else
11562 idx = (selected_p
11563 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11564 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11565
11566 eassert (ASIZE (image) >= idx);
11567 image = AREF (image, idx);
11568 }
11569 else
11570 idx = -1;
11571
11572 /* Ignore invalid image specifications. */
11573 if (!valid_image_p (image))
11574 continue;
11575
11576 /* Display the tool-bar button pressed, or depressed. */
11577 plist = Fcopy_sequence (XCDR (image));
11578
11579 /* Compute margin and relief to draw. */
11580 relief = (tool_bar_button_relief >= 0
11581 ? tool_bar_button_relief
11582 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11583 hmargin = vmargin = relief;
11584
11585 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11586 INT_MAX - max (hmargin, vmargin)))
11587 {
11588 hmargin += XFASTINT (Vtool_bar_button_margin);
11589 vmargin += XFASTINT (Vtool_bar_button_margin);
11590 }
11591 else if (CONSP (Vtool_bar_button_margin))
11592 {
11593 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11594 INT_MAX - hmargin))
11595 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11596
11597 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11598 INT_MAX - vmargin))
11599 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11600 }
11601
11602 if (auto_raise_tool_bar_buttons_p)
11603 {
11604 /* Add a `:relief' property to the image spec if the item is
11605 selected. */
11606 if (selected_p)
11607 {
11608 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11609 hmargin -= relief;
11610 vmargin -= relief;
11611 }
11612 }
11613 else
11614 {
11615 /* If image is selected, display it pressed, i.e. with a
11616 negative relief. If it's not selected, display it with a
11617 raised relief. */
11618 plist = Fplist_put (plist, QCrelief,
11619 (selected_p
11620 ? make_number (-relief)
11621 : make_number (relief)));
11622 hmargin -= relief;
11623 vmargin -= relief;
11624 }
11625
11626 /* Put a margin around the image. */
11627 if (hmargin || vmargin)
11628 {
11629 if (hmargin == vmargin)
11630 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11631 else
11632 plist = Fplist_put (plist, QCmargin,
11633 Fcons (make_number (hmargin),
11634 make_number (vmargin)));
11635 }
11636
11637 /* If button is not enabled, and we don't have special images
11638 for the disabled state, make the image appear disabled by
11639 applying an appropriate algorithm to it. */
11640 if (!enabled_p && idx < 0)
11641 plist = Fplist_put (plist, QCconversion, Qdisabled);
11642
11643 /* Put a `display' text property on the string for the image to
11644 display. Put a `menu-item' property on the string that gives
11645 the start of this item's properties in the tool-bar items
11646 vector. */
11647 image = Fcons (Qimage, plist);
11648 props = list4 (Qdisplay, image,
11649 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11650
11651 /* Let the last image hide all remaining spaces in the tool bar
11652 string. The string can be longer than needed when we reuse a
11653 previous string. */
11654 if (i + 1 == f->n_tool_bar_items)
11655 end = SCHARS (FVAR (f, desired_tool_bar_string));
11656 else
11657 end = i + 1;
11658 Fadd_text_properties (make_number (i), make_number (end),
11659 props, FVAR (f, desired_tool_bar_string));
11660 #undef PROP
11661 }
11662
11663 UNGCPRO;
11664 }
11665
11666
11667 /* Display one line of the tool-bar of frame IT->f.
11668
11669 HEIGHT specifies the desired height of the tool-bar line.
11670 If the actual height of the glyph row is less than HEIGHT, the
11671 row's height is increased to HEIGHT, and the icons are centered
11672 vertically in the new height.
11673
11674 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11675 count a final empty row in case the tool-bar width exactly matches
11676 the window width.
11677 */
11678
11679 static void
11680 display_tool_bar_line (struct it *it, int height)
11681 {
11682 struct glyph_row *row = it->glyph_row;
11683 int max_x = it->last_visible_x;
11684 struct glyph *last;
11685
11686 prepare_desired_row (row);
11687 row->y = it->current_y;
11688
11689 /* Note that this isn't made use of if the face hasn't a box,
11690 so there's no need to check the face here. */
11691 it->start_of_box_run_p = 1;
11692
11693 while (it->current_x < max_x)
11694 {
11695 int x, n_glyphs_before, i, nglyphs;
11696 struct it it_before;
11697
11698 /* Get the next display element. */
11699 if (!get_next_display_element (it))
11700 {
11701 /* Don't count empty row if we are counting needed tool-bar lines. */
11702 if (height < 0 && !it->hpos)
11703 return;
11704 break;
11705 }
11706
11707 /* Produce glyphs. */
11708 n_glyphs_before = row->used[TEXT_AREA];
11709 it_before = *it;
11710
11711 PRODUCE_GLYPHS (it);
11712
11713 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11714 i = 0;
11715 x = it_before.current_x;
11716 while (i < nglyphs)
11717 {
11718 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11719
11720 if (x + glyph->pixel_width > max_x)
11721 {
11722 /* Glyph doesn't fit on line. Backtrack. */
11723 row->used[TEXT_AREA] = n_glyphs_before;
11724 *it = it_before;
11725 /* If this is the only glyph on this line, it will never fit on the
11726 tool-bar, so skip it. But ensure there is at least one glyph,
11727 so we don't accidentally disable the tool-bar. */
11728 if (n_glyphs_before == 0
11729 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11730 break;
11731 goto out;
11732 }
11733
11734 ++it->hpos;
11735 x += glyph->pixel_width;
11736 ++i;
11737 }
11738
11739 /* Stop at line end. */
11740 if (ITERATOR_AT_END_OF_LINE_P (it))
11741 break;
11742
11743 set_iterator_to_next (it, 1);
11744 }
11745
11746 out:;
11747
11748 row->displays_text_p = row->used[TEXT_AREA] != 0;
11749
11750 /* Use default face for the border below the tool bar.
11751
11752 FIXME: When auto-resize-tool-bars is grow-only, there is
11753 no additional border below the possibly empty tool-bar lines.
11754 So to make the extra empty lines look "normal", we have to
11755 use the tool-bar face for the border too. */
11756 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11757 it->face_id = DEFAULT_FACE_ID;
11758
11759 extend_face_to_end_of_line (it);
11760 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11761 last->right_box_line_p = 1;
11762 if (last == row->glyphs[TEXT_AREA])
11763 last->left_box_line_p = 1;
11764
11765 /* Make line the desired height and center it vertically. */
11766 if ((height -= it->max_ascent + it->max_descent) > 0)
11767 {
11768 /* Don't add more than one line height. */
11769 height %= FRAME_LINE_HEIGHT (it->f);
11770 it->max_ascent += height / 2;
11771 it->max_descent += (height + 1) / 2;
11772 }
11773
11774 compute_line_metrics (it);
11775
11776 /* If line is empty, make it occupy the rest of the tool-bar. */
11777 if (!row->displays_text_p)
11778 {
11779 row->height = row->phys_height = it->last_visible_y - row->y;
11780 row->visible_height = row->height;
11781 row->ascent = row->phys_ascent = 0;
11782 row->extra_line_spacing = 0;
11783 }
11784
11785 row->full_width_p = 1;
11786 row->continued_p = 0;
11787 row->truncated_on_left_p = 0;
11788 row->truncated_on_right_p = 0;
11789
11790 it->current_x = it->hpos = 0;
11791 it->current_y += row->height;
11792 ++it->vpos;
11793 ++it->glyph_row;
11794 }
11795
11796
11797 /* Max tool-bar height. */
11798
11799 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11800 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11801
11802 /* Value is the number of screen lines needed to make all tool-bar
11803 items of frame F visible. The number of actual rows needed is
11804 returned in *N_ROWS if non-NULL. */
11805
11806 static int
11807 tool_bar_lines_needed (struct frame *f, int *n_rows)
11808 {
11809 struct window *w = XWINDOW (FVAR (f, tool_bar_window));
11810 struct it it;
11811 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11812 the desired matrix, so use (unused) mode-line row as temporary row to
11813 avoid destroying the first tool-bar row. */
11814 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11815
11816 /* Initialize an iterator for iteration over
11817 F->desired_tool_bar_string in the tool-bar window of frame F. */
11818 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11819 it.first_visible_x = 0;
11820 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11821 reseat_to_string (&it, NULL, FVAR (f, desired_tool_bar_string), 0, 0, 0, -1);
11822 it.paragraph_embedding = L2R;
11823
11824 while (!ITERATOR_AT_END_P (&it))
11825 {
11826 clear_glyph_row (temp_row);
11827 it.glyph_row = temp_row;
11828 display_tool_bar_line (&it, -1);
11829 }
11830 clear_glyph_row (temp_row);
11831
11832 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11833 if (n_rows)
11834 *n_rows = it.vpos > 0 ? it.vpos : -1;
11835
11836 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11837 }
11838
11839
11840 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11841 0, 1, 0,
11842 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11843 (Lisp_Object frame)
11844 {
11845 struct frame *f;
11846 struct window *w;
11847 int nlines = 0;
11848
11849 if (NILP (frame))
11850 frame = selected_frame;
11851 else
11852 CHECK_FRAME (frame);
11853 f = XFRAME (frame);
11854
11855 if (WINDOWP (FVAR (f, tool_bar_window))
11856 && (w = XWINDOW (FVAR (f, tool_bar_window)),
11857 WINDOW_TOTAL_LINES (w) > 0))
11858 {
11859 update_tool_bar (f, 1);
11860 if (f->n_tool_bar_items)
11861 {
11862 build_desired_tool_bar_string (f);
11863 nlines = tool_bar_lines_needed (f, NULL);
11864 }
11865 }
11866
11867 return make_number (nlines);
11868 }
11869
11870
11871 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11872 height should be changed. */
11873
11874 static int
11875 redisplay_tool_bar (struct frame *f)
11876 {
11877 struct window *w;
11878 struct it it;
11879 struct glyph_row *row;
11880
11881 #if defined (USE_GTK) || defined (HAVE_NS)
11882 if (FRAME_EXTERNAL_TOOL_BAR (f))
11883 update_frame_tool_bar (f);
11884 return 0;
11885 #endif
11886
11887 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11888 do anything. This means you must start with tool-bar-lines
11889 non-zero to get the auto-sizing effect. Or in other words, you
11890 can turn off tool-bars by specifying tool-bar-lines zero. */
11891 if (!WINDOWP (FVAR (f, tool_bar_window))
11892 || (w = XWINDOW (FVAR (f, tool_bar_window)),
11893 WINDOW_TOTAL_LINES (w) == 0))
11894 return 0;
11895
11896 /* Set up an iterator for the tool-bar window. */
11897 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11898 it.first_visible_x = 0;
11899 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11900 row = it.glyph_row;
11901
11902 /* Build a string that represents the contents of the tool-bar. */
11903 build_desired_tool_bar_string (f);
11904 reseat_to_string (&it, NULL, FVAR (f, desired_tool_bar_string), 0, 0, 0, -1);
11905 /* FIXME: This should be controlled by a user option. But it
11906 doesn't make sense to have an R2L tool bar if the menu bar cannot
11907 be drawn also R2L, and making the menu bar R2L is tricky due
11908 toolkit-specific code that implements it. If an R2L tool bar is
11909 ever supported, display_tool_bar_line should also be augmented to
11910 call unproduce_glyphs like display_line and display_string
11911 do. */
11912 it.paragraph_embedding = L2R;
11913
11914 if (f->n_tool_bar_rows == 0)
11915 {
11916 int nlines;
11917
11918 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11919 nlines != WINDOW_TOTAL_LINES (w)))
11920 {
11921 Lisp_Object frame;
11922 int old_height = WINDOW_TOTAL_LINES (w);
11923
11924 XSETFRAME (frame, f);
11925 Fmodify_frame_parameters (frame,
11926 Fcons (Fcons (Qtool_bar_lines,
11927 make_number (nlines)),
11928 Qnil));
11929 if (WINDOW_TOTAL_LINES (w) != old_height)
11930 {
11931 clear_glyph_matrix (w->desired_matrix);
11932 fonts_changed_p = 1;
11933 return 1;
11934 }
11935 }
11936 }
11937
11938 /* Display as many lines as needed to display all tool-bar items. */
11939
11940 if (f->n_tool_bar_rows > 0)
11941 {
11942 int border, rows, height, extra;
11943
11944 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11945 border = XINT (Vtool_bar_border);
11946 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11947 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11948 else if (EQ (Vtool_bar_border, Qborder_width))
11949 border = f->border_width;
11950 else
11951 border = 0;
11952 if (border < 0)
11953 border = 0;
11954
11955 rows = f->n_tool_bar_rows;
11956 height = max (1, (it.last_visible_y - border) / rows);
11957 extra = it.last_visible_y - border - height * rows;
11958
11959 while (it.current_y < it.last_visible_y)
11960 {
11961 int h = 0;
11962 if (extra > 0 && rows-- > 0)
11963 {
11964 h = (extra + rows - 1) / rows;
11965 extra -= h;
11966 }
11967 display_tool_bar_line (&it, height + h);
11968 }
11969 }
11970 else
11971 {
11972 while (it.current_y < it.last_visible_y)
11973 display_tool_bar_line (&it, 0);
11974 }
11975
11976 /* It doesn't make much sense to try scrolling in the tool-bar
11977 window, so don't do it. */
11978 w->desired_matrix->no_scrolling_p = 1;
11979 w->must_be_updated_p = 1;
11980
11981 if (!NILP (Vauto_resize_tool_bars))
11982 {
11983 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11984 int change_height_p = 0;
11985
11986 /* If we couldn't display everything, change the tool-bar's
11987 height if there is room for more. */
11988 if (IT_STRING_CHARPOS (it) < it.end_charpos
11989 && it.current_y < max_tool_bar_height)
11990 change_height_p = 1;
11991
11992 row = it.glyph_row - 1;
11993
11994 /* If there are blank lines at the end, except for a partially
11995 visible blank line at the end that is smaller than
11996 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11997 if (!row->displays_text_p
11998 && row->height >= FRAME_LINE_HEIGHT (f))
11999 change_height_p = 1;
12000
12001 /* If row displays tool-bar items, but is partially visible,
12002 change the tool-bar's height. */
12003 if (row->displays_text_p
12004 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12005 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12006 change_height_p = 1;
12007
12008 /* Resize windows as needed by changing the `tool-bar-lines'
12009 frame parameter. */
12010 if (change_height_p)
12011 {
12012 Lisp_Object frame;
12013 int old_height = WINDOW_TOTAL_LINES (w);
12014 int nrows;
12015 int nlines = tool_bar_lines_needed (f, &nrows);
12016
12017 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12018 && !f->minimize_tool_bar_window_p)
12019 ? (nlines > old_height)
12020 : (nlines != old_height));
12021 f->minimize_tool_bar_window_p = 0;
12022
12023 if (change_height_p)
12024 {
12025 XSETFRAME (frame, f);
12026 Fmodify_frame_parameters (frame,
12027 Fcons (Fcons (Qtool_bar_lines,
12028 make_number (nlines)),
12029 Qnil));
12030 if (WINDOW_TOTAL_LINES (w) != old_height)
12031 {
12032 clear_glyph_matrix (w->desired_matrix);
12033 f->n_tool_bar_rows = nrows;
12034 fonts_changed_p = 1;
12035 return 1;
12036 }
12037 }
12038 }
12039 }
12040
12041 f->minimize_tool_bar_window_p = 0;
12042 return 0;
12043 }
12044
12045
12046 /* Get information about the tool-bar item which is displayed in GLYPH
12047 on frame F. Return in *PROP_IDX the index where tool-bar item
12048 properties start in F->tool_bar_items. Value is zero if
12049 GLYPH doesn't display a tool-bar item. */
12050
12051 static int
12052 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12053 {
12054 Lisp_Object prop;
12055 int success_p;
12056 int charpos;
12057
12058 /* This function can be called asynchronously, which means we must
12059 exclude any possibility that Fget_text_property signals an
12060 error. */
12061 charpos = min (SCHARS (FVAR (f, current_tool_bar_string)), glyph->charpos);
12062 charpos = max (0, charpos);
12063
12064 /* Get the text property `menu-item' at pos. The value of that
12065 property is the start index of this item's properties in
12066 F->tool_bar_items. */
12067 prop = Fget_text_property (make_number (charpos),
12068 Qmenu_item, FVAR (f, current_tool_bar_string));
12069 if (INTEGERP (prop))
12070 {
12071 *prop_idx = XINT (prop);
12072 success_p = 1;
12073 }
12074 else
12075 success_p = 0;
12076
12077 return success_p;
12078 }
12079
12080 \f
12081 /* Get information about the tool-bar item at position X/Y on frame F.
12082 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12083 the current matrix of the tool-bar window of F, or NULL if not
12084 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12085 item in F->tool_bar_items. Value is
12086
12087 -1 if X/Y is not on a tool-bar item
12088 0 if X/Y is on the same item that was highlighted before.
12089 1 otherwise. */
12090
12091 static int
12092 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12093 int *hpos, int *vpos, int *prop_idx)
12094 {
12095 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12096 struct window *w = XWINDOW (FVAR (f, tool_bar_window));
12097 int area;
12098
12099 /* Find the glyph under X/Y. */
12100 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12101 if (*glyph == NULL)
12102 return -1;
12103
12104 /* Get the start of this tool-bar item's properties in
12105 f->tool_bar_items. */
12106 if (!tool_bar_item_info (f, *glyph, prop_idx))
12107 return -1;
12108
12109 /* Is mouse on the highlighted item? */
12110 if (EQ (FVAR (f, tool_bar_window), hlinfo->mouse_face_window)
12111 && *vpos >= hlinfo->mouse_face_beg_row
12112 && *vpos <= hlinfo->mouse_face_end_row
12113 && (*vpos > hlinfo->mouse_face_beg_row
12114 || *hpos >= hlinfo->mouse_face_beg_col)
12115 && (*vpos < hlinfo->mouse_face_end_row
12116 || *hpos < hlinfo->mouse_face_end_col
12117 || hlinfo->mouse_face_past_end))
12118 return 0;
12119
12120 return 1;
12121 }
12122
12123
12124 /* EXPORT:
12125 Handle mouse button event on the tool-bar of frame F, at
12126 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12127 0 for button release. MODIFIERS is event modifiers for button
12128 release. */
12129
12130 void
12131 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12132 int modifiers)
12133 {
12134 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12135 struct window *w = XWINDOW (FVAR (f, tool_bar_window));
12136 int hpos, vpos, prop_idx;
12137 struct glyph *glyph;
12138 Lisp_Object enabled_p;
12139
12140 /* If not on the highlighted tool-bar item, return. */
12141 frame_to_window_pixel_xy (w, &x, &y);
12142 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12143 return;
12144
12145 /* If item is disabled, do nothing. */
12146 enabled_p = AREF (FVAR (f, tool_bar_items), prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12147 if (NILP (enabled_p))
12148 return;
12149
12150 if (down_p)
12151 {
12152 /* Show item in pressed state. */
12153 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12154 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12155 last_tool_bar_item = prop_idx;
12156 }
12157 else
12158 {
12159 Lisp_Object key, frame;
12160 struct input_event event;
12161 EVENT_INIT (event);
12162
12163 /* Show item in released state. */
12164 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12165 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12166
12167 key = AREF (FVAR (f, tool_bar_items), prop_idx + TOOL_BAR_ITEM_KEY);
12168
12169 XSETFRAME (frame, f);
12170 event.kind = TOOL_BAR_EVENT;
12171 event.frame_or_window = frame;
12172 event.arg = frame;
12173 kbd_buffer_store_event (&event);
12174
12175 event.kind = TOOL_BAR_EVENT;
12176 event.frame_or_window = frame;
12177 event.arg = key;
12178 event.modifiers = modifiers;
12179 kbd_buffer_store_event (&event);
12180 last_tool_bar_item = -1;
12181 }
12182 }
12183
12184
12185 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12186 tool-bar window-relative coordinates X/Y. Called from
12187 note_mouse_highlight. */
12188
12189 static void
12190 note_tool_bar_highlight (struct frame *f, int x, int y)
12191 {
12192 Lisp_Object window = FVAR (f, tool_bar_window);
12193 struct window *w = XWINDOW (window);
12194 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12195 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12196 int hpos, vpos;
12197 struct glyph *glyph;
12198 struct glyph_row *row;
12199 int i;
12200 Lisp_Object enabled_p;
12201 int prop_idx;
12202 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12203 int mouse_down_p, rc;
12204
12205 /* Function note_mouse_highlight is called with negative X/Y
12206 values when mouse moves outside of the frame. */
12207 if (x <= 0 || y <= 0)
12208 {
12209 clear_mouse_face (hlinfo);
12210 return;
12211 }
12212
12213 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12214 if (rc < 0)
12215 {
12216 /* Not on tool-bar item. */
12217 clear_mouse_face (hlinfo);
12218 return;
12219 }
12220 else if (rc == 0)
12221 /* On same tool-bar item as before. */
12222 goto set_help_echo;
12223
12224 clear_mouse_face (hlinfo);
12225
12226 /* Mouse is down, but on different tool-bar item? */
12227 mouse_down_p = (dpyinfo->grabbed
12228 && f == last_mouse_frame
12229 && FRAME_LIVE_P (f));
12230 if (mouse_down_p
12231 && last_tool_bar_item != prop_idx)
12232 return;
12233
12234 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12235 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12236
12237 /* If tool-bar item is not enabled, don't highlight it. */
12238 enabled_p = AREF (FVAR (f, tool_bar_items), prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12239 if (!NILP (enabled_p))
12240 {
12241 /* Compute the x-position of the glyph. In front and past the
12242 image is a space. We include this in the highlighted area. */
12243 row = MATRIX_ROW (w->current_matrix, vpos);
12244 for (i = x = 0; i < hpos; ++i)
12245 x += row->glyphs[TEXT_AREA][i].pixel_width;
12246
12247 /* Record this as the current active region. */
12248 hlinfo->mouse_face_beg_col = hpos;
12249 hlinfo->mouse_face_beg_row = vpos;
12250 hlinfo->mouse_face_beg_x = x;
12251 hlinfo->mouse_face_beg_y = row->y;
12252 hlinfo->mouse_face_past_end = 0;
12253
12254 hlinfo->mouse_face_end_col = hpos + 1;
12255 hlinfo->mouse_face_end_row = vpos;
12256 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12257 hlinfo->mouse_face_end_y = row->y;
12258 hlinfo->mouse_face_window = window;
12259 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12260
12261 /* Display it as active. */
12262 show_mouse_face (hlinfo, draw);
12263 hlinfo->mouse_face_image_state = draw;
12264 }
12265
12266 set_help_echo:
12267
12268 /* Set help_echo_string to a help string to display for this tool-bar item.
12269 XTread_socket does the rest. */
12270 help_echo_object = help_echo_window = Qnil;
12271 help_echo_pos = -1;
12272 help_echo_string = AREF (FVAR (f, tool_bar_items), prop_idx + TOOL_BAR_ITEM_HELP);
12273 if (NILP (help_echo_string))
12274 help_echo_string = AREF (FVAR (f, tool_bar_items), prop_idx + TOOL_BAR_ITEM_CAPTION);
12275 }
12276
12277 #endif /* HAVE_WINDOW_SYSTEM */
12278
12279
12280 \f
12281 /************************************************************************
12282 Horizontal scrolling
12283 ************************************************************************/
12284
12285 static int hscroll_window_tree (Lisp_Object);
12286 static int hscroll_windows (Lisp_Object);
12287
12288 /* For all leaf windows in the window tree rooted at WINDOW, set their
12289 hscroll value so that PT is (i) visible in the window, and (ii) so
12290 that it is not within a certain margin at the window's left and
12291 right border. Value is non-zero if any window's hscroll has been
12292 changed. */
12293
12294 static int
12295 hscroll_window_tree (Lisp_Object window)
12296 {
12297 int hscrolled_p = 0;
12298 int hscroll_relative_p = FLOATP (Vhscroll_step);
12299 int hscroll_step_abs = 0;
12300 double hscroll_step_rel = 0;
12301
12302 if (hscroll_relative_p)
12303 {
12304 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12305 if (hscroll_step_rel < 0)
12306 {
12307 hscroll_relative_p = 0;
12308 hscroll_step_abs = 0;
12309 }
12310 }
12311 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12312 {
12313 hscroll_step_abs = XINT (Vhscroll_step);
12314 if (hscroll_step_abs < 0)
12315 hscroll_step_abs = 0;
12316 }
12317 else
12318 hscroll_step_abs = 0;
12319
12320 while (WINDOWP (window))
12321 {
12322 struct window *w = XWINDOW (window);
12323
12324 if (WINDOWP (w->hchild))
12325 hscrolled_p |= hscroll_window_tree (w->hchild);
12326 else if (WINDOWP (w->vchild))
12327 hscrolled_p |= hscroll_window_tree (w->vchild);
12328 else if (w->cursor.vpos >= 0)
12329 {
12330 int h_margin;
12331 int text_area_width;
12332 struct glyph_row *current_cursor_row
12333 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12334 struct glyph_row *desired_cursor_row
12335 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12336 struct glyph_row *cursor_row
12337 = (desired_cursor_row->enabled_p
12338 ? desired_cursor_row
12339 : current_cursor_row);
12340 int row_r2l_p = cursor_row->reversed_p;
12341
12342 text_area_width = window_box_width (w, TEXT_AREA);
12343
12344 /* Scroll when cursor is inside this scroll margin. */
12345 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12346
12347 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12348 /* For left-to-right rows, hscroll when cursor is either
12349 (i) inside the right hscroll margin, or (ii) if it is
12350 inside the left margin and the window is already
12351 hscrolled. */
12352 && ((!row_r2l_p
12353 && ((w->hscroll
12354 && w->cursor.x <= h_margin)
12355 || (cursor_row->enabled_p
12356 && cursor_row->truncated_on_right_p
12357 && (w->cursor.x >= text_area_width - h_margin))))
12358 /* For right-to-left rows, the logic is similar,
12359 except that rules for scrolling to left and right
12360 are reversed. E.g., if cursor.x <= h_margin, we
12361 need to hscroll "to the right" unconditionally,
12362 and that will scroll the screen to the left so as
12363 to reveal the next portion of the row. */
12364 || (row_r2l_p
12365 && ((cursor_row->enabled_p
12366 /* FIXME: It is confusing to set the
12367 truncated_on_right_p flag when R2L rows
12368 are actually truncated on the left. */
12369 && cursor_row->truncated_on_right_p
12370 && w->cursor.x <= h_margin)
12371 || (w->hscroll
12372 && (w->cursor.x >= text_area_width - h_margin))))))
12373 {
12374 struct it it;
12375 ptrdiff_t hscroll;
12376 struct buffer *saved_current_buffer;
12377 ptrdiff_t pt;
12378 int wanted_x;
12379
12380 /* Find point in a display of infinite width. */
12381 saved_current_buffer = current_buffer;
12382 current_buffer = XBUFFER (w->buffer);
12383
12384 if (w == XWINDOW (selected_window))
12385 pt = PT;
12386 else
12387 {
12388 pt = marker_position (w->pointm);
12389 pt = max (BEGV, pt);
12390 pt = min (ZV, pt);
12391 }
12392
12393 /* Move iterator to pt starting at cursor_row->start in
12394 a line with infinite width. */
12395 init_to_row_start (&it, w, cursor_row);
12396 it.last_visible_x = INFINITY;
12397 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12398 current_buffer = saved_current_buffer;
12399
12400 /* Position cursor in window. */
12401 if (!hscroll_relative_p && hscroll_step_abs == 0)
12402 hscroll = max (0, (it.current_x
12403 - (ITERATOR_AT_END_OF_LINE_P (&it)
12404 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12405 : (text_area_width / 2))))
12406 / FRAME_COLUMN_WIDTH (it.f);
12407 else if ((!row_r2l_p
12408 && w->cursor.x >= text_area_width - h_margin)
12409 || (row_r2l_p && w->cursor.x <= h_margin))
12410 {
12411 if (hscroll_relative_p)
12412 wanted_x = text_area_width * (1 - hscroll_step_rel)
12413 - h_margin;
12414 else
12415 wanted_x = text_area_width
12416 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12417 - h_margin;
12418 hscroll
12419 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12420 }
12421 else
12422 {
12423 if (hscroll_relative_p)
12424 wanted_x = text_area_width * hscroll_step_rel
12425 + h_margin;
12426 else
12427 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12428 + h_margin;
12429 hscroll
12430 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12431 }
12432 hscroll = max (hscroll, w->min_hscroll);
12433
12434 /* Don't prevent redisplay optimizations if hscroll
12435 hasn't changed, as it will unnecessarily slow down
12436 redisplay. */
12437 if (w->hscroll != hscroll)
12438 {
12439 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12440 w->hscroll = hscroll;
12441 hscrolled_p = 1;
12442 }
12443 }
12444 }
12445
12446 window = w->next;
12447 }
12448
12449 /* Value is non-zero if hscroll of any leaf window has been changed. */
12450 return hscrolled_p;
12451 }
12452
12453
12454 /* Set hscroll so that cursor is visible and not inside horizontal
12455 scroll margins for all windows in the tree rooted at WINDOW. See
12456 also hscroll_window_tree above. Value is non-zero if any window's
12457 hscroll has been changed. If it has, desired matrices on the frame
12458 of WINDOW are cleared. */
12459
12460 static int
12461 hscroll_windows (Lisp_Object window)
12462 {
12463 int hscrolled_p = hscroll_window_tree (window);
12464 if (hscrolled_p)
12465 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12466 return hscrolled_p;
12467 }
12468
12469
12470 \f
12471 /************************************************************************
12472 Redisplay
12473 ************************************************************************/
12474
12475 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12476 to a non-zero value. This is sometimes handy to have in a debugger
12477 session. */
12478
12479 #ifdef GLYPH_DEBUG
12480
12481 /* First and last unchanged row for try_window_id. */
12482
12483 static int debug_first_unchanged_at_end_vpos;
12484 static int debug_last_unchanged_at_beg_vpos;
12485
12486 /* Delta vpos and y. */
12487
12488 static int debug_dvpos, debug_dy;
12489
12490 /* Delta in characters and bytes for try_window_id. */
12491
12492 static ptrdiff_t debug_delta, debug_delta_bytes;
12493
12494 /* Values of window_end_pos and window_end_vpos at the end of
12495 try_window_id. */
12496
12497 static ptrdiff_t debug_end_vpos;
12498
12499 /* Append a string to W->desired_matrix->method. FMT is a printf
12500 format string. If trace_redisplay_p is non-zero also printf the
12501 resulting string to stderr. */
12502
12503 static void debug_method_add (struct window *, char const *, ...)
12504 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12505
12506 static void
12507 debug_method_add (struct window *w, char const *fmt, ...)
12508 {
12509 char *method = w->desired_matrix->method;
12510 int len = strlen (method);
12511 int size = sizeof w->desired_matrix->method;
12512 int remaining = size - len - 1;
12513 va_list ap;
12514
12515 if (len && remaining)
12516 {
12517 method[len] = '|';
12518 --remaining, ++len;
12519 }
12520
12521 va_start (ap, fmt);
12522 vsnprintf (method + len, remaining + 1, fmt, ap);
12523 va_end (ap);
12524
12525 if (trace_redisplay_p)
12526 fprintf (stderr, "%p (%s): %s\n",
12527 w,
12528 ((BUFFERP (w->buffer)
12529 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12530 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12531 : "no buffer"),
12532 method + len);
12533 }
12534
12535 #endif /* GLYPH_DEBUG */
12536
12537
12538 /* Value is non-zero if all changes in window W, which displays
12539 current_buffer, are in the text between START and END. START is a
12540 buffer position, END is given as a distance from Z. Used in
12541 redisplay_internal for display optimization. */
12542
12543 static inline int
12544 text_outside_line_unchanged_p (struct window *w,
12545 ptrdiff_t start, ptrdiff_t end)
12546 {
12547 int unchanged_p = 1;
12548
12549 /* If text or overlays have changed, see where. */
12550 if (w->last_modified < MODIFF
12551 || w->last_overlay_modified < OVERLAY_MODIFF)
12552 {
12553 /* Gap in the line? */
12554 if (GPT < start || Z - GPT < end)
12555 unchanged_p = 0;
12556
12557 /* Changes start in front of the line, or end after it? */
12558 if (unchanged_p
12559 && (BEG_UNCHANGED < start - 1
12560 || END_UNCHANGED < end))
12561 unchanged_p = 0;
12562
12563 /* If selective display, can't optimize if changes start at the
12564 beginning of the line. */
12565 if (unchanged_p
12566 && INTEGERP (BVAR (current_buffer, selective_display))
12567 && XINT (BVAR (current_buffer, selective_display)) > 0
12568 && (BEG_UNCHANGED < start || GPT <= start))
12569 unchanged_p = 0;
12570
12571 /* If there are overlays at the start or end of the line, these
12572 may have overlay strings with newlines in them. A change at
12573 START, for instance, may actually concern the display of such
12574 overlay strings as well, and they are displayed on different
12575 lines. So, quickly rule out this case. (For the future, it
12576 might be desirable to implement something more telling than
12577 just BEG/END_UNCHANGED.) */
12578 if (unchanged_p)
12579 {
12580 if (BEG + BEG_UNCHANGED == start
12581 && overlay_touches_p (start))
12582 unchanged_p = 0;
12583 if (END_UNCHANGED == end
12584 && overlay_touches_p (Z - end))
12585 unchanged_p = 0;
12586 }
12587
12588 /* Under bidi reordering, adding or deleting a character in the
12589 beginning of a paragraph, before the first strong directional
12590 character, can change the base direction of the paragraph (unless
12591 the buffer specifies a fixed paragraph direction), which will
12592 require to redisplay the whole paragraph. It might be worthwhile
12593 to find the paragraph limits and widen the range of redisplayed
12594 lines to that, but for now just give up this optimization. */
12595 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12596 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12597 unchanged_p = 0;
12598 }
12599
12600 return unchanged_p;
12601 }
12602
12603
12604 /* Do a frame update, taking possible shortcuts into account. This is
12605 the main external entry point for redisplay.
12606
12607 If the last redisplay displayed an echo area message and that message
12608 is no longer requested, we clear the echo area or bring back the
12609 mini-buffer if that is in use. */
12610
12611 void
12612 redisplay (void)
12613 {
12614 redisplay_internal ();
12615 }
12616
12617
12618 static Lisp_Object
12619 overlay_arrow_string_or_property (Lisp_Object var)
12620 {
12621 Lisp_Object val;
12622
12623 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12624 return val;
12625
12626 return Voverlay_arrow_string;
12627 }
12628
12629 /* Return 1 if there are any overlay-arrows in current_buffer. */
12630 static int
12631 overlay_arrow_in_current_buffer_p (void)
12632 {
12633 Lisp_Object vlist;
12634
12635 for (vlist = Voverlay_arrow_variable_list;
12636 CONSP (vlist);
12637 vlist = XCDR (vlist))
12638 {
12639 Lisp_Object var = XCAR (vlist);
12640 Lisp_Object val;
12641
12642 if (!SYMBOLP (var))
12643 continue;
12644 val = find_symbol_value (var);
12645 if (MARKERP (val)
12646 && current_buffer == XMARKER (val)->buffer)
12647 return 1;
12648 }
12649 return 0;
12650 }
12651
12652
12653 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12654 has changed. */
12655
12656 static int
12657 overlay_arrows_changed_p (void)
12658 {
12659 Lisp_Object vlist;
12660
12661 for (vlist = Voverlay_arrow_variable_list;
12662 CONSP (vlist);
12663 vlist = XCDR (vlist))
12664 {
12665 Lisp_Object var = XCAR (vlist);
12666 Lisp_Object val, pstr;
12667
12668 if (!SYMBOLP (var))
12669 continue;
12670 val = find_symbol_value (var);
12671 if (!MARKERP (val))
12672 continue;
12673 if (! EQ (COERCE_MARKER (val),
12674 Fget (var, Qlast_arrow_position))
12675 || ! (pstr = overlay_arrow_string_or_property (var),
12676 EQ (pstr, Fget (var, Qlast_arrow_string))))
12677 return 1;
12678 }
12679 return 0;
12680 }
12681
12682 /* Mark overlay arrows to be updated on next redisplay. */
12683
12684 static void
12685 update_overlay_arrows (int up_to_date)
12686 {
12687 Lisp_Object vlist;
12688
12689 for (vlist = Voverlay_arrow_variable_list;
12690 CONSP (vlist);
12691 vlist = XCDR (vlist))
12692 {
12693 Lisp_Object var = XCAR (vlist);
12694
12695 if (!SYMBOLP (var))
12696 continue;
12697
12698 if (up_to_date > 0)
12699 {
12700 Lisp_Object val = find_symbol_value (var);
12701 Fput (var, Qlast_arrow_position,
12702 COERCE_MARKER (val));
12703 Fput (var, Qlast_arrow_string,
12704 overlay_arrow_string_or_property (var));
12705 }
12706 else if (up_to_date < 0
12707 || !NILP (Fget (var, Qlast_arrow_position)))
12708 {
12709 Fput (var, Qlast_arrow_position, Qt);
12710 Fput (var, Qlast_arrow_string, Qt);
12711 }
12712 }
12713 }
12714
12715
12716 /* Return overlay arrow string to display at row.
12717 Return integer (bitmap number) for arrow bitmap in left fringe.
12718 Return nil if no overlay arrow. */
12719
12720 static Lisp_Object
12721 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12722 {
12723 Lisp_Object vlist;
12724
12725 for (vlist = Voverlay_arrow_variable_list;
12726 CONSP (vlist);
12727 vlist = XCDR (vlist))
12728 {
12729 Lisp_Object var = XCAR (vlist);
12730 Lisp_Object val;
12731
12732 if (!SYMBOLP (var))
12733 continue;
12734
12735 val = find_symbol_value (var);
12736
12737 if (MARKERP (val)
12738 && current_buffer == XMARKER (val)->buffer
12739 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12740 {
12741 if (FRAME_WINDOW_P (it->f)
12742 /* FIXME: if ROW->reversed_p is set, this should test
12743 the right fringe, not the left one. */
12744 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12745 {
12746 #ifdef HAVE_WINDOW_SYSTEM
12747 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12748 {
12749 int fringe_bitmap;
12750 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12751 return make_number (fringe_bitmap);
12752 }
12753 #endif
12754 return make_number (-1); /* Use default arrow bitmap */
12755 }
12756 return overlay_arrow_string_or_property (var);
12757 }
12758 }
12759
12760 return Qnil;
12761 }
12762
12763 /* Return 1 if point moved out of or into a composition. Otherwise
12764 return 0. PREV_BUF and PREV_PT are the last point buffer and
12765 position. BUF and PT are the current point buffer and position. */
12766
12767 static int
12768 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12769 struct buffer *buf, ptrdiff_t pt)
12770 {
12771 ptrdiff_t start, end;
12772 Lisp_Object prop;
12773 Lisp_Object buffer;
12774
12775 XSETBUFFER (buffer, buf);
12776 /* Check a composition at the last point if point moved within the
12777 same buffer. */
12778 if (prev_buf == buf)
12779 {
12780 if (prev_pt == pt)
12781 /* Point didn't move. */
12782 return 0;
12783
12784 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12785 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12786 && COMPOSITION_VALID_P (start, end, prop)
12787 && start < prev_pt && end > prev_pt)
12788 /* The last point was within the composition. Return 1 iff
12789 point moved out of the composition. */
12790 return (pt <= start || pt >= end);
12791 }
12792
12793 /* Check a composition at the current point. */
12794 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12795 && find_composition (pt, -1, &start, &end, &prop, buffer)
12796 && COMPOSITION_VALID_P (start, end, prop)
12797 && start < pt && end > pt);
12798 }
12799
12800
12801 /* Reconsider the setting of B->clip_changed which is displayed
12802 in window W. */
12803
12804 static inline void
12805 reconsider_clip_changes (struct window *w, struct buffer *b)
12806 {
12807 if (b->clip_changed
12808 && !NILP (w->window_end_valid)
12809 && w->current_matrix->buffer == b
12810 && w->current_matrix->zv == BUF_ZV (b)
12811 && w->current_matrix->begv == BUF_BEGV (b))
12812 b->clip_changed = 0;
12813
12814 /* If display wasn't paused, and W is not a tool bar window, see if
12815 point has been moved into or out of a composition. In that case,
12816 we set b->clip_changed to 1 to force updating the screen. If
12817 b->clip_changed has already been set to 1, we can skip this
12818 check. */
12819 if (!b->clip_changed
12820 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12821 {
12822 ptrdiff_t pt;
12823
12824 if (w == XWINDOW (selected_window))
12825 pt = PT;
12826 else
12827 pt = marker_position (w->pointm);
12828
12829 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12830 || pt != w->last_point)
12831 && check_point_in_composition (w->current_matrix->buffer,
12832 w->last_point,
12833 XBUFFER (w->buffer), pt))
12834 b->clip_changed = 1;
12835 }
12836 }
12837 \f
12838
12839 /* Select FRAME to forward the values of frame-local variables into C
12840 variables so that the redisplay routines can access those values
12841 directly. */
12842
12843 static void
12844 select_frame_for_redisplay (Lisp_Object frame)
12845 {
12846 Lisp_Object tail, tem;
12847 Lisp_Object old = selected_frame;
12848 struct Lisp_Symbol *sym;
12849
12850 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12851
12852 selected_frame = frame;
12853
12854 do {
12855 for (tail = FVAR (XFRAME (frame), param_alist);
12856 CONSP (tail); tail = XCDR (tail))
12857 if (CONSP (XCAR (tail))
12858 && (tem = XCAR (XCAR (tail)),
12859 SYMBOLP (tem))
12860 && (sym = indirect_variable (XSYMBOL (tem)),
12861 sym->redirect == SYMBOL_LOCALIZED)
12862 && sym->val.blv->frame_local)
12863 /* Use find_symbol_value rather than Fsymbol_value
12864 to avoid an error if it is void. */
12865 find_symbol_value (tem);
12866 } while (!EQ (frame, old) && (frame = old, 1));
12867 }
12868
12869
12870 #define STOP_POLLING \
12871 do { if (! polling_stopped_here) stop_polling (); \
12872 polling_stopped_here = 1; } while (0)
12873
12874 #define RESUME_POLLING \
12875 do { if (polling_stopped_here) start_polling (); \
12876 polling_stopped_here = 0; } while (0)
12877
12878
12879 /* Perhaps in the future avoid recentering windows if it
12880 is not necessary; currently that causes some problems. */
12881
12882 static void
12883 redisplay_internal (void)
12884 {
12885 struct window *w = XWINDOW (selected_window);
12886 struct window *sw;
12887 struct frame *fr;
12888 int pending;
12889 int must_finish = 0;
12890 struct text_pos tlbufpos, tlendpos;
12891 int number_of_visible_frames;
12892 ptrdiff_t count, count1;
12893 struct frame *sf;
12894 int polling_stopped_here = 0;
12895 Lisp_Object old_frame = selected_frame;
12896
12897 /* Non-zero means redisplay has to consider all windows on all
12898 frames. Zero means, only selected_window is considered. */
12899 int consider_all_windows_p;
12900
12901 /* Non-zero means redisplay has to redisplay the miniwindow */
12902 int update_miniwindow_p = 0;
12903
12904 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12905
12906 /* No redisplay if running in batch mode or frame is not yet fully
12907 initialized, or redisplay is explicitly turned off by setting
12908 Vinhibit_redisplay. */
12909 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12910 || !NILP (Vinhibit_redisplay))
12911 return;
12912
12913 /* Don't examine these until after testing Vinhibit_redisplay.
12914 When Emacs is shutting down, perhaps because its connection to
12915 X has dropped, we should not look at them at all. */
12916 fr = XFRAME (w->frame);
12917 sf = SELECTED_FRAME ();
12918
12919 if (!fr->glyphs_initialized_p)
12920 return;
12921
12922 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12923 if (popup_activated ())
12924 return;
12925 #endif
12926
12927 /* I don't think this happens but let's be paranoid. */
12928 if (redisplaying_p)
12929 return;
12930
12931 /* Record a function that resets redisplaying_p to its old value
12932 when we leave this function. */
12933 count = SPECPDL_INDEX ();
12934 record_unwind_protect (unwind_redisplay,
12935 Fcons (make_number (redisplaying_p), selected_frame));
12936 ++redisplaying_p;
12937 specbind (Qinhibit_free_realized_faces, Qnil);
12938
12939 {
12940 Lisp_Object tail, frame;
12941
12942 FOR_EACH_FRAME (tail, frame)
12943 {
12944 struct frame *f = XFRAME (frame);
12945 f->already_hscrolled_p = 0;
12946 }
12947 }
12948
12949 retry:
12950 /* Remember the currently selected window. */
12951 sw = w;
12952
12953 if (!EQ (old_frame, selected_frame)
12954 && FRAME_LIVE_P (XFRAME (old_frame)))
12955 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12956 selected_frame and selected_window to be temporarily out-of-sync so
12957 when we come back here via `goto retry', we need to resync because we
12958 may need to run Elisp code (via prepare_menu_bars). */
12959 select_frame_for_redisplay (old_frame);
12960
12961 pending = 0;
12962 reconsider_clip_changes (w, current_buffer);
12963 last_escape_glyph_frame = NULL;
12964 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12965 last_glyphless_glyph_frame = NULL;
12966 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12967
12968 /* If new fonts have been loaded that make a glyph matrix adjustment
12969 necessary, do it. */
12970 if (fonts_changed_p)
12971 {
12972 adjust_glyphs (NULL);
12973 ++windows_or_buffers_changed;
12974 fonts_changed_p = 0;
12975 }
12976
12977 /* If face_change_count is non-zero, init_iterator will free all
12978 realized faces, which includes the faces referenced from current
12979 matrices. So, we can't reuse current matrices in this case. */
12980 if (face_change_count)
12981 ++windows_or_buffers_changed;
12982
12983 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12984 && FRAME_TTY (sf)->previous_frame != sf)
12985 {
12986 /* Since frames on a single ASCII terminal share the same
12987 display area, displaying a different frame means redisplay
12988 the whole thing. */
12989 windows_or_buffers_changed++;
12990 SET_FRAME_GARBAGED (sf);
12991 #ifndef DOS_NT
12992 set_tty_color_mode (FRAME_TTY (sf), sf);
12993 #endif
12994 FRAME_TTY (sf)->previous_frame = sf;
12995 }
12996
12997 /* Set the visible flags for all frames. Do this before checking
12998 for resized or garbaged frames; they want to know if their frames
12999 are visible. See the comment in frame.h for
13000 FRAME_SAMPLE_VISIBILITY. */
13001 {
13002 Lisp_Object tail, frame;
13003
13004 number_of_visible_frames = 0;
13005
13006 FOR_EACH_FRAME (tail, frame)
13007 {
13008 struct frame *f = XFRAME (frame);
13009
13010 FRAME_SAMPLE_VISIBILITY (f);
13011 if (FRAME_VISIBLE_P (f))
13012 ++number_of_visible_frames;
13013 clear_desired_matrices (f);
13014 }
13015 }
13016
13017 /* Notice any pending interrupt request to change frame size. */
13018 do_pending_window_change (1);
13019
13020 /* do_pending_window_change could change the selected_window due to
13021 frame resizing which makes the selected window too small. */
13022 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13023 {
13024 sw = w;
13025 reconsider_clip_changes (w, current_buffer);
13026 }
13027
13028 /* Clear frames marked as garbaged. */
13029 if (frame_garbaged)
13030 clear_garbaged_frames ();
13031
13032 /* Build menubar and tool-bar items. */
13033 if (NILP (Vmemory_full))
13034 prepare_menu_bars ();
13035
13036 if (windows_or_buffers_changed)
13037 update_mode_lines++;
13038
13039 /* Detect case that we need to write or remove a star in the mode line. */
13040 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13041 {
13042 w->update_mode_line = 1;
13043 if (buffer_shared > 1)
13044 update_mode_lines++;
13045 }
13046
13047 /* Avoid invocation of point motion hooks by `current_column' below. */
13048 count1 = SPECPDL_INDEX ();
13049 specbind (Qinhibit_point_motion_hooks, Qt);
13050
13051 /* If %c is in the mode line, update it if needed. */
13052 if (!NILP (w->column_number_displayed)
13053 /* This alternative quickly identifies a common case
13054 where no change is needed. */
13055 && !(PT == w->last_point
13056 && w->last_modified >= MODIFF
13057 && w->last_overlay_modified >= OVERLAY_MODIFF)
13058 && (XFASTINT (w->column_number_displayed) != current_column ()))
13059 w->update_mode_line = 1;
13060
13061 unbind_to (count1, Qnil);
13062
13063 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13064
13065 /* The variable buffer_shared is set in redisplay_window and
13066 indicates that we redisplay a buffer in different windows. See
13067 there. */
13068 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13069 || cursor_type_changed);
13070
13071 /* If specs for an arrow have changed, do thorough redisplay
13072 to ensure we remove any arrow that should no longer exist. */
13073 if (overlay_arrows_changed_p ())
13074 consider_all_windows_p = windows_or_buffers_changed = 1;
13075
13076 /* Normally the message* functions will have already displayed and
13077 updated the echo area, but the frame may have been trashed, or
13078 the update may have been preempted, so display the echo area
13079 again here. Checking message_cleared_p captures the case that
13080 the echo area should be cleared. */
13081 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13082 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13083 || (message_cleared_p
13084 && minibuf_level == 0
13085 /* If the mini-window is currently selected, this means the
13086 echo-area doesn't show through. */
13087 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13088 {
13089 int window_height_changed_p = echo_area_display (0);
13090
13091 if (message_cleared_p)
13092 update_miniwindow_p = 1;
13093
13094 must_finish = 1;
13095
13096 /* If we don't display the current message, don't clear the
13097 message_cleared_p flag, because, if we did, we wouldn't clear
13098 the echo area in the next redisplay which doesn't preserve
13099 the echo area. */
13100 if (!display_last_displayed_message_p)
13101 message_cleared_p = 0;
13102
13103 if (fonts_changed_p)
13104 goto retry;
13105 else if (window_height_changed_p)
13106 {
13107 consider_all_windows_p = 1;
13108 ++update_mode_lines;
13109 ++windows_or_buffers_changed;
13110
13111 /* If window configuration was changed, frames may have been
13112 marked garbaged. Clear them or we will experience
13113 surprises wrt scrolling. */
13114 if (frame_garbaged)
13115 clear_garbaged_frames ();
13116 }
13117 }
13118 else if (EQ (selected_window, minibuf_window)
13119 && (current_buffer->clip_changed
13120 || w->last_modified < MODIFF
13121 || w->last_overlay_modified < OVERLAY_MODIFF)
13122 && resize_mini_window (w, 0))
13123 {
13124 /* Resized active mini-window to fit the size of what it is
13125 showing if its contents might have changed. */
13126 must_finish = 1;
13127 /* FIXME: this causes all frames to be updated, which seems unnecessary
13128 since only the current frame needs to be considered. This function needs
13129 to be rewritten with two variables, consider_all_windows and
13130 consider_all_frames. */
13131 consider_all_windows_p = 1;
13132 ++windows_or_buffers_changed;
13133 ++update_mode_lines;
13134
13135 /* If window configuration was changed, frames may have been
13136 marked garbaged. Clear them or we will experience
13137 surprises wrt scrolling. */
13138 if (frame_garbaged)
13139 clear_garbaged_frames ();
13140 }
13141
13142
13143 /* If showing the region, and mark has changed, we must redisplay
13144 the whole window. The assignment to this_line_start_pos prevents
13145 the optimization directly below this if-statement. */
13146 if (((!NILP (Vtransient_mark_mode)
13147 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13148 != !NILP (w->region_showing))
13149 || (!NILP (w->region_showing)
13150 && !EQ (w->region_showing,
13151 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13152 CHARPOS (this_line_start_pos) = 0;
13153
13154 /* Optimize the case that only the line containing the cursor in the
13155 selected window has changed. Variables starting with this_ are
13156 set in display_line and record information about the line
13157 containing the cursor. */
13158 tlbufpos = this_line_start_pos;
13159 tlendpos = this_line_end_pos;
13160 if (!consider_all_windows_p
13161 && CHARPOS (tlbufpos) > 0
13162 && !w->update_mode_line
13163 && !current_buffer->clip_changed
13164 && !current_buffer->prevent_redisplay_optimizations_p
13165 && FRAME_VISIBLE_P (XFRAME (w->frame))
13166 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13167 /* Make sure recorded data applies to current buffer, etc. */
13168 && this_line_buffer == current_buffer
13169 && current_buffer == XBUFFER (w->buffer)
13170 && !w->force_start
13171 && !w->optional_new_start
13172 /* Point must be on the line that we have info recorded about. */
13173 && PT >= CHARPOS (tlbufpos)
13174 && PT <= Z - CHARPOS (tlendpos)
13175 /* All text outside that line, including its final newline,
13176 must be unchanged. */
13177 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13178 CHARPOS (tlendpos)))
13179 {
13180 if (CHARPOS (tlbufpos) > BEGV
13181 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13182 && (CHARPOS (tlbufpos) == ZV
13183 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13184 /* Former continuation line has disappeared by becoming empty. */
13185 goto cancel;
13186 else if (w->last_modified < MODIFF
13187 || w->last_overlay_modified < OVERLAY_MODIFF
13188 || MINI_WINDOW_P (w))
13189 {
13190 /* We have to handle the case of continuation around a
13191 wide-column character (see the comment in indent.c around
13192 line 1340).
13193
13194 For instance, in the following case:
13195
13196 -------- Insert --------
13197 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13198 J_I_ ==> J_I_ `^^' are cursors.
13199 ^^ ^^
13200 -------- --------
13201
13202 As we have to redraw the line above, we cannot use this
13203 optimization. */
13204
13205 struct it it;
13206 int line_height_before = this_line_pixel_height;
13207
13208 /* Note that start_display will handle the case that the
13209 line starting at tlbufpos is a continuation line. */
13210 start_display (&it, w, tlbufpos);
13211
13212 /* Implementation note: It this still necessary? */
13213 if (it.current_x != this_line_start_x)
13214 goto cancel;
13215
13216 TRACE ((stderr, "trying display optimization 1\n"));
13217 w->cursor.vpos = -1;
13218 overlay_arrow_seen = 0;
13219 it.vpos = this_line_vpos;
13220 it.current_y = this_line_y;
13221 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13222 display_line (&it);
13223
13224 /* If line contains point, is not continued,
13225 and ends at same distance from eob as before, we win. */
13226 if (w->cursor.vpos >= 0
13227 /* Line is not continued, otherwise this_line_start_pos
13228 would have been set to 0 in display_line. */
13229 && CHARPOS (this_line_start_pos)
13230 /* Line ends as before. */
13231 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13232 /* Line has same height as before. Otherwise other lines
13233 would have to be shifted up or down. */
13234 && this_line_pixel_height == line_height_before)
13235 {
13236 /* If this is not the window's last line, we must adjust
13237 the charstarts of the lines below. */
13238 if (it.current_y < it.last_visible_y)
13239 {
13240 struct glyph_row *row
13241 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13242 ptrdiff_t delta, delta_bytes;
13243
13244 /* We used to distinguish between two cases here,
13245 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13246 when the line ends in a newline or the end of the
13247 buffer's accessible portion. But both cases did
13248 the same, so they were collapsed. */
13249 delta = (Z
13250 - CHARPOS (tlendpos)
13251 - MATRIX_ROW_START_CHARPOS (row));
13252 delta_bytes = (Z_BYTE
13253 - BYTEPOS (tlendpos)
13254 - MATRIX_ROW_START_BYTEPOS (row));
13255
13256 increment_matrix_positions (w->current_matrix,
13257 this_line_vpos + 1,
13258 w->current_matrix->nrows,
13259 delta, delta_bytes);
13260 }
13261
13262 /* If this row displays text now but previously didn't,
13263 or vice versa, w->window_end_vpos may have to be
13264 adjusted. */
13265 if ((it.glyph_row - 1)->displays_text_p)
13266 {
13267 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13268 XSETINT (w->window_end_vpos, this_line_vpos);
13269 }
13270 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13271 && this_line_vpos > 0)
13272 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13273 w->window_end_valid = Qnil;
13274
13275 /* Update hint: No need to try to scroll in update_window. */
13276 w->desired_matrix->no_scrolling_p = 1;
13277
13278 #ifdef GLYPH_DEBUG
13279 *w->desired_matrix->method = 0;
13280 debug_method_add (w, "optimization 1");
13281 #endif
13282 #ifdef HAVE_WINDOW_SYSTEM
13283 update_window_fringes (w, 0);
13284 #endif
13285 goto update;
13286 }
13287 else
13288 goto cancel;
13289 }
13290 else if (/* Cursor position hasn't changed. */
13291 PT == w->last_point
13292 /* Make sure the cursor was last displayed
13293 in this window. Otherwise we have to reposition it. */
13294 && 0 <= w->cursor.vpos
13295 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13296 {
13297 if (!must_finish)
13298 {
13299 do_pending_window_change (1);
13300 /* If selected_window changed, redisplay again. */
13301 if (WINDOWP (selected_window)
13302 && (w = XWINDOW (selected_window)) != sw)
13303 goto retry;
13304
13305 /* We used to always goto end_of_redisplay here, but this
13306 isn't enough if we have a blinking cursor. */
13307 if (w->cursor_off_p == w->last_cursor_off_p)
13308 goto end_of_redisplay;
13309 }
13310 goto update;
13311 }
13312 /* If highlighting the region, or if the cursor is in the echo area,
13313 then we can't just move the cursor. */
13314 else if (! (!NILP (Vtransient_mark_mode)
13315 && !NILP (BVAR (current_buffer, mark_active)))
13316 && (EQ (selected_window,
13317 BVAR (current_buffer, last_selected_window))
13318 || highlight_nonselected_windows)
13319 && NILP (w->region_showing)
13320 && NILP (Vshow_trailing_whitespace)
13321 && !cursor_in_echo_area)
13322 {
13323 struct it it;
13324 struct glyph_row *row;
13325
13326 /* Skip from tlbufpos to PT and see where it is. Note that
13327 PT may be in invisible text. If so, we will end at the
13328 next visible position. */
13329 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13330 NULL, DEFAULT_FACE_ID);
13331 it.current_x = this_line_start_x;
13332 it.current_y = this_line_y;
13333 it.vpos = this_line_vpos;
13334
13335 /* The call to move_it_to stops in front of PT, but
13336 moves over before-strings. */
13337 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13338
13339 if (it.vpos == this_line_vpos
13340 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13341 row->enabled_p))
13342 {
13343 eassert (this_line_vpos == it.vpos);
13344 eassert (this_line_y == it.current_y);
13345 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13346 #ifdef GLYPH_DEBUG
13347 *w->desired_matrix->method = 0;
13348 debug_method_add (w, "optimization 3");
13349 #endif
13350 goto update;
13351 }
13352 else
13353 goto cancel;
13354 }
13355
13356 cancel:
13357 /* Text changed drastically or point moved off of line. */
13358 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13359 }
13360
13361 CHARPOS (this_line_start_pos) = 0;
13362 consider_all_windows_p |= buffer_shared > 1;
13363 ++clear_face_cache_count;
13364 #ifdef HAVE_WINDOW_SYSTEM
13365 ++clear_image_cache_count;
13366 #endif
13367
13368 /* Build desired matrices, and update the display. If
13369 consider_all_windows_p is non-zero, do it for all windows on all
13370 frames. Otherwise do it for selected_window, only. */
13371
13372 if (consider_all_windows_p)
13373 {
13374 Lisp_Object tail, frame;
13375
13376 FOR_EACH_FRAME (tail, frame)
13377 XFRAME (frame)->updated_p = 0;
13378
13379 /* Recompute # windows showing selected buffer. This will be
13380 incremented each time such a window is displayed. */
13381 buffer_shared = 0;
13382
13383 FOR_EACH_FRAME (tail, frame)
13384 {
13385 struct frame *f = XFRAME (frame);
13386
13387 /* We don't have to do anything for unselected terminal
13388 frames. */
13389 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13390 && !EQ (FRAME_TTY (f)->top_frame, frame))
13391 continue;
13392
13393 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13394 {
13395 if (! EQ (frame, selected_frame))
13396 /* Select the frame, for the sake of frame-local
13397 variables. */
13398 select_frame_for_redisplay (frame);
13399
13400 /* Mark all the scroll bars to be removed; we'll redeem
13401 the ones we want when we redisplay their windows. */
13402 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13403 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13404
13405 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13406 redisplay_windows (FRAME_ROOT_WINDOW (f));
13407
13408 /* The X error handler may have deleted that frame. */
13409 if (!FRAME_LIVE_P (f))
13410 continue;
13411
13412 /* Any scroll bars which redisplay_windows should have
13413 nuked should now go away. */
13414 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13415 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13416
13417 /* If fonts changed, display again. */
13418 /* ??? rms: I suspect it is a mistake to jump all the way
13419 back to retry here. It should just retry this frame. */
13420 if (fonts_changed_p)
13421 goto retry;
13422
13423 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13424 {
13425 /* See if we have to hscroll. */
13426 if (!f->already_hscrolled_p)
13427 {
13428 f->already_hscrolled_p = 1;
13429 if (hscroll_windows (FVAR (f, root_window)))
13430 goto retry;
13431 }
13432
13433 /* Prevent various kinds of signals during display
13434 update. stdio is not robust about handling
13435 signals, which can cause an apparent I/O
13436 error. */
13437 if (interrupt_input)
13438 unrequest_sigio ();
13439 STOP_POLLING;
13440
13441 /* Update the display. */
13442 set_window_update_flags (XWINDOW (FVAR (f, root_window)), 1);
13443 pending |= update_frame (f, 0, 0);
13444 f->updated_p = 1;
13445 }
13446 }
13447 }
13448
13449 if (!EQ (old_frame, selected_frame)
13450 && FRAME_LIVE_P (XFRAME (old_frame)))
13451 /* We played a bit fast-and-loose above and allowed selected_frame
13452 and selected_window to be temporarily out-of-sync but let's make
13453 sure this stays contained. */
13454 select_frame_for_redisplay (old_frame);
13455 eassert (EQ (FVAR (XFRAME (selected_frame), selected_window),
13456 selected_window));
13457
13458 if (!pending)
13459 {
13460 /* Do the mark_window_display_accurate after all windows have
13461 been redisplayed because this call resets flags in buffers
13462 which are needed for proper redisplay. */
13463 FOR_EACH_FRAME (tail, frame)
13464 {
13465 struct frame *f = XFRAME (frame);
13466 if (f->updated_p)
13467 {
13468 mark_window_display_accurate (FVAR (f, root_window), 1);
13469 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13470 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13471 }
13472 }
13473 }
13474 }
13475 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13476 {
13477 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13478 struct frame *mini_frame;
13479
13480 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13481 /* Use list_of_error, not Qerror, so that
13482 we catch only errors and don't run the debugger. */
13483 internal_condition_case_1 (redisplay_window_1, selected_window,
13484 list_of_error,
13485 redisplay_window_error);
13486 if (update_miniwindow_p)
13487 internal_condition_case_1 (redisplay_window_1, mini_window,
13488 list_of_error,
13489 redisplay_window_error);
13490
13491 /* Compare desired and current matrices, perform output. */
13492
13493 update:
13494 /* If fonts changed, display again. */
13495 if (fonts_changed_p)
13496 goto retry;
13497
13498 /* Prevent various kinds of signals during display update.
13499 stdio is not robust about handling signals,
13500 which can cause an apparent I/O error. */
13501 if (interrupt_input)
13502 unrequest_sigio ();
13503 STOP_POLLING;
13504
13505 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13506 {
13507 if (hscroll_windows (selected_window))
13508 goto retry;
13509
13510 XWINDOW (selected_window)->must_be_updated_p = 1;
13511 pending = update_frame (sf, 0, 0);
13512 }
13513
13514 /* We may have called echo_area_display at the top of this
13515 function. If the echo area is on another frame, that may
13516 have put text on a frame other than the selected one, so the
13517 above call to update_frame would not have caught it. Catch
13518 it here. */
13519 mini_window = FRAME_MINIBUF_WINDOW (sf);
13520 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13521
13522 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13523 {
13524 XWINDOW (mini_window)->must_be_updated_p = 1;
13525 pending |= update_frame (mini_frame, 0, 0);
13526 if (!pending && hscroll_windows (mini_window))
13527 goto retry;
13528 }
13529 }
13530
13531 /* If display was paused because of pending input, make sure we do a
13532 thorough update the next time. */
13533 if (pending)
13534 {
13535 /* Prevent the optimization at the beginning of
13536 redisplay_internal that tries a single-line update of the
13537 line containing the cursor in the selected window. */
13538 CHARPOS (this_line_start_pos) = 0;
13539
13540 /* Let the overlay arrow be updated the next time. */
13541 update_overlay_arrows (0);
13542
13543 /* If we pause after scrolling, some rows in the current
13544 matrices of some windows are not valid. */
13545 if (!WINDOW_FULL_WIDTH_P (w)
13546 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13547 update_mode_lines = 1;
13548 }
13549 else
13550 {
13551 if (!consider_all_windows_p)
13552 {
13553 /* This has already been done above if
13554 consider_all_windows_p is set. */
13555 mark_window_display_accurate_1 (w, 1);
13556
13557 /* Say overlay arrows are up to date. */
13558 update_overlay_arrows (1);
13559
13560 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13561 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13562 }
13563
13564 update_mode_lines = 0;
13565 windows_or_buffers_changed = 0;
13566 cursor_type_changed = 0;
13567 }
13568
13569 /* Start SIGIO interrupts coming again. Having them off during the
13570 code above makes it less likely one will discard output, but not
13571 impossible, since there might be stuff in the system buffer here.
13572 But it is much hairier to try to do anything about that. */
13573 if (interrupt_input)
13574 request_sigio ();
13575 RESUME_POLLING;
13576
13577 /* If a frame has become visible which was not before, redisplay
13578 again, so that we display it. Expose events for such a frame
13579 (which it gets when becoming visible) don't call the parts of
13580 redisplay constructing glyphs, so simply exposing a frame won't
13581 display anything in this case. So, we have to display these
13582 frames here explicitly. */
13583 if (!pending)
13584 {
13585 Lisp_Object tail, frame;
13586 int new_count = 0;
13587
13588 FOR_EACH_FRAME (tail, frame)
13589 {
13590 int this_is_visible = 0;
13591
13592 if (XFRAME (frame)->visible)
13593 this_is_visible = 1;
13594 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13595 if (XFRAME (frame)->visible)
13596 this_is_visible = 1;
13597
13598 if (this_is_visible)
13599 new_count++;
13600 }
13601
13602 if (new_count != number_of_visible_frames)
13603 windows_or_buffers_changed++;
13604 }
13605
13606 /* Change frame size now if a change is pending. */
13607 do_pending_window_change (1);
13608
13609 /* If we just did a pending size change, or have additional
13610 visible frames, or selected_window changed, redisplay again. */
13611 if ((windows_or_buffers_changed && !pending)
13612 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13613 goto retry;
13614
13615 /* Clear the face and image caches.
13616
13617 We used to do this only if consider_all_windows_p. But the cache
13618 needs to be cleared if a timer creates images in the current
13619 buffer (e.g. the test case in Bug#6230). */
13620
13621 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13622 {
13623 clear_face_cache (0);
13624 clear_face_cache_count = 0;
13625 }
13626
13627 #ifdef HAVE_WINDOW_SYSTEM
13628 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13629 {
13630 clear_image_caches (Qnil);
13631 clear_image_cache_count = 0;
13632 }
13633 #endif /* HAVE_WINDOW_SYSTEM */
13634
13635 end_of_redisplay:
13636 unbind_to (count, Qnil);
13637 RESUME_POLLING;
13638 }
13639
13640
13641 /* Redisplay, but leave alone any recent echo area message unless
13642 another message has been requested in its place.
13643
13644 This is useful in situations where you need to redisplay but no
13645 user action has occurred, making it inappropriate for the message
13646 area to be cleared. See tracking_off and
13647 wait_reading_process_output for examples of these situations.
13648
13649 FROM_WHERE is an integer saying from where this function was
13650 called. This is useful for debugging. */
13651
13652 void
13653 redisplay_preserve_echo_area (int from_where)
13654 {
13655 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13656
13657 if (!NILP (echo_area_buffer[1]))
13658 {
13659 /* We have a previously displayed message, but no current
13660 message. Redisplay the previous message. */
13661 display_last_displayed_message_p = 1;
13662 redisplay_internal ();
13663 display_last_displayed_message_p = 0;
13664 }
13665 else
13666 redisplay_internal ();
13667
13668 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13669 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13670 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13671 }
13672
13673
13674 /* Function registered with record_unwind_protect in
13675 redisplay_internal. Reset redisplaying_p to the value it had
13676 before redisplay_internal was called, and clear
13677 prevent_freeing_realized_faces_p. It also selects the previously
13678 selected frame, unless it has been deleted (by an X connection
13679 failure during redisplay, for example). */
13680
13681 static Lisp_Object
13682 unwind_redisplay (Lisp_Object val)
13683 {
13684 Lisp_Object old_redisplaying_p, old_frame;
13685
13686 old_redisplaying_p = XCAR (val);
13687 redisplaying_p = XFASTINT (old_redisplaying_p);
13688 old_frame = XCDR (val);
13689 if (! EQ (old_frame, selected_frame)
13690 && FRAME_LIVE_P (XFRAME (old_frame)))
13691 select_frame_for_redisplay (old_frame);
13692 return Qnil;
13693 }
13694
13695
13696 /* Mark the display of window W as accurate or inaccurate. If
13697 ACCURATE_P is non-zero mark display of W as accurate. If
13698 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13699 redisplay_internal is called. */
13700
13701 static void
13702 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13703 {
13704 if (BUFFERP (w->buffer))
13705 {
13706 struct buffer *b = XBUFFER (w->buffer);
13707
13708 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13709 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13710 w->last_had_star
13711 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13712
13713 if (accurate_p)
13714 {
13715 b->clip_changed = 0;
13716 b->prevent_redisplay_optimizations_p = 0;
13717
13718 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13719 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13720 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13721 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13722
13723 w->current_matrix->buffer = b;
13724 w->current_matrix->begv = BUF_BEGV (b);
13725 w->current_matrix->zv = BUF_ZV (b);
13726
13727 w->last_cursor = w->cursor;
13728 w->last_cursor_off_p = w->cursor_off_p;
13729
13730 if (w == XWINDOW (selected_window))
13731 w->last_point = BUF_PT (b);
13732 else
13733 w->last_point = XMARKER (w->pointm)->charpos;
13734 }
13735 }
13736
13737 if (accurate_p)
13738 {
13739 w->window_end_valid = w->buffer;
13740 w->update_mode_line = 0;
13741 }
13742 }
13743
13744
13745 /* Mark the display of windows in the window tree rooted at WINDOW as
13746 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13747 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13748 be redisplayed the next time redisplay_internal is called. */
13749
13750 void
13751 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13752 {
13753 struct window *w;
13754
13755 for (; !NILP (window); window = w->next)
13756 {
13757 w = XWINDOW (window);
13758 mark_window_display_accurate_1 (w, accurate_p);
13759
13760 if (!NILP (w->vchild))
13761 mark_window_display_accurate (w->vchild, accurate_p);
13762 if (!NILP (w->hchild))
13763 mark_window_display_accurate (w->hchild, accurate_p);
13764 }
13765
13766 if (accurate_p)
13767 {
13768 update_overlay_arrows (1);
13769 }
13770 else
13771 {
13772 /* Force a thorough redisplay the next time by setting
13773 last_arrow_position and last_arrow_string to t, which is
13774 unequal to any useful value of Voverlay_arrow_... */
13775 update_overlay_arrows (-1);
13776 }
13777 }
13778
13779
13780 /* Return value in display table DP (Lisp_Char_Table *) for character
13781 C. Since a display table doesn't have any parent, we don't have to
13782 follow parent. Do not call this function directly but use the
13783 macro DISP_CHAR_VECTOR. */
13784
13785 Lisp_Object
13786 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13787 {
13788 Lisp_Object val;
13789
13790 if (ASCII_CHAR_P (c))
13791 {
13792 val = dp->ascii;
13793 if (SUB_CHAR_TABLE_P (val))
13794 val = XSUB_CHAR_TABLE (val)->contents[c];
13795 }
13796 else
13797 {
13798 Lisp_Object table;
13799
13800 XSETCHAR_TABLE (table, dp);
13801 val = char_table_ref (table, c);
13802 }
13803 if (NILP (val))
13804 val = dp->defalt;
13805 return val;
13806 }
13807
13808
13809 \f
13810 /***********************************************************************
13811 Window Redisplay
13812 ***********************************************************************/
13813
13814 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13815
13816 static void
13817 redisplay_windows (Lisp_Object window)
13818 {
13819 while (!NILP (window))
13820 {
13821 struct window *w = XWINDOW (window);
13822
13823 if (!NILP (w->hchild))
13824 redisplay_windows (w->hchild);
13825 else if (!NILP (w->vchild))
13826 redisplay_windows (w->vchild);
13827 else if (!NILP (w->buffer))
13828 {
13829 displayed_buffer = XBUFFER (w->buffer);
13830 /* Use list_of_error, not Qerror, so that
13831 we catch only errors and don't run the debugger. */
13832 internal_condition_case_1 (redisplay_window_0, window,
13833 list_of_error,
13834 redisplay_window_error);
13835 }
13836
13837 window = w->next;
13838 }
13839 }
13840
13841 static Lisp_Object
13842 redisplay_window_error (Lisp_Object ignore)
13843 {
13844 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13845 return Qnil;
13846 }
13847
13848 static Lisp_Object
13849 redisplay_window_0 (Lisp_Object window)
13850 {
13851 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13852 redisplay_window (window, 0);
13853 return Qnil;
13854 }
13855
13856 static Lisp_Object
13857 redisplay_window_1 (Lisp_Object window)
13858 {
13859 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13860 redisplay_window (window, 1);
13861 return Qnil;
13862 }
13863 \f
13864
13865 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13866 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13867 which positions recorded in ROW differ from current buffer
13868 positions.
13869
13870 Return 0 if cursor is not on this row, 1 otherwise. */
13871
13872 static int
13873 set_cursor_from_row (struct window *w, struct glyph_row *row,
13874 struct glyph_matrix *matrix,
13875 ptrdiff_t delta, ptrdiff_t delta_bytes,
13876 int dy, int dvpos)
13877 {
13878 struct glyph *glyph = row->glyphs[TEXT_AREA];
13879 struct glyph *end = glyph + row->used[TEXT_AREA];
13880 struct glyph *cursor = NULL;
13881 /* The last known character position in row. */
13882 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13883 int x = row->x;
13884 ptrdiff_t pt_old = PT - delta;
13885 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13886 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13887 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13888 /* A glyph beyond the edge of TEXT_AREA which we should never
13889 touch. */
13890 struct glyph *glyphs_end = end;
13891 /* Non-zero means we've found a match for cursor position, but that
13892 glyph has the avoid_cursor_p flag set. */
13893 int match_with_avoid_cursor = 0;
13894 /* Non-zero means we've seen at least one glyph that came from a
13895 display string. */
13896 int string_seen = 0;
13897 /* Largest and smallest buffer positions seen so far during scan of
13898 glyph row. */
13899 ptrdiff_t bpos_max = pos_before;
13900 ptrdiff_t bpos_min = pos_after;
13901 /* Last buffer position covered by an overlay string with an integer
13902 `cursor' property. */
13903 ptrdiff_t bpos_covered = 0;
13904 /* Non-zero means the display string on which to display the cursor
13905 comes from a text property, not from an overlay. */
13906 int string_from_text_prop = 0;
13907
13908 /* Don't even try doing anything if called for a mode-line or
13909 header-line row, since the rest of the code isn't prepared to
13910 deal with such calamities. */
13911 eassert (!row->mode_line_p);
13912 if (row->mode_line_p)
13913 return 0;
13914
13915 /* Skip over glyphs not having an object at the start and the end of
13916 the row. These are special glyphs like truncation marks on
13917 terminal frames. */
13918 if (row->displays_text_p)
13919 {
13920 if (!row->reversed_p)
13921 {
13922 while (glyph < end
13923 && INTEGERP (glyph->object)
13924 && glyph->charpos < 0)
13925 {
13926 x += glyph->pixel_width;
13927 ++glyph;
13928 }
13929 while (end > glyph
13930 && INTEGERP ((end - 1)->object)
13931 /* CHARPOS is zero for blanks and stretch glyphs
13932 inserted by extend_face_to_end_of_line. */
13933 && (end - 1)->charpos <= 0)
13934 --end;
13935 glyph_before = glyph - 1;
13936 glyph_after = end;
13937 }
13938 else
13939 {
13940 struct glyph *g;
13941
13942 /* If the glyph row is reversed, we need to process it from back
13943 to front, so swap the edge pointers. */
13944 glyphs_end = end = glyph - 1;
13945 glyph += row->used[TEXT_AREA] - 1;
13946
13947 while (glyph > end + 1
13948 && INTEGERP (glyph->object)
13949 && glyph->charpos < 0)
13950 {
13951 --glyph;
13952 x -= glyph->pixel_width;
13953 }
13954 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13955 --glyph;
13956 /* By default, in reversed rows we put the cursor on the
13957 rightmost (first in the reading order) glyph. */
13958 for (g = end + 1; g < glyph; g++)
13959 x += g->pixel_width;
13960 while (end < glyph
13961 && INTEGERP ((end + 1)->object)
13962 && (end + 1)->charpos <= 0)
13963 ++end;
13964 glyph_before = glyph + 1;
13965 glyph_after = end;
13966 }
13967 }
13968 else if (row->reversed_p)
13969 {
13970 /* In R2L rows that don't display text, put the cursor on the
13971 rightmost glyph. Case in point: an empty last line that is
13972 part of an R2L paragraph. */
13973 cursor = end - 1;
13974 /* Avoid placing the cursor on the last glyph of the row, where
13975 on terminal frames we hold the vertical border between
13976 adjacent windows. */
13977 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13978 && !WINDOW_RIGHTMOST_P (w)
13979 && cursor == row->glyphs[LAST_AREA] - 1)
13980 cursor--;
13981 x = -1; /* will be computed below, at label compute_x */
13982 }
13983
13984 /* Step 1: Try to find the glyph whose character position
13985 corresponds to point. If that's not possible, find 2 glyphs
13986 whose character positions are the closest to point, one before
13987 point, the other after it. */
13988 if (!row->reversed_p)
13989 while (/* not marched to end of glyph row */
13990 glyph < end
13991 /* glyph was not inserted by redisplay for internal purposes */
13992 && !INTEGERP (glyph->object))
13993 {
13994 if (BUFFERP (glyph->object))
13995 {
13996 ptrdiff_t dpos = glyph->charpos - pt_old;
13997
13998 if (glyph->charpos > bpos_max)
13999 bpos_max = glyph->charpos;
14000 if (glyph->charpos < bpos_min)
14001 bpos_min = glyph->charpos;
14002 if (!glyph->avoid_cursor_p)
14003 {
14004 /* If we hit point, we've found the glyph on which to
14005 display the cursor. */
14006 if (dpos == 0)
14007 {
14008 match_with_avoid_cursor = 0;
14009 break;
14010 }
14011 /* See if we've found a better approximation to
14012 POS_BEFORE or to POS_AFTER. */
14013 if (0 > dpos && dpos > pos_before - pt_old)
14014 {
14015 pos_before = glyph->charpos;
14016 glyph_before = glyph;
14017 }
14018 else if (0 < dpos && dpos < pos_after - pt_old)
14019 {
14020 pos_after = glyph->charpos;
14021 glyph_after = glyph;
14022 }
14023 }
14024 else if (dpos == 0)
14025 match_with_avoid_cursor = 1;
14026 }
14027 else if (STRINGP (glyph->object))
14028 {
14029 Lisp_Object chprop;
14030 ptrdiff_t glyph_pos = glyph->charpos;
14031
14032 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14033 glyph->object);
14034 if (!NILP (chprop))
14035 {
14036 /* If the string came from a `display' text property,
14037 look up the buffer position of that property and
14038 use that position to update bpos_max, as if we
14039 actually saw such a position in one of the row's
14040 glyphs. This helps with supporting integer values
14041 of `cursor' property on the display string in
14042 situations where most or all of the row's buffer
14043 text is completely covered by display properties,
14044 so that no glyph with valid buffer positions is
14045 ever seen in the row. */
14046 ptrdiff_t prop_pos =
14047 string_buffer_position_lim (glyph->object, pos_before,
14048 pos_after, 0);
14049
14050 if (prop_pos >= pos_before)
14051 bpos_max = prop_pos - 1;
14052 }
14053 if (INTEGERP (chprop))
14054 {
14055 bpos_covered = bpos_max + XINT (chprop);
14056 /* If the `cursor' property covers buffer positions up
14057 to and including point, we should display cursor on
14058 this glyph. Note that, if a `cursor' property on one
14059 of the string's characters has an integer value, we
14060 will break out of the loop below _before_ we get to
14061 the position match above. IOW, integer values of
14062 the `cursor' property override the "exact match for
14063 point" strategy of positioning the cursor. */
14064 /* Implementation note: bpos_max == pt_old when, e.g.,
14065 we are in an empty line, where bpos_max is set to
14066 MATRIX_ROW_START_CHARPOS, see above. */
14067 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14068 {
14069 cursor = glyph;
14070 break;
14071 }
14072 }
14073
14074 string_seen = 1;
14075 }
14076 x += glyph->pixel_width;
14077 ++glyph;
14078 }
14079 else if (glyph > end) /* row is reversed */
14080 while (!INTEGERP (glyph->object))
14081 {
14082 if (BUFFERP (glyph->object))
14083 {
14084 ptrdiff_t dpos = glyph->charpos - pt_old;
14085
14086 if (glyph->charpos > bpos_max)
14087 bpos_max = glyph->charpos;
14088 if (glyph->charpos < bpos_min)
14089 bpos_min = glyph->charpos;
14090 if (!glyph->avoid_cursor_p)
14091 {
14092 if (dpos == 0)
14093 {
14094 match_with_avoid_cursor = 0;
14095 break;
14096 }
14097 if (0 > dpos && dpos > pos_before - pt_old)
14098 {
14099 pos_before = glyph->charpos;
14100 glyph_before = glyph;
14101 }
14102 else if (0 < dpos && dpos < pos_after - pt_old)
14103 {
14104 pos_after = glyph->charpos;
14105 glyph_after = glyph;
14106 }
14107 }
14108 else if (dpos == 0)
14109 match_with_avoid_cursor = 1;
14110 }
14111 else if (STRINGP (glyph->object))
14112 {
14113 Lisp_Object chprop;
14114 ptrdiff_t glyph_pos = glyph->charpos;
14115
14116 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14117 glyph->object);
14118 if (!NILP (chprop))
14119 {
14120 ptrdiff_t prop_pos =
14121 string_buffer_position_lim (glyph->object, pos_before,
14122 pos_after, 0);
14123
14124 if (prop_pos >= pos_before)
14125 bpos_max = prop_pos - 1;
14126 }
14127 if (INTEGERP (chprop))
14128 {
14129 bpos_covered = bpos_max + XINT (chprop);
14130 /* If the `cursor' property covers buffer positions up
14131 to and including point, we should display cursor on
14132 this glyph. */
14133 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14134 {
14135 cursor = glyph;
14136 break;
14137 }
14138 }
14139 string_seen = 1;
14140 }
14141 --glyph;
14142 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14143 {
14144 x--; /* can't use any pixel_width */
14145 break;
14146 }
14147 x -= glyph->pixel_width;
14148 }
14149
14150 /* Step 2: If we didn't find an exact match for point, we need to
14151 look for a proper place to put the cursor among glyphs between
14152 GLYPH_BEFORE and GLYPH_AFTER. */
14153 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14154 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14155 && bpos_covered < pt_old)
14156 {
14157 /* An empty line has a single glyph whose OBJECT is zero and
14158 whose CHARPOS is the position of a newline on that line.
14159 Note that on a TTY, there are more glyphs after that, which
14160 were produced by extend_face_to_end_of_line, but their
14161 CHARPOS is zero or negative. */
14162 int empty_line_p =
14163 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14164 && INTEGERP (glyph->object) && glyph->charpos > 0;
14165
14166 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14167 {
14168 ptrdiff_t ellipsis_pos;
14169
14170 /* Scan back over the ellipsis glyphs. */
14171 if (!row->reversed_p)
14172 {
14173 ellipsis_pos = (glyph - 1)->charpos;
14174 while (glyph > row->glyphs[TEXT_AREA]
14175 && (glyph - 1)->charpos == ellipsis_pos)
14176 glyph--, x -= glyph->pixel_width;
14177 /* That loop always goes one position too far, including
14178 the glyph before the ellipsis. So scan forward over
14179 that one. */
14180 x += glyph->pixel_width;
14181 glyph++;
14182 }
14183 else /* row is reversed */
14184 {
14185 ellipsis_pos = (glyph + 1)->charpos;
14186 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14187 && (glyph + 1)->charpos == ellipsis_pos)
14188 glyph++, x += glyph->pixel_width;
14189 x -= glyph->pixel_width;
14190 glyph--;
14191 }
14192 }
14193 else if (match_with_avoid_cursor)
14194 {
14195 cursor = glyph_after;
14196 x = -1;
14197 }
14198 else if (string_seen)
14199 {
14200 int incr = row->reversed_p ? -1 : +1;
14201
14202 /* Need to find the glyph that came out of a string which is
14203 present at point. That glyph is somewhere between
14204 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14205 positioned between POS_BEFORE and POS_AFTER in the
14206 buffer. */
14207 struct glyph *start, *stop;
14208 ptrdiff_t pos = pos_before;
14209
14210 x = -1;
14211
14212 /* If the row ends in a newline from a display string,
14213 reordering could have moved the glyphs belonging to the
14214 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14215 in this case we extend the search to the last glyph in
14216 the row that was not inserted by redisplay. */
14217 if (row->ends_in_newline_from_string_p)
14218 {
14219 glyph_after = end;
14220 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14221 }
14222
14223 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14224 correspond to POS_BEFORE and POS_AFTER, respectively. We
14225 need START and STOP in the order that corresponds to the
14226 row's direction as given by its reversed_p flag. If the
14227 directionality of characters between POS_BEFORE and
14228 POS_AFTER is the opposite of the row's base direction,
14229 these characters will have been reordered for display,
14230 and we need to reverse START and STOP. */
14231 if (!row->reversed_p)
14232 {
14233 start = min (glyph_before, glyph_after);
14234 stop = max (glyph_before, glyph_after);
14235 }
14236 else
14237 {
14238 start = max (glyph_before, glyph_after);
14239 stop = min (glyph_before, glyph_after);
14240 }
14241 for (glyph = start + incr;
14242 row->reversed_p ? glyph > stop : glyph < stop; )
14243 {
14244
14245 /* Any glyphs that come from the buffer are here because
14246 of bidi reordering. Skip them, and only pay
14247 attention to glyphs that came from some string. */
14248 if (STRINGP (glyph->object))
14249 {
14250 Lisp_Object str;
14251 ptrdiff_t tem;
14252 /* If the display property covers the newline, we
14253 need to search for it one position farther. */
14254 ptrdiff_t lim = pos_after
14255 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14256
14257 string_from_text_prop = 0;
14258 str = glyph->object;
14259 tem = string_buffer_position_lim (str, pos, lim, 0);
14260 if (tem == 0 /* from overlay */
14261 || pos <= tem)
14262 {
14263 /* If the string from which this glyph came is
14264 found in the buffer at point, or at position
14265 that is closer to point than pos_after, then
14266 we've found the glyph we've been looking for.
14267 If it comes from an overlay (tem == 0), and
14268 it has the `cursor' property on one of its
14269 glyphs, record that glyph as a candidate for
14270 displaying the cursor. (As in the
14271 unidirectional version, we will display the
14272 cursor on the last candidate we find.) */
14273 if (tem == 0
14274 || tem == pt_old
14275 || (tem - pt_old > 0 && tem < pos_after))
14276 {
14277 /* The glyphs from this string could have
14278 been reordered. Find the one with the
14279 smallest string position. Or there could
14280 be a character in the string with the
14281 `cursor' property, which means display
14282 cursor on that character's glyph. */
14283 ptrdiff_t strpos = glyph->charpos;
14284
14285 if (tem)
14286 {
14287 cursor = glyph;
14288 string_from_text_prop = 1;
14289 }
14290 for ( ;
14291 (row->reversed_p ? glyph > stop : glyph < stop)
14292 && EQ (glyph->object, str);
14293 glyph += incr)
14294 {
14295 Lisp_Object cprop;
14296 ptrdiff_t gpos = glyph->charpos;
14297
14298 cprop = Fget_char_property (make_number (gpos),
14299 Qcursor,
14300 glyph->object);
14301 if (!NILP (cprop))
14302 {
14303 cursor = glyph;
14304 break;
14305 }
14306 if (tem && glyph->charpos < strpos)
14307 {
14308 strpos = glyph->charpos;
14309 cursor = glyph;
14310 }
14311 }
14312
14313 if (tem == pt_old
14314 || (tem - pt_old > 0 && tem < pos_after))
14315 goto compute_x;
14316 }
14317 if (tem)
14318 pos = tem + 1; /* don't find previous instances */
14319 }
14320 /* This string is not what we want; skip all of the
14321 glyphs that came from it. */
14322 while ((row->reversed_p ? glyph > stop : glyph < stop)
14323 && EQ (glyph->object, str))
14324 glyph += incr;
14325 }
14326 else
14327 glyph += incr;
14328 }
14329
14330 /* If we reached the end of the line, and END was from a string,
14331 the cursor is not on this line. */
14332 if (cursor == NULL
14333 && (row->reversed_p ? glyph <= end : glyph >= end)
14334 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14335 && STRINGP (end->object)
14336 && row->continued_p)
14337 return 0;
14338 }
14339 /* A truncated row may not include PT among its character positions.
14340 Setting the cursor inside the scroll margin will trigger
14341 recalculation of hscroll in hscroll_window_tree. But if a
14342 display string covers point, defer to the string-handling
14343 code below to figure this out. */
14344 else if (row->truncated_on_left_p && pt_old < bpos_min)
14345 {
14346 cursor = glyph_before;
14347 x = -1;
14348 }
14349 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14350 /* Zero-width characters produce no glyphs. */
14351 || (!empty_line_p
14352 && (row->reversed_p
14353 ? glyph_after > glyphs_end
14354 : glyph_after < glyphs_end)))
14355 {
14356 cursor = glyph_after;
14357 x = -1;
14358 }
14359 }
14360
14361 compute_x:
14362 if (cursor != NULL)
14363 glyph = cursor;
14364 else if (glyph == glyphs_end
14365 && pos_before == pos_after
14366 && STRINGP ((row->reversed_p
14367 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14368 : row->glyphs[TEXT_AREA])->object))
14369 {
14370 /* If all the glyphs of this row came from strings, put the
14371 cursor on the first glyph of the row. This avoids having the
14372 cursor outside of the text area in this very rare and hard
14373 use case. */
14374 glyph =
14375 row->reversed_p
14376 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14377 : row->glyphs[TEXT_AREA];
14378 }
14379 if (x < 0)
14380 {
14381 struct glyph *g;
14382
14383 /* Need to compute x that corresponds to GLYPH. */
14384 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14385 {
14386 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14387 abort ();
14388 x += g->pixel_width;
14389 }
14390 }
14391
14392 /* ROW could be part of a continued line, which, under bidi
14393 reordering, might have other rows whose start and end charpos
14394 occlude point. Only set w->cursor if we found a better
14395 approximation to the cursor position than we have from previously
14396 examined candidate rows belonging to the same continued line. */
14397 if (/* we already have a candidate row */
14398 w->cursor.vpos >= 0
14399 /* that candidate is not the row we are processing */
14400 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14401 /* Make sure cursor.vpos specifies a row whose start and end
14402 charpos occlude point, and it is valid candidate for being a
14403 cursor-row. This is because some callers of this function
14404 leave cursor.vpos at the row where the cursor was displayed
14405 during the last redisplay cycle. */
14406 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14407 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14408 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14409 {
14410 struct glyph *g1 =
14411 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14412
14413 /* Don't consider glyphs that are outside TEXT_AREA. */
14414 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14415 return 0;
14416 /* Keep the candidate whose buffer position is the closest to
14417 point or has the `cursor' property. */
14418 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14419 w->cursor.hpos >= 0
14420 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14421 && ((BUFFERP (g1->object)
14422 && (g1->charpos == pt_old /* an exact match always wins */
14423 || (BUFFERP (glyph->object)
14424 && eabs (g1->charpos - pt_old)
14425 < eabs (glyph->charpos - pt_old))))
14426 /* previous candidate is a glyph from a string that has
14427 a non-nil `cursor' property */
14428 || (STRINGP (g1->object)
14429 && (!NILP (Fget_char_property (make_number (g1->charpos),
14430 Qcursor, g1->object))
14431 /* previous candidate is from the same display
14432 string as this one, and the display string
14433 came from a text property */
14434 || (EQ (g1->object, glyph->object)
14435 && string_from_text_prop)
14436 /* this candidate is from newline and its
14437 position is not an exact match */
14438 || (INTEGERP (glyph->object)
14439 && glyph->charpos != pt_old)))))
14440 return 0;
14441 /* If this candidate gives an exact match, use that. */
14442 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14443 /* If this candidate is a glyph created for the
14444 terminating newline of a line, and point is on that
14445 newline, it wins because it's an exact match. */
14446 || (!row->continued_p
14447 && INTEGERP (glyph->object)
14448 && glyph->charpos == 0
14449 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14450 /* Otherwise, keep the candidate that comes from a row
14451 spanning less buffer positions. This may win when one or
14452 both candidate positions are on glyphs that came from
14453 display strings, for which we cannot compare buffer
14454 positions. */
14455 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14456 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14457 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14458 return 0;
14459 }
14460 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14461 w->cursor.x = x;
14462 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14463 w->cursor.y = row->y + dy;
14464
14465 if (w == XWINDOW (selected_window))
14466 {
14467 if (!row->continued_p
14468 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14469 && row->x == 0)
14470 {
14471 this_line_buffer = XBUFFER (w->buffer);
14472
14473 CHARPOS (this_line_start_pos)
14474 = MATRIX_ROW_START_CHARPOS (row) + delta;
14475 BYTEPOS (this_line_start_pos)
14476 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14477
14478 CHARPOS (this_line_end_pos)
14479 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14480 BYTEPOS (this_line_end_pos)
14481 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14482
14483 this_line_y = w->cursor.y;
14484 this_line_pixel_height = row->height;
14485 this_line_vpos = w->cursor.vpos;
14486 this_line_start_x = row->x;
14487 }
14488 else
14489 CHARPOS (this_line_start_pos) = 0;
14490 }
14491
14492 return 1;
14493 }
14494
14495
14496 /* Run window scroll functions, if any, for WINDOW with new window
14497 start STARTP. Sets the window start of WINDOW to that position.
14498
14499 We assume that the window's buffer is really current. */
14500
14501 static inline struct text_pos
14502 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14503 {
14504 struct window *w = XWINDOW (window);
14505 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14506
14507 if (current_buffer != XBUFFER (w->buffer))
14508 abort ();
14509
14510 if (!NILP (Vwindow_scroll_functions))
14511 {
14512 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14513 make_number (CHARPOS (startp)));
14514 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14515 /* In case the hook functions switch buffers. */
14516 if (current_buffer != XBUFFER (w->buffer))
14517 set_buffer_internal_1 (XBUFFER (w->buffer));
14518 }
14519
14520 return startp;
14521 }
14522
14523
14524 /* Make sure the line containing the cursor is fully visible.
14525 A value of 1 means there is nothing to be done.
14526 (Either the line is fully visible, or it cannot be made so,
14527 or we cannot tell.)
14528
14529 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14530 is higher than window.
14531
14532 A value of 0 means the caller should do scrolling
14533 as if point had gone off the screen. */
14534
14535 static int
14536 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14537 {
14538 struct glyph_matrix *matrix;
14539 struct glyph_row *row;
14540 int window_height;
14541
14542 if (!make_cursor_line_fully_visible_p)
14543 return 1;
14544
14545 /* It's not always possible to find the cursor, e.g, when a window
14546 is full of overlay strings. Don't do anything in that case. */
14547 if (w->cursor.vpos < 0)
14548 return 1;
14549
14550 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14551 row = MATRIX_ROW (matrix, w->cursor.vpos);
14552
14553 /* If the cursor row is not partially visible, there's nothing to do. */
14554 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14555 return 1;
14556
14557 /* If the row the cursor is in is taller than the window's height,
14558 it's not clear what to do, so do nothing. */
14559 window_height = window_box_height (w);
14560 if (row->height >= window_height)
14561 {
14562 if (!force_p || MINI_WINDOW_P (w)
14563 || w->vscroll || w->cursor.vpos == 0)
14564 return 1;
14565 }
14566 return 0;
14567 }
14568
14569
14570 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14571 non-zero means only WINDOW is redisplayed in redisplay_internal.
14572 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14573 in redisplay_window to bring a partially visible line into view in
14574 the case that only the cursor has moved.
14575
14576 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14577 last screen line's vertical height extends past the end of the screen.
14578
14579 Value is
14580
14581 1 if scrolling succeeded
14582
14583 0 if scrolling didn't find point.
14584
14585 -1 if new fonts have been loaded so that we must interrupt
14586 redisplay, adjust glyph matrices, and try again. */
14587
14588 enum
14589 {
14590 SCROLLING_SUCCESS,
14591 SCROLLING_FAILED,
14592 SCROLLING_NEED_LARGER_MATRICES
14593 };
14594
14595 /* If scroll-conservatively is more than this, never recenter.
14596
14597 If you change this, don't forget to update the doc string of
14598 `scroll-conservatively' and the Emacs manual. */
14599 #define SCROLL_LIMIT 100
14600
14601 static int
14602 try_scrolling (Lisp_Object window, int just_this_one_p,
14603 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14604 int temp_scroll_step, int last_line_misfit)
14605 {
14606 struct window *w = XWINDOW (window);
14607 struct frame *f = XFRAME (w->frame);
14608 struct text_pos pos, startp;
14609 struct it it;
14610 int this_scroll_margin, scroll_max, rc, height;
14611 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14612 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14613 Lisp_Object aggressive;
14614 /* We will never try scrolling more than this number of lines. */
14615 int scroll_limit = SCROLL_LIMIT;
14616
14617 #ifdef GLYPH_DEBUG
14618 debug_method_add (w, "try_scrolling");
14619 #endif
14620
14621 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14622
14623 /* Compute scroll margin height in pixels. We scroll when point is
14624 within this distance from the top or bottom of the window. */
14625 if (scroll_margin > 0)
14626 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14627 * FRAME_LINE_HEIGHT (f);
14628 else
14629 this_scroll_margin = 0;
14630
14631 /* Force arg_scroll_conservatively to have a reasonable value, to
14632 avoid scrolling too far away with slow move_it_* functions. Note
14633 that the user can supply scroll-conservatively equal to
14634 `most-positive-fixnum', which can be larger than INT_MAX. */
14635 if (arg_scroll_conservatively > scroll_limit)
14636 {
14637 arg_scroll_conservatively = scroll_limit + 1;
14638 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14639 }
14640 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14641 /* Compute how much we should try to scroll maximally to bring
14642 point into view. */
14643 scroll_max = (max (scroll_step,
14644 max (arg_scroll_conservatively, temp_scroll_step))
14645 * FRAME_LINE_HEIGHT (f));
14646 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14647 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14648 /* We're trying to scroll because of aggressive scrolling but no
14649 scroll_step is set. Choose an arbitrary one. */
14650 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14651 else
14652 scroll_max = 0;
14653
14654 too_near_end:
14655
14656 /* Decide whether to scroll down. */
14657 if (PT > CHARPOS (startp))
14658 {
14659 int scroll_margin_y;
14660
14661 /* Compute the pixel ypos of the scroll margin, then move IT to
14662 either that ypos or PT, whichever comes first. */
14663 start_display (&it, w, startp);
14664 scroll_margin_y = it.last_visible_y - this_scroll_margin
14665 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14666 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14667 (MOVE_TO_POS | MOVE_TO_Y));
14668
14669 if (PT > CHARPOS (it.current.pos))
14670 {
14671 int y0 = line_bottom_y (&it);
14672 /* Compute how many pixels below window bottom to stop searching
14673 for PT. This avoids costly search for PT that is far away if
14674 the user limited scrolling by a small number of lines, but
14675 always finds PT if scroll_conservatively is set to a large
14676 number, such as most-positive-fixnum. */
14677 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14678 int y_to_move = it.last_visible_y + slack;
14679
14680 /* Compute the distance from the scroll margin to PT or to
14681 the scroll limit, whichever comes first. This should
14682 include the height of the cursor line, to make that line
14683 fully visible. */
14684 move_it_to (&it, PT, -1, y_to_move,
14685 -1, MOVE_TO_POS | MOVE_TO_Y);
14686 dy = line_bottom_y (&it) - y0;
14687
14688 if (dy > scroll_max)
14689 return SCROLLING_FAILED;
14690
14691 if (dy > 0)
14692 scroll_down_p = 1;
14693 }
14694 }
14695
14696 if (scroll_down_p)
14697 {
14698 /* Point is in or below the bottom scroll margin, so move the
14699 window start down. If scrolling conservatively, move it just
14700 enough down to make point visible. If scroll_step is set,
14701 move it down by scroll_step. */
14702 if (arg_scroll_conservatively)
14703 amount_to_scroll
14704 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14705 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14706 else if (scroll_step || temp_scroll_step)
14707 amount_to_scroll = scroll_max;
14708 else
14709 {
14710 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14711 height = WINDOW_BOX_TEXT_HEIGHT (w);
14712 if (NUMBERP (aggressive))
14713 {
14714 double float_amount = XFLOATINT (aggressive) * height;
14715 amount_to_scroll = float_amount;
14716 if (amount_to_scroll == 0 && float_amount > 0)
14717 amount_to_scroll = 1;
14718 /* Don't let point enter the scroll margin near top of
14719 the window. */
14720 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14721 amount_to_scroll = height - 2*this_scroll_margin + dy;
14722 }
14723 }
14724
14725 if (amount_to_scroll <= 0)
14726 return SCROLLING_FAILED;
14727
14728 start_display (&it, w, startp);
14729 if (arg_scroll_conservatively <= scroll_limit)
14730 move_it_vertically (&it, amount_to_scroll);
14731 else
14732 {
14733 /* Extra precision for users who set scroll-conservatively
14734 to a large number: make sure the amount we scroll
14735 the window start is never less than amount_to_scroll,
14736 which was computed as distance from window bottom to
14737 point. This matters when lines at window top and lines
14738 below window bottom have different height. */
14739 struct it it1;
14740 void *it1data = NULL;
14741 /* We use a temporary it1 because line_bottom_y can modify
14742 its argument, if it moves one line down; see there. */
14743 int start_y;
14744
14745 SAVE_IT (it1, it, it1data);
14746 start_y = line_bottom_y (&it1);
14747 do {
14748 RESTORE_IT (&it, &it, it1data);
14749 move_it_by_lines (&it, 1);
14750 SAVE_IT (it1, it, it1data);
14751 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14752 }
14753
14754 /* If STARTP is unchanged, move it down another screen line. */
14755 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14756 move_it_by_lines (&it, 1);
14757 startp = it.current.pos;
14758 }
14759 else
14760 {
14761 struct text_pos scroll_margin_pos = startp;
14762
14763 /* See if point is inside the scroll margin at the top of the
14764 window. */
14765 if (this_scroll_margin)
14766 {
14767 start_display (&it, w, startp);
14768 move_it_vertically (&it, this_scroll_margin);
14769 scroll_margin_pos = it.current.pos;
14770 }
14771
14772 if (PT < CHARPOS (scroll_margin_pos))
14773 {
14774 /* Point is in the scroll margin at the top of the window or
14775 above what is displayed in the window. */
14776 int y0, y_to_move;
14777
14778 /* Compute the vertical distance from PT to the scroll
14779 margin position. Move as far as scroll_max allows, or
14780 one screenful, or 10 screen lines, whichever is largest.
14781 Give up if distance is greater than scroll_max. */
14782 SET_TEXT_POS (pos, PT, PT_BYTE);
14783 start_display (&it, w, pos);
14784 y0 = it.current_y;
14785 y_to_move = max (it.last_visible_y,
14786 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14787 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14788 y_to_move, -1,
14789 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14790 dy = it.current_y - y0;
14791 if (dy > scroll_max)
14792 return SCROLLING_FAILED;
14793
14794 /* Compute new window start. */
14795 start_display (&it, w, startp);
14796
14797 if (arg_scroll_conservatively)
14798 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14799 max (scroll_step, temp_scroll_step));
14800 else if (scroll_step || temp_scroll_step)
14801 amount_to_scroll = scroll_max;
14802 else
14803 {
14804 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14805 height = WINDOW_BOX_TEXT_HEIGHT (w);
14806 if (NUMBERP (aggressive))
14807 {
14808 double float_amount = XFLOATINT (aggressive) * height;
14809 amount_to_scroll = float_amount;
14810 if (amount_to_scroll == 0 && float_amount > 0)
14811 amount_to_scroll = 1;
14812 amount_to_scroll -=
14813 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14814 /* Don't let point enter the scroll margin near
14815 bottom of the window. */
14816 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14817 amount_to_scroll = height - 2*this_scroll_margin + dy;
14818 }
14819 }
14820
14821 if (amount_to_scroll <= 0)
14822 return SCROLLING_FAILED;
14823
14824 move_it_vertically_backward (&it, amount_to_scroll);
14825 startp = it.current.pos;
14826 }
14827 }
14828
14829 /* Run window scroll functions. */
14830 startp = run_window_scroll_functions (window, startp);
14831
14832 /* Display the window. Give up if new fonts are loaded, or if point
14833 doesn't appear. */
14834 if (!try_window (window, startp, 0))
14835 rc = SCROLLING_NEED_LARGER_MATRICES;
14836 else if (w->cursor.vpos < 0)
14837 {
14838 clear_glyph_matrix (w->desired_matrix);
14839 rc = SCROLLING_FAILED;
14840 }
14841 else
14842 {
14843 /* Maybe forget recorded base line for line number display. */
14844 if (!just_this_one_p
14845 || current_buffer->clip_changed
14846 || BEG_UNCHANGED < CHARPOS (startp))
14847 w->base_line_number = Qnil;
14848
14849 /* If cursor ends up on a partially visible line,
14850 treat that as being off the bottom of the screen. */
14851 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14852 /* It's possible that the cursor is on the first line of the
14853 buffer, which is partially obscured due to a vscroll
14854 (Bug#7537). In that case, avoid looping forever . */
14855 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14856 {
14857 clear_glyph_matrix (w->desired_matrix);
14858 ++extra_scroll_margin_lines;
14859 goto too_near_end;
14860 }
14861 rc = SCROLLING_SUCCESS;
14862 }
14863
14864 return rc;
14865 }
14866
14867
14868 /* Compute a suitable window start for window W if display of W starts
14869 on a continuation line. Value is non-zero if a new window start
14870 was computed.
14871
14872 The new window start will be computed, based on W's width, starting
14873 from the start of the continued line. It is the start of the
14874 screen line with the minimum distance from the old start W->start. */
14875
14876 static int
14877 compute_window_start_on_continuation_line (struct window *w)
14878 {
14879 struct text_pos pos, start_pos;
14880 int window_start_changed_p = 0;
14881
14882 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14883
14884 /* If window start is on a continuation line... Window start may be
14885 < BEGV in case there's invisible text at the start of the
14886 buffer (M-x rmail, for example). */
14887 if (CHARPOS (start_pos) > BEGV
14888 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14889 {
14890 struct it it;
14891 struct glyph_row *row;
14892
14893 /* Handle the case that the window start is out of range. */
14894 if (CHARPOS (start_pos) < BEGV)
14895 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14896 else if (CHARPOS (start_pos) > ZV)
14897 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14898
14899 /* Find the start of the continued line. This should be fast
14900 because scan_buffer is fast (newline cache). */
14901 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14902 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14903 row, DEFAULT_FACE_ID);
14904 reseat_at_previous_visible_line_start (&it);
14905
14906 /* If the line start is "too far" away from the window start,
14907 say it takes too much time to compute a new window start. */
14908 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14909 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14910 {
14911 int min_distance, distance;
14912
14913 /* Move forward by display lines to find the new window
14914 start. If window width was enlarged, the new start can
14915 be expected to be > the old start. If window width was
14916 decreased, the new window start will be < the old start.
14917 So, we're looking for the display line start with the
14918 minimum distance from the old window start. */
14919 pos = it.current.pos;
14920 min_distance = INFINITY;
14921 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14922 distance < min_distance)
14923 {
14924 min_distance = distance;
14925 pos = it.current.pos;
14926 move_it_by_lines (&it, 1);
14927 }
14928
14929 /* Set the window start there. */
14930 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14931 window_start_changed_p = 1;
14932 }
14933 }
14934
14935 return window_start_changed_p;
14936 }
14937
14938
14939 /* Try cursor movement in case text has not changed in window WINDOW,
14940 with window start STARTP. Value is
14941
14942 CURSOR_MOVEMENT_SUCCESS if successful
14943
14944 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14945
14946 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14947 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14948 we want to scroll as if scroll-step were set to 1. See the code.
14949
14950 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14951 which case we have to abort this redisplay, and adjust matrices
14952 first. */
14953
14954 enum
14955 {
14956 CURSOR_MOVEMENT_SUCCESS,
14957 CURSOR_MOVEMENT_CANNOT_BE_USED,
14958 CURSOR_MOVEMENT_MUST_SCROLL,
14959 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14960 };
14961
14962 static int
14963 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14964 {
14965 struct window *w = XWINDOW (window);
14966 struct frame *f = XFRAME (w->frame);
14967 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14968
14969 #ifdef GLYPH_DEBUG
14970 if (inhibit_try_cursor_movement)
14971 return rc;
14972 #endif
14973
14974 /* Previously, there was a check for Lisp integer in the
14975 if-statement below. Now, this field is converted to
14976 ptrdiff_t, thus zero means invalid position in a buffer. */
14977 eassert (w->last_point > 0);
14978
14979 /* Handle case where text has not changed, only point, and it has
14980 not moved off the frame. */
14981 if (/* Point may be in this window. */
14982 PT >= CHARPOS (startp)
14983 /* Selective display hasn't changed. */
14984 && !current_buffer->clip_changed
14985 /* Function force-mode-line-update is used to force a thorough
14986 redisplay. It sets either windows_or_buffers_changed or
14987 update_mode_lines. So don't take a shortcut here for these
14988 cases. */
14989 && !update_mode_lines
14990 && !windows_or_buffers_changed
14991 && !cursor_type_changed
14992 /* Can't use this case if highlighting a region. When a
14993 region exists, cursor movement has to do more than just
14994 set the cursor. */
14995 && !(!NILP (Vtransient_mark_mode)
14996 && !NILP (BVAR (current_buffer, mark_active)))
14997 && NILP (w->region_showing)
14998 && NILP (Vshow_trailing_whitespace)
14999 /* This code is not used for mini-buffer for the sake of the case
15000 of redisplaying to replace an echo area message; since in
15001 that case the mini-buffer contents per se are usually
15002 unchanged. This code is of no real use in the mini-buffer
15003 since the handling of this_line_start_pos, etc., in redisplay
15004 handles the same cases. */
15005 && !EQ (window, minibuf_window)
15006 /* When splitting windows or for new windows, it happens that
15007 redisplay is called with a nil window_end_vpos or one being
15008 larger than the window. This should really be fixed in
15009 window.c. I don't have this on my list, now, so we do
15010 approximately the same as the old redisplay code. --gerd. */
15011 && INTEGERP (w->window_end_vpos)
15012 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15013 && (FRAME_WINDOW_P (f)
15014 || !overlay_arrow_in_current_buffer_p ()))
15015 {
15016 int this_scroll_margin, top_scroll_margin;
15017 struct glyph_row *row = NULL;
15018
15019 #ifdef GLYPH_DEBUG
15020 debug_method_add (w, "cursor movement");
15021 #endif
15022
15023 /* Scroll if point within this distance from the top or bottom
15024 of the window. This is a pixel value. */
15025 if (scroll_margin > 0)
15026 {
15027 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15028 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15029 }
15030 else
15031 this_scroll_margin = 0;
15032
15033 top_scroll_margin = this_scroll_margin;
15034 if (WINDOW_WANTS_HEADER_LINE_P (w))
15035 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15036
15037 /* Start with the row the cursor was displayed during the last
15038 not paused redisplay. Give up if that row is not valid. */
15039 if (w->last_cursor.vpos < 0
15040 || w->last_cursor.vpos >= w->current_matrix->nrows)
15041 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15042 else
15043 {
15044 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15045 if (row->mode_line_p)
15046 ++row;
15047 if (!row->enabled_p)
15048 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15049 }
15050
15051 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15052 {
15053 int scroll_p = 0, must_scroll = 0;
15054 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15055
15056 if (PT > w->last_point)
15057 {
15058 /* Point has moved forward. */
15059 while (MATRIX_ROW_END_CHARPOS (row) < PT
15060 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15061 {
15062 eassert (row->enabled_p);
15063 ++row;
15064 }
15065
15066 /* If the end position of a row equals the start
15067 position of the next row, and PT is at that position,
15068 we would rather display cursor in the next line. */
15069 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15070 && MATRIX_ROW_END_CHARPOS (row) == PT
15071 && row < w->current_matrix->rows
15072 + w->current_matrix->nrows - 1
15073 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15074 && !cursor_row_p (row))
15075 ++row;
15076
15077 /* If within the scroll margin, scroll. Note that
15078 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15079 the next line would be drawn, and that
15080 this_scroll_margin can be zero. */
15081 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15082 || PT > MATRIX_ROW_END_CHARPOS (row)
15083 /* Line is completely visible last line in window
15084 and PT is to be set in the next line. */
15085 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15086 && PT == MATRIX_ROW_END_CHARPOS (row)
15087 && !row->ends_at_zv_p
15088 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15089 scroll_p = 1;
15090 }
15091 else if (PT < w->last_point)
15092 {
15093 /* Cursor has to be moved backward. Note that PT >=
15094 CHARPOS (startp) because of the outer if-statement. */
15095 while (!row->mode_line_p
15096 && (MATRIX_ROW_START_CHARPOS (row) > PT
15097 || (MATRIX_ROW_START_CHARPOS (row) == PT
15098 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15099 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15100 row > w->current_matrix->rows
15101 && (row-1)->ends_in_newline_from_string_p))))
15102 && (row->y > top_scroll_margin
15103 || CHARPOS (startp) == BEGV))
15104 {
15105 eassert (row->enabled_p);
15106 --row;
15107 }
15108
15109 /* Consider the following case: Window starts at BEGV,
15110 there is invisible, intangible text at BEGV, so that
15111 display starts at some point START > BEGV. It can
15112 happen that we are called with PT somewhere between
15113 BEGV and START. Try to handle that case. */
15114 if (row < w->current_matrix->rows
15115 || row->mode_line_p)
15116 {
15117 row = w->current_matrix->rows;
15118 if (row->mode_line_p)
15119 ++row;
15120 }
15121
15122 /* Due to newlines in overlay strings, we may have to
15123 skip forward over overlay strings. */
15124 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15125 && MATRIX_ROW_END_CHARPOS (row) == PT
15126 && !cursor_row_p (row))
15127 ++row;
15128
15129 /* If within the scroll margin, scroll. */
15130 if (row->y < top_scroll_margin
15131 && CHARPOS (startp) != BEGV)
15132 scroll_p = 1;
15133 }
15134 else
15135 {
15136 /* Cursor did not move. So don't scroll even if cursor line
15137 is partially visible, as it was so before. */
15138 rc = CURSOR_MOVEMENT_SUCCESS;
15139 }
15140
15141 if (PT < MATRIX_ROW_START_CHARPOS (row)
15142 || PT > MATRIX_ROW_END_CHARPOS (row))
15143 {
15144 /* if PT is not in the glyph row, give up. */
15145 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15146 must_scroll = 1;
15147 }
15148 else if (rc != CURSOR_MOVEMENT_SUCCESS
15149 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15150 {
15151 struct glyph_row *row1;
15152
15153 /* If rows are bidi-reordered and point moved, back up
15154 until we find a row that does not belong to a
15155 continuation line. This is because we must consider
15156 all rows of a continued line as candidates for the
15157 new cursor positioning, since row start and end
15158 positions change non-linearly with vertical position
15159 in such rows. */
15160 /* FIXME: Revisit this when glyph ``spilling'' in
15161 continuation lines' rows is implemented for
15162 bidi-reordered rows. */
15163 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15164 MATRIX_ROW_CONTINUATION_LINE_P (row);
15165 --row)
15166 {
15167 /* If we hit the beginning of the displayed portion
15168 without finding the first row of a continued
15169 line, give up. */
15170 if (row <= row1)
15171 {
15172 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15173 break;
15174 }
15175 eassert (row->enabled_p);
15176 }
15177 }
15178 if (must_scroll)
15179 ;
15180 else if (rc != CURSOR_MOVEMENT_SUCCESS
15181 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15182 /* Make sure this isn't a header line by any chance, since
15183 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15184 && !row->mode_line_p
15185 && make_cursor_line_fully_visible_p)
15186 {
15187 if (PT == MATRIX_ROW_END_CHARPOS (row)
15188 && !row->ends_at_zv_p
15189 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15190 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15191 else if (row->height > window_box_height (w))
15192 {
15193 /* If we end up in a partially visible line, let's
15194 make it fully visible, except when it's taller
15195 than the window, in which case we can't do much
15196 about it. */
15197 *scroll_step = 1;
15198 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15199 }
15200 else
15201 {
15202 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15203 if (!cursor_row_fully_visible_p (w, 0, 1))
15204 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15205 else
15206 rc = CURSOR_MOVEMENT_SUCCESS;
15207 }
15208 }
15209 else if (scroll_p)
15210 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15211 else if (rc != CURSOR_MOVEMENT_SUCCESS
15212 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15213 {
15214 /* With bidi-reordered rows, there could be more than
15215 one candidate row whose start and end positions
15216 occlude point. We need to let set_cursor_from_row
15217 find the best candidate. */
15218 /* FIXME: Revisit this when glyph ``spilling'' in
15219 continuation lines' rows is implemented for
15220 bidi-reordered rows. */
15221 int rv = 0;
15222
15223 do
15224 {
15225 int at_zv_p = 0, exact_match_p = 0;
15226
15227 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15228 && PT <= MATRIX_ROW_END_CHARPOS (row)
15229 && cursor_row_p (row))
15230 rv |= set_cursor_from_row (w, row, w->current_matrix,
15231 0, 0, 0, 0);
15232 /* As soon as we've found the exact match for point,
15233 or the first suitable row whose ends_at_zv_p flag
15234 is set, we are done. */
15235 at_zv_p =
15236 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15237 if (rv && !at_zv_p
15238 && w->cursor.hpos >= 0
15239 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15240 w->cursor.vpos))
15241 {
15242 struct glyph_row *candidate =
15243 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15244 struct glyph *g =
15245 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15246 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15247
15248 exact_match_p =
15249 (BUFFERP (g->object) && g->charpos == PT)
15250 || (INTEGERP (g->object)
15251 && (g->charpos == PT
15252 || (g->charpos == 0 && endpos - 1 == PT)));
15253 }
15254 if (rv && (at_zv_p || exact_match_p))
15255 {
15256 rc = CURSOR_MOVEMENT_SUCCESS;
15257 break;
15258 }
15259 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15260 break;
15261 ++row;
15262 }
15263 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15264 || row->continued_p)
15265 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15266 || (MATRIX_ROW_START_CHARPOS (row) == PT
15267 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15268 /* If we didn't find any candidate rows, or exited the
15269 loop before all the candidates were examined, signal
15270 to the caller that this method failed. */
15271 if (rc != CURSOR_MOVEMENT_SUCCESS
15272 && !(rv
15273 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15274 && !row->continued_p))
15275 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15276 else if (rv)
15277 rc = CURSOR_MOVEMENT_SUCCESS;
15278 }
15279 else
15280 {
15281 do
15282 {
15283 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15284 {
15285 rc = CURSOR_MOVEMENT_SUCCESS;
15286 break;
15287 }
15288 ++row;
15289 }
15290 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15291 && MATRIX_ROW_START_CHARPOS (row) == PT
15292 && cursor_row_p (row));
15293 }
15294 }
15295 }
15296
15297 return rc;
15298 }
15299
15300 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15301 static
15302 #endif
15303 void
15304 set_vertical_scroll_bar (struct window *w)
15305 {
15306 ptrdiff_t start, end, whole;
15307
15308 /* Calculate the start and end positions for the current window.
15309 At some point, it would be nice to choose between scrollbars
15310 which reflect the whole buffer size, with special markers
15311 indicating narrowing, and scrollbars which reflect only the
15312 visible region.
15313
15314 Note that mini-buffers sometimes aren't displaying any text. */
15315 if (!MINI_WINDOW_P (w)
15316 || (w == XWINDOW (minibuf_window)
15317 && NILP (echo_area_buffer[0])))
15318 {
15319 struct buffer *buf = XBUFFER (w->buffer);
15320 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15321 start = marker_position (w->start) - BUF_BEGV (buf);
15322 /* I don't think this is guaranteed to be right. For the
15323 moment, we'll pretend it is. */
15324 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15325
15326 if (end < start)
15327 end = start;
15328 if (whole < (end - start))
15329 whole = end - start;
15330 }
15331 else
15332 start = end = whole = 0;
15333
15334 /* Indicate what this scroll bar ought to be displaying now. */
15335 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15336 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15337 (w, end - start, whole, start);
15338 }
15339
15340
15341 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15342 selected_window is redisplayed.
15343
15344 We can return without actually redisplaying the window if
15345 fonts_changed_p is nonzero. In that case, redisplay_internal will
15346 retry. */
15347
15348 static void
15349 redisplay_window (Lisp_Object window, int just_this_one_p)
15350 {
15351 struct window *w = XWINDOW (window);
15352 struct frame *f = XFRAME (w->frame);
15353 struct buffer *buffer = XBUFFER (w->buffer);
15354 struct buffer *old = current_buffer;
15355 struct text_pos lpoint, opoint, startp;
15356 int update_mode_line;
15357 int tem;
15358 struct it it;
15359 /* Record it now because it's overwritten. */
15360 int current_matrix_up_to_date_p = 0;
15361 int used_current_matrix_p = 0;
15362 /* This is less strict than current_matrix_up_to_date_p.
15363 It indicates that the buffer contents and narrowing are unchanged. */
15364 int buffer_unchanged_p = 0;
15365 int temp_scroll_step = 0;
15366 ptrdiff_t count = SPECPDL_INDEX ();
15367 int rc;
15368 int centering_position = -1;
15369 int last_line_misfit = 0;
15370 ptrdiff_t beg_unchanged, end_unchanged;
15371
15372 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15373 opoint = lpoint;
15374
15375 /* W must be a leaf window here. */
15376 eassert (!NILP (w->buffer));
15377 #ifdef GLYPH_DEBUG
15378 *w->desired_matrix->method = 0;
15379 #endif
15380
15381 restart:
15382 reconsider_clip_changes (w, buffer);
15383
15384 /* Has the mode line to be updated? */
15385 update_mode_line = (w->update_mode_line
15386 || update_mode_lines
15387 || buffer->clip_changed
15388 || buffer->prevent_redisplay_optimizations_p);
15389
15390 if (MINI_WINDOW_P (w))
15391 {
15392 if (w == XWINDOW (echo_area_window)
15393 && !NILP (echo_area_buffer[0]))
15394 {
15395 if (update_mode_line)
15396 /* We may have to update a tty frame's menu bar or a
15397 tool-bar. Example `M-x C-h C-h C-g'. */
15398 goto finish_menu_bars;
15399 else
15400 /* We've already displayed the echo area glyphs in this window. */
15401 goto finish_scroll_bars;
15402 }
15403 else if ((w != XWINDOW (minibuf_window)
15404 || minibuf_level == 0)
15405 /* When buffer is nonempty, redisplay window normally. */
15406 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15407 /* Quail displays non-mini buffers in minibuffer window.
15408 In that case, redisplay the window normally. */
15409 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15410 {
15411 /* W is a mini-buffer window, but it's not active, so clear
15412 it. */
15413 int yb = window_text_bottom_y (w);
15414 struct glyph_row *row;
15415 int y;
15416
15417 for (y = 0, row = w->desired_matrix->rows;
15418 y < yb;
15419 y += row->height, ++row)
15420 blank_row (w, row, y);
15421 goto finish_scroll_bars;
15422 }
15423
15424 clear_glyph_matrix (w->desired_matrix);
15425 }
15426
15427 /* Otherwise set up data on this window; select its buffer and point
15428 value. */
15429 /* Really select the buffer, for the sake of buffer-local
15430 variables. */
15431 set_buffer_internal_1 (XBUFFER (w->buffer));
15432
15433 current_matrix_up_to_date_p
15434 = (!NILP (w->window_end_valid)
15435 && !current_buffer->clip_changed
15436 && !current_buffer->prevent_redisplay_optimizations_p
15437 && w->last_modified >= MODIFF
15438 && w->last_overlay_modified >= OVERLAY_MODIFF);
15439
15440 /* Run the window-bottom-change-functions
15441 if it is possible that the text on the screen has changed
15442 (either due to modification of the text, or any other reason). */
15443 if (!current_matrix_up_to_date_p
15444 && !NILP (Vwindow_text_change_functions))
15445 {
15446 safe_run_hooks (Qwindow_text_change_functions);
15447 goto restart;
15448 }
15449
15450 beg_unchanged = BEG_UNCHANGED;
15451 end_unchanged = END_UNCHANGED;
15452
15453 SET_TEXT_POS (opoint, PT, PT_BYTE);
15454
15455 specbind (Qinhibit_point_motion_hooks, Qt);
15456
15457 buffer_unchanged_p
15458 = (!NILP (w->window_end_valid)
15459 && !current_buffer->clip_changed
15460 && w->last_modified >= MODIFF
15461 && w->last_overlay_modified >= OVERLAY_MODIFF);
15462
15463 /* When windows_or_buffers_changed is non-zero, we can't rely on
15464 the window end being valid, so set it to nil there. */
15465 if (windows_or_buffers_changed)
15466 {
15467 /* If window starts on a continuation line, maybe adjust the
15468 window start in case the window's width changed. */
15469 if (XMARKER (w->start)->buffer == current_buffer)
15470 compute_window_start_on_continuation_line (w);
15471
15472 w->window_end_valid = Qnil;
15473 }
15474
15475 /* Some sanity checks. */
15476 CHECK_WINDOW_END (w);
15477 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15478 abort ();
15479 if (BYTEPOS (opoint) < CHARPOS (opoint))
15480 abort ();
15481
15482 /* If %c is in mode line, update it if needed. */
15483 if (!NILP (w->column_number_displayed)
15484 /* This alternative quickly identifies a common case
15485 where no change is needed. */
15486 && !(PT == w->last_point
15487 && w->last_modified >= MODIFF
15488 && w->last_overlay_modified >= OVERLAY_MODIFF)
15489 && (XFASTINT (w->column_number_displayed) != current_column ()))
15490 update_mode_line = 1;
15491
15492 /* Count number of windows showing the selected buffer. An indirect
15493 buffer counts as its base buffer. */
15494 if (!just_this_one_p)
15495 {
15496 struct buffer *current_base, *window_base;
15497 current_base = current_buffer;
15498 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15499 if (current_base->base_buffer)
15500 current_base = current_base->base_buffer;
15501 if (window_base->base_buffer)
15502 window_base = window_base->base_buffer;
15503 if (current_base == window_base)
15504 buffer_shared++;
15505 }
15506
15507 /* Point refers normally to the selected window. For any other
15508 window, set up appropriate value. */
15509 if (!EQ (window, selected_window))
15510 {
15511 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15512 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15513 if (new_pt < BEGV)
15514 {
15515 new_pt = BEGV;
15516 new_pt_byte = BEGV_BYTE;
15517 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15518 }
15519 else if (new_pt > (ZV - 1))
15520 {
15521 new_pt = ZV;
15522 new_pt_byte = ZV_BYTE;
15523 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15524 }
15525
15526 /* We don't use SET_PT so that the point-motion hooks don't run. */
15527 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15528 }
15529
15530 /* If any of the character widths specified in the display table
15531 have changed, invalidate the width run cache. It's true that
15532 this may be a bit late to catch such changes, but the rest of
15533 redisplay goes (non-fatally) haywire when the display table is
15534 changed, so why should we worry about doing any better? */
15535 if (current_buffer->width_run_cache)
15536 {
15537 struct Lisp_Char_Table *disptab = buffer_display_table ();
15538
15539 if (! disptab_matches_widthtab (disptab,
15540 XVECTOR (BVAR (current_buffer, width_table))))
15541 {
15542 invalidate_region_cache (current_buffer,
15543 current_buffer->width_run_cache,
15544 BEG, Z);
15545 recompute_width_table (current_buffer, disptab);
15546 }
15547 }
15548
15549 /* If window-start is screwed up, choose a new one. */
15550 if (XMARKER (w->start)->buffer != current_buffer)
15551 goto recenter;
15552
15553 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15554
15555 /* If someone specified a new starting point but did not insist,
15556 check whether it can be used. */
15557 if (w->optional_new_start
15558 && CHARPOS (startp) >= BEGV
15559 && CHARPOS (startp) <= ZV)
15560 {
15561 w->optional_new_start = 0;
15562 start_display (&it, w, startp);
15563 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15564 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15565 if (IT_CHARPOS (it) == PT)
15566 w->force_start = 1;
15567 /* IT may overshoot PT if text at PT is invisible. */
15568 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15569 w->force_start = 1;
15570 }
15571
15572 force_start:
15573
15574 /* Handle case where place to start displaying has been specified,
15575 unless the specified location is outside the accessible range. */
15576 if (w->force_start || w->frozen_window_start_p)
15577 {
15578 /* We set this later on if we have to adjust point. */
15579 int new_vpos = -1;
15580
15581 w->force_start = 0;
15582 w->vscroll = 0;
15583 w->window_end_valid = Qnil;
15584
15585 /* Forget any recorded base line for line number display. */
15586 if (!buffer_unchanged_p)
15587 w->base_line_number = Qnil;
15588
15589 /* Redisplay the mode line. Select the buffer properly for that.
15590 Also, run the hook window-scroll-functions
15591 because we have scrolled. */
15592 /* Note, we do this after clearing force_start because
15593 if there's an error, it is better to forget about force_start
15594 than to get into an infinite loop calling the hook functions
15595 and having them get more errors. */
15596 if (!update_mode_line
15597 || ! NILP (Vwindow_scroll_functions))
15598 {
15599 update_mode_line = 1;
15600 w->update_mode_line = 1;
15601 startp = run_window_scroll_functions (window, startp);
15602 }
15603
15604 w->last_modified = 0;
15605 w->last_overlay_modified = 0;
15606 if (CHARPOS (startp) < BEGV)
15607 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15608 else if (CHARPOS (startp) > ZV)
15609 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15610
15611 /* Redisplay, then check if cursor has been set during the
15612 redisplay. Give up if new fonts were loaded. */
15613 /* We used to issue a CHECK_MARGINS argument to try_window here,
15614 but this causes scrolling to fail when point begins inside
15615 the scroll margin (bug#148) -- cyd */
15616 if (!try_window (window, startp, 0))
15617 {
15618 w->force_start = 1;
15619 clear_glyph_matrix (w->desired_matrix);
15620 goto need_larger_matrices;
15621 }
15622
15623 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15624 {
15625 /* If point does not appear, try to move point so it does
15626 appear. The desired matrix has been built above, so we
15627 can use it here. */
15628 new_vpos = window_box_height (w) / 2;
15629 }
15630
15631 if (!cursor_row_fully_visible_p (w, 0, 0))
15632 {
15633 /* Point does appear, but on a line partly visible at end of window.
15634 Move it back to a fully-visible line. */
15635 new_vpos = window_box_height (w);
15636 }
15637
15638 /* If we need to move point for either of the above reasons,
15639 now actually do it. */
15640 if (new_vpos >= 0)
15641 {
15642 struct glyph_row *row;
15643
15644 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15645 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15646 ++row;
15647
15648 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15649 MATRIX_ROW_START_BYTEPOS (row));
15650
15651 if (w != XWINDOW (selected_window))
15652 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15653 else if (current_buffer == old)
15654 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15655
15656 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15657
15658 /* If we are highlighting the region, then we just changed
15659 the region, so redisplay to show it. */
15660 if (!NILP (Vtransient_mark_mode)
15661 && !NILP (BVAR (current_buffer, mark_active)))
15662 {
15663 clear_glyph_matrix (w->desired_matrix);
15664 if (!try_window (window, startp, 0))
15665 goto need_larger_matrices;
15666 }
15667 }
15668
15669 #ifdef GLYPH_DEBUG
15670 debug_method_add (w, "forced window start");
15671 #endif
15672 goto done;
15673 }
15674
15675 /* Handle case where text has not changed, only point, and it has
15676 not moved off the frame, and we are not retrying after hscroll.
15677 (current_matrix_up_to_date_p is nonzero when retrying.) */
15678 if (current_matrix_up_to_date_p
15679 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15680 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15681 {
15682 switch (rc)
15683 {
15684 case CURSOR_MOVEMENT_SUCCESS:
15685 used_current_matrix_p = 1;
15686 goto done;
15687
15688 case CURSOR_MOVEMENT_MUST_SCROLL:
15689 goto try_to_scroll;
15690
15691 default:
15692 abort ();
15693 }
15694 }
15695 /* If current starting point was originally the beginning of a line
15696 but no longer is, find a new starting point. */
15697 else if (w->start_at_line_beg
15698 && !(CHARPOS (startp) <= BEGV
15699 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15700 {
15701 #ifdef GLYPH_DEBUG
15702 debug_method_add (w, "recenter 1");
15703 #endif
15704 goto recenter;
15705 }
15706
15707 /* Try scrolling with try_window_id. Value is > 0 if update has
15708 been done, it is -1 if we know that the same window start will
15709 not work. It is 0 if unsuccessful for some other reason. */
15710 else if ((tem = try_window_id (w)) != 0)
15711 {
15712 #ifdef GLYPH_DEBUG
15713 debug_method_add (w, "try_window_id %d", tem);
15714 #endif
15715
15716 if (fonts_changed_p)
15717 goto need_larger_matrices;
15718 if (tem > 0)
15719 goto done;
15720
15721 /* Otherwise try_window_id has returned -1 which means that we
15722 don't want the alternative below this comment to execute. */
15723 }
15724 else if (CHARPOS (startp) >= BEGV
15725 && CHARPOS (startp) <= ZV
15726 && PT >= CHARPOS (startp)
15727 && (CHARPOS (startp) < ZV
15728 /* Avoid starting at end of buffer. */
15729 || CHARPOS (startp) == BEGV
15730 || (w->last_modified >= MODIFF
15731 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15732 {
15733 int d1, d2, d3, d4, d5, d6;
15734
15735 /* If first window line is a continuation line, and window start
15736 is inside the modified region, but the first change is before
15737 current window start, we must select a new window start.
15738
15739 However, if this is the result of a down-mouse event (e.g. by
15740 extending the mouse-drag-overlay), we don't want to select a
15741 new window start, since that would change the position under
15742 the mouse, resulting in an unwanted mouse-movement rather
15743 than a simple mouse-click. */
15744 if (!w->start_at_line_beg
15745 && NILP (do_mouse_tracking)
15746 && CHARPOS (startp) > BEGV
15747 && CHARPOS (startp) > BEG + beg_unchanged
15748 && CHARPOS (startp) <= Z - end_unchanged
15749 /* Even if w->start_at_line_beg is nil, a new window may
15750 start at a line_beg, since that's how set_buffer_window
15751 sets it. So, we need to check the return value of
15752 compute_window_start_on_continuation_line. (See also
15753 bug#197). */
15754 && XMARKER (w->start)->buffer == current_buffer
15755 && compute_window_start_on_continuation_line (w)
15756 /* It doesn't make sense to force the window start like we
15757 do at label force_start if it is already known that point
15758 will not be visible in the resulting window, because
15759 doing so will move point from its correct position
15760 instead of scrolling the window to bring point into view.
15761 See bug#9324. */
15762 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15763 {
15764 w->force_start = 1;
15765 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15766 goto force_start;
15767 }
15768
15769 #ifdef GLYPH_DEBUG
15770 debug_method_add (w, "same window start");
15771 #endif
15772
15773 /* Try to redisplay starting at same place as before.
15774 If point has not moved off frame, accept the results. */
15775 if (!current_matrix_up_to_date_p
15776 /* Don't use try_window_reusing_current_matrix in this case
15777 because a window scroll function can have changed the
15778 buffer. */
15779 || !NILP (Vwindow_scroll_functions)
15780 || MINI_WINDOW_P (w)
15781 || !(used_current_matrix_p
15782 = try_window_reusing_current_matrix (w)))
15783 {
15784 IF_DEBUG (debug_method_add (w, "1"));
15785 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15786 /* -1 means we need to scroll.
15787 0 means we need new matrices, but fonts_changed_p
15788 is set in that case, so we will detect it below. */
15789 goto try_to_scroll;
15790 }
15791
15792 if (fonts_changed_p)
15793 goto need_larger_matrices;
15794
15795 if (w->cursor.vpos >= 0)
15796 {
15797 if (!just_this_one_p
15798 || current_buffer->clip_changed
15799 || BEG_UNCHANGED < CHARPOS (startp))
15800 /* Forget any recorded base line for line number display. */
15801 w->base_line_number = Qnil;
15802
15803 if (!cursor_row_fully_visible_p (w, 1, 0))
15804 {
15805 clear_glyph_matrix (w->desired_matrix);
15806 last_line_misfit = 1;
15807 }
15808 /* Drop through and scroll. */
15809 else
15810 goto done;
15811 }
15812 else
15813 clear_glyph_matrix (w->desired_matrix);
15814 }
15815
15816 try_to_scroll:
15817
15818 w->last_modified = 0;
15819 w->last_overlay_modified = 0;
15820
15821 /* Redisplay the mode line. Select the buffer properly for that. */
15822 if (!update_mode_line)
15823 {
15824 update_mode_line = 1;
15825 w->update_mode_line = 1;
15826 }
15827
15828 /* Try to scroll by specified few lines. */
15829 if ((scroll_conservatively
15830 || emacs_scroll_step
15831 || temp_scroll_step
15832 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15833 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15834 && CHARPOS (startp) >= BEGV
15835 && CHARPOS (startp) <= ZV)
15836 {
15837 /* The function returns -1 if new fonts were loaded, 1 if
15838 successful, 0 if not successful. */
15839 int ss = try_scrolling (window, just_this_one_p,
15840 scroll_conservatively,
15841 emacs_scroll_step,
15842 temp_scroll_step, last_line_misfit);
15843 switch (ss)
15844 {
15845 case SCROLLING_SUCCESS:
15846 goto done;
15847
15848 case SCROLLING_NEED_LARGER_MATRICES:
15849 goto need_larger_matrices;
15850
15851 case SCROLLING_FAILED:
15852 break;
15853
15854 default:
15855 abort ();
15856 }
15857 }
15858
15859 /* Finally, just choose a place to start which positions point
15860 according to user preferences. */
15861
15862 recenter:
15863
15864 #ifdef GLYPH_DEBUG
15865 debug_method_add (w, "recenter");
15866 #endif
15867
15868 /* w->vscroll = 0; */
15869
15870 /* Forget any previously recorded base line for line number display. */
15871 if (!buffer_unchanged_p)
15872 w->base_line_number = Qnil;
15873
15874 /* Determine the window start relative to point. */
15875 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15876 it.current_y = it.last_visible_y;
15877 if (centering_position < 0)
15878 {
15879 int margin =
15880 scroll_margin > 0
15881 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15882 : 0;
15883 ptrdiff_t margin_pos = CHARPOS (startp);
15884 Lisp_Object aggressive;
15885 int scrolling_up;
15886
15887 /* If there is a scroll margin at the top of the window, find
15888 its character position. */
15889 if (margin
15890 /* Cannot call start_display if startp is not in the
15891 accessible region of the buffer. This can happen when we
15892 have just switched to a different buffer and/or changed
15893 its restriction. In that case, startp is initialized to
15894 the character position 1 (BEGV) because we did not yet
15895 have chance to display the buffer even once. */
15896 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15897 {
15898 struct it it1;
15899 void *it1data = NULL;
15900
15901 SAVE_IT (it1, it, it1data);
15902 start_display (&it1, w, startp);
15903 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15904 margin_pos = IT_CHARPOS (it1);
15905 RESTORE_IT (&it, &it, it1data);
15906 }
15907 scrolling_up = PT > margin_pos;
15908 aggressive =
15909 scrolling_up
15910 ? BVAR (current_buffer, scroll_up_aggressively)
15911 : BVAR (current_buffer, scroll_down_aggressively);
15912
15913 if (!MINI_WINDOW_P (w)
15914 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15915 {
15916 int pt_offset = 0;
15917
15918 /* Setting scroll-conservatively overrides
15919 scroll-*-aggressively. */
15920 if (!scroll_conservatively && NUMBERP (aggressive))
15921 {
15922 double float_amount = XFLOATINT (aggressive);
15923
15924 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15925 if (pt_offset == 0 && float_amount > 0)
15926 pt_offset = 1;
15927 if (pt_offset && margin > 0)
15928 margin -= 1;
15929 }
15930 /* Compute how much to move the window start backward from
15931 point so that point will be displayed where the user
15932 wants it. */
15933 if (scrolling_up)
15934 {
15935 centering_position = it.last_visible_y;
15936 if (pt_offset)
15937 centering_position -= pt_offset;
15938 centering_position -=
15939 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15940 + WINDOW_HEADER_LINE_HEIGHT (w);
15941 /* Don't let point enter the scroll margin near top of
15942 the window. */
15943 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15944 centering_position = margin * FRAME_LINE_HEIGHT (f);
15945 }
15946 else
15947 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15948 }
15949 else
15950 /* Set the window start half the height of the window backward
15951 from point. */
15952 centering_position = window_box_height (w) / 2;
15953 }
15954 move_it_vertically_backward (&it, centering_position);
15955
15956 eassert (IT_CHARPOS (it) >= BEGV);
15957
15958 /* The function move_it_vertically_backward may move over more
15959 than the specified y-distance. If it->w is small, e.g. a
15960 mini-buffer window, we may end up in front of the window's
15961 display area. Start displaying at the start of the line
15962 containing PT in this case. */
15963 if (it.current_y <= 0)
15964 {
15965 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15966 move_it_vertically_backward (&it, 0);
15967 it.current_y = 0;
15968 }
15969
15970 it.current_x = it.hpos = 0;
15971
15972 /* Set the window start position here explicitly, to avoid an
15973 infinite loop in case the functions in window-scroll-functions
15974 get errors. */
15975 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15976
15977 /* Run scroll hooks. */
15978 startp = run_window_scroll_functions (window, it.current.pos);
15979
15980 /* Redisplay the window. */
15981 if (!current_matrix_up_to_date_p
15982 || windows_or_buffers_changed
15983 || cursor_type_changed
15984 /* Don't use try_window_reusing_current_matrix in this case
15985 because it can have changed the buffer. */
15986 || !NILP (Vwindow_scroll_functions)
15987 || !just_this_one_p
15988 || MINI_WINDOW_P (w)
15989 || !(used_current_matrix_p
15990 = try_window_reusing_current_matrix (w)))
15991 try_window (window, startp, 0);
15992
15993 /* If new fonts have been loaded (due to fontsets), give up. We
15994 have to start a new redisplay since we need to re-adjust glyph
15995 matrices. */
15996 if (fonts_changed_p)
15997 goto need_larger_matrices;
15998
15999 /* If cursor did not appear assume that the middle of the window is
16000 in the first line of the window. Do it again with the next line.
16001 (Imagine a window of height 100, displaying two lines of height
16002 60. Moving back 50 from it->last_visible_y will end in the first
16003 line.) */
16004 if (w->cursor.vpos < 0)
16005 {
16006 if (!NILP (w->window_end_valid)
16007 && 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 margin =
16054 scroll_margin > 0
16055 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16056 : 0;
16057 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16058
16059 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16060 clear_glyph_matrix (w->desired_matrix);
16061 if (1 == try_window (window, it.current.pos,
16062 TRY_WINDOW_CHECK_MARGINS))
16063 goto done;
16064 }
16065
16066 /* If centering point failed to make the whole line visible,
16067 put point at the top instead. That has to make the whole line
16068 visible, if it can be done. */
16069 if (centering_position == 0)
16070 goto done;
16071
16072 clear_glyph_matrix (w->desired_matrix);
16073 centering_position = 0;
16074 goto recenter;
16075 }
16076
16077 done:
16078
16079 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16080 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16081 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16082
16083 /* Display the mode line, if we must. */
16084 if ((update_mode_line
16085 /* If window not full width, must redo its mode line
16086 if (a) the window to its side is being redone and
16087 (b) we do a frame-based redisplay. This is a consequence
16088 of how inverted lines are drawn in frame-based redisplay. */
16089 || (!just_this_one_p
16090 && !FRAME_WINDOW_P (f)
16091 && !WINDOW_FULL_WIDTH_P (w))
16092 /* Line number to display. */
16093 || INTEGERP (w->base_line_pos)
16094 /* Column number is displayed and different from the one displayed. */
16095 || (!NILP (w->column_number_displayed)
16096 && (XFASTINT (w->column_number_displayed) != current_column ())))
16097 /* This means that the window has a mode line. */
16098 && (WINDOW_WANTS_MODELINE_P (w)
16099 || WINDOW_WANTS_HEADER_LINE_P (w)))
16100 {
16101 display_mode_lines (w);
16102
16103 /* If mode line height has changed, arrange for a thorough
16104 immediate redisplay using the correct mode line height. */
16105 if (WINDOW_WANTS_MODELINE_P (w)
16106 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16107 {
16108 fonts_changed_p = 1;
16109 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16110 = DESIRED_MODE_LINE_HEIGHT (w);
16111 }
16112
16113 /* If header line height has changed, arrange for a thorough
16114 immediate redisplay using the correct header line height. */
16115 if (WINDOW_WANTS_HEADER_LINE_P (w)
16116 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16117 {
16118 fonts_changed_p = 1;
16119 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16120 = DESIRED_HEADER_LINE_HEIGHT (w);
16121 }
16122
16123 if (fonts_changed_p)
16124 goto need_larger_matrices;
16125 }
16126
16127 if (!line_number_displayed
16128 && !BUFFERP (w->base_line_pos))
16129 {
16130 w->base_line_pos = Qnil;
16131 w->base_line_number = Qnil;
16132 }
16133
16134 finish_menu_bars:
16135
16136 /* When we reach a frame's selected window, redo the frame's menu bar. */
16137 if (update_mode_line
16138 && EQ (FRAME_SELECTED_WINDOW (f), window))
16139 {
16140 int redisplay_menu_p = 0;
16141
16142 if (FRAME_WINDOW_P (f))
16143 {
16144 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16145 || defined (HAVE_NS) || defined (USE_GTK)
16146 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16147 #else
16148 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16149 #endif
16150 }
16151 else
16152 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16153
16154 if (redisplay_menu_p)
16155 display_menu_bar (w);
16156
16157 #ifdef HAVE_WINDOW_SYSTEM
16158 if (FRAME_WINDOW_P (f))
16159 {
16160 #if defined (USE_GTK) || defined (HAVE_NS)
16161 if (FRAME_EXTERNAL_TOOL_BAR (f))
16162 redisplay_tool_bar (f);
16163 #else
16164 if (WINDOWP (FVAR (f, tool_bar_window))
16165 && (FRAME_TOOL_BAR_LINES (f) > 0
16166 || !NILP (Vauto_resize_tool_bars))
16167 && redisplay_tool_bar (f))
16168 ignore_mouse_drag_p = 1;
16169 #endif
16170 }
16171 #endif
16172 }
16173
16174 #ifdef HAVE_WINDOW_SYSTEM
16175 if (FRAME_WINDOW_P (f)
16176 && update_window_fringes (w, (just_this_one_p
16177 || (!used_current_matrix_p && !overlay_arrow_seen)
16178 || w->pseudo_window_p)))
16179 {
16180 update_begin (f);
16181 BLOCK_INPUT;
16182 if (draw_window_fringes (w, 1))
16183 x_draw_vertical_border (w);
16184 UNBLOCK_INPUT;
16185 update_end (f);
16186 }
16187 #endif /* HAVE_WINDOW_SYSTEM */
16188
16189 /* We go to this label, with fonts_changed_p nonzero,
16190 if it is necessary to try again using larger glyph matrices.
16191 We have to redeem the scroll bar even in this case,
16192 because the loop in redisplay_internal expects that. */
16193 need_larger_matrices:
16194 ;
16195 finish_scroll_bars:
16196
16197 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16198 {
16199 /* Set the thumb's position and size. */
16200 set_vertical_scroll_bar (w);
16201
16202 /* Note that we actually used the scroll bar attached to this
16203 window, so it shouldn't be deleted at the end of redisplay. */
16204 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16205 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16206 }
16207
16208 /* Restore current_buffer and value of point in it. The window
16209 update may have changed the buffer, so first make sure `opoint'
16210 is still valid (Bug#6177). */
16211 if (CHARPOS (opoint) < BEGV)
16212 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16213 else if (CHARPOS (opoint) > ZV)
16214 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16215 else
16216 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16217
16218 set_buffer_internal_1 (old);
16219 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16220 shorter. This can be caused by log truncation in *Messages*. */
16221 if (CHARPOS (lpoint) <= ZV)
16222 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16223
16224 unbind_to (count, Qnil);
16225 }
16226
16227
16228 /* Build the complete desired matrix of WINDOW with a window start
16229 buffer position POS.
16230
16231 Value is 1 if successful. It is zero if fonts were loaded during
16232 redisplay which makes re-adjusting glyph matrices necessary, and -1
16233 if point would appear in the scroll margins.
16234 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16235 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16236 set in FLAGS.) */
16237
16238 int
16239 try_window (Lisp_Object window, struct text_pos pos, int flags)
16240 {
16241 struct window *w = XWINDOW (window);
16242 struct it it;
16243 struct glyph_row *last_text_row = NULL;
16244 struct frame *f = XFRAME (w->frame);
16245
16246 /* Make POS the new window start. */
16247 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16248
16249 /* Mark cursor position as unknown. No overlay arrow seen. */
16250 w->cursor.vpos = -1;
16251 overlay_arrow_seen = 0;
16252
16253 /* Initialize iterator and info to start at POS. */
16254 start_display (&it, w, pos);
16255
16256 /* Display all lines of W. */
16257 while (it.current_y < it.last_visible_y)
16258 {
16259 if (display_line (&it))
16260 last_text_row = it.glyph_row - 1;
16261 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16262 return 0;
16263 }
16264
16265 /* Don't let the cursor end in the scroll margins. */
16266 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16267 && !MINI_WINDOW_P (w))
16268 {
16269 int this_scroll_margin;
16270
16271 if (scroll_margin > 0)
16272 {
16273 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16274 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16275 }
16276 else
16277 this_scroll_margin = 0;
16278
16279 if ((w->cursor.y >= 0 /* not vscrolled */
16280 && w->cursor.y < this_scroll_margin
16281 && CHARPOS (pos) > BEGV
16282 && IT_CHARPOS (it) < ZV)
16283 /* rms: considering make_cursor_line_fully_visible_p here
16284 seems to give wrong results. We don't want to recenter
16285 when the last line is partly visible, we want to allow
16286 that case to be handled in the usual way. */
16287 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16288 {
16289 w->cursor.vpos = -1;
16290 clear_glyph_matrix (w->desired_matrix);
16291 return -1;
16292 }
16293 }
16294
16295 /* If bottom moved off end of frame, change mode line percentage. */
16296 if (XFASTINT (w->window_end_pos) <= 0
16297 && Z != IT_CHARPOS (it))
16298 w->update_mode_line = 1;
16299
16300 /* Set window_end_pos to the offset of the last character displayed
16301 on the window from the end of current_buffer. Set
16302 window_end_vpos to its row number. */
16303 if (last_text_row)
16304 {
16305 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16306 w->window_end_bytepos
16307 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16308 w->window_end_pos
16309 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16310 w->window_end_vpos
16311 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16312 eassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16313 ->displays_text_p);
16314 }
16315 else
16316 {
16317 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16318 w->window_end_pos = make_number (Z - ZV);
16319 w->window_end_vpos = make_number (0);
16320 }
16321
16322 /* But that is not valid info until redisplay finishes. */
16323 w->window_end_valid = Qnil;
16324 return 1;
16325 }
16326
16327
16328 \f
16329 /************************************************************************
16330 Window redisplay reusing current matrix when buffer has not changed
16331 ************************************************************************/
16332
16333 /* Try redisplay of window W showing an unchanged buffer with a
16334 different window start than the last time it was displayed by
16335 reusing its current matrix. Value is non-zero if successful.
16336 W->start is the new window start. */
16337
16338 static int
16339 try_window_reusing_current_matrix (struct window *w)
16340 {
16341 struct frame *f = XFRAME (w->frame);
16342 struct glyph_row *bottom_row;
16343 struct it it;
16344 struct run run;
16345 struct text_pos start, new_start;
16346 int nrows_scrolled, i;
16347 struct glyph_row *last_text_row;
16348 struct glyph_row *last_reused_text_row;
16349 struct glyph_row *start_row;
16350 int start_vpos, min_y, max_y;
16351
16352 #ifdef GLYPH_DEBUG
16353 if (inhibit_try_window_reusing)
16354 return 0;
16355 #endif
16356
16357 if (/* This function doesn't handle terminal frames. */
16358 !FRAME_WINDOW_P (f)
16359 /* Don't try to reuse the display if windows have been split
16360 or such. */
16361 || windows_or_buffers_changed
16362 || cursor_type_changed)
16363 return 0;
16364
16365 /* Can't do this if region may have changed. */
16366 if ((!NILP (Vtransient_mark_mode)
16367 && !NILP (BVAR (current_buffer, mark_active)))
16368 || !NILP (w->region_showing)
16369 || !NILP (Vshow_trailing_whitespace))
16370 return 0;
16371
16372 /* If top-line visibility has changed, give up. */
16373 if (WINDOW_WANTS_HEADER_LINE_P (w)
16374 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16375 return 0;
16376
16377 /* Give up if old or new display is scrolled vertically. We could
16378 make this function handle this, but right now it doesn't. */
16379 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16380 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16381 return 0;
16382
16383 /* The variable new_start now holds the new window start. The old
16384 start `start' can be determined from the current matrix. */
16385 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16386 start = start_row->minpos;
16387 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16388
16389 /* Clear the desired matrix for the display below. */
16390 clear_glyph_matrix (w->desired_matrix);
16391
16392 if (CHARPOS (new_start) <= CHARPOS (start))
16393 {
16394 /* Don't use this method if the display starts with an ellipsis
16395 displayed for invisible text. It's not easy to handle that case
16396 below, and it's certainly not worth the effort since this is
16397 not a frequent case. */
16398 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16399 return 0;
16400
16401 IF_DEBUG (debug_method_add (w, "twu1"));
16402
16403 /* Display up to a row that can be reused. The variable
16404 last_text_row is set to the last row displayed that displays
16405 text. Note that it.vpos == 0 if or if not there is a
16406 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16407 start_display (&it, w, new_start);
16408 w->cursor.vpos = -1;
16409 last_text_row = last_reused_text_row = NULL;
16410
16411 while (it.current_y < it.last_visible_y
16412 && !fonts_changed_p)
16413 {
16414 /* If we have reached into the characters in the START row,
16415 that means the line boundaries have changed. So we
16416 can't start copying with the row START. Maybe it will
16417 work to start copying with the following row. */
16418 while (IT_CHARPOS (it) > CHARPOS (start))
16419 {
16420 /* Advance to the next row as the "start". */
16421 start_row++;
16422 start = start_row->minpos;
16423 /* If there are no more rows to try, or just one, give up. */
16424 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16425 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16426 || CHARPOS (start) == ZV)
16427 {
16428 clear_glyph_matrix (w->desired_matrix);
16429 return 0;
16430 }
16431
16432 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16433 }
16434 /* If we have reached alignment, we can copy the rest of the
16435 rows. */
16436 if (IT_CHARPOS (it) == CHARPOS (start)
16437 /* Don't accept "alignment" inside a display vector,
16438 since start_row could have started in the middle of
16439 that same display vector (thus their character
16440 positions match), and we have no way of telling if
16441 that is the case. */
16442 && it.current.dpvec_index < 0)
16443 break;
16444
16445 if (display_line (&it))
16446 last_text_row = it.glyph_row - 1;
16447
16448 }
16449
16450 /* A value of current_y < last_visible_y means that we stopped
16451 at the previous window start, which in turn means that we
16452 have at least one reusable row. */
16453 if (it.current_y < it.last_visible_y)
16454 {
16455 struct glyph_row *row;
16456
16457 /* IT.vpos always starts from 0; it counts text lines. */
16458 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16459
16460 /* Find PT if not already found in the lines displayed. */
16461 if (w->cursor.vpos < 0)
16462 {
16463 int dy = it.current_y - start_row->y;
16464
16465 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16466 row = row_containing_pos (w, PT, row, NULL, dy);
16467 if (row)
16468 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16469 dy, nrows_scrolled);
16470 else
16471 {
16472 clear_glyph_matrix (w->desired_matrix);
16473 return 0;
16474 }
16475 }
16476
16477 /* Scroll the display. Do it before the current matrix is
16478 changed. The problem here is that update has not yet
16479 run, i.e. part of the current matrix is not up to date.
16480 scroll_run_hook will clear the cursor, and use the
16481 current matrix to get the height of the row the cursor is
16482 in. */
16483 run.current_y = start_row->y;
16484 run.desired_y = it.current_y;
16485 run.height = it.last_visible_y - it.current_y;
16486
16487 if (run.height > 0 && run.current_y != run.desired_y)
16488 {
16489 update_begin (f);
16490 FRAME_RIF (f)->update_window_begin_hook (w);
16491 FRAME_RIF (f)->clear_window_mouse_face (w);
16492 FRAME_RIF (f)->scroll_run_hook (w, &run);
16493 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16494 update_end (f);
16495 }
16496
16497 /* Shift current matrix down by nrows_scrolled lines. */
16498 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16499 rotate_matrix (w->current_matrix,
16500 start_vpos,
16501 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16502 nrows_scrolled);
16503
16504 /* Disable lines that must be updated. */
16505 for (i = 0; i < nrows_scrolled; ++i)
16506 (start_row + i)->enabled_p = 0;
16507
16508 /* Re-compute Y positions. */
16509 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16510 max_y = it.last_visible_y;
16511 for (row = start_row + nrows_scrolled;
16512 row < bottom_row;
16513 ++row)
16514 {
16515 row->y = it.current_y;
16516 row->visible_height = row->height;
16517
16518 if (row->y < min_y)
16519 row->visible_height -= min_y - row->y;
16520 if (row->y + row->height > max_y)
16521 row->visible_height -= row->y + row->height - max_y;
16522 if (row->fringe_bitmap_periodic_p)
16523 row->redraw_fringe_bitmaps_p = 1;
16524
16525 it.current_y += row->height;
16526
16527 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16528 last_reused_text_row = row;
16529 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16530 break;
16531 }
16532
16533 /* Disable lines in the current matrix which are now
16534 below the window. */
16535 for (++row; row < bottom_row; ++row)
16536 row->enabled_p = row->mode_line_p = 0;
16537 }
16538
16539 /* Update window_end_pos etc.; last_reused_text_row is the last
16540 reused row from the current matrix containing text, if any.
16541 The value of last_text_row is the last displayed line
16542 containing text. */
16543 if (last_reused_text_row)
16544 {
16545 w->window_end_bytepos
16546 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16547 w->window_end_pos
16548 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16549 w->window_end_vpos
16550 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16551 w->current_matrix));
16552 }
16553 else if (last_text_row)
16554 {
16555 w->window_end_bytepos
16556 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16557 w->window_end_pos
16558 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16559 w->window_end_vpos
16560 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16561 }
16562 else
16563 {
16564 /* This window must be completely empty. */
16565 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16566 w->window_end_pos = make_number (Z - ZV);
16567 w->window_end_vpos = make_number (0);
16568 }
16569 w->window_end_valid = Qnil;
16570
16571 /* Update hint: don't try scrolling again in update_window. */
16572 w->desired_matrix->no_scrolling_p = 1;
16573
16574 #ifdef GLYPH_DEBUG
16575 debug_method_add (w, "try_window_reusing_current_matrix 1");
16576 #endif
16577 return 1;
16578 }
16579 else if (CHARPOS (new_start) > CHARPOS (start))
16580 {
16581 struct glyph_row *pt_row, *row;
16582 struct glyph_row *first_reusable_row;
16583 struct glyph_row *first_row_to_display;
16584 int dy;
16585 int yb = window_text_bottom_y (w);
16586
16587 /* Find the row starting at new_start, if there is one. Don't
16588 reuse a partially visible line at the end. */
16589 first_reusable_row = start_row;
16590 while (first_reusable_row->enabled_p
16591 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16592 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16593 < CHARPOS (new_start)))
16594 ++first_reusable_row;
16595
16596 /* Give up if there is no row to reuse. */
16597 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16598 || !first_reusable_row->enabled_p
16599 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16600 != CHARPOS (new_start)))
16601 return 0;
16602
16603 /* We can reuse fully visible rows beginning with
16604 first_reusable_row to the end of the window. Set
16605 first_row_to_display to the first row that cannot be reused.
16606 Set pt_row to the row containing point, if there is any. */
16607 pt_row = NULL;
16608 for (first_row_to_display = first_reusable_row;
16609 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16610 ++first_row_to_display)
16611 {
16612 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16613 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16614 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16615 && first_row_to_display->ends_at_zv_p
16616 && pt_row == NULL)))
16617 pt_row = first_row_to_display;
16618 }
16619
16620 /* Start displaying at the start of first_row_to_display. */
16621 eassert (first_row_to_display->y < yb);
16622 init_to_row_start (&it, w, first_row_to_display);
16623
16624 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16625 - start_vpos);
16626 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16627 - nrows_scrolled);
16628 it.current_y = (first_row_to_display->y - first_reusable_row->y
16629 + WINDOW_HEADER_LINE_HEIGHT (w));
16630
16631 /* Display lines beginning with first_row_to_display in the
16632 desired matrix. Set last_text_row to the last row displayed
16633 that displays text. */
16634 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16635 if (pt_row == NULL)
16636 w->cursor.vpos = -1;
16637 last_text_row = NULL;
16638 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16639 if (display_line (&it))
16640 last_text_row = it.glyph_row - 1;
16641
16642 /* If point is in a reused row, adjust y and vpos of the cursor
16643 position. */
16644 if (pt_row)
16645 {
16646 w->cursor.vpos -= nrows_scrolled;
16647 w->cursor.y -= first_reusable_row->y - start_row->y;
16648 }
16649
16650 /* Give up if point isn't in a row displayed or reused. (This
16651 also handles the case where w->cursor.vpos < nrows_scrolled
16652 after the calls to display_line, which can happen with scroll
16653 margins. See bug#1295.) */
16654 if (w->cursor.vpos < 0)
16655 {
16656 clear_glyph_matrix (w->desired_matrix);
16657 return 0;
16658 }
16659
16660 /* Scroll the display. */
16661 run.current_y = first_reusable_row->y;
16662 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16663 run.height = it.last_visible_y - run.current_y;
16664 dy = run.current_y - run.desired_y;
16665
16666 if (run.height)
16667 {
16668 update_begin (f);
16669 FRAME_RIF (f)->update_window_begin_hook (w);
16670 FRAME_RIF (f)->clear_window_mouse_face (w);
16671 FRAME_RIF (f)->scroll_run_hook (w, &run);
16672 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16673 update_end (f);
16674 }
16675
16676 /* Adjust Y positions of reused rows. */
16677 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16678 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16679 max_y = it.last_visible_y;
16680 for (row = first_reusable_row; row < first_row_to_display; ++row)
16681 {
16682 row->y -= dy;
16683 row->visible_height = row->height;
16684 if (row->y < min_y)
16685 row->visible_height -= min_y - row->y;
16686 if (row->y + row->height > max_y)
16687 row->visible_height -= row->y + row->height - max_y;
16688 if (row->fringe_bitmap_periodic_p)
16689 row->redraw_fringe_bitmaps_p = 1;
16690 }
16691
16692 /* Scroll the current matrix. */
16693 eassert (nrows_scrolled > 0);
16694 rotate_matrix (w->current_matrix,
16695 start_vpos,
16696 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16697 -nrows_scrolled);
16698
16699 /* Disable rows not reused. */
16700 for (row -= nrows_scrolled; row < bottom_row; ++row)
16701 row->enabled_p = 0;
16702
16703 /* Point may have moved to a different line, so we cannot assume that
16704 the previous cursor position is valid; locate the correct row. */
16705 if (pt_row)
16706 {
16707 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16708 row < bottom_row
16709 && PT >= MATRIX_ROW_END_CHARPOS (row)
16710 && !row->ends_at_zv_p;
16711 row++)
16712 {
16713 w->cursor.vpos++;
16714 w->cursor.y = row->y;
16715 }
16716 if (row < bottom_row)
16717 {
16718 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16719 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16720
16721 /* Can't use this optimization with bidi-reordered glyph
16722 rows, unless cursor is already at point. */
16723 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16724 {
16725 if (!(w->cursor.hpos >= 0
16726 && w->cursor.hpos < row->used[TEXT_AREA]
16727 && BUFFERP (glyph->object)
16728 && glyph->charpos == PT))
16729 return 0;
16730 }
16731 else
16732 for (; glyph < end
16733 && (!BUFFERP (glyph->object)
16734 || glyph->charpos < PT);
16735 glyph++)
16736 {
16737 w->cursor.hpos++;
16738 w->cursor.x += glyph->pixel_width;
16739 }
16740 }
16741 }
16742
16743 /* Adjust window end. A null value of last_text_row means that
16744 the window end is in reused rows which in turn means that
16745 only its vpos can have changed. */
16746 if (last_text_row)
16747 {
16748 w->window_end_bytepos
16749 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16750 w->window_end_pos
16751 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16752 w->window_end_vpos
16753 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16754 }
16755 else
16756 {
16757 w->window_end_vpos
16758 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16759 }
16760
16761 w->window_end_valid = Qnil;
16762 w->desired_matrix->no_scrolling_p = 1;
16763
16764 #ifdef GLYPH_DEBUG
16765 debug_method_add (w, "try_window_reusing_current_matrix 2");
16766 #endif
16767 return 1;
16768 }
16769
16770 return 0;
16771 }
16772
16773
16774 \f
16775 /************************************************************************
16776 Window redisplay reusing current matrix when buffer has changed
16777 ************************************************************************/
16778
16779 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16780 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16781 ptrdiff_t *, ptrdiff_t *);
16782 static struct glyph_row *
16783 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16784 struct glyph_row *);
16785
16786
16787 /* Return the last row in MATRIX displaying text. If row START is
16788 non-null, start searching with that row. IT gives the dimensions
16789 of the display. Value is null if matrix is empty; otherwise it is
16790 a pointer to the row found. */
16791
16792 static struct glyph_row *
16793 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16794 struct glyph_row *start)
16795 {
16796 struct glyph_row *row, *row_found;
16797
16798 /* Set row_found to the last row in IT->w's current matrix
16799 displaying text. The loop looks funny but think of partially
16800 visible lines. */
16801 row_found = NULL;
16802 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16803 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16804 {
16805 eassert (row->enabled_p);
16806 row_found = row;
16807 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16808 break;
16809 ++row;
16810 }
16811
16812 return row_found;
16813 }
16814
16815
16816 /* Return the last row in the current matrix of W that is not affected
16817 by changes at the start of current_buffer that occurred since W's
16818 current matrix was built. Value is null if no such row exists.
16819
16820 BEG_UNCHANGED us the number of characters unchanged at the start of
16821 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16822 first changed character in current_buffer. Characters at positions <
16823 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16824 when the current matrix was built. */
16825
16826 static struct glyph_row *
16827 find_last_unchanged_at_beg_row (struct window *w)
16828 {
16829 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16830 struct glyph_row *row;
16831 struct glyph_row *row_found = NULL;
16832 int yb = window_text_bottom_y (w);
16833
16834 /* Find the last row displaying unchanged text. */
16835 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16836 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16837 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16838 ++row)
16839 {
16840 if (/* If row ends before first_changed_pos, it is unchanged,
16841 except in some case. */
16842 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16843 /* When row ends in ZV and we write at ZV it is not
16844 unchanged. */
16845 && !row->ends_at_zv_p
16846 /* When first_changed_pos is the end of a continued line,
16847 row is not unchanged because it may be no longer
16848 continued. */
16849 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16850 && (row->continued_p
16851 || row->exact_window_width_line_p))
16852 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16853 needs to be recomputed, so don't consider this row as
16854 unchanged. This happens when the last line was
16855 bidi-reordered and was killed immediately before this
16856 redisplay cycle. In that case, ROW->end stores the
16857 buffer position of the first visual-order character of
16858 the killed text, which is now beyond ZV. */
16859 && CHARPOS (row->end.pos) <= ZV)
16860 row_found = row;
16861
16862 /* Stop if last visible row. */
16863 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16864 break;
16865 }
16866
16867 return row_found;
16868 }
16869
16870
16871 /* Find the first glyph row in the current matrix of W that is not
16872 affected by changes at the end of current_buffer since the
16873 time W's current matrix was built.
16874
16875 Return in *DELTA the number of chars by which buffer positions in
16876 unchanged text at the end of current_buffer must be adjusted.
16877
16878 Return in *DELTA_BYTES the corresponding number of bytes.
16879
16880 Value is null if no such row exists, i.e. all rows are affected by
16881 changes. */
16882
16883 static struct glyph_row *
16884 find_first_unchanged_at_end_row (struct window *w,
16885 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16886 {
16887 struct glyph_row *row;
16888 struct glyph_row *row_found = NULL;
16889
16890 *delta = *delta_bytes = 0;
16891
16892 /* Display must not have been paused, otherwise the current matrix
16893 is not up to date. */
16894 eassert (!NILP (w->window_end_valid));
16895
16896 /* A value of window_end_pos >= END_UNCHANGED means that the window
16897 end is in the range of changed text. If so, there is no
16898 unchanged row at the end of W's current matrix. */
16899 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16900 return NULL;
16901
16902 /* Set row to the last row in W's current matrix displaying text. */
16903 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16904
16905 /* If matrix is entirely empty, no unchanged row exists. */
16906 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16907 {
16908 /* The value of row is the last glyph row in the matrix having a
16909 meaningful buffer position in it. The end position of row
16910 corresponds to window_end_pos. This allows us to translate
16911 buffer positions in the current matrix to current buffer
16912 positions for characters not in changed text. */
16913 ptrdiff_t Z_old =
16914 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16915 ptrdiff_t Z_BYTE_old =
16916 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16917 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16918 struct glyph_row *first_text_row
16919 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16920
16921 *delta = Z - Z_old;
16922 *delta_bytes = Z_BYTE - Z_BYTE_old;
16923
16924 /* Set last_unchanged_pos to the buffer position of the last
16925 character in the buffer that has not been changed. Z is the
16926 index + 1 of the last character in current_buffer, i.e. by
16927 subtracting END_UNCHANGED we get the index of the last
16928 unchanged character, and we have to add BEG to get its buffer
16929 position. */
16930 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16931 last_unchanged_pos_old = last_unchanged_pos - *delta;
16932
16933 /* Search backward from ROW for a row displaying a line that
16934 starts at a minimum position >= last_unchanged_pos_old. */
16935 for (; row > first_text_row; --row)
16936 {
16937 /* This used to abort, but it can happen.
16938 It is ok to just stop the search instead here. KFS. */
16939 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16940 break;
16941
16942 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16943 row_found = row;
16944 }
16945 }
16946
16947 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16948
16949 return row_found;
16950 }
16951
16952
16953 /* Make sure that glyph rows in the current matrix of window W
16954 reference the same glyph memory as corresponding rows in the
16955 frame's frame matrix. This function is called after scrolling W's
16956 current matrix on a terminal frame in try_window_id and
16957 try_window_reusing_current_matrix. */
16958
16959 static void
16960 sync_frame_with_window_matrix_rows (struct window *w)
16961 {
16962 struct frame *f = XFRAME (w->frame);
16963 struct glyph_row *window_row, *window_row_end, *frame_row;
16964
16965 /* Preconditions: W must be a leaf window and full-width. Its frame
16966 must have a frame matrix. */
16967 eassert (NILP (w->hchild) && NILP (w->vchild));
16968 eassert (WINDOW_FULL_WIDTH_P (w));
16969 eassert (!FRAME_WINDOW_P (f));
16970
16971 /* If W is a full-width window, glyph pointers in W's current matrix
16972 have, by definition, to be the same as glyph pointers in the
16973 corresponding frame matrix. Note that frame matrices have no
16974 marginal areas (see build_frame_matrix). */
16975 window_row = w->current_matrix->rows;
16976 window_row_end = window_row + w->current_matrix->nrows;
16977 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16978 while (window_row < window_row_end)
16979 {
16980 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16981 struct glyph *end = window_row->glyphs[LAST_AREA];
16982
16983 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16984 frame_row->glyphs[TEXT_AREA] = start;
16985 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16986 frame_row->glyphs[LAST_AREA] = end;
16987
16988 /* Disable frame rows whose corresponding window rows have
16989 been disabled in try_window_id. */
16990 if (!window_row->enabled_p)
16991 frame_row->enabled_p = 0;
16992
16993 ++window_row, ++frame_row;
16994 }
16995 }
16996
16997
16998 /* Find the glyph row in window W containing CHARPOS. Consider all
16999 rows between START and END (not inclusive). END null means search
17000 all rows to the end of the display area of W. Value is the row
17001 containing CHARPOS or null. */
17002
17003 struct glyph_row *
17004 row_containing_pos (struct window *w, ptrdiff_t charpos,
17005 struct glyph_row *start, struct glyph_row *end, int dy)
17006 {
17007 struct glyph_row *row = start;
17008 struct glyph_row *best_row = NULL;
17009 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17010 int last_y;
17011
17012 /* If we happen to start on a header-line, skip that. */
17013 if (row->mode_line_p)
17014 ++row;
17015
17016 if ((end && row >= end) || !row->enabled_p)
17017 return NULL;
17018
17019 last_y = window_text_bottom_y (w) - dy;
17020
17021 while (1)
17022 {
17023 /* Give up if we have gone too far. */
17024 if (end && row >= end)
17025 return NULL;
17026 /* This formerly returned if they were equal.
17027 I think that both quantities are of a "last plus one" type;
17028 if so, when they are equal, the row is within the screen. -- rms. */
17029 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17030 return NULL;
17031
17032 /* If it is in this row, return this row. */
17033 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17034 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17035 /* The end position of a row equals the start
17036 position of the next row. If CHARPOS is there, we
17037 would rather display it in the next line, except
17038 when this line ends in ZV. */
17039 && !row->ends_at_zv_p
17040 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17041 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17042 {
17043 struct glyph *g;
17044
17045 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17046 || (!best_row && !row->continued_p))
17047 return row;
17048 /* In bidi-reordered rows, there could be several rows
17049 occluding point, all of them belonging to the same
17050 continued line. We need to find the row which fits
17051 CHARPOS the best. */
17052 for (g = row->glyphs[TEXT_AREA];
17053 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17054 g++)
17055 {
17056 if (!STRINGP (g->object))
17057 {
17058 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17059 {
17060 mindif = eabs (g->charpos - charpos);
17061 best_row = row;
17062 /* Exact match always wins. */
17063 if (mindif == 0)
17064 return best_row;
17065 }
17066 }
17067 }
17068 }
17069 else if (best_row && !row->continued_p)
17070 return best_row;
17071 ++row;
17072 }
17073 }
17074
17075
17076 /* Try to redisplay window W by reusing its existing display. W's
17077 current matrix must be up to date when this function is called,
17078 i.e. window_end_valid must not be nil.
17079
17080 Value is
17081
17082 1 if display has been updated
17083 0 if otherwise unsuccessful
17084 -1 if redisplay with same window start is known not to succeed
17085
17086 The following steps are performed:
17087
17088 1. Find the last row in the current matrix of W that is not
17089 affected by changes at the start of current_buffer. If no such row
17090 is found, give up.
17091
17092 2. Find the first row in W's current matrix that is not affected by
17093 changes at the end of current_buffer. Maybe there is no such row.
17094
17095 3. Display lines beginning with the row + 1 found in step 1 to the
17096 row found in step 2 or, if step 2 didn't find a row, to the end of
17097 the window.
17098
17099 4. If cursor is not known to appear on the window, give up.
17100
17101 5. If display stopped at the row found in step 2, scroll the
17102 display and current matrix as needed.
17103
17104 6. Maybe display some lines at the end of W, if we must. This can
17105 happen under various circumstances, like a partially visible line
17106 becoming fully visible, or because newly displayed lines are displayed
17107 in smaller font sizes.
17108
17109 7. Update W's window end information. */
17110
17111 static int
17112 try_window_id (struct window *w)
17113 {
17114 struct frame *f = XFRAME (w->frame);
17115 struct glyph_matrix *current_matrix = w->current_matrix;
17116 struct glyph_matrix *desired_matrix = w->desired_matrix;
17117 struct glyph_row *last_unchanged_at_beg_row;
17118 struct glyph_row *first_unchanged_at_end_row;
17119 struct glyph_row *row;
17120 struct glyph_row *bottom_row;
17121 int bottom_vpos;
17122 struct it it;
17123 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17124 int dvpos, dy;
17125 struct text_pos start_pos;
17126 struct run run;
17127 int first_unchanged_at_end_vpos = 0;
17128 struct glyph_row *last_text_row, *last_text_row_at_end;
17129 struct text_pos start;
17130 ptrdiff_t first_changed_charpos, last_changed_charpos;
17131
17132 #ifdef GLYPH_DEBUG
17133 if (inhibit_try_window_id)
17134 return 0;
17135 #endif
17136
17137 /* This is handy for debugging. */
17138 #if 0
17139 #define GIVE_UP(X) \
17140 do { \
17141 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17142 return 0; \
17143 } while (0)
17144 #else
17145 #define GIVE_UP(X) return 0
17146 #endif
17147
17148 SET_TEXT_POS_FROM_MARKER (start, w->start);
17149
17150 /* Don't use this for mini-windows because these can show
17151 messages and mini-buffers, and we don't handle that here. */
17152 if (MINI_WINDOW_P (w))
17153 GIVE_UP (1);
17154
17155 /* This flag is used to prevent redisplay optimizations. */
17156 if (windows_or_buffers_changed || cursor_type_changed)
17157 GIVE_UP (2);
17158
17159 /* Verify that narrowing has not changed.
17160 Also verify that we were not told to prevent redisplay optimizations.
17161 It would be nice to further
17162 reduce the number of cases where this prevents try_window_id. */
17163 if (current_buffer->clip_changed
17164 || current_buffer->prevent_redisplay_optimizations_p)
17165 GIVE_UP (3);
17166
17167 /* Window must either use window-based redisplay or be full width. */
17168 if (!FRAME_WINDOW_P (f)
17169 && (!FRAME_LINE_INS_DEL_OK (f)
17170 || !WINDOW_FULL_WIDTH_P (w)))
17171 GIVE_UP (4);
17172
17173 /* Give up if point is known NOT to appear in W. */
17174 if (PT < CHARPOS (start))
17175 GIVE_UP (5);
17176
17177 /* Another way to prevent redisplay optimizations. */
17178 if (w->last_modified == 0)
17179 GIVE_UP (6);
17180
17181 /* Verify that window is not hscrolled. */
17182 if (w->hscroll != 0)
17183 GIVE_UP (7);
17184
17185 /* Verify that display wasn't paused. */
17186 if (NILP (w->window_end_valid))
17187 GIVE_UP (8);
17188
17189 /* Can't use this if highlighting a region because a cursor movement
17190 will do more than just set the cursor. */
17191 if (!NILP (Vtransient_mark_mode)
17192 && !NILP (BVAR (current_buffer, mark_active)))
17193 GIVE_UP (9);
17194
17195 /* Likewise if highlighting trailing whitespace. */
17196 if (!NILP (Vshow_trailing_whitespace))
17197 GIVE_UP (11);
17198
17199 /* Likewise if showing a region. */
17200 if (!NILP (w->region_showing))
17201 GIVE_UP (10);
17202
17203 /* Can't use this if overlay arrow position and/or string have
17204 changed. */
17205 if (overlay_arrows_changed_p ())
17206 GIVE_UP (12);
17207
17208 /* When word-wrap is on, adding a space to the first word of a
17209 wrapped line can change the wrap position, altering the line
17210 above it. It might be worthwhile to handle this more
17211 intelligently, but for now just redisplay from scratch. */
17212 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17213 GIVE_UP (21);
17214
17215 /* Under bidi reordering, adding or deleting a character in the
17216 beginning of a paragraph, before the first strong directional
17217 character, can change the base direction of the paragraph (unless
17218 the buffer specifies a fixed paragraph direction), which will
17219 require to redisplay the whole paragraph. It might be worthwhile
17220 to find the paragraph limits and widen the range of redisplayed
17221 lines to that, but for now just give up this optimization and
17222 redisplay from scratch. */
17223 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17224 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17225 GIVE_UP (22);
17226
17227 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17228 only if buffer has really changed. The reason is that the gap is
17229 initially at Z for freshly visited files. The code below would
17230 set end_unchanged to 0 in that case. */
17231 if (MODIFF > SAVE_MODIFF
17232 /* This seems to happen sometimes after saving a buffer. */
17233 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17234 {
17235 if (GPT - BEG < BEG_UNCHANGED)
17236 BEG_UNCHANGED = GPT - BEG;
17237 if (Z - GPT < END_UNCHANGED)
17238 END_UNCHANGED = Z - GPT;
17239 }
17240
17241 /* The position of the first and last character that has been changed. */
17242 first_changed_charpos = BEG + BEG_UNCHANGED;
17243 last_changed_charpos = Z - END_UNCHANGED;
17244
17245 /* If window starts after a line end, and the last change is in
17246 front of that newline, then changes don't affect the display.
17247 This case happens with stealth-fontification. Note that although
17248 the display is unchanged, glyph positions in the matrix have to
17249 be adjusted, of course. */
17250 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17251 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17252 && ((last_changed_charpos < CHARPOS (start)
17253 && CHARPOS (start) == BEGV)
17254 || (last_changed_charpos < CHARPOS (start) - 1
17255 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17256 {
17257 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17258 struct glyph_row *r0;
17259
17260 /* Compute how many chars/bytes have been added to or removed
17261 from the buffer. */
17262 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17263 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17264 Z_delta = Z - Z_old;
17265 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17266
17267 /* Give up if PT is not in the window. Note that it already has
17268 been checked at the start of try_window_id that PT is not in
17269 front of the window start. */
17270 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17271 GIVE_UP (13);
17272
17273 /* If window start is unchanged, we can reuse the whole matrix
17274 as is, after adjusting glyph positions. No need to compute
17275 the window end again, since its offset from Z hasn't changed. */
17276 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17277 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17278 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17279 /* PT must not be in a partially visible line. */
17280 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17281 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17282 {
17283 /* Adjust positions in the glyph matrix. */
17284 if (Z_delta || Z_delta_bytes)
17285 {
17286 struct glyph_row *r1
17287 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17288 increment_matrix_positions (w->current_matrix,
17289 MATRIX_ROW_VPOS (r0, current_matrix),
17290 MATRIX_ROW_VPOS (r1, current_matrix),
17291 Z_delta, Z_delta_bytes);
17292 }
17293
17294 /* Set the cursor. */
17295 row = row_containing_pos (w, PT, r0, NULL, 0);
17296 if (row)
17297 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17298 else
17299 abort ();
17300 return 1;
17301 }
17302 }
17303
17304 /* Handle the case that changes are all below what is displayed in
17305 the window, and that PT is in the window. This shortcut cannot
17306 be taken if ZV is visible in the window, and text has been added
17307 there that is visible in the window. */
17308 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17309 /* ZV is not visible in the window, or there are no
17310 changes at ZV, actually. */
17311 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17312 || first_changed_charpos == last_changed_charpos))
17313 {
17314 struct glyph_row *r0;
17315
17316 /* Give up if PT is not in the window. Note that it already has
17317 been checked at the start of try_window_id that PT is not in
17318 front of the window start. */
17319 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17320 GIVE_UP (14);
17321
17322 /* If window start is unchanged, we can reuse the whole matrix
17323 as is, without changing glyph positions since no text has
17324 been added/removed in front of the window end. */
17325 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17326 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17327 /* PT must not be in a partially visible line. */
17328 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17329 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17330 {
17331 /* We have to compute the window end anew since text
17332 could have been added/removed after it. */
17333 w->window_end_pos
17334 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17335 w->window_end_bytepos
17336 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17337
17338 /* Set the cursor. */
17339 row = row_containing_pos (w, PT, r0, NULL, 0);
17340 if (row)
17341 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17342 else
17343 abort ();
17344 return 2;
17345 }
17346 }
17347
17348 /* Give up if window start is in the changed area.
17349
17350 The condition used to read
17351
17352 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17353
17354 but why that was tested escapes me at the moment. */
17355 if (CHARPOS (start) >= first_changed_charpos
17356 && CHARPOS (start) <= last_changed_charpos)
17357 GIVE_UP (15);
17358
17359 /* Check that window start agrees with the start of the first glyph
17360 row in its current matrix. Check this after we know the window
17361 start is not in changed text, otherwise positions would not be
17362 comparable. */
17363 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17364 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17365 GIVE_UP (16);
17366
17367 /* Give up if the window ends in strings. Overlay strings
17368 at the end are difficult to handle, so don't try. */
17369 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17370 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17371 GIVE_UP (20);
17372
17373 /* Compute the position at which we have to start displaying new
17374 lines. Some of the lines at the top of the window might be
17375 reusable because they are not displaying changed text. Find the
17376 last row in W's current matrix not affected by changes at the
17377 start of current_buffer. Value is null if changes start in the
17378 first line of window. */
17379 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17380 if (last_unchanged_at_beg_row)
17381 {
17382 /* Avoid starting to display in the middle of a character, a TAB
17383 for instance. This is easier than to set up the iterator
17384 exactly, and it's not a frequent case, so the additional
17385 effort wouldn't really pay off. */
17386 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17387 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17388 && last_unchanged_at_beg_row > w->current_matrix->rows)
17389 --last_unchanged_at_beg_row;
17390
17391 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17392 GIVE_UP (17);
17393
17394 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17395 GIVE_UP (18);
17396 start_pos = it.current.pos;
17397
17398 /* Start displaying new lines in the desired matrix at the same
17399 vpos we would use in the current matrix, i.e. below
17400 last_unchanged_at_beg_row. */
17401 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17402 current_matrix);
17403 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17404 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17405
17406 eassert (it.hpos == 0 && it.current_x == 0);
17407 }
17408 else
17409 {
17410 /* There are no reusable lines at the start of the window.
17411 Start displaying in the first text line. */
17412 start_display (&it, w, start);
17413 it.vpos = it.first_vpos;
17414 start_pos = it.current.pos;
17415 }
17416
17417 /* Find the first row that is not affected by changes at the end of
17418 the buffer. Value will be null if there is no unchanged row, in
17419 which case we must redisplay to the end of the window. delta
17420 will be set to the value by which buffer positions beginning with
17421 first_unchanged_at_end_row have to be adjusted due to text
17422 changes. */
17423 first_unchanged_at_end_row
17424 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17425 IF_DEBUG (debug_delta = delta);
17426 IF_DEBUG (debug_delta_bytes = delta_bytes);
17427
17428 /* Set stop_pos to the buffer position up to which we will have to
17429 display new lines. If first_unchanged_at_end_row != NULL, this
17430 is the buffer position of the start of the line displayed in that
17431 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17432 that we don't stop at a buffer position. */
17433 stop_pos = 0;
17434 if (first_unchanged_at_end_row)
17435 {
17436 eassert (last_unchanged_at_beg_row == NULL
17437 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17438
17439 /* If this is a continuation line, move forward to the next one
17440 that isn't. Changes in lines above affect this line.
17441 Caution: this may move first_unchanged_at_end_row to a row
17442 not displaying text. */
17443 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17444 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17445 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17446 < it.last_visible_y))
17447 ++first_unchanged_at_end_row;
17448
17449 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17450 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17451 >= it.last_visible_y))
17452 first_unchanged_at_end_row = NULL;
17453 else
17454 {
17455 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17456 + delta);
17457 first_unchanged_at_end_vpos
17458 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17459 eassert (stop_pos >= Z - END_UNCHANGED);
17460 }
17461 }
17462 else if (last_unchanged_at_beg_row == NULL)
17463 GIVE_UP (19);
17464
17465
17466 #ifdef GLYPH_DEBUG
17467
17468 /* Either there is no unchanged row at the end, or the one we have
17469 now displays text. This is a necessary condition for the window
17470 end pos calculation at the end of this function. */
17471 eassert (first_unchanged_at_end_row == NULL
17472 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17473
17474 debug_last_unchanged_at_beg_vpos
17475 = (last_unchanged_at_beg_row
17476 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17477 : -1);
17478 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17479
17480 #endif /* GLYPH_DEBUG */
17481
17482
17483 /* Display new lines. Set last_text_row to the last new line
17484 displayed which has text on it, i.e. might end up as being the
17485 line where the window_end_vpos is. */
17486 w->cursor.vpos = -1;
17487 last_text_row = NULL;
17488 overlay_arrow_seen = 0;
17489 while (it.current_y < it.last_visible_y
17490 && !fonts_changed_p
17491 && (first_unchanged_at_end_row == NULL
17492 || IT_CHARPOS (it) < stop_pos))
17493 {
17494 if (display_line (&it))
17495 last_text_row = it.glyph_row - 1;
17496 }
17497
17498 if (fonts_changed_p)
17499 return -1;
17500
17501
17502 /* Compute differences in buffer positions, y-positions etc. for
17503 lines reused at the bottom of the window. Compute what we can
17504 scroll. */
17505 if (first_unchanged_at_end_row
17506 /* No lines reused because we displayed everything up to the
17507 bottom of the window. */
17508 && it.current_y < it.last_visible_y)
17509 {
17510 dvpos = (it.vpos
17511 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17512 current_matrix));
17513 dy = it.current_y - first_unchanged_at_end_row->y;
17514 run.current_y = first_unchanged_at_end_row->y;
17515 run.desired_y = run.current_y + dy;
17516 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17517 }
17518 else
17519 {
17520 delta = delta_bytes = dvpos = dy
17521 = run.current_y = run.desired_y = run.height = 0;
17522 first_unchanged_at_end_row = NULL;
17523 }
17524 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17525
17526
17527 /* Find the cursor if not already found. We have to decide whether
17528 PT will appear on this window (it sometimes doesn't, but this is
17529 not a very frequent case.) This decision has to be made before
17530 the current matrix is altered. A value of cursor.vpos < 0 means
17531 that PT is either in one of the lines beginning at
17532 first_unchanged_at_end_row or below the window. Don't care for
17533 lines that might be displayed later at the window end; as
17534 mentioned, this is not a frequent case. */
17535 if (w->cursor.vpos < 0)
17536 {
17537 /* Cursor in unchanged rows at the top? */
17538 if (PT < CHARPOS (start_pos)
17539 && last_unchanged_at_beg_row)
17540 {
17541 row = row_containing_pos (w, PT,
17542 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17543 last_unchanged_at_beg_row + 1, 0);
17544 if (row)
17545 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17546 }
17547
17548 /* Start from first_unchanged_at_end_row looking for PT. */
17549 else if (first_unchanged_at_end_row)
17550 {
17551 row = row_containing_pos (w, PT - delta,
17552 first_unchanged_at_end_row, NULL, 0);
17553 if (row)
17554 set_cursor_from_row (w, row, w->current_matrix, delta,
17555 delta_bytes, dy, dvpos);
17556 }
17557
17558 /* Give up if cursor was not found. */
17559 if (w->cursor.vpos < 0)
17560 {
17561 clear_glyph_matrix (w->desired_matrix);
17562 return -1;
17563 }
17564 }
17565
17566 /* Don't let the cursor end in the scroll margins. */
17567 {
17568 int this_scroll_margin, cursor_height;
17569
17570 this_scroll_margin =
17571 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17572 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17573 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17574
17575 if ((w->cursor.y < this_scroll_margin
17576 && CHARPOS (start) > BEGV)
17577 /* Old redisplay didn't take scroll margin into account at the bottom,
17578 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17579 || (w->cursor.y + (make_cursor_line_fully_visible_p
17580 ? cursor_height + this_scroll_margin
17581 : 1)) > it.last_visible_y)
17582 {
17583 w->cursor.vpos = -1;
17584 clear_glyph_matrix (w->desired_matrix);
17585 return -1;
17586 }
17587 }
17588
17589 /* Scroll the display. Do it before changing the current matrix so
17590 that xterm.c doesn't get confused about where the cursor glyph is
17591 found. */
17592 if (dy && run.height)
17593 {
17594 update_begin (f);
17595
17596 if (FRAME_WINDOW_P (f))
17597 {
17598 FRAME_RIF (f)->update_window_begin_hook (w);
17599 FRAME_RIF (f)->clear_window_mouse_face (w);
17600 FRAME_RIF (f)->scroll_run_hook (w, &run);
17601 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17602 }
17603 else
17604 {
17605 /* Terminal frame. In this case, dvpos gives the number of
17606 lines to scroll by; dvpos < 0 means scroll up. */
17607 int from_vpos
17608 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17609 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17610 int end = (WINDOW_TOP_EDGE_LINE (w)
17611 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17612 + window_internal_height (w));
17613
17614 #if defined (HAVE_GPM) || defined (MSDOS)
17615 x_clear_window_mouse_face (w);
17616 #endif
17617 /* Perform the operation on the screen. */
17618 if (dvpos > 0)
17619 {
17620 /* Scroll last_unchanged_at_beg_row to the end of the
17621 window down dvpos lines. */
17622 set_terminal_window (f, end);
17623
17624 /* On dumb terminals delete dvpos lines at the end
17625 before inserting dvpos empty lines. */
17626 if (!FRAME_SCROLL_REGION_OK (f))
17627 ins_del_lines (f, end - dvpos, -dvpos);
17628
17629 /* Insert dvpos empty lines in front of
17630 last_unchanged_at_beg_row. */
17631 ins_del_lines (f, from, dvpos);
17632 }
17633 else if (dvpos < 0)
17634 {
17635 /* Scroll up last_unchanged_at_beg_vpos to the end of
17636 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17637 set_terminal_window (f, end);
17638
17639 /* Delete dvpos lines in front of
17640 last_unchanged_at_beg_vpos. ins_del_lines will set
17641 the cursor to the given vpos and emit |dvpos| delete
17642 line sequences. */
17643 ins_del_lines (f, from + dvpos, dvpos);
17644
17645 /* On a dumb terminal insert dvpos empty lines at the
17646 end. */
17647 if (!FRAME_SCROLL_REGION_OK (f))
17648 ins_del_lines (f, end + dvpos, -dvpos);
17649 }
17650
17651 set_terminal_window (f, 0);
17652 }
17653
17654 update_end (f);
17655 }
17656
17657 /* Shift reused rows of the current matrix to the right position.
17658 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17659 text. */
17660 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17661 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17662 if (dvpos < 0)
17663 {
17664 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17665 bottom_vpos, dvpos);
17666 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17667 bottom_vpos, 0);
17668 }
17669 else if (dvpos > 0)
17670 {
17671 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17672 bottom_vpos, dvpos);
17673 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17674 first_unchanged_at_end_vpos + dvpos, 0);
17675 }
17676
17677 /* For frame-based redisplay, make sure that current frame and window
17678 matrix are in sync with respect to glyph memory. */
17679 if (!FRAME_WINDOW_P (f))
17680 sync_frame_with_window_matrix_rows (w);
17681
17682 /* Adjust buffer positions in reused rows. */
17683 if (delta || delta_bytes)
17684 increment_matrix_positions (current_matrix,
17685 first_unchanged_at_end_vpos + dvpos,
17686 bottom_vpos, delta, delta_bytes);
17687
17688 /* Adjust Y positions. */
17689 if (dy)
17690 shift_glyph_matrix (w, current_matrix,
17691 first_unchanged_at_end_vpos + dvpos,
17692 bottom_vpos, dy);
17693
17694 if (first_unchanged_at_end_row)
17695 {
17696 first_unchanged_at_end_row += dvpos;
17697 if (first_unchanged_at_end_row->y >= it.last_visible_y
17698 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17699 first_unchanged_at_end_row = NULL;
17700 }
17701
17702 /* If scrolling up, there may be some lines to display at the end of
17703 the window. */
17704 last_text_row_at_end = NULL;
17705 if (dy < 0)
17706 {
17707 /* Scrolling up can leave for example a partially visible line
17708 at the end of the window to be redisplayed. */
17709 /* Set last_row to the glyph row in the current matrix where the
17710 window end line is found. It has been moved up or down in
17711 the matrix by dvpos. */
17712 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17713 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17714
17715 /* If last_row is the window end line, it should display text. */
17716 eassert (last_row->displays_text_p);
17717
17718 /* If window end line was partially visible before, begin
17719 displaying at that line. Otherwise begin displaying with the
17720 line following it. */
17721 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17722 {
17723 init_to_row_start (&it, w, last_row);
17724 it.vpos = last_vpos;
17725 it.current_y = last_row->y;
17726 }
17727 else
17728 {
17729 init_to_row_end (&it, w, last_row);
17730 it.vpos = 1 + last_vpos;
17731 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17732 ++last_row;
17733 }
17734
17735 /* We may start in a continuation line. If so, we have to
17736 get the right continuation_lines_width and current_x. */
17737 it.continuation_lines_width = last_row->continuation_lines_width;
17738 it.hpos = it.current_x = 0;
17739
17740 /* Display the rest of the lines at the window end. */
17741 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17742 while (it.current_y < it.last_visible_y
17743 && !fonts_changed_p)
17744 {
17745 /* Is it always sure that the display agrees with lines in
17746 the current matrix? I don't think so, so we mark rows
17747 displayed invalid in the current matrix by setting their
17748 enabled_p flag to zero. */
17749 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17750 if (display_line (&it))
17751 last_text_row_at_end = it.glyph_row - 1;
17752 }
17753 }
17754
17755 /* Update window_end_pos and window_end_vpos. */
17756 if (first_unchanged_at_end_row
17757 && !last_text_row_at_end)
17758 {
17759 /* Window end line if one of the preserved rows from the current
17760 matrix. Set row to the last row displaying text in current
17761 matrix starting at first_unchanged_at_end_row, after
17762 scrolling. */
17763 eassert (first_unchanged_at_end_row->displays_text_p);
17764 row = find_last_row_displaying_text (w->current_matrix, &it,
17765 first_unchanged_at_end_row);
17766 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17767
17768 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17769 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17770 w->window_end_vpos
17771 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17772 eassert (w->window_end_bytepos >= 0);
17773 IF_DEBUG (debug_method_add (w, "A"));
17774 }
17775 else if (last_text_row_at_end)
17776 {
17777 w->window_end_pos
17778 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17779 w->window_end_bytepos
17780 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17781 w->window_end_vpos
17782 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17783 eassert (w->window_end_bytepos >= 0);
17784 IF_DEBUG (debug_method_add (w, "B"));
17785 }
17786 else if (last_text_row)
17787 {
17788 /* We have displayed either to the end of the window or at the
17789 end of the window, i.e. the last row with text is to be found
17790 in the desired matrix. */
17791 w->window_end_pos
17792 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17793 w->window_end_bytepos
17794 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17795 w->window_end_vpos
17796 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17797 eassert (w->window_end_bytepos >= 0);
17798 }
17799 else if (first_unchanged_at_end_row == NULL
17800 && last_text_row == NULL
17801 && last_text_row_at_end == NULL)
17802 {
17803 /* Displayed to end of window, but no line containing text was
17804 displayed. Lines were deleted at the end of the window. */
17805 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17806 int vpos = XFASTINT (w->window_end_vpos);
17807 struct glyph_row *current_row = current_matrix->rows + vpos;
17808 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17809
17810 for (row = NULL;
17811 row == NULL && vpos >= first_vpos;
17812 --vpos, --current_row, --desired_row)
17813 {
17814 if (desired_row->enabled_p)
17815 {
17816 if (desired_row->displays_text_p)
17817 row = desired_row;
17818 }
17819 else if (current_row->displays_text_p)
17820 row = current_row;
17821 }
17822
17823 eassert (row != NULL);
17824 w->window_end_vpos = make_number (vpos + 1);
17825 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17826 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17827 eassert (w->window_end_bytepos >= 0);
17828 IF_DEBUG (debug_method_add (w, "C"));
17829 }
17830 else
17831 abort ();
17832
17833 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17834 debug_end_vpos = XFASTINT (w->window_end_vpos));
17835
17836 /* Record that display has not been completed. */
17837 w->window_end_valid = Qnil;
17838 w->desired_matrix->no_scrolling_p = 1;
17839 return 3;
17840
17841 #undef GIVE_UP
17842 }
17843
17844
17845 \f
17846 /***********************************************************************
17847 More debugging support
17848 ***********************************************************************/
17849
17850 #ifdef GLYPH_DEBUG
17851
17852 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17853 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17854 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17855
17856
17857 /* Dump the contents of glyph matrix MATRIX on stderr.
17858
17859 GLYPHS 0 means don't show glyph contents.
17860 GLYPHS 1 means show glyphs in short form
17861 GLYPHS > 1 means show glyphs in long form. */
17862
17863 void
17864 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17865 {
17866 int i;
17867 for (i = 0; i < matrix->nrows; ++i)
17868 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17869 }
17870
17871
17872 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17873 the glyph row and area where the glyph comes from. */
17874
17875 void
17876 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17877 {
17878 if (glyph->type == CHAR_GLYPH)
17879 {
17880 fprintf (stderr,
17881 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17882 glyph - row->glyphs[TEXT_AREA],
17883 'C',
17884 glyph->charpos,
17885 (BUFFERP (glyph->object)
17886 ? 'B'
17887 : (STRINGP (glyph->object)
17888 ? 'S'
17889 : '-')),
17890 glyph->pixel_width,
17891 glyph->u.ch,
17892 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17893 ? glyph->u.ch
17894 : '.'),
17895 glyph->face_id,
17896 glyph->left_box_line_p,
17897 glyph->right_box_line_p);
17898 }
17899 else if (glyph->type == STRETCH_GLYPH)
17900 {
17901 fprintf (stderr,
17902 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17903 glyph - row->glyphs[TEXT_AREA],
17904 'S',
17905 glyph->charpos,
17906 (BUFFERP (glyph->object)
17907 ? 'B'
17908 : (STRINGP (glyph->object)
17909 ? 'S'
17910 : '-')),
17911 glyph->pixel_width,
17912 0,
17913 '.',
17914 glyph->face_id,
17915 glyph->left_box_line_p,
17916 glyph->right_box_line_p);
17917 }
17918 else if (glyph->type == IMAGE_GLYPH)
17919 {
17920 fprintf (stderr,
17921 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17922 glyph - row->glyphs[TEXT_AREA],
17923 'I',
17924 glyph->charpos,
17925 (BUFFERP (glyph->object)
17926 ? 'B'
17927 : (STRINGP (glyph->object)
17928 ? 'S'
17929 : '-')),
17930 glyph->pixel_width,
17931 glyph->u.img_id,
17932 '.',
17933 glyph->face_id,
17934 glyph->left_box_line_p,
17935 glyph->right_box_line_p);
17936 }
17937 else if (glyph->type == COMPOSITE_GLYPH)
17938 {
17939 fprintf (stderr,
17940 " %5td %4c %6"pI"d %c %3d 0x%05x",
17941 glyph - row->glyphs[TEXT_AREA],
17942 '+',
17943 glyph->charpos,
17944 (BUFFERP (glyph->object)
17945 ? 'B'
17946 : (STRINGP (glyph->object)
17947 ? 'S'
17948 : '-')),
17949 glyph->pixel_width,
17950 glyph->u.cmp.id);
17951 if (glyph->u.cmp.automatic)
17952 fprintf (stderr,
17953 "[%d-%d]",
17954 glyph->slice.cmp.from, glyph->slice.cmp.to);
17955 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17956 glyph->face_id,
17957 glyph->left_box_line_p,
17958 glyph->right_box_line_p);
17959 }
17960 }
17961
17962
17963 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17964 GLYPHS 0 means don't show glyph contents.
17965 GLYPHS 1 means show glyphs in short form
17966 GLYPHS > 1 means show glyphs in long form. */
17967
17968 void
17969 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17970 {
17971 if (glyphs != 1)
17972 {
17973 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17974 fprintf (stderr, "======================================================================\n");
17975
17976 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17977 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17978 vpos,
17979 MATRIX_ROW_START_CHARPOS (row),
17980 MATRIX_ROW_END_CHARPOS (row),
17981 row->used[TEXT_AREA],
17982 row->contains_overlapping_glyphs_p,
17983 row->enabled_p,
17984 row->truncated_on_left_p,
17985 row->truncated_on_right_p,
17986 row->continued_p,
17987 MATRIX_ROW_CONTINUATION_LINE_P (row),
17988 row->displays_text_p,
17989 row->ends_at_zv_p,
17990 row->fill_line_p,
17991 row->ends_in_middle_of_char_p,
17992 row->starts_in_middle_of_char_p,
17993 row->mouse_face_p,
17994 row->x,
17995 row->y,
17996 row->pixel_width,
17997 row->height,
17998 row->visible_height,
17999 row->ascent,
18000 row->phys_ascent);
18001 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18002 row->end.overlay_string_index,
18003 row->continuation_lines_width);
18004 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18005 CHARPOS (row->start.string_pos),
18006 CHARPOS (row->end.string_pos));
18007 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18008 row->end.dpvec_index);
18009 }
18010
18011 if (glyphs > 1)
18012 {
18013 int area;
18014
18015 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18016 {
18017 struct glyph *glyph = row->glyphs[area];
18018 struct glyph *glyph_end = glyph + row->used[area];
18019
18020 /* Glyph for a line end in text. */
18021 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18022 ++glyph_end;
18023
18024 if (glyph < glyph_end)
18025 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18026
18027 for (; glyph < glyph_end; ++glyph)
18028 dump_glyph (row, glyph, area);
18029 }
18030 }
18031 else if (glyphs == 1)
18032 {
18033 int area;
18034
18035 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18036 {
18037 char *s = alloca (row->used[area] + 1);
18038 int i;
18039
18040 for (i = 0; i < row->used[area]; ++i)
18041 {
18042 struct glyph *glyph = row->glyphs[area] + i;
18043 if (glyph->type == CHAR_GLYPH
18044 && glyph->u.ch < 0x80
18045 && glyph->u.ch >= ' ')
18046 s[i] = glyph->u.ch;
18047 else
18048 s[i] = '.';
18049 }
18050
18051 s[i] = '\0';
18052 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18053 }
18054 }
18055 }
18056
18057
18058 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18059 Sdump_glyph_matrix, 0, 1, "p",
18060 doc: /* Dump the current matrix of the selected window to stderr.
18061 Shows contents of glyph row structures. With non-nil
18062 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18063 glyphs in short form, otherwise show glyphs in long form. */)
18064 (Lisp_Object glyphs)
18065 {
18066 struct window *w = XWINDOW (selected_window);
18067 struct buffer *buffer = XBUFFER (w->buffer);
18068
18069 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18070 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18071 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18072 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18073 fprintf (stderr, "=============================================\n");
18074 dump_glyph_matrix (w->current_matrix,
18075 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18076 return Qnil;
18077 }
18078
18079
18080 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18081 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18082 (void)
18083 {
18084 struct frame *f = XFRAME (selected_frame);
18085 dump_glyph_matrix (f->current_matrix, 1);
18086 return Qnil;
18087 }
18088
18089
18090 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18091 doc: /* Dump glyph row ROW to stderr.
18092 GLYPH 0 means don't dump glyphs.
18093 GLYPH 1 means dump glyphs in short form.
18094 GLYPH > 1 or omitted means dump glyphs in long form. */)
18095 (Lisp_Object row, Lisp_Object glyphs)
18096 {
18097 struct glyph_matrix *matrix;
18098 EMACS_INT vpos;
18099
18100 CHECK_NUMBER (row);
18101 matrix = XWINDOW (selected_window)->current_matrix;
18102 vpos = XINT (row);
18103 if (vpos >= 0 && vpos < matrix->nrows)
18104 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18105 vpos,
18106 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18107 return Qnil;
18108 }
18109
18110
18111 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18112 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18113 GLYPH 0 means don't dump glyphs.
18114 GLYPH 1 means dump glyphs in short form.
18115 GLYPH > 1 or omitted means dump glyphs in long form. */)
18116 (Lisp_Object row, Lisp_Object glyphs)
18117 {
18118 struct frame *sf = SELECTED_FRAME ();
18119 struct glyph_matrix *m = XWINDOW (FVAR (sf, tool_bar_window))->current_matrix;
18120 EMACS_INT vpos;
18121
18122 CHECK_NUMBER (row);
18123 vpos = XINT (row);
18124 if (vpos >= 0 && vpos < m->nrows)
18125 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18126 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18127 return Qnil;
18128 }
18129
18130
18131 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18132 doc: /* Toggle tracing of redisplay.
18133 With ARG, turn tracing on if and only if ARG is positive. */)
18134 (Lisp_Object arg)
18135 {
18136 if (NILP (arg))
18137 trace_redisplay_p = !trace_redisplay_p;
18138 else
18139 {
18140 arg = Fprefix_numeric_value (arg);
18141 trace_redisplay_p = XINT (arg) > 0;
18142 }
18143
18144 return Qnil;
18145 }
18146
18147
18148 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18149 doc: /* Like `format', but print result to stderr.
18150 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18151 (ptrdiff_t nargs, Lisp_Object *args)
18152 {
18153 Lisp_Object s = Fformat (nargs, args);
18154 fprintf (stderr, "%s", SDATA (s));
18155 return Qnil;
18156 }
18157
18158 #endif /* GLYPH_DEBUG */
18159
18160
18161 \f
18162 /***********************************************************************
18163 Building Desired Matrix Rows
18164 ***********************************************************************/
18165
18166 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18167 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18168
18169 static struct glyph_row *
18170 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18171 {
18172 struct frame *f = XFRAME (WINDOW_FRAME (w));
18173 struct buffer *buffer = XBUFFER (w->buffer);
18174 struct buffer *old = current_buffer;
18175 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18176 int arrow_len = SCHARS (overlay_arrow_string);
18177 const unsigned char *arrow_end = arrow_string + arrow_len;
18178 const unsigned char *p;
18179 struct it it;
18180 int multibyte_p;
18181 int n_glyphs_before;
18182
18183 set_buffer_temp (buffer);
18184 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18185 it.glyph_row->used[TEXT_AREA] = 0;
18186 SET_TEXT_POS (it.position, 0, 0);
18187
18188 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18189 p = arrow_string;
18190 while (p < arrow_end)
18191 {
18192 Lisp_Object face, ilisp;
18193
18194 /* Get the next character. */
18195 if (multibyte_p)
18196 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18197 else
18198 {
18199 it.c = it.char_to_display = *p, it.len = 1;
18200 if (! ASCII_CHAR_P (it.c))
18201 it.char_to_display = BYTE8_TO_CHAR (it.c);
18202 }
18203 p += it.len;
18204
18205 /* Get its face. */
18206 ilisp = make_number (p - arrow_string);
18207 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18208 it.face_id = compute_char_face (f, it.char_to_display, face);
18209
18210 /* Compute its width, get its glyphs. */
18211 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18212 SET_TEXT_POS (it.position, -1, -1);
18213 PRODUCE_GLYPHS (&it);
18214
18215 /* If this character doesn't fit any more in the line, we have
18216 to remove some glyphs. */
18217 if (it.current_x > it.last_visible_x)
18218 {
18219 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18220 break;
18221 }
18222 }
18223
18224 set_buffer_temp (old);
18225 return it.glyph_row;
18226 }
18227
18228
18229 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18230 glyphs to insert is determined by produce_special_glyphs. */
18231
18232 static void
18233 insert_left_trunc_glyphs (struct it *it)
18234 {
18235 struct it truncate_it;
18236 struct glyph *from, *end, *to, *toend;
18237
18238 eassert (!FRAME_WINDOW_P (it->f)
18239 || (!it->glyph_row->reversed_p
18240 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18241 || (it->glyph_row->reversed_p
18242 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18243
18244 /* Get the truncation glyphs. */
18245 truncate_it = *it;
18246 truncate_it.current_x = 0;
18247 truncate_it.face_id = DEFAULT_FACE_ID;
18248 truncate_it.glyph_row = &scratch_glyph_row;
18249 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18250 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18251 truncate_it.object = make_number (0);
18252 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18253
18254 /* Overwrite glyphs from IT with truncation glyphs. */
18255 if (!it->glyph_row->reversed_p)
18256 {
18257 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18258
18259 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18260 end = from + tused;
18261 to = it->glyph_row->glyphs[TEXT_AREA];
18262 toend = to + it->glyph_row->used[TEXT_AREA];
18263 if (FRAME_WINDOW_P (it->f))
18264 {
18265 /* On GUI frames, when variable-size fonts are displayed,
18266 the truncation glyphs may need more pixels than the row's
18267 glyphs they overwrite. We overwrite more glyphs to free
18268 enough screen real estate, and enlarge the stretch glyph
18269 on the right (see display_line), if there is one, to
18270 preserve the screen position of the truncation glyphs on
18271 the right. */
18272 int w = 0;
18273 struct glyph *g = to;
18274 short used;
18275
18276 /* The first glyph could be partially visible, in which case
18277 it->glyph_row->x will be negative. But we want the left
18278 truncation glyphs to be aligned at the left margin of the
18279 window, so we override the x coordinate at which the row
18280 will begin. */
18281 it->glyph_row->x = 0;
18282 while (g < toend && w < it->truncation_pixel_width)
18283 {
18284 w += g->pixel_width;
18285 ++g;
18286 }
18287 if (g - to - tused > 0)
18288 {
18289 memmove (to + tused, g, (toend - g) * sizeof(*g));
18290 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18291 }
18292 used = it->glyph_row->used[TEXT_AREA];
18293 if (it->glyph_row->truncated_on_right_p
18294 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18295 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18296 == STRETCH_GLYPH)
18297 {
18298 int extra = w - it->truncation_pixel_width;
18299
18300 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18301 }
18302 }
18303
18304 while (from < end)
18305 *to++ = *from++;
18306
18307 /* There may be padding glyphs left over. Overwrite them too. */
18308 if (!FRAME_WINDOW_P (it->f))
18309 {
18310 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18311 {
18312 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18313 while (from < end)
18314 *to++ = *from++;
18315 }
18316 }
18317
18318 if (to > toend)
18319 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18320 }
18321 else
18322 {
18323 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18324
18325 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18326 that back to front. */
18327 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18328 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18329 toend = it->glyph_row->glyphs[TEXT_AREA];
18330 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18331 if (FRAME_WINDOW_P (it->f))
18332 {
18333 int w = 0;
18334 struct glyph *g = to;
18335
18336 while (g >= toend && w < it->truncation_pixel_width)
18337 {
18338 w += g->pixel_width;
18339 --g;
18340 }
18341 if (to - g - tused > 0)
18342 to = g + tused;
18343 if (it->glyph_row->truncated_on_right_p
18344 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18345 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18346 {
18347 int extra = w - it->truncation_pixel_width;
18348
18349 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18350 }
18351 }
18352
18353 while (from >= end && to >= toend)
18354 *to-- = *from--;
18355 if (!FRAME_WINDOW_P (it->f))
18356 {
18357 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18358 {
18359 from =
18360 truncate_it.glyph_row->glyphs[TEXT_AREA]
18361 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18362 while (from >= end && to >= toend)
18363 *to-- = *from--;
18364 }
18365 }
18366 if (from >= end)
18367 {
18368 /* Need to free some room before prepending additional
18369 glyphs. */
18370 int move_by = from - end + 1;
18371 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18372 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18373
18374 for ( ; g >= g0; g--)
18375 g[move_by] = *g;
18376 while (from >= end)
18377 *to-- = *from--;
18378 it->glyph_row->used[TEXT_AREA] += move_by;
18379 }
18380 }
18381 }
18382
18383 /* Compute the hash code for ROW. */
18384 unsigned
18385 row_hash (struct glyph_row *row)
18386 {
18387 int area, k;
18388 unsigned hashval = 0;
18389
18390 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18391 for (k = 0; k < row->used[area]; ++k)
18392 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18393 + row->glyphs[area][k].u.val
18394 + row->glyphs[area][k].face_id
18395 + row->glyphs[area][k].padding_p
18396 + (row->glyphs[area][k].type << 2));
18397
18398 return hashval;
18399 }
18400
18401 /* Compute the pixel height and width of IT->glyph_row.
18402
18403 Most of the time, ascent and height of a display line will be equal
18404 to the max_ascent and max_height values of the display iterator
18405 structure. This is not the case if
18406
18407 1. We hit ZV without displaying anything. In this case, max_ascent
18408 and max_height will be zero.
18409
18410 2. We have some glyphs that don't contribute to the line height.
18411 (The glyph row flag contributes_to_line_height_p is for future
18412 pixmap extensions).
18413
18414 The first case is easily covered by using default values because in
18415 these cases, the line height does not really matter, except that it
18416 must not be zero. */
18417
18418 static void
18419 compute_line_metrics (struct it *it)
18420 {
18421 struct glyph_row *row = it->glyph_row;
18422
18423 if (FRAME_WINDOW_P (it->f))
18424 {
18425 int i, min_y, max_y;
18426
18427 /* The line may consist of one space only, that was added to
18428 place the cursor on it. If so, the row's height hasn't been
18429 computed yet. */
18430 if (row->height == 0)
18431 {
18432 if (it->max_ascent + it->max_descent == 0)
18433 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18434 row->ascent = it->max_ascent;
18435 row->height = it->max_ascent + it->max_descent;
18436 row->phys_ascent = it->max_phys_ascent;
18437 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18438 row->extra_line_spacing = it->max_extra_line_spacing;
18439 }
18440
18441 /* Compute the width of this line. */
18442 row->pixel_width = row->x;
18443 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18444 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18445
18446 eassert (row->pixel_width >= 0);
18447 eassert (row->ascent >= 0 && row->height > 0);
18448
18449 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18450 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18451
18452 /* If first line's physical ascent is larger than its logical
18453 ascent, use the physical ascent, and make the row taller.
18454 This makes accented characters fully visible. */
18455 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18456 && row->phys_ascent > row->ascent)
18457 {
18458 row->height += row->phys_ascent - row->ascent;
18459 row->ascent = row->phys_ascent;
18460 }
18461
18462 /* Compute how much of the line is visible. */
18463 row->visible_height = row->height;
18464
18465 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18466 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18467
18468 if (row->y < min_y)
18469 row->visible_height -= min_y - row->y;
18470 if (row->y + row->height > max_y)
18471 row->visible_height -= row->y + row->height - max_y;
18472 }
18473 else
18474 {
18475 row->pixel_width = row->used[TEXT_AREA];
18476 if (row->continued_p)
18477 row->pixel_width -= it->continuation_pixel_width;
18478 else if (row->truncated_on_right_p)
18479 row->pixel_width -= it->truncation_pixel_width;
18480 row->ascent = row->phys_ascent = 0;
18481 row->height = row->phys_height = row->visible_height = 1;
18482 row->extra_line_spacing = 0;
18483 }
18484
18485 /* Compute a hash code for this row. */
18486 row->hash = row_hash (row);
18487
18488 it->max_ascent = it->max_descent = 0;
18489 it->max_phys_ascent = it->max_phys_descent = 0;
18490 }
18491
18492
18493 /* Append one space to the glyph row of iterator IT if doing a
18494 window-based redisplay. The space has the same face as
18495 IT->face_id. Value is non-zero if a space was added.
18496
18497 This function is called to make sure that there is always one glyph
18498 at the end of a glyph row that the cursor can be set on under
18499 window-systems. (If there weren't such a glyph we would not know
18500 how wide and tall a box cursor should be displayed).
18501
18502 At the same time this space let's a nicely handle clearing to the
18503 end of the line if the row ends in italic text. */
18504
18505 static int
18506 append_space_for_newline (struct it *it, int default_face_p)
18507 {
18508 if (FRAME_WINDOW_P (it->f))
18509 {
18510 int n = it->glyph_row->used[TEXT_AREA];
18511
18512 if (it->glyph_row->glyphs[TEXT_AREA] + n
18513 < it->glyph_row->glyphs[1 + TEXT_AREA])
18514 {
18515 /* Save some values that must not be changed.
18516 Must save IT->c and IT->len because otherwise
18517 ITERATOR_AT_END_P wouldn't work anymore after
18518 append_space_for_newline has been called. */
18519 enum display_element_type saved_what = it->what;
18520 int saved_c = it->c, saved_len = it->len;
18521 int saved_char_to_display = it->char_to_display;
18522 int saved_x = it->current_x;
18523 int saved_face_id = it->face_id;
18524 struct text_pos saved_pos;
18525 Lisp_Object saved_object;
18526 struct face *face;
18527
18528 saved_object = it->object;
18529 saved_pos = it->position;
18530
18531 it->what = IT_CHARACTER;
18532 memset (&it->position, 0, sizeof it->position);
18533 it->object = make_number (0);
18534 it->c = it->char_to_display = ' ';
18535 it->len = 1;
18536
18537 /* If the default face was remapped, be sure to use the
18538 remapped face for the appended newline. */
18539 if (default_face_p)
18540 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18541 else if (it->face_before_selective_p)
18542 it->face_id = it->saved_face_id;
18543 face = FACE_FROM_ID (it->f, it->face_id);
18544 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18545
18546 PRODUCE_GLYPHS (it);
18547
18548 it->override_ascent = -1;
18549 it->constrain_row_ascent_descent_p = 0;
18550 it->current_x = saved_x;
18551 it->object = saved_object;
18552 it->position = saved_pos;
18553 it->what = saved_what;
18554 it->face_id = saved_face_id;
18555 it->len = saved_len;
18556 it->c = saved_c;
18557 it->char_to_display = saved_char_to_display;
18558 return 1;
18559 }
18560 }
18561
18562 return 0;
18563 }
18564
18565
18566 /* Extend the face of the last glyph in the text area of IT->glyph_row
18567 to the end of the display line. Called from display_line. If the
18568 glyph row is empty, add a space glyph to it so that we know the
18569 face to draw. Set the glyph row flag fill_line_p. If the glyph
18570 row is R2L, prepend a stretch glyph to cover the empty space to the
18571 left of the leftmost glyph. */
18572
18573 static void
18574 extend_face_to_end_of_line (struct it *it)
18575 {
18576 struct face *face, *default_face;
18577 struct frame *f = it->f;
18578
18579 /* If line is already filled, do nothing. Non window-system frames
18580 get a grace of one more ``pixel'' because their characters are
18581 1-``pixel'' wide, so they hit the equality too early. This grace
18582 is needed only for R2L rows that are not continued, to produce
18583 one extra blank where we could display the cursor. */
18584 if (it->current_x >= it->last_visible_x
18585 + (!FRAME_WINDOW_P (f)
18586 && it->glyph_row->reversed_p
18587 && !it->glyph_row->continued_p))
18588 return;
18589
18590 /* The default face, possibly remapped. */
18591 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18592
18593 /* Face extension extends the background and box of IT->face_id
18594 to the end of the line. If the background equals the background
18595 of the frame, we don't have to do anything. */
18596 if (it->face_before_selective_p)
18597 face = FACE_FROM_ID (f, it->saved_face_id);
18598 else
18599 face = FACE_FROM_ID (f, it->face_id);
18600
18601 if (FRAME_WINDOW_P (f)
18602 && it->glyph_row->displays_text_p
18603 && face->box == FACE_NO_BOX
18604 && face->background == FRAME_BACKGROUND_PIXEL (f)
18605 && !face->stipple
18606 && !it->glyph_row->reversed_p)
18607 return;
18608
18609 /* Set the glyph row flag indicating that the face of the last glyph
18610 in the text area has to be drawn to the end of the text area. */
18611 it->glyph_row->fill_line_p = 1;
18612
18613 /* If current character of IT is not ASCII, make sure we have the
18614 ASCII face. This will be automatically undone the next time
18615 get_next_display_element returns a multibyte character. Note
18616 that the character will always be single byte in unibyte
18617 text. */
18618 if (!ASCII_CHAR_P (it->c))
18619 {
18620 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18621 }
18622
18623 if (FRAME_WINDOW_P (f))
18624 {
18625 /* If the row is empty, add a space with the current face of IT,
18626 so that we know which face to draw. */
18627 if (it->glyph_row->used[TEXT_AREA] == 0)
18628 {
18629 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18630 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18631 it->glyph_row->used[TEXT_AREA] = 1;
18632 }
18633 #ifdef HAVE_WINDOW_SYSTEM
18634 if (it->glyph_row->reversed_p)
18635 {
18636 /* Prepend a stretch glyph to the row, such that the
18637 rightmost glyph will be drawn flushed all the way to the
18638 right margin of the window. The stretch glyph that will
18639 occupy the empty space, if any, to the left of the
18640 glyphs. */
18641 struct font *font = face->font ? face->font : FRAME_FONT (f);
18642 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18643 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18644 struct glyph *g;
18645 int row_width, stretch_ascent, stretch_width;
18646 struct text_pos saved_pos;
18647 int saved_face_id, saved_avoid_cursor;
18648
18649 for (row_width = 0, g = row_start; g < row_end; g++)
18650 row_width += g->pixel_width;
18651 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18652 if (stretch_width > 0)
18653 {
18654 stretch_ascent =
18655 (((it->ascent + it->descent)
18656 * FONT_BASE (font)) / FONT_HEIGHT (font));
18657 saved_pos = it->position;
18658 memset (&it->position, 0, sizeof it->position);
18659 saved_avoid_cursor = it->avoid_cursor_p;
18660 it->avoid_cursor_p = 1;
18661 saved_face_id = it->face_id;
18662 /* The last row's stretch glyph should get the default
18663 face, to avoid painting the rest of the window with
18664 the region face, if the region ends at ZV. */
18665 if (it->glyph_row->ends_at_zv_p)
18666 it->face_id = default_face->id;
18667 else
18668 it->face_id = face->id;
18669 append_stretch_glyph (it, make_number (0), stretch_width,
18670 it->ascent + it->descent, stretch_ascent);
18671 it->position = saved_pos;
18672 it->avoid_cursor_p = saved_avoid_cursor;
18673 it->face_id = saved_face_id;
18674 }
18675 }
18676 #endif /* HAVE_WINDOW_SYSTEM */
18677 }
18678 else
18679 {
18680 /* Save some values that must not be changed. */
18681 int saved_x = it->current_x;
18682 struct text_pos saved_pos;
18683 Lisp_Object saved_object;
18684 enum display_element_type saved_what = it->what;
18685 int saved_face_id = it->face_id;
18686
18687 saved_object = it->object;
18688 saved_pos = it->position;
18689
18690 it->what = IT_CHARACTER;
18691 memset (&it->position, 0, sizeof it->position);
18692 it->object = make_number (0);
18693 it->c = it->char_to_display = ' ';
18694 it->len = 1;
18695 /* The last row's blank glyphs should get the default face, to
18696 avoid painting the rest of the window with the region face,
18697 if the region ends at ZV. */
18698 if (it->glyph_row->ends_at_zv_p)
18699 it->face_id = default_face->id;
18700 else
18701 it->face_id = face->id;
18702
18703 PRODUCE_GLYPHS (it);
18704
18705 while (it->current_x <= it->last_visible_x)
18706 PRODUCE_GLYPHS (it);
18707
18708 /* Don't count these blanks really. It would let us insert a left
18709 truncation glyph below and make us set the cursor on them, maybe. */
18710 it->current_x = saved_x;
18711 it->object = saved_object;
18712 it->position = saved_pos;
18713 it->what = saved_what;
18714 it->face_id = saved_face_id;
18715 }
18716 }
18717
18718
18719 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18720 trailing whitespace. */
18721
18722 static int
18723 trailing_whitespace_p (ptrdiff_t charpos)
18724 {
18725 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18726 int c = 0;
18727
18728 while (bytepos < ZV_BYTE
18729 && (c = FETCH_CHAR (bytepos),
18730 c == ' ' || c == '\t'))
18731 ++bytepos;
18732
18733 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18734 {
18735 if (bytepos != PT_BYTE)
18736 return 1;
18737 }
18738 return 0;
18739 }
18740
18741
18742 /* Highlight trailing whitespace, if any, in ROW. */
18743
18744 static void
18745 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18746 {
18747 int used = row->used[TEXT_AREA];
18748
18749 if (used)
18750 {
18751 struct glyph *start = row->glyphs[TEXT_AREA];
18752 struct glyph *glyph = start + used - 1;
18753
18754 if (row->reversed_p)
18755 {
18756 /* Right-to-left rows need to be processed in the opposite
18757 direction, so swap the edge pointers. */
18758 glyph = start;
18759 start = row->glyphs[TEXT_AREA] + used - 1;
18760 }
18761
18762 /* Skip over glyphs inserted to display the cursor at the
18763 end of a line, for extending the face of the last glyph
18764 to the end of the line on terminals, and for truncation
18765 and continuation glyphs. */
18766 if (!row->reversed_p)
18767 {
18768 while (glyph >= start
18769 && glyph->type == CHAR_GLYPH
18770 && INTEGERP (glyph->object))
18771 --glyph;
18772 }
18773 else
18774 {
18775 while (glyph <= start
18776 && glyph->type == CHAR_GLYPH
18777 && INTEGERP (glyph->object))
18778 ++glyph;
18779 }
18780
18781 /* If last glyph is a space or stretch, and it's trailing
18782 whitespace, set the face of all trailing whitespace glyphs in
18783 IT->glyph_row to `trailing-whitespace'. */
18784 if ((row->reversed_p ? glyph <= start : glyph >= start)
18785 && BUFFERP (glyph->object)
18786 && (glyph->type == STRETCH_GLYPH
18787 || (glyph->type == CHAR_GLYPH
18788 && glyph->u.ch == ' '))
18789 && trailing_whitespace_p (glyph->charpos))
18790 {
18791 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18792 if (face_id < 0)
18793 return;
18794
18795 if (!row->reversed_p)
18796 {
18797 while (glyph >= start
18798 && BUFFERP (glyph->object)
18799 && (glyph->type == STRETCH_GLYPH
18800 || (glyph->type == CHAR_GLYPH
18801 && glyph->u.ch == ' ')))
18802 (glyph--)->face_id = face_id;
18803 }
18804 else
18805 {
18806 while (glyph <= start
18807 && BUFFERP (glyph->object)
18808 && (glyph->type == STRETCH_GLYPH
18809 || (glyph->type == CHAR_GLYPH
18810 && glyph->u.ch == ' ')))
18811 (glyph++)->face_id = face_id;
18812 }
18813 }
18814 }
18815 }
18816
18817
18818 /* Value is non-zero if glyph row ROW should be
18819 used to hold the cursor. */
18820
18821 static int
18822 cursor_row_p (struct glyph_row *row)
18823 {
18824 int result = 1;
18825
18826 if (PT == CHARPOS (row->end.pos)
18827 || PT == MATRIX_ROW_END_CHARPOS (row))
18828 {
18829 /* Suppose the row ends on a string.
18830 Unless the row is continued, that means it ends on a newline
18831 in the string. If it's anything other than a display string
18832 (e.g., a before-string from an overlay), we don't want the
18833 cursor there. (This heuristic seems to give the optimal
18834 behavior for the various types of multi-line strings.)
18835 One exception: if the string has `cursor' property on one of
18836 its characters, we _do_ want the cursor there. */
18837 if (CHARPOS (row->end.string_pos) >= 0)
18838 {
18839 if (row->continued_p)
18840 result = 1;
18841 else
18842 {
18843 /* Check for `display' property. */
18844 struct glyph *beg = row->glyphs[TEXT_AREA];
18845 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18846 struct glyph *glyph;
18847
18848 result = 0;
18849 for (glyph = end; glyph >= beg; --glyph)
18850 if (STRINGP (glyph->object))
18851 {
18852 Lisp_Object prop
18853 = Fget_char_property (make_number (PT),
18854 Qdisplay, Qnil);
18855 result =
18856 (!NILP (prop)
18857 && display_prop_string_p (prop, glyph->object));
18858 /* If there's a `cursor' property on one of the
18859 string's characters, this row is a cursor row,
18860 even though this is not a display string. */
18861 if (!result)
18862 {
18863 Lisp_Object s = glyph->object;
18864
18865 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18866 {
18867 ptrdiff_t gpos = glyph->charpos;
18868
18869 if (!NILP (Fget_char_property (make_number (gpos),
18870 Qcursor, s)))
18871 {
18872 result = 1;
18873 break;
18874 }
18875 }
18876 }
18877 break;
18878 }
18879 }
18880 }
18881 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18882 {
18883 /* If the row ends in middle of a real character,
18884 and the line is continued, we want the cursor here.
18885 That's because CHARPOS (ROW->end.pos) would equal
18886 PT if PT is before the character. */
18887 if (!row->ends_in_ellipsis_p)
18888 result = row->continued_p;
18889 else
18890 /* If the row ends in an ellipsis, then
18891 CHARPOS (ROW->end.pos) will equal point after the
18892 invisible text. We want that position to be displayed
18893 after the ellipsis. */
18894 result = 0;
18895 }
18896 /* If the row ends at ZV, display the cursor at the end of that
18897 row instead of at the start of the row below. */
18898 else if (row->ends_at_zv_p)
18899 result = 1;
18900 else
18901 result = 0;
18902 }
18903
18904 return result;
18905 }
18906
18907 \f
18908
18909 /* Push the property PROP so that it will be rendered at the current
18910 position in IT. Return 1 if PROP was successfully pushed, 0
18911 otherwise. Called from handle_line_prefix to handle the
18912 `line-prefix' and `wrap-prefix' properties. */
18913
18914 static int
18915 push_prefix_prop (struct it *it, Lisp_Object prop)
18916 {
18917 struct text_pos pos =
18918 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18919
18920 eassert (it->method == GET_FROM_BUFFER
18921 || it->method == GET_FROM_DISPLAY_VECTOR
18922 || it->method == GET_FROM_STRING);
18923
18924 /* We need to save the current buffer/string position, so it will be
18925 restored by pop_it, because iterate_out_of_display_property
18926 depends on that being set correctly, but some situations leave
18927 it->position not yet set when this function is called. */
18928 push_it (it, &pos);
18929
18930 if (STRINGP (prop))
18931 {
18932 if (SCHARS (prop) == 0)
18933 {
18934 pop_it (it);
18935 return 0;
18936 }
18937
18938 it->string = prop;
18939 it->string_from_prefix_prop_p = 1;
18940 it->multibyte_p = STRING_MULTIBYTE (it->string);
18941 it->current.overlay_string_index = -1;
18942 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18943 it->end_charpos = it->string_nchars = SCHARS (it->string);
18944 it->method = GET_FROM_STRING;
18945 it->stop_charpos = 0;
18946 it->prev_stop = 0;
18947 it->base_level_stop = 0;
18948
18949 /* Force paragraph direction to be that of the parent
18950 buffer/string. */
18951 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18952 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18953 else
18954 it->paragraph_embedding = L2R;
18955
18956 /* Set up the bidi iterator for this display string. */
18957 if (it->bidi_p)
18958 {
18959 it->bidi_it.string.lstring = it->string;
18960 it->bidi_it.string.s = NULL;
18961 it->bidi_it.string.schars = it->end_charpos;
18962 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18963 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18964 it->bidi_it.string.unibyte = !it->multibyte_p;
18965 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18966 }
18967 }
18968 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18969 {
18970 it->method = GET_FROM_STRETCH;
18971 it->object = prop;
18972 }
18973 #ifdef HAVE_WINDOW_SYSTEM
18974 else if (IMAGEP (prop))
18975 {
18976 it->what = IT_IMAGE;
18977 it->image_id = lookup_image (it->f, prop);
18978 it->method = GET_FROM_IMAGE;
18979 }
18980 #endif /* HAVE_WINDOW_SYSTEM */
18981 else
18982 {
18983 pop_it (it); /* bogus display property, give up */
18984 return 0;
18985 }
18986
18987 return 1;
18988 }
18989
18990 /* Return the character-property PROP at the current position in IT. */
18991
18992 static Lisp_Object
18993 get_it_property (struct it *it, Lisp_Object prop)
18994 {
18995 Lisp_Object position;
18996
18997 if (STRINGP (it->object))
18998 position = make_number (IT_STRING_CHARPOS (*it));
18999 else if (BUFFERP (it->object))
19000 position = make_number (IT_CHARPOS (*it));
19001 else
19002 return Qnil;
19003
19004 return Fget_char_property (position, prop, it->object);
19005 }
19006
19007 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19008
19009 static void
19010 handle_line_prefix (struct it *it)
19011 {
19012 Lisp_Object prefix;
19013
19014 if (it->continuation_lines_width > 0)
19015 {
19016 prefix = get_it_property (it, Qwrap_prefix);
19017 if (NILP (prefix))
19018 prefix = Vwrap_prefix;
19019 }
19020 else
19021 {
19022 prefix = get_it_property (it, Qline_prefix);
19023 if (NILP (prefix))
19024 prefix = Vline_prefix;
19025 }
19026 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19027 {
19028 /* If the prefix is wider than the window, and we try to wrap
19029 it, it would acquire its own wrap prefix, and so on till the
19030 iterator stack overflows. So, don't wrap the prefix. */
19031 it->line_wrap = TRUNCATE;
19032 it->avoid_cursor_p = 1;
19033 }
19034 }
19035
19036 \f
19037
19038 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19039 only for R2L lines from display_line and display_string, when they
19040 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19041 the line/string needs to be continued on the next glyph row. */
19042 static void
19043 unproduce_glyphs (struct it *it, int n)
19044 {
19045 struct glyph *glyph, *end;
19046
19047 eassert (it->glyph_row);
19048 eassert (it->glyph_row->reversed_p);
19049 eassert (it->area == TEXT_AREA);
19050 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19051
19052 if (n > it->glyph_row->used[TEXT_AREA])
19053 n = it->glyph_row->used[TEXT_AREA];
19054 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19055 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19056 for ( ; glyph < end; glyph++)
19057 glyph[-n] = *glyph;
19058 }
19059
19060 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19061 and ROW->maxpos. */
19062 static void
19063 find_row_edges (struct it *it, struct glyph_row *row,
19064 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19065 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19066 {
19067 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19068 lines' rows is implemented for bidi-reordered rows. */
19069
19070 /* ROW->minpos is the value of min_pos, the minimal buffer position
19071 we have in ROW, or ROW->start.pos if that is smaller. */
19072 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19073 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19074 else
19075 /* We didn't find buffer positions smaller than ROW->start, or
19076 didn't find _any_ valid buffer positions in any of the glyphs,
19077 so we must trust the iterator's computed positions. */
19078 row->minpos = row->start.pos;
19079 if (max_pos <= 0)
19080 {
19081 max_pos = CHARPOS (it->current.pos);
19082 max_bpos = BYTEPOS (it->current.pos);
19083 }
19084
19085 /* Here are the various use-cases for ending the row, and the
19086 corresponding values for ROW->maxpos:
19087
19088 Line ends in a newline from buffer eol_pos + 1
19089 Line is continued from buffer max_pos + 1
19090 Line is truncated on right it->current.pos
19091 Line ends in a newline from string max_pos + 1(*)
19092 (*) + 1 only when line ends in a forward scan
19093 Line is continued from string max_pos
19094 Line is continued from display vector max_pos
19095 Line is entirely from a string min_pos == max_pos
19096 Line is entirely from a display vector min_pos == max_pos
19097 Line that ends at ZV ZV
19098
19099 If you discover other use-cases, please add them here as
19100 appropriate. */
19101 if (row->ends_at_zv_p)
19102 row->maxpos = it->current.pos;
19103 else if (row->used[TEXT_AREA])
19104 {
19105 int seen_this_string = 0;
19106 struct glyph_row *r1 = row - 1;
19107
19108 /* Did we see the same display string on the previous row? */
19109 if (STRINGP (it->object)
19110 /* this is not the first row */
19111 && row > it->w->desired_matrix->rows
19112 /* previous row is not the header line */
19113 && !r1->mode_line_p
19114 /* previous row also ends in a newline from a string */
19115 && r1->ends_in_newline_from_string_p)
19116 {
19117 struct glyph *start, *end;
19118
19119 /* Search for the last glyph of the previous row that came
19120 from buffer or string. Depending on whether the row is
19121 L2R or R2L, we need to process it front to back or the
19122 other way round. */
19123 if (!r1->reversed_p)
19124 {
19125 start = r1->glyphs[TEXT_AREA];
19126 end = start + r1->used[TEXT_AREA];
19127 /* Glyphs inserted by redisplay have an integer (zero)
19128 as their object. */
19129 while (end > start
19130 && INTEGERP ((end - 1)->object)
19131 && (end - 1)->charpos <= 0)
19132 --end;
19133 if (end > start)
19134 {
19135 if (EQ ((end - 1)->object, it->object))
19136 seen_this_string = 1;
19137 }
19138 else
19139 /* If all the glyphs of the previous row were inserted
19140 by redisplay, it means the previous row was
19141 produced from a single newline, which is only
19142 possible if that newline came from the same string
19143 as the one which produced this ROW. */
19144 seen_this_string = 1;
19145 }
19146 else
19147 {
19148 end = r1->glyphs[TEXT_AREA] - 1;
19149 start = end + r1->used[TEXT_AREA];
19150 while (end < start
19151 && INTEGERP ((end + 1)->object)
19152 && (end + 1)->charpos <= 0)
19153 ++end;
19154 if (end < start)
19155 {
19156 if (EQ ((end + 1)->object, it->object))
19157 seen_this_string = 1;
19158 }
19159 else
19160 seen_this_string = 1;
19161 }
19162 }
19163 /* Take note of each display string that covers a newline only
19164 once, the first time we see it. This is for when a display
19165 string includes more than one newline in it. */
19166 if (row->ends_in_newline_from_string_p && !seen_this_string)
19167 {
19168 /* If we were scanning the buffer forward when we displayed
19169 the string, we want to account for at least one buffer
19170 position that belongs to this row (position covered by
19171 the display string), so that cursor positioning will
19172 consider this row as a candidate when point is at the end
19173 of the visual line represented by this row. This is not
19174 required when scanning back, because max_pos will already
19175 have a much larger value. */
19176 if (CHARPOS (row->end.pos) > max_pos)
19177 INC_BOTH (max_pos, max_bpos);
19178 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19179 }
19180 else if (CHARPOS (it->eol_pos) > 0)
19181 SET_TEXT_POS (row->maxpos,
19182 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19183 else if (row->continued_p)
19184 {
19185 /* If max_pos is different from IT's current position, it
19186 means IT->method does not belong to the display element
19187 at max_pos. However, it also means that the display
19188 element at max_pos was displayed in its entirety on this
19189 line, which is equivalent to saying that the next line
19190 starts at the next buffer position. */
19191 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19192 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19193 else
19194 {
19195 INC_BOTH (max_pos, max_bpos);
19196 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19197 }
19198 }
19199 else if (row->truncated_on_right_p)
19200 /* display_line already called reseat_at_next_visible_line_start,
19201 which puts the iterator at the beginning of the next line, in
19202 the logical order. */
19203 row->maxpos = it->current.pos;
19204 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19205 /* A line that is entirely from a string/image/stretch... */
19206 row->maxpos = row->minpos;
19207 else
19208 abort ();
19209 }
19210 else
19211 row->maxpos = it->current.pos;
19212 }
19213
19214 /* Construct the glyph row IT->glyph_row in the desired matrix of
19215 IT->w from text at the current position of IT. See dispextern.h
19216 for an overview of struct it. Value is non-zero if
19217 IT->glyph_row displays text, as opposed to a line displaying ZV
19218 only. */
19219
19220 static int
19221 display_line (struct it *it)
19222 {
19223 struct glyph_row *row = it->glyph_row;
19224 Lisp_Object overlay_arrow_string;
19225 struct it wrap_it;
19226 void *wrap_data = NULL;
19227 int may_wrap = 0, wrap_x IF_LINT (= 0);
19228 int wrap_row_used = -1;
19229 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19230 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19231 int wrap_row_extra_line_spacing IF_LINT (= 0);
19232 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19233 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19234 int cvpos;
19235 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19236 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19237
19238 /* We always start displaying at hpos zero even if hscrolled. */
19239 eassert (it->hpos == 0 && it->current_x == 0);
19240
19241 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19242 >= it->w->desired_matrix->nrows)
19243 {
19244 it->w->nrows_scale_factor++;
19245 fonts_changed_p = 1;
19246 return 0;
19247 }
19248
19249 /* Is IT->w showing the region? */
19250 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19251
19252 /* Clear the result glyph row and enable it. */
19253 prepare_desired_row (row);
19254
19255 row->y = it->current_y;
19256 row->start = it->start;
19257 row->continuation_lines_width = it->continuation_lines_width;
19258 row->displays_text_p = 1;
19259 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19260 it->starts_in_middle_of_char_p = 0;
19261
19262 /* Arrange the overlays nicely for our purposes. Usually, we call
19263 display_line on only one line at a time, in which case this
19264 can't really hurt too much, or we call it on lines which appear
19265 one after another in the buffer, in which case all calls to
19266 recenter_overlay_lists but the first will be pretty cheap. */
19267 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19268
19269 /* Move over display elements that are not visible because we are
19270 hscrolled. This may stop at an x-position < IT->first_visible_x
19271 if the first glyph is partially visible or if we hit a line end. */
19272 if (it->current_x < it->first_visible_x)
19273 {
19274 enum move_it_result move_result;
19275
19276 this_line_min_pos = row->start.pos;
19277 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19278 MOVE_TO_POS | MOVE_TO_X);
19279 /* If we are under a large hscroll, move_it_in_display_line_to
19280 could hit the end of the line without reaching
19281 it->first_visible_x. Pretend that we did reach it. This is
19282 especially important on a TTY, where we will call
19283 extend_face_to_end_of_line, which needs to know how many
19284 blank glyphs to produce. */
19285 if (it->current_x < it->first_visible_x
19286 && (move_result == MOVE_NEWLINE_OR_CR
19287 || move_result == MOVE_POS_MATCH_OR_ZV))
19288 it->current_x = it->first_visible_x;
19289
19290 /* Record the smallest positions seen while we moved over
19291 display elements that are not visible. This is needed by
19292 redisplay_internal for optimizing the case where the cursor
19293 stays inside the same line. The rest of this function only
19294 considers positions that are actually displayed, so
19295 RECORD_MAX_MIN_POS will not otherwise record positions that
19296 are hscrolled to the left of the left edge of the window. */
19297 min_pos = CHARPOS (this_line_min_pos);
19298 min_bpos = BYTEPOS (this_line_min_pos);
19299 }
19300 else
19301 {
19302 /* We only do this when not calling `move_it_in_display_line_to'
19303 above, because move_it_in_display_line_to calls
19304 handle_line_prefix itself. */
19305 handle_line_prefix (it);
19306 }
19307
19308 /* Get the initial row height. This is either the height of the
19309 text hscrolled, if there is any, or zero. */
19310 row->ascent = it->max_ascent;
19311 row->height = it->max_ascent + it->max_descent;
19312 row->phys_ascent = it->max_phys_ascent;
19313 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19314 row->extra_line_spacing = it->max_extra_line_spacing;
19315
19316 /* Utility macro to record max and min buffer positions seen until now. */
19317 #define RECORD_MAX_MIN_POS(IT) \
19318 do \
19319 { \
19320 int composition_p = !STRINGP ((IT)->string) \
19321 && ((IT)->what == IT_COMPOSITION); \
19322 ptrdiff_t current_pos = \
19323 composition_p ? (IT)->cmp_it.charpos \
19324 : IT_CHARPOS (*(IT)); \
19325 ptrdiff_t current_bpos = \
19326 composition_p ? CHAR_TO_BYTE (current_pos) \
19327 : IT_BYTEPOS (*(IT)); \
19328 if (current_pos < min_pos) \
19329 { \
19330 min_pos = current_pos; \
19331 min_bpos = current_bpos; \
19332 } \
19333 if (IT_CHARPOS (*it) > max_pos) \
19334 { \
19335 max_pos = IT_CHARPOS (*it); \
19336 max_bpos = IT_BYTEPOS (*it); \
19337 } \
19338 } \
19339 while (0)
19340
19341 /* Loop generating characters. The loop is left with IT on the next
19342 character to display. */
19343 while (1)
19344 {
19345 int n_glyphs_before, hpos_before, x_before;
19346 int x, nglyphs;
19347 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19348
19349 /* Retrieve the next thing to display. Value is zero if end of
19350 buffer reached. */
19351 if (!get_next_display_element (it))
19352 {
19353 /* Maybe add a space at the end of this line that is used to
19354 display the cursor there under X. Set the charpos of the
19355 first glyph of blank lines not corresponding to any text
19356 to -1. */
19357 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19358 row->exact_window_width_line_p = 1;
19359 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19360 || row->used[TEXT_AREA] == 0)
19361 {
19362 row->glyphs[TEXT_AREA]->charpos = -1;
19363 row->displays_text_p = 0;
19364
19365 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19366 && (!MINI_WINDOW_P (it->w)
19367 || (minibuf_level && EQ (it->window, minibuf_window))))
19368 row->indicate_empty_line_p = 1;
19369 }
19370
19371 it->continuation_lines_width = 0;
19372 row->ends_at_zv_p = 1;
19373 /* A row that displays right-to-left text must always have
19374 its last face extended all the way to the end of line,
19375 even if this row ends in ZV, because we still write to
19376 the screen left to right. We also need to extend the
19377 last face if the default face is remapped to some
19378 different face, otherwise the functions that clear
19379 portions of the screen will clear with the default face's
19380 background color. */
19381 if (row->reversed_p
19382 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19383 extend_face_to_end_of_line (it);
19384 break;
19385 }
19386
19387 /* Now, get the metrics of what we want to display. This also
19388 generates glyphs in `row' (which is IT->glyph_row). */
19389 n_glyphs_before = row->used[TEXT_AREA];
19390 x = it->current_x;
19391
19392 /* Remember the line height so far in case the next element doesn't
19393 fit on the line. */
19394 if (it->line_wrap != TRUNCATE)
19395 {
19396 ascent = it->max_ascent;
19397 descent = it->max_descent;
19398 phys_ascent = it->max_phys_ascent;
19399 phys_descent = it->max_phys_descent;
19400
19401 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19402 {
19403 if (IT_DISPLAYING_WHITESPACE (it))
19404 may_wrap = 1;
19405 else if (may_wrap)
19406 {
19407 SAVE_IT (wrap_it, *it, wrap_data);
19408 wrap_x = x;
19409 wrap_row_used = row->used[TEXT_AREA];
19410 wrap_row_ascent = row->ascent;
19411 wrap_row_height = row->height;
19412 wrap_row_phys_ascent = row->phys_ascent;
19413 wrap_row_phys_height = row->phys_height;
19414 wrap_row_extra_line_spacing = row->extra_line_spacing;
19415 wrap_row_min_pos = min_pos;
19416 wrap_row_min_bpos = min_bpos;
19417 wrap_row_max_pos = max_pos;
19418 wrap_row_max_bpos = max_bpos;
19419 may_wrap = 0;
19420 }
19421 }
19422 }
19423
19424 PRODUCE_GLYPHS (it);
19425
19426 /* If this display element was in marginal areas, continue with
19427 the next one. */
19428 if (it->area != TEXT_AREA)
19429 {
19430 row->ascent = max (row->ascent, it->max_ascent);
19431 row->height = max (row->height, it->max_ascent + it->max_descent);
19432 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19433 row->phys_height = max (row->phys_height,
19434 it->max_phys_ascent + it->max_phys_descent);
19435 row->extra_line_spacing = max (row->extra_line_spacing,
19436 it->max_extra_line_spacing);
19437 set_iterator_to_next (it, 1);
19438 continue;
19439 }
19440
19441 /* Does the display element fit on the line? If we truncate
19442 lines, we should draw past the right edge of the window. If
19443 we don't truncate, we want to stop so that we can display the
19444 continuation glyph before the right margin. If lines are
19445 continued, there are two possible strategies for characters
19446 resulting in more than 1 glyph (e.g. tabs): Display as many
19447 glyphs as possible in this line and leave the rest for the
19448 continuation line, or display the whole element in the next
19449 line. Original redisplay did the former, so we do it also. */
19450 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19451 hpos_before = it->hpos;
19452 x_before = x;
19453
19454 if (/* Not a newline. */
19455 nglyphs > 0
19456 /* Glyphs produced fit entirely in the line. */
19457 && it->current_x < it->last_visible_x)
19458 {
19459 it->hpos += nglyphs;
19460 row->ascent = max (row->ascent, it->max_ascent);
19461 row->height = max (row->height, it->max_ascent + it->max_descent);
19462 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19463 row->phys_height = max (row->phys_height,
19464 it->max_phys_ascent + it->max_phys_descent);
19465 row->extra_line_spacing = max (row->extra_line_spacing,
19466 it->max_extra_line_spacing);
19467 if (it->current_x - it->pixel_width < it->first_visible_x)
19468 row->x = x - it->first_visible_x;
19469 /* Record the maximum and minimum buffer positions seen so
19470 far in glyphs that will be displayed by this row. */
19471 if (it->bidi_p)
19472 RECORD_MAX_MIN_POS (it);
19473 }
19474 else
19475 {
19476 int i, new_x;
19477 struct glyph *glyph;
19478
19479 for (i = 0; i < nglyphs; ++i, x = new_x)
19480 {
19481 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19482 new_x = x + glyph->pixel_width;
19483
19484 if (/* Lines are continued. */
19485 it->line_wrap != TRUNCATE
19486 && (/* Glyph doesn't fit on the line. */
19487 new_x > it->last_visible_x
19488 /* Or it fits exactly on a window system frame. */
19489 || (new_x == it->last_visible_x
19490 && FRAME_WINDOW_P (it->f)
19491 && (row->reversed_p
19492 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19493 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19494 {
19495 /* End of a continued line. */
19496
19497 if (it->hpos == 0
19498 || (new_x == it->last_visible_x
19499 && FRAME_WINDOW_P (it->f)
19500 && (row->reversed_p
19501 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19502 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19503 {
19504 /* Current glyph is the only one on the line or
19505 fits exactly on the line. We must continue
19506 the line because we can't draw the cursor
19507 after the glyph. */
19508 row->continued_p = 1;
19509 it->current_x = new_x;
19510 it->continuation_lines_width += new_x;
19511 ++it->hpos;
19512 if (i == nglyphs - 1)
19513 {
19514 /* If line-wrap is on, check if a previous
19515 wrap point was found. */
19516 if (wrap_row_used > 0
19517 /* Even if there is a previous wrap
19518 point, continue the line here as
19519 usual, if (i) the previous character
19520 was a space or tab AND (ii) the
19521 current character is not. */
19522 && (!may_wrap
19523 || IT_DISPLAYING_WHITESPACE (it)))
19524 goto back_to_wrap;
19525
19526 /* Record the maximum and minimum buffer
19527 positions seen so far in glyphs that will be
19528 displayed by this row. */
19529 if (it->bidi_p)
19530 RECORD_MAX_MIN_POS (it);
19531 set_iterator_to_next (it, 1);
19532 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19533 {
19534 if (!get_next_display_element (it))
19535 {
19536 row->exact_window_width_line_p = 1;
19537 it->continuation_lines_width = 0;
19538 row->continued_p = 0;
19539 row->ends_at_zv_p = 1;
19540 }
19541 else if (ITERATOR_AT_END_OF_LINE_P (it))
19542 {
19543 row->continued_p = 0;
19544 row->exact_window_width_line_p = 1;
19545 }
19546 }
19547 }
19548 else if (it->bidi_p)
19549 RECORD_MAX_MIN_POS (it);
19550 }
19551 else if (CHAR_GLYPH_PADDING_P (*glyph)
19552 && !FRAME_WINDOW_P (it->f))
19553 {
19554 /* A padding glyph that doesn't fit on this line.
19555 This means the whole character doesn't fit
19556 on the line. */
19557 if (row->reversed_p)
19558 unproduce_glyphs (it, row->used[TEXT_AREA]
19559 - n_glyphs_before);
19560 row->used[TEXT_AREA] = n_glyphs_before;
19561
19562 /* Fill the rest of the row with continuation
19563 glyphs like in 20.x. */
19564 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19565 < row->glyphs[1 + TEXT_AREA])
19566 produce_special_glyphs (it, IT_CONTINUATION);
19567
19568 row->continued_p = 1;
19569 it->current_x = x_before;
19570 it->continuation_lines_width += x_before;
19571
19572 /* Restore the height to what it was before the
19573 element not fitting on the line. */
19574 it->max_ascent = ascent;
19575 it->max_descent = descent;
19576 it->max_phys_ascent = phys_ascent;
19577 it->max_phys_descent = phys_descent;
19578 }
19579 else if (wrap_row_used > 0)
19580 {
19581 back_to_wrap:
19582 if (row->reversed_p)
19583 unproduce_glyphs (it,
19584 row->used[TEXT_AREA] - wrap_row_used);
19585 RESTORE_IT (it, &wrap_it, wrap_data);
19586 it->continuation_lines_width += wrap_x;
19587 row->used[TEXT_AREA] = wrap_row_used;
19588 row->ascent = wrap_row_ascent;
19589 row->height = wrap_row_height;
19590 row->phys_ascent = wrap_row_phys_ascent;
19591 row->phys_height = wrap_row_phys_height;
19592 row->extra_line_spacing = wrap_row_extra_line_spacing;
19593 min_pos = wrap_row_min_pos;
19594 min_bpos = wrap_row_min_bpos;
19595 max_pos = wrap_row_max_pos;
19596 max_bpos = wrap_row_max_bpos;
19597 row->continued_p = 1;
19598 row->ends_at_zv_p = 0;
19599 row->exact_window_width_line_p = 0;
19600 it->continuation_lines_width += x;
19601
19602 /* Make sure that a non-default face is extended
19603 up to the right margin of the window. */
19604 extend_face_to_end_of_line (it);
19605 }
19606 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19607 {
19608 /* A TAB that extends past the right edge of the
19609 window. This produces a single glyph on
19610 window system frames. We leave the glyph in
19611 this row and let it fill the row, but don't
19612 consume the TAB. */
19613 if ((row->reversed_p
19614 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19615 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19616 produce_special_glyphs (it, IT_CONTINUATION);
19617 it->continuation_lines_width += it->last_visible_x;
19618 row->ends_in_middle_of_char_p = 1;
19619 row->continued_p = 1;
19620 glyph->pixel_width = it->last_visible_x - x;
19621 it->starts_in_middle_of_char_p = 1;
19622 }
19623 else
19624 {
19625 /* Something other than a TAB that draws past
19626 the right edge of the window. Restore
19627 positions to values before the element. */
19628 if (row->reversed_p)
19629 unproduce_glyphs (it, row->used[TEXT_AREA]
19630 - (n_glyphs_before + i));
19631 row->used[TEXT_AREA] = n_glyphs_before + i;
19632
19633 /* Display continuation glyphs. */
19634 it->current_x = x_before;
19635 it->continuation_lines_width += x;
19636 if (!FRAME_WINDOW_P (it->f)
19637 || (row->reversed_p
19638 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19639 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19640 produce_special_glyphs (it, IT_CONTINUATION);
19641 row->continued_p = 1;
19642
19643 extend_face_to_end_of_line (it);
19644
19645 if (nglyphs > 1 && i > 0)
19646 {
19647 row->ends_in_middle_of_char_p = 1;
19648 it->starts_in_middle_of_char_p = 1;
19649 }
19650
19651 /* Restore the height to what it was before the
19652 element not fitting on the line. */
19653 it->max_ascent = ascent;
19654 it->max_descent = descent;
19655 it->max_phys_ascent = phys_ascent;
19656 it->max_phys_descent = phys_descent;
19657 }
19658
19659 break;
19660 }
19661 else if (new_x > it->first_visible_x)
19662 {
19663 /* Increment number of glyphs actually displayed. */
19664 ++it->hpos;
19665
19666 /* Record the maximum and minimum buffer positions
19667 seen so far in glyphs that will be displayed by
19668 this row. */
19669 if (it->bidi_p)
19670 RECORD_MAX_MIN_POS (it);
19671
19672 if (x < it->first_visible_x)
19673 /* Glyph is partially visible, i.e. row starts at
19674 negative X position. */
19675 row->x = x - it->first_visible_x;
19676 }
19677 else
19678 {
19679 /* Glyph is completely off the left margin of the
19680 window. This should not happen because of the
19681 move_it_in_display_line at the start of this
19682 function, unless the text display area of the
19683 window is empty. */
19684 eassert (it->first_visible_x <= it->last_visible_x);
19685 }
19686 }
19687 /* Even if this display element produced no glyphs at all,
19688 we want to record its position. */
19689 if (it->bidi_p && nglyphs == 0)
19690 RECORD_MAX_MIN_POS (it);
19691
19692 row->ascent = max (row->ascent, it->max_ascent);
19693 row->height = max (row->height, it->max_ascent + it->max_descent);
19694 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19695 row->phys_height = max (row->phys_height,
19696 it->max_phys_ascent + it->max_phys_descent);
19697 row->extra_line_spacing = max (row->extra_line_spacing,
19698 it->max_extra_line_spacing);
19699
19700 /* End of this display line if row is continued. */
19701 if (row->continued_p || row->ends_at_zv_p)
19702 break;
19703 }
19704
19705 at_end_of_line:
19706 /* Is this a line end? If yes, we're also done, after making
19707 sure that a non-default face is extended up to the right
19708 margin of the window. */
19709 if (ITERATOR_AT_END_OF_LINE_P (it))
19710 {
19711 int used_before = row->used[TEXT_AREA];
19712
19713 row->ends_in_newline_from_string_p = STRINGP (it->object);
19714
19715 /* Add a space at the end of the line that is used to
19716 display the cursor there. */
19717 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19718 append_space_for_newline (it, 0);
19719
19720 /* Extend the face to the end of the line. */
19721 extend_face_to_end_of_line (it);
19722
19723 /* Make sure we have the position. */
19724 if (used_before == 0)
19725 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19726
19727 /* Record the position of the newline, for use in
19728 find_row_edges. */
19729 it->eol_pos = it->current.pos;
19730
19731 /* Consume the line end. This skips over invisible lines. */
19732 set_iterator_to_next (it, 1);
19733 it->continuation_lines_width = 0;
19734 break;
19735 }
19736
19737 /* Proceed with next display element. Note that this skips
19738 over lines invisible because of selective display. */
19739 set_iterator_to_next (it, 1);
19740
19741 /* If we truncate lines, we are done when the last displayed
19742 glyphs reach past the right margin of the window. */
19743 if (it->line_wrap == TRUNCATE
19744 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19745 ? (it->current_x >= it->last_visible_x)
19746 : (it->current_x > it->last_visible_x)))
19747 {
19748 /* Maybe add truncation glyphs. */
19749 if (!FRAME_WINDOW_P (it->f)
19750 || (row->reversed_p
19751 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19752 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19753 {
19754 int i, n;
19755
19756 if (!row->reversed_p)
19757 {
19758 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19759 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19760 break;
19761 }
19762 else
19763 {
19764 for (i = 0; i < row->used[TEXT_AREA]; i++)
19765 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19766 break;
19767 /* Remove any padding glyphs at the front of ROW, to
19768 make room for the truncation glyphs we will be
19769 adding below. The loop below always inserts at
19770 least one truncation glyph, so also remove the
19771 last glyph added to ROW. */
19772 unproduce_glyphs (it, i + 1);
19773 /* Adjust i for the loop below. */
19774 i = row->used[TEXT_AREA] - (i + 1);
19775 }
19776
19777 it->current_x = x_before;
19778 if (!FRAME_WINDOW_P (it->f))
19779 {
19780 for (n = row->used[TEXT_AREA]; i < n; ++i)
19781 {
19782 row->used[TEXT_AREA] = i;
19783 produce_special_glyphs (it, IT_TRUNCATION);
19784 }
19785 }
19786 else
19787 {
19788 row->used[TEXT_AREA] = i;
19789 produce_special_glyphs (it, IT_TRUNCATION);
19790 }
19791 }
19792 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19793 {
19794 /* Don't truncate if we can overflow newline into fringe. */
19795 if (!get_next_display_element (it))
19796 {
19797 it->continuation_lines_width = 0;
19798 row->ends_at_zv_p = 1;
19799 row->exact_window_width_line_p = 1;
19800 break;
19801 }
19802 if (ITERATOR_AT_END_OF_LINE_P (it))
19803 {
19804 row->exact_window_width_line_p = 1;
19805 goto at_end_of_line;
19806 }
19807 it->current_x = x_before;
19808 }
19809
19810 row->truncated_on_right_p = 1;
19811 it->continuation_lines_width = 0;
19812 reseat_at_next_visible_line_start (it, 0);
19813 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19814 it->hpos = hpos_before;
19815 break;
19816 }
19817 }
19818
19819 if (wrap_data)
19820 bidi_unshelve_cache (wrap_data, 1);
19821
19822 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19823 at the left window margin. */
19824 if (it->first_visible_x
19825 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19826 {
19827 if (!FRAME_WINDOW_P (it->f)
19828 || (row->reversed_p
19829 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19830 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19831 insert_left_trunc_glyphs (it);
19832 row->truncated_on_left_p = 1;
19833 }
19834
19835 /* Remember the position at which this line ends.
19836
19837 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19838 cannot be before the call to find_row_edges below, since that is
19839 where these positions are determined. */
19840 row->end = it->current;
19841 if (!it->bidi_p)
19842 {
19843 row->minpos = row->start.pos;
19844 row->maxpos = row->end.pos;
19845 }
19846 else
19847 {
19848 /* ROW->minpos and ROW->maxpos must be the smallest and
19849 `1 + the largest' buffer positions in ROW. But if ROW was
19850 bidi-reordered, these two positions can be anywhere in the
19851 row, so we must determine them now. */
19852 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19853 }
19854
19855 /* If the start of this line is the overlay arrow-position, then
19856 mark this glyph row as the one containing the overlay arrow.
19857 This is clearly a mess with variable size fonts. It would be
19858 better to let it be displayed like cursors under X. */
19859 if ((row->displays_text_p || !overlay_arrow_seen)
19860 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19861 !NILP (overlay_arrow_string)))
19862 {
19863 /* Overlay arrow in window redisplay is a fringe bitmap. */
19864 if (STRINGP (overlay_arrow_string))
19865 {
19866 struct glyph_row *arrow_row
19867 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19868 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19869 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19870 struct glyph *p = row->glyphs[TEXT_AREA];
19871 struct glyph *p2, *end;
19872
19873 /* Copy the arrow glyphs. */
19874 while (glyph < arrow_end)
19875 *p++ = *glyph++;
19876
19877 /* Throw away padding glyphs. */
19878 p2 = p;
19879 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19880 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19881 ++p2;
19882 if (p2 > p)
19883 {
19884 while (p2 < end)
19885 *p++ = *p2++;
19886 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19887 }
19888 }
19889 else
19890 {
19891 eassert (INTEGERP (overlay_arrow_string));
19892 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19893 }
19894 overlay_arrow_seen = 1;
19895 }
19896
19897 /* Highlight trailing whitespace. */
19898 if (!NILP (Vshow_trailing_whitespace))
19899 highlight_trailing_whitespace (it->f, it->glyph_row);
19900
19901 /* Compute pixel dimensions of this line. */
19902 compute_line_metrics (it);
19903
19904 /* Implementation note: No changes in the glyphs of ROW or in their
19905 faces can be done past this point, because compute_line_metrics
19906 computes ROW's hash value and stores it within the glyph_row
19907 structure. */
19908
19909 /* Record whether this row ends inside an ellipsis. */
19910 row->ends_in_ellipsis_p
19911 = (it->method == GET_FROM_DISPLAY_VECTOR
19912 && it->ellipsis_p);
19913
19914 /* Save fringe bitmaps in this row. */
19915 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19916 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19917 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19918 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19919
19920 it->left_user_fringe_bitmap = 0;
19921 it->left_user_fringe_face_id = 0;
19922 it->right_user_fringe_bitmap = 0;
19923 it->right_user_fringe_face_id = 0;
19924
19925 /* Maybe set the cursor. */
19926 cvpos = it->w->cursor.vpos;
19927 if ((cvpos < 0
19928 /* In bidi-reordered rows, keep checking for proper cursor
19929 position even if one has been found already, because buffer
19930 positions in such rows change non-linearly with ROW->VPOS,
19931 when a line is continued. One exception: when we are at ZV,
19932 display cursor on the first suitable glyph row, since all
19933 the empty rows after that also have their position set to ZV. */
19934 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19935 lines' rows is implemented for bidi-reordered rows. */
19936 || (it->bidi_p
19937 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19938 && PT >= MATRIX_ROW_START_CHARPOS (row)
19939 && PT <= MATRIX_ROW_END_CHARPOS (row)
19940 && cursor_row_p (row))
19941 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19942
19943 /* Prepare for the next line. This line starts horizontally at (X
19944 HPOS) = (0 0). Vertical positions are incremented. As a
19945 convenience for the caller, IT->glyph_row is set to the next
19946 row to be used. */
19947 it->current_x = it->hpos = 0;
19948 it->current_y += row->height;
19949 SET_TEXT_POS (it->eol_pos, 0, 0);
19950 ++it->vpos;
19951 ++it->glyph_row;
19952 /* The next row should by default use the same value of the
19953 reversed_p flag as this one. set_iterator_to_next decides when
19954 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19955 the flag accordingly. */
19956 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19957 it->glyph_row->reversed_p = row->reversed_p;
19958 it->start = row->end;
19959 return row->displays_text_p;
19960
19961 #undef RECORD_MAX_MIN_POS
19962 }
19963
19964 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19965 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19966 doc: /* Return paragraph direction at point in BUFFER.
19967 Value is either `left-to-right' or `right-to-left'.
19968 If BUFFER is omitted or nil, it defaults to the current buffer.
19969
19970 Paragraph direction determines how the text in the paragraph is displayed.
19971 In left-to-right paragraphs, text begins at the left margin of the window
19972 and the reading direction is generally left to right. In right-to-left
19973 paragraphs, text begins at the right margin and is read from right to left.
19974
19975 See also `bidi-paragraph-direction'. */)
19976 (Lisp_Object buffer)
19977 {
19978 struct buffer *buf = current_buffer;
19979 struct buffer *old = buf;
19980
19981 if (! NILP (buffer))
19982 {
19983 CHECK_BUFFER (buffer);
19984 buf = XBUFFER (buffer);
19985 }
19986
19987 if (NILP (BVAR (buf, bidi_display_reordering))
19988 || NILP (BVAR (buf, enable_multibyte_characters))
19989 /* When we are loading loadup.el, the character property tables
19990 needed for bidi iteration are not yet available. */
19991 || !NILP (Vpurify_flag))
19992 return Qleft_to_right;
19993 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19994 return BVAR (buf, bidi_paragraph_direction);
19995 else
19996 {
19997 /* Determine the direction from buffer text. We could try to
19998 use current_matrix if it is up to date, but this seems fast
19999 enough as it is. */
20000 struct bidi_it itb;
20001 ptrdiff_t pos = BUF_PT (buf);
20002 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20003 int c;
20004 void *itb_data = bidi_shelve_cache ();
20005
20006 set_buffer_temp (buf);
20007 /* bidi_paragraph_init finds the base direction of the paragraph
20008 by searching forward from paragraph start. We need the base
20009 direction of the current or _previous_ paragraph, so we need
20010 to make sure we are within that paragraph. To that end, find
20011 the previous non-empty line. */
20012 if (pos >= ZV && pos > BEGV)
20013 {
20014 pos--;
20015 bytepos = CHAR_TO_BYTE (pos);
20016 }
20017 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20018 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20019 {
20020 while ((c = FETCH_BYTE (bytepos)) == '\n'
20021 || c == ' ' || c == '\t' || c == '\f')
20022 {
20023 if (bytepos <= BEGV_BYTE)
20024 break;
20025 bytepos--;
20026 pos--;
20027 }
20028 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20029 bytepos--;
20030 }
20031 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20032 itb.paragraph_dir = NEUTRAL_DIR;
20033 itb.string.s = NULL;
20034 itb.string.lstring = Qnil;
20035 itb.string.bufpos = 0;
20036 itb.string.unibyte = 0;
20037 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20038 bidi_unshelve_cache (itb_data, 0);
20039 set_buffer_temp (old);
20040 switch (itb.paragraph_dir)
20041 {
20042 case L2R:
20043 return Qleft_to_right;
20044 break;
20045 case R2L:
20046 return Qright_to_left;
20047 break;
20048 default:
20049 abort ();
20050 }
20051 }
20052 }
20053
20054
20055 \f
20056 /***********************************************************************
20057 Menu Bar
20058 ***********************************************************************/
20059
20060 /* Redisplay the menu bar in the frame for window W.
20061
20062 The menu bar of X frames that don't have X toolkit support is
20063 displayed in a special window W->frame->menu_bar_window.
20064
20065 The menu bar of terminal frames is treated specially as far as
20066 glyph matrices are concerned. Menu bar lines are not part of
20067 windows, so the update is done directly on the frame matrix rows
20068 for the menu bar. */
20069
20070 static void
20071 display_menu_bar (struct window *w)
20072 {
20073 struct frame *f = XFRAME (WINDOW_FRAME (w));
20074 struct it it;
20075 Lisp_Object items;
20076 int i;
20077
20078 /* Don't do all this for graphical frames. */
20079 #ifdef HAVE_NTGUI
20080 if (FRAME_W32_P (f))
20081 return;
20082 #endif
20083 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20084 if (FRAME_X_P (f))
20085 return;
20086 #endif
20087
20088 #ifdef HAVE_NS
20089 if (FRAME_NS_P (f))
20090 return;
20091 #endif /* HAVE_NS */
20092
20093 #ifdef USE_X_TOOLKIT
20094 eassert (!FRAME_WINDOW_P (f));
20095 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20096 it.first_visible_x = 0;
20097 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20098 #else /* not USE_X_TOOLKIT */
20099 if (FRAME_WINDOW_P (f))
20100 {
20101 /* Menu bar lines are displayed in the desired matrix of the
20102 dummy window menu_bar_window. */
20103 struct window *menu_w;
20104 eassert (WINDOWP (FVAR (f, menu_bar_window)));
20105 menu_w = XWINDOW (FVAR (f, menu_bar_window));
20106 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20107 MENU_FACE_ID);
20108 it.first_visible_x = 0;
20109 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20110 }
20111 else
20112 {
20113 /* This is a TTY frame, i.e. character hpos/vpos are used as
20114 pixel x/y. */
20115 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20116 MENU_FACE_ID);
20117 it.first_visible_x = 0;
20118 it.last_visible_x = FRAME_COLS (f);
20119 }
20120 #endif /* not USE_X_TOOLKIT */
20121
20122 /* FIXME: This should be controlled by a user option. See the
20123 comments in redisplay_tool_bar and display_mode_line about
20124 this. */
20125 it.paragraph_embedding = L2R;
20126
20127 if (! mode_line_inverse_video)
20128 /* Force the menu-bar to be displayed in the default face. */
20129 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20130
20131 /* Clear all rows of the menu bar. */
20132 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20133 {
20134 struct glyph_row *row = it.glyph_row + i;
20135 clear_glyph_row (row);
20136 row->enabled_p = 1;
20137 row->full_width_p = 1;
20138 }
20139
20140 /* Display all items of the menu bar. */
20141 items = FRAME_MENU_BAR_ITEMS (it.f);
20142 for (i = 0; i < ASIZE (items); i += 4)
20143 {
20144 Lisp_Object string;
20145
20146 /* Stop at nil string. */
20147 string = AREF (items, i + 1);
20148 if (NILP (string))
20149 break;
20150
20151 /* Remember where item was displayed. */
20152 ASET (items, i + 3, make_number (it.hpos));
20153
20154 /* Display the item, pad with one space. */
20155 if (it.current_x < it.last_visible_x)
20156 display_string (NULL, string, Qnil, 0, 0, &it,
20157 SCHARS (string) + 1, 0, 0, -1);
20158 }
20159
20160 /* Fill out the line with spaces. */
20161 if (it.current_x < it.last_visible_x)
20162 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20163
20164 /* Compute the total height of the lines. */
20165 compute_line_metrics (&it);
20166 }
20167
20168
20169 \f
20170 /***********************************************************************
20171 Mode Line
20172 ***********************************************************************/
20173
20174 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20175 FORCE is non-zero, redisplay mode lines unconditionally.
20176 Otherwise, redisplay only mode lines that are garbaged. Value is
20177 the number of windows whose mode lines were redisplayed. */
20178
20179 static int
20180 redisplay_mode_lines (Lisp_Object window, int force)
20181 {
20182 int nwindows = 0;
20183
20184 while (!NILP (window))
20185 {
20186 struct window *w = XWINDOW (window);
20187
20188 if (WINDOWP (w->hchild))
20189 nwindows += redisplay_mode_lines (w->hchild, force);
20190 else if (WINDOWP (w->vchild))
20191 nwindows += redisplay_mode_lines (w->vchild, force);
20192 else if (force
20193 || FRAME_GARBAGED_P (XFRAME (w->frame))
20194 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20195 {
20196 struct text_pos lpoint;
20197 struct buffer *old = current_buffer;
20198
20199 /* Set the window's buffer for the mode line display. */
20200 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20201 set_buffer_internal_1 (XBUFFER (w->buffer));
20202
20203 /* Point refers normally to the selected window. For any
20204 other window, set up appropriate value. */
20205 if (!EQ (window, selected_window))
20206 {
20207 struct text_pos pt;
20208
20209 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20210 if (CHARPOS (pt) < BEGV)
20211 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20212 else if (CHARPOS (pt) > (ZV - 1))
20213 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20214 else
20215 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20216 }
20217
20218 /* Display mode lines. */
20219 clear_glyph_matrix (w->desired_matrix);
20220 if (display_mode_lines (w))
20221 {
20222 ++nwindows;
20223 w->must_be_updated_p = 1;
20224 }
20225
20226 /* Restore old settings. */
20227 set_buffer_internal_1 (old);
20228 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20229 }
20230
20231 window = w->next;
20232 }
20233
20234 return nwindows;
20235 }
20236
20237
20238 /* Display the mode and/or header line of window W. Value is the
20239 sum number of mode lines and header lines displayed. */
20240
20241 static int
20242 display_mode_lines (struct window *w)
20243 {
20244 Lisp_Object old_selected_window, old_selected_frame;
20245 int n = 0;
20246
20247 old_selected_frame = selected_frame;
20248 selected_frame = w->frame;
20249 old_selected_window = selected_window;
20250 XSETWINDOW (selected_window, w);
20251
20252 /* These will be set while the mode line specs are processed. */
20253 line_number_displayed = 0;
20254 w->column_number_displayed = Qnil;
20255
20256 if (WINDOW_WANTS_MODELINE_P (w))
20257 {
20258 struct window *sel_w = XWINDOW (old_selected_window);
20259
20260 /* Select mode line face based on the real selected window. */
20261 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20262 BVAR (current_buffer, mode_line_format));
20263 ++n;
20264 }
20265
20266 if (WINDOW_WANTS_HEADER_LINE_P (w))
20267 {
20268 display_mode_line (w, HEADER_LINE_FACE_ID,
20269 BVAR (current_buffer, header_line_format));
20270 ++n;
20271 }
20272
20273 selected_frame = old_selected_frame;
20274 selected_window = old_selected_window;
20275 return n;
20276 }
20277
20278
20279 /* Display mode or header line of window W. FACE_ID specifies which
20280 line to display; it is either MODE_LINE_FACE_ID or
20281 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20282 display. Value is the pixel height of the mode/header line
20283 displayed. */
20284
20285 static int
20286 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20287 {
20288 struct it it;
20289 struct face *face;
20290 ptrdiff_t count = SPECPDL_INDEX ();
20291
20292 init_iterator (&it, w, -1, -1, NULL, face_id);
20293 /* Don't extend on a previously drawn mode-line.
20294 This may happen if called from pos_visible_p. */
20295 it.glyph_row->enabled_p = 0;
20296 prepare_desired_row (it.glyph_row);
20297
20298 it.glyph_row->mode_line_p = 1;
20299
20300 if (! mode_line_inverse_video)
20301 /* Force the mode-line to be displayed in the default face. */
20302 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20303
20304 /* FIXME: This should be controlled by a user option. But
20305 supporting such an option is not trivial, since the mode line is
20306 made up of many separate strings. */
20307 it.paragraph_embedding = L2R;
20308
20309 record_unwind_protect (unwind_format_mode_line,
20310 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20311
20312 mode_line_target = MODE_LINE_DISPLAY;
20313
20314 /* Temporarily make frame's keyboard the current kboard so that
20315 kboard-local variables in the mode_line_format will get the right
20316 values. */
20317 push_kboard (FRAME_KBOARD (it.f));
20318 record_unwind_save_match_data ();
20319 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20320 pop_kboard ();
20321
20322 unbind_to (count, Qnil);
20323
20324 /* Fill up with spaces. */
20325 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20326
20327 compute_line_metrics (&it);
20328 it.glyph_row->full_width_p = 1;
20329 it.glyph_row->continued_p = 0;
20330 it.glyph_row->truncated_on_left_p = 0;
20331 it.glyph_row->truncated_on_right_p = 0;
20332
20333 /* Make a 3D mode-line have a shadow at its right end. */
20334 face = FACE_FROM_ID (it.f, face_id);
20335 extend_face_to_end_of_line (&it);
20336 if (face->box != FACE_NO_BOX)
20337 {
20338 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20339 + it.glyph_row->used[TEXT_AREA] - 1);
20340 last->right_box_line_p = 1;
20341 }
20342
20343 return it.glyph_row->height;
20344 }
20345
20346 /* Move element ELT in LIST to the front of LIST.
20347 Return the updated list. */
20348
20349 static Lisp_Object
20350 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20351 {
20352 register Lisp_Object tail, prev;
20353 register Lisp_Object tem;
20354
20355 tail = list;
20356 prev = Qnil;
20357 while (CONSP (tail))
20358 {
20359 tem = XCAR (tail);
20360
20361 if (EQ (elt, tem))
20362 {
20363 /* Splice out the link TAIL. */
20364 if (NILP (prev))
20365 list = XCDR (tail);
20366 else
20367 Fsetcdr (prev, XCDR (tail));
20368
20369 /* Now make it the first. */
20370 Fsetcdr (tail, list);
20371 return tail;
20372 }
20373 else
20374 prev = tail;
20375 tail = XCDR (tail);
20376 QUIT;
20377 }
20378
20379 /* Not found--return unchanged LIST. */
20380 return list;
20381 }
20382
20383 /* Contribute ELT to the mode line for window IT->w. How it
20384 translates into text depends on its data type.
20385
20386 IT describes the display environment in which we display, as usual.
20387
20388 DEPTH is the depth in recursion. It is used to prevent
20389 infinite recursion here.
20390
20391 FIELD_WIDTH is the number of characters the display of ELT should
20392 occupy in the mode line, and PRECISION is the maximum number of
20393 characters to display from ELT's representation. See
20394 display_string for details.
20395
20396 Returns the hpos of the end of the text generated by ELT.
20397
20398 PROPS is a property list to add to any string we encounter.
20399
20400 If RISKY is nonzero, remove (disregard) any properties in any string
20401 we encounter, and ignore :eval and :propertize.
20402
20403 The global variable `mode_line_target' determines whether the
20404 output is passed to `store_mode_line_noprop',
20405 `store_mode_line_string', or `display_string'. */
20406
20407 static int
20408 display_mode_element (struct it *it, int depth, int field_width, int precision,
20409 Lisp_Object elt, Lisp_Object props, int risky)
20410 {
20411 int n = 0, field, prec;
20412 int literal = 0;
20413
20414 tail_recurse:
20415 if (depth > 100)
20416 elt = build_string ("*too-deep*");
20417
20418 depth++;
20419
20420 switch (XTYPE (elt))
20421 {
20422 case Lisp_String:
20423 {
20424 /* A string: output it and check for %-constructs within it. */
20425 unsigned char c;
20426 ptrdiff_t offset = 0;
20427
20428 if (SCHARS (elt) > 0
20429 && (!NILP (props) || risky))
20430 {
20431 Lisp_Object oprops, aelt;
20432 oprops = Ftext_properties_at (make_number (0), elt);
20433
20434 /* If the starting string's properties are not what
20435 we want, translate the string. Also, if the string
20436 is risky, do that anyway. */
20437
20438 if (NILP (Fequal (props, oprops)) || risky)
20439 {
20440 /* If the starting string has properties,
20441 merge the specified ones onto the existing ones. */
20442 if (! NILP (oprops) && !risky)
20443 {
20444 Lisp_Object tem;
20445
20446 oprops = Fcopy_sequence (oprops);
20447 tem = props;
20448 while (CONSP (tem))
20449 {
20450 oprops = Fplist_put (oprops, XCAR (tem),
20451 XCAR (XCDR (tem)));
20452 tem = XCDR (XCDR (tem));
20453 }
20454 props = oprops;
20455 }
20456
20457 aelt = Fassoc (elt, mode_line_proptrans_alist);
20458 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20459 {
20460 /* AELT is what we want. Move it to the front
20461 without consing. */
20462 elt = XCAR (aelt);
20463 mode_line_proptrans_alist
20464 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20465 }
20466 else
20467 {
20468 Lisp_Object tem;
20469
20470 /* If AELT has the wrong props, it is useless.
20471 so get rid of it. */
20472 if (! NILP (aelt))
20473 mode_line_proptrans_alist
20474 = Fdelq (aelt, mode_line_proptrans_alist);
20475
20476 elt = Fcopy_sequence (elt);
20477 Fset_text_properties (make_number (0), Flength (elt),
20478 props, elt);
20479 /* Add this item to mode_line_proptrans_alist. */
20480 mode_line_proptrans_alist
20481 = Fcons (Fcons (elt, props),
20482 mode_line_proptrans_alist);
20483 /* Truncate mode_line_proptrans_alist
20484 to at most 50 elements. */
20485 tem = Fnthcdr (make_number (50),
20486 mode_line_proptrans_alist);
20487 if (! NILP (tem))
20488 XSETCDR (tem, Qnil);
20489 }
20490 }
20491 }
20492
20493 offset = 0;
20494
20495 if (literal)
20496 {
20497 prec = precision - n;
20498 switch (mode_line_target)
20499 {
20500 case MODE_LINE_NOPROP:
20501 case MODE_LINE_TITLE:
20502 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20503 break;
20504 case MODE_LINE_STRING:
20505 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20506 break;
20507 case MODE_LINE_DISPLAY:
20508 n += display_string (NULL, elt, Qnil, 0, 0, it,
20509 0, prec, 0, STRING_MULTIBYTE (elt));
20510 break;
20511 }
20512
20513 break;
20514 }
20515
20516 /* Handle the non-literal case. */
20517
20518 while ((precision <= 0 || n < precision)
20519 && SREF (elt, offset) != 0
20520 && (mode_line_target != MODE_LINE_DISPLAY
20521 || it->current_x < it->last_visible_x))
20522 {
20523 ptrdiff_t last_offset = offset;
20524
20525 /* Advance to end of string or next format specifier. */
20526 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20527 ;
20528
20529 if (offset - 1 != last_offset)
20530 {
20531 ptrdiff_t nchars, nbytes;
20532
20533 /* Output to end of string or up to '%'. Field width
20534 is length of string. Don't output more than
20535 PRECISION allows us. */
20536 offset--;
20537
20538 prec = c_string_width (SDATA (elt) + last_offset,
20539 offset - last_offset, precision - n,
20540 &nchars, &nbytes);
20541
20542 switch (mode_line_target)
20543 {
20544 case MODE_LINE_NOPROP:
20545 case MODE_LINE_TITLE:
20546 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20547 break;
20548 case MODE_LINE_STRING:
20549 {
20550 ptrdiff_t bytepos = last_offset;
20551 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20552 ptrdiff_t endpos = (precision <= 0
20553 ? string_byte_to_char (elt, offset)
20554 : charpos + nchars);
20555
20556 n += store_mode_line_string (NULL,
20557 Fsubstring (elt, make_number (charpos),
20558 make_number (endpos)),
20559 0, 0, 0, Qnil);
20560 }
20561 break;
20562 case MODE_LINE_DISPLAY:
20563 {
20564 ptrdiff_t bytepos = last_offset;
20565 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20566
20567 if (precision <= 0)
20568 nchars = string_byte_to_char (elt, offset) - charpos;
20569 n += display_string (NULL, elt, Qnil, 0, charpos,
20570 it, 0, nchars, 0,
20571 STRING_MULTIBYTE (elt));
20572 }
20573 break;
20574 }
20575 }
20576 else /* c == '%' */
20577 {
20578 ptrdiff_t percent_position = offset;
20579
20580 /* Get the specified minimum width. Zero means
20581 don't pad. */
20582 field = 0;
20583 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20584 field = field * 10 + c - '0';
20585
20586 /* Don't pad beyond the total padding allowed. */
20587 if (field_width - n > 0 && field > field_width - n)
20588 field = field_width - n;
20589
20590 /* Note that either PRECISION <= 0 or N < PRECISION. */
20591 prec = precision - n;
20592
20593 if (c == 'M')
20594 n += display_mode_element (it, depth, field, prec,
20595 Vglobal_mode_string, props,
20596 risky);
20597 else if (c != 0)
20598 {
20599 int multibyte;
20600 ptrdiff_t bytepos, charpos;
20601 const char *spec;
20602 Lisp_Object string;
20603
20604 bytepos = percent_position;
20605 charpos = (STRING_MULTIBYTE (elt)
20606 ? string_byte_to_char (elt, bytepos)
20607 : bytepos);
20608 spec = decode_mode_spec (it->w, c, field, &string);
20609 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20610
20611 switch (mode_line_target)
20612 {
20613 case MODE_LINE_NOPROP:
20614 case MODE_LINE_TITLE:
20615 n += store_mode_line_noprop (spec, field, prec);
20616 break;
20617 case MODE_LINE_STRING:
20618 {
20619 Lisp_Object tem = build_string (spec);
20620 props = Ftext_properties_at (make_number (charpos), elt);
20621 /* Should only keep face property in props */
20622 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20623 }
20624 break;
20625 case MODE_LINE_DISPLAY:
20626 {
20627 int nglyphs_before, nwritten;
20628
20629 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20630 nwritten = display_string (spec, string, elt,
20631 charpos, 0, it,
20632 field, prec, 0,
20633 multibyte);
20634
20635 /* Assign to the glyphs written above the
20636 string where the `%x' came from, position
20637 of the `%'. */
20638 if (nwritten > 0)
20639 {
20640 struct glyph *glyph
20641 = (it->glyph_row->glyphs[TEXT_AREA]
20642 + nglyphs_before);
20643 int i;
20644
20645 for (i = 0; i < nwritten; ++i)
20646 {
20647 glyph[i].object = elt;
20648 glyph[i].charpos = charpos;
20649 }
20650
20651 n += nwritten;
20652 }
20653 }
20654 break;
20655 }
20656 }
20657 else /* c == 0 */
20658 break;
20659 }
20660 }
20661 }
20662 break;
20663
20664 case Lisp_Symbol:
20665 /* A symbol: process the value of the symbol recursively
20666 as if it appeared here directly. Avoid error if symbol void.
20667 Special case: if value of symbol is a string, output the string
20668 literally. */
20669 {
20670 register Lisp_Object tem;
20671
20672 /* If the variable is not marked as risky to set
20673 then its contents are risky to use. */
20674 if (NILP (Fget (elt, Qrisky_local_variable)))
20675 risky = 1;
20676
20677 tem = Fboundp (elt);
20678 if (!NILP (tem))
20679 {
20680 tem = Fsymbol_value (elt);
20681 /* If value is a string, output that string literally:
20682 don't check for % within it. */
20683 if (STRINGP (tem))
20684 literal = 1;
20685
20686 if (!EQ (tem, elt))
20687 {
20688 /* Give up right away for nil or t. */
20689 elt = tem;
20690 goto tail_recurse;
20691 }
20692 }
20693 }
20694 break;
20695
20696 case Lisp_Cons:
20697 {
20698 register Lisp_Object car, tem;
20699
20700 /* A cons cell: five distinct cases.
20701 If first element is :eval or :propertize, do something special.
20702 If first element is a string or a cons, process all the elements
20703 and effectively concatenate them.
20704 If first element is a negative number, truncate displaying cdr to
20705 at most that many characters. If positive, pad (with spaces)
20706 to at least that many characters.
20707 If first element is a symbol, process the cadr or caddr recursively
20708 according to whether the symbol's value is non-nil or nil. */
20709 car = XCAR (elt);
20710 if (EQ (car, QCeval))
20711 {
20712 /* An element of the form (:eval FORM) means evaluate FORM
20713 and use the result as mode line elements. */
20714
20715 if (risky)
20716 break;
20717
20718 if (CONSP (XCDR (elt)))
20719 {
20720 Lisp_Object spec;
20721 spec = safe_eval (XCAR (XCDR (elt)));
20722 n += display_mode_element (it, depth, field_width - n,
20723 precision - n, spec, props,
20724 risky);
20725 }
20726 }
20727 else if (EQ (car, QCpropertize))
20728 {
20729 /* An element of the form (:propertize ELT PROPS...)
20730 means display ELT but applying properties PROPS. */
20731
20732 if (risky)
20733 break;
20734
20735 if (CONSP (XCDR (elt)))
20736 n += display_mode_element (it, depth, field_width - n,
20737 precision - n, XCAR (XCDR (elt)),
20738 XCDR (XCDR (elt)), risky);
20739 }
20740 else if (SYMBOLP (car))
20741 {
20742 tem = Fboundp (car);
20743 elt = XCDR (elt);
20744 if (!CONSP (elt))
20745 goto invalid;
20746 /* elt is now the cdr, and we know it is a cons cell.
20747 Use its car if CAR has a non-nil value. */
20748 if (!NILP (tem))
20749 {
20750 tem = Fsymbol_value (car);
20751 if (!NILP (tem))
20752 {
20753 elt = XCAR (elt);
20754 goto tail_recurse;
20755 }
20756 }
20757 /* Symbol's value is nil (or symbol is unbound)
20758 Get the cddr of the original list
20759 and if possible find the caddr and use that. */
20760 elt = XCDR (elt);
20761 if (NILP (elt))
20762 break;
20763 else if (!CONSP (elt))
20764 goto invalid;
20765 elt = XCAR (elt);
20766 goto tail_recurse;
20767 }
20768 else if (INTEGERP (car))
20769 {
20770 register int lim = XINT (car);
20771 elt = XCDR (elt);
20772 if (lim < 0)
20773 {
20774 /* Negative int means reduce maximum width. */
20775 if (precision <= 0)
20776 precision = -lim;
20777 else
20778 precision = min (precision, -lim);
20779 }
20780 else if (lim > 0)
20781 {
20782 /* Padding specified. Don't let it be more than
20783 current maximum. */
20784 if (precision > 0)
20785 lim = min (precision, lim);
20786
20787 /* If that's more padding than already wanted, queue it.
20788 But don't reduce padding already specified even if
20789 that is beyond the current truncation point. */
20790 field_width = max (lim, field_width);
20791 }
20792 goto tail_recurse;
20793 }
20794 else if (STRINGP (car) || CONSP (car))
20795 {
20796 Lisp_Object halftail = elt;
20797 int len = 0;
20798
20799 while (CONSP (elt)
20800 && (precision <= 0 || n < precision))
20801 {
20802 n += display_mode_element (it, depth,
20803 /* Do padding only after the last
20804 element in the list. */
20805 (! CONSP (XCDR (elt))
20806 ? field_width - n
20807 : 0),
20808 precision - n, XCAR (elt),
20809 props, risky);
20810 elt = XCDR (elt);
20811 len++;
20812 if ((len & 1) == 0)
20813 halftail = XCDR (halftail);
20814 /* Check for cycle. */
20815 if (EQ (halftail, elt))
20816 break;
20817 }
20818 }
20819 }
20820 break;
20821
20822 default:
20823 invalid:
20824 elt = build_string ("*invalid*");
20825 goto tail_recurse;
20826 }
20827
20828 /* Pad to FIELD_WIDTH. */
20829 if (field_width > 0 && n < field_width)
20830 {
20831 switch (mode_line_target)
20832 {
20833 case MODE_LINE_NOPROP:
20834 case MODE_LINE_TITLE:
20835 n += store_mode_line_noprop ("", field_width - n, 0);
20836 break;
20837 case MODE_LINE_STRING:
20838 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20839 break;
20840 case MODE_LINE_DISPLAY:
20841 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20842 0, 0, 0);
20843 break;
20844 }
20845 }
20846
20847 return n;
20848 }
20849
20850 /* Store a mode-line string element in mode_line_string_list.
20851
20852 If STRING is non-null, display that C string. Otherwise, the Lisp
20853 string LISP_STRING is displayed.
20854
20855 FIELD_WIDTH is the minimum number of output glyphs to produce.
20856 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20857 with spaces. FIELD_WIDTH <= 0 means don't pad.
20858
20859 PRECISION is the maximum number of characters to output from
20860 STRING. PRECISION <= 0 means don't truncate the string.
20861
20862 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20863 properties to the string.
20864
20865 PROPS are the properties to add to the string.
20866 The mode_line_string_face face property is always added to the string.
20867 */
20868
20869 static int
20870 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20871 int field_width, int precision, Lisp_Object props)
20872 {
20873 ptrdiff_t len;
20874 int n = 0;
20875
20876 if (string != NULL)
20877 {
20878 len = strlen (string);
20879 if (precision > 0 && len > precision)
20880 len = precision;
20881 lisp_string = make_string (string, len);
20882 if (NILP (props))
20883 props = mode_line_string_face_prop;
20884 else if (!NILP (mode_line_string_face))
20885 {
20886 Lisp_Object face = Fplist_get (props, Qface);
20887 props = Fcopy_sequence (props);
20888 if (NILP (face))
20889 face = mode_line_string_face;
20890 else
20891 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20892 props = Fplist_put (props, Qface, face);
20893 }
20894 Fadd_text_properties (make_number (0), make_number (len),
20895 props, lisp_string);
20896 }
20897 else
20898 {
20899 len = XFASTINT (Flength (lisp_string));
20900 if (precision > 0 && len > precision)
20901 {
20902 len = precision;
20903 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20904 precision = -1;
20905 }
20906 if (!NILP (mode_line_string_face))
20907 {
20908 Lisp_Object face;
20909 if (NILP (props))
20910 props = Ftext_properties_at (make_number (0), lisp_string);
20911 face = Fplist_get (props, Qface);
20912 if (NILP (face))
20913 face = mode_line_string_face;
20914 else
20915 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20916 props = Fcons (Qface, Fcons (face, Qnil));
20917 if (copy_string)
20918 lisp_string = Fcopy_sequence (lisp_string);
20919 }
20920 if (!NILP (props))
20921 Fadd_text_properties (make_number (0), make_number (len),
20922 props, lisp_string);
20923 }
20924
20925 if (len > 0)
20926 {
20927 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20928 n += len;
20929 }
20930
20931 if (field_width > len)
20932 {
20933 field_width -= len;
20934 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20935 if (!NILP (props))
20936 Fadd_text_properties (make_number (0), make_number (field_width),
20937 props, lisp_string);
20938 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20939 n += field_width;
20940 }
20941
20942 return n;
20943 }
20944
20945
20946 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20947 1, 4, 0,
20948 doc: /* Format a string out of a mode line format specification.
20949 First arg FORMAT specifies the mode line format (see `mode-line-format'
20950 for details) to use.
20951
20952 By default, the format is evaluated for the currently selected window.
20953
20954 Optional second arg FACE specifies the face property to put on all
20955 characters for which no face is specified. The value nil means the
20956 default face. The value t means whatever face the window's mode line
20957 currently uses (either `mode-line' or `mode-line-inactive',
20958 depending on whether the window is the selected window or not).
20959 An integer value means the value string has no text
20960 properties.
20961
20962 Optional third and fourth args WINDOW and BUFFER specify the window
20963 and buffer to use as the context for the formatting (defaults
20964 are the selected window and the WINDOW's buffer). */)
20965 (Lisp_Object format, Lisp_Object face,
20966 Lisp_Object window, Lisp_Object buffer)
20967 {
20968 struct it it;
20969 int len;
20970 struct window *w;
20971 struct buffer *old_buffer = NULL;
20972 int face_id;
20973 int no_props = INTEGERP (face);
20974 ptrdiff_t count = SPECPDL_INDEX ();
20975 Lisp_Object str;
20976 int string_start = 0;
20977
20978 if (NILP (window))
20979 window = selected_window;
20980 CHECK_WINDOW (window);
20981 w = XWINDOW (window);
20982
20983 if (NILP (buffer))
20984 buffer = w->buffer;
20985 CHECK_BUFFER (buffer);
20986
20987 /* Make formatting the modeline a non-op when noninteractive, otherwise
20988 there will be problems later caused by a partially initialized frame. */
20989 if (NILP (format) || noninteractive)
20990 return empty_unibyte_string;
20991
20992 if (no_props)
20993 face = Qnil;
20994
20995 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20996 : EQ (face, Qt) ? (EQ (window, selected_window)
20997 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20998 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20999 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21000 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21001 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21002 : DEFAULT_FACE_ID;
21003
21004 if (XBUFFER (buffer) != current_buffer)
21005 old_buffer = current_buffer;
21006
21007 /* Save things including mode_line_proptrans_alist,
21008 and set that to nil so that we don't alter the outer value. */
21009 record_unwind_protect (unwind_format_mode_line,
21010 format_mode_line_unwind_data
21011 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21012 old_buffer, selected_window, 1));
21013 mode_line_proptrans_alist = Qnil;
21014
21015 Fselect_window (window, Qt);
21016 if (old_buffer)
21017 set_buffer_internal_1 (XBUFFER (buffer));
21018
21019 init_iterator (&it, w, -1, -1, NULL, face_id);
21020
21021 if (no_props)
21022 {
21023 mode_line_target = MODE_LINE_NOPROP;
21024 mode_line_string_face_prop = Qnil;
21025 mode_line_string_list = Qnil;
21026 string_start = MODE_LINE_NOPROP_LEN (0);
21027 }
21028 else
21029 {
21030 mode_line_target = MODE_LINE_STRING;
21031 mode_line_string_list = Qnil;
21032 mode_line_string_face = face;
21033 mode_line_string_face_prop
21034 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21035 }
21036
21037 push_kboard (FRAME_KBOARD (it.f));
21038 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21039 pop_kboard ();
21040
21041 if (no_props)
21042 {
21043 len = MODE_LINE_NOPROP_LEN (string_start);
21044 str = make_string (mode_line_noprop_buf + string_start, len);
21045 }
21046 else
21047 {
21048 mode_line_string_list = Fnreverse (mode_line_string_list);
21049 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21050 empty_unibyte_string);
21051 }
21052
21053 unbind_to (count, Qnil);
21054 return str;
21055 }
21056
21057 /* Write a null-terminated, right justified decimal representation of
21058 the positive integer D to BUF using a minimal field width WIDTH. */
21059
21060 static void
21061 pint2str (register char *buf, register int width, register ptrdiff_t d)
21062 {
21063 register char *p = buf;
21064
21065 if (d <= 0)
21066 *p++ = '0';
21067 else
21068 {
21069 while (d > 0)
21070 {
21071 *p++ = d % 10 + '0';
21072 d /= 10;
21073 }
21074 }
21075
21076 for (width -= (int) (p - buf); width > 0; --width)
21077 *p++ = ' ';
21078 *p-- = '\0';
21079 while (p > buf)
21080 {
21081 d = *buf;
21082 *buf++ = *p;
21083 *p-- = d;
21084 }
21085 }
21086
21087 /* Write a null-terminated, right justified decimal and "human
21088 readable" representation of the nonnegative integer D to BUF using
21089 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21090
21091 static const char power_letter[] =
21092 {
21093 0, /* no letter */
21094 'k', /* kilo */
21095 'M', /* mega */
21096 'G', /* giga */
21097 'T', /* tera */
21098 'P', /* peta */
21099 'E', /* exa */
21100 'Z', /* zetta */
21101 'Y' /* yotta */
21102 };
21103
21104 static void
21105 pint2hrstr (char *buf, int width, ptrdiff_t d)
21106 {
21107 /* We aim to represent the nonnegative integer D as
21108 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21109 ptrdiff_t quotient = d;
21110 int remainder = 0;
21111 /* -1 means: do not use TENTHS. */
21112 int tenths = -1;
21113 int exponent = 0;
21114
21115 /* Length of QUOTIENT.TENTHS as a string. */
21116 int length;
21117
21118 char * psuffix;
21119 char * p;
21120
21121 if (1000 <= quotient)
21122 {
21123 /* Scale to the appropriate EXPONENT. */
21124 do
21125 {
21126 remainder = quotient % 1000;
21127 quotient /= 1000;
21128 exponent++;
21129 }
21130 while (1000 <= quotient);
21131
21132 /* Round to nearest and decide whether to use TENTHS or not. */
21133 if (quotient <= 9)
21134 {
21135 tenths = remainder / 100;
21136 if (50 <= remainder % 100)
21137 {
21138 if (tenths < 9)
21139 tenths++;
21140 else
21141 {
21142 quotient++;
21143 if (quotient == 10)
21144 tenths = -1;
21145 else
21146 tenths = 0;
21147 }
21148 }
21149 }
21150 else
21151 if (500 <= remainder)
21152 {
21153 if (quotient < 999)
21154 quotient++;
21155 else
21156 {
21157 quotient = 1;
21158 exponent++;
21159 tenths = 0;
21160 }
21161 }
21162 }
21163
21164 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21165 if (tenths == -1 && quotient <= 99)
21166 if (quotient <= 9)
21167 length = 1;
21168 else
21169 length = 2;
21170 else
21171 length = 3;
21172 p = psuffix = buf + max (width, length);
21173
21174 /* Print EXPONENT. */
21175 *psuffix++ = power_letter[exponent];
21176 *psuffix = '\0';
21177
21178 /* Print TENTHS. */
21179 if (tenths >= 0)
21180 {
21181 *--p = '0' + tenths;
21182 *--p = '.';
21183 }
21184
21185 /* Print QUOTIENT. */
21186 do
21187 {
21188 int digit = quotient % 10;
21189 *--p = '0' + digit;
21190 }
21191 while ((quotient /= 10) != 0);
21192
21193 /* Print leading spaces. */
21194 while (buf < p)
21195 *--p = ' ';
21196 }
21197
21198 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21199 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21200 type of CODING_SYSTEM. Return updated pointer into BUF. */
21201
21202 static unsigned char invalid_eol_type[] = "(*invalid*)";
21203
21204 static char *
21205 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21206 {
21207 Lisp_Object val;
21208 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21209 const unsigned char *eol_str;
21210 int eol_str_len;
21211 /* The EOL conversion we are using. */
21212 Lisp_Object eoltype;
21213
21214 val = CODING_SYSTEM_SPEC (coding_system);
21215 eoltype = Qnil;
21216
21217 if (!VECTORP (val)) /* Not yet decided. */
21218 {
21219 *buf++ = multibyte ? '-' : ' ';
21220 if (eol_flag)
21221 eoltype = eol_mnemonic_undecided;
21222 /* Don't mention EOL conversion if it isn't decided. */
21223 }
21224 else
21225 {
21226 Lisp_Object attrs;
21227 Lisp_Object eolvalue;
21228
21229 attrs = AREF (val, 0);
21230 eolvalue = AREF (val, 2);
21231
21232 *buf++ = multibyte
21233 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21234 : ' ';
21235
21236 if (eol_flag)
21237 {
21238 /* The EOL conversion that is normal on this system. */
21239
21240 if (NILP (eolvalue)) /* Not yet decided. */
21241 eoltype = eol_mnemonic_undecided;
21242 else if (VECTORP (eolvalue)) /* Not yet decided. */
21243 eoltype = eol_mnemonic_undecided;
21244 else /* eolvalue is Qunix, Qdos, or Qmac. */
21245 eoltype = (EQ (eolvalue, Qunix)
21246 ? eol_mnemonic_unix
21247 : (EQ (eolvalue, Qdos) == 1
21248 ? eol_mnemonic_dos : eol_mnemonic_mac));
21249 }
21250 }
21251
21252 if (eol_flag)
21253 {
21254 /* Mention the EOL conversion if it is not the usual one. */
21255 if (STRINGP (eoltype))
21256 {
21257 eol_str = SDATA (eoltype);
21258 eol_str_len = SBYTES (eoltype);
21259 }
21260 else if (CHARACTERP (eoltype))
21261 {
21262 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21263 int c = XFASTINT (eoltype);
21264 eol_str_len = CHAR_STRING (c, tmp);
21265 eol_str = tmp;
21266 }
21267 else
21268 {
21269 eol_str = invalid_eol_type;
21270 eol_str_len = sizeof (invalid_eol_type) - 1;
21271 }
21272 memcpy (buf, eol_str, eol_str_len);
21273 buf += eol_str_len;
21274 }
21275
21276 return buf;
21277 }
21278
21279 /* Return a string for the output of a mode line %-spec for window W,
21280 generated by character C. FIELD_WIDTH > 0 means pad the string
21281 returned with spaces to that value. Return a Lisp string in
21282 *STRING if the resulting string is taken from that Lisp string.
21283
21284 Note we operate on the current buffer for most purposes,
21285 the exception being w->base_line_pos. */
21286
21287 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21288
21289 static const char *
21290 decode_mode_spec (struct window *w, register int c, int field_width,
21291 Lisp_Object *string)
21292 {
21293 Lisp_Object obj;
21294 struct frame *f = XFRAME (WINDOW_FRAME (w));
21295 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21296 struct buffer *b = current_buffer;
21297
21298 obj = Qnil;
21299 *string = Qnil;
21300
21301 switch (c)
21302 {
21303 case '*':
21304 if (!NILP (BVAR (b, read_only)))
21305 return "%";
21306 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21307 return "*";
21308 return "-";
21309
21310 case '+':
21311 /* This differs from %* only for a modified read-only buffer. */
21312 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21313 return "*";
21314 if (!NILP (BVAR (b, read_only)))
21315 return "%";
21316 return "-";
21317
21318 case '&':
21319 /* This differs from %* in ignoring read-only-ness. */
21320 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21321 return "*";
21322 return "-";
21323
21324 case '%':
21325 return "%";
21326
21327 case '[':
21328 {
21329 int i;
21330 char *p;
21331
21332 if (command_loop_level > 5)
21333 return "[[[... ";
21334 p = decode_mode_spec_buf;
21335 for (i = 0; i < command_loop_level; i++)
21336 *p++ = '[';
21337 *p = 0;
21338 return decode_mode_spec_buf;
21339 }
21340
21341 case ']':
21342 {
21343 int i;
21344 char *p;
21345
21346 if (command_loop_level > 5)
21347 return " ...]]]";
21348 p = decode_mode_spec_buf;
21349 for (i = 0; i < command_loop_level; i++)
21350 *p++ = ']';
21351 *p = 0;
21352 return decode_mode_spec_buf;
21353 }
21354
21355 case '-':
21356 {
21357 register int i;
21358
21359 /* Let lots_of_dashes be a string of infinite length. */
21360 if (mode_line_target == MODE_LINE_NOPROP ||
21361 mode_line_target == MODE_LINE_STRING)
21362 return "--";
21363 if (field_width <= 0
21364 || field_width > sizeof (lots_of_dashes))
21365 {
21366 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21367 decode_mode_spec_buf[i] = '-';
21368 decode_mode_spec_buf[i] = '\0';
21369 return decode_mode_spec_buf;
21370 }
21371 else
21372 return lots_of_dashes;
21373 }
21374
21375 case 'b':
21376 obj = BVAR (b, name);
21377 break;
21378
21379 case 'c':
21380 /* %c and %l are ignored in `frame-title-format'.
21381 (In redisplay_internal, the frame title is drawn _before_ the
21382 windows are updated, so the stuff which depends on actual
21383 window contents (such as %l) may fail to render properly, or
21384 even crash emacs.) */
21385 if (mode_line_target == MODE_LINE_TITLE)
21386 return "";
21387 else
21388 {
21389 ptrdiff_t col = current_column ();
21390 w->column_number_displayed = make_number (col);
21391 pint2str (decode_mode_spec_buf, field_width, col);
21392 return decode_mode_spec_buf;
21393 }
21394
21395 case 'e':
21396 #ifndef SYSTEM_MALLOC
21397 {
21398 if (NILP (Vmemory_full))
21399 return "";
21400 else
21401 return "!MEM FULL! ";
21402 }
21403 #else
21404 return "";
21405 #endif
21406
21407 case 'F':
21408 /* %F displays the frame name. */
21409 if (!NILP (FVAR (f, title)))
21410 return SSDATA (FVAR (f, title));
21411 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21412 return SSDATA (FVAR (f, name));
21413 return "Emacs";
21414
21415 case 'f':
21416 obj = BVAR (b, filename);
21417 break;
21418
21419 case 'i':
21420 {
21421 ptrdiff_t size = ZV - BEGV;
21422 pint2str (decode_mode_spec_buf, field_width, size);
21423 return decode_mode_spec_buf;
21424 }
21425
21426 case 'I':
21427 {
21428 ptrdiff_t size = ZV - BEGV;
21429 pint2hrstr (decode_mode_spec_buf, field_width, size);
21430 return decode_mode_spec_buf;
21431 }
21432
21433 case 'l':
21434 {
21435 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21436 ptrdiff_t topline, nlines, height;
21437 ptrdiff_t junk;
21438
21439 /* %c and %l are ignored in `frame-title-format'. */
21440 if (mode_line_target == MODE_LINE_TITLE)
21441 return "";
21442
21443 startpos = XMARKER (w->start)->charpos;
21444 startpos_byte = marker_byte_position (w->start);
21445 height = WINDOW_TOTAL_LINES (w);
21446
21447 /* If we decided that this buffer isn't suitable for line numbers,
21448 don't forget that too fast. */
21449 if (EQ (w->base_line_pos, w->buffer))
21450 goto no_value;
21451 /* But do forget it, if the window shows a different buffer now. */
21452 else if (BUFFERP (w->base_line_pos))
21453 w->base_line_pos = Qnil;
21454
21455 /* If the buffer is very big, don't waste time. */
21456 if (INTEGERP (Vline_number_display_limit)
21457 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21458 {
21459 w->base_line_pos = Qnil;
21460 w->base_line_number = Qnil;
21461 goto no_value;
21462 }
21463
21464 if (INTEGERP (w->base_line_number)
21465 && INTEGERP (w->base_line_pos)
21466 && XFASTINT (w->base_line_pos) <= startpos)
21467 {
21468 line = XFASTINT (w->base_line_number);
21469 linepos = XFASTINT (w->base_line_pos);
21470 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21471 }
21472 else
21473 {
21474 line = 1;
21475 linepos = BUF_BEGV (b);
21476 linepos_byte = BUF_BEGV_BYTE (b);
21477 }
21478
21479 /* Count lines from base line to window start position. */
21480 nlines = display_count_lines (linepos_byte,
21481 startpos_byte,
21482 startpos, &junk);
21483
21484 topline = nlines + line;
21485
21486 /* Determine a new base line, if the old one is too close
21487 or too far away, or if we did not have one.
21488 "Too close" means it's plausible a scroll-down would
21489 go back past it. */
21490 if (startpos == BUF_BEGV (b))
21491 {
21492 w->base_line_number = make_number (topline);
21493 w->base_line_pos = make_number (BUF_BEGV (b));
21494 }
21495 else if (nlines < height + 25 || nlines > height * 3 + 50
21496 || linepos == BUF_BEGV (b))
21497 {
21498 ptrdiff_t limit = BUF_BEGV (b);
21499 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21500 ptrdiff_t position;
21501 ptrdiff_t distance =
21502 (height * 2 + 30) * line_number_display_limit_width;
21503
21504 if (startpos - distance > limit)
21505 {
21506 limit = startpos - distance;
21507 limit_byte = CHAR_TO_BYTE (limit);
21508 }
21509
21510 nlines = display_count_lines (startpos_byte,
21511 limit_byte,
21512 - (height * 2 + 30),
21513 &position);
21514 /* If we couldn't find the lines we wanted within
21515 line_number_display_limit_width chars per line,
21516 give up on line numbers for this window. */
21517 if (position == limit_byte && limit == startpos - distance)
21518 {
21519 w->base_line_pos = w->buffer;
21520 w->base_line_number = Qnil;
21521 goto no_value;
21522 }
21523
21524 w->base_line_number = make_number (topline - nlines);
21525 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21526 }
21527
21528 /* Now count lines from the start pos to point. */
21529 nlines = display_count_lines (startpos_byte,
21530 PT_BYTE, PT, &junk);
21531
21532 /* Record that we did display the line number. */
21533 line_number_displayed = 1;
21534
21535 /* Make the string to show. */
21536 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21537 return decode_mode_spec_buf;
21538 no_value:
21539 {
21540 char* p = decode_mode_spec_buf;
21541 int pad = field_width - 2;
21542 while (pad-- > 0)
21543 *p++ = ' ';
21544 *p++ = '?';
21545 *p++ = '?';
21546 *p = '\0';
21547 return decode_mode_spec_buf;
21548 }
21549 }
21550 break;
21551
21552 case 'm':
21553 obj = BVAR (b, mode_name);
21554 break;
21555
21556 case 'n':
21557 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21558 return " Narrow";
21559 break;
21560
21561 case 'p':
21562 {
21563 ptrdiff_t pos = marker_position (w->start);
21564 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21565
21566 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21567 {
21568 if (pos <= BUF_BEGV (b))
21569 return "All";
21570 else
21571 return "Bottom";
21572 }
21573 else if (pos <= BUF_BEGV (b))
21574 return "Top";
21575 else
21576 {
21577 if (total > 1000000)
21578 /* Do it differently for a large value, to avoid overflow. */
21579 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21580 else
21581 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21582 /* We can't normally display a 3-digit number,
21583 so get us a 2-digit number that is close. */
21584 if (total == 100)
21585 total = 99;
21586 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21587 return decode_mode_spec_buf;
21588 }
21589 }
21590
21591 /* Display percentage of size above the bottom of the screen. */
21592 case 'P':
21593 {
21594 ptrdiff_t toppos = marker_position (w->start);
21595 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21596 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21597
21598 if (botpos >= BUF_ZV (b))
21599 {
21600 if (toppos <= BUF_BEGV (b))
21601 return "All";
21602 else
21603 return "Bottom";
21604 }
21605 else
21606 {
21607 if (total > 1000000)
21608 /* Do it differently for a large value, to avoid overflow. */
21609 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21610 else
21611 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21612 /* We can't normally display a 3-digit number,
21613 so get us a 2-digit number that is close. */
21614 if (total == 100)
21615 total = 99;
21616 if (toppos <= BUF_BEGV (b))
21617 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21618 else
21619 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21620 return decode_mode_spec_buf;
21621 }
21622 }
21623
21624 case 's':
21625 /* status of process */
21626 obj = Fget_buffer_process (Fcurrent_buffer ());
21627 if (NILP (obj))
21628 return "no process";
21629 #ifndef MSDOS
21630 obj = Fsymbol_name (Fprocess_status (obj));
21631 #endif
21632 break;
21633
21634 case '@':
21635 {
21636 ptrdiff_t count = inhibit_garbage_collection ();
21637 Lisp_Object val = call1 (intern ("file-remote-p"),
21638 BVAR (current_buffer, directory));
21639 unbind_to (count, Qnil);
21640
21641 if (NILP (val))
21642 return "-";
21643 else
21644 return "@";
21645 }
21646
21647 case 't': /* indicate TEXT or BINARY */
21648 return "T";
21649
21650 case 'z':
21651 /* coding-system (not including end-of-line format) */
21652 case 'Z':
21653 /* coding-system (including end-of-line type) */
21654 {
21655 int eol_flag = (c == 'Z');
21656 char *p = decode_mode_spec_buf;
21657
21658 if (! FRAME_WINDOW_P (f))
21659 {
21660 /* No need to mention EOL here--the terminal never needs
21661 to do EOL conversion. */
21662 p = decode_mode_spec_coding (CODING_ID_NAME
21663 (FRAME_KEYBOARD_CODING (f)->id),
21664 p, 0);
21665 p = decode_mode_spec_coding (CODING_ID_NAME
21666 (FRAME_TERMINAL_CODING (f)->id),
21667 p, 0);
21668 }
21669 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21670 p, eol_flag);
21671
21672 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21673 #ifdef subprocesses
21674 obj = Fget_buffer_process (Fcurrent_buffer ());
21675 if (PROCESSP (obj))
21676 {
21677 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21678 p, eol_flag);
21679 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21680 p, eol_flag);
21681 }
21682 #endif /* subprocesses */
21683 #endif /* 0 */
21684 *p = 0;
21685 return decode_mode_spec_buf;
21686 }
21687 }
21688
21689 if (STRINGP (obj))
21690 {
21691 *string = obj;
21692 return SSDATA (obj);
21693 }
21694 else
21695 return "";
21696 }
21697
21698
21699 /* Count up to COUNT lines starting from START_BYTE.
21700 But don't go beyond LIMIT_BYTE.
21701 Return the number of lines thus found (always nonnegative).
21702
21703 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21704
21705 static ptrdiff_t
21706 display_count_lines (ptrdiff_t start_byte,
21707 ptrdiff_t limit_byte, ptrdiff_t count,
21708 ptrdiff_t *byte_pos_ptr)
21709 {
21710 register unsigned char *cursor;
21711 unsigned char *base;
21712
21713 register ptrdiff_t ceiling;
21714 register unsigned char *ceiling_addr;
21715 ptrdiff_t orig_count = count;
21716
21717 /* If we are not in selective display mode,
21718 check only for newlines. */
21719 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21720 && !INTEGERP (BVAR (current_buffer, selective_display)));
21721
21722 if (count > 0)
21723 {
21724 while (start_byte < limit_byte)
21725 {
21726 ceiling = BUFFER_CEILING_OF (start_byte);
21727 ceiling = min (limit_byte - 1, ceiling);
21728 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21729 base = (cursor = BYTE_POS_ADDR (start_byte));
21730 while (1)
21731 {
21732 if (selective_display)
21733 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21734 ;
21735 else
21736 while (*cursor != '\n' && ++cursor != ceiling_addr)
21737 ;
21738
21739 if (cursor != ceiling_addr)
21740 {
21741 if (--count == 0)
21742 {
21743 start_byte += cursor - base + 1;
21744 *byte_pos_ptr = start_byte;
21745 return orig_count;
21746 }
21747 else
21748 if (++cursor == ceiling_addr)
21749 break;
21750 }
21751 else
21752 break;
21753 }
21754 start_byte += cursor - base;
21755 }
21756 }
21757 else
21758 {
21759 while (start_byte > limit_byte)
21760 {
21761 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21762 ceiling = max (limit_byte, ceiling);
21763 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21764 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21765 while (1)
21766 {
21767 if (selective_display)
21768 while (--cursor != ceiling_addr
21769 && *cursor != '\n' && *cursor != 015)
21770 ;
21771 else
21772 while (--cursor != ceiling_addr && *cursor != '\n')
21773 ;
21774
21775 if (cursor != ceiling_addr)
21776 {
21777 if (++count == 0)
21778 {
21779 start_byte += cursor - base + 1;
21780 *byte_pos_ptr = start_byte;
21781 /* When scanning backwards, we should
21782 not count the newline posterior to which we stop. */
21783 return - orig_count - 1;
21784 }
21785 }
21786 else
21787 break;
21788 }
21789 /* Here we add 1 to compensate for the last decrement
21790 of CURSOR, which took it past the valid range. */
21791 start_byte += cursor - base + 1;
21792 }
21793 }
21794
21795 *byte_pos_ptr = limit_byte;
21796
21797 if (count < 0)
21798 return - orig_count + count;
21799 return orig_count - count;
21800
21801 }
21802
21803
21804 \f
21805 /***********************************************************************
21806 Displaying strings
21807 ***********************************************************************/
21808
21809 /* Display a NUL-terminated string, starting with index START.
21810
21811 If STRING is non-null, display that C string. Otherwise, the Lisp
21812 string LISP_STRING is displayed. There's a case that STRING is
21813 non-null and LISP_STRING is not nil. It means STRING is a string
21814 data of LISP_STRING. In that case, we display LISP_STRING while
21815 ignoring its text properties.
21816
21817 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21818 FACE_STRING. Display STRING or LISP_STRING with the face at
21819 FACE_STRING_POS in FACE_STRING:
21820
21821 Display the string in the environment given by IT, but use the
21822 standard display table, temporarily.
21823
21824 FIELD_WIDTH is the minimum number of output glyphs to produce.
21825 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21826 with spaces. If STRING has more characters, more than FIELD_WIDTH
21827 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21828
21829 PRECISION is the maximum number of characters to output from
21830 STRING. PRECISION < 0 means don't truncate the string.
21831
21832 This is roughly equivalent to printf format specifiers:
21833
21834 FIELD_WIDTH PRECISION PRINTF
21835 ----------------------------------------
21836 -1 -1 %s
21837 -1 10 %.10s
21838 10 -1 %10s
21839 20 10 %20.10s
21840
21841 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21842 display them, and < 0 means obey the current buffer's value of
21843 enable_multibyte_characters.
21844
21845 Value is the number of columns displayed. */
21846
21847 static int
21848 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21849 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21850 int field_width, int precision, int max_x, int multibyte)
21851 {
21852 int hpos_at_start = it->hpos;
21853 int saved_face_id = it->face_id;
21854 struct glyph_row *row = it->glyph_row;
21855 ptrdiff_t it_charpos;
21856
21857 /* Initialize the iterator IT for iteration over STRING beginning
21858 with index START. */
21859 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21860 precision, field_width, multibyte);
21861 if (string && STRINGP (lisp_string))
21862 /* LISP_STRING is the one returned by decode_mode_spec. We should
21863 ignore its text properties. */
21864 it->stop_charpos = it->end_charpos;
21865
21866 /* If displaying STRING, set up the face of the iterator from
21867 FACE_STRING, if that's given. */
21868 if (STRINGP (face_string))
21869 {
21870 ptrdiff_t endptr;
21871 struct face *face;
21872
21873 it->face_id
21874 = face_at_string_position (it->w, face_string, face_string_pos,
21875 0, it->region_beg_charpos,
21876 it->region_end_charpos,
21877 &endptr, it->base_face_id, 0);
21878 face = FACE_FROM_ID (it->f, it->face_id);
21879 it->face_box_p = face->box != FACE_NO_BOX;
21880 }
21881
21882 /* Set max_x to the maximum allowed X position. Don't let it go
21883 beyond the right edge of the window. */
21884 if (max_x <= 0)
21885 max_x = it->last_visible_x;
21886 else
21887 max_x = min (max_x, it->last_visible_x);
21888
21889 /* Skip over display elements that are not visible. because IT->w is
21890 hscrolled. */
21891 if (it->current_x < it->first_visible_x)
21892 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21893 MOVE_TO_POS | MOVE_TO_X);
21894
21895 row->ascent = it->max_ascent;
21896 row->height = it->max_ascent + it->max_descent;
21897 row->phys_ascent = it->max_phys_ascent;
21898 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21899 row->extra_line_spacing = it->max_extra_line_spacing;
21900
21901 if (STRINGP (it->string))
21902 it_charpos = IT_STRING_CHARPOS (*it);
21903 else
21904 it_charpos = IT_CHARPOS (*it);
21905
21906 /* This condition is for the case that we are called with current_x
21907 past last_visible_x. */
21908 while (it->current_x < max_x)
21909 {
21910 int x_before, x, n_glyphs_before, i, nglyphs;
21911
21912 /* Get the next display element. */
21913 if (!get_next_display_element (it))
21914 break;
21915
21916 /* Produce glyphs. */
21917 x_before = it->current_x;
21918 n_glyphs_before = row->used[TEXT_AREA];
21919 PRODUCE_GLYPHS (it);
21920
21921 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21922 i = 0;
21923 x = x_before;
21924 while (i < nglyphs)
21925 {
21926 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21927
21928 if (it->line_wrap != TRUNCATE
21929 && x + glyph->pixel_width > max_x)
21930 {
21931 /* End of continued line or max_x reached. */
21932 if (CHAR_GLYPH_PADDING_P (*glyph))
21933 {
21934 /* A wide character is unbreakable. */
21935 if (row->reversed_p)
21936 unproduce_glyphs (it, row->used[TEXT_AREA]
21937 - n_glyphs_before);
21938 row->used[TEXT_AREA] = n_glyphs_before;
21939 it->current_x = x_before;
21940 }
21941 else
21942 {
21943 if (row->reversed_p)
21944 unproduce_glyphs (it, row->used[TEXT_AREA]
21945 - (n_glyphs_before + i));
21946 row->used[TEXT_AREA] = n_glyphs_before + i;
21947 it->current_x = x;
21948 }
21949 break;
21950 }
21951 else if (x + glyph->pixel_width >= it->first_visible_x)
21952 {
21953 /* Glyph is at least partially visible. */
21954 ++it->hpos;
21955 if (x < it->first_visible_x)
21956 row->x = x - it->first_visible_x;
21957 }
21958 else
21959 {
21960 /* Glyph is off the left margin of the display area.
21961 Should not happen. */
21962 abort ();
21963 }
21964
21965 row->ascent = max (row->ascent, it->max_ascent);
21966 row->height = max (row->height, it->max_ascent + it->max_descent);
21967 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21968 row->phys_height = max (row->phys_height,
21969 it->max_phys_ascent + it->max_phys_descent);
21970 row->extra_line_spacing = max (row->extra_line_spacing,
21971 it->max_extra_line_spacing);
21972 x += glyph->pixel_width;
21973 ++i;
21974 }
21975
21976 /* Stop if max_x reached. */
21977 if (i < nglyphs)
21978 break;
21979
21980 /* Stop at line ends. */
21981 if (ITERATOR_AT_END_OF_LINE_P (it))
21982 {
21983 it->continuation_lines_width = 0;
21984 break;
21985 }
21986
21987 set_iterator_to_next (it, 1);
21988 if (STRINGP (it->string))
21989 it_charpos = IT_STRING_CHARPOS (*it);
21990 else
21991 it_charpos = IT_CHARPOS (*it);
21992
21993 /* Stop if truncating at the right edge. */
21994 if (it->line_wrap == TRUNCATE
21995 && it->current_x >= it->last_visible_x)
21996 {
21997 /* Add truncation mark, but don't do it if the line is
21998 truncated at a padding space. */
21999 if (it_charpos < it->string_nchars)
22000 {
22001 if (!FRAME_WINDOW_P (it->f))
22002 {
22003 int ii, n;
22004
22005 if (it->current_x > it->last_visible_x)
22006 {
22007 if (!row->reversed_p)
22008 {
22009 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22010 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22011 break;
22012 }
22013 else
22014 {
22015 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22016 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22017 break;
22018 unproduce_glyphs (it, ii + 1);
22019 ii = row->used[TEXT_AREA] - (ii + 1);
22020 }
22021 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22022 {
22023 row->used[TEXT_AREA] = ii;
22024 produce_special_glyphs (it, IT_TRUNCATION);
22025 }
22026 }
22027 produce_special_glyphs (it, IT_TRUNCATION);
22028 }
22029 row->truncated_on_right_p = 1;
22030 }
22031 break;
22032 }
22033 }
22034
22035 /* Maybe insert a truncation at the left. */
22036 if (it->first_visible_x
22037 && it_charpos > 0)
22038 {
22039 if (!FRAME_WINDOW_P (it->f)
22040 || (row->reversed_p
22041 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22042 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22043 insert_left_trunc_glyphs (it);
22044 row->truncated_on_left_p = 1;
22045 }
22046
22047 it->face_id = saved_face_id;
22048
22049 /* Value is number of columns displayed. */
22050 return it->hpos - hpos_at_start;
22051 }
22052
22053
22054 \f
22055 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22056 appears as an element of LIST or as the car of an element of LIST.
22057 If PROPVAL is a list, compare each element against LIST in that
22058 way, and return 1/2 if any element of PROPVAL is found in LIST.
22059 Otherwise return 0. This function cannot quit.
22060 The return value is 2 if the text is invisible but with an ellipsis
22061 and 1 if it's invisible and without an ellipsis. */
22062
22063 int
22064 invisible_p (register Lisp_Object propval, Lisp_Object list)
22065 {
22066 register Lisp_Object tail, proptail;
22067
22068 for (tail = list; CONSP (tail); tail = XCDR (tail))
22069 {
22070 register Lisp_Object tem;
22071 tem = XCAR (tail);
22072 if (EQ (propval, tem))
22073 return 1;
22074 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22075 return NILP (XCDR (tem)) ? 1 : 2;
22076 }
22077
22078 if (CONSP (propval))
22079 {
22080 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22081 {
22082 Lisp_Object propelt;
22083 propelt = XCAR (proptail);
22084 for (tail = list; CONSP (tail); tail = XCDR (tail))
22085 {
22086 register Lisp_Object tem;
22087 tem = XCAR (tail);
22088 if (EQ (propelt, tem))
22089 return 1;
22090 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22091 return NILP (XCDR (tem)) ? 1 : 2;
22092 }
22093 }
22094 }
22095
22096 return 0;
22097 }
22098
22099 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22100 doc: /* Non-nil if the property makes the text invisible.
22101 POS-OR-PROP can be a marker or number, in which case it is taken to be
22102 a position in the current buffer and the value of the `invisible' property
22103 is checked; or it can be some other value, which is then presumed to be the
22104 value of the `invisible' property of the text of interest.
22105 The non-nil value returned can be t for truly invisible text or something
22106 else if the text is replaced by an ellipsis. */)
22107 (Lisp_Object pos_or_prop)
22108 {
22109 Lisp_Object prop
22110 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22111 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22112 : pos_or_prop);
22113 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22114 return (invis == 0 ? Qnil
22115 : invis == 1 ? Qt
22116 : make_number (invis));
22117 }
22118
22119 /* Calculate a width or height in pixels from a specification using
22120 the following elements:
22121
22122 SPEC ::=
22123 NUM - a (fractional) multiple of the default font width/height
22124 (NUM) - specifies exactly NUM pixels
22125 UNIT - a fixed number of pixels, see below.
22126 ELEMENT - size of a display element in pixels, see below.
22127 (NUM . SPEC) - equals NUM * SPEC
22128 (+ SPEC SPEC ...) - add pixel values
22129 (- SPEC SPEC ...) - subtract pixel values
22130 (- SPEC) - negate pixel value
22131
22132 NUM ::=
22133 INT or FLOAT - a number constant
22134 SYMBOL - use symbol's (buffer local) variable binding.
22135
22136 UNIT ::=
22137 in - pixels per inch *)
22138 mm - pixels per 1/1000 meter *)
22139 cm - pixels per 1/100 meter *)
22140 width - width of current font in pixels.
22141 height - height of current font in pixels.
22142
22143 *) using the ratio(s) defined in display-pixels-per-inch.
22144
22145 ELEMENT ::=
22146
22147 left-fringe - left fringe width in pixels
22148 right-fringe - right fringe width in pixels
22149
22150 left-margin - left margin width in pixels
22151 right-margin - right margin width in pixels
22152
22153 scroll-bar - scroll-bar area width in pixels
22154
22155 Examples:
22156
22157 Pixels corresponding to 5 inches:
22158 (5 . in)
22159
22160 Total width of non-text areas on left side of window (if scroll-bar is on left):
22161 '(space :width (+ left-fringe left-margin scroll-bar))
22162
22163 Align to first text column (in header line):
22164 '(space :align-to 0)
22165
22166 Align to middle of text area minus half the width of variable `my-image'
22167 containing a loaded image:
22168 '(space :align-to (0.5 . (- text my-image)))
22169
22170 Width of left margin minus width of 1 character in the default font:
22171 '(space :width (- left-margin 1))
22172
22173 Width of left margin minus width of 2 characters in the current font:
22174 '(space :width (- left-margin (2 . width)))
22175
22176 Center 1 character over left-margin (in header line):
22177 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22178
22179 Different ways to express width of left fringe plus left margin minus one pixel:
22180 '(space :width (- (+ left-fringe left-margin) (1)))
22181 '(space :width (+ left-fringe left-margin (- (1))))
22182 '(space :width (+ left-fringe left-margin (-1)))
22183
22184 */
22185
22186 #define NUMVAL(X) \
22187 ((INTEGERP (X) || FLOATP (X)) \
22188 ? XFLOATINT (X) \
22189 : - 1)
22190
22191 static int
22192 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22193 struct font *font, int width_p, int *align_to)
22194 {
22195 double pixels;
22196
22197 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22198 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22199
22200 if (NILP (prop))
22201 return OK_PIXELS (0);
22202
22203 eassert (FRAME_LIVE_P (it->f));
22204
22205 if (SYMBOLP (prop))
22206 {
22207 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22208 {
22209 char *unit = SSDATA (SYMBOL_NAME (prop));
22210
22211 if (unit[0] == 'i' && unit[1] == 'n')
22212 pixels = 1.0;
22213 else if (unit[0] == 'm' && unit[1] == 'm')
22214 pixels = 25.4;
22215 else if (unit[0] == 'c' && unit[1] == 'm')
22216 pixels = 2.54;
22217 else
22218 pixels = 0;
22219 if (pixels > 0)
22220 {
22221 double ppi;
22222 #ifdef HAVE_WINDOW_SYSTEM
22223 if (FRAME_WINDOW_P (it->f)
22224 && (ppi = (width_p
22225 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22226 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22227 ppi > 0))
22228 return OK_PIXELS (ppi / pixels);
22229 #endif
22230
22231 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22232 || (CONSP (Vdisplay_pixels_per_inch)
22233 && (ppi = (width_p
22234 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22235 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22236 ppi > 0)))
22237 return OK_PIXELS (ppi / pixels);
22238
22239 return 0;
22240 }
22241 }
22242
22243 #ifdef HAVE_WINDOW_SYSTEM
22244 if (EQ (prop, Qheight))
22245 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22246 if (EQ (prop, Qwidth))
22247 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22248 #else
22249 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22250 return OK_PIXELS (1);
22251 #endif
22252
22253 if (EQ (prop, Qtext))
22254 return OK_PIXELS (width_p
22255 ? window_box_width (it->w, TEXT_AREA)
22256 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22257
22258 if (align_to && *align_to < 0)
22259 {
22260 *res = 0;
22261 if (EQ (prop, Qleft))
22262 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22263 if (EQ (prop, Qright))
22264 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22265 if (EQ (prop, Qcenter))
22266 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22267 + window_box_width (it->w, TEXT_AREA) / 2);
22268 if (EQ (prop, Qleft_fringe))
22269 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22270 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22271 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22272 if (EQ (prop, Qright_fringe))
22273 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22274 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22275 : window_box_right_offset (it->w, TEXT_AREA));
22276 if (EQ (prop, Qleft_margin))
22277 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22278 if (EQ (prop, Qright_margin))
22279 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22280 if (EQ (prop, Qscroll_bar))
22281 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22282 ? 0
22283 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22284 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22285 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22286 : 0)));
22287 }
22288 else
22289 {
22290 if (EQ (prop, Qleft_fringe))
22291 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22292 if (EQ (prop, Qright_fringe))
22293 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22294 if (EQ (prop, Qleft_margin))
22295 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22296 if (EQ (prop, Qright_margin))
22297 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22298 if (EQ (prop, Qscroll_bar))
22299 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22300 }
22301
22302 prop = buffer_local_value_1 (prop, it->w->buffer);
22303 if (EQ (prop, Qunbound))
22304 prop = Qnil;
22305 }
22306
22307 if (INTEGERP (prop) || FLOATP (prop))
22308 {
22309 int base_unit = (width_p
22310 ? FRAME_COLUMN_WIDTH (it->f)
22311 : FRAME_LINE_HEIGHT (it->f));
22312 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22313 }
22314
22315 if (CONSP (prop))
22316 {
22317 Lisp_Object car = XCAR (prop);
22318 Lisp_Object cdr = XCDR (prop);
22319
22320 if (SYMBOLP (car))
22321 {
22322 #ifdef HAVE_WINDOW_SYSTEM
22323 if (FRAME_WINDOW_P (it->f)
22324 && valid_image_p (prop))
22325 {
22326 ptrdiff_t id = lookup_image (it->f, prop);
22327 struct image *img = IMAGE_FROM_ID (it->f, id);
22328
22329 return OK_PIXELS (width_p ? img->width : img->height);
22330 }
22331 #endif
22332 if (EQ (car, Qplus) || EQ (car, Qminus))
22333 {
22334 int first = 1;
22335 double px;
22336
22337 pixels = 0;
22338 while (CONSP (cdr))
22339 {
22340 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22341 font, width_p, align_to))
22342 return 0;
22343 if (first)
22344 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22345 else
22346 pixels += px;
22347 cdr = XCDR (cdr);
22348 }
22349 if (EQ (car, Qminus))
22350 pixels = -pixels;
22351 return OK_PIXELS (pixels);
22352 }
22353
22354 car = buffer_local_value_1 (car, it->w->buffer);
22355 if (EQ (car, Qunbound))
22356 car = Qnil;
22357 }
22358
22359 if (INTEGERP (car) || FLOATP (car))
22360 {
22361 double fact;
22362 pixels = XFLOATINT (car);
22363 if (NILP (cdr))
22364 return OK_PIXELS (pixels);
22365 if (calc_pixel_width_or_height (&fact, it, cdr,
22366 font, width_p, align_to))
22367 return OK_PIXELS (pixels * fact);
22368 return 0;
22369 }
22370
22371 return 0;
22372 }
22373
22374 return 0;
22375 }
22376
22377 \f
22378 /***********************************************************************
22379 Glyph Display
22380 ***********************************************************************/
22381
22382 #ifdef HAVE_WINDOW_SYSTEM
22383
22384 #ifdef GLYPH_DEBUG
22385
22386 void
22387 dump_glyph_string (struct glyph_string *s)
22388 {
22389 fprintf (stderr, "glyph string\n");
22390 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22391 s->x, s->y, s->width, s->height);
22392 fprintf (stderr, " ybase = %d\n", s->ybase);
22393 fprintf (stderr, " hl = %d\n", s->hl);
22394 fprintf (stderr, " left overhang = %d, right = %d\n",
22395 s->left_overhang, s->right_overhang);
22396 fprintf (stderr, " nchars = %d\n", s->nchars);
22397 fprintf (stderr, " extends to end of line = %d\n",
22398 s->extends_to_end_of_line_p);
22399 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22400 fprintf (stderr, " bg width = %d\n", s->background_width);
22401 }
22402
22403 #endif /* GLYPH_DEBUG */
22404
22405 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22406 of XChar2b structures for S; it can't be allocated in
22407 init_glyph_string because it must be allocated via `alloca'. W
22408 is the window on which S is drawn. ROW and AREA are the glyph row
22409 and area within the row from which S is constructed. START is the
22410 index of the first glyph structure covered by S. HL is a
22411 face-override for drawing S. */
22412
22413 #ifdef HAVE_NTGUI
22414 #define OPTIONAL_HDC(hdc) HDC hdc,
22415 #define DECLARE_HDC(hdc) HDC hdc;
22416 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22417 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22418 #endif
22419
22420 #ifndef OPTIONAL_HDC
22421 #define OPTIONAL_HDC(hdc)
22422 #define DECLARE_HDC(hdc)
22423 #define ALLOCATE_HDC(hdc, f)
22424 #define RELEASE_HDC(hdc, f)
22425 #endif
22426
22427 static void
22428 init_glyph_string (struct glyph_string *s,
22429 OPTIONAL_HDC (hdc)
22430 XChar2b *char2b, struct window *w, struct glyph_row *row,
22431 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22432 {
22433 memset (s, 0, sizeof *s);
22434 s->w = w;
22435 s->f = XFRAME (w->frame);
22436 #ifdef HAVE_NTGUI
22437 s->hdc = hdc;
22438 #endif
22439 s->display = FRAME_X_DISPLAY (s->f);
22440 s->window = FRAME_X_WINDOW (s->f);
22441 s->char2b = char2b;
22442 s->hl = hl;
22443 s->row = row;
22444 s->area = area;
22445 s->first_glyph = row->glyphs[area] + start;
22446 s->height = row->height;
22447 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22448 s->ybase = s->y + row->ascent;
22449 }
22450
22451
22452 /* Append the list of glyph strings with head H and tail T to the list
22453 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22454
22455 static inline void
22456 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22457 struct glyph_string *h, struct glyph_string *t)
22458 {
22459 if (h)
22460 {
22461 if (*head)
22462 (*tail)->next = h;
22463 else
22464 *head = h;
22465 h->prev = *tail;
22466 *tail = t;
22467 }
22468 }
22469
22470
22471 /* Prepend the list of glyph strings with head H and tail T to the
22472 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22473 result. */
22474
22475 static inline void
22476 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22477 struct glyph_string *h, struct glyph_string *t)
22478 {
22479 if (h)
22480 {
22481 if (*head)
22482 (*head)->prev = t;
22483 else
22484 *tail = t;
22485 t->next = *head;
22486 *head = h;
22487 }
22488 }
22489
22490
22491 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22492 Set *HEAD and *TAIL to the resulting list. */
22493
22494 static inline void
22495 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22496 struct glyph_string *s)
22497 {
22498 s->next = s->prev = NULL;
22499 append_glyph_string_lists (head, tail, s, s);
22500 }
22501
22502
22503 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22504 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22505 make sure that X resources for the face returned are allocated.
22506 Value is a pointer to a realized face that is ready for display if
22507 DISPLAY_P is non-zero. */
22508
22509 static inline struct face *
22510 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22511 XChar2b *char2b, int display_p)
22512 {
22513 struct face *face = FACE_FROM_ID (f, face_id);
22514
22515 if (face->font)
22516 {
22517 unsigned code = face->font->driver->encode_char (face->font, c);
22518
22519 if (code != FONT_INVALID_CODE)
22520 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22521 else
22522 STORE_XCHAR2B (char2b, 0, 0);
22523 }
22524
22525 /* Make sure X resources of the face are allocated. */
22526 #ifdef HAVE_X_WINDOWS
22527 if (display_p)
22528 #endif
22529 {
22530 eassert (face != NULL);
22531 PREPARE_FACE_FOR_DISPLAY (f, face);
22532 }
22533
22534 return face;
22535 }
22536
22537
22538 /* Get face and two-byte form of character glyph GLYPH on frame F.
22539 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22540 a pointer to a realized face that is ready for display. */
22541
22542 static inline struct face *
22543 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22544 XChar2b *char2b, int *two_byte_p)
22545 {
22546 struct face *face;
22547
22548 eassert (glyph->type == CHAR_GLYPH);
22549 face = FACE_FROM_ID (f, glyph->face_id);
22550
22551 if (two_byte_p)
22552 *two_byte_p = 0;
22553
22554 if (face->font)
22555 {
22556 unsigned code;
22557
22558 if (CHAR_BYTE8_P (glyph->u.ch))
22559 code = CHAR_TO_BYTE8 (glyph->u.ch);
22560 else
22561 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22562
22563 if (code != FONT_INVALID_CODE)
22564 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22565 else
22566 STORE_XCHAR2B (char2b, 0, 0);
22567 }
22568
22569 /* Make sure X resources of the face are allocated. */
22570 eassert (face != NULL);
22571 PREPARE_FACE_FOR_DISPLAY (f, face);
22572 return face;
22573 }
22574
22575
22576 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22577 Return 1 if FONT has a glyph for C, otherwise return 0. */
22578
22579 static inline int
22580 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22581 {
22582 unsigned code;
22583
22584 if (CHAR_BYTE8_P (c))
22585 code = CHAR_TO_BYTE8 (c);
22586 else
22587 code = font->driver->encode_char (font, c);
22588
22589 if (code == FONT_INVALID_CODE)
22590 return 0;
22591 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22592 return 1;
22593 }
22594
22595
22596 /* Fill glyph string S with composition components specified by S->cmp.
22597
22598 BASE_FACE is the base face of the composition.
22599 S->cmp_from is the index of the first component for S.
22600
22601 OVERLAPS non-zero means S should draw the foreground only, and use
22602 its physical height for clipping. See also draw_glyphs.
22603
22604 Value is the index of a component not in S. */
22605
22606 static int
22607 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22608 int overlaps)
22609 {
22610 int i;
22611 /* For all glyphs of this composition, starting at the offset
22612 S->cmp_from, until we reach the end of the definition or encounter a
22613 glyph that requires the different face, add it to S. */
22614 struct face *face;
22615
22616 eassert (s);
22617
22618 s->for_overlaps = overlaps;
22619 s->face = NULL;
22620 s->font = NULL;
22621 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22622 {
22623 int c = COMPOSITION_GLYPH (s->cmp, i);
22624
22625 /* TAB in a composition means display glyphs with padding space
22626 on the left or right. */
22627 if (c != '\t')
22628 {
22629 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22630 -1, Qnil);
22631
22632 face = get_char_face_and_encoding (s->f, c, face_id,
22633 s->char2b + i, 1);
22634 if (face)
22635 {
22636 if (! s->face)
22637 {
22638 s->face = face;
22639 s->font = s->face->font;
22640 }
22641 else if (s->face != face)
22642 break;
22643 }
22644 }
22645 ++s->nchars;
22646 }
22647 s->cmp_to = i;
22648
22649 if (s->face == NULL)
22650 {
22651 s->face = base_face->ascii_face;
22652 s->font = s->face->font;
22653 }
22654
22655 /* All glyph strings for the same composition has the same width,
22656 i.e. the width set for the first component of the composition. */
22657 s->width = s->first_glyph->pixel_width;
22658
22659 /* If the specified font could not be loaded, use the frame's
22660 default font, but record the fact that we couldn't load it in
22661 the glyph string so that we can draw rectangles for the
22662 characters of the glyph string. */
22663 if (s->font == NULL)
22664 {
22665 s->font_not_found_p = 1;
22666 s->font = FRAME_FONT (s->f);
22667 }
22668
22669 /* Adjust base line for subscript/superscript text. */
22670 s->ybase += s->first_glyph->voffset;
22671
22672 /* This glyph string must always be drawn with 16-bit functions. */
22673 s->two_byte_p = 1;
22674
22675 return s->cmp_to;
22676 }
22677
22678 static int
22679 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22680 int start, int end, int overlaps)
22681 {
22682 struct glyph *glyph, *last;
22683 Lisp_Object lgstring;
22684 int i;
22685
22686 s->for_overlaps = overlaps;
22687 glyph = s->row->glyphs[s->area] + start;
22688 last = s->row->glyphs[s->area] + end;
22689 s->cmp_id = glyph->u.cmp.id;
22690 s->cmp_from = glyph->slice.cmp.from;
22691 s->cmp_to = glyph->slice.cmp.to + 1;
22692 s->face = FACE_FROM_ID (s->f, face_id);
22693 lgstring = composition_gstring_from_id (s->cmp_id);
22694 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22695 glyph++;
22696 while (glyph < last
22697 && glyph->u.cmp.automatic
22698 && glyph->u.cmp.id == s->cmp_id
22699 && s->cmp_to == glyph->slice.cmp.from)
22700 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22701
22702 for (i = s->cmp_from; i < s->cmp_to; i++)
22703 {
22704 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22705 unsigned code = LGLYPH_CODE (lglyph);
22706
22707 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22708 }
22709 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22710 return glyph - s->row->glyphs[s->area];
22711 }
22712
22713
22714 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22715 See the comment of fill_glyph_string for arguments.
22716 Value is the index of the first glyph not in S. */
22717
22718
22719 static int
22720 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22721 int start, int end, int overlaps)
22722 {
22723 struct glyph *glyph, *last;
22724 int voffset;
22725
22726 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22727 s->for_overlaps = overlaps;
22728 glyph = s->row->glyphs[s->area] + start;
22729 last = s->row->glyphs[s->area] + end;
22730 voffset = glyph->voffset;
22731 s->face = FACE_FROM_ID (s->f, face_id);
22732 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22733 s->nchars = 1;
22734 s->width = glyph->pixel_width;
22735 glyph++;
22736 while (glyph < last
22737 && glyph->type == GLYPHLESS_GLYPH
22738 && glyph->voffset == voffset
22739 && glyph->face_id == face_id)
22740 {
22741 s->nchars++;
22742 s->width += glyph->pixel_width;
22743 glyph++;
22744 }
22745 s->ybase += voffset;
22746 return glyph - s->row->glyphs[s->area];
22747 }
22748
22749
22750 /* Fill glyph string S from a sequence of character glyphs.
22751
22752 FACE_ID is the face id of the string. START is the index of the
22753 first glyph to consider, END is the index of the last + 1.
22754 OVERLAPS non-zero means S should draw the foreground only, and use
22755 its physical height for clipping. See also draw_glyphs.
22756
22757 Value is the index of the first glyph not in S. */
22758
22759 static int
22760 fill_glyph_string (struct glyph_string *s, int face_id,
22761 int start, int end, int overlaps)
22762 {
22763 struct glyph *glyph, *last;
22764 int voffset;
22765 int glyph_not_available_p;
22766
22767 eassert (s->f == XFRAME (s->w->frame));
22768 eassert (s->nchars == 0);
22769 eassert (start >= 0 && end > start);
22770
22771 s->for_overlaps = overlaps;
22772 glyph = s->row->glyphs[s->area] + start;
22773 last = s->row->glyphs[s->area] + end;
22774 voffset = glyph->voffset;
22775 s->padding_p = glyph->padding_p;
22776 glyph_not_available_p = glyph->glyph_not_available_p;
22777
22778 while (glyph < last
22779 && glyph->type == CHAR_GLYPH
22780 && glyph->voffset == voffset
22781 /* Same face id implies same font, nowadays. */
22782 && glyph->face_id == face_id
22783 && glyph->glyph_not_available_p == glyph_not_available_p)
22784 {
22785 int two_byte_p;
22786
22787 s->face = get_glyph_face_and_encoding (s->f, glyph,
22788 s->char2b + s->nchars,
22789 &two_byte_p);
22790 s->two_byte_p = two_byte_p;
22791 ++s->nchars;
22792 eassert (s->nchars <= end - start);
22793 s->width += glyph->pixel_width;
22794 if (glyph++->padding_p != s->padding_p)
22795 break;
22796 }
22797
22798 s->font = s->face->font;
22799
22800 /* If the specified font could not be loaded, use the frame's font,
22801 but record the fact that we couldn't load it in
22802 S->font_not_found_p so that we can draw rectangles for the
22803 characters of the glyph string. */
22804 if (s->font == NULL || glyph_not_available_p)
22805 {
22806 s->font_not_found_p = 1;
22807 s->font = FRAME_FONT (s->f);
22808 }
22809
22810 /* Adjust base line for subscript/superscript text. */
22811 s->ybase += voffset;
22812
22813 eassert (s->face && s->face->gc);
22814 return glyph - s->row->glyphs[s->area];
22815 }
22816
22817
22818 /* Fill glyph string S from image glyph S->first_glyph. */
22819
22820 static void
22821 fill_image_glyph_string (struct glyph_string *s)
22822 {
22823 eassert (s->first_glyph->type == IMAGE_GLYPH);
22824 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22825 eassert (s->img);
22826 s->slice = s->first_glyph->slice.img;
22827 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22828 s->font = s->face->font;
22829 s->width = s->first_glyph->pixel_width;
22830
22831 /* Adjust base line for subscript/superscript text. */
22832 s->ybase += s->first_glyph->voffset;
22833 }
22834
22835
22836 /* Fill glyph string S from a sequence of stretch glyphs.
22837
22838 START is the index of the first glyph to consider,
22839 END is the index of the last + 1.
22840
22841 Value is the index of the first glyph not in S. */
22842
22843 static int
22844 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22845 {
22846 struct glyph *glyph, *last;
22847 int voffset, face_id;
22848
22849 eassert (s->first_glyph->type == STRETCH_GLYPH);
22850
22851 glyph = s->row->glyphs[s->area] + start;
22852 last = s->row->glyphs[s->area] + end;
22853 face_id = glyph->face_id;
22854 s->face = FACE_FROM_ID (s->f, face_id);
22855 s->font = s->face->font;
22856 s->width = glyph->pixel_width;
22857 s->nchars = 1;
22858 voffset = glyph->voffset;
22859
22860 for (++glyph;
22861 (glyph < last
22862 && glyph->type == STRETCH_GLYPH
22863 && glyph->voffset == voffset
22864 && glyph->face_id == face_id);
22865 ++glyph)
22866 s->width += glyph->pixel_width;
22867
22868 /* Adjust base line for subscript/superscript text. */
22869 s->ybase += voffset;
22870
22871 /* The case that face->gc == 0 is handled when drawing the glyph
22872 string by calling PREPARE_FACE_FOR_DISPLAY. */
22873 eassert (s->face);
22874 return glyph - s->row->glyphs[s->area];
22875 }
22876
22877 static struct font_metrics *
22878 get_per_char_metric (struct font *font, XChar2b *char2b)
22879 {
22880 static struct font_metrics metrics;
22881 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22882
22883 if (! font || code == FONT_INVALID_CODE)
22884 return NULL;
22885 font->driver->text_extents (font, &code, 1, &metrics);
22886 return &metrics;
22887 }
22888
22889 /* EXPORT for RIF:
22890 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22891 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22892 assumed to be zero. */
22893
22894 void
22895 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22896 {
22897 *left = *right = 0;
22898
22899 if (glyph->type == CHAR_GLYPH)
22900 {
22901 struct face *face;
22902 XChar2b char2b;
22903 struct font_metrics *pcm;
22904
22905 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22906 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22907 {
22908 if (pcm->rbearing > pcm->width)
22909 *right = pcm->rbearing - pcm->width;
22910 if (pcm->lbearing < 0)
22911 *left = -pcm->lbearing;
22912 }
22913 }
22914 else if (glyph->type == COMPOSITE_GLYPH)
22915 {
22916 if (! glyph->u.cmp.automatic)
22917 {
22918 struct composition *cmp = composition_table[glyph->u.cmp.id];
22919
22920 if (cmp->rbearing > cmp->pixel_width)
22921 *right = cmp->rbearing - cmp->pixel_width;
22922 if (cmp->lbearing < 0)
22923 *left = - cmp->lbearing;
22924 }
22925 else
22926 {
22927 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22928 struct font_metrics metrics;
22929
22930 composition_gstring_width (gstring, glyph->slice.cmp.from,
22931 glyph->slice.cmp.to + 1, &metrics);
22932 if (metrics.rbearing > metrics.width)
22933 *right = metrics.rbearing - metrics.width;
22934 if (metrics.lbearing < 0)
22935 *left = - metrics.lbearing;
22936 }
22937 }
22938 }
22939
22940
22941 /* Return the index of the first glyph preceding glyph string S that
22942 is overwritten by S because of S's left overhang. Value is -1
22943 if no glyphs are overwritten. */
22944
22945 static int
22946 left_overwritten (struct glyph_string *s)
22947 {
22948 int k;
22949
22950 if (s->left_overhang)
22951 {
22952 int x = 0, i;
22953 struct glyph *glyphs = s->row->glyphs[s->area];
22954 int first = s->first_glyph - glyphs;
22955
22956 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22957 x -= glyphs[i].pixel_width;
22958
22959 k = i + 1;
22960 }
22961 else
22962 k = -1;
22963
22964 return k;
22965 }
22966
22967
22968 /* Return the index of the first glyph preceding glyph string S that
22969 is overwriting S because of its right overhang. Value is -1 if no
22970 glyph in front of S overwrites S. */
22971
22972 static int
22973 left_overwriting (struct glyph_string *s)
22974 {
22975 int i, k, x;
22976 struct glyph *glyphs = s->row->glyphs[s->area];
22977 int first = s->first_glyph - glyphs;
22978
22979 k = -1;
22980 x = 0;
22981 for (i = first - 1; i >= 0; --i)
22982 {
22983 int left, right;
22984 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22985 if (x + right > 0)
22986 k = i;
22987 x -= glyphs[i].pixel_width;
22988 }
22989
22990 return k;
22991 }
22992
22993
22994 /* Return the index of the last glyph following glyph string S that is
22995 overwritten by S because of S's right overhang. Value is -1 if
22996 no such glyph is found. */
22997
22998 static int
22999 right_overwritten (struct glyph_string *s)
23000 {
23001 int k = -1;
23002
23003 if (s->right_overhang)
23004 {
23005 int x = 0, i;
23006 struct glyph *glyphs = s->row->glyphs[s->area];
23007 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23008 int end = s->row->used[s->area];
23009
23010 for (i = first; i < end && s->right_overhang > x; ++i)
23011 x += glyphs[i].pixel_width;
23012
23013 k = i;
23014 }
23015
23016 return k;
23017 }
23018
23019
23020 /* Return the index of the last glyph following glyph string S that
23021 overwrites S because of its left overhang. Value is negative
23022 if no such glyph is found. */
23023
23024 static int
23025 right_overwriting (struct glyph_string *s)
23026 {
23027 int i, k, x;
23028 int end = s->row->used[s->area];
23029 struct glyph *glyphs = s->row->glyphs[s->area];
23030 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23031
23032 k = -1;
23033 x = 0;
23034 for (i = first; i < end; ++i)
23035 {
23036 int left, right;
23037 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23038 if (x - left < 0)
23039 k = i;
23040 x += glyphs[i].pixel_width;
23041 }
23042
23043 return k;
23044 }
23045
23046
23047 /* Set background width of glyph string S. START is the index of the
23048 first glyph following S. LAST_X is the right-most x-position + 1
23049 in the drawing area. */
23050
23051 static inline void
23052 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23053 {
23054 /* If the face of this glyph string has to be drawn to the end of
23055 the drawing area, set S->extends_to_end_of_line_p. */
23056
23057 if (start == s->row->used[s->area]
23058 && s->area == TEXT_AREA
23059 && ((s->row->fill_line_p
23060 && (s->hl == DRAW_NORMAL_TEXT
23061 || s->hl == DRAW_IMAGE_RAISED
23062 || s->hl == DRAW_IMAGE_SUNKEN))
23063 || s->hl == DRAW_MOUSE_FACE))
23064 s->extends_to_end_of_line_p = 1;
23065
23066 /* If S extends its face to the end of the line, set its
23067 background_width to the distance to the right edge of the drawing
23068 area. */
23069 if (s->extends_to_end_of_line_p)
23070 s->background_width = last_x - s->x + 1;
23071 else
23072 s->background_width = s->width;
23073 }
23074
23075
23076 /* Compute overhangs and x-positions for glyph string S and its
23077 predecessors, or successors. X is the starting x-position for S.
23078 BACKWARD_P non-zero means process predecessors. */
23079
23080 static void
23081 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23082 {
23083 if (backward_p)
23084 {
23085 while (s)
23086 {
23087 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23088 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23089 x -= s->width;
23090 s->x = x;
23091 s = s->prev;
23092 }
23093 }
23094 else
23095 {
23096 while (s)
23097 {
23098 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23099 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23100 s->x = x;
23101 x += s->width;
23102 s = s->next;
23103 }
23104 }
23105 }
23106
23107
23108
23109 /* The following macros are only called from draw_glyphs below.
23110 They reference the following parameters of that function directly:
23111 `w', `row', `area', and `overlap_p'
23112 as well as the following local variables:
23113 `s', `f', and `hdc' (in W32) */
23114
23115 #ifdef HAVE_NTGUI
23116 /* On W32, silently add local `hdc' variable to argument list of
23117 init_glyph_string. */
23118 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23119 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23120 #else
23121 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23122 init_glyph_string (s, char2b, w, row, area, start, hl)
23123 #endif
23124
23125 /* Add a glyph string for a stretch glyph to the list of strings
23126 between HEAD and TAIL. START is the index of the stretch glyph in
23127 row area AREA of glyph row ROW. END is the index of the last glyph
23128 in that glyph row area. X is the current output position assigned
23129 to the new glyph string constructed. HL overrides that face of the
23130 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23131 is the right-most x-position of the drawing area. */
23132
23133 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23134 and below -- keep them on one line. */
23135 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23136 do \
23137 { \
23138 s = alloca (sizeof *s); \
23139 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23140 START = fill_stretch_glyph_string (s, START, END); \
23141 append_glyph_string (&HEAD, &TAIL, s); \
23142 s->x = (X); \
23143 } \
23144 while (0)
23145
23146
23147 /* Add a glyph string for an image glyph to the list of strings
23148 between HEAD and TAIL. START is the index of the image glyph in
23149 row area AREA of glyph row ROW. END is the index of the last glyph
23150 in that glyph row area. X is the current output position assigned
23151 to the new glyph string constructed. HL overrides that face of the
23152 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23153 is the right-most x-position of the drawing area. */
23154
23155 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23156 do \
23157 { \
23158 s = alloca (sizeof *s); \
23159 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23160 fill_image_glyph_string (s); \
23161 append_glyph_string (&HEAD, &TAIL, s); \
23162 ++START; \
23163 s->x = (X); \
23164 } \
23165 while (0)
23166
23167
23168 /* Add a glyph string for a sequence of character glyphs to the list
23169 of strings between HEAD and TAIL. START is the index of the first
23170 glyph in row area AREA of glyph row ROW that is part of the new
23171 glyph string. END is the index of the last glyph in that glyph row
23172 area. X is the current output position assigned to the new glyph
23173 string constructed. HL overrides that face of the glyph; e.g. it
23174 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23175 right-most x-position of the drawing area. */
23176
23177 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23178 do \
23179 { \
23180 int face_id; \
23181 XChar2b *char2b; \
23182 \
23183 face_id = (row)->glyphs[area][START].face_id; \
23184 \
23185 s = alloca (sizeof *s); \
23186 char2b = alloca ((END - START) * sizeof *char2b); \
23187 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23188 append_glyph_string (&HEAD, &TAIL, s); \
23189 s->x = (X); \
23190 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23191 } \
23192 while (0)
23193
23194
23195 /* Add a glyph string for a composite sequence to the list of strings
23196 between HEAD and TAIL. START is the index of the first glyph in
23197 row area AREA of glyph row ROW that is part of the new glyph
23198 string. END is the index of the last glyph in that glyph row area.
23199 X is the current output position assigned to the new glyph string
23200 constructed. HL overrides that face of the glyph; e.g. it is
23201 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23202 x-position of the drawing area. */
23203
23204 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23205 do { \
23206 int face_id = (row)->glyphs[area][START].face_id; \
23207 struct face *base_face = FACE_FROM_ID (f, face_id); \
23208 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23209 struct composition *cmp = composition_table[cmp_id]; \
23210 XChar2b *char2b; \
23211 struct glyph_string *first_s = NULL; \
23212 int n; \
23213 \
23214 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23215 \
23216 /* Make glyph_strings for each glyph sequence that is drawable by \
23217 the same face, and append them to HEAD/TAIL. */ \
23218 for (n = 0; n < cmp->glyph_len;) \
23219 { \
23220 s = alloca (sizeof *s); \
23221 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23222 append_glyph_string (&(HEAD), &(TAIL), s); \
23223 s->cmp = cmp; \
23224 s->cmp_from = n; \
23225 s->x = (X); \
23226 if (n == 0) \
23227 first_s = s; \
23228 n = fill_composite_glyph_string (s, base_face, overlaps); \
23229 } \
23230 \
23231 ++START; \
23232 s = first_s; \
23233 } while (0)
23234
23235
23236 /* Add a glyph string for a glyph-string sequence to the list of strings
23237 between HEAD and TAIL. */
23238
23239 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23240 do { \
23241 int face_id; \
23242 XChar2b *char2b; \
23243 Lisp_Object gstring; \
23244 \
23245 face_id = (row)->glyphs[area][START].face_id; \
23246 gstring = (composition_gstring_from_id \
23247 ((row)->glyphs[area][START].u.cmp.id)); \
23248 s = alloca (sizeof *s); \
23249 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23250 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23251 append_glyph_string (&(HEAD), &(TAIL), s); \
23252 s->x = (X); \
23253 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23254 } while (0)
23255
23256
23257 /* Add a glyph string for a sequence of glyphless character's glyphs
23258 to the list of strings between HEAD and TAIL. The meanings of
23259 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23260
23261 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23262 do \
23263 { \
23264 int face_id; \
23265 \
23266 face_id = (row)->glyphs[area][START].face_id; \
23267 \
23268 s = alloca (sizeof *s); \
23269 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23270 append_glyph_string (&HEAD, &TAIL, s); \
23271 s->x = (X); \
23272 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23273 overlaps); \
23274 } \
23275 while (0)
23276
23277
23278 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23279 of AREA of glyph row ROW on window W between indices START and END.
23280 HL overrides the face for drawing glyph strings, e.g. it is
23281 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23282 x-positions of the drawing area.
23283
23284 This is an ugly monster macro construct because we must use alloca
23285 to allocate glyph strings (because draw_glyphs can be called
23286 asynchronously). */
23287
23288 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23289 do \
23290 { \
23291 HEAD = TAIL = NULL; \
23292 while (START < END) \
23293 { \
23294 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23295 switch (first_glyph->type) \
23296 { \
23297 case CHAR_GLYPH: \
23298 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23299 HL, X, LAST_X); \
23300 break; \
23301 \
23302 case COMPOSITE_GLYPH: \
23303 if (first_glyph->u.cmp.automatic) \
23304 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23305 HL, X, LAST_X); \
23306 else \
23307 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23308 HL, X, LAST_X); \
23309 break; \
23310 \
23311 case STRETCH_GLYPH: \
23312 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23313 HL, X, LAST_X); \
23314 break; \
23315 \
23316 case IMAGE_GLYPH: \
23317 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23318 HL, X, LAST_X); \
23319 break; \
23320 \
23321 case GLYPHLESS_GLYPH: \
23322 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23323 HL, X, LAST_X); \
23324 break; \
23325 \
23326 default: \
23327 abort (); \
23328 } \
23329 \
23330 if (s) \
23331 { \
23332 set_glyph_string_background_width (s, START, LAST_X); \
23333 (X) += s->width; \
23334 } \
23335 } \
23336 } while (0)
23337
23338
23339 /* Draw glyphs between START and END in AREA of ROW on window W,
23340 starting at x-position X. X is relative to AREA in W. HL is a
23341 face-override with the following meaning:
23342
23343 DRAW_NORMAL_TEXT draw normally
23344 DRAW_CURSOR draw in cursor face
23345 DRAW_MOUSE_FACE draw in mouse face.
23346 DRAW_INVERSE_VIDEO draw in mode line face
23347 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23348 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23349
23350 If OVERLAPS is non-zero, draw only the foreground of characters and
23351 clip to the physical height of ROW. Non-zero value also defines
23352 the overlapping part to be drawn:
23353
23354 OVERLAPS_PRED overlap with preceding rows
23355 OVERLAPS_SUCC overlap with succeeding rows
23356 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23357 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23358
23359 Value is the x-position reached, relative to AREA of W. */
23360
23361 static int
23362 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23363 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23364 enum draw_glyphs_face hl, int overlaps)
23365 {
23366 struct glyph_string *head, *tail;
23367 struct glyph_string *s;
23368 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23369 int i, j, x_reached, last_x, area_left = 0;
23370 struct frame *f = XFRAME (WINDOW_FRAME (w));
23371 DECLARE_HDC (hdc);
23372
23373 ALLOCATE_HDC (hdc, f);
23374
23375 /* Let's rather be paranoid than getting a SEGV. */
23376 end = min (end, row->used[area]);
23377 start = max (0, start);
23378 start = min (end, start);
23379
23380 /* Translate X to frame coordinates. Set last_x to the right
23381 end of the drawing area. */
23382 if (row->full_width_p)
23383 {
23384 /* X is relative to the left edge of W, without scroll bars
23385 or fringes. */
23386 area_left = WINDOW_LEFT_EDGE_X (w);
23387 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23388 }
23389 else
23390 {
23391 area_left = window_box_left (w, area);
23392 last_x = area_left + window_box_width (w, area);
23393 }
23394 x += area_left;
23395
23396 /* Build a doubly-linked list of glyph_string structures between
23397 head and tail from what we have to draw. Note that the macro
23398 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23399 the reason we use a separate variable `i'. */
23400 i = start;
23401 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23402 if (tail)
23403 x_reached = tail->x + tail->background_width;
23404 else
23405 x_reached = x;
23406
23407 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23408 the row, redraw some glyphs in front or following the glyph
23409 strings built above. */
23410 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23411 {
23412 struct glyph_string *h, *t;
23413 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23414 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23415 int check_mouse_face = 0;
23416 int dummy_x = 0;
23417
23418 /* If mouse highlighting is on, we may need to draw adjacent
23419 glyphs using mouse-face highlighting. */
23420 if (area == TEXT_AREA && row->mouse_face_p)
23421 {
23422 struct glyph_row *mouse_beg_row, *mouse_end_row;
23423
23424 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23425 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23426
23427 if (row >= mouse_beg_row && row <= mouse_end_row)
23428 {
23429 check_mouse_face = 1;
23430 mouse_beg_col = (row == mouse_beg_row)
23431 ? hlinfo->mouse_face_beg_col : 0;
23432 mouse_end_col = (row == mouse_end_row)
23433 ? hlinfo->mouse_face_end_col
23434 : row->used[TEXT_AREA];
23435 }
23436 }
23437
23438 /* Compute overhangs for all glyph strings. */
23439 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23440 for (s = head; s; s = s->next)
23441 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23442
23443 /* Prepend glyph strings for glyphs in front of the first glyph
23444 string that are overwritten because of the first glyph
23445 string's left overhang. The background of all strings
23446 prepended must be drawn because the first glyph string
23447 draws over it. */
23448 i = left_overwritten (head);
23449 if (i >= 0)
23450 {
23451 enum draw_glyphs_face overlap_hl;
23452
23453 /* If this row contains mouse highlighting, attempt to draw
23454 the overlapped glyphs with the correct highlight. This
23455 code fails if the overlap encompasses more than one glyph
23456 and mouse-highlight spans only some of these glyphs.
23457 However, making it work perfectly involves a lot more
23458 code, and I don't know if the pathological case occurs in
23459 practice, so we'll stick to this for now. --- cyd */
23460 if (check_mouse_face
23461 && mouse_beg_col < start && mouse_end_col > i)
23462 overlap_hl = DRAW_MOUSE_FACE;
23463 else
23464 overlap_hl = DRAW_NORMAL_TEXT;
23465
23466 j = i;
23467 BUILD_GLYPH_STRINGS (j, start, h, t,
23468 overlap_hl, dummy_x, last_x);
23469 start = i;
23470 compute_overhangs_and_x (t, head->x, 1);
23471 prepend_glyph_string_lists (&head, &tail, h, t);
23472 clip_head = head;
23473 }
23474
23475 /* Prepend glyph strings for glyphs in front of the first glyph
23476 string that overwrite that glyph string because of their
23477 right overhang. For these strings, only the foreground must
23478 be drawn, because it draws over the glyph string at `head'.
23479 The background must not be drawn because this would overwrite
23480 right overhangs of preceding glyphs for which no glyph
23481 strings exist. */
23482 i = left_overwriting (head);
23483 if (i >= 0)
23484 {
23485 enum draw_glyphs_face overlap_hl;
23486
23487 if (check_mouse_face
23488 && mouse_beg_col < start && mouse_end_col > i)
23489 overlap_hl = DRAW_MOUSE_FACE;
23490 else
23491 overlap_hl = DRAW_NORMAL_TEXT;
23492
23493 clip_head = head;
23494 BUILD_GLYPH_STRINGS (i, start, h, t,
23495 overlap_hl, dummy_x, last_x);
23496 for (s = h; s; s = s->next)
23497 s->background_filled_p = 1;
23498 compute_overhangs_and_x (t, head->x, 1);
23499 prepend_glyph_string_lists (&head, &tail, h, t);
23500 }
23501
23502 /* Append glyphs strings for glyphs following the last glyph
23503 string tail that are overwritten by tail. The background of
23504 these strings has to be drawn because tail's foreground draws
23505 over it. */
23506 i = right_overwritten (tail);
23507 if (i >= 0)
23508 {
23509 enum draw_glyphs_face overlap_hl;
23510
23511 if (check_mouse_face
23512 && mouse_beg_col < i && mouse_end_col > end)
23513 overlap_hl = DRAW_MOUSE_FACE;
23514 else
23515 overlap_hl = DRAW_NORMAL_TEXT;
23516
23517 BUILD_GLYPH_STRINGS (end, i, h, t,
23518 overlap_hl, x, last_x);
23519 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23520 we don't have `end = i;' here. */
23521 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23522 append_glyph_string_lists (&head, &tail, h, t);
23523 clip_tail = tail;
23524 }
23525
23526 /* Append glyph strings for glyphs following the last glyph
23527 string tail that overwrite tail. The foreground of such
23528 glyphs has to be drawn because it writes into the background
23529 of tail. The background must not be drawn because it could
23530 paint over the foreground of following glyphs. */
23531 i = right_overwriting (tail);
23532 if (i >= 0)
23533 {
23534 enum draw_glyphs_face overlap_hl;
23535 if (check_mouse_face
23536 && mouse_beg_col < i && mouse_end_col > end)
23537 overlap_hl = DRAW_MOUSE_FACE;
23538 else
23539 overlap_hl = DRAW_NORMAL_TEXT;
23540
23541 clip_tail = tail;
23542 i++; /* We must include the Ith glyph. */
23543 BUILD_GLYPH_STRINGS (end, i, h, t,
23544 overlap_hl, x, last_x);
23545 for (s = h; s; s = s->next)
23546 s->background_filled_p = 1;
23547 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23548 append_glyph_string_lists (&head, &tail, h, t);
23549 }
23550 if (clip_head || clip_tail)
23551 for (s = head; s; s = s->next)
23552 {
23553 s->clip_head = clip_head;
23554 s->clip_tail = clip_tail;
23555 }
23556 }
23557
23558 /* Draw all strings. */
23559 for (s = head; s; s = s->next)
23560 FRAME_RIF (f)->draw_glyph_string (s);
23561
23562 #ifndef HAVE_NS
23563 /* When focus a sole frame and move horizontally, this sets on_p to 0
23564 causing a failure to erase prev cursor position. */
23565 if (area == TEXT_AREA
23566 && !row->full_width_p
23567 /* When drawing overlapping rows, only the glyph strings'
23568 foreground is drawn, which doesn't erase a cursor
23569 completely. */
23570 && !overlaps)
23571 {
23572 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23573 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23574 : (tail ? tail->x + tail->background_width : x));
23575 x0 -= area_left;
23576 x1 -= area_left;
23577
23578 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23579 row->y, MATRIX_ROW_BOTTOM_Y (row));
23580 }
23581 #endif
23582
23583 /* Value is the x-position up to which drawn, relative to AREA of W.
23584 This doesn't include parts drawn because of overhangs. */
23585 if (row->full_width_p)
23586 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23587 else
23588 x_reached -= area_left;
23589
23590 RELEASE_HDC (hdc, f);
23591
23592 return x_reached;
23593 }
23594
23595 /* Expand row matrix if too narrow. Don't expand if area
23596 is not present. */
23597
23598 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23599 { \
23600 if (!fonts_changed_p \
23601 && (it->glyph_row->glyphs[area] \
23602 < it->glyph_row->glyphs[area + 1])) \
23603 { \
23604 it->w->ncols_scale_factor++; \
23605 fonts_changed_p = 1; \
23606 } \
23607 }
23608
23609 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23610 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23611
23612 static inline void
23613 append_glyph (struct it *it)
23614 {
23615 struct glyph *glyph;
23616 enum glyph_row_area area = it->area;
23617
23618 eassert (it->glyph_row);
23619 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23620
23621 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23622 if (glyph < it->glyph_row->glyphs[area + 1])
23623 {
23624 /* If the glyph row is reversed, we need to prepend the glyph
23625 rather than append it. */
23626 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23627 {
23628 struct glyph *g;
23629
23630 /* Make room for the additional glyph. */
23631 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23632 g[1] = *g;
23633 glyph = it->glyph_row->glyphs[area];
23634 }
23635 glyph->charpos = CHARPOS (it->position);
23636 glyph->object = it->object;
23637 if (it->pixel_width > 0)
23638 {
23639 glyph->pixel_width = it->pixel_width;
23640 glyph->padding_p = 0;
23641 }
23642 else
23643 {
23644 /* Assure at least 1-pixel width. Otherwise, cursor can't
23645 be displayed correctly. */
23646 glyph->pixel_width = 1;
23647 glyph->padding_p = 1;
23648 }
23649 glyph->ascent = it->ascent;
23650 glyph->descent = it->descent;
23651 glyph->voffset = it->voffset;
23652 glyph->type = CHAR_GLYPH;
23653 glyph->avoid_cursor_p = it->avoid_cursor_p;
23654 glyph->multibyte_p = it->multibyte_p;
23655 glyph->left_box_line_p = it->start_of_box_run_p;
23656 glyph->right_box_line_p = it->end_of_box_run_p;
23657 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23658 || it->phys_descent > it->descent);
23659 glyph->glyph_not_available_p = it->glyph_not_available_p;
23660 glyph->face_id = it->face_id;
23661 glyph->u.ch = it->char_to_display;
23662 glyph->slice.img = null_glyph_slice;
23663 glyph->font_type = FONT_TYPE_UNKNOWN;
23664 if (it->bidi_p)
23665 {
23666 glyph->resolved_level = it->bidi_it.resolved_level;
23667 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23668 abort ();
23669 glyph->bidi_type = it->bidi_it.type;
23670 }
23671 else
23672 {
23673 glyph->resolved_level = 0;
23674 glyph->bidi_type = UNKNOWN_BT;
23675 }
23676 ++it->glyph_row->used[area];
23677 }
23678 else
23679 IT_EXPAND_MATRIX_WIDTH (it, area);
23680 }
23681
23682 /* Store one glyph for the composition IT->cmp_it.id in
23683 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23684 non-null. */
23685
23686 static inline void
23687 append_composite_glyph (struct it *it)
23688 {
23689 struct glyph *glyph;
23690 enum glyph_row_area area = it->area;
23691
23692 eassert (it->glyph_row);
23693
23694 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23695 if (glyph < it->glyph_row->glyphs[area + 1])
23696 {
23697 /* If the glyph row is reversed, we need to prepend the glyph
23698 rather than append it. */
23699 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23700 {
23701 struct glyph *g;
23702
23703 /* Make room for the new glyph. */
23704 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23705 g[1] = *g;
23706 glyph = it->glyph_row->glyphs[it->area];
23707 }
23708 glyph->charpos = it->cmp_it.charpos;
23709 glyph->object = it->object;
23710 glyph->pixel_width = it->pixel_width;
23711 glyph->ascent = it->ascent;
23712 glyph->descent = it->descent;
23713 glyph->voffset = it->voffset;
23714 glyph->type = COMPOSITE_GLYPH;
23715 if (it->cmp_it.ch < 0)
23716 {
23717 glyph->u.cmp.automatic = 0;
23718 glyph->u.cmp.id = it->cmp_it.id;
23719 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23720 }
23721 else
23722 {
23723 glyph->u.cmp.automatic = 1;
23724 glyph->u.cmp.id = it->cmp_it.id;
23725 glyph->slice.cmp.from = it->cmp_it.from;
23726 glyph->slice.cmp.to = it->cmp_it.to - 1;
23727 }
23728 glyph->avoid_cursor_p = it->avoid_cursor_p;
23729 glyph->multibyte_p = it->multibyte_p;
23730 glyph->left_box_line_p = it->start_of_box_run_p;
23731 glyph->right_box_line_p = it->end_of_box_run_p;
23732 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23733 || it->phys_descent > it->descent);
23734 glyph->padding_p = 0;
23735 glyph->glyph_not_available_p = 0;
23736 glyph->face_id = it->face_id;
23737 glyph->font_type = FONT_TYPE_UNKNOWN;
23738 if (it->bidi_p)
23739 {
23740 glyph->resolved_level = it->bidi_it.resolved_level;
23741 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23742 abort ();
23743 glyph->bidi_type = it->bidi_it.type;
23744 }
23745 ++it->glyph_row->used[area];
23746 }
23747 else
23748 IT_EXPAND_MATRIX_WIDTH (it, area);
23749 }
23750
23751
23752 /* Change IT->ascent and IT->height according to the setting of
23753 IT->voffset. */
23754
23755 static inline void
23756 take_vertical_position_into_account (struct it *it)
23757 {
23758 if (it->voffset)
23759 {
23760 if (it->voffset < 0)
23761 /* Increase the ascent so that we can display the text higher
23762 in the line. */
23763 it->ascent -= it->voffset;
23764 else
23765 /* Increase the descent so that we can display the text lower
23766 in the line. */
23767 it->descent += it->voffset;
23768 }
23769 }
23770
23771
23772 /* Produce glyphs/get display metrics for the image IT is loaded with.
23773 See the description of struct display_iterator in dispextern.h for
23774 an overview of struct display_iterator. */
23775
23776 static void
23777 produce_image_glyph (struct it *it)
23778 {
23779 struct image *img;
23780 struct face *face;
23781 int glyph_ascent, crop;
23782 struct glyph_slice slice;
23783
23784 eassert (it->what == IT_IMAGE);
23785
23786 face = FACE_FROM_ID (it->f, it->face_id);
23787 eassert (face);
23788 /* Make sure X resources of the face is loaded. */
23789 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23790
23791 if (it->image_id < 0)
23792 {
23793 /* Fringe bitmap. */
23794 it->ascent = it->phys_ascent = 0;
23795 it->descent = it->phys_descent = 0;
23796 it->pixel_width = 0;
23797 it->nglyphs = 0;
23798 return;
23799 }
23800
23801 img = IMAGE_FROM_ID (it->f, it->image_id);
23802 eassert (img);
23803 /* Make sure X resources of the image is loaded. */
23804 prepare_image_for_display (it->f, img);
23805
23806 slice.x = slice.y = 0;
23807 slice.width = img->width;
23808 slice.height = img->height;
23809
23810 if (INTEGERP (it->slice.x))
23811 slice.x = XINT (it->slice.x);
23812 else if (FLOATP (it->slice.x))
23813 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23814
23815 if (INTEGERP (it->slice.y))
23816 slice.y = XINT (it->slice.y);
23817 else if (FLOATP (it->slice.y))
23818 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23819
23820 if (INTEGERP (it->slice.width))
23821 slice.width = XINT (it->slice.width);
23822 else if (FLOATP (it->slice.width))
23823 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23824
23825 if (INTEGERP (it->slice.height))
23826 slice.height = XINT (it->slice.height);
23827 else if (FLOATP (it->slice.height))
23828 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23829
23830 if (slice.x >= img->width)
23831 slice.x = img->width;
23832 if (slice.y >= img->height)
23833 slice.y = img->height;
23834 if (slice.x + slice.width >= img->width)
23835 slice.width = img->width - slice.x;
23836 if (slice.y + slice.height > img->height)
23837 slice.height = img->height - slice.y;
23838
23839 if (slice.width == 0 || slice.height == 0)
23840 return;
23841
23842 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23843
23844 it->descent = slice.height - glyph_ascent;
23845 if (slice.y == 0)
23846 it->descent += img->vmargin;
23847 if (slice.y + slice.height == img->height)
23848 it->descent += img->vmargin;
23849 it->phys_descent = it->descent;
23850
23851 it->pixel_width = slice.width;
23852 if (slice.x == 0)
23853 it->pixel_width += img->hmargin;
23854 if (slice.x + slice.width == img->width)
23855 it->pixel_width += img->hmargin;
23856
23857 /* It's quite possible for images to have an ascent greater than
23858 their height, so don't get confused in that case. */
23859 if (it->descent < 0)
23860 it->descent = 0;
23861
23862 it->nglyphs = 1;
23863
23864 if (face->box != FACE_NO_BOX)
23865 {
23866 if (face->box_line_width > 0)
23867 {
23868 if (slice.y == 0)
23869 it->ascent += face->box_line_width;
23870 if (slice.y + slice.height == img->height)
23871 it->descent += face->box_line_width;
23872 }
23873
23874 if (it->start_of_box_run_p && slice.x == 0)
23875 it->pixel_width += eabs (face->box_line_width);
23876 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23877 it->pixel_width += eabs (face->box_line_width);
23878 }
23879
23880 take_vertical_position_into_account (it);
23881
23882 /* Automatically crop wide image glyphs at right edge so we can
23883 draw the cursor on same display row. */
23884 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23885 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23886 {
23887 it->pixel_width -= crop;
23888 slice.width -= crop;
23889 }
23890
23891 if (it->glyph_row)
23892 {
23893 struct glyph *glyph;
23894 enum glyph_row_area area = it->area;
23895
23896 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23897 if (glyph < it->glyph_row->glyphs[area + 1])
23898 {
23899 glyph->charpos = CHARPOS (it->position);
23900 glyph->object = it->object;
23901 glyph->pixel_width = it->pixel_width;
23902 glyph->ascent = glyph_ascent;
23903 glyph->descent = it->descent;
23904 glyph->voffset = it->voffset;
23905 glyph->type = IMAGE_GLYPH;
23906 glyph->avoid_cursor_p = it->avoid_cursor_p;
23907 glyph->multibyte_p = it->multibyte_p;
23908 glyph->left_box_line_p = it->start_of_box_run_p;
23909 glyph->right_box_line_p = it->end_of_box_run_p;
23910 glyph->overlaps_vertically_p = 0;
23911 glyph->padding_p = 0;
23912 glyph->glyph_not_available_p = 0;
23913 glyph->face_id = it->face_id;
23914 glyph->u.img_id = img->id;
23915 glyph->slice.img = slice;
23916 glyph->font_type = FONT_TYPE_UNKNOWN;
23917 if (it->bidi_p)
23918 {
23919 glyph->resolved_level = it->bidi_it.resolved_level;
23920 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23921 abort ();
23922 glyph->bidi_type = it->bidi_it.type;
23923 }
23924 ++it->glyph_row->used[area];
23925 }
23926 else
23927 IT_EXPAND_MATRIX_WIDTH (it, area);
23928 }
23929 }
23930
23931
23932 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23933 of the glyph, WIDTH and HEIGHT are the width and height of the
23934 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23935
23936 static void
23937 append_stretch_glyph (struct it *it, Lisp_Object object,
23938 int width, int height, int ascent)
23939 {
23940 struct glyph *glyph;
23941 enum glyph_row_area area = it->area;
23942
23943 eassert (ascent >= 0 && ascent <= height);
23944
23945 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23946 if (glyph < it->glyph_row->glyphs[area + 1])
23947 {
23948 /* If the glyph row is reversed, we need to prepend the glyph
23949 rather than append it. */
23950 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23951 {
23952 struct glyph *g;
23953
23954 /* Make room for the additional glyph. */
23955 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23956 g[1] = *g;
23957 glyph = it->glyph_row->glyphs[area];
23958 }
23959 glyph->charpos = CHARPOS (it->position);
23960 glyph->object = object;
23961 glyph->pixel_width = width;
23962 glyph->ascent = ascent;
23963 glyph->descent = height - ascent;
23964 glyph->voffset = it->voffset;
23965 glyph->type = STRETCH_GLYPH;
23966 glyph->avoid_cursor_p = it->avoid_cursor_p;
23967 glyph->multibyte_p = it->multibyte_p;
23968 glyph->left_box_line_p = it->start_of_box_run_p;
23969 glyph->right_box_line_p = it->end_of_box_run_p;
23970 glyph->overlaps_vertically_p = 0;
23971 glyph->padding_p = 0;
23972 glyph->glyph_not_available_p = 0;
23973 glyph->face_id = it->face_id;
23974 glyph->u.stretch.ascent = ascent;
23975 glyph->u.stretch.height = height;
23976 glyph->slice.img = null_glyph_slice;
23977 glyph->font_type = FONT_TYPE_UNKNOWN;
23978 if (it->bidi_p)
23979 {
23980 glyph->resolved_level = it->bidi_it.resolved_level;
23981 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23982 abort ();
23983 glyph->bidi_type = it->bidi_it.type;
23984 }
23985 else
23986 {
23987 glyph->resolved_level = 0;
23988 glyph->bidi_type = UNKNOWN_BT;
23989 }
23990 ++it->glyph_row->used[area];
23991 }
23992 else
23993 IT_EXPAND_MATRIX_WIDTH (it, area);
23994 }
23995
23996 #endif /* HAVE_WINDOW_SYSTEM */
23997
23998 /* Produce a stretch glyph for iterator IT. IT->object is the value
23999 of the glyph property displayed. The value must be a list
24000 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24001 being recognized:
24002
24003 1. `:width WIDTH' specifies that the space should be WIDTH *
24004 canonical char width wide. WIDTH may be an integer or floating
24005 point number.
24006
24007 2. `:relative-width FACTOR' specifies that the width of the stretch
24008 should be computed from the width of the first character having the
24009 `glyph' property, and should be FACTOR times that width.
24010
24011 3. `:align-to HPOS' specifies that the space should be wide enough
24012 to reach HPOS, a value in canonical character units.
24013
24014 Exactly one of the above pairs must be present.
24015
24016 4. `:height HEIGHT' specifies that the height of the stretch produced
24017 should be HEIGHT, measured in canonical character units.
24018
24019 5. `:relative-height FACTOR' specifies that the height of the
24020 stretch should be FACTOR times the height of the characters having
24021 the glyph property.
24022
24023 Either none or exactly one of 4 or 5 must be present.
24024
24025 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24026 of the stretch should be used for the ascent of the stretch.
24027 ASCENT must be in the range 0 <= ASCENT <= 100. */
24028
24029 void
24030 produce_stretch_glyph (struct it *it)
24031 {
24032 /* (space :width WIDTH :height HEIGHT ...) */
24033 Lisp_Object prop, plist;
24034 int width = 0, height = 0, align_to = -1;
24035 int zero_width_ok_p = 0;
24036 int ascent = 0;
24037 double tem;
24038 struct face *face = NULL;
24039 struct font *font = NULL;
24040
24041 #ifdef HAVE_WINDOW_SYSTEM
24042 int zero_height_ok_p = 0;
24043
24044 if (FRAME_WINDOW_P (it->f))
24045 {
24046 face = FACE_FROM_ID (it->f, it->face_id);
24047 font = face->font ? face->font : FRAME_FONT (it->f);
24048 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24049 }
24050 #endif
24051
24052 /* List should start with `space'. */
24053 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24054 plist = XCDR (it->object);
24055
24056 /* Compute the width of the stretch. */
24057 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24058 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24059 {
24060 /* Absolute width `:width WIDTH' specified and valid. */
24061 zero_width_ok_p = 1;
24062 width = (int)tem;
24063 }
24064 #ifdef HAVE_WINDOW_SYSTEM
24065 else if (FRAME_WINDOW_P (it->f)
24066 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24067 {
24068 /* Relative width `:relative-width FACTOR' specified and valid.
24069 Compute the width of the characters having the `glyph'
24070 property. */
24071 struct it it2;
24072 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24073
24074 it2 = *it;
24075 if (it->multibyte_p)
24076 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24077 else
24078 {
24079 it2.c = it2.char_to_display = *p, it2.len = 1;
24080 if (! ASCII_CHAR_P (it2.c))
24081 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24082 }
24083
24084 it2.glyph_row = NULL;
24085 it2.what = IT_CHARACTER;
24086 x_produce_glyphs (&it2);
24087 width = NUMVAL (prop) * it2.pixel_width;
24088 }
24089 #endif /* HAVE_WINDOW_SYSTEM */
24090 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24091 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24092 {
24093 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24094 align_to = (align_to < 0
24095 ? 0
24096 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24097 else if (align_to < 0)
24098 align_to = window_box_left_offset (it->w, TEXT_AREA);
24099 width = max (0, (int)tem + align_to - it->current_x);
24100 zero_width_ok_p = 1;
24101 }
24102 else
24103 /* Nothing specified -> width defaults to canonical char width. */
24104 width = FRAME_COLUMN_WIDTH (it->f);
24105
24106 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24107 width = 1;
24108
24109 #ifdef HAVE_WINDOW_SYSTEM
24110 /* Compute height. */
24111 if (FRAME_WINDOW_P (it->f))
24112 {
24113 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24114 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24115 {
24116 height = (int)tem;
24117 zero_height_ok_p = 1;
24118 }
24119 else if (prop = Fplist_get (plist, QCrelative_height),
24120 NUMVAL (prop) > 0)
24121 height = FONT_HEIGHT (font) * NUMVAL (prop);
24122 else
24123 height = FONT_HEIGHT (font);
24124
24125 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24126 height = 1;
24127
24128 /* Compute percentage of height used for ascent. If
24129 `:ascent ASCENT' is present and valid, use that. Otherwise,
24130 derive the ascent from the font in use. */
24131 if (prop = Fplist_get (plist, QCascent),
24132 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24133 ascent = height * NUMVAL (prop) / 100.0;
24134 else if (!NILP (prop)
24135 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24136 ascent = min (max (0, (int)tem), height);
24137 else
24138 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24139 }
24140 else
24141 #endif /* HAVE_WINDOW_SYSTEM */
24142 height = 1;
24143
24144 if (width > 0 && it->line_wrap != TRUNCATE
24145 && it->current_x + width > it->last_visible_x)
24146 {
24147 width = it->last_visible_x - it->current_x;
24148 #ifdef HAVE_WINDOW_SYSTEM
24149 /* Subtract one more pixel from the stretch width, but only on
24150 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24151 width -= FRAME_WINDOW_P (it->f);
24152 #endif
24153 }
24154
24155 if (width > 0 && height > 0 && it->glyph_row)
24156 {
24157 Lisp_Object o_object = it->object;
24158 Lisp_Object object = it->stack[it->sp - 1].string;
24159 int n = width;
24160
24161 if (!STRINGP (object))
24162 object = it->w->buffer;
24163 #ifdef HAVE_WINDOW_SYSTEM
24164 if (FRAME_WINDOW_P (it->f))
24165 append_stretch_glyph (it, object, width, height, ascent);
24166 else
24167 #endif
24168 {
24169 it->object = object;
24170 it->char_to_display = ' ';
24171 it->pixel_width = it->len = 1;
24172 while (n--)
24173 tty_append_glyph (it);
24174 it->object = o_object;
24175 }
24176 }
24177
24178 it->pixel_width = width;
24179 #ifdef HAVE_WINDOW_SYSTEM
24180 if (FRAME_WINDOW_P (it->f))
24181 {
24182 it->ascent = it->phys_ascent = ascent;
24183 it->descent = it->phys_descent = height - it->ascent;
24184 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24185 take_vertical_position_into_account (it);
24186 }
24187 else
24188 #endif
24189 it->nglyphs = width;
24190 }
24191
24192 /* Get information about special display element WHAT in an
24193 environment described by IT. WHAT is one of IT_TRUNCATION or
24194 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24195 non-null glyph_row member. This function ensures that fields like
24196 face_id, c, len of IT are left untouched. */
24197
24198 static void
24199 produce_special_glyphs (struct it *it, enum display_element_type what)
24200 {
24201 struct it temp_it;
24202 Lisp_Object gc;
24203 GLYPH glyph;
24204
24205 temp_it = *it;
24206 temp_it.object = make_number (0);
24207 memset (&temp_it.current, 0, sizeof temp_it.current);
24208
24209 if (what == IT_CONTINUATION)
24210 {
24211 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24212 if (it->bidi_it.paragraph_dir == R2L)
24213 SET_GLYPH_FROM_CHAR (glyph, '/');
24214 else
24215 SET_GLYPH_FROM_CHAR (glyph, '\\');
24216 if (it->dp
24217 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24218 {
24219 /* FIXME: Should we mirror GC for R2L lines? */
24220 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24221 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24222 }
24223 }
24224 else if (what == IT_TRUNCATION)
24225 {
24226 /* Truncation glyph. */
24227 SET_GLYPH_FROM_CHAR (glyph, '$');
24228 if (it->dp
24229 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24230 {
24231 /* FIXME: Should we mirror GC for R2L lines? */
24232 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24233 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24234 }
24235 }
24236 else
24237 abort ();
24238
24239 #ifdef HAVE_WINDOW_SYSTEM
24240 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24241 is turned off, we precede the truncation/continuation glyphs by a
24242 stretch glyph whose width is computed such that these special
24243 glyphs are aligned at the window margin, even when very different
24244 fonts are used in different glyph rows. */
24245 if (FRAME_WINDOW_P (temp_it.f)
24246 /* init_iterator calls this with it->glyph_row == NULL, and it
24247 wants only the pixel width of the truncation/continuation
24248 glyphs. */
24249 && temp_it.glyph_row
24250 /* insert_left_trunc_glyphs calls us at the beginning of the
24251 row, and it has its own calculation of the stretch glyph
24252 width. */
24253 && temp_it.glyph_row->used[TEXT_AREA] > 0
24254 && (temp_it.glyph_row->reversed_p
24255 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24256 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24257 {
24258 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24259
24260 if (stretch_width > 0)
24261 {
24262 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24263 struct font *font =
24264 face->font ? face->font : FRAME_FONT (temp_it.f);
24265 int stretch_ascent =
24266 (((temp_it.ascent + temp_it.descent)
24267 * FONT_BASE (font)) / FONT_HEIGHT (font));
24268
24269 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24270 temp_it.ascent + temp_it.descent,
24271 stretch_ascent);
24272 }
24273 }
24274 #endif
24275
24276 temp_it.dp = NULL;
24277 temp_it.what = IT_CHARACTER;
24278 temp_it.len = 1;
24279 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24280 temp_it.face_id = GLYPH_FACE (glyph);
24281 temp_it.len = CHAR_BYTES (temp_it.c);
24282
24283 PRODUCE_GLYPHS (&temp_it);
24284 it->pixel_width = temp_it.pixel_width;
24285 it->nglyphs = temp_it.pixel_width;
24286 }
24287
24288 #ifdef HAVE_WINDOW_SYSTEM
24289
24290 /* Calculate line-height and line-spacing properties.
24291 An integer value specifies explicit pixel value.
24292 A float value specifies relative value to current face height.
24293 A cons (float . face-name) specifies relative value to
24294 height of specified face font.
24295
24296 Returns height in pixels, or nil. */
24297
24298
24299 static Lisp_Object
24300 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24301 int boff, int override)
24302 {
24303 Lisp_Object face_name = Qnil;
24304 int ascent, descent, height;
24305
24306 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24307 return val;
24308
24309 if (CONSP (val))
24310 {
24311 face_name = XCAR (val);
24312 val = XCDR (val);
24313 if (!NUMBERP (val))
24314 val = make_number (1);
24315 if (NILP (face_name))
24316 {
24317 height = it->ascent + it->descent;
24318 goto scale;
24319 }
24320 }
24321
24322 if (NILP (face_name))
24323 {
24324 font = FRAME_FONT (it->f);
24325 boff = FRAME_BASELINE_OFFSET (it->f);
24326 }
24327 else if (EQ (face_name, Qt))
24328 {
24329 override = 0;
24330 }
24331 else
24332 {
24333 int face_id;
24334 struct face *face;
24335
24336 face_id = lookup_named_face (it->f, face_name, 0);
24337 if (face_id < 0)
24338 return make_number (-1);
24339
24340 face = FACE_FROM_ID (it->f, face_id);
24341 font = face->font;
24342 if (font == NULL)
24343 return make_number (-1);
24344 boff = font->baseline_offset;
24345 if (font->vertical_centering)
24346 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24347 }
24348
24349 ascent = FONT_BASE (font) + boff;
24350 descent = FONT_DESCENT (font) - boff;
24351
24352 if (override)
24353 {
24354 it->override_ascent = ascent;
24355 it->override_descent = descent;
24356 it->override_boff = boff;
24357 }
24358
24359 height = ascent + descent;
24360
24361 scale:
24362 if (FLOATP (val))
24363 height = (int)(XFLOAT_DATA (val) * height);
24364 else if (INTEGERP (val))
24365 height *= XINT (val);
24366
24367 return make_number (height);
24368 }
24369
24370
24371 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24372 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24373 and only if this is for a character for which no font was found.
24374
24375 If the display method (it->glyphless_method) is
24376 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24377 length of the acronym or the hexadecimal string, UPPER_XOFF and
24378 UPPER_YOFF are pixel offsets for the upper part of the string,
24379 LOWER_XOFF and LOWER_YOFF are for the lower part.
24380
24381 For the other display methods, LEN through LOWER_YOFF are zero. */
24382
24383 static void
24384 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24385 short upper_xoff, short upper_yoff,
24386 short lower_xoff, short lower_yoff)
24387 {
24388 struct glyph *glyph;
24389 enum glyph_row_area area = it->area;
24390
24391 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24392 if (glyph < it->glyph_row->glyphs[area + 1])
24393 {
24394 /* If the glyph row is reversed, we need to prepend the glyph
24395 rather than append it. */
24396 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24397 {
24398 struct glyph *g;
24399
24400 /* Make room for the additional glyph. */
24401 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24402 g[1] = *g;
24403 glyph = it->glyph_row->glyphs[area];
24404 }
24405 glyph->charpos = CHARPOS (it->position);
24406 glyph->object = it->object;
24407 glyph->pixel_width = it->pixel_width;
24408 glyph->ascent = it->ascent;
24409 glyph->descent = it->descent;
24410 glyph->voffset = it->voffset;
24411 glyph->type = GLYPHLESS_GLYPH;
24412 glyph->u.glyphless.method = it->glyphless_method;
24413 glyph->u.glyphless.for_no_font = for_no_font;
24414 glyph->u.glyphless.len = len;
24415 glyph->u.glyphless.ch = it->c;
24416 glyph->slice.glyphless.upper_xoff = upper_xoff;
24417 glyph->slice.glyphless.upper_yoff = upper_yoff;
24418 glyph->slice.glyphless.lower_xoff = lower_xoff;
24419 glyph->slice.glyphless.lower_yoff = lower_yoff;
24420 glyph->avoid_cursor_p = it->avoid_cursor_p;
24421 glyph->multibyte_p = it->multibyte_p;
24422 glyph->left_box_line_p = it->start_of_box_run_p;
24423 glyph->right_box_line_p = it->end_of_box_run_p;
24424 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24425 || it->phys_descent > it->descent);
24426 glyph->padding_p = 0;
24427 glyph->glyph_not_available_p = 0;
24428 glyph->face_id = face_id;
24429 glyph->font_type = FONT_TYPE_UNKNOWN;
24430 if (it->bidi_p)
24431 {
24432 glyph->resolved_level = it->bidi_it.resolved_level;
24433 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24434 abort ();
24435 glyph->bidi_type = it->bidi_it.type;
24436 }
24437 ++it->glyph_row->used[area];
24438 }
24439 else
24440 IT_EXPAND_MATRIX_WIDTH (it, area);
24441 }
24442
24443
24444 /* Produce a glyph for a glyphless character for iterator IT.
24445 IT->glyphless_method specifies which method to use for displaying
24446 the character. See the description of enum
24447 glyphless_display_method in dispextern.h for the detail.
24448
24449 FOR_NO_FONT is nonzero if and only if this is for a character for
24450 which no font was found. ACRONYM, if non-nil, is an acronym string
24451 for the character. */
24452
24453 static void
24454 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24455 {
24456 int face_id;
24457 struct face *face;
24458 struct font *font;
24459 int base_width, base_height, width, height;
24460 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24461 int len;
24462
24463 /* Get the metrics of the base font. We always refer to the current
24464 ASCII face. */
24465 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24466 font = face->font ? face->font : FRAME_FONT (it->f);
24467 it->ascent = FONT_BASE (font) + font->baseline_offset;
24468 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24469 base_height = it->ascent + it->descent;
24470 base_width = font->average_width;
24471
24472 /* Get a face ID for the glyph by utilizing a cache (the same way as
24473 done for `escape-glyph' in get_next_display_element). */
24474 if (it->f == last_glyphless_glyph_frame
24475 && it->face_id == last_glyphless_glyph_face_id)
24476 {
24477 face_id = last_glyphless_glyph_merged_face_id;
24478 }
24479 else
24480 {
24481 /* Merge the `glyphless-char' face into the current face. */
24482 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24483 last_glyphless_glyph_frame = it->f;
24484 last_glyphless_glyph_face_id = it->face_id;
24485 last_glyphless_glyph_merged_face_id = face_id;
24486 }
24487
24488 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24489 {
24490 it->pixel_width = THIN_SPACE_WIDTH;
24491 len = 0;
24492 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24493 }
24494 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24495 {
24496 width = CHAR_WIDTH (it->c);
24497 if (width == 0)
24498 width = 1;
24499 else if (width > 4)
24500 width = 4;
24501 it->pixel_width = base_width * width;
24502 len = 0;
24503 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24504 }
24505 else
24506 {
24507 char buf[7];
24508 const char *str;
24509 unsigned int code[6];
24510 int upper_len;
24511 int ascent, descent;
24512 struct font_metrics metrics_upper, metrics_lower;
24513
24514 face = FACE_FROM_ID (it->f, face_id);
24515 font = face->font ? face->font : FRAME_FONT (it->f);
24516 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24517
24518 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24519 {
24520 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24521 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24522 if (CONSP (acronym))
24523 acronym = XCAR (acronym);
24524 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24525 }
24526 else
24527 {
24528 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24529 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24530 str = buf;
24531 }
24532 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24533 code[len] = font->driver->encode_char (font, str[len]);
24534 upper_len = (len + 1) / 2;
24535 font->driver->text_extents (font, code, upper_len,
24536 &metrics_upper);
24537 font->driver->text_extents (font, code + upper_len, len - upper_len,
24538 &metrics_lower);
24539
24540
24541
24542 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24543 width = max (metrics_upper.width, metrics_lower.width) + 4;
24544 upper_xoff = upper_yoff = 2; /* the typical case */
24545 if (base_width >= width)
24546 {
24547 /* Align the upper to the left, the lower to the right. */
24548 it->pixel_width = base_width;
24549 lower_xoff = base_width - 2 - metrics_lower.width;
24550 }
24551 else
24552 {
24553 /* Center the shorter one. */
24554 it->pixel_width = width;
24555 if (metrics_upper.width >= metrics_lower.width)
24556 lower_xoff = (width - metrics_lower.width) / 2;
24557 else
24558 {
24559 /* FIXME: This code doesn't look right. It formerly was
24560 missing the "lower_xoff = 0;", which couldn't have
24561 been right since it left lower_xoff uninitialized. */
24562 lower_xoff = 0;
24563 upper_xoff = (width - metrics_upper.width) / 2;
24564 }
24565 }
24566
24567 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24568 top, bottom, and between upper and lower strings. */
24569 height = (metrics_upper.ascent + metrics_upper.descent
24570 + metrics_lower.ascent + metrics_lower.descent) + 5;
24571 /* Center vertically.
24572 H:base_height, D:base_descent
24573 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24574
24575 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24576 descent = D - H/2 + h/2;
24577 lower_yoff = descent - 2 - ld;
24578 upper_yoff = lower_yoff - la - 1 - ud; */
24579 ascent = - (it->descent - (base_height + height + 1) / 2);
24580 descent = it->descent - (base_height - height) / 2;
24581 lower_yoff = descent - 2 - metrics_lower.descent;
24582 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24583 - metrics_upper.descent);
24584 /* Don't make the height shorter than the base height. */
24585 if (height > base_height)
24586 {
24587 it->ascent = ascent;
24588 it->descent = descent;
24589 }
24590 }
24591
24592 it->phys_ascent = it->ascent;
24593 it->phys_descent = it->descent;
24594 if (it->glyph_row)
24595 append_glyphless_glyph (it, face_id, for_no_font, len,
24596 upper_xoff, upper_yoff,
24597 lower_xoff, lower_yoff);
24598 it->nglyphs = 1;
24599 take_vertical_position_into_account (it);
24600 }
24601
24602
24603 /* RIF:
24604 Produce glyphs/get display metrics for the display element IT is
24605 loaded with. See the description of struct it in dispextern.h
24606 for an overview of struct it. */
24607
24608 void
24609 x_produce_glyphs (struct it *it)
24610 {
24611 int extra_line_spacing = it->extra_line_spacing;
24612
24613 it->glyph_not_available_p = 0;
24614
24615 if (it->what == IT_CHARACTER)
24616 {
24617 XChar2b char2b;
24618 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24619 struct font *font = face->font;
24620 struct font_metrics *pcm = NULL;
24621 int boff; /* baseline offset */
24622
24623 if (font == NULL)
24624 {
24625 /* When no suitable font is found, display this character by
24626 the method specified in the first extra slot of
24627 Vglyphless_char_display. */
24628 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24629
24630 eassert (it->what == IT_GLYPHLESS);
24631 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24632 goto done;
24633 }
24634
24635 boff = font->baseline_offset;
24636 if (font->vertical_centering)
24637 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24638
24639 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24640 {
24641 int stretched_p;
24642
24643 it->nglyphs = 1;
24644
24645 if (it->override_ascent >= 0)
24646 {
24647 it->ascent = it->override_ascent;
24648 it->descent = it->override_descent;
24649 boff = it->override_boff;
24650 }
24651 else
24652 {
24653 it->ascent = FONT_BASE (font) + boff;
24654 it->descent = FONT_DESCENT (font) - boff;
24655 }
24656
24657 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24658 {
24659 pcm = get_per_char_metric (font, &char2b);
24660 if (pcm->width == 0
24661 && pcm->rbearing == 0 && pcm->lbearing == 0)
24662 pcm = NULL;
24663 }
24664
24665 if (pcm)
24666 {
24667 it->phys_ascent = pcm->ascent + boff;
24668 it->phys_descent = pcm->descent - boff;
24669 it->pixel_width = pcm->width;
24670 }
24671 else
24672 {
24673 it->glyph_not_available_p = 1;
24674 it->phys_ascent = it->ascent;
24675 it->phys_descent = it->descent;
24676 it->pixel_width = font->space_width;
24677 }
24678
24679 if (it->constrain_row_ascent_descent_p)
24680 {
24681 if (it->descent > it->max_descent)
24682 {
24683 it->ascent += it->descent - it->max_descent;
24684 it->descent = it->max_descent;
24685 }
24686 if (it->ascent > it->max_ascent)
24687 {
24688 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24689 it->ascent = it->max_ascent;
24690 }
24691 it->phys_ascent = min (it->phys_ascent, it->ascent);
24692 it->phys_descent = min (it->phys_descent, it->descent);
24693 extra_line_spacing = 0;
24694 }
24695
24696 /* If this is a space inside a region of text with
24697 `space-width' property, change its width. */
24698 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24699 if (stretched_p)
24700 it->pixel_width *= XFLOATINT (it->space_width);
24701
24702 /* If face has a box, add the box thickness to the character
24703 height. If character has a box line to the left and/or
24704 right, add the box line width to the character's width. */
24705 if (face->box != FACE_NO_BOX)
24706 {
24707 int thick = face->box_line_width;
24708
24709 if (thick > 0)
24710 {
24711 it->ascent += thick;
24712 it->descent += thick;
24713 }
24714 else
24715 thick = -thick;
24716
24717 if (it->start_of_box_run_p)
24718 it->pixel_width += thick;
24719 if (it->end_of_box_run_p)
24720 it->pixel_width += thick;
24721 }
24722
24723 /* If face has an overline, add the height of the overline
24724 (1 pixel) and a 1 pixel margin to the character height. */
24725 if (face->overline_p)
24726 it->ascent += overline_margin;
24727
24728 if (it->constrain_row_ascent_descent_p)
24729 {
24730 if (it->ascent > it->max_ascent)
24731 it->ascent = it->max_ascent;
24732 if (it->descent > it->max_descent)
24733 it->descent = it->max_descent;
24734 }
24735
24736 take_vertical_position_into_account (it);
24737
24738 /* If we have to actually produce glyphs, do it. */
24739 if (it->glyph_row)
24740 {
24741 if (stretched_p)
24742 {
24743 /* Translate a space with a `space-width' property
24744 into a stretch glyph. */
24745 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24746 / FONT_HEIGHT (font));
24747 append_stretch_glyph (it, it->object, it->pixel_width,
24748 it->ascent + it->descent, ascent);
24749 }
24750 else
24751 append_glyph (it);
24752
24753 /* If characters with lbearing or rbearing are displayed
24754 in this line, record that fact in a flag of the
24755 glyph row. This is used to optimize X output code. */
24756 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24757 it->glyph_row->contains_overlapping_glyphs_p = 1;
24758 }
24759 if (! stretched_p && it->pixel_width == 0)
24760 /* We assure that all visible glyphs have at least 1-pixel
24761 width. */
24762 it->pixel_width = 1;
24763 }
24764 else if (it->char_to_display == '\n')
24765 {
24766 /* A newline has no width, but we need the height of the
24767 line. But if previous part of the line sets a height,
24768 don't increase that height */
24769
24770 Lisp_Object height;
24771 Lisp_Object total_height = Qnil;
24772
24773 it->override_ascent = -1;
24774 it->pixel_width = 0;
24775 it->nglyphs = 0;
24776
24777 height = get_it_property (it, Qline_height);
24778 /* Split (line-height total-height) list */
24779 if (CONSP (height)
24780 && CONSP (XCDR (height))
24781 && NILP (XCDR (XCDR (height))))
24782 {
24783 total_height = XCAR (XCDR (height));
24784 height = XCAR (height);
24785 }
24786 height = calc_line_height_property (it, height, font, boff, 1);
24787
24788 if (it->override_ascent >= 0)
24789 {
24790 it->ascent = it->override_ascent;
24791 it->descent = it->override_descent;
24792 boff = it->override_boff;
24793 }
24794 else
24795 {
24796 it->ascent = FONT_BASE (font) + boff;
24797 it->descent = FONT_DESCENT (font) - boff;
24798 }
24799
24800 if (EQ (height, Qt))
24801 {
24802 if (it->descent > it->max_descent)
24803 {
24804 it->ascent += it->descent - it->max_descent;
24805 it->descent = it->max_descent;
24806 }
24807 if (it->ascent > it->max_ascent)
24808 {
24809 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24810 it->ascent = it->max_ascent;
24811 }
24812 it->phys_ascent = min (it->phys_ascent, it->ascent);
24813 it->phys_descent = min (it->phys_descent, it->descent);
24814 it->constrain_row_ascent_descent_p = 1;
24815 extra_line_spacing = 0;
24816 }
24817 else
24818 {
24819 Lisp_Object spacing;
24820
24821 it->phys_ascent = it->ascent;
24822 it->phys_descent = it->descent;
24823
24824 if ((it->max_ascent > 0 || it->max_descent > 0)
24825 && face->box != FACE_NO_BOX
24826 && face->box_line_width > 0)
24827 {
24828 it->ascent += face->box_line_width;
24829 it->descent += face->box_line_width;
24830 }
24831 if (!NILP (height)
24832 && XINT (height) > it->ascent + it->descent)
24833 it->ascent = XINT (height) - it->descent;
24834
24835 if (!NILP (total_height))
24836 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24837 else
24838 {
24839 spacing = get_it_property (it, Qline_spacing);
24840 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24841 }
24842 if (INTEGERP (spacing))
24843 {
24844 extra_line_spacing = XINT (spacing);
24845 if (!NILP (total_height))
24846 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24847 }
24848 }
24849 }
24850 else /* i.e. (it->char_to_display == '\t') */
24851 {
24852 if (font->space_width > 0)
24853 {
24854 int tab_width = it->tab_width * font->space_width;
24855 int x = it->current_x + it->continuation_lines_width;
24856 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24857
24858 /* If the distance from the current position to the next tab
24859 stop is less than a space character width, use the
24860 tab stop after that. */
24861 if (next_tab_x - x < font->space_width)
24862 next_tab_x += tab_width;
24863
24864 it->pixel_width = next_tab_x - x;
24865 it->nglyphs = 1;
24866 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24867 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24868
24869 if (it->glyph_row)
24870 {
24871 append_stretch_glyph (it, it->object, it->pixel_width,
24872 it->ascent + it->descent, it->ascent);
24873 }
24874 }
24875 else
24876 {
24877 it->pixel_width = 0;
24878 it->nglyphs = 1;
24879 }
24880 }
24881 }
24882 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24883 {
24884 /* A static composition.
24885
24886 Note: A composition is represented as one glyph in the
24887 glyph matrix. There are no padding glyphs.
24888
24889 Important note: pixel_width, ascent, and descent are the
24890 values of what is drawn by draw_glyphs (i.e. the values of
24891 the overall glyphs composed). */
24892 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24893 int boff; /* baseline offset */
24894 struct composition *cmp = composition_table[it->cmp_it.id];
24895 int glyph_len = cmp->glyph_len;
24896 struct font *font = face->font;
24897
24898 it->nglyphs = 1;
24899
24900 /* If we have not yet calculated pixel size data of glyphs of
24901 the composition for the current face font, calculate them
24902 now. Theoretically, we have to check all fonts for the
24903 glyphs, but that requires much time and memory space. So,
24904 here we check only the font of the first glyph. This may
24905 lead to incorrect display, but it's very rare, and C-l
24906 (recenter-top-bottom) can correct the display anyway. */
24907 if (! cmp->font || cmp->font != font)
24908 {
24909 /* Ascent and descent of the font of the first character
24910 of this composition (adjusted by baseline offset).
24911 Ascent and descent of overall glyphs should not be less
24912 than these, respectively. */
24913 int font_ascent, font_descent, font_height;
24914 /* Bounding box of the overall glyphs. */
24915 int leftmost, rightmost, lowest, highest;
24916 int lbearing, rbearing;
24917 int i, width, ascent, descent;
24918 int left_padded = 0, right_padded = 0;
24919 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24920 XChar2b char2b;
24921 struct font_metrics *pcm;
24922 int font_not_found_p;
24923 ptrdiff_t pos;
24924
24925 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24926 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24927 break;
24928 if (glyph_len < cmp->glyph_len)
24929 right_padded = 1;
24930 for (i = 0; i < glyph_len; i++)
24931 {
24932 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24933 break;
24934 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24935 }
24936 if (i > 0)
24937 left_padded = 1;
24938
24939 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24940 : IT_CHARPOS (*it));
24941 /* If no suitable font is found, use the default font. */
24942 font_not_found_p = font == NULL;
24943 if (font_not_found_p)
24944 {
24945 face = face->ascii_face;
24946 font = face->font;
24947 }
24948 boff = font->baseline_offset;
24949 if (font->vertical_centering)
24950 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24951 font_ascent = FONT_BASE (font) + boff;
24952 font_descent = FONT_DESCENT (font) - boff;
24953 font_height = FONT_HEIGHT (font);
24954
24955 cmp->font = (void *) font;
24956
24957 pcm = NULL;
24958 if (! font_not_found_p)
24959 {
24960 get_char_face_and_encoding (it->f, c, it->face_id,
24961 &char2b, 0);
24962 pcm = get_per_char_metric (font, &char2b);
24963 }
24964
24965 /* Initialize the bounding box. */
24966 if (pcm)
24967 {
24968 width = cmp->glyph_len > 0 ? pcm->width : 0;
24969 ascent = pcm->ascent;
24970 descent = pcm->descent;
24971 lbearing = pcm->lbearing;
24972 rbearing = pcm->rbearing;
24973 }
24974 else
24975 {
24976 width = cmp->glyph_len > 0 ? font->space_width : 0;
24977 ascent = FONT_BASE (font);
24978 descent = FONT_DESCENT (font);
24979 lbearing = 0;
24980 rbearing = width;
24981 }
24982
24983 rightmost = width;
24984 leftmost = 0;
24985 lowest = - descent + boff;
24986 highest = ascent + boff;
24987
24988 if (! font_not_found_p
24989 && font->default_ascent
24990 && CHAR_TABLE_P (Vuse_default_ascent)
24991 && !NILP (Faref (Vuse_default_ascent,
24992 make_number (it->char_to_display))))
24993 highest = font->default_ascent + boff;
24994
24995 /* Draw the first glyph at the normal position. It may be
24996 shifted to right later if some other glyphs are drawn
24997 at the left. */
24998 cmp->offsets[i * 2] = 0;
24999 cmp->offsets[i * 2 + 1] = boff;
25000 cmp->lbearing = lbearing;
25001 cmp->rbearing = rbearing;
25002
25003 /* Set cmp->offsets for the remaining glyphs. */
25004 for (i++; i < glyph_len; i++)
25005 {
25006 int left, right, btm, top;
25007 int ch = COMPOSITION_GLYPH (cmp, i);
25008 int face_id;
25009 struct face *this_face;
25010
25011 if (ch == '\t')
25012 ch = ' ';
25013 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25014 this_face = FACE_FROM_ID (it->f, face_id);
25015 font = this_face->font;
25016
25017 if (font == NULL)
25018 pcm = NULL;
25019 else
25020 {
25021 get_char_face_and_encoding (it->f, ch, face_id,
25022 &char2b, 0);
25023 pcm = get_per_char_metric (font, &char2b);
25024 }
25025 if (! pcm)
25026 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25027 else
25028 {
25029 width = pcm->width;
25030 ascent = pcm->ascent;
25031 descent = pcm->descent;
25032 lbearing = pcm->lbearing;
25033 rbearing = pcm->rbearing;
25034 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25035 {
25036 /* Relative composition with or without
25037 alternate chars. */
25038 left = (leftmost + rightmost - width) / 2;
25039 btm = - descent + boff;
25040 if (font->relative_compose
25041 && (! CHAR_TABLE_P (Vignore_relative_composition)
25042 || NILP (Faref (Vignore_relative_composition,
25043 make_number (ch)))))
25044 {
25045
25046 if (- descent >= font->relative_compose)
25047 /* One extra pixel between two glyphs. */
25048 btm = highest + 1;
25049 else if (ascent <= 0)
25050 /* One extra pixel between two glyphs. */
25051 btm = lowest - 1 - ascent - descent;
25052 }
25053 }
25054 else
25055 {
25056 /* A composition rule is specified by an integer
25057 value that encodes global and new reference
25058 points (GREF and NREF). GREF and NREF are
25059 specified by numbers as below:
25060
25061 0---1---2 -- ascent
25062 | |
25063 | |
25064 | |
25065 9--10--11 -- center
25066 | |
25067 ---3---4---5--- baseline
25068 | |
25069 6---7---8 -- descent
25070 */
25071 int rule = COMPOSITION_RULE (cmp, i);
25072 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25073
25074 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25075 grefx = gref % 3, nrefx = nref % 3;
25076 grefy = gref / 3, nrefy = nref / 3;
25077 if (xoff)
25078 xoff = font_height * (xoff - 128) / 256;
25079 if (yoff)
25080 yoff = font_height * (yoff - 128) / 256;
25081
25082 left = (leftmost
25083 + grefx * (rightmost - leftmost) / 2
25084 - nrefx * width / 2
25085 + xoff);
25086
25087 btm = ((grefy == 0 ? highest
25088 : grefy == 1 ? 0
25089 : grefy == 2 ? lowest
25090 : (highest + lowest) / 2)
25091 - (nrefy == 0 ? ascent + descent
25092 : nrefy == 1 ? descent - boff
25093 : nrefy == 2 ? 0
25094 : (ascent + descent) / 2)
25095 + yoff);
25096 }
25097
25098 cmp->offsets[i * 2] = left;
25099 cmp->offsets[i * 2 + 1] = btm + descent;
25100
25101 /* Update the bounding box of the overall glyphs. */
25102 if (width > 0)
25103 {
25104 right = left + width;
25105 if (left < leftmost)
25106 leftmost = left;
25107 if (right > rightmost)
25108 rightmost = right;
25109 }
25110 top = btm + descent + ascent;
25111 if (top > highest)
25112 highest = top;
25113 if (btm < lowest)
25114 lowest = btm;
25115
25116 if (cmp->lbearing > left + lbearing)
25117 cmp->lbearing = left + lbearing;
25118 if (cmp->rbearing < left + rbearing)
25119 cmp->rbearing = left + rbearing;
25120 }
25121 }
25122
25123 /* If there are glyphs whose x-offsets are negative,
25124 shift all glyphs to the right and make all x-offsets
25125 non-negative. */
25126 if (leftmost < 0)
25127 {
25128 for (i = 0; i < cmp->glyph_len; i++)
25129 cmp->offsets[i * 2] -= leftmost;
25130 rightmost -= leftmost;
25131 cmp->lbearing -= leftmost;
25132 cmp->rbearing -= leftmost;
25133 }
25134
25135 if (left_padded && cmp->lbearing < 0)
25136 {
25137 for (i = 0; i < cmp->glyph_len; i++)
25138 cmp->offsets[i * 2] -= cmp->lbearing;
25139 rightmost -= cmp->lbearing;
25140 cmp->rbearing -= cmp->lbearing;
25141 cmp->lbearing = 0;
25142 }
25143 if (right_padded && rightmost < cmp->rbearing)
25144 {
25145 rightmost = cmp->rbearing;
25146 }
25147
25148 cmp->pixel_width = rightmost;
25149 cmp->ascent = highest;
25150 cmp->descent = - lowest;
25151 if (cmp->ascent < font_ascent)
25152 cmp->ascent = font_ascent;
25153 if (cmp->descent < font_descent)
25154 cmp->descent = font_descent;
25155 }
25156
25157 if (it->glyph_row
25158 && (cmp->lbearing < 0
25159 || cmp->rbearing > cmp->pixel_width))
25160 it->glyph_row->contains_overlapping_glyphs_p = 1;
25161
25162 it->pixel_width = cmp->pixel_width;
25163 it->ascent = it->phys_ascent = cmp->ascent;
25164 it->descent = it->phys_descent = cmp->descent;
25165 if (face->box != FACE_NO_BOX)
25166 {
25167 int thick = face->box_line_width;
25168
25169 if (thick > 0)
25170 {
25171 it->ascent += thick;
25172 it->descent += thick;
25173 }
25174 else
25175 thick = - thick;
25176
25177 if (it->start_of_box_run_p)
25178 it->pixel_width += thick;
25179 if (it->end_of_box_run_p)
25180 it->pixel_width += thick;
25181 }
25182
25183 /* If face has an overline, add the height of the overline
25184 (1 pixel) and a 1 pixel margin to the character height. */
25185 if (face->overline_p)
25186 it->ascent += overline_margin;
25187
25188 take_vertical_position_into_account (it);
25189 if (it->ascent < 0)
25190 it->ascent = 0;
25191 if (it->descent < 0)
25192 it->descent = 0;
25193
25194 if (it->glyph_row && cmp->glyph_len > 0)
25195 append_composite_glyph (it);
25196 }
25197 else if (it->what == IT_COMPOSITION)
25198 {
25199 /* A dynamic (automatic) composition. */
25200 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25201 Lisp_Object gstring;
25202 struct font_metrics metrics;
25203
25204 it->nglyphs = 1;
25205
25206 gstring = composition_gstring_from_id (it->cmp_it.id);
25207 it->pixel_width
25208 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25209 &metrics);
25210 if (it->glyph_row
25211 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25212 it->glyph_row->contains_overlapping_glyphs_p = 1;
25213 it->ascent = it->phys_ascent = metrics.ascent;
25214 it->descent = it->phys_descent = metrics.descent;
25215 if (face->box != FACE_NO_BOX)
25216 {
25217 int thick = face->box_line_width;
25218
25219 if (thick > 0)
25220 {
25221 it->ascent += thick;
25222 it->descent += thick;
25223 }
25224 else
25225 thick = - thick;
25226
25227 if (it->start_of_box_run_p)
25228 it->pixel_width += thick;
25229 if (it->end_of_box_run_p)
25230 it->pixel_width += thick;
25231 }
25232 /* If face has an overline, add the height of the overline
25233 (1 pixel) and a 1 pixel margin to the character height. */
25234 if (face->overline_p)
25235 it->ascent += overline_margin;
25236 take_vertical_position_into_account (it);
25237 if (it->ascent < 0)
25238 it->ascent = 0;
25239 if (it->descent < 0)
25240 it->descent = 0;
25241
25242 if (it->glyph_row)
25243 append_composite_glyph (it);
25244 }
25245 else if (it->what == IT_GLYPHLESS)
25246 produce_glyphless_glyph (it, 0, Qnil);
25247 else if (it->what == IT_IMAGE)
25248 produce_image_glyph (it);
25249 else if (it->what == IT_STRETCH)
25250 produce_stretch_glyph (it);
25251
25252 done:
25253 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25254 because this isn't true for images with `:ascent 100'. */
25255 eassert (it->ascent >= 0 && it->descent >= 0);
25256 if (it->area == TEXT_AREA)
25257 it->current_x += it->pixel_width;
25258
25259 if (extra_line_spacing > 0)
25260 {
25261 it->descent += extra_line_spacing;
25262 if (extra_line_spacing > it->max_extra_line_spacing)
25263 it->max_extra_line_spacing = extra_line_spacing;
25264 }
25265
25266 it->max_ascent = max (it->max_ascent, it->ascent);
25267 it->max_descent = max (it->max_descent, it->descent);
25268 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25269 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25270 }
25271
25272 /* EXPORT for RIF:
25273 Output LEN glyphs starting at START at the nominal cursor position.
25274 Advance the nominal cursor over the text. The global variable
25275 updated_window contains the window being updated, updated_row is
25276 the glyph row being updated, and updated_area is the area of that
25277 row being updated. */
25278
25279 void
25280 x_write_glyphs (struct glyph *start, int len)
25281 {
25282 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25283
25284 eassert (updated_window && updated_row);
25285 /* When the window is hscrolled, cursor hpos can legitimately be out
25286 of bounds, but we draw the cursor at the corresponding window
25287 margin in that case. */
25288 if (!updated_row->reversed_p && chpos < 0)
25289 chpos = 0;
25290 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25291 chpos = updated_row->used[TEXT_AREA] - 1;
25292
25293 BLOCK_INPUT;
25294
25295 /* Write glyphs. */
25296
25297 hpos = start - updated_row->glyphs[updated_area];
25298 x = draw_glyphs (updated_window, output_cursor.x,
25299 updated_row, updated_area,
25300 hpos, hpos + len,
25301 DRAW_NORMAL_TEXT, 0);
25302
25303 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25304 if (updated_area == TEXT_AREA
25305 && updated_window->phys_cursor_on_p
25306 && updated_window->phys_cursor.vpos == output_cursor.vpos
25307 && chpos >= hpos
25308 && chpos < hpos + len)
25309 updated_window->phys_cursor_on_p = 0;
25310
25311 UNBLOCK_INPUT;
25312
25313 /* Advance the output cursor. */
25314 output_cursor.hpos += len;
25315 output_cursor.x = x;
25316 }
25317
25318
25319 /* EXPORT for RIF:
25320 Insert LEN glyphs from START at the nominal cursor position. */
25321
25322 void
25323 x_insert_glyphs (struct glyph *start, int len)
25324 {
25325 struct frame *f;
25326 struct window *w;
25327 int line_height, shift_by_width, shifted_region_width;
25328 struct glyph_row *row;
25329 struct glyph *glyph;
25330 int frame_x, frame_y;
25331 ptrdiff_t hpos;
25332
25333 eassert (updated_window && updated_row);
25334 BLOCK_INPUT;
25335 w = updated_window;
25336 f = XFRAME (WINDOW_FRAME (w));
25337
25338 /* Get the height of the line we are in. */
25339 row = updated_row;
25340 line_height = row->height;
25341
25342 /* Get the width of the glyphs to insert. */
25343 shift_by_width = 0;
25344 for (glyph = start; glyph < start + len; ++glyph)
25345 shift_by_width += glyph->pixel_width;
25346
25347 /* Get the width of the region to shift right. */
25348 shifted_region_width = (window_box_width (w, updated_area)
25349 - output_cursor.x
25350 - shift_by_width);
25351
25352 /* Shift right. */
25353 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25354 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25355
25356 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25357 line_height, shift_by_width);
25358
25359 /* Write the glyphs. */
25360 hpos = start - row->glyphs[updated_area];
25361 draw_glyphs (w, output_cursor.x, row, updated_area,
25362 hpos, hpos + len,
25363 DRAW_NORMAL_TEXT, 0);
25364
25365 /* Advance the output cursor. */
25366 output_cursor.hpos += len;
25367 output_cursor.x += shift_by_width;
25368 UNBLOCK_INPUT;
25369 }
25370
25371
25372 /* EXPORT for RIF:
25373 Erase the current text line from the nominal cursor position
25374 (inclusive) to pixel column TO_X (exclusive). The idea is that
25375 everything from TO_X onward is already erased.
25376
25377 TO_X is a pixel position relative to updated_area of
25378 updated_window. TO_X == -1 means clear to the end of this area. */
25379
25380 void
25381 x_clear_end_of_line (int to_x)
25382 {
25383 struct frame *f;
25384 struct window *w = updated_window;
25385 int max_x, min_y, max_y;
25386 int from_x, from_y, to_y;
25387
25388 eassert (updated_window && updated_row);
25389 f = XFRAME (w->frame);
25390
25391 if (updated_row->full_width_p)
25392 max_x = WINDOW_TOTAL_WIDTH (w);
25393 else
25394 max_x = window_box_width (w, updated_area);
25395 max_y = window_text_bottom_y (w);
25396
25397 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25398 of window. For TO_X > 0, truncate to end of drawing area. */
25399 if (to_x == 0)
25400 return;
25401 else if (to_x < 0)
25402 to_x = max_x;
25403 else
25404 to_x = min (to_x, max_x);
25405
25406 to_y = min (max_y, output_cursor.y + updated_row->height);
25407
25408 /* Notice if the cursor will be cleared by this operation. */
25409 if (!updated_row->full_width_p)
25410 notice_overwritten_cursor (w, updated_area,
25411 output_cursor.x, -1,
25412 updated_row->y,
25413 MATRIX_ROW_BOTTOM_Y (updated_row));
25414
25415 from_x = output_cursor.x;
25416
25417 /* Translate to frame coordinates. */
25418 if (updated_row->full_width_p)
25419 {
25420 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25421 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25422 }
25423 else
25424 {
25425 int area_left = window_box_left (w, updated_area);
25426 from_x += area_left;
25427 to_x += area_left;
25428 }
25429
25430 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25431 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25432 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25433
25434 /* Prevent inadvertently clearing to end of the X window. */
25435 if (to_x > from_x && to_y > from_y)
25436 {
25437 BLOCK_INPUT;
25438 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25439 to_x - from_x, to_y - from_y);
25440 UNBLOCK_INPUT;
25441 }
25442 }
25443
25444 #endif /* HAVE_WINDOW_SYSTEM */
25445
25446
25447 \f
25448 /***********************************************************************
25449 Cursor types
25450 ***********************************************************************/
25451
25452 /* Value is the internal representation of the specified cursor type
25453 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25454 of the bar cursor. */
25455
25456 static enum text_cursor_kinds
25457 get_specified_cursor_type (Lisp_Object arg, int *width)
25458 {
25459 enum text_cursor_kinds type;
25460
25461 if (NILP (arg))
25462 return NO_CURSOR;
25463
25464 if (EQ (arg, Qbox))
25465 return FILLED_BOX_CURSOR;
25466
25467 if (EQ (arg, Qhollow))
25468 return HOLLOW_BOX_CURSOR;
25469
25470 if (EQ (arg, Qbar))
25471 {
25472 *width = 2;
25473 return BAR_CURSOR;
25474 }
25475
25476 if (CONSP (arg)
25477 && EQ (XCAR (arg), Qbar)
25478 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25479 {
25480 *width = XINT (XCDR (arg));
25481 return BAR_CURSOR;
25482 }
25483
25484 if (EQ (arg, Qhbar))
25485 {
25486 *width = 2;
25487 return HBAR_CURSOR;
25488 }
25489
25490 if (CONSP (arg)
25491 && EQ (XCAR (arg), Qhbar)
25492 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25493 {
25494 *width = XINT (XCDR (arg));
25495 return HBAR_CURSOR;
25496 }
25497
25498 /* Treat anything unknown as "hollow box cursor".
25499 It was bad to signal an error; people have trouble fixing
25500 .Xdefaults with Emacs, when it has something bad in it. */
25501 type = HOLLOW_BOX_CURSOR;
25502
25503 return type;
25504 }
25505
25506 /* Set the default cursor types for specified frame. */
25507 void
25508 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25509 {
25510 int width = 1;
25511 Lisp_Object tem;
25512
25513 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25514 FRAME_CURSOR_WIDTH (f) = width;
25515
25516 /* By default, set up the blink-off state depending on the on-state. */
25517
25518 tem = Fassoc (arg, Vblink_cursor_alist);
25519 if (!NILP (tem))
25520 {
25521 FRAME_BLINK_OFF_CURSOR (f)
25522 = get_specified_cursor_type (XCDR (tem), &width);
25523 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25524 }
25525 else
25526 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25527 }
25528
25529
25530 #ifdef HAVE_WINDOW_SYSTEM
25531
25532 /* Return the cursor we want to be displayed in window W. Return
25533 width of bar/hbar cursor through WIDTH arg. Return with
25534 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25535 (i.e. if the `system caret' should track this cursor).
25536
25537 In a mini-buffer window, we want the cursor only to appear if we
25538 are reading input from this window. For the selected window, we
25539 want the cursor type given by the frame parameter or buffer local
25540 setting of cursor-type. If explicitly marked off, draw no cursor.
25541 In all other cases, we want a hollow box cursor. */
25542
25543 static enum text_cursor_kinds
25544 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25545 int *active_cursor)
25546 {
25547 struct frame *f = XFRAME (w->frame);
25548 struct buffer *b = XBUFFER (w->buffer);
25549 int cursor_type = DEFAULT_CURSOR;
25550 Lisp_Object alt_cursor;
25551 int non_selected = 0;
25552
25553 *active_cursor = 1;
25554
25555 /* Echo area */
25556 if (cursor_in_echo_area
25557 && FRAME_HAS_MINIBUF_P (f)
25558 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25559 {
25560 if (w == XWINDOW (echo_area_window))
25561 {
25562 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25563 {
25564 *width = FRAME_CURSOR_WIDTH (f);
25565 return FRAME_DESIRED_CURSOR (f);
25566 }
25567 else
25568 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25569 }
25570
25571 *active_cursor = 0;
25572 non_selected = 1;
25573 }
25574
25575 /* Detect a nonselected window or nonselected frame. */
25576 else if (w != XWINDOW (FVAR (f, selected_window))
25577 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25578 {
25579 *active_cursor = 0;
25580
25581 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25582 return NO_CURSOR;
25583
25584 non_selected = 1;
25585 }
25586
25587 /* Never display a cursor in a window in which cursor-type is nil. */
25588 if (NILP (BVAR (b, cursor_type)))
25589 return NO_CURSOR;
25590
25591 /* Get the normal cursor type for this window. */
25592 if (EQ (BVAR (b, cursor_type), Qt))
25593 {
25594 cursor_type = FRAME_DESIRED_CURSOR (f);
25595 *width = FRAME_CURSOR_WIDTH (f);
25596 }
25597 else
25598 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25599
25600 /* Use cursor-in-non-selected-windows instead
25601 for non-selected window or frame. */
25602 if (non_selected)
25603 {
25604 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25605 if (!EQ (Qt, alt_cursor))
25606 return get_specified_cursor_type (alt_cursor, width);
25607 /* t means modify the normal cursor type. */
25608 if (cursor_type == FILLED_BOX_CURSOR)
25609 cursor_type = HOLLOW_BOX_CURSOR;
25610 else if (cursor_type == BAR_CURSOR && *width > 1)
25611 --*width;
25612 return cursor_type;
25613 }
25614
25615 /* Use normal cursor if not blinked off. */
25616 if (!w->cursor_off_p)
25617 {
25618 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25619 {
25620 if (cursor_type == FILLED_BOX_CURSOR)
25621 {
25622 /* Using a block cursor on large images can be very annoying.
25623 So use a hollow cursor for "large" images.
25624 If image is not transparent (no mask), also use hollow cursor. */
25625 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25626 if (img != NULL && IMAGEP (img->spec))
25627 {
25628 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25629 where N = size of default frame font size.
25630 This should cover most of the "tiny" icons people may use. */
25631 if (!img->mask
25632 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25633 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25634 cursor_type = HOLLOW_BOX_CURSOR;
25635 }
25636 }
25637 else if (cursor_type != NO_CURSOR)
25638 {
25639 /* Display current only supports BOX and HOLLOW cursors for images.
25640 So for now, unconditionally use a HOLLOW cursor when cursor is
25641 not a solid box cursor. */
25642 cursor_type = HOLLOW_BOX_CURSOR;
25643 }
25644 }
25645 return cursor_type;
25646 }
25647
25648 /* Cursor is blinked off, so determine how to "toggle" it. */
25649
25650 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25651 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25652 return get_specified_cursor_type (XCDR (alt_cursor), width);
25653
25654 /* Then see if frame has specified a specific blink off cursor type. */
25655 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25656 {
25657 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25658 return FRAME_BLINK_OFF_CURSOR (f);
25659 }
25660
25661 #if 0
25662 /* Some people liked having a permanently visible blinking cursor,
25663 while others had very strong opinions against it. So it was
25664 decided to remove it. KFS 2003-09-03 */
25665
25666 /* Finally perform built-in cursor blinking:
25667 filled box <-> hollow box
25668 wide [h]bar <-> narrow [h]bar
25669 narrow [h]bar <-> no cursor
25670 other type <-> no cursor */
25671
25672 if (cursor_type == FILLED_BOX_CURSOR)
25673 return HOLLOW_BOX_CURSOR;
25674
25675 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25676 {
25677 *width = 1;
25678 return cursor_type;
25679 }
25680 #endif
25681
25682 return NO_CURSOR;
25683 }
25684
25685
25686 /* Notice when the text cursor of window W has been completely
25687 overwritten by a drawing operation that outputs glyphs in AREA
25688 starting at X0 and ending at X1 in the line starting at Y0 and
25689 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25690 the rest of the line after X0 has been written. Y coordinates
25691 are window-relative. */
25692
25693 static void
25694 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25695 int x0, int x1, int y0, int y1)
25696 {
25697 int cx0, cx1, cy0, cy1;
25698 struct glyph_row *row;
25699
25700 if (!w->phys_cursor_on_p)
25701 return;
25702 if (area != TEXT_AREA)
25703 return;
25704
25705 if (w->phys_cursor.vpos < 0
25706 || w->phys_cursor.vpos >= w->current_matrix->nrows
25707 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25708 !(row->enabled_p && row->displays_text_p)))
25709 return;
25710
25711 if (row->cursor_in_fringe_p)
25712 {
25713 row->cursor_in_fringe_p = 0;
25714 draw_fringe_bitmap (w, row, row->reversed_p);
25715 w->phys_cursor_on_p = 0;
25716 return;
25717 }
25718
25719 cx0 = w->phys_cursor.x;
25720 cx1 = cx0 + w->phys_cursor_width;
25721 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25722 return;
25723
25724 /* The cursor image will be completely removed from the
25725 screen if the output area intersects the cursor area in
25726 y-direction. When we draw in [y0 y1[, and some part of
25727 the cursor is at y < y0, that part must have been drawn
25728 before. When scrolling, the cursor is erased before
25729 actually scrolling, so we don't come here. When not
25730 scrolling, the rows above the old cursor row must have
25731 changed, and in this case these rows must have written
25732 over the cursor image.
25733
25734 Likewise if part of the cursor is below y1, with the
25735 exception of the cursor being in the first blank row at
25736 the buffer and window end because update_text_area
25737 doesn't draw that row. (Except when it does, but
25738 that's handled in update_text_area.) */
25739
25740 cy0 = w->phys_cursor.y;
25741 cy1 = cy0 + w->phys_cursor_height;
25742 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25743 return;
25744
25745 w->phys_cursor_on_p = 0;
25746 }
25747
25748 #endif /* HAVE_WINDOW_SYSTEM */
25749
25750 \f
25751 /************************************************************************
25752 Mouse Face
25753 ************************************************************************/
25754
25755 #ifdef HAVE_WINDOW_SYSTEM
25756
25757 /* EXPORT for RIF:
25758 Fix the display of area AREA of overlapping row ROW in window W
25759 with respect to the overlapping part OVERLAPS. */
25760
25761 void
25762 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25763 enum glyph_row_area area, int overlaps)
25764 {
25765 int i, x;
25766
25767 BLOCK_INPUT;
25768
25769 x = 0;
25770 for (i = 0; i < row->used[area];)
25771 {
25772 if (row->glyphs[area][i].overlaps_vertically_p)
25773 {
25774 int start = i, start_x = x;
25775
25776 do
25777 {
25778 x += row->glyphs[area][i].pixel_width;
25779 ++i;
25780 }
25781 while (i < row->used[area]
25782 && row->glyphs[area][i].overlaps_vertically_p);
25783
25784 draw_glyphs (w, start_x, row, area,
25785 start, i,
25786 DRAW_NORMAL_TEXT, overlaps);
25787 }
25788 else
25789 {
25790 x += row->glyphs[area][i].pixel_width;
25791 ++i;
25792 }
25793 }
25794
25795 UNBLOCK_INPUT;
25796 }
25797
25798
25799 /* EXPORT:
25800 Draw the cursor glyph of window W in glyph row ROW. See the
25801 comment of draw_glyphs for the meaning of HL. */
25802
25803 void
25804 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25805 enum draw_glyphs_face hl)
25806 {
25807 /* If cursor hpos is out of bounds, don't draw garbage. This can
25808 happen in mini-buffer windows when switching between echo area
25809 glyphs and mini-buffer. */
25810 if ((row->reversed_p
25811 ? (w->phys_cursor.hpos >= 0)
25812 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25813 {
25814 int on_p = w->phys_cursor_on_p;
25815 int x1;
25816 int hpos = w->phys_cursor.hpos;
25817
25818 /* When the window is hscrolled, cursor hpos can legitimately be
25819 out of bounds, but we draw the cursor at the corresponding
25820 window margin in that case. */
25821 if (!row->reversed_p && hpos < 0)
25822 hpos = 0;
25823 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25824 hpos = row->used[TEXT_AREA] - 1;
25825
25826 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25827 hl, 0);
25828 w->phys_cursor_on_p = on_p;
25829
25830 if (hl == DRAW_CURSOR)
25831 w->phys_cursor_width = x1 - w->phys_cursor.x;
25832 /* When we erase the cursor, and ROW is overlapped by other
25833 rows, make sure that these overlapping parts of other rows
25834 are redrawn. */
25835 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25836 {
25837 w->phys_cursor_width = x1 - w->phys_cursor.x;
25838
25839 if (row > w->current_matrix->rows
25840 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25841 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25842 OVERLAPS_ERASED_CURSOR);
25843
25844 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25845 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25846 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25847 OVERLAPS_ERASED_CURSOR);
25848 }
25849 }
25850 }
25851
25852
25853 /* EXPORT:
25854 Erase the image of a cursor of window W from the screen. */
25855
25856 void
25857 erase_phys_cursor (struct window *w)
25858 {
25859 struct frame *f = XFRAME (w->frame);
25860 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25861 int hpos = w->phys_cursor.hpos;
25862 int vpos = w->phys_cursor.vpos;
25863 int mouse_face_here_p = 0;
25864 struct glyph_matrix *active_glyphs = w->current_matrix;
25865 struct glyph_row *cursor_row;
25866 struct glyph *cursor_glyph;
25867 enum draw_glyphs_face hl;
25868
25869 /* No cursor displayed or row invalidated => nothing to do on the
25870 screen. */
25871 if (w->phys_cursor_type == NO_CURSOR)
25872 goto mark_cursor_off;
25873
25874 /* VPOS >= active_glyphs->nrows means that window has been resized.
25875 Don't bother to erase the cursor. */
25876 if (vpos >= active_glyphs->nrows)
25877 goto mark_cursor_off;
25878
25879 /* If row containing cursor is marked invalid, there is nothing we
25880 can do. */
25881 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25882 if (!cursor_row->enabled_p)
25883 goto mark_cursor_off;
25884
25885 /* If line spacing is > 0, old cursor may only be partially visible in
25886 window after split-window. So adjust visible height. */
25887 cursor_row->visible_height = min (cursor_row->visible_height,
25888 window_text_bottom_y (w) - cursor_row->y);
25889
25890 /* If row is completely invisible, don't attempt to delete a cursor which
25891 isn't there. This can happen if cursor is at top of a window, and
25892 we switch to a buffer with a header line in that window. */
25893 if (cursor_row->visible_height <= 0)
25894 goto mark_cursor_off;
25895
25896 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25897 if (cursor_row->cursor_in_fringe_p)
25898 {
25899 cursor_row->cursor_in_fringe_p = 0;
25900 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25901 goto mark_cursor_off;
25902 }
25903
25904 /* This can happen when the new row is shorter than the old one.
25905 In this case, either draw_glyphs or clear_end_of_line
25906 should have cleared the cursor. Note that we wouldn't be
25907 able to erase the cursor in this case because we don't have a
25908 cursor glyph at hand. */
25909 if ((cursor_row->reversed_p
25910 ? (w->phys_cursor.hpos < 0)
25911 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25912 goto mark_cursor_off;
25913
25914 /* When the window is hscrolled, cursor hpos can legitimately be out
25915 of bounds, but we draw the cursor at the corresponding window
25916 margin in that case. */
25917 if (!cursor_row->reversed_p && hpos < 0)
25918 hpos = 0;
25919 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25920 hpos = cursor_row->used[TEXT_AREA] - 1;
25921
25922 /* If the cursor is in the mouse face area, redisplay that when
25923 we clear the cursor. */
25924 if (! NILP (hlinfo->mouse_face_window)
25925 && coords_in_mouse_face_p (w, hpos, vpos)
25926 /* Don't redraw the cursor's spot in mouse face if it is at the
25927 end of a line (on a newline). The cursor appears there, but
25928 mouse highlighting does not. */
25929 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25930 mouse_face_here_p = 1;
25931
25932 /* Maybe clear the display under the cursor. */
25933 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25934 {
25935 int x, y, left_x;
25936 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25937 int width;
25938
25939 cursor_glyph = get_phys_cursor_glyph (w);
25940 if (cursor_glyph == NULL)
25941 goto mark_cursor_off;
25942
25943 width = cursor_glyph->pixel_width;
25944 left_x = window_box_left_offset (w, TEXT_AREA);
25945 x = w->phys_cursor.x;
25946 if (x < left_x)
25947 width -= left_x - x;
25948 width = min (width, window_box_width (w, TEXT_AREA) - x);
25949 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25950 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25951
25952 if (width > 0)
25953 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25954 }
25955
25956 /* Erase the cursor by redrawing the character underneath it. */
25957 if (mouse_face_here_p)
25958 hl = DRAW_MOUSE_FACE;
25959 else
25960 hl = DRAW_NORMAL_TEXT;
25961 draw_phys_cursor_glyph (w, cursor_row, hl);
25962
25963 mark_cursor_off:
25964 w->phys_cursor_on_p = 0;
25965 w->phys_cursor_type = NO_CURSOR;
25966 }
25967
25968
25969 /* EXPORT:
25970 Display or clear cursor of window W. If ON is zero, clear the
25971 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25972 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25973
25974 void
25975 display_and_set_cursor (struct window *w, int on,
25976 int hpos, int vpos, int x, int y)
25977 {
25978 struct frame *f = XFRAME (w->frame);
25979 int new_cursor_type;
25980 int new_cursor_width;
25981 int active_cursor;
25982 struct glyph_row *glyph_row;
25983 struct glyph *glyph;
25984
25985 /* This is pointless on invisible frames, and dangerous on garbaged
25986 windows and frames; in the latter case, the frame or window may
25987 be in the midst of changing its size, and x and y may be off the
25988 window. */
25989 if (! FRAME_VISIBLE_P (f)
25990 || FRAME_GARBAGED_P (f)
25991 || vpos >= w->current_matrix->nrows
25992 || hpos >= w->current_matrix->matrix_w)
25993 return;
25994
25995 /* If cursor is off and we want it off, return quickly. */
25996 if (!on && !w->phys_cursor_on_p)
25997 return;
25998
25999 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26000 /* If cursor row is not enabled, we don't really know where to
26001 display the cursor. */
26002 if (!glyph_row->enabled_p)
26003 {
26004 w->phys_cursor_on_p = 0;
26005 return;
26006 }
26007
26008 glyph = NULL;
26009 if (!glyph_row->exact_window_width_line_p
26010 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26011 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26012
26013 eassert (interrupt_input_blocked);
26014
26015 /* Set new_cursor_type to the cursor we want to be displayed. */
26016 new_cursor_type = get_window_cursor_type (w, glyph,
26017 &new_cursor_width, &active_cursor);
26018
26019 /* If cursor is currently being shown and we don't want it to be or
26020 it is in the wrong place, or the cursor type is not what we want,
26021 erase it. */
26022 if (w->phys_cursor_on_p
26023 && (!on
26024 || w->phys_cursor.x != x
26025 || w->phys_cursor.y != y
26026 || new_cursor_type != w->phys_cursor_type
26027 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26028 && new_cursor_width != w->phys_cursor_width)))
26029 erase_phys_cursor (w);
26030
26031 /* Don't check phys_cursor_on_p here because that flag is only set
26032 to zero in some cases where we know that the cursor has been
26033 completely erased, to avoid the extra work of erasing the cursor
26034 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26035 still not be visible, or it has only been partly erased. */
26036 if (on)
26037 {
26038 w->phys_cursor_ascent = glyph_row->ascent;
26039 w->phys_cursor_height = glyph_row->height;
26040
26041 /* Set phys_cursor_.* before x_draw_.* is called because some
26042 of them may need the information. */
26043 w->phys_cursor.x = x;
26044 w->phys_cursor.y = glyph_row->y;
26045 w->phys_cursor.hpos = hpos;
26046 w->phys_cursor.vpos = vpos;
26047 }
26048
26049 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26050 new_cursor_type, new_cursor_width,
26051 on, active_cursor);
26052 }
26053
26054
26055 /* Switch the display of W's cursor on or off, according to the value
26056 of ON. */
26057
26058 static void
26059 update_window_cursor (struct window *w, int on)
26060 {
26061 /* Don't update cursor in windows whose frame is in the process
26062 of being deleted. */
26063 if (w->current_matrix)
26064 {
26065 int hpos = w->phys_cursor.hpos;
26066 int vpos = w->phys_cursor.vpos;
26067 struct glyph_row *row;
26068
26069 if (vpos >= w->current_matrix->nrows
26070 || hpos >= w->current_matrix->matrix_w)
26071 return;
26072
26073 row = MATRIX_ROW (w->current_matrix, vpos);
26074
26075 /* When the window is hscrolled, cursor hpos can legitimately be
26076 out of bounds, but we draw the cursor at the corresponding
26077 window margin in that case. */
26078 if (!row->reversed_p && hpos < 0)
26079 hpos = 0;
26080 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26081 hpos = row->used[TEXT_AREA] - 1;
26082
26083 BLOCK_INPUT;
26084 display_and_set_cursor (w, on, hpos, vpos,
26085 w->phys_cursor.x, w->phys_cursor.y);
26086 UNBLOCK_INPUT;
26087 }
26088 }
26089
26090
26091 /* Call update_window_cursor with parameter ON_P on all leaf windows
26092 in the window tree rooted at W. */
26093
26094 static void
26095 update_cursor_in_window_tree (struct window *w, int on_p)
26096 {
26097 while (w)
26098 {
26099 if (!NILP (w->hchild))
26100 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26101 else if (!NILP (w->vchild))
26102 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26103 else
26104 update_window_cursor (w, on_p);
26105
26106 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26107 }
26108 }
26109
26110
26111 /* EXPORT:
26112 Display the cursor on window W, or clear it, according to ON_P.
26113 Don't change the cursor's position. */
26114
26115 void
26116 x_update_cursor (struct frame *f, int on_p)
26117 {
26118 update_cursor_in_window_tree (XWINDOW (FVAR (f, root_window)), on_p);
26119 }
26120
26121
26122 /* EXPORT:
26123 Clear the cursor of window W to background color, and mark the
26124 cursor as not shown. This is used when the text where the cursor
26125 is about to be rewritten. */
26126
26127 void
26128 x_clear_cursor (struct window *w)
26129 {
26130 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26131 update_window_cursor (w, 0);
26132 }
26133
26134 #endif /* HAVE_WINDOW_SYSTEM */
26135
26136 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26137 and MSDOS. */
26138 static void
26139 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26140 int start_hpos, int end_hpos,
26141 enum draw_glyphs_face draw)
26142 {
26143 #ifdef HAVE_WINDOW_SYSTEM
26144 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26145 {
26146 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26147 return;
26148 }
26149 #endif
26150 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26151 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26152 #endif
26153 }
26154
26155 /* Display the active region described by mouse_face_* according to DRAW. */
26156
26157 static void
26158 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26159 {
26160 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26161 struct frame *f = XFRAME (WINDOW_FRAME (w));
26162
26163 if (/* If window is in the process of being destroyed, don't bother
26164 to do anything. */
26165 w->current_matrix != NULL
26166 /* Don't update mouse highlight if hidden */
26167 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26168 /* Recognize when we are called to operate on rows that don't exist
26169 anymore. This can happen when a window is split. */
26170 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26171 {
26172 int phys_cursor_on_p = w->phys_cursor_on_p;
26173 struct glyph_row *row, *first, *last;
26174
26175 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26176 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26177
26178 for (row = first; row <= last && row->enabled_p; ++row)
26179 {
26180 int start_hpos, end_hpos, start_x;
26181
26182 /* For all but the first row, the highlight starts at column 0. */
26183 if (row == first)
26184 {
26185 /* R2L rows have BEG and END in reversed order, but the
26186 screen drawing geometry is always left to right. So
26187 we need to mirror the beginning and end of the
26188 highlighted area in R2L rows. */
26189 if (!row->reversed_p)
26190 {
26191 start_hpos = hlinfo->mouse_face_beg_col;
26192 start_x = hlinfo->mouse_face_beg_x;
26193 }
26194 else if (row == last)
26195 {
26196 start_hpos = hlinfo->mouse_face_end_col;
26197 start_x = hlinfo->mouse_face_end_x;
26198 }
26199 else
26200 {
26201 start_hpos = 0;
26202 start_x = 0;
26203 }
26204 }
26205 else if (row->reversed_p && row == last)
26206 {
26207 start_hpos = hlinfo->mouse_face_end_col;
26208 start_x = hlinfo->mouse_face_end_x;
26209 }
26210 else
26211 {
26212 start_hpos = 0;
26213 start_x = 0;
26214 }
26215
26216 if (row == last)
26217 {
26218 if (!row->reversed_p)
26219 end_hpos = hlinfo->mouse_face_end_col;
26220 else if (row == first)
26221 end_hpos = hlinfo->mouse_face_beg_col;
26222 else
26223 {
26224 end_hpos = row->used[TEXT_AREA];
26225 if (draw == DRAW_NORMAL_TEXT)
26226 row->fill_line_p = 1; /* Clear to end of line */
26227 }
26228 }
26229 else if (row->reversed_p && row == first)
26230 end_hpos = hlinfo->mouse_face_beg_col;
26231 else
26232 {
26233 end_hpos = row->used[TEXT_AREA];
26234 if (draw == DRAW_NORMAL_TEXT)
26235 row->fill_line_p = 1; /* Clear to end of line */
26236 }
26237
26238 if (end_hpos > start_hpos)
26239 {
26240 draw_row_with_mouse_face (w, start_x, row,
26241 start_hpos, end_hpos, draw);
26242
26243 row->mouse_face_p
26244 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26245 }
26246 }
26247
26248 #ifdef HAVE_WINDOW_SYSTEM
26249 /* When we've written over the cursor, arrange for it to
26250 be displayed again. */
26251 if (FRAME_WINDOW_P (f)
26252 && phys_cursor_on_p && !w->phys_cursor_on_p)
26253 {
26254 int hpos = w->phys_cursor.hpos;
26255
26256 /* When the window is hscrolled, cursor hpos can legitimately be
26257 out of bounds, but we draw the cursor at the corresponding
26258 window margin in that case. */
26259 if (!row->reversed_p && hpos < 0)
26260 hpos = 0;
26261 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26262 hpos = row->used[TEXT_AREA] - 1;
26263
26264 BLOCK_INPUT;
26265 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26266 w->phys_cursor.x, w->phys_cursor.y);
26267 UNBLOCK_INPUT;
26268 }
26269 #endif /* HAVE_WINDOW_SYSTEM */
26270 }
26271
26272 #ifdef HAVE_WINDOW_SYSTEM
26273 /* Change the mouse cursor. */
26274 if (FRAME_WINDOW_P (f))
26275 {
26276 if (draw == DRAW_NORMAL_TEXT
26277 && !EQ (hlinfo->mouse_face_window, FVAR (f, tool_bar_window)))
26278 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26279 else if (draw == DRAW_MOUSE_FACE)
26280 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26281 else
26282 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26283 }
26284 #endif /* HAVE_WINDOW_SYSTEM */
26285 }
26286
26287 /* EXPORT:
26288 Clear out the mouse-highlighted active region.
26289 Redraw it un-highlighted first. Value is non-zero if mouse
26290 face was actually drawn unhighlighted. */
26291
26292 int
26293 clear_mouse_face (Mouse_HLInfo *hlinfo)
26294 {
26295 int cleared = 0;
26296
26297 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26298 {
26299 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26300 cleared = 1;
26301 }
26302
26303 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26304 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26305 hlinfo->mouse_face_window = Qnil;
26306 hlinfo->mouse_face_overlay = Qnil;
26307 return cleared;
26308 }
26309
26310 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26311 within the mouse face on that window. */
26312 static int
26313 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26314 {
26315 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26316
26317 /* Quickly resolve the easy cases. */
26318 if (!(WINDOWP (hlinfo->mouse_face_window)
26319 && XWINDOW (hlinfo->mouse_face_window) == w))
26320 return 0;
26321 if (vpos < hlinfo->mouse_face_beg_row
26322 || vpos > hlinfo->mouse_face_end_row)
26323 return 0;
26324 if (vpos > hlinfo->mouse_face_beg_row
26325 && vpos < hlinfo->mouse_face_end_row)
26326 return 1;
26327
26328 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26329 {
26330 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26331 {
26332 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26333 return 1;
26334 }
26335 else if ((vpos == hlinfo->mouse_face_beg_row
26336 && hpos >= hlinfo->mouse_face_beg_col)
26337 || (vpos == hlinfo->mouse_face_end_row
26338 && hpos < hlinfo->mouse_face_end_col))
26339 return 1;
26340 }
26341 else
26342 {
26343 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26344 {
26345 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26346 return 1;
26347 }
26348 else if ((vpos == hlinfo->mouse_face_beg_row
26349 && hpos <= hlinfo->mouse_face_beg_col)
26350 || (vpos == hlinfo->mouse_face_end_row
26351 && hpos > hlinfo->mouse_face_end_col))
26352 return 1;
26353 }
26354 return 0;
26355 }
26356
26357
26358 /* EXPORT:
26359 Non-zero if physical cursor of window W is within mouse face. */
26360
26361 int
26362 cursor_in_mouse_face_p (struct window *w)
26363 {
26364 int hpos = w->phys_cursor.hpos;
26365 int vpos = w->phys_cursor.vpos;
26366 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26367
26368 /* When the window is hscrolled, cursor hpos can legitimately be out
26369 of bounds, but we draw the cursor at the corresponding window
26370 margin in that case. */
26371 if (!row->reversed_p && hpos < 0)
26372 hpos = 0;
26373 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26374 hpos = row->used[TEXT_AREA] - 1;
26375
26376 return coords_in_mouse_face_p (w, hpos, vpos);
26377 }
26378
26379
26380 \f
26381 /* Find the glyph rows START_ROW and END_ROW of window W that display
26382 characters between buffer positions START_CHARPOS and END_CHARPOS
26383 (excluding END_CHARPOS). DISP_STRING is a display string that
26384 covers these buffer positions. This is similar to
26385 row_containing_pos, but is more accurate when bidi reordering makes
26386 buffer positions change non-linearly with glyph rows. */
26387 static void
26388 rows_from_pos_range (struct window *w,
26389 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26390 Lisp_Object disp_string,
26391 struct glyph_row **start, struct glyph_row **end)
26392 {
26393 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26394 int last_y = window_text_bottom_y (w);
26395 struct glyph_row *row;
26396
26397 *start = NULL;
26398 *end = NULL;
26399
26400 while (!first->enabled_p
26401 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26402 first++;
26403
26404 /* Find the START row. */
26405 for (row = first;
26406 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26407 row++)
26408 {
26409 /* A row can potentially be the START row if the range of the
26410 characters it displays intersects the range
26411 [START_CHARPOS..END_CHARPOS). */
26412 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26413 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26414 /* See the commentary in row_containing_pos, for the
26415 explanation of the complicated way to check whether
26416 some position is beyond the end of the characters
26417 displayed by a row. */
26418 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26419 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26420 && !row->ends_at_zv_p
26421 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26422 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26423 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26424 && !row->ends_at_zv_p
26425 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26426 {
26427 /* Found a candidate row. Now make sure at least one of the
26428 glyphs it displays has a charpos from the range
26429 [START_CHARPOS..END_CHARPOS).
26430
26431 This is not obvious because bidi reordering could make
26432 buffer positions of a row be 1,2,3,102,101,100, and if we
26433 want to highlight characters in [50..60), we don't want
26434 this row, even though [50..60) does intersect [1..103),
26435 the range of character positions given by the row's start
26436 and end positions. */
26437 struct glyph *g = row->glyphs[TEXT_AREA];
26438 struct glyph *e = g + row->used[TEXT_AREA];
26439
26440 while (g < e)
26441 {
26442 if (((BUFFERP (g->object) || INTEGERP (g->object))
26443 && start_charpos <= g->charpos && g->charpos < end_charpos)
26444 /* A glyph that comes from DISP_STRING is by
26445 definition to be highlighted. */
26446 || EQ (g->object, disp_string))
26447 *start = row;
26448 g++;
26449 }
26450 if (*start)
26451 break;
26452 }
26453 }
26454
26455 /* Find the END row. */
26456 if (!*start
26457 /* If the last row is partially visible, start looking for END
26458 from that row, instead of starting from FIRST. */
26459 && !(row->enabled_p
26460 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26461 row = first;
26462 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26463 {
26464 struct glyph_row *next = row + 1;
26465 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26466
26467 if (!next->enabled_p
26468 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26469 /* The first row >= START whose range of displayed characters
26470 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26471 is the row END + 1. */
26472 || (start_charpos < next_start
26473 && end_charpos < next_start)
26474 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26475 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26476 && !next->ends_at_zv_p
26477 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26478 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26479 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26480 && !next->ends_at_zv_p
26481 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26482 {
26483 *end = row;
26484 break;
26485 }
26486 else
26487 {
26488 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26489 but none of the characters it displays are in the range, it is
26490 also END + 1. */
26491 struct glyph *g = next->glyphs[TEXT_AREA];
26492 struct glyph *s = g;
26493 struct glyph *e = g + next->used[TEXT_AREA];
26494
26495 while (g < e)
26496 {
26497 if (((BUFFERP (g->object) || INTEGERP (g->object))
26498 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26499 /* If the buffer position of the first glyph in
26500 the row is equal to END_CHARPOS, it means
26501 the last character to be highlighted is the
26502 newline of ROW, and we must consider NEXT as
26503 END, not END+1. */
26504 || (((!next->reversed_p && g == s)
26505 || (next->reversed_p && g == e - 1))
26506 && (g->charpos == end_charpos
26507 /* Special case for when NEXT is an
26508 empty line at ZV. */
26509 || (g->charpos == -1
26510 && !row->ends_at_zv_p
26511 && next_start == end_charpos)))))
26512 /* A glyph that comes from DISP_STRING is by
26513 definition to be highlighted. */
26514 || EQ (g->object, disp_string))
26515 break;
26516 g++;
26517 }
26518 if (g == e)
26519 {
26520 *end = row;
26521 break;
26522 }
26523 /* The first row that ends at ZV must be the last to be
26524 highlighted. */
26525 else if (next->ends_at_zv_p)
26526 {
26527 *end = next;
26528 break;
26529 }
26530 }
26531 }
26532 }
26533
26534 /* This function sets the mouse_face_* elements of HLINFO, assuming
26535 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26536 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26537 for the overlay or run of text properties specifying the mouse
26538 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26539 before-string and after-string that must also be highlighted.
26540 DISP_STRING, if non-nil, is a display string that may cover some
26541 or all of the highlighted text. */
26542
26543 static void
26544 mouse_face_from_buffer_pos (Lisp_Object window,
26545 Mouse_HLInfo *hlinfo,
26546 ptrdiff_t mouse_charpos,
26547 ptrdiff_t start_charpos,
26548 ptrdiff_t end_charpos,
26549 Lisp_Object before_string,
26550 Lisp_Object after_string,
26551 Lisp_Object disp_string)
26552 {
26553 struct window *w = XWINDOW (window);
26554 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26555 struct glyph_row *r1, *r2;
26556 struct glyph *glyph, *end;
26557 ptrdiff_t ignore, pos;
26558 int x;
26559
26560 eassert (NILP (disp_string) || STRINGP (disp_string));
26561 eassert (NILP (before_string) || STRINGP (before_string));
26562 eassert (NILP (after_string) || STRINGP (after_string));
26563
26564 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26565 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26566 if (r1 == NULL)
26567 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26568 /* If the before-string or display-string contains newlines,
26569 rows_from_pos_range skips to its last row. Move back. */
26570 if (!NILP (before_string) || !NILP (disp_string))
26571 {
26572 struct glyph_row *prev;
26573 while ((prev = r1 - 1, prev >= first)
26574 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26575 && prev->used[TEXT_AREA] > 0)
26576 {
26577 struct glyph *beg = prev->glyphs[TEXT_AREA];
26578 glyph = beg + prev->used[TEXT_AREA];
26579 while (--glyph >= beg && INTEGERP (glyph->object));
26580 if (glyph < beg
26581 || !(EQ (glyph->object, before_string)
26582 || EQ (glyph->object, disp_string)))
26583 break;
26584 r1 = prev;
26585 }
26586 }
26587 if (r2 == NULL)
26588 {
26589 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26590 hlinfo->mouse_face_past_end = 1;
26591 }
26592 else if (!NILP (after_string))
26593 {
26594 /* If the after-string has newlines, advance to its last row. */
26595 struct glyph_row *next;
26596 struct glyph_row *last
26597 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26598
26599 for (next = r2 + 1;
26600 next <= last
26601 && next->used[TEXT_AREA] > 0
26602 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26603 ++next)
26604 r2 = next;
26605 }
26606 /* The rest of the display engine assumes that mouse_face_beg_row is
26607 either above mouse_face_end_row or identical to it. But with
26608 bidi-reordered continued lines, the row for START_CHARPOS could
26609 be below the row for END_CHARPOS. If so, swap the rows and store
26610 them in correct order. */
26611 if (r1->y > r2->y)
26612 {
26613 struct glyph_row *tem = r2;
26614
26615 r2 = r1;
26616 r1 = tem;
26617 }
26618
26619 hlinfo->mouse_face_beg_y = r1->y;
26620 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26621 hlinfo->mouse_face_end_y = r2->y;
26622 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26623
26624 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26625 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26626 could be anywhere in the row and in any order. The strategy
26627 below is to find the leftmost and the rightmost glyph that
26628 belongs to either of these 3 strings, or whose position is
26629 between START_CHARPOS and END_CHARPOS, and highlight all the
26630 glyphs between those two. This may cover more than just the text
26631 between START_CHARPOS and END_CHARPOS if the range of characters
26632 strides the bidi level boundary, e.g. if the beginning is in R2L
26633 text while the end is in L2R text or vice versa. */
26634 if (!r1->reversed_p)
26635 {
26636 /* This row is in a left to right paragraph. Scan it left to
26637 right. */
26638 glyph = r1->glyphs[TEXT_AREA];
26639 end = glyph + r1->used[TEXT_AREA];
26640 x = r1->x;
26641
26642 /* Skip truncation glyphs at the start of the glyph row. */
26643 if (r1->displays_text_p)
26644 for (; glyph < end
26645 && INTEGERP (glyph->object)
26646 && glyph->charpos < 0;
26647 ++glyph)
26648 x += glyph->pixel_width;
26649
26650 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26651 or DISP_STRING, and the first glyph from buffer whose
26652 position is between START_CHARPOS and END_CHARPOS. */
26653 for (; glyph < end
26654 && !INTEGERP (glyph->object)
26655 && !EQ (glyph->object, disp_string)
26656 && !(BUFFERP (glyph->object)
26657 && (glyph->charpos >= start_charpos
26658 && glyph->charpos < end_charpos));
26659 ++glyph)
26660 {
26661 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26662 are present at buffer positions between START_CHARPOS and
26663 END_CHARPOS, or if they come from an overlay. */
26664 if (EQ (glyph->object, before_string))
26665 {
26666 pos = string_buffer_position (before_string,
26667 start_charpos);
26668 /* If pos == 0, it means before_string came from an
26669 overlay, not from a buffer position. */
26670 if (!pos || (pos >= start_charpos && pos < end_charpos))
26671 break;
26672 }
26673 else if (EQ (glyph->object, after_string))
26674 {
26675 pos = string_buffer_position (after_string, end_charpos);
26676 if (!pos || (pos >= start_charpos && pos < end_charpos))
26677 break;
26678 }
26679 x += glyph->pixel_width;
26680 }
26681 hlinfo->mouse_face_beg_x = x;
26682 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26683 }
26684 else
26685 {
26686 /* This row is in a right to left paragraph. Scan it right to
26687 left. */
26688 struct glyph *g;
26689
26690 end = r1->glyphs[TEXT_AREA] - 1;
26691 glyph = end + r1->used[TEXT_AREA];
26692
26693 /* Skip truncation glyphs at the start of the glyph row. */
26694 if (r1->displays_text_p)
26695 for (; glyph > end
26696 && INTEGERP (glyph->object)
26697 && glyph->charpos < 0;
26698 --glyph)
26699 ;
26700
26701 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26702 or DISP_STRING, and the first glyph from buffer whose
26703 position is between START_CHARPOS and END_CHARPOS. */
26704 for (; glyph > end
26705 && !INTEGERP (glyph->object)
26706 && !EQ (glyph->object, disp_string)
26707 && !(BUFFERP (glyph->object)
26708 && (glyph->charpos >= start_charpos
26709 && glyph->charpos < end_charpos));
26710 --glyph)
26711 {
26712 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26713 are present at buffer positions between START_CHARPOS and
26714 END_CHARPOS, or if they come from an overlay. */
26715 if (EQ (glyph->object, before_string))
26716 {
26717 pos = string_buffer_position (before_string, start_charpos);
26718 /* If pos == 0, it means before_string came from an
26719 overlay, not from a buffer position. */
26720 if (!pos || (pos >= start_charpos && pos < end_charpos))
26721 break;
26722 }
26723 else if (EQ (glyph->object, after_string))
26724 {
26725 pos = string_buffer_position (after_string, end_charpos);
26726 if (!pos || (pos >= start_charpos && pos < end_charpos))
26727 break;
26728 }
26729 }
26730
26731 glyph++; /* first glyph to the right of the highlighted area */
26732 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26733 x += g->pixel_width;
26734 hlinfo->mouse_face_beg_x = x;
26735 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26736 }
26737
26738 /* If the highlight ends in a different row, compute GLYPH and END
26739 for the end row. Otherwise, reuse the values computed above for
26740 the row where the highlight begins. */
26741 if (r2 != r1)
26742 {
26743 if (!r2->reversed_p)
26744 {
26745 glyph = r2->glyphs[TEXT_AREA];
26746 end = glyph + r2->used[TEXT_AREA];
26747 x = r2->x;
26748 }
26749 else
26750 {
26751 end = r2->glyphs[TEXT_AREA] - 1;
26752 glyph = end + r2->used[TEXT_AREA];
26753 }
26754 }
26755
26756 if (!r2->reversed_p)
26757 {
26758 /* Skip truncation and continuation glyphs near the end of the
26759 row, and also blanks and stretch glyphs inserted by
26760 extend_face_to_end_of_line. */
26761 while (end > glyph
26762 && INTEGERP ((end - 1)->object))
26763 --end;
26764 /* Scan the rest of the glyph row from the end, looking for the
26765 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26766 DISP_STRING, or whose position is between START_CHARPOS
26767 and END_CHARPOS */
26768 for (--end;
26769 end > glyph
26770 && !INTEGERP (end->object)
26771 && !EQ (end->object, disp_string)
26772 && !(BUFFERP (end->object)
26773 && (end->charpos >= start_charpos
26774 && end->charpos < end_charpos));
26775 --end)
26776 {
26777 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26778 are present at buffer positions between START_CHARPOS and
26779 END_CHARPOS, or if they come from an overlay. */
26780 if (EQ (end->object, before_string))
26781 {
26782 pos = string_buffer_position (before_string, start_charpos);
26783 if (!pos || (pos >= start_charpos && pos < end_charpos))
26784 break;
26785 }
26786 else if (EQ (end->object, after_string))
26787 {
26788 pos = string_buffer_position (after_string, end_charpos);
26789 if (!pos || (pos >= start_charpos && pos < end_charpos))
26790 break;
26791 }
26792 }
26793 /* Find the X coordinate of the last glyph to be highlighted. */
26794 for (; glyph <= end; ++glyph)
26795 x += glyph->pixel_width;
26796
26797 hlinfo->mouse_face_end_x = x;
26798 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26799 }
26800 else
26801 {
26802 /* Skip truncation and continuation glyphs near the end of the
26803 row, and also blanks and stretch glyphs inserted by
26804 extend_face_to_end_of_line. */
26805 x = r2->x;
26806 end++;
26807 while (end < glyph
26808 && INTEGERP (end->object))
26809 {
26810 x += end->pixel_width;
26811 ++end;
26812 }
26813 /* Scan the rest of the glyph row from the end, looking for the
26814 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26815 DISP_STRING, or whose position is between START_CHARPOS
26816 and END_CHARPOS */
26817 for ( ;
26818 end < glyph
26819 && !INTEGERP (end->object)
26820 && !EQ (end->object, disp_string)
26821 && !(BUFFERP (end->object)
26822 && (end->charpos >= start_charpos
26823 && end->charpos < end_charpos));
26824 ++end)
26825 {
26826 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26827 are present at buffer positions between START_CHARPOS and
26828 END_CHARPOS, or if they come from an overlay. */
26829 if (EQ (end->object, before_string))
26830 {
26831 pos = string_buffer_position (before_string, start_charpos);
26832 if (!pos || (pos >= start_charpos && pos < end_charpos))
26833 break;
26834 }
26835 else if (EQ (end->object, after_string))
26836 {
26837 pos = string_buffer_position (after_string, end_charpos);
26838 if (!pos || (pos >= start_charpos && pos < end_charpos))
26839 break;
26840 }
26841 x += end->pixel_width;
26842 }
26843 /* If we exited the above loop because we arrived at the last
26844 glyph of the row, and its buffer position is still not in
26845 range, it means the last character in range is the preceding
26846 newline. Bump the end column and x values to get past the
26847 last glyph. */
26848 if (end == glyph
26849 && BUFFERP (end->object)
26850 && (end->charpos < start_charpos
26851 || end->charpos >= end_charpos))
26852 {
26853 x += end->pixel_width;
26854 ++end;
26855 }
26856 hlinfo->mouse_face_end_x = x;
26857 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26858 }
26859
26860 hlinfo->mouse_face_window = window;
26861 hlinfo->mouse_face_face_id
26862 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26863 mouse_charpos + 1,
26864 !hlinfo->mouse_face_hidden, -1);
26865 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26866 }
26867
26868 /* The following function is not used anymore (replaced with
26869 mouse_face_from_string_pos), but I leave it here for the time
26870 being, in case someone would. */
26871
26872 #if 0 /* not used */
26873
26874 /* Find the position of the glyph for position POS in OBJECT in
26875 window W's current matrix, and return in *X, *Y the pixel
26876 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26877
26878 RIGHT_P non-zero means return the position of the right edge of the
26879 glyph, RIGHT_P zero means return the left edge position.
26880
26881 If no glyph for POS exists in the matrix, return the position of
26882 the glyph with the next smaller position that is in the matrix, if
26883 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26884 exists in the matrix, return the position of the glyph with the
26885 next larger position in OBJECT.
26886
26887 Value is non-zero if a glyph was found. */
26888
26889 static int
26890 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26891 int *hpos, int *vpos, int *x, int *y, int right_p)
26892 {
26893 int yb = window_text_bottom_y (w);
26894 struct glyph_row *r;
26895 struct glyph *best_glyph = NULL;
26896 struct glyph_row *best_row = NULL;
26897 int best_x = 0;
26898
26899 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26900 r->enabled_p && r->y < yb;
26901 ++r)
26902 {
26903 struct glyph *g = r->glyphs[TEXT_AREA];
26904 struct glyph *e = g + r->used[TEXT_AREA];
26905 int gx;
26906
26907 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26908 if (EQ (g->object, object))
26909 {
26910 if (g->charpos == pos)
26911 {
26912 best_glyph = g;
26913 best_x = gx;
26914 best_row = r;
26915 goto found;
26916 }
26917 else if (best_glyph == NULL
26918 || ((eabs (g->charpos - pos)
26919 < eabs (best_glyph->charpos - pos))
26920 && (right_p
26921 ? g->charpos < pos
26922 : g->charpos > pos)))
26923 {
26924 best_glyph = g;
26925 best_x = gx;
26926 best_row = r;
26927 }
26928 }
26929 }
26930
26931 found:
26932
26933 if (best_glyph)
26934 {
26935 *x = best_x;
26936 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26937
26938 if (right_p)
26939 {
26940 *x += best_glyph->pixel_width;
26941 ++*hpos;
26942 }
26943
26944 *y = best_row->y;
26945 *vpos = best_row - w->current_matrix->rows;
26946 }
26947
26948 return best_glyph != NULL;
26949 }
26950 #endif /* not used */
26951
26952 /* Find the positions of the first and the last glyphs in window W's
26953 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26954 (assumed to be a string), and return in HLINFO's mouse_face_*
26955 members the pixel and column/row coordinates of those glyphs. */
26956
26957 static void
26958 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26959 Lisp_Object object,
26960 ptrdiff_t startpos, ptrdiff_t endpos)
26961 {
26962 int yb = window_text_bottom_y (w);
26963 struct glyph_row *r;
26964 struct glyph *g, *e;
26965 int gx;
26966 int found = 0;
26967
26968 /* Find the glyph row with at least one position in the range
26969 [STARTPOS..ENDPOS], and the first glyph in that row whose
26970 position belongs to that range. */
26971 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26972 r->enabled_p && r->y < yb;
26973 ++r)
26974 {
26975 if (!r->reversed_p)
26976 {
26977 g = r->glyphs[TEXT_AREA];
26978 e = g + r->used[TEXT_AREA];
26979 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26980 if (EQ (g->object, object)
26981 && startpos <= g->charpos && g->charpos <= endpos)
26982 {
26983 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26984 hlinfo->mouse_face_beg_y = r->y;
26985 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26986 hlinfo->mouse_face_beg_x = gx;
26987 found = 1;
26988 break;
26989 }
26990 }
26991 else
26992 {
26993 struct glyph *g1;
26994
26995 e = r->glyphs[TEXT_AREA];
26996 g = e + r->used[TEXT_AREA];
26997 for ( ; g > e; --g)
26998 if (EQ ((g-1)->object, object)
26999 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27000 {
27001 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27002 hlinfo->mouse_face_beg_y = r->y;
27003 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27004 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27005 gx += g1->pixel_width;
27006 hlinfo->mouse_face_beg_x = gx;
27007 found = 1;
27008 break;
27009 }
27010 }
27011 if (found)
27012 break;
27013 }
27014
27015 if (!found)
27016 return;
27017
27018 /* Starting with the next row, look for the first row which does NOT
27019 include any glyphs whose positions are in the range. */
27020 for (++r; r->enabled_p && r->y < yb; ++r)
27021 {
27022 g = r->glyphs[TEXT_AREA];
27023 e = g + r->used[TEXT_AREA];
27024 found = 0;
27025 for ( ; g < e; ++g)
27026 if (EQ (g->object, object)
27027 && startpos <= g->charpos && g->charpos <= endpos)
27028 {
27029 found = 1;
27030 break;
27031 }
27032 if (!found)
27033 break;
27034 }
27035
27036 /* The highlighted region ends on the previous row. */
27037 r--;
27038
27039 /* Set the end row and its vertical pixel coordinate. */
27040 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27041 hlinfo->mouse_face_end_y = r->y;
27042
27043 /* Compute and set the end column and the end column's horizontal
27044 pixel coordinate. */
27045 if (!r->reversed_p)
27046 {
27047 g = r->glyphs[TEXT_AREA];
27048 e = g + r->used[TEXT_AREA];
27049 for ( ; e > g; --e)
27050 if (EQ ((e-1)->object, object)
27051 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27052 break;
27053 hlinfo->mouse_face_end_col = e - g;
27054
27055 for (gx = r->x; g < e; ++g)
27056 gx += g->pixel_width;
27057 hlinfo->mouse_face_end_x = gx;
27058 }
27059 else
27060 {
27061 e = r->glyphs[TEXT_AREA];
27062 g = e + r->used[TEXT_AREA];
27063 for (gx = r->x ; e < g; ++e)
27064 {
27065 if (EQ (e->object, object)
27066 && startpos <= e->charpos && e->charpos <= endpos)
27067 break;
27068 gx += e->pixel_width;
27069 }
27070 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27071 hlinfo->mouse_face_end_x = gx;
27072 }
27073 }
27074
27075 #ifdef HAVE_WINDOW_SYSTEM
27076
27077 /* See if position X, Y is within a hot-spot of an image. */
27078
27079 static int
27080 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27081 {
27082 if (!CONSP (hot_spot))
27083 return 0;
27084
27085 if (EQ (XCAR (hot_spot), Qrect))
27086 {
27087 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27088 Lisp_Object rect = XCDR (hot_spot);
27089 Lisp_Object tem;
27090 if (!CONSP (rect))
27091 return 0;
27092 if (!CONSP (XCAR (rect)))
27093 return 0;
27094 if (!CONSP (XCDR (rect)))
27095 return 0;
27096 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27097 return 0;
27098 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27099 return 0;
27100 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27101 return 0;
27102 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27103 return 0;
27104 return 1;
27105 }
27106 else if (EQ (XCAR (hot_spot), Qcircle))
27107 {
27108 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27109 Lisp_Object circ = XCDR (hot_spot);
27110 Lisp_Object lr, lx0, ly0;
27111 if (CONSP (circ)
27112 && CONSP (XCAR (circ))
27113 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27114 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27115 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27116 {
27117 double r = XFLOATINT (lr);
27118 double dx = XINT (lx0) - x;
27119 double dy = XINT (ly0) - y;
27120 return (dx * dx + dy * dy <= r * r);
27121 }
27122 }
27123 else if (EQ (XCAR (hot_spot), Qpoly))
27124 {
27125 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27126 if (VECTORP (XCDR (hot_spot)))
27127 {
27128 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27129 Lisp_Object *poly = v->contents;
27130 ptrdiff_t n = v->header.size;
27131 ptrdiff_t i;
27132 int inside = 0;
27133 Lisp_Object lx, ly;
27134 int x0, y0;
27135
27136 /* Need an even number of coordinates, and at least 3 edges. */
27137 if (n < 6 || n & 1)
27138 return 0;
27139
27140 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27141 If count is odd, we are inside polygon. Pixels on edges
27142 may or may not be included depending on actual geometry of the
27143 polygon. */
27144 if ((lx = poly[n-2], !INTEGERP (lx))
27145 || (ly = poly[n-1], !INTEGERP (lx)))
27146 return 0;
27147 x0 = XINT (lx), y0 = XINT (ly);
27148 for (i = 0; i < n; i += 2)
27149 {
27150 int x1 = x0, y1 = y0;
27151 if ((lx = poly[i], !INTEGERP (lx))
27152 || (ly = poly[i+1], !INTEGERP (ly)))
27153 return 0;
27154 x0 = XINT (lx), y0 = XINT (ly);
27155
27156 /* Does this segment cross the X line? */
27157 if (x0 >= x)
27158 {
27159 if (x1 >= x)
27160 continue;
27161 }
27162 else if (x1 < x)
27163 continue;
27164 if (y > y0 && y > y1)
27165 continue;
27166 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27167 inside = !inside;
27168 }
27169 return inside;
27170 }
27171 }
27172 return 0;
27173 }
27174
27175 Lisp_Object
27176 find_hot_spot (Lisp_Object map, int x, int y)
27177 {
27178 while (CONSP (map))
27179 {
27180 if (CONSP (XCAR (map))
27181 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27182 return XCAR (map);
27183 map = XCDR (map);
27184 }
27185
27186 return Qnil;
27187 }
27188
27189 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27190 3, 3, 0,
27191 doc: /* Lookup in image map MAP coordinates X and Y.
27192 An image map is an alist where each element has the format (AREA ID PLIST).
27193 An AREA is specified as either a rectangle, a circle, or a polygon:
27194 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27195 pixel coordinates of the upper left and bottom right corners.
27196 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27197 and the radius of the circle; r may be a float or integer.
27198 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27199 vector describes one corner in the polygon.
27200 Returns the alist element for the first matching AREA in MAP. */)
27201 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27202 {
27203 if (NILP (map))
27204 return Qnil;
27205
27206 CHECK_NUMBER (x);
27207 CHECK_NUMBER (y);
27208
27209 return find_hot_spot (map,
27210 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27211 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27212 }
27213
27214
27215 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27216 static void
27217 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27218 {
27219 /* Do not change cursor shape while dragging mouse. */
27220 if (!NILP (do_mouse_tracking))
27221 return;
27222
27223 if (!NILP (pointer))
27224 {
27225 if (EQ (pointer, Qarrow))
27226 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27227 else if (EQ (pointer, Qhand))
27228 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27229 else if (EQ (pointer, Qtext))
27230 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27231 else if (EQ (pointer, intern ("hdrag")))
27232 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27233 #ifdef HAVE_X_WINDOWS
27234 else if (EQ (pointer, intern ("vdrag")))
27235 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27236 #endif
27237 else if (EQ (pointer, intern ("hourglass")))
27238 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27239 else if (EQ (pointer, Qmodeline))
27240 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27241 else
27242 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27243 }
27244
27245 if (cursor != No_Cursor)
27246 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27247 }
27248
27249 #endif /* HAVE_WINDOW_SYSTEM */
27250
27251 /* Take proper action when mouse has moved to the mode or header line
27252 or marginal area AREA of window W, x-position X and y-position Y.
27253 X is relative to the start of the text display area of W, so the
27254 width of bitmap areas and scroll bars must be subtracted to get a
27255 position relative to the start of the mode line. */
27256
27257 static void
27258 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27259 enum window_part area)
27260 {
27261 struct window *w = XWINDOW (window);
27262 struct frame *f = XFRAME (w->frame);
27263 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27264 #ifdef HAVE_WINDOW_SYSTEM
27265 Display_Info *dpyinfo;
27266 #endif
27267 Cursor cursor = No_Cursor;
27268 Lisp_Object pointer = Qnil;
27269 int dx, dy, width, height;
27270 ptrdiff_t charpos;
27271 Lisp_Object string, object = Qnil;
27272 Lisp_Object pos IF_LINT (= Qnil), help;
27273
27274 Lisp_Object mouse_face;
27275 int original_x_pixel = x;
27276 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27277 struct glyph_row *row IF_LINT (= 0);
27278
27279 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27280 {
27281 int x0;
27282 struct glyph *end;
27283
27284 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27285 returns them in row/column units! */
27286 string = mode_line_string (w, area, &x, &y, &charpos,
27287 &object, &dx, &dy, &width, &height);
27288
27289 row = (area == ON_MODE_LINE
27290 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27291 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27292
27293 /* Find the glyph under the mouse pointer. */
27294 if (row->mode_line_p && row->enabled_p)
27295 {
27296 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27297 end = glyph + row->used[TEXT_AREA];
27298
27299 for (x0 = original_x_pixel;
27300 glyph < end && x0 >= glyph->pixel_width;
27301 ++glyph)
27302 x0 -= glyph->pixel_width;
27303
27304 if (glyph >= end)
27305 glyph = NULL;
27306 }
27307 }
27308 else
27309 {
27310 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27311 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27312 returns them in row/column units! */
27313 string = marginal_area_string (w, area, &x, &y, &charpos,
27314 &object, &dx, &dy, &width, &height);
27315 }
27316
27317 help = Qnil;
27318
27319 #ifdef HAVE_WINDOW_SYSTEM
27320 if (IMAGEP (object))
27321 {
27322 Lisp_Object image_map, hotspot;
27323 if ((image_map = Fplist_get (XCDR (object), QCmap),
27324 !NILP (image_map))
27325 && (hotspot = find_hot_spot (image_map, dx, dy),
27326 CONSP (hotspot))
27327 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27328 {
27329 Lisp_Object plist;
27330
27331 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27332 If so, we could look for mouse-enter, mouse-leave
27333 properties in PLIST (and do something...). */
27334 hotspot = XCDR (hotspot);
27335 if (CONSP (hotspot)
27336 && (plist = XCAR (hotspot), CONSP (plist)))
27337 {
27338 pointer = Fplist_get (plist, Qpointer);
27339 if (NILP (pointer))
27340 pointer = Qhand;
27341 help = Fplist_get (plist, Qhelp_echo);
27342 if (!NILP (help))
27343 {
27344 help_echo_string = help;
27345 XSETWINDOW (help_echo_window, w);
27346 help_echo_object = w->buffer;
27347 help_echo_pos = charpos;
27348 }
27349 }
27350 }
27351 if (NILP (pointer))
27352 pointer = Fplist_get (XCDR (object), QCpointer);
27353 }
27354 #endif /* HAVE_WINDOW_SYSTEM */
27355
27356 if (STRINGP (string))
27357 pos = make_number (charpos);
27358
27359 /* Set the help text and mouse pointer. If the mouse is on a part
27360 of the mode line without any text (e.g. past the right edge of
27361 the mode line text), use the default help text and pointer. */
27362 if (STRINGP (string) || area == ON_MODE_LINE)
27363 {
27364 /* Arrange to display the help by setting the global variables
27365 help_echo_string, help_echo_object, and help_echo_pos. */
27366 if (NILP (help))
27367 {
27368 if (STRINGP (string))
27369 help = Fget_text_property (pos, Qhelp_echo, string);
27370
27371 if (!NILP (help))
27372 {
27373 help_echo_string = help;
27374 XSETWINDOW (help_echo_window, w);
27375 help_echo_object = string;
27376 help_echo_pos = charpos;
27377 }
27378 else if (area == ON_MODE_LINE)
27379 {
27380 Lisp_Object default_help
27381 = buffer_local_value_1 (Qmode_line_default_help_echo,
27382 w->buffer);
27383
27384 if (STRINGP (default_help))
27385 {
27386 help_echo_string = default_help;
27387 XSETWINDOW (help_echo_window, w);
27388 help_echo_object = Qnil;
27389 help_echo_pos = -1;
27390 }
27391 }
27392 }
27393
27394 #ifdef HAVE_WINDOW_SYSTEM
27395 /* Change the mouse pointer according to what is under it. */
27396 if (FRAME_WINDOW_P (f))
27397 {
27398 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27399 if (STRINGP (string))
27400 {
27401 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27402
27403 if (NILP (pointer))
27404 pointer = Fget_text_property (pos, Qpointer, string);
27405
27406 /* Change the mouse pointer according to what is under X/Y. */
27407 if (NILP (pointer)
27408 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27409 {
27410 Lisp_Object map;
27411 map = Fget_text_property (pos, Qlocal_map, string);
27412 if (!KEYMAPP (map))
27413 map = Fget_text_property (pos, Qkeymap, string);
27414 if (!KEYMAPP (map))
27415 cursor = dpyinfo->vertical_scroll_bar_cursor;
27416 }
27417 }
27418 else
27419 /* Default mode-line pointer. */
27420 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27421 }
27422 #endif
27423 }
27424
27425 /* Change the mouse face according to what is under X/Y. */
27426 if (STRINGP (string))
27427 {
27428 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27429 if (!NILP (mouse_face)
27430 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27431 && glyph)
27432 {
27433 Lisp_Object b, e;
27434
27435 struct glyph * tmp_glyph;
27436
27437 int gpos;
27438 int gseq_length;
27439 int total_pixel_width;
27440 ptrdiff_t begpos, endpos, ignore;
27441
27442 int vpos, hpos;
27443
27444 b = Fprevious_single_property_change (make_number (charpos + 1),
27445 Qmouse_face, string, Qnil);
27446 if (NILP (b))
27447 begpos = 0;
27448 else
27449 begpos = XINT (b);
27450
27451 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27452 if (NILP (e))
27453 endpos = SCHARS (string);
27454 else
27455 endpos = XINT (e);
27456
27457 /* Calculate the glyph position GPOS of GLYPH in the
27458 displayed string, relative to the beginning of the
27459 highlighted part of the string.
27460
27461 Note: GPOS is different from CHARPOS. CHARPOS is the
27462 position of GLYPH in the internal string object. A mode
27463 line string format has structures which are converted to
27464 a flattened string by the Emacs Lisp interpreter. The
27465 internal string is an element of those structures. The
27466 displayed string is the flattened string. */
27467 tmp_glyph = row_start_glyph;
27468 while (tmp_glyph < glyph
27469 && (!(EQ (tmp_glyph->object, glyph->object)
27470 && begpos <= tmp_glyph->charpos
27471 && tmp_glyph->charpos < endpos)))
27472 tmp_glyph++;
27473 gpos = glyph - tmp_glyph;
27474
27475 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27476 the highlighted part of the displayed string to which
27477 GLYPH belongs. Note: GSEQ_LENGTH is different from
27478 SCHARS (STRING), because the latter returns the length of
27479 the internal string. */
27480 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27481 tmp_glyph > glyph
27482 && (!(EQ (tmp_glyph->object, glyph->object)
27483 && begpos <= tmp_glyph->charpos
27484 && tmp_glyph->charpos < endpos));
27485 tmp_glyph--)
27486 ;
27487 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27488
27489 /* Calculate the total pixel width of all the glyphs between
27490 the beginning of the highlighted area and GLYPH. */
27491 total_pixel_width = 0;
27492 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27493 total_pixel_width += tmp_glyph->pixel_width;
27494
27495 /* Pre calculation of re-rendering position. Note: X is in
27496 column units here, after the call to mode_line_string or
27497 marginal_area_string. */
27498 hpos = x - gpos;
27499 vpos = (area == ON_MODE_LINE
27500 ? (w->current_matrix)->nrows - 1
27501 : 0);
27502
27503 /* If GLYPH's position is included in the region that is
27504 already drawn in mouse face, we have nothing to do. */
27505 if ( EQ (window, hlinfo->mouse_face_window)
27506 && (!row->reversed_p
27507 ? (hlinfo->mouse_face_beg_col <= hpos
27508 && hpos < hlinfo->mouse_face_end_col)
27509 /* In R2L rows we swap BEG and END, see below. */
27510 : (hlinfo->mouse_face_end_col <= hpos
27511 && hpos < hlinfo->mouse_face_beg_col))
27512 && hlinfo->mouse_face_beg_row == vpos )
27513 return;
27514
27515 if (clear_mouse_face (hlinfo))
27516 cursor = No_Cursor;
27517
27518 if (!row->reversed_p)
27519 {
27520 hlinfo->mouse_face_beg_col = hpos;
27521 hlinfo->mouse_face_beg_x = original_x_pixel
27522 - (total_pixel_width + dx);
27523 hlinfo->mouse_face_end_col = hpos + gseq_length;
27524 hlinfo->mouse_face_end_x = 0;
27525 }
27526 else
27527 {
27528 /* In R2L rows, show_mouse_face expects BEG and END
27529 coordinates to be swapped. */
27530 hlinfo->mouse_face_end_col = hpos;
27531 hlinfo->mouse_face_end_x = original_x_pixel
27532 - (total_pixel_width + dx);
27533 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27534 hlinfo->mouse_face_beg_x = 0;
27535 }
27536
27537 hlinfo->mouse_face_beg_row = vpos;
27538 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27539 hlinfo->mouse_face_beg_y = 0;
27540 hlinfo->mouse_face_end_y = 0;
27541 hlinfo->mouse_face_past_end = 0;
27542 hlinfo->mouse_face_window = window;
27543
27544 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27545 charpos,
27546 0, 0, 0,
27547 &ignore,
27548 glyph->face_id,
27549 1);
27550 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27551
27552 if (NILP (pointer))
27553 pointer = Qhand;
27554 }
27555 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27556 clear_mouse_face (hlinfo);
27557 }
27558 #ifdef HAVE_WINDOW_SYSTEM
27559 if (FRAME_WINDOW_P (f))
27560 define_frame_cursor1 (f, cursor, pointer);
27561 #endif
27562 }
27563
27564
27565 /* EXPORT:
27566 Take proper action when the mouse has moved to position X, Y on
27567 frame F as regards highlighting characters that have mouse-face
27568 properties. Also de-highlighting chars where the mouse was before.
27569 X and Y can be negative or out of range. */
27570
27571 void
27572 note_mouse_highlight (struct frame *f, int x, int y)
27573 {
27574 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27575 enum window_part part = ON_NOTHING;
27576 Lisp_Object window;
27577 struct window *w;
27578 Cursor cursor = No_Cursor;
27579 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27580 struct buffer *b;
27581
27582 /* When a menu is active, don't highlight because this looks odd. */
27583 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27584 if (popup_activated ())
27585 return;
27586 #endif
27587
27588 if (NILP (Vmouse_highlight)
27589 || !f->glyphs_initialized_p
27590 || f->pointer_invisible)
27591 return;
27592
27593 hlinfo->mouse_face_mouse_x = x;
27594 hlinfo->mouse_face_mouse_y = y;
27595 hlinfo->mouse_face_mouse_frame = f;
27596
27597 if (hlinfo->mouse_face_defer)
27598 return;
27599
27600 if (gc_in_progress)
27601 {
27602 hlinfo->mouse_face_deferred_gc = 1;
27603 return;
27604 }
27605
27606 /* Which window is that in? */
27607 window = window_from_coordinates (f, x, y, &part, 1);
27608
27609 /* If displaying active text in another window, clear that. */
27610 if (! EQ (window, hlinfo->mouse_face_window)
27611 /* Also clear if we move out of text area in same window. */
27612 || (!NILP (hlinfo->mouse_face_window)
27613 && !NILP (window)
27614 && part != ON_TEXT
27615 && part != ON_MODE_LINE
27616 && part != ON_HEADER_LINE))
27617 clear_mouse_face (hlinfo);
27618
27619 /* Not on a window -> return. */
27620 if (!WINDOWP (window))
27621 return;
27622
27623 /* Reset help_echo_string. It will get recomputed below. */
27624 help_echo_string = Qnil;
27625
27626 /* Convert to window-relative pixel coordinates. */
27627 w = XWINDOW (window);
27628 frame_to_window_pixel_xy (w, &x, &y);
27629
27630 #ifdef HAVE_WINDOW_SYSTEM
27631 /* Handle tool-bar window differently since it doesn't display a
27632 buffer. */
27633 if (EQ (window, FVAR (f, tool_bar_window)))
27634 {
27635 note_tool_bar_highlight (f, x, y);
27636 return;
27637 }
27638 #endif
27639
27640 /* Mouse is on the mode, header line or margin? */
27641 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27642 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27643 {
27644 note_mode_line_or_margin_highlight (window, x, y, part);
27645 return;
27646 }
27647
27648 #ifdef HAVE_WINDOW_SYSTEM
27649 if (part == ON_VERTICAL_BORDER)
27650 {
27651 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27652 help_echo_string = build_string ("drag-mouse-1: resize");
27653 }
27654 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27655 || part == ON_SCROLL_BAR)
27656 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27657 else
27658 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27659 #endif
27660
27661 /* Are we in a window whose display is up to date?
27662 And verify the buffer's text has not changed. */
27663 b = XBUFFER (w->buffer);
27664 if (part == ON_TEXT
27665 && EQ (w->window_end_valid, w->buffer)
27666 && w->last_modified == BUF_MODIFF (b)
27667 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27668 {
27669 int hpos, vpos, dx, dy, area = LAST_AREA;
27670 ptrdiff_t pos;
27671 struct glyph *glyph;
27672 Lisp_Object object;
27673 Lisp_Object mouse_face = Qnil, position;
27674 Lisp_Object *overlay_vec = NULL;
27675 ptrdiff_t i, noverlays;
27676 struct buffer *obuf;
27677 ptrdiff_t obegv, ozv;
27678 int same_region;
27679
27680 /* Find the glyph under X/Y. */
27681 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27682
27683 #ifdef HAVE_WINDOW_SYSTEM
27684 /* Look for :pointer property on image. */
27685 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27686 {
27687 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27688 if (img != NULL && IMAGEP (img->spec))
27689 {
27690 Lisp_Object image_map, hotspot;
27691 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27692 !NILP (image_map))
27693 && (hotspot = find_hot_spot (image_map,
27694 glyph->slice.img.x + dx,
27695 glyph->slice.img.y + dy),
27696 CONSP (hotspot))
27697 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27698 {
27699 Lisp_Object plist;
27700
27701 /* Could check XCAR (hotspot) to see if we enter/leave
27702 this hot-spot.
27703 If so, we could look for mouse-enter, mouse-leave
27704 properties in PLIST (and do something...). */
27705 hotspot = XCDR (hotspot);
27706 if (CONSP (hotspot)
27707 && (plist = XCAR (hotspot), CONSP (plist)))
27708 {
27709 pointer = Fplist_get (plist, Qpointer);
27710 if (NILP (pointer))
27711 pointer = Qhand;
27712 help_echo_string = Fplist_get (plist, Qhelp_echo);
27713 if (!NILP (help_echo_string))
27714 {
27715 help_echo_window = window;
27716 help_echo_object = glyph->object;
27717 help_echo_pos = glyph->charpos;
27718 }
27719 }
27720 }
27721 if (NILP (pointer))
27722 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27723 }
27724 }
27725 #endif /* HAVE_WINDOW_SYSTEM */
27726
27727 /* Clear mouse face if X/Y not over text. */
27728 if (glyph == NULL
27729 || area != TEXT_AREA
27730 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27731 /* Glyph's OBJECT is an integer for glyphs inserted by the
27732 display engine for its internal purposes, like truncation
27733 and continuation glyphs and blanks beyond the end of
27734 line's text on text terminals. If we are over such a
27735 glyph, we are not over any text. */
27736 || INTEGERP (glyph->object)
27737 /* R2L rows have a stretch glyph at their front, which
27738 stands for no text, whereas L2R rows have no glyphs at
27739 all beyond the end of text. Treat such stretch glyphs
27740 like we do with NULL glyphs in L2R rows. */
27741 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27742 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27743 && glyph->type == STRETCH_GLYPH
27744 && glyph->avoid_cursor_p))
27745 {
27746 if (clear_mouse_face (hlinfo))
27747 cursor = No_Cursor;
27748 #ifdef HAVE_WINDOW_SYSTEM
27749 if (FRAME_WINDOW_P (f) && NILP (pointer))
27750 {
27751 if (area != TEXT_AREA)
27752 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27753 else
27754 pointer = Vvoid_text_area_pointer;
27755 }
27756 #endif
27757 goto set_cursor;
27758 }
27759
27760 pos = glyph->charpos;
27761 object = glyph->object;
27762 if (!STRINGP (object) && !BUFFERP (object))
27763 goto set_cursor;
27764
27765 /* If we get an out-of-range value, return now; avoid an error. */
27766 if (BUFFERP (object) && pos > BUF_Z (b))
27767 goto set_cursor;
27768
27769 /* Make the window's buffer temporarily current for
27770 overlays_at and compute_char_face. */
27771 obuf = current_buffer;
27772 current_buffer = b;
27773 obegv = BEGV;
27774 ozv = ZV;
27775 BEGV = BEG;
27776 ZV = Z;
27777
27778 /* Is this char mouse-active or does it have help-echo? */
27779 position = make_number (pos);
27780
27781 if (BUFFERP (object))
27782 {
27783 /* Put all the overlays we want in a vector in overlay_vec. */
27784 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27785 /* Sort overlays into increasing priority order. */
27786 noverlays = sort_overlays (overlay_vec, noverlays, w);
27787 }
27788 else
27789 noverlays = 0;
27790
27791 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27792
27793 if (same_region)
27794 cursor = No_Cursor;
27795
27796 /* Check mouse-face highlighting. */
27797 if (! same_region
27798 /* If there exists an overlay with mouse-face overlapping
27799 the one we are currently highlighting, we have to
27800 check if we enter the overlapping overlay, and then
27801 highlight only that. */
27802 || (OVERLAYP (hlinfo->mouse_face_overlay)
27803 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27804 {
27805 /* Find the highest priority overlay with a mouse-face. */
27806 Lisp_Object overlay = Qnil;
27807 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27808 {
27809 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27810 if (!NILP (mouse_face))
27811 overlay = overlay_vec[i];
27812 }
27813
27814 /* If we're highlighting the same overlay as before, there's
27815 no need to do that again. */
27816 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27817 goto check_help_echo;
27818 hlinfo->mouse_face_overlay = overlay;
27819
27820 /* Clear the display of the old active region, if any. */
27821 if (clear_mouse_face (hlinfo))
27822 cursor = No_Cursor;
27823
27824 /* If no overlay applies, get a text property. */
27825 if (NILP (overlay))
27826 mouse_face = Fget_text_property (position, Qmouse_face, object);
27827
27828 /* Next, compute the bounds of the mouse highlighting and
27829 display it. */
27830 if (!NILP (mouse_face) && STRINGP (object))
27831 {
27832 /* The mouse-highlighting comes from a display string
27833 with a mouse-face. */
27834 Lisp_Object s, e;
27835 ptrdiff_t ignore;
27836
27837 s = Fprevious_single_property_change
27838 (make_number (pos + 1), Qmouse_face, object, Qnil);
27839 e = Fnext_single_property_change
27840 (position, Qmouse_face, object, Qnil);
27841 if (NILP (s))
27842 s = make_number (0);
27843 if (NILP (e))
27844 e = make_number (SCHARS (object) - 1);
27845 mouse_face_from_string_pos (w, hlinfo, object,
27846 XINT (s), XINT (e));
27847 hlinfo->mouse_face_past_end = 0;
27848 hlinfo->mouse_face_window = window;
27849 hlinfo->mouse_face_face_id
27850 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27851 glyph->face_id, 1);
27852 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27853 cursor = No_Cursor;
27854 }
27855 else
27856 {
27857 /* The mouse-highlighting, if any, comes from an overlay
27858 or text property in the buffer. */
27859 Lisp_Object buffer IF_LINT (= Qnil);
27860 Lisp_Object disp_string IF_LINT (= Qnil);
27861
27862 if (STRINGP (object))
27863 {
27864 /* If we are on a display string with no mouse-face,
27865 check if the text under it has one. */
27866 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27867 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27868 pos = string_buffer_position (object, start);
27869 if (pos > 0)
27870 {
27871 mouse_face = get_char_property_and_overlay
27872 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27873 buffer = w->buffer;
27874 disp_string = object;
27875 }
27876 }
27877 else
27878 {
27879 buffer = object;
27880 disp_string = Qnil;
27881 }
27882
27883 if (!NILP (mouse_face))
27884 {
27885 Lisp_Object before, after;
27886 Lisp_Object before_string, after_string;
27887 /* To correctly find the limits of mouse highlight
27888 in a bidi-reordered buffer, we must not use the
27889 optimization of limiting the search in
27890 previous-single-property-change and
27891 next-single-property-change, because
27892 rows_from_pos_range needs the real start and end
27893 positions to DTRT in this case. That's because
27894 the first row visible in a window does not
27895 necessarily display the character whose position
27896 is the smallest. */
27897 Lisp_Object lim1 =
27898 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27899 ? Fmarker_position (w->start)
27900 : Qnil;
27901 Lisp_Object lim2 =
27902 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27903 ? make_number (BUF_Z (XBUFFER (buffer))
27904 - XFASTINT (w->window_end_pos))
27905 : Qnil;
27906
27907 if (NILP (overlay))
27908 {
27909 /* Handle the text property case. */
27910 before = Fprevious_single_property_change
27911 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27912 after = Fnext_single_property_change
27913 (make_number (pos), Qmouse_face, buffer, lim2);
27914 before_string = after_string = Qnil;
27915 }
27916 else
27917 {
27918 /* Handle the overlay case. */
27919 before = Foverlay_start (overlay);
27920 after = Foverlay_end (overlay);
27921 before_string = Foverlay_get (overlay, Qbefore_string);
27922 after_string = Foverlay_get (overlay, Qafter_string);
27923
27924 if (!STRINGP (before_string)) before_string = Qnil;
27925 if (!STRINGP (after_string)) after_string = Qnil;
27926 }
27927
27928 mouse_face_from_buffer_pos (window, hlinfo, pos,
27929 NILP (before)
27930 ? 1
27931 : XFASTINT (before),
27932 NILP (after)
27933 ? BUF_Z (XBUFFER (buffer))
27934 : XFASTINT (after),
27935 before_string, after_string,
27936 disp_string);
27937 cursor = No_Cursor;
27938 }
27939 }
27940 }
27941
27942 check_help_echo:
27943
27944 /* Look for a `help-echo' property. */
27945 if (NILP (help_echo_string)) {
27946 Lisp_Object help, overlay;
27947
27948 /* Check overlays first. */
27949 help = overlay = Qnil;
27950 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27951 {
27952 overlay = overlay_vec[i];
27953 help = Foverlay_get (overlay, Qhelp_echo);
27954 }
27955
27956 if (!NILP (help))
27957 {
27958 help_echo_string = help;
27959 help_echo_window = window;
27960 help_echo_object = overlay;
27961 help_echo_pos = pos;
27962 }
27963 else
27964 {
27965 Lisp_Object obj = glyph->object;
27966 ptrdiff_t charpos = glyph->charpos;
27967
27968 /* Try text properties. */
27969 if (STRINGP (obj)
27970 && charpos >= 0
27971 && charpos < SCHARS (obj))
27972 {
27973 help = Fget_text_property (make_number (charpos),
27974 Qhelp_echo, obj);
27975 if (NILP (help))
27976 {
27977 /* If the string itself doesn't specify a help-echo,
27978 see if the buffer text ``under'' it does. */
27979 struct glyph_row *r
27980 = MATRIX_ROW (w->current_matrix, vpos);
27981 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27982 ptrdiff_t p = string_buffer_position (obj, start);
27983 if (p > 0)
27984 {
27985 help = Fget_char_property (make_number (p),
27986 Qhelp_echo, w->buffer);
27987 if (!NILP (help))
27988 {
27989 charpos = p;
27990 obj = w->buffer;
27991 }
27992 }
27993 }
27994 }
27995 else if (BUFFERP (obj)
27996 && charpos >= BEGV
27997 && charpos < ZV)
27998 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27999 obj);
28000
28001 if (!NILP (help))
28002 {
28003 help_echo_string = help;
28004 help_echo_window = window;
28005 help_echo_object = obj;
28006 help_echo_pos = charpos;
28007 }
28008 }
28009 }
28010
28011 #ifdef HAVE_WINDOW_SYSTEM
28012 /* Look for a `pointer' property. */
28013 if (FRAME_WINDOW_P (f) && NILP (pointer))
28014 {
28015 /* Check overlays first. */
28016 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28017 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28018
28019 if (NILP (pointer))
28020 {
28021 Lisp_Object obj = glyph->object;
28022 ptrdiff_t charpos = glyph->charpos;
28023
28024 /* Try text properties. */
28025 if (STRINGP (obj)
28026 && charpos >= 0
28027 && charpos < SCHARS (obj))
28028 {
28029 pointer = Fget_text_property (make_number (charpos),
28030 Qpointer, obj);
28031 if (NILP (pointer))
28032 {
28033 /* If the string itself doesn't specify a pointer,
28034 see if the buffer text ``under'' it does. */
28035 struct glyph_row *r
28036 = MATRIX_ROW (w->current_matrix, vpos);
28037 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28038 ptrdiff_t p = string_buffer_position (obj, start);
28039 if (p > 0)
28040 pointer = Fget_char_property (make_number (p),
28041 Qpointer, w->buffer);
28042 }
28043 }
28044 else if (BUFFERP (obj)
28045 && charpos >= BEGV
28046 && charpos < ZV)
28047 pointer = Fget_text_property (make_number (charpos),
28048 Qpointer, obj);
28049 }
28050 }
28051 #endif /* HAVE_WINDOW_SYSTEM */
28052
28053 BEGV = obegv;
28054 ZV = ozv;
28055 current_buffer = obuf;
28056 }
28057
28058 set_cursor:
28059
28060 #ifdef HAVE_WINDOW_SYSTEM
28061 if (FRAME_WINDOW_P (f))
28062 define_frame_cursor1 (f, cursor, pointer);
28063 #else
28064 /* This is here to prevent a compiler error, about "label at end of
28065 compound statement". */
28066 return;
28067 #endif
28068 }
28069
28070
28071 /* EXPORT for RIF:
28072 Clear any mouse-face on window W. This function is part of the
28073 redisplay interface, and is called from try_window_id and similar
28074 functions to ensure the mouse-highlight is off. */
28075
28076 void
28077 x_clear_window_mouse_face (struct window *w)
28078 {
28079 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28080 Lisp_Object window;
28081
28082 BLOCK_INPUT;
28083 XSETWINDOW (window, w);
28084 if (EQ (window, hlinfo->mouse_face_window))
28085 clear_mouse_face (hlinfo);
28086 UNBLOCK_INPUT;
28087 }
28088
28089
28090 /* EXPORT:
28091 Just discard the mouse face information for frame F, if any.
28092 This is used when the size of F is changed. */
28093
28094 void
28095 cancel_mouse_face (struct frame *f)
28096 {
28097 Lisp_Object window;
28098 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28099
28100 window = hlinfo->mouse_face_window;
28101 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28102 {
28103 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28104 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28105 hlinfo->mouse_face_window = Qnil;
28106 }
28107 }
28108
28109
28110 \f
28111 /***********************************************************************
28112 Exposure Events
28113 ***********************************************************************/
28114
28115 #ifdef HAVE_WINDOW_SYSTEM
28116
28117 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28118 which intersects rectangle R. R is in window-relative coordinates. */
28119
28120 static void
28121 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28122 enum glyph_row_area area)
28123 {
28124 struct glyph *first = row->glyphs[area];
28125 struct glyph *end = row->glyphs[area] + row->used[area];
28126 struct glyph *last;
28127 int first_x, start_x, x;
28128
28129 if (area == TEXT_AREA && row->fill_line_p)
28130 /* If row extends face to end of line write the whole line. */
28131 draw_glyphs (w, 0, row, area,
28132 0, row->used[area],
28133 DRAW_NORMAL_TEXT, 0);
28134 else
28135 {
28136 /* Set START_X to the window-relative start position for drawing glyphs of
28137 AREA. The first glyph of the text area can be partially visible.
28138 The first glyphs of other areas cannot. */
28139 start_x = window_box_left_offset (w, area);
28140 x = start_x;
28141 if (area == TEXT_AREA)
28142 x += row->x;
28143
28144 /* Find the first glyph that must be redrawn. */
28145 while (first < end
28146 && x + first->pixel_width < r->x)
28147 {
28148 x += first->pixel_width;
28149 ++first;
28150 }
28151
28152 /* Find the last one. */
28153 last = first;
28154 first_x = x;
28155 while (last < end
28156 && x < r->x + r->width)
28157 {
28158 x += last->pixel_width;
28159 ++last;
28160 }
28161
28162 /* Repaint. */
28163 if (last > first)
28164 draw_glyphs (w, first_x - start_x, row, area,
28165 first - row->glyphs[area], last - row->glyphs[area],
28166 DRAW_NORMAL_TEXT, 0);
28167 }
28168 }
28169
28170
28171 /* Redraw the parts of the glyph row ROW on window W intersecting
28172 rectangle R. R is in window-relative coordinates. Value is
28173 non-zero if mouse-face was overwritten. */
28174
28175 static int
28176 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28177 {
28178 eassert (row->enabled_p);
28179
28180 if (row->mode_line_p || w->pseudo_window_p)
28181 draw_glyphs (w, 0, row, TEXT_AREA,
28182 0, row->used[TEXT_AREA],
28183 DRAW_NORMAL_TEXT, 0);
28184 else
28185 {
28186 if (row->used[LEFT_MARGIN_AREA])
28187 expose_area (w, row, r, LEFT_MARGIN_AREA);
28188 if (row->used[TEXT_AREA])
28189 expose_area (w, row, r, TEXT_AREA);
28190 if (row->used[RIGHT_MARGIN_AREA])
28191 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28192 draw_row_fringe_bitmaps (w, row);
28193 }
28194
28195 return row->mouse_face_p;
28196 }
28197
28198
28199 /* Redraw those parts of glyphs rows during expose event handling that
28200 overlap other rows. Redrawing of an exposed line writes over parts
28201 of lines overlapping that exposed line; this function fixes that.
28202
28203 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28204 row in W's current matrix that is exposed and overlaps other rows.
28205 LAST_OVERLAPPING_ROW is the last such row. */
28206
28207 static void
28208 expose_overlaps (struct window *w,
28209 struct glyph_row *first_overlapping_row,
28210 struct glyph_row *last_overlapping_row,
28211 XRectangle *r)
28212 {
28213 struct glyph_row *row;
28214
28215 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28216 if (row->overlapping_p)
28217 {
28218 eassert (row->enabled_p && !row->mode_line_p);
28219
28220 row->clip = r;
28221 if (row->used[LEFT_MARGIN_AREA])
28222 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28223
28224 if (row->used[TEXT_AREA])
28225 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28226
28227 if (row->used[RIGHT_MARGIN_AREA])
28228 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28229 row->clip = NULL;
28230 }
28231 }
28232
28233
28234 /* Return non-zero if W's cursor intersects rectangle R. */
28235
28236 static int
28237 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28238 {
28239 XRectangle cr, result;
28240 struct glyph *cursor_glyph;
28241 struct glyph_row *row;
28242
28243 if (w->phys_cursor.vpos >= 0
28244 && w->phys_cursor.vpos < w->current_matrix->nrows
28245 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28246 row->enabled_p)
28247 && row->cursor_in_fringe_p)
28248 {
28249 /* Cursor is in the fringe. */
28250 cr.x = window_box_right_offset (w,
28251 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28252 ? RIGHT_MARGIN_AREA
28253 : TEXT_AREA));
28254 cr.y = row->y;
28255 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28256 cr.height = row->height;
28257 return x_intersect_rectangles (&cr, r, &result);
28258 }
28259
28260 cursor_glyph = get_phys_cursor_glyph (w);
28261 if (cursor_glyph)
28262 {
28263 /* r is relative to W's box, but w->phys_cursor.x is relative
28264 to left edge of W's TEXT area. Adjust it. */
28265 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28266 cr.y = w->phys_cursor.y;
28267 cr.width = cursor_glyph->pixel_width;
28268 cr.height = w->phys_cursor_height;
28269 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28270 I assume the effect is the same -- and this is portable. */
28271 return x_intersect_rectangles (&cr, r, &result);
28272 }
28273 /* If we don't understand the format, pretend we're not in the hot-spot. */
28274 return 0;
28275 }
28276
28277
28278 /* EXPORT:
28279 Draw a vertical window border to the right of window W if W doesn't
28280 have vertical scroll bars. */
28281
28282 void
28283 x_draw_vertical_border (struct window *w)
28284 {
28285 struct frame *f = XFRAME (WINDOW_FRAME (w));
28286
28287 /* We could do better, if we knew what type of scroll-bar the adjacent
28288 windows (on either side) have... But we don't :-(
28289 However, I think this works ok. ++KFS 2003-04-25 */
28290
28291 /* Redraw borders between horizontally adjacent windows. Don't
28292 do it for frames with vertical scroll bars because either the
28293 right scroll bar of a window, or the left scroll bar of its
28294 neighbor will suffice as a border. */
28295 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28296 return;
28297
28298 if (!WINDOW_RIGHTMOST_P (w)
28299 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28300 {
28301 int x0, x1, y0, y1;
28302
28303 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28304 y1 -= 1;
28305
28306 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28307 x1 -= 1;
28308
28309 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28310 }
28311 else if (!WINDOW_LEFTMOST_P (w)
28312 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28313 {
28314 int x0, x1, y0, y1;
28315
28316 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28317 y1 -= 1;
28318
28319 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28320 x0 -= 1;
28321
28322 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28323 }
28324 }
28325
28326
28327 /* Redraw the part of window W intersection rectangle FR. Pixel
28328 coordinates in FR are frame-relative. Call this function with
28329 input blocked. Value is non-zero if the exposure overwrites
28330 mouse-face. */
28331
28332 static int
28333 expose_window (struct window *w, XRectangle *fr)
28334 {
28335 struct frame *f = XFRAME (w->frame);
28336 XRectangle wr, r;
28337 int mouse_face_overwritten_p = 0;
28338
28339 /* If window is not yet fully initialized, do nothing. This can
28340 happen when toolkit scroll bars are used and a window is split.
28341 Reconfiguring the scroll bar will generate an expose for a newly
28342 created window. */
28343 if (w->current_matrix == NULL)
28344 return 0;
28345
28346 /* When we're currently updating the window, display and current
28347 matrix usually don't agree. Arrange for a thorough display
28348 later. */
28349 if (w == updated_window)
28350 {
28351 SET_FRAME_GARBAGED (f);
28352 return 0;
28353 }
28354
28355 /* Frame-relative pixel rectangle of W. */
28356 wr.x = WINDOW_LEFT_EDGE_X (w);
28357 wr.y = WINDOW_TOP_EDGE_Y (w);
28358 wr.width = WINDOW_TOTAL_WIDTH (w);
28359 wr.height = WINDOW_TOTAL_HEIGHT (w);
28360
28361 if (x_intersect_rectangles (fr, &wr, &r))
28362 {
28363 int yb = window_text_bottom_y (w);
28364 struct glyph_row *row;
28365 int cursor_cleared_p, phys_cursor_on_p;
28366 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28367
28368 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28369 r.x, r.y, r.width, r.height));
28370
28371 /* Convert to window coordinates. */
28372 r.x -= WINDOW_LEFT_EDGE_X (w);
28373 r.y -= WINDOW_TOP_EDGE_Y (w);
28374
28375 /* Turn off the cursor. */
28376 if (!w->pseudo_window_p
28377 && phys_cursor_in_rect_p (w, &r))
28378 {
28379 x_clear_cursor (w);
28380 cursor_cleared_p = 1;
28381 }
28382 else
28383 cursor_cleared_p = 0;
28384
28385 /* If the row containing the cursor extends face to end of line,
28386 then expose_area might overwrite the cursor outside the
28387 rectangle and thus notice_overwritten_cursor might clear
28388 w->phys_cursor_on_p. We remember the original value and
28389 check later if it is changed. */
28390 phys_cursor_on_p = w->phys_cursor_on_p;
28391
28392 /* Update lines intersecting rectangle R. */
28393 first_overlapping_row = last_overlapping_row = NULL;
28394 for (row = w->current_matrix->rows;
28395 row->enabled_p;
28396 ++row)
28397 {
28398 int y0 = row->y;
28399 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28400
28401 if ((y0 >= r.y && y0 < r.y + r.height)
28402 || (y1 > r.y && y1 < r.y + r.height)
28403 || (r.y >= y0 && r.y < y1)
28404 || (r.y + r.height > y0 && r.y + r.height < y1))
28405 {
28406 /* A header line may be overlapping, but there is no need
28407 to fix overlapping areas for them. KFS 2005-02-12 */
28408 if (row->overlapping_p && !row->mode_line_p)
28409 {
28410 if (first_overlapping_row == NULL)
28411 first_overlapping_row = row;
28412 last_overlapping_row = row;
28413 }
28414
28415 row->clip = fr;
28416 if (expose_line (w, row, &r))
28417 mouse_face_overwritten_p = 1;
28418 row->clip = NULL;
28419 }
28420 else if (row->overlapping_p)
28421 {
28422 /* We must redraw a row overlapping the exposed area. */
28423 if (y0 < r.y
28424 ? y0 + row->phys_height > r.y
28425 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28426 {
28427 if (first_overlapping_row == NULL)
28428 first_overlapping_row = row;
28429 last_overlapping_row = row;
28430 }
28431 }
28432
28433 if (y1 >= yb)
28434 break;
28435 }
28436
28437 /* Display the mode line if there is one. */
28438 if (WINDOW_WANTS_MODELINE_P (w)
28439 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28440 row->enabled_p)
28441 && row->y < r.y + r.height)
28442 {
28443 if (expose_line (w, row, &r))
28444 mouse_face_overwritten_p = 1;
28445 }
28446
28447 if (!w->pseudo_window_p)
28448 {
28449 /* Fix the display of overlapping rows. */
28450 if (first_overlapping_row)
28451 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28452 fr);
28453
28454 /* Draw border between windows. */
28455 x_draw_vertical_border (w);
28456
28457 /* Turn the cursor on again. */
28458 if (cursor_cleared_p
28459 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28460 update_window_cursor (w, 1);
28461 }
28462 }
28463
28464 return mouse_face_overwritten_p;
28465 }
28466
28467
28468
28469 /* Redraw (parts) of all windows in the window tree rooted at W that
28470 intersect R. R contains frame pixel coordinates. Value is
28471 non-zero if the exposure overwrites mouse-face. */
28472
28473 static int
28474 expose_window_tree (struct window *w, XRectangle *r)
28475 {
28476 struct frame *f = XFRAME (w->frame);
28477 int mouse_face_overwritten_p = 0;
28478
28479 while (w && !FRAME_GARBAGED_P (f))
28480 {
28481 if (!NILP (w->hchild))
28482 mouse_face_overwritten_p
28483 |= expose_window_tree (XWINDOW (w->hchild), r);
28484 else if (!NILP (w->vchild))
28485 mouse_face_overwritten_p
28486 |= expose_window_tree (XWINDOW (w->vchild), r);
28487 else
28488 mouse_face_overwritten_p |= expose_window (w, r);
28489
28490 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28491 }
28492
28493 return mouse_face_overwritten_p;
28494 }
28495
28496
28497 /* EXPORT:
28498 Redisplay an exposed area of frame F. X and Y are the upper-left
28499 corner of the exposed rectangle. W and H are width and height of
28500 the exposed area. All are pixel values. W or H zero means redraw
28501 the entire frame. */
28502
28503 void
28504 expose_frame (struct frame *f, int x, int y, int w, int h)
28505 {
28506 XRectangle r;
28507 int mouse_face_overwritten_p = 0;
28508
28509 TRACE ((stderr, "expose_frame "));
28510
28511 /* No need to redraw if frame will be redrawn soon. */
28512 if (FRAME_GARBAGED_P (f))
28513 {
28514 TRACE ((stderr, " garbaged\n"));
28515 return;
28516 }
28517
28518 /* If basic faces haven't been realized yet, there is no point in
28519 trying to redraw anything. This can happen when we get an expose
28520 event while Emacs is starting, e.g. by moving another window. */
28521 if (FRAME_FACE_CACHE (f) == NULL
28522 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28523 {
28524 TRACE ((stderr, " no faces\n"));
28525 return;
28526 }
28527
28528 if (w == 0 || h == 0)
28529 {
28530 r.x = r.y = 0;
28531 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28532 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28533 }
28534 else
28535 {
28536 r.x = x;
28537 r.y = y;
28538 r.width = w;
28539 r.height = h;
28540 }
28541
28542 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28543 mouse_face_overwritten_p = expose_window_tree (XWINDOW (FVAR (f, root_window)), &r);
28544
28545 if (WINDOWP (FVAR (f, tool_bar_window)))
28546 mouse_face_overwritten_p
28547 |= expose_window (XWINDOW (FVAR (f, tool_bar_window)), &r);
28548
28549 #ifdef HAVE_X_WINDOWS
28550 #ifndef MSDOS
28551 #ifndef USE_X_TOOLKIT
28552 if (WINDOWP (FVAR (f, menu_bar_window)))
28553 mouse_face_overwritten_p
28554 |= expose_window (XWINDOW (FVAR (f, menu_bar_window)), &r);
28555 #endif /* not USE_X_TOOLKIT */
28556 #endif
28557 #endif
28558
28559 /* Some window managers support a focus-follows-mouse style with
28560 delayed raising of frames. Imagine a partially obscured frame,
28561 and moving the mouse into partially obscured mouse-face on that
28562 frame. The visible part of the mouse-face will be highlighted,
28563 then the WM raises the obscured frame. With at least one WM, KDE
28564 2.1, Emacs is not getting any event for the raising of the frame
28565 (even tried with SubstructureRedirectMask), only Expose events.
28566 These expose events will draw text normally, i.e. not
28567 highlighted. Which means we must redo the highlight here.
28568 Subsume it under ``we love X''. --gerd 2001-08-15 */
28569 /* Included in Windows version because Windows most likely does not
28570 do the right thing if any third party tool offers
28571 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28572 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28573 {
28574 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28575 if (f == hlinfo->mouse_face_mouse_frame)
28576 {
28577 int mouse_x = hlinfo->mouse_face_mouse_x;
28578 int mouse_y = hlinfo->mouse_face_mouse_y;
28579 clear_mouse_face (hlinfo);
28580 note_mouse_highlight (f, mouse_x, mouse_y);
28581 }
28582 }
28583 }
28584
28585
28586 /* EXPORT:
28587 Determine the intersection of two rectangles R1 and R2. Return
28588 the intersection in *RESULT. Value is non-zero if RESULT is not
28589 empty. */
28590
28591 int
28592 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28593 {
28594 XRectangle *left, *right;
28595 XRectangle *upper, *lower;
28596 int intersection_p = 0;
28597
28598 /* Rearrange so that R1 is the left-most rectangle. */
28599 if (r1->x < r2->x)
28600 left = r1, right = r2;
28601 else
28602 left = r2, right = r1;
28603
28604 /* X0 of the intersection is right.x0, if this is inside R1,
28605 otherwise there is no intersection. */
28606 if (right->x <= left->x + left->width)
28607 {
28608 result->x = right->x;
28609
28610 /* The right end of the intersection is the minimum of
28611 the right ends of left and right. */
28612 result->width = (min (left->x + left->width, right->x + right->width)
28613 - result->x);
28614
28615 /* Same game for Y. */
28616 if (r1->y < r2->y)
28617 upper = r1, lower = r2;
28618 else
28619 upper = r2, lower = r1;
28620
28621 /* The upper end of the intersection is lower.y0, if this is inside
28622 of upper. Otherwise, there is no intersection. */
28623 if (lower->y <= upper->y + upper->height)
28624 {
28625 result->y = lower->y;
28626
28627 /* The lower end of the intersection is the minimum of the lower
28628 ends of upper and lower. */
28629 result->height = (min (lower->y + lower->height,
28630 upper->y + upper->height)
28631 - result->y);
28632 intersection_p = 1;
28633 }
28634 }
28635
28636 return intersection_p;
28637 }
28638
28639 #endif /* HAVE_WINDOW_SYSTEM */
28640
28641 \f
28642 /***********************************************************************
28643 Initialization
28644 ***********************************************************************/
28645
28646 void
28647 syms_of_xdisp (void)
28648 {
28649 Vwith_echo_area_save_vector = Qnil;
28650 staticpro (&Vwith_echo_area_save_vector);
28651
28652 Vmessage_stack = Qnil;
28653 staticpro (&Vmessage_stack);
28654
28655 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28656
28657 message_dolog_marker1 = Fmake_marker ();
28658 staticpro (&message_dolog_marker1);
28659 message_dolog_marker2 = Fmake_marker ();
28660 staticpro (&message_dolog_marker2);
28661 message_dolog_marker3 = Fmake_marker ();
28662 staticpro (&message_dolog_marker3);
28663
28664 #ifdef GLYPH_DEBUG
28665 defsubr (&Sdump_frame_glyph_matrix);
28666 defsubr (&Sdump_glyph_matrix);
28667 defsubr (&Sdump_glyph_row);
28668 defsubr (&Sdump_tool_bar_row);
28669 defsubr (&Strace_redisplay);
28670 defsubr (&Strace_to_stderr);
28671 #endif
28672 #ifdef HAVE_WINDOW_SYSTEM
28673 defsubr (&Stool_bar_lines_needed);
28674 defsubr (&Slookup_image_map);
28675 #endif
28676 defsubr (&Sformat_mode_line);
28677 defsubr (&Sinvisible_p);
28678 defsubr (&Scurrent_bidi_paragraph_direction);
28679
28680 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28681 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28682 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28683 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28684 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28685 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28686 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28687 DEFSYM (Qeval, "eval");
28688 DEFSYM (QCdata, ":data");
28689 DEFSYM (Qdisplay, "display");
28690 DEFSYM (Qspace_width, "space-width");
28691 DEFSYM (Qraise, "raise");
28692 DEFSYM (Qslice, "slice");
28693 DEFSYM (Qspace, "space");
28694 DEFSYM (Qmargin, "margin");
28695 DEFSYM (Qpointer, "pointer");
28696 DEFSYM (Qleft_margin, "left-margin");
28697 DEFSYM (Qright_margin, "right-margin");
28698 DEFSYM (Qcenter, "center");
28699 DEFSYM (Qline_height, "line-height");
28700 DEFSYM (QCalign_to, ":align-to");
28701 DEFSYM (QCrelative_width, ":relative-width");
28702 DEFSYM (QCrelative_height, ":relative-height");
28703 DEFSYM (QCeval, ":eval");
28704 DEFSYM (QCpropertize, ":propertize");
28705 DEFSYM (QCfile, ":file");
28706 DEFSYM (Qfontified, "fontified");
28707 DEFSYM (Qfontification_functions, "fontification-functions");
28708 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28709 DEFSYM (Qescape_glyph, "escape-glyph");
28710 DEFSYM (Qnobreak_space, "nobreak-space");
28711 DEFSYM (Qimage, "image");
28712 DEFSYM (Qtext, "text");
28713 DEFSYM (Qboth, "both");
28714 DEFSYM (Qboth_horiz, "both-horiz");
28715 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28716 DEFSYM (QCmap, ":map");
28717 DEFSYM (QCpointer, ":pointer");
28718 DEFSYM (Qrect, "rect");
28719 DEFSYM (Qcircle, "circle");
28720 DEFSYM (Qpoly, "poly");
28721 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28722 DEFSYM (Qgrow_only, "grow-only");
28723 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28724 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28725 DEFSYM (Qposition, "position");
28726 DEFSYM (Qbuffer_position, "buffer-position");
28727 DEFSYM (Qobject, "object");
28728 DEFSYM (Qbar, "bar");
28729 DEFSYM (Qhbar, "hbar");
28730 DEFSYM (Qbox, "box");
28731 DEFSYM (Qhollow, "hollow");
28732 DEFSYM (Qhand, "hand");
28733 DEFSYM (Qarrow, "arrow");
28734 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28735
28736 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28737 Fcons (intern_c_string ("void-variable"), Qnil)),
28738 Qnil);
28739 staticpro (&list_of_error);
28740
28741 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28742 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28743 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28744 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28745
28746 echo_buffer[0] = echo_buffer[1] = Qnil;
28747 staticpro (&echo_buffer[0]);
28748 staticpro (&echo_buffer[1]);
28749
28750 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28751 staticpro (&echo_area_buffer[0]);
28752 staticpro (&echo_area_buffer[1]);
28753
28754 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28755 staticpro (&Vmessages_buffer_name);
28756
28757 mode_line_proptrans_alist = Qnil;
28758 staticpro (&mode_line_proptrans_alist);
28759 mode_line_string_list = Qnil;
28760 staticpro (&mode_line_string_list);
28761 mode_line_string_face = Qnil;
28762 staticpro (&mode_line_string_face);
28763 mode_line_string_face_prop = Qnil;
28764 staticpro (&mode_line_string_face_prop);
28765 Vmode_line_unwind_vector = Qnil;
28766 staticpro (&Vmode_line_unwind_vector);
28767
28768 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28769
28770 help_echo_string = Qnil;
28771 staticpro (&help_echo_string);
28772 help_echo_object = Qnil;
28773 staticpro (&help_echo_object);
28774 help_echo_window = Qnil;
28775 staticpro (&help_echo_window);
28776 previous_help_echo_string = Qnil;
28777 staticpro (&previous_help_echo_string);
28778 help_echo_pos = -1;
28779
28780 DEFSYM (Qright_to_left, "right-to-left");
28781 DEFSYM (Qleft_to_right, "left-to-right");
28782
28783 #ifdef HAVE_WINDOW_SYSTEM
28784 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28785 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28786 For example, if a block cursor is over a tab, it will be drawn as
28787 wide as that tab on the display. */);
28788 x_stretch_cursor_p = 0;
28789 #endif
28790
28791 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28792 doc: /* Non-nil means highlight trailing whitespace.
28793 The face used for trailing whitespace is `trailing-whitespace'. */);
28794 Vshow_trailing_whitespace = Qnil;
28795
28796 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28797 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28798 If the value is t, Emacs highlights non-ASCII chars which have the
28799 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28800 or `escape-glyph' face respectively.
28801
28802 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28803 U+2011 (non-breaking hyphen) are affected.
28804
28805 Any other non-nil value means to display these characters as a escape
28806 glyph followed by an ordinary space or hyphen.
28807
28808 A value of nil means no special handling of these characters. */);
28809 Vnobreak_char_display = Qt;
28810
28811 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28812 doc: /* The pointer shape to show in void text areas.
28813 A value of nil means to show the text pointer. Other options are `arrow',
28814 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28815 Vvoid_text_area_pointer = Qarrow;
28816
28817 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28818 doc: /* Non-nil means don't actually do any redisplay.
28819 This is used for internal purposes. */);
28820 Vinhibit_redisplay = Qnil;
28821
28822 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28823 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28824 Vglobal_mode_string = Qnil;
28825
28826 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28827 doc: /* Marker for where to display an arrow on top of the buffer text.
28828 This must be the beginning of a line in order to work.
28829 See also `overlay-arrow-string'. */);
28830 Voverlay_arrow_position = Qnil;
28831
28832 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28833 doc: /* String to display as an arrow in non-window frames.
28834 See also `overlay-arrow-position'. */);
28835 Voverlay_arrow_string = build_pure_c_string ("=>");
28836
28837 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28838 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28839 The symbols on this list are examined during redisplay to determine
28840 where to display overlay arrows. */);
28841 Voverlay_arrow_variable_list
28842 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28843
28844 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28845 doc: /* The number of lines to try scrolling a window by when point moves out.
28846 If that fails to bring point back on frame, point is centered instead.
28847 If this is zero, point is always centered after it moves off frame.
28848 If you want scrolling to always be a line at a time, you should set
28849 `scroll-conservatively' to a large value rather than set this to 1. */);
28850
28851 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28852 doc: /* Scroll up to this many lines, to bring point back on screen.
28853 If point moves off-screen, redisplay will scroll by up to
28854 `scroll-conservatively' lines in order to bring point just barely
28855 onto the screen again. If that cannot be done, then redisplay
28856 recenters point as usual.
28857
28858 If the value is greater than 100, redisplay will never recenter point,
28859 but will always scroll just enough text to bring point into view, even
28860 if you move far away.
28861
28862 A value of zero means always recenter point if it moves off screen. */);
28863 scroll_conservatively = 0;
28864
28865 DEFVAR_INT ("scroll-margin", scroll_margin,
28866 doc: /* Number of lines of margin at the top and bottom of a window.
28867 Recenter the window whenever point gets within this many lines
28868 of the top or bottom of the window. */);
28869 scroll_margin = 0;
28870
28871 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28872 doc: /* Pixels per inch value for non-window system displays.
28873 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28874 Vdisplay_pixels_per_inch = make_float (72.0);
28875
28876 #ifdef GLYPH_DEBUG
28877 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28878 #endif
28879
28880 DEFVAR_LISP ("truncate-partial-width-windows",
28881 Vtruncate_partial_width_windows,
28882 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28883 For an integer value, truncate lines in each window narrower than the
28884 full frame width, provided the window width is less than that integer;
28885 otherwise, respect the value of `truncate-lines'.
28886
28887 For any other non-nil value, truncate lines in all windows that do
28888 not span the full frame width.
28889
28890 A value of nil means to respect the value of `truncate-lines'.
28891
28892 If `word-wrap' is enabled, you might want to reduce this. */);
28893 Vtruncate_partial_width_windows = make_number (50);
28894
28895 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28896 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28897 Any other value means to use the appropriate face, `mode-line',
28898 `header-line', or `menu' respectively. */);
28899 mode_line_inverse_video = 1;
28900
28901 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28902 doc: /* Maximum buffer size for which line number should be displayed.
28903 If the buffer is bigger than this, the line number does not appear
28904 in the mode line. A value of nil means no limit. */);
28905 Vline_number_display_limit = Qnil;
28906
28907 DEFVAR_INT ("line-number-display-limit-width",
28908 line_number_display_limit_width,
28909 doc: /* Maximum line width (in characters) for line number display.
28910 If the average length of the lines near point is bigger than this, then the
28911 line number may be omitted from the mode line. */);
28912 line_number_display_limit_width = 200;
28913
28914 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28915 doc: /* Non-nil means highlight region even in nonselected windows. */);
28916 highlight_nonselected_windows = 0;
28917
28918 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28919 doc: /* Non-nil if more than one frame is visible on this display.
28920 Minibuffer-only frames don't count, but iconified frames do.
28921 This variable is not guaranteed to be accurate except while processing
28922 `frame-title-format' and `icon-title-format'. */);
28923
28924 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28925 doc: /* Template for displaying the title bar of visible frames.
28926 \(Assuming the window manager supports this feature.)
28927
28928 This variable has the same structure as `mode-line-format', except that
28929 the %c and %l constructs are ignored. It is used only on frames for
28930 which no explicit name has been set \(see `modify-frame-parameters'). */);
28931
28932 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28933 doc: /* Template for displaying the title bar of an iconified frame.
28934 \(Assuming the window manager supports this feature.)
28935 This variable has the same structure as `mode-line-format' (which see),
28936 and is used only on frames for which no explicit name has been set
28937 \(see `modify-frame-parameters'). */);
28938 Vicon_title_format
28939 = Vframe_title_format
28940 = listn (CONSTYPE_PURE, 3,
28941 intern_c_string ("multiple-frames"),
28942 build_pure_c_string ("%b"),
28943 listn (CONSTYPE_PURE, 4,
28944 empty_unibyte_string,
28945 intern_c_string ("invocation-name"),
28946 build_pure_c_string ("@"),
28947 intern_c_string ("system-name")));
28948
28949 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28950 doc: /* Maximum number of lines to keep in the message log buffer.
28951 If nil, disable message logging. If t, log messages but don't truncate
28952 the buffer when it becomes large. */);
28953 Vmessage_log_max = make_number (100);
28954
28955 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28956 doc: /* Functions called before redisplay, if window sizes have changed.
28957 The value should be a list of functions that take one argument.
28958 Just before redisplay, for each frame, if any of its windows have changed
28959 size since the last redisplay, or have been split or deleted,
28960 all the functions in the list are called, with the frame as argument. */);
28961 Vwindow_size_change_functions = Qnil;
28962
28963 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28964 doc: /* List of functions to call before redisplaying a window with scrolling.
28965 Each function is called with two arguments, the window and its new
28966 display-start position. Note that these functions are also called by
28967 `set-window-buffer'. Also note that the value of `window-end' is not
28968 valid when these functions are called.
28969
28970 Warning: Do not use this feature to alter the way the window
28971 is scrolled. It is not designed for that, and such use probably won't
28972 work. */);
28973 Vwindow_scroll_functions = Qnil;
28974
28975 DEFVAR_LISP ("window-text-change-functions",
28976 Vwindow_text_change_functions,
28977 doc: /* Functions to call in redisplay when text in the window might change. */);
28978 Vwindow_text_change_functions = Qnil;
28979
28980 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28981 doc: /* Functions called when redisplay of a window reaches the end trigger.
28982 Each function is called with two arguments, the window and the end trigger value.
28983 See `set-window-redisplay-end-trigger'. */);
28984 Vredisplay_end_trigger_functions = Qnil;
28985
28986 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28987 doc: /* Non-nil means autoselect window with mouse pointer.
28988 If nil, do not autoselect windows.
28989 A positive number means delay autoselection by that many seconds: a
28990 window is autoselected only after the mouse has remained in that
28991 window for the duration of the delay.
28992 A negative number has a similar effect, but causes windows to be
28993 autoselected only after the mouse has stopped moving. \(Because of
28994 the way Emacs compares mouse events, you will occasionally wait twice
28995 that time before the window gets selected.\)
28996 Any other value means to autoselect window instantaneously when the
28997 mouse pointer enters it.
28998
28999 Autoselection selects the minibuffer only if it is active, and never
29000 unselects the minibuffer if it is active.
29001
29002 When customizing this variable make sure that the actual value of
29003 `focus-follows-mouse' matches the behavior of your window manager. */);
29004 Vmouse_autoselect_window = Qnil;
29005
29006 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29007 doc: /* Non-nil means automatically resize tool-bars.
29008 This dynamically changes the tool-bar's height to the minimum height
29009 that is needed to make all tool-bar items visible.
29010 If value is `grow-only', the tool-bar's height is only increased
29011 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29012 Vauto_resize_tool_bars = Qt;
29013
29014 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29015 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29016 auto_raise_tool_bar_buttons_p = 1;
29017
29018 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29019 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29020 make_cursor_line_fully_visible_p = 1;
29021
29022 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29023 doc: /* Border below tool-bar in pixels.
29024 If an integer, use it as the height of the border.
29025 If it is one of `internal-border-width' or `border-width', use the
29026 value of the corresponding frame parameter.
29027 Otherwise, no border is added below the tool-bar. */);
29028 Vtool_bar_border = Qinternal_border_width;
29029
29030 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29031 doc: /* Margin around tool-bar buttons in pixels.
29032 If an integer, use that for both horizontal and vertical margins.
29033 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29034 HORZ specifying the horizontal margin, and VERT specifying the
29035 vertical margin. */);
29036 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29037
29038 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29039 doc: /* Relief thickness of tool-bar buttons. */);
29040 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29041
29042 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29043 doc: /* Tool bar style to use.
29044 It can be one of
29045 image - show images only
29046 text - show text only
29047 both - show both, text below image
29048 both-horiz - show text to the right of the image
29049 text-image-horiz - show text to the left of the image
29050 any other - use system default or image if no system default.
29051
29052 This variable only affects the GTK+ toolkit version of Emacs. */);
29053 Vtool_bar_style = Qnil;
29054
29055 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29056 doc: /* Maximum number of characters a label can have to be shown.
29057 The tool bar style must also show labels for this to have any effect, see
29058 `tool-bar-style'. */);
29059 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29060
29061 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29062 doc: /* List of functions to call to fontify regions of text.
29063 Each function is called with one argument POS. Functions must
29064 fontify a region starting at POS in the current buffer, and give
29065 fontified regions the property `fontified'. */);
29066 Vfontification_functions = Qnil;
29067 Fmake_variable_buffer_local (Qfontification_functions);
29068
29069 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29070 unibyte_display_via_language_environment,
29071 doc: /* Non-nil means display unibyte text according to language environment.
29072 Specifically, this means that raw bytes in the range 160-255 decimal
29073 are displayed by converting them to the equivalent multibyte characters
29074 according to the current language environment. As a result, they are
29075 displayed according to the current fontset.
29076
29077 Note that this variable affects only how these bytes are displayed,
29078 but does not change the fact they are interpreted as raw bytes. */);
29079 unibyte_display_via_language_environment = 0;
29080
29081 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29082 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29083 If a float, it specifies a fraction of the mini-window frame's height.
29084 If an integer, it specifies a number of lines. */);
29085 Vmax_mini_window_height = make_float (0.25);
29086
29087 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29088 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29089 A value of nil means don't automatically resize mini-windows.
29090 A value of t means resize them to fit the text displayed in them.
29091 A value of `grow-only', the default, means let mini-windows grow only;
29092 they return to their normal size when the minibuffer is closed, or the
29093 echo area becomes empty. */);
29094 Vresize_mini_windows = Qgrow_only;
29095
29096 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29097 doc: /* Alist specifying how to blink the cursor off.
29098 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29099 `cursor-type' frame-parameter or variable equals ON-STATE,
29100 comparing using `equal', Emacs uses OFF-STATE to specify
29101 how to blink it off. ON-STATE and OFF-STATE are values for
29102 the `cursor-type' frame parameter.
29103
29104 If a frame's ON-STATE has no entry in this list,
29105 the frame's other specifications determine how to blink the cursor off. */);
29106 Vblink_cursor_alist = Qnil;
29107
29108 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29109 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29110 If non-nil, windows are automatically scrolled horizontally to make
29111 point visible. */);
29112 automatic_hscrolling_p = 1;
29113 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29114
29115 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29116 doc: /* How many columns away from the window edge point is allowed to get
29117 before automatic hscrolling will horizontally scroll the window. */);
29118 hscroll_margin = 5;
29119
29120 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29121 doc: /* How many columns to scroll the window when point gets too close to the edge.
29122 When point is less than `hscroll-margin' columns from the window
29123 edge, automatic hscrolling will scroll the window by the amount of columns
29124 determined by this variable. If its value is a positive integer, scroll that
29125 many columns. If it's a positive floating-point number, it specifies the
29126 fraction of the window's width to scroll. If it's nil or zero, point will be
29127 centered horizontally after the scroll. Any other value, including negative
29128 numbers, are treated as if the value were zero.
29129
29130 Automatic hscrolling always moves point outside the scroll margin, so if
29131 point was more than scroll step columns inside the margin, the window will
29132 scroll more than the value given by the scroll step.
29133
29134 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29135 and `scroll-right' overrides this variable's effect. */);
29136 Vhscroll_step = make_number (0);
29137
29138 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29139 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29140 Bind this around calls to `message' to let it take effect. */);
29141 message_truncate_lines = 0;
29142
29143 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29144 doc: /* Normal hook run to update the menu bar definitions.
29145 Redisplay runs this hook before it redisplays the menu bar.
29146 This is used to update submenus such as Buffers,
29147 whose contents depend on various data. */);
29148 Vmenu_bar_update_hook = Qnil;
29149
29150 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29151 doc: /* Frame for which we are updating a menu.
29152 The enable predicate for a menu binding should check this variable. */);
29153 Vmenu_updating_frame = Qnil;
29154
29155 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29156 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29157 inhibit_menubar_update = 0;
29158
29159 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29160 doc: /* Prefix prepended to all continuation lines at display time.
29161 The value may be a string, an image, or a stretch-glyph; it is
29162 interpreted in the same way as the value of a `display' text property.
29163
29164 This variable is overridden by any `wrap-prefix' text or overlay
29165 property.
29166
29167 To add a prefix to non-continuation lines, use `line-prefix'. */);
29168 Vwrap_prefix = Qnil;
29169 DEFSYM (Qwrap_prefix, "wrap-prefix");
29170 Fmake_variable_buffer_local (Qwrap_prefix);
29171
29172 DEFVAR_LISP ("line-prefix", Vline_prefix,
29173 doc: /* Prefix prepended to all non-continuation lines at display time.
29174 The value may be a string, an image, or a stretch-glyph; it is
29175 interpreted in the same way as the value of a `display' text property.
29176
29177 This variable is overridden by any `line-prefix' text or overlay
29178 property.
29179
29180 To add a prefix to continuation lines, use `wrap-prefix'. */);
29181 Vline_prefix = Qnil;
29182 DEFSYM (Qline_prefix, "line-prefix");
29183 Fmake_variable_buffer_local (Qline_prefix);
29184
29185 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29186 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29187 inhibit_eval_during_redisplay = 0;
29188
29189 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29190 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29191 inhibit_free_realized_faces = 0;
29192
29193 #ifdef GLYPH_DEBUG
29194 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29195 doc: /* Inhibit try_window_id display optimization. */);
29196 inhibit_try_window_id = 0;
29197
29198 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29199 doc: /* Inhibit try_window_reusing display optimization. */);
29200 inhibit_try_window_reusing = 0;
29201
29202 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29203 doc: /* Inhibit try_cursor_movement display optimization. */);
29204 inhibit_try_cursor_movement = 0;
29205 #endif /* GLYPH_DEBUG */
29206
29207 DEFVAR_INT ("overline-margin", overline_margin,
29208 doc: /* Space between overline and text, in pixels.
29209 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29210 margin to the character height. */);
29211 overline_margin = 2;
29212
29213 DEFVAR_INT ("underline-minimum-offset",
29214 underline_minimum_offset,
29215 doc: /* Minimum distance between baseline and underline.
29216 This can improve legibility of underlined text at small font sizes,
29217 particularly when using variable `x-use-underline-position-properties'
29218 with fonts that specify an UNDERLINE_POSITION relatively close to the
29219 baseline. The default value is 1. */);
29220 underline_minimum_offset = 1;
29221
29222 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29223 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29224 This feature only works when on a window system that can change
29225 cursor shapes. */);
29226 display_hourglass_p = 1;
29227
29228 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29229 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29230 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29231
29232 hourglass_atimer = NULL;
29233 hourglass_shown_p = 0;
29234
29235 DEFSYM (Qglyphless_char, "glyphless-char");
29236 DEFSYM (Qhex_code, "hex-code");
29237 DEFSYM (Qempty_box, "empty-box");
29238 DEFSYM (Qthin_space, "thin-space");
29239 DEFSYM (Qzero_width, "zero-width");
29240
29241 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29242 /* Intern this now in case it isn't already done.
29243 Setting this variable twice is harmless.
29244 But don't staticpro it here--that is done in alloc.c. */
29245 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29246 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29247
29248 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29249 doc: /* Char-table defining glyphless characters.
29250 Each element, if non-nil, should be one of the following:
29251 an ASCII acronym string: display this string in a box
29252 `hex-code': display the hexadecimal code of a character in a box
29253 `empty-box': display as an empty box
29254 `thin-space': display as 1-pixel width space
29255 `zero-width': don't display
29256 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29257 display method for graphical terminals and text terminals respectively.
29258 GRAPHICAL and TEXT should each have one of the values listed above.
29259
29260 The char-table has one extra slot to control the display of a character for
29261 which no font is found. This slot only takes effect on graphical terminals.
29262 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29263 `thin-space'. The default is `empty-box'. */);
29264 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29265 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29266 Qempty_box);
29267 }
29268
29269
29270 /* Initialize this module when Emacs starts. */
29271
29272 void
29273 init_xdisp (void)
29274 {
29275 current_header_line_height = current_mode_line_height = -1;
29276
29277 CHARPOS (this_line_start_pos) = 0;
29278
29279 if (!noninteractive)
29280 {
29281 struct window *m = XWINDOW (minibuf_window);
29282 Lisp_Object frame = m->frame;
29283 struct frame *f = XFRAME (frame);
29284 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29285 struct window *r = XWINDOW (root);
29286 int i;
29287
29288 echo_area_window = minibuf_window;
29289
29290 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29291 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29292 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29293 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29294 XSETFASTINT (m->total_lines, 1);
29295 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29296
29297 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29298 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29299 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29300
29301 /* The default ellipsis glyphs `...'. */
29302 for (i = 0; i < 3; ++i)
29303 default_invis_vector[i] = make_number ('.');
29304 }
29305
29306 {
29307 /* Allocate the buffer for frame titles.
29308 Also used for `format-mode-line'. */
29309 int size = 100;
29310 mode_line_noprop_buf = xmalloc (size);
29311 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29312 mode_line_noprop_ptr = mode_line_noprop_buf;
29313 mode_line_target = MODE_LINE_DISPLAY;
29314 }
29315
29316 help_echo_showing_p = 0;
29317 }
29318
29319 /* Since w32 does not support atimers, it defines its own implementation of
29320 the following three functions in w32fns.c. */
29321 #ifndef WINDOWSNT
29322
29323 /* Platform-independent portion of hourglass implementation. */
29324
29325 /* Cancel a currently active hourglass timer, and start a new one. */
29326 void
29327 start_hourglass (void)
29328 {
29329 #if defined (HAVE_WINDOW_SYSTEM)
29330 EMACS_TIME delay;
29331
29332 cancel_hourglass ();
29333
29334 if (INTEGERP (Vhourglass_delay)
29335 && XINT (Vhourglass_delay) > 0)
29336 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29337 TYPE_MAXIMUM (time_t)),
29338 0);
29339 else if (FLOATP (Vhourglass_delay)
29340 && XFLOAT_DATA (Vhourglass_delay) > 0)
29341 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29342 else
29343 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29344
29345 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29346 show_hourglass, NULL);
29347 #endif
29348 }
29349
29350
29351 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29352 shown. */
29353 void
29354 cancel_hourglass (void)
29355 {
29356 #if defined (HAVE_WINDOW_SYSTEM)
29357 if (hourglass_atimer)
29358 {
29359 cancel_atimer (hourglass_atimer);
29360 hourglass_atimer = NULL;
29361 }
29362
29363 if (hourglass_shown_p)
29364 hide_hourglass ();
29365 #endif
29366 }
29367 #endif /* ! WINDOWSNT */