Convert safe_call to use variable number of arguments.
[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, 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 (f->selected_window, Qt);
11056 set_buffer_internal_1 (XBUFFER (XWINDOW (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 (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 (f->name)
11074 || SBYTES (f->name) != len
11075 || memcmp (title, SDATA (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 (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 (f->tool_bar_window)
11417 && WINDOW_TOTAL_LINES (XWINDOW (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 (Fcopy_sequence (f->tool_bar_items),
11479 &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, 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 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 (f->desired_tool_bar_string)
11523 ? SCHARS (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 (f->desired_tool_bar_string))
11531 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11532 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, 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) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11546
11547 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11548 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11549 int hmargin, vmargin, relief, idx, end;
11550
11551 /* If image is a vector, choose the image according to the
11552 button state. */
11553 image = PROP (TOOL_BAR_ITEM_IMAGES);
11554 if (VECTORP (image))
11555 {
11556 if (enabled_p)
11557 idx = (selected_p
11558 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11559 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11560 else
11561 idx = (selected_p
11562 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11563 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11564
11565 eassert (ASIZE (image) >= idx);
11566 image = AREF (image, idx);
11567 }
11568 else
11569 idx = -1;
11570
11571 /* Ignore invalid image specifications. */
11572 if (!valid_image_p (image))
11573 continue;
11574
11575 /* Display the tool-bar button pressed, or depressed. */
11576 plist = Fcopy_sequence (XCDR (image));
11577
11578 /* Compute margin and relief to draw. */
11579 relief = (tool_bar_button_relief >= 0
11580 ? tool_bar_button_relief
11581 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11582 hmargin = vmargin = relief;
11583
11584 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11585 INT_MAX - max (hmargin, vmargin)))
11586 {
11587 hmargin += XFASTINT (Vtool_bar_button_margin);
11588 vmargin += XFASTINT (Vtool_bar_button_margin);
11589 }
11590 else if (CONSP (Vtool_bar_button_margin))
11591 {
11592 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11593 INT_MAX - hmargin))
11594 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11595
11596 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11597 INT_MAX - vmargin))
11598 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11599 }
11600
11601 if (auto_raise_tool_bar_buttons_p)
11602 {
11603 /* Add a `:relief' property to the image spec if the item is
11604 selected. */
11605 if (selected_p)
11606 {
11607 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11608 hmargin -= relief;
11609 vmargin -= relief;
11610 }
11611 }
11612 else
11613 {
11614 /* If image is selected, display it pressed, i.e. with a
11615 negative relief. If it's not selected, display it with a
11616 raised relief. */
11617 plist = Fplist_put (plist, QCrelief,
11618 (selected_p
11619 ? make_number (-relief)
11620 : make_number (relief)));
11621 hmargin -= relief;
11622 vmargin -= relief;
11623 }
11624
11625 /* Put a margin around the image. */
11626 if (hmargin || vmargin)
11627 {
11628 if (hmargin == vmargin)
11629 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11630 else
11631 plist = Fplist_put (plist, QCmargin,
11632 Fcons (make_number (hmargin),
11633 make_number (vmargin)));
11634 }
11635
11636 /* If button is not enabled, and we don't have special images
11637 for the disabled state, make the image appear disabled by
11638 applying an appropriate algorithm to it. */
11639 if (!enabled_p && idx < 0)
11640 plist = Fplist_put (plist, QCconversion, Qdisabled);
11641
11642 /* Put a `display' text property on the string for the image to
11643 display. Put a `menu-item' property on the string that gives
11644 the start of this item's properties in the tool-bar items
11645 vector. */
11646 image = Fcons (Qimage, plist);
11647 props = list4 (Qdisplay, image,
11648 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11649
11650 /* Let the last image hide all remaining spaces in the tool bar
11651 string. The string can be longer than needed when we reuse a
11652 previous string. */
11653 if (i + 1 == f->n_tool_bar_items)
11654 end = SCHARS (f->desired_tool_bar_string);
11655 else
11656 end = i + 1;
11657 Fadd_text_properties (make_number (i), make_number (end),
11658 props, f->desired_tool_bar_string);
11659 #undef PROP
11660 }
11661
11662 UNGCPRO;
11663 }
11664
11665
11666 /* Display one line of the tool-bar of frame IT->f.
11667
11668 HEIGHT specifies the desired height of the tool-bar line.
11669 If the actual height of the glyph row is less than HEIGHT, the
11670 row's height is increased to HEIGHT, and the icons are centered
11671 vertically in the new height.
11672
11673 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11674 count a final empty row in case the tool-bar width exactly matches
11675 the window width.
11676 */
11677
11678 static void
11679 display_tool_bar_line (struct it *it, int height)
11680 {
11681 struct glyph_row *row = it->glyph_row;
11682 int max_x = it->last_visible_x;
11683 struct glyph *last;
11684
11685 prepare_desired_row (row);
11686 row->y = it->current_y;
11687
11688 /* Note that this isn't made use of if the face hasn't a box,
11689 so there's no need to check the face here. */
11690 it->start_of_box_run_p = 1;
11691
11692 while (it->current_x < max_x)
11693 {
11694 int x, n_glyphs_before, i, nglyphs;
11695 struct it it_before;
11696
11697 /* Get the next display element. */
11698 if (!get_next_display_element (it))
11699 {
11700 /* Don't count empty row if we are counting needed tool-bar lines. */
11701 if (height < 0 && !it->hpos)
11702 return;
11703 break;
11704 }
11705
11706 /* Produce glyphs. */
11707 n_glyphs_before = row->used[TEXT_AREA];
11708 it_before = *it;
11709
11710 PRODUCE_GLYPHS (it);
11711
11712 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11713 i = 0;
11714 x = it_before.current_x;
11715 while (i < nglyphs)
11716 {
11717 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11718
11719 if (x + glyph->pixel_width > max_x)
11720 {
11721 /* Glyph doesn't fit on line. Backtrack. */
11722 row->used[TEXT_AREA] = n_glyphs_before;
11723 *it = it_before;
11724 /* If this is the only glyph on this line, it will never fit on the
11725 tool-bar, so skip it. But ensure there is at least one glyph,
11726 so we don't accidentally disable the tool-bar. */
11727 if (n_glyphs_before == 0
11728 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11729 break;
11730 goto out;
11731 }
11732
11733 ++it->hpos;
11734 x += glyph->pixel_width;
11735 ++i;
11736 }
11737
11738 /* Stop at line end. */
11739 if (ITERATOR_AT_END_OF_LINE_P (it))
11740 break;
11741
11742 set_iterator_to_next (it, 1);
11743 }
11744
11745 out:;
11746
11747 row->displays_text_p = row->used[TEXT_AREA] != 0;
11748
11749 /* Use default face for the border below the tool bar.
11750
11751 FIXME: When auto-resize-tool-bars is grow-only, there is
11752 no additional border below the possibly empty tool-bar lines.
11753 So to make the extra empty lines look "normal", we have to
11754 use the tool-bar face for the border too. */
11755 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11756 it->face_id = DEFAULT_FACE_ID;
11757
11758 extend_face_to_end_of_line (it);
11759 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11760 last->right_box_line_p = 1;
11761 if (last == row->glyphs[TEXT_AREA])
11762 last->left_box_line_p = 1;
11763
11764 /* Make line the desired height and center it vertically. */
11765 if ((height -= it->max_ascent + it->max_descent) > 0)
11766 {
11767 /* Don't add more than one line height. */
11768 height %= FRAME_LINE_HEIGHT (it->f);
11769 it->max_ascent += height / 2;
11770 it->max_descent += (height + 1) / 2;
11771 }
11772
11773 compute_line_metrics (it);
11774
11775 /* If line is empty, make it occupy the rest of the tool-bar. */
11776 if (!row->displays_text_p)
11777 {
11778 row->height = row->phys_height = it->last_visible_y - row->y;
11779 row->visible_height = row->height;
11780 row->ascent = row->phys_ascent = 0;
11781 row->extra_line_spacing = 0;
11782 }
11783
11784 row->full_width_p = 1;
11785 row->continued_p = 0;
11786 row->truncated_on_left_p = 0;
11787 row->truncated_on_right_p = 0;
11788
11789 it->current_x = it->hpos = 0;
11790 it->current_y += row->height;
11791 ++it->vpos;
11792 ++it->glyph_row;
11793 }
11794
11795
11796 /* Max tool-bar height. */
11797
11798 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11799 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11800
11801 /* Value is the number of screen lines needed to make all tool-bar
11802 items of frame F visible. The number of actual rows needed is
11803 returned in *N_ROWS if non-NULL. */
11804
11805 static int
11806 tool_bar_lines_needed (struct frame *f, int *n_rows)
11807 {
11808 struct window *w = XWINDOW (f->tool_bar_window);
11809 struct it it;
11810 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11811 the desired matrix, so use (unused) mode-line row as temporary row to
11812 avoid destroying the first tool-bar row. */
11813 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11814
11815 /* Initialize an iterator for iteration over
11816 F->desired_tool_bar_string in the tool-bar window of frame F. */
11817 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11818 it.first_visible_x = 0;
11819 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11820 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11821 it.paragraph_embedding = L2R;
11822
11823 while (!ITERATOR_AT_END_P (&it))
11824 {
11825 clear_glyph_row (temp_row);
11826 it.glyph_row = temp_row;
11827 display_tool_bar_line (&it, -1);
11828 }
11829 clear_glyph_row (temp_row);
11830
11831 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11832 if (n_rows)
11833 *n_rows = it.vpos > 0 ? it.vpos : -1;
11834
11835 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11836 }
11837
11838
11839 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11840 0, 1, 0,
11841 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11842 (Lisp_Object frame)
11843 {
11844 struct frame *f;
11845 struct window *w;
11846 int nlines = 0;
11847
11848 if (NILP (frame))
11849 frame = selected_frame;
11850 else
11851 CHECK_FRAME (frame);
11852 f = XFRAME (frame);
11853
11854 if (WINDOWP (f->tool_bar_window)
11855 && (w = XWINDOW (f->tool_bar_window),
11856 WINDOW_TOTAL_LINES (w) > 0))
11857 {
11858 update_tool_bar (f, 1);
11859 if (f->n_tool_bar_items)
11860 {
11861 build_desired_tool_bar_string (f);
11862 nlines = tool_bar_lines_needed (f, NULL);
11863 }
11864 }
11865
11866 return make_number (nlines);
11867 }
11868
11869
11870 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11871 height should be changed. */
11872
11873 static int
11874 redisplay_tool_bar (struct frame *f)
11875 {
11876 struct window *w;
11877 struct it it;
11878 struct glyph_row *row;
11879
11880 #if defined (USE_GTK) || defined (HAVE_NS)
11881 if (FRAME_EXTERNAL_TOOL_BAR (f))
11882 update_frame_tool_bar (f);
11883 return 0;
11884 #endif
11885
11886 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11887 do anything. This means you must start with tool-bar-lines
11888 non-zero to get the auto-sizing effect. Or in other words, you
11889 can turn off tool-bars by specifying tool-bar-lines zero. */
11890 if (!WINDOWP (f->tool_bar_window)
11891 || (w = XWINDOW (f->tool_bar_window),
11892 WINDOW_TOTAL_LINES (w) == 0))
11893 return 0;
11894
11895 /* Set up an iterator for the tool-bar window. */
11896 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11897 it.first_visible_x = 0;
11898 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11899 row = it.glyph_row;
11900
11901 /* Build a string that represents the contents of the tool-bar. */
11902 build_desired_tool_bar_string (f);
11903 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11904 /* FIXME: This should be controlled by a user option. But it
11905 doesn't make sense to have an R2L tool bar if the menu bar cannot
11906 be drawn also R2L, and making the menu bar R2L is tricky due
11907 toolkit-specific code that implements it. If an R2L tool bar is
11908 ever supported, display_tool_bar_line should also be augmented to
11909 call unproduce_glyphs like display_line and display_string
11910 do. */
11911 it.paragraph_embedding = L2R;
11912
11913 if (f->n_tool_bar_rows == 0)
11914 {
11915 int nlines;
11916
11917 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11918 nlines != WINDOW_TOTAL_LINES (w)))
11919 {
11920 Lisp_Object frame;
11921 int old_height = WINDOW_TOTAL_LINES (w);
11922
11923 XSETFRAME (frame, f);
11924 Fmodify_frame_parameters (frame,
11925 Fcons (Fcons (Qtool_bar_lines,
11926 make_number (nlines)),
11927 Qnil));
11928 if (WINDOW_TOTAL_LINES (w) != old_height)
11929 {
11930 clear_glyph_matrix (w->desired_matrix);
11931 fonts_changed_p = 1;
11932 return 1;
11933 }
11934 }
11935 }
11936
11937 /* Display as many lines as needed to display all tool-bar items. */
11938
11939 if (f->n_tool_bar_rows > 0)
11940 {
11941 int border, rows, height, extra;
11942
11943 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11944 border = XINT (Vtool_bar_border);
11945 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11946 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11947 else if (EQ (Vtool_bar_border, Qborder_width))
11948 border = f->border_width;
11949 else
11950 border = 0;
11951 if (border < 0)
11952 border = 0;
11953
11954 rows = f->n_tool_bar_rows;
11955 height = max (1, (it.last_visible_y - border) / rows);
11956 extra = it.last_visible_y - border - height * rows;
11957
11958 while (it.current_y < it.last_visible_y)
11959 {
11960 int h = 0;
11961 if (extra > 0 && rows-- > 0)
11962 {
11963 h = (extra + rows - 1) / rows;
11964 extra -= h;
11965 }
11966 display_tool_bar_line (&it, height + h);
11967 }
11968 }
11969 else
11970 {
11971 while (it.current_y < it.last_visible_y)
11972 display_tool_bar_line (&it, 0);
11973 }
11974
11975 /* It doesn't make much sense to try scrolling in the tool-bar
11976 window, so don't do it. */
11977 w->desired_matrix->no_scrolling_p = 1;
11978 w->must_be_updated_p = 1;
11979
11980 if (!NILP (Vauto_resize_tool_bars))
11981 {
11982 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11983 int change_height_p = 0;
11984
11985 /* If we couldn't display everything, change the tool-bar's
11986 height if there is room for more. */
11987 if (IT_STRING_CHARPOS (it) < it.end_charpos
11988 && it.current_y < max_tool_bar_height)
11989 change_height_p = 1;
11990
11991 row = it.glyph_row - 1;
11992
11993 /* If there are blank lines at the end, except for a partially
11994 visible blank line at the end that is smaller than
11995 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11996 if (!row->displays_text_p
11997 && row->height >= FRAME_LINE_HEIGHT (f))
11998 change_height_p = 1;
11999
12000 /* If row displays tool-bar items, but is partially visible,
12001 change the tool-bar's height. */
12002 if (row->displays_text_p
12003 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12004 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12005 change_height_p = 1;
12006
12007 /* Resize windows as needed by changing the `tool-bar-lines'
12008 frame parameter. */
12009 if (change_height_p)
12010 {
12011 Lisp_Object frame;
12012 int old_height = WINDOW_TOTAL_LINES (w);
12013 int nrows;
12014 int nlines = tool_bar_lines_needed (f, &nrows);
12015
12016 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12017 && !f->minimize_tool_bar_window_p)
12018 ? (nlines > old_height)
12019 : (nlines != old_height));
12020 f->minimize_tool_bar_window_p = 0;
12021
12022 if (change_height_p)
12023 {
12024 XSETFRAME (frame, f);
12025 Fmodify_frame_parameters (frame,
12026 Fcons (Fcons (Qtool_bar_lines,
12027 make_number (nlines)),
12028 Qnil));
12029 if (WINDOW_TOTAL_LINES (w) != old_height)
12030 {
12031 clear_glyph_matrix (w->desired_matrix);
12032 f->n_tool_bar_rows = nrows;
12033 fonts_changed_p = 1;
12034 return 1;
12035 }
12036 }
12037 }
12038 }
12039
12040 f->minimize_tool_bar_window_p = 0;
12041 return 0;
12042 }
12043
12044
12045 /* Get information about the tool-bar item which is displayed in GLYPH
12046 on frame F. Return in *PROP_IDX the index where tool-bar item
12047 properties start in F->tool_bar_items. Value is zero if
12048 GLYPH doesn't display a tool-bar item. */
12049
12050 static int
12051 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12052 {
12053 Lisp_Object prop;
12054 int success_p;
12055 int charpos;
12056
12057 /* This function can be called asynchronously, which means we must
12058 exclude any possibility that Fget_text_property signals an
12059 error. */
12060 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12061 charpos = max (0, charpos);
12062
12063 /* Get the text property `menu-item' at pos. The value of that
12064 property is the start index of this item's properties in
12065 F->tool_bar_items. */
12066 prop = Fget_text_property (make_number (charpos),
12067 Qmenu_item, f->current_tool_bar_string);
12068 if (INTEGERP (prop))
12069 {
12070 *prop_idx = XINT (prop);
12071 success_p = 1;
12072 }
12073 else
12074 success_p = 0;
12075
12076 return success_p;
12077 }
12078
12079 \f
12080 /* Get information about the tool-bar item at position X/Y on frame F.
12081 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12082 the current matrix of the tool-bar window of F, or NULL if not
12083 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12084 item in F->tool_bar_items. Value is
12085
12086 -1 if X/Y is not on a tool-bar item
12087 0 if X/Y is on the same item that was highlighted before.
12088 1 otherwise. */
12089
12090 static int
12091 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12092 int *hpos, int *vpos, int *prop_idx)
12093 {
12094 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12095 struct window *w = XWINDOW (f->tool_bar_window);
12096 int area;
12097
12098 /* Find the glyph under X/Y. */
12099 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12100 if (*glyph == NULL)
12101 return -1;
12102
12103 /* Get the start of this tool-bar item's properties in
12104 f->tool_bar_items. */
12105 if (!tool_bar_item_info (f, *glyph, prop_idx))
12106 return -1;
12107
12108 /* Is mouse on the highlighted item? */
12109 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12110 && *vpos >= hlinfo->mouse_face_beg_row
12111 && *vpos <= hlinfo->mouse_face_end_row
12112 && (*vpos > hlinfo->mouse_face_beg_row
12113 || *hpos >= hlinfo->mouse_face_beg_col)
12114 && (*vpos < hlinfo->mouse_face_end_row
12115 || *hpos < hlinfo->mouse_face_end_col
12116 || hlinfo->mouse_face_past_end))
12117 return 0;
12118
12119 return 1;
12120 }
12121
12122
12123 /* EXPORT:
12124 Handle mouse button event on the tool-bar of frame F, at
12125 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12126 0 for button release. MODIFIERS is event modifiers for button
12127 release. */
12128
12129 void
12130 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12131 int modifiers)
12132 {
12133 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12134 struct window *w = XWINDOW (f->tool_bar_window);
12135 int hpos, vpos, prop_idx;
12136 struct glyph *glyph;
12137 Lisp_Object enabled_p;
12138
12139 /* If not on the highlighted tool-bar item, return. */
12140 frame_to_window_pixel_xy (w, &x, &y);
12141 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12142 return;
12143
12144 /* If item is disabled, do nothing. */
12145 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12146 if (NILP (enabled_p))
12147 return;
12148
12149 if (down_p)
12150 {
12151 /* Show item in pressed state. */
12152 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12153 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12154 last_tool_bar_item = prop_idx;
12155 }
12156 else
12157 {
12158 Lisp_Object key, frame;
12159 struct input_event event;
12160 EVENT_INIT (event);
12161
12162 /* Show item in released state. */
12163 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12164 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12165
12166 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12167
12168 XSETFRAME (frame, f);
12169 event.kind = TOOL_BAR_EVENT;
12170 event.frame_or_window = frame;
12171 event.arg = frame;
12172 kbd_buffer_store_event (&event);
12173
12174 event.kind = TOOL_BAR_EVENT;
12175 event.frame_or_window = frame;
12176 event.arg = key;
12177 event.modifiers = modifiers;
12178 kbd_buffer_store_event (&event);
12179 last_tool_bar_item = -1;
12180 }
12181 }
12182
12183
12184 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12185 tool-bar window-relative coordinates X/Y. Called from
12186 note_mouse_highlight. */
12187
12188 static void
12189 note_tool_bar_highlight (struct frame *f, int x, int y)
12190 {
12191 Lisp_Object window = f->tool_bar_window;
12192 struct window *w = XWINDOW (window);
12193 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12194 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12195 int hpos, vpos;
12196 struct glyph *glyph;
12197 struct glyph_row *row;
12198 int i;
12199 Lisp_Object enabled_p;
12200 int prop_idx;
12201 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12202 int mouse_down_p, rc;
12203
12204 /* Function note_mouse_highlight is called with negative X/Y
12205 values when mouse moves outside of the frame. */
12206 if (x <= 0 || y <= 0)
12207 {
12208 clear_mouse_face (hlinfo);
12209 return;
12210 }
12211
12212 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12213 if (rc < 0)
12214 {
12215 /* Not on tool-bar item. */
12216 clear_mouse_face (hlinfo);
12217 return;
12218 }
12219 else if (rc == 0)
12220 /* On same tool-bar item as before. */
12221 goto set_help_echo;
12222
12223 clear_mouse_face (hlinfo);
12224
12225 /* Mouse is down, but on different tool-bar item? */
12226 mouse_down_p = (dpyinfo->grabbed
12227 && f == last_mouse_frame
12228 && FRAME_LIVE_P (f));
12229 if (mouse_down_p
12230 && last_tool_bar_item != prop_idx)
12231 return;
12232
12233 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12234 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12235
12236 /* If tool-bar item is not enabled, don't highlight it. */
12237 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12238 if (!NILP (enabled_p))
12239 {
12240 /* Compute the x-position of the glyph. In front and past the
12241 image is a space. We include this in the highlighted area. */
12242 row = MATRIX_ROW (w->current_matrix, vpos);
12243 for (i = x = 0; i < hpos; ++i)
12244 x += row->glyphs[TEXT_AREA][i].pixel_width;
12245
12246 /* Record this as the current active region. */
12247 hlinfo->mouse_face_beg_col = hpos;
12248 hlinfo->mouse_face_beg_row = vpos;
12249 hlinfo->mouse_face_beg_x = x;
12250 hlinfo->mouse_face_beg_y = row->y;
12251 hlinfo->mouse_face_past_end = 0;
12252
12253 hlinfo->mouse_face_end_col = hpos + 1;
12254 hlinfo->mouse_face_end_row = vpos;
12255 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12256 hlinfo->mouse_face_end_y = row->y;
12257 hlinfo->mouse_face_window = window;
12258 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12259
12260 /* Display it as active. */
12261 show_mouse_face (hlinfo, draw);
12262 hlinfo->mouse_face_image_state = draw;
12263 }
12264
12265 set_help_echo:
12266
12267 /* Set help_echo_string to a help string to display for this tool-bar item.
12268 XTread_socket does the rest. */
12269 help_echo_object = help_echo_window = Qnil;
12270 help_echo_pos = -1;
12271 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12272 if (NILP (help_echo_string))
12273 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12274 }
12275
12276 #endif /* HAVE_WINDOW_SYSTEM */
12277
12278
12279 \f
12280 /************************************************************************
12281 Horizontal scrolling
12282 ************************************************************************/
12283
12284 static int hscroll_window_tree (Lisp_Object);
12285 static int hscroll_windows (Lisp_Object);
12286
12287 /* For all leaf windows in the window tree rooted at WINDOW, set their
12288 hscroll value so that PT is (i) visible in the window, and (ii) so
12289 that it is not within a certain margin at the window's left and
12290 right border. Value is non-zero if any window's hscroll has been
12291 changed. */
12292
12293 static int
12294 hscroll_window_tree (Lisp_Object window)
12295 {
12296 int hscrolled_p = 0;
12297 int hscroll_relative_p = FLOATP (Vhscroll_step);
12298 int hscroll_step_abs = 0;
12299 double hscroll_step_rel = 0;
12300
12301 if (hscroll_relative_p)
12302 {
12303 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12304 if (hscroll_step_rel < 0)
12305 {
12306 hscroll_relative_p = 0;
12307 hscroll_step_abs = 0;
12308 }
12309 }
12310 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12311 {
12312 hscroll_step_abs = XINT (Vhscroll_step);
12313 if (hscroll_step_abs < 0)
12314 hscroll_step_abs = 0;
12315 }
12316 else
12317 hscroll_step_abs = 0;
12318
12319 while (WINDOWP (window))
12320 {
12321 struct window *w = XWINDOW (window);
12322
12323 if (WINDOWP (w->hchild))
12324 hscrolled_p |= hscroll_window_tree (w->hchild);
12325 else if (WINDOWP (w->vchild))
12326 hscrolled_p |= hscroll_window_tree (w->vchild);
12327 else if (w->cursor.vpos >= 0)
12328 {
12329 int h_margin;
12330 int text_area_width;
12331 struct glyph_row *current_cursor_row
12332 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12333 struct glyph_row *desired_cursor_row
12334 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12335 struct glyph_row *cursor_row
12336 = (desired_cursor_row->enabled_p
12337 ? desired_cursor_row
12338 : current_cursor_row);
12339 int row_r2l_p = cursor_row->reversed_p;
12340
12341 text_area_width = window_box_width (w, TEXT_AREA);
12342
12343 /* Scroll when cursor is inside this scroll margin. */
12344 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12345
12346 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12347 /* For left-to-right rows, hscroll when cursor is either
12348 (i) inside the right hscroll margin, or (ii) if it is
12349 inside the left margin and the window is already
12350 hscrolled. */
12351 && ((!row_r2l_p
12352 && ((w->hscroll
12353 && w->cursor.x <= h_margin)
12354 || (cursor_row->enabled_p
12355 && cursor_row->truncated_on_right_p
12356 && (w->cursor.x >= text_area_width - h_margin))))
12357 /* For right-to-left rows, the logic is similar,
12358 except that rules for scrolling to left and right
12359 are reversed. E.g., if cursor.x <= h_margin, we
12360 need to hscroll "to the right" unconditionally,
12361 and that will scroll the screen to the left so as
12362 to reveal the next portion of the row. */
12363 || (row_r2l_p
12364 && ((cursor_row->enabled_p
12365 /* FIXME: It is confusing to set the
12366 truncated_on_right_p flag when R2L rows
12367 are actually truncated on the left. */
12368 && cursor_row->truncated_on_right_p
12369 && w->cursor.x <= h_margin)
12370 || (w->hscroll
12371 && (w->cursor.x >= text_area_width - h_margin))))))
12372 {
12373 struct it it;
12374 ptrdiff_t hscroll;
12375 struct buffer *saved_current_buffer;
12376 ptrdiff_t pt;
12377 int wanted_x;
12378
12379 /* Find point in a display of infinite width. */
12380 saved_current_buffer = current_buffer;
12381 current_buffer = XBUFFER (w->buffer);
12382
12383 if (w == XWINDOW (selected_window))
12384 pt = PT;
12385 else
12386 {
12387 pt = marker_position (w->pointm);
12388 pt = max (BEGV, pt);
12389 pt = min (ZV, pt);
12390 }
12391
12392 /* Move iterator to pt starting at cursor_row->start in
12393 a line with infinite width. */
12394 init_to_row_start (&it, w, cursor_row);
12395 it.last_visible_x = INFINITY;
12396 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12397 current_buffer = saved_current_buffer;
12398
12399 /* Position cursor in window. */
12400 if (!hscroll_relative_p && hscroll_step_abs == 0)
12401 hscroll = max (0, (it.current_x
12402 - (ITERATOR_AT_END_OF_LINE_P (&it)
12403 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12404 : (text_area_width / 2))))
12405 / FRAME_COLUMN_WIDTH (it.f);
12406 else if ((!row_r2l_p
12407 && w->cursor.x >= text_area_width - h_margin)
12408 || (row_r2l_p && w->cursor.x <= h_margin))
12409 {
12410 if (hscroll_relative_p)
12411 wanted_x = text_area_width * (1 - hscroll_step_rel)
12412 - h_margin;
12413 else
12414 wanted_x = text_area_width
12415 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12416 - h_margin;
12417 hscroll
12418 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12419 }
12420 else
12421 {
12422 if (hscroll_relative_p)
12423 wanted_x = text_area_width * hscroll_step_rel
12424 + h_margin;
12425 else
12426 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12427 + h_margin;
12428 hscroll
12429 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12430 }
12431 hscroll = max (hscroll, w->min_hscroll);
12432
12433 /* Don't prevent redisplay optimizations if hscroll
12434 hasn't changed, as it will unnecessarily slow down
12435 redisplay. */
12436 if (w->hscroll != hscroll)
12437 {
12438 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12439 w->hscroll = hscroll;
12440 hscrolled_p = 1;
12441 }
12442 }
12443 }
12444
12445 window = w->next;
12446 }
12447
12448 /* Value is non-zero if hscroll of any leaf window has been changed. */
12449 return hscrolled_p;
12450 }
12451
12452
12453 /* Set hscroll so that cursor is visible and not inside horizontal
12454 scroll margins for all windows in the tree rooted at WINDOW. See
12455 also hscroll_window_tree above. Value is non-zero if any window's
12456 hscroll has been changed. If it has, desired matrices on the frame
12457 of WINDOW are cleared. */
12458
12459 static int
12460 hscroll_windows (Lisp_Object window)
12461 {
12462 int hscrolled_p = hscroll_window_tree (window);
12463 if (hscrolled_p)
12464 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12465 return hscrolled_p;
12466 }
12467
12468
12469 \f
12470 /************************************************************************
12471 Redisplay
12472 ************************************************************************/
12473
12474 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12475 to a non-zero value. This is sometimes handy to have in a debugger
12476 session. */
12477
12478 #ifdef GLYPH_DEBUG
12479
12480 /* First and last unchanged row for try_window_id. */
12481
12482 static int debug_first_unchanged_at_end_vpos;
12483 static int debug_last_unchanged_at_beg_vpos;
12484
12485 /* Delta vpos and y. */
12486
12487 static int debug_dvpos, debug_dy;
12488
12489 /* Delta in characters and bytes for try_window_id. */
12490
12491 static ptrdiff_t debug_delta, debug_delta_bytes;
12492
12493 /* Values of window_end_pos and window_end_vpos at the end of
12494 try_window_id. */
12495
12496 static ptrdiff_t debug_end_vpos;
12497
12498 /* Append a string to W->desired_matrix->method. FMT is a printf
12499 format string. If trace_redisplay_p is non-zero also printf the
12500 resulting string to stderr. */
12501
12502 static void debug_method_add (struct window *, char const *, ...)
12503 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12504
12505 static void
12506 debug_method_add (struct window *w, char const *fmt, ...)
12507 {
12508 char *method = w->desired_matrix->method;
12509 int len = strlen (method);
12510 int size = sizeof w->desired_matrix->method;
12511 int remaining = size - len - 1;
12512 va_list ap;
12513
12514 if (len && remaining)
12515 {
12516 method[len] = '|';
12517 --remaining, ++len;
12518 }
12519
12520 va_start (ap, fmt);
12521 vsnprintf (method + len, remaining + 1, fmt, ap);
12522 va_end (ap);
12523
12524 if (trace_redisplay_p)
12525 fprintf (stderr, "%p (%s): %s\n",
12526 w,
12527 ((BUFFERP (w->buffer)
12528 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12529 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12530 : "no buffer"),
12531 method + len);
12532 }
12533
12534 #endif /* GLYPH_DEBUG */
12535
12536
12537 /* Value is non-zero if all changes in window W, which displays
12538 current_buffer, are in the text between START and END. START is a
12539 buffer position, END is given as a distance from Z. Used in
12540 redisplay_internal for display optimization. */
12541
12542 static inline int
12543 text_outside_line_unchanged_p (struct window *w,
12544 ptrdiff_t start, ptrdiff_t end)
12545 {
12546 int unchanged_p = 1;
12547
12548 /* If text or overlays have changed, see where. */
12549 if (w->last_modified < MODIFF
12550 || w->last_overlay_modified < OVERLAY_MODIFF)
12551 {
12552 /* Gap in the line? */
12553 if (GPT < start || Z - GPT < end)
12554 unchanged_p = 0;
12555
12556 /* Changes start in front of the line, or end after it? */
12557 if (unchanged_p
12558 && (BEG_UNCHANGED < start - 1
12559 || END_UNCHANGED < end))
12560 unchanged_p = 0;
12561
12562 /* If selective display, can't optimize if changes start at the
12563 beginning of the line. */
12564 if (unchanged_p
12565 && INTEGERP (BVAR (current_buffer, selective_display))
12566 && XINT (BVAR (current_buffer, selective_display)) > 0
12567 && (BEG_UNCHANGED < start || GPT <= start))
12568 unchanged_p = 0;
12569
12570 /* If there are overlays at the start or end of the line, these
12571 may have overlay strings with newlines in them. A change at
12572 START, for instance, may actually concern the display of such
12573 overlay strings as well, and they are displayed on different
12574 lines. So, quickly rule out this case. (For the future, it
12575 might be desirable to implement something more telling than
12576 just BEG/END_UNCHANGED.) */
12577 if (unchanged_p)
12578 {
12579 if (BEG + BEG_UNCHANGED == start
12580 && overlay_touches_p (start))
12581 unchanged_p = 0;
12582 if (END_UNCHANGED == end
12583 && overlay_touches_p (Z - end))
12584 unchanged_p = 0;
12585 }
12586
12587 /* Under bidi reordering, adding or deleting a character in the
12588 beginning of a paragraph, before the first strong directional
12589 character, can change the base direction of the paragraph (unless
12590 the buffer specifies a fixed paragraph direction), which will
12591 require to redisplay the whole paragraph. It might be worthwhile
12592 to find the paragraph limits and widen the range of redisplayed
12593 lines to that, but for now just give up this optimization. */
12594 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12595 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12596 unchanged_p = 0;
12597 }
12598
12599 return unchanged_p;
12600 }
12601
12602
12603 /* Do a frame update, taking possible shortcuts into account. This is
12604 the main external entry point for redisplay.
12605
12606 If the last redisplay displayed an echo area message and that message
12607 is no longer requested, we clear the echo area or bring back the
12608 mini-buffer if that is in use. */
12609
12610 void
12611 redisplay (void)
12612 {
12613 redisplay_internal ();
12614 }
12615
12616
12617 static Lisp_Object
12618 overlay_arrow_string_or_property (Lisp_Object var)
12619 {
12620 Lisp_Object val;
12621
12622 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12623 return val;
12624
12625 return Voverlay_arrow_string;
12626 }
12627
12628 /* Return 1 if there are any overlay-arrows in current_buffer. */
12629 static int
12630 overlay_arrow_in_current_buffer_p (void)
12631 {
12632 Lisp_Object vlist;
12633
12634 for (vlist = Voverlay_arrow_variable_list;
12635 CONSP (vlist);
12636 vlist = XCDR (vlist))
12637 {
12638 Lisp_Object var = XCAR (vlist);
12639 Lisp_Object val;
12640
12641 if (!SYMBOLP (var))
12642 continue;
12643 val = find_symbol_value (var);
12644 if (MARKERP (val)
12645 && current_buffer == XMARKER (val)->buffer)
12646 return 1;
12647 }
12648 return 0;
12649 }
12650
12651
12652 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12653 has changed. */
12654
12655 static int
12656 overlay_arrows_changed_p (void)
12657 {
12658 Lisp_Object vlist;
12659
12660 for (vlist = Voverlay_arrow_variable_list;
12661 CONSP (vlist);
12662 vlist = XCDR (vlist))
12663 {
12664 Lisp_Object var = XCAR (vlist);
12665 Lisp_Object val, pstr;
12666
12667 if (!SYMBOLP (var))
12668 continue;
12669 val = find_symbol_value (var);
12670 if (!MARKERP (val))
12671 continue;
12672 if (! EQ (COERCE_MARKER (val),
12673 Fget (var, Qlast_arrow_position))
12674 || ! (pstr = overlay_arrow_string_or_property (var),
12675 EQ (pstr, Fget (var, Qlast_arrow_string))))
12676 return 1;
12677 }
12678 return 0;
12679 }
12680
12681 /* Mark overlay arrows to be updated on next redisplay. */
12682
12683 static void
12684 update_overlay_arrows (int up_to_date)
12685 {
12686 Lisp_Object vlist;
12687
12688 for (vlist = Voverlay_arrow_variable_list;
12689 CONSP (vlist);
12690 vlist = XCDR (vlist))
12691 {
12692 Lisp_Object var = XCAR (vlist);
12693
12694 if (!SYMBOLP (var))
12695 continue;
12696
12697 if (up_to_date > 0)
12698 {
12699 Lisp_Object val = find_symbol_value (var);
12700 Fput (var, Qlast_arrow_position,
12701 COERCE_MARKER (val));
12702 Fput (var, Qlast_arrow_string,
12703 overlay_arrow_string_or_property (var));
12704 }
12705 else if (up_to_date < 0
12706 || !NILP (Fget (var, Qlast_arrow_position)))
12707 {
12708 Fput (var, Qlast_arrow_position, Qt);
12709 Fput (var, Qlast_arrow_string, Qt);
12710 }
12711 }
12712 }
12713
12714
12715 /* Return overlay arrow string to display at row.
12716 Return integer (bitmap number) for arrow bitmap in left fringe.
12717 Return nil if no overlay arrow. */
12718
12719 static Lisp_Object
12720 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12721 {
12722 Lisp_Object vlist;
12723
12724 for (vlist = Voverlay_arrow_variable_list;
12725 CONSP (vlist);
12726 vlist = XCDR (vlist))
12727 {
12728 Lisp_Object var = XCAR (vlist);
12729 Lisp_Object val;
12730
12731 if (!SYMBOLP (var))
12732 continue;
12733
12734 val = find_symbol_value (var);
12735
12736 if (MARKERP (val)
12737 && current_buffer == XMARKER (val)->buffer
12738 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12739 {
12740 if (FRAME_WINDOW_P (it->f)
12741 /* FIXME: if ROW->reversed_p is set, this should test
12742 the right fringe, not the left one. */
12743 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12744 {
12745 #ifdef HAVE_WINDOW_SYSTEM
12746 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12747 {
12748 int fringe_bitmap;
12749 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12750 return make_number (fringe_bitmap);
12751 }
12752 #endif
12753 return make_number (-1); /* Use default arrow bitmap */
12754 }
12755 return overlay_arrow_string_or_property (var);
12756 }
12757 }
12758
12759 return Qnil;
12760 }
12761
12762 /* Return 1 if point moved out of or into a composition. Otherwise
12763 return 0. PREV_BUF and PREV_PT are the last point buffer and
12764 position. BUF and PT are the current point buffer and position. */
12765
12766 static int
12767 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12768 struct buffer *buf, ptrdiff_t pt)
12769 {
12770 ptrdiff_t start, end;
12771 Lisp_Object prop;
12772 Lisp_Object buffer;
12773
12774 XSETBUFFER (buffer, buf);
12775 /* Check a composition at the last point if point moved within the
12776 same buffer. */
12777 if (prev_buf == buf)
12778 {
12779 if (prev_pt == pt)
12780 /* Point didn't move. */
12781 return 0;
12782
12783 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12784 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12785 && COMPOSITION_VALID_P (start, end, prop)
12786 && start < prev_pt && end > prev_pt)
12787 /* The last point was within the composition. Return 1 iff
12788 point moved out of the composition. */
12789 return (pt <= start || pt >= end);
12790 }
12791
12792 /* Check a composition at the current point. */
12793 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12794 && find_composition (pt, -1, &start, &end, &prop, buffer)
12795 && COMPOSITION_VALID_P (start, end, prop)
12796 && start < pt && end > pt);
12797 }
12798
12799
12800 /* Reconsider the setting of B->clip_changed which is displayed
12801 in window W. */
12802
12803 static inline void
12804 reconsider_clip_changes (struct window *w, struct buffer *b)
12805 {
12806 if (b->clip_changed
12807 && !NILP (w->window_end_valid)
12808 && w->current_matrix->buffer == b
12809 && w->current_matrix->zv == BUF_ZV (b)
12810 && w->current_matrix->begv == BUF_BEGV (b))
12811 b->clip_changed = 0;
12812
12813 /* If display wasn't paused, and W is not a tool bar window, see if
12814 point has been moved into or out of a composition. In that case,
12815 we set b->clip_changed to 1 to force updating the screen. If
12816 b->clip_changed has already been set to 1, we can skip this
12817 check. */
12818 if (!b->clip_changed
12819 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12820 {
12821 ptrdiff_t pt;
12822
12823 if (w == XWINDOW (selected_window))
12824 pt = PT;
12825 else
12826 pt = marker_position (w->pointm);
12827
12828 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12829 || pt != w->last_point)
12830 && check_point_in_composition (w->current_matrix->buffer,
12831 w->last_point,
12832 XBUFFER (w->buffer), pt))
12833 b->clip_changed = 1;
12834 }
12835 }
12836 \f
12837
12838 /* Select FRAME to forward the values of frame-local variables into C
12839 variables so that the redisplay routines can access those values
12840 directly. */
12841
12842 static void
12843 select_frame_for_redisplay (Lisp_Object frame)
12844 {
12845 Lisp_Object tail, tem;
12846 Lisp_Object old = selected_frame;
12847 struct Lisp_Symbol *sym;
12848
12849 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12850
12851 selected_frame = frame;
12852
12853 do {
12854 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12855 if (CONSP (XCAR (tail))
12856 && (tem = XCAR (XCAR (tail)),
12857 SYMBOLP (tem))
12858 && (sym = indirect_variable (XSYMBOL (tem)),
12859 sym->redirect == SYMBOL_LOCALIZED)
12860 && sym->val.blv->frame_local)
12861 /* Use find_symbol_value rather than Fsymbol_value
12862 to avoid an error if it is void. */
12863 find_symbol_value (tem);
12864 } while (!EQ (frame, old) && (frame = old, 1));
12865 }
12866
12867
12868 #define STOP_POLLING \
12869 do { if (! polling_stopped_here) stop_polling (); \
12870 polling_stopped_here = 1; } while (0)
12871
12872 #define RESUME_POLLING \
12873 do { if (polling_stopped_here) start_polling (); \
12874 polling_stopped_here = 0; } while (0)
12875
12876
12877 /* Perhaps in the future avoid recentering windows if it
12878 is not necessary; currently that causes some problems. */
12879
12880 static void
12881 redisplay_internal (void)
12882 {
12883 struct window *w = XWINDOW (selected_window);
12884 struct window *sw;
12885 struct frame *fr;
12886 int pending;
12887 int must_finish = 0;
12888 struct text_pos tlbufpos, tlendpos;
12889 int number_of_visible_frames;
12890 ptrdiff_t count, count1;
12891 struct frame *sf;
12892 int polling_stopped_here = 0;
12893 Lisp_Object old_frame = selected_frame;
12894
12895 /* Non-zero means redisplay has to consider all windows on all
12896 frames. Zero means, only selected_window is considered. */
12897 int consider_all_windows_p;
12898
12899 /* Non-zero means redisplay has to redisplay the miniwindow */
12900 int update_miniwindow_p = 0;
12901
12902 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12903
12904 /* No redisplay if running in batch mode or frame is not yet fully
12905 initialized, or redisplay is explicitly turned off by setting
12906 Vinhibit_redisplay. */
12907 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12908 || !NILP (Vinhibit_redisplay))
12909 return;
12910
12911 /* Don't examine these until after testing Vinhibit_redisplay.
12912 When Emacs is shutting down, perhaps because its connection to
12913 X has dropped, we should not look at them at all. */
12914 fr = XFRAME (w->frame);
12915 sf = SELECTED_FRAME ();
12916
12917 if (!fr->glyphs_initialized_p)
12918 return;
12919
12920 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12921 if (popup_activated ())
12922 return;
12923 #endif
12924
12925 /* I don't think this happens but let's be paranoid. */
12926 if (redisplaying_p)
12927 return;
12928
12929 /* Record a function that resets redisplaying_p to its old value
12930 when we leave this function. */
12931 count = SPECPDL_INDEX ();
12932 record_unwind_protect (unwind_redisplay,
12933 Fcons (make_number (redisplaying_p), selected_frame));
12934 ++redisplaying_p;
12935 specbind (Qinhibit_free_realized_faces, Qnil);
12936
12937 {
12938 Lisp_Object tail, frame;
12939
12940 FOR_EACH_FRAME (tail, frame)
12941 {
12942 struct frame *f = XFRAME (frame);
12943 f->already_hscrolled_p = 0;
12944 }
12945 }
12946
12947 retry:
12948 /* Remember the currently selected window. */
12949 sw = w;
12950
12951 if (!EQ (old_frame, selected_frame)
12952 && FRAME_LIVE_P (XFRAME (old_frame)))
12953 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12954 selected_frame and selected_window to be temporarily out-of-sync so
12955 when we come back here via `goto retry', we need to resync because we
12956 may need to run Elisp code (via prepare_menu_bars). */
12957 select_frame_for_redisplay (old_frame);
12958
12959 pending = 0;
12960 reconsider_clip_changes (w, current_buffer);
12961 last_escape_glyph_frame = NULL;
12962 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12963 last_glyphless_glyph_frame = NULL;
12964 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12965
12966 /* If new fonts have been loaded that make a glyph matrix adjustment
12967 necessary, do it. */
12968 if (fonts_changed_p)
12969 {
12970 adjust_glyphs (NULL);
12971 ++windows_or_buffers_changed;
12972 fonts_changed_p = 0;
12973 }
12974
12975 /* If face_change_count is non-zero, init_iterator will free all
12976 realized faces, which includes the faces referenced from current
12977 matrices. So, we can't reuse current matrices in this case. */
12978 if (face_change_count)
12979 ++windows_or_buffers_changed;
12980
12981 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12982 && FRAME_TTY (sf)->previous_frame != sf)
12983 {
12984 /* Since frames on a single ASCII terminal share the same
12985 display area, displaying a different frame means redisplay
12986 the whole thing. */
12987 windows_or_buffers_changed++;
12988 SET_FRAME_GARBAGED (sf);
12989 #ifndef DOS_NT
12990 set_tty_color_mode (FRAME_TTY (sf), sf);
12991 #endif
12992 FRAME_TTY (sf)->previous_frame = sf;
12993 }
12994
12995 /* Set the visible flags for all frames. Do this before checking
12996 for resized or garbaged frames; they want to know if their frames
12997 are visible. See the comment in frame.h for
12998 FRAME_SAMPLE_VISIBILITY. */
12999 {
13000 Lisp_Object tail, frame;
13001
13002 number_of_visible_frames = 0;
13003
13004 FOR_EACH_FRAME (tail, frame)
13005 {
13006 struct frame *f = XFRAME (frame);
13007
13008 FRAME_SAMPLE_VISIBILITY (f);
13009 if (FRAME_VISIBLE_P (f))
13010 ++number_of_visible_frames;
13011 clear_desired_matrices (f);
13012 }
13013 }
13014
13015 /* Notice any pending interrupt request to change frame size. */
13016 do_pending_window_change (1);
13017
13018 /* do_pending_window_change could change the selected_window due to
13019 frame resizing which makes the selected window too small. */
13020 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13021 {
13022 sw = w;
13023 reconsider_clip_changes (w, current_buffer);
13024 }
13025
13026 /* Clear frames marked as garbaged. */
13027 if (frame_garbaged)
13028 clear_garbaged_frames ();
13029
13030 /* Build menubar and tool-bar items. */
13031 if (NILP (Vmemory_full))
13032 prepare_menu_bars ();
13033
13034 if (windows_or_buffers_changed)
13035 update_mode_lines++;
13036
13037 /* Detect case that we need to write or remove a star in the mode line. */
13038 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13039 {
13040 w->update_mode_line = 1;
13041 if (buffer_shared > 1)
13042 update_mode_lines++;
13043 }
13044
13045 /* Avoid invocation of point motion hooks by `current_column' below. */
13046 count1 = SPECPDL_INDEX ();
13047 specbind (Qinhibit_point_motion_hooks, Qt);
13048
13049 /* If %c is in the mode line, update it if needed. */
13050 if (!NILP (w->column_number_displayed)
13051 /* This alternative quickly identifies a common case
13052 where no change is needed. */
13053 && !(PT == w->last_point
13054 && w->last_modified >= MODIFF
13055 && w->last_overlay_modified >= OVERLAY_MODIFF)
13056 && (XFASTINT (w->column_number_displayed) != current_column ()))
13057 w->update_mode_line = 1;
13058
13059 unbind_to (count1, Qnil);
13060
13061 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13062
13063 /* The variable buffer_shared is set in redisplay_window and
13064 indicates that we redisplay a buffer in different windows. See
13065 there. */
13066 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13067 || cursor_type_changed);
13068
13069 /* If specs for an arrow have changed, do thorough redisplay
13070 to ensure we remove any arrow that should no longer exist. */
13071 if (overlay_arrows_changed_p ())
13072 consider_all_windows_p = windows_or_buffers_changed = 1;
13073
13074 /* Normally the message* functions will have already displayed and
13075 updated the echo area, but the frame may have been trashed, or
13076 the update may have been preempted, so display the echo area
13077 again here. Checking message_cleared_p captures the case that
13078 the echo area should be cleared. */
13079 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13080 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13081 || (message_cleared_p
13082 && minibuf_level == 0
13083 /* If the mini-window is currently selected, this means the
13084 echo-area doesn't show through. */
13085 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13086 {
13087 int window_height_changed_p = echo_area_display (0);
13088
13089 if (message_cleared_p)
13090 update_miniwindow_p = 1;
13091
13092 must_finish = 1;
13093
13094 /* If we don't display the current message, don't clear the
13095 message_cleared_p flag, because, if we did, we wouldn't clear
13096 the echo area in the next redisplay which doesn't preserve
13097 the echo area. */
13098 if (!display_last_displayed_message_p)
13099 message_cleared_p = 0;
13100
13101 if (fonts_changed_p)
13102 goto retry;
13103 else if (window_height_changed_p)
13104 {
13105 consider_all_windows_p = 1;
13106 ++update_mode_lines;
13107 ++windows_or_buffers_changed;
13108
13109 /* If window configuration was changed, frames may have been
13110 marked garbaged. Clear them or we will experience
13111 surprises wrt scrolling. */
13112 if (frame_garbaged)
13113 clear_garbaged_frames ();
13114 }
13115 }
13116 else if (EQ (selected_window, minibuf_window)
13117 && (current_buffer->clip_changed
13118 || w->last_modified < MODIFF
13119 || w->last_overlay_modified < OVERLAY_MODIFF)
13120 && resize_mini_window (w, 0))
13121 {
13122 /* Resized active mini-window to fit the size of what it is
13123 showing if its contents might have changed. */
13124 must_finish = 1;
13125 /* FIXME: this causes all frames to be updated, which seems unnecessary
13126 since only the current frame needs to be considered. This function needs
13127 to be rewritten with two variables, consider_all_windows and
13128 consider_all_frames. */
13129 consider_all_windows_p = 1;
13130 ++windows_or_buffers_changed;
13131 ++update_mode_lines;
13132
13133 /* If window configuration was changed, frames may have been
13134 marked garbaged. Clear them or we will experience
13135 surprises wrt scrolling. */
13136 if (frame_garbaged)
13137 clear_garbaged_frames ();
13138 }
13139
13140
13141 /* If showing the region, and mark has changed, we must redisplay
13142 the whole window. The assignment to this_line_start_pos prevents
13143 the optimization directly below this if-statement. */
13144 if (((!NILP (Vtransient_mark_mode)
13145 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13146 != !NILP (w->region_showing))
13147 || (!NILP (w->region_showing)
13148 && !EQ (w->region_showing,
13149 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13150 CHARPOS (this_line_start_pos) = 0;
13151
13152 /* Optimize the case that only the line containing the cursor in the
13153 selected window has changed. Variables starting with this_ are
13154 set in display_line and record information about the line
13155 containing the cursor. */
13156 tlbufpos = this_line_start_pos;
13157 tlendpos = this_line_end_pos;
13158 if (!consider_all_windows_p
13159 && CHARPOS (tlbufpos) > 0
13160 && !w->update_mode_line
13161 && !current_buffer->clip_changed
13162 && !current_buffer->prevent_redisplay_optimizations_p
13163 && FRAME_VISIBLE_P (XFRAME (w->frame))
13164 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13165 /* Make sure recorded data applies to current buffer, etc. */
13166 && this_line_buffer == current_buffer
13167 && current_buffer == XBUFFER (w->buffer)
13168 && !w->force_start
13169 && !w->optional_new_start
13170 /* Point must be on the line that we have info recorded about. */
13171 && PT >= CHARPOS (tlbufpos)
13172 && PT <= Z - CHARPOS (tlendpos)
13173 /* All text outside that line, including its final newline,
13174 must be unchanged. */
13175 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13176 CHARPOS (tlendpos)))
13177 {
13178 if (CHARPOS (tlbufpos) > BEGV
13179 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13180 && (CHARPOS (tlbufpos) == ZV
13181 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13182 /* Former continuation line has disappeared by becoming empty. */
13183 goto cancel;
13184 else if (w->last_modified < MODIFF
13185 || w->last_overlay_modified < OVERLAY_MODIFF
13186 || MINI_WINDOW_P (w))
13187 {
13188 /* We have to handle the case of continuation around a
13189 wide-column character (see the comment in indent.c around
13190 line 1340).
13191
13192 For instance, in the following case:
13193
13194 -------- Insert --------
13195 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13196 J_I_ ==> J_I_ `^^' are cursors.
13197 ^^ ^^
13198 -------- --------
13199
13200 As we have to redraw the line above, we cannot use this
13201 optimization. */
13202
13203 struct it it;
13204 int line_height_before = this_line_pixel_height;
13205
13206 /* Note that start_display will handle the case that the
13207 line starting at tlbufpos is a continuation line. */
13208 start_display (&it, w, tlbufpos);
13209
13210 /* Implementation note: It this still necessary? */
13211 if (it.current_x != this_line_start_x)
13212 goto cancel;
13213
13214 TRACE ((stderr, "trying display optimization 1\n"));
13215 w->cursor.vpos = -1;
13216 overlay_arrow_seen = 0;
13217 it.vpos = this_line_vpos;
13218 it.current_y = this_line_y;
13219 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13220 display_line (&it);
13221
13222 /* If line contains point, is not continued,
13223 and ends at same distance from eob as before, we win. */
13224 if (w->cursor.vpos >= 0
13225 /* Line is not continued, otherwise this_line_start_pos
13226 would have been set to 0 in display_line. */
13227 && CHARPOS (this_line_start_pos)
13228 /* Line ends as before. */
13229 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13230 /* Line has same height as before. Otherwise other lines
13231 would have to be shifted up or down. */
13232 && this_line_pixel_height == line_height_before)
13233 {
13234 /* If this is not the window's last line, we must adjust
13235 the charstarts of the lines below. */
13236 if (it.current_y < it.last_visible_y)
13237 {
13238 struct glyph_row *row
13239 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13240 ptrdiff_t delta, delta_bytes;
13241
13242 /* We used to distinguish between two cases here,
13243 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13244 when the line ends in a newline or the end of the
13245 buffer's accessible portion. But both cases did
13246 the same, so they were collapsed. */
13247 delta = (Z
13248 - CHARPOS (tlendpos)
13249 - MATRIX_ROW_START_CHARPOS (row));
13250 delta_bytes = (Z_BYTE
13251 - BYTEPOS (tlendpos)
13252 - MATRIX_ROW_START_BYTEPOS (row));
13253
13254 increment_matrix_positions (w->current_matrix,
13255 this_line_vpos + 1,
13256 w->current_matrix->nrows,
13257 delta, delta_bytes);
13258 }
13259
13260 /* If this row displays text now but previously didn't,
13261 or vice versa, w->window_end_vpos may have to be
13262 adjusted. */
13263 if ((it.glyph_row - 1)->displays_text_p)
13264 {
13265 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13266 XSETINT (w->window_end_vpos, this_line_vpos);
13267 }
13268 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13269 && this_line_vpos > 0)
13270 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13271 w->window_end_valid = Qnil;
13272
13273 /* Update hint: No need to try to scroll in update_window. */
13274 w->desired_matrix->no_scrolling_p = 1;
13275
13276 #ifdef GLYPH_DEBUG
13277 *w->desired_matrix->method = 0;
13278 debug_method_add (w, "optimization 1");
13279 #endif
13280 #ifdef HAVE_WINDOW_SYSTEM
13281 update_window_fringes (w, 0);
13282 #endif
13283 goto update;
13284 }
13285 else
13286 goto cancel;
13287 }
13288 else if (/* Cursor position hasn't changed. */
13289 PT == w->last_point
13290 /* Make sure the cursor was last displayed
13291 in this window. Otherwise we have to reposition it. */
13292 && 0 <= w->cursor.vpos
13293 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13294 {
13295 if (!must_finish)
13296 {
13297 do_pending_window_change (1);
13298 /* If selected_window changed, redisplay again. */
13299 if (WINDOWP (selected_window)
13300 && (w = XWINDOW (selected_window)) != sw)
13301 goto retry;
13302
13303 /* We used to always goto end_of_redisplay here, but this
13304 isn't enough if we have a blinking cursor. */
13305 if (w->cursor_off_p == w->last_cursor_off_p)
13306 goto end_of_redisplay;
13307 }
13308 goto update;
13309 }
13310 /* If highlighting the region, or if the cursor is in the echo area,
13311 then we can't just move the cursor. */
13312 else if (! (!NILP (Vtransient_mark_mode)
13313 && !NILP (BVAR (current_buffer, mark_active)))
13314 && (EQ (selected_window,
13315 BVAR (current_buffer, last_selected_window))
13316 || highlight_nonselected_windows)
13317 && NILP (w->region_showing)
13318 && NILP (Vshow_trailing_whitespace)
13319 && !cursor_in_echo_area)
13320 {
13321 struct it it;
13322 struct glyph_row *row;
13323
13324 /* Skip from tlbufpos to PT and see where it is. Note that
13325 PT may be in invisible text. If so, we will end at the
13326 next visible position. */
13327 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13328 NULL, DEFAULT_FACE_ID);
13329 it.current_x = this_line_start_x;
13330 it.current_y = this_line_y;
13331 it.vpos = this_line_vpos;
13332
13333 /* The call to move_it_to stops in front of PT, but
13334 moves over before-strings. */
13335 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13336
13337 if (it.vpos == this_line_vpos
13338 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13339 row->enabled_p))
13340 {
13341 eassert (this_line_vpos == it.vpos);
13342 eassert (this_line_y == it.current_y);
13343 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13344 #ifdef GLYPH_DEBUG
13345 *w->desired_matrix->method = 0;
13346 debug_method_add (w, "optimization 3");
13347 #endif
13348 goto update;
13349 }
13350 else
13351 goto cancel;
13352 }
13353
13354 cancel:
13355 /* Text changed drastically or point moved off of line. */
13356 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13357 }
13358
13359 CHARPOS (this_line_start_pos) = 0;
13360 consider_all_windows_p |= buffer_shared > 1;
13361 ++clear_face_cache_count;
13362 #ifdef HAVE_WINDOW_SYSTEM
13363 ++clear_image_cache_count;
13364 #endif
13365
13366 /* Build desired matrices, and update the display. If
13367 consider_all_windows_p is non-zero, do it for all windows on all
13368 frames. Otherwise do it for selected_window, only. */
13369
13370 if (consider_all_windows_p)
13371 {
13372 Lisp_Object tail, frame;
13373
13374 FOR_EACH_FRAME (tail, frame)
13375 XFRAME (frame)->updated_p = 0;
13376
13377 /* Recompute # windows showing selected buffer. This will be
13378 incremented each time such a window is displayed. */
13379 buffer_shared = 0;
13380
13381 FOR_EACH_FRAME (tail, frame)
13382 {
13383 struct frame *f = XFRAME (frame);
13384
13385 /* We don't have to do anything for unselected terminal
13386 frames. */
13387 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13388 && !EQ (FRAME_TTY (f)->top_frame, frame))
13389 continue;
13390
13391 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13392 {
13393 if (! EQ (frame, selected_frame))
13394 /* Select the frame, for the sake of frame-local
13395 variables. */
13396 select_frame_for_redisplay (frame);
13397
13398 /* Mark all the scroll bars to be removed; we'll redeem
13399 the ones we want when we redisplay their windows. */
13400 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13401 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13402
13403 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13404 redisplay_windows (FRAME_ROOT_WINDOW (f));
13405
13406 /* The X error handler may have deleted that frame. */
13407 if (!FRAME_LIVE_P (f))
13408 continue;
13409
13410 /* Any scroll bars which redisplay_windows should have
13411 nuked should now go away. */
13412 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13413 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13414
13415 /* If fonts changed, display again. */
13416 /* ??? rms: I suspect it is a mistake to jump all the way
13417 back to retry here. It should just retry this frame. */
13418 if (fonts_changed_p)
13419 goto retry;
13420
13421 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13422 {
13423 /* See if we have to hscroll. */
13424 if (!f->already_hscrolled_p)
13425 {
13426 f->already_hscrolled_p = 1;
13427 if (hscroll_windows (f->root_window))
13428 goto retry;
13429 }
13430
13431 /* Prevent various kinds of signals during display
13432 update. stdio is not robust about handling
13433 signals, which can cause an apparent I/O
13434 error. */
13435 if (interrupt_input)
13436 unrequest_sigio ();
13437 STOP_POLLING;
13438
13439 /* Update the display. */
13440 set_window_update_flags (XWINDOW (f->root_window), 1);
13441 pending |= update_frame (f, 0, 0);
13442 f->updated_p = 1;
13443 }
13444 }
13445 }
13446
13447 if (!EQ (old_frame, selected_frame)
13448 && FRAME_LIVE_P (XFRAME (old_frame)))
13449 /* We played a bit fast-and-loose above and allowed selected_frame
13450 and selected_window to be temporarily out-of-sync but let's make
13451 sure this stays contained. */
13452 select_frame_for_redisplay (old_frame);
13453 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13454
13455 if (!pending)
13456 {
13457 /* Do the mark_window_display_accurate after all windows have
13458 been redisplayed because this call resets flags in buffers
13459 which are needed for proper redisplay. */
13460 FOR_EACH_FRAME (tail, frame)
13461 {
13462 struct frame *f = XFRAME (frame);
13463 if (f->updated_p)
13464 {
13465 mark_window_display_accurate (f->root_window, 1);
13466 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13467 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13468 }
13469 }
13470 }
13471 }
13472 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13473 {
13474 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13475 struct frame *mini_frame;
13476
13477 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13478 /* Use list_of_error, not Qerror, so that
13479 we catch only errors and don't run the debugger. */
13480 internal_condition_case_1 (redisplay_window_1, selected_window,
13481 list_of_error,
13482 redisplay_window_error);
13483 if (update_miniwindow_p)
13484 internal_condition_case_1 (redisplay_window_1, mini_window,
13485 list_of_error,
13486 redisplay_window_error);
13487
13488 /* Compare desired and current matrices, perform output. */
13489
13490 update:
13491 /* If fonts changed, display again. */
13492 if (fonts_changed_p)
13493 goto retry;
13494
13495 /* Prevent various kinds of signals during display update.
13496 stdio is not robust about handling signals,
13497 which can cause an apparent I/O error. */
13498 if (interrupt_input)
13499 unrequest_sigio ();
13500 STOP_POLLING;
13501
13502 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13503 {
13504 if (hscroll_windows (selected_window))
13505 goto retry;
13506
13507 XWINDOW (selected_window)->must_be_updated_p = 1;
13508 pending = update_frame (sf, 0, 0);
13509 }
13510
13511 /* We may have called echo_area_display at the top of this
13512 function. If the echo area is on another frame, that may
13513 have put text on a frame other than the selected one, so the
13514 above call to update_frame would not have caught it. Catch
13515 it here. */
13516 mini_window = FRAME_MINIBUF_WINDOW (sf);
13517 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13518
13519 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13520 {
13521 XWINDOW (mini_window)->must_be_updated_p = 1;
13522 pending |= update_frame (mini_frame, 0, 0);
13523 if (!pending && hscroll_windows (mini_window))
13524 goto retry;
13525 }
13526 }
13527
13528 /* If display was paused because of pending input, make sure we do a
13529 thorough update the next time. */
13530 if (pending)
13531 {
13532 /* Prevent the optimization at the beginning of
13533 redisplay_internal that tries a single-line update of the
13534 line containing the cursor in the selected window. */
13535 CHARPOS (this_line_start_pos) = 0;
13536
13537 /* Let the overlay arrow be updated the next time. */
13538 update_overlay_arrows (0);
13539
13540 /* If we pause after scrolling, some rows in the current
13541 matrices of some windows are not valid. */
13542 if (!WINDOW_FULL_WIDTH_P (w)
13543 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13544 update_mode_lines = 1;
13545 }
13546 else
13547 {
13548 if (!consider_all_windows_p)
13549 {
13550 /* This has already been done above if
13551 consider_all_windows_p is set. */
13552 mark_window_display_accurate_1 (w, 1);
13553
13554 /* Say overlay arrows are up to date. */
13555 update_overlay_arrows (1);
13556
13557 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13558 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13559 }
13560
13561 update_mode_lines = 0;
13562 windows_or_buffers_changed = 0;
13563 cursor_type_changed = 0;
13564 }
13565
13566 /* Start SIGIO interrupts coming again. Having them off during the
13567 code above makes it less likely one will discard output, but not
13568 impossible, since there might be stuff in the system buffer here.
13569 But it is much hairier to try to do anything about that. */
13570 if (interrupt_input)
13571 request_sigio ();
13572 RESUME_POLLING;
13573
13574 /* If a frame has become visible which was not before, redisplay
13575 again, so that we display it. Expose events for such a frame
13576 (which it gets when becoming visible) don't call the parts of
13577 redisplay constructing glyphs, so simply exposing a frame won't
13578 display anything in this case. So, we have to display these
13579 frames here explicitly. */
13580 if (!pending)
13581 {
13582 Lisp_Object tail, frame;
13583 int new_count = 0;
13584
13585 FOR_EACH_FRAME (tail, frame)
13586 {
13587 int this_is_visible = 0;
13588
13589 if (XFRAME (frame)->visible)
13590 this_is_visible = 1;
13591 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13592 if (XFRAME (frame)->visible)
13593 this_is_visible = 1;
13594
13595 if (this_is_visible)
13596 new_count++;
13597 }
13598
13599 if (new_count != number_of_visible_frames)
13600 windows_or_buffers_changed++;
13601 }
13602
13603 /* Change frame size now if a change is pending. */
13604 do_pending_window_change (1);
13605
13606 /* If we just did a pending size change, or have additional
13607 visible frames, or selected_window changed, redisplay again. */
13608 if ((windows_or_buffers_changed && !pending)
13609 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13610 goto retry;
13611
13612 /* Clear the face and image caches.
13613
13614 We used to do this only if consider_all_windows_p. But the cache
13615 needs to be cleared if a timer creates images in the current
13616 buffer (e.g. the test case in Bug#6230). */
13617
13618 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13619 {
13620 clear_face_cache (0);
13621 clear_face_cache_count = 0;
13622 }
13623
13624 #ifdef HAVE_WINDOW_SYSTEM
13625 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13626 {
13627 clear_image_caches (Qnil);
13628 clear_image_cache_count = 0;
13629 }
13630 #endif /* HAVE_WINDOW_SYSTEM */
13631
13632 end_of_redisplay:
13633 unbind_to (count, Qnil);
13634 RESUME_POLLING;
13635 }
13636
13637
13638 /* Redisplay, but leave alone any recent echo area message unless
13639 another message has been requested in its place.
13640
13641 This is useful in situations where you need to redisplay but no
13642 user action has occurred, making it inappropriate for the message
13643 area to be cleared. See tracking_off and
13644 wait_reading_process_output for examples of these situations.
13645
13646 FROM_WHERE is an integer saying from where this function was
13647 called. This is useful for debugging. */
13648
13649 void
13650 redisplay_preserve_echo_area (int from_where)
13651 {
13652 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13653
13654 if (!NILP (echo_area_buffer[1]))
13655 {
13656 /* We have a previously displayed message, but no current
13657 message. Redisplay the previous message. */
13658 display_last_displayed_message_p = 1;
13659 redisplay_internal ();
13660 display_last_displayed_message_p = 0;
13661 }
13662 else
13663 redisplay_internal ();
13664
13665 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13666 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13667 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13668 }
13669
13670
13671 /* Function registered with record_unwind_protect in
13672 redisplay_internal. Reset redisplaying_p to the value it had
13673 before redisplay_internal was called, and clear
13674 prevent_freeing_realized_faces_p. It also selects the previously
13675 selected frame, unless it has been deleted (by an X connection
13676 failure during redisplay, for example). */
13677
13678 static Lisp_Object
13679 unwind_redisplay (Lisp_Object val)
13680 {
13681 Lisp_Object old_redisplaying_p, old_frame;
13682
13683 old_redisplaying_p = XCAR (val);
13684 redisplaying_p = XFASTINT (old_redisplaying_p);
13685 old_frame = XCDR (val);
13686 if (! EQ (old_frame, selected_frame)
13687 && FRAME_LIVE_P (XFRAME (old_frame)))
13688 select_frame_for_redisplay (old_frame);
13689 return Qnil;
13690 }
13691
13692
13693 /* Mark the display of window W as accurate or inaccurate. If
13694 ACCURATE_P is non-zero mark display of W as accurate. If
13695 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13696 redisplay_internal is called. */
13697
13698 static void
13699 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13700 {
13701 if (BUFFERP (w->buffer))
13702 {
13703 struct buffer *b = XBUFFER (w->buffer);
13704
13705 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13706 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13707 w->last_had_star
13708 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13709
13710 if (accurate_p)
13711 {
13712 b->clip_changed = 0;
13713 b->prevent_redisplay_optimizations_p = 0;
13714
13715 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13716 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13717 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13718 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13719
13720 w->current_matrix->buffer = b;
13721 w->current_matrix->begv = BUF_BEGV (b);
13722 w->current_matrix->zv = BUF_ZV (b);
13723
13724 w->last_cursor = w->cursor;
13725 w->last_cursor_off_p = w->cursor_off_p;
13726
13727 if (w == XWINDOW (selected_window))
13728 w->last_point = BUF_PT (b);
13729 else
13730 w->last_point = XMARKER (w->pointm)->charpos;
13731 }
13732 }
13733
13734 if (accurate_p)
13735 {
13736 w->window_end_valid = w->buffer;
13737 w->update_mode_line = 0;
13738 }
13739 }
13740
13741
13742 /* Mark the display of windows in the window tree rooted at WINDOW as
13743 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13744 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13745 be redisplayed the next time redisplay_internal is called. */
13746
13747 void
13748 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13749 {
13750 struct window *w;
13751
13752 for (; !NILP (window); window = w->next)
13753 {
13754 w = XWINDOW (window);
13755 mark_window_display_accurate_1 (w, accurate_p);
13756
13757 if (!NILP (w->vchild))
13758 mark_window_display_accurate (w->vchild, accurate_p);
13759 if (!NILP (w->hchild))
13760 mark_window_display_accurate (w->hchild, accurate_p);
13761 }
13762
13763 if (accurate_p)
13764 {
13765 update_overlay_arrows (1);
13766 }
13767 else
13768 {
13769 /* Force a thorough redisplay the next time by setting
13770 last_arrow_position and last_arrow_string to t, which is
13771 unequal to any useful value of Voverlay_arrow_... */
13772 update_overlay_arrows (-1);
13773 }
13774 }
13775
13776
13777 /* Return value in display table DP (Lisp_Char_Table *) for character
13778 C. Since a display table doesn't have any parent, we don't have to
13779 follow parent. Do not call this function directly but use the
13780 macro DISP_CHAR_VECTOR. */
13781
13782 Lisp_Object
13783 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13784 {
13785 Lisp_Object val;
13786
13787 if (ASCII_CHAR_P (c))
13788 {
13789 val = dp->ascii;
13790 if (SUB_CHAR_TABLE_P (val))
13791 val = XSUB_CHAR_TABLE (val)->contents[c];
13792 }
13793 else
13794 {
13795 Lisp_Object table;
13796
13797 XSETCHAR_TABLE (table, dp);
13798 val = char_table_ref (table, c);
13799 }
13800 if (NILP (val))
13801 val = dp->defalt;
13802 return val;
13803 }
13804
13805
13806 \f
13807 /***********************************************************************
13808 Window Redisplay
13809 ***********************************************************************/
13810
13811 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13812
13813 static void
13814 redisplay_windows (Lisp_Object window)
13815 {
13816 while (!NILP (window))
13817 {
13818 struct window *w = XWINDOW (window);
13819
13820 if (!NILP (w->hchild))
13821 redisplay_windows (w->hchild);
13822 else if (!NILP (w->vchild))
13823 redisplay_windows (w->vchild);
13824 else if (!NILP (w->buffer))
13825 {
13826 displayed_buffer = XBUFFER (w->buffer);
13827 /* Use list_of_error, not Qerror, so that
13828 we catch only errors and don't run the debugger. */
13829 internal_condition_case_1 (redisplay_window_0, window,
13830 list_of_error,
13831 redisplay_window_error);
13832 }
13833
13834 window = w->next;
13835 }
13836 }
13837
13838 static Lisp_Object
13839 redisplay_window_error (Lisp_Object ignore)
13840 {
13841 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13842 return Qnil;
13843 }
13844
13845 static Lisp_Object
13846 redisplay_window_0 (Lisp_Object window)
13847 {
13848 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13849 redisplay_window (window, 0);
13850 return Qnil;
13851 }
13852
13853 static Lisp_Object
13854 redisplay_window_1 (Lisp_Object window)
13855 {
13856 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13857 redisplay_window (window, 1);
13858 return Qnil;
13859 }
13860 \f
13861
13862 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13863 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13864 which positions recorded in ROW differ from current buffer
13865 positions.
13866
13867 Return 0 if cursor is not on this row, 1 otherwise. */
13868
13869 static int
13870 set_cursor_from_row (struct window *w, struct glyph_row *row,
13871 struct glyph_matrix *matrix,
13872 ptrdiff_t delta, ptrdiff_t delta_bytes,
13873 int dy, int dvpos)
13874 {
13875 struct glyph *glyph = row->glyphs[TEXT_AREA];
13876 struct glyph *end = glyph + row->used[TEXT_AREA];
13877 struct glyph *cursor = NULL;
13878 /* The last known character position in row. */
13879 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13880 int x = row->x;
13881 ptrdiff_t pt_old = PT - delta;
13882 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13883 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13884 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13885 /* A glyph beyond the edge of TEXT_AREA which we should never
13886 touch. */
13887 struct glyph *glyphs_end = end;
13888 /* Non-zero means we've found a match for cursor position, but that
13889 glyph has the avoid_cursor_p flag set. */
13890 int match_with_avoid_cursor = 0;
13891 /* Non-zero means we've seen at least one glyph that came from a
13892 display string. */
13893 int string_seen = 0;
13894 /* Largest and smallest buffer positions seen so far during scan of
13895 glyph row. */
13896 ptrdiff_t bpos_max = pos_before;
13897 ptrdiff_t bpos_min = pos_after;
13898 /* Last buffer position covered by an overlay string with an integer
13899 `cursor' property. */
13900 ptrdiff_t bpos_covered = 0;
13901 /* Non-zero means the display string on which to display the cursor
13902 comes from a text property, not from an overlay. */
13903 int string_from_text_prop = 0;
13904
13905 /* Don't even try doing anything if called for a mode-line or
13906 header-line row, since the rest of the code isn't prepared to
13907 deal with such calamities. */
13908 eassert (!row->mode_line_p);
13909 if (row->mode_line_p)
13910 return 0;
13911
13912 /* Skip over glyphs not having an object at the start and the end of
13913 the row. These are special glyphs like truncation marks on
13914 terminal frames. */
13915 if (row->displays_text_p)
13916 {
13917 if (!row->reversed_p)
13918 {
13919 while (glyph < end
13920 && INTEGERP (glyph->object)
13921 && glyph->charpos < 0)
13922 {
13923 x += glyph->pixel_width;
13924 ++glyph;
13925 }
13926 while (end > glyph
13927 && INTEGERP ((end - 1)->object)
13928 /* CHARPOS is zero for blanks and stretch glyphs
13929 inserted by extend_face_to_end_of_line. */
13930 && (end - 1)->charpos <= 0)
13931 --end;
13932 glyph_before = glyph - 1;
13933 glyph_after = end;
13934 }
13935 else
13936 {
13937 struct glyph *g;
13938
13939 /* If the glyph row is reversed, we need to process it from back
13940 to front, so swap the edge pointers. */
13941 glyphs_end = end = glyph - 1;
13942 glyph += row->used[TEXT_AREA] - 1;
13943
13944 while (glyph > end + 1
13945 && INTEGERP (glyph->object)
13946 && glyph->charpos < 0)
13947 {
13948 --glyph;
13949 x -= glyph->pixel_width;
13950 }
13951 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13952 --glyph;
13953 /* By default, in reversed rows we put the cursor on the
13954 rightmost (first in the reading order) glyph. */
13955 for (g = end + 1; g < glyph; g++)
13956 x += g->pixel_width;
13957 while (end < glyph
13958 && INTEGERP ((end + 1)->object)
13959 && (end + 1)->charpos <= 0)
13960 ++end;
13961 glyph_before = glyph + 1;
13962 glyph_after = end;
13963 }
13964 }
13965 else if (row->reversed_p)
13966 {
13967 /* In R2L rows that don't display text, put the cursor on the
13968 rightmost glyph. Case in point: an empty last line that is
13969 part of an R2L paragraph. */
13970 cursor = end - 1;
13971 /* Avoid placing the cursor on the last glyph of the row, where
13972 on terminal frames we hold the vertical border between
13973 adjacent windows. */
13974 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13975 && !WINDOW_RIGHTMOST_P (w)
13976 && cursor == row->glyphs[LAST_AREA] - 1)
13977 cursor--;
13978 x = -1; /* will be computed below, at label compute_x */
13979 }
13980
13981 /* Step 1: Try to find the glyph whose character position
13982 corresponds to point. If that's not possible, find 2 glyphs
13983 whose character positions are the closest to point, one before
13984 point, the other after it. */
13985 if (!row->reversed_p)
13986 while (/* not marched to end of glyph row */
13987 glyph < end
13988 /* glyph was not inserted by redisplay for internal purposes */
13989 && !INTEGERP (glyph->object))
13990 {
13991 if (BUFFERP (glyph->object))
13992 {
13993 ptrdiff_t dpos = glyph->charpos - pt_old;
13994
13995 if (glyph->charpos > bpos_max)
13996 bpos_max = glyph->charpos;
13997 if (glyph->charpos < bpos_min)
13998 bpos_min = glyph->charpos;
13999 if (!glyph->avoid_cursor_p)
14000 {
14001 /* If we hit point, we've found the glyph on which to
14002 display the cursor. */
14003 if (dpos == 0)
14004 {
14005 match_with_avoid_cursor = 0;
14006 break;
14007 }
14008 /* See if we've found a better approximation to
14009 POS_BEFORE or to POS_AFTER. */
14010 if (0 > dpos && dpos > pos_before - pt_old)
14011 {
14012 pos_before = glyph->charpos;
14013 glyph_before = glyph;
14014 }
14015 else if (0 < dpos && dpos < pos_after - pt_old)
14016 {
14017 pos_after = glyph->charpos;
14018 glyph_after = glyph;
14019 }
14020 }
14021 else if (dpos == 0)
14022 match_with_avoid_cursor = 1;
14023 }
14024 else if (STRINGP (glyph->object))
14025 {
14026 Lisp_Object chprop;
14027 ptrdiff_t glyph_pos = glyph->charpos;
14028
14029 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14030 glyph->object);
14031 if (!NILP (chprop))
14032 {
14033 /* If the string came from a `display' text property,
14034 look up the buffer position of that property and
14035 use that position to update bpos_max, as if we
14036 actually saw such a position in one of the row's
14037 glyphs. This helps with supporting integer values
14038 of `cursor' property on the display string in
14039 situations where most or all of the row's buffer
14040 text is completely covered by display properties,
14041 so that no glyph with valid buffer positions is
14042 ever seen in the row. */
14043 ptrdiff_t prop_pos =
14044 string_buffer_position_lim (glyph->object, pos_before,
14045 pos_after, 0);
14046
14047 if (prop_pos >= pos_before)
14048 bpos_max = prop_pos - 1;
14049 }
14050 if (INTEGERP (chprop))
14051 {
14052 bpos_covered = bpos_max + XINT (chprop);
14053 /* If the `cursor' property covers buffer positions up
14054 to and including point, we should display cursor on
14055 this glyph. Note that, if a `cursor' property on one
14056 of the string's characters has an integer value, we
14057 will break out of the loop below _before_ we get to
14058 the position match above. IOW, integer values of
14059 the `cursor' property override the "exact match for
14060 point" strategy of positioning the cursor. */
14061 /* Implementation note: bpos_max == pt_old when, e.g.,
14062 we are in an empty line, where bpos_max is set to
14063 MATRIX_ROW_START_CHARPOS, see above. */
14064 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14065 {
14066 cursor = glyph;
14067 break;
14068 }
14069 }
14070
14071 string_seen = 1;
14072 }
14073 x += glyph->pixel_width;
14074 ++glyph;
14075 }
14076 else if (glyph > end) /* row is reversed */
14077 while (!INTEGERP (glyph->object))
14078 {
14079 if (BUFFERP (glyph->object))
14080 {
14081 ptrdiff_t dpos = glyph->charpos - pt_old;
14082
14083 if (glyph->charpos > bpos_max)
14084 bpos_max = glyph->charpos;
14085 if (glyph->charpos < bpos_min)
14086 bpos_min = glyph->charpos;
14087 if (!glyph->avoid_cursor_p)
14088 {
14089 if (dpos == 0)
14090 {
14091 match_with_avoid_cursor = 0;
14092 break;
14093 }
14094 if (0 > dpos && dpos > pos_before - pt_old)
14095 {
14096 pos_before = glyph->charpos;
14097 glyph_before = glyph;
14098 }
14099 else if (0 < dpos && dpos < pos_after - pt_old)
14100 {
14101 pos_after = glyph->charpos;
14102 glyph_after = glyph;
14103 }
14104 }
14105 else if (dpos == 0)
14106 match_with_avoid_cursor = 1;
14107 }
14108 else if (STRINGP (glyph->object))
14109 {
14110 Lisp_Object chprop;
14111 ptrdiff_t glyph_pos = glyph->charpos;
14112
14113 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14114 glyph->object);
14115 if (!NILP (chprop))
14116 {
14117 ptrdiff_t prop_pos =
14118 string_buffer_position_lim (glyph->object, pos_before,
14119 pos_after, 0);
14120
14121 if (prop_pos >= pos_before)
14122 bpos_max = prop_pos - 1;
14123 }
14124 if (INTEGERP (chprop))
14125 {
14126 bpos_covered = bpos_max + XINT (chprop);
14127 /* If the `cursor' property covers buffer positions up
14128 to and including point, we should display cursor on
14129 this glyph. */
14130 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14131 {
14132 cursor = glyph;
14133 break;
14134 }
14135 }
14136 string_seen = 1;
14137 }
14138 --glyph;
14139 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14140 {
14141 x--; /* can't use any pixel_width */
14142 break;
14143 }
14144 x -= glyph->pixel_width;
14145 }
14146
14147 /* Step 2: If we didn't find an exact match for point, we need to
14148 look for a proper place to put the cursor among glyphs between
14149 GLYPH_BEFORE and GLYPH_AFTER. */
14150 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14151 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14152 && bpos_covered < pt_old)
14153 {
14154 /* An empty line has a single glyph whose OBJECT is zero and
14155 whose CHARPOS is the position of a newline on that line.
14156 Note that on a TTY, there are more glyphs after that, which
14157 were produced by extend_face_to_end_of_line, but their
14158 CHARPOS is zero or negative. */
14159 int empty_line_p =
14160 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14161 && INTEGERP (glyph->object) && glyph->charpos > 0;
14162
14163 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14164 {
14165 ptrdiff_t ellipsis_pos;
14166
14167 /* Scan back over the ellipsis glyphs. */
14168 if (!row->reversed_p)
14169 {
14170 ellipsis_pos = (glyph - 1)->charpos;
14171 while (glyph > row->glyphs[TEXT_AREA]
14172 && (glyph - 1)->charpos == ellipsis_pos)
14173 glyph--, x -= glyph->pixel_width;
14174 /* That loop always goes one position too far, including
14175 the glyph before the ellipsis. So scan forward over
14176 that one. */
14177 x += glyph->pixel_width;
14178 glyph++;
14179 }
14180 else /* row is reversed */
14181 {
14182 ellipsis_pos = (glyph + 1)->charpos;
14183 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14184 && (glyph + 1)->charpos == ellipsis_pos)
14185 glyph++, x += glyph->pixel_width;
14186 x -= glyph->pixel_width;
14187 glyph--;
14188 }
14189 }
14190 else if (match_with_avoid_cursor)
14191 {
14192 cursor = glyph_after;
14193 x = -1;
14194 }
14195 else if (string_seen)
14196 {
14197 int incr = row->reversed_p ? -1 : +1;
14198
14199 /* Need to find the glyph that came out of a string which is
14200 present at point. That glyph is somewhere between
14201 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14202 positioned between POS_BEFORE and POS_AFTER in the
14203 buffer. */
14204 struct glyph *start, *stop;
14205 ptrdiff_t pos = pos_before;
14206
14207 x = -1;
14208
14209 /* If the row ends in a newline from a display string,
14210 reordering could have moved the glyphs belonging to the
14211 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14212 in this case we extend the search to the last glyph in
14213 the row that was not inserted by redisplay. */
14214 if (row->ends_in_newline_from_string_p)
14215 {
14216 glyph_after = end;
14217 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14218 }
14219
14220 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14221 correspond to POS_BEFORE and POS_AFTER, respectively. We
14222 need START and STOP in the order that corresponds to the
14223 row's direction as given by its reversed_p flag. If the
14224 directionality of characters between POS_BEFORE and
14225 POS_AFTER is the opposite of the row's base direction,
14226 these characters will have been reordered for display,
14227 and we need to reverse START and STOP. */
14228 if (!row->reversed_p)
14229 {
14230 start = min (glyph_before, glyph_after);
14231 stop = max (glyph_before, glyph_after);
14232 }
14233 else
14234 {
14235 start = max (glyph_before, glyph_after);
14236 stop = min (glyph_before, glyph_after);
14237 }
14238 for (glyph = start + incr;
14239 row->reversed_p ? glyph > stop : glyph < stop; )
14240 {
14241
14242 /* Any glyphs that come from the buffer are here because
14243 of bidi reordering. Skip them, and only pay
14244 attention to glyphs that came from some string. */
14245 if (STRINGP (glyph->object))
14246 {
14247 Lisp_Object str;
14248 ptrdiff_t tem;
14249 /* If the display property covers the newline, we
14250 need to search for it one position farther. */
14251 ptrdiff_t lim = pos_after
14252 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14253
14254 string_from_text_prop = 0;
14255 str = glyph->object;
14256 tem = string_buffer_position_lim (str, pos, lim, 0);
14257 if (tem == 0 /* from overlay */
14258 || pos <= tem)
14259 {
14260 /* If the string from which this glyph came is
14261 found in the buffer at point, or at position
14262 that is closer to point than pos_after, then
14263 we've found the glyph we've been looking for.
14264 If it comes from an overlay (tem == 0), and
14265 it has the `cursor' property on one of its
14266 glyphs, record that glyph as a candidate for
14267 displaying the cursor. (As in the
14268 unidirectional version, we will display the
14269 cursor on the last candidate we find.) */
14270 if (tem == 0
14271 || tem == pt_old
14272 || (tem - pt_old > 0 && tem < pos_after))
14273 {
14274 /* The glyphs from this string could have
14275 been reordered. Find the one with the
14276 smallest string position. Or there could
14277 be a character in the string with the
14278 `cursor' property, which means display
14279 cursor on that character's glyph. */
14280 ptrdiff_t strpos = glyph->charpos;
14281
14282 if (tem)
14283 {
14284 cursor = glyph;
14285 string_from_text_prop = 1;
14286 }
14287 for ( ;
14288 (row->reversed_p ? glyph > stop : glyph < stop)
14289 && EQ (glyph->object, str);
14290 glyph += incr)
14291 {
14292 Lisp_Object cprop;
14293 ptrdiff_t gpos = glyph->charpos;
14294
14295 cprop = Fget_char_property (make_number (gpos),
14296 Qcursor,
14297 glyph->object);
14298 if (!NILP (cprop))
14299 {
14300 cursor = glyph;
14301 break;
14302 }
14303 if (tem && glyph->charpos < strpos)
14304 {
14305 strpos = glyph->charpos;
14306 cursor = glyph;
14307 }
14308 }
14309
14310 if (tem == pt_old
14311 || (tem - pt_old > 0 && tem < pos_after))
14312 goto compute_x;
14313 }
14314 if (tem)
14315 pos = tem + 1; /* don't find previous instances */
14316 }
14317 /* This string is not what we want; skip all of the
14318 glyphs that came from it. */
14319 while ((row->reversed_p ? glyph > stop : glyph < stop)
14320 && EQ (glyph->object, str))
14321 glyph += incr;
14322 }
14323 else
14324 glyph += incr;
14325 }
14326
14327 /* If we reached the end of the line, and END was from a string,
14328 the cursor is not on this line. */
14329 if (cursor == NULL
14330 && (row->reversed_p ? glyph <= end : glyph >= end)
14331 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14332 && STRINGP (end->object)
14333 && row->continued_p)
14334 return 0;
14335 }
14336 /* A truncated row may not include PT among its character positions.
14337 Setting the cursor inside the scroll margin will trigger
14338 recalculation of hscroll in hscroll_window_tree. But if a
14339 display string covers point, defer to the string-handling
14340 code below to figure this out. */
14341 else if (row->truncated_on_left_p && pt_old < bpos_min)
14342 {
14343 cursor = glyph_before;
14344 x = -1;
14345 }
14346 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14347 /* Zero-width characters produce no glyphs. */
14348 || (!empty_line_p
14349 && (row->reversed_p
14350 ? glyph_after > glyphs_end
14351 : glyph_after < glyphs_end)))
14352 {
14353 cursor = glyph_after;
14354 x = -1;
14355 }
14356 }
14357
14358 compute_x:
14359 if (cursor != NULL)
14360 glyph = cursor;
14361 else if (glyph == glyphs_end
14362 && pos_before == pos_after
14363 && STRINGP ((row->reversed_p
14364 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14365 : row->glyphs[TEXT_AREA])->object))
14366 {
14367 /* If all the glyphs of this row came from strings, put the
14368 cursor on the first glyph of the row. This avoids having the
14369 cursor outside of the text area in this very rare and hard
14370 use case. */
14371 glyph =
14372 row->reversed_p
14373 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14374 : row->glyphs[TEXT_AREA];
14375 }
14376 if (x < 0)
14377 {
14378 struct glyph *g;
14379
14380 /* Need to compute x that corresponds to GLYPH. */
14381 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14382 {
14383 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14384 abort ();
14385 x += g->pixel_width;
14386 }
14387 }
14388
14389 /* ROW could be part of a continued line, which, under bidi
14390 reordering, might have other rows whose start and end charpos
14391 occlude point. Only set w->cursor if we found a better
14392 approximation to the cursor position than we have from previously
14393 examined candidate rows belonging to the same continued line. */
14394 if (/* we already have a candidate row */
14395 w->cursor.vpos >= 0
14396 /* that candidate is not the row we are processing */
14397 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14398 /* Make sure cursor.vpos specifies a row whose start and end
14399 charpos occlude point, and it is valid candidate for being a
14400 cursor-row. This is because some callers of this function
14401 leave cursor.vpos at the row where the cursor was displayed
14402 during the last redisplay cycle. */
14403 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14404 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14405 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14406 {
14407 struct glyph *g1 =
14408 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14409
14410 /* Don't consider glyphs that are outside TEXT_AREA. */
14411 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14412 return 0;
14413 /* Keep the candidate whose buffer position is the closest to
14414 point or has the `cursor' property. */
14415 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14416 w->cursor.hpos >= 0
14417 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14418 && ((BUFFERP (g1->object)
14419 && (g1->charpos == pt_old /* an exact match always wins */
14420 || (BUFFERP (glyph->object)
14421 && eabs (g1->charpos - pt_old)
14422 < eabs (glyph->charpos - pt_old))))
14423 /* previous candidate is a glyph from a string that has
14424 a non-nil `cursor' property */
14425 || (STRINGP (g1->object)
14426 && (!NILP (Fget_char_property (make_number (g1->charpos),
14427 Qcursor, g1->object))
14428 /* previous candidate is from the same display
14429 string as this one, and the display string
14430 came from a text property */
14431 || (EQ (g1->object, glyph->object)
14432 && string_from_text_prop)
14433 /* this candidate is from newline and its
14434 position is not an exact match */
14435 || (INTEGERP (glyph->object)
14436 && glyph->charpos != pt_old)))))
14437 return 0;
14438 /* If this candidate gives an exact match, use that. */
14439 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14440 /* If this candidate is a glyph created for the
14441 terminating newline of a line, and point is on that
14442 newline, it wins because it's an exact match. */
14443 || (!row->continued_p
14444 && INTEGERP (glyph->object)
14445 && glyph->charpos == 0
14446 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14447 /* Otherwise, keep the candidate that comes from a row
14448 spanning less buffer positions. This may win when one or
14449 both candidate positions are on glyphs that came from
14450 display strings, for which we cannot compare buffer
14451 positions. */
14452 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14453 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14454 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14455 return 0;
14456 }
14457 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14458 w->cursor.x = x;
14459 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14460 w->cursor.y = row->y + dy;
14461
14462 if (w == XWINDOW (selected_window))
14463 {
14464 if (!row->continued_p
14465 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14466 && row->x == 0)
14467 {
14468 this_line_buffer = XBUFFER (w->buffer);
14469
14470 CHARPOS (this_line_start_pos)
14471 = MATRIX_ROW_START_CHARPOS (row) + delta;
14472 BYTEPOS (this_line_start_pos)
14473 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14474
14475 CHARPOS (this_line_end_pos)
14476 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14477 BYTEPOS (this_line_end_pos)
14478 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14479
14480 this_line_y = w->cursor.y;
14481 this_line_pixel_height = row->height;
14482 this_line_vpos = w->cursor.vpos;
14483 this_line_start_x = row->x;
14484 }
14485 else
14486 CHARPOS (this_line_start_pos) = 0;
14487 }
14488
14489 return 1;
14490 }
14491
14492
14493 /* Run window scroll functions, if any, for WINDOW with new window
14494 start STARTP. Sets the window start of WINDOW to that position.
14495
14496 We assume that the window's buffer is really current. */
14497
14498 static inline struct text_pos
14499 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14500 {
14501 struct window *w = XWINDOW (window);
14502 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14503
14504 if (current_buffer != XBUFFER (w->buffer))
14505 abort ();
14506
14507 if (!NILP (Vwindow_scroll_functions))
14508 {
14509 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14510 make_number (CHARPOS (startp)));
14511 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14512 /* In case the hook functions switch buffers. */
14513 if (current_buffer != XBUFFER (w->buffer))
14514 set_buffer_internal_1 (XBUFFER (w->buffer));
14515 }
14516
14517 return startp;
14518 }
14519
14520
14521 /* Make sure the line containing the cursor is fully visible.
14522 A value of 1 means there is nothing to be done.
14523 (Either the line is fully visible, or it cannot be made so,
14524 or we cannot tell.)
14525
14526 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14527 is higher than window.
14528
14529 A value of 0 means the caller should do scrolling
14530 as if point had gone off the screen. */
14531
14532 static int
14533 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14534 {
14535 struct glyph_matrix *matrix;
14536 struct glyph_row *row;
14537 int window_height;
14538
14539 if (!make_cursor_line_fully_visible_p)
14540 return 1;
14541
14542 /* It's not always possible to find the cursor, e.g, when a window
14543 is full of overlay strings. Don't do anything in that case. */
14544 if (w->cursor.vpos < 0)
14545 return 1;
14546
14547 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14548 row = MATRIX_ROW (matrix, w->cursor.vpos);
14549
14550 /* If the cursor row is not partially visible, there's nothing to do. */
14551 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14552 return 1;
14553
14554 /* If the row the cursor is in is taller than the window's height,
14555 it's not clear what to do, so do nothing. */
14556 window_height = window_box_height (w);
14557 if (row->height >= window_height)
14558 {
14559 if (!force_p || MINI_WINDOW_P (w)
14560 || w->vscroll || w->cursor.vpos == 0)
14561 return 1;
14562 }
14563 return 0;
14564 }
14565
14566
14567 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14568 non-zero means only WINDOW is redisplayed in redisplay_internal.
14569 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14570 in redisplay_window to bring a partially visible line into view in
14571 the case that only the cursor has moved.
14572
14573 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14574 last screen line's vertical height extends past the end of the screen.
14575
14576 Value is
14577
14578 1 if scrolling succeeded
14579
14580 0 if scrolling didn't find point.
14581
14582 -1 if new fonts have been loaded so that we must interrupt
14583 redisplay, adjust glyph matrices, and try again. */
14584
14585 enum
14586 {
14587 SCROLLING_SUCCESS,
14588 SCROLLING_FAILED,
14589 SCROLLING_NEED_LARGER_MATRICES
14590 };
14591
14592 /* If scroll-conservatively is more than this, never recenter.
14593
14594 If you change this, don't forget to update the doc string of
14595 `scroll-conservatively' and the Emacs manual. */
14596 #define SCROLL_LIMIT 100
14597
14598 static int
14599 try_scrolling (Lisp_Object window, int just_this_one_p,
14600 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14601 int temp_scroll_step, int last_line_misfit)
14602 {
14603 struct window *w = XWINDOW (window);
14604 struct frame *f = XFRAME (w->frame);
14605 struct text_pos pos, startp;
14606 struct it it;
14607 int this_scroll_margin, scroll_max, rc, height;
14608 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14609 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14610 Lisp_Object aggressive;
14611 /* We will never try scrolling more than this number of lines. */
14612 int scroll_limit = SCROLL_LIMIT;
14613
14614 #ifdef GLYPH_DEBUG
14615 debug_method_add (w, "try_scrolling");
14616 #endif
14617
14618 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14619
14620 /* Compute scroll margin height in pixels. We scroll when point is
14621 within this distance from the top or bottom of the window. */
14622 if (scroll_margin > 0)
14623 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14624 * FRAME_LINE_HEIGHT (f);
14625 else
14626 this_scroll_margin = 0;
14627
14628 /* Force arg_scroll_conservatively to have a reasonable value, to
14629 avoid scrolling too far away with slow move_it_* functions. Note
14630 that the user can supply scroll-conservatively equal to
14631 `most-positive-fixnum', which can be larger than INT_MAX. */
14632 if (arg_scroll_conservatively > scroll_limit)
14633 {
14634 arg_scroll_conservatively = scroll_limit + 1;
14635 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14636 }
14637 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14638 /* Compute how much we should try to scroll maximally to bring
14639 point into view. */
14640 scroll_max = (max (scroll_step,
14641 max (arg_scroll_conservatively, temp_scroll_step))
14642 * FRAME_LINE_HEIGHT (f));
14643 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14644 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14645 /* We're trying to scroll because of aggressive scrolling but no
14646 scroll_step is set. Choose an arbitrary one. */
14647 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14648 else
14649 scroll_max = 0;
14650
14651 too_near_end:
14652
14653 /* Decide whether to scroll down. */
14654 if (PT > CHARPOS (startp))
14655 {
14656 int scroll_margin_y;
14657
14658 /* Compute the pixel ypos of the scroll margin, then move IT to
14659 either that ypos or PT, whichever comes first. */
14660 start_display (&it, w, startp);
14661 scroll_margin_y = it.last_visible_y - this_scroll_margin
14662 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14663 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14664 (MOVE_TO_POS | MOVE_TO_Y));
14665
14666 if (PT > CHARPOS (it.current.pos))
14667 {
14668 int y0 = line_bottom_y (&it);
14669 /* Compute how many pixels below window bottom to stop searching
14670 for PT. This avoids costly search for PT that is far away if
14671 the user limited scrolling by a small number of lines, but
14672 always finds PT if scroll_conservatively is set to a large
14673 number, such as most-positive-fixnum. */
14674 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14675 int y_to_move = it.last_visible_y + slack;
14676
14677 /* Compute the distance from the scroll margin to PT or to
14678 the scroll limit, whichever comes first. This should
14679 include the height of the cursor line, to make that line
14680 fully visible. */
14681 move_it_to (&it, PT, -1, y_to_move,
14682 -1, MOVE_TO_POS | MOVE_TO_Y);
14683 dy = line_bottom_y (&it) - y0;
14684
14685 if (dy > scroll_max)
14686 return SCROLLING_FAILED;
14687
14688 if (dy > 0)
14689 scroll_down_p = 1;
14690 }
14691 }
14692
14693 if (scroll_down_p)
14694 {
14695 /* Point is in or below the bottom scroll margin, so move the
14696 window start down. If scrolling conservatively, move it just
14697 enough down to make point visible. If scroll_step is set,
14698 move it down by scroll_step. */
14699 if (arg_scroll_conservatively)
14700 amount_to_scroll
14701 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14702 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14703 else if (scroll_step || temp_scroll_step)
14704 amount_to_scroll = scroll_max;
14705 else
14706 {
14707 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14708 height = WINDOW_BOX_TEXT_HEIGHT (w);
14709 if (NUMBERP (aggressive))
14710 {
14711 double float_amount = XFLOATINT (aggressive) * height;
14712 amount_to_scroll = float_amount;
14713 if (amount_to_scroll == 0 && float_amount > 0)
14714 amount_to_scroll = 1;
14715 /* Don't let point enter the scroll margin near top of
14716 the window. */
14717 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14718 amount_to_scroll = height - 2*this_scroll_margin + dy;
14719 }
14720 }
14721
14722 if (amount_to_scroll <= 0)
14723 return SCROLLING_FAILED;
14724
14725 start_display (&it, w, startp);
14726 if (arg_scroll_conservatively <= scroll_limit)
14727 move_it_vertically (&it, amount_to_scroll);
14728 else
14729 {
14730 /* Extra precision for users who set scroll-conservatively
14731 to a large number: make sure the amount we scroll
14732 the window start is never less than amount_to_scroll,
14733 which was computed as distance from window bottom to
14734 point. This matters when lines at window top and lines
14735 below window bottom have different height. */
14736 struct it it1;
14737 void *it1data = NULL;
14738 /* We use a temporary it1 because line_bottom_y can modify
14739 its argument, if it moves one line down; see there. */
14740 int start_y;
14741
14742 SAVE_IT (it1, it, it1data);
14743 start_y = line_bottom_y (&it1);
14744 do {
14745 RESTORE_IT (&it, &it, it1data);
14746 move_it_by_lines (&it, 1);
14747 SAVE_IT (it1, it, it1data);
14748 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14749 }
14750
14751 /* If STARTP is unchanged, move it down another screen line. */
14752 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14753 move_it_by_lines (&it, 1);
14754 startp = it.current.pos;
14755 }
14756 else
14757 {
14758 struct text_pos scroll_margin_pos = startp;
14759
14760 /* See if point is inside the scroll margin at the top of the
14761 window. */
14762 if (this_scroll_margin)
14763 {
14764 start_display (&it, w, startp);
14765 move_it_vertically (&it, this_scroll_margin);
14766 scroll_margin_pos = it.current.pos;
14767 }
14768
14769 if (PT < CHARPOS (scroll_margin_pos))
14770 {
14771 /* Point is in the scroll margin at the top of the window or
14772 above what is displayed in the window. */
14773 int y0, y_to_move;
14774
14775 /* Compute the vertical distance from PT to the scroll
14776 margin position. Move as far as scroll_max allows, or
14777 one screenful, or 10 screen lines, whichever is largest.
14778 Give up if distance is greater than scroll_max. */
14779 SET_TEXT_POS (pos, PT, PT_BYTE);
14780 start_display (&it, w, pos);
14781 y0 = it.current_y;
14782 y_to_move = max (it.last_visible_y,
14783 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14784 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14785 y_to_move, -1,
14786 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14787 dy = it.current_y - y0;
14788 if (dy > scroll_max)
14789 return SCROLLING_FAILED;
14790
14791 /* Compute new window start. */
14792 start_display (&it, w, startp);
14793
14794 if (arg_scroll_conservatively)
14795 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14796 max (scroll_step, temp_scroll_step));
14797 else if (scroll_step || temp_scroll_step)
14798 amount_to_scroll = scroll_max;
14799 else
14800 {
14801 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14802 height = WINDOW_BOX_TEXT_HEIGHT (w);
14803 if (NUMBERP (aggressive))
14804 {
14805 double float_amount = XFLOATINT (aggressive) * height;
14806 amount_to_scroll = float_amount;
14807 if (amount_to_scroll == 0 && float_amount > 0)
14808 amount_to_scroll = 1;
14809 amount_to_scroll -=
14810 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14811 /* Don't let point enter the scroll margin near
14812 bottom of the window. */
14813 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14814 amount_to_scroll = height - 2*this_scroll_margin + dy;
14815 }
14816 }
14817
14818 if (amount_to_scroll <= 0)
14819 return SCROLLING_FAILED;
14820
14821 move_it_vertically_backward (&it, amount_to_scroll);
14822 startp = it.current.pos;
14823 }
14824 }
14825
14826 /* Run window scroll functions. */
14827 startp = run_window_scroll_functions (window, startp);
14828
14829 /* Display the window. Give up if new fonts are loaded, or if point
14830 doesn't appear. */
14831 if (!try_window (window, startp, 0))
14832 rc = SCROLLING_NEED_LARGER_MATRICES;
14833 else if (w->cursor.vpos < 0)
14834 {
14835 clear_glyph_matrix (w->desired_matrix);
14836 rc = SCROLLING_FAILED;
14837 }
14838 else
14839 {
14840 /* Maybe forget recorded base line for line number display. */
14841 if (!just_this_one_p
14842 || current_buffer->clip_changed
14843 || BEG_UNCHANGED < CHARPOS (startp))
14844 w->base_line_number = Qnil;
14845
14846 /* If cursor ends up on a partially visible line,
14847 treat that as being off the bottom of the screen. */
14848 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14849 /* It's possible that the cursor is on the first line of the
14850 buffer, which is partially obscured due to a vscroll
14851 (Bug#7537). In that case, avoid looping forever . */
14852 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14853 {
14854 clear_glyph_matrix (w->desired_matrix);
14855 ++extra_scroll_margin_lines;
14856 goto too_near_end;
14857 }
14858 rc = SCROLLING_SUCCESS;
14859 }
14860
14861 return rc;
14862 }
14863
14864
14865 /* Compute a suitable window start for window W if display of W starts
14866 on a continuation line. Value is non-zero if a new window start
14867 was computed.
14868
14869 The new window start will be computed, based on W's width, starting
14870 from the start of the continued line. It is the start of the
14871 screen line with the minimum distance from the old start W->start. */
14872
14873 static int
14874 compute_window_start_on_continuation_line (struct window *w)
14875 {
14876 struct text_pos pos, start_pos;
14877 int window_start_changed_p = 0;
14878
14879 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14880
14881 /* If window start is on a continuation line... Window start may be
14882 < BEGV in case there's invisible text at the start of the
14883 buffer (M-x rmail, for example). */
14884 if (CHARPOS (start_pos) > BEGV
14885 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14886 {
14887 struct it it;
14888 struct glyph_row *row;
14889
14890 /* Handle the case that the window start is out of range. */
14891 if (CHARPOS (start_pos) < BEGV)
14892 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14893 else if (CHARPOS (start_pos) > ZV)
14894 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14895
14896 /* Find the start of the continued line. This should be fast
14897 because scan_buffer is fast (newline cache). */
14898 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14899 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14900 row, DEFAULT_FACE_ID);
14901 reseat_at_previous_visible_line_start (&it);
14902
14903 /* If the line start is "too far" away from the window start,
14904 say it takes too much time to compute a new window start. */
14905 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14906 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14907 {
14908 int min_distance, distance;
14909
14910 /* Move forward by display lines to find the new window
14911 start. If window width was enlarged, the new start can
14912 be expected to be > the old start. If window width was
14913 decreased, the new window start will be < the old start.
14914 So, we're looking for the display line start with the
14915 minimum distance from the old window start. */
14916 pos = it.current.pos;
14917 min_distance = INFINITY;
14918 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14919 distance < min_distance)
14920 {
14921 min_distance = distance;
14922 pos = it.current.pos;
14923 move_it_by_lines (&it, 1);
14924 }
14925
14926 /* Set the window start there. */
14927 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14928 window_start_changed_p = 1;
14929 }
14930 }
14931
14932 return window_start_changed_p;
14933 }
14934
14935
14936 /* Try cursor movement in case text has not changed in window WINDOW,
14937 with window start STARTP. Value is
14938
14939 CURSOR_MOVEMENT_SUCCESS if successful
14940
14941 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14942
14943 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14944 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14945 we want to scroll as if scroll-step were set to 1. See the code.
14946
14947 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14948 which case we have to abort this redisplay, and adjust matrices
14949 first. */
14950
14951 enum
14952 {
14953 CURSOR_MOVEMENT_SUCCESS,
14954 CURSOR_MOVEMENT_CANNOT_BE_USED,
14955 CURSOR_MOVEMENT_MUST_SCROLL,
14956 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14957 };
14958
14959 static int
14960 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14961 {
14962 struct window *w = XWINDOW (window);
14963 struct frame *f = XFRAME (w->frame);
14964 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14965
14966 #ifdef GLYPH_DEBUG
14967 if (inhibit_try_cursor_movement)
14968 return rc;
14969 #endif
14970
14971 /* Previously, there was a check for Lisp integer in the
14972 if-statement below. Now, this field is converted to
14973 ptrdiff_t, thus zero means invalid position in a buffer. */
14974 eassert (w->last_point > 0);
14975
14976 /* Handle case where text has not changed, only point, and it has
14977 not moved off the frame. */
14978 if (/* Point may be in this window. */
14979 PT >= CHARPOS (startp)
14980 /* Selective display hasn't changed. */
14981 && !current_buffer->clip_changed
14982 /* Function force-mode-line-update is used to force a thorough
14983 redisplay. It sets either windows_or_buffers_changed or
14984 update_mode_lines. So don't take a shortcut here for these
14985 cases. */
14986 && !update_mode_lines
14987 && !windows_or_buffers_changed
14988 && !cursor_type_changed
14989 /* Can't use this case if highlighting a region. When a
14990 region exists, cursor movement has to do more than just
14991 set the cursor. */
14992 && !(!NILP (Vtransient_mark_mode)
14993 && !NILP (BVAR (current_buffer, mark_active)))
14994 && NILP (w->region_showing)
14995 && NILP (Vshow_trailing_whitespace)
14996 /* This code is not used for mini-buffer for the sake of the case
14997 of redisplaying to replace an echo area message; since in
14998 that case the mini-buffer contents per se are usually
14999 unchanged. This code is of no real use in the mini-buffer
15000 since the handling of this_line_start_pos, etc., in redisplay
15001 handles the same cases. */
15002 && !EQ (window, minibuf_window)
15003 /* When splitting windows or for new windows, it happens that
15004 redisplay is called with a nil window_end_vpos or one being
15005 larger than the window. This should really be fixed in
15006 window.c. I don't have this on my list, now, so we do
15007 approximately the same as the old redisplay code. --gerd. */
15008 && INTEGERP (w->window_end_vpos)
15009 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15010 && (FRAME_WINDOW_P (f)
15011 || !overlay_arrow_in_current_buffer_p ()))
15012 {
15013 int this_scroll_margin, top_scroll_margin;
15014 struct glyph_row *row = NULL;
15015
15016 #ifdef GLYPH_DEBUG
15017 debug_method_add (w, "cursor movement");
15018 #endif
15019
15020 /* Scroll if point within this distance from the top or bottom
15021 of the window. This is a pixel value. */
15022 if (scroll_margin > 0)
15023 {
15024 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15025 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15026 }
15027 else
15028 this_scroll_margin = 0;
15029
15030 top_scroll_margin = this_scroll_margin;
15031 if (WINDOW_WANTS_HEADER_LINE_P (w))
15032 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15033
15034 /* Start with the row the cursor was displayed during the last
15035 not paused redisplay. Give up if that row is not valid. */
15036 if (w->last_cursor.vpos < 0
15037 || w->last_cursor.vpos >= w->current_matrix->nrows)
15038 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15039 else
15040 {
15041 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15042 if (row->mode_line_p)
15043 ++row;
15044 if (!row->enabled_p)
15045 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15046 }
15047
15048 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15049 {
15050 int scroll_p = 0, must_scroll = 0;
15051 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15052
15053 if (PT > w->last_point)
15054 {
15055 /* Point has moved forward. */
15056 while (MATRIX_ROW_END_CHARPOS (row) < PT
15057 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15058 {
15059 eassert (row->enabled_p);
15060 ++row;
15061 }
15062
15063 /* If the end position of a row equals the start
15064 position of the next row, and PT is at that position,
15065 we would rather display cursor in the next line. */
15066 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15067 && MATRIX_ROW_END_CHARPOS (row) == PT
15068 && row < w->current_matrix->rows
15069 + w->current_matrix->nrows - 1
15070 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15071 && !cursor_row_p (row))
15072 ++row;
15073
15074 /* If within the scroll margin, scroll. Note that
15075 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15076 the next line would be drawn, and that
15077 this_scroll_margin can be zero. */
15078 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15079 || PT > MATRIX_ROW_END_CHARPOS (row)
15080 /* Line is completely visible last line in window
15081 and PT is to be set in the next line. */
15082 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15083 && PT == MATRIX_ROW_END_CHARPOS (row)
15084 && !row->ends_at_zv_p
15085 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15086 scroll_p = 1;
15087 }
15088 else if (PT < w->last_point)
15089 {
15090 /* Cursor has to be moved backward. Note that PT >=
15091 CHARPOS (startp) because of the outer if-statement. */
15092 while (!row->mode_line_p
15093 && (MATRIX_ROW_START_CHARPOS (row) > PT
15094 || (MATRIX_ROW_START_CHARPOS (row) == PT
15095 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15096 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15097 row > w->current_matrix->rows
15098 && (row-1)->ends_in_newline_from_string_p))))
15099 && (row->y > top_scroll_margin
15100 || CHARPOS (startp) == BEGV))
15101 {
15102 eassert (row->enabled_p);
15103 --row;
15104 }
15105
15106 /* Consider the following case: Window starts at BEGV,
15107 there is invisible, intangible text at BEGV, so that
15108 display starts at some point START > BEGV. It can
15109 happen that we are called with PT somewhere between
15110 BEGV and START. Try to handle that case. */
15111 if (row < w->current_matrix->rows
15112 || row->mode_line_p)
15113 {
15114 row = w->current_matrix->rows;
15115 if (row->mode_line_p)
15116 ++row;
15117 }
15118
15119 /* Due to newlines in overlay strings, we may have to
15120 skip forward over overlay strings. */
15121 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15122 && MATRIX_ROW_END_CHARPOS (row) == PT
15123 && !cursor_row_p (row))
15124 ++row;
15125
15126 /* If within the scroll margin, scroll. */
15127 if (row->y < top_scroll_margin
15128 && CHARPOS (startp) != BEGV)
15129 scroll_p = 1;
15130 }
15131 else
15132 {
15133 /* Cursor did not move. So don't scroll even if cursor line
15134 is partially visible, as it was so before. */
15135 rc = CURSOR_MOVEMENT_SUCCESS;
15136 }
15137
15138 if (PT < MATRIX_ROW_START_CHARPOS (row)
15139 || PT > MATRIX_ROW_END_CHARPOS (row))
15140 {
15141 /* if PT is not in the glyph row, give up. */
15142 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15143 must_scroll = 1;
15144 }
15145 else if (rc != CURSOR_MOVEMENT_SUCCESS
15146 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15147 {
15148 struct glyph_row *row1;
15149
15150 /* If rows are bidi-reordered and point moved, back up
15151 until we find a row that does not belong to a
15152 continuation line. This is because we must consider
15153 all rows of a continued line as candidates for the
15154 new cursor positioning, since row start and end
15155 positions change non-linearly with vertical position
15156 in such rows. */
15157 /* FIXME: Revisit this when glyph ``spilling'' in
15158 continuation lines' rows is implemented for
15159 bidi-reordered rows. */
15160 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15161 MATRIX_ROW_CONTINUATION_LINE_P (row);
15162 --row)
15163 {
15164 /* If we hit the beginning of the displayed portion
15165 without finding the first row of a continued
15166 line, give up. */
15167 if (row <= row1)
15168 {
15169 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15170 break;
15171 }
15172 eassert (row->enabled_p);
15173 }
15174 }
15175 if (must_scroll)
15176 ;
15177 else if (rc != CURSOR_MOVEMENT_SUCCESS
15178 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15179 /* Make sure this isn't a header line by any chance, since
15180 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15181 && !row->mode_line_p
15182 && make_cursor_line_fully_visible_p)
15183 {
15184 if (PT == MATRIX_ROW_END_CHARPOS (row)
15185 && !row->ends_at_zv_p
15186 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15187 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15188 else if (row->height > window_box_height (w))
15189 {
15190 /* If we end up in a partially visible line, let's
15191 make it fully visible, except when it's taller
15192 than the window, in which case we can't do much
15193 about it. */
15194 *scroll_step = 1;
15195 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15196 }
15197 else
15198 {
15199 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15200 if (!cursor_row_fully_visible_p (w, 0, 1))
15201 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15202 else
15203 rc = CURSOR_MOVEMENT_SUCCESS;
15204 }
15205 }
15206 else if (scroll_p)
15207 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15208 else if (rc != CURSOR_MOVEMENT_SUCCESS
15209 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15210 {
15211 /* With bidi-reordered rows, there could be more than
15212 one candidate row whose start and end positions
15213 occlude point. We need to let set_cursor_from_row
15214 find the best candidate. */
15215 /* FIXME: Revisit this when glyph ``spilling'' in
15216 continuation lines' rows is implemented for
15217 bidi-reordered rows. */
15218 int rv = 0;
15219
15220 do
15221 {
15222 int at_zv_p = 0, exact_match_p = 0;
15223
15224 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15225 && PT <= MATRIX_ROW_END_CHARPOS (row)
15226 && cursor_row_p (row))
15227 rv |= set_cursor_from_row (w, row, w->current_matrix,
15228 0, 0, 0, 0);
15229 /* As soon as we've found the exact match for point,
15230 or the first suitable row whose ends_at_zv_p flag
15231 is set, we are done. */
15232 at_zv_p =
15233 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15234 if (rv && !at_zv_p
15235 && w->cursor.hpos >= 0
15236 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15237 w->cursor.vpos))
15238 {
15239 struct glyph_row *candidate =
15240 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15241 struct glyph *g =
15242 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15243 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15244
15245 exact_match_p =
15246 (BUFFERP (g->object) && g->charpos == PT)
15247 || (INTEGERP (g->object)
15248 && (g->charpos == PT
15249 || (g->charpos == 0 && endpos - 1 == PT)));
15250 }
15251 if (rv && (at_zv_p || exact_match_p))
15252 {
15253 rc = CURSOR_MOVEMENT_SUCCESS;
15254 break;
15255 }
15256 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15257 break;
15258 ++row;
15259 }
15260 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15261 || row->continued_p)
15262 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15263 || (MATRIX_ROW_START_CHARPOS (row) == PT
15264 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15265 /* If we didn't find any candidate rows, or exited the
15266 loop before all the candidates were examined, signal
15267 to the caller that this method failed. */
15268 if (rc != CURSOR_MOVEMENT_SUCCESS
15269 && !(rv
15270 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15271 && !row->continued_p))
15272 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15273 else if (rv)
15274 rc = CURSOR_MOVEMENT_SUCCESS;
15275 }
15276 else
15277 {
15278 do
15279 {
15280 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15281 {
15282 rc = CURSOR_MOVEMENT_SUCCESS;
15283 break;
15284 }
15285 ++row;
15286 }
15287 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15288 && MATRIX_ROW_START_CHARPOS (row) == PT
15289 && cursor_row_p (row));
15290 }
15291 }
15292 }
15293
15294 return rc;
15295 }
15296
15297 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15298 static
15299 #endif
15300 void
15301 set_vertical_scroll_bar (struct window *w)
15302 {
15303 ptrdiff_t start, end, whole;
15304
15305 /* Calculate the start and end positions for the current window.
15306 At some point, it would be nice to choose between scrollbars
15307 which reflect the whole buffer size, with special markers
15308 indicating narrowing, and scrollbars which reflect only the
15309 visible region.
15310
15311 Note that mini-buffers sometimes aren't displaying any text. */
15312 if (!MINI_WINDOW_P (w)
15313 || (w == XWINDOW (minibuf_window)
15314 && NILP (echo_area_buffer[0])))
15315 {
15316 struct buffer *buf = XBUFFER (w->buffer);
15317 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15318 start = marker_position (w->start) - BUF_BEGV (buf);
15319 /* I don't think this is guaranteed to be right. For the
15320 moment, we'll pretend it is. */
15321 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15322
15323 if (end < start)
15324 end = start;
15325 if (whole < (end - start))
15326 whole = end - start;
15327 }
15328 else
15329 start = end = whole = 0;
15330
15331 /* Indicate what this scroll bar ought to be displaying now. */
15332 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15333 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15334 (w, end - start, whole, start);
15335 }
15336
15337
15338 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15339 selected_window is redisplayed.
15340
15341 We can return without actually redisplaying the window if
15342 fonts_changed_p is nonzero. In that case, redisplay_internal will
15343 retry. */
15344
15345 static void
15346 redisplay_window (Lisp_Object window, int just_this_one_p)
15347 {
15348 struct window *w = XWINDOW (window);
15349 struct frame *f = XFRAME (w->frame);
15350 struct buffer *buffer = XBUFFER (w->buffer);
15351 struct buffer *old = current_buffer;
15352 struct text_pos lpoint, opoint, startp;
15353 int update_mode_line;
15354 int tem;
15355 struct it it;
15356 /* Record it now because it's overwritten. */
15357 int current_matrix_up_to_date_p = 0;
15358 int used_current_matrix_p = 0;
15359 /* This is less strict than current_matrix_up_to_date_p.
15360 It indicates that the buffer contents and narrowing are unchanged. */
15361 int buffer_unchanged_p = 0;
15362 int temp_scroll_step = 0;
15363 ptrdiff_t count = SPECPDL_INDEX ();
15364 int rc;
15365 int centering_position = -1;
15366 int last_line_misfit = 0;
15367 ptrdiff_t beg_unchanged, end_unchanged;
15368
15369 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15370 opoint = lpoint;
15371
15372 /* W must be a leaf window here. */
15373 eassert (!NILP (w->buffer));
15374 #ifdef GLYPH_DEBUG
15375 *w->desired_matrix->method = 0;
15376 #endif
15377
15378 restart:
15379 reconsider_clip_changes (w, buffer);
15380
15381 /* Has the mode line to be updated? */
15382 update_mode_line = (w->update_mode_line
15383 || update_mode_lines
15384 || buffer->clip_changed
15385 || buffer->prevent_redisplay_optimizations_p);
15386
15387 if (MINI_WINDOW_P (w))
15388 {
15389 if (w == XWINDOW (echo_area_window)
15390 && !NILP (echo_area_buffer[0]))
15391 {
15392 if (update_mode_line)
15393 /* We may have to update a tty frame's menu bar or a
15394 tool-bar. Example `M-x C-h C-h C-g'. */
15395 goto finish_menu_bars;
15396 else
15397 /* We've already displayed the echo area glyphs in this window. */
15398 goto finish_scroll_bars;
15399 }
15400 else if ((w != XWINDOW (minibuf_window)
15401 || minibuf_level == 0)
15402 /* When buffer is nonempty, redisplay window normally. */
15403 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15404 /* Quail displays non-mini buffers in minibuffer window.
15405 In that case, redisplay the window normally. */
15406 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15407 {
15408 /* W is a mini-buffer window, but it's not active, so clear
15409 it. */
15410 int yb = window_text_bottom_y (w);
15411 struct glyph_row *row;
15412 int y;
15413
15414 for (y = 0, row = w->desired_matrix->rows;
15415 y < yb;
15416 y += row->height, ++row)
15417 blank_row (w, row, y);
15418 goto finish_scroll_bars;
15419 }
15420
15421 clear_glyph_matrix (w->desired_matrix);
15422 }
15423
15424 /* Otherwise set up data on this window; select its buffer and point
15425 value. */
15426 /* Really select the buffer, for the sake of buffer-local
15427 variables. */
15428 set_buffer_internal_1 (XBUFFER (w->buffer));
15429
15430 current_matrix_up_to_date_p
15431 = (!NILP (w->window_end_valid)
15432 && !current_buffer->clip_changed
15433 && !current_buffer->prevent_redisplay_optimizations_p
15434 && w->last_modified >= MODIFF
15435 && w->last_overlay_modified >= OVERLAY_MODIFF);
15436
15437 /* Run the window-bottom-change-functions
15438 if it is possible that the text on the screen has changed
15439 (either due to modification of the text, or any other reason). */
15440 if (!current_matrix_up_to_date_p
15441 && !NILP (Vwindow_text_change_functions))
15442 {
15443 safe_run_hooks (Qwindow_text_change_functions);
15444 goto restart;
15445 }
15446
15447 beg_unchanged = BEG_UNCHANGED;
15448 end_unchanged = END_UNCHANGED;
15449
15450 SET_TEXT_POS (opoint, PT, PT_BYTE);
15451
15452 specbind (Qinhibit_point_motion_hooks, Qt);
15453
15454 buffer_unchanged_p
15455 = (!NILP (w->window_end_valid)
15456 && !current_buffer->clip_changed
15457 && w->last_modified >= MODIFF
15458 && w->last_overlay_modified >= OVERLAY_MODIFF);
15459
15460 /* When windows_or_buffers_changed is non-zero, we can't rely on
15461 the window end being valid, so set it to nil there. */
15462 if (windows_or_buffers_changed)
15463 {
15464 /* If window starts on a continuation line, maybe adjust the
15465 window start in case the window's width changed. */
15466 if (XMARKER (w->start)->buffer == current_buffer)
15467 compute_window_start_on_continuation_line (w);
15468
15469 w->window_end_valid = Qnil;
15470 }
15471
15472 /* Some sanity checks. */
15473 CHECK_WINDOW_END (w);
15474 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15475 abort ();
15476 if (BYTEPOS (opoint) < CHARPOS (opoint))
15477 abort ();
15478
15479 /* If %c is in mode line, update it if needed. */
15480 if (!NILP (w->column_number_displayed)
15481 /* This alternative quickly identifies a common case
15482 where no change is needed. */
15483 && !(PT == w->last_point
15484 && w->last_modified >= MODIFF
15485 && w->last_overlay_modified >= OVERLAY_MODIFF)
15486 && (XFASTINT (w->column_number_displayed) != current_column ()))
15487 update_mode_line = 1;
15488
15489 /* Count number of windows showing the selected buffer. An indirect
15490 buffer counts as its base buffer. */
15491 if (!just_this_one_p)
15492 {
15493 struct buffer *current_base, *window_base;
15494 current_base = current_buffer;
15495 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15496 if (current_base->base_buffer)
15497 current_base = current_base->base_buffer;
15498 if (window_base->base_buffer)
15499 window_base = window_base->base_buffer;
15500 if (current_base == window_base)
15501 buffer_shared++;
15502 }
15503
15504 /* Point refers normally to the selected window. For any other
15505 window, set up appropriate value. */
15506 if (!EQ (window, selected_window))
15507 {
15508 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15509 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15510 if (new_pt < BEGV)
15511 {
15512 new_pt = BEGV;
15513 new_pt_byte = BEGV_BYTE;
15514 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15515 }
15516 else if (new_pt > (ZV - 1))
15517 {
15518 new_pt = ZV;
15519 new_pt_byte = ZV_BYTE;
15520 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15521 }
15522
15523 /* We don't use SET_PT so that the point-motion hooks don't run. */
15524 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15525 }
15526
15527 /* If any of the character widths specified in the display table
15528 have changed, invalidate the width run cache. It's true that
15529 this may be a bit late to catch such changes, but the rest of
15530 redisplay goes (non-fatally) haywire when the display table is
15531 changed, so why should we worry about doing any better? */
15532 if (current_buffer->width_run_cache)
15533 {
15534 struct Lisp_Char_Table *disptab = buffer_display_table ();
15535
15536 if (! disptab_matches_widthtab (disptab,
15537 XVECTOR (BVAR (current_buffer, width_table))))
15538 {
15539 invalidate_region_cache (current_buffer,
15540 current_buffer->width_run_cache,
15541 BEG, Z);
15542 recompute_width_table (current_buffer, disptab);
15543 }
15544 }
15545
15546 /* If window-start is screwed up, choose a new one. */
15547 if (XMARKER (w->start)->buffer != current_buffer)
15548 goto recenter;
15549
15550 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15551
15552 /* If someone specified a new starting point but did not insist,
15553 check whether it can be used. */
15554 if (w->optional_new_start
15555 && CHARPOS (startp) >= BEGV
15556 && CHARPOS (startp) <= ZV)
15557 {
15558 w->optional_new_start = 0;
15559 start_display (&it, w, startp);
15560 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15561 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15562 if (IT_CHARPOS (it) == PT)
15563 w->force_start = 1;
15564 /* IT may overshoot PT if text at PT is invisible. */
15565 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15566 w->force_start = 1;
15567 }
15568
15569 force_start:
15570
15571 /* Handle case where place to start displaying has been specified,
15572 unless the specified location is outside the accessible range. */
15573 if (w->force_start || w->frozen_window_start_p)
15574 {
15575 /* We set this later on if we have to adjust point. */
15576 int new_vpos = -1;
15577
15578 w->force_start = 0;
15579 w->vscroll = 0;
15580 w->window_end_valid = Qnil;
15581
15582 /* Forget any recorded base line for line number display. */
15583 if (!buffer_unchanged_p)
15584 w->base_line_number = Qnil;
15585
15586 /* Redisplay the mode line. Select the buffer properly for that.
15587 Also, run the hook window-scroll-functions
15588 because we have scrolled. */
15589 /* Note, we do this after clearing force_start because
15590 if there's an error, it is better to forget about force_start
15591 than to get into an infinite loop calling the hook functions
15592 and having them get more errors. */
15593 if (!update_mode_line
15594 || ! NILP (Vwindow_scroll_functions))
15595 {
15596 update_mode_line = 1;
15597 w->update_mode_line = 1;
15598 startp = run_window_scroll_functions (window, startp);
15599 }
15600
15601 w->last_modified = 0;
15602 w->last_overlay_modified = 0;
15603 if (CHARPOS (startp) < BEGV)
15604 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15605 else if (CHARPOS (startp) > ZV)
15606 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15607
15608 /* Redisplay, then check if cursor has been set during the
15609 redisplay. Give up if new fonts were loaded. */
15610 /* We used to issue a CHECK_MARGINS argument to try_window here,
15611 but this causes scrolling to fail when point begins inside
15612 the scroll margin (bug#148) -- cyd */
15613 if (!try_window (window, startp, 0))
15614 {
15615 w->force_start = 1;
15616 clear_glyph_matrix (w->desired_matrix);
15617 goto need_larger_matrices;
15618 }
15619
15620 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15621 {
15622 /* If point does not appear, try to move point so it does
15623 appear. The desired matrix has been built above, so we
15624 can use it here. */
15625 new_vpos = window_box_height (w) / 2;
15626 }
15627
15628 if (!cursor_row_fully_visible_p (w, 0, 0))
15629 {
15630 /* Point does appear, but on a line partly visible at end of window.
15631 Move it back to a fully-visible line. */
15632 new_vpos = window_box_height (w);
15633 }
15634
15635 /* If we need to move point for either of the above reasons,
15636 now actually do it. */
15637 if (new_vpos >= 0)
15638 {
15639 struct glyph_row *row;
15640
15641 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15642 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15643 ++row;
15644
15645 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15646 MATRIX_ROW_START_BYTEPOS (row));
15647
15648 if (w != XWINDOW (selected_window))
15649 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15650 else if (current_buffer == old)
15651 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15652
15653 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15654
15655 /* If we are highlighting the region, then we just changed
15656 the region, so redisplay to show it. */
15657 if (!NILP (Vtransient_mark_mode)
15658 && !NILP (BVAR (current_buffer, mark_active)))
15659 {
15660 clear_glyph_matrix (w->desired_matrix);
15661 if (!try_window (window, startp, 0))
15662 goto need_larger_matrices;
15663 }
15664 }
15665
15666 #ifdef GLYPH_DEBUG
15667 debug_method_add (w, "forced window start");
15668 #endif
15669 goto done;
15670 }
15671
15672 /* Handle case where text has not changed, only point, and it has
15673 not moved off the frame, and we are not retrying after hscroll.
15674 (current_matrix_up_to_date_p is nonzero when retrying.) */
15675 if (current_matrix_up_to_date_p
15676 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15677 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15678 {
15679 switch (rc)
15680 {
15681 case CURSOR_MOVEMENT_SUCCESS:
15682 used_current_matrix_p = 1;
15683 goto done;
15684
15685 case CURSOR_MOVEMENT_MUST_SCROLL:
15686 goto try_to_scroll;
15687
15688 default:
15689 abort ();
15690 }
15691 }
15692 /* If current starting point was originally the beginning of a line
15693 but no longer is, find a new starting point. */
15694 else if (w->start_at_line_beg
15695 && !(CHARPOS (startp) <= BEGV
15696 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15697 {
15698 #ifdef GLYPH_DEBUG
15699 debug_method_add (w, "recenter 1");
15700 #endif
15701 goto recenter;
15702 }
15703
15704 /* Try scrolling with try_window_id. Value is > 0 if update has
15705 been done, it is -1 if we know that the same window start will
15706 not work. It is 0 if unsuccessful for some other reason. */
15707 else if ((tem = try_window_id (w)) != 0)
15708 {
15709 #ifdef GLYPH_DEBUG
15710 debug_method_add (w, "try_window_id %d", tem);
15711 #endif
15712
15713 if (fonts_changed_p)
15714 goto need_larger_matrices;
15715 if (tem > 0)
15716 goto done;
15717
15718 /* Otherwise try_window_id has returned -1 which means that we
15719 don't want the alternative below this comment to execute. */
15720 }
15721 else if (CHARPOS (startp) >= BEGV
15722 && CHARPOS (startp) <= ZV
15723 && PT >= CHARPOS (startp)
15724 && (CHARPOS (startp) < ZV
15725 /* Avoid starting at end of buffer. */
15726 || CHARPOS (startp) == BEGV
15727 || (w->last_modified >= MODIFF
15728 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15729 {
15730 int d1, d2, d3, d4, d5, d6;
15731
15732 /* If first window line is a continuation line, and window start
15733 is inside the modified region, but the first change is before
15734 current window start, we must select a new window start.
15735
15736 However, if this is the result of a down-mouse event (e.g. by
15737 extending the mouse-drag-overlay), we don't want to select a
15738 new window start, since that would change the position under
15739 the mouse, resulting in an unwanted mouse-movement rather
15740 than a simple mouse-click. */
15741 if (!w->start_at_line_beg
15742 && NILP (do_mouse_tracking)
15743 && CHARPOS (startp) > BEGV
15744 && CHARPOS (startp) > BEG + beg_unchanged
15745 && CHARPOS (startp) <= Z - end_unchanged
15746 /* Even if w->start_at_line_beg is nil, a new window may
15747 start at a line_beg, since that's how set_buffer_window
15748 sets it. So, we need to check the return value of
15749 compute_window_start_on_continuation_line. (See also
15750 bug#197). */
15751 && XMARKER (w->start)->buffer == current_buffer
15752 && compute_window_start_on_continuation_line (w)
15753 /* It doesn't make sense to force the window start like we
15754 do at label force_start if it is already known that point
15755 will not be visible in the resulting window, because
15756 doing so will move point from its correct position
15757 instead of scrolling the window to bring point into view.
15758 See bug#9324. */
15759 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15760 {
15761 w->force_start = 1;
15762 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15763 goto force_start;
15764 }
15765
15766 #ifdef GLYPH_DEBUG
15767 debug_method_add (w, "same window start");
15768 #endif
15769
15770 /* Try to redisplay starting at same place as before.
15771 If point has not moved off frame, accept the results. */
15772 if (!current_matrix_up_to_date_p
15773 /* Don't use try_window_reusing_current_matrix in this case
15774 because a window scroll function can have changed the
15775 buffer. */
15776 || !NILP (Vwindow_scroll_functions)
15777 || MINI_WINDOW_P (w)
15778 || !(used_current_matrix_p
15779 = try_window_reusing_current_matrix (w)))
15780 {
15781 IF_DEBUG (debug_method_add (w, "1"));
15782 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15783 /* -1 means we need to scroll.
15784 0 means we need new matrices, but fonts_changed_p
15785 is set in that case, so we will detect it below. */
15786 goto try_to_scroll;
15787 }
15788
15789 if (fonts_changed_p)
15790 goto need_larger_matrices;
15791
15792 if (w->cursor.vpos >= 0)
15793 {
15794 if (!just_this_one_p
15795 || current_buffer->clip_changed
15796 || BEG_UNCHANGED < CHARPOS (startp))
15797 /* Forget any recorded base line for line number display. */
15798 w->base_line_number = Qnil;
15799
15800 if (!cursor_row_fully_visible_p (w, 1, 0))
15801 {
15802 clear_glyph_matrix (w->desired_matrix);
15803 last_line_misfit = 1;
15804 }
15805 /* Drop through and scroll. */
15806 else
15807 goto done;
15808 }
15809 else
15810 clear_glyph_matrix (w->desired_matrix);
15811 }
15812
15813 try_to_scroll:
15814
15815 w->last_modified = 0;
15816 w->last_overlay_modified = 0;
15817
15818 /* Redisplay the mode line. Select the buffer properly for that. */
15819 if (!update_mode_line)
15820 {
15821 update_mode_line = 1;
15822 w->update_mode_line = 1;
15823 }
15824
15825 /* Try to scroll by specified few lines. */
15826 if ((scroll_conservatively
15827 || emacs_scroll_step
15828 || temp_scroll_step
15829 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15830 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15831 && CHARPOS (startp) >= BEGV
15832 && CHARPOS (startp) <= ZV)
15833 {
15834 /* The function returns -1 if new fonts were loaded, 1 if
15835 successful, 0 if not successful. */
15836 int ss = try_scrolling (window, just_this_one_p,
15837 scroll_conservatively,
15838 emacs_scroll_step,
15839 temp_scroll_step, last_line_misfit);
15840 switch (ss)
15841 {
15842 case SCROLLING_SUCCESS:
15843 goto done;
15844
15845 case SCROLLING_NEED_LARGER_MATRICES:
15846 goto need_larger_matrices;
15847
15848 case SCROLLING_FAILED:
15849 break;
15850
15851 default:
15852 abort ();
15853 }
15854 }
15855
15856 /* Finally, just choose a place to start which positions point
15857 according to user preferences. */
15858
15859 recenter:
15860
15861 #ifdef GLYPH_DEBUG
15862 debug_method_add (w, "recenter");
15863 #endif
15864
15865 /* w->vscroll = 0; */
15866
15867 /* Forget any previously recorded base line for line number display. */
15868 if (!buffer_unchanged_p)
15869 w->base_line_number = Qnil;
15870
15871 /* Determine the window start relative to point. */
15872 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15873 it.current_y = it.last_visible_y;
15874 if (centering_position < 0)
15875 {
15876 int margin =
15877 scroll_margin > 0
15878 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15879 : 0;
15880 ptrdiff_t margin_pos = CHARPOS (startp);
15881 Lisp_Object aggressive;
15882 int scrolling_up;
15883
15884 /* If there is a scroll margin at the top of the window, find
15885 its character position. */
15886 if (margin
15887 /* Cannot call start_display if startp is not in the
15888 accessible region of the buffer. This can happen when we
15889 have just switched to a different buffer and/or changed
15890 its restriction. In that case, startp is initialized to
15891 the character position 1 (BEGV) because we did not yet
15892 have chance to display the buffer even once. */
15893 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15894 {
15895 struct it it1;
15896 void *it1data = NULL;
15897
15898 SAVE_IT (it1, it, it1data);
15899 start_display (&it1, w, startp);
15900 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15901 margin_pos = IT_CHARPOS (it1);
15902 RESTORE_IT (&it, &it, it1data);
15903 }
15904 scrolling_up = PT > margin_pos;
15905 aggressive =
15906 scrolling_up
15907 ? BVAR (current_buffer, scroll_up_aggressively)
15908 : BVAR (current_buffer, scroll_down_aggressively);
15909
15910 if (!MINI_WINDOW_P (w)
15911 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15912 {
15913 int pt_offset = 0;
15914
15915 /* Setting scroll-conservatively overrides
15916 scroll-*-aggressively. */
15917 if (!scroll_conservatively && NUMBERP (aggressive))
15918 {
15919 double float_amount = XFLOATINT (aggressive);
15920
15921 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15922 if (pt_offset == 0 && float_amount > 0)
15923 pt_offset = 1;
15924 if (pt_offset && margin > 0)
15925 margin -= 1;
15926 }
15927 /* Compute how much to move the window start backward from
15928 point so that point will be displayed where the user
15929 wants it. */
15930 if (scrolling_up)
15931 {
15932 centering_position = it.last_visible_y;
15933 if (pt_offset)
15934 centering_position -= pt_offset;
15935 centering_position -=
15936 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15937 + WINDOW_HEADER_LINE_HEIGHT (w);
15938 /* Don't let point enter the scroll margin near top of
15939 the window. */
15940 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15941 centering_position = margin * FRAME_LINE_HEIGHT (f);
15942 }
15943 else
15944 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15945 }
15946 else
15947 /* Set the window start half the height of the window backward
15948 from point. */
15949 centering_position = window_box_height (w) / 2;
15950 }
15951 move_it_vertically_backward (&it, centering_position);
15952
15953 eassert (IT_CHARPOS (it) >= BEGV);
15954
15955 /* The function move_it_vertically_backward may move over more
15956 than the specified y-distance. If it->w is small, e.g. a
15957 mini-buffer window, we may end up in front of the window's
15958 display area. Start displaying at the start of the line
15959 containing PT in this case. */
15960 if (it.current_y <= 0)
15961 {
15962 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15963 move_it_vertically_backward (&it, 0);
15964 it.current_y = 0;
15965 }
15966
15967 it.current_x = it.hpos = 0;
15968
15969 /* Set the window start position here explicitly, to avoid an
15970 infinite loop in case the functions in window-scroll-functions
15971 get errors. */
15972 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15973
15974 /* Run scroll hooks. */
15975 startp = run_window_scroll_functions (window, it.current.pos);
15976
15977 /* Redisplay the window. */
15978 if (!current_matrix_up_to_date_p
15979 || windows_or_buffers_changed
15980 || cursor_type_changed
15981 /* Don't use try_window_reusing_current_matrix in this case
15982 because it can have changed the buffer. */
15983 || !NILP (Vwindow_scroll_functions)
15984 || !just_this_one_p
15985 || MINI_WINDOW_P (w)
15986 || !(used_current_matrix_p
15987 = try_window_reusing_current_matrix (w)))
15988 try_window (window, startp, 0);
15989
15990 /* If new fonts have been loaded (due to fontsets), give up. We
15991 have to start a new redisplay since we need to re-adjust glyph
15992 matrices. */
15993 if (fonts_changed_p)
15994 goto need_larger_matrices;
15995
15996 /* If cursor did not appear assume that the middle of the window is
15997 in the first line of the window. Do it again with the next line.
15998 (Imagine a window of height 100, displaying two lines of height
15999 60. Moving back 50 from it->last_visible_y will end in the first
16000 line.) */
16001 if (w->cursor.vpos < 0)
16002 {
16003 if (!NILP (w->window_end_valid)
16004 && PT >= Z - XFASTINT (w->window_end_pos))
16005 {
16006 clear_glyph_matrix (w->desired_matrix);
16007 move_it_by_lines (&it, 1);
16008 try_window (window, it.current.pos, 0);
16009 }
16010 else if (PT < IT_CHARPOS (it))
16011 {
16012 clear_glyph_matrix (w->desired_matrix);
16013 move_it_by_lines (&it, -1);
16014 try_window (window, it.current.pos, 0);
16015 }
16016 else
16017 {
16018 /* Not much we can do about it. */
16019 }
16020 }
16021
16022 /* Consider the following case: Window starts at BEGV, there is
16023 invisible, intangible text at BEGV, so that display starts at
16024 some point START > BEGV. It can happen that we are called with
16025 PT somewhere between BEGV and START. Try to handle that case. */
16026 if (w->cursor.vpos < 0)
16027 {
16028 struct glyph_row *row = w->current_matrix->rows;
16029 if (row->mode_line_p)
16030 ++row;
16031 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16032 }
16033
16034 if (!cursor_row_fully_visible_p (w, 0, 0))
16035 {
16036 /* If vscroll is enabled, disable it and try again. */
16037 if (w->vscroll)
16038 {
16039 w->vscroll = 0;
16040 clear_glyph_matrix (w->desired_matrix);
16041 goto recenter;
16042 }
16043
16044 /* Users who set scroll-conservatively to a large number want
16045 point just above/below the scroll margin. If we ended up
16046 with point's row partially visible, move the window start to
16047 make that row fully visible and out of the margin. */
16048 if (scroll_conservatively > SCROLL_LIMIT)
16049 {
16050 int margin =
16051 scroll_margin > 0
16052 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16053 : 0;
16054 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16055
16056 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16057 clear_glyph_matrix (w->desired_matrix);
16058 if (1 == try_window (window, it.current.pos,
16059 TRY_WINDOW_CHECK_MARGINS))
16060 goto done;
16061 }
16062
16063 /* If centering point failed to make the whole line visible,
16064 put point at the top instead. That has to make the whole line
16065 visible, if it can be done. */
16066 if (centering_position == 0)
16067 goto done;
16068
16069 clear_glyph_matrix (w->desired_matrix);
16070 centering_position = 0;
16071 goto recenter;
16072 }
16073
16074 done:
16075
16076 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16077 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16078 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16079
16080 /* Display the mode line, if we must. */
16081 if ((update_mode_line
16082 /* If window not full width, must redo its mode line
16083 if (a) the window to its side is being redone and
16084 (b) we do a frame-based redisplay. This is a consequence
16085 of how inverted lines are drawn in frame-based redisplay. */
16086 || (!just_this_one_p
16087 && !FRAME_WINDOW_P (f)
16088 && !WINDOW_FULL_WIDTH_P (w))
16089 /* Line number to display. */
16090 || INTEGERP (w->base_line_pos)
16091 /* Column number is displayed and different from the one displayed. */
16092 || (!NILP (w->column_number_displayed)
16093 && (XFASTINT (w->column_number_displayed) != current_column ())))
16094 /* This means that the window has a mode line. */
16095 && (WINDOW_WANTS_MODELINE_P (w)
16096 || WINDOW_WANTS_HEADER_LINE_P (w)))
16097 {
16098 display_mode_lines (w);
16099
16100 /* If mode line height has changed, arrange for a thorough
16101 immediate redisplay using the correct mode line height. */
16102 if (WINDOW_WANTS_MODELINE_P (w)
16103 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16104 {
16105 fonts_changed_p = 1;
16106 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16107 = DESIRED_MODE_LINE_HEIGHT (w);
16108 }
16109
16110 /* If header line height has changed, arrange for a thorough
16111 immediate redisplay using the correct header line height. */
16112 if (WINDOW_WANTS_HEADER_LINE_P (w)
16113 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16114 {
16115 fonts_changed_p = 1;
16116 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16117 = DESIRED_HEADER_LINE_HEIGHT (w);
16118 }
16119
16120 if (fonts_changed_p)
16121 goto need_larger_matrices;
16122 }
16123
16124 if (!line_number_displayed
16125 && !BUFFERP (w->base_line_pos))
16126 {
16127 w->base_line_pos = Qnil;
16128 w->base_line_number = Qnil;
16129 }
16130
16131 finish_menu_bars:
16132
16133 /* When we reach a frame's selected window, redo the frame's menu bar. */
16134 if (update_mode_line
16135 && EQ (FRAME_SELECTED_WINDOW (f), window))
16136 {
16137 int redisplay_menu_p = 0;
16138
16139 if (FRAME_WINDOW_P (f))
16140 {
16141 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16142 || defined (HAVE_NS) || defined (USE_GTK)
16143 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16144 #else
16145 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16146 #endif
16147 }
16148 else
16149 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16150
16151 if (redisplay_menu_p)
16152 display_menu_bar (w);
16153
16154 #ifdef HAVE_WINDOW_SYSTEM
16155 if (FRAME_WINDOW_P (f))
16156 {
16157 #if defined (USE_GTK) || defined (HAVE_NS)
16158 if (FRAME_EXTERNAL_TOOL_BAR (f))
16159 redisplay_tool_bar (f);
16160 #else
16161 if (WINDOWP (f->tool_bar_window)
16162 && (FRAME_TOOL_BAR_LINES (f) > 0
16163 || !NILP (Vauto_resize_tool_bars))
16164 && redisplay_tool_bar (f))
16165 ignore_mouse_drag_p = 1;
16166 #endif
16167 }
16168 #endif
16169 }
16170
16171 #ifdef HAVE_WINDOW_SYSTEM
16172 if (FRAME_WINDOW_P (f)
16173 && update_window_fringes (w, (just_this_one_p
16174 || (!used_current_matrix_p && !overlay_arrow_seen)
16175 || w->pseudo_window_p)))
16176 {
16177 update_begin (f);
16178 BLOCK_INPUT;
16179 if (draw_window_fringes (w, 1))
16180 x_draw_vertical_border (w);
16181 UNBLOCK_INPUT;
16182 update_end (f);
16183 }
16184 #endif /* HAVE_WINDOW_SYSTEM */
16185
16186 /* We go to this label, with fonts_changed_p nonzero,
16187 if it is necessary to try again using larger glyph matrices.
16188 We have to redeem the scroll bar even in this case,
16189 because the loop in redisplay_internal expects that. */
16190 need_larger_matrices:
16191 ;
16192 finish_scroll_bars:
16193
16194 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16195 {
16196 /* Set the thumb's position and size. */
16197 set_vertical_scroll_bar (w);
16198
16199 /* Note that we actually used the scroll bar attached to this
16200 window, so it shouldn't be deleted at the end of redisplay. */
16201 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16202 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16203 }
16204
16205 /* Restore current_buffer and value of point in it. The window
16206 update may have changed the buffer, so first make sure `opoint'
16207 is still valid (Bug#6177). */
16208 if (CHARPOS (opoint) < BEGV)
16209 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16210 else if (CHARPOS (opoint) > ZV)
16211 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16212 else
16213 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16214
16215 set_buffer_internal_1 (old);
16216 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16217 shorter. This can be caused by log truncation in *Messages*. */
16218 if (CHARPOS (lpoint) <= ZV)
16219 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16220
16221 unbind_to (count, Qnil);
16222 }
16223
16224
16225 /* Build the complete desired matrix of WINDOW with a window start
16226 buffer position POS.
16227
16228 Value is 1 if successful. It is zero if fonts were loaded during
16229 redisplay which makes re-adjusting glyph matrices necessary, and -1
16230 if point would appear in the scroll margins.
16231 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16232 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16233 set in FLAGS.) */
16234
16235 int
16236 try_window (Lisp_Object window, struct text_pos pos, int flags)
16237 {
16238 struct window *w = XWINDOW (window);
16239 struct it it;
16240 struct glyph_row *last_text_row = NULL;
16241 struct frame *f = XFRAME (w->frame);
16242
16243 /* Make POS the new window start. */
16244 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16245
16246 /* Mark cursor position as unknown. No overlay arrow seen. */
16247 w->cursor.vpos = -1;
16248 overlay_arrow_seen = 0;
16249
16250 /* Initialize iterator and info to start at POS. */
16251 start_display (&it, w, pos);
16252
16253 /* Display all lines of W. */
16254 while (it.current_y < it.last_visible_y)
16255 {
16256 if (display_line (&it))
16257 last_text_row = it.glyph_row - 1;
16258 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16259 return 0;
16260 }
16261
16262 /* Don't let the cursor end in the scroll margins. */
16263 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16264 && !MINI_WINDOW_P (w))
16265 {
16266 int this_scroll_margin;
16267
16268 if (scroll_margin > 0)
16269 {
16270 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16271 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16272 }
16273 else
16274 this_scroll_margin = 0;
16275
16276 if ((w->cursor.y >= 0 /* not vscrolled */
16277 && w->cursor.y < this_scroll_margin
16278 && CHARPOS (pos) > BEGV
16279 && IT_CHARPOS (it) < ZV)
16280 /* rms: considering make_cursor_line_fully_visible_p here
16281 seems to give wrong results. We don't want to recenter
16282 when the last line is partly visible, we want to allow
16283 that case to be handled in the usual way. */
16284 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16285 {
16286 w->cursor.vpos = -1;
16287 clear_glyph_matrix (w->desired_matrix);
16288 return -1;
16289 }
16290 }
16291
16292 /* If bottom moved off end of frame, change mode line percentage. */
16293 if (XFASTINT (w->window_end_pos) <= 0
16294 && Z != IT_CHARPOS (it))
16295 w->update_mode_line = 1;
16296
16297 /* Set window_end_pos to the offset of the last character displayed
16298 on the window from the end of current_buffer. Set
16299 window_end_vpos to its row number. */
16300 if (last_text_row)
16301 {
16302 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16303 w->window_end_bytepos
16304 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16305 w->window_end_pos
16306 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16307 w->window_end_vpos
16308 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16309 eassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16310 ->displays_text_p);
16311 }
16312 else
16313 {
16314 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16315 w->window_end_pos = make_number (Z - ZV);
16316 w->window_end_vpos = make_number (0);
16317 }
16318
16319 /* But that is not valid info until redisplay finishes. */
16320 w->window_end_valid = Qnil;
16321 return 1;
16322 }
16323
16324
16325 \f
16326 /************************************************************************
16327 Window redisplay reusing current matrix when buffer has not changed
16328 ************************************************************************/
16329
16330 /* Try redisplay of window W showing an unchanged buffer with a
16331 different window start than the last time it was displayed by
16332 reusing its current matrix. Value is non-zero if successful.
16333 W->start is the new window start. */
16334
16335 static int
16336 try_window_reusing_current_matrix (struct window *w)
16337 {
16338 struct frame *f = XFRAME (w->frame);
16339 struct glyph_row *bottom_row;
16340 struct it it;
16341 struct run run;
16342 struct text_pos start, new_start;
16343 int nrows_scrolled, i;
16344 struct glyph_row *last_text_row;
16345 struct glyph_row *last_reused_text_row;
16346 struct glyph_row *start_row;
16347 int start_vpos, min_y, max_y;
16348
16349 #ifdef GLYPH_DEBUG
16350 if (inhibit_try_window_reusing)
16351 return 0;
16352 #endif
16353
16354 if (/* This function doesn't handle terminal frames. */
16355 !FRAME_WINDOW_P (f)
16356 /* Don't try to reuse the display if windows have been split
16357 or such. */
16358 || windows_or_buffers_changed
16359 || cursor_type_changed)
16360 return 0;
16361
16362 /* Can't do this if region may have changed. */
16363 if ((!NILP (Vtransient_mark_mode)
16364 && !NILP (BVAR (current_buffer, mark_active)))
16365 || !NILP (w->region_showing)
16366 || !NILP (Vshow_trailing_whitespace))
16367 return 0;
16368
16369 /* If top-line visibility has changed, give up. */
16370 if (WINDOW_WANTS_HEADER_LINE_P (w)
16371 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16372 return 0;
16373
16374 /* Give up if old or new display is scrolled vertically. We could
16375 make this function handle this, but right now it doesn't. */
16376 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16377 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16378 return 0;
16379
16380 /* The variable new_start now holds the new window start. The old
16381 start `start' can be determined from the current matrix. */
16382 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16383 start = start_row->minpos;
16384 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16385
16386 /* Clear the desired matrix for the display below. */
16387 clear_glyph_matrix (w->desired_matrix);
16388
16389 if (CHARPOS (new_start) <= CHARPOS (start))
16390 {
16391 /* Don't use this method if the display starts with an ellipsis
16392 displayed for invisible text. It's not easy to handle that case
16393 below, and it's certainly not worth the effort since this is
16394 not a frequent case. */
16395 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16396 return 0;
16397
16398 IF_DEBUG (debug_method_add (w, "twu1"));
16399
16400 /* Display up to a row that can be reused. The variable
16401 last_text_row is set to the last row displayed that displays
16402 text. Note that it.vpos == 0 if or if not there is a
16403 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16404 start_display (&it, w, new_start);
16405 w->cursor.vpos = -1;
16406 last_text_row = last_reused_text_row = NULL;
16407
16408 while (it.current_y < it.last_visible_y
16409 && !fonts_changed_p)
16410 {
16411 /* If we have reached into the characters in the START row,
16412 that means the line boundaries have changed. So we
16413 can't start copying with the row START. Maybe it will
16414 work to start copying with the following row. */
16415 while (IT_CHARPOS (it) > CHARPOS (start))
16416 {
16417 /* Advance to the next row as the "start". */
16418 start_row++;
16419 start = start_row->minpos;
16420 /* If there are no more rows to try, or just one, give up. */
16421 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16422 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16423 || CHARPOS (start) == ZV)
16424 {
16425 clear_glyph_matrix (w->desired_matrix);
16426 return 0;
16427 }
16428
16429 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16430 }
16431 /* If we have reached alignment, we can copy the rest of the
16432 rows. */
16433 if (IT_CHARPOS (it) == CHARPOS (start)
16434 /* Don't accept "alignment" inside a display vector,
16435 since start_row could have started in the middle of
16436 that same display vector (thus their character
16437 positions match), and we have no way of telling if
16438 that is the case. */
16439 && it.current.dpvec_index < 0)
16440 break;
16441
16442 if (display_line (&it))
16443 last_text_row = it.glyph_row - 1;
16444
16445 }
16446
16447 /* A value of current_y < last_visible_y means that we stopped
16448 at the previous window start, which in turn means that we
16449 have at least one reusable row. */
16450 if (it.current_y < it.last_visible_y)
16451 {
16452 struct glyph_row *row;
16453
16454 /* IT.vpos always starts from 0; it counts text lines. */
16455 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16456
16457 /* Find PT if not already found in the lines displayed. */
16458 if (w->cursor.vpos < 0)
16459 {
16460 int dy = it.current_y - start_row->y;
16461
16462 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16463 row = row_containing_pos (w, PT, row, NULL, dy);
16464 if (row)
16465 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16466 dy, nrows_scrolled);
16467 else
16468 {
16469 clear_glyph_matrix (w->desired_matrix);
16470 return 0;
16471 }
16472 }
16473
16474 /* Scroll the display. Do it before the current matrix is
16475 changed. The problem here is that update has not yet
16476 run, i.e. part of the current matrix is not up to date.
16477 scroll_run_hook will clear the cursor, and use the
16478 current matrix to get the height of the row the cursor is
16479 in. */
16480 run.current_y = start_row->y;
16481 run.desired_y = it.current_y;
16482 run.height = it.last_visible_y - it.current_y;
16483
16484 if (run.height > 0 && run.current_y != run.desired_y)
16485 {
16486 update_begin (f);
16487 FRAME_RIF (f)->update_window_begin_hook (w);
16488 FRAME_RIF (f)->clear_window_mouse_face (w);
16489 FRAME_RIF (f)->scroll_run_hook (w, &run);
16490 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16491 update_end (f);
16492 }
16493
16494 /* Shift current matrix down by nrows_scrolled lines. */
16495 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16496 rotate_matrix (w->current_matrix,
16497 start_vpos,
16498 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16499 nrows_scrolled);
16500
16501 /* Disable lines that must be updated. */
16502 for (i = 0; i < nrows_scrolled; ++i)
16503 (start_row + i)->enabled_p = 0;
16504
16505 /* Re-compute Y positions. */
16506 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16507 max_y = it.last_visible_y;
16508 for (row = start_row + nrows_scrolled;
16509 row < bottom_row;
16510 ++row)
16511 {
16512 row->y = it.current_y;
16513 row->visible_height = row->height;
16514
16515 if (row->y < min_y)
16516 row->visible_height -= min_y - row->y;
16517 if (row->y + row->height > max_y)
16518 row->visible_height -= row->y + row->height - max_y;
16519 if (row->fringe_bitmap_periodic_p)
16520 row->redraw_fringe_bitmaps_p = 1;
16521
16522 it.current_y += row->height;
16523
16524 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16525 last_reused_text_row = row;
16526 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16527 break;
16528 }
16529
16530 /* Disable lines in the current matrix which are now
16531 below the window. */
16532 for (++row; row < bottom_row; ++row)
16533 row->enabled_p = row->mode_line_p = 0;
16534 }
16535
16536 /* Update window_end_pos etc.; last_reused_text_row is the last
16537 reused row from the current matrix containing text, if any.
16538 The value of last_text_row is the last displayed line
16539 containing text. */
16540 if (last_reused_text_row)
16541 {
16542 w->window_end_bytepos
16543 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16544 w->window_end_pos
16545 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16546 w->window_end_vpos
16547 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16548 w->current_matrix));
16549 }
16550 else if (last_text_row)
16551 {
16552 w->window_end_bytepos
16553 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16554 w->window_end_pos
16555 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16556 w->window_end_vpos
16557 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16558 }
16559 else
16560 {
16561 /* This window must be completely empty. */
16562 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16563 w->window_end_pos = make_number (Z - ZV);
16564 w->window_end_vpos = make_number (0);
16565 }
16566 w->window_end_valid = Qnil;
16567
16568 /* Update hint: don't try scrolling again in update_window. */
16569 w->desired_matrix->no_scrolling_p = 1;
16570
16571 #ifdef GLYPH_DEBUG
16572 debug_method_add (w, "try_window_reusing_current_matrix 1");
16573 #endif
16574 return 1;
16575 }
16576 else if (CHARPOS (new_start) > CHARPOS (start))
16577 {
16578 struct glyph_row *pt_row, *row;
16579 struct glyph_row *first_reusable_row;
16580 struct glyph_row *first_row_to_display;
16581 int dy;
16582 int yb = window_text_bottom_y (w);
16583
16584 /* Find the row starting at new_start, if there is one. Don't
16585 reuse a partially visible line at the end. */
16586 first_reusable_row = start_row;
16587 while (first_reusable_row->enabled_p
16588 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16589 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16590 < CHARPOS (new_start)))
16591 ++first_reusable_row;
16592
16593 /* Give up if there is no row to reuse. */
16594 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16595 || !first_reusable_row->enabled_p
16596 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16597 != CHARPOS (new_start)))
16598 return 0;
16599
16600 /* We can reuse fully visible rows beginning with
16601 first_reusable_row to the end of the window. Set
16602 first_row_to_display to the first row that cannot be reused.
16603 Set pt_row to the row containing point, if there is any. */
16604 pt_row = NULL;
16605 for (first_row_to_display = first_reusable_row;
16606 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16607 ++first_row_to_display)
16608 {
16609 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16610 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16611 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16612 && first_row_to_display->ends_at_zv_p
16613 && pt_row == NULL)))
16614 pt_row = first_row_to_display;
16615 }
16616
16617 /* Start displaying at the start of first_row_to_display. */
16618 eassert (first_row_to_display->y < yb);
16619 init_to_row_start (&it, w, first_row_to_display);
16620
16621 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16622 - start_vpos);
16623 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16624 - nrows_scrolled);
16625 it.current_y = (first_row_to_display->y - first_reusable_row->y
16626 + WINDOW_HEADER_LINE_HEIGHT (w));
16627
16628 /* Display lines beginning with first_row_to_display in the
16629 desired matrix. Set last_text_row to the last row displayed
16630 that displays text. */
16631 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16632 if (pt_row == NULL)
16633 w->cursor.vpos = -1;
16634 last_text_row = NULL;
16635 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16636 if (display_line (&it))
16637 last_text_row = it.glyph_row - 1;
16638
16639 /* If point is in a reused row, adjust y and vpos of the cursor
16640 position. */
16641 if (pt_row)
16642 {
16643 w->cursor.vpos -= nrows_scrolled;
16644 w->cursor.y -= first_reusable_row->y - start_row->y;
16645 }
16646
16647 /* Give up if point isn't in a row displayed or reused. (This
16648 also handles the case where w->cursor.vpos < nrows_scrolled
16649 after the calls to display_line, which can happen with scroll
16650 margins. See bug#1295.) */
16651 if (w->cursor.vpos < 0)
16652 {
16653 clear_glyph_matrix (w->desired_matrix);
16654 return 0;
16655 }
16656
16657 /* Scroll the display. */
16658 run.current_y = first_reusable_row->y;
16659 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16660 run.height = it.last_visible_y - run.current_y;
16661 dy = run.current_y - run.desired_y;
16662
16663 if (run.height)
16664 {
16665 update_begin (f);
16666 FRAME_RIF (f)->update_window_begin_hook (w);
16667 FRAME_RIF (f)->clear_window_mouse_face (w);
16668 FRAME_RIF (f)->scroll_run_hook (w, &run);
16669 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16670 update_end (f);
16671 }
16672
16673 /* Adjust Y positions of reused rows. */
16674 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16675 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16676 max_y = it.last_visible_y;
16677 for (row = first_reusable_row; row < first_row_to_display; ++row)
16678 {
16679 row->y -= dy;
16680 row->visible_height = row->height;
16681 if (row->y < min_y)
16682 row->visible_height -= min_y - row->y;
16683 if (row->y + row->height > max_y)
16684 row->visible_height -= row->y + row->height - max_y;
16685 if (row->fringe_bitmap_periodic_p)
16686 row->redraw_fringe_bitmaps_p = 1;
16687 }
16688
16689 /* Scroll the current matrix. */
16690 eassert (nrows_scrolled > 0);
16691 rotate_matrix (w->current_matrix,
16692 start_vpos,
16693 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16694 -nrows_scrolled);
16695
16696 /* Disable rows not reused. */
16697 for (row -= nrows_scrolled; row < bottom_row; ++row)
16698 row->enabled_p = 0;
16699
16700 /* Point may have moved to a different line, so we cannot assume that
16701 the previous cursor position is valid; locate the correct row. */
16702 if (pt_row)
16703 {
16704 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16705 row < bottom_row
16706 && PT >= MATRIX_ROW_END_CHARPOS (row)
16707 && !row->ends_at_zv_p;
16708 row++)
16709 {
16710 w->cursor.vpos++;
16711 w->cursor.y = row->y;
16712 }
16713 if (row < bottom_row)
16714 {
16715 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16716 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16717
16718 /* Can't use this optimization with bidi-reordered glyph
16719 rows, unless cursor is already at point. */
16720 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16721 {
16722 if (!(w->cursor.hpos >= 0
16723 && w->cursor.hpos < row->used[TEXT_AREA]
16724 && BUFFERP (glyph->object)
16725 && glyph->charpos == PT))
16726 return 0;
16727 }
16728 else
16729 for (; glyph < end
16730 && (!BUFFERP (glyph->object)
16731 || glyph->charpos < PT);
16732 glyph++)
16733 {
16734 w->cursor.hpos++;
16735 w->cursor.x += glyph->pixel_width;
16736 }
16737 }
16738 }
16739
16740 /* Adjust window end. A null value of last_text_row means that
16741 the window end is in reused rows which in turn means that
16742 only its vpos can have changed. */
16743 if (last_text_row)
16744 {
16745 w->window_end_bytepos
16746 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16747 w->window_end_pos
16748 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16749 w->window_end_vpos
16750 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16751 }
16752 else
16753 {
16754 w->window_end_vpos
16755 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16756 }
16757
16758 w->window_end_valid = Qnil;
16759 w->desired_matrix->no_scrolling_p = 1;
16760
16761 #ifdef GLYPH_DEBUG
16762 debug_method_add (w, "try_window_reusing_current_matrix 2");
16763 #endif
16764 return 1;
16765 }
16766
16767 return 0;
16768 }
16769
16770
16771 \f
16772 /************************************************************************
16773 Window redisplay reusing current matrix when buffer has changed
16774 ************************************************************************/
16775
16776 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16777 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16778 ptrdiff_t *, ptrdiff_t *);
16779 static struct glyph_row *
16780 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16781 struct glyph_row *);
16782
16783
16784 /* Return the last row in MATRIX displaying text. If row START is
16785 non-null, start searching with that row. IT gives the dimensions
16786 of the display. Value is null if matrix is empty; otherwise it is
16787 a pointer to the row found. */
16788
16789 static struct glyph_row *
16790 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16791 struct glyph_row *start)
16792 {
16793 struct glyph_row *row, *row_found;
16794
16795 /* Set row_found to the last row in IT->w's current matrix
16796 displaying text. The loop looks funny but think of partially
16797 visible lines. */
16798 row_found = NULL;
16799 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16800 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16801 {
16802 eassert (row->enabled_p);
16803 row_found = row;
16804 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16805 break;
16806 ++row;
16807 }
16808
16809 return row_found;
16810 }
16811
16812
16813 /* Return the last row in the current matrix of W that is not affected
16814 by changes at the start of current_buffer that occurred since W's
16815 current matrix was built. Value is null if no such row exists.
16816
16817 BEG_UNCHANGED us the number of characters unchanged at the start of
16818 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16819 first changed character in current_buffer. Characters at positions <
16820 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16821 when the current matrix was built. */
16822
16823 static struct glyph_row *
16824 find_last_unchanged_at_beg_row (struct window *w)
16825 {
16826 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16827 struct glyph_row *row;
16828 struct glyph_row *row_found = NULL;
16829 int yb = window_text_bottom_y (w);
16830
16831 /* Find the last row displaying unchanged text. */
16832 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16833 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16834 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16835 ++row)
16836 {
16837 if (/* If row ends before first_changed_pos, it is unchanged,
16838 except in some case. */
16839 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16840 /* When row ends in ZV and we write at ZV it is not
16841 unchanged. */
16842 && !row->ends_at_zv_p
16843 /* When first_changed_pos is the end of a continued line,
16844 row is not unchanged because it may be no longer
16845 continued. */
16846 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16847 && (row->continued_p
16848 || row->exact_window_width_line_p))
16849 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16850 needs to be recomputed, so don't consider this row as
16851 unchanged. This happens when the last line was
16852 bidi-reordered and was killed immediately before this
16853 redisplay cycle. In that case, ROW->end stores the
16854 buffer position of the first visual-order character of
16855 the killed text, which is now beyond ZV. */
16856 && CHARPOS (row->end.pos) <= ZV)
16857 row_found = row;
16858
16859 /* Stop if last visible row. */
16860 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16861 break;
16862 }
16863
16864 return row_found;
16865 }
16866
16867
16868 /* Find the first glyph row in the current matrix of W that is not
16869 affected by changes at the end of current_buffer since the
16870 time W's current matrix was built.
16871
16872 Return in *DELTA the number of chars by which buffer positions in
16873 unchanged text at the end of current_buffer must be adjusted.
16874
16875 Return in *DELTA_BYTES the corresponding number of bytes.
16876
16877 Value is null if no such row exists, i.e. all rows are affected by
16878 changes. */
16879
16880 static struct glyph_row *
16881 find_first_unchanged_at_end_row (struct window *w,
16882 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16883 {
16884 struct glyph_row *row;
16885 struct glyph_row *row_found = NULL;
16886
16887 *delta = *delta_bytes = 0;
16888
16889 /* Display must not have been paused, otherwise the current matrix
16890 is not up to date. */
16891 eassert (!NILP (w->window_end_valid));
16892
16893 /* A value of window_end_pos >= END_UNCHANGED means that the window
16894 end is in the range of changed text. If so, there is no
16895 unchanged row at the end of W's current matrix. */
16896 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16897 return NULL;
16898
16899 /* Set row to the last row in W's current matrix displaying text. */
16900 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16901
16902 /* If matrix is entirely empty, no unchanged row exists. */
16903 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16904 {
16905 /* The value of row is the last glyph row in the matrix having a
16906 meaningful buffer position in it. The end position of row
16907 corresponds to window_end_pos. This allows us to translate
16908 buffer positions in the current matrix to current buffer
16909 positions for characters not in changed text. */
16910 ptrdiff_t Z_old =
16911 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16912 ptrdiff_t Z_BYTE_old =
16913 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16914 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16915 struct glyph_row *first_text_row
16916 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16917
16918 *delta = Z - Z_old;
16919 *delta_bytes = Z_BYTE - Z_BYTE_old;
16920
16921 /* Set last_unchanged_pos to the buffer position of the last
16922 character in the buffer that has not been changed. Z is the
16923 index + 1 of the last character in current_buffer, i.e. by
16924 subtracting END_UNCHANGED we get the index of the last
16925 unchanged character, and we have to add BEG to get its buffer
16926 position. */
16927 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16928 last_unchanged_pos_old = last_unchanged_pos - *delta;
16929
16930 /* Search backward from ROW for a row displaying a line that
16931 starts at a minimum position >= last_unchanged_pos_old. */
16932 for (; row > first_text_row; --row)
16933 {
16934 /* This used to abort, but it can happen.
16935 It is ok to just stop the search instead here. KFS. */
16936 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16937 break;
16938
16939 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16940 row_found = row;
16941 }
16942 }
16943
16944 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16945
16946 return row_found;
16947 }
16948
16949
16950 /* Make sure that glyph rows in the current matrix of window W
16951 reference the same glyph memory as corresponding rows in the
16952 frame's frame matrix. This function is called after scrolling W's
16953 current matrix on a terminal frame in try_window_id and
16954 try_window_reusing_current_matrix. */
16955
16956 static void
16957 sync_frame_with_window_matrix_rows (struct window *w)
16958 {
16959 struct frame *f = XFRAME (w->frame);
16960 struct glyph_row *window_row, *window_row_end, *frame_row;
16961
16962 /* Preconditions: W must be a leaf window and full-width. Its frame
16963 must have a frame matrix. */
16964 eassert (NILP (w->hchild) && NILP (w->vchild));
16965 eassert (WINDOW_FULL_WIDTH_P (w));
16966 eassert (!FRAME_WINDOW_P (f));
16967
16968 /* If W is a full-width window, glyph pointers in W's current matrix
16969 have, by definition, to be the same as glyph pointers in the
16970 corresponding frame matrix. Note that frame matrices have no
16971 marginal areas (see build_frame_matrix). */
16972 window_row = w->current_matrix->rows;
16973 window_row_end = window_row + w->current_matrix->nrows;
16974 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16975 while (window_row < window_row_end)
16976 {
16977 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16978 struct glyph *end = window_row->glyphs[LAST_AREA];
16979
16980 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16981 frame_row->glyphs[TEXT_AREA] = start;
16982 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16983 frame_row->glyphs[LAST_AREA] = end;
16984
16985 /* Disable frame rows whose corresponding window rows have
16986 been disabled in try_window_id. */
16987 if (!window_row->enabled_p)
16988 frame_row->enabled_p = 0;
16989
16990 ++window_row, ++frame_row;
16991 }
16992 }
16993
16994
16995 /* Find the glyph row in window W containing CHARPOS. Consider all
16996 rows between START and END (not inclusive). END null means search
16997 all rows to the end of the display area of W. Value is the row
16998 containing CHARPOS or null. */
16999
17000 struct glyph_row *
17001 row_containing_pos (struct window *w, ptrdiff_t charpos,
17002 struct glyph_row *start, struct glyph_row *end, int dy)
17003 {
17004 struct glyph_row *row = start;
17005 struct glyph_row *best_row = NULL;
17006 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17007 int last_y;
17008
17009 /* If we happen to start on a header-line, skip that. */
17010 if (row->mode_line_p)
17011 ++row;
17012
17013 if ((end && row >= end) || !row->enabled_p)
17014 return NULL;
17015
17016 last_y = window_text_bottom_y (w) - dy;
17017
17018 while (1)
17019 {
17020 /* Give up if we have gone too far. */
17021 if (end && row >= end)
17022 return NULL;
17023 /* This formerly returned if they were equal.
17024 I think that both quantities are of a "last plus one" type;
17025 if so, when they are equal, the row is within the screen. -- rms. */
17026 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17027 return NULL;
17028
17029 /* If it is in this row, return this row. */
17030 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17031 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17032 /* The end position of a row equals the start
17033 position of the next row. If CHARPOS is there, we
17034 would rather display it in the next line, except
17035 when this line ends in ZV. */
17036 && !row->ends_at_zv_p
17037 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17038 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17039 {
17040 struct glyph *g;
17041
17042 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17043 || (!best_row && !row->continued_p))
17044 return row;
17045 /* In bidi-reordered rows, there could be several rows
17046 occluding point, all of them belonging to the same
17047 continued line. We need to find the row which fits
17048 CHARPOS the best. */
17049 for (g = row->glyphs[TEXT_AREA];
17050 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17051 g++)
17052 {
17053 if (!STRINGP (g->object))
17054 {
17055 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17056 {
17057 mindif = eabs (g->charpos - charpos);
17058 best_row = row;
17059 /* Exact match always wins. */
17060 if (mindif == 0)
17061 return best_row;
17062 }
17063 }
17064 }
17065 }
17066 else if (best_row && !row->continued_p)
17067 return best_row;
17068 ++row;
17069 }
17070 }
17071
17072
17073 /* Try to redisplay window W by reusing its existing display. W's
17074 current matrix must be up to date when this function is called,
17075 i.e. window_end_valid must not be nil.
17076
17077 Value is
17078
17079 1 if display has been updated
17080 0 if otherwise unsuccessful
17081 -1 if redisplay with same window start is known not to succeed
17082
17083 The following steps are performed:
17084
17085 1. Find the last row in the current matrix of W that is not
17086 affected by changes at the start of current_buffer. If no such row
17087 is found, give up.
17088
17089 2. Find the first row in W's current matrix that is not affected by
17090 changes at the end of current_buffer. Maybe there is no such row.
17091
17092 3. Display lines beginning with the row + 1 found in step 1 to the
17093 row found in step 2 or, if step 2 didn't find a row, to the end of
17094 the window.
17095
17096 4. If cursor is not known to appear on the window, give up.
17097
17098 5. If display stopped at the row found in step 2, scroll the
17099 display and current matrix as needed.
17100
17101 6. Maybe display some lines at the end of W, if we must. This can
17102 happen under various circumstances, like a partially visible line
17103 becoming fully visible, or because newly displayed lines are displayed
17104 in smaller font sizes.
17105
17106 7. Update W's window end information. */
17107
17108 static int
17109 try_window_id (struct window *w)
17110 {
17111 struct frame *f = XFRAME (w->frame);
17112 struct glyph_matrix *current_matrix = w->current_matrix;
17113 struct glyph_matrix *desired_matrix = w->desired_matrix;
17114 struct glyph_row *last_unchanged_at_beg_row;
17115 struct glyph_row *first_unchanged_at_end_row;
17116 struct glyph_row *row;
17117 struct glyph_row *bottom_row;
17118 int bottom_vpos;
17119 struct it it;
17120 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17121 int dvpos, dy;
17122 struct text_pos start_pos;
17123 struct run run;
17124 int first_unchanged_at_end_vpos = 0;
17125 struct glyph_row *last_text_row, *last_text_row_at_end;
17126 struct text_pos start;
17127 ptrdiff_t first_changed_charpos, last_changed_charpos;
17128
17129 #ifdef GLYPH_DEBUG
17130 if (inhibit_try_window_id)
17131 return 0;
17132 #endif
17133
17134 /* This is handy for debugging. */
17135 #if 0
17136 #define GIVE_UP(X) \
17137 do { \
17138 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17139 return 0; \
17140 } while (0)
17141 #else
17142 #define GIVE_UP(X) return 0
17143 #endif
17144
17145 SET_TEXT_POS_FROM_MARKER (start, w->start);
17146
17147 /* Don't use this for mini-windows because these can show
17148 messages and mini-buffers, and we don't handle that here. */
17149 if (MINI_WINDOW_P (w))
17150 GIVE_UP (1);
17151
17152 /* This flag is used to prevent redisplay optimizations. */
17153 if (windows_or_buffers_changed || cursor_type_changed)
17154 GIVE_UP (2);
17155
17156 /* Verify that narrowing has not changed.
17157 Also verify that we were not told to prevent redisplay optimizations.
17158 It would be nice to further
17159 reduce the number of cases where this prevents try_window_id. */
17160 if (current_buffer->clip_changed
17161 || current_buffer->prevent_redisplay_optimizations_p)
17162 GIVE_UP (3);
17163
17164 /* Window must either use window-based redisplay or be full width. */
17165 if (!FRAME_WINDOW_P (f)
17166 && (!FRAME_LINE_INS_DEL_OK (f)
17167 || !WINDOW_FULL_WIDTH_P (w)))
17168 GIVE_UP (4);
17169
17170 /* Give up if point is known NOT to appear in W. */
17171 if (PT < CHARPOS (start))
17172 GIVE_UP (5);
17173
17174 /* Another way to prevent redisplay optimizations. */
17175 if (w->last_modified == 0)
17176 GIVE_UP (6);
17177
17178 /* Verify that window is not hscrolled. */
17179 if (w->hscroll != 0)
17180 GIVE_UP (7);
17181
17182 /* Verify that display wasn't paused. */
17183 if (NILP (w->window_end_valid))
17184 GIVE_UP (8);
17185
17186 /* Can't use this if highlighting a region because a cursor movement
17187 will do more than just set the cursor. */
17188 if (!NILP (Vtransient_mark_mode)
17189 && !NILP (BVAR (current_buffer, mark_active)))
17190 GIVE_UP (9);
17191
17192 /* Likewise if highlighting trailing whitespace. */
17193 if (!NILP (Vshow_trailing_whitespace))
17194 GIVE_UP (11);
17195
17196 /* Likewise if showing a region. */
17197 if (!NILP (w->region_showing))
17198 GIVE_UP (10);
17199
17200 /* Can't use this if overlay arrow position and/or string have
17201 changed. */
17202 if (overlay_arrows_changed_p ())
17203 GIVE_UP (12);
17204
17205 /* When word-wrap is on, adding a space to the first word of a
17206 wrapped line can change the wrap position, altering the line
17207 above it. It might be worthwhile to handle this more
17208 intelligently, but for now just redisplay from scratch. */
17209 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17210 GIVE_UP (21);
17211
17212 /* Under bidi reordering, adding or deleting a character in the
17213 beginning of a paragraph, before the first strong directional
17214 character, can change the base direction of the paragraph (unless
17215 the buffer specifies a fixed paragraph direction), which will
17216 require to redisplay the whole paragraph. It might be worthwhile
17217 to find the paragraph limits and widen the range of redisplayed
17218 lines to that, but for now just give up this optimization and
17219 redisplay from scratch. */
17220 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17221 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17222 GIVE_UP (22);
17223
17224 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17225 only if buffer has really changed. The reason is that the gap is
17226 initially at Z for freshly visited files. The code below would
17227 set end_unchanged to 0 in that case. */
17228 if (MODIFF > SAVE_MODIFF
17229 /* This seems to happen sometimes after saving a buffer. */
17230 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17231 {
17232 if (GPT - BEG < BEG_UNCHANGED)
17233 BEG_UNCHANGED = GPT - BEG;
17234 if (Z - GPT < END_UNCHANGED)
17235 END_UNCHANGED = Z - GPT;
17236 }
17237
17238 /* The position of the first and last character that has been changed. */
17239 first_changed_charpos = BEG + BEG_UNCHANGED;
17240 last_changed_charpos = Z - END_UNCHANGED;
17241
17242 /* If window starts after a line end, and the last change is in
17243 front of that newline, then changes don't affect the display.
17244 This case happens with stealth-fontification. Note that although
17245 the display is unchanged, glyph positions in the matrix have to
17246 be adjusted, of course. */
17247 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17248 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17249 && ((last_changed_charpos < CHARPOS (start)
17250 && CHARPOS (start) == BEGV)
17251 || (last_changed_charpos < CHARPOS (start) - 1
17252 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17253 {
17254 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17255 struct glyph_row *r0;
17256
17257 /* Compute how many chars/bytes have been added to or removed
17258 from the buffer. */
17259 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17260 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17261 Z_delta = Z - Z_old;
17262 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17263
17264 /* Give up if PT is not in the window. Note that it already has
17265 been checked at the start of try_window_id that PT is not in
17266 front of the window start. */
17267 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17268 GIVE_UP (13);
17269
17270 /* If window start is unchanged, we can reuse the whole matrix
17271 as is, after adjusting glyph positions. No need to compute
17272 the window end again, since its offset from Z hasn't changed. */
17273 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17274 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17275 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17276 /* PT must not be in a partially visible line. */
17277 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17278 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17279 {
17280 /* Adjust positions in the glyph matrix. */
17281 if (Z_delta || Z_delta_bytes)
17282 {
17283 struct glyph_row *r1
17284 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17285 increment_matrix_positions (w->current_matrix,
17286 MATRIX_ROW_VPOS (r0, current_matrix),
17287 MATRIX_ROW_VPOS (r1, current_matrix),
17288 Z_delta, Z_delta_bytes);
17289 }
17290
17291 /* Set the cursor. */
17292 row = row_containing_pos (w, PT, r0, NULL, 0);
17293 if (row)
17294 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17295 else
17296 abort ();
17297 return 1;
17298 }
17299 }
17300
17301 /* Handle the case that changes are all below what is displayed in
17302 the window, and that PT is in the window. This shortcut cannot
17303 be taken if ZV is visible in the window, and text has been added
17304 there that is visible in the window. */
17305 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17306 /* ZV is not visible in the window, or there are no
17307 changes at ZV, actually. */
17308 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17309 || first_changed_charpos == last_changed_charpos))
17310 {
17311 struct glyph_row *r0;
17312
17313 /* Give up if PT is not in the window. Note that it already has
17314 been checked at the start of try_window_id that PT is not in
17315 front of the window start. */
17316 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17317 GIVE_UP (14);
17318
17319 /* If window start is unchanged, we can reuse the whole matrix
17320 as is, without changing glyph positions since no text has
17321 been added/removed in front of the window end. */
17322 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17323 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17324 /* PT must not be in a partially visible line. */
17325 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17326 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17327 {
17328 /* We have to compute the window end anew since text
17329 could have been added/removed after it. */
17330 w->window_end_pos
17331 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17332 w->window_end_bytepos
17333 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17334
17335 /* Set the cursor. */
17336 row = row_containing_pos (w, PT, r0, NULL, 0);
17337 if (row)
17338 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17339 else
17340 abort ();
17341 return 2;
17342 }
17343 }
17344
17345 /* Give up if window start is in the changed area.
17346
17347 The condition used to read
17348
17349 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17350
17351 but why that was tested escapes me at the moment. */
17352 if (CHARPOS (start) >= first_changed_charpos
17353 && CHARPOS (start) <= last_changed_charpos)
17354 GIVE_UP (15);
17355
17356 /* Check that window start agrees with the start of the first glyph
17357 row in its current matrix. Check this after we know the window
17358 start is not in changed text, otherwise positions would not be
17359 comparable. */
17360 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17361 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17362 GIVE_UP (16);
17363
17364 /* Give up if the window ends in strings. Overlay strings
17365 at the end are difficult to handle, so don't try. */
17366 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17367 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17368 GIVE_UP (20);
17369
17370 /* Compute the position at which we have to start displaying new
17371 lines. Some of the lines at the top of the window might be
17372 reusable because they are not displaying changed text. Find the
17373 last row in W's current matrix not affected by changes at the
17374 start of current_buffer. Value is null if changes start in the
17375 first line of window. */
17376 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17377 if (last_unchanged_at_beg_row)
17378 {
17379 /* Avoid starting to display in the middle of a character, a TAB
17380 for instance. This is easier than to set up the iterator
17381 exactly, and it's not a frequent case, so the additional
17382 effort wouldn't really pay off. */
17383 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17384 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17385 && last_unchanged_at_beg_row > w->current_matrix->rows)
17386 --last_unchanged_at_beg_row;
17387
17388 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17389 GIVE_UP (17);
17390
17391 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17392 GIVE_UP (18);
17393 start_pos = it.current.pos;
17394
17395 /* Start displaying new lines in the desired matrix at the same
17396 vpos we would use in the current matrix, i.e. below
17397 last_unchanged_at_beg_row. */
17398 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17399 current_matrix);
17400 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17401 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17402
17403 eassert (it.hpos == 0 && it.current_x == 0);
17404 }
17405 else
17406 {
17407 /* There are no reusable lines at the start of the window.
17408 Start displaying in the first text line. */
17409 start_display (&it, w, start);
17410 it.vpos = it.first_vpos;
17411 start_pos = it.current.pos;
17412 }
17413
17414 /* Find the first row that is not affected by changes at the end of
17415 the buffer. Value will be null if there is no unchanged row, in
17416 which case we must redisplay to the end of the window. delta
17417 will be set to the value by which buffer positions beginning with
17418 first_unchanged_at_end_row have to be adjusted due to text
17419 changes. */
17420 first_unchanged_at_end_row
17421 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17422 IF_DEBUG (debug_delta = delta);
17423 IF_DEBUG (debug_delta_bytes = delta_bytes);
17424
17425 /* Set stop_pos to the buffer position up to which we will have to
17426 display new lines. If first_unchanged_at_end_row != NULL, this
17427 is the buffer position of the start of the line displayed in that
17428 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17429 that we don't stop at a buffer position. */
17430 stop_pos = 0;
17431 if (first_unchanged_at_end_row)
17432 {
17433 eassert (last_unchanged_at_beg_row == NULL
17434 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17435
17436 /* If this is a continuation line, move forward to the next one
17437 that isn't. Changes in lines above affect this line.
17438 Caution: this may move first_unchanged_at_end_row to a row
17439 not displaying text. */
17440 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17441 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17442 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17443 < it.last_visible_y))
17444 ++first_unchanged_at_end_row;
17445
17446 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17447 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17448 >= it.last_visible_y))
17449 first_unchanged_at_end_row = NULL;
17450 else
17451 {
17452 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17453 + delta);
17454 first_unchanged_at_end_vpos
17455 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17456 eassert (stop_pos >= Z - END_UNCHANGED);
17457 }
17458 }
17459 else if (last_unchanged_at_beg_row == NULL)
17460 GIVE_UP (19);
17461
17462
17463 #ifdef GLYPH_DEBUG
17464
17465 /* Either there is no unchanged row at the end, or the one we have
17466 now displays text. This is a necessary condition for the window
17467 end pos calculation at the end of this function. */
17468 eassert (first_unchanged_at_end_row == NULL
17469 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17470
17471 debug_last_unchanged_at_beg_vpos
17472 = (last_unchanged_at_beg_row
17473 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17474 : -1);
17475 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17476
17477 #endif /* GLYPH_DEBUG */
17478
17479
17480 /* Display new lines. Set last_text_row to the last new line
17481 displayed which has text on it, i.e. might end up as being the
17482 line where the window_end_vpos is. */
17483 w->cursor.vpos = -1;
17484 last_text_row = NULL;
17485 overlay_arrow_seen = 0;
17486 while (it.current_y < it.last_visible_y
17487 && !fonts_changed_p
17488 && (first_unchanged_at_end_row == NULL
17489 || IT_CHARPOS (it) < stop_pos))
17490 {
17491 if (display_line (&it))
17492 last_text_row = it.glyph_row - 1;
17493 }
17494
17495 if (fonts_changed_p)
17496 return -1;
17497
17498
17499 /* Compute differences in buffer positions, y-positions etc. for
17500 lines reused at the bottom of the window. Compute what we can
17501 scroll. */
17502 if (first_unchanged_at_end_row
17503 /* No lines reused because we displayed everything up to the
17504 bottom of the window. */
17505 && it.current_y < it.last_visible_y)
17506 {
17507 dvpos = (it.vpos
17508 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17509 current_matrix));
17510 dy = it.current_y - first_unchanged_at_end_row->y;
17511 run.current_y = first_unchanged_at_end_row->y;
17512 run.desired_y = run.current_y + dy;
17513 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17514 }
17515 else
17516 {
17517 delta = delta_bytes = dvpos = dy
17518 = run.current_y = run.desired_y = run.height = 0;
17519 first_unchanged_at_end_row = NULL;
17520 }
17521 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17522
17523
17524 /* Find the cursor if not already found. We have to decide whether
17525 PT will appear on this window (it sometimes doesn't, but this is
17526 not a very frequent case.) This decision has to be made before
17527 the current matrix is altered. A value of cursor.vpos < 0 means
17528 that PT is either in one of the lines beginning at
17529 first_unchanged_at_end_row or below the window. Don't care for
17530 lines that might be displayed later at the window end; as
17531 mentioned, this is not a frequent case. */
17532 if (w->cursor.vpos < 0)
17533 {
17534 /* Cursor in unchanged rows at the top? */
17535 if (PT < CHARPOS (start_pos)
17536 && last_unchanged_at_beg_row)
17537 {
17538 row = row_containing_pos (w, PT,
17539 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17540 last_unchanged_at_beg_row + 1, 0);
17541 if (row)
17542 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17543 }
17544
17545 /* Start from first_unchanged_at_end_row looking for PT. */
17546 else if (first_unchanged_at_end_row)
17547 {
17548 row = row_containing_pos (w, PT - delta,
17549 first_unchanged_at_end_row, NULL, 0);
17550 if (row)
17551 set_cursor_from_row (w, row, w->current_matrix, delta,
17552 delta_bytes, dy, dvpos);
17553 }
17554
17555 /* Give up if cursor was not found. */
17556 if (w->cursor.vpos < 0)
17557 {
17558 clear_glyph_matrix (w->desired_matrix);
17559 return -1;
17560 }
17561 }
17562
17563 /* Don't let the cursor end in the scroll margins. */
17564 {
17565 int this_scroll_margin, cursor_height;
17566
17567 this_scroll_margin =
17568 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17569 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17570 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17571
17572 if ((w->cursor.y < this_scroll_margin
17573 && CHARPOS (start) > BEGV)
17574 /* Old redisplay didn't take scroll margin into account at the bottom,
17575 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17576 || (w->cursor.y + (make_cursor_line_fully_visible_p
17577 ? cursor_height + this_scroll_margin
17578 : 1)) > it.last_visible_y)
17579 {
17580 w->cursor.vpos = -1;
17581 clear_glyph_matrix (w->desired_matrix);
17582 return -1;
17583 }
17584 }
17585
17586 /* Scroll the display. Do it before changing the current matrix so
17587 that xterm.c doesn't get confused about where the cursor glyph is
17588 found. */
17589 if (dy && run.height)
17590 {
17591 update_begin (f);
17592
17593 if (FRAME_WINDOW_P (f))
17594 {
17595 FRAME_RIF (f)->update_window_begin_hook (w);
17596 FRAME_RIF (f)->clear_window_mouse_face (w);
17597 FRAME_RIF (f)->scroll_run_hook (w, &run);
17598 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17599 }
17600 else
17601 {
17602 /* Terminal frame. In this case, dvpos gives the number of
17603 lines to scroll by; dvpos < 0 means scroll up. */
17604 int from_vpos
17605 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17606 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17607 int end = (WINDOW_TOP_EDGE_LINE (w)
17608 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17609 + window_internal_height (w));
17610
17611 #if defined (HAVE_GPM) || defined (MSDOS)
17612 x_clear_window_mouse_face (w);
17613 #endif
17614 /* Perform the operation on the screen. */
17615 if (dvpos > 0)
17616 {
17617 /* Scroll last_unchanged_at_beg_row to the end of the
17618 window down dvpos lines. */
17619 set_terminal_window (f, end);
17620
17621 /* On dumb terminals delete dvpos lines at the end
17622 before inserting dvpos empty lines. */
17623 if (!FRAME_SCROLL_REGION_OK (f))
17624 ins_del_lines (f, end - dvpos, -dvpos);
17625
17626 /* Insert dvpos empty lines in front of
17627 last_unchanged_at_beg_row. */
17628 ins_del_lines (f, from, dvpos);
17629 }
17630 else if (dvpos < 0)
17631 {
17632 /* Scroll up last_unchanged_at_beg_vpos to the end of
17633 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17634 set_terminal_window (f, end);
17635
17636 /* Delete dvpos lines in front of
17637 last_unchanged_at_beg_vpos. ins_del_lines will set
17638 the cursor to the given vpos and emit |dvpos| delete
17639 line sequences. */
17640 ins_del_lines (f, from + dvpos, dvpos);
17641
17642 /* On a dumb terminal insert dvpos empty lines at the
17643 end. */
17644 if (!FRAME_SCROLL_REGION_OK (f))
17645 ins_del_lines (f, end + dvpos, -dvpos);
17646 }
17647
17648 set_terminal_window (f, 0);
17649 }
17650
17651 update_end (f);
17652 }
17653
17654 /* Shift reused rows of the current matrix to the right position.
17655 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17656 text. */
17657 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17658 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17659 if (dvpos < 0)
17660 {
17661 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17662 bottom_vpos, dvpos);
17663 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17664 bottom_vpos, 0);
17665 }
17666 else if (dvpos > 0)
17667 {
17668 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17669 bottom_vpos, dvpos);
17670 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17671 first_unchanged_at_end_vpos + dvpos, 0);
17672 }
17673
17674 /* For frame-based redisplay, make sure that current frame and window
17675 matrix are in sync with respect to glyph memory. */
17676 if (!FRAME_WINDOW_P (f))
17677 sync_frame_with_window_matrix_rows (w);
17678
17679 /* Adjust buffer positions in reused rows. */
17680 if (delta || delta_bytes)
17681 increment_matrix_positions (current_matrix,
17682 first_unchanged_at_end_vpos + dvpos,
17683 bottom_vpos, delta, delta_bytes);
17684
17685 /* Adjust Y positions. */
17686 if (dy)
17687 shift_glyph_matrix (w, current_matrix,
17688 first_unchanged_at_end_vpos + dvpos,
17689 bottom_vpos, dy);
17690
17691 if (first_unchanged_at_end_row)
17692 {
17693 first_unchanged_at_end_row += dvpos;
17694 if (first_unchanged_at_end_row->y >= it.last_visible_y
17695 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17696 first_unchanged_at_end_row = NULL;
17697 }
17698
17699 /* If scrolling up, there may be some lines to display at the end of
17700 the window. */
17701 last_text_row_at_end = NULL;
17702 if (dy < 0)
17703 {
17704 /* Scrolling up can leave for example a partially visible line
17705 at the end of the window to be redisplayed. */
17706 /* Set last_row to the glyph row in the current matrix where the
17707 window end line is found. It has been moved up or down in
17708 the matrix by dvpos. */
17709 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17710 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17711
17712 /* If last_row is the window end line, it should display text. */
17713 eassert (last_row->displays_text_p);
17714
17715 /* If window end line was partially visible before, begin
17716 displaying at that line. Otherwise begin displaying with the
17717 line following it. */
17718 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17719 {
17720 init_to_row_start (&it, w, last_row);
17721 it.vpos = last_vpos;
17722 it.current_y = last_row->y;
17723 }
17724 else
17725 {
17726 init_to_row_end (&it, w, last_row);
17727 it.vpos = 1 + last_vpos;
17728 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17729 ++last_row;
17730 }
17731
17732 /* We may start in a continuation line. If so, we have to
17733 get the right continuation_lines_width and current_x. */
17734 it.continuation_lines_width = last_row->continuation_lines_width;
17735 it.hpos = it.current_x = 0;
17736
17737 /* Display the rest of the lines at the window end. */
17738 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17739 while (it.current_y < it.last_visible_y
17740 && !fonts_changed_p)
17741 {
17742 /* Is it always sure that the display agrees with lines in
17743 the current matrix? I don't think so, so we mark rows
17744 displayed invalid in the current matrix by setting their
17745 enabled_p flag to zero. */
17746 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17747 if (display_line (&it))
17748 last_text_row_at_end = it.glyph_row - 1;
17749 }
17750 }
17751
17752 /* Update window_end_pos and window_end_vpos. */
17753 if (first_unchanged_at_end_row
17754 && !last_text_row_at_end)
17755 {
17756 /* Window end line if one of the preserved rows from the current
17757 matrix. Set row to the last row displaying text in current
17758 matrix starting at first_unchanged_at_end_row, after
17759 scrolling. */
17760 eassert (first_unchanged_at_end_row->displays_text_p);
17761 row = find_last_row_displaying_text (w->current_matrix, &it,
17762 first_unchanged_at_end_row);
17763 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17764
17765 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17766 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17767 w->window_end_vpos
17768 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17769 eassert (w->window_end_bytepos >= 0);
17770 IF_DEBUG (debug_method_add (w, "A"));
17771 }
17772 else if (last_text_row_at_end)
17773 {
17774 w->window_end_pos
17775 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17776 w->window_end_bytepos
17777 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17778 w->window_end_vpos
17779 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17780 eassert (w->window_end_bytepos >= 0);
17781 IF_DEBUG (debug_method_add (w, "B"));
17782 }
17783 else if (last_text_row)
17784 {
17785 /* We have displayed either to the end of the window or at the
17786 end of the window, i.e. the last row with text is to be found
17787 in the desired matrix. */
17788 w->window_end_pos
17789 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17790 w->window_end_bytepos
17791 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17792 w->window_end_vpos
17793 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17794 eassert (w->window_end_bytepos >= 0);
17795 }
17796 else if (first_unchanged_at_end_row == NULL
17797 && last_text_row == NULL
17798 && last_text_row_at_end == NULL)
17799 {
17800 /* Displayed to end of window, but no line containing text was
17801 displayed. Lines were deleted at the end of the window. */
17802 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17803 int vpos = XFASTINT (w->window_end_vpos);
17804 struct glyph_row *current_row = current_matrix->rows + vpos;
17805 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17806
17807 for (row = NULL;
17808 row == NULL && vpos >= first_vpos;
17809 --vpos, --current_row, --desired_row)
17810 {
17811 if (desired_row->enabled_p)
17812 {
17813 if (desired_row->displays_text_p)
17814 row = desired_row;
17815 }
17816 else if (current_row->displays_text_p)
17817 row = current_row;
17818 }
17819
17820 eassert (row != NULL);
17821 w->window_end_vpos = make_number (vpos + 1);
17822 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17823 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17824 eassert (w->window_end_bytepos >= 0);
17825 IF_DEBUG (debug_method_add (w, "C"));
17826 }
17827 else
17828 abort ();
17829
17830 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17831 debug_end_vpos = XFASTINT (w->window_end_vpos));
17832
17833 /* Record that display has not been completed. */
17834 w->window_end_valid = Qnil;
17835 w->desired_matrix->no_scrolling_p = 1;
17836 return 3;
17837
17838 #undef GIVE_UP
17839 }
17840
17841
17842 \f
17843 /***********************************************************************
17844 More debugging support
17845 ***********************************************************************/
17846
17847 #ifdef GLYPH_DEBUG
17848
17849 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17850 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17851 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17852
17853
17854 /* Dump the contents of glyph matrix MATRIX on stderr.
17855
17856 GLYPHS 0 means don't show glyph contents.
17857 GLYPHS 1 means show glyphs in short form
17858 GLYPHS > 1 means show glyphs in long form. */
17859
17860 void
17861 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17862 {
17863 int i;
17864 for (i = 0; i < matrix->nrows; ++i)
17865 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17866 }
17867
17868
17869 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17870 the glyph row and area where the glyph comes from. */
17871
17872 void
17873 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17874 {
17875 if (glyph->type == CHAR_GLYPH)
17876 {
17877 fprintf (stderr,
17878 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17879 glyph - row->glyphs[TEXT_AREA],
17880 'C',
17881 glyph->charpos,
17882 (BUFFERP (glyph->object)
17883 ? 'B'
17884 : (STRINGP (glyph->object)
17885 ? 'S'
17886 : '-')),
17887 glyph->pixel_width,
17888 glyph->u.ch,
17889 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17890 ? glyph->u.ch
17891 : '.'),
17892 glyph->face_id,
17893 glyph->left_box_line_p,
17894 glyph->right_box_line_p);
17895 }
17896 else if (glyph->type == STRETCH_GLYPH)
17897 {
17898 fprintf (stderr,
17899 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17900 glyph - row->glyphs[TEXT_AREA],
17901 'S',
17902 glyph->charpos,
17903 (BUFFERP (glyph->object)
17904 ? 'B'
17905 : (STRINGP (glyph->object)
17906 ? 'S'
17907 : '-')),
17908 glyph->pixel_width,
17909 0,
17910 '.',
17911 glyph->face_id,
17912 glyph->left_box_line_p,
17913 glyph->right_box_line_p);
17914 }
17915 else if (glyph->type == IMAGE_GLYPH)
17916 {
17917 fprintf (stderr,
17918 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17919 glyph - row->glyphs[TEXT_AREA],
17920 'I',
17921 glyph->charpos,
17922 (BUFFERP (glyph->object)
17923 ? 'B'
17924 : (STRINGP (glyph->object)
17925 ? 'S'
17926 : '-')),
17927 glyph->pixel_width,
17928 glyph->u.img_id,
17929 '.',
17930 glyph->face_id,
17931 glyph->left_box_line_p,
17932 glyph->right_box_line_p);
17933 }
17934 else if (glyph->type == COMPOSITE_GLYPH)
17935 {
17936 fprintf (stderr,
17937 " %5td %4c %6"pI"d %c %3d 0x%05x",
17938 glyph - row->glyphs[TEXT_AREA],
17939 '+',
17940 glyph->charpos,
17941 (BUFFERP (glyph->object)
17942 ? 'B'
17943 : (STRINGP (glyph->object)
17944 ? 'S'
17945 : '-')),
17946 glyph->pixel_width,
17947 glyph->u.cmp.id);
17948 if (glyph->u.cmp.automatic)
17949 fprintf (stderr,
17950 "[%d-%d]",
17951 glyph->slice.cmp.from, glyph->slice.cmp.to);
17952 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17953 glyph->face_id,
17954 glyph->left_box_line_p,
17955 glyph->right_box_line_p);
17956 }
17957 }
17958
17959
17960 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17961 GLYPHS 0 means don't show glyph contents.
17962 GLYPHS 1 means show glyphs in short form
17963 GLYPHS > 1 means show glyphs in long form. */
17964
17965 void
17966 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17967 {
17968 if (glyphs != 1)
17969 {
17970 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17971 fprintf (stderr, "======================================================================\n");
17972
17973 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17974 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17975 vpos,
17976 MATRIX_ROW_START_CHARPOS (row),
17977 MATRIX_ROW_END_CHARPOS (row),
17978 row->used[TEXT_AREA],
17979 row->contains_overlapping_glyphs_p,
17980 row->enabled_p,
17981 row->truncated_on_left_p,
17982 row->truncated_on_right_p,
17983 row->continued_p,
17984 MATRIX_ROW_CONTINUATION_LINE_P (row),
17985 row->displays_text_p,
17986 row->ends_at_zv_p,
17987 row->fill_line_p,
17988 row->ends_in_middle_of_char_p,
17989 row->starts_in_middle_of_char_p,
17990 row->mouse_face_p,
17991 row->x,
17992 row->y,
17993 row->pixel_width,
17994 row->height,
17995 row->visible_height,
17996 row->ascent,
17997 row->phys_ascent);
17998 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17999 row->end.overlay_string_index,
18000 row->continuation_lines_width);
18001 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18002 CHARPOS (row->start.string_pos),
18003 CHARPOS (row->end.string_pos));
18004 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18005 row->end.dpvec_index);
18006 }
18007
18008 if (glyphs > 1)
18009 {
18010 int area;
18011
18012 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18013 {
18014 struct glyph *glyph = row->glyphs[area];
18015 struct glyph *glyph_end = glyph + row->used[area];
18016
18017 /* Glyph for a line end in text. */
18018 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18019 ++glyph_end;
18020
18021 if (glyph < glyph_end)
18022 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18023
18024 for (; glyph < glyph_end; ++glyph)
18025 dump_glyph (row, glyph, area);
18026 }
18027 }
18028 else if (glyphs == 1)
18029 {
18030 int area;
18031
18032 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18033 {
18034 char *s = alloca (row->used[area] + 1);
18035 int i;
18036
18037 for (i = 0; i < row->used[area]; ++i)
18038 {
18039 struct glyph *glyph = row->glyphs[area] + i;
18040 if (glyph->type == CHAR_GLYPH
18041 && glyph->u.ch < 0x80
18042 && glyph->u.ch >= ' ')
18043 s[i] = glyph->u.ch;
18044 else
18045 s[i] = '.';
18046 }
18047
18048 s[i] = '\0';
18049 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18050 }
18051 }
18052 }
18053
18054
18055 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18056 Sdump_glyph_matrix, 0, 1, "p",
18057 doc: /* Dump the current matrix of the selected window to stderr.
18058 Shows contents of glyph row structures. With non-nil
18059 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18060 glyphs in short form, otherwise show glyphs in long form. */)
18061 (Lisp_Object glyphs)
18062 {
18063 struct window *w = XWINDOW (selected_window);
18064 struct buffer *buffer = XBUFFER (w->buffer);
18065
18066 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18067 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18068 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18069 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18070 fprintf (stderr, "=============================================\n");
18071 dump_glyph_matrix (w->current_matrix,
18072 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18073 return Qnil;
18074 }
18075
18076
18077 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18078 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18079 (void)
18080 {
18081 struct frame *f = XFRAME (selected_frame);
18082 dump_glyph_matrix (f->current_matrix, 1);
18083 return Qnil;
18084 }
18085
18086
18087 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18088 doc: /* Dump glyph row ROW to stderr.
18089 GLYPH 0 means don't dump glyphs.
18090 GLYPH 1 means dump glyphs in short form.
18091 GLYPH > 1 or omitted means dump glyphs in long form. */)
18092 (Lisp_Object row, Lisp_Object glyphs)
18093 {
18094 struct glyph_matrix *matrix;
18095 EMACS_INT vpos;
18096
18097 CHECK_NUMBER (row);
18098 matrix = XWINDOW (selected_window)->current_matrix;
18099 vpos = XINT (row);
18100 if (vpos >= 0 && vpos < matrix->nrows)
18101 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18102 vpos,
18103 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18104 return Qnil;
18105 }
18106
18107
18108 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18109 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18110 GLYPH 0 means don't dump glyphs.
18111 GLYPH 1 means dump glyphs in short form.
18112 GLYPH > 1 or omitted means dump glyphs in long form. */)
18113 (Lisp_Object row, Lisp_Object glyphs)
18114 {
18115 struct frame *sf = SELECTED_FRAME ();
18116 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18117 EMACS_INT vpos;
18118
18119 CHECK_NUMBER (row);
18120 vpos = XINT (row);
18121 if (vpos >= 0 && vpos < m->nrows)
18122 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18123 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18124 return Qnil;
18125 }
18126
18127
18128 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18129 doc: /* Toggle tracing of redisplay.
18130 With ARG, turn tracing on if and only if ARG is positive. */)
18131 (Lisp_Object arg)
18132 {
18133 if (NILP (arg))
18134 trace_redisplay_p = !trace_redisplay_p;
18135 else
18136 {
18137 arg = Fprefix_numeric_value (arg);
18138 trace_redisplay_p = XINT (arg) > 0;
18139 }
18140
18141 return Qnil;
18142 }
18143
18144
18145 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18146 doc: /* Like `format', but print result to stderr.
18147 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18148 (ptrdiff_t nargs, Lisp_Object *args)
18149 {
18150 Lisp_Object s = Fformat (nargs, args);
18151 fprintf (stderr, "%s", SDATA (s));
18152 return Qnil;
18153 }
18154
18155 #endif /* GLYPH_DEBUG */
18156
18157
18158 \f
18159 /***********************************************************************
18160 Building Desired Matrix Rows
18161 ***********************************************************************/
18162
18163 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18164 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18165
18166 static struct glyph_row *
18167 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18168 {
18169 struct frame *f = XFRAME (WINDOW_FRAME (w));
18170 struct buffer *buffer = XBUFFER (w->buffer);
18171 struct buffer *old = current_buffer;
18172 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18173 int arrow_len = SCHARS (overlay_arrow_string);
18174 const unsigned char *arrow_end = arrow_string + arrow_len;
18175 const unsigned char *p;
18176 struct it it;
18177 int multibyte_p;
18178 int n_glyphs_before;
18179
18180 set_buffer_temp (buffer);
18181 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18182 it.glyph_row->used[TEXT_AREA] = 0;
18183 SET_TEXT_POS (it.position, 0, 0);
18184
18185 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18186 p = arrow_string;
18187 while (p < arrow_end)
18188 {
18189 Lisp_Object face, ilisp;
18190
18191 /* Get the next character. */
18192 if (multibyte_p)
18193 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18194 else
18195 {
18196 it.c = it.char_to_display = *p, it.len = 1;
18197 if (! ASCII_CHAR_P (it.c))
18198 it.char_to_display = BYTE8_TO_CHAR (it.c);
18199 }
18200 p += it.len;
18201
18202 /* Get its face. */
18203 ilisp = make_number (p - arrow_string);
18204 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18205 it.face_id = compute_char_face (f, it.char_to_display, face);
18206
18207 /* Compute its width, get its glyphs. */
18208 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18209 SET_TEXT_POS (it.position, -1, -1);
18210 PRODUCE_GLYPHS (&it);
18211
18212 /* If this character doesn't fit any more in the line, we have
18213 to remove some glyphs. */
18214 if (it.current_x > it.last_visible_x)
18215 {
18216 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18217 break;
18218 }
18219 }
18220
18221 set_buffer_temp (old);
18222 return it.glyph_row;
18223 }
18224
18225
18226 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18227 glyphs to insert is determined by produce_special_glyphs. */
18228
18229 static void
18230 insert_left_trunc_glyphs (struct it *it)
18231 {
18232 struct it truncate_it;
18233 struct glyph *from, *end, *to, *toend;
18234
18235 eassert (!FRAME_WINDOW_P (it->f)
18236 || (!it->glyph_row->reversed_p
18237 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18238 || (it->glyph_row->reversed_p
18239 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18240
18241 /* Get the truncation glyphs. */
18242 truncate_it = *it;
18243 truncate_it.current_x = 0;
18244 truncate_it.face_id = DEFAULT_FACE_ID;
18245 truncate_it.glyph_row = &scratch_glyph_row;
18246 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18247 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18248 truncate_it.object = make_number (0);
18249 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18250
18251 /* Overwrite glyphs from IT with truncation glyphs. */
18252 if (!it->glyph_row->reversed_p)
18253 {
18254 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18255
18256 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18257 end = from + tused;
18258 to = it->glyph_row->glyphs[TEXT_AREA];
18259 toend = to + it->glyph_row->used[TEXT_AREA];
18260 if (FRAME_WINDOW_P (it->f))
18261 {
18262 /* On GUI frames, when variable-size fonts are displayed,
18263 the truncation glyphs may need more pixels than the row's
18264 glyphs they overwrite. We overwrite more glyphs to free
18265 enough screen real estate, and enlarge the stretch glyph
18266 on the right (see display_line), if there is one, to
18267 preserve the screen position of the truncation glyphs on
18268 the right. */
18269 int w = 0;
18270 struct glyph *g = to;
18271 short used;
18272
18273 /* The first glyph could be partially visible, in which case
18274 it->glyph_row->x will be negative. But we want the left
18275 truncation glyphs to be aligned at the left margin of the
18276 window, so we override the x coordinate at which the row
18277 will begin. */
18278 it->glyph_row->x = 0;
18279 while (g < toend && w < it->truncation_pixel_width)
18280 {
18281 w += g->pixel_width;
18282 ++g;
18283 }
18284 if (g - to - tused > 0)
18285 {
18286 memmove (to + tused, g, (toend - g) * sizeof(*g));
18287 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18288 }
18289 used = it->glyph_row->used[TEXT_AREA];
18290 if (it->glyph_row->truncated_on_right_p
18291 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18292 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18293 == STRETCH_GLYPH)
18294 {
18295 int extra = w - it->truncation_pixel_width;
18296
18297 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18298 }
18299 }
18300
18301 while (from < end)
18302 *to++ = *from++;
18303
18304 /* There may be padding glyphs left over. Overwrite them too. */
18305 if (!FRAME_WINDOW_P (it->f))
18306 {
18307 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18308 {
18309 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18310 while (from < end)
18311 *to++ = *from++;
18312 }
18313 }
18314
18315 if (to > toend)
18316 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18317 }
18318 else
18319 {
18320 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18321
18322 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18323 that back to front. */
18324 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18325 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18326 toend = it->glyph_row->glyphs[TEXT_AREA];
18327 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18328 if (FRAME_WINDOW_P (it->f))
18329 {
18330 int w = 0;
18331 struct glyph *g = to;
18332
18333 while (g >= toend && w < it->truncation_pixel_width)
18334 {
18335 w += g->pixel_width;
18336 --g;
18337 }
18338 if (to - g - tused > 0)
18339 to = g + tused;
18340 if (it->glyph_row->truncated_on_right_p
18341 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18342 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18343 {
18344 int extra = w - it->truncation_pixel_width;
18345
18346 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18347 }
18348 }
18349
18350 while (from >= end && to >= toend)
18351 *to-- = *from--;
18352 if (!FRAME_WINDOW_P (it->f))
18353 {
18354 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18355 {
18356 from =
18357 truncate_it.glyph_row->glyphs[TEXT_AREA]
18358 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18359 while (from >= end && to >= toend)
18360 *to-- = *from--;
18361 }
18362 }
18363 if (from >= end)
18364 {
18365 /* Need to free some room before prepending additional
18366 glyphs. */
18367 int move_by = from - end + 1;
18368 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18369 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18370
18371 for ( ; g >= g0; g--)
18372 g[move_by] = *g;
18373 while (from >= end)
18374 *to-- = *from--;
18375 it->glyph_row->used[TEXT_AREA] += move_by;
18376 }
18377 }
18378 }
18379
18380 /* Compute the hash code for ROW. */
18381 unsigned
18382 row_hash (struct glyph_row *row)
18383 {
18384 int area, k;
18385 unsigned hashval = 0;
18386
18387 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18388 for (k = 0; k < row->used[area]; ++k)
18389 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18390 + row->glyphs[area][k].u.val
18391 + row->glyphs[area][k].face_id
18392 + row->glyphs[area][k].padding_p
18393 + (row->glyphs[area][k].type << 2));
18394
18395 return hashval;
18396 }
18397
18398 /* Compute the pixel height and width of IT->glyph_row.
18399
18400 Most of the time, ascent and height of a display line will be equal
18401 to the max_ascent and max_height values of the display iterator
18402 structure. This is not the case if
18403
18404 1. We hit ZV without displaying anything. In this case, max_ascent
18405 and max_height will be zero.
18406
18407 2. We have some glyphs that don't contribute to the line height.
18408 (The glyph row flag contributes_to_line_height_p is for future
18409 pixmap extensions).
18410
18411 The first case is easily covered by using default values because in
18412 these cases, the line height does not really matter, except that it
18413 must not be zero. */
18414
18415 static void
18416 compute_line_metrics (struct it *it)
18417 {
18418 struct glyph_row *row = it->glyph_row;
18419
18420 if (FRAME_WINDOW_P (it->f))
18421 {
18422 int i, min_y, max_y;
18423
18424 /* The line may consist of one space only, that was added to
18425 place the cursor on it. If so, the row's height hasn't been
18426 computed yet. */
18427 if (row->height == 0)
18428 {
18429 if (it->max_ascent + it->max_descent == 0)
18430 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18431 row->ascent = it->max_ascent;
18432 row->height = it->max_ascent + it->max_descent;
18433 row->phys_ascent = it->max_phys_ascent;
18434 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18435 row->extra_line_spacing = it->max_extra_line_spacing;
18436 }
18437
18438 /* Compute the width of this line. */
18439 row->pixel_width = row->x;
18440 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18441 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18442
18443 eassert (row->pixel_width >= 0);
18444 eassert (row->ascent >= 0 && row->height > 0);
18445
18446 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18447 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18448
18449 /* If first line's physical ascent is larger than its logical
18450 ascent, use the physical ascent, and make the row taller.
18451 This makes accented characters fully visible. */
18452 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18453 && row->phys_ascent > row->ascent)
18454 {
18455 row->height += row->phys_ascent - row->ascent;
18456 row->ascent = row->phys_ascent;
18457 }
18458
18459 /* Compute how much of the line is visible. */
18460 row->visible_height = row->height;
18461
18462 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18463 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18464
18465 if (row->y < min_y)
18466 row->visible_height -= min_y - row->y;
18467 if (row->y + row->height > max_y)
18468 row->visible_height -= row->y + row->height - max_y;
18469 }
18470 else
18471 {
18472 row->pixel_width = row->used[TEXT_AREA];
18473 if (row->continued_p)
18474 row->pixel_width -= it->continuation_pixel_width;
18475 else if (row->truncated_on_right_p)
18476 row->pixel_width -= it->truncation_pixel_width;
18477 row->ascent = row->phys_ascent = 0;
18478 row->height = row->phys_height = row->visible_height = 1;
18479 row->extra_line_spacing = 0;
18480 }
18481
18482 /* Compute a hash code for this row. */
18483 row->hash = row_hash (row);
18484
18485 it->max_ascent = it->max_descent = 0;
18486 it->max_phys_ascent = it->max_phys_descent = 0;
18487 }
18488
18489
18490 /* Append one space to the glyph row of iterator IT if doing a
18491 window-based redisplay. The space has the same face as
18492 IT->face_id. Value is non-zero if a space was added.
18493
18494 This function is called to make sure that there is always one glyph
18495 at the end of a glyph row that the cursor can be set on under
18496 window-systems. (If there weren't such a glyph we would not know
18497 how wide and tall a box cursor should be displayed).
18498
18499 At the same time this space let's a nicely handle clearing to the
18500 end of the line if the row ends in italic text. */
18501
18502 static int
18503 append_space_for_newline (struct it *it, int default_face_p)
18504 {
18505 if (FRAME_WINDOW_P (it->f))
18506 {
18507 int n = it->glyph_row->used[TEXT_AREA];
18508
18509 if (it->glyph_row->glyphs[TEXT_AREA] + n
18510 < it->glyph_row->glyphs[1 + TEXT_AREA])
18511 {
18512 /* Save some values that must not be changed.
18513 Must save IT->c and IT->len because otherwise
18514 ITERATOR_AT_END_P wouldn't work anymore after
18515 append_space_for_newline has been called. */
18516 enum display_element_type saved_what = it->what;
18517 int saved_c = it->c, saved_len = it->len;
18518 int saved_char_to_display = it->char_to_display;
18519 int saved_x = it->current_x;
18520 int saved_face_id = it->face_id;
18521 struct text_pos saved_pos;
18522 Lisp_Object saved_object;
18523 struct face *face;
18524
18525 saved_object = it->object;
18526 saved_pos = it->position;
18527
18528 it->what = IT_CHARACTER;
18529 memset (&it->position, 0, sizeof it->position);
18530 it->object = make_number (0);
18531 it->c = it->char_to_display = ' ';
18532 it->len = 1;
18533
18534 /* If the default face was remapped, be sure to use the
18535 remapped face for the appended newline. */
18536 if (default_face_p)
18537 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18538 else if (it->face_before_selective_p)
18539 it->face_id = it->saved_face_id;
18540 face = FACE_FROM_ID (it->f, it->face_id);
18541 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18542
18543 PRODUCE_GLYPHS (it);
18544
18545 it->override_ascent = -1;
18546 it->constrain_row_ascent_descent_p = 0;
18547 it->current_x = saved_x;
18548 it->object = saved_object;
18549 it->position = saved_pos;
18550 it->what = saved_what;
18551 it->face_id = saved_face_id;
18552 it->len = saved_len;
18553 it->c = saved_c;
18554 it->char_to_display = saved_char_to_display;
18555 return 1;
18556 }
18557 }
18558
18559 return 0;
18560 }
18561
18562
18563 /* Extend the face of the last glyph in the text area of IT->glyph_row
18564 to the end of the display line. Called from display_line. If the
18565 glyph row is empty, add a space glyph to it so that we know the
18566 face to draw. Set the glyph row flag fill_line_p. If the glyph
18567 row is R2L, prepend a stretch glyph to cover the empty space to the
18568 left of the leftmost glyph. */
18569
18570 static void
18571 extend_face_to_end_of_line (struct it *it)
18572 {
18573 struct face *face, *default_face;
18574 struct frame *f = it->f;
18575
18576 /* If line is already filled, do nothing. Non window-system frames
18577 get a grace of one more ``pixel'' because their characters are
18578 1-``pixel'' wide, so they hit the equality too early. This grace
18579 is needed only for R2L rows that are not continued, to produce
18580 one extra blank where we could display the cursor. */
18581 if (it->current_x >= it->last_visible_x
18582 + (!FRAME_WINDOW_P (f)
18583 && it->glyph_row->reversed_p
18584 && !it->glyph_row->continued_p))
18585 return;
18586
18587 /* The default face, possibly remapped. */
18588 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18589
18590 /* Face extension extends the background and box of IT->face_id
18591 to the end of the line. If the background equals the background
18592 of the frame, we don't have to do anything. */
18593 if (it->face_before_selective_p)
18594 face = FACE_FROM_ID (f, it->saved_face_id);
18595 else
18596 face = FACE_FROM_ID (f, it->face_id);
18597
18598 if (FRAME_WINDOW_P (f)
18599 && it->glyph_row->displays_text_p
18600 && face->box == FACE_NO_BOX
18601 && face->background == FRAME_BACKGROUND_PIXEL (f)
18602 && !face->stipple
18603 && !it->glyph_row->reversed_p)
18604 return;
18605
18606 /* Set the glyph row flag indicating that the face of the last glyph
18607 in the text area has to be drawn to the end of the text area. */
18608 it->glyph_row->fill_line_p = 1;
18609
18610 /* If current character of IT is not ASCII, make sure we have the
18611 ASCII face. This will be automatically undone the next time
18612 get_next_display_element returns a multibyte character. Note
18613 that the character will always be single byte in unibyte
18614 text. */
18615 if (!ASCII_CHAR_P (it->c))
18616 {
18617 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18618 }
18619
18620 if (FRAME_WINDOW_P (f))
18621 {
18622 /* If the row is empty, add a space with the current face of IT,
18623 so that we know which face to draw. */
18624 if (it->glyph_row->used[TEXT_AREA] == 0)
18625 {
18626 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18627 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18628 it->glyph_row->used[TEXT_AREA] = 1;
18629 }
18630 #ifdef HAVE_WINDOW_SYSTEM
18631 if (it->glyph_row->reversed_p)
18632 {
18633 /* Prepend a stretch glyph to the row, such that the
18634 rightmost glyph will be drawn flushed all the way to the
18635 right margin of the window. The stretch glyph that will
18636 occupy the empty space, if any, to the left of the
18637 glyphs. */
18638 struct font *font = face->font ? face->font : FRAME_FONT (f);
18639 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18640 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18641 struct glyph *g;
18642 int row_width, stretch_ascent, stretch_width;
18643 struct text_pos saved_pos;
18644 int saved_face_id, saved_avoid_cursor;
18645
18646 for (row_width = 0, g = row_start; g < row_end; g++)
18647 row_width += g->pixel_width;
18648 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18649 if (stretch_width > 0)
18650 {
18651 stretch_ascent =
18652 (((it->ascent + it->descent)
18653 * FONT_BASE (font)) / FONT_HEIGHT (font));
18654 saved_pos = it->position;
18655 memset (&it->position, 0, sizeof it->position);
18656 saved_avoid_cursor = it->avoid_cursor_p;
18657 it->avoid_cursor_p = 1;
18658 saved_face_id = it->face_id;
18659 /* The last row's stretch glyph should get the default
18660 face, to avoid painting the rest of the window with
18661 the region face, if the region ends at ZV. */
18662 if (it->glyph_row->ends_at_zv_p)
18663 it->face_id = default_face->id;
18664 else
18665 it->face_id = face->id;
18666 append_stretch_glyph (it, make_number (0), stretch_width,
18667 it->ascent + it->descent, stretch_ascent);
18668 it->position = saved_pos;
18669 it->avoid_cursor_p = saved_avoid_cursor;
18670 it->face_id = saved_face_id;
18671 }
18672 }
18673 #endif /* HAVE_WINDOW_SYSTEM */
18674 }
18675 else
18676 {
18677 /* Save some values that must not be changed. */
18678 int saved_x = it->current_x;
18679 struct text_pos saved_pos;
18680 Lisp_Object saved_object;
18681 enum display_element_type saved_what = it->what;
18682 int saved_face_id = it->face_id;
18683
18684 saved_object = it->object;
18685 saved_pos = it->position;
18686
18687 it->what = IT_CHARACTER;
18688 memset (&it->position, 0, sizeof it->position);
18689 it->object = make_number (0);
18690 it->c = it->char_to_display = ' ';
18691 it->len = 1;
18692 /* The last row's blank glyphs should get the default face, to
18693 avoid painting the rest of the window with the region face,
18694 if the region ends at ZV. */
18695 if (it->glyph_row->ends_at_zv_p)
18696 it->face_id = default_face->id;
18697 else
18698 it->face_id = face->id;
18699
18700 PRODUCE_GLYPHS (it);
18701
18702 while (it->current_x <= it->last_visible_x)
18703 PRODUCE_GLYPHS (it);
18704
18705 /* Don't count these blanks really. It would let us insert a left
18706 truncation glyph below and make us set the cursor on them, maybe. */
18707 it->current_x = saved_x;
18708 it->object = saved_object;
18709 it->position = saved_pos;
18710 it->what = saved_what;
18711 it->face_id = saved_face_id;
18712 }
18713 }
18714
18715
18716 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18717 trailing whitespace. */
18718
18719 static int
18720 trailing_whitespace_p (ptrdiff_t charpos)
18721 {
18722 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18723 int c = 0;
18724
18725 while (bytepos < ZV_BYTE
18726 && (c = FETCH_CHAR (bytepos),
18727 c == ' ' || c == '\t'))
18728 ++bytepos;
18729
18730 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18731 {
18732 if (bytepos != PT_BYTE)
18733 return 1;
18734 }
18735 return 0;
18736 }
18737
18738
18739 /* Highlight trailing whitespace, if any, in ROW. */
18740
18741 static void
18742 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18743 {
18744 int used = row->used[TEXT_AREA];
18745
18746 if (used)
18747 {
18748 struct glyph *start = row->glyphs[TEXT_AREA];
18749 struct glyph *glyph = start + used - 1;
18750
18751 if (row->reversed_p)
18752 {
18753 /* Right-to-left rows need to be processed in the opposite
18754 direction, so swap the edge pointers. */
18755 glyph = start;
18756 start = row->glyphs[TEXT_AREA] + used - 1;
18757 }
18758
18759 /* Skip over glyphs inserted to display the cursor at the
18760 end of a line, for extending the face of the last glyph
18761 to the end of the line on terminals, and for truncation
18762 and continuation glyphs. */
18763 if (!row->reversed_p)
18764 {
18765 while (glyph >= start
18766 && glyph->type == CHAR_GLYPH
18767 && INTEGERP (glyph->object))
18768 --glyph;
18769 }
18770 else
18771 {
18772 while (glyph <= start
18773 && glyph->type == CHAR_GLYPH
18774 && INTEGERP (glyph->object))
18775 ++glyph;
18776 }
18777
18778 /* If last glyph is a space or stretch, and it's trailing
18779 whitespace, set the face of all trailing whitespace glyphs in
18780 IT->glyph_row to `trailing-whitespace'. */
18781 if ((row->reversed_p ? glyph <= start : glyph >= start)
18782 && BUFFERP (glyph->object)
18783 && (glyph->type == STRETCH_GLYPH
18784 || (glyph->type == CHAR_GLYPH
18785 && glyph->u.ch == ' '))
18786 && trailing_whitespace_p (glyph->charpos))
18787 {
18788 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18789 if (face_id < 0)
18790 return;
18791
18792 if (!row->reversed_p)
18793 {
18794 while (glyph >= start
18795 && BUFFERP (glyph->object)
18796 && (glyph->type == STRETCH_GLYPH
18797 || (glyph->type == CHAR_GLYPH
18798 && glyph->u.ch == ' ')))
18799 (glyph--)->face_id = face_id;
18800 }
18801 else
18802 {
18803 while (glyph <= start
18804 && BUFFERP (glyph->object)
18805 && (glyph->type == STRETCH_GLYPH
18806 || (glyph->type == CHAR_GLYPH
18807 && glyph->u.ch == ' ')))
18808 (glyph++)->face_id = face_id;
18809 }
18810 }
18811 }
18812 }
18813
18814
18815 /* Value is non-zero if glyph row ROW should be
18816 used to hold the cursor. */
18817
18818 static int
18819 cursor_row_p (struct glyph_row *row)
18820 {
18821 int result = 1;
18822
18823 if (PT == CHARPOS (row->end.pos)
18824 || PT == MATRIX_ROW_END_CHARPOS (row))
18825 {
18826 /* Suppose the row ends on a string.
18827 Unless the row is continued, that means it ends on a newline
18828 in the string. If it's anything other than a display string
18829 (e.g., a before-string from an overlay), we don't want the
18830 cursor there. (This heuristic seems to give the optimal
18831 behavior for the various types of multi-line strings.)
18832 One exception: if the string has `cursor' property on one of
18833 its characters, we _do_ want the cursor there. */
18834 if (CHARPOS (row->end.string_pos) >= 0)
18835 {
18836 if (row->continued_p)
18837 result = 1;
18838 else
18839 {
18840 /* Check for `display' property. */
18841 struct glyph *beg = row->glyphs[TEXT_AREA];
18842 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18843 struct glyph *glyph;
18844
18845 result = 0;
18846 for (glyph = end; glyph >= beg; --glyph)
18847 if (STRINGP (glyph->object))
18848 {
18849 Lisp_Object prop
18850 = Fget_char_property (make_number (PT),
18851 Qdisplay, Qnil);
18852 result =
18853 (!NILP (prop)
18854 && display_prop_string_p (prop, glyph->object));
18855 /* If there's a `cursor' property on one of the
18856 string's characters, this row is a cursor row,
18857 even though this is not a display string. */
18858 if (!result)
18859 {
18860 Lisp_Object s = glyph->object;
18861
18862 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18863 {
18864 ptrdiff_t gpos = glyph->charpos;
18865
18866 if (!NILP (Fget_char_property (make_number (gpos),
18867 Qcursor, s)))
18868 {
18869 result = 1;
18870 break;
18871 }
18872 }
18873 }
18874 break;
18875 }
18876 }
18877 }
18878 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18879 {
18880 /* If the row ends in middle of a real character,
18881 and the line is continued, we want the cursor here.
18882 That's because CHARPOS (ROW->end.pos) would equal
18883 PT if PT is before the character. */
18884 if (!row->ends_in_ellipsis_p)
18885 result = row->continued_p;
18886 else
18887 /* If the row ends in an ellipsis, then
18888 CHARPOS (ROW->end.pos) will equal point after the
18889 invisible text. We want that position to be displayed
18890 after the ellipsis. */
18891 result = 0;
18892 }
18893 /* If the row ends at ZV, display the cursor at the end of that
18894 row instead of at the start of the row below. */
18895 else if (row->ends_at_zv_p)
18896 result = 1;
18897 else
18898 result = 0;
18899 }
18900
18901 return result;
18902 }
18903
18904 \f
18905
18906 /* Push the property PROP so that it will be rendered at the current
18907 position in IT. Return 1 if PROP was successfully pushed, 0
18908 otherwise. Called from handle_line_prefix to handle the
18909 `line-prefix' and `wrap-prefix' properties. */
18910
18911 static int
18912 push_prefix_prop (struct it *it, Lisp_Object prop)
18913 {
18914 struct text_pos pos =
18915 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18916
18917 eassert (it->method == GET_FROM_BUFFER
18918 || it->method == GET_FROM_DISPLAY_VECTOR
18919 || it->method == GET_FROM_STRING);
18920
18921 /* We need to save the current buffer/string position, so it will be
18922 restored by pop_it, because iterate_out_of_display_property
18923 depends on that being set correctly, but some situations leave
18924 it->position not yet set when this function is called. */
18925 push_it (it, &pos);
18926
18927 if (STRINGP (prop))
18928 {
18929 if (SCHARS (prop) == 0)
18930 {
18931 pop_it (it);
18932 return 0;
18933 }
18934
18935 it->string = prop;
18936 it->string_from_prefix_prop_p = 1;
18937 it->multibyte_p = STRING_MULTIBYTE (it->string);
18938 it->current.overlay_string_index = -1;
18939 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18940 it->end_charpos = it->string_nchars = SCHARS (it->string);
18941 it->method = GET_FROM_STRING;
18942 it->stop_charpos = 0;
18943 it->prev_stop = 0;
18944 it->base_level_stop = 0;
18945
18946 /* Force paragraph direction to be that of the parent
18947 buffer/string. */
18948 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18949 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18950 else
18951 it->paragraph_embedding = L2R;
18952
18953 /* Set up the bidi iterator for this display string. */
18954 if (it->bidi_p)
18955 {
18956 it->bidi_it.string.lstring = it->string;
18957 it->bidi_it.string.s = NULL;
18958 it->bidi_it.string.schars = it->end_charpos;
18959 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18960 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18961 it->bidi_it.string.unibyte = !it->multibyte_p;
18962 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18963 }
18964 }
18965 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18966 {
18967 it->method = GET_FROM_STRETCH;
18968 it->object = prop;
18969 }
18970 #ifdef HAVE_WINDOW_SYSTEM
18971 else if (IMAGEP (prop))
18972 {
18973 it->what = IT_IMAGE;
18974 it->image_id = lookup_image (it->f, prop);
18975 it->method = GET_FROM_IMAGE;
18976 }
18977 #endif /* HAVE_WINDOW_SYSTEM */
18978 else
18979 {
18980 pop_it (it); /* bogus display property, give up */
18981 return 0;
18982 }
18983
18984 return 1;
18985 }
18986
18987 /* Return the character-property PROP at the current position in IT. */
18988
18989 static Lisp_Object
18990 get_it_property (struct it *it, Lisp_Object prop)
18991 {
18992 Lisp_Object position;
18993
18994 if (STRINGP (it->object))
18995 position = make_number (IT_STRING_CHARPOS (*it));
18996 else if (BUFFERP (it->object))
18997 position = make_number (IT_CHARPOS (*it));
18998 else
18999 return Qnil;
19000
19001 return Fget_char_property (position, prop, it->object);
19002 }
19003
19004 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19005
19006 static void
19007 handle_line_prefix (struct it *it)
19008 {
19009 Lisp_Object prefix;
19010
19011 if (it->continuation_lines_width > 0)
19012 {
19013 prefix = get_it_property (it, Qwrap_prefix);
19014 if (NILP (prefix))
19015 prefix = Vwrap_prefix;
19016 }
19017 else
19018 {
19019 prefix = get_it_property (it, Qline_prefix);
19020 if (NILP (prefix))
19021 prefix = Vline_prefix;
19022 }
19023 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19024 {
19025 /* If the prefix is wider than the window, and we try to wrap
19026 it, it would acquire its own wrap prefix, and so on till the
19027 iterator stack overflows. So, don't wrap the prefix. */
19028 it->line_wrap = TRUNCATE;
19029 it->avoid_cursor_p = 1;
19030 }
19031 }
19032
19033 \f
19034
19035 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19036 only for R2L lines from display_line and display_string, when they
19037 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19038 the line/string needs to be continued on the next glyph row. */
19039 static void
19040 unproduce_glyphs (struct it *it, int n)
19041 {
19042 struct glyph *glyph, *end;
19043
19044 eassert (it->glyph_row);
19045 eassert (it->glyph_row->reversed_p);
19046 eassert (it->area == TEXT_AREA);
19047 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19048
19049 if (n > it->glyph_row->used[TEXT_AREA])
19050 n = it->glyph_row->used[TEXT_AREA];
19051 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19052 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19053 for ( ; glyph < end; glyph++)
19054 glyph[-n] = *glyph;
19055 }
19056
19057 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19058 and ROW->maxpos. */
19059 static void
19060 find_row_edges (struct it *it, struct glyph_row *row,
19061 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19062 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19063 {
19064 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19065 lines' rows is implemented for bidi-reordered rows. */
19066
19067 /* ROW->minpos is the value of min_pos, the minimal buffer position
19068 we have in ROW, or ROW->start.pos if that is smaller. */
19069 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19070 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19071 else
19072 /* We didn't find buffer positions smaller than ROW->start, or
19073 didn't find _any_ valid buffer positions in any of the glyphs,
19074 so we must trust the iterator's computed positions. */
19075 row->minpos = row->start.pos;
19076 if (max_pos <= 0)
19077 {
19078 max_pos = CHARPOS (it->current.pos);
19079 max_bpos = BYTEPOS (it->current.pos);
19080 }
19081
19082 /* Here are the various use-cases for ending the row, and the
19083 corresponding values for ROW->maxpos:
19084
19085 Line ends in a newline from buffer eol_pos + 1
19086 Line is continued from buffer max_pos + 1
19087 Line is truncated on right it->current.pos
19088 Line ends in a newline from string max_pos + 1(*)
19089 (*) + 1 only when line ends in a forward scan
19090 Line is continued from string max_pos
19091 Line is continued from display vector max_pos
19092 Line is entirely from a string min_pos == max_pos
19093 Line is entirely from a display vector min_pos == max_pos
19094 Line that ends at ZV ZV
19095
19096 If you discover other use-cases, please add them here as
19097 appropriate. */
19098 if (row->ends_at_zv_p)
19099 row->maxpos = it->current.pos;
19100 else if (row->used[TEXT_AREA])
19101 {
19102 int seen_this_string = 0;
19103 struct glyph_row *r1 = row - 1;
19104
19105 /* Did we see the same display string on the previous row? */
19106 if (STRINGP (it->object)
19107 /* this is not the first row */
19108 && row > it->w->desired_matrix->rows
19109 /* previous row is not the header line */
19110 && !r1->mode_line_p
19111 /* previous row also ends in a newline from a string */
19112 && r1->ends_in_newline_from_string_p)
19113 {
19114 struct glyph *start, *end;
19115
19116 /* Search for the last glyph of the previous row that came
19117 from buffer or string. Depending on whether the row is
19118 L2R or R2L, we need to process it front to back or the
19119 other way round. */
19120 if (!r1->reversed_p)
19121 {
19122 start = r1->glyphs[TEXT_AREA];
19123 end = start + r1->used[TEXT_AREA];
19124 /* Glyphs inserted by redisplay have an integer (zero)
19125 as their object. */
19126 while (end > start
19127 && INTEGERP ((end - 1)->object)
19128 && (end - 1)->charpos <= 0)
19129 --end;
19130 if (end > start)
19131 {
19132 if (EQ ((end - 1)->object, it->object))
19133 seen_this_string = 1;
19134 }
19135 else
19136 /* If all the glyphs of the previous row were inserted
19137 by redisplay, it means the previous row was
19138 produced from a single newline, which is only
19139 possible if that newline came from the same string
19140 as the one which produced this ROW. */
19141 seen_this_string = 1;
19142 }
19143 else
19144 {
19145 end = r1->glyphs[TEXT_AREA] - 1;
19146 start = end + r1->used[TEXT_AREA];
19147 while (end < start
19148 && INTEGERP ((end + 1)->object)
19149 && (end + 1)->charpos <= 0)
19150 ++end;
19151 if (end < start)
19152 {
19153 if (EQ ((end + 1)->object, it->object))
19154 seen_this_string = 1;
19155 }
19156 else
19157 seen_this_string = 1;
19158 }
19159 }
19160 /* Take note of each display string that covers a newline only
19161 once, the first time we see it. This is for when a display
19162 string includes more than one newline in it. */
19163 if (row->ends_in_newline_from_string_p && !seen_this_string)
19164 {
19165 /* If we were scanning the buffer forward when we displayed
19166 the string, we want to account for at least one buffer
19167 position that belongs to this row (position covered by
19168 the display string), so that cursor positioning will
19169 consider this row as a candidate when point is at the end
19170 of the visual line represented by this row. This is not
19171 required when scanning back, because max_pos will already
19172 have a much larger value. */
19173 if (CHARPOS (row->end.pos) > max_pos)
19174 INC_BOTH (max_pos, max_bpos);
19175 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19176 }
19177 else if (CHARPOS (it->eol_pos) > 0)
19178 SET_TEXT_POS (row->maxpos,
19179 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19180 else if (row->continued_p)
19181 {
19182 /* If max_pos is different from IT's current position, it
19183 means IT->method does not belong to the display element
19184 at max_pos. However, it also means that the display
19185 element at max_pos was displayed in its entirety on this
19186 line, which is equivalent to saying that the next line
19187 starts at the next buffer position. */
19188 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19189 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19190 else
19191 {
19192 INC_BOTH (max_pos, max_bpos);
19193 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19194 }
19195 }
19196 else if (row->truncated_on_right_p)
19197 /* display_line already called reseat_at_next_visible_line_start,
19198 which puts the iterator at the beginning of the next line, in
19199 the logical order. */
19200 row->maxpos = it->current.pos;
19201 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19202 /* A line that is entirely from a string/image/stretch... */
19203 row->maxpos = row->minpos;
19204 else
19205 abort ();
19206 }
19207 else
19208 row->maxpos = it->current.pos;
19209 }
19210
19211 /* Construct the glyph row IT->glyph_row in the desired matrix of
19212 IT->w from text at the current position of IT. See dispextern.h
19213 for an overview of struct it. Value is non-zero if
19214 IT->glyph_row displays text, as opposed to a line displaying ZV
19215 only. */
19216
19217 static int
19218 display_line (struct it *it)
19219 {
19220 struct glyph_row *row = it->glyph_row;
19221 Lisp_Object overlay_arrow_string;
19222 struct it wrap_it;
19223 void *wrap_data = NULL;
19224 int may_wrap = 0, wrap_x IF_LINT (= 0);
19225 int wrap_row_used = -1;
19226 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19227 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19228 int wrap_row_extra_line_spacing IF_LINT (= 0);
19229 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19230 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19231 int cvpos;
19232 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19233 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19234
19235 /* We always start displaying at hpos zero even if hscrolled. */
19236 eassert (it->hpos == 0 && it->current_x == 0);
19237
19238 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19239 >= it->w->desired_matrix->nrows)
19240 {
19241 it->w->nrows_scale_factor++;
19242 fonts_changed_p = 1;
19243 return 0;
19244 }
19245
19246 /* Is IT->w showing the region? */
19247 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19248
19249 /* Clear the result glyph row and enable it. */
19250 prepare_desired_row (row);
19251
19252 row->y = it->current_y;
19253 row->start = it->start;
19254 row->continuation_lines_width = it->continuation_lines_width;
19255 row->displays_text_p = 1;
19256 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19257 it->starts_in_middle_of_char_p = 0;
19258
19259 /* Arrange the overlays nicely for our purposes. Usually, we call
19260 display_line on only one line at a time, in which case this
19261 can't really hurt too much, or we call it on lines which appear
19262 one after another in the buffer, in which case all calls to
19263 recenter_overlay_lists but the first will be pretty cheap. */
19264 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19265
19266 /* Move over display elements that are not visible because we are
19267 hscrolled. This may stop at an x-position < IT->first_visible_x
19268 if the first glyph is partially visible or if we hit a line end. */
19269 if (it->current_x < it->first_visible_x)
19270 {
19271 enum move_it_result move_result;
19272
19273 this_line_min_pos = row->start.pos;
19274 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19275 MOVE_TO_POS | MOVE_TO_X);
19276 /* If we are under a large hscroll, move_it_in_display_line_to
19277 could hit the end of the line without reaching
19278 it->first_visible_x. Pretend that we did reach it. This is
19279 especially important on a TTY, where we will call
19280 extend_face_to_end_of_line, which needs to know how many
19281 blank glyphs to produce. */
19282 if (it->current_x < it->first_visible_x
19283 && (move_result == MOVE_NEWLINE_OR_CR
19284 || move_result == MOVE_POS_MATCH_OR_ZV))
19285 it->current_x = it->first_visible_x;
19286
19287 /* Record the smallest positions seen while we moved over
19288 display elements that are not visible. This is needed by
19289 redisplay_internal for optimizing the case where the cursor
19290 stays inside the same line. The rest of this function only
19291 considers positions that are actually displayed, so
19292 RECORD_MAX_MIN_POS will not otherwise record positions that
19293 are hscrolled to the left of the left edge of the window. */
19294 min_pos = CHARPOS (this_line_min_pos);
19295 min_bpos = BYTEPOS (this_line_min_pos);
19296 }
19297 else
19298 {
19299 /* We only do this when not calling `move_it_in_display_line_to'
19300 above, because move_it_in_display_line_to calls
19301 handle_line_prefix itself. */
19302 handle_line_prefix (it);
19303 }
19304
19305 /* Get the initial row height. This is either the height of the
19306 text hscrolled, if there is any, or zero. */
19307 row->ascent = it->max_ascent;
19308 row->height = it->max_ascent + it->max_descent;
19309 row->phys_ascent = it->max_phys_ascent;
19310 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19311 row->extra_line_spacing = it->max_extra_line_spacing;
19312
19313 /* Utility macro to record max and min buffer positions seen until now. */
19314 #define RECORD_MAX_MIN_POS(IT) \
19315 do \
19316 { \
19317 int composition_p = !STRINGP ((IT)->string) \
19318 && ((IT)->what == IT_COMPOSITION); \
19319 ptrdiff_t current_pos = \
19320 composition_p ? (IT)->cmp_it.charpos \
19321 : IT_CHARPOS (*(IT)); \
19322 ptrdiff_t current_bpos = \
19323 composition_p ? CHAR_TO_BYTE (current_pos) \
19324 : IT_BYTEPOS (*(IT)); \
19325 if (current_pos < min_pos) \
19326 { \
19327 min_pos = current_pos; \
19328 min_bpos = current_bpos; \
19329 } \
19330 if (IT_CHARPOS (*it) > max_pos) \
19331 { \
19332 max_pos = IT_CHARPOS (*it); \
19333 max_bpos = IT_BYTEPOS (*it); \
19334 } \
19335 } \
19336 while (0)
19337
19338 /* Loop generating characters. The loop is left with IT on the next
19339 character to display. */
19340 while (1)
19341 {
19342 int n_glyphs_before, hpos_before, x_before;
19343 int x, nglyphs;
19344 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19345
19346 /* Retrieve the next thing to display. Value is zero if end of
19347 buffer reached. */
19348 if (!get_next_display_element (it))
19349 {
19350 /* Maybe add a space at the end of this line that is used to
19351 display the cursor there under X. Set the charpos of the
19352 first glyph of blank lines not corresponding to any text
19353 to -1. */
19354 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19355 row->exact_window_width_line_p = 1;
19356 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19357 || row->used[TEXT_AREA] == 0)
19358 {
19359 row->glyphs[TEXT_AREA]->charpos = -1;
19360 row->displays_text_p = 0;
19361
19362 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19363 && (!MINI_WINDOW_P (it->w)
19364 || (minibuf_level && EQ (it->window, minibuf_window))))
19365 row->indicate_empty_line_p = 1;
19366 }
19367
19368 it->continuation_lines_width = 0;
19369 row->ends_at_zv_p = 1;
19370 /* A row that displays right-to-left text must always have
19371 its last face extended all the way to the end of line,
19372 even if this row ends in ZV, because we still write to
19373 the screen left to right. We also need to extend the
19374 last face if the default face is remapped to some
19375 different face, otherwise the functions that clear
19376 portions of the screen will clear with the default face's
19377 background color. */
19378 if (row->reversed_p
19379 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19380 extend_face_to_end_of_line (it);
19381 break;
19382 }
19383
19384 /* Now, get the metrics of what we want to display. This also
19385 generates glyphs in `row' (which is IT->glyph_row). */
19386 n_glyphs_before = row->used[TEXT_AREA];
19387 x = it->current_x;
19388
19389 /* Remember the line height so far in case the next element doesn't
19390 fit on the line. */
19391 if (it->line_wrap != TRUNCATE)
19392 {
19393 ascent = it->max_ascent;
19394 descent = it->max_descent;
19395 phys_ascent = it->max_phys_ascent;
19396 phys_descent = it->max_phys_descent;
19397
19398 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19399 {
19400 if (IT_DISPLAYING_WHITESPACE (it))
19401 may_wrap = 1;
19402 else if (may_wrap)
19403 {
19404 SAVE_IT (wrap_it, *it, wrap_data);
19405 wrap_x = x;
19406 wrap_row_used = row->used[TEXT_AREA];
19407 wrap_row_ascent = row->ascent;
19408 wrap_row_height = row->height;
19409 wrap_row_phys_ascent = row->phys_ascent;
19410 wrap_row_phys_height = row->phys_height;
19411 wrap_row_extra_line_spacing = row->extra_line_spacing;
19412 wrap_row_min_pos = min_pos;
19413 wrap_row_min_bpos = min_bpos;
19414 wrap_row_max_pos = max_pos;
19415 wrap_row_max_bpos = max_bpos;
19416 may_wrap = 0;
19417 }
19418 }
19419 }
19420
19421 PRODUCE_GLYPHS (it);
19422
19423 /* If this display element was in marginal areas, continue with
19424 the next one. */
19425 if (it->area != TEXT_AREA)
19426 {
19427 row->ascent = max (row->ascent, it->max_ascent);
19428 row->height = max (row->height, it->max_ascent + it->max_descent);
19429 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19430 row->phys_height = max (row->phys_height,
19431 it->max_phys_ascent + it->max_phys_descent);
19432 row->extra_line_spacing = max (row->extra_line_spacing,
19433 it->max_extra_line_spacing);
19434 set_iterator_to_next (it, 1);
19435 continue;
19436 }
19437
19438 /* Does the display element fit on the line? If we truncate
19439 lines, we should draw past the right edge of the window. If
19440 we don't truncate, we want to stop so that we can display the
19441 continuation glyph before the right margin. If lines are
19442 continued, there are two possible strategies for characters
19443 resulting in more than 1 glyph (e.g. tabs): Display as many
19444 glyphs as possible in this line and leave the rest for the
19445 continuation line, or display the whole element in the next
19446 line. Original redisplay did the former, so we do it also. */
19447 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19448 hpos_before = it->hpos;
19449 x_before = x;
19450
19451 if (/* Not a newline. */
19452 nglyphs > 0
19453 /* Glyphs produced fit entirely in the line. */
19454 && it->current_x < it->last_visible_x)
19455 {
19456 it->hpos += nglyphs;
19457 row->ascent = max (row->ascent, it->max_ascent);
19458 row->height = max (row->height, it->max_ascent + it->max_descent);
19459 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19460 row->phys_height = max (row->phys_height,
19461 it->max_phys_ascent + it->max_phys_descent);
19462 row->extra_line_spacing = max (row->extra_line_spacing,
19463 it->max_extra_line_spacing);
19464 if (it->current_x - it->pixel_width < it->first_visible_x)
19465 row->x = x - it->first_visible_x;
19466 /* Record the maximum and minimum buffer positions seen so
19467 far in glyphs that will be displayed by this row. */
19468 if (it->bidi_p)
19469 RECORD_MAX_MIN_POS (it);
19470 }
19471 else
19472 {
19473 int i, new_x;
19474 struct glyph *glyph;
19475
19476 for (i = 0; i < nglyphs; ++i, x = new_x)
19477 {
19478 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19479 new_x = x + glyph->pixel_width;
19480
19481 if (/* Lines are continued. */
19482 it->line_wrap != TRUNCATE
19483 && (/* Glyph doesn't fit on the line. */
19484 new_x > it->last_visible_x
19485 /* Or it fits exactly on a window system frame. */
19486 || (new_x == it->last_visible_x
19487 && FRAME_WINDOW_P (it->f)
19488 && (row->reversed_p
19489 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19490 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19491 {
19492 /* End of a continued line. */
19493
19494 if (it->hpos == 0
19495 || (new_x == it->last_visible_x
19496 && FRAME_WINDOW_P (it->f)
19497 && (row->reversed_p
19498 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19499 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19500 {
19501 /* Current glyph is the only one on the line or
19502 fits exactly on the line. We must continue
19503 the line because we can't draw the cursor
19504 after the glyph. */
19505 row->continued_p = 1;
19506 it->current_x = new_x;
19507 it->continuation_lines_width += new_x;
19508 ++it->hpos;
19509 if (i == nglyphs - 1)
19510 {
19511 /* If line-wrap is on, check if a previous
19512 wrap point was found. */
19513 if (wrap_row_used > 0
19514 /* Even if there is a previous wrap
19515 point, continue the line here as
19516 usual, if (i) the previous character
19517 was a space or tab AND (ii) the
19518 current character is not. */
19519 && (!may_wrap
19520 || IT_DISPLAYING_WHITESPACE (it)))
19521 goto back_to_wrap;
19522
19523 /* Record the maximum and minimum buffer
19524 positions seen so far in glyphs that will be
19525 displayed by this row. */
19526 if (it->bidi_p)
19527 RECORD_MAX_MIN_POS (it);
19528 set_iterator_to_next (it, 1);
19529 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19530 {
19531 if (!get_next_display_element (it))
19532 {
19533 row->exact_window_width_line_p = 1;
19534 it->continuation_lines_width = 0;
19535 row->continued_p = 0;
19536 row->ends_at_zv_p = 1;
19537 }
19538 else if (ITERATOR_AT_END_OF_LINE_P (it))
19539 {
19540 row->continued_p = 0;
19541 row->exact_window_width_line_p = 1;
19542 }
19543 }
19544 }
19545 else if (it->bidi_p)
19546 RECORD_MAX_MIN_POS (it);
19547 }
19548 else if (CHAR_GLYPH_PADDING_P (*glyph)
19549 && !FRAME_WINDOW_P (it->f))
19550 {
19551 /* A padding glyph that doesn't fit on this line.
19552 This means the whole character doesn't fit
19553 on the line. */
19554 if (row->reversed_p)
19555 unproduce_glyphs (it, row->used[TEXT_AREA]
19556 - n_glyphs_before);
19557 row->used[TEXT_AREA] = n_glyphs_before;
19558
19559 /* Fill the rest of the row with continuation
19560 glyphs like in 20.x. */
19561 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19562 < row->glyphs[1 + TEXT_AREA])
19563 produce_special_glyphs (it, IT_CONTINUATION);
19564
19565 row->continued_p = 1;
19566 it->current_x = x_before;
19567 it->continuation_lines_width += x_before;
19568
19569 /* Restore the height to what it was before the
19570 element not fitting on the line. */
19571 it->max_ascent = ascent;
19572 it->max_descent = descent;
19573 it->max_phys_ascent = phys_ascent;
19574 it->max_phys_descent = phys_descent;
19575 }
19576 else if (wrap_row_used > 0)
19577 {
19578 back_to_wrap:
19579 if (row->reversed_p)
19580 unproduce_glyphs (it,
19581 row->used[TEXT_AREA] - wrap_row_used);
19582 RESTORE_IT (it, &wrap_it, wrap_data);
19583 it->continuation_lines_width += wrap_x;
19584 row->used[TEXT_AREA] = wrap_row_used;
19585 row->ascent = wrap_row_ascent;
19586 row->height = wrap_row_height;
19587 row->phys_ascent = wrap_row_phys_ascent;
19588 row->phys_height = wrap_row_phys_height;
19589 row->extra_line_spacing = wrap_row_extra_line_spacing;
19590 min_pos = wrap_row_min_pos;
19591 min_bpos = wrap_row_min_bpos;
19592 max_pos = wrap_row_max_pos;
19593 max_bpos = wrap_row_max_bpos;
19594 row->continued_p = 1;
19595 row->ends_at_zv_p = 0;
19596 row->exact_window_width_line_p = 0;
19597 it->continuation_lines_width += x;
19598
19599 /* Make sure that a non-default face is extended
19600 up to the right margin of the window. */
19601 extend_face_to_end_of_line (it);
19602 }
19603 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19604 {
19605 /* A TAB that extends past the right edge of the
19606 window. This produces a single glyph on
19607 window system frames. We leave the glyph in
19608 this row and let it fill the row, but don't
19609 consume the TAB. */
19610 if ((row->reversed_p
19611 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19612 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19613 produce_special_glyphs (it, IT_CONTINUATION);
19614 it->continuation_lines_width += it->last_visible_x;
19615 row->ends_in_middle_of_char_p = 1;
19616 row->continued_p = 1;
19617 glyph->pixel_width = it->last_visible_x - x;
19618 it->starts_in_middle_of_char_p = 1;
19619 }
19620 else
19621 {
19622 /* Something other than a TAB that draws past
19623 the right edge of the window. Restore
19624 positions to values before the element. */
19625 if (row->reversed_p)
19626 unproduce_glyphs (it, row->used[TEXT_AREA]
19627 - (n_glyphs_before + i));
19628 row->used[TEXT_AREA] = n_glyphs_before + i;
19629
19630 /* Display continuation glyphs. */
19631 it->current_x = x_before;
19632 it->continuation_lines_width += x;
19633 if (!FRAME_WINDOW_P (it->f)
19634 || (row->reversed_p
19635 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19636 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19637 produce_special_glyphs (it, IT_CONTINUATION);
19638 row->continued_p = 1;
19639
19640 extend_face_to_end_of_line (it);
19641
19642 if (nglyphs > 1 && i > 0)
19643 {
19644 row->ends_in_middle_of_char_p = 1;
19645 it->starts_in_middle_of_char_p = 1;
19646 }
19647
19648 /* Restore the height to what it was before the
19649 element not fitting on the line. */
19650 it->max_ascent = ascent;
19651 it->max_descent = descent;
19652 it->max_phys_ascent = phys_ascent;
19653 it->max_phys_descent = phys_descent;
19654 }
19655
19656 break;
19657 }
19658 else if (new_x > it->first_visible_x)
19659 {
19660 /* Increment number of glyphs actually displayed. */
19661 ++it->hpos;
19662
19663 /* Record the maximum and minimum buffer positions
19664 seen so far in glyphs that will be displayed by
19665 this row. */
19666 if (it->bidi_p)
19667 RECORD_MAX_MIN_POS (it);
19668
19669 if (x < it->first_visible_x)
19670 /* Glyph is partially visible, i.e. row starts at
19671 negative X position. */
19672 row->x = x - it->first_visible_x;
19673 }
19674 else
19675 {
19676 /* Glyph is completely off the left margin of the
19677 window. This should not happen because of the
19678 move_it_in_display_line at the start of this
19679 function, unless the text display area of the
19680 window is empty. */
19681 eassert (it->first_visible_x <= it->last_visible_x);
19682 }
19683 }
19684 /* Even if this display element produced no glyphs at all,
19685 we want to record its position. */
19686 if (it->bidi_p && nglyphs == 0)
19687 RECORD_MAX_MIN_POS (it);
19688
19689 row->ascent = max (row->ascent, it->max_ascent);
19690 row->height = max (row->height, it->max_ascent + it->max_descent);
19691 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19692 row->phys_height = max (row->phys_height,
19693 it->max_phys_ascent + it->max_phys_descent);
19694 row->extra_line_spacing = max (row->extra_line_spacing,
19695 it->max_extra_line_spacing);
19696
19697 /* End of this display line if row is continued. */
19698 if (row->continued_p || row->ends_at_zv_p)
19699 break;
19700 }
19701
19702 at_end_of_line:
19703 /* Is this a line end? If yes, we're also done, after making
19704 sure that a non-default face is extended up to the right
19705 margin of the window. */
19706 if (ITERATOR_AT_END_OF_LINE_P (it))
19707 {
19708 int used_before = row->used[TEXT_AREA];
19709
19710 row->ends_in_newline_from_string_p = STRINGP (it->object);
19711
19712 /* Add a space at the end of the line that is used to
19713 display the cursor there. */
19714 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19715 append_space_for_newline (it, 0);
19716
19717 /* Extend the face to the end of the line. */
19718 extend_face_to_end_of_line (it);
19719
19720 /* Make sure we have the position. */
19721 if (used_before == 0)
19722 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19723
19724 /* Record the position of the newline, for use in
19725 find_row_edges. */
19726 it->eol_pos = it->current.pos;
19727
19728 /* Consume the line end. This skips over invisible lines. */
19729 set_iterator_to_next (it, 1);
19730 it->continuation_lines_width = 0;
19731 break;
19732 }
19733
19734 /* Proceed with next display element. Note that this skips
19735 over lines invisible because of selective display. */
19736 set_iterator_to_next (it, 1);
19737
19738 /* If we truncate lines, we are done when the last displayed
19739 glyphs reach past the right margin of the window. */
19740 if (it->line_wrap == TRUNCATE
19741 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19742 ? (it->current_x >= it->last_visible_x)
19743 : (it->current_x > it->last_visible_x)))
19744 {
19745 /* Maybe add truncation glyphs. */
19746 if (!FRAME_WINDOW_P (it->f)
19747 || (row->reversed_p
19748 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19749 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19750 {
19751 int i, n;
19752
19753 if (!row->reversed_p)
19754 {
19755 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19756 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19757 break;
19758 }
19759 else
19760 {
19761 for (i = 0; i < row->used[TEXT_AREA]; i++)
19762 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19763 break;
19764 /* Remove any padding glyphs at the front of ROW, to
19765 make room for the truncation glyphs we will be
19766 adding below. The loop below always inserts at
19767 least one truncation glyph, so also remove the
19768 last glyph added to ROW. */
19769 unproduce_glyphs (it, i + 1);
19770 /* Adjust i for the loop below. */
19771 i = row->used[TEXT_AREA] - (i + 1);
19772 }
19773
19774 it->current_x = x_before;
19775 if (!FRAME_WINDOW_P (it->f))
19776 {
19777 for (n = row->used[TEXT_AREA]; i < n; ++i)
19778 {
19779 row->used[TEXT_AREA] = i;
19780 produce_special_glyphs (it, IT_TRUNCATION);
19781 }
19782 }
19783 else
19784 {
19785 row->used[TEXT_AREA] = i;
19786 produce_special_glyphs (it, IT_TRUNCATION);
19787 }
19788 }
19789 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19790 {
19791 /* Don't truncate if we can overflow newline into fringe. */
19792 if (!get_next_display_element (it))
19793 {
19794 it->continuation_lines_width = 0;
19795 row->ends_at_zv_p = 1;
19796 row->exact_window_width_line_p = 1;
19797 break;
19798 }
19799 if (ITERATOR_AT_END_OF_LINE_P (it))
19800 {
19801 row->exact_window_width_line_p = 1;
19802 goto at_end_of_line;
19803 }
19804 it->current_x = x_before;
19805 }
19806
19807 row->truncated_on_right_p = 1;
19808 it->continuation_lines_width = 0;
19809 reseat_at_next_visible_line_start (it, 0);
19810 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19811 it->hpos = hpos_before;
19812 break;
19813 }
19814 }
19815
19816 if (wrap_data)
19817 bidi_unshelve_cache (wrap_data, 1);
19818
19819 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19820 at the left window margin. */
19821 if (it->first_visible_x
19822 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19823 {
19824 if (!FRAME_WINDOW_P (it->f)
19825 || (row->reversed_p
19826 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19827 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19828 insert_left_trunc_glyphs (it);
19829 row->truncated_on_left_p = 1;
19830 }
19831
19832 /* Remember the position at which this line ends.
19833
19834 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19835 cannot be before the call to find_row_edges below, since that is
19836 where these positions are determined. */
19837 row->end = it->current;
19838 if (!it->bidi_p)
19839 {
19840 row->minpos = row->start.pos;
19841 row->maxpos = row->end.pos;
19842 }
19843 else
19844 {
19845 /* ROW->minpos and ROW->maxpos must be the smallest and
19846 `1 + the largest' buffer positions in ROW. But if ROW was
19847 bidi-reordered, these two positions can be anywhere in the
19848 row, so we must determine them now. */
19849 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19850 }
19851
19852 /* If the start of this line is the overlay arrow-position, then
19853 mark this glyph row as the one containing the overlay arrow.
19854 This is clearly a mess with variable size fonts. It would be
19855 better to let it be displayed like cursors under X. */
19856 if ((row->displays_text_p || !overlay_arrow_seen)
19857 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19858 !NILP (overlay_arrow_string)))
19859 {
19860 /* Overlay arrow in window redisplay is a fringe bitmap. */
19861 if (STRINGP (overlay_arrow_string))
19862 {
19863 struct glyph_row *arrow_row
19864 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19865 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19866 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19867 struct glyph *p = row->glyphs[TEXT_AREA];
19868 struct glyph *p2, *end;
19869
19870 /* Copy the arrow glyphs. */
19871 while (glyph < arrow_end)
19872 *p++ = *glyph++;
19873
19874 /* Throw away padding glyphs. */
19875 p2 = p;
19876 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19877 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19878 ++p2;
19879 if (p2 > p)
19880 {
19881 while (p2 < end)
19882 *p++ = *p2++;
19883 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19884 }
19885 }
19886 else
19887 {
19888 eassert (INTEGERP (overlay_arrow_string));
19889 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19890 }
19891 overlay_arrow_seen = 1;
19892 }
19893
19894 /* Highlight trailing whitespace. */
19895 if (!NILP (Vshow_trailing_whitespace))
19896 highlight_trailing_whitespace (it->f, it->glyph_row);
19897
19898 /* Compute pixel dimensions of this line. */
19899 compute_line_metrics (it);
19900
19901 /* Implementation note: No changes in the glyphs of ROW or in their
19902 faces can be done past this point, because compute_line_metrics
19903 computes ROW's hash value and stores it within the glyph_row
19904 structure. */
19905
19906 /* Record whether this row ends inside an ellipsis. */
19907 row->ends_in_ellipsis_p
19908 = (it->method == GET_FROM_DISPLAY_VECTOR
19909 && it->ellipsis_p);
19910
19911 /* Save fringe bitmaps in this row. */
19912 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19913 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19914 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19915 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19916
19917 it->left_user_fringe_bitmap = 0;
19918 it->left_user_fringe_face_id = 0;
19919 it->right_user_fringe_bitmap = 0;
19920 it->right_user_fringe_face_id = 0;
19921
19922 /* Maybe set the cursor. */
19923 cvpos = it->w->cursor.vpos;
19924 if ((cvpos < 0
19925 /* In bidi-reordered rows, keep checking for proper cursor
19926 position even if one has been found already, because buffer
19927 positions in such rows change non-linearly with ROW->VPOS,
19928 when a line is continued. One exception: when we are at ZV,
19929 display cursor on the first suitable glyph row, since all
19930 the empty rows after that also have their position set to ZV. */
19931 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19932 lines' rows is implemented for bidi-reordered rows. */
19933 || (it->bidi_p
19934 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19935 && PT >= MATRIX_ROW_START_CHARPOS (row)
19936 && PT <= MATRIX_ROW_END_CHARPOS (row)
19937 && cursor_row_p (row))
19938 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19939
19940 /* Prepare for the next line. This line starts horizontally at (X
19941 HPOS) = (0 0). Vertical positions are incremented. As a
19942 convenience for the caller, IT->glyph_row is set to the next
19943 row to be used. */
19944 it->current_x = it->hpos = 0;
19945 it->current_y += row->height;
19946 SET_TEXT_POS (it->eol_pos, 0, 0);
19947 ++it->vpos;
19948 ++it->glyph_row;
19949 /* The next row should by default use the same value of the
19950 reversed_p flag as this one. set_iterator_to_next decides when
19951 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19952 the flag accordingly. */
19953 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19954 it->glyph_row->reversed_p = row->reversed_p;
19955 it->start = row->end;
19956 return row->displays_text_p;
19957
19958 #undef RECORD_MAX_MIN_POS
19959 }
19960
19961 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19962 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19963 doc: /* Return paragraph direction at point in BUFFER.
19964 Value is either `left-to-right' or `right-to-left'.
19965 If BUFFER is omitted or nil, it defaults to the current buffer.
19966
19967 Paragraph direction determines how the text in the paragraph is displayed.
19968 In left-to-right paragraphs, text begins at the left margin of the window
19969 and the reading direction is generally left to right. In right-to-left
19970 paragraphs, text begins at the right margin and is read from right to left.
19971
19972 See also `bidi-paragraph-direction'. */)
19973 (Lisp_Object buffer)
19974 {
19975 struct buffer *buf = current_buffer;
19976 struct buffer *old = buf;
19977
19978 if (! NILP (buffer))
19979 {
19980 CHECK_BUFFER (buffer);
19981 buf = XBUFFER (buffer);
19982 }
19983
19984 if (NILP (BVAR (buf, bidi_display_reordering))
19985 || NILP (BVAR (buf, enable_multibyte_characters))
19986 /* When we are loading loadup.el, the character property tables
19987 needed for bidi iteration are not yet available. */
19988 || !NILP (Vpurify_flag))
19989 return Qleft_to_right;
19990 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19991 return BVAR (buf, bidi_paragraph_direction);
19992 else
19993 {
19994 /* Determine the direction from buffer text. We could try to
19995 use current_matrix if it is up to date, but this seems fast
19996 enough as it is. */
19997 struct bidi_it itb;
19998 ptrdiff_t pos = BUF_PT (buf);
19999 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20000 int c;
20001 void *itb_data = bidi_shelve_cache ();
20002
20003 set_buffer_temp (buf);
20004 /* bidi_paragraph_init finds the base direction of the paragraph
20005 by searching forward from paragraph start. We need the base
20006 direction of the current or _previous_ paragraph, so we need
20007 to make sure we are within that paragraph. To that end, find
20008 the previous non-empty line. */
20009 if (pos >= ZV && pos > BEGV)
20010 {
20011 pos--;
20012 bytepos = CHAR_TO_BYTE (pos);
20013 }
20014 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20015 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20016 {
20017 while ((c = FETCH_BYTE (bytepos)) == '\n'
20018 || c == ' ' || c == '\t' || c == '\f')
20019 {
20020 if (bytepos <= BEGV_BYTE)
20021 break;
20022 bytepos--;
20023 pos--;
20024 }
20025 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20026 bytepos--;
20027 }
20028 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20029 itb.paragraph_dir = NEUTRAL_DIR;
20030 itb.string.s = NULL;
20031 itb.string.lstring = Qnil;
20032 itb.string.bufpos = 0;
20033 itb.string.unibyte = 0;
20034 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20035 bidi_unshelve_cache (itb_data, 0);
20036 set_buffer_temp (old);
20037 switch (itb.paragraph_dir)
20038 {
20039 case L2R:
20040 return Qleft_to_right;
20041 break;
20042 case R2L:
20043 return Qright_to_left;
20044 break;
20045 default:
20046 abort ();
20047 }
20048 }
20049 }
20050
20051
20052 \f
20053 /***********************************************************************
20054 Menu Bar
20055 ***********************************************************************/
20056
20057 /* Redisplay the menu bar in the frame for window W.
20058
20059 The menu bar of X frames that don't have X toolkit support is
20060 displayed in a special window W->frame->menu_bar_window.
20061
20062 The menu bar of terminal frames is treated specially as far as
20063 glyph matrices are concerned. Menu bar lines are not part of
20064 windows, so the update is done directly on the frame matrix rows
20065 for the menu bar. */
20066
20067 static void
20068 display_menu_bar (struct window *w)
20069 {
20070 struct frame *f = XFRAME (WINDOW_FRAME (w));
20071 struct it it;
20072 Lisp_Object items;
20073 int i;
20074
20075 /* Don't do all this for graphical frames. */
20076 #ifdef HAVE_NTGUI
20077 if (FRAME_W32_P (f))
20078 return;
20079 #endif
20080 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20081 if (FRAME_X_P (f))
20082 return;
20083 #endif
20084
20085 #ifdef HAVE_NS
20086 if (FRAME_NS_P (f))
20087 return;
20088 #endif /* HAVE_NS */
20089
20090 #ifdef USE_X_TOOLKIT
20091 eassert (!FRAME_WINDOW_P (f));
20092 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20093 it.first_visible_x = 0;
20094 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20095 #else /* not USE_X_TOOLKIT */
20096 if (FRAME_WINDOW_P (f))
20097 {
20098 /* Menu bar lines are displayed in the desired matrix of the
20099 dummy window menu_bar_window. */
20100 struct window *menu_w;
20101 eassert (WINDOWP (f->menu_bar_window));
20102 menu_w = XWINDOW (f->menu_bar_window);
20103 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20104 MENU_FACE_ID);
20105 it.first_visible_x = 0;
20106 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20107 }
20108 else
20109 {
20110 /* This is a TTY frame, i.e. character hpos/vpos are used as
20111 pixel x/y. */
20112 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20113 MENU_FACE_ID);
20114 it.first_visible_x = 0;
20115 it.last_visible_x = FRAME_COLS (f);
20116 }
20117 #endif /* not USE_X_TOOLKIT */
20118
20119 /* FIXME: This should be controlled by a user option. See the
20120 comments in redisplay_tool_bar and display_mode_line about
20121 this. */
20122 it.paragraph_embedding = L2R;
20123
20124 if (! mode_line_inverse_video)
20125 /* Force the menu-bar to be displayed in the default face. */
20126 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20127
20128 /* Clear all rows of the menu bar. */
20129 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20130 {
20131 struct glyph_row *row = it.glyph_row + i;
20132 clear_glyph_row (row);
20133 row->enabled_p = 1;
20134 row->full_width_p = 1;
20135 }
20136
20137 /* Display all items of the menu bar. */
20138 items = FRAME_MENU_BAR_ITEMS (it.f);
20139 for (i = 0; i < ASIZE (items); i += 4)
20140 {
20141 Lisp_Object string;
20142
20143 /* Stop at nil string. */
20144 string = AREF (items, i + 1);
20145 if (NILP (string))
20146 break;
20147
20148 /* Remember where item was displayed. */
20149 ASET (items, i + 3, make_number (it.hpos));
20150
20151 /* Display the item, pad with one space. */
20152 if (it.current_x < it.last_visible_x)
20153 display_string (NULL, string, Qnil, 0, 0, &it,
20154 SCHARS (string) + 1, 0, 0, -1);
20155 }
20156
20157 /* Fill out the line with spaces. */
20158 if (it.current_x < it.last_visible_x)
20159 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20160
20161 /* Compute the total height of the lines. */
20162 compute_line_metrics (&it);
20163 }
20164
20165
20166 \f
20167 /***********************************************************************
20168 Mode Line
20169 ***********************************************************************/
20170
20171 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20172 FORCE is non-zero, redisplay mode lines unconditionally.
20173 Otherwise, redisplay only mode lines that are garbaged. Value is
20174 the number of windows whose mode lines were redisplayed. */
20175
20176 static int
20177 redisplay_mode_lines (Lisp_Object window, int force)
20178 {
20179 int nwindows = 0;
20180
20181 while (!NILP (window))
20182 {
20183 struct window *w = XWINDOW (window);
20184
20185 if (WINDOWP (w->hchild))
20186 nwindows += redisplay_mode_lines (w->hchild, force);
20187 else if (WINDOWP (w->vchild))
20188 nwindows += redisplay_mode_lines (w->vchild, force);
20189 else if (force
20190 || FRAME_GARBAGED_P (XFRAME (w->frame))
20191 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20192 {
20193 struct text_pos lpoint;
20194 struct buffer *old = current_buffer;
20195
20196 /* Set the window's buffer for the mode line display. */
20197 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20198 set_buffer_internal_1 (XBUFFER (w->buffer));
20199
20200 /* Point refers normally to the selected window. For any
20201 other window, set up appropriate value. */
20202 if (!EQ (window, selected_window))
20203 {
20204 struct text_pos pt;
20205
20206 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20207 if (CHARPOS (pt) < BEGV)
20208 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20209 else if (CHARPOS (pt) > (ZV - 1))
20210 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20211 else
20212 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20213 }
20214
20215 /* Display mode lines. */
20216 clear_glyph_matrix (w->desired_matrix);
20217 if (display_mode_lines (w))
20218 {
20219 ++nwindows;
20220 w->must_be_updated_p = 1;
20221 }
20222
20223 /* Restore old settings. */
20224 set_buffer_internal_1 (old);
20225 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20226 }
20227
20228 window = w->next;
20229 }
20230
20231 return nwindows;
20232 }
20233
20234
20235 /* Display the mode and/or header line of window W. Value is the
20236 sum number of mode lines and header lines displayed. */
20237
20238 static int
20239 display_mode_lines (struct window *w)
20240 {
20241 Lisp_Object old_selected_window, old_selected_frame;
20242 int n = 0;
20243
20244 old_selected_frame = selected_frame;
20245 selected_frame = w->frame;
20246 old_selected_window = selected_window;
20247 XSETWINDOW (selected_window, w);
20248
20249 /* These will be set while the mode line specs are processed. */
20250 line_number_displayed = 0;
20251 w->column_number_displayed = Qnil;
20252
20253 if (WINDOW_WANTS_MODELINE_P (w))
20254 {
20255 struct window *sel_w = XWINDOW (old_selected_window);
20256
20257 /* Select mode line face based on the real selected window. */
20258 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20259 BVAR (current_buffer, mode_line_format));
20260 ++n;
20261 }
20262
20263 if (WINDOW_WANTS_HEADER_LINE_P (w))
20264 {
20265 display_mode_line (w, HEADER_LINE_FACE_ID,
20266 BVAR (current_buffer, header_line_format));
20267 ++n;
20268 }
20269
20270 selected_frame = old_selected_frame;
20271 selected_window = old_selected_window;
20272 return n;
20273 }
20274
20275
20276 /* Display mode or header line of window W. FACE_ID specifies which
20277 line to display; it is either MODE_LINE_FACE_ID or
20278 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20279 display. Value is the pixel height of the mode/header line
20280 displayed. */
20281
20282 static int
20283 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20284 {
20285 struct it it;
20286 struct face *face;
20287 ptrdiff_t count = SPECPDL_INDEX ();
20288
20289 init_iterator (&it, w, -1, -1, NULL, face_id);
20290 /* Don't extend on a previously drawn mode-line.
20291 This may happen if called from pos_visible_p. */
20292 it.glyph_row->enabled_p = 0;
20293 prepare_desired_row (it.glyph_row);
20294
20295 it.glyph_row->mode_line_p = 1;
20296
20297 if (! mode_line_inverse_video)
20298 /* Force the mode-line to be displayed in the default face. */
20299 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20300
20301 /* FIXME: This should be controlled by a user option. But
20302 supporting such an option is not trivial, since the mode line is
20303 made up of many separate strings. */
20304 it.paragraph_embedding = L2R;
20305
20306 record_unwind_protect (unwind_format_mode_line,
20307 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20308
20309 mode_line_target = MODE_LINE_DISPLAY;
20310
20311 /* Temporarily make frame's keyboard the current kboard so that
20312 kboard-local variables in the mode_line_format will get the right
20313 values. */
20314 push_kboard (FRAME_KBOARD (it.f));
20315 record_unwind_save_match_data ();
20316 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20317 pop_kboard ();
20318
20319 unbind_to (count, Qnil);
20320
20321 /* Fill up with spaces. */
20322 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20323
20324 compute_line_metrics (&it);
20325 it.glyph_row->full_width_p = 1;
20326 it.glyph_row->continued_p = 0;
20327 it.glyph_row->truncated_on_left_p = 0;
20328 it.glyph_row->truncated_on_right_p = 0;
20329
20330 /* Make a 3D mode-line have a shadow at its right end. */
20331 face = FACE_FROM_ID (it.f, face_id);
20332 extend_face_to_end_of_line (&it);
20333 if (face->box != FACE_NO_BOX)
20334 {
20335 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20336 + it.glyph_row->used[TEXT_AREA] - 1);
20337 last->right_box_line_p = 1;
20338 }
20339
20340 return it.glyph_row->height;
20341 }
20342
20343 /* Move element ELT in LIST to the front of LIST.
20344 Return the updated list. */
20345
20346 static Lisp_Object
20347 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20348 {
20349 register Lisp_Object tail, prev;
20350 register Lisp_Object tem;
20351
20352 tail = list;
20353 prev = Qnil;
20354 while (CONSP (tail))
20355 {
20356 tem = XCAR (tail);
20357
20358 if (EQ (elt, tem))
20359 {
20360 /* Splice out the link TAIL. */
20361 if (NILP (prev))
20362 list = XCDR (tail);
20363 else
20364 Fsetcdr (prev, XCDR (tail));
20365
20366 /* Now make it the first. */
20367 Fsetcdr (tail, list);
20368 return tail;
20369 }
20370 else
20371 prev = tail;
20372 tail = XCDR (tail);
20373 QUIT;
20374 }
20375
20376 /* Not found--return unchanged LIST. */
20377 return list;
20378 }
20379
20380 /* Contribute ELT to the mode line for window IT->w. How it
20381 translates into text depends on its data type.
20382
20383 IT describes the display environment in which we display, as usual.
20384
20385 DEPTH is the depth in recursion. It is used to prevent
20386 infinite recursion here.
20387
20388 FIELD_WIDTH is the number of characters the display of ELT should
20389 occupy in the mode line, and PRECISION is the maximum number of
20390 characters to display from ELT's representation. See
20391 display_string for details.
20392
20393 Returns the hpos of the end of the text generated by ELT.
20394
20395 PROPS is a property list to add to any string we encounter.
20396
20397 If RISKY is nonzero, remove (disregard) any properties in any string
20398 we encounter, and ignore :eval and :propertize.
20399
20400 The global variable `mode_line_target' determines whether the
20401 output is passed to `store_mode_line_noprop',
20402 `store_mode_line_string', or `display_string'. */
20403
20404 static int
20405 display_mode_element (struct it *it, int depth, int field_width, int precision,
20406 Lisp_Object elt, Lisp_Object props, int risky)
20407 {
20408 int n = 0, field, prec;
20409 int literal = 0;
20410
20411 tail_recurse:
20412 if (depth > 100)
20413 elt = build_string ("*too-deep*");
20414
20415 depth++;
20416
20417 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20418 {
20419 case Lisp_String:
20420 {
20421 /* A string: output it and check for %-constructs within it. */
20422 unsigned char c;
20423 ptrdiff_t offset = 0;
20424
20425 if (SCHARS (elt) > 0
20426 && (!NILP (props) || risky))
20427 {
20428 Lisp_Object oprops, aelt;
20429 oprops = Ftext_properties_at (make_number (0), elt);
20430
20431 /* If the starting string's properties are not what
20432 we want, translate the string. Also, if the string
20433 is risky, do that anyway. */
20434
20435 if (NILP (Fequal (props, oprops)) || risky)
20436 {
20437 /* If the starting string has properties,
20438 merge the specified ones onto the existing ones. */
20439 if (! NILP (oprops) && !risky)
20440 {
20441 Lisp_Object tem;
20442
20443 oprops = Fcopy_sequence (oprops);
20444 tem = props;
20445 while (CONSP (tem))
20446 {
20447 oprops = Fplist_put (oprops, XCAR (tem),
20448 XCAR (XCDR (tem)));
20449 tem = XCDR (XCDR (tem));
20450 }
20451 props = oprops;
20452 }
20453
20454 aelt = Fassoc (elt, mode_line_proptrans_alist);
20455 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20456 {
20457 /* AELT is what we want. Move it to the front
20458 without consing. */
20459 elt = XCAR (aelt);
20460 mode_line_proptrans_alist
20461 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20462 }
20463 else
20464 {
20465 Lisp_Object tem;
20466
20467 /* If AELT has the wrong props, it is useless.
20468 so get rid of it. */
20469 if (! NILP (aelt))
20470 mode_line_proptrans_alist
20471 = Fdelq (aelt, mode_line_proptrans_alist);
20472
20473 elt = Fcopy_sequence (elt);
20474 Fset_text_properties (make_number (0), Flength (elt),
20475 props, elt);
20476 /* Add this item to mode_line_proptrans_alist. */
20477 mode_line_proptrans_alist
20478 = Fcons (Fcons (elt, props),
20479 mode_line_proptrans_alist);
20480 /* Truncate mode_line_proptrans_alist
20481 to at most 50 elements. */
20482 tem = Fnthcdr (make_number (50),
20483 mode_line_proptrans_alist);
20484 if (! NILP (tem))
20485 XSETCDR (tem, Qnil);
20486 }
20487 }
20488 }
20489
20490 offset = 0;
20491
20492 if (literal)
20493 {
20494 prec = precision - n;
20495 switch (mode_line_target)
20496 {
20497 case MODE_LINE_NOPROP:
20498 case MODE_LINE_TITLE:
20499 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20500 break;
20501 case MODE_LINE_STRING:
20502 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20503 break;
20504 case MODE_LINE_DISPLAY:
20505 n += display_string (NULL, elt, Qnil, 0, 0, it,
20506 0, prec, 0, STRING_MULTIBYTE (elt));
20507 break;
20508 }
20509
20510 break;
20511 }
20512
20513 /* Handle the non-literal case. */
20514
20515 while ((precision <= 0 || n < precision)
20516 && SREF (elt, offset) != 0
20517 && (mode_line_target != MODE_LINE_DISPLAY
20518 || it->current_x < it->last_visible_x))
20519 {
20520 ptrdiff_t last_offset = offset;
20521
20522 /* Advance to end of string or next format specifier. */
20523 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20524 ;
20525
20526 if (offset - 1 != last_offset)
20527 {
20528 ptrdiff_t nchars, nbytes;
20529
20530 /* Output to end of string or up to '%'. Field width
20531 is length of string. Don't output more than
20532 PRECISION allows us. */
20533 offset--;
20534
20535 prec = c_string_width (SDATA (elt) + last_offset,
20536 offset - last_offset, precision - n,
20537 &nchars, &nbytes);
20538
20539 switch (mode_line_target)
20540 {
20541 case MODE_LINE_NOPROP:
20542 case MODE_LINE_TITLE:
20543 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20544 break;
20545 case MODE_LINE_STRING:
20546 {
20547 ptrdiff_t bytepos = last_offset;
20548 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20549 ptrdiff_t endpos = (precision <= 0
20550 ? string_byte_to_char (elt, offset)
20551 : charpos + nchars);
20552
20553 n += store_mode_line_string (NULL,
20554 Fsubstring (elt, make_number (charpos),
20555 make_number (endpos)),
20556 0, 0, 0, Qnil);
20557 }
20558 break;
20559 case MODE_LINE_DISPLAY:
20560 {
20561 ptrdiff_t bytepos = last_offset;
20562 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20563
20564 if (precision <= 0)
20565 nchars = string_byte_to_char (elt, offset) - charpos;
20566 n += display_string (NULL, elt, Qnil, 0, charpos,
20567 it, 0, nchars, 0,
20568 STRING_MULTIBYTE (elt));
20569 }
20570 break;
20571 }
20572 }
20573 else /* c == '%' */
20574 {
20575 ptrdiff_t percent_position = offset;
20576
20577 /* Get the specified minimum width. Zero means
20578 don't pad. */
20579 field = 0;
20580 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20581 field = field * 10 + c - '0';
20582
20583 /* Don't pad beyond the total padding allowed. */
20584 if (field_width - n > 0 && field > field_width - n)
20585 field = field_width - n;
20586
20587 /* Note that either PRECISION <= 0 or N < PRECISION. */
20588 prec = precision - n;
20589
20590 if (c == 'M')
20591 n += display_mode_element (it, depth, field, prec,
20592 Vglobal_mode_string, props,
20593 risky);
20594 else if (c != 0)
20595 {
20596 int multibyte;
20597 ptrdiff_t bytepos, charpos;
20598 const char *spec;
20599 Lisp_Object string;
20600
20601 bytepos = percent_position;
20602 charpos = (STRING_MULTIBYTE (elt)
20603 ? string_byte_to_char (elt, bytepos)
20604 : bytepos);
20605 spec = decode_mode_spec (it->w, c, field, &string);
20606 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20607
20608 switch (mode_line_target)
20609 {
20610 case MODE_LINE_NOPROP:
20611 case MODE_LINE_TITLE:
20612 n += store_mode_line_noprop (spec, field, prec);
20613 break;
20614 case MODE_LINE_STRING:
20615 {
20616 Lisp_Object tem = build_string (spec);
20617 props = Ftext_properties_at (make_number (charpos), elt);
20618 /* Should only keep face property in props */
20619 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20620 }
20621 break;
20622 case MODE_LINE_DISPLAY:
20623 {
20624 int nglyphs_before, nwritten;
20625
20626 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20627 nwritten = display_string (spec, string, elt,
20628 charpos, 0, it,
20629 field, prec, 0,
20630 multibyte);
20631
20632 /* Assign to the glyphs written above the
20633 string where the `%x' came from, position
20634 of the `%'. */
20635 if (nwritten > 0)
20636 {
20637 struct glyph *glyph
20638 = (it->glyph_row->glyphs[TEXT_AREA]
20639 + nglyphs_before);
20640 int i;
20641
20642 for (i = 0; i < nwritten; ++i)
20643 {
20644 glyph[i].object = elt;
20645 glyph[i].charpos = charpos;
20646 }
20647
20648 n += nwritten;
20649 }
20650 }
20651 break;
20652 }
20653 }
20654 else /* c == 0 */
20655 break;
20656 }
20657 }
20658 }
20659 break;
20660
20661 case Lisp_Symbol:
20662 /* A symbol: process the value of the symbol recursively
20663 as if it appeared here directly. Avoid error if symbol void.
20664 Special case: if value of symbol is a string, output the string
20665 literally. */
20666 {
20667 register Lisp_Object tem;
20668
20669 /* If the variable is not marked as risky to set
20670 then its contents are risky to use. */
20671 if (NILP (Fget (elt, Qrisky_local_variable)))
20672 risky = 1;
20673
20674 tem = Fboundp (elt);
20675 if (!NILP (tem))
20676 {
20677 tem = Fsymbol_value (elt);
20678 /* If value is a string, output that string literally:
20679 don't check for % within it. */
20680 if (STRINGP (tem))
20681 literal = 1;
20682
20683 if (!EQ (tem, elt))
20684 {
20685 /* Give up right away for nil or t. */
20686 elt = tem;
20687 goto tail_recurse;
20688 }
20689 }
20690 }
20691 break;
20692
20693 case Lisp_Cons:
20694 {
20695 register Lisp_Object car, tem;
20696
20697 /* A cons cell: five distinct cases.
20698 If first element is :eval or :propertize, do something special.
20699 If first element is a string or a cons, process all the elements
20700 and effectively concatenate them.
20701 If first element is a negative number, truncate displaying cdr to
20702 at most that many characters. If positive, pad (with spaces)
20703 to at least that many characters.
20704 If first element is a symbol, process the cadr or caddr recursively
20705 according to whether the symbol's value is non-nil or nil. */
20706 car = XCAR (elt);
20707 if (EQ (car, QCeval))
20708 {
20709 /* An element of the form (:eval FORM) means evaluate FORM
20710 and use the result as mode line elements. */
20711
20712 if (risky)
20713 break;
20714
20715 if (CONSP (XCDR (elt)))
20716 {
20717 Lisp_Object spec;
20718 spec = safe_eval (XCAR (XCDR (elt)));
20719 n += display_mode_element (it, depth, field_width - n,
20720 precision - n, spec, props,
20721 risky);
20722 }
20723 }
20724 else if (EQ (car, QCpropertize))
20725 {
20726 /* An element of the form (:propertize ELT PROPS...)
20727 means display ELT but applying properties PROPS. */
20728
20729 if (risky)
20730 break;
20731
20732 if (CONSP (XCDR (elt)))
20733 n += display_mode_element (it, depth, field_width - n,
20734 precision - n, XCAR (XCDR (elt)),
20735 XCDR (XCDR (elt)), risky);
20736 }
20737 else if (SYMBOLP (car))
20738 {
20739 tem = Fboundp (car);
20740 elt = XCDR (elt);
20741 if (!CONSP (elt))
20742 goto invalid;
20743 /* elt is now the cdr, and we know it is a cons cell.
20744 Use its car if CAR has a non-nil value. */
20745 if (!NILP (tem))
20746 {
20747 tem = Fsymbol_value (car);
20748 if (!NILP (tem))
20749 {
20750 elt = XCAR (elt);
20751 goto tail_recurse;
20752 }
20753 }
20754 /* Symbol's value is nil (or symbol is unbound)
20755 Get the cddr of the original list
20756 and if possible find the caddr and use that. */
20757 elt = XCDR (elt);
20758 if (NILP (elt))
20759 break;
20760 else if (!CONSP (elt))
20761 goto invalid;
20762 elt = XCAR (elt);
20763 goto tail_recurse;
20764 }
20765 else if (INTEGERP (car))
20766 {
20767 register int lim = XINT (car);
20768 elt = XCDR (elt);
20769 if (lim < 0)
20770 {
20771 /* Negative int means reduce maximum width. */
20772 if (precision <= 0)
20773 precision = -lim;
20774 else
20775 precision = min (precision, -lim);
20776 }
20777 else if (lim > 0)
20778 {
20779 /* Padding specified. Don't let it be more than
20780 current maximum. */
20781 if (precision > 0)
20782 lim = min (precision, lim);
20783
20784 /* If that's more padding than already wanted, queue it.
20785 But don't reduce padding already specified even if
20786 that is beyond the current truncation point. */
20787 field_width = max (lim, field_width);
20788 }
20789 goto tail_recurse;
20790 }
20791 else if (STRINGP (car) || CONSP (car))
20792 {
20793 Lisp_Object halftail = elt;
20794 int len = 0;
20795
20796 while (CONSP (elt)
20797 && (precision <= 0 || n < precision))
20798 {
20799 n += display_mode_element (it, depth,
20800 /* Do padding only after the last
20801 element in the list. */
20802 (! CONSP (XCDR (elt))
20803 ? field_width - n
20804 : 0),
20805 precision - n, XCAR (elt),
20806 props, risky);
20807 elt = XCDR (elt);
20808 len++;
20809 if ((len & 1) == 0)
20810 halftail = XCDR (halftail);
20811 /* Check for cycle. */
20812 if (EQ (halftail, elt))
20813 break;
20814 }
20815 }
20816 }
20817 break;
20818
20819 default:
20820 invalid:
20821 elt = build_string ("*invalid*");
20822 goto tail_recurse;
20823 }
20824
20825 /* Pad to FIELD_WIDTH. */
20826 if (field_width > 0 && n < field_width)
20827 {
20828 switch (mode_line_target)
20829 {
20830 case MODE_LINE_NOPROP:
20831 case MODE_LINE_TITLE:
20832 n += store_mode_line_noprop ("", field_width - n, 0);
20833 break;
20834 case MODE_LINE_STRING:
20835 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20836 break;
20837 case MODE_LINE_DISPLAY:
20838 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20839 0, 0, 0);
20840 break;
20841 }
20842 }
20843
20844 return n;
20845 }
20846
20847 /* Store a mode-line string element in mode_line_string_list.
20848
20849 If STRING is non-null, display that C string. Otherwise, the Lisp
20850 string LISP_STRING is displayed.
20851
20852 FIELD_WIDTH is the minimum number of output glyphs to produce.
20853 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20854 with spaces. FIELD_WIDTH <= 0 means don't pad.
20855
20856 PRECISION is the maximum number of characters to output from
20857 STRING. PRECISION <= 0 means don't truncate the string.
20858
20859 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20860 properties to the string.
20861
20862 PROPS are the properties to add to the string.
20863 The mode_line_string_face face property is always added to the string.
20864 */
20865
20866 static int
20867 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20868 int field_width, int precision, Lisp_Object props)
20869 {
20870 ptrdiff_t len;
20871 int n = 0;
20872
20873 if (string != NULL)
20874 {
20875 len = strlen (string);
20876 if (precision > 0 && len > precision)
20877 len = precision;
20878 lisp_string = make_string (string, len);
20879 if (NILP (props))
20880 props = mode_line_string_face_prop;
20881 else if (!NILP (mode_line_string_face))
20882 {
20883 Lisp_Object face = Fplist_get (props, Qface);
20884 props = Fcopy_sequence (props);
20885 if (NILP (face))
20886 face = mode_line_string_face;
20887 else
20888 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20889 props = Fplist_put (props, Qface, face);
20890 }
20891 Fadd_text_properties (make_number (0), make_number (len),
20892 props, lisp_string);
20893 }
20894 else
20895 {
20896 len = XFASTINT (Flength (lisp_string));
20897 if (precision > 0 && len > precision)
20898 {
20899 len = precision;
20900 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20901 precision = -1;
20902 }
20903 if (!NILP (mode_line_string_face))
20904 {
20905 Lisp_Object face;
20906 if (NILP (props))
20907 props = Ftext_properties_at (make_number (0), lisp_string);
20908 face = Fplist_get (props, Qface);
20909 if (NILP (face))
20910 face = mode_line_string_face;
20911 else
20912 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20913 props = Fcons (Qface, Fcons (face, Qnil));
20914 if (copy_string)
20915 lisp_string = Fcopy_sequence (lisp_string);
20916 }
20917 if (!NILP (props))
20918 Fadd_text_properties (make_number (0), make_number (len),
20919 props, lisp_string);
20920 }
20921
20922 if (len > 0)
20923 {
20924 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20925 n += len;
20926 }
20927
20928 if (field_width > len)
20929 {
20930 field_width -= len;
20931 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20932 if (!NILP (props))
20933 Fadd_text_properties (make_number (0), make_number (field_width),
20934 props, lisp_string);
20935 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20936 n += field_width;
20937 }
20938
20939 return n;
20940 }
20941
20942
20943 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20944 1, 4, 0,
20945 doc: /* Format a string out of a mode line format specification.
20946 First arg FORMAT specifies the mode line format (see `mode-line-format'
20947 for details) to use.
20948
20949 By default, the format is evaluated for the currently selected window.
20950
20951 Optional second arg FACE specifies the face property to put on all
20952 characters for which no face is specified. The value nil means the
20953 default face. The value t means whatever face the window's mode line
20954 currently uses (either `mode-line' or `mode-line-inactive',
20955 depending on whether the window is the selected window or not).
20956 An integer value means the value string has no text
20957 properties.
20958
20959 Optional third and fourth args WINDOW and BUFFER specify the window
20960 and buffer to use as the context for the formatting (defaults
20961 are the selected window and the WINDOW's buffer). */)
20962 (Lisp_Object format, Lisp_Object face,
20963 Lisp_Object window, Lisp_Object buffer)
20964 {
20965 struct it it;
20966 int len;
20967 struct window *w;
20968 struct buffer *old_buffer = NULL;
20969 int face_id;
20970 int no_props = INTEGERP (face);
20971 ptrdiff_t count = SPECPDL_INDEX ();
20972 Lisp_Object str;
20973 int string_start = 0;
20974
20975 if (NILP (window))
20976 window = selected_window;
20977 CHECK_WINDOW (window);
20978 w = XWINDOW (window);
20979
20980 if (NILP (buffer))
20981 buffer = w->buffer;
20982 CHECK_BUFFER (buffer);
20983
20984 /* Make formatting the modeline a non-op when noninteractive, otherwise
20985 there will be problems later caused by a partially initialized frame. */
20986 if (NILP (format) || noninteractive)
20987 return empty_unibyte_string;
20988
20989 if (no_props)
20990 face = Qnil;
20991
20992 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20993 : EQ (face, Qt) ? (EQ (window, selected_window)
20994 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20995 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20996 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20997 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20998 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20999 : DEFAULT_FACE_ID;
21000
21001 if (XBUFFER (buffer) != current_buffer)
21002 old_buffer = current_buffer;
21003
21004 /* Save things including mode_line_proptrans_alist,
21005 and set that to nil so that we don't alter the outer value. */
21006 record_unwind_protect (unwind_format_mode_line,
21007 format_mode_line_unwind_data
21008 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21009 old_buffer, selected_window, 1));
21010 mode_line_proptrans_alist = Qnil;
21011
21012 Fselect_window (window, Qt);
21013 if (old_buffer)
21014 set_buffer_internal_1 (XBUFFER (buffer));
21015
21016 init_iterator (&it, w, -1, -1, NULL, face_id);
21017
21018 if (no_props)
21019 {
21020 mode_line_target = MODE_LINE_NOPROP;
21021 mode_line_string_face_prop = Qnil;
21022 mode_line_string_list = Qnil;
21023 string_start = MODE_LINE_NOPROP_LEN (0);
21024 }
21025 else
21026 {
21027 mode_line_target = MODE_LINE_STRING;
21028 mode_line_string_list = Qnil;
21029 mode_line_string_face = face;
21030 mode_line_string_face_prop
21031 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21032 }
21033
21034 push_kboard (FRAME_KBOARD (it.f));
21035 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21036 pop_kboard ();
21037
21038 if (no_props)
21039 {
21040 len = MODE_LINE_NOPROP_LEN (string_start);
21041 str = make_string (mode_line_noprop_buf + string_start, len);
21042 }
21043 else
21044 {
21045 mode_line_string_list = Fnreverse (mode_line_string_list);
21046 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21047 empty_unibyte_string);
21048 }
21049
21050 unbind_to (count, Qnil);
21051 return str;
21052 }
21053
21054 /* Write a null-terminated, right justified decimal representation of
21055 the positive integer D to BUF using a minimal field width WIDTH. */
21056
21057 static void
21058 pint2str (register char *buf, register int width, register ptrdiff_t d)
21059 {
21060 register char *p = buf;
21061
21062 if (d <= 0)
21063 *p++ = '0';
21064 else
21065 {
21066 while (d > 0)
21067 {
21068 *p++ = d % 10 + '0';
21069 d /= 10;
21070 }
21071 }
21072
21073 for (width -= (int) (p - buf); width > 0; --width)
21074 *p++ = ' ';
21075 *p-- = '\0';
21076 while (p > buf)
21077 {
21078 d = *buf;
21079 *buf++ = *p;
21080 *p-- = d;
21081 }
21082 }
21083
21084 /* Write a null-terminated, right justified decimal and "human
21085 readable" representation of the nonnegative integer D to BUF using
21086 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21087
21088 static const char power_letter[] =
21089 {
21090 0, /* no letter */
21091 'k', /* kilo */
21092 'M', /* mega */
21093 'G', /* giga */
21094 'T', /* tera */
21095 'P', /* peta */
21096 'E', /* exa */
21097 'Z', /* zetta */
21098 'Y' /* yotta */
21099 };
21100
21101 static void
21102 pint2hrstr (char *buf, int width, ptrdiff_t d)
21103 {
21104 /* We aim to represent the nonnegative integer D as
21105 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21106 ptrdiff_t quotient = d;
21107 int remainder = 0;
21108 /* -1 means: do not use TENTHS. */
21109 int tenths = -1;
21110 int exponent = 0;
21111
21112 /* Length of QUOTIENT.TENTHS as a string. */
21113 int length;
21114
21115 char * psuffix;
21116 char * p;
21117
21118 if (1000 <= quotient)
21119 {
21120 /* Scale to the appropriate EXPONENT. */
21121 do
21122 {
21123 remainder = quotient % 1000;
21124 quotient /= 1000;
21125 exponent++;
21126 }
21127 while (1000 <= quotient);
21128
21129 /* Round to nearest and decide whether to use TENTHS or not. */
21130 if (quotient <= 9)
21131 {
21132 tenths = remainder / 100;
21133 if (50 <= remainder % 100)
21134 {
21135 if (tenths < 9)
21136 tenths++;
21137 else
21138 {
21139 quotient++;
21140 if (quotient == 10)
21141 tenths = -1;
21142 else
21143 tenths = 0;
21144 }
21145 }
21146 }
21147 else
21148 if (500 <= remainder)
21149 {
21150 if (quotient < 999)
21151 quotient++;
21152 else
21153 {
21154 quotient = 1;
21155 exponent++;
21156 tenths = 0;
21157 }
21158 }
21159 }
21160
21161 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21162 if (tenths == -1 && quotient <= 99)
21163 if (quotient <= 9)
21164 length = 1;
21165 else
21166 length = 2;
21167 else
21168 length = 3;
21169 p = psuffix = buf + max (width, length);
21170
21171 /* Print EXPONENT. */
21172 *psuffix++ = power_letter[exponent];
21173 *psuffix = '\0';
21174
21175 /* Print TENTHS. */
21176 if (tenths >= 0)
21177 {
21178 *--p = '0' + tenths;
21179 *--p = '.';
21180 }
21181
21182 /* Print QUOTIENT. */
21183 do
21184 {
21185 int digit = quotient % 10;
21186 *--p = '0' + digit;
21187 }
21188 while ((quotient /= 10) != 0);
21189
21190 /* Print leading spaces. */
21191 while (buf < p)
21192 *--p = ' ';
21193 }
21194
21195 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21196 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21197 type of CODING_SYSTEM. Return updated pointer into BUF. */
21198
21199 static unsigned char invalid_eol_type[] = "(*invalid*)";
21200
21201 static char *
21202 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21203 {
21204 Lisp_Object val;
21205 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21206 const unsigned char *eol_str;
21207 int eol_str_len;
21208 /* The EOL conversion we are using. */
21209 Lisp_Object eoltype;
21210
21211 val = CODING_SYSTEM_SPEC (coding_system);
21212 eoltype = Qnil;
21213
21214 if (!VECTORP (val)) /* Not yet decided. */
21215 {
21216 *buf++ = multibyte ? '-' : ' ';
21217 if (eol_flag)
21218 eoltype = eol_mnemonic_undecided;
21219 /* Don't mention EOL conversion if it isn't decided. */
21220 }
21221 else
21222 {
21223 Lisp_Object attrs;
21224 Lisp_Object eolvalue;
21225
21226 attrs = AREF (val, 0);
21227 eolvalue = AREF (val, 2);
21228
21229 *buf++ = multibyte
21230 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21231 : ' ';
21232
21233 if (eol_flag)
21234 {
21235 /* The EOL conversion that is normal on this system. */
21236
21237 if (NILP (eolvalue)) /* Not yet decided. */
21238 eoltype = eol_mnemonic_undecided;
21239 else if (VECTORP (eolvalue)) /* Not yet decided. */
21240 eoltype = eol_mnemonic_undecided;
21241 else /* eolvalue is Qunix, Qdos, or Qmac. */
21242 eoltype = (EQ (eolvalue, Qunix)
21243 ? eol_mnemonic_unix
21244 : (EQ (eolvalue, Qdos) == 1
21245 ? eol_mnemonic_dos : eol_mnemonic_mac));
21246 }
21247 }
21248
21249 if (eol_flag)
21250 {
21251 /* Mention the EOL conversion if it is not the usual one. */
21252 if (STRINGP (eoltype))
21253 {
21254 eol_str = SDATA (eoltype);
21255 eol_str_len = SBYTES (eoltype);
21256 }
21257 else if (CHARACTERP (eoltype))
21258 {
21259 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21260 int c = XFASTINT (eoltype);
21261 eol_str_len = CHAR_STRING (c, tmp);
21262 eol_str = tmp;
21263 }
21264 else
21265 {
21266 eol_str = invalid_eol_type;
21267 eol_str_len = sizeof (invalid_eol_type) - 1;
21268 }
21269 memcpy (buf, eol_str, eol_str_len);
21270 buf += eol_str_len;
21271 }
21272
21273 return buf;
21274 }
21275
21276 /* Return a string for the output of a mode line %-spec for window W,
21277 generated by character C. FIELD_WIDTH > 0 means pad the string
21278 returned with spaces to that value. Return a Lisp string in
21279 *STRING if the resulting string is taken from that Lisp string.
21280
21281 Note we operate on the current buffer for most purposes,
21282 the exception being w->base_line_pos. */
21283
21284 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21285
21286 static const char *
21287 decode_mode_spec (struct window *w, register int c, int field_width,
21288 Lisp_Object *string)
21289 {
21290 Lisp_Object obj;
21291 struct frame *f = XFRAME (WINDOW_FRAME (w));
21292 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21293 struct buffer *b = current_buffer;
21294
21295 obj = Qnil;
21296 *string = Qnil;
21297
21298 switch (c)
21299 {
21300 case '*':
21301 if (!NILP (BVAR (b, read_only)))
21302 return "%";
21303 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21304 return "*";
21305 return "-";
21306
21307 case '+':
21308 /* This differs from %* only for a modified read-only buffer. */
21309 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21310 return "*";
21311 if (!NILP (BVAR (b, read_only)))
21312 return "%";
21313 return "-";
21314
21315 case '&':
21316 /* This differs from %* in ignoring read-only-ness. */
21317 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21318 return "*";
21319 return "-";
21320
21321 case '%':
21322 return "%";
21323
21324 case '[':
21325 {
21326 int i;
21327 char *p;
21328
21329 if (command_loop_level > 5)
21330 return "[[[... ";
21331 p = decode_mode_spec_buf;
21332 for (i = 0; i < command_loop_level; i++)
21333 *p++ = '[';
21334 *p = 0;
21335 return decode_mode_spec_buf;
21336 }
21337
21338 case ']':
21339 {
21340 int i;
21341 char *p;
21342
21343 if (command_loop_level > 5)
21344 return " ...]]]";
21345 p = decode_mode_spec_buf;
21346 for (i = 0; i < command_loop_level; i++)
21347 *p++ = ']';
21348 *p = 0;
21349 return decode_mode_spec_buf;
21350 }
21351
21352 case '-':
21353 {
21354 register int i;
21355
21356 /* Let lots_of_dashes be a string of infinite length. */
21357 if (mode_line_target == MODE_LINE_NOPROP ||
21358 mode_line_target == MODE_LINE_STRING)
21359 return "--";
21360 if (field_width <= 0
21361 || field_width > sizeof (lots_of_dashes))
21362 {
21363 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21364 decode_mode_spec_buf[i] = '-';
21365 decode_mode_spec_buf[i] = '\0';
21366 return decode_mode_spec_buf;
21367 }
21368 else
21369 return lots_of_dashes;
21370 }
21371
21372 case 'b':
21373 obj = BVAR (b, name);
21374 break;
21375
21376 case 'c':
21377 /* %c and %l are ignored in `frame-title-format'.
21378 (In redisplay_internal, the frame title is drawn _before_ the
21379 windows are updated, so the stuff which depends on actual
21380 window contents (such as %l) may fail to render properly, or
21381 even crash emacs.) */
21382 if (mode_line_target == MODE_LINE_TITLE)
21383 return "";
21384 else
21385 {
21386 ptrdiff_t col = current_column ();
21387 w->column_number_displayed = make_number (col);
21388 pint2str (decode_mode_spec_buf, field_width, col);
21389 return decode_mode_spec_buf;
21390 }
21391
21392 case 'e':
21393 #ifndef SYSTEM_MALLOC
21394 {
21395 if (NILP (Vmemory_full))
21396 return "";
21397 else
21398 return "!MEM FULL! ";
21399 }
21400 #else
21401 return "";
21402 #endif
21403
21404 case 'F':
21405 /* %F displays the frame name. */
21406 if (!NILP (f->title))
21407 return SSDATA (f->title);
21408 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21409 return SSDATA (f->name);
21410 return "Emacs";
21411
21412 case 'f':
21413 obj = BVAR (b, filename);
21414 break;
21415
21416 case 'i':
21417 {
21418 ptrdiff_t size = ZV - BEGV;
21419 pint2str (decode_mode_spec_buf, field_width, size);
21420 return decode_mode_spec_buf;
21421 }
21422
21423 case 'I':
21424 {
21425 ptrdiff_t size = ZV - BEGV;
21426 pint2hrstr (decode_mode_spec_buf, field_width, size);
21427 return decode_mode_spec_buf;
21428 }
21429
21430 case 'l':
21431 {
21432 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21433 ptrdiff_t topline, nlines, height;
21434 ptrdiff_t junk;
21435
21436 /* %c and %l are ignored in `frame-title-format'. */
21437 if (mode_line_target == MODE_LINE_TITLE)
21438 return "";
21439
21440 startpos = XMARKER (w->start)->charpos;
21441 startpos_byte = marker_byte_position (w->start);
21442 height = WINDOW_TOTAL_LINES (w);
21443
21444 /* If we decided that this buffer isn't suitable for line numbers,
21445 don't forget that too fast. */
21446 if (EQ (w->base_line_pos, w->buffer))
21447 goto no_value;
21448 /* But do forget it, if the window shows a different buffer now. */
21449 else if (BUFFERP (w->base_line_pos))
21450 w->base_line_pos = Qnil;
21451
21452 /* If the buffer is very big, don't waste time. */
21453 if (INTEGERP (Vline_number_display_limit)
21454 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21455 {
21456 w->base_line_pos = Qnil;
21457 w->base_line_number = Qnil;
21458 goto no_value;
21459 }
21460
21461 if (INTEGERP (w->base_line_number)
21462 && INTEGERP (w->base_line_pos)
21463 && XFASTINT (w->base_line_pos) <= startpos)
21464 {
21465 line = XFASTINT (w->base_line_number);
21466 linepos = XFASTINT (w->base_line_pos);
21467 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21468 }
21469 else
21470 {
21471 line = 1;
21472 linepos = BUF_BEGV (b);
21473 linepos_byte = BUF_BEGV_BYTE (b);
21474 }
21475
21476 /* Count lines from base line to window start position. */
21477 nlines = display_count_lines (linepos_byte,
21478 startpos_byte,
21479 startpos, &junk);
21480
21481 topline = nlines + line;
21482
21483 /* Determine a new base line, if the old one is too close
21484 or too far away, or if we did not have one.
21485 "Too close" means it's plausible a scroll-down would
21486 go back past it. */
21487 if (startpos == BUF_BEGV (b))
21488 {
21489 w->base_line_number = make_number (topline);
21490 w->base_line_pos = make_number (BUF_BEGV (b));
21491 }
21492 else if (nlines < height + 25 || nlines > height * 3 + 50
21493 || linepos == BUF_BEGV (b))
21494 {
21495 ptrdiff_t limit = BUF_BEGV (b);
21496 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21497 ptrdiff_t position;
21498 ptrdiff_t distance =
21499 (height * 2 + 30) * line_number_display_limit_width;
21500
21501 if (startpos - distance > limit)
21502 {
21503 limit = startpos - distance;
21504 limit_byte = CHAR_TO_BYTE (limit);
21505 }
21506
21507 nlines = display_count_lines (startpos_byte,
21508 limit_byte,
21509 - (height * 2 + 30),
21510 &position);
21511 /* If we couldn't find the lines we wanted within
21512 line_number_display_limit_width chars per line,
21513 give up on line numbers for this window. */
21514 if (position == limit_byte && limit == startpos - distance)
21515 {
21516 w->base_line_pos = w->buffer;
21517 w->base_line_number = Qnil;
21518 goto no_value;
21519 }
21520
21521 w->base_line_number = make_number (topline - nlines);
21522 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21523 }
21524
21525 /* Now count lines from the start pos to point. */
21526 nlines = display_count_lines (startpos_byte,
21527 PT_BYTE, PT, &junk);
21528
21529 /* Record that we did display the line number. */
21530 line_number_displayed = 1;
21531
21532 /* Make the string to show. */
21533 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21534 return decode_mode_spec_buf;
21535 no_value:
21536 {
21537 char* p = decode_mode_spec_buf;
21538 int pad = field_width - 2;
21539 while (pad-- > 0)
21540 *p++ = ' ';
21541 *p++ = '?';
21542 *p++ = '?';
21543 *p = '\0';
21544 return decode_mode_spec_buf;
21545 }
21546 }
21547 break;
21548
21549 case 'm':
21550 obj = BVAR (b, mode_name);
21551 break;
21552
21553 case 'n':
21554 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21555 return " Narrow";
21556 break;
21557
21558 case 'p':
21559 {
21560 ptrdiff_t pos = marker_position (w->start);
21561 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21562
21563 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21564 {
21565 if (pos <= BUF_BEGV (b))
21566 return "All";
21567 else
21568 return "Bottom";
21569 }
21570 else if (pos <= BUF_BEGV (b))
21571 return "Top";
21572 else
21573 {
21574 if (total > 1000000)
21575 /* Do it differently for a large value, to avoid overflow. */
21576 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21577 else
21578 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21579 /* We can't normally display a 3-digit number,
21580 so get us a 2-digit number that is close. */
21581 if (total == 100)
21582 total = 99;
21583 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21584 return decode_mode_spec_buf;
21585 }
21586 }
21587
21588 /* Display percentage of size above the bottom of the screen. */
21589 case 'P':
21590 {
21591 ptrdiff_t toppos = marker_position (w->start);
21592 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21593 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21594
21595 if (botpos >= BUF_ZV (b))
21596 {
21597 if (toppos <= BUF_BEGV (b))
21598 return "All";
21599 else
21600 return "Bottom";
21601 }
21602 else
21603 {
21604 if (total > 1000000)
21605 /* Do it differently for a large value, to avoid overflow. */
21606 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21607 else
21608 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21609 /* We can't normally display a 3-digit number,
21610 so get us a 2-digit number that is close. */
21611 if (total == 100)
21612 total = 99;
21613 if (toppos <= BUF_BEGV (b))
21614 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21615 else
21616 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21617 return decode_mode_spec_buf;
21618 }
21619 }
21620
21621 case 's':
21622 /* status of process */
21623 obj = Fget_buffer_process (Fcurrent_buffer ());
21624 if (NILP (obj))
21625 return "no process";
21626 #ifndef MSDOS
21627 obj = Fsymbol_name (Fprocess_status (obj));
21628 #endif
21629 break;
21630
21631 case '@':
21632 {
21633 ptrdiff_t count = inhibit_garbage_collection ();
21634 Lisp_Object val = call1 (intern ("file-remote-p"),
21635 BVAR (current_buffer, directory));
21636 unbind_to (count, Qnil);
21637
21638 if (NILP (val))
21639 return "-";
21640 else
21641 return "@";
21642 }
21643
21644 case 't': /* indicate TEXT or BINARY */
21645 return "T";
21646
21647 case 'z':
21648 /* coding-system (not including end-of-line format) */
21649 case 'Z':
21650 /* coding-system (including end-of-line type) */
21651 {
21652 int eol_flag = (c == 'Z');
21653 char *p = decode_mode_spec_buf;
21654
21655 if (! FRAME_WINDOW_P (f))
21656 {
21657 /* No need to mention EOL here--the terminal never needs
21658 to do EOL conversion. */
21659 p = decode_mode_spec_coding (CODING_ID_NAME
21660 (FRAME_KEYBOARD_CODING (f)->id),
21661 p, 0);
21662 p = decode_mode_spec_coding (CODING_ID_NAME
21663 (FRAME_TERMINAL_CODING (f)->id),
21664 p, 0);
21665 }
21666 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21667 p, eol_flag);
21668
21669 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21670 #ifdef subprocesses
21671 obj = Fget_buffer_process (Fcurrent_buffer ());
21672 if (PROCESSP (obj))
21673 {
21674 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21675 p, eol_flag);
21676 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21677 p, eol_flag);
21678 }
21679 #endif /* subprocesses */
21680 #endif /* 0 */
21681 *p = 0;
21682 return decode_mode_spec_buf;
21683 }
21684 }
21685
21686 if (STRINGP (obj))
21687 {
21688 *string = obj;
21689 return SSDATA (obj);
21690 }
21691 else
21692 return "";
21693 }
21694
21695
21696 /* Count up to COUNT lines starting from START_BYTE.
21697 But don't go beyond LIMIT_BYTE.
21698 Return the number of lines thus found (always nonnegative).
21699
21700 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21701
21702 static ptrdiff_t
21703 display_count_lines (ptrdiff_t start_byte,
21704 ptrdiff_t limit_byte, ptrdiff_t count,
21705 ptrdiff_t *byte_pos_ptr)
21706 {
21707 register unsigned char *cursor;
21708 unsigned char *base;
21709
21710 register ptrdiff_t ceiling;
21711 register unsigned char *ceiling_addr;
21712 ptrdiff_t orig_count = count;
21713
21714 /* If we are not in selective display mode,
21715 check only for newlines. */
21716 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21717 && !INTEGERP (BVAR (current_buffer, selective_display)));
21718
21719 if (count > 0)
21720 {
21721 while (start_byte < limit_byte)
21722 {
21723 ceiling = BUFFER_CEILING_OF (start_byte);
21724 ceiling = min (limit_byte - 1, ceiling);
21725 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21726 base = (cursor = BYTE_POS_ADDR (start_byte));
21727 while (1)
21728 {
21729 if (selective_display)
21730 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21731 ;
21732 else
21733 while (*cursor != '\n' && ++cursor != ceiling_addr)
21734 ;
21735
21736 if (cursor != ceiling_addr)
21737 {
21738 if (--count == 0)
21739 {
21740 start_byte += cursor - base + 1;
21741 *byte_pos_ptr = start_byte;
21742 return orig_count;
21743 }
21744 else
21745 if (++cursor == ceiling_addr)
21746 break;
21747 }
21748 else
21749 break;
21750 }
21751 start_byte += cursor - base;
21752 }
21753 }
21754 else
21755 {
21756 while (start_byte > limit_byte)
21757 {
21758 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21759 ceiling = max (limit_byte, ceiling);
21760 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21761 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21762 while (1)
21763 {
21764 if (selective_display)
21765 while (--cursor != ceiling_addr
21766 && *cursor != '\n' && *cursor != 015)
21767 ;
21768 else
21769 while (--cursor != ceiling_addr && *cursor != '\n')
21770 ;
21771
21772 if (cursor != ceiling_addr)
21773 {
21774 if (++count == 0)
21775 {
21776 start_byte += cursor - base + 1;
21777 *byte_pos_ptr = start_byte;
21778 /* When scanning backwards, we should
21779 not count the newline posterior to which we stop. */
21780 return - orig_count - 1;
21781 }
21782 }
21783 else
21784 break;
21785 }
21786 /* Here we add 1 to compensate for the last decrement
21787 of CURSOR, which took it past the valid range. */
21788 start_byte += cursor - base + 1;
21789 }
21790 }
21791
21792 *byte_pos_ptr = limit_byte;
21793
21794 if (count < 0)
21795 return - orig_count + count;
21796 return orig_count - count;
21797
21798 }
21799
21800
21801 \f
21802 /***********************************************************************
21803 Displaying strings
21804 ***********************************************************************/
21805
21806 /* Display a NUL-terminated string, starting with index START.
21807
21808 If STRING is non-null, display that C string. Otherwise, the Lisp
21809 string LISP_STRING is displayed. There's a case that STRING is
21810 non-null and LISP_STRING is not nil. It means STRING is a string
21811 data of LISP_STRING. In that case, we display LISP_STRING while
21812 ignoring its text properties.
21813
21814 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21815 FACE_STRING. Display STRING or LISP_STRING with the face at
21816 FACE_STRING_POS in FACE_STRING:
21817
21818 Display the string in the environment given by IT, but use the
21819 standard display table, temporarily.
21820
21821 FIELD_WIDTH is the minimum number of output glyphs to produce.
21822 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21823 with spaces. If STRING has more characters, more than FIELD_WIDTH
21824 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21825
21826 PRECISION is the maximum number of characters to output from
21827 STRING. PRECISION < 0 means don't truncate the string.
21828
21829 This is roughly equivalent to printf format specifiers:
21830
21831 FIELD_WIDTH PRECISION PRINTF
21832 ----------------------------------------
21833 -1 -1 %s
21834 -1 10 %.10s
21835 10 -1 %10s
21836 20 10 %20.10s
21837
21838 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21839 display them, and < 0 means obey the current buffer's value of
21840 enable_multibyte_characters.
21841
21842 Value is the number of columns displayed. */
21843
21844 static int
21845 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21846 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21847 int field_width, int precision, int max_x, int multibyte)
21848 {
21849 int hpos_at_start = it->hpos;
21850 int saved_face_id = it->face_id;
21851 struct glyph_row *row = it->glyph_row;
21852 ptrdiff_t it_charpos;
21853
21854 /* Initialize the iterator IT for iteration over STRING beginning
21855 with index START. */
21856 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21857 precision, field_width, multibyte);
21858 if (string && STRINGP (lisp_string))
21859 /* LISP_STRING is the one returned by decode_mode_spec. We should
21860 ignore its text properties. */
21861 it->stop_charpos = it->end_charpos;
21862
21863 /* If displaying STRING, set up the face of the iterator from
21864 FACE_STRING, if that's given. */
21865 if (STRINGP (face_string))
21866 {
21867 ptrdiff_t endptr;
21868 struct face *face;
21869
21870 it->face_id
21871 = face_at_string_position (it->w, face_string, face_string_pos,
21872 0, it->region_beg_charpos,
21873 it->region_end_charpos,
21874 &endptr, it->base_face_id, 0);
21875 face = FACE_FROM_ID (it->f, it->face_id);
21876 it->face_box_p = face->box != FACE_NO_BOX;
21877 }
21878
21879 /* Set max_x to the maximum allowed X position. Don't let it go
21880 beyond the right edge of the window. */
21881 if (max_x <= 0)
21882 max_x = it->last_visible_x;
21883 else
21884 max_x = min (max_x, it->last_visible_x);
21885
21886 /* Skip over display elements that are not visible. because IT->w is
21887 hscrolled. */
21888 if (it->current_x < it->first_visible_x)
21889 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21890 MOVE_TO_POS | MOVE_TO_X);
21891
21892 row->ascent = it->max_ascent;
21893 row->height = it->max_ascent + it->max_descent;
21894 row->phys_ascent = it->max_phys_ascent;
21895 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21896 row->extra_line_spacing = it->max_extra_line_spacing;
21897
21898 if (STRINGP (it->string))
21899 it_charpos = IT_STRING_CHARPOS (*it);
21900 else
21901 it_charpos = IT_CHARPOS (*it);
21902
21903 /* This condition is for the case that we are called with current_x
21904 past last_visible_x. */
21905 while (it->current_x < max_x)
21906 {
21907 int x_before, x, n_glyphs_before, i, nglyphs;
21908
21909 /* Get the next display element. */
21910 if (!get_next_display_element (it))
21911 break;
21912
21913 /* Produce glyphs. */
21914 x_before = it->current_x;
21915 n_glyphs_before = row->used[TEXT_AREA];
21916 PRODUCE_GLYPHS (it);
21917
21918 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21919 i = 0;
21920 x = x_before;
21921 while (i < nglyphs)
21922 {
21923 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21924
21925 if (it->line_wrap != TRUNCATE
21926 && x + glyph->pixel_width > max_x)
21927 {
21928 /* End of continued line or max_x reached. */
21929 if (CHAR_GLYPH_PADDING_P (*glyph))
21930 {
21931 /* A wide character is unbreakable. */
21932 if (row->reversed_p)
21933 unproduce_glyphs (it, row->used[TEXT_AREA]
21934 - n_glyphs_before);
21935 row->used[TEXT_AREA] = n_glyphs_before;
21936 it->current_x = x_before;
21937 }
21938 else
21939 {
21940 if (row->reversed_p)
21941 unproduce_glyphs (it, row->used[TEXT_AREA]
21942 - (n_glyphs_before + i));
21943 row->used[TEXT_AREA] = n_glyphs_before + i;
21944 it->current_x = x;
21945 }
21946 break;
21947 }
21948 else if (x + glyph->pixel_width >= it->first_visible_x)
21949 {
21950 /* Glyph is at least partially visible. */
21951 ++it->hpos;
21952 if (x < it->first_visible_x)
21953 row->x = x - it->first_visible_x;
21954 }
21955 else
21956 {
21957 /* Glyph is off the left margin of the display area.
21958 Should not happen. */
21959 abort ();
21960 }
21961
21962 row->ascent = max (row->ascent, it->max_ascent);
21963 row->height = max (row->height, it->max_ascent + it->max_descent);
21964 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21965 row->phys_height = max (row->phys_height,
21966 it->max_phys_ascent + it->max_phys_descent);
21967 row->extra_line_spacing = max (row->extra_line_spacing,
21968 it->max_extra_line_spacing);
21969 x += glyph->pixel_width;
21970 ++i;
21971 }
21972
21973 /* Stop if max_x reached. */
21974 if (i < nglyphs)
21975 break;
21976
21977 /* Stop at line ends. */
21978 if (ITERATOR_AT_END_OF_LINE_P (it))
21979 {
21980 it->continuation_lines_width = 0;
21981 break;
21982 }
21983
21984 set_iterator_to_next (it, 1);
21985 if (STRINGP (it->string))
21986 it_charpos = IT_STRING_CHARPOS (*it);
21987 else
21988 it_charpos = IT_CHARPOS (*it);
21989
21990 /* Stop if truncating at the right edge. */
21991 if (it->line_wrap == TRUNCATE
21992 && it->current_x >= it->last_visible_x)
21993 {
21994 /* Add truncation mark, but don't do it if the line is
21995 truncated at a padding space. */
21996 if (it_charpos < it->string_nchars)
21997 {
21998 if (!FRAME_WINDOW_P (it->f))
21999 {
22000 int ii, n;
22001
22002 if (it->current_x > it->last_visible_x)
22003 {
22004 if (!row->reversed_p)
22005 {
22006 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22007 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22008 break;
22009 }
22010 else
22011 {
22012 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22013 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22014 break;
22015 unproduce_glyphs (it, ii + 1);
22016 ii = row->used[TEXT_AREA] - (ii + 1);
22017 }
22018 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22019 {
22020 row->used[TEXT_AREA] = ii;
22021 produce_special_glyphs (it, IT_TRUNCATION);
22022 }
22023 }
22024 produce_special_glyphs (it, IT_TRUNCATION);
22025 }
22026 row->truncated_on_right_p = 1;
22027 }
22028 break;
22029 }
22030 }
22031
22032 /* Maybe insert a truncation at the left. */
22033 if (it->first_visible_x
22034 && it_charpos > 0)
22035 {
22036 if (!FRAME_WINDOW_P (it->f)
22037 || (row->reversed_p
22038 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22039 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22040 insert_left_trunc_glyphs (it);
22041 row->truncated_on_left_p = 1;
22042 }
22043
22044 it->face_id = saved_face_id;
22045
22046 /* Value is number of columns displayed. */
22047 return it->hpos - hpos_at_start;
22048 }
22049
22050
22051 \f
22052 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22053 appears as an element of LIST or as the car of an element of LIST.
22054 If PROPVAL is a list, compare each element against LIST in that
22055 way, and return 1/2 if any element of PROPVAL is found in LIST.
22056 Otherwise return 0. This function cannot quit.
22057 The return value is 2 if the text is invisible but with an ellipsis
22058 and 1 if it's invisible and without an ellipsis. */
22059
22060 int
22061 invisible_p (register Lisp_Object propval, Lisp_Object list)
22062 {
22063 register Lisp_Object tail, proptail;
22064
22065 for (tail = list; CONSP (tail); tail = XCDR (tail))
22066 {
22067 register Lisp_Object tem;
22068 tem = XCAR (tail);
22069 if (EQ (propval, tem))
22070 return 1;
22071 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22072 return NILP (XCDR (tem)) ? 1 : 2;
22073 }
22074
22075 if (CONSP (propval))
22076 {
22077 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22078 {
22079 Lisp_Object propelt;
22080 propelt = XCAR (proptail);
22081 for (tail = list; CONSP (tail); tail = XCDR (tail))
22082 {
22083 register Lisp_Object tem;
22084 tem = XCAR (tail);
22085 if (EQ (propelt, tem))
22086 return 1;
22087 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22088 return NILP (XCDR (tem)) ? 1 : 2;
22089 }
22090 }
22091 }
22092
22093 return 0;
22094 }
22095
22096 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22097 doc: /* Non-nil if the property makes the text invisible.
22098 POS-OR-PROP can be a marker or number, in which case it is taken to be
22099 a position in the current buffer and the value of the `invisible' property
22100 is checked; or it can be some other value, which is then presumed to be the
22101 value of the `invisible' property of the text of interest.
22102 The non-nil value returned can be t for truly invisible text or something
22103 else if the text is replaced by an ellipsis. */)
22104 (Lisp_Object pos_or_prop)
22105 {
22106 Lisp_Object prop
22107 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22108 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22109 : pos_or_prop);
22110 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22111 return (invis == 0 ? Qnil
22112 : invis == 1 ? Qt
22113 : make_number (invis));
22114 }
22115
22116 /* Calculate a width or height in pixels from a specification using
22117 the following elements:
22118
22119 SPEC ::=
22120 NUM - a (fractional) multiple of the default font width/height
22121 (NUM) - specifies exactly NUM pixels
22122 UNIT - a fixed number of pixels, see below.
22123 ELEMENT - size of a display element in pixels, see below.
22124 (NUM . SPEC) - equals NUM * SPEC
22125 (+ SPEC SPEC ...) - add pixel values
22126 (- SPEC SPEC ...) - subtract pixel values
22127 (- SPEC) - negate pixel value
22128
22129 NUM ::=
22130 INT or FLOAT - a number constant
22131 SYMBOL - use symbol's (buffer local) variable binding.
22132
22133 UNIT ::=
22134 in - pixels per inch *)
22135 mm - pixels per 1/1000 meter *)
22136 cm - pixels per 1/100 meter *)
22137 width - width of current font in pixels.
22138 height - height of current font in pixels.
22139
22140 *) using the ratio(s) defined in display-pixels-per-inch.
22141
22142 ELEMENT ::=
22143
22144 left-fringe - left fringe width in pixels
22145 right-fringe - right fringe width in pixels
22146
22147 left-margin - left margin width in pixels
22148 right-margin - right margin width in pixels
22149
22150 scroll-bar - scroll-bar area width in pixels
22151
22152 Examples:
22153
22154 Pixels corresponding to 5 inches:
22155 (5 . in)
22156
22157 Total width of non-text areas on left side of window (if scroll-bar is on left):
22158 '(space :width (+ left-fringe left-margin scroll-bar))
22159
22160 Align to first text column (in header line):
22161 '(space :align-to 0)
22162
22163 Align to middle of text area minus half the width of variable `my-image'
22164 containing a loaded image:
22165 '(space :align-to (0.5 . (- text my-image)))
22166
22167 Width of left margin minus width of 1 character in the default font:
22168 '(space :width (- left-margin 1))
22169
22170 Width of left margin minus width of 2 characters in the current font:
22171 '(space :width (- left-margin (2 . width)))
22172
22173 Center 1 character over left-margin (in header line):
22174 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22175
22176 Different ways to express width of left fringe plus left margin minus one pixel:
22177 '(space :width (- (+ left-fringe left-margin) (1)))
22178 '(space :width (+ left-fringe left-margin (- (1))))
22179 '(space :width (+ left-fringe left-margin (-1)))
22180
22181 */
22182
22183 #define NUMVAL(X) \
22184 ((INTEGERP (X) || FLOATP (X)) \
22185 ? XFLOATINT (X) \
22186 : - 1)
22187
22188 static int
22189 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22190 struct font *font, int width_p, int *align_to)
22191 {
22192 double pixels;
22193
22194 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22195 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22196
22197 if (NILP (prop))
22198 return OK_PIXELS (0);
22199
22200 eassert (FRAME_LIVE_P (it->f));
22201
22202 if (SYMBOLP (prop))
22203 {
22204 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22205 {
22206 char *unit = SSDATA (SYMBOL_NAME (prop));
22207
22208 if (unit[0] == 'i' && unit[1] == 'n')
22209 pixels = 1.0;
22210 else if (unit[0] == 'm' && unit[1] == 'm')
22211 pixels = 25.4;
22212 else if (unit[0] == 'c' && unit[1] == 'm')
22213 pixels = 2.54;
22214 else
22215 pixels = 0;
22216 if (pixels > 0)
22217 {
22218 double ppi;
22219 #ifdef HAVE_WINDOW_SYSTEM
22220 if (FRAME_WINDOW_P (it->f)
22221 && (ppi = (width_p
22222 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22223 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22224 ppi > 0))
22225 return OK_PIXELS (ppi / pixels);
22226 #endif
22227
22228 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22229 || (CONSP (Vdisplay_pixels_per_inch)
22230 && (ppi = (width_p
22231 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22232 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22233 ppi > 0)))
22234 return OK_PIXELS (ppi / pixels);
22235
22236 return 0;
22237 }
22238 }
22239
22240 #ifdef HAVE_WINDOW_SYSTEM
22241 if (EQ (prop, Qheight))
22242 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22243 if (EQ (prop, Qwidth))
22244 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22245 #else
22246 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22247 return OK_PIXELS (1);
22248 #endif
22249
22250 if (EQ (prop, Qtext))
22251 return OK_PIXELS (width_p
22252 ? window_box_width (it->w, TEXT_AREA)
22253 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22254
22255 if (align_to && *align_to < 0)
22256 {
22257 *res = 0;
22258 if (EQ (prop, Qleft))
22259 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22260 if (EQ (prop, Qright))
22261 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22262 if (EQ (prop, Qcenter))
22263 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22264 + window_box_width (it->w, TEXT_AREA) / 2);
22265 if (EQ (prop, Qleft_fringe))
22266 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22267 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22268 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22269 if (EQ (prop, Qright_fringe))
22270 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22271 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22272 : window_box_right_offset (it->w, TEXT_AREA));
22273 if (EQ (prop, Qleft_margin))
22274 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22275 if (EQ (prop, Qright_margin))
22276 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22277 if (EQ (prop, Qscroll_bar))
22278 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22279 ? 0
22280 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22281 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22282 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22283 : 0)));
22284 }
22285 else
22286 {
22287 if (EQ (prop, Qleft_fringe))
22288 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22289 if (EQ (prop, Qright_fringe))
22290 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22291 if (EQ (prop, Qleft_margin))
22292 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22293 if (EQ (prop, Qright_margin))
22294 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22295 if (EQ (prop, Qscroll_bar))
22296 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22297 }
22298
22299 prop = buffer_local_value_1 (prop, it->w->buffer);
22300 if (EQ (prop, Qunbound))
22301 prop = Qnil;
22302 }
22303
22304 if (INTEGERP (prop) || FLOATP (prop))
22305 {
22306 int base_unit = (width_p
22307 ? FRAME_COLUMN_WIDTH (it->f)
22308 : FRAME_LINE_HEIGHT (it->f));
22309 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22310 }
22311
22312 if (CONSP (prop))
22313 {
22314 Lisp_Object car = XCAR (prop);
22315 Lisp_Object cdr = XCDR (prop);
22316
22317 if (SYMBOLP (car))
22318 {
22319 #ifdef HAVE_WINDOW_SYSTEM
22320 if (FRAME_WINDOW_P (it->f)
22321 && valid_image_p (prop))
22322 {
22323 ptrdiff_t id = lookup_image (it->f, prop);
22324 struct image *img = IMAGE_FROM_ID (it->f, id);
22325
22326 return OK_PIXELS (width_p ? img->width : img->height);
22327 }
22328 #endif
22329 if (EQ (car, Qplus) || EQ (car, Qminus))
22330 {
22331 int first = 1;
22332 double px;
22333
22334 pixels = 0;
22335 while (CONSP (cdr))
22336 {
22337 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22338 font, width_p, align_to))
22339 return 0;
22340 if (first)
22341 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22342 else
22343 pixels += px;
22344 cdr = XCDR (cdr);
22345 }
22346 if (EQ (car, Qminus))
22347 pixels = -pixels;
22348 return OK_PIXELS (pixels);
22349 }
22350
22351 car = buffer_local_value_1 (car, it->w->buffer);
22352 if (EQ (car, Qunbound))
22353 car = Qnil;
22354 }
22355
22356 if (INTEGERP (car) || FLOATP (car))
22357 {
22358 double fact;
22359 pixels = XFLOATINT (car);
22360 if (NILP (cdr))
22361 return OK_PIXELS (pixels);
22362 if (calc_pixel_width_or_height (&fact, it, cdr,
22363 font, width_p, align_to))
22364 return OK_PIXELS (pixels * fact);
22365 return 0;
22366 }
22367
22368 return 0;
22369 }
22370
22371 return 0;
22372 }
22373
22374 \f
22375 /***********************************************************************
22376 Glyph Display
22377 ***********************************************************************/
22378
22379 #ifdef HAVE_WINDOW_SYSTEM
22380
22381 #ifdef GLYPH_DEBUG
22382
22383 void
22384 dump_glyph_string (struct glyph_string *s)
22385 {
22386 fprintf (stderr, "glyph string\n");
22387 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22388 s->x, s->y, s->width, s->height);
22389 fprintf (stderr, " ybase = %d\n", s->ybase);
22390 fprintf (stderr, " hl = %d\n", s->hl);
22391 fprintf (stderr, " left overhang = %d, right = %d\n",
22392 s->left_overhang, s->right_overhang);
22393 fprintf (stderr, " nchars = %d\n", s->nchars);
22394 fprintf (stderr, " extends to end of line = %d\n",
22395 s->extends_to_end_of_line_p);
22396 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22397 fprintf (stderr, " bg width = %d\n", s->background_width);
22398 }
22399
22400 #endif /* GLYPH_DEBUG */
22401
22402 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22403 of XChar2b structures for S; it can't be allocated in
22404 init_glyph_string because it must be allocated via `alloca'. W
22405 is the window on which S is drawn. ROW and AREA are the glyph row
22406 and area within the row from which S is constructed. START is the
22407 index of the first glyph structure covered by S. HL is a
22408 face-override for drawing S. */
22409
22410 #ifdef HAVE_NTGUI
22411 #define OPTIONAL_HDC(hdc) HDC hdc,
22412 #define DECLARE_HDC(hdc) HDC hdc;
22413 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22414 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22415 #endif
22416
22417 #ifndef OPTIONAL_HDC
22418 #define OPTIONAL_HDC(hdc)
22419 #define DECLARE_HDC(hdc)
22420 #define ALLOCATE_HDC(hdc, f)
22421 #define RELEASE_HDC(hdc, f)
22422 #endif
22423
22424 static void
22425 init_glyph_string (struct glyph_string *s,
22426 OPTIONAL_HDC (hdc)
22427 XChar2b *char2b, struct window *w, struct glyph_row *row,
22428 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22429 {
22430 memset (s, 0, sizeof *s);
22431 s->w = w;
22432 s->f = XFRAME (w->frame);
22433 #ifdef HAVE_NTGUI
22434 s->hdc = hdc;
22435 #endif
22436 s->display = FRAME_X_DISPLAY (s->f);
22437 s->window = FRAME_X_WINDOW (s->f);
22438 s->char2b = char2b;
22439 s->hl = hl;
22440 s->row = row;
22441 s->area = area;
22442 s->first_glyph = row->glyphs[area] + start;
22443 s->height = row->height;
22444 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22445 s->ybase = s->y + row->ascent;
22446 }
22447
22448
22449 /* Append the list of glyph strings with head H and tail T to the list
22450 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22451
22452 static inline void
22453 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22454 struct glyph_string *h, struct glyph_string *t)
22455 {
22456 if (h)
22457 {
22458 if (*head)
22459 (*tail)->next = h;
22460 else
22461 *head = h;
22462 h->prev = *tail;
22463 *tail = t;
22464 }
22465 }
22466
22467
22468 /* Prepend the list of glyph strings with head H and tail T to the
22469 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22470 result. */
22471
22472 static inline void
22473 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22474 struct glyph_string *h, struct glyph_string *t)
22475 {
22476 if (h)
22477 {
22478 if (*head)
22479 (*head)->prev = t;
22480 else
22481 *tail = t;
22482 t->next = *head;
22483 *head = h;
22484 }
22485 }
22486
22487
22488 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22489 Set *HEAD and *TAIL to the resulting list. */
22490
22491 static inline void
22492 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22493 struct glyph_string *s)
22494 {
22495 s->next = s->prev = NULL;
22496 append_glyph_string_lists (head, tail, s, s);
22497 }
22498
22499
22500 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22501 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22502 make sure that X resources for the face returned are allocated.
22503 Value is a pointer to a realized face that is ready for display if
22504 DISPLAY_P is non-zero. */
22505
22506 static inline struct face *
22507 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22508 XChar2b *char2b, int display_p)
22509 {
22510 struct face *face = FACE_FROM_ID (f, face_id);
22511
22512 if (face->font)
22513 {
22514 unsigned code = face->font->driver->encode_char (face->font, c);
22515
22516 if (code != FONT_INVALID_CODE)
22517 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22518 else
22519 STORE_XCHAR2B (char2b, 0, 0);
22520 }
22521
22522 /* Make sure X resources of the face are allocated. */
22523 #ifdef HAVE_X_WINDOWS
22524 if (display_p)
22525 #endif
22526 {
22527 eassert (face != NULL);
22528 PREPARE_FACE_FOR_DISPLAY (f, face);
22529 }
22530
22531 return face;
22532 }
22533
22534
22535 /* Get face and two-byte form of character glyph GLYPH on frame F.
22536 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22537 a pointer to a realized face that is ready for display. */
22538
22539 static inline struct face *
22540 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22541 XChar2b *char2b, int *two_byte_p)
22542 {
22543 struct face *face;
22544
22545 eassert (glyph->type == CHAR_GLYPH);
22546 face = FACE_FROM_ID (f, glyph->face_id);
22547
22548 if (two_byte_p)
22549 *two_byte_p = 0;
22550
22551 if (face->font)
22552 {
22553 unsigned code;
22554
22555 if (CHAR_BYTE8_P (glyph->u.ch))
22556 code = CHAR_TO_BYTE8 (glyph->u.ch);
22557 else
22558 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22559
22560 if (code != FONT_INVALID_CODE)
22561 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22562 else
22563 STORE_XCHAR2B (char2b, 0, 0);
22564 }
22565
22566 /* Make sure X resources of the face are allocated. */
22567 eassert (face != NULL);
22568 PREPARE_FACE_FOR_DISPLAY (f, face);
22569 return face;
22570 }
22571
22572
22573 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22574 Return 1 if FONT has a glyph for C, otherwise return 0. */
22575
22576 static inline int
22577 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22578 {
22579 unsigned code;
22580
22581 if (CHAR_BYTE8_P (c))
22582 code = CHAR_TO_BYTE8 (c);
22583 else
22584 code = font->driver->encode_char (font, c);
22585
22586 if (code == FONT_INVALID_CODE)
22587 return 0;
22588 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22589 return 1;
22590 }
22591
22592
22593 /* Fill glyph string S with composition components specified by S->cmp.
22594
22595 BASE_FACE is the base face of the composition.
22596 S->cmp_from is the index of the first component for S.
22597
22598 OVERLAPS non-zero means S should draw the foreground only, and use
22599 its physical height for clipping. See also draw_glyphs.
22600
22601 Value is the index of a component not in S. */
22602
22603 static int
22604 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22605 int overlaps)
22606 {
22607 int i;
22608 /* For all glyphs of this composition, starting at the offset
22609 S->cmp_from, until we reach the end of the definition or encounter a
22610 glyph that requires the different face, add it to S. */
22611 struct face *face;
22612
22613 eassert (s);
22614
22615 s->for_overlaps = overlaps;
22616 s->face = NULL;
22617 s->font = NULL;
22618 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22619 {
22620 int c = COMPOSITION_GLYPH (s->cmp, i);
22621
22622 /* TAB in a composition means display glyphs with padding space
22623 on the left or right. */
22624 if (c != '\t')
22625 {
22626 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22627 -1, Qnil);
22628
22629 face = get_char_face_and_encoding (s->f, c, face_id,
22630 s->char2b + i, 1);
22631 if (face)
22632 {
22633 if (! s->face)
22634 {
22635 s->face = face;
22636 s->font = s->face->font;
22637 }
22638 else if (s->face != face)
22639 break;
22640 }
22641 }
22642 ++s->nchars;
22643 }
22644 s->cmp_to = i;
22645
22646 if (s->face == NULL)
22647 {
22648 s->face = base_face->ascii_face;
22649 s->font = s->face->font;
22650 }
22651
22652 /* All glyph strings for the same composition has the same width,
22653 i.e. the width set for the first component of the composition. */
22654 s->width = s->first_glyph->pixel_width;
22655
22656 /* If the specified font could not be loaded, use the frame's
22657 default font, but record the fact that we couldn't load it in
22658 the glyph string so that we can draw rectangles for the
22659 characters of the glyph string. */
22660 if (s->font == NULL)
22661 {
22662 s->font_not_found_p = 1;
22663 s->font = FRAME_FONT (s->f);
22664 }
22665
22666 /* Adjust base line for subscript/superscript text. */
22667 s->ybase += s->first_glyph->voffset;
22668
22669 /* This glyph string must always be drawn with 16-bit functions. */
22670 s->two_byte_p = 1;
22671
22672 return s->cmp_to;
22673 }
22674
22675 static int
22676 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22677 int start, int end, int overlaps)
22678 {
22679 struct glyph *glyph, *last;
22680 Lisp_Object lgstring;
22681 int i;
22682
22683 s->for_overlaps = overlaps;
22684 glyph = s->row->glyphs[s->area] + start;
22685 last = s->row->glyphs[s->area] + end;
22686 s->cmp_id = glyph->u.cmp.id;
22687 s->cmp_from = glyph->slice.cmp.from;
22688 s->cmp_to = glyph->slice.cmp.to + 1;
22689 s->face = FACE_FROM_ID (s->f, face_id);
22690 lgstring = composition_gstring_from_id (s->cmp_id);
22691 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22692 glyph++;
22693 while (glyph < last
22694 && glyph->u.cmp.automatic
22695 && glyph->u.cmp.id == s->cmp_id
22696 && s->cmp_to == glyph->slice.cmp.from)
22697 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22698
22699 for (i = s->cmp_from; i < s->cmp_to; i++)
22700 {
22701 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22702 unsigned code = LGLYPH_CODE (lglyph);
22703
22704 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22705 }
22706 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22707 return glyph - s->row->glyphs[s->area];
22708 }
22709
22710
22711 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22712 See the comment of fill_glyph_string for arguments.
22713 Value is the index of the first glyph not in S. */
22714
22715
22716 static int
22717 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22718 int start, int end, int overlaps)
22719 {
22720 struct glyph *glyph, *last;
22721 int voffset;
22722
22723 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22724 s->for_overlaps = overlaps;
22725 glyph = s->row->glyphs[s->area] + start;
22726 last = s->row->glyphs[s->area] + end;
22727 voffset = glyph->voffset;
22728 s->face = FACE_FROM_ID (s->f, face_id);
22729 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22730 s->nchars = 1;
22731 s->width = glyph->pixel_width;
22732 glyph++;
22733 while (glyph < last
22734 && glyph->type == GLYPHLESS_GLYPH
22735 && glyph->voffset == voffset
22736 && glyph->face_id == face_id)
22737 {
22738 s->nchars++;
22739 s->width += glyph->pixel_width;
22740 glyph++;
22741 }
22742 s->ybase += voffset;
22743 return glyph - s->row->glyphs[s->area];
22744 }
22745
22746
22747 /* Fill glyph string S from a sequence of character glyphs.
22748
22749 FACE_ID is the face id of the string. START is the index of the
22750 first glyph to consider, END is the index of the last + 1.
22751 OVERLAPS non-zero means S should draw the foreground only, and use
22752 its physical height for clipping. See also draw_glyphs.
22753
22754 Value is the index of the first glyph not in S. */
22755
22756 static int
22757 fill_glyph_string (struct glyph_string *s, int face_id,
22758 int start, int end, int overlaps)
22759 {
22760 struct glyph *glyph, *last;
22761 int voffset;
22762 int glyph_not_available_p;
22763
22764 eassert (s->f == XFRAME (s->w->frame));
22765 eassert (s->nchars == 0);
22766 eassert (start >= 0 && end > start);
22767
22768 s->for_overlaps = overlaps;
22769 glyph = s->row->glyphs[s->area] + start;
22770 last = s->row->glyphs[s->area] + end;
22771 voffset = glyph->voffset;
22772 s->padding_p = glyph->padding_p;
22773 glyph_not_available_p = glyph->glyph_not_available_p;
22774
22775 while (glyph < last
22776 && glyph->type == CHAR_GLYPH
22777 && glyph->voffset == voffset
22778 /* Same face id implies same font, nowadays. */
22779 && glyph->face_id == face_id
22780 && glyph->glyph_not_available_p == glyph_not_available_p)
22781 {
22782 int two_byte_p;
22783
22784 s->face = get_glyph_face_and_encoding (s->f, glyph,
22785 s->char2b + s->nchars,
22786 &two_byte_p);
22787 s->two_byte_p = two_byte_p;
22788 ++s->nchars;
22789 eassert (s->nchars <= end - start);
22790 s->width += glyph->pixel_width;
22791 if (glyph++->padding_p != s->padding_p)
22792 break;
22793 }
22794
22795 s->font = s->face->font;
22796
22797 /* If the specified font could not be loaded, use the frame's font,
22798 but record the fact that we couldn't load it in
22799 S->font_not_found_p so that we can draw rectangles for the
22800 characters of the glyph string. */
22801 if (s->font == NULL || glyph_not_available_p)
22802 {
22803 s->font_not_found_p = 1;
22804 s->font = FRAME_FONT (s->f);
22805 }
22806
22807 /* Adjust base line for subscript/superscript text. */
22808 s->ybase += voffset;
22809
22810 eassert (s->face && s->face->gc);
22811 return glyph - s->row->glyphs[s->area];
22812 }
22813
22814
22815 /* Fill glyph string S from image glyph S->first_glyph. */
22816
22817 static void
22818 fill_image_glyph_string (struct glyph_string *s)
22819 {
22820 eassert (s->first_glyph->type == IMAGE_GLYPH);
22821 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22822 eassert (s->img);
22823 s->slice = s->first_glyph->slice.img;
22824 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22825 s->font = s->face->font;
22826 s->width = s->first_glyph->pixel_width;
22827
22828 /* Adjust base line for subscript/superscript text. */
22829 s->ybase += s->first_glyph->voffset;
22830 }
22831
22832
22833 /* Fill glyph string S from a sequence of stretch glyphs.
22834
22835 START is the index of the first glyph to consider,
22836 END is the index of the last + 1.
22837
22838 Value is the index of the first glyph not in S. */
22839
22840 static int
22841 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22842 {
22843 struct glyph *glyph, *last;
22844 int voffset, face_id;
22845
22846 eassert (s->first_glyph->type == STRETCH_GLYPH);
22847
22848 glyph = s->row->glyphs[s->area] + start;
22849 last = s->row->glyphs[s->area] + end;
22850 face_id = glyph->face_id;
22851 s->face = FACE_FROM_ID (s->f, face_id);
22852 s->font = s->face->font;
22853 s->width = glyph->pixel_width;
22854 s->nchars = 1;
22855 voffset = glyph->voffset;
22856
22857 for (++glyph;
22858 (glyph < last
22859 && glyph->type == STRETCH_GLYPH
22860 && glyph->voffset == voffset
22861 && glyph->face_id == face_id);
22862 ++glyph)
22863 s->width += glyph->pixel_width;
22864
22865 /* Adjust base line for subscript/superscript text. */
22866 s->ybase += voffset;
22867
22868 /* The case that face->gc == 0 is handled when drawing the glyph
22869 string by calling PREPARE_FACE_FOR_DISPLAY. */
22870 eassert (s->face);
22871 return glyph - s->row->glyphs[s->area];
22872 }
22873
22874 static struct font_metrics *
22875 get_per_char_metric (struct font *font, XChar2b *char2b)
22876 {
22877 static struct font_metrics metrics;
22878 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22879
22880 if (! font || code == FONT_INVALID_CODE)
22881 return NULL;
22882 font->driver->text_extents (font, &code, 1, &metrics);
22883 return &metrics;
22884 }
22885
22886 /* EXPORT for RIF:
22887 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22888 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22889 assumed to be zero. */
22890
22891 void
22892 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22893 {
22894 *left = *right = 0;
22895
22896 if (glyph->type == CHAR_GLYPH)
22897 {
22898 struct face *face;
22899 XChar2b char2b;
22900 struct font_metrics *pcm;
22901
22902 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22903 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22904 {
22905 if (pcm->rbearing > pcm->width)
22906 *right = pcm->rbearing - pcm->width;
22907 if (pcm->lbearing < 0)
22908 *left = -pcm->lbearing;
22909 }
22910 }
22911 else if (glyph->type == COMPOSITE_GLYPH)
22912 {
22913 if (! glyph->u.cmp.automatic)
22914 {
22915 struct composition *cmp = composition_table[glyph->u.cmp.id];
22916
22917 if (cmp->rbearing > cmp->pixel_width)
22918 *right = cmp->rbearing - cmp->pixel_width;
22919 if (cmp->lbearing < 0)
22920 *left = - cmp->lbearing;
22921 }
22922 else
22923 {
22924 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22925 struct font_metrics metrics;
22926
22927 composition_gstring_width (gstring, glyph->slice.cmp.from,
22928 glyph->slice.cmp.to + 1, &metrics);
22929 if (metrics.rbearing > metrics.width)
22930 *right = metrics.rbearing - metrics.width;
22931 if (metrics.lbearing < 0)
22932 *left = - metrics.lbearing;
22933 }
22934 }
22935 }
22936
22937
22938 /* Return the index of the first glyph preceding glyph string S that
22939 is overwritten by S because of S's left overhang. Value is -1
22940 if no glyphs are overwritten. */
22941
22942 static int
22943 left_overwritten (struct glyph_string *s)
22944 {
22945 int k;
22946
22947 if (s->left_overhang)
22948 {
22949 int x = 0, i;
22950 struct glyph *glyphs = s->row->glyphs[s->area];
22951 int first = s->first_glyph - glyphs;
22952
22953 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22954 x -= glyphs[i].pixel_width;
22955
22956 k = i + 1;
22957 }
22958 else
22959 k = -1;
22960
22961 return k;
22962 }
22963
22964
22965 /* Return the index of the first glyph preceding glyph string S that
22966 is overwriting S because of its right overhang. Value is -1 if no
22967 glyph in front of S overwrites S. */
22968
22969 static int
22970 left_overwriting (struct glyph_string *s)
22971 {
22972 int i, k, x;
22973 struct glyph *glyphs = s->row->glyphs[s->area];
22974 int first = s->first_glyph - glyphs;
22975
22976 k = -1;
22977 x = 0;
22978 for (i = first - 1; i >= 0; --i)
22979 {
22980 int left, right;
22981 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22982 if (x + right > 0)
22983 k = i;
22984 x -= glyphs[i].pixel_width;
22985 }
22986
22987 return k;
22988 }
22989
22990
22991 /* Return the index of the last glyph following glyph string S that is
22992 overwritten by S because of S's right overhang. Value is -1 if
22993 no such glyph is found. */
22994
22995 static int
22996 right_overwritten (struct glyph_string *s)
22997 {
22998 int k = -1;
22999
23000 if (s->right_overhang)
23001 {
23002 int x = 0, i;
23003 struct glyph *glyphs = s->row->glyphs[s->area];
23004 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23005 int end = s->row->used[s->area];
23006
23007 for (i = first; i < end && s->right_overhang > x; ++i)
23008 x += glyphs[i].pixel_width;
23009
23010 k = i;
23011 }
23012
23013 return k;
23014 }
23015
23016
23017 /* Return the index of the last glyph following glyph string S that
23018 overwrites S because of its left overhang. Value is negative
23019 if no such glyph is found. */
23020
23021 static int
23022 right_overwriting (struct glyph_string *s)
23023 {
23024 int i, k, x;
23025 int end = s->row->used[s->area];
23026 struct glyph *glyphs = s->row->glyphs[s->area];
23027 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23028
23029 k = -1;
23030 x = 0;
23031 for (i = first; i < end; ++i)
23032 {
23033 int left, right;
23034 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23035 if (x - left < 0)
23036 k = i;
23037 x += glyphs[i].pixel_width;
23038 }
23039
23040 return k;
23041 }
23042
23043
23044 /* Set background width of glyph string S. START is the index of the
23045 first glyph following S. LAST_X is the right-most x-position + 1
23046 in the drawing area. */
23047
23048 static inline void
23049 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23050 {
23051 /* If the face of this glyph string has to be drawn to the end of
23052 the drawing area, set S->extends_to_end_of_line_p. */
23053
23054 if (start == s->row->used[s->area]
23055 && s->area == TEXT_AREA
23056 && ((s->row->fill_line_p
23057 && (s->hl == DRAW_NORMAL_TEXT
23058 || s->hl == DRAW_IMAGE_RAISED
23059 || s->hl == DRAW_IMAGE_SUNKEN))
23060 || s->hl == DRAW_MOUSE_FACE))
23061 s->extends_to_end_of_line_p = 1;
23062
23063 /* If S extends its face to the end of the line, set its
23064 background_width to the distance to the right edge of the drawing
23065 area. */
23066 if (s->extends_to_end_of_line_p)
23067 s->background_width = last_x - s->x + 1;
23068 else
23069 s->background_width = s->width;
23070 }
23071
23072
23073 /* Compute overhangs and x-positions for glyph string S and its
23074 predecessors, or successors. X is the starting x-position for S.
23075 BACKWARD_P non-zero means process predecessors. */
23076
23077 static void
23078 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23079 {
23080 if (backward_p)
23081 {
23082 while (s)
23083 {
23084 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23085 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23086 x -= s->width;
23087 s->x = x;
23088 s = s->prev;
23089 }
23090 }
23091 else
23092 {
23093 while (s)
23094 {
23095 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23096 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23097 s->x = x;
23098 x += s->width;
23099 s = s->next;
23100 }
23101 }
23102 }
23103
23104
23105
23106 /* The following macros are only called from draw_glyphs below.
23107 They reference the following parameters of that function directly:
23108 `w', `row', `area', and `overlap_p'
23109 as well as the following local variables:
23110 `s', `f', and `hdc' (in W32) */
23111
23112 #ifdef HAVE_NTGUI
23113 /* On W32, silently add local `hdc' variable to argument list of
23114 init_glyph_string. */
23115 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23116 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23117 #else
23118 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23119 init_glyph_string (s, char2b, w, row, area, start, hl)
23120 #endif
23121
23122 /* Add a glyph string for a stretch glyph to the list of strings
23123 between HEAD and TAIL. START is the index of the stretch glyph in
23124 row area AREA of glyph row ROW. END is the index of the last glyph
23125 in that glyph row area. X is the current output position assigned
23126 to the new glyph string constructed. HL overrides that face of the
23127 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23128 is the right-most x-position of the drawing area. */
23129
23130 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23131 and below -- keep them on one line. */
23132 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23133 do \
23134 { \
23135 s = alloca (sizeof *s); \
23136 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23137 START = fill_stretch_glyph_string (s, START, END); \
23138 append_glyph_string (&HEAD, &TAIL, s); \
23139 s->x = (X); \
23140 } \
23141 while (0)
23142
23143
23144 /* Add a glyph string for an image glyph to the list of strings
23145 between HEAD and TAIL. START is the index of the image glyph in
23146 row area AREA of glyph row ROW. END is the index of the last glyph
23147 in that glyph row area. X is the current output position assigned
23148 to the new glyph string constructed. HL overrides that face of the
23149 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23150 is the right-most x-position of the drawing area. */
23151
23152 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23153 do \
23154 { \
23155 s = alloca (sizeof *s); \
23156 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23157 fill_image_glyph_string (s); \
23158 append_glyph_string (&HEAD, &TAIL, s); \
23159 ++START; \
23160 s->x = (X); \
23161 } \
23162 while (0)
23163
23164
23165 /* Add a glyph string for a sequence of character glyphs to the list
23166 of strings between HEAD and TAIL. START is the index of the first
23167 glyph in row area AREA of glyph row ROW that is part of the new
23168 glyph string. END is the index of the last glyph in that glyph row
23169 area. X is the current output position assigned to the new glyph
23170 string constructed. HL overrides that face of the glyph; e.g. it
23171 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23172 right-most x-position of the drawing area. */
23173
23174 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23175 do \
23176 { \
23177 int face_id; \
23178 XChar2b *char2b; \
23179 \
23180 face_id = (row)->glyphs[area][START].face_id; \
23181 \
23182 s = alloca (sizeof *s); \
23183 char2b = alloca ((END - START) * sizeof *char2b); \
23184 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23185 append_glyph_string (&HEAD, &TAIL, s); \
23186 s->x = (X); \
23187 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23188 } \
23189 while (0)
23190
23191
23192 /* Add a glyph string for a composite sequence to the list of strings
23193 between HEAD and TAIL. START is the index of the first glyph in
23194 row area AREA of glyph row ROW that is part of the new glyph
23195 string. END is the index of the last glyph in that glyph row area.
23196 X is the current output position assigned to the new glyph string
23197 constructed. HL overrides that face of the glyph; e.g. it is
23198 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23199 x-position of the drawing area. */
23200
23201 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23202 do { \
23203 int face_id = (row)->glyphs[area][START].face_id; \
23204 struct face *base_face = FACE_FROM_ID (f, face_id); \
23205 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23206 struct composition *cmp = composition_table[cmp_id]; \
23207 XChar2b *char2b; \
23208 struct glyph_string *first_s = NULL; \
23209 int n; \
23210 \
23211 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23212 \
23213 /* Make glyph_strings for each glyph sequence that is drawable by \
23214 the same face, and append them to HEAD/TAIL. */ \
23215 for (n = 0; n < cmp->glyph_len;) \
23216 { \
23217 s = alloca (sizeof *s); \
23218 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23219 append_glyph_string (&(HEAD), &(TAIL), s); \
23220 s->cmp = cmp; \
23221 s->cmp_from = n; \
23222 s->x = (X); \
23223 if (n == 0) \
23224 first_s = s; \
23225 n = fill_composite_glyph_string (s, base_face, overlaps); \
23226 } \
23227 \
23228 ++START; \
23229 s = first_s; \
23230 } while (0)
23231
23232
23233 /* Add a glyph string for a glyph-string sequence to the list of strings
23234 between HEAD and TAIL. */
23235
23236 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23237 do { \
23238 int face_id; \
23239 XChar2b *char2b; \
23240 Lisp_Object gstring; \
23241 \
23242 face_id = (row)->glyphs[area][START].face_id; \
23243 gstring = (composition_gstring_from_id \
23244 ((row)->glyphs[area][START].u.cmp.id)); \
23245 s = alloca (sizeof *s); \
23246 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23247 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23248 append_glyph_string (&(HEAD), &(TAIL), s); \
23249 s->x = (X); \
23250 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23251 } while (0)
23252
23253
23254 /* Add a glyph string for a sequence of glyphless character's glyphs
23255 to the list of strings between HEAD and TAIL. The meanings of
23256 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23257
23258 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23259 do \
23260 { \
23261 int face_id; \
23262 \
23263 face_id = (row)->glyphs[area][START].face_id; \
23264 \
23265 s = alloca (sizeof *s); \
23266 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23267 append_glyph_string (&HEAD, &TAIL, s); \
23268 s->x = (X); \
23269 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23270 overlaps); \
23271 } \
23272 while (0)
23273
23274
23275 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23276 of AREA of glyph row ROW on window W between indices START and END.
23277 HL overrides the face for drawing glyph strings, e.g. it is
23278 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23279 x-positions of the drawing area.
23280
23281 This is an ugly monster macro construct because we must use alloca
23282 to allocate glyph strings (because draw_glyphs can be called
23283 asynchronously). */
23284
23285 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23286 do \
23287 { \
23288 HEAD = TAIL = NULL; \
23289 while (START < END) \
23290 { \
23291 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23292 switch (first_glyph->type) \
23293 { \
23294 case CHAR_GLYPH: \
23295 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23296 HL, X, LAST_X); \
23297 break; \
23298 \
23299 case COMPOSITE_GLYPH: \
23300 if (first_glyph->u.cmp.automatic) \
23301 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23302 HL, X, LAST_X); \
23303 else \
23304 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23305 HL, X, LAST_X); \
23306 break; \
23307 \
23308 case STRETCH_GLYPH: \
23309 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23310 HL, X, LAST_X); \
23311 break; \
23312 \
23313 case IMAGE_GLYPH: \
23314 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23315 HL, X, LAST_X); \
23316 break; \
23317 \
23318 case GLYPHLESS_GLYPH: \
23319 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23320 HL, X, LAST_X); \
23321 break; \
23322 \
23323 default: \
23324 abort (); \
23325 } \
23326 \
23327 if (s) \
23328 { \
23329 set_glyph_string_background_width (s, START, LAST_X); \
23330 (X) += s->width; \
23331 } \
23332 } \
23333 } while (0)
23334
23335
23336 /* Draw glyphs between START and END in AREA of ROW on window W,
23337 starting at x-position X. X is relative to AREA in W. HL is a
23338 face-override with the following meaning:
23339
23340 DRAW_NORMAL_TEXT draw normally
23341 DRAW_CURSOR draw in cursor face
23342 DRAW_MOUSE_FACE draw in mouse face.
23343 DRAW_INVERSE_VIDEO draw in mode line face
23344 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23345 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23346
23347 If OVERLAPS is non-zero, draw only the foreground of characters and
23348 clip to the physical height of ROW. Non-zero value also defines
23349 the overlapping part to be drawn:
23350
23351 OVERLAPS_PRED overlap with preceding rows
23352 OVERLAPS_SUCC overlap with succeeding rows
23353 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23354 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23355
23356 Value is the x-position reached, relative to AREA of W. */
23357
23358 static int
23359 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23360 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23361 enum draw_glyphs_face hl, int overlaps)
23362 {
23363 struct glyph_string *head, *tail;
23364 struct glyph_string *s;
23365 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23366 int i, j, x_reached, last_x, area_left = 0;
23367 struct frame *f = XFRAME (WINDOW_FRAME (w));
23368 DECLARE_HDC (hdc);
23369
23370 ALLOCATE_HDC (hdc, f);
23371
23372 /* Let's rather be paranoid than getting a SEGV. */
23373 end = min (end, row->used[area]);
23374 start = max (0, start);
23375 start = min (end, start);
23376
23377 /* Translate X to frame coordinates. Set last_x to the right
23378 end of the drawing area. */
23379 if (row->full_width_p)
23380 {
23381 /* X is relative to the left edge of W, without scroll bars
23382 or fringes. */
23383 area_left = WINDOW_LEFT_EDGE_X (w);
23384 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23385 }
23386 else
23387 {
23388 area_left = window_box_left (w, area);
23389 last_x = area_left + window_box_width (w, area);
23390 }
23391 x += area_left;
23392
23393 /* Build a doubly-linked list of glyph_string structures between
23394 head and tail from what we have to draw. Note that the macro
23395 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23396 the reason we use a separate variable `i'. */
23397 i = start;
23398 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23399 if (tail)
23400 x_reached = tail->x + tail->background_width;
23401 else
23402 x_reached = x;
23403
23404 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23405 the row, redraw some glyphs in front or following the glyph
23406 strings built above. */
23407 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23408 {
23409 struct glyph_string *h, *t;
23410 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23411 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23412 int check_mouse_face = 0;
23413 int dummy_x = 0;
23414
23415 /* If mouse highlighting is on, we may need to draw adjacent
23416 glyphs using mouse-face highlighting. */
23417 if (area == TEXT_AREA && row->mouse_face_p)
23418 {
23419 struct glyph_row *mouse_beg_row, *mouse_end_row;
23420
23421 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23422 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23423
23424 if (row >= mouse_beg_row && row <= mouse_end_row)
23425 {
23426 check_mouse_face = 1;
23427 mouse_beg_col = (row == mouse_beg_row)
23428 ? hlinfo->mouse_face_beg_col : 0;
23429 mouse_end_col = (row == mouse_end_row)
23430 ? hlinfo->mouse_face_end_col
23431 : row->used[TEXT_AREA];
23432 }
23433 }
23434
23435 /* Compute overhangs for all glyph strings. */
23436 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23437 for (s = head; s; s = s->next)
23438 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23439
23440 /* Prepend glyph strings for glyphs in front of the first glyph
23441 string that are overwritten because of the first glyph
23442 string's left overhang. The background of all strings
23443 prepended must be drawn because the first glyph string
23444 draws over it. */
23445 i = left_overwritten (head);
23446 if (i >= 0)
23447 {
23448 enum draw_glyphs_face overlap_hl;
23449
23450 /* If this row contains mouse highlighting, attempt to draw
23451 the overlapped glyphs with the correct highlight. This
23452 code fails if the overlap encompasses more than one glyph
23453 and mouse-highlight spans only some of these glyphs.
23454 However, making it work perfectly involves a lot more
23455 code, and I don't know if the pathological case occurs in
23456 practice, so we'll stick to this for now. --- cyd */
23457 if (check_mouse_face
23458 && mouse_beg_col < start && mouse_end_col > i)
23459 overlap_hl = DRAW_MOUSE_FACE;
23460 else
23461 overlap_hl = DRAW_NORMAL_TEXT;
23462
23463 j = i;
23464 BUILD_GLYPH_STRINGS (j, start, h, t,
23465 overlap_hl, dummy_x, last_x);
23466 start = i;
23467 compute_overhangs_and_x (t, head->x, 1);
23468 prepend_glyph_string_lists (&head, &tail, h, t);
23469 clip_head = head;
23470 }
23471
23472 /* Prepend glyph strings for glyphs in front of the first glyph
23473 string that overwrite that glyph string because of their
23474 right overhang. For these strings, only the foreground must
23475 be drawn, because it draws over the glyph string at `head'.
23476 The background must not be drawn because this would overwrite
23477 right overhangs of preceding glyphs for which no glyph
23478 strings exist. */
23479 i = left_overwriting (head);
23480 if (i >= 0)
23481 {
23482 enum draw_glyphs_face overlap_hl;
23483
23484 if (check_mouse_face
23485 && mouse_beg_col < start && mouse_end_col > i)
23486 overlap_hl = DRAW_MOUSE_FACE;
23487 else
23488 overlap_hl = DRAW_NORMAL_TEXT;
23489
23490 clip_head = head;
23491 BUILD_GLYPH_STRINGS (i, start, h, t,
23492 overlap_hl, dummy_x, last_x);
23493 for (s = h; s; s = s->next)
23494 s->background_filled_p = 1;
23495 compute_overhangs_and_x (t, head->x, 1);
23496 prepend_glyph_string_lists (&head, &tail, h, t);
23497 }
23498
23499 /* Append glyphs strings for glyphs following the last glyph
23500 string tail that are overwritten by tail. The background of
23501 these strings has to be drawn because tail's foreground draws
23502 over it. */
23503 i = right_overwritten (tail);
23504 if (i >= 0)
23505 {
23506 enum draw_glyphs_face overlap_hl;
23507
23508 if (check_mouse_face
23509 && mouse_beg_col < i && mouse_end_col > end)
23510 overlap_hl = DRAW_MOUSE_FACE;
23511 else
23512 overlap_hl = DRAW_NORMAL_TEXT;
23513
23514 BUILD_GLYPH_STRINGS (end, i, h, t,
23515 overlap_hl, x, last_x);
23516 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23517 we don't have `end = i;' here. */
23518 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23519 append_glyph_string_lists (&head, &tail, h, t);
23520 clip_tail = tail;
23521 }
23522
23523 /* Append glyph strings for glyphs following the last glyph
23524 string tail that overwrite tail. The foreground of such
23525 glyphs has to be drawn because it writes into the background
23526 of tail. The background must not be drawn because it could
23527 paint over the foreground of following glyphs. */
23528 i = right_overwriting (tail);
23529 if (i >= 0)
23530 {
23531 enum draw_glyphs_face overlap_hl;
23532 if (check_mouse_face
23533 && mouse_beg_col < i && mouse_end_col > end)
23534 overlap_hl = DRAW_MOUSE_FACE;
23535 else
23536 overlap_hl = DRAW_NORMAL_TEXT;
23537
23538 clip_tail = tail;
23539 i++; /* We must include the Ith glyph. */
23540 BUILD_GLYPH_STRINGS (end, i, h, t,
23541 overlap_hl, x, last_x);
23542 for (s = h; s; s = s->next)
23543 s->background_filled_p = 1;
23544 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23545 append_glyph_string_lists (&head, &tail, h, t);
23546 }
23547 if (clip_head || clip_tail)
23548 for (s = head; s; s = s->next)
23549 {
23550 s->clip_head = clip_head;
23551 s->clip_tail = clip_tail;
23552 }
23553 }
23554
23555 /* Draw all strings. */
23556 for (s = head; s; s = s->next)
23557 FRAME_RIF (f)->draw_glyph_string (s);
23558
23559 #ifndef HAVE_NS
23560 /* When focus a sole frame and move horizontally, this sets on_p to 0
23561 causing a failure to erase prev cursor position. */
23562 if (area == TEXT_AREA
23563 && !row->full_width_p
23564 /* When drawing overlapping rows, only the glyph strings'
23565 foreground is drawn, which doesn't erase a cursor
23566 completely. */
23567 && !overlaps)
23568 {
23569 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23570 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23571 : (tail ? tail->x + tail->background_width : x));
23572 x0 -= area_left;
23573 x1 -= area_left;
23574
23575 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23576 row->y, MATRIX_ROW_BOTTOM_Y (row));
23577 }
23578 #endif
23579
23580 /* Value is the x-position up to which drawn, relative to AREA of W.
23581 This doesn't include parts drawn because of overhangs. */
23582 if (row->full_width_p)
23583 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23584 else
23585 x_reached -= area_left;
23586
23587 RELEASE_HDC (hdc, f);
23588
23589 return x_reached;
23590 }
23591
23592 /* Expand row matrix if too narrow. Don't expand if area
23593 is not present. */
23594
23595 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23596 { \
23597 if (!fonts_changed_p \
23598 && (it->glyph_row->glyphs[area] \
23599 < it->glyph_row->glyphs[area + 1])) \
23600 { \
23601 it->w->ncols_scale_factor++; \
23602 fonts_changed_p = 1; \
23603 } \
23604 }
23605
23606 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23607 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23608
23609 static inline void
23610 append_glyph (struct it *it)
23611 {
23612 struct glyph *glyph;
23613 enum glyph_row_area area = it->area;
23614
23615 eassert (it->glyph_row);
23616 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23617
23618 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23619 if (glyph < it->glyph_row->glyphs[area + 1])
23620 {
23621 /* If the glyph row is reversed, we need to prepend the glyph
23622 rather than append it. */
23623 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23624 {
23625 struct glyph *g;
23626
23627 /* Make room for the additional glyph. */
23628 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23629 g[1] = *g;
23630 glyph = it->glyph_row->glyphs[area];
23631 }
23632 glyph->charpos = CHARPOS (it->position);
23633 glyph->object = it->object;
23634 if (it->pixel_width > 0)
23635 {
23636 glyph->pixel_width = it->pixel_width;
23637 glyph->padding_p = 0;
23638 }
23639 else
23640 {
23641 /* Assure at least 1-pixel width. Otherwise, cursor can't
23642 be displayed correctly. */
23643 glyph->pixel_width = 1;
23644 glyph->padding_p = 1;
23645 }
23646 glyph->ascent = it->ascent;
23647 glyph->descent = it->descent;
23648 glyph->voffset = it->voffset;
23649 glyph->type = CHAR_GLYPH;
23650 glyph->avoid_cursor_p = it->avoid_cursor_p;
23651 glyph->multibyte_p = it->multibyte_p;
23652 glyph->left_box_line_p = it->start_of_box_run_p;
23653 glyph->right_box_line_p = it->end_of_box_run_p;
23654 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23655 || it->phys_descent > it->descent);
23656 glyph->glyph_not_available_p = it->glyph_not_available_p;
23657 glyph->face_id = it->face_id;
23658 glyph->u.ch = it->char_to_display;
23659 glyph->slice.img = null_glyph_slice;
23660 glyph->font_type = FONT_TYPE_UNKNOWN;
23661 if (it->bidi_p)
23662 {
23663 glyph->resolved_level = it->bidi_it.resolved_level;
23664 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23665 abort ();
23666 glyph->bidi_type = it->bidi_it.type;
23667 }
23668 else
23669 {
23670 glyph->resolved_level = 0;
23671 glyph->bidi_type = UNKNOWN_BT;
23672 }
23673 ++it->glyph_row->used[area];
23674 }
23675 else
23676 IT_EXPAND_MATRIX_WIDTH (it, area);
23677 }
23678
23679 /* Store one glyph for the composition IT->cmp_it.id in
23680 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23681 non-null. */
23682
23683 static inline void
23684 append_composite_glyph (struct it *it)
23685 {
23686 struct glyph *glyph;
23687 enum glyph_row_area area = it->area;
23688
23689 eassert (it->glyph_row);
23690
23691 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23692 if (glyph < it->glyph_row->glyphs[area + 1])
23693 {
23694 /* If the glyph row is reversed, we need to prepend the glyph
23695 rather than append it. */
23696 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23697 {
23698 struct glyph *g;
23699
23700 /* Make room for the new glyph. */
23701 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23702 g[1] = *g;
23703 glyph = it->glyph_row->glyphs[it->area];
23704 }
23705 glyph->charpos = it->cmp_it.charpos;
23706 glyph->object = it->object;
23707 glyph->pixel_width = it->pixel_width;
23708 glyph->ascent = it->ascent;
23709 glyph->descent = it->descent;
23710 glyph->voffset = it->voffset;
23711 glyph->type = COMPOSITE_GLYPH;
23712 if (it->cmp_it.ch < 0)
23713 {
23714 glyph->u.cmp.automatic = 0;
23715 glyph->u.cmp.id = it->cmp_it.id;
23716 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23717 }
23718 else
23719 {
23720 glyph->u.cmp.automatic = 1;
23721 glyph->u.cmp.id = it->cmp_it.id;
23722 glyph->slice.cmp.from = it->cmp_it.from;
23723 glyph->slice.cmp.to = it->cmp_it.to - 1;
23724 }
23725 glyph->avoid_cursor_p = it->avoid_cursor_p;
23726 glyph->multibyte_p = it->multibyte_p;
23727 glyph->left_box_line_p = it->start_of_box_run_p;
23728 glyph->right_box_line_p = it->end_of_box_run_p;
23729 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23730 || it->phys_descent > it->descent);
23731 glyph->padding_p = 0;
23732 glyph->glyph_not_available_p = 0;
23733 glyph->face_id = it->face_id;
23734 glyph->font_type = FONT_TYPE_UNKNOWN;
23735 if (it->bidi_p)
23736 {
23737 glyph->resolved_level = it->bidi_it.resolved_level;
23738 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23739 abort ();
23740 glyph->bidi_type = it->bidi_it.type;
23741 }
23742 ++it->glyph_row->used[area];
23743 }
23744 else
23745 IT_EXPAND_MATRIX_WIDTH (it, area);
23746 }
23747
23748
23749 /* Change IT->ascent and IT->height according to the setting of
23750 IT->voffset. */
23751
23752 static inline void
23753 take_vertical_position_into_account (struct it *it)
23754 {
23755 if (it->voffset)
23756 {
23757 if (it->voffset < 0)
23758 /* Increase the ascent so that we can display the text higher
23759 in the line. */
23760 it->ascent -= it->voffset;
23761 else
23762 /* Increase the descent so that we can display the text lower
23763 in the line. */
23764 it->descent += it->voffset;
23765 }
23766 }
23767
23768
23769 /* Produce glyphs/get display metrics for the image IT is loaded with.
23770 See the description of struct display_iterator in dispextern.h for
23771 an overview of struct display_iterator. */
23772
23773 static void
23774 produce_image_glyph (struct it *it)
23775 {
23776 struct image *img;
23777 struct face *face;
23778 int glyph_ascent, crop;
23779 struct glyph_slice slice;
23780
23781 eassert (it->what == IT_IMAGE);
23782
23783 face = FACE_FROM_ID (it->f, it->face_id);
23784 eassert (face);
23785 /* Make sure X resources of the face is loaded. */
23786 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23787
23788 if (it->image_id < 0)
23789 {
23790 /* Fringe bitmap. */
23791 it->ascent = it->phys_ascent = 0;
23792 it->descent = it->phys_descent = 0;
23793 it->pixel_width = 0;
23794 it->nglyphs = 0;
23795 return;
23796 }
23797
23798 img = IMAGE_FROM_ID (it->f, it->image_id);
23799 eassert (img);
23800 /* Make sure X resources of the image is loaded. */
23801 prepare_image_for_display (it->f, img);
23802
23803 slice.x = slice.y = 0;
23804 slice.width = img->width;
23805 slice.height = img->height;
23806
23807 if (INTEGERP (it->slice.x))
23808 slice.x = XINT (it->slice.x);
23809 else if (FLOATP (it->slice.x))
23810 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23811
23812 if (INTEGERP (it->slice.y))
23813 slice.y = XINT (it->slice.y);
23814 else if (FLOATP (it->slice.y))
23815 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23816
23817 if (INTEGERP (it->slice.width))
23818 slice.width = XINT (it->slice.width);
23819 else if (FLOATP (it->slice.width))
23820 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23821
23822 if (INTEGERP (it->slice.height))
23823 slice.height = XINT (it->slice.height);
23824 else if (FLOATP (it->slice.height))
23825 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23826
23827 if (slice.x >= img->width)
23828 slice.x = img->width;
23829 if (slice.y >= img->height)
23830 slice.y = img->height;
23831 if (slice.x + slice.width >= img->width)
23832 slice.width = img->width - slice.x;
23833 if (slice.y + slice.height > img->height)
23834 slice.height = img->height - slice.y;
23835
23836 if (slice.width == 0 || slice.height == 0)
23837 return;
23838
23839 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23840
23841 it->descent = slice.height - glyph_ascent;
23842 if (slice.y == 0)
23843 it->descent += img->vmargin;
23844 if (slice.y + slice.height == img->height)
23845 it->descent += img->vmargin;
23846 it->phys_descent = it->descent;
23847
23848 it->pixel_width = slice.width;
23849 if (slice.x == 0)
23850 it->pixel_width += img->hmargin;
23851 if (slice.x + slice.width == img->width)
23852 it->pixel_width += img->hmargin;
23853
23854 /* It's quite possible for images to have an ascent greater than
23855 their height, so don't get confused in that case. */
23856 if (it->descent < 0)
23857 it->descent = 0;
23858
23859 it->nglyphs = 1;
23860
23861 if (face->box != FACE_NO_BOX)
23862 {
23863 if (face->box_line_width > 0)
23864 {
23865 if (slice.y == 0)
23866 it->ascent += face->box_line_width;
23867 if (slice.y + slice.height == img->height)
23868 it->descent += face->box_line_width;
23869 }
23870
23871 if (it->start_of_box_run_p && slice.x == 0)
23872 it->pixel_width += eabs (face->box_line_width);
23873 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23874 it->pixel_width += eabs (face->box_line_width);
23875 }
23876
23877 take_vertical_position_into_account (it);
23878
23879 /* Automatically crop wide image glyphs at right edge so we can
23880 draw the cursor on same display row. */
23881 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23882 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23883 {
23884 it->pixel_width -= crop;
23885 slice.width -= crop;
23886 }
23887
23888 if (it->glyph_row)
23889 {
23890 struct glyph *glyph;
23891 enum glyph_row_area area = it->area;
23892
23893 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23894 if (glyph < it->glyph_row->glyphs[area + 1])
23895 {
23896 glyph->charpos = CHARPOS (it->position);
23897 glyph->object = it->object;
23898 glyph->pixel_width = it->pixel_width;
23899 glyph->ascent = glyph_ascent;
23900 glyph->descent = it->descent;
23901 glyph->voffset = it->voffset;
23902 glyph->type = IMAGE_GLYPH;
23903 glyph->avoid_cursor_p = it->avoid_cursor_p;
23904 glyph->multibyte_p = it->multibyte_p;
23905 glyph->left_box_line_p = it->start_of_box_run_p;
23906 glyph->right_box_line_p = it->end_of_box_run_p;
23907 glyph->overlaps_vertically_p = 0;
23908 glyph->padding_p = 0;
23909 glyph->glyph_not_available_p = 0;
23910 glyph->face_id = it->face_id;
23911 glyph->u.img_id = img->id;
23912 glyph->slice.img = slice;
23913 glyph->font_type = FONT_TYPE_UNKNOWN;
23914 if (it->bidi_p)
23915 {
23916 glyph->resolved_level = it->bidi_it.resolved_level;
23917 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23918 abort ();
23919 glyph->bidi_type = it->bidi_it.type;
23920 }
23921 ++it->glyph_row->used[area];
23922 }
23923 else
23924 IT_EXPAND_MATRIX_WIDTH (it, area);
23925 }
23926 }
23927
23928
23929 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23930 of the glyph, WIDTH and HEIGHT are the width and height of the
23931 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23932
23933 static void
23934 append_stretch_glyph (struct it *it, Lisp_Object object,
23935 int width, int height, int ascent)
23936 {
23937 struct glyph *glyph;
23938 enum glyph_row_area area = it->area;
23939
23940 eassert (ascent >= 0 && ascent <= height);
23941
23942 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23943 if (glyph < it->glyph_row->glyphs[area + 1])
23944 {
23945 /* If the glyph row is reversed, we need to prepend the glyph
23946 rather than append it. */
23947 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23948 {
23949 struct glyph *g;
23950
23951 /* Make room for the additional glyph. */
23952 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23953 g[1] = *g;
23954 glyph = it->glyph_row->glyphs[area];
23955 }
23956 glyph->charpos = CHARPOS (it->position);
23957 glyph->object = object;
23958 glyph->pixel_width = width;
23959 glyph->ascent = ascent;
23960 glyph->descent = height - ascent;
23961 glyph->voffset = it->voffset;
23962 glyph->type = STRETCH_GLYPH;
23963 glyph->avoid_cursor_p = it->avoid_cursor_p;
23964 glyph->multibyte_p = it->multibyte_p;
23965 glyph->left_box_line_p = it->start_of_box_run_p;
23966 glyph->right_box_line_p = it->end_of_box_run_p;
23967 glyph->overlaps_vertically_p = 0;
23968 glyph->padding_p = 0;
23969 glyph->glyph_not_available_p = 0;
23970 glyph->face_id = it->face_id;
23971 glyph->u.stretch.ascent = ascent;
23972 glyph->u.stretch.height = height;
23973 glyph->slice.img = null_glyph_slice;
23974 glyph->font_type = FONT_TYPE_UNKNOWN;
23975 if (it->bidi_p)
23976 {
23977 glyph->resolved_level = it->bidi_it.resolved_level;
23978 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23979 abort ();
23980 glyph->bidi_type = it->bidi_it.type;
23981 }
23982 else
23983 {
23984 glyph->resolved_level = 0;
23985 glyph->bidi_type = UNKNOWN_BT;
23986 }
23987 ++it->glyph_row->used[area];
23988 }
23989 else
23990 IT_EXPAND_MATRIX_WIDTH (it, area);
23991 }
23992
23993 #endif /* HAVE_WINDOW_SYSTEM */
23994
23995 /* Produce a stretch glyph for iterator IT. IT->object is the value
23996 of the glyph property displayed. The value must be a list
23997 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23998 being recognized:
23999
24000 1. `:width WIDTH' specifies that the space should be WIDTH *
24001 canonical char width wide. WIDTH may be an integer or floating
24002 point number.
24003
24004 2. `:relative-width FACTOR' specifies that the width of the stretch
24005 should be computed from the width of the first character having the
24006 `glyph' property, and should be FACTOR times that width.
24007
24008 3. `:align-to HPOS' specifies that the space should be wide enough
24009 to reach HPOS, a value in canonical character units.
24010
24011 Exactly one of the above pairs must be present.
24012
24013 4. `:height HEIGHT' specifies that the height of the stretch produced
24014 should be HEIGHT, measured in canonical character units.
24015
24016 5. `:relative-height FACTOR' specifies that the height of the
24017 stretch should be FACTOR times the height of the characters having
24018 the glyph property.
24019
24020 Either none or exactly one of 4 or 5 must be present.
24021
24022 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24023 of the stretch should be used for the ascent of the stretch.
24024 ASCENT must be in the range 0 <= ASCENT <= 100. */
24025
24026 void
24027 produce_stretch_glyph (struct it *it)
24028 {
24029 /* (space :width WIDTH :height HEIGHT ...) */
24030 Lisp_Object prop, plist;
24031 int width = 0, height = 0, align_to = -1;
24032 int zero_width_ok_p = 0;
24033 int ascent = 0;
24034 double tem;
24035 struct face *face = NULL;
24036 struct font *font = NULL;
24037
24038 #ifdef HAVE_WINDOW_SYSTEM
24039 int zero_height_ok_p = 0;
24040
24041 if (FRAME_WINDOW_P (it->f))
24042 {
24043 face = FACE_FROM_ID (it->f, it->face_id);
24044 font = face->font ? face->font : FRAME_FONT (it->f);
24045 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24046 }
24047 #endif
24048
24049 /* List should start with `space'. */
24050 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24051 plist = XCDR (it->object);
24052
24053 /* Compute the width of the stretch. */
24054 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24055 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24056 {
24057 /* Absolute width `:width WIDTH' specified and valid. */
24058 zero_width_ok_p = 1;
24059 width = (int)tem;
24060 }
24061 #ifdef HAVE_WINDOW_SYSTEM
24062 else if (FRAME_WINDOW_P (it->f)
24063 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24064 {
24065 /* Relative width `:relative-width FACTOR' specified and valid.
24066 Compute the width of the characters having the `glyph'
24067 property. */
24068 struct it it2;
24069 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24070
24071 it2 = *it;
24072 if (it->multibyte_p)
24073 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24074 else
24075 {
24076 it2.c = it2.char_to_display = *p, it2.len = 1;
24077 if (! ASCII_CHAR_P (it2.c))
24078 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24079 }
24080
24081 it2.glyph_row = NULL;
24082 it2.what = IT_CHARACTER;
24083 x_produce_glyphs (&it2);
24084 width = NUMVAL (prop) * it2.pixel_width;
24085 }
24086 #endif /* HAVE_WINDOW_SYSTEM */
24087 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24088 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24089 {
24090 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24091 align_to = (align_to < 0
24092 ? 0
24093 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24094 else if (align_to < 0)
24095 align_to = window_box_left_offset (it->w, TEXT_AREA);
24096 width = max (0, (int)tem + align_to - it->current_x);
24097 zero_width_ok_p = 1;
24098 }
24099 else
24100 /* Nothing specified -> width defaults to canonical char width. */
24101 width = FRAME_COLUMN_WIDTH (it->f);
24102
24103 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24104 width = 1;
24105
24106 #ifdef HAVE_WINDOW_SYSTEM
24107 /* Compute height. */
24108 if (FRAME_WINDOW_P (it->f))
24109 {
24110 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24111 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24112 {
24113 height = (int)tem;
24114 zero_height_ok_p = 1;
24115 }
24116 else if (prop = Fplist_get (plist, QCrelative_height),
24117 NUMVAL (prop) > 0)
24118 height = FONT_HEIGHT (font) * NUMVAL (prop);
24119 else
24120 height = FONT_HEIGHT (font);
24121
24122 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24123 height = 1;
24124
24125 /* Compute percentage of height used for ascent. If
24126 `:ascent ASCENT' is present and valid, use that. Otherwise,
24127 derive the ascent from the font in use. */
24128 if (prop = Fplist_get (plist, QCascent),
24129 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24130 ascent = height * NUMVAL (prop) / 100.0;
24131 else if (!NILP (prop)
24132 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24133 ascent = min (max (0, (int)tem), height);
24134 else
24135 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24136 }
24137 else
24138 #endif /* HAVE_WINDOW_SYSTEM */
24139 height = 1;
24140
24141 if (width > 0 && it->line_wrap != TRUNCATE
24142 && it->current_x + width > it->last_visible_x)
24143 {
24144 width = it->last_visible_x - it->current_x;
24145 #ifdef HAVE_WINDOW_SYSTEM
24146 /* Subtract one more pixel from the stretch width, but only on
24147 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24148 width -= FRAME_WINDOW_P (it->f);
24149 #endif
24150 }
24151
24152 if (width > 0 && height > 0 && it->glyph_row)
24153 {
24154 Lisp_Object o_object = it->object;
24155 Lisp_Object object = it->stack[it->sp - 1].string;
24156 int n = width;
24157
24158 if (!STRINGP (object))
24159 object = it->w->buffer;
24160 #ifdef HAVE_WINDOW_SYSTEM
24161 if (FRAME_WINDOW_P (it->f))
24162 append_stretch_glyph (it, object, width, height, ascent);
24163 else
24164 #endif
24165 {
24166 it->object = object;
24167 it->char_to_display = ' ';
24168 it->pixel_width = it->len = 1;
24169 while (n--)
24170 tty_append_glyph (it);
24171 it->object = o_object;
24172 }
24173 }
24174
24175 it->pixel_width = width;
24176 #ifdef HAVE_WINDOW_SYSTEM
24177 if (FRAME_WINDOW_P (it->f))
24178 {
24179 it->ascent = it->phys_ascent = ascent;
24180 it->descent = it->phys_descent = height - it->ascent;
24181 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24182 take_vertical_position_into_account (it);
24183 }
24184 else
24185 #endif
24186 it->nglyphs = width;
24187 }
24188
24189 /* Get information about special display element WHAT in an
24190 environment described by IT. WHAT is one of IT_TRUNCATION or
24191 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24192 non-null glyph_row member. This function ensures that fields like
24193 face_id, c, len of IT are left untouched. */
24194
24195 static void
24196 produce_special_glyphs (struct it *it, enum display_element_type what)
24197 {
24198 struct it temp_it;
24199 Lisp_Object gc;
24200 GLYPH glyph;
24201
24202 temp_it = *it;
24203 temp_it.object = make_number (0);
24204 memset (&temp_it.current, 0, sizeof temp_it.current);
24205
24206 if (what == IT_CONTINUATION)
24207 {
24208 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24209 if (it->bidi_it.paragraph_dir == R2L)
24210 SET_GLYPH_FROM_CHAR (glyph, '/');
24211 else
24212 SET_GLYPH_FROM_CHAR (glyph, '\\');
24213 if (it->dp
24214 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24215 {
24216 /* FIXME: Should we mirror GC for R2L lines? */
24217 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24218 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24219 }
24220 }
24221 else if (what == IT_TRUNCATION)
24222 {
24223 /* Truncation glyph. */
24224 SET_GLYPH_FROM_CHAR (glyph, '$');
24225 if (it->dp
24226 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24227 {
24228 /* FIXME: Should we mirror GC for R2L lines? */
24229 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24230 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24231 }
24232 }
24233 else
24234 abort ();
24235
24236 #ifdef HAVE_WINDOW_SYSTEM
24237 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24238 is turned off, we precede the truncation/continuation glyphs by a
24239 stretch glyph whose width is computed such that these special
24240 glyphs are aligned at the window margin, even when very different
24241 fonts are used in different glyph rows. */
24242 if (FRAME_WINDOW_P (temp_it.f)
24243 /* init_iterator calls this with it->glyph_row == NULL, and it
24244 wants only the pixel width of the truncation/continuation
24245 glyphs. */
24246 && temp_it.glyph_row
24247 /* insert_left_trunc_glyphs calls us at the beginning of the
24248 row, and it has its own calculation of the stretch glyph
24249 width. */
24250 && temp_it.glyph_row->used[TEXT_AREA] > 0
24251 && (temp_it.glyph_row->reversed_p
24252 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24253 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24254 {
24255 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24256
24257 if (stretch_width > 0)
24258 {
24259 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24260 struct font *font =
24261 face->font ? face->font : FRAME_FONT (temp_it.f);
24262 int stretch_ascent =
24263 (((temp_it.ascent + temp_it.descent)
24264 * FONT_BASE (font)) / FONT_HEIGHT (font));
24265
24266 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24267 temp_it.ascent + temp_it.descent,
24268 stretch_ascent);
24269 }
24270 }
24271 #endif
24272
24273 temp_it.dp = NULL;
24274 temp_it.what = IT_CHARACTER;
24275 temp_it.len = 1;
24276 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24277 temp_it.face_id = GLYPH_FACE (glyph);
24278 temp_it.len = CHAR_BYTES (temp_it.c);
24279
24280 PRODUCE_GLYPHS (&temp_it);
24281 it->pixel_width = temp_it.pixel_width;
24282 it->nglyphs = temp_it.pixel_width;
24283 }
24284
24285 #ifdef HAVE_WINDOW_SYSTEM
24286
24287 /* Calculate line-height and line-spacing properties.
24288 An integer value specifies explicit pixel value.
24289 A float value specifies relative value to current face height.
24290 A cons (float . face-name) specifies relative value to
24291 height of specified face font.
24292
24293 Returns height in pixels, or nil. */
24294
24295
24296 static Lisp_Object
24297 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24298 int boff, int override)
24299 {
24300 Lisp_Object face_name = Qnil;
24301 int ascent, descent, height;
24302
24303 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24304 return val;
24305
24306 if (CONSP (val))
24307 {
24308 face_name = XCAR (val);
24309 val = XCDR (val);
24310 if (!NUMBERP (val))
24311 val = make_number (1);
24312 if (NILP (face_name))
24313 {
24314 height = it->ascent + it->descent;
24315 goto scale;
24316 }
24317 }
24318
24319 if (NILP (face_name))
24320 {
24321 font = FRAME_FONT (it->f);
24322 boff = FRAME_BASELINE_OFFSET (it->f);
24323 }
24324 else if (EQ (face_name, Qt))
24325 {
24326 override = 0;
24327 }
24328 else
24329 {
24330 int face_id;
24331 struct face *face;
24332
24333 face_id = lookup_named_face (it->f, face_name, 0);
24334 if (face_id < 0)
24335 return make_number (-1);
24336
24337 face = FACE_FROM_ID (it->f, face_id);
24338 font = face->font;
24339 if (font == NULL)
24340 return make_number (-1);
24341 boff = font->baseline_offset;
24342 if (font->vertical_centering)
24343 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24344 }
24345
24346 ascent = FONT_BASE (font) + boff;
24347 descent = FONT_DESCENT (font) - boff;
24348
24349 if (override)
24350 {
24351 it->override_ascent = ascent;
24352 it->override_descent = descent;
24353 it->override_boff = boff;
24354 }
24355
24356 height = ascent + descent;
24357
24358 scale:
24359 if (FLOATP (val))
24360 height = (int)(XFLOAT_DATA (val) * height);
24361 else if (INTEGERP (val))
24362 height *= XINT (val);
24363
24364 return make_number (height);
24365 }
24366
24367
24368 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24369 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24370 and only if this is for a character for which no font was found.
24371
24372 If the display method (it->glyphless_method) is
24373 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24374 length of the acronym or the hexadecimal string, UPPER_XOFF and
24375 UPPER_YOFF are pixel offsets for the upper part of the string,
24376 LOWER_XOFF and LOWER_YOFF are for the lower part.
24377
24378 For the other display methods, LEN through LOWER_YOFF are zero. */
24379
24380 static void
24381 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24382 short upper_xoff, short upper_yoff,
24383 short lower_xoff, short lower_yoff)
24384 {
24385 struct glyph *glyph;
24386 enum glyph_row_area area = it->area;
24387
24388 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24389 if (glyph < it->glyph_row->glyphs[area + 1])
24390 {
24391 /* If the glyph row is reversed, we need to prepend the glyph
24392 rather than append it. */
24393 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24394 {
24395 struct glyph *g;
24396
24397 /* Make room for the additional glyph. */
24398 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24399 g[1] = *g;
24400 glyph = it->glyph_row->glyphs[area];
24401 }
24402 glyph->charpos = CHARPOS (it->position);
24403 glyph->object = it->object;
24404 glyph->pixel_width = it->pixel_width;
24405 glyph->ascent = it->ascent;
24406 glyph->descent = it->descent;
24407 glyph->voffset = it->voffset;
24408 glyph->type = GLYPHLESS_GLYPH;
24409 glyph->u.glyphless.method = it->glyphless_method;
24410 glyph->u.glyphless.for_no_font = for_no_font;
24411 glyph->u.glyphless.len = len;
24412 glyph->u.glyphless.ch = it->c;
24413 glyph->slice.glyphless.upper_xoff = upper_xoff;
24414 glyph->slice.glyphless.upper_yoff = upper_yoff;
24415 glyph->slice.glyphless.lower_xoff = lower_xoff;
24416 glyph->slice.glyphless.lower_yoff = lower_yoff;
24417 glyph->avoid_cursor_p = it->avoid_cursor_p;
24418 glyph->multibyte_p = it->multibyte_p;
24419 glyph->left_box_line_p = it->start_of_box_run_p;
24420 glyph->right_box_line_p = it->end_of_box_run_p;
24421 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24422 || it->phys_descent > it->descent);
24423 glyph->padding_p = 0;
24424 glyph->glyph_not_available_p = 0;
24425 glyph->face_id = face_id;
24426 glyph->font_type = FONT_TYPE_UNKNOWN;
24427 if (it->bidi_p)
24428 {
24429 glyph->resolved_level = it->bidi_it.resolved_level;
24430 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24431 abort ();
24432 glyph->bidi_type = it->bidi_it.type;
24433 }
24434 ++it->glyph_row->used[area];
24435 }
24436 else
24437 IT_EXPAND_MATRIX_WIDTH (it, area);
24438 }
24439
24440
24441 /* Produce a glyph for a glyphless character for iterator IT.
24442 IT->glyphless_method specifies which method to use for displaying
24443 the character. See the description of enum
24444 glyphless_display_method in dispextern.h for the detail.
24445
24446 FOR_NO_FONT is nonzero if and only if this is for a character for
24447 which no font was found. ACRONYM, if non-nil, is an acronym string
24448 for the character. */
24449
24450 static void
24451 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24452 {
24453 int face_id;
24454 struct face *face;
24455 struct font *font;
24456 int base_width, base_height, width, height;
24457 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24458 int len;
24459
24460 /* Get the metrics of the base font. We always refer to the current
24461 ASCII face. */
24462 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24463 font = face->font ? face->font : FRAME_FONT (it->f);
24464 it->ascent = FONT_BASE (font) + font->baseline_offset;
24465 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24466 base_height = it->ascent + it->descent;
24467 base_width = font->average_width;
24468
24469 /* Get a face ID for the glyph by utilizing a cache (the same way as
24470 done for `escape-glyph' in get_next_display_element). */
24471 if (it->f == last_glyphless_glyph_frame
24472 && it->face_id == last_glyphless_glyph_face_id)
24473 {
24474 face_id = last_glyphless_glyph_merged_face_id;
24475 }
24476 else
24477 {
24478 /* Merge the `glyphless-char' face into the current face. */
24479 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24480 last_glyphless_glyph_frame = it->f;
24481 last_glyphless_glyph_face_id = it->face_id;
24482 last_glyphless_glyph_merged_face_id = face_id;
24483 }
24484
24485 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24486 {
24487 it->pixel_width = THIN_SPACE_WIDTH;
24488 len = 0;
24489 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24490 }
24491 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24492 {
24493 width = CHAR_WIDTH (it->c);
24494 if (width == 0)
24495 width = 1;
24496 else if (width > 4)
24497 width = 4;
24498 it->pixel_width = base_width * width;
24499 len = 0;
24500 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24501 }
24502 else
24503 {
24504 char buf[7];
24505 const char *str;
24506 unsigned int code[6];
24507 int upper_len;
24508 int ascent, descent;
24509 struct font_metrics metrics_upper, metrics_lower;
24510
24511 face = FACE_FROM_ID (it->f, face_id);
24512 font = face->font ? face->font : FRAME_FONT (it->f);
24513 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24514
24515 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24516 {
24517 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24518 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24519 if (CONSP (acronym))
24520 acronym = XCAR (acronym);
24521 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24522 }
24523 else
24524 {
24525 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24526 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24527 str = buf;
24528 }
24529 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24530 code[len] = font->driver->encode_char (font, str[len]);
24531 upper_len = (len + 1) / 2;
24532 font->driver->text_extents (font, code, upper_len,
24533 &metrics_upper);
24534 font->driver->text_extents (font, code + upper_len, len - upper_len,
24535 &metrics_lower);
24536
24537
24538
24539 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24540 width = max (metrics_upper.width, metrics_lower.width) + 4;
24541 upper_xoff = upper_yoff = 2; /* the typical case */
24542 if (base_width >= width)
24543 {
24544 /* Align the upper to the left, the lower to the right. */
24545 it->pixel_width = base_width;
24546 lower_xoff = base_width - 2 - metrics_lower.width;
24547 }
24548 else
24549 {
24550 /* Center the shorter one. */
24551 it->pixel_width = width;
24552 if (metrics_upper.width >= metrics_lower.width)
24553 lower_xoff = (width - metrics_lower.width) / 2;
24554 else
24555 {
24556 /* FIXME: This code doesn't look right. It formerly was
24557 missing the "lower_xoff = 0;", which couldn't have
24558 been right since it left lower_xoff uninitialized. */
24559 lower_xoff = 0;
24560 upper_xoff = (width - metrics_upper.width) / 2;
24561 }
24562 }
24563
24564 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24565 top, bottom, and between upper and lower strings. */
24566 height = (metrics_upper.ascent + metrics_upper.descent
24567 + metrics_lower.ascent + metrics_lower.descent) + 5;
24568 /* Center vertically.
24569 H:base_height, D:base_descent
24570 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24571
24572 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24573 descent = D - H/2 + h/2;
24574 lower_yoff = descent - 2 - ld;
24575 upper_yoff = lower_yoff - la - 1 - ud; */
24576 ascent = - (it->descent - (base_height + height + 1) / 2);
24577 descent = it->descent - (base_height - height) / 2;
24578 lower_yoff = descent - 2 - metrics_lower.descent;
24579 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24580 - metrics_upper.descent);
24581 /* Don't make the height shorter than the base height. */
24582 if (height > base_height)
24583 {
24584 it->ascent = ascent;
24585 it->descent = descent;
24586 }
24587 }
24588
24589 it->phys_ascent = it->ascent;
24590 it->phys_descent = it->descent;
24591 if (it->glyph_row)
24592 append_glyphless_glyph (it, face_id, for_no_font, len,
24593 upper_xoff, upper_yoff,
24594 lower_xoff, lower_yoff);
24595 it->nglyphs = 1;
24596 take_vertical_position_into_account (it);
24597 }
24598
24599
24600 /* RIF:
24601 Produce glyphs/get display metrics for the display element IT is
24602 loaded with. See the description of struct it in dispextern.h
24603 for an overview of struct it. */
24604
24605 void
24606 x_produce_glyphs (struct it *it)
24607 {
24608 int extra_line_spacing = it->extra_line_spacing;
24609
24610 it->glyph_not_available_p = 0;
24611
24612 if (it->what == IT_CHARACTER)
24613 {
24614 XChar2b char2b;
24615 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24616 struct font *font = face->font;
24617 struct font_metrics *pcm = NULL;
24618 int boff; /* baseline offset */
24619
24620 if (font == NULL)
24621 {
24622 /* When no suitable font is found, display this character by
24623 the method specified in the first extra slot of
24624 Vglyphless_char_display. */
24625 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24626
24627 eassert (it->what == IT_GLYPHLESS);
24628 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24629 goto done;
24630 }
24631
24632 boff = font->baseline_offset;
24633 if (font->vertical_centering)
24634 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24635
24636 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24637 {
24638 int stretched_p;
24639
24640 it->nglyphs = 1;
24641
24642 if (it->override_ascent >= 0)
24643 {
24644 it->ascent = it->override_ascent;
24645 it->descent = it->override_descent;
24646 boff = it->override_boff;
24647 }
24648 else
24649 {
24650 it->ascent = FONT_BASE (font) + boff;
24651 it->descent = FONT_DESCENT (font) - boff;
24652 }
24653
24654 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24655 {
24656 pcm = get_per_char_metric (font, &char2b);
24657 if (pcm->width == 0
24658 && pcm->rbearing == 0 && pcm->lbearing == 0)
24659 pcm = NULL;
24660 }
24661
24662 if (pcm)
24663 {
24664 it->phys_ascent = pcm->ascent + boff;
24665 it->phys_descent = pcm->descent - boff;
24666 it->pixel_width = pcm->width;
24667 }
24668 else
24669 {
24670 it->glyph_not_available_p = 1;
24671 it->phys_ascent = it->ascent;
24672 it->phys_descent = it->descent;
24673 it->pixel_width = font->space_width;
24674 }
24675
24676 if (it->constrain_row_ascent_descent_p)
24677 {
24678 if (it->descent > it->max_descent)
24679 {
24680 it->ascent += it->descent - it->max_descent;
24681 it->descent = it->max_descent;
24682 }
24683 if (it->ascent > it->max_ascent)
24684 {
24685 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24686 it->ascent = it->max_ascent;
24687 }
24688 it->phys_ascent = min (it->phys_ascent, it->ascent);
24689 it->phys_descent = min (it->phys_descent, it->descent);
24690 extra_line_spacing = 0;
24691 }
24692
24693 /* If this is a space inside a region of text with
24694 `space-width' property, change its width. */
24695 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24696 if (stretched_p)
24697 it->pixel_width *= XFLOATINT (it->space_width);
24698
24699 /* If face has a box, add the box thickness to the character
24700 height. If character has a box line to the left and/or
24701 right, add the box line width to the character's width. */
24702 if (face->box != FACE_NO_BOX)
24703 {
24704 int thick = face->box_line_width;
24705
24706 if (thick > 0)
24707 {
24708 it->ascent += thick;
24709 it->descent += thick;
24710 }
24711 else
24712 thick = -thick;
24713
24714 if (it->start_of_box_run_p)
24715 it->pixel_width += thick;
24716 if (it->end_of_box_run_p)
24717 it->pixel_width += thick;
24718 }
24719
24720 /* If face has an overline, add the height of the overline
24721 (1 pixel) and a 1 pixel margin to the character height. */
24722 if (face->overline_p)
24723 it->ascent += overline_margin;
24724
24725 if (it->constrain_row_ascent_descent_p)
24726 {
24727 if (it->ascent > it->max_ascent)
24728 it->ascent = it->max_ascent;
24729 if (it->descent > it->max_descent)
24730 it->descent = it->max_descent;
24731 }
24732
24733 take_vertical_position_into_account (it);
24734
24735 /* If we have to actually produce glyphs, do it. */
24736 if (it->glyph_row)
24737 {
24738 if (stretched_p)
24739 {
24740 /* Translate a space with a `space-width' property
24741 into a stretch glyph. */
24742 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24743 / FONT_HEIGHT (font));
24744 append_stretch_glyph (it, it->object, it->pixel_width,
24745 it->ascent + it->descent, ascent);
24746 }
24747 else
24748 append_glyph (it);
24749
24750 /* If characters with lbearing or rbearing are displayed
24751 in this line, record that fact in a flag of the
24752 glyph row. This is used to optimize X output code. */
24753 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24754 it->glyph_row->contains_overlapping_glyphs_p = 1;
24755 }
24756 if (! stretched_p && it->pixel_width == 0)
24757 /* We assure that all visible glyphs have at least 1-pixel
24758 width. */
24759 it->pixel_width = 1;
24760 }
24761 else if (it->char_to_display == '\n')
24762 {
24763 /* A newline has no width, but we need the height of the
24764 line. But if previous part of the line sets a height,
24765 don't increase that height */
24766
24767 Lisp_Object height;
24768 Lisp_Object total_height = Qnil;
24769
24770 it->override_ascent = -1;
24771 it->pixel_width = 0;
24772 it->nglyphs = 0;
24773
24774 height = get_it_property (it, Qline_height);
24775 /* Split (line-height total-height) list */
24776 if (CONSP (height)
24777 && CONSP (XCDR (height))
24778 && NILP (XCDR (XCDR (height))))
24779 {
24780 total_height = XCAR (XCDR (height));
24781 height = XCAR (height);
24782 }
24783 height = calc_line_height_property (it, height, font, boff, 1);
24784
24785 if (it->override_ascent >= 0)
24786 {
24787 it->ascent = it->override_ascent;
24788 it->descent = it->override_descent;
24789 boff = it->override_boff;
24790 }
24791 else
24792 {
24793 it->ascent = FONT_BASE (font) + boff;
24794 it->descent = FONT_DESCENT (font) - boff;
24795 }
24796
24797 if (EQ (height, Qt))
24798 {
24799 if (it->descent > it->max_descent)
24800 {
24801 it->ascent += it->descent - it->max_descent;
24802 it->descent = it->max_descent;
24803 }
24804 if (it->ascent > it->max_ascent)
24805 {
24806 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24807 it->ascent = it->max_ascent;
24808 }
24809 it->phys_ascent = min (it->phys_ascent, it->ascent);
24810 it->phys_descent = min (it->phys_descent, it->descent);
24811 it->constrain_row_ascent_descent_p = 1;
24812 extra_line_spacing = 0;
24813 }
24814 else
24815 {
24816 Lisp_Object spacing;
24817
24818 it->phys_ascent = it->ascent;
24819 it->phys_descent = it->descent;
24820
24821 if ((it->max_ascent > 0 || it->max_descent > 0)
24822 && face->box != FACE_NO_BOX
24823 && face->box_line_width > 0)
24824 {
24825 it->ascent += face->box_line_width;
24826 it->descent += face->box_line_width;
24827 }
24828 if (!NILP (height)
24829 && XINT (height) > it->ascent + it->descent)
24830 it->ascent = XINT (height) - it->descent;
24831
24832 if (!NILP (total_height))
24833 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24834 else
24835 {
24836 spacing = get_it_property (it, Qline_spacing);
24837 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24838 }
24839 if (INTEGERP (spacing))
24840 {
24841 extra_line_spacing = XINT (spacing);
24842 if (!NILP (total_height))
24843 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24844 }
24845 }
24846 }
24847 else /* i.e. (it->char_to_display == '\t') */
24848 {
24849 if (font->space_width > 0)
24850 {
24851 int tab_width = it->tab_width * font->space_width;
24852 int x = it->current_x + it->continuation_lines_width;
24853 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24854
24855 /* If the distance from the current position to the next tab
24856 stop is less than a space character width, use the
24857 tab stop after that. */
24858 if (next_tab_x - x < font->space_width)
24859 next_tab_x += tab_width;
24860
24861 it->pixel_width = next_tab_x - x;
24862 it->nglyphs = 1;
24863 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24864 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24865
24866 if (it->glyph_row)
24867 {
24868 append_stretch_glyph (it, it->object, it->pixel_width,
24869 it->ascent + it->descent, it->ascent);
24870 }
24871 }
24872 else
24873 {
24874 it->pixel_width = 0;
24875 it->nglyphs = 1;
24876 }
24877 }
24878 }
24879 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24880 {
24881 /* A static composition.
24882
24883 Note: A composition is represented as one glyph in the
24884 glyph matrix. There are no padding glyphs.
24885
24886 Important note: pixel_width, ascent, and descent are the
24887 values of what is drawn by draw_glyphs (i.e. the values of
24888 the overall glyphs composed). */
24889 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24890 int boff; /* baseline offset */
24891 struct composition *cmp = composition_table[it->cmp_it.id];
24892 int glyph_len = cmp->glyph_len;
24893 struct font *font = face->font;
24894
24895 it->nglyphs = 1;
24896
24897 /* If we have not yet calculated pixel size data of glyphs of
24898 the composition for the current face font, calculate them
24899 now. Theoretically, we have to check all fonts for the
24900 glyphs, but that requires much time and memory space. So,
24901 here we check only the font of the first glyph. This may
24902 lead to incorrect display, but it's very rare, and C-l
24903 (recenter-top-bottom) can correct the display anyway. */
24904 if (! cmp->font || cmp->font != font)
24905 {
24906 /* Ascent and descent of the font of the first character
24907 of this composition (adjusted by baseline offset).
24908 Ascent and descent of overall glyphs should not be less
24909 than these, respectively. */
24910 int font_ascent, font_descent, font_height;
24911 /* Bounding box of the overall glyphs. */
24912 int leftmost, rightmost, lowest, highest;
24913 int lbearing, rbearing;
24914 int i, width, ascent, descent;
24915 int left_padded = 0, right_padded = 0;
24916 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24917 XChar2b char2b;
24918 struct font_metrics *pcm;
24919 int font_not_found_p;
24920 ptrdiff_t pos;
24921
24922 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24923 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24924 break;
24925 if (glyph_len < cmp->glyph_len)
24926 right_padded = 1;
24927 for (i = 0; i < glyph_len; i++)
24928 {
24929 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24930 break;
24931 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24932 }
24933 if (i > 0)
24934 left_padded = 1;
24935
24936 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24937 : IT_CHARPOS (*it));
24938 /* If no suitable font is found, use the default font. */
24939 font_not_found_p = font == NULL;
24940 if (font_not_found_p)
24941 {
24942 face = face->ascii_face;
24943 font = face->font;
24944 }
24945 boff = font->baseline_offset;
24946 if (font->vertical_centering)
24947 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24948 font_ascent = FONT_BASE (font) + boff;
24949 font_descent = FONT_DESCENT (font) - boff;
24950 font_height = FONT_HEIGHT (font);
24951
24952 cmp->font = (void *) font;
24953
24954 pcm = NULL;
24955 if (! font_not_found_p)
24956 {
24957 get_char_face_and_encoding (it->f, c, it->face_id,
24958 &char2b, 0);
24959 pcm = get_per_char_metric (font, &char2b);
24960 }
24961
24962 /* Initialize the bounding box. */
24963 if (pcm)
24964 {
24965 width = cmp->glyph_len > 0 ? pcm->width : 0;
24966 ascent = pcm->ascent;
24967 descent = pcm->descent;
24968 lbearing = pcm->lbearing;
24969 rbearing = pcm->rbearing;
24970 }
24971 else
24972 {
24973 width = cmp->glyph_len > 0 ? font->space_width : 0;
24974 ascent = FONT_BASE (font);
24975 descent = FONT_DESCENT (font);
24976 lbearing = 0;
24977 rbearing = width;
24978 }
24979
24980 rightmost = width;
24981 leftmost = 0;
24982 lowest = - descent + boff;
24983 highest = ascent + boff;
24984
24985 if (! font_not_found_p
24986 && font->default_ascent
24987 && CHAR_TABLE_P (Vuse_default_ascent)
24988 && !NILP (Faref (Vuse_default_ascent,
24989 make_number (it->char_to_display))))
24990 highest = font->default_ascent + boff;
24991
24992 /* Draw the first glyph at the normal position. It may be
24993 shifted to right later if some other glyphs are drawn
24994 at the left. */
24995 cmp->offsets[i * 2] = 0;
24996 cmp->offsets[i * 2 + 1] = boff;
24997 cmp->lbearing = lbearing;
24998 cmp->rbearing = rbearing;
24999
25000 /* Set cmp->offsets for the remaining glyphs. */
25001 for (i++; i < glyph_len; i++)
25002 {
25003 int left, right, btm, top;
25004 int ch = COMPOSITION_GLYPH (cmp, i);
25005 int face_id;
25006 struct face *this_face;
25007
25008 if (ch == '\t')
25009 ch = ' ';
25010 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25011 this_face = FACE_FROM_ID (it->f, face_id);
25012 font = this_face->font;
25013
25014 if (font == NULL)
25015 pcm = NULL;
25016 else
25017 {
25018 get_char_face_and_encoding (it->f, ch, face_id,
25019 &char2b, 0);
25020 pcm = get_per_char_metric (font, &char2b);
25021 }
25022 if (! pcm)
25023 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25024 else
25025 {
25026 width = pcm->width;
25027 ascent = pcm->ascent;
25028 descent = pcm->descent;
25029 lbearing = pcm->lbearing;
25030 rbearing = pcm->rbearing;
25031 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25032 {
25033 /* Relative composition with or without
25034 alternate chars. */
25035 left = (leftmost + rightmost - width) / 2;
25036 btm = - descent + boff;
25037 if (font->relative_compose
25038 && (! CHAR_TABLE_P (Vignore_relative_composition)
25039 || NILP (Faref (Vignore_relative_composition,
25040 make_number (ch)))))
25041 {
25042
25043 if (- descent >= font->relative_compose)
25044 /* One extra pixel between two glyphs. */
25045 btm = highest + 1;
25046 else if (ascent <= 0)
25047 /* One extra pixel between two glyphs. */
25048 btm = lowest - 1 - ascent - descent;
25049 }
25050 }
25051 else
25052 {
25053 /* A composition rule is specified by an integer
25054 value that encodes global and new reference
25055 points (GREF and NREF). GREF and NREF are
25056 specified by numbers as below:
25057
25058 0---1---2 -- ascent
25059 | |
25060 | |
25061 | |
25062 9--10--11 -- center
25063 | |
25064 ---3---4---5--- baseline
25065 | |
25066 6---7---8 -- descent
25067 */
25068 int rule = COMPOSITION_RULE (cmp, i);
25069 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25070
25071 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25072 grefx = gref % 3, nrefx = nref % 3;
25073 grefy = gref / 3, nrefy = nref / 3;
25074 if (xoff)
25075 xoff = font_height * (xoff - 128) / 256;
25076 if (yoff)
25077 yoff = font_height * (yoff - 128) / 256;
25078
25079 left = (leftmost
25080 + grefx * (rightmost - leftmost) / 2
25081 - nrefx * width / 2
25082 + xoff);
25083
25084 btm = ((grefy == 0 ? highest
25085 : grefy == 1 ? 0
25086 : grefy == 2 ? lowest
25087 : (highest + lowest) / 2)
25088 - (nrefy == 0 ? ascent + descent
25089 : nrefy == 1 ? descent - boff
25090 : nrefy == 2 ? 0
25091 : (ascent + descent) / 2)
25092 + yoff);
25093 }
25094
25095 cmp->offsets[i * 2] = left;
25096 cmp->offsets[i * 2 + 1] = btm + descent;
25097
25098 /* Update the bounding box of the overall glyphs. */
25099 if (width > 0)
25100 {
25101 right = left + width;
25102 if (left < leftmost)
25103 leftmost = left;
25104 if (right > rightmost)
25105 rightmost = right;
25106 }
25107 top = btm + descent + ascent;
25108 if (top > highest)
25109 highest = top;
25110 if (btm < lowest)
25111 lowest = btm;
25112
25113 if (cmp->lbearing > left + lbearing)
25114 cmp->lbearing = left + lbearing;
25115 if (cmp->rbearing < left + rbearing)
25116 cmp->rbearing = left + rbearing;
25117 }
25118 }
25119
25120 /* If there are glyphs whose x-offsets are negative,
25121 shift all glyphs to the right and make all x-offsets
25122 non-negative. */
25123 if (leftmost < 0)
25124 {
25125 for (i = 0; i < cmp->glyph_len; i++)
25126 cmp->offsets[i * 2] -= leftmost;
25127 rightmost -= leftmost;
25128 cmp->lbearing -= leftmost;
25129 cmp->rbearing -= leftmost;
25130 }
25131
25132 if (left_padded && cmp->lbearing < 0)
25133 {
25134 for (i = 0; i < cmp->glyph_len; i++)
25135 cmp->offsets[i * 2] -= cmp->lbearing;
25136 rightmost -= cmp->lbearing;
25137 cmp->rbearing -= cmp->lbearing;
25138 cmp->lbearing = 0;
25139 }
25140 if (right_padded && rightmost < cmp->rbearing)
25141 {
25142 rightmost = cmp->rbearing;
25143 }
25144
25145 cmp->pixel_width = rightmost;
25146 cmp->ascent = highest;
25147 cmp->descent = - lowest;
25148 if (cmp->ascent < font_ascent)
25149 cmp->ascent = font_ascent;
25150 if (cmp->descent < font_descent)
25151 cmp->descent = font_descent;
25152 }
25153
25154 if (it->glyph_row
25155 && (cmp->lbearing < 0
25156 || cmp->rbearing > cmp->pixel_width))
25157 it->glyph_row->contains_overlapping_glyphs_p = 1;
25158
25159 it->pixel_width = cmp->pixel_width;
25160 it->ascent = it->phys_ascent = cmp->ascent;
25161 it->descent = it->phys_descent = cmp->descent;
25162 if (face->box != FACE_NO_BOX)
25163 {
25164 int thick = face->box_line_width;
25165
25166 if (thick > 0)
25167 {
25168 it->ascent += thick;
25169 it->descent += thick;
25170 }
25171 else
25172 thick = - thick;
25173
25174 if (it->start_of_box_run_p)
25175 it->pixel_width += thick;
25176 if (it->end_of_box_run_p)
25177 it->pixel_width += thick;
25178 }
25179
25180 /* If face has an overline, add the height of the overline
25181 (1 pixel) and a 1 pixel margin to the character height. */
25182 if (face->overline_p)
25183 it->ascent += overline_margin;
25184
25185 take_vertical_position_into_account (it);
25186 if (it->ascent < 0)
25187 it->ascent = 0;
25188 if (it->descent < 0)
25189 it->descent = 0;
25190
25191 if (it->glyph_row && cmp->glyph_len > 0)
25192 append_composite_glyph (it);
25193 }
25194 else if (it->what == IT_COMPOSITION)
25195 {
25196 /* A dynamic (automatic) composition. */
25197 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25198 Lisp_Object gstring;
25199 struct font_metrics metrics;
25200
25201 it->nglyphs = 1;
25202
25203 gstring = composition_gstring_from_id (it->cmp_it.id);
25204 it->pixel_width
25205 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25206 &metrics);
25207 if (it->glyph_row
25208 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25209 it->glyph_row->contains_overlapping_glyphs_p = 1;
25210 it->ascent = it->phys_ascent = metrics.ascent;
25211 it->descent = it->phys_descent = metrics.descent;
25212 if (face->box != FACE_NO_BOX)
25213 {
25214 int thick = face->box_line_width;
25215
25216 if (thick > 0)
25217 {
25218 it->ascent += thick;
25219 it->descent += thick;
25220 }
25221 else
25222 thick = - thick;
25223
25224 if (it->start_of_box_run_p)
25225 it->pixel_width += thick;
25226 if (it->end_of_box_run_p)
25227 it->pixel_width += thick;
25228 }
25229 /* If face has an overline, add the height of the overline
25230 (1 pixel) and a 1 pixel margin to the character height. */
25231 if (face->overline_p)
25232 it->ascent += overline_margin;
25233 take_vertical_position_into_account (it);
25234 if (it->ascent < 0)
25235 it->ascent = 0;
25236 if (it->descent < 0)
25237 it->descent = 0;
25238
25239 if (it->glyph_row)
25240 append_composite_glyph (it);
25241 }
25242 else if (it->what == IT_GLYPHLESS)
25243 produce_glyphless_glyph (it, 0, Qnil);
25244 else if (it->what == IT_IMAGE)
25245 produce_image_glyph (it);
25246 else if (it->what == IT_STRETCH)
25247 produce_stretch_glyph (it);
25248
25249 done:
25250 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25251 because this isn't true for images with `:ascent 100'. */
25252 eassert (it->ascent >= 0 && it->descent >= 0);
25253 if (it->area == TEXT_AREA)
25254 it->current_x += it->pixel_width;
25255
25256 if (extra_line_spacing > 0)
25257 {
25258 it->descent += extra_line_spacing;
25259 if (extra_line_spacing > it->max_extra_line_spacing)
25260 it->max_extra_line_spacing = extra_line_spacing;
25261 }
25262
25263 it->max_ascent = max (it->max_ascent, it->ascent);
25264 it->max_descent = max (it->max_descent, it->descent);
25265 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25266 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25267 }
25268
25269 /* EXPORT for RIF:
25270 Output LEN glyphs starting at START at the nominal cursor position.
25271 Advance the nominal cursor over the text. The global variable
25272 updated_window contains the window being updated, updated_row is
25273 the glyph row being updated, and updated_area is the area of that
25274 row being updated. */
25275
25276 void
25277 x_write_glyphs (struct glyph *start, int len)
25278 {
25279 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25280
25281 eassert (updated_window && updated_row);
25282 /* When the window is hscrolled, cursor hpos can legitimately be out
25283 of bounds, but we draw the cursor at the corresponding window
25284 margin in that case. */
25285 if (!updated_row->reversed_p && chpos < 0)
25286 chpos = 0;
25287 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25288 chpos = updated_row->used[TEXT_AREA] - 1;
25289
25290 BLOCK_INPUT;
25291
25292 /* Write glyphs. */
25293
25294 hpos = start - updated_row->glyphs[updated_area];
25295 x = draw_glyphs (updated_window, output_cursor.x,
25296 updated_row, updated_area,
25297 hpos, hpos + len,
25298 DRAW_NORMAL_TEXT, 0);
25299
25300 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25301 if (updated_area == TEXT_AREA
25302 && updated_window->phys_cursor_on_p
25303 && updated_window->phys_cursor.vpos == output_cursor.vpos
25304 && chpos >= hpos
25305 && chpos < hpos + len)
25306 updated_window->phys_cursor_on_p = 0;
25307
25308 UNBLOCK_INPUT;
25309
25310 /* Advance the output cursor. */
25311 output_cursor.hpos += len;
25312 output_cursor.x = x;
25313 }
25314
25315
25316 /* EXPORT for RIF:
25317 Insert LEN glyphs from START at the nominal cursor position. */
25318
25319 void
25320 x_insert_glyphs (struct glyph *start, int len)
25321 {
25322 struct frame *f;
25323 struct window *w;
25324 int line_height, shift_by_width, shifted_region_width;
25325 struct glyph_row *row;
25326 struct glyph *glyph;
25327 int frame_x, frame_y;
25328 ptrdiff_t hpos;
25329
25330 eassert (updated_window && updated_row);
25331 BLOCK_INPUT;
25332 w = updated_window;
25333 f = XFRAME (WINDOW_FRAME (w));
25334
25335 /* Get the height of the line we are in. */
25336 row = updated_row;
25337 line_height = row->height;
25338
25339 /* Get the width of the glyphs to insert. */
25340 shift_by_width = 0;
25341 for (glyph = start; glyph < start + len; ++glyph)
25342 shift_by_width += glyph->pixel_width;
25343
25344 /* Get the width of the region to shift right. */
25345 shifted_region_width = (window_box_width (w, updated_area)
25346 - output_cursor.x
25347 - shift_by_width);
25348
25349 /* Shift right. */
25350 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25351 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25352
25353 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25354 line_height, shift_by_width);
25355
25356 /* Write the glyphs. */
25357 hpos = start - row->glyphs[updated_area];
25358 draw_glyphs (w, output_cursor.x, row, updated_area,
25359 hpos, hpos + len,
25360 DRAW_NORMAL_TEXT, 0);
25361
25362 /* Advance the output cursor. */
25363 output_cursor.hpos += len;
25364 output_cursor.x += shift_by_width;
25365 UNBLOCK_INPUT;
25366 }
25367
25368
25369 /* EXPORT for RIF:
25370 Erase the current text line from the nominal cursor position
25371 (inclusive) to pixel column TO_X (exclusive). The idea is that
25372 everything from TO_X onward is already erased.
25373
25374 TO_X is a pixel position relative to updated_area of
25375 updated_window. TO_X == -1 means clear to the end of this area. */
25376
25377 void
25378 x_clear_end_of_line (int to_x)
25379 {
25380 struct frame *f;
25381 struct window *w = updated_window;
25382 int max_x, min_y, max_y;
25383 int from_x, from_y, to_y;
25384
25385 eassert (updated_window && updated_row);
25386 f = XFRAME (w->frame);
25387
25388 if (updated_row->full_width_p)
25389 max_x = WINDOW_TOTAL_WIDTH (w);
25390 else
25391 max_x = window_box_width (w, updated_area);
25392 max_y = window_text_bottom_y (w);
25393
25394 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25395 of window. For TO_X > 0, truncate to end of drawing area. */
25396 if (to_x == 0)
25397 return;
25398 else if (to_x < 0)
25399 to_x = max_x;
25400 else
25401 to_x = min (to_x, max_x);
25402
25403 to_y = min (max_y, output_cursor.y + updated_row->height);
25404
25405 /* Notice if the cursor will be cleared by this operation. */
25406 if (!updated_row->full_width_p)
25407 notice_overwritten_cursor (w, updated_area,
25408 output_cursor.x, -1,
25409 updated_row->y,
25410 MATRIX_ROW_BOTTOM_Y (updated_row));
25411
25412 from_x = output_cursor.x;
25413
25414 /* Translate to frame coordinates. */
25415 if (updated_row->full_width_p)
25416 {
25417 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25418 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25419 }
25420 else
25421 {
25422 int area_left = window_box_left (w, updated_area);
25423 from_x += area_left;
25424 to_x += area_left;
25425 }
25426
25427 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25428 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25429 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25430
25431 /* Prevent inadvertently clearing to end of the X window. */
25432 if (to_x > from_x && to_y > from_y)
25433 {
25434 BLOCK_INPUT;
25435 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25436 to_x - from_x, to_y - from_y);
25437 UNBLOCK_INPUT;
25438 }
25439 }
25440
25441 #endif /* HAVE_WINDOW_SYSTEM */
25442
25443
25444 \f
25445 /***********************************************************************
25446 Cursor types
25447 ***********************************************************************/
25448
25449 /* Value is the internal representation of the specified cursor type
25450 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25451 of the bar cursor. */
25452
25453 static enum text_cursor_kinds
25454 get_specified_cursor_type (Lisp_Object arg, int *width)
25455 {
25456 enum text_cursor_kinds type;
25457
25458 if (NILP (arg))
25459 return NO_CURSOR;
25460
25461 if (EQ (arg, Qbox))
25462 return FILLED_BOX_CURSOR;
25463
25464 if (EQ (arg, Qhollow))
25465 return HOLLOW_BOX_CURSOR;
25466
25467 if (EQ (arg, Qbar))
25468 {
25469 *width = 2;
25470 return BAR_CURSOR;
25471 }
25472
25473 if (CONSP (arg)
25474 && EQ (XCAR (arg), Qbar)
25475 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25476 {
25477 *width = XINT (XCDR (arg));
25478 return BAR_CURSOR;
25479 }
25480
25481 if (EQ (arg, Qhbar))
25482 {
25483 *width = 2;
25484 return HBAR_CURSOR;
25485 }
25486
25487 if (CONSP (arg)
25488 && EQ (XCAR (arg), Qhbar)
25489 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25490 {
25491 *width = XINT (XCDR (arg));
25492 return HBAR_CURSOR;
25493 }
25494
25495 /* Treat anything unknown as "hollow box cursor".
25496 It was bad to signal an error; people have trouble fixing
25497 .Xdefaults with Emacs, when it has something bad in it. */
25498 type = HOLLOW_BOX_CURSOR;
25499
25500 return type;
25501 }
25502
25503 /* Set the default cursor types for specified frame. */
25504 void
25505 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25506 {
25507 int width = 1;
25508 Lisp_Object tem;
25509
25510 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25511 FRAME_CURSOR_WIDTH (f) = width;
25512
25513 /* By default, set up the blink-off state depending on the on-state. */
25514
25515 tem = Fassoc (arg, Vblink_cursor_alist);
25516 if (!NILP (tem))
25517 {
25518 FRAME_BLINK_OFF_CURSOR (f)
25519 = get_specified_cursor_type (XCDR (tem), &width);
25520 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25521 }
25522 else
25523 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25524 }
25525
25526
25527 #ifdef HAVE_WINDOW_SYSTEM
25528
25529 /* Return the cursor we want to be displayed in window W. Return
25530 width of bar/hbar cursor through WIDTH arg. Return with
25531 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25532 (i.e. if the `system caret' should track this cursor).
25533
25534 In a mini-buffer window, we want the cursor only to appear if we
25535 are reading input from this window. For the selected window, we
25536 want the cursor type given by the frame parameter or buffer local
25537 setting of cursor-type. If explicitly marked off, draw no cursor.
25538 In all other cases, we want a hollow box cursor. */
25539
25540 static enum text_cursor_kinds
25541 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25542 int *active_cursor)
25543 {
25544 struct frame *f = XFRAME (w->frame);
25545 struct buffer *b = XBUFFER (w->buffer);
25546 int cursor_type = DEFAULT_CURSOR;
25547 Lisp_Object alt_cursor;
25548 int non_selected = 0;
25549
25550 *active_cursor = 1;
25551
25552 /* Echo area */
25553 if (cursor_in_echo_area
25554 && FRAME_HAS_MINIBUF_P (f)
25555 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25556 {
25557 if (w == XWINDOW (echo_area_window))
25558 {
25559 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25560 {
25561 *width = FRAME_CURSOR_WIDTH (f);
25562 return FRAME_DESIRED_CURSOR (f);
25563 }
25564 else
25565 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25566 }
25567
25568 *active_cursor = 0;
25569 non_selected = 1;
25570 }
25571
25572 /* Detect a nonselected window or nonselected frame. */
25573 else if (w != XWINDOW (f->selected_window)
25574 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25575 {
25576 *active_cursor = 0;
25577
25578 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25579 return NO_CURSOR;
25580
25581 non_selected = 1;
25582 }
25583
25584 /* Never display a cursor in a window in which cursor-type is nil. */
25585 if (NILP (BVAR (b, cursor_type)))
25586 return NO_CURSOR;
25587
25588 /* Get the normal cursor type for this window. */
25589 if (EQ (BVAR (b, cursor_type), Qt))
25590 {
25591 cursor_type = FRAME_DESIRED_CURSOR (f);
25592 *width = FRAME_CURSOR_WIDTH (f);
25593 }
25594 else
25595 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25596
25597 /* Use cursor-in-non-selected-windows instead
25598 for non-selected window or frame. */
25599 if (non_selected)
25600 {
25601 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25602 if (!EQ (Qt, alt_cursor))
25603 return get_specified_cursor_type (alt_cursor, width);
25604 /* t means modify the normal cursor type. */
25605 if (cursor_type == FILLED_BOX_CURSOR)
25606 cursor_type = HOLLOW_BOX_CURSOR;
25607 else if (cursor_type == BAR_CURSOR && *width > 1)
25608 --*width;
25609 return cursor_type;
25610 }
25611
25612 /* Use normal cursor if not blinked off. */
25613 if (!w->cursor_off_p)
25614 {
25615 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25616 {
25617 if (cursor_type == FILLED_BOX_CURSOR)
25618 {
25619 /* Using a block cursor on large images can be very annoying.
25620 So use a hollow cursor for "large" images.
25621 If image is not transparent (no mask), also use hollow cursor. */
25622 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25623 if (img != NULL && IMAGEP (img->spec))
25624 {
25625 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25626 where N = size of default frame font size.
25627 This should cover most of the "tiny" icons people may use. */
25628 if (!img->mask
25629 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25630 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25631 cursor_type = HOLLOW_BOX_CURSOR;
25632 }
25633 }
25634 else if (cursor_type != NO_CURSOR)
25635 {
25636 /* Display current only supports BOX and HOLLOW cursors for images.
25637 So for now, unconditionally use a HOLLOW cursor when cursor is
25638 not a solid box cursor. */
25639 cursor_type = HOLLOW_BOX_CURSOR;
25640 }
25641 }
25642 return cursor_type;
25643 }
25644
25645 /* Cursor is blinked off, so determine how to "toggle" it. */
25646
25647 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25648 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25649 return get_specified_cursor_type (XCDR (alt_cursor), width);
25650
25651 /* Then see if frame has specified a specific blink off cursor type. */
25652 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25653 {
25654 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25655 return FRAME_BLINK_OFF_CURSOR (f);
25656 }
25657
25658 #if 0
25659 /* Some people liked having a permanently visible blinking cursor,
25660 while others had very strong opinions against it. So it was
25661 decided to remove it. KFS 2003-09-03 */
25662
25663 /* Finally perform built-in cursor blinking:
25664 filled box <-> hollow box
25665 wide [h]bar <-> narrow [h]bar
25666 narrow [h]bar <-> no cursor
25667 other type <-> no cursor */
25668
25669 if (cursor_type == FILLED_BOX_CURSOR)
25670 return HOLLOW_BOX_CURSOR;
25671
25672 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25673 {
25674 *width = 1;
25675 return cursor_type;
25676 }
25677 #endif
25678
25679 return NO_CURSOR;
25680 }
25681
25682
25683 /* Notice when the text cursor of window W has been completely
25684 overwritten by a drawing operation that outputs glyphs in AREA
25685 starting at X0 and ending at X1 in the line starting at Y0 and
25686 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25687 the rest of the line after X0 has been written. Y coordinates
25688 are window-relative. */
25689
25690 static void
25691 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25692 int x0, int x1, int y0, int y1)
25693 {
25694 int cx0, cx1, cy0, cy1;
25695 struct glyph_row *row;
25696
25697 if (!w->phys_cursor_on_p)
25698 return;
25699 if (area != TEXT_AREA)
25700 return;
25701
25702 if (w->phys_cursor.vpos < 0
25703 || w->phys_cursor.vpos >= w->current_matrix->nrows
25704 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25705 !(row->enabled_p && row->displays_text_p)))
25706 return;
25707
25708 if (row->cursor_in_fringe_p)
25709 {
25710 row->cursor_in_fringe_p = 0;
25711 draw_fringe_bitmap (w, row, row->reversed_p);
25712 w->phys_cursor_on_p = 0;
25713 return;
25714 }
25715
25716 cx0 = w->phys_cursor.x;
25717 cx1 = cx0 + w->phys_cursor_width;
25718 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25719 return;
25720
25721 /* The cursor image will be completely removed from the
25722 screen if the output area intersects the cursor area in
25723 y-direction. When we draw in [y0 y1[, and some part of
25724 the cursor is at y < y0, that part must have been drawn
25725 before. When scrolling, the cursor is erased before
25726 actually scrolling, so we don't come here. When not
25727 scrolling, the rows above the old cursor row must have
25728 changed, and in this case these rows must have written
25729 over the cursor image.
25730
25731 Likewise if part of the cursor is below y1, with the
25732 exception of the cursor being in the first blank row at
25733 the buffer and window end because update_text_area
25734 doesn't draw that row. (Except when it does, but
25735 that's handled in update_text_area.) */
25736
25737 cy0 = w->phys_cursor.y;
25738 cy1 = cy0 + w->phys_cursor_height;
25739 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25740 return;
25741
25742 w->phys_cursor_on_p = 0;
25743 }
25744
25745 #endif /* HAVE_WINDOW_SYSTEM */
25746
25747 \f
25748 /************************************************************************
25749 Mouse Face
25750 ************************************************************************/
25751
25752 #ifdef HAVE_WINDOW_SYSTEM
25753
25754 /* EXPORT for RIF:
25755 Fix the display of area AREA of overlapping row ROW in window W
25756 with respect to the overlapping part OVERLAPS. */
25757
25758 void
25759 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25760 enum glyph_row_area area, int overlaps)
25761 {
25762 int i, x;
25763
25764 BLOCK_INPUT;
25765
25766 x = 0;
25767 for (i = 0; i < row->used[area];)
25768 {
25769 if (row->glyphs[area][i].overlaps_vertically_p)
25770 {
25771 int start = i, start_x = x;
25772
25773 do
25774 {
25775 x += row->glyphs[area][i].pixel_width;
25776 ++i;
25777 }
25778 while (i < row->used[area]
25779 && row->glyphs[area][i].overlaps_vertically_p);
25780
25781 draw_glyphs (w, start_x, row, area,
25782 start, i,
25783 DRAW_NORMAL_TEXT, overlaps);
25784 }
25785 else
25786 {
25787 x += row->glyphs[area][i].pixel_width;
25788 ++i;
25789 }
25790 }
25791
25792 UNBLOCK_INPUT;
25793 }
25794
25795
25796 /* EXPORT:
25797 Draw the cursor glyph of window W in glyph row ROW. See the
25798 comment of draw_glyphs for the meaning of HL. */
25799
25800 void
25801 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25802 enum draw_glyphs_face hl)
25803 {
25804 /* If cursor hpos is out of bounds, don't draw garbage. This can
25805 happen in mini-buffer windows when switching between echo area
25806 glyphs and mini-buffer. */
25807 if ((row->reversed_p
25808 ? (w->phys_cursor.hpos >= 0)
25809 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25810 {
25811 int on_p = w->phys_cursor_on_p;
25812 int x1;
25813 int hpos = w->phys_cursor.hpos;
25814
25815 /* When the window is hscrolled, cursor hpos can legitimately be
25816 out of bounds, but we draw the cursor at the corresponding
25817 window margin in that case. */
25818 if (!row->reversed_p && hpos < 0)
25819 hpos = 0;
25820 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25821 hpos = row->used[TEXT_AREA] - 1;
25822
25823 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25824 hl, 0);
25825 w->phys_cursor_on_p = on_p;
25826
25827 if (hl == DRAW_CURSOR)
25828 w->phys_cursor_width = x1 - w->phys_cursor.x;
25829 /* When we erase the cursor, and ROW is overlapped by other
25830 rows, make sure that these overlapping parts of other rows
25831 are redrawn. */
25832 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25833 {
25834 w->phys_cursor_width = x1 - w->phys_cursor.x;
25835
25836 if (row > w->current_matrix->rows
25837 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25838 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25839 OVERLAPS_ERASED_CURSOR);
25840
25841 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25842 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25843 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25844 OVERLAPS_ERASED_CURSOR);
25845 }
25846 }
25847 }
25848
25849
25850 /* EXPORT:
25851 Erase the image of a cursor of window W from the screen. */
25852
25853 void
25854 erase_phys_cursor (struct window *w)
25855 {
25856 struct frame *f = XFRAME (w->frame);
25857 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25858 int hpos = w->phys_cursor.hpos;
25859 int vpos = w->phys_cursor.vpos;
25860 int mouse_face_here_p = 0;
25861 struct glyph_matrix *active_glyphs = w->current_matrix;
25862 struct glyph_row *cursor_row;
25863 struct glyph *cursor_glyph;
25864 enum draw_glyphs_face hl;
25865
25866 /* No cursor displayed or row invalidated => nothing to do on the
25867 screen. */
25868 if (w->phys_cursor_type == NO_CURSOR)
25869 goto mark_cursor_off;
25870
25871 /* VPOS >= active_glyphs->nrows means that window has been resized.
25872 Don't bother to erase the cursor. */
25873 if (vpos >= active_glyphs->nrows)
25874 goto mark_cursor_off;
25875
25876 /* If row containing cursor is marked invalid, there is nothing we
25877 can do. */
25878 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25879 if (!cursor_row->enabled_p)
25880 goto mark_cursor_off;
25881
25882 /* If line spacing is > 0, old cursor may only be partially visible in
25883 window after split-window. So adjust visible height. */
25884 cursor_row->visible_height = min (cursor_row->visible_height,
25885 window_text_bottom_y (w) - cursor_row->y);
25886
25887 /* If row is completely invisible, don't attempt to delete a cursor which
25888 isn't there. This can happen if cursor is at top of a window, and
25889 we switch to a buffer with a header line in that window. */
25890 if (cursor_row->visible_height <= 0)
25891 goto mark_cursor_off;
25892
25893 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25894 if (cursor_row->cursor_in_fringe_p)
25895 {
25896 cursor_row->cursor_in_fringe_p = 0;
25897 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25898 goto mark_cursor_off;
25899 }
25900
25901 /* This can happen when the new row is shorter than the old one.
25902 In this case, either draw_glyphs or clear_end_of_line
25903 should have cleared the cursor. Note that we wouldn't be
25904 able to erase the cursor in this case because we don't have a
25905 cursor glyph at hand. */
25906 if ((cursor_row->reversed_p
25907 ? (w->phys_cursor.hpos < 0)
25908 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25909 goto mark_cursor_off;
25910
25911 /* When the window is hscrolled, cursor hpos can legitimately be out
25912 of bounds, but we draw the cursor at the corresponding window
25913 margin in that case. */
25914 if (!cursor_row->reversed_p && hpos < 0)
25915 hpos = 0;
25916 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25917 hpos = cursor_row->used[TEXT_AREA] - 1;
25918
25919 /* If the cursor is in the mouse face area, redisplay that when
25920 we clear the cursor. */
25921 if (! NILP (hlinfo->mouse_face_window)
25922 && coords_in_mouse_face_p (w, hpos, vpos)
25923 /* Don't redraw the cursor's spot in mouse face if it is at the
25924 end of a line (on a newline). The cursor appears there, but
25925 mouse highlighting does not. */
25926 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25927 mouse_face_here_p = 1;
25928
25929 /* Maybe clear the display under the cursor. */
25930 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25931 {
25932 int x, y, left_x;
25933 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25934 int width;
25935
25936 cursor_glyph = get_phys_cursor_glyph (w);
25937 if (cursor_glyph == NULL)
25938 goto mark_cursor_off;
25939
25940 width = cursor_glyph->pixel_width;
25941 left_x = window_box_left_offset (w, TEXT_AREA);
25942 x = w->phys_cursor.x;
25943 if (x < left_x)
25944 width -= left_x - x;
25945 width = min (width, window_box_width (w, TEXT_AREA) - x);
25946 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25947 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25948
25949 if (width > 0)
25950 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25951 }
25952
25953 /* Erase the cursor by redrawing the character underneath it. */
25954 if (mouse_face_here_p)
25955 hl = DRAW_MOUSE_FACE;
25956 else
25957 hl = DRAW_NORMAL_TEXT;
25958 draw_phys_cursor_glyph (w, cursor_row, hl);
25959
25960 mark_cursor_off:
25961 w->phys_cursor_on_p = 0;
25962 w->phys_cursor_type = NO_CURSOR;
25963 }
25964
25965
25966 /* EXPORT:
25967 Display or clear cursor of window W. If ON is zero, clear the
25968 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25969 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25970
25971 void
25972 display_and_set_cursor (struct window *w, int on,
25973 int hpos, int vpos, int x, int y)
25974 {
25975 struct frame *f = XFRAME (w->frame);
25976 int new_cursor_type;
25977 int new_cursor_width;
25978 int active_cursor;
25979 struct glyph_row *glyph_row;
25980 struct glyph *glyph;
25981
25982 /* This is pointless on invisible frames, and dangerous on garbaged
25983 windows and frames; in the latter case, the frame or window may
25984 be in the midst of changing its size, and x and y may be off the
25985 window. */
25986 if (! FRAME_VISIBLE_P (f)
25987 || FRAME_GARBAGED_P (f)
25988 || vpos >= w->current_matrix->nrows
25989 || hpos >= w->current_matrix->matrix_w)
25990 return;
25991
25992 /* If cursor is off and we want it off, return quickly. */
25993 if (!on && !w->phys_cursor_on_p)
25994 return;
25995
25996 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25997 /* If cursor row is not enabled, we don't really know where to
25998 display the cursor. */
25999 if (!glyph_row->enabled_p)
26000 {
26001 w->phys_cursor_on_p = 0;
26002 return;
26003 }
26004
26005 glyph = NULL;
26006 if (!glyph_row->exact_window_width_line_p
26007 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26008 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26009
26010 eassert (interrupt_input_blocked);
26011
26012 /* Set new_cursor_type to the cursor we want to be displayed. */
26013 new_cursor_type = get_window_cursor_type (w, glyph,
26014 &new_cursor_width, &active_cursor);
26015
26016 /* If cursor is currently being shown and we don't want it to be or
26017 it is in the wrong place, or the cursor type is not what we want,
26018 erase it. */
26019 if (w->phys_cursor_on_p
26020 && (!on
26021 || w->phys_cursor.x != x
26022 || w->phys_cursor.y != y
26023 || new_cursor_type != w->phys_cursor_type
26024 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26025 && new_cursor_width != w->phys_cursor_width)))
26026 erase_phys_cursor (w);
26027
26028 /* Don't check phys_cursor_on_p here because that flag is only set
26029 to zero in some cases where we know that the cursor has been
26030 completely erased, to avoid the extra work of erasing the cursor
26031 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26032 still not be visible, or it has only been partly erased. */
26033 if (on)
26034 {
26035 w->phys_cursor_ascent = glyph_row->ascent;
26036 w->phys_cursor_height = glyph_row->height;
26037
26038 /* Set phys_cursor_.* before x_draw_.* is called because some
26039 of them may need the information. */
26040 w->phys_cursor.x = x;
26041 w->phys_cursor.y = glyph_row->y;
26042 w->phys_cursor.hpos = hpos;
26043 w->phys_cursor.vpos = vpos;
26044 }
26045
26046 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26047 new_cursor_type, new_cursor_width,
26048 on, active_cursor);
26049 }
26050
26051
26052 /* Switch the display of W's cursor on or off, according to the value
26053 of ON. */
26054
26055 static void
26056 update_window_cursor (struct window *w, int on)
26057 {
26058 /* Don't update cursor in windows whose frame is in the process
26059 of being deleted. */
26060 if (w->current_matrix)
26061 {
26062 int hpos = w->phys_cursor.hpos;
26063 int vpos = w->phys_cursor.vpos;
26064 struct glyph_row *row;
26065
26066 if (vpos >= w->current_matrix->nrows
26067 || hpos >= w->current_matrix->matrix_w)
26068 return;
26069
26070 row = MATRIX_ROW (w->current_matrix, vpos);
26071
26072 /* When the window is hscrolled, cursor hpos can legitimately be
26073 out of bounds, but we draw the cursor at the corresponding
26074 window margin in that case. */
26075 if (!row->reversed_p && hpos < 0)
26076 hpos = 0;
26077 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26078 hpos = row->used[TEXT_AREA] - 1;
26079
26080 BLOCK_INPUT;
26081 display_and_set_cursor (w, on, hpos, vpos,
26082 w->phys_cursor.x, w->phys_cursor.y);
26083 UNBLOCK_INPUT;
26084 }
26085 }
26086
26087
26088 /* Call update_window_cursor with parameter ON_P on all leaf windows
26089 in the window tree rooted at W. */
26090
26091 static void
26092 update_cursor_in_window_tree (struct window *w, int on_p)
26093 {
26094 while (w)
26095 {
26096 if (!NILP (w->hchild))
26097 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26098 else if (!NILP (w->vchild))
26099 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26100 else
26101 update_window_cursor (w, on_p);
26102
26103 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26104 }
26105 }
26106
26107
26108 /* EXPORT:
26109 Display the cursor on window W, or clear it, according to ON_P.
26110 Don't change the cursor's position. */
26111
26112 void
26113 x_update_cursor (struct frame *f, int on_p)
26114 {
26115 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26116 }
26117
26118
26119 /* EXPORT:
26120 Clear the cursor of window W to background color, and mark the
26121 cursor as not shown. This is used when the text where the cursor
26122 is about to be rewritten. */
26123
26124 void
26125 x_clear_cursor (struct window *w)
26126 {
26127 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26128 update_window_cursor (w, 0);
26129 }
26130
26131 #endif /* HAVE_WINDOW_SYSTEM */
26132
26133 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26134 and MSDOS. */
26135 static void
26136 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26137 int start_hpos, int end_hpos,
26138 enum draw_glyphs_face draw)
26139 {
26140 #ifdef HAVE_WINDOW_SYSTEM
26141 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26142 {
26143 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26144 return;
26145 }
26146 #endif
26147 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26148 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26149 #endif
26150 }
26151
26152 /* Display the active region described by mouse_face_* according to DRAW. */
26153
26154 static void
26155 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26156 {
26157 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26158 struct frame *f = XFRAME (WINDOW_FRAME (w));
26159
26160 if (/* If window is in the process of being destroyed, don't bother
26161 to do anything. */
26162 w->current_matrix != NULL
26163 /* Don't update mouse highlight if hidden */
26164 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26165 /* Recognize when we are called to operate on rows that don't exist
26166 anymore. This can happen when a window is split. */
26167 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26168 {
26169 int phys_cursor_on_p = w->phys_cursor_on_p;
26170 struct glyph_row *row, *first, *last;
26171
26172 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26173 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26174
26175 for (row = first; row <= last && row->enabled_p; ++row)
26176 {
26177 int start_hpos, end_hpos, start_x;
26178
26179 /* For all but the first row, the highlight starts at column 0. */
26180 if (row == first)
26181 {
26182 /* R2L rows have BEG and END in reversed order, but the
26183 screen drawing geometry is always left to right. So
26184 we need to mirror the beginning and end of the
26185 highlighted area in R2L rows. */
26186 if (!row->reversed_p)
26187 {
26188 start_hpos = hlinfo->mouse_face_beg_col;
26189 start_x = hlinfo->mouse_face_beg_x;
26190 }
26191 else if (row == last)
26192 {
26193 start_hpos = hlinfo->mouse_face_end_col;
26194 start_x = hlinfo->mouse_face_end_x;
26195 }
26196 else
26197 {
26198 start_hpos = 0;
26199 start_x = 0;
26200 }
26201 }
26202 else if (row->reversed_p && row == last)
26203 {
26204 start_hpos = hlinfo->mouse_face_end_col;
26205 start_x = hlinfo->mouse_face_end_x;
26206 }
26207 else
26208 {
26209 start_hpos = 0;
26210 start_x = 0;
26211 }
26212
26213 if (row == last)
26214 {
26215 if (!row->reversed_p)
26216 end_hpos = hlinfo->mouse_face_end_col;
26217 else if (row == first)
26218 end_hpos = hlinfo->mouse_face_beg_col;
26219 else
26220 {
26221 end_hpos = row->used[TEXT_AREA];
26222 if (draw == DRAW_NORMAL_TEXT)
26223 row->fill_line_p = 1; /* Clear to end of line */
26224 }
26225 }
26226 else if (row->reversed_p && row == first)
26227 end_hpos = hlinfo->mouse_face_beg_col;
26228 else
26229 {
26230 end_hpos = row->used[TEXT_AREA];
26231 if (draw == DRAW_NORMAL_TEXT)
26232 row->fill_line_p = 1; /* Clear to end of line */
26233 }
26234
26235 if (end_hpos > start_hpos)
26236 {
26237 draw_row_with_mouse_face (w, start_x, row,
26238 start_hpos, end_hpos, draw);
26239
26240 row->mouse_face_p
26241 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26242 }
26243 }
26244
26245 #ifdef HAVE_WINDOW_SYSTEM
26246 /* When we've written over the cursor, arrange for it to
26247 be displayed again. */
26248 if (FRAME_WINDOW_P (f)
26249 && phys_cursor_on_p && !w->phys_cursor_on_p)
26250 {
26251 int hpos = w->phys_cursor.hpos;
26252
26253 /* When the window is hscrolled, cursor hpos can legitimately be
26254 out of bounds, but we draw the cursor at the corresponding
26255 window margin in that case. */
26256 if (!row->reversed_p && hpos < 0)
26257 hpos = 0;
26258 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26259 hpos = row->used[TEXT_AREA] - 1;
26260
26261 BLOCK_INPUT;
26262 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26263 w->phys_cursor.x, w->phys_cursor.y);
26264 UNBLOCK_INPUT;
26265 }
26266 #endif /* HAVE_WINDOW_SYSTEM */
26267 }
26268
26269 #ifdef HAVE_WINDOW_SYSTEM
26270 /* Change the mouse cursor. */
26271 if (FRAME_WINDOW_P (f))
26272 {
26273 if (draw == DRAW_NORMAL_TEXT
26274 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26275 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26276 else if (draw == DRAW_MOUSE_FACE)
26277 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26278 else
26279 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26280 }
26281 #endif /* HAVE_WINDOW_SYSTEM */
26282 }
26283
26284 /* EXPORT:
26285 Clear out the mouse-highlighted active region.
26286 Redraw it un-highlighted first. Value is non-zero if mouse
26287 face was actually drawn unhighlighted. */
26288
26289 int
26290 clear_mouse_face (Mouse_HLInfo *hlinfo)
26291 {
26292 int cleared = 0;
26293
26294 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26295 {
26296 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26297 cleared = 1;
26298 }
26299
26300 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26301 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26302 hlinfo->mouse_face_window = Qnil;
26303 hlinfo->mouse_face_overlay = Qnil;
26304 return cleared;
26305 }
26306
26307 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26308 within the mouse face on that window. */
26309 static int
26310 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26311 {
26312 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26313
26314 /* Quickly resolve the easy cases. */
26315 if (!(WINDOWP (hlinfo->mouse_face_window)
26316 && XWINDOW (hlinfo->mouse_face_window) == w))
26317 return 0;
26318 if (vpos < hlinfo->mouse_face_beg_row
26319 || vpos > hlinfo->mouse_face_end_row)
26320 return 0;
26321 if (vpos > hlinfo->mouse_face_beg_row
26322 && vpos < hlinfo->mouse_face_end_row)
26323 return 1;
26324
26325 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26326 {
26327 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26328 {
26329 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26330 return 1;
26331 }
26332 else if ((vpos == hlinfo->mouse_face_beg_row
26333 && hpos >= hlinfo->mouse_face_beg_col)
26334 || (vpos == hlinfo->mouse_face_end_row
26335 && hpos < hlinfo->mouse_face_end_col))
26336 return 1;
26337 }
26338 else
26339 {
26340 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26341 {
26342 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26343 return 1;
26344 }
26345 else if ((vpos == hlinfo->mouse_face_beg_row
26346 && hpos <= hlinfo->mouse_face_beg_col)
26347 || (vpos == hlinfo->mouse_face_end_row
26348 && hpos > hlinfo->mouse_face_end_col))
26349 return 1;
26350 }
26351 return 0;
26352 }
26353
26354
26355 /* EXPORT:
26356 Non-zero if physical cursor of window W is within mouse face. */
26357
26358 int
26359 cursor_in_mouse_face_p (struct window *w)
26360 {
26361 int hpos = w->phys_cursor.hpos;
26362 int vpos = w->phys_cursor.vpos;
26363 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26364
26365 /* When the window is hscrolled, cursor hpos can legitimately be out
26366 of bounds, but we draw the cursor at the corresponding window
26367 margin in that case. */
26368 if (!row->reversed_p && hpos < 0)
26369 hpos = 0;
26370 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26371 hpos = row->used[TEXT_AREA] - 1;
26372
26373 return coords_in_mouse_face_p (w, hpos, vpos);
26374 }
26375
26376
26377 \f
26378 /* Find the glyph rows START_ROW and END_ROW of window W that display
26379 characters between buffer positions START_CHARPOS and END_CHARPOS
26380 (excluding END_CHARPOS). DISP_STRING is a display string that
26381 covers these buffer positions. This is similar to
26382 row_containing_pos, but is more accurate when bidi reordering makes
26383 buffer positions change non-linearly with glyph rows. */
26384 static void
26385 rows_from_pos_range (struct window *w,
26386 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26387 Lisp_Object disp_string,
26388 struct glyph_row **start, struct glyph_row **end)
26389 {
26390 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26391 int last_y = window_text_bottom_y (w);
26392 struct glyph_row *row;
26393
26394 *start = NULL;
26395 *end = NULL;
26396
26397 while (!first->enabled_p
26398 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26399 first++;
26400
26401 /* Find the START row. */
26402 for (row = first;
26403 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26404 row++)
26405 {
26406 /* A row can potentially be the START row if the range of the
26407 characters it displays intersects the range
26408 [START_CHARPOS..END_CHARPOS). */
26409 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26410 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26411 /* See the commentary in row_containing_pos, for the
26412 explanation of the complicated way to check whether
26413 some position is beyond the end of the characters
26414 displayed by a row. */
26415 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26416 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26417 && !row->ends_at_zv_p
26418 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26419 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26420 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26421 && !row->ends_at_zv_p
26422 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26423 {
26424 /* Found a candidate row. Now make sure at least one of the
26425 glyphs it displays has a charpos from the range
26426 [START_CHARPOS..END_CHARPOS).
26427
26428 This is not obvious because bidi reordering could make
26429 buffer positions of a row be 1,2,3,102,101,100, and if we
26430 want to highlight characters in [50..60), we don't want
26431 this row, even though [50..60) does intersect [1..103),
26432 the range of character positions given by the row's start
26433 and end positions. */
26434 struct glyph *g = row->glyphs[TEXT_AREA];
26435 struct glyph *e = g + row->used[TEXT_AREA];
26436
26437 while (g < e)
26438 {
26439 if (((BUFFERP (g->object) || INTEGERP (g->object))
26440 && start_charpos <= g->charpos && g->charpos < end_charpos)
26441 /* A glyph that comes from DISP_STRING is by
26442 definition to be highlighted. */
26443 || EQ (g->object, disp_string))
26444 *start = row;
26445 g++;
26446 }
26447 if (*start)
26448 break;
26449 }
26450 }
26451
26452 /* Find the END row. */
26453 if (!*start
26454 /* If the last row is partially visible, start looking for END
26455 from that row, instead of starting from FIRST. */
26456 && !(row->enabled_p
26457 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26458 row = first;
26459 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26460 {
26461 struct glyph_row *next = row + 1;
26462 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26463
26464 if (!next->enabled_p
26465 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26466 /* The first row >= START whose range of displayed characters
26467 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26468 is the row END + 1. */
26469 || (start_charpos < next_start
26470 && end_charpos < next_start)
26471 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26472 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26473 && !next->ends_at_zv_p
26474 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26475 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26476 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26477 && !next->ends_at_zv_p
26478 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26479 {
26480 *end = row;
26481 break;
26482 }
26483 else
26484 {
26485 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26486 but none of the characters it displays are in the range, it is
26487 also END + 1. */
26488 struct glyph *g = next->glyphs[TEXT_AREA];
26489 struct glyph *s = g;
26490 struct glyph *e = g + next->used[TEXT_AREA];
26491
26492 while (g < e)
26493 {
26494 if (((BUFFERP (g->object) || INTEGERP (g->object))
26495 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26496 /* If the buffer position of the first glyph in
26497 the row is equal to END_CHARPOS, it means
26498 the last character to be highlighted is the
26499 newline of ROW, and we must consider NEXT as
26500 END, not END+1. */
26501 || (((!next->reversed_p && g == s)
26502 || (next->reversed_p && g == e - 1))
26503 && (g->charpos == end_charpos
26504 /* Special case for when NEXT is an
26505 empty line at ZV. */
26506 || (g->charpos == -1
26507 && !row->ends_at_zv_p
26508 && next_start == end_charpos)))))
26509 /* A glyph that comes from DISP_STRING is by
26510 definition to be highlighted. */
26511 || EQ (g->object, disp_string))
26512 break;
26513 g++;
26514 }
26515 if (g == e)
26516 {
26517 *end = row;
26518 break;
26519 }
26520 /* The first row that ends at ZV must be the last to be
26521 highlighted. */
26522 else if (next->ends_at_zv_p)
26523 {
26524 *end = next;
26525 break;
26526 }
26527 }
26528 }
26529 }
26530
26531 /* This function sets the mouse_face_* elements of HLINFO, assuming
26532 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26533 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26534 for the overlay or run of text properties specifying the mouse
26535 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26536 before-string and after-string that must also be highlighted.
26537 DISP_STRING, if non-nil, is a display string that may cover some
26538 or all of the highlighted text. */
26539
26540 static void
26541 mouse_face_from_buffer_pos (Lisp_Object window,
26542 Mouse_HLInfo *hlinfo,
26543 ptrdiff_t mouse_charpos,
26544 ptrdiff_t start_charpos,
26545 ptrdiff_t end_charpos,
26546 Lisp_Object before_string,
26547 Lisp_Object after_string,
26548 Lisp_Object disp_string)
26549 {
26550 struct window *w = XWINDOW (window);
26551 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26552 struct glyph_row *r1, *r2;
26553 struct glyph *glyph, *end;
26554 ptrdiff_t ignore, pos;
26555 int x;
26556
26557 eassert (NILP (disp_string) || STRINGP (disp_string));
26558 eassert (NILP (before_string) || STRINGP (before_string));
26559 eassert (NILP (after_string) || STRINGP (after_string));
26560
26561 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26562 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26563 if (r1 == NULL)
26564 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26565 /* If the before-string or display-string contains newlines,
26566 rows_from_pos_range skips to its last row. Move back. */
26567 if (!NILP (before_string) || !NILP (disp_string))
26568 {
26569 struct glyph_row *prev;
26570 while ((prev = r1 - 1, prev >= first)
26571 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26572 && prev->used[TEXT_AREA] > 0)
26573 {
26574 struct glyph *beg = prev->glyphs[TEXT_AREA];
26575 glyph = beg + prev->used[TEXT_AREA];
26576 while (--glyph >= beg && INTEGERP (glyph->object));
26577 if (glyph < beg
26578 || !(EQ (glyph->object, before_string)
26579 || EQ (glyph->object, disp_string)))
26580 break;
26581 r1 = prev;
26582 }
26583 }
26584 if (r2 == NULL)
26585 {
26586 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26587 hlinfo->mouse_face_past_end = 1;
26588 }
26589 else if (!NILP (after_string))
26590 {
26591 /* If the after-string has newlines, advance to its last row. */
26592 struct glyph_row *next;
26593 struct glyph_row *last
26594 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26595
26596 for (next = r2 + 1;
26597 next <= last
26598 && next->used[TEXT_AREA] > 0
26599 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26600 ++next)
26601 r2 = next;
26602 }
26603 /* The rest of the display engine assumes that mouse_face_beg_row is
26604 either above mouse_face_end_row or identical to it. But with
26605 bidi-reordered continued lines, the row for START_CHARPOS could
26606 be below the row for END_CHARPOS. If so, swap the rows and store
26607 them in correct order. */
26608 if (r1->y > r2->y)
26609 {
26610 struct glyph_row *tem = r2;
26611
26612 r2 = r1;
26613 r1 = tem;
26614 }
26615
26616 hlinfo->mouse_face_beg_y = r1->y;
26617 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26618 hlinfo->mouse_face_end_y = r2->y;
26619 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26620
26621 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26622 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26623 could be anywhere in the row and in any order. The strategy
26624 below is to find the leftmost and the rightmost glyph that
26625 belongs to either of these 3 strings, or whose position is
26626 between START_CHARPOS and END_CHARPOS, and highlight all the
26627 glyphs between those two. This may cover more than just the text
26628 between START_CHARPOS and END_CHARPOS if the range of characters
26629 strides the bidi level boundary, e.g. if the beginning is in R2L
26630 text while the end is in L2R text or vice versa. */
26631 if (!r1->reversed_p)
26632 {
26633 /* This row is in a left to right paragraph. Scan it left to
26634 right. */
26635 glyph = r1->glyphs[TEXT_AREA];
26636 end = glyph + r1->used[TEXT_AREA];
26637 x = r1->x;
26638
26639 /* Skip truncation glyphs at the start of the glyph row. */
26640 if (r1->displays_text_p)
26641 for (; glyph < end
26642 && INTEGERP (glyph->object)
26643 && glyph->charpos < 0;
26644 ++glyph)
26645 x += glyph->pixel_width;
26646
26647 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26648 or DISP_STRING, and the first glyph from buffer whose
26649 position is between START_CHARPOS and END_CHARPOS. */
26650 for (; glyph < end
26651 && !INTEGERP (glyph->object)
26652 && !EQ (glyph->object, disp_string)
26653 && !(BUFFERP (glyph->object)
26654 && (glyph->charpos >= start_charpos
26655 && glyph->charpos < end_charpos));
26656 ++glyph)
26657 {
26658 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26659 are present at buffer positions between START_CHARPOS and
26660 END_CHARPOS, or if they come from an overlay. */
26661 if (EQ (glyph->object, before_string))
26662 {
26663 pos = string_buffer_position (before_string,
26664 start_charpos);
26665 /* If pos == 0, it means before_string came from an
26666 overlay, not from a buffer position. */
26667 if (!pos || (pos >= start_charpos && pos < end_charpos))
26668 break;
26669 }
26670 else if (EQ (glyph->object, after_string))
26671 {
26672 pos = string_buffer_position (after_string, end_charpos);
26673 if (!pos || (pos >= start_charpos && pos < end_charpos))
26674 break;
26675 }
26676 x += glyph->pixel_width;
26677 }
26678 hlinfo->mouse_face_beg_x = x;
26679 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26680 }
26681 else
26682 {
26683 /* This row is in a right to left paragraph. Scan it right to
26684 left. */
26685 struct glyph *g;
26686
26687 end = r1->glyphs[TEXT_AREA] - 1;
26688 glyph = end + r1->used[TEXT_AREA];
26689
26690 /* Skip truncation glyphs at the start of the glyph row. */
26691 if (r1->displays_text_p)
26692 for (; glyph > end
26693 && INTEGERP (glyph->object)
26694 && glyph->charpos < 0;
26695 --glyph)
26696 ;
26697
26698 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26699 or DISP_STRING, and the first glyph from buffer whose
26700 position is between START_CHARPOS and END_CHARPOS. */
26701 for (; glyph > end
26702 && !INTEGERP (glyph->object)
26703 && !EQ (glyph->object, disp_string)
26704 && !(BUFFERP (glyph->object)
26705 && (glyph->charpos >= start_charpos
26706 && glyph->charpos < end_charpos));
26707 --glyph)
26708 {
26709 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26710 are present at buffer positions between START_CHARPOS and
26711 END_CHARPOS, or if they come from an overlay. */
26712 if (EQ (glyph->object, before_string))
26713 {
26714 pos = string_buffer_position (before_string, start_charpos);
26715 /* If pos == 0, it means before_string came from an
26716 overlay, not from a buffer position. */
26717 if (!pos || (pos >= start_charpos && pos < end_charpos))
26718 break;
26719 }
26720 else if (EQ (glyph->object, after_string))
26721 {
26722 pos = string_buffer_position (after_string, end_charpos);
26723 if (!pos || (pos >= start_charpos && pos < end_charpos))
26724 break;
26725 }
26726 }
26727
26728 glyph++; /* first glyph to the right of the highlighted area */
26729 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26730 x += g->pixel_width;
26731 hlinfo->mouse_face_beg_x = x;
26732 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26733 }
26734
26735 /* If the highlight ends in a different row, compute GLYPH and END
26736 for the end row. Otherwise, reuse the values computed above for
26737 the row where the highlight begins. */
26738 if (r2 != r1)
26739 {
26740 if (!r2->reversed_p)
26741 {
26742 glyph = r2->glyphs[TEXT_AREA];
26743 end = glyph + r2->used[TEXT_AREA];
26744 x = r2->x;
26745 }
26746 else
26747 {
26748 end = r2->glyphs[TEXT_AREA] - 1;
26749 glyph = end + r2->used[TEXT_AREA];
26750 }
26751 }
26752
26753 if (!r2->reversed_p)
26754 {
26755 /* Skip truncation and continuation glyphs near the end of the
26756 row, and also blanks and stretch glyphs inserted by
26757 extend_face_to_end_of_line. */
26758 while (end > glyph
26759 && INTEGERP ((end - 1)->object))
26760 --end;
26761 /* Scan the rest of the glyph row from the end, looking for the
26762 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26763 DISP_STRING, or whose position is between START_CHARPOS
26764 and END_CHARPOS */
26765 for (--end;
26766 end > glyph
26767 && !INTEGERP (end->object)
26768 && !EQ (end->object, disp_string)
26769 && !(BUFFERP (end->object)
26770 && (end->charpos >= start_charpos
26771 && end->charpos < end_charpos));
26772 --end)
26773 {
26774 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26775 are present at buffer positions between START_CHARPOS and
26776 END_CHARPOS, or if they come from an overlay. */
26777 if (EQ (end->object, before_string))
26778 {
26779 pos = string_buffer_position (before_string, start_charpos);
26780 if (!pos || (pos >= start_charpos && pos < end_charpos))
26781 break;
26782 }
26783 else if (EQ (end->object, after_string))
26784 {
26785 pos = string_buffer_position (after_string, end_charpos);
26786 if (!pos || (pos >= start_charpos && pos < end_charpos))
26787 break;
26788 }
26789 }
26790 /* Find the X coordinate of the last glyph to be highlighted. */
26791 for (; glyph <= end; ++glyph)
26792 x += glyph->pixel_width;
26793
26794 hlinfo->mouse_face_end_x = x;
26795 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26796 }
26797 else
26798 {
26799 /* Skip truncation and continuation glyphs near the end of the
26800 row, and also blanks and stretch glyphs inserted by
26801 extend_face_to_end_of_line. */
26802 x = r2->x;
26803 end++;
26804 while (end < glyph
26805 && INTEGERP (end->object))
26806 {
26807 x += end->pixel_width;
26808 ++end;
26809 }
26810 /* Scan the rest of the glyph row from the end, looking for the
26811 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26812 DISP_STRING, or whose position is between START_CHARPOS
26813 and END_CHARPOS */
26814 for ( ;
26815 end < glyph
26816 && !INTEGERP (end->object)
26817 && !EQ (end->object, disp_string)
26818 && !(BUFFERP (end->object)
26819 && (end->charpos >= start_charpos
26820 && end->charpos < end_charpos));
26821 ++end)
26822 {
26823 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26824 are present at buffer positions between START_CHARPOS and
26825 END_CHARPOS, or if they come from an overlay. */
26826 if (EQ (end->object, before_string))
26827 {
26828 pos = string_buffer_position (before_string, start_charpos);
26829 if (!pos || (pos >= start_charpos && pos < end_charpos))
26830 break;
26831 }
26832 else if (EQ (end->object, after_string))
26833 {
26834 pos = string_buffer_position (after_string, end_charpos);
26835 if (!pos || (pos >= start_charpos && pos < end_charpos))
26836 break;
26837 }
26838 x += end->pixel_width;
26839 }
26840 /* If we exited the above loop because we arrived at the last
26841 glyph of the row, and its buffer position is still not in
26842 range, it means the last character in range is the preceding
26843 newline. Bump the end column and x values to get past the
26844 last glyph. */
26845 if (end == glyph
26846 && BUFFERP (end->object)
26847 && (end->charpos < start_charpos
26848 || end->charpos >= end_charpos))
26849 {
26850 x += end->pixel_width;
26851 ++end;
26852 }
26853 hlinfo->mouse_face_end_x = x;
26854 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26855 }
26856
26857 hlinfo->mouse_face_window = window;
26858 hlinfo->mouse_face_face_id
26859 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26860 mouse_charpos + 1,
26861 !hlinfo->mouse_face_hidden, -1);
26862 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26863 }
26864
26865 /* The following function is not used anymore (replaced with
26866 mouse_face_from_string_pos), but I leave it here for the time
26867 being, in case someone would. */
26868
26869 #if 0 /* not used */
26870
26871 /* Find the position of the glyph for position POS in OBJECT in
26872 window W's current matrix, and return in *X, *Y the pixel
26873 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26874
26875 RIGHT_P non-zero means return the position of the right edge of the
26876 glyph, RIGHT_P zero means return the left edge position.
26877
26878 If no glyph for POS exists in the matrix, return the position of
26879 the glyph with the next smaller position that is in the matrix, if
26880 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26881 exists in the matrix, return the position of the glyph with the
26882 next larger position in OBJECT.
26883
26884 Value is non-zero if a glyph was found. */
26885
26886 static int
26887 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26888 int *hpos, int *vpos, int *x, int *y, int right_p)
26889 {
26890 int yb = window_text_bottom_y (w);
26891 struct glyph_row *r;
26892 struct glyph *best_glyph = NULL;
26893 struct glyph_row *best_row = NULL;
26894 int best_x = 0;
26895
26896 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26897 r->enabled_p && r->y < yb;
26898 ++r)
26899 {
26900 struct glyph *g = r->glyphs[TEXT_AREA];
26901 struct glyph *e = g + r->used[TEXT_AREA];
26902 int gx;
26903
26904 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26905 if (EQ (g->object, object))
26906 {
26907 if (g->charpos == pos)
26908 {
26909 best_glyph = g;
26910 best_x = gx;
26911 best_row = r;
26912 goto found;
26913 }
26914 else if (best_glyph == NULL
26915 || ((eabs (g->charpos - pos)
26916 < eabs (best_glyph->charpos - pos))
26917 && (right_p
26918 ? g->charpos < pos
26919 : g->charpos > pos)))
26920 {
26921 best_glyph = g;
26922 best_x = gx;
26923 best_row = r;
26924 }
26925 }
26926 }
26927
26928 found:
26929
26930 if (best_glyph)
26931 {
26932 *x = best_x;
26933 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26934
26935 if (right_p)
26936 {
26937 *x += best_glyph->pixel_width;
26938 ++*hpos;
26939 }
26940
26941 *y = best_row->y;
26942 *vpos = best_row - w->current_matrix->rows;
26943 }
26944
26945 return best_glyph != NULL;
26946 }
26947 #endif /* not used */
26948
26949 /* Find the positions of the first and the last glyphs in window W's
26950 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26951 (assumed to be a string), and return in HLINFO's mouse_face_*
26952 members the pixel and column/row coordinates of those glyphs. */
26953
26954 static void
26955 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26956 Lisp_Object object,
26957 ptrdiff_t startpos, ptrdiff_t endpos)
26958 {
26959 int yb = window_text_bottom_y (w);
26960 struct glyph_row *r;
26961 struct glyph *g, *e;
26962 int gx;
26963 int found = 0;
26964
26965 /* Find the glyph row with at least one position in the range
26966 [STARTPOS..ENDPOS], and the first glyph in that row whose
26967 position belongs to that range. */
26968 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26969 r->enabled_p && r->y < yb;
26970 ++r)
26971 {
26972 if (!r->reversed_p)
26973 {
26974 g = r->glyphs[TEXT_AREA];
26975 e = g + r->used[TEXT_AREA];
26976 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26977 if (EQ (g->object, object)
26978 && startpos <= g->charpos && g->charpos <= endpos)
26979 {
26980 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26981 hlinfo->mouse_face_beg_y = r->y;
26982 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26983 hlinfo->mouse_face_beg_x = gx;
26984 found = 1;
26985 break;
26986 }
26987 }
26988 else
26989 {
26990 struct glyph *g1;
26991
26992 e = r->glyphs[TEXT_AREA];
26993 g = e + r->used[TEXT_AREA];
26994 for ( ; g > e; --g)
26995 if (EQ ((g-1)->object, object)
26996 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26997 {
26998 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26999 hlinfo->mouse_face_beg_y = r->y;
27000 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27001 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27002 gx += g1->pixel_width;
27003 hlinfo->mouse_face_beg_x = gx;
27004 found = 1;
27005 break;
27006 }
27007 }
27008 if (found)
27009 break;
27010 }
27011
27012 if (!found)
27013 return;
27014
27015 /* Starting with the next row, look for the first row which does NOT
27016 include any glyphs whose positions are in the range. */
27017 for (++r; r->enabled_p && r->y < yb; ++r)
27018 {
27019 g = r->glyphs[TEXT_AREA];
27020 e = g + r->used[TEXT_AREA];
27021 found = 0;
27022 for ( ; g < e; ++g)
27023 if (EQ (g->object, object)
27024 && startpos <= g->charpos && g->charpos <= endpos)
27025 {
27026 found = 1;
27027 break;
27028 }
27029 if (!found)
27030 break;
27031 }
27032
27033 /* The highlighted region ends on the previous row. */
27034 r--;
27035
27036 /* Set the end row and its vertical pixel coordinate. */
27037 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27038 hlinfo->mouse_face_end_y = r->y;
27039
27040 /* Compute and set the end column and the end column's horizontal
27041 pixel coordinate. */
27042 if (!r->reversed_p)
27043 {
27044 g = r->glyphs[TEXT_AREA];
27045 e = g + r->used[TEXT_AREA];
27046 for ( ; e > g; --e)
27047 if (EQ ((e-1)->object, object)
27048 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27049 break;
27050 hlinfo->mouse_face_end_col = e - g;
27051
27052 for (gx = r->x; g < e; ++g)
27053 gx += g->pixel_width;
27054 hlinfo->mouse_face_end_x = gx;
27055 }
27056 else
27057 {
27058 e = r->glyphs[TEXT_AREA];
27059 g = e + r->used[TEXT_AREA];
27060 for (gx = r->x ; e < g; ++e)
27061 {
27062 if (EQ (e->object, object)
27063 && startpos <= e->charpos && e->charpos <= endpos)
27064 break;
27065 gx += e->pixel_width;
27066 }
27067 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27068 hlinfo->mouse_face_end_x = gx;
27069 }
27070 }
27071
27072 #ifdef HAVE_WINDOW_SYSTEM
27073
27074 /* See if position X, Y is within a hot-spot of an image. */
27075
27076 static int
27077 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27078 {
27079 if (!CONSP (hot_spot))
27080 return 0;
27081
27082 if (EQ (XCAR (hot_spot), Qrect))
27083 {
27084 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27085 Lisp_Object rect = XCDR (hot_spot);
27086 Lisp_Object tem;
27087 if (!CONSP (rect))
27088 return 0;
27089 if (!CONSP (XCAR (rect)))
27090 return 0;
27091 if (!CONSP (XCDR (rect)))
27092 return 0;
27093 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27094 return 0;
27095 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27096 return 0;
27097 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27098 return 0;
27099 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27100 return 0;
27101 return 1;
27102 }
27103 else if (EQ (XCAR (hot_spot), Qcircle))
27104 {
27105 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27106 Lisp_Object circ = XCDR (hot_spot);
27107 Lisp_Object lr, lx0, ly0;
27108 if (CONSP (circ)
27109 && CONSP (XCAR (circ))
27110 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27111 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27112 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27113 {
27114 double r = XFLOATINT (lr);
27115 double dx = XINT (lx0) - x;
27116 double dy = XINT (ly0) - y;
27117 return (dx * dx + dy * dy <= r * r);
27118 }
27119 }
27120 else if (EQ (XCAR (hot_spot), Qpoly))
27121 {
27122 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27123 if (VECTORP (XCDR (hot_spot)))
27124 {
27125 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27126 Lisp_Object *poly = v->contents;
27127 ptrdiff_t n = v->header.size;
27128 ptrdiff_t i;
27129 int inside = 0;
27130 Lisp_Object lx, ly;
27131 int x0, y0;
27132
27133 /* Need an even number of coordinates, and at least 3 edges. */
27134 if (n < 6 || n & 1)
27135 return 0;
27136
27137 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27138 If count is odd, we are inside polygon. Pixels on edges
27139 may or may not be included depending on actual geometry of the
27140 polygon. */
27141 if ((lx = poly[n-2], !INTEGERP (lx))
27142 || (ly = poly[n-1], !INTEGERP (lx)))
27143 return 0;
27144 x0 = XINT (lx), y0 = XINT (ly);
27145 for (i = 0; i < n; i += 2)
27146 {
27147 int x1 = x0, y1 = y0;
27148 if ((lx = poly[i], !INTEGERP (lx))
27149 || (ly = poly[i+1], !INTEGERP (ly)))
27150 return 0;
27151 x0 = XINT (lx), y0 = XINT (ly);
27152
27153 /* Does this segment cross the X line? */
27154 if (x0 >= x)
27155 {
27156 if (x1 >= x)
27157 continue;
27158 }
27159 else if (x1 < x)
27160 continue;
27161 if (y > y0 && y > y1)
27162 continue;
27163 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27164 inside = !inside;
27165 }
27166 return inside;
27167 }
27168 }
27169 return 0;
27170 }
27171
27172 Lisp_Object
27173 find_hot_spot (Lisp_Object map, int x, int y)
27174 {
27175 while (CONSP (map))
27176 {
27177 if (CONSP (XCAR (map))
27178 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27179 return XCAR (map);
27180 map = XCDR (map);
27181 }
27182
27183 return Qnil;
27184 }
27185
27186 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27187 3, 3, 0,
27188 doc: /* Lookup in image map MAP coordinates X and Y.
27189 An image map is an alist where each element has the format (AREA ID PLIST).
27190 An AREA is specified as either a rectangle, a circle, or a polygon:
27191 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27192 pixel coordinates of the upper left and bottom right corners.
27193 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27194 and the radius of the circle; r may be a float or integer.
27195 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27196 vector describes one corner in the polygon.
27197 Returns the alist element for the first matching AREA in MAP. */)
27198 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27199 {
27200 if (NILP (map))
27201 return Qnil;
27202
27203 CHECK_NUMBER (x);
27204 CHECK_NUMBER (y);
27205
27206 return find_hot_spot (map,
27207 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27208 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27209 }
27210
27211
27212 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27213 static void
27214 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27215 {
27216 /* Do not change cursor shape while dragging mouse. */
27217 if (!NILP (do_mouse_tracking))
27218 return;
27219
27220 if (!NILP (pointer))
27221 {
27222 if (EQ (pointer, Qarrow))
27223 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27224 else if (EQ (pointer, Qhand))
27225 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27226 else if (EQ (pointer, Qtext))
27227 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27228 else if (EQ (pointer, intern ("hdrag")))
27229 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27230 #ifdef HAVE_X_WINDOWS
27231 else if (EQ (pointer, intern ("vdrag")))
27232 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27233 #endif
27234 else if (EQ (pointer, intern ("hourglass")))
27235 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27236 else if (EQ (pointer, Qmodeline))
27237 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27238 else
27239 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27240 }
27241
27242 if (cursor != No_Cursor)
27243 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27244 }
27245
27246 #endif /* HAVE_WINDOW_SYSTEM */
27247
27248 /* Take proper action when mouse has moved to the mode or header line
27249 or marginal area AREA of window W, x-position X and y-position Y.
27250 X is relative to the start of the text display area of W, so the
27251 width of bitmap areas and scroll bars must be subtracted to get a
27252 position relative to the start of the mode line. */
27253
27254 static void
27255 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27256 enum window_part area)
27257 {
27258 struct window *w = XWINDOW (window);
27259 struct frame *f = XFRAME (w->frame);
27260 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27261 #ifdef HAVE_WINDOW_SYSTEM
27262 Display_Info *dpyinfo;
27263 #endif
27264 Cursor cursor = No_Cursor;
27265 Lisp_Object pointer = Qnil;
27266 int dx, dy, width, height;
27267 ptrdiff_t charpos;
27268 Lisp_Object string, object = Qnil;
27269 Lisp_Object pos IF_LINT (= Qnil), help;
27270
27271 Lisp_Object mouse_face;
27272 int original_x_pixel = x;
27273 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27274 struct glyph_row *row IF_LINT (= 0);
27275
27276 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27277 {
27278 int x0;
27279 struct glyph *end;
27280
27281 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27282 returns them in row/column units! */
27283 string = mode_line_string (w, area, &x, &y, &charpos,
27284 &object, &dx, &dy, &width, &height);
27285
27286 row = (area == ON_MODE_LINE
27287 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27288 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27289
27290 /* Find the glyph under the mouse pointer. */
27291 if (row->mode_line_p && row->enabled_p)
27292 {
27293 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27294 end = glyph + row->used[TEXT_AREA];
27295
27296 for (x0 = original_x_pixel;
27297 glyph < end && x0 >= glyph->pixel_width;
27298 ++glyph)
27299 x0 -= glyph->pixel_width;
27300
27301 if (glyph >= end)
27302 glyph = NULL;
27303 }
27304 }
27305 else
27306 {
27307 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27308 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27309 returns them in row/column units! */
27310 string = marginal_area_string (w, area, &x, &y, &charpos,
27311 &object, &dx, &dy, &width, &height);
27312 }
27313
27314 help = Qnil;
27315
27316 #ifdef HAVE_WINDOW_SYSTEM
27317 if (IMAGEP (object))
27318 {
27319 Lisp_Object image_map, hotspot;
27320 if ((image_map = Fplist_get (XCDR (object), QCmap),
27321 !NILP (image_map))
27322 && (hotspot = find_hot_spot (image_map, dx, dy),
27323 CONSP (hotspot))
27324 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27325 {
27326 Lisp_Object plist;
27327
27328 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27329 If so, we could look for mouse-enter, mouse-leave
27330 properties in PLIST (and do something...). */
27331 hotspot = XCDR (hotspot);
27332 if (CONSP (hotspot)
27333 && (plist = XCAR (hotspot), CONSP (plist)))
27334 {
27335 pointer = Fplist_get (plist, Qpointer);
27336 if (NILP (pointer))
27337 pointer = Qhand;
27338 help = Fplist_get (plist, Qhelp_echo);
27339 if (!NILP (help))
27340 {
27341 help_echo_string = help;
27342 XSETWINDOW (help_echo_window, w);
27343 help_echo_object = w->buffer;
27344 help_echo_pos = charpos;
27345 }
27346 }
27347 }
27348 if (NILP (pointer))
27349 pointer = Fplist_get (XCDR (object), QCpointer);
27350 }
27351 #endif /* HAVE_WINDOW_SYSTEM */
27352
27353 if (STRINGP (string))
27354 pos = make_number (charpos);
27355
27356 /* Set the help text and mouse pointer. If the mouse is on a part
27357 of the mode line without any text (e.g. past the right edge of
27358 the mode line text), use the default help text and pointer. */
27359 if (STRINGP (string) || area == ON_MODE_LINE)
27360 {
27361 /* Arrange to display the help by setting the global variables
27362 help_echo_string, help_echo_object, and help_echo_pos. */
27363 if (NILP (help))
27364 {
27365 if (STRINGP (string))
27366 help = Fget_text_property (pos, Qhelp_echo, string);
27367
27368 if (!NILP (help))
27369 {
27370 help_echo_string = help;
27371 XSETWINDOW (help_echo_window, w);
27372 help_echo_object = string;
27373 help_echo_pos = charpos;
27374 }
27375 else if (area == ON_MODE_LINE)
27376 {
27377 Lisp_Object default_help
27378 = buffer_local_value_1 (Qmode_line_default_help_echo,
27379 w->buffer);
27380
27381 if (STRINGP (default_help))
27382 {
27383 help_echo_string = default_help;
27384 XSETWINDOW (help_echo_window, w);
27385 help_echo_object = Qnil;
27386 help_echo_pos = -1;
27387 }
27388 }
27389 }
27390
27391 #ifdef HAVE_WINDOW_SYSTEM
27392 /* Change the mouse pointer according to what is under it. */
27393 if (FRAME_WINDOW_P (f))
27394 {
27395 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27396 if (STRINGP (string))
27397 {
27398 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27399
27400 if (NILP (pointer))
27401 pointer = Fget_text_property (pos, Qpointer, string);
27402
27403 /* Change the mouse pointer according to what is under X/Y. */
27404 if (NILP (pointer)
27405 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27406 {
27407 Lisp_Object map;
27408 map = Fget_text_property (pos, Qlocal_map, string);
27409 if (!KEYMAPP (map))
27410 map = Fget_text_property (pos, Qkeymap, string);
27411 if (!KEYMAPP (map))
27412 cursor = dpyinfo->vertical_scroll_bar_cursor;
27413 }
27414 }
27415 else
27416 /* Default mode-line pointer. */
27417 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27418 }
27419 #endif
27420 }
27421
27422 /* Change the mouse face according to what is under X/Y. */
27423 if (STRINGP (string))
27424 {
27425 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27426 if (!NILP (mouse_face)
27427 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27428 && glyph)
27429 {
27430 Lisp_Object b, e;
27431
27432 struct glyph * tmp_glyph;
27433
27434 int gpos;
27435 int gseq_length;
27436 int total_pixel_width;
27437 ptrdiff_t begpos, endpos, ignore;
27438
27439 int vpos, hpos;
27440
27441 b = Fprevious_single_property_change (make_number (charpos + 1),
27442 Qmouse_face, string, Qnil);
27443 if (NILP (b))
27444 begpos = 0;
27445 else
27446 begpos = XINT (b);
27447
27448 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27449 if (NILP (e))
27450 endpos = SCHARS (string);
27451 else
27452 endpos = XINT (e);
27453
27454 /* Calculate the glyph position GPOS of GLYPH in the
27455 displayed string, relative to the beginning of the
27456 highlighted part of the string.
27457
27458 Note: GPOS is different from CHARPOS. CHARPOS is the
27459 position of GLYPH in the internal string object. A mode
27460 line string format has structures which are converted to
27461 a flattened string by the Emacs Lisp interpreter. The
27462 internal string is an element of those structures. The
27463 displayed string is the flattened string. */
27464 tmp_glyph = row_start_glyph;
27465 while (tmp_glyph < glyph
27466 && (!(EQ (tmp_glyph->object, glyph->object)
27467 && begpos <= tmp_glyph->charpos
27468 && tmp_glyph->charpos < endpos)))
27469 tmp_glyph++;
27470 gpos = glyph - tmp_glyph;
27471
27472 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27473 the highlighted part of the displayed string to which
27474 GLYPH belongs. Note: GSEQ_LENGTH is different from
27475 SCHARS (STRING), because the latter returns the length of
27476 the internal string. */
27477 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27478 tmp_glyph > glyph
27479 && (!(EQ (tmp_glyph->object, glyph->object)
27480 && begpos <= tmp_glyph->charpos
27481 && tmp_glyph->charpos < endpos));
27482 tmp_glyph--)
27483 ;
27484 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27485
27486 /* Calculate the total pixel width of all the glyphs between
27487 the beginning of the highlighted area and GLYPH. */
27488 total_pixel_width = 0;
27489 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27490 total_pixel_width += tmp_glyph->pixel_width;
27491
27492 /* Pre calculation of re-rendering position. Note: X is in
27493 column units here, after the call to mode_line_string or
27494 marginal_area_string. */
27495 hpos = x - gpos;
27496 vpos = (area == ON_MODE_LINE
27497 ? (w->current_matrix)->nrows - 1
27498 : 0);
27499
27500 /* If GLYPH's position is included in the region that is
27501 already drawn in mouse face, we have nothing to do. */
27502 if ( EQ (window, hlinfo->mouse_face_window)
27503 && (!row->reversed_p
27504 ? (hlinfo->mouse_face_beg_col <= hpos
27505 && hpos < hlinfo->mouse_face_end_col)
27506 /* In R2L rows we swap BEG and END, see below. */
27507 : (hlinfo->mouse_face_end_col <= hpos
27508 && hpos < hlinfo->mouse_face_beg_col))
27509 && hlinfo->mouse_face_beg_row == vpos )
27510 return;
27511
27512 if (clear_mouse_face (hlinfo))
27513 cursor = No_Cursor;
27514
27515 if (!row->reversed_p)
27516 {
27517 hlinfo->mouse_face_beg_col = hpos;
27518 hlinfo->mouse_face_beg_x = original_x_pixel
27519 - (total_pixel_width + dx);
27520 hlinfo->mouse_face_end_col = hpos + gseq_length;
27521 hlinfo->mouse_face_end_x = 0;
27522 }
27523 else
27524 {
27525 /* In R2L rows, show_mouse_face expects BEG and END
27526 coordinates to be swapped. */
27527 hlinfo->mouse_face_end_col = hpos;
27528 hlinfo->mouse_face_end_x = original_x_pixel
27529 - (total_pixel_width + dx);
27530 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27531 hlinfo->mouse_face_beg_x = 0;
27532 }
27533
27534 hlinfo->mouse_face_beg_row = vpos;
27535 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27536 hlinfo->mouse_face_beg_y = 0;
27537 hlinfo->mouse_face_end_y = 0;
27538 hlinfo->mouse_face_past_end = 0;
27539 hlinfo->mouse_face_window = window;
27540
27541 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27542 charpos,
27543 0, 0, 0,
27544 &ignore,
27545 glyph->face_id,
27546 1);
27547 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27548
27549 if (NILP (pointer))
27550 pointer = Qhand;
27551 }
27552 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27553 clear_mouse_face (hlinfo);
27554 }
27555 #ifdef HAVE_WINDOW_SYSTEM
27556 if (FRAME_WINDOW_P (f))
27557 define_frame_cursor1 (f, cursor, pointer);
27558 #endif
27559 }
27560
27561
27562 /* EXPORT:
27563 Take proper action when the mouse has moved to position X, Y on
27564 frame F as regards highlighting characters that have mouse-face
27565 properties. Also de-highlighting chars where the mouse was before.
27566 X and Y can be negative or out of range. */
27567
27568 void
27569 note_mouse_highlight (struct frame *f, int x, int y)
27570 {
27571 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27572 enum window_part part = ON_NOTHING;
27573 Lisp_Object window;
27574 struct window *w;
27575 Cursor cursor = No_Cursor;
27576 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27577 struct buffer *b;
27578
27579 /* When a menu is active, don't highlight because this looks odd. */
27580 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27581 if (popup_activated ())
27582 return;
27583 #endif
27584
27585 if (NILP (Vmouse_highlight)
27586 || !f->glyphs_initialized_p
27587 || f->pointer_invisible)
27588 return;
27589
27590 hlinfo->mouse_face_mouse_x = x;
27591 hlinfo->mouse_face_mouse_y = y;
27592 hlinfo->mouse_face_mouse_frame = f;
27593
27594 if (hlinfo->mouse_face_defer)
27595 return;
27596
27597 if (gc_in_progress)
27598 {
27599 hlinfo->mouse_face_deferred_gc = 1;
27600 return;
27601 }
27602
27603 /* Which window is that in? */
27604 window = window_from_coordinates (f, x, y, &part, 1);
27605
27606 /* If displaying active text in another window, clear that. */
27607 if (! EQ (window, hlinfo->mouse_face_window)
27608 /* Also clear if we move out of text area in same window. */
27609 || (!NILP (hlinfo->mouse_face_window)
27610 && !NILP (window)
27611 && part != ON_TEXT
27612 && part != ON_MODE_LINE
27613 && part != ON_HEADER_LINE))
27614 clear_mouse_face (hlinfo);
27615
27616 /* Not on a window -> return. */
27617 if (!WINDOWP (window))
27618 return;
27619
27620 /* Reset help_echo_string. It will get recomputed below. */
27621 help_echo_string = Qnil;
27622
27623 /* Convert to window-relative pixel coordinates. */
27624 w = XWINDOW (window);
27625 frame_to_window_pixel_xy (w, &x, &y);
27626
27627 #ifdef HAVE_WINDOW_SYSTEM
27628 /* Handle tool-bar window differently since it doesn't display a
27629 buffer. */
27630 if (EQ (window, f->tool_bar_window))
27631 {
27632 note_tool_bar_highlight (f, x, y);
27633 return;
27634 }
27635 #endif
27636
27637 /* Mouse is on the mode, header line or margin? */
27638 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27639 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27640 {
27641 note_mode_line_or_margin_highlight (window, x, y, part);
27642 return;
27643 }
27644
27645 #ifdef HAVE_WINDOW_SYSTEM
27646 if (part == ON_VERTICAL_BORDER)
27647 {
27648 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27649 help_echo_string = build_string ("drag-mouse-1: resize");
27650 }
27651 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27652 || part == ON_SCROLL_BAR)
27653 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27654 else
27655 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27656 #endif
27657
27658 /* Are we in a window whose display is up to date?
27659 And verify the buffer's text has not changed. */
27660 b = XBUFFER (w->buffer);
27661 if (part == ON_TEXT
27662 && EQ (w->window_end_valid, w->buffer)
27663 && w->last_modified == BUF_MODIFF (b)
27664 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27665 {
27666 int hpos, vpos, dx, dy, area = LAST_AREA;
27667 ptrdiff_t pos;
27668 struct glyph *glyph;
27669 Lisp_Object object;
27670 Lisp_Object mouse_face = Qnil, position;
27671 Lisp_Object *overlay_vec = NULL;
27672 ptrdiff_t i, noverlays;
27673 struct buffer *obuf;
27674 ptrdiff_t obegv, ozv;
27675 int same_region;
27676
27677 /* Find the glyph under X/Y. */
27678 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27679
27680 #ifdef HAVE_WINDOW_SYSTEM
27681 /* Look for :pointer property on image. */
27682 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27683 {
27684 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27685 if (img != NULL && IMAGEP (img->spec))
27686 {
27687 Lisp_Object image_map, hotspot;
27688 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27689 !NILP (image_map))
27690 && (hotspot = find_hot_spot (image_map,
27691 glyph->slice.img.x + dx,
27692 glyph->slice.img.y + dy),
27693 CONSP (hotspot))
27694 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27695 {
27696 Lisp_Object plist;
27697
27698 /* Could check XCAR (hotspot) to see if we enter/leave
27699 this hot-spot.
27700 If so, we could look for mouse-enter, mouse-leave
27701 properties in PLIST (and do something...). */
27702 hotspot = XCDR (hotspot);
27703 if (CONSP (hotspot)
27704 && (plist = XCAR (hotspot), CONSP (plist)))
27705 {
27706 pointer = Fplist_get (plist, Qpointer);
27707 if (NILP (pointer))
27708 pointer = Qhand;
27709 help_echo_string = Fplist_get (plist, Qhelp_echo);
27710 if (!NILP (help_echo_string))
27711 {
27712 help_echo_window = window;
27713 help_echo_object = glyph->object;
27714 help_echo_pos = glyph->charpos;
27715 }
27716 }
27717 }
27718 if (NILP (pointer))
27719 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27720 }
27721 }
27722 #endif /* HAVE_WINDOW_SYSTEM */
27723
27724 /* Clear mouse face if X/Y not over text. */
27725 if (glyph == NULL
27726 || area != TEXT_AREA
27727 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27728 /* Glyph's OBJECT is an integer for glyphs inserted by the
27729 display engine for its internal purposes, like truncation
27730 and continuation glyphs and blanks beyond the end of
27731 line's text on text terminals. If we are over such a
27732 glyph, we are not over any text. */
27733 || INTEGERP (glyph->object)
27734 /* R2L rows have a stretch glyph at their front, which
27735 stands for no text, whereas L2R rows have no glyphs at
27736 all beyond the end of text. Treat such stretch glyphs
27737 like we do with NULL glyphs in L2R rows. */
27738 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27739 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27740 && glyph->type == STRETCH_GLYPH
27741 && glyph->avoid_cursor_p))
27742 {
27743 if (clear_mouse_face (hlinfo))
27744 cursor = No_Cursor;
27745 #ifdef HAVE_WINDOW_SYSTEM
27746 if (FRAME_WINDOW_P (f) && NILP (pointer))
27747 {
27748 if (area != TEXT_AREA)
27749 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27750 else
27751 pointer = Vvoid_text_area_pointer;
27752 }
27753 #endif
27754 goto set_cursor;
27755 }
27756
27757 pos = glyph->charpos;
27758 object = glyph->object;
27759 if (!STRINGP (object) && !BUFFERP (object))
27760 goto set_cursor;
27761
27762 /* If we get an out-of-range value, return now; avoid an error. */
27763 if (BUFFERP (object) && pos > BUF_Z (b))
27764 goto set_cursor;
27765
27766 /* Make the window's buffer temporarily current for
27767 overlays_at and compute_char_face. */
27768 obuf = current_buffer;
27769 current_buffer = b;
27770 obegv = BEGV;
27771 ozv = ZV;
27772 BEGV = BEG;
27773 ZV = Z;
27774
27775 /* Is this char mouse-active or does it have help-echo? */
27776 position = make_number (pos);
27777
27778 if (BUFFERP (object))
27779 {
27780 /* Put all the overlays we want in a vector in overlay_vec. */
27781 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27782 /* Sort overlays into increasing priority order. */
27783 noverlays = sort_overlays (overlay_vec, noverlays, w);
27784 }
27785 else
27786 noverlays = 0;
27787
27788 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27789
27790 if (same_region)
27791 cursor = No_Cursor;
27792
27793 /* Check mouse-face highlighting. */
27794 if (! same_region
27795 /* If there exists an overlay with mouse-face overlapping
27796 the one we are currently highlighting, we have to
27797 check if we enter the overlapping overlay, and then
27798 highlight only that. */
27799 || (OVERLAYP (hlinfo->mouse_face_overlay)
27800 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27801 {
27802 /* Find the highest priority overlay with a mouse-face. */
27803 Lisp_Object overlay = Qnil;
27804 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27805 {
27806 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27807 if (!NILP (mouse_face))
27808 overlay = overlay_vec[i];
27809 }
27810
27811 /* If we're highlighting the same overlay as before, there's
27812 no need to do that again. */
27813 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27814 goto check_help_echo;
27815 hlinfo->mouse_face_overlay = overlay;
27816
27817 /* Clear the display of the old active region, if any. */
27818 if (clear_mouse_face (hlinfo))
27819 cursor = No_Cursor;
27820
27821 /* If no overlay applies, get a text property. */
27822 if (NILP (overlay))
27823 mouse_face = Fget_text_property (position, Qmouse_face, object);
27824
27825 /* Next, compute the bounds of the mouse highlighting and
27826 display it. */
27827 if (!NILP (mouse_face) && STRINGP (object))
27828 {
27829 /* The mouse-highlighting comes from a display string
27830 with a mouse-face. */
27831 Lisp_Object s, e;
27832 ptrdiff_t ignore;
27833
27834 s = Fprevious_single_property_change
27835 (make_number (pos + 1), Qmouse_face, object, Qnil);
27836 e = Fnext_single_property_change
27837 (position, Qmouse_face, object, Qnil);
27838 if (NILP (s))
27839 s = make_number (0);
27840 if (NILP (e))
27841 e = make_number (SCHARS (object) - 1);
27842 mouse_face_from_string_pos (w, hlinfo, object,
27843 XINT (s), XINT (e));
27844 hlinfo->mouse_face_past_end = 0;
27845 hlinfo->mouse_face_window = window;
27846 hlinfo->mouse_face_face_id
27847 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27848 glyph->face_id, 1);
27849 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27850 cursor = No_Cursor;
27851 }
27852 else
27853 {
27854 /* The mouse-highlighting, if any, comes from an overlay
27855 or text property in the buffer. */
27856 Lisp_Object buffer IF_LINT (= Qnil);
27857 Lisp_Object disp_string IF_LINT (= Qnil);
27858
27859 if (STRINGP (object))
27860 {
27861 /* If we are on a display string with no mouse-face,
27862 check if the text under it has one. */
27863 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27864 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27865 pos = string_buffer_position (object, start);
27866 if (pos > 0)
27867 {
27868 mouse_face = get_char_property_and_overlay
27869 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27870 buffer = w->buffer;
27871 disp_string = object;
27872 }
27873 }
27874 else
27875 {
27876 buffer = object;
27877 disp_string = Qnil;
27878 }
27879
27880 if (!NILP (mouse_face))
27881 {
27882 Lisp_Object before, after;
27883 Lisp_Object before_string, after_string;
27884 /* To correctly find the limits of mouse highlight
27885 in a bidi-reordered buffer, we must not use the
27886 optimization of limiting the search in
27887 previous-single-property-change and
27888 next-single-property-change, because
27889 rows_from_pos_range needs the real start and end
27890 positions to DTRT in this case. That's because
27891 the first row visible in a window does not
27892 necessarily display the character whose position
27893 is the smallest. */
27894 Lisp_Object lim1 =
27895 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27896 ? Fmarker_position (w->start)
27897 : Qnil;
27898 Lisp_Object lim2 =
27899 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27900 ? make_number (BUF_Z (XBUFFER (buffer))
27901 - XFASTINT (w->window_end_pos))
27902 : Qnil;
27903
27904 if (NILP (overlay))
27905 {
27906 /* Handle the text property case. */
27907 before = Fprevious_single_property_change
27908 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27909 after = Fnext_single_property_change
27910 (make_number (pos), Qmouse_face, buffer, lim2);
27911 before_string = after_string = Qnil;
27912 }
27913 else
27914 {
27915 /* Handle the overlay case. */
27916 before = Foverlay_start (overlay);
27917 after = Foverlay_end (overlay);
27918 before_string = Foverlay_get (overlay, Qbefore_string);
27919 after_string = Foverlay_get (overlay, Qafter_string);
27920
27921 if (!STRINGP (before_string)) before_string = Qnil;
27922 if (!STRINGP (after_string)) after_string = Qnil;
27923 }
27924
27925 mouse_face_from_buffer_pos (window, hlinfo, pos,
27926 NILP (before)
27927 ? 1
27928 : XFASTINT (before),
27929 NILP (after)
27930 ? BUF_Z (XBUFFER (buffer))
27931 : XFASTINT (after),
27932 before_string, after_string,
27933 disp_string);
27934 cursor = No_Cursor;
27935 }
27936 }
27937 }
27938
27939 check_help_echo:
27940
27941 /* Look for a `help-echo' property. */
27942 if (NILP (help_echo_string)) {
27943 Lisp_Object help, overlay;
27944
27945 /* Check overlays first. */
27946 help = overlay = Qnil;
27947 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27948 {
27949 overlay = overlay_vec[i];
27950 help = Foverlay_get (overlay, Qhelp_echo);
27951 }
27952
27953 if (!NILP (help))
27954 {
27955 help_echo_string = help;
27956 help_echo_window = window;
27957 help_echo_object = overlay;
27958 help_echo_pos = pos;
27959 }
27960 else
27961 {
27962 Lisp_Object obj = glyph->object;
27963 ptrdiff_t charpos = glyph->charpos;
27964
27965 /* Try text properties. */
27966 if (STRINGP (obj)
27967 && charpos >= 0
27968 && charpos < SCHARS (obj))
27969 {
27970 help = Fget_text_property (make_number (charpos),
27971 Qhelp_echo, obj);
27972 if (NILP (help))
27973 {
27974 /* If the string itself doesn't specify a help-echo,
27975 see if the buffer text ``under'' it does. */
27976 struct glyph_row *r
27977 = MATRIX_ROW (w->current_matrix, vpos);
27978 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27979 ptrdiff_t p = string_buffer_position (obj, start);
27980 if (p > 0)
27981 {
27982 help = Fget_char_property (make_number (p),
27983 Qhelp_echo, w->buffer);
27984 if (!NILP (help))
27985 {
27986 charpos = p;
27987 obj = w->buffer;
27988 }
27989 }
27990 }
27991 }
27992 else if (BUFFERP (obj)
27993 && charpos >= BEGV
27994 && charpos < ZV)
27995 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27996 obj);
27997
27998 if (!NILP (help))
27999 {
28000 help_echo_string = help;
28001 help_echo_window = window;
28002 help_echo_object = obj;
28003 help_echo_pos = charpos;
28004 }
28005 }
28006 }
28007
28008 #ifdef HAVE_WINDOW_SYSTEM
28009 /* Look for a `pointer' property. */
28010 if (FRAME_WINDOW_P (f) && NILP (pointer))
28011 {
28012 /* Check overlays first. */
28013 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28014 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28015
28016 if (NILP (pointer))
28017 {
28018 Lisp_Object obj = glyph->object;
28019 ptrdiff_t charpos = glyph->charpos;
28020
28021 /* Try text properties. */
28022 if (STRINGP (obj)
28023 && charpos >= 0
28024 && charpos < SCHARS (obj))
28025 {
28026 pointer = Fget_text_property (make_number (charpos),
28027 Qpointer, obj);
28028 if (NILP (pointer))
28029 {
28030 /* If the string itself doesn't specify a pointer,
28031 see if the buffer text ``under'' it does. */
28032 struct glyph_row *r
28033 = MATRIX_ROW (w->current_matrix, vpos);
28034 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28035 ptrdiff_t p = string_buffer_position (obj, start);
28036 if (p > 0)
28037 pointer = Fget_char_property (make_number (p),
28038 Qpointer, w->buffer);
28039 }
28040 }
28041 else if (BUFFERP (obj)
28042 && charpos >= BEGV
28043 && charpos < ZV)
28044 pointer = Fget_text_property (make_number (charpos),
28045 Qpointer, obj);
28046 }
28047 }
28048 #endif /* HAVE_WINDOW_SYSTEM */
28049
28050 BEGV = obegv;
28051 ZV = ozv;
28052 current_buffer = obuf;
28053 }
28054
28055 set_cursor:
28056
28057 #ifdef HAVE_WINDOW_SYSTEM
28058 if (FRAME_WINDOW_P (f))
28059 define_frame_cursor1 (f, cursor, pointer);
28060 #else
28061 /* This is here to prevent a compiler error, about "label at end of
28062 compound statement". */
28063 return;
28064 #endif
28065 }
28066
28067
28068 /* EXPORT for RIF:
28069 Clear any mouse-face on window W. This function is part of the
28070 redisplay interface, and is called from try_window_id and similar
28071 functions to ensure the mouse-highlight is off. */
28072
28073 void
28074 x_clear_window_mouse_face (struct window *w)
28075 {
28076 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28077 Lisp_Object window;
28078
28079 BLOCK_INPUT;
28080 XSETWINDOW (window, w);
28081 if (EQ (window, hlinfo->mouse_face_window))
28082 clear_mouse_face (hlinfo);
28083 UNBLOCK_INPUT;
28084 }
28085
28086
28087 /* EXPORT:
28088 Just discard the mouse face information for frame F, if any.
28089 This is used when the size of F is changed. */
28090
28091 void
28092 cancel_mouse_face (struct frame *f)
28093 {
28094 Lisp_Object window;
28095 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28096
28097 window = hlinfo->mouse_face_window;
28098 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28099 {
28100 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28101 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28102 hlinfo->mouse_face_window = Qnil;
28103 }
28104 }
28105
28106
28107 \f
28108 /***********************************************************************
28109 Exposure Events
28110 ***********************************************************************/
28111
28112 #ifdef HAVE_WINDOW_SYSTEM
28113
28114 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28115 which intersects rectangle R. R is in window-relative coordinates. */
28116
28117 static void
28118 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28119 enum glyph_row_area area)
28120 {
28121 struct glyph *first = row->glyphs[area];
28122 struct glyph *end = row->glyphs[area] + row->used[area];
28123 struct glyph *last;
28124 int first_x, start_x, x;
28125
28126 if (area == TEXT_AREA && row->fill_line_p)
28127 /* If row extends face to end of line write the whole line. */
28128 draw_glyphs (w, 0, row, area,
28129 0, row->used[area],
28130 DRAW_NORMAL_TEXT, 0);
28131 else
28132 {
28133 /* Set START_X to the window-relative start position for drawing glyphs of
28134 AREA. The first glyph of the text area can be partially visible.
28135 The first glyphs of other areas cannot. */
28136 start_x = window_box_left_offset (w, area);
28137 x = start_x;
28138 if (area == TEXT_AREA)
28139 x += row->x;
28140
28141 /* Find the first glyph that must be redrawn. */
28142 while (first < end
28143 && x + first->pixel_width < r->x)
28144 {
28145 x += first->pixel_width;
28146 ++first;
28147 }
28148
28149 /* Find the last one. */
28150 last = first;
28151 first_x = x;
28152 while (last < end
28153 && x < r->x + r->width)
28154 {
28155 x += last->pixel_width;
28156 ++last;
28157 }
28158
28159 /* Repaint. */
28160 if (last > first)
28161 draw_glyphs (w, first_x - start_x, row, area,
28162 first - row->glyphs[area], last - row->glyphs[area],
28163 DRAW_NORMAL_TEXT, 0);
28164 }
28165 }
28166
28167
28168 /* Redraw the parts of the glyph row ROW on window W intersecting
28169 rectangle R. R is in window-relative coordinates. Value is
28170 non-zero if mouse-face was overwritten. */
28171
28172 static int
28173 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28174 {
28175 eassert (row->enabled_p);
28176
28177 if (row->mode_line_p || w->pseudo_window_p)
28178 draw_glyphs (w, 0, row, TEXT_AREA,
28179 0, row->used[TEXT_AREA],
28180 DRAW_NORMAL_TEXT, 0);
28181 else
28182 {
28183 if (row->used[LEFT_MARGIN_AREA])
28184 expose_area (w, row, r, LEFT_MARGIN_AREA);
28185 if (row->used[TEXT_AREA])
28186 expose_area (w, row, r, TEXT_AREA);
28187 if (row->used[RIGHT_MARGIN_AREA])
28188 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28189 draw_row_fringe_bitmaps (w, row);
28190 }
28191
28192 return row->mouse_face_p;
28193 }
28194
28195
28196 /* Redraw those parts of glyphs rows during expose event handling that
28197 overlap other rows. Redrawing of an exposed line writes over parts
28198 of lines overlapping that exposed line; this function fixes that.
28199
28200 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28201 row in W's current matrix that is exposed and overlaps other rows.
28202 LAST_OVERLAPPING_ROW is the last such row. */
28203
28204 static void
28205 expose_overlaps (struct window *w,
28206 struct glyph_row *first_overlapping_row,
28207 struct glyph_row *last_overlapping_row,
28208 XRectangle *r)
28209 {
28210 struct glyph_row *row;
28211
28212 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28213 if (row->overlapping_p)
28214 {
28215 eassert (row->enabled_p && !row->mode_line_p);
28216
28217 row->clip = r;
28218 if (row->used[LEFT_MARGIN_AREA])
28219 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28220
28221 if (row->used[TEXT_AREA])
28222 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28223
28224 if (row->used[RIGHT_MARGIN_AREA])
28225 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28226 row->clip = NULL;
28227 }
28228 }
28229
28230
28231 /* Return non-zero if W's cursor intersects rectangle R. */
28232
28233 static int
28234 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28235 {
28236 XRectangle cr, result;
28237 struct glyph *cursor_glyph;
28238 struct glyph_row *row;
28239
28240 if (w->phys_cursor.vpos >= 0
28241 && w->phys_cursor.vpos < w->current_matrix->nrows
28242 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28243 row->enabled_p)
28244 && row->cursor_in_fringe_p)
28245 {
28246 /* Cursor is in the fringe. */
28247 cr.x = window_box_right_offset (w,
28248 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28249 ? RIGHT_MARGIN_AREA
28250 : TEXT_AREA));
28251 cr.y = row->y;
28252 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28253 cr.height = row->height;
28254 return x_intersect_rectangles (&cr, r, &result);
28255 }
28256
28257 cursor_glyph = get_phys_cursor_glyph (w);
28258 if (cursor_glyph)
28259 {
28260 /* r is relative to W's box, but w->phys_cursor.x is relative
28261 to left edge of W's TEXT area. Adjust it. */
28262 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28263 cr.y = w->phys_cursor.y;
28264 cr.width = cursor_glyph->pixel_width;
28265 cr.height = w->phys_cursor_height;
28266 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28267 I assume the effect is the same -- and this is portable. */
28268 return x_intersect_rectangles (&cr, r, &result);
28269 }
28270 /* If we don't understand the format, pretend we're not in the hot-spot. */
28271 return 0;
28272 }
28273
28274
28275 /* EXPORT:
28276 Draw a vertical window border to the right of window W if W doesn't
28277 have vertical scroll bars. */
28278
28279 void
28280 x_draw_vertical_border (struct window *w)
28281 {
28282 struct frame *f = XFRAME (WINDOW_FRAME (w));
28283
28284 /* We could do better, if we knew what type of scroll-bar the adjacent
28285 windows (on either side) have... But we don't :-(
28286 However, I think this works ok. ++KFS 2003-04-25 */
28287
28288 /* Redraw borders between horizontally adjacent windows. Don't
28289 do it for frames with vertical scroll bars because either the
28290 right scroll bar of a window, or the left scroll bar of its
28291 neighbor will suffice as a border. */
28292 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28293 return;
28294
28295 if (!WINDOW_RIGHTMOST_P (w)
28296 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28297 {
28298 int x0, x1, y0, y1;
28299
28300 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28301 y1 -= 1;
28302
28303 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28304 x1 -= 1;
28305
28306 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28307 }
28308 else if (!WINDOW_LEFTMOST_P (w)
28309 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28310 {
28311 int x0, x1, y0, y1;
28312
28313 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28314 y1 -= 1;
28315
28316 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28317 x0 -= 1;
28318
28319 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28320 }
28321 }
28322
28323
28324 /* Redraw the part of window W intersection rectangle FR. Pixel
28325 coordinates in FR are frame-relative. Call this function with
28326 input blocked. Value is non-zero if the exposure overwrites
28327 mouse-face. */
28328
28329 static int
28330 expose_window (struct window *w, XRectangle *fr)
28331 {
28332 struct frame *f = XFRAME (w->frame);
28333 XRectangle wr, r;
28334 int mouse_face_overwritten_p = 0;
28335
28336 /* If window is not yet fully initialized, do nothing. This can
28337 happen when toolkit scroll bars are used and a window is split.
28338 Reconfiguring the scroll bar will generate an expose for a newly
28339 created window. */
28340 if (w->current_matrix == NULL)
28341 return 0;
28342
28343 /* When we're currently updating the window, display and current
28344 matrix usually don't agree. Arrange for a thorough display
28345 later. */
28346 if (w == updated_window)
28347 {
28348 SET_FRAME_GARBAGED (f);
28349 return 0;
28350 }
28351
28352 /* Frame-relative pixel rectangle of W. */
28353 wr.x = WINDOW_LEFT_EDGE_X (w);
28354 wr.y = WINDOW_TOP_EDGE_Y (w);
28355 wr.width = WINDOW_TOTAL_WIDTH (w);
28356 wr.height = WINDOW_TOTAL_HEIGHT (w);
28357
28358 if (x_intersect_rectangles (fr, &wr, &r))
28359 {
28360 int yb = window_text_bottom_y (w);
28361 struct glyph_row *row;
28362 int cursor_cleared_p, phys_cursor_on_p;
28363 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28364
28365 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28366 r.x, r.y, r.width, r.height));
28367
28368 /* Convert to window coordinates. */
28369 r.x -= WINDOW_LEFT_EDGE_X (w);
28370 r.y -= WINDOW_TOP_EDGE_Y (w);
28371
28372 /* Turn off the cursor. */
28373 if (!w->pseudo_window_p
28374 && phys_cursor_in_rect_p (w, &r))
28375 {
28376 x_clear_cursor (w);
28377 cursor_cleared_p = 1;
28378 }
28379 else
28380 cursor_cleared_p = 0;
28381
28382 /* If the row containing the cursor extends face to end of line,
28383 then expose_area might overwrite the cursor outside the
28384 rectangle and thus notice_overwritten_cursor might clear
28385 w->phys_cursor_on_p. We remember the original value and
28386 check later if it is changed. */
28387 phys_cursor_on_p = w->phys_cursor_on_p;
28388
28389 /* Update lines intersecting rectangle R. */
28390 first_overlapping_row = last_overlapping_row = NULL;
28391 for (row = w->current_matrix->rows;
28392 row->enabled_p;
28393 ++row)
28394 {
28395 int y0 = row->y;
28396 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28397
28398 if ((y0 >= r.y && y0 < r.y + r.height)
28399 || (y1 > r.y && y1 < r.y + r.height)
28400 || (r.y >= y0 && r.y < y1)
28401 || (r.y + r.height > y0 && r.y + r.height < y1))
28402 {
28403 /* A header line may be overlapping, but there is no need
28404 to fix overlapping areas for them. KFS 2005-02-12 */
28405 if (row->overlapping_p && !row->mode_line_p)
28406 {
28407 if (first_overlapping_row == NULL)
28408 first_overlapping_row = row;
28409 last_overlapping_row = row;
28410 }
28411
28412 row->clip = fr;
28413 if (expose_line (w, row, &r))
28414 mouse_face_overwritten_p = 1;
28415 row->clip = NULL;
28416 }
28417 else if (row->overlapping_p)
28418 {
28419 /* We must redraw a row overlapping the exposed area. */
28420 if (y0 < r.y
28421 ? y0 + row->phys_height > r.y
28422 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28423 {
28424 if (first_overlapping_row == NULL)
28425 first_overlapping_row = row;
28426 last_overlapping_row = row;
28427 }
28428 }
28429
28430 if (y1 >= yb)
28431 break;
28432 }
28433
28434 /* Display the mode line if there is one. */
28435 if (WINDOW_WANTS_MODELINE_P (w)
28436 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28437 row->enabled_p)
28438 && row->y < r.y + r.height)
28439 {
28440 if (expose_line (w, row, &r))
28441 mouse_face_overwritten_p = 1;
28442 }
28443
28444 if (!w->pseudo_window_p)
28445 {
28446 /* Fix the display of overlapping rows. */
28447 if (first_overlapping_row)
28448 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28449 fr);
28450
28451 /* Draw border between windows. */
28452 x_draw_vertical_border (w);
28453
28454 /* Turn the cursor on again. */
28455 if (cursor_cleared_p
28456 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28457 update_window_cursor (w, 1);
28458 }
28459 }
28460
28461 return mouse_face_overwritten_p;
28462 }
28463
28464
28465
28466 /* Redraw (parts) of all windows in the window tree rooted at W that
28467 intersect R. R contains frame pixel coordinates. Value is
28468 non-zero if the exposure overwrites mouse-face. */
28469
28470 static int
28471 expose_window_tree (struct window *w, XRectangle *r)
28472 {
28473 struct frame *f = XFRAME (w->frame);
28474 int mouse_face_overwritten_p = 0;
28475
28476 while (w && !FRAME_GARBAGED_P (f))
28477 {
28478 if (!NILP (w->hchild))
28479 mouse_face_overwritten_p
28480 |= expose_window_tree (XWINDOW (w->hchild), r);
28481 else if (!NILP (w->vchild))
28482 mouse_face_overwritten_p
28483 |= expose_window_tree (XWINDOW (w->vchild), r);
28484 else
28485 mouse_face_overwritten_p |= expose_window (w, r);
28486
28487 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28488 }
28489
28490 return mouse_face_overwritten_p;
28491 }
28492
28493
28494 /* EXPORT:
28495 Redisplay an exposed area of frame F. X and Y are the upper-left
28496 corner of the exposed rectangle. W and H are width and height of
28497 the exposed area. All are pixel values. W or H zero means redraw
28498 the entire frame. */
28499
28500 void
28501 expose_frame (struct frame *f, int x, int y, int w, int h)
28502 {
28503 XRectangle r;
28504 int mouse_face_overwritten_p = 0;
28505
28506 TRACE ((stderr, "expose_frame "));
28507
28508 /* No need to redraw if frame will be redrawn soon. */
28509 if (FRAME_GARBAGED_P (f))
28510 {
28511 TRACE ((stderr, " garbaged\n"));
28512 return;
28513 }
28514
28515 /* If basic faces haven't been realized yet, there is no point in
28516 trying to redraw anything. This can happen when we get an expose
28517 event while Emacs is starting, e.g. by moving another window. */
28518 if (FRAME_FACE_CACHE (f) == NULL
28519 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28520 {
28521 TRACE ((stderr, " no faces\n"));
28522 return;
28523 }
28524
28525 if (w == 0 || h == 0)
28526 {
28527 r.x = r.y = 0;
28528 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28529 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28530 }
28531 else
28532 {
28533 r.x = x;
28534 r.y = y;
28535 r.width = w;
28536 r.height = h;
28537 }
28538
28539 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28540 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28541
28542 if (WINDOWP (f->tool_bar_window))
28543 mouse_face_overwritten_p
28544 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28545
28546 #ifdef HAVE_X_WINDOWS
28547 #ifndef MSDOS
28548 #ifndef USE_X_TOOLKIT
28549 if (WINDOWP (f->menu_bar_window))
28550 mouse_face_overwritten_p
28551 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28552 #endif /* not USE_X_TOOLKIT */
28553 #endif
28554 #endif
28555
28556 /* Some window managers support a focus-follows-mouse style with
28557 delayed raising of frames. Imagine a partially obscured frame,
28558 and moving the mouse into partially obscured mouse-face on that
28559 frame. The visible part of the mouse-face will be highlighted,
28560 then the WM raises the obscured frame. With at least one WM, KDE
28561 2.1, Emacs is not getting any event for the raising of the frame
28562 (even tried with SubstructureRedirectMask), only Expose events.
28563 These expose events will draw text normally, i.e. not
28564 highlighted. Which means we must redo the highlight here.
28565 Subsume it under ``we love X''. --gerd 2001-08-15 */
28566 /* Included in Windows version because Windows most likely does not
28567 do the right thing if any third party tool offers
28568 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28569 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28570 {
28571 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28572 if (f == hlinfo->mouse_face_mouse_frame)
28573 {
28574 int mouse_x = hlinfo->mouse_face_mouse_x;
28575 int mouse_y = hlinfo->mouse_face_mouse_y;
28576 clear_mouse_face (hlinfo);
28577 note_mouse_highlight (f, mouse_x, mouse_y);
28578 }
28579 }
28580 }
28581
28582
28583 /* EXPORT:
28584 Determine the intersection of two rectangles R1 and R2. Return
28585 the intersection in *RESULT. Value is non-zero if RESULT is not
28586 empty. */
28587
28588 int
28589 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28590 {
28591 XRectangle *left, *right;
28592 XRectangle *upper, *lower;
28593 int intersection_p = 0;
28594
28595 /* Rearrange so that R1 is the left-most rectangle. */
28596 if (r1->x < r2->x)
28597 left = r1, right = r2;
28598 else
28599 left = r2, right = r1;
28600
28601 /* X0 of the intersection is right.x0, if this is inside R1,
28602 otherwise there is no intersection. */
28603 if (right->x <= left->x + left->width)
28604 {
28605 result->x = right->x;
28606
28607 /* The right end of the intersection is the minimum of
28608 the right ends of left and right. */
28609 result->width = (min (left->x + left->width, right->x + right->width)
28610 - result->x);
28611
28612 /* Same game for Y. */
28613 if (r1->y < r2->y)
28614 upper = r1, lower = r2;
28615 else
28616 upper = r2, lower = r1;
28617
28618 /* The upper end of the intersection is lower.y0, if this is inside
28619 of upper. Otherwise, there is no intersection. */
28620 if (lower->y <= upper->y + upper->height)
28621 {
28622 result->y = lower->y;
28623
28624 /* The lower end of the intersection is the minimum of the lower
28625 ends of upper and lower. */
28626 result->height = (min (lower->y + lower->height,
28627 upper->y + upper->height)
28628 - result->y);
28629 intersection_p = 1;
28630 }
28631 }
28632
28633 return intersection_p;
28634 }
28635
28636 #endif /* HAVE_WINDOW_SYSTEM */
28637
28638 \f
28639 /***********************************************************************
28640 Initialization
28641 ***********************************************************************/
28642
28643 void
28644 syms_of_xdisp (void)
28645 {
28646 Vwith_echo_area_save_vector = Qnil;
28647 staticpro (&Vwith_echo_area_save_vector);
28648
28649 Vmessage_stack = Qnil;
28650 staticpro (&Vmessage_stack);
28651
28652 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28653
28654 message_dolog_marker1 = Fmake_marker ();
28655 staticpro (&message_dolog_marker1);
28656 message_dolog_marker2 = Fmake_marker ();
28657 staticpro (&message_dolog_marker2);
28658 message_dolog_marker3 = Fmake_marker ();
28659 staticpro (&message_dolog_marker3);
28660
28661 #ifdef GLYPH_DEBUG
28662 defsubr (&Sdump_frame_glyph_matrix);
28663 defsubr (&Sdump_glyph_matrix);
28664 defsubr (&Sdump_glyph_row);
28665 defsubr (&Sdump_tool_bar_row);
28666 defsubr (&Strace_redisplay);
28667 defsubr (&Strace_to_stderr);
28668 #endif
28669 #ifdef HAVE_WINDOW_SYSTEM
28670 defsubr (&Stool_bar_lines_needed);
28671 defsubr (&Slookup_image_map);
28672 #endif
28673 defsubr (&Sformat_mode_line);
28674 defsubr (&Sinvisible_p);
28675 defsubr (&Scurrent_bidi_paragraph_direction);
28676
28677 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28678 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28679 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28680 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28681 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28682 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28683 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28684 DEFSYM (Qeval, "eval");
28685 DEFSYM (QCdata, ":data");
28686 DEFSYM (Qdisplay, "display");
28687 DEFSYM (Qspace_width, "space-width");
28688 DEFSYM (Qraise, "raise");
28689 DEFSYM (Qslice, "slice");
28690 DEFSYM (Qspace, "space");
28691 DEFSYM (Qmargin, "margin");
28692 DEFSYM (Qpointer, "pointer");
28693 DEFSYM (Qleft_margin, "left-margin");
28694 DEFSYM (Qright_margin, "right-margin");
28695 DEFSYM (Qcenter, "center");
28696 DEFSYM (Qline_height, "line-height");
28697 DEFSYM (QCalign_to, ":align-to");
28698 DEFSYM (QCrelative_width, ":relative-width");
28699 DEFSYM (QCrelative_height, ":relative-height");
28700 DEFSYM (QCeval, ":eval");
28701 DEFSYM (QCpropertize, ":propertize");
28702 DEFSYM (QCfile, ":file");
28703 DEFSYM (Qfontified, "fontified");
28704 DEFSYM (Qfontification_functions, "fontification-functions");
28705 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28706 DEFSYM (Qescape_glyph, "escape-glyph");
28707 DEFSYM (Qnobreak_space, "nobreak-space");
28708 DEFSYM (Qimage, "image");
28709 DEFSYM (Qtext, "text");
28710 DEFSYM (Qboth, "both");
28711 DEFSYM (Qboth_horiz, "both-horiz");
28712 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28713 DEFSYM (QCmap, ":map");
28714 DEFSYM (QCpointer, ":pointer");
28715 DEFSYM (Qrect, "rect");
28716 DEFSYM (Qcircle, "circle");
28717 DEFSYM (Qpoly, "poly");
28718 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28719 DEFSYM (Qgrow_only, "grow-only");
28720 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28721 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28722 DEFSYM (Qposition, "position");
28723 DEFSYM (Qbuffer_position, "buffer-position");
28724 DEFSYM (Qobject, "object");
28725 DEFSYM (Qbar, "bar");
28726 DEFSYM (Qhbar, "hbar");
28727 DEFSYM (Qbox, "box");
28728 DEFSYM (Qhollow, "hollow");
28729 DEFSYM (Qhand, "hand");
28730 DEFSYM (Qarrow, "arrow");
28731 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28732
28733 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28734 Fcons (intern_c_string ("void-variable"), Qnil)),
28735 Qnil);
28736 staticpro (&list_of_error);
28737
28738 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28739 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28740 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28741 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28742
28743 echo_buffer[0] = echo_buffer[1] = Qnil;
28744 staticpro (&echo_buffer[0]);
28745 staticpro (&echo_buffer[1]);
28746
28747 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28748 staticpro (&echo_area_buffer[0]);
28749 staticpro (&echo_area_buffer[1]);
28750
28751 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28752 staticpro (&Vmessages_buffer_name);
28753
28754 mode_line_proptrans_alist = Qnil;
28755 staticpro (&mode_line_proptrans_alist);
28756 mode_line_string_list = Qnil;
28757 staticpro (&mode_line_string_list);
28758 mode_line_string_face = Qnil;
28759 staticpro (&mode_line_string_face);
28760 mode_line_string_face_prop = Qnil;
28761 staticpro (&mode_line_string_face_prop);
28762 Vmode_line_unwind_vector = Qnil;
28763 staticpro (&Vmode_line_unwind_vector);
28764
28765 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28766
28767 help_echo_string = Qnil;
28768 staticpro (&help_echo_string);
28769 help_echo_object = Qnil;
28770 staticpro (&help_echo_object);
28771 help_echo_window = Qnil;
28772 staticpro (&help_echo_window);
28773 previous_help_echo_string = Qnil;
28774 staticpro (&previous_help_echo_string);
28775 help_echo_pos = -1;
28776
28777 DEFSYM (Qright_to_left, "right-to-left");
28778 DEFSYM (Qleft_to_right, "left-to-right");
28779
28780 #ifdef HAVE_WINDOW_SYSTEM
28781 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28782 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28783 For example, if a block cursor is over a tab, it will be drawn as
28784 wide as that tab on the display. */);
28785 x_stretch_cursor_p = 0;
28786 #endif
28787
28788 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28789 doc: /* Non-nil means highlight trailing whitespace.
28790 The face used for trailing whitespace is `trailing-whitespace'. */);
28791 Vshow_trailing_whitespace = Qnil;
28792
28793 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28794 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28795 If the value is t, Emacs highlights non-ASCII chars which have the
28796 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28797 or `escape-glyph' face respectively.
28798
28799 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28800 U+2011 (non-breaking hyphen) are affected.
28801
28802 Any other non-nil value means to display these characters as a escape
28803 glyph followed by an ordinary space or hyphen.
28804
28805 A value of nil means no special handling of these characters. */);
28806 Vnobreak_char_display = Qt;
28807
28808 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28809 doc: /* The pointer shape to show in void text areas.
28810 A value of nil means to show the text pointer. Other options are `arrow',
28811 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28812 Vvoid_text_area_pointer = Qarrow;
28813
28814 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28815 doc: /* Non-nil means don't actually do any redisplay.
28816 This is used for internal purposes. */);
28817 Vinhibit_redisplay = Qnil;
28818
28819 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28820 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28821 Vglobal_mode_string = Qnil;
28822
28823 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28824 doc: /* Marker for where to display an arrow on top of the buffer text.
28825 This must be the beginning of a line in order to work.
28826 See also `overlay-arrow-string'. */);
28827 Voverlay_arrow_position = Qnil;
28828
28829 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28830 doc: /* String to display as an arrow in non-window frames.
28831 See also `overlay-arrow-position'. */);
28832 Voverlay_arrow_string = build_pure_c_string ("=>");
28833
28834 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28835 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28836 The symbols on this list are examined during redisplay to determine
28837 where to display overlay arrows. */);
28838 Voverlay_arrow_variable_list
28839 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28840
28841 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28842 doc: /* The number of lines to try scrolling a window by when point moves out.
28843 If that fails to bring point back on frame, point is centered instead.
28844 If this is zero, point is always centered after it moves off frame.
28845 If you want scrolling to always be a line at a time, you should set
28846 `scroll-conservatively' to a large value rather than set this to 1. */);
28847
28848 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28849 doc: /* Scroll up to this many lines, to bring point back on screen.
28850 If point moves off-screen, redisplay will scroll by up to
28851 `scroll-conservatively' lines in order to bring point just barely
28852 onto the screen again. If that cannot be done, then redisplay
28853 recenters point as usual.
28854
28855 If the value is greater than 100, redisplay will never recenter point,
28856 but will always scroll just enough text to bring point into view, even
28857 if you move far away.
28858
28859 A value of zero means always recenter point if it moves off screen. */);
28860 scroll_conservatively = 0;
28861
28862 DEFVAR_INT ("scroll-margin", scroll_margin,
28863 doc: /* Number of lines of margin at the top and bottom of a window.
28864 Recenter the window whenever point gets within this many lines
28865 of the top or bottom of the window. */);
28866 scroll_margin = 0;
28867
28868 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28869 doc: /* Pixels per inch value for non-window system displays.
28870 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28871 Vdisplay_pixels_per_inch = make_float (72.0);
28872
28873 #ifdef GLYPH_DEBUG
28874 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28875 #endif
28876
28877 DEFVAR_LISP ("truncate-partial-width-windows",
28878 Vtruncate_partial_width_windows,
28879 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28880 For an integer value, truncate lines in each window narrower than the
28881 full frame width, provided the window width is less than that integer;
28882 otherwise, respect the value of `truncate-lines'.
28883
28884 For any other non-nil value, truncate lines in all windows that do
28885 not span the full frame width.
28886
28887 A value of nil means to respect the value of `truncate-lines'.
28888
28889 If `word-wrap' is enabled, you might want to reduce this. */);
28890 Vtruncate_partial_width_windows = make_number (50);
28891
28892 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28893 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28894 Any other value means to use the appropriate face, `mode-line',
28895 `header-line', or `menu' respectively. */);
28896 mode_line_inverse_video = 1;
28897
28898 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28899 doc: /* Maximum buffer size for which line number should be displayed.
28900 If the buffer is bigger than this, the line number does not appear
28901 in the mode line. A value of nil means no limit. */);
28902 Vline_number_display_limit = Qnil;
28903
28904 DEFVAR_INT ("line-number-display-limit-width",
28905 line_number_display_limit_width,
28906 doc: /* Maximum line width (in characters) for line number display.
28907 If the average length of the lines near point is bigger than this, then the
28908 line number may be omitted from the mode line. */);
28909 line_number_display_limit_width = 200;
28910
28911 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28912 doc: /* Non-nil means highlight region even in nonselected windows. */);
28913 highlight_nonselected_windows = 0;
28914
28915 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28916 doc: /* Non-nil if more than one frame is visible on this display.
28917 Minibuffer-only frames don't count, but iconified frames do.
28918 This variable is not guaranteed to be accurate except while processing
28919 `frame-title-format' and `icon-title-format'. */);
28920
28921 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28922 doc: /* Template for displaying the title bar of visible frames.
28923 \(Assuming the window manager supports this feature.)
28924
28925 This variable has the same structure as `mode-line-format', except that
28926 the %c and %l constructs are ignored. It is used only on frames for
28927 which no explicit name has been set \(see `modify-frame-parameters'). */);
28928
28929 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28930 doc: /* Template for displaying the title bar of an iconified frame.
28931 \(Assuming the window manager supports this feature.)
28932 This variable has the same structure as `mode-line-format' (which see),
28933 and is used only on frames for which no explicit name has been set
28934 \(see `modify-frame-parameters'). */);
28935 Vicon_title_format
28936 = Vframe_title_format
28937 = listn (CONSTYPE_PURE, 3,
28938 intern_c_string ("multiple-frames"),
28939 build_pure_c_string ("%b"),
28940 listn (CONSTYPE_PURE, 4,
28941 empty_unibyte_string,
28942 intern_c_string ("invocation-name"),
28943 build_pure_c_string ("@"),
28944 intern_c_string ("system-name")));
28945
28946 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28947 doc: /* Maximum number of lines to keep in the message log buffer.
28948 If nil, disable message logging. If t, log messages but don't truncate
28949 the buffer when it becomes large. */);
28950 Vmessage_log_max = make_number (100);
28951
28952 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28953 doc: /* Functions called before redisplay, if window sizes have changed.
28954 The value should be a list of functions that take one argument.
28955 Just before redisplay, for each frame, if any of its windows have changed
28956 size since the last redisplay, or have been split or deleted,
28957 all the functions in the list are called, with the frame as argument. */);
28958 Vwindow_size_change_functions = Qnil;
28959
28960 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28961 doc: /* List of functions to call before redisplaying a window with scrolling.
28962 Each function is called with two arguments, the window and its new
28963 display-start position. Note that these functions are also called by
28964 `set-window-buffer'. Also note that the value of `window-end' is not
28965 valid when these functions are called.
28966
28967 Warning: Do not use this feature to alter the way the window
28968 is scrolled. It is not designed for that, and such use probably won't
28969 work. */);
28970 Vwindow_scroll_functions = Qnil;
28971
28972 DEFVAR_LISP ("window-text-change-functions",
28973 Vwindow_text_change_functions,
28974 doc: /* Functions to call in redisplay when text in the window might change. */);
28975 Vwindow_text_change_functions = Qnil;
28976
28977 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28978 doc: /* Functions called when redisplay of a window reaches the end trigger.
28979 Each function is called with two arguments, the window and the end trigger value.
28980 See `set-window-redisplay-end-trigger'. */);
28981 Vredisplay_end_trigger_functions = Qnil;
28982
28983 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28984 doc: /* Non-nil means autoselect window with mouse pointer.
28985 If nil, do not autoselect windows.
28986 A positive number means delay autoselection by that many seconds: a
28987 window is autoselected only after the mouse has remained in that
28988 window for the duration of the delay.
28989 A negative number has a similar effect, but causes windows to be
28990 autoselected only after the mouse has stopped moving. \(Because of
28991 the way Emacs compares mouse events, you will occasionally wait twice
28992 that time before the window gets selected.\)
28993 Any other value means to autoselect window instantaneously when the
28994 mouse pointer enters it.
28995
28996 Autoselection selects the minibuffer only if it is active, and never
28997 unselects the minibuffer if it is active.
28998
28999 When customizing this variable make sure that the actual value of
29000 `focus-follows-mouse' matches the behavior of your window manager. */);
29001 Vmouse_autoselect_window = Qnil;
29002
29003 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29004 doc: /* Non-nil means automatically resize tool-bars.
29005 This dynamically changes the tool-bar's height to the minimum height
29006 that is needed to make all tool-bar items visible.
29007 If value is `grow-only', the tool-bar's height is only increased
29008 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29009 Vauto_resize_tool_bars = Qt;
29010
29011 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29012 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29013 auto_raise_tool_bar_buttons_p = 1;
29014
29015 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29016 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29017 make_cursor_line_fully_visible_p = 1;
29018
29019 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29020 doc: /* Border below tool-bar in pixels.
29021 If an integer, use it as the height of the border.
29022 If it is one of `internal-border-width' or `border-width', use the
29023 value of the corresponding frame parameter.
29024 Otherwise, no border is added below the tool-bar. */);
29025 Vtool_bar_border = Qinternal_border_width;
29026
29027 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29028 doc: /* Margin around tool-bar buttons in pixels.
29029 If an integer, use that for both horizontal and vertical margins.
29030 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29031 HORZ specifying the horizontal margin, and VERT specifying the
29032 vertical margin. */);
29033 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29034
29035 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29036 doc: /* Relief thickness of tool-bar buttons. */);
29037 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29038
29039 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29040 doc: /* Tool bar style to use.
29041 It can be one of
29042 image - show images only
29043 text - show text only
29044 both - show both, text below image
29045 both-horiz - show text to the right of the image
29046 text-image-horiz - show text to the left of the image
29047 any other - use system default or image if no system default.
29048
29049 This variable only affects the GTK+ toolkit version of Emacs. */);
29050 Vtool_bar_style = Qnil;
29051
29052 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29053 doc: /* Maximum number of characters a label can have to be shown.
29054 The tool bar style must also show labels for this to have any effect, see
29055 `tool-bar-style'. */);
29056 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29057
29058 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29059 doc: /* List of functions to call to fontify regions of text.
29060 Each function is called with one argument POS. Functions must
29061 fontify a region starting at POS in the current buffer, and give
29062 fontified regions the property `fontified'. */);
29063 Vfontification_functions = Qnil;
29064 Fmake_variable_buffer_local (Qfontification_functions);
29065
29066 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29067 unibyte_display_via_language_environment,
29068 doc: /* Non-nil means display unibyte text according to language environment.
29069 Specifically, this means that raw bytes in the range 160-255 decimal
29070 are displayed by converting them to the equivalent multibyte characters
29071 according to the current language environment. As a result, they are
29072 displayed according to the current fontset.
29073
29074 Note that this variable affects only how these bytes are displayed,
29075 but does not change the fact they are interpreted as raw bytes. */);
29076 unibyte_display_via_language_environment = 0;
29077
29078 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29079 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29080 If a float, it specifies a fraction of the mini-window frame's height.
29081 If an integer, it specifies a number of lines. */);
29082 Vmax_mini_window_height = make_float (0.25);
29083
29084 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29085 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29086 A value of nil means don't automatically resize mini-windows.
29087 A value of t means resize them to fit the text displayed in them.
29088 A value of `grow-only', the default, means let mini-windows grow only;
29089 they return to their normal size when the minibuffer is closed, or the
29090 echo area becomes empty. */);
29091 Vresize_mini_windows = Qgrow_only;
29092
29093 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29094 doc: /* Alist specifying how to blink the cursor off.
29095 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29096 `cursor-type' frame-parameter or variable equals ON-STATE,
29097 comparing using `equal', Emacs uses OFF-STATE to specify
29098 how to blink it off. ON-STATE and OFF-STATE are values for
29099 the `cursor-type' frame parameter.
29100
29101 If a frame's ON-STATE has no entry in this list,
29102 the frame's other specifications determine how to blink the cursor off. */);
29103 Vblink_cursor_alist = Qnil;
29104
29105 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29106 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29107 If non-nil, windows are automatically scrolled horizontally to make
29108 point visible. */);
29109 automatic_hscrolling_p = 1;
29110 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29111
29112 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29113 doc: /* How many columns away from the window edge point is allowed to get
29114 before automatic hscrolling will horizontally scroll the window. */);
29115 hscroll_margin = 5;
29116
29117 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29118 doc: /* How many columns to scroll the window when point gets too close to the edge.
29119 When point is less than `hscroll-margin' columns from the window
29120 edge, automatic hscrolling will scroll the window by the amount of columns
29121 determined by this variable. If its value is a positive integer, scroll that
29122 many columns. If it's a positive floating-point number, it specifies the
29123 fraction of the window's width to scroll. If it's nil or zero, point will be
29124 centered horizontally after the scroll. Any other value, including negative
29125 numbers, are treated as if the value were zero.
29126
29127 Automatic hscrolling always moves point outside the scroll margin, so if
29128 point was more than scroll step columns inside the margin, the window will
29129 scroll more than the value given by the scroll step.
29130
29131 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29132 and `scroll-right' overrides this variable's effect. */);
29133 Vhscroll_step = make_number (0);
29134
29135 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29136 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29137 Bind this around calls to `message' to let it take effect. */);
29138 message_truncate_lines = 0;
29139
29140 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29141 doc: /* Normal hook run to update the menu bar definitions.
29142 Redisplay runs this hook before it redisplays the menu bar.
29143 This is used to update submenus such as Buffers,
29144 whose contents depend on various data. */);
29145 Vmenu_bar_update_hook = Qnil;
29146
29147 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29148 doc: /* Frame for which we are updating a menu.
29149 The enable predicate for a menu binding should check this variable. */);
29150 Vmenu_updating_frame = Qnil;
29151
29152 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29153 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29154 inhibit_menubar_update = 0;
29155
29156 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29157 doc: /* Prefix prepended to all continuation lines at display time.
29158 The value may be a string, an image, or a stretch-glyph; it is
29159 interpreted in the same way as the value of a `display' text property.
29160
29161 This variable is overridden by any `wrap-prefix' text or overlay
29162 property.
29163
29164 To add a prefix to non-continuation lines, use `line-prefix'. */);
29165 Vwrap_prefix = Qnil;
29166 DEFSYM (Qwrap_prefix, "wrap-prefix");
29167 Fmake_variable_buffer_local (Qwrap_prefix);
29168
29169 DEFVAR_LISP ("line-prefix", Vline_prefix,
29170 doc: /* Prefix prepended to all non-continuation lines at display time.
29171 The value may be a string, an image, or a stretch-glyph; it is
29172 interpreted in the same way as the value of a `display' text property.
29173
29174 This variable is overridden by any `line-prefix' text or overlay
29175 property.
29176
29177 To add a prefix to continuation lines, use `wrap-prefix'. */);
29178 Vline_prefix = Qnil;
29179 DEFSYM (Qline_prefix, "line-prefix");
29180 Fmake_variable_buffer_local (Qline_prefix);
29181
29182 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29183 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29184 inhibit_eval_during_redisplay = 0;
29185
29186 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29187 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29188 inhibit_free_realized_faces = 0;
29189
29190 #ifdef GLYPH_DEBUG
29191 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29192 doc: /* Inhibit try_window_id display optimization. */);
29193 inhibit_try_window_id = 0;
29194
29195 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29196 doc: /* Inhibit try_window_reusing display optimization. */);
29197 inhibit_try_window_reusing = 0;
29198
29199 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29200 doc: /* Inhibit try_cursor_movement display optimization. */);
29201 inhibit_try_cursor_movement = 0;
29202 #endif /* GLYPH_DEBUG */
29203
29204 DEFVAR_INT ("overline-margin", overline_margin,
29205 doc: /* Space between overline and text, in pixels.
29206 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29207 margin to the character height. */);
29208 overline_margin = 2;
29209
29210 DEFVAR_INT ("underline-minimum-offset",
29211 underline_minimum_offset,
29212 doc: /* Minimum distance between baseline and underline.
29213 This can improve legibility of underlined text at small font sizes,
29214 particularly when using variable `x-use-underline-position-properties'
29215 with fonts that specify an UNDERLINE_POSITION relatively close to the
29216 baseline. The default value is 1. */);
29217 underline_minimum_offset = 1;
29218
29219 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29220 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29221 This feature only works when on a window system that can change
29222 cursor shapes. */);
29223 display_hourglass_p = 1;
29224
29225 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29226 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29227 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29228
29229 hourglass_atimer = NULL;
29230 hourglass_shown_p = 0;
29231
29232 DEFSYM (Qglyphless_char, "glyphless-char");
29233 DEFSYM (Qhex_code, "hex-code");
29234 DEFSYM (Qempty_box, "empty-box");
29235 DEFSYM (Qthin_space, "thin-space");
29236 DEFSYM (Qzero_width, "zero-width");
29237
29238 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29239 /* Intern this now in case it isn't already done.
29240 Setting this variable twice is harmless.
29241 But don't staticpro it here--that is done in alloc.c. */
29242 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29243 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29244
29245 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29246 doc: /* Char-table defining glyphless characters.
29247 Each element, if non-nil, should be one of the following:
29248 an ASCII acronym string: display this string in a box
29249 `hex-code': display the hexadecimal code of a character in a box
29250 `empty-box': display as an empty box
29251 `thin-space': display as 1-pixel width space
29252 `zero-width': don't display
29253 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29254 display method for graphical terminals and text terminals respectively.
29255 GRAPHICAL and TEXT should each have one of the values listed above.
29256
29257 The char-table has one extra slot to control the display of a character for
29258 which no font is found. This slot only takes effect on graphical terminals.
29259 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29260 `thin-space'. The default is `empty-box'. */);
29261 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29262 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29263 Qempty_box);
29264 }
29265
29266
29267 /* Initialize this module when Emacs starts. */
29268
29269 void
29270 init_xdisp (void)
29271 {
29272 current_header_line_height = current_mode_line_height = -1;
29273
29274 CHARPOS (this_line_start_pos) = 0;
29275
29276 if (!noninteractive)
29277 {
29278 struct window *m = XWINDOW (minibuf_window);
29279 Lisp_Object frame = m->frame;
29280 struct frame *f = XFRAME (frame);
29281 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29282 struct window *r = XWINDOW (root);
29283 int i;
29284
29285 echo_area_window = minibuf_window;
29286
29287 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29288 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29289 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29290 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29291 XSETFASTINT (m->total_lines, 1);
29292 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29293
29294 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29295 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29296 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29297
29298 /* The default ellipsis glyphs `...'. */
29299 for (i = 0; i < 3; ++i)
29300 default_invis_vector[i] = make_number ('.');
29301 }
29302
29303 {
29304 /* Allocate the buffer for frame titles.
29305 Also used for `format-mode-line'. */
29306 int size = 100;
29307 mode_line_noprop_buf = xmalloc (size);
29308 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29309 mode_line_noprop_ptr = mode_line_noprop_buf;
29310 mode_line_target = MODE_LINE_DISPLAY;
29311 }
29312
29313 help_echo_showing_p = 0;
29314 }
29315
29316 /* Since w32 does not support atimers, it defines its own implementation of
29317 the following three functions in w32fns.c. */
29318 #ifndef WINDOWSNT
29319
29320 /* Platform-independent portion of hourglass implementation. */
29321
29322 /* Cancel a currently active hourglass timer, and start a new one. */
29323 void
29324 start_hourglass (void)
29325 {
29326 #if defined (HAVE_WINDOW_SYSTEM)
29327 EMACS_TIME delay;
29328
29329 cancel_hourglass ();
29330
29331 if (INTEGERP (Vhourglass_delay)
29332 && XINT (Vhourglass_delay) > 0)
29333 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29334 TYPE_MAXIMUM (time_t)),
29335 0);
29336 else if (FLOATP (Vhourglass_delay)
29337 && XFLOAT_DATA (Vhourglass_delay) > 0)
29338 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29339 else
29340 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29341
29342 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29343 show_hourglass, NULL);
29344 #endif
29345 }
29346
29347
29348 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29349 shown. */
29350 void
29351 cancel_hourglass (void)
29352 {
29353 #if defined (HAVE_WINDOW_SYSTEM)
29354 if (hourglass_atimer)
29355 {
29356 cancel_atimer (hourglass_atimer);
29357 hourglass_atimer = NULL;
29358 }
29359
29360 if (hourglass_shown_p)
29361 hide_hourglass ();
29362 #endif
29363 }
29364 #endif /* ! WINDOWSNT */