Fix failure to compile on Windows due to 2012-07-27T06:04:35Z!dmantipov@yandex.ru.
[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
2407 /* Evaluate SEXPR and return the result, or nil if something went
2408 wrong. Prevent redisplay during the evaluation. */
2409
2410 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2411 Return the result, or nil if something went wrong. Prevent
2412 redisplay during the evaluation. */
2413
2414 Lisp_Object
2415 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2416 {
2417 Lisp_Object val;
2418
2419 if (inhibit_eval_during_redisplay)
2420 val = Qnil;
2421 else
2422 {
2423 ptrdiff_t count = SPECPDL_INDEX ();
2424 struct gcpro gcpro1;
2425
2426 GCPRO1 (args[0]);
2427 gcpro1.nvars = nargs;
2428 specbind (Qinhibit_redisplay, Qt);
2429 /* Use Qt to ensure debugger does not run,
2430 so there is no possibility of wanting to redisplay. */
2431 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2432 safe_eval_handler);
2433 UNGCPRO;
2434 val = unbind_to (count, val);
2435 }
2436
2437 return val;
2438 }
2439
2440
2441 /* Call function FN with one argument ARG.
2442 Return the result, or nil if something went wrong. */
2443
2444 Lisp_Object
2445 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2446 {
2447 Lisp_Object args[2];
2448 args[0] = fn;
2449 args[1] = arg;
2450 return safe_call (2, args);
2451 }
2452
2453 static Lisp_Object Qeval;
2454
2455 Lisp_Object
2456 safe_eval (Lisp_Object sexpr)
2457 {
2458 return safe_call1 (Qeval, sexpr);
2459 }
2460
2461 /* Call function FN with one argument ARG.
2462 Return the result, or nil if something went wrong. */
2463
2464 Lisp_Object
2465 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2466 {
2467 Lisp_Object args[3];
2468 args[0] = fn;
2469 args[1] = arg1;
2470 args[2] = arg2;
2471 return safe_call (3, args);
2472 }
2473
2474
2475 \f
2476 /***********************************************************************
2477 Debugging
2478 ***********************************************************************/
2479
2480 #if 0
2481
2482 /* Define CHECK_IT to perform sanity checks on iterators.
2483 This is for debugging. It is too slow to do unconditionally. */
2484
2485 static void
2486 check_it (struct it *it)
2487 {
2488 if (it->method == GET_FROM_STRING)
2489 {
2490 eassert (STRINGP (it->string));
2491 eassert (IT_STRING_CHARPOS (*it) >= 0);
2492 }
2493 else
2494 {
2495 eassert (IT_STRING_CHARPOS (*it) < 0);
2496 if (it->method == GET_FROM_BUFFER)
2497 {
2498 /* Check that character and byte positions agree. */
2499 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2500 }
2501 }
2502
2503 if (it->dpvec)
2504 eassert (it->current.dpvec_index >= 0);
2505 else
2506 eassert (it->current.dpvec_index < 0);
2507 }
2508
2509 #define CHECK_IT(IT) check_it ((IT))
2510
2511 #else /* not 0 */
2512
2513 #define CHECK_IT(IT) (void) 0
2514
2515 #endif /* not 0 */
2516
2517
2518 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2519
2520 /* Check that the window end of window W is what we expect it
2521 to be---the last row in the current matrix displaying text. */
2522
2523 static void
2524 check_window_end (struct window *w)
2525 {
2526 if (!MINI_WINDOW_P (w)
2527 && !NILP (w->window_end_valid))
2528 {
2529 struct glyph_row *row;
2530 eassert ((row = MATRIX_ROW (w->current_matrix,
2531 XFASTINT (w->window_end_vpos)),
2532 !row->enabled_p
2533 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2534 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2535 }
2536 }
2537
2538 #define CHECK_WINDOW_END(W) check_window_end ((W))
2539
2540 #else
2541
2542 #define CHECK_WINDOW_END(W) (void) 0
2543
2544 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2545
2546
2547 \f
2548 /***********************************************************************
2549 Iterator initialization
2550 ***********************************************************************/
2551
2552 /* Initialize IT for displaying current_buffer in window W, starting
2553 at character position CHARPOS. CHARPOS < 0 means that no buffer
2554 position is specified which is useful when the iterator is assigned
2555 a position later. BYTEPOS is the byte position corresponding to
2556 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2557
2558 If ROW is not null, calls to produce_glyphs with IT as parameter
2559 will produce glyphs in that row.
2560
2561 BASE_FACE_ID is the id of a base face to use. It must be one of
2562 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2563 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2564 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2565
2566 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2567 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2568 will be initialized to use the corresponding mode line glyph row of
2569 the desired matrix of W. */
2570
2571 void
2572 init_iterator (struct it *it, struct window *w,
2573 ptrdiff_t charpos, ptrdiff_t bytepos,
2574 struct glyph_row *row, enum face_id base_face_id)
2575 {
2576 int highlight_region_p;
2577 enum face_id remapped_base_face_id = base_face_id;
2578
2579 /* Some precondition checks. */
2580 eassert (w != NULL && it != NULL);
2581 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2582 && charpos <= ZV));
2583
2584 /* If face attributes have been changed since the last redisplay,
2585 free realized faces now because they depend on face definitions
2586 that might have changed. Don't free faces while there might be
2587 desired matrices pending which reference these faces. */
2588 if (face_change_count && !inhibit_free_realized_faces)
2589 {
2590 face_change_count = 0;
2591 free_all_realized_faces (Qnil);
2592 }
2593
2594 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2595 if (! NILP (Vface_remapping_alist))
2596 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2597
2598 /* Use one of the mode line rows of W's desired matrix if
2599 appropriate. */
2600 if (row == NULL)
2601 {
2602 if (base_face_id == MODE_LINE_FACE_ID
2603 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2604 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2605 else if (base_face_id == HEADER_LINE_FACE_ID)
2606 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2607 }
2608
2609 /* Clear IT. */
2610 memset (it, 0, sizeof *it);
2611 it->current.overlay_string_index = -1;
2612 it->current.dpvec_index = -1;
2613 it->base_face_id = remapped_base_face_id;
2614 it->string = Qnil;
2615 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2616 it->paragraph_embedding = L2R;
2617 it->bidi_it.string.lstring = Qnil;
2618 it->bidi_it.string.s = NULL;
2619 it->bidi_it.string.bufpos = 0;
2620
2621 /* The window in which we iterate over current_buffer: */
2622 XSETWINDOW (it->window, w);
2623 it->w = w;
2624 it->f = XFRAME (w->frame);
2625
2626 it->cmp_it.id = -1;
2627
2628 /* Extra space between lines (on window systems only). */
2629 if (base_face_id == DEFAULT_FACE_ID
2630 && FRAME_WINDOW_P (it->f))
2631 {
2632 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2633 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2634 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2635 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2636 * FRAME_LINE_HEIGHT (it->f));
2637 else if (it->f->extra_line_spacing > 0)
2638 it->extra_line_spacing = it->f->extra_line_spacing;
2639 it->max_extra_line_spacing = 0;
2640 }
2641
2642 /* If realized faces have been removed, e.g. because of face
2643 attribute changes of named faces, recompute them. When running
2644 in batch mode, the face cache of the initial frame is null. If
2645 we happen to get called, make a dummy face cache. */
2646 if (FRAME_FACE_CACHE (it->f) == NULL)
2647 init_frame_faces (it->f);
2648 if (FRAME_FACE_CACHE (it->f)->used == 0)
2649 recompute_basic_faces (it->f);
2650
2651 /* Current value of the `slice', `space-width', and 'height' properties. */
2652 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2653 it->space_width = Qnil;
2654 it->font_height = Qnil;
2655 it->override_ascent = -1;
2656
2657 /* Are control characters displayed as `^C'? */
2658 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2659
2660 /* -1 means everything between a CR and the following line end
2661 is invisible. >0 means lines indented more than this value are
2662 invisible. */
2663 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2664 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2665 selective_display)),
2666 PTRDIFF_MAX)
2667 : (!NILP (BVAR (current_buffer, selective_display))
2668 ? -1 : 0));
2669 it->selective_display_ellipsis_p
2670 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2671
2672 /* Display table to use. */
2673 it->dp = window_display_table (w);
2674
2675 /* Are multibyte characters enabled in current_buffer? */
2676 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2677
2678 /* Non-zero if we should highlight the region. */
2679 highlight_region_p
2680 = (!NILP (Vtransient_mark_mode)
2681 && !NILP (BVAR (current_buffer, mark_active))
2682 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2683
2684 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2685 start and end of a visible region in window IT->w. Set both to
2686 -1 to indicate no region. */
2687 if (highlight_region_p
2688 /* Maybe highlight only in selected window. */
2689 && (/* Either show region everywhere. */
2690 highlight_nonselected_windows
2691 /* Or show region in the selected window. */
2692 || w == XWINDOW (selected_window)
2693 /* Or show the region if we are in the mini-buffer and W is
2694 the window the mini-buffer refers to. */
2695 || (MINI_WINDOW_P (XWINDOW (selected_window))
2696 && WINDOWP (minibuf_selected_window)
2697 && w == XWINDOW (minibuf_selected_window))))
2698 {
2699 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2700 it->region_beg_charpos = min (PT, markpos);
2701 it->region_end_charpos = max (PT, markpos);
2702 }
2703 else
2704 it->region_beg_charpos = it->region_end_charpos = -1;
2705
2706 /* Get the position at which the redisplay_end_trigger hook should
2707 be run, if it is to be run at all. */
2708 if (MARKERP (w->redisplay_end_trigger)
2709 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2710 it->redisplay_end_trigger_charpos
2711 = marker_position (w->redisplay_end_trigger);
2712 else if (INTEGERP (w->redisplay_end_trigger))
2713 it->redisplay_end_trigger_charpos =
2714 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2715
2716 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2717
2718 /* Are lines in the display truncated? */
2719 if (base_face_id != DEFAULT_FACE_ID
2720 || it->w->hscroll
2721 || (! WINDOW_FULL_WIDTH_P (it->w)
2722 && ((!NILP (Vtruncate_partial_width_windows)
2723 && !INTEGERP (Vtruncate_partial_width_windows))
2724 || (INTEGERP (Vtruncate_partial_width_windows)
2725 && (WINDOW_TOTAL_COLS (it->w)
2726 < XINT (Vtruncate_partial_width_windows))))))
2727 it->line_wrap = TRUNCATE;
2728 else if (NILP (BVAR (current_buffer, truncate_lines)))
2729 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2730 ? WINDOW_WRAP : WORD_WRAP;
2731 else
2732 it->line_wrap = TRUNCATE;
2733
2734 /* Get dimensions of truncation and continuation glyphs. These are
2735 displayed as fringe bitmaps under X, but we need them for such
2736 frames when the fringes are turned off. But leave the dimensions
2737 zero for tooltip frames, as these glyphs look ugly there and also
2738 sabotage calculations of tooltip dimensions in x-show-tip. */
2739 if (!(FRAMEP (tip_frame) && it->f == XFRAME (tip_frame)))
2740 {
2741 if (it->line_wrap == TRUNCATE)
2742 {
2743 /* We will need the truncation glyph. */
2744 eassert (it->glyph_row == NULL);
2745 produce_special_glyphs (it, IT_TRUNCATION);
2746 it->truncation_pixel_width = it->pixel_width;
2747 }
2748 else
2749 {
2750 /* We will need the continuation glyph. */
2751 eassert (it->glyph_row == NULL);
2752 produce_special_glyphs (it, IT_CONTINUATION);
2753 it->continuation_pixel_width = it->pixel_width;
2754 }
2755 }
2756
2757 /* Reset these values to zero because the produce_special_glyphs
2758 above has changed them. */
2759 it->pixel_width = it->ascent = it->descent = 0;
2760 it->phys_ascent = it->phys_descent = 0;
2761
2762 /* Set this after getting the dimensions of truncation and
2763 continuation glyphs, so that we don't produce glyphs when calling
2764 produce_special_glyphs, above. */
2765 it->glyph_row = row;
2766 it->area = TEXT_AREA;
2767
2768 /* Forget any previous info about this row being reversed. */
2769 if (it->glyph_row)
2770 it->glyph_row->reversed_p = 0;
2771
2772 /* Get the dimensions of the display area. The display area
2773 consists of the visible window area plus a horizontally scrolled
2774 part to the left of the window. All x-values are relative to the
2775 start of this total display area. */
2776 if (base_face_id != DEFAULT_FACE_ID)
2777 {
2778 /* Mode lines, menu bar in terminal frames. */
2779 it->first_visible_x = 0;
2780 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2781 }
2782 else
2783 {
2784 it->first_visible_x =
2785 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2786 it->last_visible_x = (it->first_visible_x
2787 + window_box_width (w, TEXT_AREA));
2788
2789 /* If we truncate lines, leave room for the truncation glyph(s) at
2790 the right margin. Otherwise, leave room for the continuation
2791 glyph(s). Done only if the window has no fringes. Since we
2792 don't know at this point whether there will be any R2L lines in
2793 the window, we reserve space for truncation/continuation glyphs
2794 even if only one of the fringes is absent. */
2795 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2796 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2797 {
2798 if (it->line_wrap == TRUNCATE)
2799 it->last_visible_x -= it->truncation_pixel_width;
2800 else
2801 it->last_visible_x -= it->continuation_pixel_width;
2802 }
2803
2804 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2805 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2806 }
2807
2808 /* Leave room for a border glyph. */
2809 if (!FRAME_WINDOW_P (it->f)
2810 && !WINDOW_RIGHTMOST_P (it->w))
2811 it->last_visible_x -= 1;
2812
2813 it->last_visible_y = window_text_bottom_y (w);
2814
2815 /* For mode lines and alike, arrange for the first glyph having a
2816 left box line if the face specifies a box. */
2817 if (base_face_id != DEFAULT_FACE_ID)
2818 {
2819 struct face *face;
2820
2821 it->face_id = remapped_base_face_id;
2822
2823 /* If we have a boxed mode line, make the first character appear
2824 with a left box line. */
2825 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2826 if (face->box != FACE_NO_BOX)
2827 it->start_of_box_run_p = 1;
2828 }
2829
2830 /* If a buffer position was specified, set the iterator there,
2831 getting overlays and face properties from that position. */
2832 if (charpos >= BUF_BEG (current_buffer))
2833 {
2834 it->end_charpos = ZV;
2835 IT_CHARPOS (*it) = charpos;
2836
2837 /* We will rely on `reseat' to set this up properly, via
2838 handle_face_prop. */
2839 it->face_id = it->base_face_id;
2840
2841 /* Compute byte position if not specified. */
2842 if (bytepos < charpos)
2843 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2844 else
2845 IT_BYTEPOS (*it) = bytepos;
2846
2847 it->start = it->current;
2848 /* Do we need to reorder bidirectional text? Not if this is a
2849 unibyte buffer: by definition, none of the single-byte
2850 characters are strong R2L, so no reordering is needed. And
2851 bidi.c doesn't support unibyte buffers anyway. Also, don't
2852 reorder while we are loading loadup.el, since the tables of
2853 character properties needed for reordering are not yet
2854 available. */
2855 it->bidi_p =
2856 NILP (Vpurify_flag)
2857 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2858 && it->multibyte_p;
2859
2860 /* If we are to reorder bidirectional text, init the bidi
2861 iterator. */
2862 if (it->bidi_p)
2863 {
2864 /* Note the paragraph direction that this buffer wants to
2865 use. */
2866 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2867 Qleft_to_right))
2868 it->paragraph_embedding = L2R;
2869 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2870 Qright_to_left))
2871 it->paragraph_embedding = R2L;
2872 else
2873 it->paragraph_embedding = NEUTRAL_DIR;
2874 bidi_unshelve_cache (NULL, 0);
2875 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2876 &it->bidi_it);
2877 }
2878
2879 /* Compute faces etc. */
2880 reseat (it, it->current.pos, 1);
2881 }
2882
2883 CHECK_IT (it);
2884 }
2885
2886
2887 /* Initialize IT for the display of window W with window start POS. */
2888
2889 void
2890 start_display (struct it *it, struct window *w, struct text_pos pos)
2891 {
2892 struct glyph_row *row;
2893 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2894
2895 row = w->desired_matrix->rows + first_vpos;
2896 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2897 it->first_vpos = first_vpos;
2898
2899 /* Don't reseat to previous visible line start if current start
2900 position is in a string or image. */
2901 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2902 {
2903 int start_at_line_beg_p;
2904 int first_y = it->current_y;
2905
2906 /* If window start is not at a line start, skip forward to POS to
2907 get the correct continuation lines width. */
2908 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2909 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2910 if (!start_at_line_beg_p)
2911 {
2912 int new_x;
2913
2914 reseat_at_previous_visible_line_start (it);
2915 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2916
2917 new_x = it->current_x + it->pixel_width;
2918
2919 /* If lines are continued, this line may end in the middle
2920 of a multi-glyph character (e.g. a control character
2921 displayed as \003, or in the middle of an overlay
2922 string). In this case move_it_to above will not have
2923 taken us to the start of the continuation line but to the
2924 end of the continued line. */
2925 if (it->current_x > 0
2926 && it->line_wrap != TRUNCATE /* Lines are continued. */
2927 && (/* And glyph doesn't fit on the line. */
2928 new_x > it->last_visible_x
2929 /* Or it fits exactly and we're on a window
2930 system frame. */
2931 || (new_x == it->last_visible_x
2932 && FRAME_WINDOW_P (it->f)
2933 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2934 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2935 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2936 {
2937 if ((it->current.dpvec_index >= 0
2938 || it->current.overlay_string_index >= 0)
2939 /* If we are on a newline from a display vector or
2940 overlay string, then we are already at the end of
2941 a screen line; no need to go to the next line in
2942 that case, as this line is not really continued.
2943 (If we do go to the next line, C-e will not DTRT.) */
2944 && it->c != '\n')
2945 {
2946 set_iterator_to_next (it, 1);
2947 move_it_in_display_line_to (it, -1, -1, 0);
2948 }
2949
2950 it->continuation_lines_width += it->current_x;
2951 }
2952 /* If the character at POS is displayed via a display
2953 vector, move_it_to above stops at the final glyph of
2954 IT->dpvec. To make the caller redisplay that character
2955 again (a.k.a. start at POS), we need to reset the
2956 dpvec_index to the beginning of IT->dpvec. */
2957 else if (it->current.dpvec_index >= 0)
2958 it->current.dpvec_index = 0;
2959
2960 /* We're starting a new display line, not affected by the
2961 height of the continued line, so clear the appropriate
2962 fields in the iterator structure. */
2963 it->max_ascent = it->max_descent = 0;
2964 it->max_phys_ascent = it->max_phys_descent = 0;
2965
2966 it->current_y = first_y;
2967 it->vpos = 0;
2968 it->current_x = it->hpos = 0;
2969 }
2970 }
2971 }
2972
2973
2974 /* Return 1 if POS is a position in ellipses displayed for invisible
2975 text. W is the window we display, for text property lookup. */
2976
2977 static int
2978 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2979 {
2980 Lisp_Object prop, window;
2981 int ellipses_p = 0;
2982 ptrdiff_t charpos = CHARPOS (pos->pos);
2983
2984 /* If POS specifies a position in a display vector, this might
2985 be for an ellipsis displayed for invisible text. We won't
2986 get the iterator set up for delivering that ellipsis unless
2987 we make sure that it gets aware of the invisible text. */
2988 if (pos->dpvec_index >= 0
2989 && pos->overlay_string_index < 0
2990 && CHARPOS (pos->string_pos) < 0
2991 && charpos > BEGV
2992 && (XSETWINDOW (window, w),
2993 prop = Fget_char_property (make_number (charpos),
2994 Qinvisible, window),
2995 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2996 {
2997 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2998 window);
2999 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3000 }
3001
3002 return ellipses_p;
3003 }
3004
3005
3006 /* Initialize IT for stepping through current_buffer in window W,
3007 starting at position POS that includes overlay string and display
3008 vector/ control character translation position information. Value
3009 is zero if there are overlay strings with newlines at POS. */
3010
3011 static int
3012 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3013 {
3014 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3015 int i, overlay_strings_with_newlines = 0;
3016
3017 /* If POS specifies a position in a display vector, this might
3018 be for an ellipsis displayed for invisible text. We won't
3019 get the iterator set up for delivering that ellipsis unless
3020 we make sure that it gets aware of the invisible text. */
3021 if (in_ellipses_for_invisible_text_p (pos, w))
3022 {
3023 --charpos;
3024 bytepos = 0;
3025 }
3026
3027 /* Keep in mind: the call to reseat in init_iterator skips invisible
3028 text, so we might end up at a position different from POS. This
3029 is only a problem when POS is a row start after a newline and an
3030 overlay starts there with an after-string, and the overlay has an
3031 invisible property. Since we don't skip invisible text in
3032 display_line and elsewhere immediately after consuming the
3033 newline before the row start, such a POS will not be in a string,
3034 but the call to init_iterator below will move us to the
3035 after-string. */
3036 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3037
3038 /* This only scans the current chunk -- it should scan all chunks.
3039 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3040 to 16 in 22.1 to make this a lesser problem. */
3041 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3042 {
3043 const char *s = SSDATA (it->overlay_strings[i]);
3044 const char *e = s + SBYTES (it->overlay_strings[i]);
3045
3046 while (s < e && *s != '\n')
3047 ++s;
3048
3049 if (s < e)
3050 {
3051 overlay_strings_with_newlines = 1;
3052 break;
3053 }
3054 }
3055
3056 /* If position is within an overlay string, set up IT to the right
3057 overlay string. */
3058 if (pos->overlay_string_index >= 0)
3059 {
3060 int relative_index;
3061
3062 /* If the first overlay string happens to have a `display'
3063 property for an image, the iterator will be set up for that
3064 image, and we have to undo that setup first before we can
3065 correct the overlay string index. */
3066 if (it->method == GET_FROM_IMAGE)
3067 pop_it (it);
3068
3069 /* We already have the first chunk of overlay strings in
3070 IT->overlay_strings. Load more until the one for
3071 pos->overlay_string_index is in IT->overlay_strings. */
3072 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3073 {
3074 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3075 it->current.overlay_string_index = 0;
3076 while (n--)
3077 {
3078 load_overlay_strings (it, 0);
3079 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3080 }
3081 }
3082
3083 it->current.overlay_string_index = pos->overlay_string_index;
3084 relative_index = (it->current.overlay_string_index
3085 % OVERLAY_STRING_CHUNK_SIZE);
3086 it->string = it->overlay_strings[relative_index];
3087 eassert (STRINGP (it->string));
3088 it->current.string_pos = pos->string_pos;
3089 it->method = GET_FROM_STRING;
3090 }
3091
3092 if (CHARPOS (pos->string_pos) >= 0)
3093 {
3094 /* Recorded position is not in an overlay string, but in another
3095 string. This can only be a string from a `display' property.
3096 IT should already be filled with that string. */
3097 it->current.string_pos = pos->string_pos;
3098 eassert (STRINGP (it->string));
3099 }
3100
3101 /* Restore position in display vector translations, control
3102 character translations or ellipses. */
3103 if (pos->dpvec_index >= 0)
3104 {
3105 if (it->dpvec == NULL)
3106 get_next_display_element (it);
3107 eassert (it->dpvec && it->current.dpvec_index == 0);
3108 it->current.dpvec_index = pos->dpvec_index;
3109 }
3110
3111 CHECK_IT (it);
3112 return !overlay_strings_with_newlines;
3113 }
3114
3115
3116 /* Initialize IT for stepping through current_buffer in window W
3117 starting at ROW->start. */
3118
3119 static void
3120 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3121 {
3122 init_from_display_pos (it, w, &row->start);
3123 it->start = row->start;
3124 it->continuation_lines_width = row->continuation_lines_width;
3125 CHECK_IT (it);
3126 }
3127
3128
3129 /* Initialize IT for stepping through current_buffer in window W
3130 starting in the line following ROW, i.e. starting at ROW->end.
3131 Value is zero if there are overlay strings with newlines at ROW's
3132 end position. */
3133
3134 static int
3135 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3136 {
3137 int success = 0;
3138
3139 if (init_from_display_pos (it, w, &row->end))
3140 {
3141 if (row->continued_p)
3142 it->continuation_lines_width
3143 = row->continuation_lines_width + row->pixel_width;
3144 CHECK_IT (it);
3145 success = 1;
3146 }
3147
3148 return success;
3149 }
3150
3151
3152
3153 \f
3154 /***********************************************************************
3155 Text properties
3156 ***********************************************************************/
3157
3158 /* Called when IT reaches IT->stop_charpos. Handle text property and
3159 overlay changes. Set IT->stop_charpos to the next position where
3160 to stop. */
3161
3162 static void
3163 handle_stop (struct it *it)
3164 {
3165 enum prop_handled handled;
3166 int handle_overlay_change_p;
3167 struct props *p;
3168
3169 it->dpvec = NULL;
3170 it->current.dpvec_index = -1;
3171 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3172 it->ignore_overlay_strings_at_pos_p = 0;
3173 it->ellipsis_p = 0;
3174
3175 /* Use face of preceding text for ellipsis (if invisible) */
3176 if (it->selective_display_ellipsis_p)
3177 it->saved_face_id = it->face_id;
3178
3179 do
3180 {
3181 handled = HANDLED_NORMALLY;
3182
3183 /* Call text property handlers. */
3184 for (p = it_props; p->handler; ++p)
3185 {
3186 handled = p->handler (it);
3187
3188 if (handled == HANDLED_RECOMPUTE_PROPS)
3189 break;
3190 else if (handled == HANDLED_RETURN)
3191 {
3192 /* We still want to show before and after strings from
3193 overlays even if the actual buffer text is replaced. */
3194 if (!handle_overlay_change_p
3195 || it->sp > 1
3196 /* Don't call get_overlay_strings_1 if we already
3197 have overlay strings loaded, because doing so
3198 will load them again and push the iterator state
3199 onto the stack one more time, which is not
3200 expected by the rest of the code that processes
3201 overlay strings. */
3202 || (it->current.overlay_string_index < 0
3203 ? !get_overlay_strings_1 (it, 0, 0)
3204 : 0))
3205 {
3206 if (it->ellipsis_p)
3207 setup_for_ellipsis (it, 0);
3208 /* When handling a display spec, we might load an
3209 empty string. In that case, discard it here. We
3210 used to discard it in handle_single_display_spec,
3211 but that causes get_overlay_strings_1, above, to
3212 ignore overlay strings that we must check. */
3213 if (STRINGP (it->string) && !SCHARS (it->string))
3214 pop_it (it);
3215 return;
3216 }
3217 else if (STRINGP (it->string) && !SCHARS (it->string))
3218 pop_it (it);
3219 else
3220 {
3221 it->ignore_overlay_strings_at_pos_p = 1;
3222 it->string_from_display_prop_p = 0;
3223 it->from_disp_prop_p = 0;
3224 handle_overlay_change_p = 0;
3225 }
3226 handled = HANDLED_RECOMPUTE_PROPS;
3227 break;
3228 }
3229 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3230 handle_overlay_change_p = 0;
3231 }
3232
3233 if (handled != HANDLED_RECOMPUTE_PROPS)
3234 {
3235 /* Don't check for overlay strings below when set to deliver
3236 characters from a display vector. */
3237 if (it->method == GET_FROM_DISPLAY_VECTOR)
3238 handle_overlay_change_p = 0;
3239
3240 /* Handle overlay changes.
3241 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3242 if it finds overlays. */
3243 if (handle_overlay_change_p)
3244 handled = handle_overlay_change (it);
3245 }
3246
3247 if (it->ellipsis_p)
3248 {
3249 setup_for_ellipsis (it, 0);
3250 break;
3251 }
3252 }
3253 while (handled == HANDLED_RECOMPUTE_PROPS);
3254
3255 /* Determine where to stop next. */
3256 if (handled == HANDLED_NORMALLY)
3257 compute_stop_pos (it);
3258 }
3259
3260
3261 /* Compute IT->stop_charpos from text property and overlay change
3262 information for IT's current position. */
3263
3264 static void
3265 compute_stop_pos (struct it *it)
3266 {
3267 register INTERVAL iv, next_iv;
3268 Lisp_Object object, limit, position;
3269 ptrdiff_t charpos, bytepos;
3270
3271 if (STRINGP (it->string))
3272 {
3273 /* Strings are usually short, so don't limit the search for
3274 properties. */
3275 it->stop_charpos = it->end_charpos;
3276 object = it->string;
3277 limit = Qnil;
3278 charpos = IT_STRING_CHARPOS (*it);
3279 bytepos = IT_STRING_BYTEPOS (*it);
3280 }
3281 else
3282 {
3283 ptrdiff_t pos;
3284
3285 /* If end_charpos is out of range for some reason, such as a
3286 misbehaving display function, rationalize it (Bug#5984). */
3287 if (it->end_charpos > ZV)
3288 it->end_charpos = ZV;
3289 it->stop_charpos = it->end_charpos;
3290
3291 /* If next overlay change is in front of the current stop pos
3292 (which is IT->end_charpos), stop there. Note: value of
3293 next_overlay_change is point-max if no overlay change
3294 follows. */
3295 charpos = IT_CHARPOS (*it);
3296 bytepos = IT_BYTEPOS (*it);
3297 pos = next_overlay_change (charpos);
3298 if (pos < it->stop_charpos)
3299 it->stop_charpos = pos;
3300
3301 /* If showing the region, we have to stop at the region
3302 start or end because the face might change there. */
3303 if (it->region_beg_charpos > 0)
3304 {
3305 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3306 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3307 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3308 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3309 }
3310
3311 /* Set up variables for computing the stop position from text
3312 property changes. */
3313 XSETBUFFER (object, current_buffer);
3314 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3315 }
3316
3317 /* Get the interval containing IT's position. Value is a null
3318 interval if there isn't such an interval. */
3319 position = make_number (charpos);
3320 iv = validate_interval_range (object, &position, &position, 0);
3321 if (!NULL_INTERVAL_P (iv))
3322 {
3323 Lisp_Object values_here[LAST_PROP_IDX];
3324 struct props *p;
3325
3326 /* Get properties here. */
3327 for (p = it_props; p->handler; ++p)
3328 values_here[p->idx] = textget (iv->plist, *p->name);
3329
3330 /* Look for an interval following iv that has different
3331 properties. */
3332 for (next_iv = next_interval (iv);
3333 (!NULL_INTERVAL_P (next_iv)
3334 && (NILP (limit)
3335 || XFASTINT (limit) > next_iv->position));
3336 next_iv = next_interval (next_iv))
3337 {
3338 for (p = it_props; p->handler; ++p)
3339 {
3340 Lisp_Object new_value;
3341
3342 new_value = textget (next_iv->plist, *p->name);
3343 if (!EQ (values_here[p->idx], new_value))
3344 break;
3345 }
3346
3347 if (p->handler)
3348 break;
3349 }
3350
3351 if (!NULL_INTERVAL_P (next_iv))
3352 {
3353 if (INTEGERP (limit)
3354 && next_iv->position >= XFASTINT (limit))
3355 /* No text property change up to limit. */
3356 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3357 else
3358 /* Text properties change in next_iv. */
3359 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3360 }
3361 }
3362
3363 if (it->cmp_it.id < 0)
3364 {
3365 ptrdiff_t stoppos = it->end_charpos;
3366
3367 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3368 stoppos = -1;
3369 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3370 stoppos, it->string);
3371 }
3372
3373 eassert (STRINGP (it->string)
3374 || (it->stop_charpos >= BEGV
3375 && it->stop_charpos >= IT_CHARPOS (*it)));
3376 }
3377
3378
3379 /* Return the position of the next overlay change after POS in
3380 current_buffer. Value is point-max if no overlay change
3381 follows. This is like `next-overlay-change' but doesn't use
3382 xmalloc. */
3383
3384 static ptrdiff_t
3385 next_overlay_change (ptrdiff_t pos)
3386 {
3387 ptrdiff_t i, noverlays;
3388 ptrdiff_t endpos;
3389 Lisp_Object *overlays;
3390
3391 /* Get all overlays at the given position. */
3392 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3393
3394 /* If any of these overlays ends before endpos,
3395 use its ending point instead. */
3396 for (i = 0; i < noverlays; ++i)
3397 {
3398 Lisp_Object oend;
3399 ptrdiff_t oendpos;
3400
3401 oend = OVERLAY_END (overlays[i]);
3402 oendpos = OVERLAY_POSITION (oend);
3403 endpos = min (endpos, oendpos);
3404 }
3405
3406 return endpos;
3407 }
3408
3409 /* How many characters forward to search for a display property or
3410 display string. Searching too far forward makes the bidi display
3411 sluggish, especially in small windows. */
3412 #define MAX_DISP_SCAN 250
3413
3414 /* Return the character position of a display string at or after
3415 position specified by POSITION. If no display string exists at or
3416 after POSITION, return ZV. A display string is either an overlay
3417 with `display' property whose value is a string, or a `display'
3418 text property whose value is a string. STRING is data about the
3419 string to iterate; if STRING->lstring is nil, we are iterating a
3420 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3421 on a GUI frame. DISP_PROP is set to zero if we searched
3422 MAX_DISP_SCAN characters forward without finding any display
3423 strings, non-zero otherwise. It is set to 2 if the display string
3424 uses any kind of `(space ...)' spec that will produce a stretch of
3425 white space in the text area. */
3426 ptrdiff_t
3427 compute_display_string_pos (struct text_pos *position,
3428 struct bidi_string_data *string,
3429 int frame_window_p, int *disp_prop)
3430 {
3431 /* OBJECT = nil means current buffer. */
3432 Lisp_Object object =
3433 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3434 Lisp_Object pos, spec, limpos;
3435 int string_p = (string && (STRINGP (string->lstring) || string->s));
3436 ptrdiff_t eob = string_p ? string->schars : ZV;
3437 ptrdiff_t begb = string_p ? 0 : BEGV;
3438 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3439 ptrdiff_t lim =
3440 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3441 struct text_pos tpos;
3442 int rv = 0;
3443
3444 *disp_prop = 1;
3445
3446 if (charpos >= eob
3447 /* We don't support display properties whose values are strings
3448 that have display string properties. */
3449 || string->from_disp_str
3450 /* C strings cannot have display properties. */
3451 || (string->s && !STRINGP (object)))
3452 {
3453 *disp_prop = 0;
3454 return eob;
3455 }
3456
3457 /* If the character at CHARPOS is where the display string begins,
3458 return CHARPOS. */
3459 pos = make_number (charpos);
3460 if (STRINGP (object))
3461 bufpos = string->bufpos;
3462 else
3463 bufpos = charpos;
3464 tpos = *position;
3465 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3466 && (charpos <= begb
3467 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3468 object),
3469 spec))
3470 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3471 frame_window_p)))
3472 {
3473 if (rv == 2)
3474 *disp_prop = 2;
3475 return charpos;
3476 }
3477
3478 /* Look forward for the first character with a `display' property
3479 that will replace the underlying text when displayed. */
3480 limpos = make_number (lim);
3481 do {
3482 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3483 CHARPOS (tpos) = XFASTINT (pos);
3484 if (CHARPOS (tpos) >= lim)
3485 {
3486 *disp_prop = 0;
3487 break;
3488 }
3489 if (STRINGP (object))
3490 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3491 else
3492 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3493 spec = Fget_char_property (pos, Qdisplay, object);
3494 if (!STRINGP (object))
3495 bufpos = CHARPOS (tpos);
3496 } while (NILP (spec)
3497 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3498 bufpos, frame_window_p)));
3499 if (rv == 2)
3500 *disp_prop = 2;
3501
3502 return CHARPOS (tpos);
3503 }
3504
3505 /* Return the character position of the end of the display string that
3506 started at CHARPOS. If there's no display string at CHARPOS,
3507 return -1. A display string is either an overlay with `display'
3508 property whose value is a string or a `display' text property whose
3509 value is a string. */
3510 ptrdiff_t
3511 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3512 {
3513 /* OBJECT = nil means current buffer. */
3514 Lisp_Object object =
3515 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3516 Lisp_Object pos = make_number (charpos);
3517 ptrdiff_t eob =
3518 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3519
3520 if (charpos >= eob || (string->s && !STRINGP (object)))
3521 return eob;
3522
3523 /* It could happen that the display property or overlay was removed
3524 since we found it in compute_display_string_pos above. One way
3525 this can happen is if JIT font-lock was called (through
3526 handle_fontified_prop), and jit-lock-functions remove text
3527 properties or overlays from the portion of buffer that includes
3528 CHARPOS. Muse mode is known to do that, for example. In this
3529 case, we return -1 to the caller, to signal that no display
3530 string is actually present at CHARPOS. See bidi_fetch_char for
3531 how this is handled.
3532
3533 An alternative would be to never look for display properties past
3534 it->stop_charpos. But neither compute_display_string_pos nor
3535 bidi_fetch_char that calls it know or care where the next
3536 stop_charpos is. */
3537 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3538 return -1;
3539
3540 /* Look forward for the first character where the `display' property
3541 changes. */
3542 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3543
3544 return XFASTINT (pos);
3545 }
3546
3547
3548 \f
3549 /***********************************************************************
3550 Fontification
3551 ***********************************************************************/
3552
3553 /* Handle changes in the `fontified' property of the current buffer by
3554 calling hook functions from Qfontification_functions to fontify
3555 regions of text. */
3556
3557 static enum prop_handled
3558 handle_fontified_prop (struct it *it)
3559 {
3560 Lisp_Object prop, pos;
3561 enum prop_handled handled = HANDLED_NORMALLY;
3562
3563 if (!NILP (Vmemory_full))
3564 return handled;
3565
3566 /* Get the value of the `fontified' property at IT's current buffer
3567 position. (The `fontified' property doesn't have a special
3568 meaning in strings.) If the value is nil, call functions from
3569 Qfontification_functions. */
3570 if (!STRINGP (it->string)
3571 && it->s == NULL
3572 && !NILP (Vfontification_functions)
3573 && !NILP (Vrun_hooks)
3574 && (pos = make_number (IT_CHARPOS (*it)),
3575 prop = Fget_char_property (pos, Qfontified, Qnil),
3576 /* Ignore the special cased nil value always present at EOB since
3577 no amount of fontifying will be able to change it. */
3578 NILP (prop) && IT_CHARPOS (*it) < Z))
3579 {
3580 ptrdiff_t count = SPECPDL_INDEX ();
3581 Lisp_Object val;
3582 struct buffer *obuf = current_buffer;
3583 int begv = BEGV, zv = ZV;
3584 int old_clip_changed = current_buffer->clip_changed;
3585
3586 val = Vfontification_functions;
3587 specbind (Qfontification_functions, Qnil);
3588
3589 eassert (it->end_charpos == ZV);
3590
3591 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3592 safe_call1 (val, pos);
3593 else
3594 {
3595 Lisp_Object fns, fn;
3596 struct gcpro gcpro1, gcpro2;
3597
3598 fns = Qnil;
3599 GCPRO2 (val, fns);
3600
3601 for (; CONSP (val); val = XCDR (val))
3602 {
3603 fn = XCAR (val);
3604
3605 if (EQ (fn, Qt))
3606 {
3607 /* A value of t indicates this hook has a local
3608 binding; it means to run the global binding too.
3609 In a global value, t should not occur. If it
3610 does, we must ignore it to avoid an endless
3611 loop. */
3612 for (fns = Fdefault_value (Qfontification_functions);
3613 CONSP (fns);
3614 fns = XCDR (fns))
3615 {
3616 fn = XCAR (fns);
3617 if (!EQ (fn, Qt))
3618 safe_call1 (fn, pos);
3619 }
3620 }
3621 else
3622 safe_call1 (fn, pos);
3623 }
3624
3625 UNGCPRO;
3626 }
3627
3628 unbind_to (count, Qnil);
3629
3630 /* Fontification functions routinely call `save-restriction'.
3631 Normally, this tags clip_changed, which can confuse redisplay
3632 (see discussion in Bug#6671). Since we don't perform any
3633 special handling of fontification changes in the case where
3634 `save-restriction' isn't called, there's no point doing so in
3635 this case either. So, if the buffer's restrictions are
3636 actually left unchanged, reset clip_changed. */
3637 if (obuf == current_buffer)
3638 {
3639 if (begv == BEGV && zv == ZV)
3640 current_buffer->clip_changed = old_clip_changed;
3641 }
3642 /* There isn't much we can reasonably do to protect against
3643 misbehaving fontification, but here's a fig leaf. */
3644 else if (!NILP (BVAR (obuf, name)))
3645 set_buffer_internal_1 (obuf);
3646
3647 /* The fontification code may have added/removed text.
3648 It could do even a lot worse, but let's at least protect against
3649 the most obvious case where only the text past `pos' gets changed',
3650 as is/was done in grep.el where some escapes sequences are turned
3651 into face properties (bug#7876). */
3652 it->end_charpos = ZV;
3653
3654 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3655 something. This avoids an endless loop if they failed to
3656 fontify the text for which reason ever. */
3657 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3658 handled = HANDLED_RECOMPUTE_PROPS;
3659 }
3660
3661 return handled;
3662 }
3663
3664
3665 \f
3666 /***********************************************************************
3667 Faces
3668 ***********************************************************************/
3669
3670 /* Set up iterator IT from face properties at its current position.
3671 Called from handle_stop. */
3672
3673 static enum prop_handled
3674 handle_face_prop (struct it *it)
3675 {
3676 int new_face_id;
3677 ptrdiff_t next_stop;
3678
3679 if (!STRINGP (it->string))
3680 {
3681 new_face_id
3682 = face_at_buffer_position (it->w,
3683 IT_CHARPOS (*it),
3684 it->region_beg_charpos,
3685 it->region_end_charpos,
3686 &next_stop,
3687 (IT_CHARPOS (*it)
3688 + TEXT_PROP_DISTANCE_LIMIT),
3689 0, it->base_face_id);
3690
3691 /* Is this a start of a run of characters with box face?
3692 Caveat: this can be called for a freshly initialized
3693 iterator; face_id is -1 in this case. We know that the new
3694 face will not change until limit, i.e. if the new face has a
3695 box, all characters up to limit will have one. But, as
3696 usual, we don't know whether limit is really the end. */
3697 if (new_face_id != it->face_id)
3698 {
3699 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3700
3701 /* If new face has a box but old face has not, this is
3702 the start of a run of characters with box, i.e. it has
3703 a shadow on the left side. The value of face_id of the
3704 iterator will be -1 if this is the initial call that gets
3705 the face. In this case, we have to look in front of IT's
3706 position and see whether there is a face != new_face_id. */
3707 it->start_of_box_run_p
3708 = (new_face->box != FACE_NO_BOX
3709 && (it->face_id >= 0
3710 || IT_CHARPOS (*it) == BEG
3711 || new_face_id != face_before_it_pos (it)));
3712 it->face_box_p = new_face->box != FACE_NO_BOX;
3713 }
3714 }
3715 else
3716 {
3717 int base_face_id;
3718 ptrdiff_t bufpos;
3719 int i;
3720 Lisp_Object from_overlay
3721 = (it->current.overlay_string_index >= 0
3722 ? it->string_overlays[it->current.overlay_string_index
3723 % OVERLAY_STRING_CHUNK_SIZE]
3724 : Qnil);
3725
3726 /* See if we got to this string directly or indirectly from
3727 an overlay property. That includes the before-string or
3728 after-string of an overlay, strings in display properties
3729 provided by an overlay, their text properties, etc.
3730
3731 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3732 if (! NILP (from_overlay))
3733 for (i = it->sp - 1; i >= 0; i--)
3734 {
3735 if (it->stack[i].current.overlay_string_index >= 0)
3736 from_overlay
3737 = it->string_overlays[it->stack[i].current.overlay_string_index
3738 % OVERLAY_STRING_CHUNK_SIZE];
3739 else if (! NILP (it->stack[i].from_overlay))
3740 from_overlay = it->stack[i].from_overlay;
3741
3742 if (!NILP (from_overlay))
3743 break;
3744 }
3745
3746 if (! NILP (from_overlay))
3747 {
3748 bufpos = IT_CHARPOS (*it);
3749 /* For a string from an overlay, the base face depends
3750 only on text properties and ignores overlays. */
3751 base_face_id
3752 = face_for_overlay_string (it->w,
3753 IT_CHARPOS (*it),
3754 it->region_beg_charpos,
3755 it->region_end_charpos,
3756 &next_stop,
3757 (IT_CHARPOS (*it)
3758 + TEXT_PROP_DISTANCE_LIMIT),
3759 0,
3760 from_overlay);
3761 }
3762 else
3763 {
3764 bufpos = 0;
3765
3766 /* For strings from a `display' property, use the face at
3767 IT's current buffer position as the base face to merge
3768 with, so that overlay strings appear in the same face as
3769 surrounding text, unless they specify their own
3770 faces. */
3771 base_face_id = it->string_from_prefix_prop_p
3772 ? DEFAULT_FACE_ID
3773 : underlying_face_id (it);
3774 }
3775
3776 new_face_id = face_at_string_position (it->w,
3777 it->string,
3778 IT_STRING_CHARPOS (*it),
3779 bufpos,
3780 it->region_beg_charpos,
3781 it->region_end_charpos,
3782 &next_stop,
3783 base_face_id, 0);
3784
3785 /* Is this a start of a run of characters with box? Caveat:
3786 this can be called for a freshly allocated iterator; face_id
3787 is -1 is this case. We know that the new face will not
3788 change until the next check pos, i.e. if the new face has a
3789 box, all characters up to that position will have a
3790 box. But, as usual, we don't know whether that position
3791 is really the end. */
3792 if (new_face_id != it->face_id)
3793 {
3794 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3795 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3796
3797 /* If new face has a box but old face hasn't, this is the
3798 start of a run of characters with box, i.e. it has a
3799 shadow on the left side. */
3800 it->start_of_box_run_p
3801 = new_face->box && (old_face == NULL || !old_face->box);
3802 it->face_box_p = new_face->box != FACE_NO_BOX;
3803 }
3804 }
3805
3806 it->face_id = new_face_id;
3807 return HANDLED_NORMALLY;
3808 }
3809
3810
3811 /* Return the ID of the face ``underlying'' IT's current position,
3812 which is in a string. If the iterator is associated with a
3813 buffer, return the face at IT's current buffer position.
3814 Otherwise, use the iterator's base_face_id. */
3815
3816 static int
3817 underlying_face_id (struct it *it)
3818 {
3819 int face_id = it->base_face_id, i;
3820
3821 eassert (STRINGP (it->string));
3822
3823 for (i = it->sp - 1; i >= 0; --i)
3824 if (NILP (it->stack[i].string))
3825 face_id = it->stack[i].face_id;
3826
3827 return face_id;
3828 }
3829
3830
3831 /* Compute the face one character before or after the current position
3832 of IT, in the visual order. BEFORE_P non-zero means get the face
3833 in front (to the left in L2R paragraphs, to the right in R2L
3834 paragraphs) of IT's screen position. Value is the ID of the face. */
3835
3836 static int
3837 face_before_or_after_it_pos (struct it *it, int before_p)
3838 {
3839 int face_id, limit;
3840 ptrdiff_t next_check_charpos;
3841 struct it it_copy;
3842 void *it_copy_data = NULL;
3843
3844 eassert (it->s == NULL);
3845
3846 if (STRINGP (it->string))
3847 {
3848 ptrdiff_t bufpos, charpos;
3849 int base_face_id;
3850
3851 /* No face change past the end of the string (for the case
3852 we are padding with spaces). No face change before the
3853 string start. */
3854 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3855 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3856 return it->face_id;
3857
3858 if (!it->bidi_p)
3859 {
3860 /* Set charpos to the position before or after IT's current
3861 position, in the logical order, which in the non-bidi
3862 case is the same as the visual order. */
3863 if (before_p)
3864 charpos = IT_STRING_CHARPOS (*it) - 1;
3865 else if (it->what == IT_COMPOSITION)
3866 /* For composition, we must check the character after the
3867 composition. */
3868 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3869 else
3870 charpos = IT_STRING_CHARPOS (*it) + 1;
3871 }
3872 else
3873 {
3874 if (before_p)
3875 {
3876 /* With bidi iteration, the character before the current
3877 in the visual order cannot be found by simple
3878 iteration, because "reverse" reordering is not
3879 supported. Instead, we need to use the move_it_*
3880 family of functions. */
3881 /* Ignore face changes before the first visible
3882 character on this display line. */
3883 if (it->current_x <= it->first_visible_x)
3884 return it->face_id;
3885 SAVE_IT (it_copy, *it, it_copy_data);
3886 /* Implementation note: Since move_it_in_display_line
3887 works in the iterator geometry, and thinks the first
3888 character is always the leftmost, even in R2L lines,
3889 we don't need to distinguish between the R2L and L2R
3890 cases here. */
3891 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3892 it_copy.current_x - 1, MOVE_TO_X);
3893 charpos = IT_STRING_CHARPOS (it_copy);
3894 RESTORE_IT (it, it, it_copy_data);
3895 }
3896 else
3897 {
3898 /* Set charpos to the string position of the character
3899 that comes after IT's current position in the visual
3900 order. */
3901 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3902
3903 it_copy = *it;
3904 while (n--)
3905 bidi_move_to_visually_next (&it_copy.bidi_it);
3906
3907 charpos = it_copy.bidi_it.charpos;
3908 }
3909 }
3910 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3911
3912 if (it->current.overlay_string_index >= 0)
3913 bufpos = IT_CHARPOS (*it);
3914 else
3915 bufpos = 0;
3916
3917 base_face_id = underlying_face_id (it);
3918
3919 /* Get the face for ASCII, or unibyte. */
3920 face_id = face_at_string_position (it->w,
3921 it->string,
3922 charpos,
3923 bufpos,
3924 it->region_beg_charpos,
3925 it->region_end_charpos,
3926 &next_check_charpos,
3927 base_face_id, 0);
3928
3929 /* Correct the face for charsets different from ASCII. Do it
3930 for the multibyte case only. The face returned above is
3931 suitable for unibyte text if IT->string is unibyte. */
3932 if (STRING_MULTIBYTE (it->string))
3933 {
3934 struct text_pos pos1 = string_pos (charpos, it->string);
3935 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3936 int c, len;
3937 struct face *face = FACE_FROM_ID (it->f, face_id);
3938
3939 c = string_char_and_length (p, &len);
3940 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3941 }
3942 }
3943 else
3944 {
3945 struct text_pos pos;
3946
3947 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3948 || (IT_CHARPOS (*it) <= BEGV && before_p))
3949 return it->face_id;
3950
3951 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3952 pos = it->current.pos;
3953
3954 if (!it->bidi_p)
3955 {
3956 if (before_p)
3957 DEC_TEXT_POS (pos, it->multibyte_p);
3958 else
3959 {
3960 if (it->what == IT_COMPOSITION)
3961 {
3962 /* For composition, we must check the position after
3963 the composition. */
3964 pos.charpos += it->cmp_it.nchars;
3965 pos.bytepos += it->len;
3966 }
3967 else
3968 INC_TEXT_POS (pos, it->multibyte_p);
3969 }
3970 }
3971 else
3972 {
3973 if (before_p)
3974 {
3975 /* With bidi iteration, the character before the current
3976 in the visual order cannot be found by simple
3977 iteration, because "reverse" reordering is not
3978 supported. Instead, we need to use the move_it_*
3979 family of functions. */
3980 /* Ignore face changes before the first visible
3981 character on this display line. */
3982 if (it->current_x <= it->first_visible_x)
3983 return it->face_id;
3984 SAVE_IT (it_copy, *it, it_copy_data);
3985 /* Implementation note: Since move_it_in_display_line
3986 works in the iterator geometry, and thinks the first
3987 character is always the leftmost, even in R2L lines,
3988 we don't need to distinguish between the R2L and L2R
3989 cases here. */
3990 move_it_in_display_line (&it_copy, ZV,
3991 it_copy.current_x - 1, MOVE_TO_X);
3992 pos = it_copy.current.pos;
3993 RESTORE_IT (it, it, it_copy_data);
3994 }
3995 else
3996 {
3997 /* Set charpos to the buffer position of the character
3998 that comes after IT's current position in the visual
3999 order. */
4000 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4001
4002 it_copy = *it;
4003 while (n--)
4004 bidi_move_to_visually_next (&it_copy.bidi_it);
4005
4006 SET_TEXT_POS (pos,
4007 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4008 }
4009 }
4010 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4011
4012 /* Determine face for CHARSET_ASCII, or unibyte. */
4013 face_id = face_at_buffer_position (it->w,
4014 CHARPOS (pos),
4015 it->region_beg_charpos,
4016 it->region_end_charpos,
4017 &next_check_charpos,
4018 limit, 0, -1);
4019
4020 /* Correct the face for charsets different from ASCII. Do it
4021 for the multibyte case only. The face returned above is
4022 suitable for unibyte text if current_buffer is unibyte. */
4023 if (it->multibyte_p)
4024 {
4025 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4026 struct face *face = FACE_FROM_ID (it->f, face_id);
4027 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4028 }
4029 }
4030
4031 return face_id;
4032 }
4033
4034
4035 \f
4036 /***********************************************************************
4037 Invisible text
4038 ***********************************************************************/
4039
4040 /* Set up iterator IT from invisible properties at its current
4041 position. Called from handle_stop. */
4042
4043 static enum prop_handled
4044 handle_invisible_prop (struct it *it)
4045 {
4046 enum prop_handled handled = HANDLED_NORMALLY;
4047
4048 if (STRINGP (it->string))
4049 {
4050 Lisp_Object prop, end_charpos, limit, charpos;
4051
4052 /* Get the value of the invisible text property at the
4053 current position. Value will be nil if there is no such
4054 property. */
4055 charpos = make_number (IT_STRING_CHARPOS (*it));
4056 prop = Fget_text_property (charpos, Qinvisible, it->string);
4057
4058 if (!NILP (prop)
4059 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4060 {
4061 ptrdiff_t endpos;
4062
4063 handled = HANDLED_RECOMPUTE_PROPS;
4064
4065 /* Get the position at which the next change of the
4066 invisible text property can be found in IT->string.
4067 Value will be nil if the property value is the same for
4068 all the rest of IT->string. */
4069 XSETINT (limit, SCHARS (it->string));
4070 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4071 it->string, limit);
4072
4073 /* Text at current position is invisible. The next
4074 change in the property is at position end_charpos.
4075 Move IT's current position to that position. */
4076 if (INTEGERP (end_charpos)
4077 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4078 {
4079 struct text_pos old;
4080 ptrdiff_t oldpos;
4081
4082 old = it->current.string_pos;
4083 oldpos = CHARPOS (old);
4084 if (it->bidi_p)
4085 {
4086 if (it->bidi_it.first_elt
4087 && it->bidi_it.charpos < SCHARS (it->string))
4088 bidi_paragraph_init (it->paragraph_embedding,
4089 &it->bidi_it, 1);
4090 /* Bidi-iterate out of the invisible text. */
4091 do
4092 {
4093 bidi_move_to_visually_next (&it->bidi_it);
4094 }
4095 while (oldpos <= it->bidi_it.charpos
4096 && it->bidi_it.charpos < endpos);
4097
4098 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4099 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4100 if (IT_CHARPOS (*it) >= endpos)
4101 it->prev_stop = endpos;
4102 }
4103 else
4104 {
4105 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4106 compute_string_pos (&it->current.string_pos, old, it->string);
4107 }
4108 }
4109 else
4110 {
4111 /* The rest of the string is invisible. If this is an
4112 overlay string, proceed with the next overlay string
4113 or whatever comes and return a character from there. */
4114 if (it->current.overlay_string_index >= 0)
4115 {
4116 next_overlay_string (it);
4117 /* Don't check for overlay strings when we just
4118 finished processing them. */
4119 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4120 }
4121 else
4122 {
4123 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4124 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4125 }
4126 }
4127 }
4128 }
4129 else
4130 {
4131 int invis_p;
4132 ptrdiff_t newpos, next_stop, start_charpos, tem;
4133 Lisp_Object pos, prop, overlay;
4134
4135 /* First of all, is there invisible text at this position? */
4136 tem = start_charpos = IT_CHARPOS (*it);
4137 pos = make_number (tem);
4138 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4139 &overlay);
4140 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4141
4142 /* If we are on invisible text, skip over it. */
4143 if (invis_p && start_charpos < it->end_charpos)
4144 {
4145 /* Record whether we have to display an ellipsis for the
4146 invisible text. */
4147 int display_ellipsis_p = invis_p == 2;
4148
4149 handled = HANDLED_RECOMPUTE_PROPS;
4150
4151 /* Loop skipping over invisible text. The loop is left at
4152 ZV or with IT on the first char being visible again. */
4153 do
4154 {
4155 /* Try to skip some invisible text. Return value is the
4156 position reached which can be equal to where we start
4157 if there is nothing invisible there. This skips both
4158 over invisible text properties and overlays with
4159 invisible property. */
4160 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4161
4162 /* If we skipped nothing at all we weren't at invisible
4163 text in the first place. If everything to the end of
4164 the buffer was skipped, end the loop. */
4165 if (newpos == tem || newpos >= ZV)
4166 invis_p = 0;
4167 else
4168 {
4169 /* We skipped some characters but not necessarily
4170 all there are. Check if we ended up on visible
4171 text. Fget_char_property returns the property of
4172 the char before the given position, i.e. if we
4173 get invis_p = 0, this means that the char at
4174 newpos is visible. */
4175 pos = make_number (newpos);
4176 prop = Fget_char_property (pos, Qinvisible, it->window);
4177 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4178 }
4179
4180 /* If we ended up on invisible text, proceed to
4181 skip starting with next_stop. */
4182 if (invis_p)
4183 tem = next_stop;
4184
4185 /* If there are adjacent invisible texts, don't lose the
4186 second one's ellipsis. */
4187 if (invis_p == 2)
4188 display_ellipsis_p = 1;
4189 }
4190 while (invis_p);
4191
4192 /* The position newpos is now either ZV or on visible text. */
4193 if (it->bidi_p)
4194 {
4195 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4196 int on_newline =
4197 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4198 int after_newline =
4199 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4200
4201 /* If the invisible text ends on a newline or on a
4202 character after a newline, we can avoid the costly,
4203 character by character, bidi iteration to NEWPOS, and
4204 instead simply reseat the iterator there. That's
4205 because all bidi reordering information is tossed at
4206 the newline. This is a big win for modes that hide
4207 complete lines, like Outline, Org, etc. */
4208 if (on_newline || after_newline)
4209 {
4210 struct text_pos tpos;
4211 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4212
4213 SET_TEXT_POS (tpos, newpos, bpos);
4214 reseat_1 (it, tpos, 0);
4215 /* If we reseat on a newline/ZV, we need to prep the
4216 bidi iterator for advancing to the next character
4217 after the newline/EOB, keeping the current paragraph
4218 direction (so that PRODUCE_GLYPHS does TRT wrt
4219 prepending/appending glyphs to a glyph row). */
4220 if (on_newline)
4221 {
4222 it->bidi_it.first_elt = 0;
4223 it->bidi_it.paragraph_dir = pdir;
4224 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4225 it->bidi_it.nchars = 1;
4226 it->bidi_it.ch_len = 1;
4227 }
4228 }
4229 else /* Must use the slow method. */
4230 {
4231 /* With bidi iteration, the region of invisible text
4232 could start and/or end in the middle of a
4233 non-base embedding level. Therefore, we need to
4234 skip invisible text using the bidi iterator,
4235 starting at IT's current position, until we find
4236 ourselves outside of the invisible text.
4237 Skipping invisible text _after_ bidi iteration
4238 avoids affecting the visual order of the
4239 displayed text when invisible properties are
4240 added or removed. */
4241 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4242 {
4243 /* If we were `reseat'ed to a new paragraph,
4244 determine the paragraph base direction. We
4245 need to do it now because
4246 next_element_from_buffer may not have a
4247 chance to do it, if we are going to skip any
4248 text at the beginning, which resets the
4249 FIRST_ELT flag. */
4250 bidi_paragraph_init (it->paragraph_embedding,
4251 &it->bidi_it, 1);
4252 }
4253 do
4254 {
4255 bidi_move_to_visually_next (&it->bidi_it);
4256 }
4257 while (it->stop_charpos <= it->bidi_it.charpos
4258 && it->bidi_it.charpos < newpos);
4259 IT_CHARPOS (*it) = it->bidi_it.charpos;
4260 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4261 /* If we overstepped NEWPOS, record its position in
4262 the iterator, so that we skip invisible text if
4263 later the bidi iteration lands us in the
4264 invisible region again. */
4265 if (IT_CHARPOS (*it) >= newpos)
4266 it->prev_stop = newpos;
4267 }
4268 }
4269 else
4270 {
4271 IT_CHARPOS (*it) = newpos;
4272 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4273 }
4274
4275 /* If there are before-strings at the start of invisible
4276 text, and the text is invisible because of a text
4277 property, arrange to show before-strings because 20.x did
4278 it that way. (If the text is invisible because of an
4279 overlay property instead of a text property, this is
4280 already handled in the overlay code.) */
4281 if (NILP (overlay)
4282 && get_overlay_strings (it, it->stop_charpos))
4283 {
4284 handled = HANDLED_RECOMPUTE_PROPS;
4285 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4286 }
4287 else if (display_ellipsis_p)
4288 {
4289 /* Make sure that the glyphs of the ellipsis will get
4290 correct `charpos' values. If we would not update
4291 it->position here, the glyphs would belong to the
4292 last visible character _before_ the invisible
4293 text, which confuses `set_cursor_from_row'.
4294
4295 We use the last invisible position instead of the
4296 first because this way the cursor is always drawn on
4297 the first "." of the ellipsis, whenever PT is inside
4298 the invisible text. Otherwise the cursor would be
4299 placed _after_ the ellipsis when the point is after the
4300 first invisible character. */
4301 if (!STRINGP (it->object))
4302 {
4303 it->position.charpos = newpos - 1;
4304 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4305 }
4306 it->ellipsis_p = 1;
4307 /* Let the ellipsis display before
4308 considering any properties of the following char.
4309 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4310 handled = HANDLED_RETURN;
4311 }
4312 }
4313 }
4314
4315 return handled;
4316 }
4317
4318
4319 /* Make iterator IT return `...' next.
4320 Replaces LEN characters from buffer. */
4321
4322 static void
4323 setup_for_ellipsis (struct it *it, int len)
4324 {
4325 /* Use the display table definition for `...'. Invalid glyphs
4326 will be handled by the method returning elements from dpvec. */
4327 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4328 {
4329 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4330 it->dpvec = v->contents;
4331 it->dpend = v->contents + v->header.size;
4332 }
4333 else
4334 {
4335 /* Default `...'. */
4336 it->dpvec = default_invis_vector;
4337 it->dpend = default_invis_vector + 3;
4338 }
4339
4340 it->dpvec_char_len = len;
4341 it->current.dpvec_index = 0;
4342 it->dpvec_face_id = -1;
4343
4344 /* Remember the current face id in case glyphs specify faces.
4345 IT's face is restored in set_iterator_to_next.
4346 saved_face_id was set to preceding char's face in handle_stop. */
4347 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4348 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4349
4350 it->method = GET_FROM_DISPLAY_VECTOR;
4351 it->ellipsis_p = 1;
4352 }
4353
4354
4355 \f
4356 /***********************************************************************
4357 'display' property
4358 ***********************************************************************/
4359
4360 /* Set up iterator IT from `display' property at its current position.
4361 Called from handle_stop.
4362 We return HANDLED_RETURN if some part of the display property
4363 overrides the display of the buffer text itself.
4364 Otherwise we return HANDLED_NORMALLY. */
4365
4366 static enum prop_handled
4367 handle_display_prop (struct it *it)
4368 {
4369 Lisp_Object propval, object, overlay;
4370 struct text_pos *position;
4371 ptrdiff_t bufpos;
4372 /* Nonzero if some property replaces the display of the text itself. */
4373 int display_replaced_p = 0;
4374
4375 if (STRINGP (it->string))
4376 {
4377 object = it->string;
4378 position = &it->current.string_pos;
4379 bufpos = CHARPOS (it->current.pos);
4380 }
4381 else
4382 {
4383 XSETWINDOW (object, it->w);
4384 position = &it->current.pos;
4385 bufpos = CHARPOS (*position);
4386 }
4387
4388 /* Reset those iterator values set from display property values. */
4389 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4390 it->space_width = Qnil;
4391 it->font_height = Qnil;
4392 it->voffset = 0;
4393
4394 /* We don't support recursive `display' properties, i.e. string
4395 values that have a string `display' property, that have a string
4396 `display' property etc. */
4397 if (!it->string_from_display_prop_p)
4398 it->area = TEXT_AREA;
4399
4400 propval = get_char_property_and_overlay (make_number (position->charpos),
4401 Qdisplay, object, &overlay);
4402 if (NILP (propval))
4403 return HANDLED_NORMALLY;
4404 /* Now OVERLAY is the overlay that gave us this property, or nil
4405 if it was a text property. */
4406
4407 if (!STRINGP (it->string))
4408 object = it->w->buffer;
4409
4410 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4411 position, bufpos,
4412 FRAME_WINDOW_P (it->f));
4413
4414 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4415 }
4416
4417 /* Subroutine of handle_display_prop. Returns non-zero if the display
4418 specification in SPEC is a replacing specification, i.e. it would
4419 replace the text covered by `display' property with something else,
4420 such as an image or a display string. If SPEC includes any kind or
4421 `(space ...) specification, the value is 2; this is used by
4422 compute_display_string_pos, which see.
4423
4424 See handle_single_display_spec for documentation of arguments.
4425 frame_window_p is non-zero if the window being redisplayed is on a
4426 GUI frame; this argument is used only if IT is NULL, see below.
4427
4428 IT can be NULL, if this is called by the bidi reordering code
4429 through compute_display_string_pos, which see. In that case, this
4430 function only examines SPEC, but does not otherwise "handle" it, in
4431 the sense that it doesn't set up members of IT from the display
4432 spec. */
4433 static int
4434 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4435 Lisp_Object overlay, struct text_pos *position,
4436 ptrdiff_t bufpos, int frame_window_p)
4437 {
4438 int replacing_p = 0;
4439 int rv;
4440
4441 if (CONSP (spec)
4442 /* Simple specifications. */
4443 && !EQ (XCAR (spec), Qimage)
4444 && !EQ (XCAR (spec), Qspace)
4445 && !EQ (XCAR (spec), Qwhen)
4446 && !EQ (XCAR (spec), Qslice)
4447 && !EQ (XCAR (spec), Qspace_width)
4448 && !EQ (XCAR (spec), Qheight)
4449 && !EQ (XCAR (spec), Qraise)
4450 /* Marginal area specifications. */
4451 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4452 && !EQ (XCAR (spec), Qleft_fringe)
4453 && !EQ (XCAR (spec), Qright_fringe)
4454 && !NILP (XCAR (spec)))
4455 {
4456 for (; CONSP (spec); spec = XCDR (spec))
4457 {
4458 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4459 overlay, position, bufpos,
4460 replacing_p, frame_window_p)))
4461 {
4462 replacing_p = rv;
4463 /* If some text in a string is replaced, `position' no
4464 longer points to the position of `object'. */
4465 if (!it || STRINGP (object))
4466 break;
4467 }
4468 }
4469 }
4470 else if (VECTORP (spec))
4471 {
4472 ptrdiff_t i;
4473 for (i = 0; i < ASIZE (spec); ++i)
4474 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4475 overlay, position, bufpos,
4476 replacing_p, frame_window_p)))
4477 {
4478 replacing_p = rv;
4479 /* If some text in a string is replaced, `position' no
4480 longer points to the position of `object'. */
4481 if (!it || STRINGP (object))
4482 break;
4483 }
4484 }
4485 else
4486 {
4487 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4488 position, bufpos, 0,
4489 frame_window_p)))
4490 replacing_p = rv;
4491 }
4492
4493 return replacing_p;
4494 }
4495
4496 /* Value is the position of the end of the `display' property starting
4497 at START_POS in OBJECT. */
4498
4499 static struct text_pos
4500 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4501 {
4502 Lisp_Object end;
4503 struct text_pos end_pos;
4504
4505 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4506 Qdisplay, object, Qnil);
4507 CHARPOS (end_pos) = XFASTINT (end);
4508 if (STRINGP (object))
4509 compute_string_pos (&end_pos, start_pos, it->string);
4510 else
4511 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4512
4513 return end_pos;
4514 }
4515
4516
4517 /* Set up IT from a single `display' property specification SPEC. OBJECT
4518 is the object in which the `display' property was found. *POSITION
4519 is the position in OBJECT at which the `display' property was found.
4520 BUFPOS is the buffer position of OBJECT (different from POSITION if
4521 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4522 previously saw a display specification which already replaced text
4523 display with something else, for example an image; we ignore such
4524 properties after the first one has been processed.
4525
4526 OVERLAY is the overlay this `display' property came from,
4527 or nil if it was a text property.
4528
4529 If SPEC is a `space' or `image' specification, and in some other
4530 cases too, set *POSITION to the position where the `display'
4531 property ends.
4532
4533 If IT is NULL, only examine the property specification in SPEC, but
4534 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4535 is intended to be displayed in a window on a GUI frame.
4536
4537 Value is non-zero if something was found which replaces the display
4538 of buffer or string text. */
4539
4540 static int
4541 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4542 Lisp_Object overlay, struct text_pos *position,
4543 ptrdiff_t bufpos, int display_replaced_p,
4544 int frame_window_p)
4545 {
4546 Lisp_Object form;
4547 Lisp_Object location, value;
4548 struct text_pos start_pos = *position;
4549 int valid_p;
4550
4551 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4552 If the result is non-nil, use VALUE instead of SPEC. */
4553 form = Qt;
4554 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4555 {
4556 spec = XCDR (spec);
4557 if (!CONSP (spec))
4558 return 0;
4559 form = XCAR (spec);
4560 spec = XCDR (spec);
4561 }
4562
4563 if (!NILP (form) && !EQ (form, Qt))
4564 {
4565 ptrdiff_t count = SPECPDL_INDEX ();
4566 struct gcpro gcpro1;
4567
4568 /* Bind `object' to the object having the `display' property, a
4569 buffer or string. Bind `position' to the position in the
4570 object where the property was found, and `buffer-position'
4571 to the current position in the buffer. */
4572
4573 if (NILP (object))
4574 XSETBUFFER (object, current_buffer);
4575 specbind (Qobject, object);
4576 specbind (Qposition, make_number (CHARPOS (*position)));
4577 specbind (Qbuffer_position, make_number (bufpos));
4578 GCPRO1 (form);
4579 form = safe_eval (form);
4580 UNGCPRO;
4581 unbind_to (count, Qnil);
4582 }
4583
4584 if (NILP (form))
4585 return 0;
4586
4587 /* Handle `(height HEIGHT)' specifications. */
4588 if (CONSP (spec)
4589 && EQ (XCAR (spec), Qheight)
4590 && CONSP (XCDR (spec)))
4591 {
4592 if (it)
4593 {
4594 if (!FRAME_WINDOW_P (it->f))
4595 return 0;
4596
4597 it->font_height = XCAR (XCDR (spec));
4598 if (!NILP (it->font_height))
4599 {
4600 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4601 int new_height = -1;
4602
4603 if (CONSP (it->font_height)
4604 && (EQ (XCAR (it->font_height), Qplus)
4605 || EQ (XCAR (it->font_height), Qminus))
4606 && CONSP (XCDR (it->font_height))
4607 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4608 {
4609 /* `(+ N)' or `(- N)' where N is an integer. */
4610 int steps = XINT (XCAR (XCDR (it->font_height)));
4611 if (EQ (XCAR (it->font_height), Qplus))
4612 steps = - steps;
4613 it->face_id = smaller_face (it->f, it->face_id, steps);
4614 }
4615 else if (FUNCTIONP (it->font_height))
4616 {
4617 /* Call function with current height as argument.
4618 Value is the new height. */
4619 Lisp_Object height;
4620 height = safe_call1 (it->font_height,
4621 face->lface[LFACE_HEIGHT_INDEX]);
4622 if (NUMBERP (height))
4623 new_height = XFLOATINT (height);
4624 }
4625 else if (NUMBERP (it->font_height))
4626 {
4627 /* Value is a multiple of the canonical char height. */
4628 struct face *f;
4629
4630 f = FACE_FROM_ID (it->f,
4631 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4632 new_height = (XFLOATINT (it->font_height)
4633 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4634 }
4635 else
4636 {
4637 /* Evaluate IT->font_height with `height' bound to the
4638 current specified height to get the new height. */
4639 ptrdiff_t count = SPECPDL_INDEX ();
4640
4641 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4642 value = safe_eval (it->font_height);
4643 unbind_to (count, Qnil);
4644
4645 if (NUMBERP (value))
4646 new_height = XFLOATINT (value);
4647 }
4648
4649 if (new_height > 0)
4650 it->face_id = face_with_height (it->f, it->face_id, new_height);
4651 }
4652 }
4653
4654 return 0;
4655 }
4656
4657 /* Handle `(space-width WIDTH)'. */
4658 if (CONSP (spec)
4659 && EQ (XCAR (spec), Qspace_width)
4660 && CONSP (XCDR (spec)))
4661 {
4662 if (it)
4663 {
4664 if (!FRAME_WINDOW_P (it->f))
4665 return 0;
4666
4667 value = XCAR (XCDR (spec));
4668 if (NUMBERP (value) && XFLOATINT (value) > 0)
4669 it->space_width = value;
4670 }
4671
4672 return 0;
4673 }
4674
4675 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4676 if (CONSP (spec)
4677 && EQ (XCAR (spec), Qslice))
4678 {
4679 Lisp_Object tem;
4680
4681 if (it)
4682 {
4683 if (!FRAME_WINDOW_P (it->f))
4684 return 0;
4685
4686 if (tem = XCDR (spec), CONSP (tem))
4687 {
4688 it->slice.x = XCAR (tem);
4689 if (tem = XCDR (tem), CONSP (tem))
4690 {
4691 it->slice.y = XCAR (tem);
4692 if (tem = XCDR (tem), CONSP (tem))
4693 {
4694 it->slice.width = XCAR (tem);
4695 if (tem = XCDR (tem), CONSP (tem))
4696 it->slice.height = XCAR (tem);
4697 }
4698 }
4699 }
4700 }
4701
4702 return 0;
4703 }
4704
4705 /* Handle `(raise FACTOR)'. */
4706 if (CONSP (spec)
4707 && EQ (XCAR (spec), Qraise)
4708 && CONSP (XCDR (spec)))
4709 {
4710 if (it)
4711 {
4712 if (!FRAME_WINDOW_P (it->f))
4713 return 0;
4714
4715 #ifdef HAVE_WINDOW_SYSTEM
4716 value = XCAR (XCDR (spec));
4717 if (NUMBERP (value))
4718 {
4719 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4720 it->voffset = - (XFLOATINT (value)
4721 * (FONT_HEIGHT (face->font)));
4722 }
4723 #endif /* HAVE_WINDOW_SYSTEM */
4724 }
4725
4726 return 0;
4727 }
4728
4729 /* Don't handle the other kinds of display specifications
4730 inside a string that we got from a `display' property. */
4731 if (it && it->string_from_display_prop_p)
4732 return 0;
4733
4734 /* Characters having this form of property are not displayed, so
4735 we have to find the end of the property. */
4736 if (it)
4737 {
4738 start_pos = *position;
4739 *position = display_prop_end (it, object, start_pos);
4740 }
4741 value = Qnil;
4742
4743 /* Stop the scan at that end position--we assume that all
4744 text properties change there. */
4745 if (it)
4746 it->stop_charpos = position->charpos;
4747
4748 /* Handle `(left-fringe BITMAP [FACE])'
4749 and `(right-fringe BITMAP [FACE])'. */
4750 if (CONSP (spec)
4751 && (EQ (XCAR (spec), Qleft_fringe)
4752 || EQ (XCAR (spec), Qright_fringe))
4753 && CONSP (XCDR (spec)))
4754 {
4755 int fringe_bitmap;
4756
4757 if (it)
4758 {
4759 if (!FRAME_WINDOW_P (it->f))
4760 /* If we return here, POSITION has been advanced
4761 across the text with this property. */
4762 {
4763 /* Synchronize the bidi iterator with POSITION. This is
4764 needed because we are not going to push the iterator
4765 on behalf of this display property, so there will be
4766 no pop_it call to do this synchronization for us. */
4767 if (it->bidi_p)
4768 {
4769 it->position = *position;
4770 iterate_out_of_display_property (it);
4771 *position = it->position;
4772 }
4773 return 1;
4774 }
4775 }
4776 else if (!frame_window_p)
4777 return 1;
4778
4779 #ifdef HAVE_WINDOW_SYSTEM
4780 value = XCAR (XCDR (spec));
4781 if (!SYMBOLP (value)
4782 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4783 /* If we return here, POSITION has been advanced
4784 across the text with this property. */
4785 {
4786 if (it && it->bidi_p)
4787 {
4788 it->position = *position;
4789 iterate_out_of_display_property (it);
4790 *position = it->position;
4791 }
4792 return 1;
4793 }
4794
4795 if (it)
4796 {
4797 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4798
4799 if (CONSP (XCDR (XCDR (spec))))
4800 {
4801 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4802 int face_id2 = lookup_derived_face (it->f, face_name,
4803 FRINGE_FACE_ID, 0);
4804 if (face_id2 >= 0)
4805 face_id = face_id2;
4806 }
4807
4808 /* Save current settings of IT so that we can restore them
4809 when we are finished with the glyph property value. */
4810 push_it (it, position);
4811
4812 it->area = TEXT_AREA;
4813 it->what = IT_IMAGE;
4814 it->image_id = -1; /* no image */
4815 it->position = start_pos;
4816 it->object = NILP (object) ? it->w->buffer : object;
4817 it->method = GET_FROM_IMAGE;
4818 it->from_overlay = Qnil;
4819 it->face_id = face_id;
4820 it->from_disp_prop_p = 1;
4821
4822 /* Say that we haven't consumed the characters with
4823 `display' property yet. The call to pop_it in
4824 set_iterator_to_next will clean this up. */
4825 *position = start_pos;
4826
4827 if (EQ (XCAR (spec), Qleft_fringe))
4828 {
4829 it->left_user_fringe_bitmap = fringe_bitmap;
4830 it->left_user_fringe_face_id = face_id;
4831 }
4832 else
4833 {
4834 it->right_user_fringe_bitmap = fringe_bitmap;
4835 it->right_user_fringe_face_id = face_id;
4836 }
4837 }
4838 #endif /* HAVE_WINDOW_SYSTEM */
4839 return 1;
4840 }
4841
4842 /* Prepare to handle `((margin left-margin) ...)',
4843 `((margin right-margin) ...)' and `((margin nil) ...)'
4844 prefixes for display specifications. */
4845 location = Qunbound;
4846 if (CONSP (spec) && CONSP (XCAR (spec)))
4847 {
4848 Lisp_Object tem;
4849
4850 value = XCDR (spec);
4851 if (CONSP (value))
4852 value = XCAR (value);
4853
4854 tem = XCAR (spec);
4855 if (EQ (XCAR (tem), Qmargin)
4856 && (tem = XCDR (tem),
4857 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4858 (NILP (tem)
4859 || EQ (tem, Qleft_margin)
4860 || EQ (tem, Qright_margin))))
4861 location = tem;
4862 }
4863
4864 if (EQ (location, Qunbound))
4865 {
4866 location = Qnil;
4867 value = spec;
4868 }
4869
4870 /* After this point, VALUE is the property after any
4871 margin prefix has been stripped. It must be a string,
4872 an image specification, or `(space ...)'.
4873
4874 LOCATION specifies where to display: `left-margin',
4875 `right-margin' or nil. */
4876
4877 valid_p = (STRINGP (value)
4878 #ifdef HAVE_WINDOW_SYSTEM
4879 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4880 && valid_image_p (value))
4881 #endif /* not HAVE_WINDOW_SYSTEM */
4882 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4883
4884 if (valid_p && !display_replaced_p)
4885 {
4886 int retval = 1;
4887
4888 if (!it)
4889 {
4890 /* Callers need to know whether the display spec is any kind
4891 of `(space ...)' spec that is about to affect text-area
4892 display. */
4893 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4894 retval = 2;
4895 return retval;
4896 }
4897
4898 /* Save current settings of IT so that we can restore them
4899 when we are finished with the glyph property value. */
4900 push_it (it, position);
4901 it->from_overlay = overlay;
4902 it->from_disp_prop_p = 1;
4903
4904 if (NILP (location))
4905 it->area = TEXT_AREA;
4906 else if (EQ (location, Qleft_margin))
4907 it->area = LEFT_MARGIN_AREA;
4908 else
4909 it->area = RIGHT_MARGIN_AREA;
4910
4911 if (STRINGP (value))
4912 {
4913 it->string = value;
4914 it->multibyte_p = STRING_MULTIBYTE (it->string);
4915 it->current.overlay_string_index = -1;
4916 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4917 it->end_charpos = it->string_nchars = SCHARS (it->string);
4918 it->method = GET_FROM_STRING;
4919 it->stop_charpos = 0;
4920 it->prev_stop = 0;
4921 it->base_level_stop = 0;
4922 it->string_from_display_prop_p = 1;
4923 /* Say that we haven't consumed the characters with
4924 `display' property yet. The call to pop_it in
4925 set_iterator_to_next will clean this up. */
4926 if (BUFFERP (object))
4927 *position = start_pos;
4928
4929 /* Force paragraph direction to be that of the parent
4930 object. If the parent object's paragraph direction is
4931 not yet determined, default to L2R. */
4932 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4933 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4934 else
4935 it->paragraph_embedding = L2R;
4936
4937 /* Set up the bidi iterator for this display string. */
4938 if (it->bidi_p)
4939 {
4940 it->bidi_it.string.lstring = it->string;
4941 it->bidi_it.string.s = NULL;
4942 it->bidi_it.string.schars = it->end_charpos;
4943 it->bidi_it.string.bufpos = bufpos;
4944 it->bidi_it.string.from_disp_str = 1;
4945 it->bidi_it.string.unibyte = !it->multibyte_p;
4946 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4947 }
4948 }
4949 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4950 {
4951 it->method = GET_FROM_STRETCH;
4952 it->object = value;
4953 *position = it->position = start_pos;
4954 retval = 1 + (it->area == TEXT_AREA);
4955 }
4956 #ifdef HAVE_WINDOW_SYSTEM
4957 else
4958 {
4959 it->what = IT_IMAGE;
4960 it->image_id = lookup_image (it->f, value);
4961 it->position = start_pos;
4962 it->object = NILP (object) ? it->w->buffer : object;
4963 it->method = GET_FROM_IMAGE;
4964
4965 /* Say that we haven't consumed the characters with
4966 `display' property yet. The call to pop_it in
4967 set_iterator_to_next will clean this up. */
4968 *position = start_pos;
4969 }
4970 #endif /* HAVE_WINDOW_SYSTEM */
4971
4972 return retval;
4973 }
4974
4975 /* Invalid property or property not supported. Restore
4976 POSITION to what it was before. */
4977 *position = start_pos;
4978 return 0;
4979 }
4980
4981 /* Check if PROP is a display property value whose text should be
4982 treated as intangible. OVERLAY is the overlay from which PROP
4983 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4984 specify the buffer position covered by PROP. */
4985
4986 int
4987 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4988 ptrdiff_t charpos, ptrdiff_t bytepos)
4989 {
4990 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4991 struct text_pos position;
4992
4993 SET_TEXT_POS (position, charpos, bytepos);
4994 return handle_display_spec (NULL, prop, Qnil, overlay,
4995 &position, charpos, frame_window_p);
4996 }
4997
4998
4999 /* Return 1 if PROP is a display sub-property value containing STRING.
5000
5001 Implementation note: this and the following function are really
5002 special cases of handle_display_spec and
5003 handle_single_display_spec, and should ideally use the same code.
5004 Until they do, these two pairs must be consistent and must be
5005 modified in sync. */
5006
5007 static int
5008 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5009 {
5010 if (EQ (string, prop))
5011 return 1;
5012
5013 /* Skip over `when FORM'. */
5014 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5015 {
5016 prop = XCDR (prop);
5017 if (!CONSP (prop))
5018 return 0;
5019 /* Actually, the condition following `when' should be eval'ed,
5020 like handle_single_display_spec does, and we should return
5021 zero if it evaluates to nil. However, this function is
5022 called only when the buffer was already displayed and some
5023 glyph in the glyph matrix was found to come from a display
5024 string. Therefore, the condition was already evaluated, and
5025 the result was non-nil, otherwise the display string wouldn't
5026 have been displayed and we would have never been called for
5027 this property. Thus, we can skip the evaluation and assume
5028 its result is non-nil. */
5029 prop = XCDR (prop);
5030 }
5031
5032 if (CONSP (prop))
5033 /* Skip over `margin LOCATION'. */
5034 if (EQ (XCAR (prop), Qmargin))
5035 {
5036 prop = XCDR (prop);
5037 if (!CONSP (prop))
5038 return 0;
5039
5040 prop = XCDR (prop);
5041 if (!CONSP (prop))
5042 return 0;
5043 }
5044
5045 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5046 }
5047
5048
5049 /* Return 1 if STRING appears in the `display' property PROP. */
5050
5051 static int
5052 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5053 {
5054 if (CONSP (prop)
5055 && !EQ (XCAR (prop), Qwhen)
5056 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5057 {
5058 /* A list of sub-properties. */
5059 while (CONSP (prop))
5060 {
5061 if (single_display_spec_string_p (XCAR (prop), string))
5062 return 1;
5063 prop = XCDR (prop);
5064 }
5065 }
5066 else if (VECTORP (prop))
5067 {
5068 /* A vector of sub-properties. */
5069 ptrdiff_t i;
5070 for (i = 0; i < ASIZE (prop); ++i)
5071 if (single_display_spec_string_p (AREF (prop, i), string))
5072 return 1;
5073 }
5074 else
5075 return single_display_spec_string_p (prop, string);
5076
5077 return 0;
5078 }
5079
5080 /* Look for STRING in overlays and text properties in the current
5081 buffer, between character positions FROM and TO (excluding TO).
5082 BACK_P non-zero means look back (in this case, TO is supposed to be
5083 less than FROM).
5084 Value is the first character position where STRING was found, or
5085 zero if it wasn't found before hitting TO.
5086
5087 This function may only use code that doesn't eval because it is
5088 called asynchronously from note_mouse_highlight. */
5089
5090 static ptrdiff_t
5091 string_buffer_position_lim (Lisp_Object string,
5092 ptrdiff_t from, ptrdiff_t to, int back_p)
5093 {
5094 Lisp_Object limit, prop, pos;
5095 int found = 0;
5096
5097 pos = make_number (max (from, BEGV));
5098
5099 if (!back_p) /* looking forward */
5100 {
5101 limit = make_number (min (to, ZV));
5102 while (!found && !EQ (pos, limit))
5103 {
5104 prop = Fget_char_property (pos, Qdisplay, Qnil);
5105 if (!NILP (prop) && display_prop_string_p (prop, string))
5106 found = 1;
5107 else
5108 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5109 limit);
5110 }
5111 }
5112 else /* looking back */
5113 {
5114 limit = make_number (max (to, BEGV));
5115 while (!found && !EQ (pos, limit))
5116 {
5117 prop = Fget_char_property (pos, Qdisplay, Qnil);
5118 if (!NILP (prop) && display_prop_string_p (prop, string))
5119 found = 1;
5120 else
5121 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5122 limit);
5123 }
5124 }
5125
5126 return found ? XINT (pos) : 0;
5127 }
5128
5129 /* Determine which buffer position in current buffer STRING comes from.
5130 AROUND_CHARPOS is an approximate position where it could come from.
5131 Value is the buffer position or 0 if it couldn't be determined.
5132
5133 This function is necessary because we don't record buffer positions
5134 in glyphs generated from strings (to keep struct glyph small).
5135 This function may only use code that doesn't eval because it is
5136 called asynchronously from note_mouse_highlight. */
5137
5138 static ptrdiff_t
5139 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5140 {
5141 const int MAX_DISTANCE = 1000;
5142 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5143 around_charpos + MAX_DISTANCE,
5144 0);
5145
5146 if (!found)
5147 found = string_buffer_position_lim (string, around_charpos,
5148 around_charpos - MAX_DISTANCE, 1);
5149 return found;
5150 }
5151
5152
5153 \f
5154 /***********************************************************************
5155 `composition' property
5156 ***********************************************************************/
5157
5158 /* Set up iterator IT from `composition' property at its current
5159 position. Called from handle_stop. */
5160
5161 static enum prop_handled
5162 handle_composition_prop (struct it *it)
5163 {
5164 Lisp_Object prop, string;
5165 ptrdiff_t pos, pos_byte, start, end;
5166
5167 if (STRINGP (it->string))
5168 {
5169 unsigned char *s;
5170
5171 pos = IT_STRING_CHARPOS (*it);
5172 pos_byte = IT_STRING_BYTEPOS (*it);
5173 string = it->string;
5174 s = SDATA (string) + pos_byte;
5175 it->c = STRING_CHAR (s);
5176 }
5177 else
5178 {
5179 pos = IT_CHARPOS (*it);
5180 pos_byte = IT_BYTEPOS (*it);
5181 string = Qnil;
5182 it->c = FETCH_CHAR (pos_byte);
5183 }
5184
5185 /* If there's a valid composition and point is not inside of the
5186 composition (in the case that the composition is from the current
5187 buffer), draw a glyph composed from the composition components. */
5188 if (find_composition (pos, -1, &start, &end, &prop, string)
5189 && COMPOSITION_VALID_P (start, end, prop)
5190 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5191 {
5192 if (start < pos)
5193 /* As we can't handle this situation (perhaps font-lock added
5194 a new composition), we just return here hoping that next
5195 redisplay will detect this composition much earlier. */
5196 return HANDLED_NORMALLY;
5197 if (start != pos)
5198 {
5199 if (STRINGP (it->string))
5200 pos_byte = string_char_to_byte (it->string, start);
5201 else
5202 pos_byte = CHAR_TO_BYTE (start);
5203 }
5204 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5205 prop, string);
5206
5207 if (it->cmp_it.id >= 0)
5208 {
5209 it->cmp_it.ch = -1;
5210 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5211 it->cmp_it.nglyphs = -1;
5212 }
5213 }
5214
5215 return HANDLED_NORMALLY;
5216 }
5217
5218
5219 \f
5220 /***********************************************************************
5221 Overlay strings
5222 ***********************************************************************/
5223
5224 /* The following structure is used to record overlay strings for
5225 later sorting in load_overlay_strings. */
5226
5227 struct overlay_entry
5228 {
5229 Lisp_Object overlay;
5230 Lisp_Object string;
5231 EMACS_INT priority;
5232 int after_string_p;
5233 };
5234
5235
5236 /* Set up iterator IT from overlay strings at its current position.
5237 Called from handle_stop. */
5238
5239 static enum prop_handled
5240 handle_overlay_change (struct it *it)
5241 {
5242 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5243 return HANDLED_RECOMPUTE_PROPS;
5244 else
5245 return HANDLED_NORMALLY;
5246 }
5247
5248
5249 /* Set up the next overlay string for delivery by IT, if there is an
5250 overlay string to deliver. Called by set_iterator_to_next when the
5251 end of the current overlay string is reached. If there are more
5252 overlay strings to display, IT->string and
5253 IT->current.overlay_string_index are set appropriately here.
5254 Otherwise IT->string is set to nil. */
5255
5256 static void
5257 next_overlay_string (struct it *it)
5258 {
5259 ++it->current.overlay_string_index;
5260 if (it->current.overlay_string_index == it->n_overlay_strings)
5261 {
5262 /* No more overlay strings. Restore IT's settings to what
5263 they were before overlay strings were processed, and
5264 continue to deliver from current_buffer. */
5265
5266 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5267 pop_it (it);
5268 eassert (it->sp > 0
5269 || (NILP (it->string)
5270 && it->method == GET_FROM_BUFFER
5271 && it->stop_charpos >= BEGV
5272 && it->stop_charpos <= it->end_charpos));
5273 it->current.overlay_string_index = -1;
5274 it->n_overlay_strings = 0;
5275 it->overlay_strings_charpos = -1;
5276 /* If there's an empty display string on the stack, pop the
5277 stack, to resync the bidi iterator with IT's position. Such
5278 empty strings are pushed onto the stack in
5279 get_overlay_strings_1. */
5280 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5281 pop_it (it);
5282
5283 /* If we're at the end of the buffer, record that we have
5284 processed the overlay strings there already, so that
5285 next_element_from_buffer doesn't try it again. */
5286 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5287 it->overlay_strings_at_end_processed_p = 1;
5288 }
5289 else
5290 {
5291 /* There are more overlay strings to process. If
5292 IT->current.overlay_string_index has advanced to a position
5293 where we must load IT->overlay_strings with more strings, do
5294 it. We must load at the IT->overlay_strings_charpos where
5295 IT->n_overlay_strings was originally computed; when invisible
5296 text is present, this might not be IT_CHARPOS (Bug#7016). */
5297 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5298
5299 if (it->current.overlay_string_index && i == 0)
5300 load_overlay_strings (it, it->overlay_strings_charpos);
5301
5302 /* Initialize IT to deliver display elements from the overlay
5303 string. */
5304 it->string = it->overlay_strings[i];
5305 it->multibyte_p = STRING_MULTIBYTE (it->string);
5306 SET_TEXT_POS (it->current.string_pos, 0, 0);
5307 it->method = GET_FROM_STRING;
5308 it->stop_charpos = 0;
5309 if (it->cmp_it.stop_pos >= 0)
5310 it->cmp_it.stop_pos = 0;
5311 it->prev_stop = 0;
5312 it->base_level_stop = 0;
5313
5314 /* Set up the bidi iterator for this overlay string. */
5315 if (it->bidi_p)
5316 {
5317 it->bidi_it.string.lstring = it->string;
5318 it->bidi_it.string.s = NULL;
5319 it->bidi_it.string.schars = SCHARS (it->string);
5320 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5321 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5322 it->bidi_it.string.unibyte = !it->multibyte_p;
5323 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5324 }
5325 }
5326
5327 CHECK_IT (it);
5328 }
5329
5330
5331 /* Compare two overlay_entry structures E1 and E2. Used as a
5332 comparison function for qsort in load_overlay_strings. Overlay
5333 strings for the same position are sorted so that
5334
5335 1. All after-strings come in front of before-strings, except
5336 when they come from the same overlay.
5337
5338 2. Within after-strings, strings are sorted so that overlay strings
5339 from overlays with higher priorities come first.
5340
5341 2. Within before-strings, strings are sorted so that overlay
5342 strings from overlays with higher priorities come last.
5343
5344 Value is analogous to strcmp. */
5345
5346
5347 static int
5348 compare_overlay_entries (const void *e1, const void *e2)
5349 {
5350 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5351 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5352 int result;
5353
5354 if (entry1->after_string_p != entry2->after_string_p)
5355 {
5356 /* Let after-strings appear in front of before-strings if
5357 they come from different overlays. */
5358 if (EQ (entry1->overlay, entry2->overlay))
5359 result = entry1->after_string_p ? 1 : -1;
5360 else
5361 result = entry1->after_string_p ? -1 : 1;
5362 }
5363 else if (entry1->priority != entry2->priority)
5364 {
5365 if (entry1->after_string_p)
5366 /* After-strings sorted in order of decreasing priority. */
5367 result = entry2->priority < entry1->priority ? -1 : 1;
5368 else
5369 /* Before-strings sorted in order of increasing priority. */
5370 result = entry1->priority < entry2->priority ? -1 : 1;
5371 }
5372 else
5373 result = 0;
5374
5375 return result;
5376 }
5377
5378
5379 /* Load the vector IT->overlay_strings with overlay strings from IT's
5380 current buffer position, or from CHARPOS if that is > 0. Set
5381 IT->n_overlays to the total number of overlay strings found.
5382
5383 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5384 a time. On entry into load_overlay_strings,
5385 IT->current.overlay_string_index gives the number of overlay
5386 strings that have already been loaded by previous calls to this
5387 function.
5388
5389 IT->add_overlay_start contains an additional overlay start
5390 position to consider for taking overlay strings from, if non-zero.
5391 This position comes into play when the overlay has an `invisible'
5392 property, and both before and after-strings. When we've skipped to
5393 the end of the overlay, because of its `invisible' property, we
5394 nevertheless want its before-string to appear.
5395 IT->add_overlay_start will contain the overlay start position
5396 in this case.
5397
5398 Overlay strings are sorted so that after-string strings come in
5399 front of before-string strings. Within before and after-strings,
5400 strings are sorted by overlay priority. See also function
5401 compare_overlay_entries. */
5402
5403 static void
5404 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5405 {
5406 Lisp_Object overlay, window, str, invisible;
5407 struct Lisp_Overlay *ov;
5408 ptrdiff_t start, end;
5409 ptrdiff_t size = 20;
5410 ptrdiff_t n = 0, i, j;
5411 int invis_p;
5412 struct overlay_entry *entries = alloca (size * sizeof *entries);
5413 USE_SAFE_ALLOCA;
5414
5415 if (charpos <= 0)
5416 charpos = IT_CHARPOS (*it);
5417
5418 /* Append the overlay string STRING of overlay OVERLAY to vector
5419 `entries' which has size `size' and currently contains `n'
5420 elements. AFTER_P non-zero means STRING is an after-string of
5421 OVERLAY. */
5422 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5423 do \
5424 { \
5425 Lisp_Object priority; \
5426 \
5427 if (n == size) \
5428 { \
5429 struct overlay_entry *old = entries; \
5430 SAFE_NALLOCA (entries, 2, size); \
5431 memcpy (entries, old, size * sizeof *entries); \
5432 size *= 2; \
5433 } \
5434 \
5435 entries[n].string = (STRING); \
5436 entries[n].overlay = (OVERLAY); \
5437 priority = Foverlay_get ((OVERLAY), Qpriority); \
5438 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5439 entries[n].after_string_p = (AFTER_P); \
5440 ++n; \
5441 } \
5442 while (0)
5443
5444 /* Process overlay before the overlay center. */
5445 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5446 {
5447 XSETMISC (overlay, ov);
5448 eassert (OVERLAYP (overlay));
5449 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5450 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5451
5452 if (end < charpos)
5453 break;
5454
5455 /* Skip this overlay if it doesn't start or end at IT's current
5456 position. */
5457 if (end != charpos && start != charpos)
5458 continue;
5459
5460 /* Skip this overlay if it doesn't apply to IT->w. */
5461 window = Foverlay_get (overlay, Qwindow);
5462 if (WINDOWP (window) && XWINDOW (window) != it->w)
5463 continue;
5464
5465 /* If the text ``under'' the overlay is invisible, both before-
5466 and after-strings from this overlay are visible; start and
5467 end position are indistinguishable. */
5468 invisible = Foverlay_get (overlay, Qinvisible);
5469 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5470
5471 /* If overlay has a non-empty before-string, record it. */
5472 if ((start == charpos || (end == charpos && invis_p))
5473 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5474 && SCHARS (str))
5475 RECORD_OVERLAY_STRING (overlay, str, 0);
5476
5477 /* If overlay has a non-empty after-string, record it. */
5478 if ((end == charpos || (start == charpos && invis_p))
5479 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5480 && SCHARS (str))
5481 RECORD_OVERLAY_STRING (overlay, str, 1);
5482 }
5483
5484 /* Process overlays after the overlay center. */
5485 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5486 {
5487 XSETMISC (overlay, ov);
5488 eassert (OVERLAYP (overlay));
5489 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5490 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5491
5492 if (start > charpos)
5493 break;
5494
5495 /* Skip this overlay if it doesn't start or end at IT's current
5496 position. */
5497 if (end != charpos && start != charpos)
5498 continue;
5499
5500 /* Skip this overlay if it doesn't apply to IT->w. */
5501 window = Foverlay_get (overlay, Qwindow);
5502 if (WINDOWP (window) && XWINDOW (window) != it->w)
5503 continue;
5504
5505 /* If the text ``under'' the overlay is invisible, it has a zero
5506 dimension, and both before- and after-strings apply. */
5507 invisible = Foverlay_get (overlay, Qinvisible);
5508 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5509
5510 /* If overlay has a non-empty before-string, record it. */
5511 if ((start == charpos || (end == charpos && invis_p))
5512 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5513 && SCHARS (str))
5514 RECORD_OVERLAY_STRING (overlay, str, 0);
5515
5516 /* If overlay has a non-empty after-string, record it. */
5517 if ((end == charpos || (start == charpos && invis_p))
5518 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5519 && SCHARS (str))
5520 RECORD_OVERLAY_STRING (overlay, str, 1);
5521 }
5522
5523 #undef RECORD_OVERLAY_STRING
5524
5525 /* Sort entries. */
5526 if (n > 1)
5527 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5528
5529 /* Record number of overlay strings, and where we computed it. */
5530 it->n_overlay_strings = n;
5531 it->overlay_strings_charpos = charpos;
5532
5533 /* IT->current.overlay_string_index is the number of overlay strings
5534 that have already been consumed by IT. Copy some of the
5535 remaining overlay strings to IT->overlay_strings. */
5536 i = 0;
5537 j = it->current.overlay_string_index;
5538 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5539 {
5540 it->overlay_strings[i] = entries[j].string;
5541 it->string_overlays[i++] = entries[j++].overlay;
5542 }
5543
5544 CHECK_IT (it);
5545 SAFE_FREE ();
5546 }
5547
5548
5549 /* Get the first chunk of overlay strings at IT's current buffer
5550 position, or at CHARPOS if that is > 0. Value is non-zero if at
5551 least one overlay string was found. */
5552
5553 static int
5554 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5555 {
5556 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5557 process. This fills IT->overlay_strings with strings, and sets
5558 IT->n_overlay_strings to the total number of strings to process.
5559 IT->pos.overlay_string_index has to be set temporarily to zero
5560 because load_overlay_strings needs this; it must be set to -1
5561 when no overlay strings are found because a zero value would
5562 indicate a position in the first overlay string. */
5563 it->current.overlay_string_index = 0;
5564 load_overlay_strings (it, charpos);
5565
5566 /* If we found overlay strings, set up IT to deliver display
5567 elements from the first one. Otherwise set up IT to deliver
5568 from current_buffer. */
5569 if (it->n_overlay_strings)
5570 {
5571 /* Make sure we know settings in current_buffer, so that we can
5572 restore meaningful values when we're done with the overlay
5573 strings. */
5574 if (compute_stop_p)
5575 compute_stop_pos (it);
5576 eassert (it->face_id >= 0);
5577
5578 /* Save IT's settings. They are restored after all overlay
5579 strings have been processed. */
5580 eassert (!compute_stop_p || it->sp == 0);
5581
5582 /* When called from handle_stop, there might be an empty display
5583 string loaded. In that case, don't bother saving it. But
5584 don't use this optimization with the bidi iterator, since we
5585 need the corresponding pop_it call to resync the bidi
5586 iterator's position with IT's position, after we are done
5587 with the overlay strings. (The corresponding call to pop_it
5588 in case of an empty display string is in
5589 next_overlay_string.) */
5590 if (!(!it->bidi_p
5591 && STRINGP (it->string) && !SCHARS (it->string)))
5592 push_it (it, NULL);
5593
5594 /* Set up IT to deliver display elements from the first overlay
5595 string. */
5596 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5597 it->string = it->overlay_strings[0];
5598 it->from_overlay = Qnil;
5599 it->stop_charpos = 0;
5600 eassert (STRINGP (it->string));
5601 it->end_charpos = SCHARS (it->string);
5602 it->prev_stop = 0;
5603 it->base_level_stop = 0;
5604 it->multibyte_p = STRING_MULTIBYTE (it->string);
5605 it->method = GET_FROM_STRING;
5606 it->from_disp_prop_p = 0;
5607
5608 /* Force paragraph direction to be that of the parent
5609 buffer. */
5610 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5611 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5612 else
5613 it->paragraph_embedding = L2R;
5614
5615 /* Set up the bidi iterator for this overlay string. */
5616 if (it->bidi_p)
5617 {
5618 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5619
5620 it->bidi_it.string.lstring = it->string;
5621 it->bidi_it.string.s = NULL;
5622 it->bidi_it.string.schars = SCHARS (it->string);
5623 it->bidi_it.string.bufpos = pos;
5624 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5625 it->bidi_it.string.unibyte = !it->multibyte_p;
5626 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5627 }
5628 return 1;
5629 }
5630
5631 it->current.overlay_string_index = -1;
5632 return 0;
5633 }
5634
5635 static int
5636 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5637 {
5638 it->string = Qnil;
5639 it->method = GET_FROM_BUFFER;
5640
5641 (void) get_overlay_strings_1 (it, charpos, 1);
5642
5643 CHECK_IT (it);
5644
5645 /* Value is non-zero if we found at least one overlay string. */
5646 return STRINGP (it->string);
5647 }
5648
5649
5650 \f
5651 /***********************************************************************
5652 Saving and restoring state
5653 ***********************************************************************/
5654
5655 /* Save current settings of IT on IT->stack. Called, for example,
5656 before setting up IT for an overlay string, to be able to restore
5657 IT's settings to what they were after the overlay string has been
5658 processed. If POSITION is non-NULL, it is the position to save on
5659 the stack instead of IT->position. */
5660
5661 static void
5662 push_it (struct it *it, struct text_pos *position)
5663 {
5664 struct iterator_stack_entry *p;
5665
5666 eassert (it->sp < IT_STACK_SIZE);
5667 p = it->stack + it->sp;
5668
5669 p->stop_charpos = it->stop_charpos;
5670 p->prev_stop = it->prev_stop;
5671 p->base_level_stop = it->base_level_stop;
5672 p->cmp_it = it->cmp_it;
5673 eassert (it->face_id >= 0);
5674 p->face_id = it->face_id;
5675 p->string = it->string;
5676 p->method = it->method;
5677 p->from_overlay = it->from_overlay;
5678 switch (p->method)
5679 {
5680 case GET_FROM_IMAGE:
5681 p->u.image.object = it->object;
5682 p->u.image.image_id = it->image_id;
5683 p->u.image.slice = it->slice;
5684 break;
5685 case GET_FROM_STRETCH:
5686 p->u.stretch.object = it->object;
5687 break;
5688 }
5689 p->position = position ? *position : it->position;
5690 p->current = it->current;
5691 p->end_charpos = it->end_charpos;
5692 p->string_nchars = it->string_nchars;
5693 p->area = it->area;
5694 p->multibyte_p = it->multibyte_p;
5695 p->avoid_cursor_p = it->avoid_cursor_p;
5696 p->space_width = it->space_width;
5697 p->font_height = it->font_height;
5698 p->voffset = it->voffset;
5699 p->string_from_display_prop_p = it->string_from_display_prop_p;
5700 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5701 p->display_ellipsis_p = 0;
5702 p->line_wrap = it->line_wrap;
5703 p->bidi_p = it->bidi_p;
5704 p->paragraph_embedding = it->paragraph_embedding;
5705 p->from_disp_prop_p = it->from_disp_prop_p;
5706 ++it->sp;
5707
5708 /* Save the state of the bidi iterator as well. */
5709 if (it->bidi_p)
5710 bidi_push_it (&it->bidi_it);
5711 }
5712
5713 static void
5714 iterate_out_of_display_property (struct it *it)
5715 {
5716 int buffer_p = !STRINGP (it->string);
5717 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5718 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5719
5720 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5721
5722 /* Maybe initialize paragraph direction. If we are at the beginning
5723 of a new paragraph, next_element_from_buffer may not have a
5724 chance to do that. */
5725 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5726 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5727 /* prev_stop can be zero, so check against BEGV as well. */
5728 while (it->bidi_it.charpos >= bob
5729 && it->prev_stop <= it->bidi_it.charpos
5730 && it->bidi_it.charpos < CHARPOS (it->position)
5731 && it->bidi_it.charpos < eob)
5732 bidi_move_to_visually_next (&it->bidi_it);
5733 /* Record the stop_pos we just crossed, for when we cross it
5734 back, maybe. */
5735 if (it->bidi_it.charpos > CHARPOS (it->position))
5736 it->prev_stop = CHARPOS (it->position);
5737 /* If we ended up not where pop_it put us, resync IT's
5738 positional members with the bidi iterator. */
5739 if (it->bidi_it.charpos != CHARPOS (it->position))
5740 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5741 if (buffer_p)
5742 it->current.pos = it->position;
5743 else
5744 it->current.string_pos = it->position;
5745 }
5746
5747 /* Restore IT's settings from IT->stack. Called, for example, when no
5748 more overlay strings must be processed, and we return to delivering
5749 display elements from a buffer, or when the end of a string from a
5750 `display' property is reached and we return to delivering display
5751 elements from an overlay string, or from a buffer. */
5752
5753 static void
5754 pop_it (struct it *it)
5755 {
5756 struct iterator_stack_entry *p;
5757 int from_display_prop = it->from_disp_prop_p;
5758
5759 eassert (it->sp > 0);
5760 --it->sp;
5761 p = it->stack + it->sp;
5762 it->stop_charpos = p->stop_charpos;
5763 it->prev_stop = p->prev_stop;
5764 it->base_level_stop = p->base_level_stop;
5765 it->cmp_it = p->cmp_it;
5766 it->face_id = p->face_id;
5767 it->current = p->current;
5768 it->position = p->position;
5769 it->string = p->string;
5770 it->from_overlay = p->from_overlay;
5771 if (NILP (it->string))
5772 SET_TEXT_POS (it->current.string_pos, -1, -1);
5773 it->method = p->method;
5774 switch (it->method)
5775 {
5776 case GET_FROM_IMAGE:
5777 it->image_id = p->u.image.image_id;
5778 it->object = p->u.image.object;
5779 it->slice = p->u.image.slice;
5780 break;
5781 case GET_FROM_STRETCH:
5782 it->object = p->u.stretch.object;
5783 break;
5784 case GET_FROM_BUFFER:
5785 it->object = it->w->buffer;
5786 break;
5787 case GET_FROM_STRING:
5788 it->object = it->string;
5789 break;
5790 case GET_FROM_DISPLAY_VECTOR:
5791 if (it->s)
5792 it->method = GET_FROM_C_STRING;
5793 else if (STRINGP (it->string))
5794 it->method = GET_FROM_STRING;
5795 else
5796 {
5797 it->method = GET_FROM_BUFFER;
5798 it->object = it->w->buffer;
5799 }
5800 }
5801 it->end_charpos = p->end_charpos;
5802 it->string_nchars = p->string_nchars;
5803 it->area = p->area;
5804 it->multibyte_p = p->multibyte_p;
5805 it->avoid_cursor_p = p->avoid_cursor_p;
5806 it->space_width = p->space_width;
5807 it->font_height = p->font_height;
5808 it->voffset = p->voffset;
5809 it->string_from_display_prop_p = p->string_from_display_prop_p;
5810 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5811 it->line_wrap = p->line_wrap;
5812 it->bidi_p = p->bidi_p;
5813 it->paragraph_embedding = p->paragraph_embedding;
5814 it->from_disp_prop_p = p->from_disp_prop_p;
5815 if (it->bidi_p)
5816 {
5817 bidi_pop_it (&it->bidi_it);
5818 /* Bidi-iterate until we get out of the portion of text, if any,
5819 covered by a `display' text property or by an overlay with
5820 `display' property. (We cannot just jump there, because the
5821 internal coherency of the bidi iterator state can not be
5822 preserved across such jumps.) We also must determine the
5823 paragraph base direction if the overlay we just processed is
5824 at the beginning of a new paragraph. */
5825 if (from_display_prop
5826 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5827 iterate_out_of_display_property (it);
5828
5829 eassert ((BUFFERP (it->object)
5830 && IT_CHARPOS (*it) == it->bidi_it.charpos
5831 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5832 || (STRINGP (it->object)
5833 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5834 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5835 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5836 }
5837 }
5838
5839
5840 \f
5841 /***********************************************************************
5842 Moving over lines
5843 ***********************************************************************/
5844
5845 /* Set IT's current position to the previous line start. */
5846
5847 static void
5848 back_to_previous_line_start (struct it *it)
5849 {
5850 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5851 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5852 }
5853
5854
5855 /* Move IT to the next line start.
5856
5857 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5858 we skipped over part of the text (as opposed to moving the iterator
5859 continuously over the text). Otherwise, don't change the value
5860 of *SKIPPED_P.
5861
5862 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5863 iterator on the newline, if it was found.
5864
5865 Newlines may come from buffer text, overlay strings, or strings
5866 displayed via the `display' property. That's the reason we can't
5867 simply use find_next_newline_no_quit.
5868
5869 Note that this function may not skip over invisible text that is so
5870 because of text properties and immediately follows a newline. If
5871 it would, function reseat_at_next_visible_line_start, when called
5872 from set_iterator_to_next, would effectively make invisible
5873 characters following a newline part of the wrong glyph row, which
5874 leads to wrong cursor motion. */
5875
5876 static int
5877 forward_to_next_line_start (struct it *it, int *skipped_p,
5878 struct bidi_it *bidi_it_prev)
5879 {
5880 ptrdiff_t old_selective;
5881 int newline_found_p, n;
5882 const int MAX_NEWLINE_DISTANCE = 500;
5883
5884 /* If already on a newline, just consume it to avoid unintended
5885 skipping over invisible text below. */
5886 if (it->what == IT_CHARACTER
5887 && it->c == '\n'
5888 && CHARPOS (it->position) == IT_CHARPOS (*it))
5889 {
5890 if (it->bidi_p && bidi_it_prev)
5891 *bidi_it_prev = it->bidi_it;
5892 set_iterator_to_next (it, 0);
5893 it->c = 0;
5894 return 1;
5895 }
5896
5897 /* Don't handle selective display in the following. It's (a)
5898 unnecessary because it's done by the caller, and (b) leads to an
5899 infinite recursion because next_element_from_ellipsis indirectly
5900 calls this function. */
5901 old_selective = it->selective;
5902 it->selective = 0;
5903
5904 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5905 from buffer text. */
5906 for (n = newline_found_p = 0;
5907 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5908 n += STRINGP (it->string) ? 0 : 1)
5909 {
5910 if (!get_next_display_element (it))
5911 return 0;
5912 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5913 if (newline_found_p && it->bidi_p && bidi_it_prev)
5914 *bidi_it_prev = it->bidi_it;
5915 set_iterator_to_next (it, 0);
5916 }
5917
5918 /* If we didn't find a newline near enough, see if we can use a
5919 short-cut. */
5920 if (!newline_found_p)
5921 {
5922 ptrdiff_t start = IT_CHARPOS (*it);
5923 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5924 Lisp_Object pos;
5925
5926 eassert (!STRINGP (it->string));
5927
5928 /* If there isn't any `display' property in sight, and no
5929 overlays, we can just use the position of the newline in
5930 buffer text. */
5931 if (it->stop_charpos >= limit
5932 || ((pos = Fnext_single_property_change (make_number (start),
5933 Qdisplay, Qnil,
5934 make_number (limit)),
5935 NILP (pos))
5936 && next_overlay_change (start) == ZV))
5937 {
5938 if (!it->bidi_p)
5939 {
5940 IT_CHARPOS (*it) = limit;
5941 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5942 }
5943 else
5944 {
5945 struct bidi_it bprev;
5946
5947 /* Help bidi.c avoid expensive searches for display
5948 properties and overlays, by telling it that there are
5949 none up to `limit'. */
5950 if (it->bidi_it.disp_pos < limit)
5951 {
5952 it->bidi_it.disp_pos = limit;
5953 it->bidi_it.disp_prop = 0;
5954 }
5955 do {
5956 bprev = it->bidi_it;
5957 bidi_move_to_visually_next (&it->bidi_it);
5958 } while (it->bidi_it.charpos != limit);
5959 IT_CHARPOS (*it) = limit;
5960 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5961 if (bidi_it_prev)
5962 *bidi_it_prev = bprev;
5963 }
5964 *skipped_p = newline_found_p = 1;
5965 }
5966 else
5967 {
5968 while (get_next_display_element (it)
5969 && !newline_found_p)
5970 {
5971 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5972 if (newline_found_p && it->bidi_p && bidi_it_prev)
5973 *bidi_it_prev = it->bidi_it;
5974 set_iterator_to_next (it, 0);
5975 }
5976 }
5977 }
5978
5979 it->selective = old_selective;
5980 return newline_found_p;
5981 }
5982
5983
5984 /* Set IT's current position to the previous visible line start. Skip
5985 invisible text that is so either due to text properties or due to
5986 selective display. Caution: this does not change IT->current_x and
5987 IT->hpos. */
5988
5989 static void
5990 back_to_previous_visible_line_start (struct it *it)
5991 {
5992 while (IT_CHARPOS (*it) > BEGV)
5993 {
5994 back_to_previous_line_start (it);
5995
5996 if (IT_CHARPOS (*it) <= BEGV)
5997 break;
5998
5999 /* If selective > 0, then lines indented more than its value are
6000 invisible. */
6001 if (it->selective > 0
6002 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6003 it->selective))
6004 continue;
6005
6006 /* Check the newline before point for invisibility. */
6007 {
6008 Lisp_Object prop;
6009 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6010 Qinvisible, it->window);
6011 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6012 continue;
6013 }
6014
6015 if (IT_CHARPOS (*it) <= BEGV)
6016 break;
6017
6018 {
6019 struct it it2;
6020 void *it2data = NULL;
6021 ptrdiff_t pos;
6022 ptrdiff_t beg, end;
6023 Lisp_Object val, overlay;
6024
6025 SAVE_IT (it2, *it, it2data);
6026
6027 /* If newline is part of a composition, continue from start of composition */
6028 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6029 && beg < IT_CHARPOS (*it))
6030 goto replaced;
6031
6032 /* If newline is replaced by a display property, find start of overlay
6033 or interval and continue search from that point. */
6034 pos = --IT_CHARPOS (it2);
6035 --IT_BYTEPOS (it2);
6036 it2.sp = 0;
6037 bidi_unshelve_cache (NULL, 0);
6038 it2.string_from_display_prop_p = 0;
6039 it2.from_disp_prop_p = 0;
6040 if (handle_display_prop (&it2) == HANDLED_RETURN
6041 && !NILP (val = get_char_property_and_overlay
6042 (make_number (pos), Qdisplay, Qnil, &overlay))
6043 && (OVERLAYP (overlay)
6044 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6045 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6046 {
6047 RESTORE_IT (it, it, it2data);
6048 goto replaced;
6049 }
6050
6051 /* Newline is not replaced by anything -- so we are done. */
6052 RESTORE_IT (it, it, it2data);
6053 break;
6054
6055 replaced:
6056 if (beg < BEGV)
6057 beg = BEGV;
6058 IT_CHARPOS (*it) = beg;
6059 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6060 }
6061 }
6062
6063 it->continuation_lines_width = 0;
6064
6065 eassert (IT_CHARPOS (*it) >= BEGV);
6066 eassert (IT_CHARPOS (*it) == BEGV
6067 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6068 CHECK_IT (it);
6069 }
6070
6071
6072 /* Reseat iterator IT at the previous visible line start. Skip
6073 invisible text that is so either due to text properties or due to
6074 selective display. At the end, update IT's overlay information,
6075 face information etc. */
6076
6077 void
6078 reseat_at_previous_visible_line_start (struct it *it)
6079 {
6080 back_to_previous_visible_line_start (it);
6081 reseat (it, it->current.pos, 1);
6082 CHECK_IT (it);
6083 }
6084
6085
6086 /* Reseat iterator IT on the next visible line start in the current
6087 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6088 preceding the line start. Skip over invisible text that is so
6089 because of selective display. Compute faces, overlays etc at the
6090 new position. Note that this function does not skip over text that
6091 is invisible because of text properties. */
6092
6093 static void
6094 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6095 {
6096 int newline_found_p, skipped_p = 0;
6097 struct bidi_it bidi_it_prev;
6098
6099 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6100
6101 /* Skip over lines that are invisible because they are indented
6102 more than the value of IT->selective. */
6103 if (it->selective > 0)
6104 while (IT_CHARPOS (*it) < ZV
6105 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6106 it->selective))
6107 {
6108 eassert (IT_BYTEPOS (*it) == BEGV
6109 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6110 newline_found_p =
6111 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6112 }
6113
6114 /* Position on the newline if that's what's requested. */
6115 if (on_newline_p && newline_found_p)
6116 {
6117 if (STRINGP (it->string))
6118 {
6119 if (IT_STRING_CHARPOS (*it) > 0)
6120 {
6121 if (!it->bidi_p)
6122 {
6123 --IT_STRING_CHARPOS (*it);
6124 --IT_STRING_BYTEPOS (*it);
6125 }
6126 else
6127 {
6128 /* We need to restore the bidi iterator to the state
6129 it had on the newline, and resync the IT's
6130 position with that. */
6131 it->bidi_it = bidi_it_prev;
6132 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6133 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6134 }
6135 }
6136 }
6137 else if (IT_CHARPOS (*it) > BEGV)
6138 {
6139 if (!it->bidi_p)
6140 {
6141 --IT_CHARPOS (*it);
6142 --IT_BYTEPOS (*it);
6143 }
6144 else
6145 {
6146 /* We need to restore the bidi iterator to the state it
6147 had on the newline and resync IT with that. */
6148 it->bidi_it = bidi_it_prev;
6149 IT_CHARPOS (*it) = it->bidi_it.charpos;
6150 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6151 }
6152 reseat (it, it->current.pos, 0);
6153 }
6154 }
6155 else if (skipped_p)
6156 reseat (it, it->current.pos, 0);
6157
6158 CHECK_IT (it);
6159 }
6160
6161
6162 \f
6163 /***********************************************************************
6164 Changing an iterator's position
6165 ***********************************************************************/
6166
6167 /* Change IT's current position to POS in current_buffer. If FORCE_P
6168 is non-zero, always check for text properties at the new position.
6169 Otherwise, text properties are only looked up if POS >=
6170 IT->check_charpos of a property. */
6171
6172 static void
6173 reseat (struct it *it, struct text_pos pos, int force_p)
6174 {
6175 ptrdiff_t original_pos = IT_CHARPOS (*it);
6176
6177 reseat_1 (it, pos, 0);
6178
6179 /* Determine where to check text properties. Avoid doing it
6180 where possible because text property lookup is very expensive. */
6181 if (force_p
6182 || CHARPOS (pos) > it->stop_charpos
6183 || CHARPOS (pos) < original_pos)
6184 {
6185 if (it->bidi_p)
6186 {
6187 /* For bidi iteration, we need to prime prev_stop and
6188 base_level_stop with our best estimations. */
6189 /* Implementation note: Of course, POS is not necessarily a
6190 stop position, so assigning prev_pos to it is a lie; we
6191 should have called compute_stop_backwards. However, if
6192 the current buffer does not include any R2L characters,
6193 that call would be a waste of cycles, because the
6194 iterator will never move back, and thus never cross this
6195 "fake" stop position. So we delay that backward search
6196 until the time we really need it, in next_element_from_buffer. */
6197 if (CHARPOS (pos) != it->prev_stop)
6198 it->prev_stop = CHARPOS (pos);
6199 if (CHARPOS (pos) < it->base_level_stop)
6200 it->base_level_stop = 0; /* meaning it's unknown */
6201 handle_stop (it);
6202 }
6203 else
6204 {
6205 handle_stop (it);
6206 it->prev_stop = it->base_level_stop = 0;
6207 }
6208
6209 }
6210
6211 CHECK_IT (it);
6212 }
6213
6214
6215 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6216 IT->stop_pos to POS, also. */
6217
6218 static void
6219 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6220 {
6221 /* Don't call this function when scanning a C string. */
6222 eassert (it->s == NULL);
6223
6224 /* POS must be a reasonable value. */
6225 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6226
6227 it->current.pos = it->position = pos;
6228 it->end_charpos = ZV;
6229 it->dpvec = NULL;
6230 it->current.dpvec_index = -1;
6231 it->current.overlay_string_index = -1;
6232 IT_STRING_CHARPOS (*it) = -1;
6233 IT_STRING_BYTEPOS (*it) = -1;
6234 it->string = Qnil;
6235 it->method = GET_FROM_BUFFER;
6236 it->object = it->w->buffer;
6237 it->area = TEXT_AREA;
6238 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6239 it->sp = 0;
6240 it->string_from_display_prop_p = 0;
6241 it->string_from_prefix_prop_p = 0;
6242
6243 it->from_disp_prop_p = 0;
6244 it->face_before_selective_p = 0;
6245 if (it->bidi_p)
6246 {
6247 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6248 &it->bidi_it);
6249 bidi_unshelve_cache (NULL, 0);
6250 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6251 it->bidi_it.string.s = NULL;
6252 it->bidi_it.string.lstring = Qnil;
6253 it->bidi_it.string.bufpos = 0;
6254 it->bidi_it.string.unibyte = 0;
6255 }
6256
6257 if (set_stop_p)
6258 {
6259 it->stop_charpos = CHARPOS (pos);
6260 it->base_level_stop = CHARPOS (pos);
6261 }
6262 }
6263
6264
6265 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6266 If S is non-null, it is a C string to iterate over. Otherwise,
6267 STRING gives a Lisp string to iterate over.
6268
6269 If PRECISION > 0, don't return more then PRECISION number of
6270 characters from the string.
6271
6272 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6273 characters have been returned. FIELD_WIDTH < 0 means an infinite
6274 field width.
6275
6276 MULTIBYTE = 0 means disable processing of multibyte characters,
6277 MULTIBYTE > 0 means enable it,
6278 MULTIBYTE < 0 means use IT->multibyte_p.
6279
6280 IT must be initialized via a prior call to init_iterator before
6281 calling this function. */
6282
6283 static void
6284 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6285 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6286 int multibyte)
6287 {
6288 /* No region in strings. */
6289 it->region_beg_charpos = it->region_end_charpos = -1;
6290
6291 /* No text property checks performed by default, but see below. */
6292 it->stop_charpos = -1;
6293
6294 /* Set iterator position and end position. */
6295 memset (&it->current, 0, sizeof it->current);
6296 it->current.overlay_string_index = -1;
6297 it->current.dpvec_index = -1;
6298 eassert (charpos >= 0);
6299
6300 /* If STRING is specified, use its multibyteness, otherwise use the
6301 setting of MULTIBYTE, if specified. */
6302 if (multibyte >= 0)
6303 it->multibyte_p = multibyte > 0;
6304
6305 /* Bidirectional reordering of strings is controlled by the default
6306 value of bidi-display-reordering. Don't try to reorder while
6307 loading loadup.el, as the necessary character property tables are
6308 not yet available. */
6309 it->bidi_p =
6310 NILP (Vpurify_flag)
6311 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6312
6313 if (s == NULL)
6314 {
6315 eassert (STRINGP (string));
6316 it->string = string;
6317 it->s = NULL;
6318 it->end_charpos = it->string_nchars = SCHARS (string);
6319 it->method = GET_FROM_STRING;
6320 it->current.string_pos = string_pos (charpos, string);
6321
6322 if (it->bidi_p)
6323 {
6324 it->bidi_it.string.lstring = string;
6325 it->bidi_it.string.s = NULL;
6326 it->bidi_it.string.schars = it->end_charpos;
6327 it->bidi_it.string.bufpos = 0;
6328 it->bidi_it.string.from_disp_str = 0;
6329 it->bidi_it.string.unibyte = !it->multibyte_p;
6330 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6331 FRAME_WINDOW_P (it->f), &it->bidi_it);
6332 }
6333 }
6334 else
6335 {
6336 it->s = (const unsigned char *) s;
6337 it->string = Qnil;
6338
6339 /* Note that we use IT->current.pos, not it->current.string_pos,
6340 for displaying C strings. */
6341 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6342 if (it->multibyte_p)
6343 {
6344 it->current.pos = c_string_pos (charpos, s, 1);
6345 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6346 }
6347 else
6348 {
6349 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6350 it->end_charpos = it->string_nchars = strlen (s);
6351 }
6352
6353 if (it->bidi_p)
6354 {
6355 it->bidi_it.string.lstring = Qnil;
6356 it->bidi_it.string.s = (const unsigned char *) s;
6357 it->bidi_it.string.schars = it->end_charpos;
6358 it->bidi_it.string.bufpos = 0;
6359 it->bidi_it.string.from_disp_str = 0;
6360 it->bidi_it.string.unibyte = !it->multibyte_p;
6361 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6362 &it->bidi_it);
6363 }
6364 it->method = GET_FROM_C_STRING;
6365 }
6366
6367 /* PRECISION > 0 means don't return more than PRECISION characters
6368 from the string. */
6369 if (precision > 0 && it->end_charpos - charpos > precision)
6370 {
6371 it->end_charpos = it->string_nchars = charpos + precision;
6372 if (it->bidi_p)
6373 it->bidi_it.string.schars = it->end_charpos;
6374 }
6375
6376 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6377 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6378 FIELD_WIDTH < 0 means infinite field width. This is useful for
6379 padding with `-' at the end of a mode line. */
6380 if (field_width < 0)
6381 field_width = INFINITY;
6382 /* Implementation note: We deliberately don't enlarge
6383 it->bidi_it.string.schars here to fit it->end_charpos, because
6384 the bidi iterator cannot produce characters out of thin air. */
6385 if (field_width > it->end_charpos - charpos)
6386 it->end_charpos = charpos + field_width;
6387
6388 /* Use the standard display table for displaying strings. */
6389 if (DISP_TABLE_P (Vstandard_display_table))
6390 it->dp = XCHAR_TABLE (Vstandard_display_table);
6391
6392 it->stop_charpos = charpos;
6393 it->prev_stop = charpos;
6394 it->base_level_stop = 0;
6395 if (it->bidi_p)
6396 {
6397 it->bidi_it.first_elt = 1;
6398 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6399 it->bidi_it.disp_pos = -1;
6400 }
6401 if (s == NULL && it->multibyte_p)
6402 {
6403 ptrdiff_t endpos = SCHARS (it->string);
6404 if (endpos > it->end_charpos)
6405 endpos = it->end_charpos;
6406 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6407 it->string);
6408 }
6409 CHECK_IT (it);
6410 }
6411
6412
6413 \f
6414 /***********************************************************************
6415 Iteration
6416 ***********************************************************************/
6417
6418 /* Map enum it_method value to corresponding next_element_from_* function. */
6419
6420 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6421 {
6422 next_element_from_buffer,
6423 next_element_from_display_vector,
6424 next_element_from_string,
6425 next_element_from_c_string,
6426 next_element_from_image,
6427 next_element_from_stretch
6428 };
6429
6430 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6431
6432
6433 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6434 (possibly with the following characters). */
6435
6436 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6437 ((IT)->cmp_it.id >= 0 \
6438 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6439 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6440 END_CHARPOS, (IT)->w, \
6441 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6442 (IT)->string)))
6443
6444
6445 /* Lookup the char-table Vglyphless_char_display for character C (-1
6446 if we want information for no-font case), and return the display
6447 method symbol. By side-effect, update it->what and
6448 it->glyphless_method. This function is called from
6449 get_next_display_element for each character element, and from
6450 x_produce_glyphs when no suitable font was found. */
6451
6452 Lisp_Object
6453 lookup_glyphless_char_display (int c, struct it *it)
6454 {
6455 Lisp_Object glyphless_method = Qnil;
6456
6457 if (CHAR_TABLE_P (Vglyphless_char_display)
6458 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6459 {
6460 if (c >= 0)
6461 {
6462 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6463 if (CONSP (glyphless_method))
6464 glyphless_method = FRAME_WINDOW_P (it->f)
6465 ? XCAR (glyphless_method)
6466 : XCDR (glyphless_method);
6467 }
6468 else
6469 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6470 }
6471
6472 retry:
6473 if (NILP (glyphless_method))
6474 {
6475 if (c >= 0)
6476 /* The default is to display the character by a proper font. */
6477 return Qnil;
6478 /* The default for the no-font case is to display an empty box. */
6479 glyphless_method = Qempty_box;
6480 }
6481 if (EQ (glyphless_method, Qzero_width))
6482 {
6483 if (c >= 0)
6484 return glyphless_method;
6485 /* This method can't be used for the no-font case. */
6486 glyphless_method = Qempty_box;
6487 }
6488 if (EQ (glyphless_method, Qthin_space))
6489 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6490 else if (EQ (glyphless_method, Qempty_box))
6491 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6492 else if (EQ (glyphless_method, Qhex_code))
6493 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6494 else if (STRINGP (glyphless_method))
6495 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6496 else
6497 {
6498 /* Invalid value. We use the default method. */
6499 glyphless_method = Qnil;
6500 goto retry;
6501 }
6502 it->what = IT_GLYPHLESS;
6503 return glyphless_method;
6504 }
6505
6506 /* Load IT's display element fields with information about the next
6507 display element from the current position of IT. Value is zero if
6508 end of buffer (or C string) is reached. */
6509
6510 static struct frame *last_escape_glyph_frame = NULL;
6511 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6512 static int last_escape_glyph_merged_face_id = 0;
6513
6514 struct frame *last_glyphless_glyph_frame = NULL;
6515 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6516 int last_glyphless_glyph_merged_face_id = 0;
6517
6518 static int
6519 get_next_display_element (struct it *it)
6520 {
6521 /* Non-zero means that we found a display element. Zero means that
6522 we hit the end of what we iterate over. Performance note: the
6523 function pointer `method' used here turns out to be faster than
6524 using a sequence of if-statements. */
6525 int success_p;
6526
6527 get_next:
6528 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6529
6530 if (it->what == IT_CHARACTER)
6531 {
6532 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6533 and only if (a) the resolved directionality of that character
6534 is R..." */
6535 /* FIXME: Do we need an exception for characters from display
6536 tables? */
6537 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6538 it->c = bidi_mirror_char (it->c);
6539 /* Map via display table or translate control characters.
6540 IT->c, IT->len etc. have been set to the next character by
6541 the function call above. If we have a display table, and it
6542 contains an entry for IT->c, translate it. Don't do this if
6543 IT->c itself comes from a display table, otherwise we could
6544 end up in an infinite recursion. (An alternative could be to
6545 count the recursion depth of this function and signal an
6546 error when a certain maximum depth is reached.) Is it worth
6547 it? */
6548 if (success_p && it->dpvec == NULL)
6549 {
6550 Lisp_Object dv;
6551 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6552 int nonascii_space_p = 0;
6553 int nonascii_hyphen_p = 0;
6554 int c = it->c; /* This is the character to display. */
6555
6556 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6557 {
6558 eassert (SINGLE_BYTE_CHAR_P (c));
6559 if (unibyte_display_via_language_environment)
6560 {
6561 c = DECODE_CHAR (unibyte, c);
6562 if (c < 0)
6563 c = BYTE8_TO_CHAR (it->c);
6564 }
6565 else
6566 c = BYTE8_TO_CHAR (it->c);
6567 }
6568
6569 if (it->dp
6570 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6571 VECTORP (dv)))
6572 {
6573 struct Lisp_Vector *v = XVECTOR (dv);
6574
6575 /* Return the first character from the display table
6576 entry, if not empty. If empty, don't display the
6577 current character. */
6578 if (v->header.size)
6579 {
6580 it->dpvec_char_len = it->len;
6581 it->dpvec = v->contents;
6582 it->dpend = v->contents + v->header.size;
6583 it->current.dpvec_index = 0;
6584 it->dpvec_face_id = -1;
6585 it->saved_face_id = it->face_id;
6586 it->method = GET_FROM_DISPLAY_VECTOR;
6587 it->ellipsis_p = 0;
6588 }
6589 else
6590 {
6591 set_iterator_to_next (it, 0);
6592 }
6593 goto get_next;
6594 }
6595
6596 if (! NILP (lookup_glyphless_char_display (c, it)))
6597 {
6598 if (it->what == IT_GLYPHLESS)
6599 goto done;
6600 /* Don't display this character. */
6601 set_iterator_to_next (it, 0);
6602 goto get_next;
6603 }
6604
6605 /* If `nobreak-char-display' is non-nil, we display
6606 non-ASCII spaces and hyphens specially. */
6607 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6608 {
6609 if (c == 0xA0)
6610 nonascii_space_p = 1;
6611 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6612 nonascii_hyphen_p = 1;
6613 }
6614
6615 /* Translate control characters into `\003' or `^C' form.
6616 Control characters coming from a display table entry are
6617 currently not translated because we use IT->dpvec to hold
6618 the translation. This could easily be changed but I
6619 don't believe that it is worth doing.
6620
6621 The characters handled by `nobreak-char-display' must be
6622 translated too.
6623
6624 Non-printable characters and raw-byte characters are also
6625 translated to octal form. */
6626 if (((c < ' ' || c == 127) /* ASCII control chars */
6627 ? (it->area != TEXT_AREA
6628 /* In mode line, treat \n, \t like other crl chars. */
6629 || (c != '\t'
6630 && it->glyph_row
6631 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6632 || (c != '\n' && c != '\t'))
6633 : (nonascii_space_p
6634 || nonascii_hyphen_p
6635 || CHAR_BYTE8_P (c)
6636 || ! CHAR_PRINTABLE_P (c))))
6637 {
6638 /* C is a control character, non-ASCII space/hyphen,
6639 raw-byte, or a non-printable character which must be
6640 displayed either as '\003' or as `^C' where the '\\'
6641 and '^' can be defined in the display table. Fill
6642 IT->ctl_chars with glyphs for what we have to
6643 display. Then, set IT->dpvec to these glyphs. */
6644 Lisp_Object gc;
6645 int ctl_len;
6646 int face_id;
6647 int lface_id = 0;
6648 int escape_glyph;
6649
6650 /* Handle control characters with ^. */
6651
6652 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6653 {
6654 int g;
6655
6656 g = '^'; /* default glyph for Control */
6657 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6658 if (it->dp
6659 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6660 {
6661 g = GLYPH_CODE_CHAR (gc);
6662 lface_id = GLYPH_CODE_FACE (gc);
6663 }
6664 if (lface_id)
6665 {
6666 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6667 }
6668 else if (it->f == last_escape_glyph_frame
6669 && it->face_id == last_escape_glyph_face_id)
6670 {
6671 face_id = last_escape_glyph_merged_face_id;
6672 }
6673 else
6674 {
6675 /* Merge the escape-glyph face into the current face. */
6676 face_id = merge_faces (it->f, Qescape_glyph, 0,
6677 it->face_id);
6678 last_escape_glyph_frame = it->f;
6679 last_escape_glyph_face_id = it->face_id;
6680 last_escape_glyph_merged_face_id = face_id;
6681 }
6682
6683 XSETINT (it->ctl_chars[0], g);
6684 XSETINT (it->ctl_chars[1], c ^ 0100);
6685 ctl_len = 2;
6686 goto display_control;
6687 }
6688
6689 /* Handle non-ascii space in the mode where it only gets
6690 highlighting. */
6691
6692 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6693 {
6694 /* Merge `nobreak-space' into the current face. */
6695 face_id = merge_faces (it->f, Qnobreak_space, 0,
6696 it->face_id);
6697 XSETINT (it->ctl_chars[0], ' ');
6698 ctl_len = 1;
6699 goto display_control;
6700 }
6701
6702 /* Handle sequences that start with the "escape glyph". */
6703
6704 /* the default escape glyph is \. */
6705 escape_glyph = '\\';
6706
6707 if (it->dp
6708 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6709 {
6710 escape_glyph = GLYPH_CODE_CHAR (gc);
6711 lface_id = GLYPH_CODE_FACE (gc);
6712 }
6713 if (lface_id)
6714 {
6715 /* The display table specified a face.
6716 Merge it into face_id and also into escape_glyph. */
6717 face_id = merge_faces (it->f, Qt, lface_id,
6718 it->face_id);
6719 }
6720 else if (it->f == last_escape_glyph_frame
6721 && it->face_id == last_escape_glyph_face_id)
6722 {
6723 face_id = last_escape_glyph_merged_face_id;
6724 }
6725 else
6726 {
6727 /* Merge the escape-glyph face into the current face. */
6728 face_id = merge_faces (it->f, Qescape_glyph, 0,
6729 it->face_id);
6730 last_escape_glyph_frame = it->f;
6731 last_escape_glyph_face_id = it->face_id;
6732 last_escape_glyph_merged_face_id = face_id;
6733 }
6734
6735 /* Draw non-ASCII hyphen with just highlighting: */
6736
6737 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6738 {
6739 XSETINT (it->ctl_chars[0], '-');
6740 ctl_len = 1;
6741 goto display_control;
6742 }
6743
6744 /* Draw non-ASCII space/hyphen with escape glyph: */
6745
6746 if (nonascii_space_p || nonascii_hyphen_p)
6747 {
6748 XSETINT (it->ctl_chars[0], escape_glyph);
6749 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6750 ctl_len = 2;
6751 goto display_control;
6752 }
6753
6754 {
6755 char str[10];
6756 int len, i;
6757
6758 if (CHAR_BYTE8_P (c))
6759 /* Display \200 instead of \17777600. */
6760 c = CHAR_TO_BYTE8 (c);
6761 len = sprintf (str, "%03o", c);
6762
6763 XSETINT (it->ctl_chars[0], escape_glyph);
6764 for (i = 0; i < len; i++)
6765 XSETINT (it->ctl_chars[i + 1], str[i]);
6766 ctl_len = len + 1;
6767 }
6768
6769 display_control:
6770 /* Set up IT->dpvec and return first character from it. */
6771 it->dpvec_char_len = it->len;
6772 it->dpvec = it->ctl_chars;
6773 it->dpend = it->dpvec + ctl_len;
6774 it->current.dpvec_index = 0;
6775 it->dpvec_face_id = face_id;
6776 it->saved_face_id = it->face_id;
6777 it->method = GET_FROM_DISPLAY_VECTOR;
6778 it->ellipsis_p = 0;
6779 goto get_next;
6780 }
6781 it->char_to_display = c;
6782 }
6783 else if (success_p)
6784 {
6785 it->char_to_display = it->c;
6786 }
6787 }
6788
6789 /* Adjust face id for a multibyte character. There are no multibyte
6790 character in unibyte text. */
6791 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6792 && it->multibyte_p
6793 && success_p
6794 && FRAME_WINDOW_P (it->f))
6795 {
6796 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6797
6798 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6799 {
6800 /* Automatic composition with glyph-string. */
6801 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6802
6803 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6804 }
6805 else
6806 {
6807 ptrdiff_t pos = (it->s ? -1
6808 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6809 : IT_CHARPOS (*it));
6810 int c;
6811
6812 if (it->what == IT_CHARACTER)
6813 c = it->char_to_display;
6814 else
6815 {
6816 struct composition *cmp = composition_table[it->cmp_it.id];
6817 int i;
6818
6819 c = ' ';
6820 for (i = 0; i < cmp->glyph_len; i++)
6821 /* TAB in a composition means display glyphs with
6822 padding space on the left or right. */
6823 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6824 break;
6825 }
6826 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6827 }
6828 }
6829
6830 done:
6831 /* Is this character the last one of a run of characters with
6832 box? If yes, set IT->end_of_box_run_p to 1. */
6833 if (it->face_box_p
6834 && it->s == NULL)
6835 {
6836 if (it->method == GET_FROM_STRING && it->sp)
6837 {
6838 int face_id = underlying_face_id (it);
6839 struct face *face = FACE_FROM_ID (it->f, face_id);
6840
6841 if (face)
6842 {
6843 if (face->box == FACE_NO_BOX)
6844 {
6845 /* If the box comes from face properties in a
6846 display string, check faces in that string. */
6847 int string_face_id = face_after_it_pos (it);
6848 it->end_of_box_run_p
6849 = (FACE_FROM_ID (it->f, string_face_id)->box
6850 == FACE_NO_BOX);
6851 }
6852 /* Otherwise, the box comes from the underlying face.
6853 If this is the last string character displayed, check
6854 the next buffer location. */
6855 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6856 && (it->current.overlay_string_index
6857 == it->n_overlay_strings - 1))
6858 {
6859 ptrdiff_t ignore;
6860 int next_face_id;
6861 struct text_pos pos = it->current.pos;
6862 INC_TEXT_POS (pos, it->multibyte_p);
6863
6864 next_face_id = face_at_buffer_position
6865 (it->w, CHARPOS (pos), it->region_beg_charpos,
6866 it->region_end_charpos, &ignore,
6867 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6868 -1);
6869 it->end_of_box_run_p
6870 = (FACE_FROM_ID (it->f, next_face_id)->box
6871 == FACE_NO_BOX);
6872 }
6873 }
6874 }
6875 else
6876 {
6877 int face_id = face_after_it_pos (it);
6878 it->end_of_box_run_p
6879 = (face_id != it->face_id
6880 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6881 }
6882 }
6883 /* If we reached the end of the object we've been iterating (e.g., a
6884 display string or an overlay string), and there's something on
6885 IT->stack, proceed with what's on the stack. It doesn't make
6886 sense to return zero if there's unprocessed stuff on the stack,
6887 because otherwise that stuff will never be displayed. */
6888 if (!success_p && it->sp > 0)
6889 {
6890 set_iterator_to_next (it, 0);
6891 success_p = get_next_display_element (it);
6892 }
6893
6894 /* Value is 0 if end of buffer or string reached. */
6895 return success_p;
6896 }
6897
6898
6899 /* Move IT to the next display element.
6900
6901 RESEAT_P non-zero means if called on a newline in buffer text,
6902 skip to the next visible line start.
6903
6904 Functions get_next_display_element and set_iterator_to_next are
6905 separate because I find this arrangement easier to handle than a
6906 get_next_display_element function that also increments IT's
6907 position. The way it is we can first look at an iterator's current
6908 display element, decide whether it fits on a line, and if it does,
6909 increment the iterator position. The other way around we probably
6910 would either need a flag indicating whether the iterator has to be
6911 incremented the next time, or we would have to implement a
6912 decrement position function which would not be easy to write. */
6913
6914 void
6915 set_iterator_to_next (struct it *it, int reseat_p)
6916 {
6917 /* Reset flags indicating start and end of a sequence of characters
6918 with box. Reset them at the start of this function because
6919 moving the iterator to a new position might set them. */
6920 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6921
6922 switch (it->method)
6923 {
6924 case GET_FROM_BUFFER:
6925 /* The current display element of IT is a character from
6926 current_buffer. Advance in the buffer, and maybe skip over
6927 invisible lines that are so because of selective display. */
6928 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6929 reseat_at_next_visible_line_start (it, 0);
6930 else if (it->cmp_it.id >= 0)
6931 {
6932 /* We are currently getting glyphs from a composition. */
6933 int i;
6934
6935 if (! it->bidi_p)
6936 {
6937 IT_CHARPOS (*it) += it->cmp_it.nchars;
6938 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6939 if (it->cmp_it.to < it->cmp_it.nglyphs)
6940 {
6941 it->cmp_it.from = it->cmp_it.to;
6942 }
6943 else
6944 {
6945 it->cmp_it.id = -1;
6946 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6947 IT_BYTEPOS (*it),
6948 it->end_charpos, Qnil);
6949 }
6950 }
6951 else if (! it->cmp_it.reversed_p)
6952 {
6953 /* Composition created while scanning forward. */
6954 /* Update IT's char/byte positions to point to the first
6955 character of the next grapheme cluster, or to the
6956 character visually after the current composition. */
6957 for (i = 0; i < it->cmp_it.nchars; i++)
6958 bidi_move_to_visually_next (&it->bidi_it);
6959 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6960 IT_CHARPOS (*it) = it->bidi_it.charpos;
6961
6962 if (it->cmp_it.to < it->cmp_it.nglyphs)
6963 {
6964 /* Proceed to the next grapheme cluster. */
6965 it->cmp_it.from = it->cmp_it.to;
6966 }
6967 else
6968 {
6969 /* No more grapheme clusters in this composition.
6970 Find the next stop position. */
6971 ptrdiff_t stop = it->end_charpos;
6972 if (it->bidi_it.scan_dir < 0)
6973 /* Now we are scanning backward and don't know
6974 where to stop. */
6975 stop = -1;
6976 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6977 IT_BYTEPOS (*it), stop, Qnil);
6978 }
6979 }
6980 else
6981 {
6982 /* Composition created while scanning backward. */
6983 /* Update IT's char/byte positions to point to the last
6984 character of the previous grapheme cluster, or the
6985 character visually after the current composition. */
6986 for (i = 0; i < it->cmp_it.nchars; i++)
6987 bidi_move_to_visually_next (&it->bidi_it);
6988 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6989 IT_CHARPOS (*it) = it->bidi_it.charpos;
6990 if (it->cmp_it.from > 0)
6991 {
6992 /* Proceed to the previous grapheme cluster. */
6993 it->cmp_it.to = it->cmp_it.from;
6994 }
6995 else
6996 {
6997 /* No more grapheme clusters in this composition.
6998 Find the next stop position. */
6999 ptrdiff_t stop = it->end_charpos;
7000 if (it->bidi_it.scan_dir < 0)
7001 /* Now we are scanning backward and don't know
7002 where to stop. */
7003 stop = -1;
7004 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7005 IT_BYTEPOS (*it), stop, Qnil);
7006 }
7007 }
7008 }
7009 else
7010 {
7011 eassert (it->len != 0);
7012
7013 if (!it->bidi_p)
7014 {
7015 IT_BYTEPOS (*it) += it->len;
7016 IT_CHARPOS (*it) += 1;
7017 }
7018 else
7019 {
7020 int prev_scan_dir = it->bidi_it.scan_dir;
7021 /* If this is a new paragraph, determine its base
7022 direction (a.k.a. its base embedding level). */
7023 if (it->bidi_it.new_paragraph)
7024 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7025 bidi_move_to_visually_next (&it->bidi_it);
7026 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7027 IT_CHARPOS (*it) = it->bidi_it.charpos;
7028 if (prev_scan_dir != it->bidi_it.scan_dir)
7029 {
7030 /* As the scan direction was changed, we must
7031 re-compute the stop position for composition. */
7032 ptrdiff_t stop = it->end_charpos;
7033 if (it->bidi_it.scan_dir < 0)
7034 stop = -1;
7035 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7036 IT_BYTEPOS (*it), stop, Qnil);
7037 }
7038 }
7039 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7040 }
7041 break;
7042
7043 case GET_FROM_C_STRING:
7044 /* Current display element of IT is from a C string. */
7045 if (!it->bidi_p
7046 /* If the string position is beyond string's end, it means
7047 next_element_from_c_string is padding the string with
7048 blanks, in which case we bypass the bidi iterator,
7049 because it cannot deal with such virtual characters. */
7050 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7051 {
7052 IT_BYTEPOS (*it) += it->len;
7053 IT_CHARPOS (*it) += 1;
7054 }
7055 else
7056 {
7057 bidi_move_to_visually_next (&it->bidi_it);
7058 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7059 IT_CHARPOS (*it) = it->bidi_it.charpos;
7060 }
7061 break;
7062
7063 case GET_FROM_DISPLAY_VECTOR:
7064 /* Current display element of IT is from a display table entry.
7065 Advance in the display table definition. Reset it to null if
7066 end reached, and continue with characters from buffers/
7067 strings. */
7068 ++it->current.dpvec_index;
7069
7070 /* Restore face of the iterator to what they were before the
7071 display vector entry (these entries may contain faces). */
7072 it->face_id = it->saved_face_id;
7073
7074 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7075 {
7076 int recheck_faces = it->ellipsis_p;
7077
7078 if (it->s)
7079 it->method = GET_FROM_C_STRING;
7080 else if (STRINGP (it->string))
7081 it->method = GET_FROM_STRING;
7082 else
7083 {
7084 it->method = GET_FROM_BUFFER;
7085 it->object = it->w->buffer;
7086 }
7087
7088 it->dpvec = NULL;
7089 it->current.dpvec_index = -1;
7090
7091 /* Skip over characters which were displayed via IT->dpvec. */
7092 if (it->dpvec_char_len < 0)
7093 reseat_at_next_visible_line_start (it, 1);
7094 else if (it->dpvec_char_len > 0)
7095 {
7096 if (it->method == GET_FROM_STRING
7097 && it->n_overlay_strings > 0)
7098 it->ignore_overlay_strings_at_pos_p = 1;
7099 it->len = it->dpvec_char_len;
7100 set_iterator_to_next (it, reseat_p);
7101 }
7102
7103 /* Maybe recheck faces after display vector */
7104 if (recheck_faces)
7105 it->stop_charpos = IT_CHARPOS (*it);
7106 }
7107 break;
7108
7109 case GET_FROM_STRING:
7110 /* Current display element is a character from a Lisp string. */
7111 eassert (it->s == NULL && STRINGP (it->string));
7112 /* Don't advance past string end. These conditions are true
7113 when set_iterator_to_next is called at the end of
7114 get_next_display_element, in which case the Lisp string is
7115 already exhausted, and all we want is pop the iterator
7116 stack. */
7117 if (it->current.overlay_string_index >= 0)
7118 {
7119 /* This is an overlay string, so there's no padding with
7120 spaces, and the number of characters in the string is
7121 where the string ends. */
7122 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7123 goto consider_string_end;
7124 }
7125 else
7126 {
7127 /* Not an overlay string. There could be padding, so test
7128 against it->end_charpos . */
7129 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7130 goto consider_string_end;
7131 }
7132 if (it->cmp_it.id >= 0)
7133 {
7134 int i;
7135
7136 if (! it->bidi_p)
7137 {
7138 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7139 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7140 if (it->cmp_it.to < it->cmp_it.nglyphs)
7141 it->cmp_it.from = it->cmp_it.to;
7142 else
7143 {
7144 it->cmp_it.id = -1;
7145 composition_compute_stop_pos (&it->cmp_it,
7146 IT_STRING_CHARPOS (*it),
7147 IT_STRING_BYTEPOS (*it),
7148 it->end_charpos, it->string);
7149 }
7150 }
7151 else if (! it->cmp_it.reversed_p)
7152 {
7153 for (i = 0; i < it->cmp_it.nchars; i++)
7154 bidi_move_to_visually_next (&it->bidi_it);
7155 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7156 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7157
7158 if (it->cmp_it.to < it->cmp_it.nglyphs)
7159 it->cmp_it.from = it->cmp_it.to;
7160 else
7161 {
7162 ptrdiff_t stop = it->end_charpos;
7163 if (it->bidi_it.scan_dir < 0)
7164 stop = -1;
7165 composition_compute_stop_pos (&it->cmp_it,
7166 IT_STRING_CHARPOS (*it),
7167 IT_STRING_BYTEPOS (*it), stop,
7168 it->string);
7169 }
7170 }
7171 else
7172 {
7173 for (i = 0; i < it->cmp_it.nchars; i++)
7174 bidi_move_to_visually_next (&it->bidi_it);
7175 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7176 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7177 if (it->cmp_it.from > 0)
7178 it->cmp_it.to = it->cmp_it.from;
7179 else
7180 {
7181 ptrdiff_t stop = it->end_charpos;
7182 if (it->bidi_it.scan_dir < 0)
7183 stop = -1;
7184 composition_compute_stop_pos (&it->cmp_it,
7185 IT_STRING_CHARPOS (*it),
7186 IT_STRING_BYTEPOS (*it), stop,
7187 it->string);
7188 }
7189 }
7190 }
7191 else
7192 {
7193 if (!it->bidi_p
7194 /* If the string position is beyond string's end, it
7195 means next_element_from_string is padding the string
7196 with blanks, in which case we bypass the bidi
7197 iterator, because it cannot deal with such virtual
7198 characters. */
7199 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7200 {
7201 IT_STRING_BYTEPOS (*it) += it->len;
7202 IT_STRING_CHARPOS (*it) += 1;
7203 }
7204 else
7205 {
7206 int prev_scan_dir = it->bidi_it.scan_dir;
7207
7208 bidi_move_to_visually_next (&it->bidi_it);
7209 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7210 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7211 if (prev_scan_dir != it->bidi_it.scan_dir)
7212 {
7213 ptrdiff_t stop = it->end_charpos;
7214
7215 if (it->bidi_it.scan_dir < 0)
7216 stop = -1;
7217 composition_compute_stop_pos (&it->cmp_it,
7218 IT_STRING_CHARPOS (*it),
7219 IT_STRING_BYTEPOS (*it), stop,
7220 it->string);
7221 }
7222 }
7223 }
7224
7225 consider_string_end:
7226
7227 if (it->current.overlay_string_index >= 0)
7228 {
7229 /* IT->string is an overlay string. Advance to the
7230 next, if there is one. */
7231 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7232 {
7233 it->ellipsis_p = 0;
7234 next_overlay_string (it);
7235 if (it->ellipsis_p)
7236 setup_for_ellipsis (it, 0);
7237 }
7238 }
7239 else
7240 {
7241 /* IT->string is not an overlay string. If we reached
7242 its end, and there is something on IT->stack, proceed
7243 with what is on the stack. This can be either another
7244 string, this time an overlay string, or a buffer. */
7245 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7246 && it->sp > 0)
7247 {
7248 pop_it (it);
7249 if (it->method == GET_FROM_STRING)
7250 goto consider_string_end;
7251 }
7252 }
7253 break;
7254
7255 case GET_FROM_IMAGE:
7256 case GET_FROM_STRETCH:
7257 /* The position etc with which we have to proceed are on
7258 the stack. The position may be at the end of a string,
7259 if the `display' property takes up the whole string. */
7260 eassert (it->sp > 0);
7261 pop_it (it);
7262 if (it->method == GET_FROM_STRING)
7263 goto consider_string_end;
7264 break;
7265
7266 default:
7267 /* There are no other methods defined, so this should be a bug. */
7268 abort ();
7269 }
7270
7271 eassert (it->method != GET_FROM_STRING
7272 || (STRINGP (it->string)
7273 && IT_STRING_CHARPOS (*it) >= 0));
7274 }
7275
7276 /* Load IT's display element fields with information about the next
7277 display element which comes from a display table entry or from the
7278 result of translating a control character to one of the forms `^C'
7279 or `\003'.
7280
7281 IT->dpvec holds the glyphs to return as characters.
7282 IT->saved_face_id holds the face id before the display vector--it
7283 is restored into IT->face_id in set_iterator_to_next. */
7284
7285 static int
7286 next_element_from_display_vector (struct it *it)
7287 {
7288 Lisp_Object gc;
7289
7290 /* Precondition. */
7291 eassert (it->dpvec && it->current.dpvec_index >= 0);
7292
7293 it->face_id = it->saved_face_id;
7294
7295 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7296 That seemed totally bogus - so I changed it... */
7297 gc = it->dpvec[it->current.dpvec_index];
7298
7299 if (GLYPH_CODE_P (gc))
7300 {
7301 it->c = GLYPH_CODE_CHAR (gc);
7302 it->len = CHAR_BYTES (it->c);
7303
7304 /* The entry may contain a face id to use. Such a face id is
7305 the id of a Lisp face, not a realized face. A face id of
7306 zero means no face is specified. */
7307 if (it->dpvec_face_id >= 0)
7308 it->face_id = it->dpvec_face_id;
7309 else
7310 {
7311 int lface_id = GLYPH_CODE_FACE (gc);
7312 if (lface_id > 0)
7313 it->face_id = merge_faces (it->f, Qt, lface_id,
7314 it->saved_face_id);
7315 }
7316 }
7317 else
7318 /* Display table entry is invalid. Return a space. */
7319 it->c = ' ', it->len = 1;
7320
7321 /* Don't change position and object of the iterator here. They are
7322 still the values of the character that had this display table
7323 entry or was translated, and that's what we want. */
7324 it->what = IT_CHARACTER;
7325 return 1;
7326 }
7327
7328 /* Get the first element of string/buffer in the visual order, after
7329 being reseated to a new position in a string or a buffer. */
7330 static void
7331 get_visually_first_element (struct it *it)
7332 {
7333 int string_p = STRINGP (it->string) || it->s;
7334 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7335 ptrdiff_t bob = (string_p ? 0 : BEGV);
7336
7337 if (STRINGP (it->string))
7338 {
7339 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7340 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7341 }
7342 else
7343 {
7344 it->bidi_it.charpos = IT_CHARPOS (*it);
7345 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7346 }
7347
7348 if (it->bidi_it.charpos == eob)
7349 {
7350 /* Nothing to do, but reset the FIRST_ELT flag, like
7351 bidi_paragraph_init does, because we are not going to
7352 call it. */
7353 it->bidi_it.first_elt = 0;
7354 }
7355 else if (it->bidi_it.charpos == bob
7356 || (!string_p
7357 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7358 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7359 {
7360 /* If we are at the beginning of a line/string, we can produce
7361 the next element right away. */
7362 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7363 bidi_move_to_visually_next (&it->bidi_it);
7364 }
7365 else
7366 {
7367 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7368
7369 /* We need to prime the bidi iterator starting at the line's or
7370 string's beginning, before we will be able to produce the
7371 next element. */
7372 if (string_p)
7373 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7374 else
7375 {
7376 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7377 -1);
7378 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7379 }
7380 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7381 do
7382 {
7383 /* Now return to buffer/string position where we were asked
7384 to get the next display element, and produce that. */
7385 bidi_move_to_visually_next (&it->bidi_it);
7386 }
7387 while (it->bidi_it.bytepos != orig_bytepos
7388 && it->bidi_it.charpos < eob);
7389 }
7390
7391 /* Adjust IT's position information to where we ended up. */
7392 if (STRINGP (it->string))
7393 {
7394 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7395 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7396 }
7397 else
7398 {
7399 IT_CHARPOS (*it) = it->bidi_it.charpos;
7400 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7401 }
7402
7403 if (STRINGP (it->string) || !it->s)
7404 {
7405 ptrdiff_t stop, charpos, bytepos;
7406
7407 if (STRINGP (it->string))
7408 {
7409 eassert (!it->s);
7410 stop = SCHARS (it->string);
7411 if (stop > it->end_charpos)
7412 stop = it->end_charpos;
7413 charpos = IT_STRING_CHARPOS (*it);
7414 bytepos = IT_STRING_BYTEPOS (*it);
7415 }
7416 else
7417 {
7418 stop = it->end_charpos;
7419 charpos = IT_CHARPOS (*it);
7420 bytepos = IT_BYTEPOS (*it);
7421 }
7422 if (it->bidi_it.scan_dir < 0)
7423 stop = -1;
7424 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7425 it->string);
7426 }
7427 }
7428
7429 /* Load IT with the next display element from Lisp string IT->string.
7430 IT->current.string_pos is the current position within the string.
7431 If IT->current.overlay_string_index >= 0, the Lisp string is an
7432 overlay string. */
7433
7434 static int
7435 next_element_from_string (struct it *it)
7436 {
7437 struct text_pos position;
7438
7439 eassert (STRINGP (it->string));
7440 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7441 eassert (IT_STRING_CHARPOS (*it) >= 0);
7442 position = it->current.string_pos;
7443
7444 /* With bidi reordering, the character to display might not be the
7445 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7446 that we were reseat()ed to a new string, whose paragraph
7447 direction is not known. */
7448 if (it->bidi_p && it->bidi_it.first_elt)
7449 {
7450 get_visually_first_element (it);
7451 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7452 }
7453
7454 /* Time to check for invisible text? */
7455 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7456 {
7457 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7458 {
7459 if (!(!it->bidi_p
7460 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7461 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7462 {
7463 /* With bidi non-linear iteration, we could find
7464 ourselves far beyond the last computed stop_charpos,
7465 with several other stop positions in between that we
7466 missed. Scan them all now, in buffer's logical
7467 order, until we find and handle the last stop_charpos
7468 that precedes our current position. */
7469 handle_stop_backwards (it, it->stop_charpos);
7470 return GET_NEXT_DISPLAY_ELEMENT (it);
7471 }
7472 else
7473 {
7474 if (it->bidi_p)
7475 {
7476 /* Take note of the stop position we just moved
7477 across, for when we will move back across it. */
7478 it->prev_stop = it->stop_charpos;
7479 /* If we are at base paragraph embedding level, take
7480 note of the last stop position seen at this
7481 level. */
7482 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7483 it->base_level_stop = it->stop_charpos;
7484 }
7485 handle_stop (it);
7486
7487 /* Since a handler may have changed IT->method, we must
7488 recurse here. */
7489 return GET_NEXT_DISPLAY_ELEMENT (it);
7490 }
7491 }
7492 else if (it->bidi_p
7493 /* If we are before prev_stop, we may have overstepped
7494 on our way backwards a stop_pos, and if so, we need
7495 to handle that stop_pos. */
7496 && IT_STRING_CHARPOS (*it) < it->prev_stop
7497 /* We can sometimes back up for reasons that have nothing
7498 to do with bidi reordering. E.g., compositions. The
7499 code below is only needed when we are above the base
7500 embedding level, so test for that explicitly. */
7501 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7502 {
7503 /* If we lost track of base_level_stop, we have no better
7504 place for handle_stop_backwards to start from than string
7505 beginning. This happens, e.g., when we were reseated to
7506 the previous screenful of text by vertical-motion. */
7507 if (it->base_level_stop <= 0
7508 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7509 it->base_level_stop = 0;
7510 handle_stop_backwards (it, it->base_level_stop);
7511 return GET_NEXT_DISPLAY_ELEMENT (it);
7512 }
7513 }
7514
7515 if (it->current.overlay_string_index >= 0)
7516 {
7517 /* Get the next character from an overlay string. In overlay
7518 strings, there is no field width or padding with spaces to
7519 do. */
7520 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7521 {
7522 it->what = IT_EOB;
7523 return 0;
7524 }
7525 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7526 IT_STRING_BYTEPOS (*it),
7527 it->bidi_it.scan_dir < 0
7528 ? -1
7529 : SCHARS (it->string))
7530 && next_element_from_composition (it))
7531 {
7532 return 1;
7533 }
7534 else if (STRING_MULTIBYTE (it->string))
7535 {
7536 const unsigned char *s = (SDATA (it->string)
7537 + IT_STRING_BYTEPOS (*it));
7538 it->c = string_char_and_length (s, &it->len);
7539 }
7540 else
7541 {
7542 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7543 it->len = 1;
7544 }
7545 }
7546 else
7547 {
7548 /* Get the next character from a Lisp string that is not an
7549 overlay string. Such strings come from the mode line, for
7550 example. We may have to pad with spaces, or truncate the
7551 string. See also next_element_from_c_string. */
7552 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7553 {
7554 it->what = IT_EOB;
7555 return 0;
7556 }
7557 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7558 {
7559 /* Pad with spaces. */
7560 it->c = ' ', it->len = 1;
7561 CHARPOS (position) = BYTEPOS (position) = -1;
7562 }
7563 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7564 IT_STRING_BYTEPOS (*it),
7565 it->bidi_it.scan_dir < 0
7566 ? -1
7567 : it->string_nchars)
7568 && next_element_from_composition (it))
7569 {
7570 return 1;
7571 }
7572 else if (STRING_MULTIBYTE (it->string))
7573 {
7574 const unsigned char *s = (SDATA (it->string)
7575 + IT_STRING_BYTEPOS (*it));
7576 it->c = string_char_and_length (s, &it->len);
7577 }
7578 else
7579 {
7580 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7581 it->len = 1;
7582 }
7583 }
7584
7585 /* Record what we have and where it came from. */
7586 it->what = IT_CHARACTER;
7587 it->object = it->string;
7588 it->position = position;
7589 return 1;
7590 }
7591
7592
7593 /* Load IT with next display element from C string IT->s.
7594 IT->string_nchars is the maximum number of characters to return
7595 from the string. IT->end_charpos may be greater than
7596 IT->string_nchars when this function is called, in which case we
7597 may have to return padding spaces. Value is zero if end of string
7598 reached, including padding spaces. */
7599
7600 static int
7601 next_element_from_c_string (struct it *it)
7602 {
7603 int success_p = 1;
7604
7605 eassert (it->s);
7606 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7607 it->what = IT_CHARACTER;
7608 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7609 it->object = Qnil;
7610
7611 /* With bidi reordering, the character to display might not be the
7612 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7613 we were reseated to a new string, whose paragraph direction is
7614 not known. */
7615 if (it->bidi_p && it->bidi_it.first_elt)
7616 get_visually_first_element (it);
7617
7618 /* IT's position can be greater than IT->string_nchars in case a
7619 field width or precision has been specified when the iterator was
7620 initialized. */
7621 if (IT_CHARPOS (*it) >= it->end_charpos)
7622 {
7623 /* End of the game. */
7624 it->what = IT_EOB;
7625 success_p = 0;
7626 }
7627 else if (IT_CHARPOS (*it) >= it->string_nchars)
7628 {
7629 /* Pad with spaces. */
7630 it->c = ' ', it->len = 1;
7631 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7632 }
7633 else if (it->multibyte_p)
7634 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7635 else
7636 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7637
7638 return success_p;
7639 }
7640
7641
7642 /* Set up IT to return characters from an ellipsis, if appropriate.
7643 The definition of the ellipsis glyphs may come from a display table
7644 entry. This function fills IT with the first glyph from the
7645 ellipsis if an ellipsis is to be displayed. */
7646
7647 static int
7648 next_element_from_ellipsis (struct it *it)
7649 {
7650 if (it->selective_display_ellipsis_p)
7651 setup_for_ellipsis (it, it->len);
7652 else
7653 {
7654 /* The face at the current position may be different from the
7655 face we find after the invisible text. Remember what it
7656 was in IT->saved_face_id, and signal that it's there by
7657 setting face_before_selective_p. */
7658 it->saved_face_id = it->face_id;
7659 it->method = GET_FROM_BUFFER;
7660 it->object = it->w->buffer;
7661 reseat_at_next_visible_line_start (it, 1);
7662 it->face_before_selective_p = 1;
7663 }
7664
7665 return GET_NEXT_DISPLAY_ELEMENT (it);
7666 }
7667
7668
7669 /* Deliver an image display element. The iterator IT is already
7670 filled with image information (done in handle_display_prop). Value
7671 is always 1. */
7672
7673
7674 static int
7675 next_element_from_image (struct it *it)
7676 {
7677 it->what = IT_IMAGE;
7678 it->ignore_overlay_strings_at_pos_p = 0;
7679 return 1;
7680 }
7681
7682
7683 /* Fill iterator IT with next display element from a stretch glyph
7684 property. IT->object is the value of the text property. Value is
7685 always 1. */
7686
7687 static int
7688 next_element_from_stretch (struct it *it)
7689 {
7690 it->what = IT_STRETCH;
7691 return 1;
7692 }
7693
7694 /* Scan backwards from IT's current position until we find a stop
7695 position, or until BEGV. This is called when we find ourself
7696 before both the last known prev_stop and base_level_stop while
7697 reordering bidirectional text. */
7698
7699 static void
7700 compute_stop_pos_backwards (struct it *it)
7701 {
7702 const int SCAN_BACK_LIMIT = 1000;
7703 struct text_pos pos;
7704 struct display_pos save_current = it->current;
7705 struct text_pos save_position = it->position;
7706 ptrdiff_t charpos = IT_CHARPOS (*it);
7707 ptrdiff_t where_we_are = charpos;
7708 ptrdiff_t save_stop_pos = it->stop_charpos;
7709 ptrdiff_t save_end_pos = it->end_charpos;
7710
7711 eassert (NILP (it->string) && !it->s);
7712 eassert (it->bidi_p);
7713 it->bidi_p = 0;
7714 do
7715 {
7716 it->end_charpos = min (charpos + 1, ZV);
7717 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7718 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7719 reseat_1 (it, pos, 0);
7720 compute_stop_pos (it);
7721 /* We must advance forward, right? */
7722 if (it->stop_charpos <= charpos)
7723 abort ();
7724 }
7725 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7726
7727 if (it->stop_charpos <= where_we_are)
7728 it->prev_stop = it->stop_charpos;
7729 else
7730 it->prev_stop = BEGV;
7731 it->bidi_p = 1;
7732 it->current = save_current;
7733 it->position = save_position;
7734 it->stop_charpos = save_stop_pos;
7735 it->end_charpos = save_end_pos;
7736 }
7737
7738 /* Scan forward from CHARPOS in the current buffer/string, until we
7739 find a stop position > current IT's position. Then handle the stop
7740 position before that. This is called when we bump into a stop
7741 position while reordering bidirectional text. CHARPOS should be
7742 the last previously processed stop_pos (or BEGV/0, if none were
7743 processed yet) whose position is less that IT's current
7744 position. */
7745
7746 static void
7747 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7748 {
7749 int bufp = !STRINGP (it->string);
7750 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7751 struct display_pos save_current = it->current;
7752 struct text_pos save_position = it->position;
7753 struct text_pos pos1;
7754 ptrdiff_t next_stop;
7755
7756 /* Scan in strict logical order. */
7757 eassert (it->bidi_p);
7758 it->bidi_p = 0;
7759 do
7760 {
7761 it->prev_stop = charpos;
7762 if (bufp)
7763 {
7764 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7765 reseat_1 (it, pos1, 0);
7766 }
7767 else
7768 it->current.string_pos = string_pos (charpos, it->string);
7769 compute_stop_pos (it);
7770 /* We must advance forward, right? */
7771 if (it->stop_charpos <= it->prev_stop)
7772 abort ();
7773 charpos = it->stop_charpos;
7774 }
7775 while (charpos <= where_we_are);
7776
7777 it->bidi_p = 1;
7778 it->current = save_current;
7779 it->position = save_position;
7780 next_stop = it->stop_charpos;
7781 it->stop_charpos = it->prev_stop;
7782 handle_stop (it);
7783 it->stop_charpos = next_stop;
7784 }
7785
7786 /* Load IT with the next display element from current_buffer. Value
7787 is zero if end of buffer reached. IT->stop_charpos is the next
7788 position at which to stop and check for text properties or buffer
7789 end. */
7790
7791 static int
7792 next_element_from_buffer (struct it *it)
7793 {
7794 int success_p = 1;
7795
7796 eassert (IT_CHARPOS (*it) >= BEGV);
7797 eassert (NILP (it->string) && !it->s);
7798 eassert (!it->bidi_p
7799 || (EQ (it->bidi_it.string.lstring, Qnil)
7800 && it->bidi_it.string.s == NULL));
7801
7802 /* With bidi reordering, the character to display might not be the
7803 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7804 we were reseat()ed to a new buffer position, which is potentially
7805 a different paragraph. */
7806 if (it->bidi_p && it->bidi_it.first_elt)
7807 {
7808 get_visually_first_element (it);
7809 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7810 }
7811
7812 if (IT_CHARPOS (*it) >= it->stop_charpos)
7813 {
7814 if (IT_CHARPOS (*it) >= it->end_charpos)
7815 {
7816 int overlay_strings_follow_p;
7817
7818 /* End of the game, except when overlay strings follow that
7819 haven't been returned yet. */
7820 if (it->overlay_strings_at_end_processed_p)
7821 overlay_strings_follow_p = 0;
7822 else
7823 {
7824 it->overlay_strings_at_end_processed_p = 1;
7825 overlay_strings_follow_p = get_overlay_strings (it, 0);
7826 }
7827
7828 if (overlay_strings_follow_p)
7829 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7830 else
7831 {
7832 it->what = IT_EOB;
7833 it->position = it->current.pos;
7834 success_p = 0;
7835 }
7836 }
7837 else if (!(!it->bidi_p
7838 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7839 || IT_CHARPOS (*it) == it->stop_charpos))
7840 {
7841 /* With bidi non-linear iteration, we could find ourselves
7842 far beyond the last computed stop_charpos, with several
7843 other stop positions in between that we missed. Scan
7844 them all now, in buffer's logical order, until we find
7845 and handle the last stop_charpos that precedes our
7846 current position. */
7847 handle_stop_backwards (it, it->stop_charpos);
7848 return GET_NEXT_DISPLAY_ELEMENT (it);
7849 }
7850 else
7851 {
7852 if (it->bidi_p)
7853 {
7854 /* Take note of the stop position we just moved across,
7855 for when we will move back across it. */
7856 it->prev_stop = it->stop_charpos;
7857 /* If we are at base paragraph embedding level, take
7858 note of the last stop position seen at this
7859 level. */
7860 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7861 it->base_level_stop = it->stop_charpos;
7862 }
7863 handle_stop (it);
7864 return GET_NEXT_DISPLAY_ELEMENT (it);
7865 }
7866 }
7867 else if (it->bidi_p
7868 /* If we are before prev_stop, we may have overstepped on
7869 our way backwards a stop_pos, and if so, we need to
7870 handle that stop_pos. */
7871 && IT_CHARPOS (*it) < it->prev_stop
7872 /* We can sometimes back up for reasons that have nothing
7873 to do with bidi reordering. E.g., compositions. The
7874 code below is only needed when we are above the base
7875 embedding level, so test for that explicitly. */
7876 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7877 {
7878 if (it->base_level_stop <= 0
7879 || IT_CHARPOS (*it) < it->base_level_stop)
7880 {
7881 /* If we lost track of base_level_stop, we need to find
7882 prev_stop by looking backwards. This happens, e.g., when
7883 we were reseated to the previous screenful of text by
7884 vertical-motion. */
7885 it->base_level_stop = BEGV;
7886 compute_stop_pos_backwards (it);
7887 handle_stop_backwards (it, it->prev_stop);
7888 }
7889 else
7890 handle_stop_backwards (it, it->base_level_stop);
7891 return GET_NEXT_DISPLAY_ELEMENT (it);
7892 }
7893 else
7894 {
7895 /* No face changes, overlays etc. in sight, so just return a
7896 character from current_buffer. */
7897 unsigned char *p;
7898 ptrdiff_t stop;
7899
7900 /* Maybe run the redisplay end trigger hook. Performance note:
7901 This doesn't seem to cost measurable time. */
7902 if (it->redisplay_end_trigger_charpos
7903 && it->glyph_row
7904 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7905 run_redisplay_end_trigger_hook (it);
7906
7907 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7908 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7909 stop)
7910 && next_element_from_composition (it))
7911 {
7912 return 1;
7913 }
7914
7915 /* Get the next character, maybe multibyte. */
7916 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7917 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7918 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7919 else
7920 it->c = *p, it->len = 1;
7921
7922 /* Record what we have and where it came from. */
7923 it->what = IT_CHARACTER;
7924 it->object = it->w->buffer;
7925 it->position = it->current.pos;
7926
7927 /* Normally we return the character found above, except when we
7928 really want to return an ellipsis for selective display. */
7929 if (it->selective)
7930 {
7931 if (it->c == '\n')
7932 {
7933 /* A value of selective > 0 means hide lines indented more
7934 than that number of columns. */
7935 if (it->selective > 0
7936 && IT_CHARPOS (*it) + 1 < ZV
7937 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7938 IT_BYTEPOS (*it) + 1,
7939 it->selective))
7940 {
7941 success_p = next_element_from_ellipsis (it);
7942 it->dpvec_char_len = -1;
7943 }
7944 }
7945 else if (it->c == '\r' && it->selective == -1)
7946 {
7947 /* A value of selective == -1 means that everything from the
7948 CR to the end of the line is invisible, with maybe an
7949 ellipsis displayed for it. */
7950 success_p = next_element_from_ellipsis (it);
7951 it->dpvec_char_len = -1;
7952 }
7953 }
7954 }
7955
7956 /* Value is zero if end of buffer reached. */
7957 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7958 return success_p;
7959 }
7960
7961
7962 /* Run the redisplay end trigger hook for IT. */
7963
7964 static void
7965 run_redisplay_end_trigger_hook (struct it *it)
7966 {
7967 Lisp_Object args[3];
7968
7969 /* IT->glyph_row should be non-null, i.e. we should be actually
7970 displaying something, or otherwise we should not run the hook. */
7971 eassert (it->glyph_row);
7972
7973 /* Set up hook arguments. */
7974 args[0] = Qredisplay_end_trigger_functions;
7975 args[1] = it->window;
7976 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7977 it->redisplay_end_trigger_charpos = 0;
7978
7979 /* Since we are *trying* to run these functions, don't try to run
7980 them again, even if they get an error. */
7981 it->w->redisplay_end_trigger = Qnil;
7982 Frun_hook_with_args (3, args);
7983
7984 /* Notice if it changed the face of the character we are on. */
7985 handle_face_prop (it);
7986 }
7987
7988
7989 /* Deliver a composition display element. Unlike the other
7990 next_element_from_XXX, this function is not registered in the array
7991 get_next_element[]. It is called from next_element_from_buffer and
7992 next_element_from_string when necessary. */
7993
7994 static int
7995 next_element_from_composition (struct it *it)
7996 {
7997 it->what = IT_COMPOSITION;
7998 it->len = it->cmp_it.nbytes;
7999 if (STRINGP (it->string))
8000 {
8001 if (it->c < 0)
8002 {
8003 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8004 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8005 return 0;
8006 }
8007 it->position = it->current.string_pos;
8008 it->object = it->string;
8009 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8010 IT_STRING_BYTEPOS (*it), it->string);
8011 }
8012 else
8013 {
8014 if (it->c < 0)
8015 {
8016 IT_CHARPOS (*it) += it->cmp_it.nchars;
8017 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8018 if (it->bidi_p)
8019 {
8020 if (it->bidi_it.new_paragraph)
8021 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8022 /* Resync the bidi iterator with IT's new position.
8023 FIXME: this doesn't support bidirectional text. */
8024 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8025 bidi_move_to_visually_next (&it->bidi_it);
8026 }
8027 return 0;
8028 }
8029 it->position = it->current.pos;
8030 it->object = it->w->buffer;
8031 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8032 IT_BYTEPOS (*it), Qnil);
8033 }
8034 return 1;
8035 }
8036
8037
8038 \f
8039 /***********************************************************************
8040 Moving an iterator without producing glyphs
8041 ***********************************************************************/
8042
8043 /* Check if iterator is at a position corresponding to a valid buffer
8044 position after some move_it_ call. */
8045
8046 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8047 ((it)->method == GET_FROM_STRING \
8048 ? IT_STRING_CHARPOS (*it) == 0 \
8049 : 1)
8050
8051
8052 /* Move iterator IT to a specified buffer or X position within one
8053 line on the display without producing glyphs.
8054
8055 OP should be a bit mask including some or all of these bits:
8056 MOVE_TO_X: Stop upon reaching x-position TO_X.
8057 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8058 Regardless of OP's value, stop upon reaching the end of the display line.
8059
8060 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8061 This means, in particular, that TO_X includes window's horizontal
8062 scroll amount.
8063
8064 The return value has several possible values that
8065 say what condition caused the scan to stop:
8066
8067 MOVE_POS_MATCH_OR_ZV
8068 - when TO_POS or ZV was reached.
8069
8070 MOVE_X_REACHED
8071 -when TO_X was reached before TO_POS or ZV were reached.
8072
8073 MOVE_LINE_CONTINUED
8074 - when we reached the end of the display area and the line must
8075 be continued.
8076
8077 MOVE_LINE_TRUNCATED
8078 - when we reached the end of the display area and the line is
8079 truncated.
8080
8081 MOVE_NEWLINE_OR_CR
8082 - when we stopped at a line end, i.e. a newline or a CR and selective
8083 display is on. */
8084
8085 static enum move_it_result
8086 move_it_in_display_line_to (struct it *it,
8087 ptrdiff_t to_charpos, int to_x,
8088 enum move_operation_enum op)
8089 {
8090 enum move_it_result result = MOVE_UNDEFINED;
8091 struct glyph_row *saved_glyph_row;
8092 struct it wrap_it, atpos_it, atx_it, ppos_it;
8093 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8094 void *ppos_data = NULL;
8095 int may_wrap = 0;
8096 enum it_method prev_method = it->method;
8097 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8098 int saw_smaller_pos = prev_pos < to_charpos;
8099
8100 /* Don't produce glyphs in produce_glyphs. */
8101 saved_glyph_row = it->glyph_row;
8102 it->glyph_row = NULL;
8103
8104 /* Use wrap_it to save a copy of IT wherever a word wrap could
8105 occur. Use atpos_it to save a copy of IT at the desired buffer
8106 position, if found, so that we can scan ahead and check if the
8107 word later overshoots the window edge. Use atx_it similarly, for
8108 pixel positions. */
8109 wrap_it.sp = -1;
8110 atpos_it.sp = -1;
8111 atx_it.sp = -1;
8112
8113 /* Use ppos_it under bidi reordering to save a copy of IT for the
8114 position > CHARPOS that is the closest to CHARPOS. We restore
8115 that position in IT when we have scanned the entire display line
8116 without finding a match for CHARPOS and all the character
8117 positions are greater than CHARPOS. */
8118 if (it->bidi_p)
8119 {
8120 SAVE_IT (ppos_it, *it, ppos_data);
8121 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8122 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8123 SAVE_IT (ppos_it, *it, ppos_data);
8124 }
8125
8126 #define BUFFER_POS_REACHED_P() \
8127 ((op & MOVE_TO_POS) != 0 \
8128 && BUFFERP (it->object) \
8129 && (IT_CHARPOS (*it) == to_charpos \
8130 || ((!it->bidi_p \
8131 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8132 && IT_CHARPOS (*it) > to_charpos) \
8133 || (it->what == IT_COMPOSITION \
8134 && ((IT_CHARPOS (*it) > to_charpos \
8135 && to_charpos >= it->cmp_it.charpos) \
8136 || (IT_CHARPOS (*it) < to_charpos \
8137 && to_charpos <= it->cmp_it.charpos)))) \
8138 && (it->method == GET_FROM_BUFFER \
8139 || (it->method == GET_FROM_DISPLAY_VECTOR \
8140 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8141
8142 /* If there's a line-/wrap-prefix, handle it. */
8143 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8144 && it->current_y < it->last_visible_y)
8145 handle_line_prefix (it);
8146
8147 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8148 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8149
8150 while (1)
8151 {
8152 int x, i, ascent = 0, descent = 0;
8153
8154 /* Utility macro to reset an iterator with x, ascent, and descent. */
8155 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8156 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8157 (IT)->max_descent = descent)
8158
8159 /* Stop if we move beyond TO_CHARPOS (after an image or a
8160 display string or stretch glyph). */
8161 if ((op & MOVE_TO_POS) != 0
8162 && BUFFERP (it->object)
8163 && it->method == GET_FROM_BUFFER
8164 && (((!it->bidi_p
8165 /* When the iterator is at base embedding level, we
8166 are guaranteed that characters are delivered for
8167 display in strictly increasing order of their
8168 buffer positions. */
8169 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8170 && IT_CHARPOS (*it) > to_charpos)
8171 || (it->bidi_p
8172 && (prev_method == GET_FROM_IMAGE
8173 || prev_method == GET_FROM_STRETCH
8174 || prev_method == GET_FROM_STRING)
8175 /* Passed TO_CHARPOS from left to right. */
8176 && ((prev_pos < to_charpos
8177 && IT_CHARPOS (*it) > to_charpos)
8178 /* Passed TO_CHARPOS from right to left. */
8179 || (prev_pos > to_charpos
8180 && IT_CHARPOS (*it) < to_charpos)))))
8181 {
8182 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8183 {
8184 result = MOVE_POS_MATCH_OR_ZV;
8185 break;
8186 }
8187 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8188 /* If wrap_it is valid, the current position might be in a
8189 word that is wrapped. So, save the iterator in
8190 atpos_it and continue to see if wrapping happens. */
8191 SAVE_IT (atpos_it, *it, atpos_data);
8192 }
8193
8194 /* Stop when ZV reached.
8195 We used to stop here when TO_CHARPOS reached as well, but that is
8196 too soon if this glyph does not fit on this line. So we handle it
8197 explicitly below. */
8198 if (!get_next_display_element (it))
8199 {
8200 result = MOVE_POS_MATCH_OR_ZV;
8201 break;
8202 }
8203
8204 if (it->line_wrap == TRUNCATE)
8205 {
8206 if (BUFFER_POS_REACHED_P ())
8207 {
8208 result = MOVE_POS_MATCH_OR_ZV;
8209 break;
8210 }
8211 }
8212 else
8213 {
8214 if (it->line_wrap == WORD_WRAP)
8215 {
8216 if (IT_DISPLAYING_WHITESPACE (it))
8217 may_wrap = 1;
8218 else if (may_wrap)
8219 {
8220 /* We have reached a glyph that follows one or more
8221 whitespace characters. If the position is
8222 already found, we are done. */
8223 if (atpos_it.sp >= 0)
8224 {
8225 RESTORE_IT (it, &atpos_it, atpos_data);
8226 result = MOVE_POS_MATCH_OR_ZV;
8227 goto done;
8228 }
8229 if (atx_it.sp >= 0)
8230 {
8231 RESTORE_IT (it, &atx_it, atx_data);
8232 result = MOVE_X_REACHED;
8233 goto done;
8234 }
8235 /* Otherwise, we can wrap here. */
8236 SAVE_IT (wrap_it, *it, wrap_data);
8237 may_wrap = 0;
8238 }
8239 }
8240 }
8241
8242 /* Remember the line height for the current line, in case
8243 the next element doesn't fit on the line. */
8244 ascent = it->max_ascent;
8245 descent = it->max_descent;
8246
8247 /* The call to produce_glyphs will get the metrics of the
8248 display element IT is loaded with. Record the x-position
8249 before this display element, in case it doesn't fit on the
8250 line. */
8251 x = it->current_x;
8252
8253 PRODUCE_GLYPHS (it);
8254
8255 if (it->area != TEXT_AREA)
8256 {
8257 prev_method = it->method;
8258 if (it->method == GET_FROM_BUFFER)
8259 prev_pos = IT_CHARPOS (*it);
8260 set_iterator_to_next (it, 1);
8261 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8262 SET_TEXT_POS (this_line_min_pos,
8263 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8264 if (it->bidi_p
8265 && (op & MOVE_TO_POS)
8266 && IT_CHARPOS (*it) > to_charpos
8267 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8268 SAVE_IT (ppos_it, *it, ppos_data);
8269 continue;
8270 }
8271
8272 /* The number of glyphs we get back in IT->nglyphs will normally
8273 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8274 character on a terminal frame, or (iii) a line end. For the
8275 second case, IT->nglyphs - 1 padding glyphs will be present.
8276 (On X frames, there is only one glyph produced for a
8277 composite character.)
8278
8279 The behavior implemented below means, for continuation lines,
8280 that as many spaces of a TAB as fit on the current line are
8281 displayed there. For terminal frames, as many glyphs of a
8282 multi-glyph character are displayed in the current line, too.
8283 This is what the old redisplay code did, and we keep it that
8284 way. Under X, the whole shape of a complex character must
8285 fit on the line or it will be completely displayed in the
8286 next line.
8287
8288 Note that both for tabs and padding glyphs, all glyphs have
8289 the same width. */
8290 if (it->nglyphs)
8291 {
8292 /* More than one glyph or glyph doesn't fit on line. All
8293 glyphs have the same width. */
8294 int single_glyph_width = it->pixel_width / it->nglyphs;
8295 int new_x;
8296 int x_before_this_char = x;
8297 int hpos_before_this_char = it->hpos;
8298
8299 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8300 {
8301 new_x = x + single_glyph_width;
8302
8303 /* We want to leave anything reaching TO_X to the caller. */
8304 if ((op & MOVE_TO_X) && new_x > to_x)
8305 {
8306 if (BUFFER_POS_REACHED_P ())
8307 {
8308 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8309 goto buffer_pos_reached;
8310 if (atpos_it.sp < 0)
8311 {
8312 SAVE_IT (atpos_it, *it, atpos_data);
8313 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8314 }
8315 }
8316 else
8317 {
8318 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8319 {
8320 it->current_x = x;
8321 result = MOVE_X_REACHED;
8322 break;
8323 }
8324 if (atx_it.sp < 0)
8325 {
8326 SAVE_IT (atx_it, *it, atx_data);
8327 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8328 }
8329 }
8330 }
8331
8332 if (/* Lines are continued. */
8333 it->line_wrap != TRUNCATE
8334 && (/* And glyph doesn't fit on the line. */
8335 new_x > it->last_visible_x
8336 /* Or it fits exactly and we're on a window
8337 system frame. */
8338 || (new_x == it->last_visible_x
8339 && FRAME_WINDOW_P (it->f)
8340 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8341 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8342 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8343 {
8344 if (/* IT->hpos == 0 means the very first glyph
8345 doesn't fit on the line, e.g. a wide image. */
8346 it->hpos == 0
8347 || (new_x == it->last_visible_x
8348 && FRAME_WINDOW_P (it->f)))
8349 {
8350 ++it->hpos;
8351 it->current_x = new_x;
8352
8353 /* The character's last glyph just barely fits
8354 in this row. */
8355 if (i == it->nglyphs - 1)
8356 {
8357 /* If this is the destination position,
8358 return a position *before* it in this row,
8359 now that we know it fits in this row. */
8360 if (BUFFER_POS_REACHED_P ())
8361 {
8362 if (it->line_wrap != WORD_WRAP
8363 || wrap_it.sp < 0)
8364 {
8365 it->hpos = hpos_before_this_char;
8366 it->current_x = x_before_this_char;
8367 result = MOVE_POS_MATCH_OR_ZV;
8368 break;
8369 }
8370 if (it->line_wrap == WORD_WRAP
8371 && atpos_it.sp < 0)
8372 {
8373 SAVE_IT (atpos_it, *it, atpos_data);
8374 atpos_it.current_x = x_before_this_char;
8375 atpos_it.hpos = hpos_before_this_char;
8376 }
8377 }
8378
8379 prev_method = it->method;
8380 if (it->method == GET_FROM_BUFFER)
8381 prev_pos = IT_CHARPOS (*it);
8382 set_iterator_to_next (it, 1);
8383 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8384 SET_TEXT_POS (this_line_min_pos,
8385 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8386 /* On graphical terminals, newlines may
8387 "overflow" into the fringe if
8388 overflow-newline-into-fringe is non-nil.
8389 On text terminals, and on graphical
8390 terminals with no right margin, newlines
8391 may overflow into the last glyph on the
8392 display line.*/
8393 if (!FRAME_WINDOW_P (it->f)
8394 || ((it->bidi_p
8395 && it->bidi_it.paragraph_dir == R2L)
8396 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8397 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8398 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8399 {
8400 if (!get_next_display_element (it))
8401 {
8402 result = MOVE_POS_MATCH_OR_ZV;
8403 break;
8404 }
8405 if (BUFFER_POS_REACHED_P ())
8406 {
8407 if (ITERATOR_AT_END_OF_LINE_P (it))
8408 result = MOVE_POS_MATCH_OR_ZV;
8409 else
8410 result = MOVE_LINE_CONTINUED;
8411 break;
8412 }
8413 if (ITERATOR_AT_END_OF_LINE_P (it))
8414 {
8415 result = MOVE_NEWLINE_OR_CR;
8416 break;
8417 }
8418 }
8419 }
8420 }
8421 else
8422 IT_RESET_X_ASCENT_DESCENT (it);
8423
8424 if (wrap_it.sp >= 0)
8425 {
8426 RESTORE_IT (it, &wrap_it, wrap_data);
8427 atpos_it.sp = -1;
8428 atx_it.sp = -1;
8429 }
8430
8431 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8432 IT_CHARPOS (*it)));
8433 result = MOVE_LINE_CONTINUED;
8434 break;
8435 }
8436
8437 if (BUFFER_POS_REACHED_P ())
8438 {
8439 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8440 goto buffer_pos_reached;
8441 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8442 {
8443 SAVE_IT (atpos_it, *it, atpos_data);
8444 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8445 }
8446 }
8447
8448 if (new_x > it->first_visible_x)
8449 {
8450 /* Glyph is visible. Increment number of glyphs that
8451 would be displayed. */
8452 ++it->hpos;
8453 }
8454 }
8455
8456 if (result != MOVE_UNDEFINED)
8457 break;
8458 }
8459 else if (BUFFER_POS_REACHED_P ())
8460 {
8461 buffer_pos_reached:
8462 IT_RESET_X_ASCENT_DESCENT (it);
8463 result = MOVE_POS_MATCH_OR_ZV;
8464 break;
8465 }
8466 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8467 {
8468 /* Stop when TO_X specified and reached. This check is
8469 necessary here because of lines consisting of a line end,
8470 only. The line end will not produce any glyphs and we
8471 would never get MOVE_X_REACHED. */
8472 eassert (it->nglyphs == 0);
8473 result = MOVE_X_REACHED;
8474 break;
8475 }
8476
8477 /* Is this a line end? If yes, we're done. */
8478 if (ITERATOR_AT_END_OF_LINE_P (it))
8479 {
8480 /* If we are past TO_CHARPOS, but never saw any character
8481 positions smaller than TO_CHARPOS, return
8482 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8483 did. */
8484 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8485 {
8486 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8487 {
8488 if (IT_CHARPOS (ppos_it) < ZV)
8489 {
8490 RESTORE_IT (it, &ppos_it, ppos_data);
8491 result = MOVE_POS_MATCH_OR_ZV;
8492 }
8493 else
8494 goto buffer_pos_reached;
8495 }
8496 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8497 && IT_CHARPOS (*it) > to_charpos)
8498 goto buffer_pos_reached;
8499 else
8500 result = MOVE_NEWLINE_OR_CR;
8501 }
8502 else
8503 result = MOVE_NEWLINE_OR_CR;
8504 break;
8505 }
8506
8507 prev_method = it->method;
8508 if (it->method == GET_FROM_BUFFER)
8509 prev_pos = IT_CHARPOS (*it);
8510 /* The current display element has been consumed. Advance
8511 to the next. */
8512 set_iterator_to_next (it, 1);
8513 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8514 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8515 if (IT_CHARPOS (*it) < to_charpos)
8516 saw_smaller_pos = 1;
8517 if (it->bidi_p
8518 && (op & MOVE_TO_POS)
8519 && IT_CHARPOS (*it) >= to_charpos
8520 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8521 SAVE_IT (ppos_it, *it, ppos_data);
8522
8523 /* Stop if lines are truncated and IT's current x-position is
8524 past the right edge of the window now. */
8525 if (it->line_wrap == TRUNCATE
8526 && it->current_x >= it->last_visible_x)
8527 {
8528 if (!FRAME_WINDOW_P (it->f)
8529 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8530 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8531 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8532 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8533 {
8534 int at_eob_p = 0;
8535
8536 if ((at_eob_p = !get_next_display_element (it))
8537 || BUFFER_POS_REACHED_P ()
8538 /* If we are past TO_CHARPOS, but never saw any
8539 character positions smaller than TO_CHARPOS,
8540 return MOVE_POS_MATCH_OR_ZV, like the
8541 unidirectional display did. */
8542 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8543 && !saw_smaller_pos
8544 && IT_CHARPOS (*it) > to_charpos))
8545 {
8546 if (it->bidi_p
8547 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8548 RESTORE_IT (it, &ppos_it, ppos_data);
8549 result = MOVE_POS_MATCH_OR_ZV;
8550 break;
8551 }
8552 if (ITERATOR_AT_END_OF_LINE_P (it))
8553 {
8554 result = MOVE_NEWLINE_OR_CR;
8555 break;
8556 }
8557 }
8558 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8559 && !saw_smaller_pos
8560 && IT_CHARPOS (*it) > to_charpos)
8561 {
8562 if (IT_CHARPOS (ppos_it) < ZV)
8563 RESTORE_IT (it, &ppos_it, ppos_data);
8564 result = MOVE_POS_MATCH_OR_ZV;
8565 break;
8566 }
8567 result = MOVE_LINE_TRUNCATED;
8568 break;
8569 }
8570 #undef IT_RESET_X_ASCENT_DESCENT
8571 }
8572
8573 #undef BUFFER_POS_REACHED_P
8574
8575 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8576 restore the saved iterator. */
8577 if (atpos_it.sp >= 0)
8578 RESTORE_IT (it, &atpos_it, atpos_data);
8579 else if (atx_it.sp >= 0)
8580 RESTORE_IT (it, &atx_it, atx_data);
8581
8582 done:
8583
8584 if (atpos_data)
8585 bidi_unshelve_cache (atpos_data, 1);
8586 if (atx_data)
8587 bidi_unshelve_cache (atx_data, 1);
8588 if (wrap_data)
8589 bidi_unshelve_cache (wrap_data, 1);
8590 if (ppos_data)
8591 bidi_unshelve_cache (ppos_data, 1);
8592
8593 /* Restore the iterator settings altered at the beginning of this
8594 function. */
8595 it->glyph_row = saved_glyph_row;
8596 return result;
8597 }
8598
8599 /* For external use. */
8600 void
8601 move_it_in_display_line (struct it *it,
8602 ptrdiff_t to_charpos, int to_x,
8603 enum move_operation_enum op)
8604 {
8605 if (it->line_wrap == WORD_WRAP
8606 && (op & MOVE_TO_X))
8607 {
8608 struct it save_it;
8609 void *save_data = NULL;
8610 int skip;
8611
8612 SAVE_IT (save_it, *it, save_data);
8613 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8614 /* When word-wrap is on, TO_X may lie past the end
8615 of a wrapped line. Then it->current is the
8616 character on the next line, so backtrack to the
8617 space before the wrap point. */
8618 if (skip == MOVE_LINE_CONTINUED)
8619 {
8620 int prev_x = max (it->current_x - 1, 0);
8621 RESTORE_IT (it, &save_it, save_data);
8622 move_it_in_display_line_to
8623 (it, -1, prev_x, MOVE_TO_X);
8624 }
8625 else
8626 bidi_unshelve_cache (save_data, 1);
8627 }
8628 else
8629 move_it_in_display_line_to (it, to_charpos, to_x, op);
8630 }
8631
8632
8633 /* Move IT forward until it satisfies one or more of the criteria in
8634 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8635
8636 OP is a bit-mask that specifies where to stop, and in particular,
8637 which of those four position arguments makes a difference. See the
8638 description of enum move_operation_enum.
8639
8640 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8641 screen line, this function will set IT to the next position that is
8642 displayed to the right of TO_CHARPOS on the screen. */
8643
8644 void
8645 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8646 {
8647 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8648 int line_height, line_start_x = 0, reached = 0;
8649 void *backup_data = NULL;
8650
8651 for (;;)
8652 {
8653 if (op & MOVE_TO_VPOS)
8654 {
8655 /* If no TO_CHARPOS and no TO_X specified, stop at the
8656 start of the line TO_VPOS. */
8657 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8658 {
8659 if (it->vpos == to_vpos)
8660 {
8661 reached = 1;
8662 break;
8663 }
8664 else
8665 skip = move_it_in_display_line_to (it, -1, -1, 0);
8666 }
8667 else
8668 {
8669 /* TO_VPOS >= 0 means stop at TO_X in the line at
8670 TO_VPOS, or at TO_POS, whichever comes first. */
8671 if (it->vpos == to_vpos)
8672 {
8673 reached = 2;
8674 break;
8675 }
8676
8677 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8678
8679 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8680 {
8681 reached = 3;
8682 break;
8683 }
8684 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8685 {
8686 /* We have reached TO_X but not in the line we want. */
8687 skip = move_it_in_display_line_to (it, to_charpos,
8688 -1, MOVE_TO_POS);
8689 if (skip == MOVE_POS_MATCH_OR_ZV)
8690 {
8691 reached = 4;
8692 break;
8693 }
8694 }
8695 }
8696 }
8697 else if (op & MOVE_TO_Y)
8698 {
8699 struct it it_backup;
8700
8701 if (it->line_wrap == WORD_WRAP)
8702 SAVE_IT (it_backup, *it, backup_data);
8703
8704 /* TO_Y specified means stop at TO_X in the line containing
8705 TO_Y---or at TO_CHARPOS if this is reached first. The
8706 problem is that we can't really tell whether the line
8707 contains TO_Y before we have completely scanned it, and
8708 this may skip past TO_X. What we do is to first scan to
8709 TO_X.
8710
8711 If TO_X is not specified, use a TO_X of zero. The reason
8712 is to make the outcome of this function more predictable.
8713 If we didn't use TO_X == 0, we would stop at the end of
8714 the line which is probably not what a caller would expect
8715 to happen. */
8716 skip = move_it_in_display_line_to
8717 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8718 (MOVE_TO_X | (op & MOVE_TO_POS)));
8719
8720 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8721 if (skip == MOVE_POS_MATCH_OR_ZV)
8722 reached = 5;
8723 else if (skip == MOVE_X_REACHED)
8724 {
8725 /* If TO_X was reached, we want to know whether TO_Y is
8726 in the line. We know this is the case if the already
8727 scanned glyphs make the line tall enough. Otherwise,
8728 we must check by scanning the rest of the line. */
8729 line_height = it->max_ascent + it->max_descent;
8730 if (to_y >= it->current_y
8731 && to_y < it->current_y + line_height)
8732 {
8733 reached = 6;
8734 break;
8735 }
8736 SAVE_IT (it_backup, *it, backup_data);
8737 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8738 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8739 op & MOVE_TO_POS);
8740 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8741 line_height = it->max_ascent + it->max_descent;
8742 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8743
8744 if (to_y >= it->current_y
8745 && to_y < it->current_y + line_height)
8746 {
8747 /* If TO_Y is in this line and TO_X was reached
8748 above, we scanned too far. We have to restore
8749 IT's settings to the ones before skipping. But
8750 keep the more accurate values of max_ascent and
8751 max_descent we've found while skipping the rest
8752 of the line, for the sake of callers, such as
8753 pos_visible_p, that need to know the line
8754 height. */
8755 int max_ascent = it->max_ascent;
8756 int max_descent = it->max_descent;
8757
8758 RESTORE_IT (it, &it_backup, backup_data);
8759 it->max_ascent = max_ascent;
8760 it->max_descent = max_descent;
8761 reached = 6;
8762 }
8763 else
8764 {
8765 skip = skip2;
8766 if (skip == MOVE_POS_MATCH_OR_ZV)
8767 reached = 7;
8768 }
8769 }
8770 else
8771 {
8772 /* Check whether TO_Y is in this line. */
8773 line_height = it->max_ascent + it->max_descent;
8774 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8775
8776 if (to_y >= it->current_y
8777 && to_y < it->current_y + line_height)
8778 {
8779 /* When word-wrap is on, TO_X may lie past the end
8780 of a wrapped line. Then it->current is the
8781 character on the next line, so backtrack to the
8782 space before the wrap point. */
8783 if (skip == MOVE_LINE_CONTINUED
8784 && it->line_wrap == WORD_WRAP)
8785 {
8786 int prev_x = max (it->current_x - 1, 0);
8787 RESTORE_IT (it, &it_backup, backup_data);
8788 skip = move_it_in_display_line_to
8789 (it, -1, prev_x, MOVE_TO_X);
8790 }
8791 reached = 6;
8792 }
8793 }
8794
8795 if (reached)
8796 break;
8797 }
8798 else if (BUFFERP (it->object)
8799 && (it->method == GET_FROM_BUFFER
8800 || it->method == GET_FROM_STRETCH)
8801 && IT_CHARPOS (*it) >= to_charpos
8802 /* Under bidi iteration, a call to set_iterator_to_next
8803 can scan far beyond to_charpos if the initial
8804 portion of the next line needs to be reordered. In
8805 that case, give move_it_in_display_line_to another
8806 chance below. */
8807 && !(it->bidi_p
8808 && it->bidi_it.scan_dir == -1))
8809 skip = MOVE_POS_MATCH_OR_ZV;
8810 else
8811 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8812
8813 switch (skip)
8814 {
8815 case MOVE_POS_MATCH_OR_ZV:
8816 reached = 8;
8817 goto out;
8818
8819 case MOVE_NEWLINE_OR_CR:
8820 set_iterator_to_next (it, 1);
8821 it->continuation_lines_width = 0;
8822 break;
8823
8824 case MOVE_LINE_TRUNCATED:
8825 it->continuation_lines_width = 0;
8826 reseat_at_next_visible_line_start (it, 0);
8827 if ((op & MOVE_TO_POS) != 0
8828 && IT_CHARPOS (*it) > to_charpos)
8829 {
8830 reached = 9;
8831 goto out;
8832 }
8833 break;
8834
8835 case MOVE_LINE_CONTINUED:
8836 /* For continued lines ending in a tab, some of the glyphs
8837 associated with the tab are displayed on the current
8838 line. Since it->current_x does not include these glyphs,
8839 we use it->last_visible_x instead. */
8840 if (it->c == '\t')
8841 {
8842 it->continuation_lines_width += it->last_visible_x;
8843 /* When moving by vpos, ensure that the iterator really
8844 advances to the next line (bug#847, bug#969). Fixme:
8845 do we need to do this in other circumstances? */
8846 if (it->current_x != it->last_visible_x
8847 && (op & MOVE_TO_VPOS)
8848 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8849 {
8850 line_start_x = it->current_x + it->pixel_width
8851 - it->last_visible_x;
8852 set_iterator_to_next (it, 0);
8853 }
8854 }
8855 else
8856 it->continuation_lines_width += it->current_x;
8857 break;
8858
8859 default:
8860 abort ();
8861 }
8862
8863 /* Reset/increment for the next run. */
8864 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8865 it->current_x = line_start_x;
8866 line_start_x = 0;
8867 it->hpos = 0;
8868 it->current_y += it->max_ascent + it->max_descent;
8869 ++it->vpos;
8870 last_height = it->max_ascent + it->max_descent;
8871 last_max_ascent = it->max_ascent;
8872 it->max_ascent = it->max_descent = 0;
8873 }
8874
8875 out:
8876
8877 /* On text terminals, we may stop at the end of a line in the middle
8878 of a multi-character glyph. If the glyph itself is continued,
8879 i.e. it is actually displayed on the next line, don't treat this
8880 stopping point as valid; move to the next line instead (unless
8881 that brings us offscreen). */
8882 if (!FRAME_WINDOW_P (it->f)
8883 && op & MOVE_TO_POS
8884 && IT_CHARPOS (*it) == to_charpos
8885 && it->what == IT_CHARACTER
8886 && it->nglyphs > 1
8887 && it->line_wrap == WINDOW_WRAP
8888 && it->current_x == it->last_visible_x - 1
8889 && it->c != '\n'
8890 && it->c != '\t'
8891 && it->vpos < XFASTINT (it->w->window_end_vpos))
8892 {
8893 it->continuation_lines_width += it->current_x;
8894 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8895 it->current_y += it->max_ascent + it->max_descent;
8896 ++it->vpos;
8897 last_height = it->max_ascent + it->max_descent;
8898 last_max_ascent = it->max_ascent;
8899 }
8900
8901 if (backup_data)
8902 bidi_unshelve_cache (backup_data, 1);
8903
8904 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8905 }
8906
8907
8908 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8909
8910 If DY > 0, move IT backward at least that many pixels. DY = 0
8911 means move IT backward to the preceding line start or BEGV. This
8912 function may move over more than DY pixels if IT->current_y - DY
8913 ends up in the middle of a line; in this case IT->current_y will be
8914 set to the top of the line moved to. */
8915
8916 void
8917 move_it_vertically_backward (struct it *it, int dy)
8918 {
8919 int nlines, h;
8920 struct it it2, it3;
8921 void *it2data = NULL, *it3data = NULL;
8922 ptrdiff_t start_pos;
8923
8924 move_further_back:
8925 eassert (dy >= 0);
8926
8927 start_pos = IT_CHARPOS (*it);
8928
8929 /* Estimate how many newlines we must move back. */
8930 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8931
8932 /* Set the iterator's position that many lines back. */
8933 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8934 back_to_previous_visible_line_start (it);
8935
8936 /* Reseat the iterator here. When moving backward, we don't want
8937 reseat to skip forward over invisible text, set up the iterator
8938 to deliver from overlay strings at the new position etc. So,
8939 use reseat_1 here. */
8940 reseat_1 (it, it->current.pos, 1);
8941
8942 /* We are now surely at a line start. */
8943 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8944 reordering is in effect. */
8945 it->continuation_lines_width = 0;
8946
8947 /* Move forward and see what y-distance we moved. First move to the
8948 start of the next line so that we get its height. We need this
8949 height to be able to tell whether we reached the specified
8950 y-distance. */
8951 SAVE_IT (it2, *it, it2data);
8952 it2.max_ascent = it2.max_descent = 0;
8953 do
8954 {
8955 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8956 MOVE_TO_POS | MOVE_TO_VPOS);
8957 }
8958 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8959 /* If we are in a display string which starts at START_POS,
8960 and that display string includes a newline, and we are
8961 right after that newline (i.e. at the beginning of a
8962 display line), exit the loop, because otherwise we will
8963 infloop, since move_it_to will see that it is already at
8964 START_POS and will not move. */
8965 || (it2.method == GET_FROM_STRING
8966 && IT_CHARPOS (it2) == start_pos
8967 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8968 eassert (IT_CHARPOS (*it) >= BEGV);
8969 SAVE_IT (it3, it2, it3data);
8970
8971 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8972 eassert (IT_CHARPOS (*it) >= BEGV);
8973 /* H is the actual vertical distance from the position in *IT
8974 and the starting position. */
8975 h = it2.current_y - it->current_y;
8976 /* NLINES is the distance in number of lines. */
8977 nlines = it2.vpos - it->vpos;
8978
8979 /* Correct IT's y and vpos position
8980 so that they are relative to the starting point. */
8981 it->vpos -= nlines;
8982 it->current_y -= h;
8983
8984 if (dy == 0)
8985 {
8986 /* DY == 0 means move to the start of the screen line. The
8987 value of nlines is > 0 if continuation lines were involved,
8988 or if the original IT position was at start of a line. */
8989 RESTORE_IT (it, it, it2data);
8990 if (nlines > 0)
8991 move_it_by_lines (it, nlines);
8992 /* The above code moves us to some position NLINES down,
8993 usually to its first glyph (leftmost in an L2R line), but
8994 that's not necessarily the start of the line, under bidi
8995 reordering. We want to get to the character position
8996 that is immediately after the newline of the previous
8997 line. */
8998 if (it->bidi_p
8999 && !it->continuation_lines_width
9000 && !STRINGP (it->string)
9001 && IT_CHARPOS (*it) > BEGV
9002 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9003 {
9004 ptrdiff_t nl_pos =
9005 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9006
9007 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9008 }
9009 bidi_unshelve_cache (it3data, 1);
9010 }
9011 else
9012 {
9013 /* The y-position we try to reach, relative to *IT.
9014 Note that H has been subtracted in front of the if-statement. */
9015 int target_y = it->current_y + h - dy;
9016 int y0 = it3.current_y;
9017 int y1;
9018 int line_height;
9019
9020 RESTORE_IT (&it3, &it3, it3data);
9021 y1 = line_bottom_y (&it3);
9022 line_height = y1 - y0;
9023 RESTORE_IT (it, it, it2data);
9024 /* If we did not reach target_y, try to move further backward if
9025 we can. If we moved too far backward, try to move forward. */
9026 if (target_y < it->current_y
9027 /* This is heuristic. In a window that's 3 lines high, with
9028 a line height of 13 pixels each, recentering with point
9029 on the bottom line will try to move -39/2 = 19 pixels
9030 backward. Try to avoid moving into the first line. */
9031 && (it->current_y - target_y
9032 > min (window_box_height (it->w), line_height * 2 / 3))
9033 && IT_CHARPOS (*it) > BEGV)
9034 {
9035 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9036 target_y - it->current_y));
9037 dy = it->current_y - target_y;
9038 goto move_further_back;
9039 }
9040 else if (target_y >= it->current_y + line_height
9041 && IT_CHARPOS (*it) < ZV)
9042 {
9043 /* Should move forward by at least one line, maybe more.
9044
9045 Note: Calling move_it_by_lines can be expensive on
9046 terminal frames, where compute_motion is used (via
9047 vmotion) to do the job, when there are very long lines
9048 and truncate-lines is nil. That's the reason for
9049 treating terminal frames specially here. */
9050
9051 if (!FRAME_WINDOW_P (it->f))
9052 move_it_vertically (it, target_y - (it->current_y + line_height));
9053 else
9054 {
9055 do
9056 {
9057 move_it_by_lines (it, 1);
9058 }
9059 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9060 }
9061 }
9062 }
9063 }
9064
9065
9066 /* Move IT by a specified amount of pixel lines DY. DY negative means
9067 move backwards. DY = 0 means move to start of screen line. At the
9068 end, IT will be on the start of a screen line. */
9069
9070 void
9071 move_it_vertically (struct it *it, int dy)
9072 {
9073 if (dy <= 0)
9074 move_it_vertically_backward (it, -dy);
9075 else
9076 {
9077 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9078 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9079 MOVE_TO_POS | MOVE_TO_Y);
9080 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9081
9082 /* If buffer ends in ZV without a newline, move to the start of
9083 the line to satisfy the post-condition. */
9084 if (IT_CHARPOS (*it) == ZV
9085 && ZV > BEGV
9086 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9087 move_it_by_lines (it, 0);
9088 }
9089 }
9090
9091
9092 /* Move iterator IT past the end of the text line it is in. */
9093
9094 void
9095 move_it_past_eol (struct it *it)
9096 {
9097 enum move_it_result rc;
9098
9099 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9100 if (rc == MOVE_NEWLINE_OR_CR)
9101 set_iterator_to_next (it, 0);
9102 }
9103
9104
9105 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9106 negative means move up. DVPOS == 0 means move to the start of the
9107 screen line.
9108
9109 Optimization idea: If we would know that IT->f doesn't use
9110 a face with proportional font, we could be faster for
9111 truncate-lines nil. */
9112
9113 void
9114 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9115 {
9116
9117 /* The commented-out optimization uses vmotion on terminals. This
9118 gives bad results, because elements like it->what, on which
9119 callers such as pos_visible_p rely, aren't updated. */
9120 /* struct position pos;
9121 if (!FRAME_WINDOW_P (it->f))
9122 {
9123 struct text_pos textpos;
9124
9125 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9126 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9127 reseat (it, textpos, 1);
9128 it->vpos += pos.vpos;
9129 it->current_y += pos.vpos;
9130 }
9131 else */
9132
9133 if (dvpos == 0)
9134 {
9135 /* DVPOS == 0 means move to the start of the screen line. */
9136 move_it_vertically_backward (it, 0);
9137 /* Let next call to line_bottom_y calculate real line height */
9138 last_height = 0;
9139 }
9140 else if (dvpos > 0)
9141 {
9142 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9143 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9144 {
9145 /* Only move to the next buffer position if we ended up in a
9146 string from display property, not in an overlay string
9147 (before-string or after-string). That is because the
9148 latter don't conceal the underlying buffer position, so
9149 we can ask to move the iterator to the exact position we
9150 are interested in. Note that, even if we are already at
9151 IT_CHARPOS (*it), the call below is not a no-op, as it
9152 will detect that we are at the end of the string, pop the
9153 iterator, and compute it->current_x and it->hpos
9154 correctly. */
9155 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9156 -1, -1, -1, MOVE_TO_POS);
9157 }
9158 }
9159 else
9160 {
9161 struct it it2;
9162 void *it2data = NULL;
9163 ptrdiff_t start_charpos, i;
9164
9165 /* Start at the beginning of the screen line containing IT's
9166 position. This may actually move vertically backwards,
9167 in case of overlays, so adjust dvpos accordingly. */
9168 dvpos += it->vpos;
9169 move_it_vertically_backward (it, 0);
9170 dvpos -= it->vpos;
9171
9172 /* Go back -DVPOS visible lines and reseat the iterator there. */
9173 start_charpos = IT_CHARPOS (*it);
9174 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9175 back_to_previous_visible_line_start (it);
9176 reseat (it, it->current.pos, 1);
9177
9178 /* Move further back if we end up in a string or an image. */
9179 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9180 {
9181 /* First try to move to start of display line. */
9182 dvpos += it->vpos;
9183 move_it_vertically_backward (it, 0);
9184 dvpos -= it->vpos;
9185 if (IT_POS_VALID_AFTER_MOVE_P (it))
9186 break;
9187 /* If start of line is still in string or image,
9188 move further back. */
9189 back_to_previous_visible_line_start (it);
9190 reseat (it, it->current.pos, 1);
9191 dvpos--;
9192 }
9193
9194 it->current_x = it->hpos = 0;
9195
9196 /* Above call may have moved too far if continuation lines
9197 are involved. Scan forward and see if it did. */
9198 SAVE_IT (it2, *it, it2data);
9199 it2.vpos = it2.current_y = 0;
9200 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9201 it->vpos -= it2.vpos;
9202 it->current_y -= it2.current_y;
9203 it->current_x = it->hpos = 0;
9204
9205 /* If we moved too far back, move IT some lines forward. */
9206 if (it2.vpos > -dvpos)
9207 {
9208 int delta = it2.vpos + dvpos;
9209
9210 RESTORE_IT (&it2, &it2, it2data);
9211 SAVE_IT (it2, *it, it2data);
9212 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9213 /* Move back again if we got too far ahead. */
9214 if (IT_CHARPOS (*it) >= start_charpos)
9215 RESTORE_IT (it, &it2, it2data);
9216 else
9217 bidi_unshelve_cache (it2data, 1);
9218 }
9219 else
9220 RESTORE_IT (it, it, it2data);
9221 }
9222 }
9223
9224 /* Return 1 if IT points into the middle of a display vector. */
9225
9226 int
9227 in_display_vector_p (struct it *it)
9228 {
9229 return (it->method == GET_FROM_DISPLAY_VECTOR
9230 && it->current.dpvec_index > 0
9231 && it->dpvec + it->current.dpvec_index != it->dpend);
9232 }
9233
9234 \f
9235 /***********************************************************************
9236 Messages
9237 ***********************************************************************/
9238
9239
9240 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9241 to *Messages*. */
9242
9243 void
9244 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9245 {
9246 Lisp_Object args[3];
9247 Lisp_Object msg, fmt;
9248 char *buffer;
9249 ptrdiff_t len;
9250 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9251 USE_SAFE_ALLOCA;
9252
9253 /* Do nothing if called asynchronously. Inserting text into
9254 a buffer may call after-change-functions and alike and
9255 that would means running Lisp asynchronously. */
9256 if (handling_signal)
9257 return;
9258
9259 fmt = msg = Qnil;
9260 GCPRO4 (fmt, msg, arg1, arg2);
9261
9262 args[0] = fmt = build_string (format);
9263 args[1] = arg1;
9264 args[2] = arg2;
9265 msg = Fformat (3, args);
9266
9267 len = SBYTES (msg) + 1;
9268 SAFE_ALLOCA (buffer, char *, len);
9269 memcpy (buffer, SDATA (msg), len);
9270
9271 message_dolog (buffer, len - 1, 1, 0);
9272 SAFE_FREE ();
9273
9274 UNGCPRO;
9275 }
9276
9277
9278 /* Output a newline in the *Messages* buffer if "needs" one. */
9279
9280 void
9281 message_log_maybe_newline (void)
9282 {
9283 if (message_log_need_newline)
9284 message_dolog ("", 0, 1, 0);
9285 }
9286
9287
9288 /* Add a string M of length NBYTES to the message log, optionally
9289 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9290 nonzero, means interpret the contents of M as multibyte. This
9291 function calls low-level routines in order to bypass text property
9292 hooks, etc. which might not be safe to run.
9293
9294 This may GC (insert may run before/after change hooks),
9295 so the buffer M must NOT point to a Lisp string. */
9296
9297 void
9298 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9299 {
9300 const unsigned char *msg = (const unsigned char *) m;
9301
9302 if (!NILP (Vmemory_full))
9303 return;
9304
9305 if (!NILP (Vmessage_log_max))
9306 {
9307 struct buffer *oldbuf;
9308 Lisp_Object oldpoint, oldbegv, oldzv;
9309 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9310 ptrdiff_t point_at_end = 0;
9311 ptrdiff_t zv_at_end = 0;
9312 Lisp_Object old_deactivate_mark, tem;
9313 struct gcpro gcpro1;
9314
9315 old_deactivate_mark = Vdeactivate_mark;
9316 oldbuf = current_buffer;
9317 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9318 BVAR (current_buffer, undo_list) = Qt;
9319
9320 oldpoint = message_dolog_marker1;
9321 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9322 oldbegv = message_dolog_marker2;
9323 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9324 oldzv = message_dolog_marker3;
9325 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9326 GCPRO1 (old_deactivate_mark);
9327
9328 if (PT == Z)
9329 point_at_end = 1;
9330 if (ZV == Z)
9331 zv_at_end = 1;
9332
9333 BEGV = BEG;
9334 BEGV_BYTE = BEG_BYTE;
9335 ZV = Z;
9336 ZV_BYTE = Z_BYTE;
9337 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9338
9339 /* Insert the string--maybe converting multibyte to single byte
9340 or vice versa, so that all the text fits the buffer. */
9341 if (multibyte
9342 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9343 {
9344 ptrdiff_t i;
9345 int c, char_bytes;
9346 char work[1];
9347
9348 /* Convert a multibyte string to single-byte
9349 for the *Message* buffer. */
9350 for (i = 0; i < nbytes; i += char_bytes)
9351 {
9352 c = string_char_and_length (msg + i, &char_bytes);
9353 work[0] = (ASCII_CHAR_P (c)
9354 ? c
9355 : multibyte_char_to_unibyte (c));
9356 insert_1_both (work, 1, 1, 1, 0, 0);
9357 }
9358 }
9359 else if (! multibyte
9360 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9361 {
9362 ptrdiff_t i;
9363 int c, char_bytes;
9364 unsigned char str[MAX_MULTIBYTE_LENGTH];
9365 /* Convert a single-byte string to multibyte
9366 for the *Message* buffer. */
9367 for (i = 0; i < nbytes; i++)
9368 {
9369 c = msg[i];
9370 MAKE_CHAR_MULTIBYTE (c);
9371 char_bytes = CHAR_STRING (c, str);
9372 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9373 }
9374 }
9375 else if (nbytes)
9376 insert_1 (m, nbytes, 1, 0, 0);
9377
9378 if (nlflag)
9379 {
9380 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9381 printmax_t dups;
9382 insert_1 ("\n", 1, 1, 0, 0);
9383
9384 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9385 this_bol = PT;
9386 this_bol_byte = PT_BYTE;
9387
9388 /* See if this line duplicates the previous one.
9389 If so, combine duplicates. */
9390 if (this_bol > BEG)
9391 {
9392 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9393 prev_bol = PT;
9394 prev_bol_byte = PT_BYTE;
9395
9396 dups = message_log_check_duplicate (prev_bol_byte,
9397 this_bol_byte);
9398 if (dups)
9399 {
9400 del_range_both (prev_bol, prev_bol_byte,
9401 this_bol, this_bol_byte, 0);
9402 if (dups > 1)
9403 {
9404 char dupstr[sizeof " [ times]"
9405 + INT_STRLEN_BOUND (printmax_t)];
9406
9407 /* If you change this format, don't forget to also
9408 change message_log_check_duplicate. */
9409 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9410 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9411 insert_1 (dupstr, duplen, 1, 0, 1);
9412 }
9413 }
9414 }
9415
9416 /* If we have more than the desired maximum number of lines
9417 in the *Messages* buffer now, delete the oldest ones.
9418 This is safe because we don't have undo in this buffer. */
9419
9420 if (NATNUMP (Vmessage_log_max))
9421 {
9422 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9423 -XFASTINT (Vmessage_log_max) - 1, 0);
9424 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9425 }
9426 }
9427 BEGV = XMARKER (oldbegv)->charpos;
9428 BEGV_BYTE = marker_byte_position (oldbegv);
9429
9430 if (zv_at_end)
9431 {
9432 ZV = Z;
9433 ZV_BYTE = Z_BYTE;
9434 }
9435 else
9436 {
9437 ZV = XMARKER (oldzv)->charpos;
9438 ZV_BYTE = marker_byte_position (oldzv);
9439 }
9440
9441 if (point_at_end)
9442 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9443 else
9444 /* We can't do Fgoto_char (oldpoint) because it will run some
9445 Lisp code. */
9446 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9447 XMARKER (oldpoint)->bytepos);
9448
9449 UNGCPRO;
9450 unchain_marker (XMARKER (oldpoint));
9451 unchain_marker (XMARKER (oldbegv));
9452 unchain_marker (XMARKER (oldzv));
9453
9454 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9455 set_buffer_internal (oldbuf);
9456 if (NILP (tem))
9457 windows_or_buffers_changed = old_windows_or_buffers_changed;
9458 message_log_need_newline = !nlflag;
9459 Vdeactivate_mark = old_deactivate_mark;
9460 }
9461 }
9462
9463
9464 /* We are at the end of the buffer after just having inserted a newline.
9465 (Note: We depend on the fact we won't be crossing the gap.)
9466 Check to see if the most recent message looks a lot like the previous one.
9467 Return 0 if different, 1 if the new one should just replace it, or a
9468 value N > 1 if we should also append " [N times]". */
9469
9470 static intmax_t
9471 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9472 {
9473 ptrdiff_t i;
9474 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9475 int seen_dots = 0;
9476 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9477 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9478
9479 for (i = 0; i < len; i++)
9480 {
9481 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9482 seen_dots = 1;
9483 if (p1[i] != p2[i])
9484 return seen_dots;
9485 }
9486 p1 += len;
9487 if (*p1 == '\n')
9488 return 2;
9489 if (*p1++ == ' ' && *p1++ == '[')
9490 {
9491 char *pend;
9492 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9493 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9494 return n+1;
9495 }
9496 return 0;
9497 }
9498 \f
9499
9500 /* Display an echo area message M with a specified length of NBYTES
9501 bytes. The string may include null characters. If M is 0, clear
9502 out any existing message, and let the mini-buffer text show
9503 through.
9504
9505 This may GC, so the buffer M must NOT point to a Lisp string. */
9506
9507 void
9508 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9509 {
9510 /* First flush out any partial line written with print. */
9511 message_log_maybe_newline ();
9512 if (m)
9513 message_dolog (m, nbytes, 1, multibyte);
9514 message2_nolog (m, nbytes, multibyte);
9515 }
9516
9517
9518 /* The non-logging counterpart of message2. */
9519
9520 void
9521 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9522 {
9523 struct frame *sf = SELECTED_FRAME ();
9524 message_enable_multibyte = multibyte;
9525
9526 if (FRAME_INITIAL_P (sf))
9527 {
9528 if (noninteractive_need_newline)
9529 putc ('\n', stderr);
9530 noninteractive_need_newline = 0;
9531 if (m)
9532 fwrite (m, nbytes, 1, stderr);
9533 if (cursor_in_echo_area == 0)
9534 fprintf (stderr, "\n");
9535 fflush (stderr);
9536 }
9537 /* A null message buffer means that the frame hasn't really been
9538 initialized yet. Error messages get reported properly by
9539 cmd_error, so this must be just an informative message; toss it. */
9540 else if (INTERACTIVE
9541 && sf->glyphs_initialized_p
9542 && FRAME_MESSAGE_BUF (sf))
9543 {
9544 Lisp_Object mini_window;
9545 struct frame *f;
9546
9547 /* Get the frame containing the mini-buffer
9548 that the selected frame is using. */
9549 mini_window = FRAME_MINIBUF_WINDOW (sf);
9550 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9551
9552 FRAME_SAMPLE_VISIBILITY (f);
9553 if (FRAME_VISIBLE_P (sf)
9554 && ! FRAME_VISIBLE_P (f))
9555 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9556
9557 if (m)
9558 {
9559 set_message (m, Qnil, nbytes, multibyte);
9560 if (minibuffer_auto_raise)
9561 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9562 }
9563 else
9564 clear_message (1, 1);
9565
9566 do_pending_window_change (0);
9567 echo_area_display (1);
9568 do_pending_window_change (0);
9569 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9570 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9571 }
9572 }
9573
9574
9575 /* Display an echo area message M with a specified length of NBYTES
9576 bytes. The string may include null characters. If M is not a
9577 string, clear out any existing message, and let the mini-buffer
9578 text show through.
9579
9580 This function cancels echoing. */
9581
9582 void
9583 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9584 {
9585 struct gcpro gcpro1;
9586
9587 GCPRO1 (m);
9588 clear_message (1,1);
9589 cancel_echoing ();
9590
9591 /* First flush out any partial line written with print. */
9592 message_log_maybe_newline ();
9593 if (STRINGP (m))
9594 {
9595 char *buffer;
9596 USE_SAFE_ALLOCA;
9597
9598 SAFE_ALLOCA (buffer, char *, nbytes);
9599 memcpy (buffer, SDATA (m), nbytes);
9600 message_dolog (buffer, nbytes, 1, multibyte);
9601 SAFE_FREE ();
9602 }
9603 message3_nolog (m, nbytes, multibyte);
9604
9605 UNGCPRO;
9606 }
9607
9608
9609 /* The non-logging version of message3.
9610 This does not cancel echoing, because it is used for echoing.
9611 Perhaps we need to make a separate function for echoing
9612 and make this cancel echoing. */
9613
9614 void
9615 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9616 {
9617 struct frame *sf = SELECTED_FRAME ();
9618 message_enable_multibyte = multibyte;
9619
9620 if (FRAME_INITIAL_P (sf))
9621 {
9622 if (noninteractive_need_newline)
9623 putc ('\n', stderr);
9624 noninteractive_need_newline = 0;
9625 if (STRINGP (m))
9626 fwrite (SDATA (m), nbytes, 1, stderr);
9627 if (cursor_in_echo_area == 0)
9628 fprintf (stderr, "\n");
9629 fflush (stderr);
9630 }
9631 /* A null message buffer means that the frame hasn't really been
9632 initialized yet. Error messages get reported properly by
9633 cmd_error, so this must be just an informative message; toss it. */
9634 else if (INTERACTIVE
9635 && sf->glyphs_initialized_p
9636 && FRAME_MESSAGE_BUF (sf))
9637 {
9638 Lisp_Object mini_window;
9639 Lisp_Object frame;
9640 struct frame *f;
9641
9642 /* Get the frame containing the mini-buffer
9643 that the selected frame is using. */
9644 mini_window = FRAME_MINIBUF_WINDOW (sf);
9645 frame = XWINDOW (mini_window)->frame;
9646 f = XFRAME (frame);
9647
9648 FRAME_SAMPLE_VISIBILITY (f);
9649 if (FRAME_VISIBLE_P (sf)
9650 && !FRAME_VISIBLE_P (f))
9651 Fmake_frame_visible (frame);
9652
9653 if (STRINGP (m) && SCHARS (m) > 0)
9654 {
9655 set_message (NULL, m, nbytes, multibyte);
9656 if (minibuffer_auto_raise)
9657 Fraise_frame (frame);
9658 /* Assume we are not echoing.
9659 (If we are, echo_now will override this.) */
9660 echo_message_buffer = Qnil;
9661 }
9662 else
9663 clear_message (1, 1);
9664
9665 do_pending_window_change (0);
9666 echo_area_display (1);
9667 do_pending_window_change (0);
9668 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9669 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9670 }
9671 }
9672
9673
9674 /* Display a null-terminated echo area message M. If M is 0, clear
9675 out any existing message, and let the mini-buffer text show through.
9676
9677 The buffer M must continue to exist until after the echo area gets
9678 cleared or some other message gets displayed there. Do not pass
9679 text that is stored in a Lisp string. Do not pass text in a buffer
9680 that was alloca'd. */
9681
9682 void
9683 message1 (const char *m)
9684 {
9685 message2 (m, (m ? strlen (m) : 0), 0);
9686 }
9687
9688
9689 /* The non-logging counterpart of message1. */
9690
9691 void
9692 message1_nolog (const char *m)
9693 {
9694 message2_nolog (m, (m ? strlen (m) : 0), 0);
9695 }
9696
9697 /* Display a message M which contains a single %s
9698 which gets replaced with STRING. */
9699
9700 void
9701 message_with_string (const char *m, Lisp_Object string, int log)
9702 {
9703 CHECK_STRING (string);
9704
9705 if (noninteractive)
9706 {
9707 if (m)
9708 {
9709 if (noninteractive_need_newline)
9710 putc ('\n', stderr);
9711 noninteractive_need_newline = 0;
9712 fprintf (stderr, m, SDATA (string));
9713 if (!cursor_in_echo_area)
9714 fprintf (stderr, "\n");
9715 fflush (stderr);
9716 }
9717 }
9718 else if (INTERACTIVE)
9719 {
9720 /* The frame whose minibuffer we're going to display the message on.
9721 It may be larger than the selected frame, so we need
9722 to use its buffer, not the selected frame's buffer. */
9723 Lisp_Object mini_window;
9724 struct frame *f, *sf = SELECTED_FRAME ();
9725
9726 /* Get the frame containing the minibuffer
9727 that the selected frame is using. */
9728 mini_window = FRAME_MINIBUF_WINDOW (sf);
9729 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9730
9731 /* A null message buffer means that the frame hasn't really been
9732 initialized yet. Error messages get reported properly by
9733 cmd_error, so this must be just an informative message; toss it. */
9734 if (FRAME_MESSAGE_BUF (f))
9735 {
9736 Lisp_Object args[2], msg;
9737 struct gcpro gcpro1, gcpro2;
9738
9739 args[0] = build_string (m);
9740 args[1] = msg = string;
9741 GCPRO2 (args[0], msg);
9742 gcpro1.nvars = 2;
9743
9744 msg = Fformat (2, args);
9745
9746 if (log)
9747 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9748 else
9749 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9750
9751 UNGCPRO;
9752
9753 /* Print should start at the beginning of the message
9754 buffer next time. */
9755 message_buf_print = 0;
9756 }
9757 }
9758 }
9759
9760
9761 /* Dump an informative message to the minibuf. If M is 0, clear out
9762 any existing message, and let the mini-buffer text show through. */
9763
9764 static void
9765 vmessage (const char *m, va_list ap)
9766 {
9767 if (noninteractive)
9768 {
9769 if (m)
9770 {
9771 if (noninteractive_need_newline)
9772 putc ('\n', stderr);
9773 noninteractive_need_newline = 0;
9774 vfprintf (stderr, m, ap);
9775 if (cursor_in_echo_area == 0)
9776 fprintf (stderr, "\n");
9777 fflush (stderr);
9778 }
9779 }
9780 else if (INTERACTIVE)
9781 {
9782 /* The frame whose mini-buffer we're going to display the message
9783 on. It may be larger than the selected frame, so we need to
9784 use its buffer, not the selected frame's buffer. */
9785 Lisp_Object mini_window;
9786 struct frame *f, *sf = SELECTED_FRAME ();
9787
9788 /* Get the frame containing the mini-buffer
9789 that the selected frame is using. */
9790 mini_window = FRAME_MINIBUF_WINDOW (sf);
9791 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9792
9793 /* A null message buffer means that the frame hasn't really been
9794 initialized yet. Error messages get reported properly by
9795 cmd_error, so this must be just an informative message; toss
9796 it. */
9797 if (FRAME_MESSAGE_BUF (f))
9798 {
9799 if (m)
9800 {
9801 ptrdiff_t len;
9802
9803 len = doprnt (FRAME_MESSAGE_BUF (f),
9804 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9805
9806 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9807 }
9808 else
9809 message1 (0);
9810
9811 /* Print should start at the beginning of the message
9812 buffer next time. */
9813 message_buf_print = 0;
9814 }
9815 }
9816 }
9817
9818 void
9819 message (const char *m, ...)
9820 {
9821 va_list ap;
9822 va_start (ap, m);
9823 vmessage (m, ap);
9824 va_end (ap);
9825 }
9826
9827
9828 #if 0
9829 /* The non-logging version of message. */
9830
9831 void
9832 message_nolog (const char *m, ...)
9833 {
9834 Lisp_Object old_log_max;
9835 va_list ap;
9836 va_start (ap, m);
9837 old_log_max = Vmessage_log_max;
9838 Vmessage_log_max = Qnil;
9839 vmessage (m, ap);
9840 Vmessage_log_max = old_log_max;
9841 va_end (ap);
9842 }
9843 #endif
9844
9845
9846 /* Display the current message in the current mini-buffer. This is
9847 only called from error handlers in process.c, and is not time
9848 critical. */
9849
9850 void
9851 update_echo_area (void)
9852 {
9853 if (!NILP (echo_area_buffer[0]))
9854 {
9855 Lisp_Object string;
9856 string = Fcurrent_message ();
9857 message3 (string, SBYTES (string),
9858 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9859 }
9860 }
9861
9862
9863 /* Make sure echo area buffers in `echo_buffers' are live.
9864 If they aren't, make new ones. */
9865
9866 static void
9867 ensure_echo_area_buffers (void)
9868 {
9869 int i;
9870
9871 for (i = 0; i < 2; ++i)
9872 if (!BUFFERP (echo_buffer[i])
9873 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9874 {
9875 char name[30];
9876 Lisp_Object old_buffer;
9877 int j;
9878
9879 old_buffer = echo_buffer[i];
9880 echo_buffer[i] = Fget_buffer_create
9881 (make_formatted_string (name, " *Echo Area %d*", i));
9882 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9883 /* to force word wrap in echo area -
9884 it was decided to postpone this*/
9885 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9886
9887 for (j = 0; j < 2; ++j)
9888 if (EQ (old_buffer, echo_area_buffer[j]))
9889 echo_area_buffer[j] = echo_buffer[i];
9890 }
9891 }
9892
9893
9894 /* Call FN with args A1..A4 with either the current or last displayed
9895 echo_area_buffer as current buffer.
9896
9897 WHICH zero means use the current message buffer
9898 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9899 from echo_buffer[] and clear it.
9900
9901 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9902 suitable buffer from echo_buffer[] and clear it.
9903
9904 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9905 that the current message becomes the last displayed one, make
9906 choose a suitable buffer for echo_area_buffer[0], and clear it.
9907
9908 Value is what FN returns. */
9909
9910 static int
9911 with_echo_area_buffer (struct window *w, int which,
9912 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9913 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9914 {
9915 Lisp_Object buffer;
9916 int this_one, the_other, clear_buffer_p, rc;
9917 ptrdiff_t count = SPECPDL_INDEX ();
9918
9919 /* If buffers aren't live, make new ones. */
9920 ensure_echo_area_buffers ();
9921
9922 clear_buffer_p = 0;
9923
9924 if (which == 0)
9925 this_one = 0, the_other = 1;
9926 else if (which > 0)
9927 this_one = 1, the_other = 0;
9928 else
9929 {
9930 this_one = 0, the_other = 1;
9931 clear_buffer_p = 1;
9932
9933 /* We need a fresh one in case the current echo buffer equals
9934 the one containing the last displayed echo area message. */
9935 if (!NILP (echo_area_buffer[this_one])
9936 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9937 echo_area_buffer[this_one] = Qnil;
9938 }
9939
9940 /* Choose a suitable buffer from echo_buffer[] is we don't
9941 have one. */
9942 if (NILP (echo_area_buffer[this_one]))
9943 {
9944 echo_area_buffer[this_one]
9945 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9946 ? echo_buffer[the_other]
9947 : echo_buffer[this_one]);
9948 clear_buffer_p = 1;
9949 }
9950
9951 buffer = echo_area_buffer[this_one];
9952
9953 /* Don't get confused by reusing the buffer used for echoing
9954 for a different purpose. */
9955 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9956 cancel_echoing ();
9957
9958 record_unwind_protect (unwind_with_echo_area_buffer,
9959 with_echo_area_buffer_unwind_data (w));
9960
9961 /* Make the echo area buffer current. Note that for display
9962 purposes, it is not necessary that the displayed window's buffer
9963 == current_buffer, except for text property lookup. So, let's
9964 only set that buffer temporarily here without doing a full
9965 Fset_window_buffer. We must also change w->pointm, though,
9966 because otherwise an assertions in unshow_buffer fails, and Emacs
9967 aborts. */
9968 set_buffer_internal_1 (XBUFFER (buffer));
9969 if (w)
9970 {
9971 w->buffer = buffer;
9972 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9973 }
9974
9975 BVAR (current_buffer, undo_list) = Qt;
9976 BVAR (current_buffer, read_only) = Qnil;
9977 specbind (Qinhibit_read_only, Qt);
9978 specbind (Qinhibit_modification_hooks, Qt);
9979
9980 if (clear_buffer_p && Z > BEG)
9981 del_range (BEG, Z);
9982
9983 eassert (BEGV >= BEG);
9984 eassert (ZV <= Z && ZV >= BEGV);
9985
9986 rc = fn (a1, a2, a3, a4);
9987
9988 eassert (BEGV >= BEG);
9989 eassert (ZV <= Z && ZV >= BEGV);
9990
9991 unbind_to (count, Qnil);
9992 return rc;
9993 }
9994
9995
9996 /* Save state that should be preserved around the call to the function
9997 FN called in with_echo_area_buffer. */
9998
9999 static Lisp_Object
10000 with_echo_area_buffer_unwind_data (struct window *w)
10001 {
10002 int i = 0;
10003 Lisp_Object vector, tmp;
10004
10005 /* Reduce consing by keeping one vector in
10006 Vwith_echo_area_save_vector. */
10007 vector = Vwith_echo_area_save_vector;
10008 Vwith_echo_area_save_vector = Qnil;
10009
10010 if (NILP (vector))
10011 vector = Fmake_vector (make_number (7), Qnil);
10012
10013 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10014 ASET (vector, i, Vdeactivate_mark); ++i;
10015 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10016
10017 if (w)
10018 {
10019 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10020 ASET (vector, i, w->buffer); ++i;
10021 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10022 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10023 }
10024 else
10025 {
10026 int end = i + 4;
10027 for (; i < end; ++i)
10028 ASET (vector, i, Qnil);
10029 }
10030
10031 eassert (i == ASIZE (vector));
10032 return vector;
10033 }
10034
10035
10036 /* Restore global state from VECTOR which was created by
10037 with_echo_area_buffer_unwind_data. */
10038
10039 static Lisp_Object
10040 unwind_with_echo_area_buffer (Lisp_Object vector)
10041 {
10042 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10043 Vdeactivate_mark = AREF (vector, 1);
10044 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10045
10046 if (WINDOWP (AREF (vector, 3)))
10047 {
10048 struct window *w;
10049 Lisp_Object buffer, charpos, bytepos;
10050
10051 w = XWINDOW (AREF (vector, 3));
10052 buffer = AREF (vector, 4);
10053 charpos = AREF (vector, 5);
10054 bytepos = AREF (vector, 6);
10055
10056 w->buffer = buffer;
10057 set_marker_both (w->pointm, buffer,
10058 XFASTINT (charpos), XFASTINT (bytepos));
10059 }
10060
10061 Vwith_echo_area_save_vector = vector;
10062 return Qnil;
10063 }
10064
10065
10066 /* Set up the echo area for use by print functions. MULTIBYTE_P
10067 non-zero means we will print multibyte. */
10068
10069 void
10070 setup_echo_area_for_printing (int multibyte_p)
10071 {
10072 /* If we can't find an echo area any more, exit. */
10073 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10074 Fkill_emacs (Qnil);
10075
10076 ensure_echo_area_buffers ();
10077
10078 if (!message_buf_print)
10079 {
10080 /* A message has been output since the last time we printed.
10081 Choose a fresh echo area buffer. */
10082 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10083 echo_area_buffer[0] = echo_buffer[1];
10084 else
10085 echo_area_buffer[0] = echo_buffer[0];
10086
10087 /* Switch to that buffer and clear it. */
10088 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10089 BVAR (current_buffer, truncate_lines) = Qnil;
10090
10091 if (Z > BEG)
10092 {
10093 ptrdiff_t count = SPECPDL_INDEX ();
10094 specbind (Qinhibit_read_only, Qt);
10095 /* Note that undo recording is always disabled. */
10096 del_range (BEG, Z);
10097 unbind_to (count, Qnil);
10098 }
10099 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10100
10101 /* Set up the buffer for the multibyteness we need. */
10102 if (multibyte_p
10103 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10104 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10105
10106 /* Raise the frame containing the echo area. */
10107 if (minibuffer_auto_raise)
10108 {
10109 struct frame *sf = SELECTED_FRAME ();
10110 Lisp_Object mini_window;
10111 mini_window = FRAME_MINIBUF_WINDOW (sf);
10112 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10113 }
10114
10115 message_log_maybe_newline ();
10116 message_buf_print = 1;
10117 }
10118 else
10119 {
10120 if (NILP (echo_area_buffer[0]))
10121 {
10122 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10123 echo_area_buffer[0] = echo_buffer[1];
10124 else
10125 echo_area_buffer[0] = echo_buffer[0];
10126 }
10127
10128 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10129 {
10130 /* Someone switched buffers between print requests. */
10131 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10132 BVAR (current_buffer, truncate_lines) = Qnil;
10133 }
10134 }
10135 }
10136
10137
10138 /* Display an echo area message in window W. Value is non-zero if W's
10139 height is changed. If display_last_displayed_message_p is
10140 non-zero, display the message that was last displayed, otherwise
10141 display the current message. */
10142
10143 static int
10144 display_echo_area (struct window *w)
10145 {
10146 int i, no_message_p, window_height_changed_p;
10147
10148 /* Temporarily disable garbage collections while displaying the echo
10149 area. This is done because a GC can print a message itself.
10150 That message would modify the echo area buffer's contents while a
10151 redisplay of the buffer is going on, and seriously confuse
10152 redisplay. */
10153 ptrdiff_t count = inhibit_garbage_collection ();
10154
10155 /* If there is no message, we must call display_echo_area_1
10156 nevertheless because it resizes the window. But we will have to
10157 reset the echo_area_buffer in question to nil at the end because
10158 with_echo_area_buffer will sets it to an empty buffer. */
10159 i = display_last_displayed_message_p ? 1 : 0;
10160 no_message_p = NILP (echo_area_buffer[i]);
10161
10162 window_height_changed_p
10163 = with_echo_area_buffer (w, display_last_displayed_message_p,
10164 display_echo_area_1,
10165 (intptr_t) w, Qnil, 0, 0);
10166
10167 if (no_message_p)
10168 echo_area_buffer[i] = Qnil;
10169
10170 unbind_to (count, Qnil);
10171 return window_height_changed_p;
10172 }
10173
10174
10175 /* Helper for display_echo_area. Display the current buffer which
10176 contains the current echo area message in window W, a mini-window,
10177 a pointer to which is passed in A1. A2..A4 are currently not used.
10178 Change the height of W so that all of the message is displayed.
10179 Value is non-zero if height of W was changed. */
10180
10181 static int
10182 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10183 {
10184 intptr_t i1 = a1;
10185 struct window *w = (struct window *) i1;
10186 Lisp_Object window;
10187 struct text_pos start;
10188 int window_height_changed_p = 0;
10189
10190 /* Do this before displaying, so that we have a large enough glyph
10191 matrix for the display. If we can't get enough space for the
10192 whole text, display the last N lines. That works by setting w->start. */
10193 window_height_changed_p = resize_mini_window (w, 0);
10194
10195 /* Use the starting position chosen by resize_mini_window. */
10196 SET_TEXT_POS_FROM_MARKER (start, w->start);
10197
10198 /* Display. */
10199 clear_glyph_matrix (w->desired_matrix);
10200 XSETWINDOW (window, w);
10201 try_window (window, start, 0);
10202
10203 return window_height_changed_p;
10204 }
10205
10206
10207 /* Resize the echo area window to exactly the size needed for the
10208 currently displayed message, if there is one. If a mini-buffer
10209 is active, don't shrink it. */
10210
10211 void
10212 resize_echo_area_exactly (void)
10213 {
10214 if (BUFFERP (echo_area_buffer[0])
10215 && WINDOWP (echo_area_window))
10216 {
10217 struct window *w = XWINDOW (echo_area_window);
10218 int resized_p;
10219 Lisp_Object resize_exactly;
10220
10221 if (minibuf_level == 0)
10222 resize_exactly = Qt;
10223 else
10224 resize_exactly = Qnil;
10225
10226 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10227 (intptr_t) w, resize_exactly,
10228 0, 0);
10229 if (resized_p)
10230 {
10231 ++windows_or_buffers_changed;
10232 ++update_mode_lines;
10233 redisplay_internal ();
10234 }
10235 }
10236 }
10237
10238
10239 /* Callback function for with_echo_area_buffer, when used from
10240 resize_echo_area_exactly. A1 contains a pointer to the window to
10241 resize, EXACTLY non-nil means resize the mini-window exactly to the
10242 size of the text displayed. A3 and A4 are not used. Value is what
10243 resize_mini_window returns. */
10244
10245 static int
10246 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10247 {
10248 intptr_t i1 = a1;
10249 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10250 }
10251
10252
10253 /* Resize mini-window W to fit the size of its contents. EXACT_P
10254 means size the window exactly to the size needed. Otherwise, it's
10255 only enlarged until W's buffer is empty.
10256
10257 Set W->start to the right place to begin display. If the whole
10258 contents fit, start at the beginning. Otherwise, start so as
10259 to make the end of the contents appear. This is particularly
10260 important for y-or-n-p, but seems desirable generally.
10261
10262 Value is non-zero if the window height has been changed. */
10263
10264 int
10265 resize_mini_window (struct window *w, int exact_p)
10266 {
10267 struct frame *f = XFRAME (w->frame);
10268 int window_height_changed_p = 0;
10269
10270 eassert (MINI_WINDOW_P (w));
10271
10272 /* By default, start display at the beginning. */
10273 set_marker_both (w->start, w->buffer,
10274 BUF_BEGV (XBUFFER (w->buffer)),
10275 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10276
10277 /* Don't resize windows while redisplaying a window; it would
10278 confuse redisplay functions when the size of the window they are
10279 displaying changes from under them. Such a resizing can happen,
10280 for instance, when which-func prints a long message while
10281 we are running fontification-functions. We're running these
10282 functions with safe_call which binds inhibit-redisplay to t. */
10283 if (!NILP (Vinhibit_redisplay))
10284 return 0;
10285
10286 /* Nil means don't try to resize. */
10287 if (NILP (Vresize_mini_windows)
10288 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10289 return 0;
10290
10291 if (!FRAME_MINIBUF_ONLY_P (f))
10292 {
10293 struct it it;
10294 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10295 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10296 int height;
10297 EMACS_INT max_height;
10298 int unit = FRAME_LINE_HEIGHT (f);
10299 struct text_pos start;
10300 struct buffer *old_current_buffer = NULL;
10301
10302 if (current_buffer != XBUFFER (w->buffer))
10303 {
10304 old_current_buffer = current_buffer;
10305 set_buffer_internal (XBUFFER (w->buffer));
10306 }
10307
10308 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10309
10310 /* Compute the max. number of lines specified by the user. */
10311 if (FLOATP (Vmax_mini_window_height))
10312 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10313 else if (INTEGERP (Vmax_mini_window_height))
10314 max_height = XINT (Vmax_mini_window_height);
10315 else
10316 max_height = total_height / 4;
10317
10318 /* Correct that max. height if it's bogus. */
10319 max_height = max (1, max_height);
10320 max_height = min (total_height, max_height);
10321
10322 /* Find out the height of the text in the window. */
10323 if (it.line_wrap == TRUNCATE)
10324 height = 1;
10325 else
10326 {
10327 last_height = 0;
10328 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10329 if (it.max_ascent == 0 && it.max_descent == 0)
10330 height = it.current_y + last_height;
10331 else
10332 height = it.current_y + it.max_ascent + it.max_descent;
10333 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10334 height = (height + unit - 1) / unit;
10335 }
10336
10337 /* Compute a suitable window start. */
10338 if (height > max_height)
10339 {
10340 height = max_height;
10341 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10342 move_it_vertically_backward (&it, (height - 1) * unit);
10343 start = it.current.pos;
10344 }
10345 else
10346 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10347 SET_MARKER_FROM_TEXT_POS (w->start, start);
10348
10349 if (EQ (Vresize_mini_windows, Qgrow_only))
10350 {
10351 /* Let it grow only, until we display an empty message, in which
10352 case the window shrinks again. */
10353 if (height > WINDOW_TOTAL_LINES (w))
10354 {
10355 int old_height = WINDOW_TOTAL_LINES (w);
10356 freeze_window_starts (f, 1);
10357 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10358 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10359 }
10360 else if (height < WINDOW_TOTAL_LINES (w)
10361 && (exact_p || BEGV == ZV))
10362 {
10363 int old_height = WINDOW_TOTAL_LINES (w);
10364 freeze_window_starts (f, 0);
10365 shrink_mini_window (w);
10366 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10367 }
10368 }
10369 else
10370 {
10371 /* Always resize to exact size needed. */
10372 if (height > WINDOW_TOTAL_LINES (w))
10373 {
10374 int old_height = WINDOW_TOTAL_LINES (w);
10375 freeze_window_starts (f, 1);
10376 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10377 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10378 }
10379 else if (height < WINDOW_TOTAL_LINES (w))
10380 {
10381 int old_height = WINDOW_TOTAL_LINES (w);
10382 freeze_window_starts (f, 0);
10383 shrink_mini_window (w);
10384
10385 if (height)
10386 {
10387 freeze_window_starts (f, 1);
10388 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10389 }
10390
10391 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10392 }
10393 }
10394
10395 if (old_current_buffer)
10396 set_buffer_internal (old_current_buffer);
10397 }
10398
10399 return window_height_changed_p;
10400 }
10401
10402
10403 /* Value is the current message, a string, or nil if there is no
10404 current message. */
10405
10406 Lisp_Object
10407 current_message (void)
10408 {
10409 Lisp_Object msg;
10410
10411 if (!BUFFERP (echo_area_buffer[0]))
10412 msg = Qnil;
10413 else
10414 {
10415 with_echo_area_buffer (0, 0, current_message_1,
10416 (intptr_t) &msg, Qnil, 0, 0);
10417 if (NILP (msg))
10418 echo_area_buffer[0] = Qnil;
10419 }
10420
10421 return msg;
10422 }
10423
10424
10425 static int
10426 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10427 {
10428 intptr_t i1 = a1;
10429 Lisp_Object *msg = (Lisp_Object *) i1;
10430
10431 if (Z > BEG)
10432 *msg = make_buffer_string (BEG, Z, 1);
10433 else
10434 *msg = Qnil;
10435 return 0;
10436 }
10437
10438
10439 /* Push the current message on Vmessage_stack for later restoration
10440 by restore_message. Value is non-zero if the current message isn't
10441 empty. This is a relatively infrequent operation, so it's not
10442 worth optimizing. */
10443
10444 int
10445 push_message (void)
10446 {
10447 Lisp_Object msg;
10448 msg = current_message ();
10449 Vmessage_stack = Fcons (msg, Vmessage_stack);
10450 return STRINGP (msg);
10451 }
10452
10453
10454 /* Restore message display from the top of Vmessage_stack. */
10455
10456 void
10457 restore_message (void)
10458 {
10459 Lisp_Object msg;
10460
10461 eassert (CONSP (Vmessage_stack));
10462 msg = XCAR (Vmessage_stack);
10463 if (STRINGP (msg))
10464 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10465 else
10466 message3_nolog (msg, 0, 0);
10467 }
10468
10469
10470 /* Handler for record_unwind_protect calling pop_message. */
10471
10472 Lisp_Object
10473 pop_message_unwind (Lisp_Object dummy)
10474 {
10475 pop_message ();
10476 return Qnil;
10477 }
10478
10479 /* Pop the top-most entry off Vmessage_stack. */
10480
10481 static void
10482 pop_message (void)
10483 {
10484 eassert (CONSP (Vmessage_stack));
10485 Vmessage_stack = XCDR (Vmessage_stack);
10486 }
10487
10488
10489 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10490 exits. If the stack is not empty, we have a missing pop_message
10491 somewhere. */
10492
10493 void
10494 check_message_stack (void)
10495 {
10496 if (!NILP (Vmessage_stack))
10497 abort ();
10498 }
10499
10500
10501 /* Truncate to NCHARS what will be displayed in the echo area the next
10502 time we display it---but don't redisplay it now. */
10503
10504 void
10505 truncate_echo_area (ptrdiff_t nchars)
10506 {
10507 if (nchars == 0)
10508 echo_area_buffer[0] = Qnil;
10509 /* A null message buffer means that the frame hasn't really been
10510 initialized yet. Error messages get reported properly by
10511 cmd_error, so this must be just an informative message; toss it. */
10512 else if (!noninteractive
10513 && INTERACTIVE
10514 && !NILP (echo_area_buffer[0]))
10515 {
10516 struct frame *sf = SELECTED_FRAME ();
10517 if (FRAME_MESSAGE_BUF (sf))
10518 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10519 }
10520 }
10521
10522
10523 /* Helper function for truncate_echo_area. Truncate the current
10524 message to at most NCHARS characters. */
10525
10526 static int
10527 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10528 {
10529 if (BEG + nchars < Z)
10530 del_range (BEG + nchars, Z);
10531 if (Z == BEG)
10532 echo_area_buffer[0] = Qnil;
10533 return 0;
10534 }
10535
10536
10537 /* Set the current message to a substring of S or STRING.
10538
10539 If STRING is a Lisp string, set the message to the first NBYTES
10540 bytes from STRING. NBYTES zero means use the whole string. If
10541 STRING is multibyte, the message will be displayed multibyte.
10542
10543 If S is not null, set the message to the first LEN bytes of S. LEN
10544 zero means use the whole string. MULTIBYTE_P non-zero means S is
10545 multibyte. Display the message multibyte in that case.
10546
10547 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10548 to t before calling set_message_1 (which calls insert).
10549 */
10550
10551 static void
10552 set_message (const char *s, Lisp_Object string,
10553 ptrdiff_t nbytes, int multibyte_p)
10554 {
10555 message_enable_multibyte
10556 = ((s && multibyte_p)
10557 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10558
10559 with_echo_area_buffer (0, -1, set_message_1,
10560 (intptr_t) s, string, nbytes, multibyte_p);
10561 message_buf_print = 0;
10562 help_echo_showing_p = 0;
10563 }
10564
10565
10566 /* Helper function for set_message. Arguments have the same meaning
10567 as there, with A1 corresponding to S and A2 corresponding to STRING
10568 This function is called with the echo area buffer being
10569 current. */
10570
10571 static int
10572 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10573 {
10574 intptr_t i1 = a1;
10575 const char *s = (const char *) i1;
10576 const unsigned char *msg = (const unsigned char *) s;
10577 Lisp_Object string = a2;
10578
10579 /* Change multibyteness of the echo buffer appropriately. */
10580 if (message_enable_multibyte
10581 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10582 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10583
10584 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10585 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10586 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10587
10588 /* Insert new message at BEG. */
10589 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10590
10591 if (STRINGP (string))
10592 {
10593 ptrdiff_t nchars;
10594
10595 if (nbytes == 0)
10596 nbytes = SBYTES (string);
10597 nchars = string_byte_to_char (string, nbytes);
10598
10599 /* This function takes care of single/multibyte conversion. We
10600 just have to ensure that the echo area buffer has the right
10601 setting of enable_multibyte_characters. */
10602 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10603 }
10604 else if (s)
10605 {
10606 if (nbytes == 0)
10607 nbytes = strlen (s);
10608
10609 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10610 {
10611 /* Convert from multi-byte to single-byte. */
10612 ptrdiff_t i;
10613 int c, n;
10614 char work[1];
10615
10616 /* Convert a multibyte string to single-byte. */
10617 for (i = 0; i < nbytes; i += n)
10618 {
10619 c = string_char_and_length (msg + i, &n);
10620 work[0] = (ASCII_CHAR_P (c)
10621 ? c
10622 : multibyte_char_to_unibyte (c));
10623 insert_1_both (work, 1, 1, 1, 0, 0);
10624 }
10625 }
10626 else if (!multibyte_p
10627 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10628 {
10629 /* Convert from single-byte to multi-byte. */
10630 ptrdiff_t i;
10631 int c, n;
10632 unsigned char str[MAX_MULTIBYTE_LENGTH];
10633
10634 /* Convert a single-byte string to multibyte. */
10635 for (i = 0; i < nbytes; i++)
10636 {
10637 c = msg[i];
10638 MAKE_CHAR_MULTIBYTE (c);
10639 n = CHAR_STRING (c, str);
10640 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10641 }
10642 }
10643 else
10644 insert_1 (s, nbytes, 1, 0, 0);
10645 }
10646
10647 return 0;
10648 }
10649
10650
10651 /* Clear messages. CURRENT_P non-zero means clear the current
10652 message. LAST_DISPLAYED_P non-zero means clear the message
10653 last displayed. */
10654
10655 void
10656 clear_message (int current_p, int last_displayed_p)
10657 {
10658 if (current_p)
10659 {
10660 echo_area_buffer[0] = Qnil;
10661 message_cleared_p = 1;
10662 }
10663
10664 if (last_displayed_p)
10665 echo_area_buffer[1] = Qnil;
10666
10667 message_buf_print = 0;
10668 }
10669
10670 /* Clear garbaged frames.
10671
10672 This function is used where the old redisplay called
10673 redraw_garbaged_frames which in turn called redraw_frame which in
10674 turn called clear_frame. The call to clear_frame was a source of
10675 flickering. I believe a clear_frame is not necessary. It should
10676 suffice in the new redisplay to invalidate all current matrices,
10677 and ensure a complete redisplay of all windows. */
10678
10679 static void
10680 clear_garbaged_frames (void)
10681 {
10682 if (frame_garbaged)
10683 {
10684 Lisp_Object tail, frame;
10685 int changed_count = 0;
10686
10687 FOR_EACH_FRAME (tail, frame)
10688 {
10689 struct frame *f = XFRAME (frame);
10690
10691 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10692 {
10693 if (f->resized_p)
10694 {
10695 Fredraw_frame (frame);
10696 f->force_flush_display_p = 1;
10697 }
10698 clear_current_matrices (f);
10699 changed_count++;
10700 f->garbaged = 0;
10701 f->resized_p = 0;
10702 }
10703 }
10704
10705 frame_garbaged = 0;
10706 if (changed_count)
10707 ++windows_or_buffers_changed;
10708 }
10709 }
10710
10711
10712 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10713 is non-zero update selected_frame. Value is non-zero if the
10714 mini-windows height has been changed. */
10715
10716 static int
10717 echo_area_display (int update_frame_p)
10718 {
10719 Lisp_Object mini_window;
10720 struct window *w;
10721 struct frame *f;
10722 int window_height_changed_p = 0;
10723 struct frame *sf = SELECTED_FRAME ();
10724
10725 mini_window = FRAME_MINIBUF_WINDOW (sf);
10726 w = XWINDOW (mini_window);
10727 f = XFRAME (WINDOW_FRAME (w));
10728
10729 /* Don't display if frame is invisible or not yet initialized. */
10730 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10731 return 0;
10732
10733 #ifdef HAVE_WINDOW_SYSTEM
10734 /* When Emacs starts, selected_frame may be the initial terminal
10735 frame. If we let this through, a message would be displayed on
10736 the terminal. */
10737 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10738 return 0;
10739 #endif /* HAVE_WINDOW_SYSTEM */
10740
10741 /* Redraw garbaged frames. */
10742 if (frame_garbaged)
10743 clear_garbaged_frames ();
10744
10745 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10746 {
10747 echo_area_window = mini_window;
10748 window_height_changed_p = display_echo_area (w);
10749 w->must_be_updated_p = 1;
10750
10751 /* Update the display, unless called from redisplay_internal.
10752 Also don't update the screen during redisplay itself. The
10753 update will happen at the end of redisplay, and an update
10754 here could cause confusion. */
10755 if (update_frame_p && !redisplaying_p)
10756 {
10757 int n = 0;
10758
10759 /* If the display update has been interrupted by pending
10760 input, update mode lines in the frame. Due to the
10761 pending input, it might have been that redisplay hasn't
10762 been called, so that mode lines above the echo area are
10763 garbaged. This looks odd, so we prevent it here. */
10764 if (!display_completed)
10765 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10766
10767 if (window_height_changed_p
10768 /* Don't do this if Emacs is shutting down. Redisplay
10769 needs to run hooks. */
10770 && !NILP (Vrun_hooks))
10771 {
10772 /* Must update other windows. Likewise as in other
10773 cases, don't let this update be interrupted by
10774 pending input. */
10775 ptrdiff_t count = SPECPDL_INDEX ();
10776 specbind (Qredisplay_dont_pause, Qt);
10777 windows_or_buffers_changed = 1;
10778 redisplay_internal ();
10779 unbind_to (count, Qnil);
10780 }
10781 else if (FRAME_WINDOW_P (f) && n == 0)
10782 {
10783 /* Window configuration is the same as before.
10784 Can do with a display update of the echo area,
10785 unless we displayed some mode lines. */
10786 update_single_window (w, 1);
10787 FRAME_RIF (f)->flush_display (f);
10788 }
10789 else
10790 update_frame (f, 1, 1);
10791
10792 /* If cursor is in the echo area, make sure that the next
10793 redisplay displays the minibuffer, so that the cursor will
10794 be replaced with what the minibuffer wants. */
10795 if (cursor_in_echo_area)
10796 ++windows_or_buffers_changed;
10797 }
10798 }
10799 else if (!EQ (mini_window, selected_window))
10800 windows_or_buffers_changed++;
10801
10802 /* Last displayed message is now the current message. */
10803 echo_area_buffer[1] = echo_area_buffer[0];
10804 /* Inform read_char that we're not echoing. */
10805 echo_message_buffer = Qnil;
10806
10807 /* Prevent redisplay optimization in redisplay_internal by resetting
10808 this_line_start_pos. This is done because the mini-buffer now
10809 displays the message instead of its buffer text. */
10810 if (EQ (mini_window, selected_window))
10811 CHARPOS (this_line_start_pos) = 0;
10812
10813 return window_height_changed_p;
10814 }
10815
10816
10817 \f
10818 /***********************************************************************
10819 Mode Lines and Frame Titles
10820 ***********************************************************************/
10821
10822 /* A buffer for constructing non-propertized mode-line strings and
10823 frame titles in it; allocated from the heap in init_xdisp and
10824 resized as needed in store_mode_line_noprop_char. */
10825
10826 static char *mode_line_noprop_buf;
10827
10828 /* The buffer's end, and a current output position in it. */
10829
10830 static char *mode_line_noprop_buf_end;
10831 static char *mode_line_noprop_ptr;
10832
10833 #define MODE_LINE_NOPROP_LEN(start) \
10834 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10835
10836 static enum {
10837 MODE_LINE_DISPLAY = 0,
10838 MODE_LINE_TITLE,
10839 MODE_LINE_NOPROP,
10840 MODE_LINE_STRING
10841 } mode_line_target;
10842
10843 /* Alist that caches the results of :propertize.
10844 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10845 static Lisp_Object mode_line_proptrans_alist;
10846
10847 /* List of strings making up the mode-line. */
10848 static Lisp_Object mode_line_string_list;
10849
10850 /* Base face property when building propertized mode line string. */
10851 static Lisp_Object mode_line_string_face;
10852 static Lisp_Object mode_line_string_face_prop;
10853
10854
10855 /* Unwind data for mode line strings */
10856
10857 static Lisp_Object Vmode_line_unwind_vector;
10858
10859 static Lisp_Object
10860 format_mode_line_unwind_data (struct frame *target_frame,
10861 struct buffer *obuf,
10862 Lisp_Object owin,
10863 int save_proptrans)
10864 {
10865 Lisp_Object vector, tmp;
10866
10867 /* Reduce consing by keeping one vector in
10868 Vwith_echo_area_save_vector. */
10869 vector = Vmode_line_unwind_vector;
10870 Vmode_line_unwind_vector = Qnil;
10871
10872 if (NILP (vector))
10873 vector = Fmake_vector (make_number (10), Qnil);
10874
10875 ASET (vector, 0, make_number (mode_line_target));
10876 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10877 ASET (vector, 2, mode_line_string_list);
10878 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10879 ASET (vector, 4, mode_line_string_face);
10880 ASET (vector, 5, mode_line_string_face_prop);
10881
10882 if (obuf)
10883 XSETBUFFER (tmp, obuf);
10884 else
10885 tmp = Qnil;
10886 ASET (vector, 6, tmp);
10887 ASET (vector, 7, owin);
10888 if (target_frame)
10889 {
10890 /* Similarly to `with-selected-window', if the operation selects
10891 a window on another frame, we must restore that frame's
10892 selected window, and (for a tty) the top-frame. */
10893 ASET (vector, 8, target_frame->selected_window);
10894 if (FRAME_TERMCAP_P (target_frame))
10895 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10896 }
10897
10898 return vector;
10899 }
10900
10901 static Lisp_Object
10902 unwind_format_mode_line (Lisp_Object vector)
10903 {
10904 Lisp_Object old_window = AREF (vector, 7);
10905 Lisp_Object target_frame_window = AREF (vector, 8);
10906 Lisp_Object old_top_frame = AREF (vector, 9);
10907
10908 mode_line_target = XINT (AREF (vector, 0));
10909 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10910 mode_line_string_list = AREF (vector, 2);
10911 if (! EQ (AREF (vector, 3), Qt))
10912 mode_line_proptrans_alist = AREF (vector, 3);
10913 mode_line_string_face = AREF (vector, 4);
10914 mode_line_string_face_prop = AREF (vector, 5);
10915
10916 /* Select window before buffer, since it may change the buffer. */
10917 if (!NILP (old_window))
10918 {
10919 /* If the operation that we are unwinding had selected a window
10920 on a different frame, reset its frame-selected-window. For a
10921 text terminal, reset its top-frame if necessary. */
10922 if (!NILP (target_frame_window))
10923 {
10924 Lisp_Object frame
10925 = WINDOW_FRAME (XWINDOW (target_frame_window));
10926
10927 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10928 Fselect_window (target_frame_window, Qt);
10929
10930 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10931 Fselect_frame (old_top_frame, Qt);
10932 }
10933
10934 Fselect_window (old_window, Qt);
10935 }
10936
10937 if (!NILP (AREF (vector, 6)))
10938 {
10939 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10940 ASET (vector, 6, Qnil);
10941 }
10942
10943 Vmode_line_unwind_vector = vector;
10944 return Qnil;
10945 }
10946
10947
10948 /* Store a single character C for the frame title in mode_line_noprop_buf.
10949 Re-allocate mode_line_noprop_buf if necessary. */
10950
10951 static void
10952 store_mode_line_noprop_char (char c)
10953 {
10954 /* If output position has reached the end of the allocated buffer,
10955 increase the buffer's size. */
10956 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10957 {
10958 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10959 ptrdiff_t size = len;
10960 mode_line_noprop_buf =
10961 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10962 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10963 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10964 }
10965
10966 *mode_line_noprop_ptr++ = c;
10967 }
10968
10969
10970 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10971 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10972 characters that yield more columns than PRECISION; PRECISION <= 0
10973 means copy the whole string. Pad with spaces until FIELD_WIDTH
10974 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10975 pad. Called from display_mode_element when it is used to build a
10976 frame title. */
10977
10978 static int
10979 store_mode_line_noprop (const char *string, int field_width, int precision)
10980 {
10981 const unsigned char *str = (const unsigned char *) string;
10982 int n = 0;
10983 ptrdiff_t dummy, nbytes;
10984
10985 /* Copy at most PRECISION chars from STR. */
10986 nbytes = strlen (string);
10987 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10988 while (nbytes--)
10989 store_mode_line_noprop_char (*str++);
10990
10991 /* Fill up with spaces until FIELD_WIDTH reached. */
10992 while (field_width > 0
10993 && n < field_width)
10994 {
10995 store_mode_line_noprop_char (' ');
10996 ++n;
10997 }
10998
10999 return n;
11000 }
11001
11002 /***********************************************************************
11003 Frame Titles
11004 ***********************************************************************/
11005
11006 #ifdef HAVE_WINDOW_SYSTEM
11007
11008 /* Set the title of FRAME, if it has changed. The title format is
11009 Vicon_title_format if FRAME is iconified, otherwise it is
11010 frame_title_format. */
11011
11012 static void
11013 x_consider_frame_title (Lisp_Object frame)
11014 {
11015 struct frame *f = XFRAME (frame);
11016
11017 if (FRAME_WINDOW_P (f)
11018 || FRAME_MINIBUF_ONLY_P (f)
11019 || f->explicit_name)
11020 {
11021 /* Do we have more than one visible frame on this X display? */
11022 Lisp_Object tail;
11023 Lisp_Object fmt;
11024 ptrdiff_t title_start;
11025 char *title;
11026 ptrdiff_t len;
11027 struct it it;
11028 ptrdiff_t count = SPECPDL_INDEX ();
11029
11030 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11031 {
11032 Lisp_Object other_frame = XCAR (tail);
11033 struct frame *tf = XFRAME (other_frame);
11034
11035 if (tf != f
11036 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11037 && !FRAME_MINIBUF_ONLY_P (tf)
11038 && !EQ (other_frame, tip_frame)
11039 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11040 break;
11041 }
11042
11043 /* Set global variable indicating that multiple frames exist. */
11044 multiple_frames = CONSP (tail);
11045
11046 /* Switch to the buffer of selected window of the frame. Set up
11047 mode_line_target so that display_mode_element will output into
11048 mode_line_noprop_buf; then display the title. */
11049 record_unwind_protect (unwind_format_mode_line,
11050 format_mode_line_unwind_data
11051 (f, current_buffer, selected_window, 0));
11052
11053 Fselect_window (f->selected_window, Qt);
11054 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11055 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11056
11057 mode_line_target = MODE_LINE_TITLE;
11058 title_start = MODE_LINE_NOPROP_LEN (0);
11059 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11060 NULL, DEFAULT_FACE_ID);
11061 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11062 len = MODE_LINE_NOPROP_LEN (title_start);
11063 title = mode_line_noprop_buf + title_start;
11064 unbind_to (count, Qnil);
11065
11066 /* Set the title only if it's changed. This avoids consing in
11067 the common case where it hasn't. (If it turns out that we've
11068 already wasted too much time by walking through the list with
11069 display_mode_element, then we might need to optimize at a
11070 higher level than this.) */
11071 if (! STRINGP (f->name)
11072 || SBYTES (f->name) != len
11073 || memcmp (title, SDATA (f->name), len) != 0)
11074 x_implicitly_set_name (f, make_string (title, len), Qnil);
11075 }
11076 }
11077
11078 #endif /* not HAVE_WINDOW_SYSTEM */
11079
11080 \f
11081 /***********************************************************************
11082 Menu Bars
11083 ***********************************************************************/
11084
11085
11086 /* Prepare for redisplay by updating menu-bar item lists when
11087 appropriate. This can call eval. */
11088
11089 void
11090 prepare_menu_bars (void)
11091 {
11092 int all_windows;
11093 struct gcpro gcpro1, gcpro2;
11094 struct frame *f;
11095 Lisp_Object tooltip_frame;
11096
11097 #ifdef HAVE_WINDOW_SYSTEM
11098 tooltip_frame = tip_frame;
11099 #else
11100 tooltip_frame = Qnil;
11101 #endif
11102
11103 /* Update all frame titles based on their buffer names, etc. We do
11104 this before the menu bars so that the buffer-menu will show the
11105 up-to-date frame titles. */
11106 #ifdef HAVE_WINDOW_SYSTEM
11107 if (windows_or_buffers_changed || update_mode_lines)
11108 {
11109 Lisp_Object tail, frame;
11110
11111 FOR_EACH_FRAME (tail, frame)
11112 {
11113 f = XFRAME (frame);
11114 if (!EQ (frame, tooltip_frame)
11115 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11116 x_consider_frame_title (frame);
11117 }
11118 }
11119 #endif /* HAVE_WINDOW_SYSTEM */
11120
11121 /* Update the menu bar item lists, if appropriate. This has to be
11122 done before any actual redisplay or generation of display lines. */
11123 all_windows = (update_mode_lines
11124 || buffer_shared > 1
11125 || windows_or_buffers_changed);
11126 if (all_windows)
11127 {
11128 Lisp_Object tail, frame;
11129 ptrdiff_t count = SPECPDL_INDEX ();
11130 /* 1 means that update_menu_bar has run its hooks
11131 so any further calls to update_menu_bar shouldn't do so again. */
11132 int menu_bar_hooks_run = 0;
11133
11134 record_unwind_save_match_data ();
11135
11136 FOR_EACH_FRAME (tail, frame)
11137 {
11138 f = XFRAME (frame);
11139
11140 /* Ignore tooltip frame. */
11141 if (EQ (frame, tooltip_frame))
11142 continue;
11143
11144 /* If a window on this frame changed size, report that to
11145 the user and clear the size-change flag. */
11146 if (FRAME_WINDOW_SIZES_CHANGED (f))
11147 {
11148 Lisp_Object functions;
11149
11150 /* Clear flag first in case we get an error below. */
11151 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11152 functions = Vwindow_size_change_functions;
11153 GCPRO2 (tail, functions);
11154
11155 while (CONSP (functions))
11156 {
11157 if (!EQ (XCAR (functions), Qt))
11158 call1 (XCAR (functions), frame);
11159 functions = XCDR (functions);
11160 }
11161 UNGCPRO;
11162 }
11163
11164 GCPRO1 (tail);
11165 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11166 #ifdef HAVE_WINDOW_SYSTEM
11167 update_tool_bar (f, 0);
11168 #endif
11169 #ifdef HAVE_NS
11170 if (windows_or_buffers_changed
11171 && FRAME_NS_P (f))
11172 ns_set_doc_edited (f, Fbuffer_modified_p
11173 (XWINDOW (f->selected_window)->buffer));
11174 #endif
11175 UNGCPRO;
11176 }
11177
11178 unbind_to (count, Qnil);
11179 }
11180 else
11181 {
11182 struct frame *sf = SELECTED_FRAME ();
11183 update_menu_bar (sf, 1, 0);
11184 #ifdef HAVE_WINDOW_SYSTEM
11185 update_tool_bar (sf, 1);
11186 #endif
11187 }
11188 }
11189
11190
11191 /* Update the menu bar item list for frame F. This has to be done
11192 before we start to fill in any display lines, because it can call
11193 eval.
11194
11195 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11196
11197 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11198 already ran the menu bar hooks for this redisplay, so there
11199 is no need to run them again. The return value is the
11200 updated value of this flag, to pass to the next call. */
11201
11202 static int
11203 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11204 {
11205 Lisp_Object window;
11206 register struct window *w;
11207
11208 /* If called recursively during a menu update, do nothing. This can
11209 happen when, for instance, an activate-menubar-hook causes a
11210 redisplay. */
11211 if (inhibit_menubar_update)
11212 return hooks_run;
11213
11214 window = FRAME_SELECTED_WINDOW (f);
11215 w = XWINDOW (window);
11216
11217 if (FRAME_WINDOW_P (f)
11218 ?
11219 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11220 || defined (HAVE_NS) || defined (USE_GTK)
11221 FRAME_EXTERNAL_MENU_BAR (f)
11222 #else
11223 FRAME_MENU_BAR_LINES (f) > 0
11224 #endif
11225 : FRAME_MENU_BAR_LINES (f) > 0)
11226 {
11227 /* If the user has switched buffers or windows, we need to
11228 recompute to reflect the new bindings. But we'll
11229 recompute when update_mode_lines is set too; that means
11230 that people can use force-mode-line-update to request
11231 that the menu bar be recomputed. The adverse effect on
11232 the rest of the redisplay algorithm is about the same as
11233 windows_or_buffers_changed anyway. */
11234 if (windows_or_buffers_changed
11235 /* This used to test w->update_mode_line, but we believe
11236 there is no need to recompute the menu in that case. */
11237 || update_mode_lines
11238 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11239 < BUF_MODIFF (XBUFFER (w->buffer)))
11240 != w->last_had_star)
11241 || ((!NILP (Vtransient_mark_mode)
11242 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11243 != !NILP (w->region_showing)))
11244 {
11245 struct buffer *prev = current_buffer;
11246 ptrdiff_t count = SPECPDL_INDEX ();
11247
11248 specbind (Qinhibit_menubar_update, Qt);
11249
11250 set_buffer_internal_1 (XBUFFER (w->buffer));
11251 if (save_match_data)
11252 record_unwind_save_match_data ();
11253 if (NILP (Voverriding_local_map_menu_flag))
11254 {
11255 specbind (Qoverriding_terminal_local_map, Qnil);
11256 specbind (Qoverriding_local_map, Qnil);
11257 }
11258
11259 if (!hooks_run)
11260 {
11261 /* Run the Lucid hook. */
11262 safe_run_hooks (Qactivate_menubar_hook);
11263
11264 /* If it has changed current-menubar from previous value,
11265 really recompute the menu-bar from the value. */
11266 if (! NILP (Vlucid_menu_bar_dirty_flag))
11267 call0 (Qrecompute_lucid_menubar);
11268
11269 safe_run_hooks (Qmenu_bar_update_hook);
11270
11271 hooks_run = 1;
11272 }
11273
11274 XSETFRAME (Vmenu_updating_frame, f);
11275 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11276
11277 /* Redisplay the menu bar in case we changed it. */
11278 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11279 || defined (HAVE_NS) || defined (USE_GTK)
11280 if (FRAME_WINDOW_P (f))
11281 {
11282 #if defined (HAVE_NS)
11283 /* All frames on Mac OS share the same menubar. So only
11284 the selected frame should be allowed to set it. */
11285 if (f == SELECTED_FRAME ())
11286 #endif
11287 set_frame_menubar (f, 0, 0);
11288 }
11289 else
11290 /* On a terminal screen, the menu bar is an ordinary screen
11291 line, and this makes it get updated. */
11292 w->update_mode_line = 1;
11293 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11294 /* In the non-toolkit version, the menu bar is an ordinary screen
11295 line, and this makes it get updated. */
11296 w->update_mode_line = 1;
11297 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11298
11299 unbind_to (count, Qnil);
11300 set_buffer_internal_1 (prev);
11301 }
11302 }
11303
11304 return hooks_run;
11305 }
11306
11307
11308 \f
11309 /***********************************************************************
11310 Output Cursor
11311 ***********************************************************************/
11312
11313 #ifdef HAVE_WINDOW_SYSTEM
11314
11315 /* EXPORT:
11316 Nominal cursor position -- where to draw output.
11317 HPOS and VPOS are window relative glyph matrix coordinates.
11318 X and Y are window relative pixel coordinates. */
11319
11320 struct cursor_pos output_cursor;
11321
11322
11323 /* EXPORT:
11324 Set the global variable output_cursor to CURSOR. All cursor
11325 positions are relative to updated_window. */
11326
11327 void
11328 set_output_cursor (struct cursor_pos *cursor)
11329 {
11330 output_cursor.hpos = cursor->hpos;
11331 output_cursor.vpos = cursor->vpos;
11332 output_cursor.x = cursor->x;
11333 output_cursor.y = cursor->y;
11334 }
11335
11336
11337 /* EXPORT for RIF:
11338 Set a nominal cursor position.
11339
11340 HPOS and VPOS are column/row positions in a window glyph matrix. X
11341 and Y are window text area relative pixel positions.
11342
11343 If this is done during an update, updated_window will contain the
11344 window that is being updated and the position is the future output
11345 cursor position for that window. If updated_window is null, use
11346 selected_window and display the cursor at the given position. */
11347
11348 void
11349 x_cursor_to (int vpos, int hpos, int y, int x)
11350 {
11351 struct window *w;
11352
11353 /* If updated_window is not set, work on selected_window. */
11354 if (updated_window)
11355 w = updated_window;
11356 else
11357 w = XWINDOW (selected_window);
11358
11359 /* Set the output cursor. */
11360 output_cursor.hpos = hpos;
11361 output_cursor.vpos = vpos;
11362 output_cursor.x = x;
11363 output_cursor.y = y;
11364
11365 /* If not called as part of an update, really display the cursor.
11366 This will also set the cursor position of W. */
11367 if (updated_window == NULL)
11368 {
11369 BLOCK_INPUT;
11370 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11371 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11372 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11373 UNBLOCK_INPUT;
11374 }
11375 }
11376
11377 #endif /* HAVE_WINDOW_SYSTEM */
11378
11379 \f
11380 /***********************************************************************
11381 Tool-bars
11382 ***********************************************************************/
11383
11384 #ifdef HAVE_WINDOW_SYSTEM
11385
11386 /* Where the mouse was last time we reported a mouse event. */
11387
11388 FRAME_PTR last_mouse_frame;
11389
11390 /* Tool-bar item index of the item on which a mouse button was pressed
11391 or -1. */
11392
11393 int last_tool_bar_item;
11394
11395
11396 static Lisp_Object
11397 update_tool_bar_unwind (Lisp_Object frame)
11398 {
11399 selected_frame = frame;
11400 return Qnil;
11401 }
11402
11403 /* Update the tool-bar item list for frame F. This has to be done
11404 before we start to fill in any display lines. Called from
11405 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11406 and restore it here. */
11407
11408 static void
11409 update_tool_bar (struct frame *f, int save_match_data)
11410 {
11411 #if defined (USE_GTK) || defined (HAVE_NS)
11412 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11413 #else
11414 int do_update = WINDOWP (f->tool_bar_window)
11415 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11416 #endif
11417
11418 if (do_update)
11419 {
11420 Lisp_Object window;
11421 struct window *w;
11422
11423 window = FRAME_SELECTED_WINDOW (f);
11424 w = XWINDOW (window);
11425
11426 /* If the user has switched buffers or windows, we need to
11427 recompute to reflect the new bindings. But we'll
11428 recompute when update_mode_lines is set too; that means
11429 that people can use force-mode-line-update to request
11430 that the menu bar be recomputed. The adverse effect on
11431 the rest of the redisplay algorithm is about the same as
11432 windows_or_buffers_changed anyway. */
11433 if (windows_or_buffers_changed
11434 || w->update_mode_line
11435 || update_mode_lines
11436 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11437 < BUF_MODIFF (XBUFFER (w->buffer)))
11438 != w->last_had_star)
11439 || ((!NILP (Vtransient_mark_mode)
11440 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11441 != !NILP (w->region_showing)))
11442 {
11443 struct buffer *prev = current_buffer;
11444 ptrdiff_t count = SPECPDL_INDEX ();
11445 Lisp_Object frame, new_tool_bar;
11446 int new_n_tool_bar;
11447 struct gcpro gcpro1;
11448
11449 /* Set current_buffer to the buffer of the selected
11450 window of the frame, so that we get the right local
11451 keymaps. */
11452 set_buffer_internal_1 (XBUFFER (w->buffer));
11453
11454 /* Save match data, if we must. */
11455 if (save_match_data)
11456 record_unwind_save_match_data ();
11457
11458 /* Make sure that we don't accidentally use bogus keymaps. */
11459 if (NILP (Voverriding_local_map_menu_flag))
11460 {
11461 specbind (Qoverriding_terminal_local_map, Qnil);
11462 specbind (Qoverriding_local_map, Qnil);
11463 }
11464
11465 GCPRO1 (new_tool_bar);
11466
11467 /* We must temporarily set the selected frame to this frame
11468 before calling tool_bar_items, because the calculation of
11469 the tool-bar keymap uses the selected frame (see
11470 `tool-bar-make-keymap' in tool-bar.el). */
11471 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11472 XSETFRAME (frame, f);
11473 selected_frame = frame;
11474
11475 /* Build desired tool-bar items from keymaps. */
11476 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11477 &new_n_tool_bar);
11478
11479 /* Redisplay the tool-bar if we changed it. */
11480 if (new_n_tool_bar != f->n_tool_bar_items
11481 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11482 {
11483 /* Redisplay that happens asynchronously due to an expose event
11484 may access f->tool_bar_items. Make sure we update both
11485 variables within BLOCK_INPUT so no such event interrupts. */
11486 BLOCK_INPUT;
11487 f->tool_bar_items = new_tool_bar;
11488 f->n_tool_bar_items = new_n_tool_bar;
11489 w->update_mode_line = 1;
11490 UNBLOCK_INPUT;
11491 }
11492
11493 UNGCPRO;
11494
11495 unbind_to (count, Qnil);
11496 set_buffer_internal_1 (prev);
11497 }
11498 }
11499 }
11500
11501
11502 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11503 F's desired tool-bar contents. F->tool_bar_items must have
11504 been set up previously by calling prepare_menu_bars. */
11505
11506 static void
11507 build_desired_tool_bar_string (struct frame *f)
11508 {
11509 int i, size, size_needed;
11510 struct gcpro gcpro1, gcpro2, gcpro3;
11511 Lisp_Object image, plist, props;
11512
11513 image = plist = props = Qnil;
11514 GCPRO3 (image, plist, props);
11515
11516 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11517 Otherwise, make a new string. */
11518
11519 /* The size of the string we might be able to reuse. */
11520 size = (STRINGP (f->desired_tool_bar_string)
11521 ? SCHARS (f->desired_tool_bar_string)
11522 : 0);
11523
11524 /* We need one space in the string for each image. */
11525 size_needed = f->n_tool_bar_items;
11526
11527 /* Reuse f->desired_tool_bar_string, if possible. */
11528 if (size < size_needed || NILP (f->desired_tool_bar_string))
11529 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11530 make_number (' '));
11531 else
11532 {
11533 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11534 Fremove_text_properties (make_number (0), make_number (size),
11535 props, f->desired_tool_bar_string);
11536 }
11537
11538 /* Put a `display' property on the string for the images to display,
11539 put a `menu_item' property on tool-bar items with a value that
11540 is the index of the item in F's tool-bar item vector. */
11541 for (i = 0; i < f->n_tool_bar_items; ++i)
11542 {
11543 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11544
11545 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11546 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11547 int hmargin, vmargin, relief, idx, end;
11548
11549 /* If image is a vector, choose the image according to the
11550 button state. */
11551 image = PROP (TOOL_BAR_ITEM_IMAGES);
11552 if (VECTORP (image))
11553 {
11554 if (enabled_p)
11555 idx = (selected_p
11556 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11557 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11558 else
11559 idx = (selected_p
11560 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11561 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11562
11563 eassert (ASIZE (image) >= idx);
11564 image = AREF (image, idx);
11565 }
11566 else
11567 idx = -1;
11568
11569 /* Ignore invalid image specifications. */
11570 if (!valid_image_p (image))
11571 continue;
11572
11573 /* Display the tool-bar button pressed, or depressed. */
11574 plist = Fcopy_sequence (XCDR (image));
11575
11576 /* Compute margin and relief to draw. */
11577 relief = (tool_bar_button_relief >= 0
11578 ? tool_bar_button_relief
11579 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11580 hmargin = vmargin = relief;
11581
11582 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11583 INT_MAX - max (hmargin, vmargin)))
11584 {
11585 hmargin += XFASTINT (Vtool_bar_button_margin);
11586 vmargin += XFASTINT (Vtool_bar_button_margin);
11587 }
11588 else if (CONSP (Vtool_bar_button_margin))
11589 {
11590 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11591 INT_MAX - hmargin))
11592 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11593
11594 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11595 INT_MAX - vmargin))
11596 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11597 }
11598
11599 if (auto_raise_tool_bar_buttons_p)
11600 {
11601 /* Add a `:relief' property to the image spec if the item is
11602 selected. */
11603 if (selected_p)
11604 {
11605 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11606 hmargin -= relief;
11607 vmargin -= relief;
11608 }
11609 }
11610 else
11611 {
11612 /* If image is selected, display it pressed, i.e. with a
11613 negative relief. If it's not selected, display it with a
11614 raised relief. */
11615 plist = Fplist_put (plist, QCrelief,
11616 (selected_p
11617 ? make_number (-relief)
11618 : make_number (relief)));
11619 hmargin -= relief;
11620 vmargin -= relief;
11621 }
11622
11623 /* Put a margin around the image. */
11624 if (hmargin || vmargin)
11625 {
11626 if (hmargin == vmargin)
11627 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11628 else
11629 plist = Fplist_put (plist, QCmargin,
11630 Fcons (make_number (hmargin),
11631 make_number (vmargin)));
11632 }
11633
11634 /* If button is not enabled, and we don't have special images
11635 for the disabled state, make the image appear disabled by
11636 applying an appropriate algorithm to it. */
11637 if (!enabled_p && idx < 0)
11638 plist = Fplist_put (plist, QCconversion, Qdisabled);
11639
11640 /* Put a `display' text property on the string for the image to
11641 display. Put a `menu-item' property on the string that gives
11642 the start of this item's properties in the tool-bar items
11643 vector. */
11644 image = Fcons (Qimage, plist);
11645 props = list4 (Qdisplay, image,
11646 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11647
11648 /* Let the last image hide all remaining spaces in the tool bar
11649 string. The string can be longer than needed when we reuse a
11650 previous string. */
11651 if (i + 1 == f->n_tool_bar_items)
11652 end = SCHARS (f->desired_tool_bar_string);
11653 else
11654 end = i + 1;
11655 Fadd_text_properties (make_number (i), make_number (end),
11656 props, f->desired_tool_bar_string);
11657 #undef PROP
11658 }
11659
11660 UNGCPRO;
11661 }
11662
11663
11664 /* Display one line of the tool-bar of frame IT->f.
11665
11666 HEIGHT specifies the desired height of the tool-bar line.
11667 If the actual height of the glyph row is less than HEIGHT, the
11668 row's height is increased to HEIGHT, and the icons are centered
11669 vertically in the new height.
11670
11671 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11672 count a final empty row in case the tool-bar width exactly matches
11673 the window width.
11674 */
11675
11676 static void
11677 display_tool_bar_line (struct it *it, int height)
11678 {
11679 struct glyph_row *row = it->glyph_row;
11680 int max_x = it->last_visible_x;
11681 struct glyph *last;
11682
11683 prepare_desired_row (row);
11684 row->y = it->current_y;
11685
11686 /* Note that this isn't made use of if the face hasn't a box,
11687 so there's no need to check the face here. */
11688 it->start_of_box_run_p = 1;
11689
11690 while (it->current_x < max_x)
11691 {
11692 int x, n_glyphs_before, i, nglyphs;
11693 struct it it_before;
11694
11695 /* Get the next display element. */
11696 if (!get_next_display_element (it))
11697 {
11698 /* Don't count empty row if we are counting needed tool-bar lines. */
11699 if (height < 0 && !it->hpos)
11700 return;
11701 break;
11702 }
11703
11704 /* Produce glyphs. */
11705 n_glyphs_before = row->used[TEXT_AREA];
11706 it_before = *it;
11707
11708 PRODUCE_GLYPHS (it);
11709
11710 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11711 i = 0;
11712 x = it_before.current_x;
11713 while (i < nglyphs)
11714 {
11715 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11716
11717 if (x + glyph->pixel_width > max_x)
11718 {
11719 /* Glyph doesn't fit on line. Backtrack. */
11720 row->used[TEXT_AREA] = n_glyphs_before;
11721 *it = it_before;
11722 /* If this is the only glyph on this line, it will never fit on the
11723 tool-bar, so skip it. But ensure there is at least one glyph,
11724 so we don't accidentally disable the tool-bar. */
11725 if (n_glyphs_before == 0
11726 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11727 break;
11728 goto out;
11729 }
11730
11731 ++it->hpos;
11732 x += glyph->pixel_width;
11733 ++i;
11734 }
11735
11736 /* Stop at line end. */
11737 if (ITERATOR_AT_END_OF_LINE_P (it))
11738 break;
11739
11740 set_iterator_to_next (it, 1);
11741 }
11742
11743 out:;
11744
11745 row->displays_text_p = row->used[TEXT_AREA] != 0;
11746
11747 /* Use default face for the border below the tool bar.
11748
11749 FIXME: When auto-resize-tool-bars is grow-only, there is
11750 no additional border below the possibly empty tool-bar lines.
11751 So to make the extra empty lines look "normal", we have to
11752 use the tool-bar face for the border too. */
11753 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11754 it->face_id = DEFAULT_FACE_ID;
11755
11756 extend_face_to_end_of_line (it);
11757 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11758 last->right_box_line_p = 1;
11759 if (last == row->glyphs[TEXT_AREA])
11760 last->left_box_line_p = 1;
11761
11762 /* Make line the desired height and center it vertically. */
11763 if ((height -= it->max_ascent + it->max_descent) > 0)
11764 {
11765 /* Don't add more than one line height. */
11766 height %= FRAME_LINE_HEIGHT (it->f);
11767 it->max_ascent += height / 2;
11768 it->max_descent += (height + 1) / 2;
11769 }
11770
11771 compute_line_metrics (it);
11772
11773 /* If line is empty, make it occupy the rest of the tool-bar. */
11774 if (!row->displays_text_p)
11775 {
11776 row->height = row->phys_height = it->last_visible_y - row->y;
11777 row->visible_height = row->height;
11778 row->ascent = row->phys_ascent = 0;
11779 row->extra_line_spacing = 0;
11780 }
11781
11782 row->full_width_p = 1;
11783 row->continued_p = 0;
11784 row->truncated_on_left_p = 0;
11785 row->truncated_on_right_p = 0;
11786
11787 it->current_x = it->hpos = 0;
11788 it->current_y += row->height;
11789 ++it->vpos;
11790 ++it->glyph_row;
11791 }
11792
11793
11794 /* Max tool-bar height. */
11795
11796 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11797 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11798
11799 /* Value is the number of screen lines needed to make all tool-bar
11800 items of frame F visible. The number of actual rows needed is
11801 returned in *N_ROWS if non-NULL. */
11802
11803 static int
11804 tool_bar_lines_needed (struct frame *f, int *n_rows)
11805 {
11806 struct window *w = XWINDOW (f->tool_bar_window);
11807 struct it it;
11808 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11809 the desired matrix, so use (unused) mode-line row as temporary row to
11810 avoid destroying the first tool-bar row. */
11811 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11812
11813 /* Initialize an iterator for iteration over
11814 F->desired_tool_bar_string in the tool-bar window of frame F. */
11815 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11816 it.first_visible_x = 0;
11817 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11818 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11819 it.paragraph_embedding = L2R;
11820
11821 while (!ITERATOR_AT_END_P (&it))
11822 {
11823 clear_glyph_row (temp_row);
11824 it.glyph_row = temp_row;
11825 display_tool_bar_line (&it, -1);
11826 }
11827 clear_glyph_row (temp_row);
11828
11829 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11830 if (n_rows)
11831 *n_rows = it.vpos > 0 ? it.vpos : -1;
11832
11833 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11834 }
11835
11836
11837 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11838 0, 1, 0,
11839 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11840 (Lisp_Object frame)
11841 {
11842 struct frame *f;
11843 struct window *w;
11844 int nlines = 0;
11845
11846 if (NILP (frame))
11847 frame = selected_frame;
11848 else
11849 CHECK_FRAME (frame);
11850 f = XFRAME (frame);
11851
11852 if (WINDOWP (f->tool_bar_window)
11853 && (w = XWINDOW (f->tool_bar_window),
11854 WINDOW_TOTAL_LINES (w) > 0))
11855 {
11856 update_tool_bar (f, 1);
11857 if (f->n_tool_bar_items)
11858 {
11859 build_desired_tool_bar_string (f);
11860 nlines = tool_bar_lines_needed (f, NULL);
11861 }
11862 }
11863
11864 return make_number (nlines);
11865 }
11866
11867
11868 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11869 height should be changed. */
11870
11871 static int
11872 redisplay_tool_bar (struct frame *f)
11873 {
11874 struct window *w;
11875 struct it it;
11876 struct glyph_row *row;
11877
11878 #if defined (USE_GTK) || defined (HAVE_NS)
11879 if (FRAME_EXTERNAL_TOOL_BAR (f))
11880 update_frame_tool_bar (f);
11881 return 0;
11882 #endif
11883
11884 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11885 do anything. This means you must start with tool-bar-lines
11886 non-zero to get the auto-sizing effect. Or in other words, you
11887 can turn off tool-bars by specifying tool-bar-lines zero. */
11888 if (!WINDOWP (f->tool_bar_window)
11889 || (w = XWINDOW (f->tool_bar_window),
11890 WINDOW_TOTAL_LINES (w) == 0))
11891 return 0;
11892
11893 /* Set up an iterator for the tool-bar window. */
11894 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11895 it.first_visible_x = 0;
11896 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11897 row = it.glyph_row;
11898
11899 /* Build a string that represents the contents of the tool-bar. */
11900 build_desired_tool_bar_string (f);
11901 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11902 /* FIXME: This should be controlled by a user option. But it
11903 doesn't make sense to have an R2L tool bar if the menu bar cannot
11904 be drawn also R2L, and making the menu bar R2L is tricky due
11905 toolkit-specific code that implements it. If an R2L tool bar is
11906 ever supported, display_tool_bar_line should also be augmented to
11907 call unproduce_glyphs like display_line and display_string
11908 do. */
11909 it.paragraph_embedding = L2R;
11910
11911 if (f->n_tool_bar_rows == 0)
11912 {
11913 int nlines;
11914
11915 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11916 nlines != WINDOW_TOTAL_LINES (w)))
11917 {
11918 Lisp_Object frame;
11919 int old_height = WINDOW_TOTAL_LINES (w);
11920
11921 XSETFRAME (frame, f);
11922 Fmodify_frame_parameters (frame,
11923 Fcons (Fcons (Qtool_bar_lines,
11924 make_number (nlines)),
11925 Qnil));
11926 if (WINDOW_TOTAL_LINES (w) != old_height)
11927 {
11928 clear_glyph_matrix (w->desired_matrix);
11929 fonts_changed_p = 1;
11930 return 1;
11931 }
11932 }
11933 }
11934
11935 /* Display as many lines as needed to display all tool-bar items. */
11936
11937 if (f->n_tool_bar_rows > 0)
11938 {
11939 int border, rows, height, extra;
11940
11941 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11942 border = XINT (Vtool_bar_border);
11943 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11944 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11945 else if (EQ (Vtool_bar_border, Qborder_width))
11946 border = f->border_width;
11947 else
11948 border = 0;
11949 if (border < 0)
11950 border = 0;
11951
11952 rows = f->n_tool_bar_rows;
11953 height = max (1, (it.last_visible_y - border) / rows);
11954 extra = it.last_visible_y - border - height * rows;
11955
11956 while (it.current_y < it.last_visible_y)
11957 {
11958 int h = 0;
11959 if (extra > 0 && rows-- > 0)
11960 {
11961 h = (extra + rows - 1) / rows;
11962 extra -= h;
11963 }
11964 display_tool_bar_line (&it, height + h);
11965 }
11966 }
11967 else
11968 {
11969 while (it.current_y < it.last_visible_y)
11970 display_tool_bar_line (&it, 0);
11971 }
11972
11973 /* It doesn't make much sense to try scrolling in the tool-bar
11974 window, so don't do it. */
11975 w->desired_matrix->no_scrolling_p = 1;
11976 w->must_be_updated_p = 1;
11977
11978 if (!NILP (Vauto_resize_tool_bars))
11979 {
11980 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11981 int change_height_p = 0;
11982
11983 /* If we couldn't display everything, change the tool-bar's
11984 height if there is room for more. */
11985 if (IT_STRING_CHARPOS (it) < it.end_charpos
11986 && it.current_y < max_tool_bar_height)
11987 change_height_p = 1;
11988
11989 row = it.glyph_row - 1;
11990
11991 /* If there are blank lines at the end, except for a partially
11992 visible blank line at the end that is smaller than
11993 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11994 if (!row->displays_text_p
11995 && row->height >= FRAME_LINE_HEIGHT (f))
11996 change_height_p = 1;
11997
11998 /* If row displays tool-bar items, but is partially visible,
11999 change the tool-bar's height. */
12000 if (row->displays_text_p
12001 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12002 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12003 change_height_p = 1;
12004
12005 /* Resize windows as needed by changing the `tool-bar-lines'
12006 frame parameter. */
12007 if (change_height_p)
12008 {
12009 Lisp_Object frame;
12010 int old_height = WINDOW_TOTAL_LINES (w);
12011 int nrows;
12012 int nlines = tool_bar_lines_needed (f, &nrows);
12013
12014 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12015 && !f->minimize_tool_bar_window_p)
12016 ? (nlines > old_height)
12017 : (nlines != old_height));
12018 f->minimize_tool_bar_window_p = 0;
12019
12020 if (change_height_p)
12021 {
12022 XSETFRAME (frame, f);
12023 Fmodify_frame_parameters (frame,
12024 Fcons (Fcons (Qtool_bar_lines,
12025 make_number (nlines)),
12026 Qnil));
12027 if (WINDOW_TOTAL_LINES (w) != old_height)
12028 {
12029 clear_glyph_matrix (w->desired_matrix);
12030 f->n_tool_bar_rows = nrows;
12031 fonts_changed_p = 1;
12032 return 1;
12033 }
12034 }
12035 }
12036 }
12037
12038 f->minimize_tool_bar_window_p = 0;
12039 return 0;
12040 }
12041
12042
12043 /* Get information about the tool-bar item which is displayed in GLYPH
12044 on frame F. Return in *PROP_IDX the index where tool-bar item
12045 properties start in F->tool_bar_items. Value is zero if
12046 GLYPH doesn't display a tool-bar item. */
12047
12048 static int
12049 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12050 {
12051 Lisp_Object prop;
12052 int success_p;
12053 int charpos;
12054
12055 /* This function can be called asynchronously, which means we must
12056 exclude any possibility that Fget_text_property signals an
12057 error. */
12058 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12059 charpos = max (0, charpos);
12060
12061 /* Get the text property `menu-item' at pos. The value of that
12062 property is the start index of this item's properties in
12063 F->tool_bar_items. */
12064 prop = Fget_text_property (make_number (charpos),
12065 Qmenu_item, f->current_tool_bar_string);
12066 if (INTEGERP (prop))
12067 {
12068 *prop_idx = XINT (prop);
12069 success_p = 1;
12070 }
12071 else
12072 success_p = 0;
12073
12074 return success_p;
12075 }
12076
12077 \f
12078 /* Get information about the tool-bar item at position X/Y on frame F.
12079 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12080 the current matrix of the tool-bar window of F, or NULL if not
12081 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12082 item in F->tool_bar_items. Value is
12083
12084 -1 if X/Y is not on a tool-bar item
12085 0 if X/Y is on the same item that was highlighted before.
12086 1 otherwise. */
12087
12088 static int
12089 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12090 int *hpos, int *vpos, int *prop_idx)
12091 {
12092 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12093 struct window *w = XWINDOW (f->tool_bar_window);
12094 int area;
12095
12096 /* Find the glyph under X/Y. */
12097 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12098 if (*glyph == NULL)
12099 return -1;
12100
12101 /* Get the start of this tool-bar item's properties in
12102 f->tool_bar_items. */
12103 if (!tool_bar_item_info (f, *glyph, prop_idx))
12104 return -1;
12105
12106 /* Is mouse on the highlighted item? */
12107 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12108 && *vpos >= hlinfo->mouse_face_beg_row
12109 && *vpos <= hlinfo->mouse_face_end_row
12110 && (*vpos > hlinfo->mouse_face_beg_row
12111 || *hpos >= hlinfo->mouse_face_beg_col)
12112 && (*vpos < hlinfo->mouse_face_end_row
12113 || *hpos < hlinfo->mouse_face_end_col
12114 || hlinfo->mouse_face_past_end))
12115 return 0;
12116
12117 return 1;
12118 }
12119
12120
12121 /* EXPORT:
12122 Handle mouse button event on the tool-bar of frame F, at
12123 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12124 0 for button release. MODIFIERS is event modifiers for button
12125 release. */
12126
12127 void
12128 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12129 int modifiers)
12130 {
12131 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12132 struct window *w = XWINDOW (f->tool_bar_window);
12133 int hpos, vpos, prop_idx;
12134 struct glyph *glyph;
12135 Lisp_Object enabled_p;
12136
12137 /* If not on the highlighted tool-bar item, return. */
12138 frame_to_window_pixel_xy (w, &x, &y);
12139 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12140 return;
12141
12142 /* If item is disabled, do nothing. */
12143 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12144 if (NILP (enabled_p))
12145 return;
12146
12147 if (down_p)
12148 {
12149 /* Show item in pressed state. */
12150 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12151 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12152 last_tool_bar_item = prop_idx;
12153 }
12154 else
12155 {
12156 Lisp_Object key, frame;
12157 struct input_event event;
12158 EVENT_INIT (event);
12159
12160 /* Show item in released state. */
12161 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12162 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12163
12164 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12165
12166 XSETFRAME (frame, f);
12167 event.kind = TOOL_BAR_EVENT;
12168 event.frame_or_window = frame;
12169 event.arg = frame;
12170 kbd_buffer_store_event (&event);
12171
12172 event.kind = TOOL_BAR_EVENT;
12173 event.frame_or_window = frame;
12174 event.arg = key;
12175 event.modifiers = modifiers;
12176 kbd_buffer_store_event (&event);
12177 last_tool_bar_item = -1;
12178 }
12179 }
12180
12181
12182 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12183 tool-bar window-relative coordinates X/Y. Called from
12184 note_mouse_highlight. */
12185
12186 static void
12187 note_tool_bar_highlight (struct frame *f, int x, int y)
12188 {
12189 Lisp_Object window = f->tool_bar_window;
12190 struct window *w = XWINDOW (window);
12191 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12192 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12193 int hpos, vpos;
12194 struct glyph *glyph;
12195 struct glyph_row *row;
12196 int i;
12197 Lisp_Object enabled_p;
12198 int prop_idx;
12199 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12200 int mouse_down_p, rc;
12201
12202 /* Function note_mouse_highlight is called with negative X/Y
12203 values when mouse moves outside of the frame. */
12204 if (x <= 0 || y <= 0)
12205 {
12206 clear_mouse_face (hlinfo);
12207 return;
12208 }
12209
12210 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12211 if (rc < 0)
12212 {
12213 /* Not on tool-bar item. */
12214 clear_mouse_face (hlinfo);
12215 return;
12216 }
12217 else if (rc == 0)
12218 /* On same tool-bar item as before. */
12219 goto set_help_echo;
12220
12221 clear_mouse_face (hlinfo);
12222
12223 /* Mouse is down, but on different tool-bar item? */
12224 mouse_down_p = (dpyinfo->grabbed
12225 && f == last_mouse_frame
12226 && FRAME_LIVE_P (f));
12227 if (mouse_down_p
12228 && last_tool_bar_item != prop_idx)
12229 return;
12230
12231 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12232 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12233
12234 /* If tool-bar item is not enabled, don't highlight it. */
12235 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12236 if (!NILP (enabled_p))
12237 {
12238 /* Compute the x-position of the glyph. In front and past the
12239 image is a space. We include this in the highlighted area. */
12240 row = MATRIX_ROW (w->current_matrix, vpos);
12241 for (i = x = 0; i < hpos; ++i)
12242 x += row->glyphs[TEXT_AREA][i].pixel_width;
12243
12244 /* Record this as the current active region. */
12245 hlinfo->mouse_face_beg_col = hpos;
12246 hlinfo->mouse_face_beg_row = vpos;
12247 hlinfo->mouse_face_beg_x = x;
12248 hlinfo->mouse_face_beg_y = row->y;
12249 hlinfo->mouse_face_past_end = 0;
12250
12251 hlinfo->mouse_face_end_col = hpos + 1;
12252 hlinfo->mouse_face_end_row = vpos;
12253 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12254 hlinfo->mouse_face_end_y = row->y;
12255 hlinfo->mouse_face_window = window;
12256 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12257
12258 /* Display it as active. */
12259 show_mouse_face (hlinfo, draw);
12260 hlinfo->mouse_face_image_state = draw;
12261 }
12262
12263 set_help_echo:
12264
12265 /* Set help_echo_string to a help string to display for this tool-bar item.
12266 XTread_socket does the rest. */
12267 help_echo_object = help_echo_window = Qnil;
12268 help_echo_pos = -1;
12269 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12270 if (NILP (help_echo_string))
12271 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12272 }
12273
12274 #endif /* HAVE_WINDOW_SYSTEM */
12275
12276
12277 \f
12278 /************************************************************************
12279 Horizontal scrolling
12280 ************************************************************************/
12281
12282 static int hscroll_window_tree (Lisp_Object);
12283 static int hscroll_windows (Lisp_Object);
12284
12285 /* For all leaf windows in the window tree rooted at WINDOW, set their
12286 hscroll value so that PT is (i) visible in the window, and (ii) so
12287 that it is not within a certain margin at the window's left and
12288 right border. Value is non-zero if any window's hscroll has been
12289 changed. */
12290
12291 static int
12292 hscroll_window_tree (Lisp_Object window)
12293 {
12294 int hscrolled_p = 0;
12295 int hscroll_relative_p = FLOATP (Vhscroll_step);
12296 int hscroll_step_abs = 0;
12297 double hscroll_step_rel = 0;
12298
12299 if (hscroll_relative_p)
12300 {
12301 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12302 if (hscroll_step_rel < 0)
12303 {
12304 hscroll_relative_p = 0;
12305 hscroll_step_abs = 0;
12306 }
12307 }
12308 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12309 {
12310 hscroll_step_abs = XINT (Vhscroll_step);
12311 if (hscroll_step_abs < 0)
12312 hscroll_step_abs = 0;
12313 }
12314 else
12315 hscroll_step_abs = 0;
12316
12317 while (WINDOWP (window))
12318 {
12319 struct window *w = XWINDOW (window);
12320
12321 if (WINDOWP (w->hchild))
12322 hscrolled_p |= hscroll_window_tree (w->hchild);
12323 else if (WINDOWP (w->vchild))
12324 hscrolled_p |= hscroll_window_tree (w->vchild);
12325 else if (w->cursor.vpos >= 0)
12326 {
12327 int h_margin;
12328 int text_area_width;
12329 struct glyph_row *current_cursor_row
12330 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12331 struct glyph_row *desired_cursor_row
12332 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12333 struct glyph_row *cursor_row
12334 = (desired_cursor_row->enabled_p
12335 ? desired_cursor_row
12336 : current_cursor_row);
12337 int row_r2l_p = cursor_row->reversed_p;
12338
12339 text_area_width = window_box_width (w, TEXT_AREA);
12340
12341 /* Scroll when cursor is inside this scroll margin. */
12342 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12343
12344 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12345 /* For left-to-right rows, hscroll when cursor is either
12346 (i) inside the right hscroll margin, or (ii) if it is
12347 inside the left margin and the window is already
12348 hscrolled. */
12349 && ((!row_r2l_p
12350 && ((w->hscroll
12351 && w->cursor.x <= h_margin)
12352 || (cursor_row->enabled_p
12353 && cursor_row->truncated_on_right_p
12354 && (w->cursor.x >= text_area_width - h_margin))))
12355 /* For right-to-left rows, the logic is similar,
12356 except that rules for scrolling to left and right
12357 are reversed. E.g., if cursor.x <= h_margin, we
12358 need to hscroll "to the right" unconditionally,
12359 and that will scroll the screen to the left so as
12360 to reveal the next portion of the row. */
12361 || (row_r2l_p
12362 && ((cursor_row->enabled_p
12363 /* FIXME: It is confusing to set the
12364 truncated_on_right_p flag when R2L rows
12365 are actually truncated on the left. */
12366 && cursor_row->truncated_on_right_p
12367 && w->cursor.x <= h_margin)
12368 || (w->hscroll
12369 && (w->cursor.x >= text_area_width - h_margin))))))
12370 {
12371 struct it it;
12372 ptrdiff_t hscroll;
12373 struct buffer *saved_current_buffer;
12374 ptrdiff_t pt;
12375 int wanted_x;
12376
12377 /* Find point in a display of infinite width. */
12378 saved_current_buffer = current_buffer;
12379 current_buffer = XBUFFER (w->buffer);
12380
12381 if (w == XWINDOW (selected_window))
12382 pt = PT;
12383 else
12384 {
12385 pt = marker_position (w->pointm);
12386 pt = max (BEGV, pt);
12387 pt = min (ZV, pt);
12388 }
12389
12390 /* Move iterator to pt starting at cursor_row->start in
12391 a line with infinite width. */
12392 init_to_row_start (&it, w, cursor_row);
12393 it.last_visible_x = INFINITY;
12394 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12395 current_buffer = saved_current_buffer;
12396
12397 /* Position cursor in window. */
12398 if (!hscroll_relative_p && hscroll_step_abs == 0)
12399 hscroll = max (0, (it.current_x
12400 - (ITERATOR_AT_END_OF_LINE_P (&it)
12401 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12402 : (text_area_width / 2))))
12403 / FRAME_COLUMN_WIDTH (it.f);
12404 else if ((!row_r2l_p
12405 && w->cursor.x >= text_area_width - h_margin)
12406 || (row_r2l_p && w->cursor.x <= h_margin))
12407 {
12408 if (hscroll_relative_p)
12409 wanted_x = text_area_width * (1 - hscroll_step_rel)
12410 - h_margin;
12411 else
12412 wanted_x = text_area_width
12413 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12414 - h_margin;
12415 hscroll
12416 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12417 }
12418 else
12419 {
12420 if (hscroll_relative_p)
12421 wanted_x = text_area_width * hscroll_step_rel
12422 + h_margin;
12423 else
12424 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12425 + h_margin;
12426 hscroll
12427 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12428 }
12429 hscroll = max (hscroll, w->min_hscroll);
12430
12431 /* Don't prevent redisplay optimizations if hscroll
12432 hasn't changed, as it will unnecessarily slow down
12433 redisplay. */
12434 if (w->hscroll != hscroll)
12435 {
12436 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12437 w->hscroll = hscroll;
12438 hscrolled_p = 1;
12439 }
12440 }
12441 }
12442
12443 window = w->next;
12444 }
12445
12446 /* Value is non-zero if hscroll of any leaf window has been changed. */
12447 return hscrolled_p;
12448 }
12449
12450
12451 /* Set hscroll so that cursor is visible and not inside horizontal
12452 scroll margins for all windows in the tree rooted at WINDOW. See
12453 also hscroll_window_tree above. Value is non-zero if any window's
12454 hscroll has been changed. If it has, desired matrices on the frame
12455 of WINDOW are cleared. */
12456
12457 static int
12458 hscroll_windows (Lisp_Object window)
12459 {
12460 int hscrolled_p = hscroll_window_tree (window);
12461 if (hscrolled_p)
12462 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12463 return hscrolled_p;
12464 }
12465
12466
12467 \f
12468 /************************************************************************
12469 Redisplay
12470 ************************************************************************/
12471
12472 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12473 to a non-zero value. This is sometimes handy to have in a debugger
12474 session. */
12475
12476 #ifdef GLYPH_DEBUG
12477
12478 /* First and last unchanged row for try_window_id. */
12479
12480 static int debug_first_unchanged_at_end_vpos;
12481 static int debug_last_unchanged_at_beg_vpos;
12482
12483 /* Delta vpos and y. */
12484
12485 static int debug_dvpos, debug_dy;
12486
12487 /* Delta in characters and bytes for try_window_id. */
12488
12489 static ptrdiff_t debug_delta, debug_delta_bytes;
12490
12491 /* Values of window_end_pos and window_end_vpos at the end of
12492 try_window_id. */
12493
12494 static ptrdiff_t debug_end_vpos;
12495
12496 /* Append a string to W->desired_matrix->method. FMT is a printf
12497 format string. If trace_redisplay_p is non-zero also printf the
12498 resulting string to stderr. */
12499
12500 static void debug_method_add (struct window *, char const *, ...)
12501 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12502
12503 static void
12504 debug_method_add (struct window *w, char const *fmt, ...)
12505 {
12506 char *method = w->desired_matrix->method;
12507 int len = strlen (method);
12508 int size = sizeof w->desired_matrix->method;
12509 int remaining = size - len - 1;
12510 va_list ap;
12511
12512 if (len && remaining)
12513 {
12514 method[len] = '|';
12515 --remaining, ++len;
12516 }
12517
12518 va_start (ap, fmt);
12519 vsnprintf (method + len, remaining + 1, fmt, ap);
12520 va_end (ap);
12521
12522 if (trace_redisplay_p)
12523 fprintf (stderr, "%p (%s): %s\n",
12524 w,
12525 ((BUFFERP (w->buffer)
12526 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12527 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12528 : "no buffer"),
12529 method + len);
12530 }
12531
12532 #endif /* GLYPH_DEBUG */
12533
12534
12535 /* Value is non-zero if all changes in window W, which displays
12536 current_buffer, are in the text between START and END. START is a
12537 buffer position, END is given as a distance from Z. Used in
12538 redisplay_internal for display optimization. */
12539
12540 static inline int
12541 text_outside_line_unchanged_p (struct window *w,
12542 ptrdiff_t start, ptrdiff_t end)
12543 {
12544 int unchanged_p = 1;
12545
12546 /* If text or overlays have changed, see where. */
12547 if (w->last_modified < MODIFF
12548 || w->last_overlay_modified < OVERLAY_MODIFF)
12549 {
12550 /* Gap in the line? */
12551 if (GPT < start || Z - GPT < end)
12552 unchanged_p = 0;
12553
12554 /* Changes start in front of the line, or end after it? */
12555 if (unchanged_p
12556 && (BEG_UNCHANGED < start - 1
12557 || END_UNCHANGED < end))
12558 unchanged_p = 0;
12559
12560 /* If selective display, can't optimize if changes start at the
12561 beginning of the line. */
12562 if (unchanged_p
12563 && INTEGERP (BVAR (current_buffer, selective_display))
12564 && XINT (BVAR (current_buffer, selective_display)) > 0
12565 && (BEG_UNCHANGED < start || GPT <= start))
12566 unchanged_p = 0;
12567
12568 /* If there are overlays at the start or end of the line, these
12569 may have overlay strings with newlines in them. A change at
12570 START, for instance, may actually concern the display of such
12571 overlay strings as well, and they are displayed on different
12572 lines. So, quickly rule out this case. (For the future, it
12573 might be desirable to implement something more telling than
12574 just BEG/END_UNCHANGED.) */
12575 if (unchanged_p)
12576 {
12577 if (BEG + BEG_UNCHANGED == start
12578 && overlay_touches_p (start))
12579 unchanged_p = 0;
12580 if (END_UNCHANGED == end
12581 && overlay_touches_p (Z - end))
12582 unchanged_p = 0;
12583 }
12584
12585 /* Under bidi reordering, adding or deleting a character in the
12586 beginning of a paragraph, before the first strong directional
12587 character, can change the base direction of the paragraph (unless
12588 the buffer specifies a fixed paragraph direction), which will
12589 require to redisplay the whole paragraph. It might be worthwhile
12590 to find the paragraph limits and widen the range of redisplayed
12591 lines to that, but for now just give up this optimization. */
12592 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12593 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12594 unchanged_p = 0;
12595 }
12596
12597 return unchanged_p;
12598 }
12599
12600
12601 /* Do a frame update, taking possible shortcuts into account. This is
12602 the main external entry point for redisplay.
12603
12604 If the last redisplay displayed an echo area message and that message
12605 is no longer requested, we clear the echo area or bring back the
12606 mini-buffer if that is in use. */
12607
12608 void
12609 redisplay (void)
12610 {
12611 redisplay_internal ();
12612 }
12613
12614
12615 static Lisp_Object
12616 overlay_arrow_string_or_property (Lisp_Object var)
12617 {
12618 Lisp_Object val;
12619
12620 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12621 return val;
12622
12623 return Voverlay_arrow_string;
12624 }
12625
12626 /* Return 1 if there are any overlay-arrows in current_buffer. */
12627 static int
12628 overlay_arrow_in_current_buffer_p (void)
12629 {
12630 Lisp_Object vlist;
12631
12632 for (vlist = Voverlay_arrow_variable_list;
12633 CONSP (vlist);
12634 vlist = XCDR (vlist))
12635 {
12636 Lisp_Object var = XCAR (vlist);
12637 Lisp_Object val;
12638
12639 if (!SYMBOLP (var))
12640 continue;
12641 val = find_symbol_value (var);
12642 if (MARKERP (val)
12643 && current_buffer == XMARKER (val)->buffer)
12644 return 1;
12645 }
12646 return 0;
12647 }
12648
12649
12650 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12651 has changed. */
12652
12653 static int
12654 overlay_arrows_changed_p (void)
12655 {
12656 Lisp_Object vlist;
12657
12658 for (vlist = Voverlay_arrow_variable_list;
12659 CONSP (vlist);
12660 vlist = XCDR (vlist))
12661 {
12662 Lisp_Object var = XCAR (vlist);
12663 Lisp_Object val, pstr;
12664
12665 if (!SYMBOLP (var))
12666 continue;
12667 val = find_symbol_value (var);
12668 if (!MARKERP (val))
12669 continue;
12670 if (! EQ (COERCE_MARKER (val),
12671 Fget (var, Qlast_arrow_position))
12672 || ! (pstr = overlay_arrow_string_or_property (var),
12673 EQ (pstr, Fget (var, Qlast_arrow_string))))
12674 return 1;
12675 }
12676 return 0;
12677 }
12678
12679 /* Mark overlay arrows to be updated on next redisplay. */
12680
12681 static void
12682 update_overlay_arrows (int up_to_date)
12683 {
12684 Lisp_Object vlist;
12685
12686 for (vlist = Voverlay_arrow_variable_list;
12687 CONSP (vlist);
12688 vlist = XCDR (vlist))
12689 {
12690 Lisp_Object var = XCAR (vlist);
12691
12692 if (!SYMBOLP (var))
12693 continue;
12694
12695 if (up_to_date > 0)
12696 {
12697 Lisp_Object val = find_symbol_value (var);
12698 Fput (var, Qlast_arrow_position,
12699 COERCE_MARKER (val));
12700 Fput (var, Qlast_arrow_string,
12701 overlay_arrow_string_or_property (var));
12702 }
12703 else if (up_to_date < 0
12704 || !NILP (Fget (var, Qlast_arrow_position)))
12705 {
12706 Fput (var, Qlast_arrow_position, Qt);
12707 Fput (var, Qlast_arrow_string, Qt);
12708 }
12709 }
12710 }
12711
12712
12713 /* Return overlay arrow string to display at row.
12714 Return integer (bitmap number) for arrow bitmap in left fringe.
12715 Return nil if no overlay arrow. */
12716
12717 static Lisp_Object
12718 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12719 {
12720 Lisp_Object vlist;
12721
12722 for (vlist = Voverlay_arrow_variable_list;
12723 CONSP (vlist);
12724 vlist = XCDR (vlist))
12725 {
12726 Lisp_Object var = XCAR (vlist);
12727 Lisp_Object val;
12728
12729 if (!SYMBOLP (var))
12730 continue;
12731
12732 val = find_symbol_value (var);
12733
12734 if (MARKERP (val)
12735 && current_buffer == XMARKER (val)->buffer
12736 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12737 {
12738 if (FRAME_WINDOW_P (it->f)
12739 /* FIXME: if ROW->reversed_p is set, this should test
12740 the right fringe, not the left one. */
12741 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12742 {
12743 #ifdef HAVE_WINDOW_SYSTEM
12744 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12745 {
12746 int fringe_bitmap;
12747 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12748 return make_number (fringe_bitmap);
12749 }
12750 #endif
12751 return make_number (-1); /* Use default arrow bitmap */
12752 }
12753 return overlay_arrow_string_or_property (var);
12754 }
12755 }
12756
12757 return Qnil;
12758 }
12759
12760 /* Return 1 if point moved out of or into a composition. Otherwise
12761 return 0. PREV_BUF and PREV_PT are the last point buffer and
12762 position. BUF and PT are the current point buffer and position. */
12763
12764 static int
12765 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12766 struct buffer *buf, ptrdiff_t pt)
12767 {
12768 ptrdiff_t start, end;
12769 Lisp_Object prop;
12770 Lisp_Object buffer;
12771
12772 XSETBUFFER (buffer, buf);
12773 /* Check a composition at the last point if point moved within the
12774 same buffer. */
12775 if (prev_buf == buf)
12776 {
12777 if (prev_pt == pt)
12778 /* Point didn't move. */
12779 return 0;
12780
12781 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12782 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12783 && COMPOSITION_VALID_P (start, end, prop)
12784 && start < prev_pt && end > prev_pt)
12785 /* The last point was within the composition. Return 1 iff
12786 point moved out of the composition. */
12787 return (pt <= start || pt >= end);
12788 }
12789
12790 /* Check a composition at the current point. */
12791 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12792 && find_composition (pt, -1, &start, &end, &prop, buffer)
12793 && COMPOSITION_VALID_P (start, end, prop)
12794 && start < pt && end > pt);
12795 }
12796
12797
12798 /* Reconsider the setting of B->clip_changed which is displayed
12799 in window W. */
12800
12801 static inline void
12802 reconsider_clip_changes (struct window *w, struct buffer *b)
12803 {
12804 if (b->clip_changed
12805 && !NILP (w->window_end_valid)
12806 && w->current_matrix->buffer == b
12807 && w->current_matrix->zv == BUF_ZV (b)
12808 && w->current_matrix->begv == BUF_BEGV (b))
12809 b->clip_changed = 0;
12810
12811 /* If display wasn't paused, and W is not a tool bar window, see if
12812 point has been moved into or out of a composition. In that case,
12813 we set b->clip_changed to 1 to force updating the screen. If
12814 b->clip_changed has already been set to 1, we can skip this
12815 check. */
12816 if (!b->clip_changed
12817 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12818 {
12819 ptrdiff_t pt;
12820
12821 if (w == XWINDOW (selected_window))
12822 pt = PT;
12823 else
12824 pt = marker_position (w->pointm);
12825
12826 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12827 || pt != w->last_point)
12828 && check_point_in_composition (w->current_matrix->buffer,
12829 w->last_point,
12830 XBUFFER (w->buffer), pt))
12831 b->clip_changed = 1;
12832 }
12833 }
12834 \f
12835
12836 /* Select FRAME to forward the values of frame-local variables into C
12837 variables so that the redisplay routines can access those values
12838 directly. */
12839
12840 static void
12841 select_frame_for_redisplay (Lisp_Object frame)
12842 {
12843 Lisp_Object tail, tem;
12844 Lisp_Object old = selected_frame;
12845 struct Lisp_Symbol *sym;
12846
12847 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12848
12849 selected_frame = frame;
12850
12851 do {
12852 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12853 if (CONSP (XCAR (tail))
12854 && (tem = XCAR (XCAR (tail)),
12855 SYMBOLP (tem))
12856 && (sym = indirect_variable (XSYMBOL (tem)),
12857 sym->redirect == SYMBOL_LOCALIZED)
12858 && sym->val.blv->frame_local)
12859 /* Use find_symbol_value rather than Fsymbol_value
12860 to avoid an error if it is void. */
12861 find_symbol_value (tem);
12862 } while (!EQ (frame, old) && (frame = old, 1));
12863 }
12864
12865
12866 #define STOP_POLLING \
12867 do { if (! polling_stopped_here) stop_polling (); \
12868 polling_stopped_here = 1; } while (0)
12869
12870 #define RESUME_POLLING \
12871 do { if (polling_stopped_here) start_polling (); \
12872 polling_stopped_here = 0; } while (0)
12873
12874
12875 /* Perhaps in the future avoid recentering windows if it
12876 is not necessary; currently that causes some problems. */
12877
12878 static void
12879 redisplay_internal (void)
12880 {
12881 struct window *w = XWINDOW (selected_window);
12882 struct window *sw;
12883 struct frame *fr;
12884 int pending;
12885 int must_finish = 0;
12886 struct text_pos tlbufpos, tlendpos;
12887 int number_of_visible_frames;
12888 ptrdiff_t count, count1;
12889 struct frame *sf;
12890 int polling_stopped_here = 0;
12891 Lisp_Object old_frame = selected_frame;
12892
12893 /* Non-zero means redisplay has to consider all windows on all
12894 frames. Zero means, only selected_window is considered. */
12895 int consider_all_windows_p;
12896
12897 /* Non-zero means redisplay has to redisplay the miniwindow */
12898 int update_miniwindow_p = 0;
12899
12900 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12901
12902 /* No redisplay if running in batch mode or frame is not yet fully
12903 initialized, or redisplay is explicitly turned off by setting
12904 Vinhibit_redisplay. */
12905 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12906 || !NILP (Vinhibit_redisplay))
12907 return;
12908
12909 /* Don't examine these until after testing Vinhibit_redisplay.
12910 When Emacs is shutting down, perhaps because its connection to
12911 X has dropped, we should not look at them at all. */
12912 fr = XFRAME (w->frame);
12913 sf = SELECTED_FRAME ();
12914
12915 if (!fr->glyphs_initialized_p)
12916 return;
12917
12918 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12919 if (popup_activated ())
12920 return;
12921 #endif
12922
12923 /* I don't think this happens but let's be paranoid. */
12924 if (redisplaying_p)
12925 return;
12926
12927 /* Record a function that resets redisplaying_p to its old value
12928 when we leave this function. */
12929 count = SPECPDL_INDEX ();
12930 record_unwind_protect (unwind_redisplay,
12931 Fcons (make_number (redisplaying_p), selected_frame));
12932 ++redisplaying_p;
12933 specbind (Qinhibit_free_realized_faces, Qnil);
12934
12935 {
12936 Lisp_Object tail, frame;
12937
12938 FOR_EACH_FRAME (tail, frame)
12939 {
12940 struct frame *f = XFRAME (frame);
12941 f->already_hscrolled_p = 0;
12942 }
12943 }
12944
12945 retry:
12946 /* Remember the currently selected window. */
12947 sw = w;
12948
12949 if (!EQ (old_frame, selected_frame)
12950 && FRAME_LIVE_P (XFRAME (old_frame)))
12951 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12952 selected_frame and selected_window to be temporarily out-of-sync so
12953 when we come back here via `goto retry', we need to resync because we
12954 may need to run Elisp code (via prepare_menu_bars). */
12955 select_frame_for_redisplay (old_frame);
12956
12957 pending = 0;
12958 reconsider_clip_changes (w, current_buffer);
12959 last_escape_glyph_frame = NULL;
12960 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12961 last_glyphless_glyph_frame = NULL;
12962 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12963
12964 /* If new fonts have been loaded that make a glyph matrix adjustment
12965 necessary, do it. */
12966 if (fonts_changed_p)
12967 {
12968 adjust_glyphs (NULL);
12969 ++windows_or_buffers_changed;
12970 fonts_changed_p = 0;
12971 }
12972
12973 /* If face_change_count is non-zero, init_iterator will free all
12974 realized faces, which includes the faces referenced from current
12975 matrices. So, we can't reuse current matrices in this case. */
12976 if (face_change_count)
12977 ++windows_or_buffers_changed;
12978
12979 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12980 && FRAME_TTY (sf)->previous_frame != sf)
12981 {
12982 /* Since frames on a single ASCII terminal share the same
12983 display area, displaying a different frame means redisplay
12984 the whole thing. */
12985 windows_or_buffers_changed++;
12986 SET_FRAME_GARBAGED (sf);
12987 #ifndef DOS_NT
12988 set_tty_color_mode (FRAME_TTY (sf), sf);
12989 #endif
12990 FRAME_TTY (sf)->previous_frame = sf;
12991 }
12992
12993 /* Set the visible flags for all frames. Do this before checking
12994 for resized or garbaged frames; they want to know if their frames
12995 are visible. See the comment in frame.h for
12996 FRAME_SAMPLE_VISIBILITY. */
12997 {
12998 Lisp_Object tail, frame;
12999
13000 number_of_visible_frames = 0;
13001
13002 FOR_EACH_FRAME (tail, frame)
13003 {
13004 struct frame *f = XFRAME (frame);
13005
13006 FRAME_SAMPLE_VISIBILITY (f);
13007 if (FRAME_VISIBLE_P (f))
13008 ++number_of_visible_frames;
13009 clear_desired_matrices (f);
13010 }
13011 }
13012
13013 /* Notice any pending interrupt request to change frame size. */
13014 do_pending_window_change (1);
13015
13016 /* do_pending_window_change could change the selected_window due to
13017 frame resizing which makes the selected window too small. */
13018 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13019 {
13020 sw = w;
13021 reconsider_clip_changes (w, current_buffer);
13022 }
13023
13024 /* Clear frames marked as garbaged. */
13025 if (frame_garbaged)
13026 clear_garbaged_frames ();
13027
13028 /* Build menubar and tool-bar items. */
13029 if (NILP (Vmemory_full))
13030 prepare_menu_bars ();
13031
13032 if (windows_or_buffers_changed)
13033 update_mode_lines++;
13034
13035 /* Detect case that we need to write or remove a star in the mode line. */
13036 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13037 {
13038 w->update_mode_line = 1;
13039 if (buffer_shared > 1)
13040 update_mode_lines++;
13041 }
13042
13043 /* Avoid invocation of point motion hooks by `current_column' below. */
13044 count1 = SPECPDL_INDEX ();
13045 specbind (Qinhibit_point_motion_hooks, Qt);
13046
13047 /* If %c is in the mode line, update it if needed. */
13048 if (!NILP (w->column_number_displayed)
13049 /* This alternative quickly identifies a common case
13050 where no change is needed. */
13051 && !(PT == w->last_point
13052 && w->last_modified >= MODIFF
13053 && w->last_overlay_modified >= OVERLAY_MODIFF)
13054 && (XFASTINT (w->column_number_displayed) != current_column ()))
13055 w->update_mode_line = 1;
13056
13057 unbind_to (count1, Qnil);
13058
13059 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13060
13061 /* The variable buffer_shared is set in redisplay_window and
13062 indicates that we redisplay a buffer in different windows. See
13063 there. */
13064 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13065 || cursor_type_changed);
13066
13067 /* If specs for an arrow have changed, do thorough redisplay
13068 to ensure we remove any arrow that should no longer exist. */
13069 if (overlay_arrows_changed_p ())
13070 consider_all_windows_p = windows_or_buffers_changed = 1;
13071
13072 /* Normally the message* functions will have already displayed and
13073 updated the echo area, but the frame may have been trashed, or
13074 the update may have been preempted, so display the echo area
13075 again here. Checking message_cleared_p captures the case that
13076 the echo area should be cleared. */
13077 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13078 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13079 || (message_cleared_p
13080 && minibuf_level == 0
13081 /* If the mini-window is currently selected, this means the
13082 echo-area doesn't show through. */
13083 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13084 {
13085 int window_height_changed_p = echo_area_display (0);
13086
13087 if (message_cleared_p)
13088 update_miniwindow_p = 1;
13089
13090 must_finish = 1;
13091
13092 /* If we don't display the current message, don't clear the
13093 message_cleared_p flag, because, if we did, we wouldn't clear
13094 the echo area in the next redisplay which doesn't preserve
13095 the echo area. */
13096 if (!display_last_displayed_message_p)
13097 message_cleared_p = 0;
13098
13099 if (fonts_changed_p)
13100 goto retry;
13101 else if (window_height_changed_p)
13102 {
13103 consider_all_windows_p = 1;
13104 ++update_mode_lines;
13105 ++windows_or_buffers_changed;
13106
13107 /* If window configuration was changed, frames may have been
13108 marked garbaged. Clear them or we will experience
13109 surprises wrt scrolling. */
13110 if (frame_garbaged)
13111 clear_garbaged_frames ();
13112 }
13113 }
13114 else if (EQ (selected_window, minibuf_window)
13115 && (current_buffer->clip_changed
13116 || w->last_modified < MODIFF
13117 || w->last_overlay_modified < OVERLAY_MODIFF)
13118 && resize_mini_window (w, 0))
13119 {
13120 /* Resized active mini-window to fit the size of what it is
13121 showing if its contents might have changed. */
13122 must_finish = 1;
13123 /* FIXME: this causes all frames to be updated, which seems unnecessary
13124 since only the current frame needs to be considered. This function needs
13125 to be rewritten with two variables, consider_all_windows and
13126 consider_all_frames. */
13127 consider_all_windows_p = 1;
13128 ++windows_or_buffers_changed;
13129 ++update_mode_lines;
13130
13131 /* If window configuration was changed, frames may have been
13132 marked garbaged. Clear them or we will experience
13133 surprises wrt scrolling. */
13134 if (frame_garbaged)
13135 clear_garbaged_frames ();
13136 }
13137
13138
13139 /* If showing the region, and mark has changed, we must redisplay
13140 the whole window. The assignment to this_line_start_pos prevents
13141 the optimization directly below this if-statement. */
13142 if (((!NILP (Vtransient_mark_mode)
13143 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13144 != !NILP (w->region_showing))
13145 || (!NILP (w->region_showing)
13146 && !EQ (w->region_showing,
13147 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13148 CHARPOS (this_line_start_pos) = 0;
13149
13150 /* Optimize the case that only the line containing the cursor in the
13151 selected window has changed. Variables starting with this_ are
13152 set in display_line and record information about the line
13153 containing the cursor. */
13154 tlbufpos = this_line_start_pos;
13155 tlendpos = this_line_end_pos;
13156 if (!consider_all_windows_p
13157 && CHARPOS (tlbufpos) > 0
13158 && !w->update_mode_line
13159 && !current_buffer->clip_changed
13160 && !current_buffer->prevent_redisplay_optimizations_p
13161 && FRAME_VISIBLE_P (XFRAME (w->frame))
13162 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13163 /* Make sure recorded data applies to current buffer, etc. */
13164 && this_line_buffer == current_buffer
13165 && current_buffer == XBUFFER (w->buffer)
13166 && !w->force_start
13167 && !w->optional_new_start
13168 /* Point must be on the line that we have info recorded about. */
13169 && PT >= CHARPOS (tlbufpos)
13170 && PT <= Z - CHARPOS (tlendpos)
13171 /* All text outside that line, including its final newline,
13172 must be unchanged. */
13173 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13174 CHARPOS (tlendpos)))
13175 {
13176 if (CHARPOS (tlbufpos) > BEGV
13177 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13178 && (CHARPOS (tlbufpos) == ZV
13179 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13180 /* Former continuation line has disappeared by becoming empty. */
13181 goto cancel;
13182 else if (w->last_modified < MODIFF
13183 || w->last_overlay_modified < OVERLAY_MODIFF
13184 || MINI_WINDOW_P (w))
13185 {
13186 /* We have to handle the case of continuation around a
13187 wide-column character (see the comment in indent.c around
13188 line 1340).
13189
13190 For instance, in the following case:
13191
13192 -------- Insert --------
13193 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13194 J_I_ ==> J_I_ `^^' are cursors.
13195 ^^ ^^
13196 -------- --------
13197
13198 As we have to redraw the line above, we cannot use this
13199 optimization. */
13200
13201 struct it it;
13202 int line_height_before = this_line_pixel_height;
13203
13204 /* Note that start_display will handle the case that the
13205 line starting at tlbufpos is a continuation line. */
13206 start_display (&it, w, tlbufpos);
13207
13208 /* Implementation note: It this still necessary? */
13209 if (it.current_x != this_line_start_x)
13210 goto cancel;
13211
13212 TRACE ((stderr, "trying display optimization 1\n"));
13213 w->cursor.vpos = -1;
13214 overlay_arrow_seen = 0;
13215 it.vpos = this_line_vpos;
13216 it.current_y = this_line_y;
13217 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13218 display_line (&it);
13219
13220 /* If line contains point, is not continued,
13221 and ends at same distance from eob as before, we win. */
13222 if (w->cursor.vpos >= 0
13223 /* Line is not continued, otherwise this_line_start_pos
13224 would have been set to 0 in display_line. */
13225 && CHARPOS (this_line_start_pos)
13226 /* Line ends as before. */
13227 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13228 /* Line has same height as before. Otherwise other lines
13229 would have to be shifted up or down. */
13230 && this_line_pixel_height == line_height_before)
13231 {
13232 /* If this is not the window's last line, we must adjust
13233 the charstarts of the lines below. */
13234 if (it.current_y < it.last_visible_y)
13235 {
13236 struct glyph_row *row
13237 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13238 ptrdiff_t delta, delta_bytes;
13239
13240 /* We used to distinguish between two cases here,
13241 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13242 when the line ends in a newline or the end of the
13243 buffer's accessible portion. But both cases did
13244 the same, so they were collapsed. */
13245 delta = (Z
13246 - CHARPOS (tlendpos)
13247 - MATRIX_ROW_START_CHARPOS (row));
13248 delta_bytes = (Z_BYTE
13249 - BYTEPOS (tlendpos)
13250 - MATRIX_ROW_START_BYTEPOS (row));
13251
13252 increment_matrix_positions (w->current_matrix,
13253 this_line_vpos + 1,
13254 w->current_matrix->nrows,
13255 delta, delta_bytes);
13256 }
13257
13258 /* If this row displays text now but previously didn't,
13259 or vice versa, w->window_end_vpos may have to be
13260 adjusted. */
13261 if ((it.glyph_row - 1)->displays_text_p)
13262 {
13263 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13264 XSETINT (w->window_end_vpos, this_line_vpos);
13265 }
13266 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13267 && this_line_vpos > 0)
13268 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13269 w->window_end_valid = Qnil;
13270
13271 /* Update hint: No need to try to scroll in update_window. */
13272 w->desired_matrix->no_scrolling_p = 1;
13273
13274 #ifdef GLYPH_DEBUG
13275 *w->desired_matrix->method = 0;
13276 debug_method_add (w, "optimization 1");
13277 #endif
13278 #ifdef HAVE_WINDOW_SYSTEM
13279 update_window_fringes (w, 0);
13280 #endif
13281 goto update;
13282 }
13283 else
13284 goto cancel;
13285 }
13286 else if (/* Cursor position hasn't changed. */
13287 PT == w->last_point
13288 /* Make sure the cursor was last displayed
13289 in this window. Otherwise we have to reposition it. */
13290 && 0 <= w->cursor.vpos
13291 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13292 {
13293 if (!must_finish)
13294 {
13295 do_pending_window_change (1);
13296 /* If selected_window changed, redisplay again. */
13297 if (WINDOWP (selected_window)
13298 && (w = XWINDOW (selected_window)) != sw)
13299 goto retry;
13300
13301 /* We used to always goto end_of_redisplay here, but this
13302 isn't enough if we have a blinking cursor. */
13303 if (w->cursor_off_p == w->last_cursor_off_p)
13304 goto end_of_redisplay;
13305 }
13306 goto update;
13307 }
13308 /* If highlighting the region, or if the cursor is in the echo area,
13309 then we can't just move the cursor. */
13310 else if (! (!NILP (Vtransient_mark_mode)
13311 && !NILP (BVAR (current_buffer, mark_active)))
13312 && (EQ (selected_window,
13313 BVAR (current_buffer, last_selected_window))
13314 || highlight_nonselected_windows)
13315 && NILP (w->region_showing)
13316 && NILP (Vshow_trailing_whitespace)
13317 && !cursor_in_echo_area)
13318 {
13319 struct it it;
13320 struct glyph_row *row;
13321
13322 /* Skip from tlbufpos to PT and see where it is. Note that
13323 PT may be in invisible text. If so, we will end at the
13324 next visible position. */
13325 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13326 NULL, DEFAULT_FACE_ID);
13327 it.current_x = this_line_start_x;
13328 it.current_y = this_line_y;
13329 it.vpos = this_line_vpos;
13330
13331 /* The call to move_it_to stops in front of PT, but
13332 moves over before-strings. */
13333 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13334
13335 if (it.vpos == this_line_vpos
13336 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13337 row->enabled_p))
13338 {
13339 eassert (this_line_vpos == it.vpos);
13340 eassert (this_line_y == it.current_y);
13341 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13342 #ifdef GLYPH_DEBUG
13343 *w->desired_matrix->method = 0;
13344 debug_method_add (w, "optimization 3");
13345 #endif
13346 goto update;
13347 }
13348 else
13349 goto cancel;
13350 }
13351
13352 cancel:
13353 /* Text changed drastically or point moved off of line. */
13354 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13355 }
13356
13357 CHARPOS (this_line_start_pos) = 0;
13358 consider_all_windows_p |= buffer_shared > 1;
13359 ++clear_face_cache_count;
13360 #ifdef HAVE_WINDOW_SYSTEM
13361 ++clear_image_cache_count;
13362 #endif
13363
13364 /* Build desired matrices, and update the display. If
13365 consider_all_windows_p is non-zero, do it for all windows on all
13366 frames. Otherwise do it for selected_window, only. */
13367
13368 if (consider_all_windows_p)
13369 {
13370 Lisp_Object tail, frame;
13371
13372 FOR_EACH_FRAME (tail, frame)
13373 XFRAME (frame)->updated_p = 0;
13374
13375 /* Recompute # windows showing selected buffer. This will be
13376 incremented each time such a window is displayed. */
13377 buffer_shared = 0;
13378
13379 FOR_EACH_FRAME (tail, frame)
13380 {
13381 struct frame *f = XFRAME (frame);
13382
13383 /* We don't have to do anything for unselected terminal
13384 frames. */
13385 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13386 && !EQ (FRAME_TTY (f)->top_frame, frame))
13387 continue;
13388
13389 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13390 {
13391 if (! EQ (frame, selected_frame))
13392 /* Select the frame, for the sake of frame-local
13393 variables. */
13394 select_frame_for_redisplay (frame);
13395
13396 /* Mark all the scroll bars to be removed; we'll redeem
13397 the ones we want when we redisplay their windows. */
13398 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13399 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13400
13401 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13402 redisplay_windows (FRAME_ROOT_WINDOW (f));
13403
13404 /* The X error handler may have deleted that frame. */
13405 if (!FRAME_LIVE_P (f))
13406 continue;
13407
13408 /* Any scroll bars which redisplay_windows should have
13409 nuked should now go away. */
13410 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13411 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13412
13413 /* If fonts changed, display again. */
13414 /* ??? rms: I suspect it is a mistake to jump all the way
13415 back to retry here. It should just retry this frame. */
13416 if (fonts_changed_p)
13417 goto retry;
13418
13419 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13420 {
13421 /* See if we have to hscroll. */
13422 if (!f->already_hscrolled_p)
13423 {
13424 f->already_hscrolled_p = 1;
13425 if (hscroll_windows (f->root_window))
13426 goto retry;
13427 }
13428
13429 /* Prevent various kinds of signals during display
13430 update. stdio is not robust about handling
13431 signals, which can cause an apparent I/O
13432 error. */
13433 if (interrupt_input)
13434 unrequest_sigio ();
13435 STOP_POLLING;
13436
13437 /* Update the display. */
13438 set_window_update_flags (XWINDOW (f->root_window), 1);
13439 pending |= update_frame (f, 0, 0);
13440 f->updated_p = 1;
13441 }
13442 }
13443 }
13444
13445 if (!EQ (old_frame, selected_frame)
13446 && FRAME_LIVE_P (XFRAME (old_frame)))
13447 /* We played a bit fast-and-loose above and allowed selected_frame
13448 and selected_window to be temporarily out-of-sync but let's make
13449 sure this stays contained. */
13450 select_frame_for_redisplay (old_frame);
13451 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13452
13453 if (!pending)
13454 {
13455 /* Do the mark_window_display_accurate after all windows have
13456 been redisplayed because this call resets flags in buffers
13457 which are needed for proper redisplay. */
13458 FOR_EACH_FRAME (tail, frame)
13459 {
13460 struct frame *f = XFRAME (frame);
13461 if (f->updated_p)
13462 {
13463 mark_window_display_accurate (f->root_window, 1);
13464 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13465 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13466 }
13467 }
13468 }
13469 }
13470 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13471 {
13472 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13473 struct frame *mini_frame;
13474
13475 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13476 /* Use list_of_error, not Qerror, so that
13477 we catch only errors and don't run the debugger. */
13478 internal_condition_case_1 (redisplay_window_1, selected_window,
13479 list_of_error,
13480 redisplay_window_error);
13481 if (update_miniwindow_p)
13482 internal_condition_case_1 (redisplay_window_1, mini_window,
13483 list_of_error,
13484 redisplay_window_error);
13485
13486 /* Compare desired and current matrices, perform output. */
13487
13488 update:
13489 /* If fonts changed, display again. */
13490 if (fonts_changed_p)
13491 goto retry;
13492
13493 /* Prevent various kinds of signals during display update.
13494 stdio is not robust about handling signals,
13495 which can cause an apparent I/O error. */
13496 if (interrupt_input)
13497 unrequest_sigio ();
13498 STOP_POLLING;
13499
13500 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13501 {
13502 if (hscroll_windows (selected_window))
13503 goto retry;
13504
13505 XWINDOW (selected_window)->must_be_updated_p = 1;
13506 pending = update_frame (sf, 0, 0);
13507 }
13508
13509 /* We may have called echo_area_display at the top of this
13510 function. If the echo area is on another frame, that may
13511 have put text on a frame other than the selected one, so the
13512 above call to update_frame would not have caught it. Catch
13513 it here. */
13514 mini_window = FRAME_MINIBUF_WINDOW (sf);
13515 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13516
13517 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13518 {
13519 XWINDOW (mini_window)->must_be_updated_p = 1;
13520 pending |= update_frame (mini_frame, 0, 0);
13521 if (!pending && hscroll_windows (mini_window))
13522 goto retry;
13523 }
13524 }
13525
13526 /* If display was paused because of pending input, make sure we do a
13527 thorough update the next time. */
13528 if (pending)
13529 {
13530 /* Prevent the optimization at the beginning of
13531 redisplay_internal that tries a single-line update of the
13532 line containing the cursor in the selected window. */
13533 CHARPOS (this_line_start_pos) = 0;
13534
13535 /* Let the overlay arrow be updated the next time. */
13536 update_overlay_arrows (0);
13537
13538 /* If we pause after scrolling, some rows in the current
13539 matrices of some windows are not valid. */
13540 if (!WINDOW_FULL_WIDTH_P (w)
13541 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13542 update_mode_lines = 1;
13543 }
13544 else
13545 {
13546 if (!consider_all_windows_p)
13547 {
13548 /* This has already been done above if
13549 consider_all_windows_p is set. */
13550 mark_window_display_accurate_1 (w, 1);
13551
13552 /* Say overlay arrows are up to date. */
13553 update_overlay_arrows (1);
13554
13555 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13556 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13557 }
13558
13559 update_mode_lines = 0;
13560 windows_or_buffers_changed = 0;
13561 cursor_type_changed = 0;
13562 }
13563
13564 /* Start SIGIO interrupts coming again. Having them off during the
13565 code above makes it less likely one will discard output, but not
13566 impossible, since there might be stuff in the system buffer here.
13567 But it is much hairier to try to do anything about that. */
13568 if (interrupt_input)
13569 request_sigio ();
13570 RESUME_POLLING;
13571
13572 /* If a frame has become visible which was not before, redisplay
13573 again, so that we display it. Expose events for such a frame
13574 (which it gets when becoming visible) don't call the parts of
13575 redisplay constructing glyphs, so simply exposing a frame won't
13576 display anything in this case. So, we have to display these
13577 frames here explicitly. */
13578 if (!pending)
13579 {
13580 Lisp_Object tail, frame;
13581 int new_count = 0;
13582
13583 FOR_EACH_FRAME (tail, frame)
13584 {
13585 int this_is_visible = 0;
13586
13587 if (XFRAME (frame)->visible)
13588 this_is_visible = 1;
13589 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13590 if (XFRAME (frame)->visible)
13591 this_is_visible = 1;
13592
13593 if (this_is_visible)
13594 new_count++;
13595 }
13596
13597 if (new_count != number_of_visible_frames)
13598 windows_or_buffers_changed++;
13599 }
13600
13601 /* Change frame size now if a change is pending. */
13602 do_pending_window_change (1);
13603
13604 /* If we just did a pending size change, or have additional
13605 visible frames, or selected_window changed, redisplay again. */
13606 if ((windows_or_buffers_changed && !pending)
13607 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13608 goto retry;
13609
13610 /* Clear the face and image caches.
13611
13612 We used to do this only if consider_all_windows_p. But the cache
13613 needs to be cleared if a timer creates images in the current
13614 buffer (e.g. the test case in Bug#6230). */
13615
13616 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13617 {
13618 clear_face_cache (0);
13619 clear_face_cache_count = 0;
13620 }
13621
13622 #ifdef HAVE_WINDOW_SYSTEM
13623 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13624 {
13625 clear_image_caches (Qnil);
13626 clear_image_cache_count = 0;
13627 }
13628 #endif /* HAVE_WINDOW_SYSTEM */
13629
13630 end_of_redisplay:
13631 unbind_to (count, Qnil);
13632 RESUME_POLLING;
13633 }
13634
13635
13636 /* Redisplay, but leave alone any recent echo area message unless
13637 another message has been requested in its place.
13638
13639 This is useful in situations where you need to redisplay but no
13640 user action has occurred, making it inappropriate for the message
13641 area to be cleared. See tracking_off and
13642 wait_reading_process_output for examples of these situations.
13643
13644 FROM_WHERE is an integer saying from where this function was
13645 called. This is useful for debugging. */
13646
13647 void
13648 redisplay_preserve_echo_area (int from_where)
13649 {
13650 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13651
13652 if (!NILP (echo_area_buffer[1]))
13653 {
13654 /* We have a previously displayed message, but no current
13655 message. Redisplay the previous message. */
13656 display_last_displayed_message_p = 1;
13657 redisplay_internal ();
13658 display_last_displayed_message_p = 0;
13659 }
13660 else
13661 redisplay_internal ();
13662
13663 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13664 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13665 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13666 }
13667
13668
13669 /* Function registered with record_unwind_protect in
13670 redisplay_internal. Reset redisplaying_p to the value it had
13671 before redisplay_internal was called, and clear
13672 prevent_freeing_realized_faces_p. It also selects the previously
13673 selected frame, unless it has been deleted (by an X connection
13674 failure during redisplay, for example). */
13675
13676 static Lisp_Object
13677 unwind_redisplay (Lisp_Object val)
13678 {
13679 Lisp_Object old_redisplaying_p, old_frame;
13680
13681 old_redisplaying_p = XCAR (val);
13682 redisplaying_p = XFASTINT (old_redisplaying_p);
13683 old_frame = XCDR (val);
13684 if (! EQ (old_frame, selected_frame)
13685 && FRAME_LIVE_P (XFRAME (old_frame)))
13686 select_frame_for_redisplay (old_frame);
13687 return Qnil;
13688 }
13689
13690
13691 /* Mark the display of window W as accurate or inaccurate. If
13692 ACCURATE_P is non-zero mark display of W as accurate. If
13693 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13694 redisplay_internal is called. */
13695
13696 static void
13697 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13698 {
13699 if (BUFFERP (w->buffer))
13700 {
13701 struct buffer *b = XBUFFER (w->buffer);
13702
13703 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13704 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13705 w->last_had_star
13706 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13707
13708 if (accurate_p)
13709 {
13710 b->clip_changed = 0;
13711 b->prevent_redisplay_optimizations_p = 0;
13712
13713 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13714 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13715 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13716 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13717
13718 w->current_matrix->buffer = b;
13719 w->current_matrix->begv = BUF_BEGV (b);
13720 w->current_matrix->zv = BUF_ZV (b);
13721
13722 w->last_cursor = w->cursor;
13723 w->last_cursor_off_p = w->cursor_off_p;
13724
13725 if (w == XWINDOW (selected_window))
13726 w->last_point = BUF_PT (b);
13727 else
13728 w->last_point = XMARKER (w->pointm)->charpos;
13729 }
13730 }
13731
13732 if (accurate_p)
13733 {
13734 w->window_end_valid = w->buffer;
13735 w->update_mode_line = 0;
13736 }
13737 }
13738
13739
13740 /* Mark the display of windows in the window tree rooted at WINDOW as
13741 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13742 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13743 be redisplayed the next time redisplay_internal is called. */
13744
13745 void
13746 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13747 {
13748 struct window *w;
13749
13750 for (; !NILP (window); window = w->next)
13751 {
13752 w = XWINDOW (window);
13753 mark_window_display_accurate_1 (w, accurate_p);
13754
13755 if (!NILP (w->vchild))
13756 mark_window_display_accurate (w->vchild, accurate_p);
13757 if (!NILP (w->hchild))
13758 mark_window_display_accurate (w->hchild, accurate_p);
13759 }
13760
13761 if (accurate_p)
13762 {
13763 update_overlay_arrows (1);
13764 }
13765 else
13766 {
13767 /* Force a thorough redisplay the next time by setting
13768 last_arrow_position and last_arrow_string to t, which is
13769 unequal to any useful value of Voverlay_arrow_... */
13770 update_overlay_arrows (-1);
13771 }
13772 }
13773
13774
13775 /* Return value in display table DP (Lisp_Char_Table *) for character
13776 C. Since a display table doesn't have any parent, we don't have to
13777 follow parent. Do not call this function directly but use the
13778 macro DISP_CHAR_VECTOR. */
13779
13780 Lisp_Object
13781 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13782 {
13783 Lisp_Object val;
13784
13785 if (ASCII_CHAR_P (c))
13786 {
13787 val = dp->ascii;
13788 if (SUB_CHAR_TABLE_P (val))
13789 val = XSUB_CHAR_TABLE (val)->contents[c];
13790 }
13791 else
13792 {
13793 Lisp_Object table;
13794
13795 XSETCHAR_TABLE (table, dp);
13796 val = char_table_ref (table, c);
13797 }
13798 if (NILP (val))
13799 val = dp->defalt;
13800 return val;
13801 }
13802
13803
13804 \f
13805 /***********************************************************************
13806 Window Redisplay
13807 ***********************************************************************/
13808
13809 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13810
13811 static void
13812 redisplay_windows (Lisp_Object window)
13813 {
13814 while (!NILP (window))
13815 {
13816 struct window *w = XWINDOW (window);
13817
13818 if (!NILP (w->hchild))
13819 redisplay_windows (w->hchild);
13820 else if (!NILP (w->vchild))
13821 redisplay_windows (w->vchild);
13822 else if (!NILP (w->buffer))
13823 {
13824 displayed_buffer = XBUFFER (w->buffer);
13825 /* Use list_of_error, not Qerror, so that
13826 we catch only errors and don't run the debugger. */
13827 internal_condition_case_1 (redisplay_window_0, window,
13828 list_of_error,
13829 redisplay_window_error);
13830 }
13831
13832 window = w->next;
13833 }
13834 }
13835
13836 static Lisp_Object
13837 redisplay_window_error (Lisp_Object ignore)
13838 {
13839 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13840 return Qnil;
13841 }
13842
13843 static Lisp_Object
13844 redisplay_window_0 (Lisp_Object window)
13845 {
13846 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13847 redisplay_window (window, 0);
13848 return Qnil;
13849 }
13850
13851 static Lisp_Object
13852 redisplay_window_1 (Lisp_Object window)
13853 {
13854 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13855 redisplay_window (window, 1);
13856 return Qnil;
13857 }
13858 \f
13859
13860 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13861 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13862 which positions recorded in ROW differ from current buffer
13863 positions.
13864
13865 Return 0 if cursor is not on this row, 1 otherwise. */
13866
13867 static int
13868 set_cursor_from_row (struct window *w, struct glyph_row *row,
13869 struct glyph_matrix *matrix,
13870 ptrdiff_t delta, ptrdiff_t delta_bytes,
13871 int dy, int dvpos)
13872 {
13873 struct glyph *glyph = row->glyphs[TEXT_AREA];
13874 struct glyph *end = glyph + row->used[TEXT_AREA];
13875 struct glyph *cursor = NULL;
13876 /* The last known character position in row. */
13877 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13878 int x = row->x;
13879 ptrdiff_t pt_old = PT - delta;
13880 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13881 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13882 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13883 /* A glyph beyond the edge of TEXT_AREA which we should never
13884 touch. */
13885 struct glyph *glyphs_end = end;
13886 /* Non-zero means we've found a match for cursor position, but that
13887 glyph has the avoid_cursor_p flag set. */
13888 int match_with_avoid_cursor = 0;
13889 /* Non-zero means we've seen at least one glyph that came from a
13890 display string. */
13891 int string_seen = 0;
13892 /* Largest and smallest buffer positions seen so far during scan of
13893 glyph row. */
13894 ptrdiff_t bpos_max = pos_before;
13895 ptrdiff_t bpos_min = pos_after;
13896 /* Last buffer position covered by an overlay string with an integer
13897 `cursor' property. */
13898 ptrdiff_t bpos_covered = 0;
13899 /* Non-zero means the display string on which to display the cursor
13900 comes from a text property, not from an overlay. */
13901 int string_from_text_prop = 0;
13902
13903 /* Don't even try doing anything if called for a mode-line or
13904 header-line row, since the rest of the code isn't prepared to
13905 deal with such calamities. */
13906 eassert (!row->mode_line_p);
13907 if (row->mode_line_p)
13908 return 0;
13909
13910 /* Skip over glyphs not having an object at the start and the end of
13911 the row. These are special glyphs like truncation marks on
13912 terminal frames. */
13913 if (row->displays_text_p)
13914 {
13915 if (!row->reversed_p)
13916 {
13917 while (glyph < end
13918 && INTEGERP (glyph->object)
13919 && glyph->charpos < 0)
13920 {
13921 x += glyph->pixel_width;
13922 ++glyph;
13923 }
13924 while (end > glyph
13925 && INTEGERP ((end - 1)->object)
13926 /* CHARPOS is zero for blanks and stretch glyphs
13927 inserted by extend_face_to_end_of_line. */
13928 && (end - 1)->charpos <= 0)
13929 --end;
13930 glyph_before = glyph - 1;
13931 glyph_after = end;
13932 }
13933 else
13934 {
13935 struct glyph *g;
13936
13937 /* If the glyph row is reversed, we need to process it from back
13938 to front, so swap the edge pointers. */
13939 glyphs_end = end = glyph - 1;
13940 glyph += row->used[TEXT_AREA] - 1;
13941
13942 while (glyph > end + 1
13943 && INTEGERP (glyph->object)
13944 && glyph->charpos < 0)
13945 {
13946 --glyph;
13947 x -= glyph->pixel_width;
13948 }
13949 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13950 --glyph;
13951 /* By default, in reversed rows we put the cursor on the
13952 rightmost (first in the reading order) glyph. */
13953 for (g = end + 1; g < glyph; g++)
13954 x += g->pixel_width;
13955 while (end < glyph
13956 && INTEGERP ((end + 1)->object)
13957 && (end + 1)->charpos <= 0)
13958 ++end;
13959 glyph_before = glyph + 1;
13960 glyph_after = end;
13961 }
13962 }
13963 else if (row->reversed_p)
13964 {
13965 /* In R2L rows that don't display text, put the cursor on the
13966 rightmost glyph. Case in point: an empty last line that is
13967 part of an R2L paragraph. */
13968 cursor = end - 1;
13969 /* Avoid placing the cursor on the last glyph of the row, where
13970 on terminal frames we hold the vertical border between
13971 adjacent windows. */
13972 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13973 && !WINDOW_RIGHTMOST_P (w)
13974 && cursor == row->glyphs[LAST_AREA] - 1)
13975 cursor--;
13976 x = -1; /* will be computed below, at label compute_x */
13977 }
13978
13979 /* Step 1: Try to find the glyph whose character position
13980 corresponds to point. If that's not possible, find 2 glyphs
13981 whose character positions are the closest to point, one before
13982 point, the other after it. */
13983 if (!row->reversed_p)
13984 while (/* not marched to end of glyph row */
13985 glyph < end
13986 /* glyph was not inserted by redisplay for internal purposes */
13987 && !INTEGERP (glyph->object))
13988 {
13989 if (BUFFERP (glyph->object))
13990 {
13991 ptrdiff_t dpos = glyph->charpos - pt_old;
13992
13993 if (glyph->charpos > bpos_max)
13994 bpos_max = glyph->charpos;
13995 if (glyph->charpos < bpos_min)
13996 bpos_min = glyph->charpos;
13997 if (!glyph->avoid_cursor_p)
13998 {
13999 /* If we hit point, we've found the glyph on which to
14000 display the cursor. */
14001 if (dpos == 0)
14002 {
14003 match_with_avoid_cursor = 0;
14004 break;
14005 }
14006 /* See if we've found a better approximation to
14007 POS_BEFORE or to POS_AFTER. */
14008 if (0 > dpos && dpos > pos_before - pt_old)
14009 {
14010 pos_before = glyph->charpos;
14011 glyph_before = glyph;
14012 }
14013 else if (0 < dpos && dpos < pos_after - pt_old)
14014 {
14015 pos_after = glyph->charpos;
14016 glyph_after = glyph;
14017 }
14018 }
14019 else if (dpos == 0)
14020 match_with_avoid_cursor = 1;
14021 }
14022 else if (STRINGP (glyph->object))
14023 {
14024 Lisp_Object chprop;
14025 ptrdiff_t glyph_pos = glyph->charpos;
14026
14027 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14028 glyph->object);
14029 if (!NILP (chprop))
14030 {
14031 /* If the string came from a `display' text property,
14032 look up the buffer position of that property and
14033 use that position to update bpos_max, as if we
14034 actually saw such a position in one of the row's
14035 glyphs. This helps with supporting integer values
14036 of `cursor' property on the display string in
14037 situations where most or all of the row's buffer
14038 text is completely covered by display properties,
14039 so that no glyph with valid buffer positions is
14040 ever seen in the row. */
14041 ptrdiff_t prop_pos =
14042 string_buffer_position_lim (glyph->object, pos_before,
14043 pos_after, 0);
14044
14045 if (prop_pos >= pos_before)
14046 bpos_max = prop_pos - 1;
14047 }
14048 if (INTEGERP (chprop))
14049 {
14050 bpos_covered = bpos_max + XINT (chprop);
14051 /* If the `cursor' property covers buffer positions up
14052 to and including point, we should display cursor on
14053 this glyph. Note that, if a `cursor' property on one
14054 of the string's characters has an integer value, we
14055 will break out of the loop below _before_ we get to
14056 the position match above. IOW, integer values of
14057 the `cursor' property override the "exact match for
14058 point" strategy of positioning the cursor. */
14059 /* Implementation note: bpos_max == pt_old when, e.g.,
14060 we are in an empty line, where bpos_max is set to
14061 MATRIX_ROW_START_CHARPOS, see above. */
14062 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14063 {
14064 cursor = glyph;
14065 break;
14066 }
14067 }
14068
14069 string_seen = 1;
14070 }
14071 x += glyph->pixel_width;
14072 ++glyph;
14073 }
14074 else if (glyph > end) /* row is reversed */
14075 while (!INTEGERP (glyph->object))
14076 {
14077 if (BUFFERP (glyph->object))
14078 {
14079 ptrdiff_t dpos = glyph->charpos - pt_old;
14080
14081 if (glyph->charpos > bpos_max)
14082 bpos_max = glyph->charpos;
14083 if (glyph->charpos < bpos_min)
14084 bpos_min = glyph->charpos;
14085 if (!glyph->avoid_cursor_p)
14086 {
14087 if (dpos == 0)
14088 {
14089 match_with_avoid_cursor = 0;
14090 break;
14091 }
14092 if (0 > dpos && dpos > pos_before - pt_old)
14093 {
14094 pos_before = glyph->charpos;
14095 glyph_before = glyph;
14096 }
14097 else if (0 < dpos && dpos < pos_after - pt_old)
14098 {
14099 pos_after = glyph->charpos;
14100 glyph_after = glyph;
14101 }
14102 }
14103 else if (dpos == 0)
14104 match_with_avoid_cursor = 1;
14105 }
14106 else if (STRINGP (glyph->object))
14107 {
14108 Lisp_Object chprop;
14109 ptrdiff_t glyph_pos = glyph->charpos;
14110
14111 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14112 glyph->object);
14113 if (!NILP (chprop))
14114 {
14115 ptrdiff_t prop_pos =
14116 string_buffer_position_lim (glyph->object, pos_before,
14117 pos_after, 0);
14118
14119 if (prop_pos >= pos_before)
14120 bpos_max = prop_pos - 1;
14121 }
14122 if (INTEGERP (chprop))
14123 {
14124 bpos_covered = bpos_max + XINT (chprop);
14125 /* If the `cursor' property covers buffer positions up
14126 to and including point, we should display cursor on
14127 this glyph. */
14128 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14129 {
14130 cursor = glyph;
14131 break;
14132 }
14133 }
14134 string_seen = 1;
14135 }
14136 --glyph;
14137 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14138 {
14139 x--; /* can't use any pixel_width */
14140 break;
14141 }
14142 x -= glyph->pixel_width;
14143 }
14144
14145 /* Step 2: If we didn't find an exact match for point, we need to
14146 look for a proper place to put the cursor among glyphs between
14147 GLYPH_BEFORE and GLYPH_AFTER. */
14148 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14149 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14150 && bpos_covered < pt_old)
14151 {
14152 /* An empty line has a single glyph whose OBJECT is zero and
14153 whose CHARPOS is the position of a newline on that line.
14154 Note that on a TTY, there are more glyphs after that, which
14155 were produced by extend_face_to_end_of_line, but their
14156 CHARPOS is zero or negative. */
14157 int empty_line_p =
14158 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14159 && INTEGERP (glyph->object) && glyph->charpos > 0;
14160
14161 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14162 {
14163 ptrdiff_t ellipsis_pos;
14164
14165 /* Scan back over the ellipsis glyphs. */
14166 if (!row->reversed_p)
14167 {
14168 ellipsis_pos = (glyph - 1)->charpos;
14169 while (glyph > row->glyphs[TEXT_AREA]
14170 && (glyph - 1)->charpos == ellipsis_pos)
14171 glyph--, x -= glyph->pixel_width;
14172 /* That loop always goes one position too far, including
14173 the glyph before the ellipsis. So scan forward over
14174 that one. */
14175 x += glyph->pixel_width;
14176 glyph++;
14177 }
14178 else /* row is reversed */
14179 {
14180 ellipsis_pos = (glyph + 1)->charpos;
14181 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14182 && (glyph + 1)->charpos == ellipsis_pos)
14183 glyph++, x += glyph->pixel_width;
14184 x -= glyph->pixel_width;
14185 glyph--;
14186 }
14187 }
14188 else if (match_with_avoid_cursor)
14189 {
14190 cursor = glyph_after;
14191 x = -1;
14192 }
14193 else if (string_seen)
14194 {
14195 int incr = row->reversed_p ? -1 : +1;
14196
14197 /* Need to find the glyph that came out of a string which is
14198 present at point. That glyph is somewhere between
14199 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14200 positioned between POS_BEFORE and POS_AFTER in the
14201 buffer. */
14202 struct glyph *start, *stop;
14203 ptrdiff_t pos = pos_before;
14204
14205 x = -1;
14206
14207 /* If the row ends in a newline from a display string,
14208 reordering could have moved the glyphs belonging to the
14209 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14210 in this case we extend the search to the last glyph in
14211 the row that was not inserted by redisplay. */
14212 if (row->ends_in_newline_from_string_p)
14213 {
14214 glyph_after = end;
14215 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14216 }
14217
14218 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14219 correspond to POS_BEFORE and POS_AFTER, respectively. We
14220 need START and STOP in the order that corresponds to the
14221 row's direction as given by its reversed_p flag. If the
14222 directionality of characters between POS_BEFORE and
14223 POS_AFTER is the opposite of the row's base direction,
14224 these characters will have been reordered for display,
14225 and we need to reverse START and STOP. */
14226 if (!row->reversed_p)
14227 {
14228 start = min (glyph_before, glyph_after);
14229 stop = max (glyph_before, glyph_after);
14230 }
14231 else
14232 {
14233 start = max (glyph_before, glyph_after);
14234 stop = min (glyph_before, glyph_after);
14235 }
14236 for (glyph = start + incr;
14237 row->reversed_p ? glyph > stop : glyph < stop; )
14238 {
14239
14240 /* Any glyphs that come from the buffer are here because
14241 of bidi reordering. Skip them, and only pay
14242 attention to glyphs that came from some string. */
14243 if (STRINGP (glyph->object))
14244 {
14245 Lisp_Object str;
14246 ptrdiff_t tem;
14247 /* If the display property covers the newline, we
14248 need to search for it one position farther. */
14249 ptrdiff_t lim = pos_after
14250 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14251
14252 string_from_text_prop = 0;
14253 str = glyph->object;
14254 tem = string_buffer_position_lim (str, pos, lim, 0);
14255 if (tem == 0 /* from overlay */
14256 || pos <= tem)
14257 {
14258 /* If the string from which this glyph came is
14259 found in the buffer at point, or at position
14260 that is closer to point than pos_after, then
14261 we've found the glyph we've been looking for.
14262 If it comes from an overlay (tem == 0), and
14263 it has the `cursor' property on one of its
14264 glyphs, record that glyph as a candidate for
14265 displaying the cursor. (As in the
14266 unidirectional version, we will display the
14267 cursor on the last candidate we find.) */
14268 if (tem == 0
14269 || tem == pt_old
14270 || (tem - pt_old > 0 && tem < pos_after))
14271 {
14272 /* The glyphs from this string could have
14273 been reordered. Find the one with the
14274 smallest string position. Or there could
14275 be a character in the string with the
14276 `cursor' property, which means display
14277 cursor on that character's glyph. */
14278 ptrdiff_t strpos = glyph->charpos;
14279
14280 if (tem)
14281 {
14282 cursor = glyph;
14283 string_from_text_prop = 1;
14284 }
14285 for ( ;
14286 (row->reversed_p ? glyph > stop : glyph < stop)
14287 && EQ (glyph->object, str);
14288 glyph += incr)
14289 {
14290 Lisp_Object cprop;
14291 ptrdiff_t gpos = glyph->charpos;
14292
14293 cprop = Fget_char_property (make_number (gpos),
14294 Qcursor,
14295 glyph->object);
14296 if (!NILP (cprop))
14297 {
14298 cursor = glyph;
14299 break;
14300 }
14301 if (tem && glyph->charpos < strpos)
14302 {
14303 strpos = glyph->charpos;
14304 cursor = glyph;
14305 }
14306 }
14307
14308 if (tem == pt_old
14309 || (tem - pt_old > 0 && tem < pos_after))
14310 goto compute_x;
14311 }
14312 if (tem)
14313 pos = tem + 1; /* don't find previous instances */
14314 }
14315 /* This string is not what we want; skip all of the
14316 glyphs that came from it. */
14317 while ((row->reversed_p ? glyph > stop : glyph < stop)
14318 && EQ (glyph->object, str))
14319 glyph += incr;
14320 }
14321 else
14322 glyph += incr;
14323 }
14324
14325 /* If we reached the end of the line, and END was from a string,
14326 the cursor is not on this line. */
14327 if (cursor == NULL
14328 && (row->reversed_p ? glyph <= end : glyph >= end)
14329 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14330 && STRINGP (end->object)
14331 && row->continued_p)
14332 return 0;
14333 }
14334 /* A truncated row may not include PT among its character positions.
14335 Setting the cursor inside the scroll margin will trigger
14336 recalculation of hscroll in hscroll_window_tree. But if a
14337 display string covers point, defer to the string-handling
14338 code below to figure this out. */
14339 else if (row->truncated_on_left_p && pt_old < bpos_min)
14340 {
14341 cursor = glyph_before;
14342 x = -1;
14343 }
14344 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14345 /* Zero-width characters produce no glyphs. */
14346 || (!empty_line_p
14347 && (row->reversed_p
14348 ? glyph_after > glyphs_end
14349 : glyph_after < glyphs_end)))
14350 {
14351 cursor = glyph_after;
14352 x = -1;
14353 }
14354 }
14355
14356 compute_x:
14357 if (cursor != NULL)
14358 glyph = cursor;
14359 else if (glyph == glyphs_end
14360 && pos_before == pos_after
14361 && STRINGP ((row->reversed_p
14362 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14363 : row->glyphs[TEXT_AREA])->object))
14364 {
14365 /* If all the glyphs of this row came from strings, put the
14366 cursor on the first glyph of the row. This avoids having the
14367 cursor outside of the text area in this very rare and hard
14368 use case. */
14369 glyph =
14370 row->reversed_p
14371 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14372 : row->glyphs[TEXT_AREA];
14373 }
14374 if (x < 0)
14375 {
14376 struct glyph *g;
14377
14378 /* Need to compute x that corresponds to GLYPH. */
14379 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14380 {
14381 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14382 abort ();
14383 x += g->pixel_width;
14384 }
14385 }
14386
14387 /* ROW could be part of a continued line, which, under bidi
14388 reordering, might have other rows whose start and end charpos
14389 occlude point. Only set w->cursor if we found a better
14390 approximation to the cursor position than we have from previously
14391 examined candidate rows belonging to the same continued line. */
14392 if (/* we already have a candidate row */
14393 w->cursor.vpos >= 0
14394 /* that candidate is not the row we are processing */
14395 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14396 /* Make sure cursor.vpos specifies a row whose start and end
14397 charpos occlude point, and it is valid candidate for being a
14398 cursor-row. This is because some callers of this function
14399 leave cursor.vpos at the row where the cursor was displayed
14400 during the last redisplay cycle. */
14401 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14402 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14403 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14404 {
14405 struct glyph *g1 =
14406 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14407
14408 /* Don't consider glyphs that are outside TEXT_AREA. */
14409 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14410 return 0;
14411 /* Keep the candidate whose buffer position is the closest to
14412 point or has the `cursor' property. */
14413 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14414 w->cursor.hpos >= 0
14415 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14416 && ((BUFFERP (g1->object)
14417 && (g1->charpos == pt_old /* an exact match always wins */
14418 || (BUFFERP (glyph->object)
14419 && eabs (g1->charpos - pt_old)
14420 < eabs (glyph->charpos - pt_old))))
14421 /* previous candidate is a glyph from a string that has
14422 a non-nil `cursor' property */
14423 || (STRINGP (g1->object)
14424 && (!NILP (Fget_char_property (make_number (g1->charpos),
14425 Qcursor, g1->object))
14426 /* previous candidate is from the same display
14427 string as this one, and the display string
14428 came from a text property */
14429 || (EQ (g1->object, glyph->object)
14430 && string_from_text_prop)
14431 /* this candidate is from newline and its
14432 position is not an exact match */
14433 || (INTEGERP (glyph->object)
14434 && glyph->charpos != pt_old)))))
14435 return 0;
14436 /* If this candidate gives an exact match, use that. */
14437 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14438 /* If this candidate is a glyph created for the
14439 terminating newline of a line, and point is on that
14440 newline, it wins because it's an exact match. */
14441 || (!row->continued_p
14442 && INTEGERP (glyph->object)
14443 && glyph->charpos == 0
14444 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14445 /* Otherwise, keep the candidate that comes from a row
14446 spanning less buffer positions. This may win when one or
14447 both candidate positions are on glyphs that came from
14448 display strings, for which we cannot compare buffer
14449 positions. */
14450 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14451 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14452 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14453 return 0;
14454 }
14455 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14456 w->cursor.x = x;
14457 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14458 w->cursor.y = row->y + dy;
14459
14460 if (w == XWINDOW (selected_window))
14461 {
14462 if (!row->continued_p
14463 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14464 && row->x == 0)
14465 {
14466 this_line_buffer = XBUFFER (w->buffer);
14467
14468 CHARPOS (this_line_start_pos)
14469 = MATRIX_ROW_START_CHARPOS (row) + delta;
14470 BYTEPOS (this_line_start_pos)
14471 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14472
14473 CHARPOS (this_line_end_pos)
14474 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14475 BYTEPOS (this_line_end_pos)
14476 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14477
14478 this_line_y = w->cursor.y;
14479 this_line_pixel_height = row->height;
14480 this_line_vpos = w->cursor.vpos;
14481 this_line_start_x = row->x;
14482 }
14483 else
14484 CHARPOS (this_line_start_pos) = 0;
14485 }
14486
14487 return 1;
14488 }
14489
14490
14491 /* Run window scroll functions, if any, for WINDOW with new window
14492 start STARTP. Sets the window start of WINDOW to that position.
14493
14494 We assume that the window's buffer is really current. */
14495
14496 static inline struct text_pos
14497 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14498 {
14499 struct window *w = XWINDOW (window);
14500 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14501
14502 if (current_buffer != XBUFFER (w->buffer))
14503 abort ();
14504
14505 if (!NILP (Vwindow_scroll_functions))
14506 {
14507 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14508 make_number (CHARPOS (startp)));
14509 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14510 /* In case the hook functions switch buffers. */
14511 if (current_buffer != XBUFFER (w->buffer))
14512 set_buffer_internal_1 (XBUFFER (w->buffer));
14513 }
14514
14515 return startp;
14516 }
14517
14518
14519 /* Make sure the line containing the cursor is fully visible.
14520 A value of 1 means there is nothing to be done.
14521 (Either the line is fully visible, or it cannot be made so,
14522 or we cannot tell.)
14523
14524 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14525 is higher than window.
14526
14527 A value of 0 means the caller should do scrolling
14528 as if point had gone off the screen. */
14529
14530 static int
14531 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14532 {
14533 struct glyph_matrix *matrix;
14534 struct glyph_row *row;
14535 int window_height;
14536
14537 if (!make_cursor_line_fully_visible_p)
14538 return 1;
14539
14540 /* It's not always possible to find the cursor, e.g, when a window
14541 is full of overlay strings. Don't do anything in that case. */
14542 if (w->cursor.vpos < 0)
14543 return 1;
14544
14545 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14546 row = MATRIX_ROW (matrix, w->cursor.vpos);
14547
14548 /* If the cursor row is not partially visible, there's nothing to do. */
14549 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14550 return 1;
14551
14552 /* If the row the cursor is in is taller than the window's height,
14553 it's not clear what to do, so do nothing. */
14554 window_height = window_box_height (w);
14555 if (row->height >= window_height)
14556 {
14557 if (!force_p || MINI_WINDOW_P (w)
14558 || w->vscroll || w->cursor.vpos == 0)
14559 return 1;
14560 }
14561 return 0;
14562 }
14563
14564
14565 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14566 non-zero means only WINDOW is redisplayed in redisplay_internal.
14567 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14568 in redisplay_window to bring a partially visible line into view in
14569 the case that only the cursor has moved.
14570
14571 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14572 last screen line's vertical height extends past the end of the screen.
14573
14574 Value is
14575
14576 1 if scrolling succeeded
14577
14578 0 if scrolling didn't find point.
14579
14580 -1 if new fonts have been loaded so that we must interrupt
14581 redisplay, adjust glyph matrices, and try again. */
14582
14583 enum
14584 {
14585 SCROLLING_SUCCESS,
14586 SCROLLING_FAILED,
14587 SCROLLING_NEED_LARGER_MATRICES
14588 };
14589
14590 /* If scroll-conservatively is more than this, never recenter.
14591
14592 If you change this, don't forget to update the doc string of
14593 `scroll-conservatively' and the Emacs manual. */
14594 #define SCROLL_LIMIT 100
14595
14596 static int
14597 try_scrolling (Lisp_Object window, int just_this_one_p,
14598 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14599 int temp_scroll_step, int last_line_misfit)
14600 {
14601 struct window *w = XWINDOW (window);
14602 struct frame *f = XFRAME (w->frame);
14603 struct text_pos pos, startp;
14604 struct it it;
14605 int this_scroll_margin, scroll_max, rc, height;
14606 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14607 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14608 Lisp_Object aggressive;
14609 /* We will never try scrolling more than this number of lines. */
14610 int scroll_limit = SCROLL_LIMIT;
14611
14612 #ifdef GLYPH_DEBUG
14613 debug_method_add (w, "try_scrolling");
14614 #endif
14615
14616 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14617
14618 /* Compute scroll margin height in pixels. We scroll when point is
14619 within this distance from the top or bottom of the window. */
14620 if (scroll_margin > 0)
14621 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14622 * FRAME_LINE_HEIGHT (f);
14623 else
14624 this_scroll_margin = 0;
14625
14626 /* Force arg_scroll_conservatively to have a reasonable value, to
14627 avoid scrolling too far away with slow move_it_* functions. Note
14628 that the user can supply scroll-conservatively equal to
14629 `most-positive-fixnum', which can be larger than INT_MAX. */
14630 if (arg_scroll_conservatively > scroll_limit)
14631 {
14632 arg_scroll_conservatively = scroll_limit + 1;
14633 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14634 }
14635 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14636 /* Compute how much we should try to scroll maximally to bring
14637 point into view. */
14638 scroll_max = (max (scroll_step,
14639 max (arg_scroll_conservatively, temp_scroll_step))
14640 * FRAME_LINE_HEIGHT (f));
14641 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14642 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14643 /* We're trying to scroll because of aggressive scrolling but no
14644 scroll_step is set. Choose an arbitrary one. */
14645 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14646 else
14647 scroll_max = 0;
14648
14649 too_near_end:
14650
14651 /* Decide whether to scroll down. */
14652 if (PT > CHARPOS (startp))
14653 {
14654 int scroll_margin_y;
14655
14656 /* Compute the pixel ypos of the scroll margin, then move IT to
14657 either that ypos or PT, whichever comes first. */
14658 start_display (&it, w, startp);
14659 scroll_margin_y = it.last_visible_y - this_scroll_margin
14660 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14661 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14662 (MOVE_TO_POS | MOVE_TO_Y));
14663
14664 if (PT > CHARPOS (it.current.pos))
14665 {
14666 int y0 = line_bottom_y (&it);
14667 /* Compute how many pixels below window bottom to stop searching
14668 for PT. This avoids costly search for PT that is far away if
14669 the user limited scrolling by a small number of lines, but
14670 always finds PT if scroll_conservatively is set to a large
14671 number, such as most-positive-fixnum. */
14672 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14673 int y_to_move = it.last_visible_y + slack;
14674
14675 /* Compute the distance from the scroll margin to PT or to
14676 the scroll limit, whichever comes first. This should
14677 include the height of the cursor line, to make that line
14678 fully visible. */
14679 move_it_to (&it, PT, -1, y_to_move,
14680 -1, MOVE_TO_POS | MOVE_TO_Y);
14681 dy = line_bottom_y (&it) - y0;
14682
14683 if (dy > scroll_max)
14684 return SCROLLING_FAILED;
14685
14686 if (dy > 0)
14687 scroll_down_p = 1;
14688 }
14689 }
14690
14691 if (scroll_down_p)
14692 {
14693 /* Point is in or below the bottom scroll margin, so move the
14694 window start down. If scrolling conservatively, move it just
14695 enough down to make point visible. If scroll_step is set,
14696 move it down by scroll_step. */
14697 if (arg_scroll_conservatively)
14698 amount_to_scroll
14699 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14700 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14701 else if (scroll_step || temp_scroll_step)
14702 amount_to_scroll = scroll_max;
14703 else
14704 {
14705 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14706 height = WINDOW_BOX_TEXT_HEIGHT (w);
14707 if (NUMBERP (aggressive))
14708 {
14709 double float_amount = XFLOATINT (aggressive) * height;
14710 amount_to_scroll = float_amount;
14711 if (amount_to_scroll == 0 && float_amount > 0)
14712 amount_to_scroll = 1;
14713 /* Don't let point enter the scroll margin near top of
14714 the window. */
14715 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14716 amount_to_scroll = height - 2*this_scroll_margin + dy;
14717 }
14718 }
14719
14720 if (amount_to_scroll <= 0)
14721 return SCROLLING_FAILED;
14722
14723 start_display (&it, w, startp);
14724 if (arg_scroll_conservatively <= scroll_limit)
14725 move_it_vertically (&it, amount_to_scroll);
14726 else
14727 {
14728 /* Extra precision for users who set scroll-conservatively
14729 to a large number: make sure the amount we scroll
14730 the window start is never less than amount_to_scroll,
14731 which was computed as distance from window bottom to
14732 point. This matters when lines at window top and lines
14733 below window bottom have different height. */
14734 struct it it1;
14735 void *it1data = NULL;
14736 /* We use a temporary it1 because line_bottom_y can modify
14737 its argument, if it moves one line down; see there. */
14738 int start_y;
14739
14740 SAVE_IT (it1, it, it1data);
14741 start_y = line_bottom_y (&it1);
14742 do {
14743 RESTORE_IT (&it, &it, it1data);
14744 move_it_by_lines (&it, 1);
14745 SAVE_IT (it1, it, it1data);
14746 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14747 }
14748
14749 /* If STARTP is unchanged, move it down another screen line. */
14750 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14751 move_it_by_lines (&it, 1);
14752 startp = it.current.pos;
14753 }
14754 else
14755 {
14756 struct text_pos scroll_margin_pos = startp;
14757
14758 /* See if point is inside the scroll margin at the top of the
14759 window. */
14760 if (this_scroll_margin)
14761 {
14762 start_display (&it, w, startp);
14763 move_it_vertically (&it, this_scroll_margin);
14764 scroll_margin_pos = it.current.pos;
14765 }
14766
14767 if (PT < CHARPOS (scroll_margin_pos))
14768 {
14769 /* Point is in the scroll margin at the top of the window or
14770 above what is displayed in the window. */
14771 int y0, y_to_move;
14772
14773 /* Compute the vertical distance from PT to the scroll
14774 margin position. Move as far as scroll_max allows, or
14775 one screenful, or 10 screen lines, whichever is largest.
14776 Give up if distance is greater than scroll_max. */
14777 SET_TEXT_POS (pos, PT, PT_BYTE);
14778 start_display (&it, w, pos);
14779 y0 = it.current_y;
14780 y_to_move = max (it.last_visible_y,
14781 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14782 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14783 y_to_move, -1,
14784 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14785 dy = it.current_y - y0;
14786 if (dy > scroll_max)
14787 return SCROLLING_FAILED;
14788
14789 /* Compute new window start. */
14790 start_display (&it, w, startp);
14791
14792 if (arg_scroll_conservatively)
14793 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14794 max (scroll_step, temp_scroll_step));
14795 else if (scroll_step || temp_scroll_step)
14796 amount_to_scroll = scroll_max;
14797 else
14798 {
14799 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14800 height = WINDOW_BOX_TEXT_HEIGHT (w);
14801 if (NUMBERP (aggressive))
14802 {
14803 double float_amount = XFLOATINT (aggressive) * height;
14804 amount_to_scroll = float_amount;
14805 if (amount_to_scroll == 0 && float_amount > 0)
14806 amount_to_scroll = 1;
14807 amount_to_scroll -=
14808 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14809 /* Don't let point enter the scroll margin near
14810 bottom of the window. */
14811 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14812 amount_to_scroll = height - 2*this_scroll_margin + dy;
14813 }
14814 }
14815
14816 if (amount_to_scroll <= 0)
14817 return SCROLLING_FAILED;
14818
14819 move_it_vertically_backward (&it, amount_to_scroll);
14820 startp = it.current.pos;
14821 }
14822 }
14823
14824 /* Run window scroll functions. */
14825 startp = run_window_scroll_functions (window, startp);
14826
14827 /* Display the window. Give up if new fonts are loaded, or if point
14828 doesn't appear. */
14829 if (!try_window (window, startp, 0))
14830 rc = SCROLLING_NEED_LARGER_MATRICES;
14831 else if (w->cursor.vpos < 0)
14832 {
14833 clear_glyph_matrix (w->desired_matrix);
14834 rc = SCROLLING_FAILED;
14835 }
14836 else
14837 {
14838 /* Maybe forget recorded base line for line number display. */
14839 if (!just_this_one_p
14840 || current_buffer->clip_changed
14841 || BEG_UNCHANGED < CHARPOS (startp))
14842 w->base_line_number = Qnil;
14843
14844 /* If cursor ends up on a partially visible line,
14845 treat that as being off the bottom of the screen. */
14846 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14847 /* It's possible that the cursor is on the first line of the
14848 buffer, which is partially obscured due to a vscroll
14849 (Bug#7537). In that case, avoid looping forever . */
14850 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14851 {
14852 clear_glyph_matrix (w->desired_matrix);
14853 ++extra_scroll_margin_lines;
14854 goto too_near_end;
14855 }
14856 rc = SCROLLING_SUCCESS;
14857 }
14858
14859 return rc;
14860 }
14861
14862
14863 /* Compute a suitable window start for window W if display of W starts
14864 on a continuation line. Value is non-zero if a new window start
14865 was computed.
14866
14867 The new window start will be computed, based on W's width, starting
14868 from the start of the continued line. It is the start of the
14869 screen line with the minimum distance from the old start W->start. */
14870
14871 static int
14872 compute_window_start_on_continuation_line (struct window *w)
14873 {
14874 struct text_pos pos, start_pos;
14875 int window_start_changed_p = 0;
14876
14877 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14878
14879 /* If window start is on a continuation line... Window start may be
14880 < BEGV in case there's invisible text at the start of the
14881 buffer (M-x rmail, for example). */
14882 if (CHARPOS (start_pos) > BEGV
14883 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14884 {
14885 struct it it;
14886 struct glyph_row *row;
14887
14888 /* Handle the case that the window start is out of range. */
14889 if (CHARPOS (start_pos) < BEGV)
14890 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14891 else if (CHARPOS (start_pos) > ZV)
14892 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14893
14894 /* Find the start of the continued line. This should be fast
14895 because scan_buffer is fast (newline cache). */
14896 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14897 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14898 row, DEFAULT_FACE_ID);
14899 reseat_at_previous_visible_line_start (&it);
14900
14901 /* If the line start is "too far" away from the window start,
14902 say it takes too much time to compute a new window start. */
14903 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14904 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14905 {
14906 int min_distance, distance;
14907
14908 /* Move forward by display lines to find the new window
14909 start. If window width was enlarged, the new start can
14910 be expected to be > the old start. If window width was
14911 decreased, the new window start will be < the old start.
14912 So, we're looking for the display line start with the
14913 minimum distance from the old window start. */
14914 pos = it.current.pos;
14915 min_distance = INFINITY;
14916 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14917 distance < min_distance)
14918 {
14919 min_distance = distance;
14920 pos = it.current.pos;
14921 move_it_by_lines (&it, 1);
14922 }
14923
14924 /* Set the window start there. */
14925 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14926 window_start_changed_p = 1;
14927 }
14928 }
14929
14930 return window_start_changed_p;
14931 }
14932
14933
14934 /* Try cursor movement in case text has not changed in window WINDOW,
14935 with window start STARTP. Value is
14936
14937 CURSOR_MOVEMENT_SUCCESS if successful
14938
14939 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14940
14941 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14942 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14943 we want to scroll as if scroll-step were set to 1. See the code.
14944
14945 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14946 which case we have to abort this redisplay, and adjust matrices
14947 first. */
14948
14949 enum
14950 {
14951 CURSOR_MOVEMENT_SUCCESS,
14952 CURSOR_MOVEMENT_CANNOT_BE_USED,
14953 CURSOR_MOVEMENT_MUST_SCROLL,
14954 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14955 };
14956
14957 static int
14958 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14959 {
14960 struct window *w = XWINDOW (window);
14961 struct frame *f = XFRAME (w->frame);
14962 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14963
14964 #ifdef GLYPH_DEBUG
14965 if (inhibit_try_cursor_movement)
14966 return rc;
14967 #endif
14968
14969 /* Previously, there was a check for Lisp integer in the
14970 if-statement below. Now, this field is converted to
14971 ptrdiff_t, thus zero means invalid position in a buffer. */
14972 eassert (w->last_point > 0);
14973
14974 /* Handle case where text has not changed, only point, and it has
14975 not moved off the frame. */
14976 if (/* Point may be in this window. */
14977 PT >= CHARPOS (startp)
14978 /* Selective display hasn't changed. */
14979 && !current_buffer->clip_changed
14980 /* Function force-mode-line-update is used to force a thorough
14981 redisplay. It sets either windows_or_buffers_changed or
14982 update_mode_lines. So don't take a shortcut here for these
14983 cases. */
14984 && !update_mode_lines
14985 && !windows_or_buffers_changed
14986 && !cursor_type_changed
14987 /* Can't use this case if highlighting a region. When a
14988 region exists, cursor movement has to do more than just
14989 set the cursor. */
14990 && !(!NILP (Vtransient_mark_mode)
14991 && !NILP (BVAR (current_buffer, mark_active)))
14992 && NILP (w->region_showing)
14993 && NILP (Vshow_trailing_whitespace)
14994 /* This code is not used for mini-buffer for the sake of the case
14995 of redisplaying to replace an echo area message; since in
14996 that case the mini-buffer contents per se are usually
14997 unchanged. This code is of no real use in the mini-buffer
14998 since the handling of this_line_start_pos, etc., in redisplay
14999 handles the same cases. */
15000 && !EQ (window, minibuf_window)
15001 /* When splitting windows or for new windows, it happens that
15002 redisplay is called with a nil window_end_vpos or one being
15003 larger than the window. This should really be fixed in
15004 window.c. I don't have this on my list, now, so we do
15005 approximately the same as the old redisplay code. --gerd. */
15006 && INTEGERP (w->window_end_vpos)
15007 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15008 && (FRAME_WINDOW_P (f)
15009 || !overlay_arrow_in_current_buffer_p ()))
15010 {
15011 int this_scroll_margin, top_scroll_margin;
15012 struct glyph_row *row = NULL;
15013
15014 #ifdef GLYPH_DEBUG
15015 debug_method_add (w, "cursor movement");
15016 #endif
15017
15018 /* Scroll if point within this distance from the top or bottom
15019 of the window. This is a pixel value. */
15020 if (scroll_margin > 0)
15021 {
15022 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15023 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15024 }
15025 else
15026 this_scroll_margin = 0;
15027
15028 top_scroll_margin = this_scroll_margin;
15029 if (WINDOW_WANTS_HEADER_LINE_P (w))
15030 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15031
15032 /* Start with the row the cursor was displayed during the last
15033 not paused redisplay. Give up if that row is not valid. */
15034 if (w->last_cursor.vpos < 0
15035 || w->last_cursor.vpos >= w->current_matrix->nrows)
15036 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15037 else
15038 {
15039 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15040 if (row->mode_line_p)
15041 ++row;
15042 if (!row->enabled_p)
15043 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15044 }
15045
15046 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15047 {
15048 int scroll_p = 0, must_scroll = 0;
15049 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15050
15051 if (PT > w->last_point)
15052 {
15053 /* Point has moved forward. */
15054 while (MATRIX_ROW_END_CHARPOS (row) < PT
15055 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15056 {
15057 eassert (row->enabled_p);
15058 ++row;
15059 }
15060
15061 /* If the end position of a row equals the start
15062 position of the next row, and PT is at that position,
15063 we would rather display cursor in the next line. */
15064 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15065 && MATRIX_ROW_END_CHARPOS (row) == PT
15066 && row < w->current_matrix->rows
15067 + w->current_matrix->nrows - 1
15068 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15069 && !cursor_row_p (row))
15070 ++row;
15071
15072 /* If within the scroll margin, scroll. Note that
15073 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15074 the next line would be drawn, and that
15075 this_scroll_margin can be zero. */
15076 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15077 || PT > MATRIX_ROW_END_CHARPOS (row)
15078 /* Line is completely visible last line in window
15079 and PT is to be set in the next line. */
15080 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15081 && PT == MATRIX_ROW_END_CHARPOS (row)
15082 && !row->ends_at_zv_p
15083 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15084 scroll_p = 1;
15085 }
15086 else if (PT < w->last_point)
15087 {
15088 /* Cursor has to be moved backward. Note that PT >=
15089 CHARPOS (startp) because of the outer if-statement. */
15090 while (!row->mode_line_p
15091 && (MATRIX_ROW_START_CHARPOS (row) > PT
15092 || (MATRIX_ROW_START_CHARPOS (row) == PT
15093 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15094 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15095 row > w->current_matrix->rows
15096 && (row-1)->ends_in_newline_from_string_p))))
15097 && (row->y > top_scroll_margin
15098 || CHARPOS (startp) == BEGV))
15099 {
15100 eassert (row->enabled_p);
15101 --row;
15102 }
15103
15104 /* Consider the following case: Window starts at BEGV,
15105 there is invisible, intangible text at BEGV, so that
15106 display starts at some point START > BEGV. It can
15107 happen that we are called with PT somewhere between
15108 BEGV and START. Try to handle that case. */
15109 if (row < w->current_matrix->rows
15110 || row->mode_line_p)
15111 {
15112 row = w->current_matrix->rows;
15113 if (row->mode_line_p)
15114 ++row;
15115 }
15116
15117 /* Due to newlines in overlay strings, we may have to
15118 skip forward over overlay strings. */
15119 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15120 && MATRIX_ROW_END_CHARPOS (row) == PT
15121 && !cursor_row_p (row))
15122 ++row;
15123
15124 /* If within the scroll margin, scroll. */
15125 if (row->y < top_scroll_margin
15126 && CHARPOS (startp) != BEGV)
15127 scroll_p = 1;
15128 }
15129 else
15130 {
15131 /* Cursor did not move. So don't scroll even if cursor line
15132 is partially visible, as it was so before. */
15133 rc = CURSOR_MOVEMENT_SUCCESS;
15134 }
15135
15136 if (PT < MATRIX_ROW_START_CHARPOS (row)
15137 || PT > MATRIX_ROW_END_CHARPOS (row))
15138 {
15139 /* if PT is not in the glyph row, give up. */
15140 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15141 must_scroll = 1;
15142 }
15143 else if (rc != CURSOR_MOVEMENT_SUCCESS
15144 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15145 {
15146 struct glyph_row *row1;
15147
15148 /* If rows are bidi-reordered and point moved, back up
15149 until we find a row that does not belong to a
15150 continuation line. This is because we must consider
15151 all rows of a continued line as candidates for the
15152 new cursor positioning, since row start and end
15153 positions change non-linearly with vertical position
15154 in such rows. */
15155 /* FIXME: Revisit this when glyph ``spilling'' in
15156 continuation lines' rows is implemented for
15157 bidi-reordered rows. */
15158 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15159 MATRIX_ROW_CONTINUATION_LINE_P (row);
15160 --row)
15161 {
15162 /* If we hit the beginning of the displayed portion
15163 without finding the first row of a continued
15164 line, give up. */
15165 if (row <= row1)
15166 {
15167 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15168 break;
15169 }
15170 eassert (row->enabled_p);
15171 }
15172 }
15173 if (must_scroll)
15174 ;
15175 else if (rc != CURSOR_MOVEMENT_SUCCESS
15176 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15177 /* Make sure this isn't a header line by any chance, since
15178 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15179 && !row->mode_line_p
15180 && make_cursor_line_fully_visible_p)
15181 {
15182 if (PT == MATRIX_ROW_END_CHARPOS (row)
15183 && !row->ends_at_zv_p
15184 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15185 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15186 else if (row->height > window_box_height (w))
15187 {
15188 /* If we end up in a partially visible line, let's
15189 make it fully visible, except when it's taller
15190 than the window, in which case we can't do much
15191 about it. */
15192 *scroll_step = 1;
15193 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15194 }
15195 else
15196 {
15197 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15198 if (!cursor_row_fully_visible_p (w, 0, 1))
15199 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15200 else
15201 rc = CURSOR_MOVEMENT_SUCCESS;
15202 }
15203 }
15204 else if (scroll_p)
15205 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15206 else if (rc != CURSOR_MOVEMENT_SUCCESS
15207 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15208 {
15209 /* With bidi-reordered rows, there could be more than
15210 one candidate row whose start and end positions
15211 occlude point. We need to let set_cursor_from_row
15212 find the best candidate. */
15213 /* FIXME: Revisit this when glyph ``spilling'' in
15214 continuation lines' rows is implemented for
15215 bidi-reordered rows. */
15216 int rv = 0;
15217
15218 do
15219 {
15220 int at_zv_p = 0, exact_match_p = 0;
15221
15222 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15223 && PT <= MATRIX_ROW_END_CHARPOS (row)
15224 && cursor_row_p (row))
15225 rv |= set_cursor_from_row (w, row, w->current_matrix,
15226 0, 0, 0, 0);
15227 /* As soon as we've found the exact match for point,
15228 or the first suitable row whose ends_at_zv_p flag
15229 is set, we are done. */
15230 at_zv_p =
15231 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15232 if (rv && !at_zv_p
15233 && w->cursor.hpos >= 0
15234 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15235 w->cursor.vpos))
15236 {
15237 struct glyph_row *candidate =
15238 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15239 struct glyph *g =
15240 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15241 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15242
15243 exact_match_p =
15244 (BUFFERP (g->object) && g->charpos == PT)
15245 || (INTEGERP (g->object)
15246 && (g->charpos == PT
15247 || (g->charpos == 0 && endpos - 1 == PT)));
15248 }
15249 if (rv && (at_zv_p || exact_match_p))
15250 {
15251 rc = CURSOR_MOVEMENT_SUCCESS;
15252 break;
15253 }
15254 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15255 break;
15256 ++row;
15257 }
15258 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15259 || row->continued_p)
15260 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15261 || (MATRIX_ROW_START_CHARPOS (row) == PT
15262 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15263 /* If we didn't find any candidate rows, or exited the
15264 loop before all the candidates were examined, signal
15265 to the caller that this method failed. */
15266 if (rc != CURSOR_MOVEMENT_SUCCESS
15267 && !(rv
15268 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15269 && !row->continued_p))
15270 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15271 else if (rv)
15272 rc = CURSOR_MOVEMENT_SUCCESS;
15273 }
15274 else
15275 {
15276 do
15277 {
15278 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15279 {
15280 rc = CURSOR_MOVEMENT_SUCCESS;
15281 break;
15282 }
15283 ++row;
15284 }
15285 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15286 && MATRIX_ROW_START_CHARPOS (row) == PT
15287 && cursor_row_p (row));
15288 }
15289 }
15290 }
15291
15292 return rc;
15293 }
15294
15295 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15296 static
15297 #endif
15298 void
15299 set_vertical_scroll_bar (struct window *w)
15300 {
15301 ptrdiff_t start, end, whole;
15302
15303 /* Calculate the start and end positions for the current window.
15304 At some point, it would be nice to choose between scrollbars
15305 which reflect the whole buffer size, with special markers
15306 indicating narrowing, and scrollbars which reflect only the
15307 visible region.
15308
15309 Note that mini-buffers sometimes aren't displaying any text. */
15310 if (!MINI_WINDOW_P (w)
15311 || (w == XWINDOW (minibuf_window)
15312 && NILP (echo_area_buffer[0])))
15313 {
15314 struct buffer *buf = XBUFFER (w->buffer);
15315 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15316 start = marker_position (w->start) - BUF_BEGV (buf);
15317 /* I don't think this is guaranteed to be right. For the
15318 moment, we'll pretend it is. */
15319 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15320
15321 if (end < start)
15322 end = start;
15323 if (whole < (end - start))
15324 whole = end - start;
15325 }
15326 else
15327 start = end = whole = 0;
15328
15329 /* Indicate what this scroll bar ought to be displaying now. */
15330 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15331 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15332 (w, end - start, whole, start);
15333 }
15334
15335
15336 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15337 selected_window is redisplayed.
15338
15339 We can return without actually redisplaying the window if
15340 fonts_changed_p is nonzero. In that case, redisplay_internal will
15341 retry. */
15342
15343 static void
15344 redisplay_window (Lisp_Object window, int just_this_one_p)
15345 {
15346 struct window *w = XWINDOW (window);
15347 struct frame *f = XFRAME (w->frame);
15348 struct buffer *buffer = XBUFFER (w->buffer);
15349 struct buffer *old = current_buffer;
15350 struct text_pos lpoint, opoint, startp;
15351 int update_mode_line;
15352 int tem;
15353 struct it it;
15354 /* Record it now because it's overwritten. */
15355 int current_matrix_up_to_date_p = 0;
15356 int used_current_matrix_p = 0;
15357 /* This is less strict than current_matrix_up_to_date_p.
15358 It indicates that the buffer contents and narrowing are unchanged. */
15359 int buffer_unchanged_p = 0;
15360 int temp_scroll_step = 0;
15361 ptrdiff_t count = SPECPDL_INDEX ();
15362 int rc;
15363 int centering_position = -1;
15364 int last_line_misfit = 0;
15365 ptrdiff_t beg_unchanged, end_unchanged;
15366
15367 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15368 opoint = lpoint;
15369
15370 /* W must be a leaf window here. */
15371 eassert (!NILP (w->buffer));
15372 #ifdef GLYPH_DEBUG
15373 *w->desired_matrix->method = 0;
15374 #endif
15375
15376 restart:
15377 reconsider_clip_changes (w, buffer);
15378
15379 /* Has the mode line to be updated? */
15380 update_mode_line = (w->update_mode_line
15381 || update_mode_lines
15382 || buffer->clip_changed
15383 || buffer->prevent_redisplay_optimizations_p);
15384
15385 if (MINI_WINDOW_P (w))
15386 {
15387 if (w == XWINDOW (echo_area_window)
15388 && !NILP (echo_area_buffer[0]))
15389 {
15390 if (update_mode_line)
15391 /* We may have to update a tty frame's menu bar or a
15392 tool-bar. Example `M-x C-h C-h C-g'. */
15393 goto finish_menu_bars;
15394 else
15395 /* We've already displayed the echo area glyphs in this window. */
15396 goto finish_scroll_bars;
15397 }
15398 else if ((w != XWINDOW (minibuf_window)
15399 || minibuf_level == 0)
15400 /* When buffer is nonempty, redisplay window normally. */
15401 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15402 /* Quail displays non-mini buffers in minibuffer window.
15403 In that case, redisplay the window normally. */
15404 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15405 {
15406 /* W is a mini-buffer window, but it's not active, so clear
15407 it. */
15408 int yb = window_text_bottom_y (w);
15409 struct glyph_row *row;
15410 int y;
15411
15412 for (y = 0, row = w->desired_matrix->rows;
15413 y < yb;
15414 y += row->height, ++row)
15415 blank_row (w, row, y);
15416 goto finish_scroll_bars;
15417 }
15418
15419 clear_glyph_matrix (w->desired_matrix);
15420 }
15421
15422 /* Otherwise set up data on this window; select its buffer and point
15423 value. */
15424 /* Really select the buffer, for the sake of buffer-local
15425 variables. */
15426 set_buffer_internal_1 (XBUFFER (w->buffer));
15427
15428 current_matrix_up_to_date_p
15429 = (!NILP (w->window_end_valid)
15430 && !current_buffer->clip_changed
15431 && !current_buffer->prevent_redisplay_optimizations_p
15432 && w->last_modified >= MODIFF
15433 && w->last_overlay_modified >= OVERLAY_MODIFF);
15434
15435 /* Run the window-bottom-change-functions
15436 if it is possible that the text on the screen has changed
15437 (either due to modification of the text, or any other reason). */
15438 if (!current_matrix_up_to_date_p
15439 && !NILP (Vwindow_text_change_functions))
15440 {
15441 safe_run_hooks (Qwindow_text_change_functions);
15442 goto restart;
15443 }
15444
15445 beg_unchanged = BEG_UNCHANGED;
15446 end_unchanged = END_UNCHANGED;
15447
15448 SET_TEXT_POS (opoint, PT, PT_BYTE);
15449
15450 specbind (Qinhibit_point_motion_hooks, Qt);
15451
15452 buffer_unchanged_p
15453 = (!NILP (w->window_end_valid)
15454 && !current_buffer->clip_changed
15455 && w->last_modified >= MODIFF
15456 && w->last_overlay_modified >= OVERLAY_MODIFF);
15457
15458 /* When windows_or_buffers_changed is non-zero, we can't rely on
15459 the window end being valid, so set it to nil there. */
15460 if (windows_or_buffers_changed)
15461 {
15462 /* If window starts on a continuation line, maybe adjust the
15463 window start in case the window's width changed. */
15464 if (XMARKER (w->start)->buffer == current_buffer)
15465 compute_window_start_on_continuation_line (w);
15466
15467 w->window_end_valid = Qnil;
15468 }
15469
15470 /* Some sanity checks. */
15471 CHECK_WINDOW_END (w);
15472 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15473 abort ();
15474 if (BYTEPOS (opoint) < CHARPOS (opoint))
15475 abort ();
15476
15477 /* If %c is in mode line, update it if needed. */
15478 if (!NILP (w->column_number_displayed)
15479 /* This alternative quickly identifies a common case
15480 where no change is needed. */
15481 && !(PT == w->last_point
15482 && w->last_modified >= MODIFF
15483 && w->last_overlay_modified >= OVERLAY_MODIFF)
15484 && (XFASTINT (w->column_number_displayed) != current_column ()))
15485 update_mode_line = 1;
15486
15487 /* Count number of windows showing the selected buffer. An indirect
15488 buffer counts as its base buffer. */
15489 if (!just_this_one_p)
15490 {
15491 struct buffer *current_base, *window_base;
15492 current_base = current_buffer;
15493 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15494 if (current_base->base_buffer)
15495 current_base = current_base->base_buffer;
15496 if (window_base->base_buffer)
15497 window_base = window_base->base_buffer;
15498 if (current_base == window_base)
15499 buffer_shared++;
15500 }
15501
15502 /* Point refers normally to the selected window. For any other
15503 window, set up appropriate value. */
15504 if (!EQ (window, selected_window))
15505 {
15506 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15507 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15508 if (new_pt < BEGV)
15509 {
15510 new_pt = BEGV;
15511 new_pt_byte = BEGV_BYTE;
15512 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15513 }
15514 else if (new_pt > (ZV - 1))
15515 {
15516 new_pt = ZV;
15517 new_pt_byte = ZV_BYTE;
15518 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15519 }
15520
15521 /* We don't use SET_PT so that the point-motion hooks don't run. */
15522 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15523 }
15524
15525 /* If any of the character widths specified in the display table
15526 have changed, invalidate the width run cache. It's true that
15527 this may be a bit late to catch such changes, but the rest of
15528 redisplay goes (non-fatally) haywire when the display table is
15529 changed, so why should we worry about doing any better? */
15530 if (current_buffer->width_run_cache)
15531 {
15532 struct Lisp_Char_Table *disptab = buffer_display_table ();
15533
15534 if (! disptab_matches_widthtab (disptab,
15535 XVECTOR (BVAR (current_buffer, width_table))))
15536 {
15537 invalidate_region_cache (current_buffer,
15538 current_buffer->width_run_cache,
15539 BEG, Z);
15540 recompute_width_table (current_buffer, disptab);
15541 }
15542 }
15543
15544 /* If window-start is screwed up, choose a new one. */
15545 if (XMARKER (w->start)->buffer != current_buffer)
15546 goto recenter;
15547
15548 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15549
15550 /* If someone specified a new starting point but did not insist,
15551 check whether it can be used. */
15552 if (w->optional_new_start
15553 && CHARPOS (startp) >= BEGV
15554 && CHARPOS (startp) <= ZV)
15555 {
15556 w->optional_new_start = 0;
15557 start_display (&it, w, startp);
15558 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15559 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15560 if (IT_CHARPOS (it) == PT)
15561 w->force_start = 1;
15562 /* IT may overshoot PT if text at PT is invisible. */
15563 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15564 w->force_start = 1;
15565 }
15566
15567 force_start:
15568
15569 /* Handle case where place to start displaying has been specified,
15570 unless the specified location is outside the accessible range. */
15571 if (w->force_start || w->frozen_window_start_p)
15572 {
15573 /* We set this later on if we have to adjust point. */
15574 int new_vpos = -1;
15575
15576 w->force_start = 0;
15577 w->vscroll = 0;
15578 w->window_end_valid = Qnil;
15579
15580 /* Forget any recorded base line for line number display. */
15581 if (!buffer_unchanged_p)
15582 w->base_line_number = Qnil;
15583
15584 /* Redisplay the mode line. Select the buffer properly for that.
15585 Also, run the hook window-scroll-functions
15586 because we have scrolled. */
15587 /* Note, we do this after clearing force_start because
15588 if there's an error, it is better to forget about force_start
15589 than to get into an infinite loop calling the hook functions
15590 and having them get more errors. */
15591 if (!update_mode_line
15592 || ! NILP (Vwindow_scroll_functions))
15593 {
15594 update_mode_line = 1;
15595 w->update_mode_line = 1;
15596 startp = run_window_scroll_functions (window, startp);
15597 }
15598
15599 w->last_modified = 0;
15600 w->last_overlay_modified = 0;
15601 if (CHARPOS (startp) < BEGV)
15602 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15603 else if (CHARPOS (startp) > ZV)
15604 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15605
15606 /* Redisplay, then check if cursor has been set during the
15607 redisplay. Give up if new fonts were loaded. */
15608 /* We used to issue a CHECK_MARGINS argument to try_window here,
15609 but this causes scrolling to fail when point begins inside
15610 the scroll margin (bug#148) -- cyd */
15611 if (!try_window (window, startp, 0))
15612 {
15613 w->force_start = 1;
15614 clear_glyph_matrix (w->desired_matrix);
15615 goto need_larger_matrices;
15616 }
15617
15618 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15619 {
15620 /* If point does not appear, try to move point so it does
15621 appear. The desired matrix has been built above, so we
15622 can use it here. */
15623 new_vpos = window_box_height (w) / 2;
15624 }
15625
15626 if (!cursor_row_fully_visible_p (w, 0, 0))
15627 {
15628 /* Point does appear, but on a line partly visible at end of window.
15629 Move it back to a fully-visible line. */
15630 new_vpos = window_box_height (w);
15631 }
15632
15633 /* If we need to move point for either of the above reasons,
15634 now actually do it. */
15635 if (new_vpos >= 0)
15636 {
15637 struct glyph_row *row;
15638
15639 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15640 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15641 ++row;
15642
15643 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15644 MATRIX_ROW_START_BYTEPOS (row));
15645
15646 if (w != XWINDOW (selected_window))
15647 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15648 else if (current_buffer == old)
15649 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15650
15651 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15652
15653 /* If we are highlighting the region, then we just changed
15654 the region, so redisplay to show it. */
15655 if (!NILP (Vtransient_mark_mode)
15656 && !NILP (BVAR (current_buffer, mark_active)))
15657 {
15658 clear_glyph_matrix (w->desired_matrix);
15659 if (!try_window (window, startp, 0))
15660 goto need_larger_matrices;
15661 }
15662 }
15663
15664 #ifdef GLYPH_DEBUG
15665 debug_method_add (w, "forced window start");
15666 #endif
15667 goto done;
15668 }
15669
15670 /* Handle case where text has not changed, only point, and it has
15671 not moved off the frame, and we are not retrying after hscroll.
15672 (current_matrix_up_to_date_p is nonzero when retrying.) */
15673 if (current_matrix_up_to_date_p
15674 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15675 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15676 {
15677 switch (rc)
15678 {
15679 case CURSOR_MOVEMENT_SUCCESS:
15680 used_current_matrix_p = 1;
15681 goto done;
15682
15683 case CURSOR_MOVEMENT_MUST_SCROLL:
15684 goto try_to_scroll;
15685
15686 default:
15687 abort ();
15688 }
15689 }
15690 /* If current starting point was originally the beginning of a line
15691 but no longer is, find a new starting point. */
15692 else if (w->start_at_line_beg
15693 && !(CHARPOS (startp) <= BEGV
15694 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15695 {
15696 #ifdef GLYPH_DEBUG
15697 debug_method_add (w, "recenter 1");
15698 #endif
15699 goto recenter;
15700 }
15701
15702 /* Try scrolling with try_window_id. Value is > 0 if update has
15703 been done, it is -1 if we know that the same window start will
15704 not work. It is 0 if unsuccessful for some other reason. */
15705 else if ((tem = try_window_id (w)) != 0)
15706 {
15707 #ifdef GLYPH_DEBUG
15708 debug_method_add (w, "try_window_id %d", tem);
15709 #endif
15710
15711 if (fonts_changed_p)
15712 goto need_larger_matrices;
15713 if (tem > 0)
15714 goto done;
15715
15716 /* Otherwise try_window_id has returned -1 which means that we
15717 don't want the alternative below this comment to execute. */
15718 }
15719 else if (CHARPOS (startp) >= BEGV
15720 && CHARPOS (startp) <= ZV
15721 && PT >= CHARPOS (startp)
15722 && (CHARPOS (startp) < ZV
15723 /* Avoid starting at end of buffer. */
15724 || CHARPOS (startp) == BEGV
15725 || (w->last_modified >= MODIFF
15726 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15727 {
15728 int d1, d2, d3, d4, d5, d6;
15729
15730 /* If first window line is a continuation line, and window start
15731 is inside the modified region, but the first change is before
15732 current window start, we must select a new window start.
15733
15734 However, if this is the result of a down-mouse event (e.g. by
15735 extending the mouse-drag-overlay), we don't want to select a
15736 new window start, since that would change the position under
15737 the mouse, resulting in an unwanted mouse-movement rather
15738 than a simple mouse-click. */
15739 if (!w->start_at_line_beg
15740 && NILP (do_mouse_tracking)
15741 && CHARPOS (startp) > BEGV
15742 && CHARPOS (startp) > BEG + beg_unchanged
15743 && CHARPOS (startp) <= Z - end_unchanged
15744 /* Even if w->start_at_line_beg is nil, a new window may
15745 start at a line_beg, since that's how set_buffer_window
15746 sets it. So, we need to check the return value of
15747 compute_window_start_on_continuation_line. (See also
15748 bug#197). */
15749 && XMARKER (w->start)->buffer == current_buffer
15750 && compute_window_start_on_continuation_line (w)
15751 /* It doesn't make sense to force the window start like we
15752 do at label force_start if it is already known that point
15753 will not be visible in the resulting window, because
15754 doing so will move point from its correct position
15755 instead of scrolling the window to bring point into view.
15756 See bug#9324. */
15757 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15758 {
15759 w->force_start = 1;
15760 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15761 goto force_start;
15762 }
15763
15764 #ifdef GLYPH_DEBUG
15765 debug_method_add (w, "same window start");
15766 #endif
15767
15768 /* Try to redisplay starting at same place as before.
15769 If point has not moved off frame, accept the results. */
15770 if (!current_matrix_up_to_date_p
15771 /* Don't use try_window_reusing_current_matrix in this case
15772 because a window scroll function can have changed the
15773 buffer. */
15774 || !NILP (Vwindow_scroll_functions)
15775 || MINI_WINDOW_P (w)
15776 || !(used_current_matrix_p
15777 = try_window_reusing_current_matrix (w)))
15778 {
15779 IF_DEBUG (debug_method_add (w, "1"));
15780 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15781 /* -1 means we need to scroll.
15782 0 means we need new matrices, but fonts_changed_p
15783 is set in that case, so we will detect it below. */
15784 goto try_to_scroll;
15785 }
15786
15787 if (fonts_changed_p)
15788 goto need_larger_matrices;
15789
15790 if (w->cursor.vpos >= 0)
15791 {
15792 if (!just_this_one_p
15793 || current_buffer->clip_changed
15794 || BEG_UNCHANGED < CHARPOS (startp))
15795 /* Forget any recorded base line for line number display. */
15796 w->base_line_number = Qnil;
15797
15798 if (!cursor_row_fully_visible_p (w, 1, 0))
15799 {
15800 clear_glyph_matrix (w->desired_matrix);
15801 last_line_misfit = 1;
15802 }
15803 /* Drop through and scroll. */
15804 else
15805 goto done;
15806 }
15807 else
15808 clear_glyph_matrix (w->desired_matrix);
15809 }
15810
15811 try_to_scroll:
15812
15813 w->last_modified = 0;
15814 w->last_overlay_modified = 0;
15815
15816 /* Redisplay the mode line. Select the buffer properly for that. */
15817 if (!update_mode_line)
15818 {
15819 update_mode_line = 1;
15820 w->update_mode_line = 1;
15821 }
15822
15823 /* Try to scroll by specified few lines. */
15824 if ((scroll_conservatively
15825 || emacs_scroll_step
15826 || temp_scroll_step
15827 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15828 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15829 && CHARPOS (startp) >= BEGV
15830 && CHARPOS (startp) <= ZV)
15831 {
15832 /* The function returns -1 if new fonts were loaded, 1 if
15833 successful, 0 if not successful. */
15834 int ss = try_scrolling (window, just_this_one_p,
15835 scroll_conservatively,
15836 emacs_scroll_step,
15837 temp_scroll_step, last_line_misfit);
15838 switch (ss)
15839 {
15840 case SCROLLING_SUCCESS:
15841 goto done;
15842
15843 case SCROLLING_NEED_LARGER_MATRICES:
15844 goto need_larger_matrices;
15845
15846 case SCROLLING_FAILED:
15847 break;
15848
15849 default:
15850 abort ();
15851 }
15852 }
15853
15854 /* Finally, just choose a place to start which positions point
15855 according to user preferences. */
15856
15857 recenter:
15858
15859 #ifdef GLYPH_DEBUG
15860 debug_method_add (w, "recenter");
15861 #endif
15862
15863 /* w->vscroll = 0; */
15864
15865 /* Forget any previously recorded base line for line number display. */
15866 if (!buffer_unchanged_p)
15867 w->base_line_number = Qnil;
15868
15869 /* Determine the window start relative to point. */
15870 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15871 it.current_y = it.last_visible_y;
15872 if (centering_position < 0)
15873 {
15874 int margin =
15875 scroll_margin > 0
15876 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15877 : 0;
15878 ptrdiff_t margin_pos = CHARPOS (startp);
15879 Lisp_Object aggressive;
15880 int scrolling_up;
15881
15882 /* If there is a scroll margin at the top of the window, find
15883 its character position. */
15884 if (margin
15885 /* Cannot call start_display if startp is not in the
15886 accessible region of the buffer. This can happen when we
15887 have just switched to a different buffer and/or changed
15888 its restriction. In that case, startp is initialized to
15889 the character position 1 (BEGV) because we did not yet
15890 have chance to display the buffer even once. */
15891 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15892 {
15893 struct it it1;
15894 void *it1data = NULL;
15895
15896 SAVE_IT (it1, it, it1data);
15897 start_display (&it1, w, startp);
15898 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15899 margin_pos = IT_CHARPOS (it1);
15900 RESTORE_IT (&it, &it, it1data);
15901 }
15902 scrolling_up = PT > margin_pos;
15903 aggressive =
15904 scrolling_up
15905 ? BVAR (current_buffer, scroll_up_aggressively)
15906 : BVAR (current_buffer, scroll_down_aggressively);
15907
15908 if (!MINI_WINDOW_P (w)
15909 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15910 {
15911 int pt_offset = 0;
15912
15913 /* Setting scroll-conservatively overrides
15914 scroll-*-aggressively. */
15915 if (!scroll_conservatively && NUMBERP (aggressive))
15916 {
15917 double float_amount = XFLOATINT (aggressive);
15918
15919 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15920 if (pt_offset == 0 && float_amount > 0)
15921 pt_offset = 1;
15922 if (pt_offset && margin > 0)
15923 margin -= 1;
15924 }
15925 /* Compute how much to move the window start backward from
15926 point so that point will be displayed where the user
15927 wants it. */
15928 if (scrolling_up)
15929 {
15930 centering_position = it.last_visible_y;
15931 if (pt_offset)
15932 centering_position -= pt_offset;
15933 centering_position -=
15934 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15935 + WINDOW_HEADER_LINE_HEIGHT (w);
15936 /* Don't let point enter the scroll margin near top of
15937 the window. */
15938 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15939 centering_position = margin * FRAME_LINE_HEIGHT (f);
15940 }
15941 else
15942 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15943 }
15944 else
15945 /* Set the window start half the height of the window backward
15946 from point. */
15947 centering_position = window_box_height (w) / 2;
15948 }
15949 move_it_vertically_backward (&it, centering_position);
15950
15951 eassert (IT_CHARPOS (it) >= BEGV);
15952
15953 /* The function move_it_vertically_backward may move over more
15954 than the specified y-distance. If it->w is small, e.g. a
15955 mini-buffer window, we may end up in front of the window's
15956 display area. Start displaying at the start of the line
15957 containing PT in this case. */
15958 if (it.current_y <= 0)
15959 {
15960 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15961 move_it_vertically_backward (&it, 0);
15962 it.current_y = 0;
15963 }
15964
15965 it.current_x = it.hpos = 0;
15966
15967 /* Set the window start position here explicitly, to avoid an
15968 infinite loop in case the functions in window-scroll-functions
15969 get errors. */
15970 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15971
15972 /* Run scroll hooks. */
15973 startp = run_window_scroll_functions (window, it.current.pos);
15974
15975 /* Redisplay the window. */
15976 if (!current_matrix_up_to_date_p
15977 || windows_or_buffers_changed
15978 || cursor_type_changed
15979 /* Don't use try_window_reusing_current_matrix in this case
15980 because it can have changed the buffer. */
15981 || !NILP (Vwindow_scroll_functions)
15982 || !just_this_one_p
15983 || MINI_WINDOW_P (w)
15984 || !(used_current_matrix_p
15985 = try_window_reusing_current_matrix (w)))
15986 try_window (window, startp, 0);
15987
15988 /* If new fonts have been loaded (due to fontsets), give up. We
15989 have to start a new redisplay since we need to re-adjust glyph
15990 matrices. */
15991 if (fonts_changed_p)
15992 goto need_larger_matrices;
15993
15994 /* If cursor did not appear assume that the middle of the window is
15995 in the first line of the window. Do it again with the next line.
15996 (Imagine a window of height 100, displaying two lines of height
15997 60. Moving back 50 from it->last_visible_y will end in the first
15998 line.) */
15999 if (w->cursor.vpos < 0)
16000 {
16001 if (!NILP (w->window_end_valid)
16002 && PT >= Z - XFASTINT (w->window_end_pos))
16003 {
16004 clear_glyph_matrix (w->desired_matrix);
16005 move_it_by_lines (&it, 1);
16006 try_window (window, it.current.pos, 0);
16007 }
16008 else if (PT < IT_CHARPOS (it))
16009 {
16010 clear_glyph_matrix (w->desired_matrix);
16011 move_it_by_lines (&it, -1);
16012 try_window (window, it.current.pos, 0);
16013 }
16014 else
16015 {
16016 /* Not much we can do about it. */
16017 }
16018 }
16019
16020 /* Consider the following case: Window starts at BEGV, there is
16021 invisible, intangible text at BEGV, so that display starts at
16022 some point START > BEGV. It can happen that we are called with
16023 PT somewhere between BEGV and START. Try to handle that case. */
16024 if (w->cursor.vpos < 0)
16025 {
16026 struct glyph_row *row = w->current_matrix->rows;
16027 if (row->mode_line_p)
16028 ++row;
16029 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16030 }
16031
16032 if (!cursor_row_fully_visible_p (w, 0, 0))
16033 {
16034 /* If vscroll is enabled, disable it and try again. */
16035 if (w->vscroll)
16036 {
16037 w->vscroll = 0;
16038 clear_glyph_matrix (w->desired_matrix);
16039 goto recenter;
16040 }
16041
16042 /* Users who set scroll-conservatively to a large number want
16043 point just above/below the scroll margin. If we ended up
16044 with point's row partially visible, move the window start to
16045 make that row fully visible and out of the margin. */
16046 if (scroll_conservatively > SCROLL_LIMIT)
16047 {
16048 int margin =
16049 scroll_margin > 0
16050 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16051 : 0;
16052 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16053
16054 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16055 clear_glyph_matrix (w->desired_matrix);
16056 if (1 == try_window (window, it.current.pos,
16057 TRY_WINDOW_CHECK_MARGINS))
16058 goto done;
16059 }
16060
16061 /* If centering point failed to make the whole line visible,
16062 put point at the top instead. That has to make the whole line
16063 visible, if it can be done. */
16064 if (centering_position == 0)
16065 goto done;
16066
16067 clear_glyph_matrix (w->desired_matrix);
16068 centering_position = 0;
16069 goto recenter;
16070 }
16071
16072 done:
16073
16074 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16075 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16076 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16077
16078 /* Display the mode line, if we must. */
16079 if ((update_mode_line
16080 /* If window not full width, must redo its mode line
16081 if (a) the window to its side is being redone and
16082 (b) we do a frame-based redisplay. This is a consequence
16083 of how inverted lines are drawn in frame-based redisplay. */
16084 || (!just_this_one_p
16085 && !FRAME_WINDOW_P (f)
16086 && !WINDOW_FULL_WIDTH_P (w))
16087 /* Line number to display. */
16088 || INTEGERP (w->base_line_pos)
16089 /* Column number is displayed and different from the one displayed. */
16090 || (!NILP (w->column_number_displayed)
16091 && (XFASTINT (w->column_number_displayed) != current_column ())))
16092 /* This means that the window has a mode line. */
16093 && (WINDOW_WANTS_MODELINE_P (w)
16094 || WINDOW_WANTS_HEADER_LINE_P (w)))
16095 {
16096 display_mode_lines (w);
16097
16098 /* If mode line height has changed, arrange for a thorough
16099 immediate redisplay using the correct mode line height. */
16100 if (WINDOW_WANTS_MODELINE_P (w)
16101 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16102 {
16103 fonts_changed_p = 1;
16104 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16105 = DESIRED_MODE_LINE_HEIGHT (w);
16106 }
16107
16108 /* If header line height has changed, arrange for a thorough
16109 immediate redisplay using the correct header line height. */
16110 if (WINDOW_WANTS_HEADER_LINE_P (w)
16111 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16112 {
16113 fonts_changed_p = 1;
16114 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16115 = DESIRED_HEADER_LINE_HEIGHT (w);
16116 }
16117
16118 if (fonts_changed_p)
16119 goto need_larger_matrices;
16120 }
16121
16122 if (!line_number_displayed
16123 && !BUFFERP (w->base_line_pos))
16124 {
16125 w->base_line_pos = Qnil;
16126 w->base_line_number = Qnil;
16127 }
16128
16129 finish_menu_bars:
16130
16131 /* When we reach a frame's selected window, redo the frame's menu bar. */
16132 if (update_mode_line
16133 && EQ (FRAME_SELECTED_WINDOW (f), window))
16134 {
16135 int redisplay_menu_p = 0;
16136
16137 if (FRAME_WINDOW_P (f))
16138 {
16139 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16140 || defined (HAVE_NS) || defined (USE_GTK)
16141 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16142 #else
16143 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16144 #endif
16145 }
16146 else
16147 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16148
16149 if (redisplay_menu_p)
16150 display_menu_bar (w);
16151
16152 #ifdef HAVE_WINDOW_SYSTEM
16153 if (FRAME_WINDOW_P (f))
16154 {
16155 #if defined (USE_GTK) || defined (HAVE_NS)
16156 if (FRAME_EXTERNAL_TOOL_BAR (f))
16157 redisplay_tool_bar (f);
16158 #else
16159 if (WINDOWP (f->tool_bar_window)
16160 && (FRAME_TOOL_BAR_LINES (f) > 0
16161 || !NILP (Vauto_resize_tool_bars))
16162 && redisplay_tool_bar (f))
16163 ignore_mouse_drag_p = 1;
16164 #endif
16165 }
16166 #endif
16167 }
16168
16169 #ifdef HAVE_WINDOW_SYSTEM
16170 if (FRAME_WINDOW_P (f)
16171 && update_window_fringes (w, (just_this_one_p
16172 || (!used_current_matrix_p && !overlay_arrow_seen)
16173 || w->pseudo_window_p)))
16174 {
16175 update_begin (f);
16176 BLOCK_INPUT;
16177 if (draw_window_fringes (w, 1))
16178 x_draw_vertical_border (w);
16179 UNBLOCK_INPUT;
16180 update_end (f);
16181 }
16182 #endif /* HAVE_WINDOW_SYSTEM */
16183
16184 /* We go to this label, with fonts_changed_p nonzero,
16185 if it is necessary to try again using larger glyph matrices.
16186 We have to redeem the scroll bar even in this case,
16187 because the loop in redisplay_internal expects that. */
16188 need_larger_matrices:
16189 ;
16190 finish_scroll_bars:
16191
16192 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16193 {
16194 /* Set the thumb's position and size. */
16195 set_vertical_scroll_bar (w);
16196
16197 /* Note that we actually used the scroll bar attached to this
16198 window, so it shouldn't be deleted at the end of redisplay. */
16199 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16200 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16201 }
16202
16203 /* Restore current_buffer and value of point in it. The window
16204 update may have changed the buffer, so first make sure `opoint'
16205 is still valid (Bug#6177). */
16206 if (CHARPOS (opoint) < BEGV)
16207 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16208 else if (CHARPOS (opoint) > ZV)
16209 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16210 else
16211 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16212
16213 set_buffer_internal_1 (old);
16214 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16215 shorter. This can be caused by log truncation in *Messages*. */
16216 if (CHARPOS (lpoint) <= ZV)
16217 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16218
16219 unbind_to (count, Qnil);
16220 }
16221
16222
16223 /* Build the complete desired matrix of WINDOW with a window start
16224 buffer position POS.
16225
16226 Value is 1 if successful. It is zero if fonts were loaded during
16227 redisplay which makes re-adjusting glyph matrices necessary, and -1
16228 if point would appear in the scroll margins.
16229 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16230 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16231 set in FLAGS.) */
16232
16233 int
16234 try_window (Lisp_Object window, struct text_pos pos, int flags)
16235 {
16236 struct window *w = XWINDOW (window);
16237 struct it it;
16238 struct glyph_row *last_text_row = NULL;
16239 struct frame *f = XFRAME (w->frame);
16240
16241 /* Make POS the new window start. */
16242 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16243
16244 /* Mark cursor position as unknown. No overlay arrow seen. */
16245 w->cursor.vpos = -1;
16246 overlay_arrow_seen = 0;
16247
16248 /* Initialize iterator and info to start at POS. */
16249 start_display (&it, w, pos);
16250
16251 /* Display all lines of W. */
16252 while (it.current_y < it.last_visible_y)
16253 {
16254 if (display_line (&it))
16255 last_text_row = it.glyph_row - 1;
16256 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16257 return 0;
16258 }
16259
16260 /* Don't let the cursor end in the scroll margins. */
16261 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16262 && !MINI_WINDOW_P (w))
16263 {
16264 int this_scroll_margin;
16265
16266 if (scroll_margin > 0)
16267 {
16268 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16269 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16270 }
16271 else
16272 this_scroll_margin = 0;
16273
16274 if ((w->cursor.y >= 0 /* not vscrolled */
16275 && w->cursor.y < this_scroll_margin
16276 && CHARPOS (pos) > BEGV
16277 && IT_CHARPOS (it) < ZV)
16278 /* rms: considering make_cursor_line_fully_visible_p here
16279 seems to give wrong results. We don't want to recenter
16280 when the last line is partly visible, we want to allow
16281 that case to be handled in the usual way. */
16282 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16283 {
16284 w->cursor.vpos = -1;
16285 clear_glyph_matrix (w->desired_matrix);
16286 return -1;
16287 }
16288 }
16289
16290 /* If bottom moved off end of frame, change mode line percentage. */
16291 if (XFASTINT (w->window_end_pos) <= 0
16292 && Z != IT_CHARPOS (it))
16293 w->update_mode_line = 1;
16294
16295 /* Set window_end_pos to the offset of the last character displayed
16296 on the window from the end of current_buffer. Set
16297 window_end_vpos to its row number. */
16298 if (last_text_row)
16299 {
16300 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16301 w->window_end_bytepos
16302 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16303 w->window_end_pos
16304 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16305 w->window_end_vpos
16306 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16307 eassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16308 ->displays_text_p);
16309 }
16310 else
16311 {
16312 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16313 w->window_end_pos = make_number (Z - ZV);
16314 w->window_end_vpos = make_number (0);
16315 }
16316
16317 /* But that is not valid info until redisplay finishes. */
16318 w->window_end_valid = Qnil;
16319 return 1;
16320 }
16321
16322
16323 \f
16324 /************************************************************************
16325 Window redisplay reusing current matrix when buffer has not changed
16326 ************************************************************************/
16327
16328 /* Try redisplay of window W showing an unchanged buffer with a
16329 different window start than the last time it was displayed by
16330 reusing its current matrix. Value is non-zero if successful.
16331 W->start is the new window start. */
16332
16333 static int
16334 try_window_reusing_current_matrix (struct window *w)
16335 {
16336 struct frame *f = XFRAME (w->frame);
16337 struct glyph_row *bottom_row;
16338 struct it it;
16339 struct run run;
16340 struct text_pos start, new_start;
16341 int nrows_scrolled, i;
16342 struct glyph_row *last_text_row;
16343 struct glyph_row *last_reused_text_row;
16344 struct glyph_row *start_row;
16345 int start_vpos, min_y, max_y;
16346
16347 #ifdef GLYPH_DEBUG
16348 if (inhibit_try_window_reusing)
16349 return 0;
16350 #endif
16351
16352 if (/* This function doesn't handle terminal frames. */
16353 !FRAME_WINDOW_P (f)
16354 /* Don't try to reuse the display if windows have been split
16355 or such. */
16356 || windows_or_buffers_changed
16357 || cursor_type_changed)
16358 return 0;
16359
16360 /* Can't do this if region may have changed. */
16361 if ((!NILP (Vtransient_mark_mode)
16362 && !NILP (BVAR (current_buffer, mark_active)))
16363 || !NILP (w->region_showing)
16364 || !NILP (Vshow_trailing_whitespace))
16365 return 0;
16366
16367 /* If top-line visibility has changed, give up. */
16368 if (WINDOW_WANTS_HEADER_LINE_P (w)
16369 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16370 return 0;
16371
16372 /* Give up if old or new display is scrolled vertically. We could
16373 make this function handle this, but right now it doesn't. */
16374 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16375 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16376 return 0;
16377
16378 /* The variable new_start now holds the new window start. The old
16379 start `start' can be determined from the current matrix. */
16380 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16381 start = start_row->minpos;
16382 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16383
16384 /* Clear the desired matrix for the display below. */
16385 clear_glyph_matrix (w->desired_matrix);
16386
16387 if (CHARPOS (new_start) <= CHARPOS (start))
16388 {
16389 /* Don't use this method if the display starts with an ellipsis
16390 displayed for invisible text. It's not easy to handle that case
16391 below, and it's certainly not worth the effort since this is
16392 not a frequent case. */
16393 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16394 return 0;
16395
16396 IF_DEBUG (debug_method_add (w, "twu1"));
16397
16398 /* Display up to a row that can be reused. The variable
16399 last_text_row is set to the last row displayed that displays
16400 text. Note that it.vpos == 0 if or if not there is a
16401 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16402 start_display (&it, w, new_start);
16403 w->cursor.vpos = -1;
16404 last_text_row = last_reused_text_row = NULL;
16405
16406 while (it.current_y < it.last_visible_y
16407 && !fonts_changed_p)
16408 {
16409 /* If we have reached into the characters in the START row,
16410 that means the line boundaries have changed. So we
16411 can't start copying with the row START. Maybe it will
16412 work to start copying with the following row. */
16413 while (IT_CHARPOS (it) > CHARPOS (start))
16414 {
16415 /* Advance to the next row as the "start". */
16416 start_row++;
16417 start = start_row->minpos;
16418 /* If there are no more rows to try, or just one, give up. */
16419 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16420 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16421 || CHARPOS (start) == ZV)
16422 {
16423 clear_glyph_matrix (w->desired_matrix);
16424 return 0;
16425 }
16426
16427 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16428 }
16429 /* If we have reached alignment, we can copy the rest of the
16430 rows. */
16431 if (IT_CHARPOS (it) == CHARPOS (start)
16432 /* Don't accept "alignment" inside a display vector,
16433 since start_row could have started in the middle of
16434 that same display vector (thus their character
16435 positions match), and we have no way of telling if
16436 that is the case. */
16437 && it.current.dpvec_index < 0)
16438 break;
16439
16440 if (display_line (&it))
16441 last_text_row = it.glyph_row - 1;
16442
16443 }
16444
16445 /* A value of current_y < last_visible_y means that we stopped
16446 at the previous window start, which in turn means that we
16447 have at least one reusable row. */
16448 if (it.current_y < it.last_visible_y)
16449 {
16450 struct glyph_row *row;
16451
16452 /* IT.vpos always starts from 0; it counts text lines. */
16453 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16454
16455 /* Find PT if not already found in the lines displayed. */
16456 if (w->cursor.vpos < 0)
16457 {
16458 int dy = it.current_y - start_row->y;
16459
16460 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16461 row = row_containing_pos (w, PT, row, NULL, dy);
16462 if (row)
16463 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16464 dy, nrows_scrolled);
16465 else
16466 {
16467 clear_glyph_matrix (w->desired_matrix);
16468 return 0;
16469 }
16470 }
16471
16472 /* Scroll the display. Do it before the current matrix is
16473 changed. The problem here is that update has not yet
16474 run, i.e. part of the current matrix is not up to date.
16475 scroll_run_hook will clear the cursor, and use the
16476 current matrix to get the height of the row the cursor is
16477 in. */
16478 run.current_y = start_row->y;
16479 run.desired_y = it.current_y;
16480 run.height = it.last_visible_y - it.current_y;
16481
16482 if (run.height > 0 && run.current_y != run.desired_y)
16483 {
16484 update_begin (f);
16485 FRAME_RIF (f)->update_window_begin_hook (w);
16486 FRAME_RIF (f)->clear_window_mouse_face (w);
16487 FRAME_RIF (f)->scroll_run_hook (w, &run);
16488 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16489 update_end (f);
16490 }
16491
16492 /* Shift current matrix down by nrows_scrolled lines. */
16493 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16494 rotate_matrix (w->current_matrix,
16495 start_vpos,
16496 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16497 nrows_scrolled);
16498
16499 /* Disable lines that must be updated. */
16500 for (i = 0; i < nrows_scrolled; ++i)
16501 (start_row + i)->enabled_p = 0;
16502
16503 /* Re-compute Y positions. */
16504 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16505 max_y = it.last_visible_y;
16506 for (row = start_row + nrows_scrolled;
16507 row < bottom_row;
16508 ++row)
16509 {
16510 row->y = it.current_y;
16511 row->visible_height = row->height;
16512
16513 if (row->y < min_y)
16514 row->visible_height -= min_y - row->y;
16515 if (row->y + row->height > max_y)
16516 row->visible_height -= row->y + row->height - max_y;
16517 if (row->fringe_bitmap_periodic_p)
16518 row->redraw_fringe_bitmaps_p = 1;
16519
16520 it.current_y += row->height;
16521
16522 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16523 last_reused_text_row = row;
16524 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16525 break;
16526 }
16527
16528 /* Disable lines in the current matrix which are now
16529 below the window. */
16530 for (++row; row < bottom_row; ++row)
16531 row->enabled_p = row->mode_line_p = 0;
16532 }
16533
16534 /* Update window_end_pos etc.; last_reused_text_row is the last
16535 reused row from the current matrix containing text, if any.
16536 The value of last_text_row is the last displayed line
16537 containing text. */
16538 if (last_reused_text_row)
16539 {
16540 w->window_end_bytepos
16541 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16542 w->window_end_pos
16543 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16544 w->window_end_vpos
16545 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16546 w->current_matrix));
16547 }
16548 else if (last_text_row)
16549 {
16550 w->window_end_bytepos
16551 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16552 w->window_end_pos
16553 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16554 w->window_end_vpos
16555 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16556 }
16557 else
16558 {
16559 /* This window must be completely empty. */
16560 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16561 w->window_end_pos = make_number (Z - ZV);
16562 w->window_end_vpos = make_number (0);
16563 }
16564 w->window_end_valid = Qnil;
16565
16566 /* Update hint: don't try scrolling again in update_window. */
16567 w->desired_matrix->no_scrolling_p = 1;
16568
16569 #ifdef GLYPH_DEBUG
16570 debug_method_add (w, "try_window_reusing_current_matrix 1");
16571 #endif
16572 return 1;
16573 }
16574 else if (CHARPOS (new_start) > CHARPOS (start))
16575 {
16576 struct glyph_row *pt_row, *row;
16577 struct glyph_row *first_reusable_row;
16578 struct glyph_row *first_row_to_display;
16579 int dy;
16580 int yb = window_text_bottom_y (w);
16581
16582 /* Find the row starting at new_start, if there is one. Don't
16583 reuse a partially visible line at the end. */
16584 first_reusable_row = start_row;
16585 while (first_reusable_row->enabled_p
16586 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16587 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16588 < CHARPOS (new_start)))
16589 ++first_reusable_row;
16590
16591 /* Give up if there is no row to reuse. */
16592 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16593 || !first_reusable_row->enabled_p
16594 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16595 != CHARPOS (new_start)))
16596 return 0;
16597
16598 /* We can reuse fully visible rows beginning with
16599 first_reusable_row to the end of the window. Set
16600 first_row_to_display to the first row that cannot be reused.
16601 Set pt_row to the row containing point, if there is any. */
16602 pt_row = NULL;
16603 for (first_row_to_display = first_reusable_row;
16604 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16605 ++first_row_to_display)
16606 {
16607 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16608 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16609 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16610 && first_row_to_display->ends_at_zv_p
16611 && pt_row == NULL)))
16612 pt_row = first_row_to_display;
16613 }
16614
16615 /* Start displaying at the start of first_row_to_display. */
16616 eassert (first_row_to_display->y < yb);
16617 init_to_row_start (&it, w, first_row_to_display);
16618
16619 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16620 - start_vpos);
16621 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16622 - nrows_scrolled);
16623 it.current_y = (first_row_to_display->y - first_reusable_row->y
16624 + WINDOW_HEADER_LINE_HEIGHT (w));
16625
16626 /* Display lines beginning with first_row_to_display in the
16627 desired matrix. Set last_text_row to the last row displayed
16628 that displays text. */
16629 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16630 if (pt_row == NULL)
16631 w->cursor.vpos = -1;
16632 last_text_row = NULL;
16633 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16634 if (display_line (&it))
16635 last_text_row = it.glyph_row - 1;
16636
16637 /* If point is in a reused row, adjust y and vpos of the cursor
16638 position. */
16639 if (pt_row)
16640 {
16641 w->cursor.vpos -= nrows_scrolled;
16642 w->cursor.y -= first_reusable_row->y - start_row->y;
16643 }
16644
16645 /* Give up if point isn't in a row displayed or reused. (This
16646 also handles the case where w->cursor.vpos < nrows_scrolled
16647 after the calls to display_line, which can happen with scroll
16648 margins. See bug#1295.) */
16649 if (w->cursor.vpos < 0)
16650 {
16651 clear_glyph_matrix (w->desired_matrix);
16652 return 0;
16653 }
16654
16655 /* Scroll the display. */
16656 run.current_y = first_reusable_row->y;
16657 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16658 run.height = it.last_visible_y - run.current_y;
16659 dy = run.current_y - run.desired_y;
16660
16661 if (run.height)
16662 {
16663 update_begin (f);
16664 FRAME_RIF (f)->update_window_begin_hook (w);
16665 FRAME_RIF (f)->clear_window_mouse_face (w);
16666 FRAME_RIF (f)->scroll_run_hook (w, &run);
16667 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16668 update_end (f);
16669 }
16670
16671 /* Adjust Y positions of reused rows. */
16672 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16673 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16674 max_y = it.last_visible_y;
16675 for (row = first_reusable_row; row < first_row_to_display; ++row)
16676 {
16677 row->y -= dy;
16678 row->visible_height = row->height;
16679 if (row->y < min_y)
16680 row->visible_height -= min_y - row->y;
16681 if (row->y + row->height > max_y)
16682 row->visible_height -= row->y + row->height - max_y;
16683 if (row->fringe_bitmap_periodic_p)
16684 row->redraw_fringe_bitmaps_p = 1;
16685 }
16686
16687 /* Scroll the current matrix. */
16688 eassert (nrows_scrolled > 0);
16689 rotate_matrix (w->current_matrix,
16690 start_vpos,
16691 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16692 -nrows_scrolled);
16693
16694 /* Disable rows not reused. */
16695 for (row -= nrows_scrolled; row < bottom_row; ++row)
16696 row->enabled_p = 0;
16697
16698 /* Point may have moved to a different line, so we cannot assume that
16699 the previous cursor position is valid; locate the correct row. */
16700 if (pt_row)
16701 {
16702 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16703 row < bottom_row
16704 && PT >= MATRIX_ROW_END_CHARPOS (row)
16705 && !row->ends_at_zv_p;
16706 row++)
16707 {
16708 w->cursor.vpos++;
16709 w->cursor.y = row->y;
16710 }
16711 if (row < bottom_row)
16712 {
16713 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16714 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16715
16716 /* Can't use this optimization with bidi-reordered glyph
16717 rows, unless cursor is already at point. */
16718 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16719 {
16720 if (!(w->cursor.hpos >= 0
16721 && w->cursor.hpos < row->used[TEXT_AREA]
16722 && BUFFERP (glyph->object)
16723 && glyph->charpos == PT))
16724 return 0;
16725 }
16726 else
16727 for (; glyph < end
16728 && (!BUFFERP (glyph->object)
16729 || glyph->charpos < PT);
16730 glyph++)
16731 {
16732 w->cursor.hpos++;
16733 w->cursor.x += glyph->pixel_width;
16734 }
16735 }
16736 }
16737
16738 /* Adjust window end. A null value of last_text_row means that
16739 the window end is in reused rows which in turn means that
16740 only its vpos can have changed. */
16741 if (last_text_row)
16742 {
16743 w->window_end_bytepos
16744 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16745 w->window_end_pos
16746 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16747 w->window_end_vpos
16748 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16749 }
16750 else
16751 {
16752 w->window_end_vpos
16753 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16754 }
16755
16756 w->window_end_valid = Qnil;
16757 w->desired_matrix->no_scrolling_p = 1;
16758
16759 #ifdef GLYPH_DEBUG
16760 debug_method_add (w, "try_window_reusing_current_matrix 2");
16761 #endif
16762 return 1;
16763 }
16764
16765 return 0;
16766 }
16767
16768
16769 \f
16770 /************************************************************************
16771 Window redisplay reusing current matrix when buffer has changed
16772 ************************************************************************/
16773
16774 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16775 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16776 ptrdiff_t *, ptrdiff_t *);
16777 static struct glyph_row *
16778 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16779 struct glyph_row *);
16780
16781
16782 /* Return the last row in MATRIX displaying text. If row START is
16783 non-null, start searching with that row. IT gives the dimensions
16784 of the display. Value is null if matrix is empty; otherwise it is
16785 a pointer to the row found. */
16786
16787 static struct glyph_row *
16788 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16789 struct glyph_row *start)
16790 {
16791 struct glyph_row *row, *row_found;
16792
16793 /* Set row_found to the last row in IT->w's current matrix
16794 displaying text. The loop looks funny but think of partially
16795 visible lines. */
16796 row_found = NULL;
16797 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16798 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16799 {
16800 eassert (row->enabled_p);
16801 row_found = row;
16802 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16803 break;
16804 ++row;
16805 }
16806
16807 return row_found;
16808 }
16809
16810
16811 /* Return the last row in the current matrix of W that is not affected
16812 by changes at the start of current_buffer that occurred since W's
16813 current matrix was built. Value is null if no such row exists.
16814
16815 BEG_UNCHANGED us the number of characters unchanged at the start of
16816 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16817 first changed character in current_buffer. Characters at positions <
16818 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16819 when the current matrix was built. */
16820
16821 static struct glyph_row *
16822 find_last_unchanged_at_beg_row (struct window *w)
16823 {
16824 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16825 struct glyph_row *row;
16826 struct glyph_row *row_found = NULL;
16827 int yb = window_text_bottom_y (w);
16828
16829 /* Find the last row displaying unchanged text. */
16830 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16831 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16832 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16833 ++row)
16834 {
16835 if (/* If row ends before first_changed_pos, it is unchanged,
16836 except in some case. */
16837 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16838 /* When row ends in ZV and we write at ZV it is not
16839 unchanged. */
16840 && !row->ends_at_zv_p
16841 /* When first_changed_pos is the end of a continued line,
16842 row is not unchanged because it may be no longer
16843 continued. */
16844 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16845 && (row->continued_p
16846 || row->exact_window_width_line_p))
16847 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16848 needs to be recomputed, so don't consider this row as
16849 unchanged. This happens when the last line was
16850 bidi-reordered and was killed immediately before this
16851 redisplay cycle. In that case, ROW->end stores the
16852 buffer position of the first visual-order character of
16853 the killed text, which is now beyond ZV. */
16854 && CHARPOS (row->end.pos) <= ZV)
16855 row_found = row;
16856
16857 /* Stop if last visible row. */
16858 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16859 break;
16860 }
16861
16862 return row_found;
16863 }
16864
16865
16866 /* Find the first glyph row in the current matrix of W that is not
16867 affected by changes at the end of current_buffer since the
16868 time W's current matrix was built.
16869
16870 Return in *DELTA the number of chars by which buffer positions in
16871 unchanged text at the end of current_buffer must be adjusted.
16872
16873 Return in *DELTA_BYTES the corresponding number of bytes.
16874
16875 Value is null if no such row exists, i.e. all rows are affected by
16876 changes. */
16877
16878 static struct glyph_row *
16879 find_first_unchanged_at_end_row (struct window *w,
16880 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16881 {
16882 struct glyph_row *row;
16883 struct glyph_row *row_found = NULL;
16884
16885 *delta = *delta_bytes = 0;
16886
16887 /* Display must not have been paused, otherwise the current matrix
16888 is not up to date. */
16889 eassert (!NILP (w->window_end_valid));
16890
16891 /* A value of window_end_pos >= END_UNCHANGED means that the window
16892 end is in the range of changed text. If so, there is no
16893 unchanged row at the end of W's current matrix. */
16894 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16895 return NULL;
16896
16897 /* Set row to the last row in W's current matrix displaying text. */
16898 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16899
16900 /* If matrix is entirely empty, no unchanged row exists. */
16901 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16902 {
16903 /* The value of row is the last glyph row in the matrix having a
16904 meaningful buffer position in it. The end position of row
16905 corresponds to window_end_pos. This allows us to translate
16906 buffer positions in the current matrix to current buffer
16907 positions for characters not in changed text. */
16908 ptrdiff_t Z_old =
16909 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16910 ptrdiff_t Z_BYTE_old =
16911 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16912 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16913 struct glyph_row *first_text_row
16914 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16915
16916 *delta = Z - Z_old;
16917 *delta_bytes = Z_BYTE - Z_BYTE_old;
16918
16919 /* Set last_unchanged_pos to the buffer position of the last
16920 character in the buffer that has not been changed. Z is the
16921 index + 1 of the last character in current_buffer, i.e. by
16922 subtracting END_UNCHANGED we get the index of the last
16923 unchanged character, and we have to add BEG to get its buffer
16924 position. */
16925 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16926 last_unchanged_pos_old = last_unchanged_pos - *delta;
16927
16928 /* Search backward from ROW for a row displaying a line that
16929 starts at a minimum position >= last_unchanged_pos_old. */
16930 for (; row > first_text_row; --row)
16931 {
16932 /* This used to abort, but it can happen.
16933 It is ok to just stop the search instead here. KFS. */
16934 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16935 break;
16936
16937 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16938 row_found = row;
16939 }
16940 }
16941
16942 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16943
16944 return row_found;
16945 }
16946
16947
16948 /* Make sure that glyph rows in the current matrix of window W
16949 reference the same glyph memory as corresponding rows in the
16950 frame's frame matrix. This function is called after scrolling W's
16951 current matrix on a terminal frame in try_window_id and
16952 try_window_reusing_current_matrix. */
16953
16954 static void
16955 sync_frame_with_window_matrix_rows (struct window *w)
16956 {
16957 struct frame *f = XFRAME (w->frame);
16958 struct glyph_row *window_row, *window_row_end, *frame_row;
16959
16960 /* Preconditions: W must be a leaf window and full-width. Its frame
16961 must have a frame matrix. */
16962 eassert (NILP (w->hchild) && NILP (w->vchild));
16963 eassert (WINDOW_FULL_WIDTH_P (w));
16964 eassert (!FRAME_WINDOW_P (f));
16965
16966 /* If W is a full-width window, glyph pointers in W's current matrix
16967 have, by definition, to be the same as glyph pointers in the
16968 corresponding frame matrix. Note that frame matrices have no
16969 marginal areas (see build_frame_matrix). */
16970 window_row = w->current_matrix->rows;
16971 window_row_end = window_row + w->current_matrix->nrows;
16972 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16973 while (window_row < window_row_end)
16974 {
16975 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16976 struct glyph *end = window_row->glyphs[LAST_AREA];
16977
16978 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16979 frame_row->glyphs[TEXT_AREA] = start;
16980 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16981 frame_row->glyphs[LAST_AREA] = end;
16982
16983 /* Disable frame rows whose corresponding window rows have
16984 been disabled in try_window_id. */
16985 if (!window_row->enabled_p)
16986 frame_row->enabled_p = 0;
16987
16988 ++window_row, ++frame_row;
16989 }
16990 }
16991
16992
16993 /* Find the glyph row in window W containing CHARPOS. Consider all
16994 rows between START and END (not inclusive). END null means search
16995 all rows to the end of the display area of W. Value is the row
16996 containing CHARPOS or null. */
16997
16998 struct glyph_row *
16999 row_containing_pos (struct window *w, ptrdiff_t charpos,
17000 struct glyph_row *start, struct glyph_row *end, int dy)
17001 {
17002 struct glyph_row *row = start;
17003 struct glyph_row *best_row = NULL;
17004 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17005 int last_y;
17006
17007 /* If we happen to start on a header-line, skip that. */
17008 if (row->mode_line_p)
17009 ++row;
17010
17011 if ((end && row >= end) || !row->enabled_p)
17012 return NULL;
17013
17014 last_y = window_text_bottom_y (w) - dy;
17015
17016 while (1)
17017 {
17018 /* Give up if we have gone too far. */
17019 if (end && row >= end)
17020 return NULL;
17021 /* This formerly returned if they were equal.
17022 I think that both quantities are of a "last plus one" type;
17023 if so, when they are equal, the row is within the screen. -- rms. */
17024 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17025 return NULL;
17026
17027 /* If it is in this row, return this row. */
17028 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17029 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17030 /* The end position of a row equals the start
17031 position of the next row. If CHARPOS is there, we
17032 would rather display it in the next line, except
17033 when this line ends in ZV. */
17034 && !row->ends_at_zv_p
17035 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17036 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17037 {
17038 struct glyph *g;
17039
17040 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17041 || (!best_row && !row->continued_p))
17042 return row;
17043 /* In bidi-reordered rows, there could be several rows
17044 occluding point, all of them belonging to the same
17045 continued line. We need to find the row which fits
17046 CHARPOS the best. */
17047 for (g = row->glyphs[TEXT_AREA];
17048 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17049 g++)
17050 {
17051 if (!STRINGP (g->object))
17052 {
17053 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17054 {
17055 mindif = eabs (g->charpos - charpos);
17056 best_row = row;
17057 /* Exact match always wins. */
17058 if (mindif == 0)
17059 return best_row;
17060 }
17061 }
17062 }
17063 }
17064 else if (best_row && !row->continued_p)
17065 return best_row;
17066 ++row;
17067 }
17068 }
17069
17070
17071 /* Try to redisplay window W by reusing its existing display. W's
17072 current matrix must be up to date when this function is called,
17073 i.e. window_end_valid must not be nil.
17074
17075 Value is
17076
17077 1 if display has been updated
17078 0 if otherwise unsuccessful
17079 -1 if redisplay with same window start is known not to succeed
17080
17081 The following steps are performed:
17082
17083 1. Find the last row in the current matrix of W that is not
17084 affected by changes at the start of current_buffer. If no such row
17085 is found, give up.
17086
17087 2. Find the first row in W's current matrix that is not affected by
17088 changes at the end of current_buffer. Maybe there is no such row.
17089
17090 3. Display lines beginning with the row + 1 found in step 1 to the
17091 row found in step 2 or, if step 2 didn't find a row, to the end of
17092 the window.
17093
17094 4. If cursor is not known to appear on the window, give up.
17095
17096 5. If display stopped at the row found in step 2, scroll the
17097 display and current matrix as needed.
17098
17099 6. Maybe display some lines at the end of W, if we must. This can
17100 happen under various circumstances, like a partially visible line
17101 becoming fully visible, or because newly displayed lines are displayed
17102 in smaller font sizes.
17103
17104 7. Update W's window end information. */
17105
17106 static int
17107 try_window_id (struct window *w)
17108 {
17109 struct frame *f = XFRAME (w->frame);
17110 struct glyph_matrix *current_matrix = w->current_matrix;
17111 struct glyph_matrix *desired_matrix = w->desired_matrix;
17112 struct glyph_row *last_unchanged_at_beg_row;
17113 struct glyph_row *first_unchanged_at_end_row;
17114 struct glyph_row *row;
17115 struct glyph_row *bottom_row;
17116 int bottom_vpos;
17117 struct it it;
17118 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17119 int dvpos, dy;
17120 struct text_pos start_pos;
17121 struct run run;
17122 int first_unchanged_at_end_vpos = 0;
17123 struct glyph_row *last_text_row, *last_text_row_at_end;
17124 struct text_pos start;
17125 ptrdiff_t first_changed_charpos, last_changed_charpos;
17126
17127 #ifdef GLYPH_DEBUG
17128 if (inhibit_try_window_id)
17129 return 0;
17130 #endif
17131
17132 /* This is handy for debugging. */
17133 #if 0
17134 #define GIVE_UP(X) \
17135 do { \
17136 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17137 return 0; \
17138 } while (0)
17139 #else
17140 #define GIVE_UP(X) return 0
17141 #endif
17142
17143 SET_TEXT_POS_FROM_MARKER (start, w->start);
17144
17145 /* Don't use this for mini-windows because these can show
17146 messages and mini-buffers, and we don't handle that here. */
17147 if (MINI_WINDOW_P (w))
17148 GIVE_UP (1);
17149
17150 /* This flag is used to prevent redisplay optimizations. */
17151 if (windows_or_buffers_changed || cursor_type_changed)
17152 GIVE_UP (2);
17153
17154 /* Verify that narrowing has not changed.
17155 Also verify that we were not told to prevent redisplay optimizations.
17156 It would be nice to further
17157 reduce the number of cases where this prevents try_window_id. */
17158 if (current_buffer->clip_changed
17159 || current_buffer->prevent_redisplay_optimizations_p)
17160 GIVE_UP (3);
17161
17162 /* Window must either use window-based redisplay or be full width. */
17163 if (!FRAME_WINDOW_P (f)
17164 && (!FRAME_LINE_INS_DEL_OK (f)
17165 || !WINDOW_FULL_WIDTH_P (w)))
17166 GIVE_UP (4);
17167
17168 /* Give up if point is known NOT to appear in W. */
17169 if (PT < CHARPOS (start))
17170 GIVE_UP (5);
17171
17172 /* Another way to prevent redisplay optimizations. */
17173 if (w->last_modified == 0)
17174 GIVE_UP (6);
17175
17176 /* Verify that window is not hscrolled. */
17177 if (w->hscroll != 0)
17178 GIVE_UP (7);
17179
17180 /* Verify that display wasn't paused. */
17181 if (NILP (w->window_end_valid))
17182 GIVE_UP (8);
17183
17184 /* Can't use this if highlighting a region because a cursor movement
17185 will do more than just set the cursor. */
17186 if (!NILP (Vtransient_mark_mode)
17187 && !NILP (BVAR (current_buffer, mark_active)))
17188 GIVE_UP (9);
17189
17190 /* Likewise if highlighting trailing whitespace. */
17191 if (!NILP (Vshow_trailing_whitespace))
17192 GIVE_UP (11);
17193
17194 /* Likewise if showing a region. */
17195 if (!NILP (w->region_showing))
17196 GIVE_UP (10);
17197
17198 /* Can't use this if overlay arrow position and/or string have
17199 changed. */
17200 if (overlay_arrows_changed_p ())
17201 GIVE_UP (12);
17202
17203 /* When word-wrap is on, adding a space to the first word of a
17204 wrapped line can change the wrap position, altering the line
17205 above it. It might be worthwhile to handle this more
17206 intelligently, but for now just redisplay from scratch. */
17207 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17208 GIVE_UP (21);
17209
17210 /* Under bidi reordering, adding or deleting a character in the
17211 beginning of a paragraph, before the first strong directional
17212 character, can change the base direction of the paragraph (unless
17213 the buffer specifies a fixed paragraph direction), which will
17214 require to redisplay the whole paragraph. It might be worthwhile
17215 to find the paragraph limits and widen the range of redisplayed
17216 lines to that, but for now just give up this optimization and
17217 redisplay from scratch. */
17218 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17219 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17220 GIVE_UP (22);
17221
17222 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17223 only if buffer has really changed. The reason is that the gap is
17224 initially at Z for freshly visited files. The code below would
17225 set end_unchanged to 0 in that case. */
17226 if (MODIFF > SAVE_MODIFF
17227 /* This seems to happen sometimes after saving a buffer. */
17228 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17229 {
17230 if (GPT - BEG < BEG_UNCHANGED)
17231 BEG_UNCHANGED = GPT - BEG;
17232 if (Z - GPT < END_UNCHANGED)
17233 END_UNCHANGED = Z - GPT;
17234 }
17235
17236 /* The position of the first and last character that has been changed. */
17237 first_changed_charpos = BEG + BEG_UNCHANGED;
17238 last_changed_charpos = Z - END_UNCHANGED;
17239
17240 /* If window starts after a line end, and the last change is in
17241 front of that newline, then changes don't affect the display.
17242 This case happens with stealth-fontification. Note that although
17243 the display is unchanged, glyph positions in the matrix have to
17244 be adjusted, of course. */
17245 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17246 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17247 && ((last_changed_charpos < CHARPOS (start)
17248 && CHARPOS (start) == BEGV)
17249 || (last_changed_charpos < CHARPOS (start) - 1
17250 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17251 {
17252 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17253 struct glyph_row *r0;
17254
17255 /* Compute how many chars/bytes have been added to or removed
17256 from the buffer. */
17257 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17258 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17259 Z_delta = Z - Z_old;
17260 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17261
17262 /* Give up if PT is not in the window. Note that it already has
17263 been checked at the start of try_window_id that PT is not in
17264 front of the window start. */
17265 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17266 GIVE_UP (13);
17267
17268 /* If window start is unchanged, we can reuse the whole matrix
17269 as is, after adjusting glyph positions. No need to compute
17270 the window end again, since its offset from Z hasn't changed. */
17271 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17272 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17273 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17274 /* PT must not be in a partially visible line. */
17275 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17276 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17277 {
17278 /* Adjust positions in the glyph matrix. */
17279 if (Z_delta || Z_delta_bytes)
17280 {
17281 struct glyph_row *r1
17282 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17283 increment_matrix_positions (w->current_matrix,
17284 MATRIX_ROW_VPOS (r0, current_matrix),
17285 MATRIX_ROW_VPOS (r1, current_matrix),
17286 Z_delta, Z_delta_bytes);
17287 }
17288
17289 /* Set the cursor. */
17290 row = row_containing_pos (w, PT, r0, NULL, 0);
17291 if (row)
17292 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17293 else
17294 abort ();
17295 return 1;
17296 }
17297 }
17298
17299 /* Handle the case that changes are all below what is displayed in
17300 the window, and that PT is in the window. This shortcut cannot
17301 be taken if ZV is visible in the window, and text has been added
17302 there that is visible in the window. */
17303 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17304 /* ZV is not visible in the window, or there are no
17305 changes at ZV, actually. */
17306 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17307 || first_changed_charpos == last_changed_charpos))
17308 {
17309 struct glyph_row *r0;
17310
17311 /* Give up if PT is not in the window. Note that it already has
17312 been checked at the start of try_window_id that PT is not in
17313 front of the window start. */
17314 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17315 GIVE_UP (14);
17316
17317 /* If window start is unchanged, we can reuse the whole matrix
17318 as is, without changing glyph positions since no text has
17319 been added/removed in front of the window end. */
17320 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17321 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17322 /* PT must not be in a partially visible line. */
17323 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17324 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17325 {
17326 /* We have to compute the window end anew since text
17327 could have been added/removed after it. */
17328 w->window_end_pos
17329 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17330 w->window_end_bytepos
17331 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17332
17333 /* Set the cursor. */
17334 row = row_containing_pos (w, PT, r0, NULL, 0);
17335 if (row)
17336 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17337 else
17338 abort ();
17339 return 2;
17340 }
17341 }
17342
17343 /* Give up if window start is in the changed area.
17344
17345 The condition used to read
17346
17347 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17348
17349 but why that was tested escapes me at the moment. */
17350 if (CHARPOS (start) >= first_changed_charpos
17351 && CHARPOS (start) <= last_changed_charpos)
17352 GIVE_UP (15);
17353
17354 /* Check that window start agrees with the start of the first glyph
17355 row in its current matrix. Check this after we know the window
17356 start is not in changed text, otherwise positions would not be
17357 comparable. */
17358 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17359 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17360 GIVE_UP (16);
17361
17362 /* Give up if the window ends in strings. Overlay strings
17363 at the end are difficult to handle, so don't try. */
17364 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17365 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17366 GIVE_UP (20);
17367
17368 /* Compute the position at which we have to start displaying new
17369 lines. Some of the lines at the top of the window might be
17370 reusable because they are not displaying changed text. Find the
17371 last row in W's current matrix not affected by changes at the
17372 start of current_buffer. Value is null if changes start in the
17373 first line of window. */
17374 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17375 if (last_unchanged_at_beg_row)
17376 {
17377 /* Avoid starting to display in the middle of a character, a TAB
17378 for instance. This is easier than to set up the iterator
17379 exactly, and it's not a frequent case, so the additional
17380 effort wouldn't really pay off. */
17381 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17382 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17383 && last_unchanged_at_beg_row > w->current_matrix->rows)
17384 --last_unchanged_at_beg_row;
17385
17386 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17387 GIVE_UP (17);
17388
17389 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17390 GIVE_UP (18);
17391 start_pos = it.current.pos;
17392
17393 /* Start displaying new lines in the desired matrix at the same
17394 vpos we would use in the current matrix, i.e. below
17395 last_unchanged_at_beg_row. */
17396 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17397 current_matrix);
17398 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17399 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17400
17401 eassert (it.hpos == 0 && it.current_x == 0);
17402 }
17403 else
17404 {
17405 /* There are no reusable lines at the start of the window.
17406 Start displaying in the first text line. */
17407 start_display (&it, w, start);
17408 it.vpos = it.first_vpos;
17409 start_pos = it.current.pos;
17410 }
17411
17412 /* Find the first row that is not affected by changes at the end of
17413 the buffer. Value will be null if there is no unchanged row, in
17414 which case we must redisplay to the end of the window. delta
17415 will be set to the value by which buffer positions beginning with
17416 first_unchanged_at_end_row have to be adjusted due to text
17417 changes. */
17418 first_unchanged_at_end_row
17419 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17420 IF_DEBUG (debug_delta = delta);
17421 IF_DEBUG (debug_delta_bytes = delta_bytes);
17422
17423 /* Set stop_pos to the buffer position up to which we will have to
17424 display new lines. If first_unchanged_at_end_row != NULL, this
17425 is the buffer position of the start of the line displayed in that
17426 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17427 that we don't stop at a buffer position. */
17428 stop_pos = 0;
17429 if (first_unchanged_at_end_row)
17430 {
17431 eassert (last_unchanged_at_beg_row == NULL
17432 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17433
17434 /* If this is a continuation line, move forward to the next one
17435 that isn't. Changes in lines above affect this line.
17436 Caution: this may move first_unchanged_at_end_row to a row
17437 not displaying text. */
17438 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17439 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17440 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17441 < it.last_visible_y))
17442 ++first_unchanged_at_end_row;
17443
17444 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17445 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17446 >= it.last_visible_y))
17447 first_unchanged_at_end_row = NULL;
17448 else
17449 {
17450 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17451 + delta);
17452 first_unchanged_at_end_vpos
17453 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17454 eassert (stop_pos >= Z - END_UNCHANGED);
17455 }
17456 }
17457 else if (last_unchanged_at_beg_row == NULL)
17458 GIVE_UP (19);
17459
17460
17461 #ifdef GLYPH_DEBUG
17462
17463 /* Either there is no unchanged row at the end, or the one we have
17464 now displays text. This is a necessary condition for the window
17465 end pos calculation at the end of this function. */
17466 eassert (first_unchanged_at_end_row == NULL
17467 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17468
17469 debug_last_unchanged_at_beg_vpos
17470 = (last_unchanged_at_beg_row
17471 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17472 : -1);
17473 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17474
17475 #endif /* GLYPH_DEBUG */
17476
17477
17478 /* Display new lines. Set last_text_row to the last new line
17479 displayed which has text on it, i.e. might end up as being the
17480 line where the window_end_vpos is. */
17481 w->cursor.vpos = -1;
17482 last_text_row = NULL;
17483 overlay_arrow_seen = 0;
17484 while (it.current_y < it.last_visible_y
17485 && !fonts_changed_p
17486 && (first_unchanged_at_end_row == NULL
17487 || IT_CHARPOS (it) < stop_pos))
17488 {
17489 if (display_line (&it))
17490 last_text_row = it.glyph_row - 1;
17491 }
17492
17493 if (fonts_changed_p)
17494 return -1;
17495
17496
17497 /* Compute differences in buffer positions, y-positions etc. for
17498 lines reused at the bottom of the window. Compute what we can
17499 scroll. */
17500 if (first_unchanged_at_end_row
17501 /* No lines reused because we displayed everything up to the
17502 bottom of the window. */
17503 && it.current_y < it.last_visible_y)
17504 {
17505 dvpos = (it.vpos
17506 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17507 current_matrix));
17508 dy = it.current_y - first_unchanged_at_end_row->y;
17509 run.current_y = first_unchanged_at_end_row->y;
17510 run.desired_y = run.current_y + dy;
17511 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17512 }
17513 else
17514 {
17515 delta = delta_bytes = dvpos = dy
17516 = run.current_y = run.desired_y = run.height = 0;
17517 first_unchanged_at_end_row = NULL;
17518 }
17519 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17520
17521
17522 /* Find the cursor if not already found. We have to decide whether
17523 PT will appear on this window (it sometimes doesn't, but this is
17524 not a very frequent case.) This decision has to be made before
17525 the current matrix is altered. A value of cursor.vpos < 0 means
17526 that PT is either in one of the lines beginning at
17527 first_unchanged_at_end_row or below the window. Don't care for
17528 lines that might be displayed later at the window end; as
17529 mentioned, this is not a frequent case. */
17530 if (w->cursor.vpos < 0)
17531 {
17532 /* Cursor in unchanged rows at the top? */
17533 if (PT < CHARPOS (start_pos)
17534 && last_unchanged_at_beg_row)
17535 {
17536 row = row_containing_pos (w, PT,
17537 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17538 last_unchanged_at_beg_row + 1, 0);
17539 if (row)
17540 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17541 }
17542
17543 /* Start from first_unchanged_at_end_row looking for PT. */
17544 else if (first_unchanged_at_end_row)
17545 {
17546 row = row_containing_pos (w, PT - delta,
17547 first_unchanged_at_end_row, NULL, 0);
17548 if (row)
17549 set_cursor_from_row (w, row, w->current_matrix, delta,
17550 delta_bytes, dy, dvpos);
17551 }
17552
17553 /* Give up if cursor was not found. */
17554 if (w->cursor.vpos < 0)
17555 {
17556 clear_glyph_matrix (w->desired_matrix);
17557 return -1;
17558 }
17559 }
17560
17561 /* Don't let the cursor end in the scroll margins. */
17562 {
17563 int this_scroll_margin, cursor_height;
17564
17565 this_scroll_margin =
17566 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17567 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17568 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17569
17570 if ((w->cursor.y < this_scroll_margin
17571 && CHARPOS (start) > BEGV)
17572 /* Old redisplay didn't take scroll margin into account at the bottom,
17573 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17574 || (w->cursor.y + (make_cursor_line_fully_visible_p
17575 ? cursor_height + this_scroll_margin
17576 : 1)) > it.last_visible_y)
17577 {
17578 w->cursor.vpos = -1;
17579 clear_glyph_matrix (w->desired_matrix);
17580 return -1;
17581 }
17582 }
17583
17584 /* Scroll the display. Do it before changing the current matrix so
17585 that xterm.c doesn't get confused about where the cursor glyph is
17586 found. */
17587 if (dy && run.height)
17588 {
17589 update_begin (f);
17590
17591 if (FRAME_WINDOW_P (f))
17592 {
17593 FRAME_RIF (f)->update_window_begin_hook (w);
17594 FRAME_RIF (f)->clear_window_mouse_face (w);
17595 FRAME_RIF (f)->scroll_run_hook (w, &run);
17596 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17597 }
17598 else
17599 {
17600 /* Terminal frame. In this case, dvpos gives the number of
17601 lines to scroll by; dvpos < 0 means scroll up. */
17602 int from_vpos
17603 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17604 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17605 int end = (WINDOW_TOP_EDGE_LINE (w)
17606 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17607 + window_internal_height (w));
17608
17609 #if defined (HAVE_GPM) || defined (MSDOS)
17610 x_clear_window_mouse_face (w);
17611 #endif
17612 /* Perform the operation on the screen. */
17613 if (dvpos > 0)
17614 {
17615 /* Scroll last_unchanged_at_beg_row to the end of the
17616 window down dvpos lines. */
17617 set_terminal_window (f, end);
17618
17619 /* On dumb terminals delete dvpos lines at the end
17620 before inserting dvpos empty lines. */
17621 if (!FRAME_SCROLL_REGION_OK (f))
17622 ins_del_lines (f, end - dvpos, -dvpos);
17623
17624 /* Insert dvpos empty lines in front of
17625 last_unchanged_at_beg_row. */
17626 ins_del_lines (f, from, dvpos);
17627 }
17628 else if (dvpos < 0)
17629 {
17630 /* Scroll up last_unchanged_at_beg_vpos to the end of
17631 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17632 set_terminal_window (f, end);
17633
17634 /* Delete dvpos lines in front of
17635 last_unchanged_at_beg_vpos. ins_del_lines will set
17636 the cursor to the given vpos and emit |dvpos| delete
17637 line sequences. */
17638 ins_del_lines (f, from + dvpos, dvpos);
17639
17640 /* On a dumb terminal insert dvpos empty lines at the
17641 end. */
17642 if (!FRAME_SCROLL_REGION_OK (f))
17643 ins_del_lines (f, end + dvpos, -dvpos);
17644 }
17645
17646 set_terminal_window (f, 0);
17647 }
17648
17649 update_end (f);
17650 }
17651
17652 /* Shift reused rows of the current matrix to the right position.
17653 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17654 text. */
17655 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17656 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17657 if (dvpos < 0)
17658 {
17659 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17660 bottom_vpos, dvpos);
17661 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17662 bottom_vpos, 0);
17663 }
17664 else if (dvpos > 0)
17665 {
17666 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17667 bottom_vpos, dvpos);
17668 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17669 first_unchanged_at_end_vpos + dvpos, 0);
17670 }
17671
17672 /* For frame-based redisplay, make sure that current frame and window
17673 matrix are in sync with respect to glyph memory. */
17674 if (!FRAME_WINDOW_P (f))
17675 sync_frame_with_window_matrix_rows (w);
17676
17677 /* Adjust buffer positions in reused rows. */
17678 if (delta || delta_bytes)
17679 increment_matrix_positions (current_matrix,
17680 first_unchanged_at_end_vpos + dvpos,
17681 bottom_vpos, delta, delta_bytes);
17682
17683 /* Adjust Y positions. */
17684 if (dy)
17685 shift_glyph_matrix (w, current_matrix,
17686 first_unchanged_at_end_vpos + dvpos,
17687 bottom_vpos, dy);
17688
17689 if (first_unchanged_at_end_row)
17690 {
17691 first_unchanged_at_end_row += dvpos;
17692 if (first_unchanged_at_end_row->y >= it.last_visible_y
17693 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17694 first_unchanged_at_end_row = NULL;
17695 }
17696
17697 /* If scrolling up, there may be some lines to display at the end of
17698 the window. */
17699 last_text_row_at_end = NULL;
17700 if (dy < 0)
17701 {
17702 /* Scrolling up can leave for example a partially visible line
17703 at the end of the window to be redisplayed. */
17704 /* Set last_row to the glyph row in the current matrix where the
17705 window end line is found. It has been moved up or down in
17706 the matrix by dvpos. */
17707 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17708 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17709
17710 /* If last_row is the window end line, it should display text. */
17711 eassert (last_row->displays_text_p);
17712
17713 /* If window end line was partially visible before, begin
17714 displaying at that line. Otherwise begin displaying with the
17715 line following it. */
17716 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17717 {
17718 init_to_row_start (&it, w, last_row);
17719 it.vpos = last_vpos;
17720 it.current_y = last_row->y;
17721 }
17722 else
17723 {
17724 init_to_row_end (&it, w, last_row);
17725 it.vpos = 1 + last_vpos;
17726 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17727 ++last_row;
17728 }
17729
17730 /* We may start in a continuation line. If so, we have to
17731 get the right continuation_lines_width and current_x. */
17732 it.continuation_lines_width = last_row->continuation_lines_width;
17733 it.hpos = it.current_x = 0;
17734
17735 /* Display the rest of the lines at the window end. */
17736 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17737 while (it.current_y < it.last_visible_y
17738 && !fonts_changed_p)
17739 {
17740 /* Is it always sure that the display agrees with lines in
17741 the current matrix? I don't think so, so we mark rows
17742 displayed invalid in the current matrix by setting their
17743 enabled_p flag to zero. */
17744 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17745 if (display_line (&it))
17746 last_text_row_at_end = it.glyph_row - 1;
17747 }
17748 }
17749
17750 /* Update window_end_pos and window_end_vpos. */
17751 if (first_unchanged_at_end_row
17752 && !last_text_row_at_end)
17753 {
17754 /* Window end line if one of the preserved rows from the current
17755 matrix. Set row to the last row displaying text in current
17756 matrix starting at first_unchanged_at_end_row, after
17757 scrolling. */
17758 eassert (first_unchanged_at_end_row->displays_text_p);
17759 row = find_last_row_displaying_text (w->current_matrix, &it,
17760 first_unchanged_at_end_row);
17761 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17762
17763 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17764 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17765 w->window_end_vpos
17766 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17767 eassert (w->window_end_bytepos >= 0);
17768 IF_DEBUG (debug_method_add (w, "A"));
17769 }
17770 else if (last_text_row_at_end)
17771 {
17772 w->window_end_pos
17773 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17774 w->window_end_bytepos
17775 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17776 w->window_end_vpos
17777 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17778 eassert (w->window_end_bytepos >= 0);
17779 IF_DEBUG (debug_method_add (w, "B"));
17780 }
17781 else if (last_text_row)
17782 {
17783 /* We have displayed either to the end of the window or at the
17784 end of the window, i.e. the last row with text is to be found
17785 in the desired matrix. */
17786 w->window_end_pos
17787 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17788 w->window_end_bytepos
17789 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17790 w->window_end_vpos
17791 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17792 eassert (w->window_end_bytepos >= 0);
17793 }
17794 else if (first_unchanged_at_end_row == NULL
17795 && last_text_row == NULL
17796 && last_text_row_at_end == NULL)
17797 {
17798 /* Displayed to end of window, but no line containing text was
17799 displayed. Lines were deleted at the end of the window. */
17800 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17801 int vpos = XFASTINT (w->window_end_vpos);
17802 struct glyph_row *current_row = current_matrix->rows + vpos;
17803 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17804
17805 for (row = NULL;
17806 row == NULL && vpos >= first_vpos;
17807 --vpos, --current_row, --desired_row)
17808 {
17809 if (desired_row->enabled_p)
17810 {
17811 if (desired_row->displays_text_p)
17812 row = desired_row;
17813 }
17814 else if (current_row->displays_text_p)
17815 row = current_row;
17816 }
17817
17818 eassert (row != NULL);
17819 w->window_end_vpos = make_number (vpos + 1);
17820 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17821 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17822 eassert (w->window_end_bytepos >= 0);
17823 IF_DEBUG (debug_method_add (w, "C"));
17824 }
17825 else
17826 abort ();
17827
17828 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17829 debug_end_vpos = XFASTINT (w->window_end_vpos));
17830
17831 /* Record that display has not been completed. */
17832 w->window_end_valid = Qnil;
17833 w->desired_matrix->no_scrolling_p = 1;
17834 return 3;
17835
17836 #undef GIVE_UP
17837 }
17838
17839
17840 \f
17841 /***********************************************************************
17842 More debugging support
17843 ***********************************************************************/
17844
17845 #ifdef GLYPH_DEBUG
17846
17847 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17848 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17849 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17850
17851
17852 /* Dump the contents of glyph matrix MATRIX on stderr.
17853
17854 GLYPHS 0 means don't show glyph contents.
17855 GLYPHS 1 means show glyphs in short form
17856 GLYPHS > 1 means show glyphs in long form. */
17857
17858 void
17859 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17860 {
17861 int i;
17862 for (i = 0; i < matrix->nrows; ++i)
17863 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17864 }
17865
17866
17867 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17868 the glyph row and area where the glyph comes from. */
17869
17870 void
17871 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17872 {
17873 if (glyph->type == CHAR_GLYPH)
17874 {
17875 fprintf (stderr,
17876 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17877 glyph - row->glyphs[TEXT_AREA],
17878 'C',
17879 glyph->charpos,
17880 (BUFFERP (glyph->object)
17881 ? 'B'
17882 : (STRINGP (glyph->object)
17883 ? 'S'
17884 : '-')),
17885 glyph->pixel_width,
17886 glyph->u.ch,
17887 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17888 ? glyph->u.ch
17889 : '.'),
17890 glyph->face_id,
17891 glyph->left_box_line_p,
17892 glyph->right_box_line_p);
17893 }
17894 else if (glyph->type == STRETCH_GLYPH)
17895 {
17896 fprintf (stderr,
17897 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17898 glyph - row->glyphs[TEXT_AREA],
17899 'S',
17900 glyph->charpos,
17901 (BUFFERP (glyph->object)
17902 ? 'B'
17903 : (STRINGP (glyph->object)
17904 ? 'S'
17905 : '-')),
17906 glyph->pixel_width,
17907 0,
17908 '.',
17909 glyph->face_id,
17910 glyph->left_box_line_p,
17911 glyph->right_box_line_p);
17912 }
17913 else if (glyph->type == IMAGE_GLYPH)
17914 {
17915 fprintf (stderr,
17916 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17917 glyph - row->glyphs[TEXT_AREA],
17918 'I',
17919 glyph->charpos,
17920 (BUFFERP (glyph->object)
17921 ? 'B'
17922 : (STRINGP (glyph->object)
17923 ? 'S'
17924 : '-')),
17925 glyph->pixel_width,
17926 glyph->u.img_id,
17927 '.',
17928 glyph->face_id,
17929 glyph->left_box_line_p,
17930 glyph->right_box_line_p);
17931 }
17932 else if (glyph->type == COMPOSITE_GLYPH)
17933 {
17934 fprintf (stderr,
17935 " %5td %4c %6"pI"d %c %3d 0x%05x",
17936 glyph - row->glyphs[TEXT_AREA],
17937 '+',
17938 glyph->charpos,
17939 (BUFFERP (glyph->object)
17940 ? 'B'
17941 : (STRINGP (glyph->object)
17942 ? 'S'
17943 : '-')),
17944 glyph->pixel_width,
17945 glyph->u.cmp.id);
17946 if (glyph->u.cmp.automatic)
17947 fprintf (stderr,
17948 "[%d-%d]",
17949 glyph->slice.cmp.from, glyph->slice.cmp.to);
17950 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17951 glyph->face_id,
17952 glyph->left_box_line_p,
17953 glyph->right_box_line_p);
17954 }
17955 }
17956
17957
17958 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17959 GLYPHS 0 means don't show glyph contents.
17960 GLYPHS 1 means show glyphs in short form
17961 GLYPHS > 1 means show glyphs in long form. */
17962
17963 void
17964 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17965 {
17966 if (glyphs != 1)
17967 {
17968 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17969 fprintf (stderr, "======================================================================\n");
17970
17971 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17972 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17973 vpos,
17974 MATRIX_ROW_START_CHARPOS (row),
17975 MATRIX_ROW_END_CHARPOS (row),
17976 row->used[TEXT_AREA],
17977 row->contains_overlapping_glyphs_p,
17978 row->enabled_p,
17979 row->truncated_on_left_p,
17980 row->truncated_on_right_p,
17981 row->continued_p,
17982 MATRIX_ROW_CONTINUATION_LINE_P (row),
17983 row->displays_text_p,
17984 row->ends_at_zv_p,
17985 row->fill_line_p,
17986 row->ends_in_middle_of_char_p,
17987 row->starts_in_middle_of_char_p,
17988 row->mouse_face_p,
17989 row->x,
17990 row->y,
17991 row->pixel_width,
17992 row->height,
17993 row->visible_height,
17994 row->ascent,
17995 row->phys_ascent);
17996 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17997 row->end.overlay_string_index,
17998 row->continuation_lines_width);
17999 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18000 CHARPOS (row->start.string_pos),
18001 CHARPOS (row->end.string_pos));
18002 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18003 row->end.dpvec_index);
18004 }
18005
18006 if (glyphs > 1)
18007 {
18008 int area;
18009
18010 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18011 {
18012 struct glyph *glyph = row->glyphs[area];
18013 struct glyph *glyph_end = glyph + row->used[area];
18014
18015 /* Glyph for a line end in text. */
18016 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18017 ++glyph_end;
18018
18019 if (glyph < glyph_end)
18020 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18021
18022 for (; glyph < glyph_end; ++glyph)
18023 dump_glyph (row, glyph, area);
18024 }
18025 }
18026 else if (glyphs == 1)
18027 {
18028 int area;
18029
18030 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18031 {
18032 char *s = alloca (row->used[area] + 1);
18033 int i;
18034
18035 for (i = 0; i < row->used[area]; ++i)
18036 {
18037 struct glyph *glyph = row->glyphs[area] + i;
18038 if (glyph->type == CHAR_GLYPH
18039 && glyph->u.ch < 0x80
18040 && glyph->u.ch >= ' ')
18041 s[i] = glyph->u.ch;
18042 else
18043 s[i] = '.';
18044 }
18045
18046 s[i] = '\0';
18047 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18048 }
18049 }
18050 }
18051
18052
18053 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18054 Sdump_glyph_matrix, 0, 1, "p",
18055 doc: /* Dump the current matrix of the selected window to stderr.
18056 Shows contents of glyph row structures. With non-nil
18057 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18058 glyphs in short form, otherwise show glyphs in long form. */)
18059 (Lisp_Object glyphs)
18060 {
18061 struct window *w = XWINDOW (selected_window);
18062 struct buffer *buffer = XBUFFER (w->buffer);
18063
18064 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18065 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18066 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18067 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18068 fprintf (stderr, "=============================================\n");
18069 dump_glyph_matrix (w->current_matrix,
18070 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18071 return Qnil;
18072 }
18073
18074
18075 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18076 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18077 (void)
18078 {
18079 struct frame *f = XFRAME (selected_frame);
18080 dump_glyph_matrix (f->current_matrix, 1);
18081 return Qnil;
18082 }
18083
18084
18085 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18086 doc: /* Dump glyph row ROW to stderr.
18087 GLYPH 0 means don't dump glyphs.
18088 GLYPH 1 means dump glyphs in short form.
18089 GLYPH > 1 or omitted means dump glyphs in long form. */)
18090 (Lisp_Object row, Lisp_Object glyphs)
18091 {
18092 struct glyph_matrix *matrix;
18093 EMACS_INT vpos;
18094
18095 CHECK_NUMBER (row);
18096 matrix = XWINDOW (selected_window)->current_matrix;
18097 vpos = XINT (row);
18098 if (vpos >= 0 && vpos < matrix->nrows)
18099 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18100 vpos,
18101 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18102 return Qnil;
18103 }
18104
18105
18106 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18107 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18108 GLYPH 0 means don't dump glyphs.
18109 GLYPH 1 means dump glyphs in short form.
18110 GLYPH > 1 or omitted means dump glyphs in long form. */)
18111 (Lisp_Object row, Lisp_Object glyphs)
18112 {
18113 struct frame *sf = SELECTED_FRAME ();
18114 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18115 EMACS_INT vpos;
18116
18117 CHECK_NUMBER (row);
18118 vpos = XINT (row);
18119 if (vpos >= 0 && vpos < m->nrows)
18120 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18121 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18122 return Qnil;
18123 }
18124
18125
18126 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18127 doc: /* Toggle tracing of redisplay.
18128 With ARG, turn tracing on if and only if ARG is positive. */)
18129 (Lisp_Object arg)
18130 {
18131 if (NILP (arg))
18132 trace_redisplay_p = !trace_redisplay_p;
18133 else
18134 {
18135 arg = Fprefix_numeric_value (arg);
18136 trace_redisplay_p = XINT (arg) > 0;
18137 }
18138
18139 return Qnil;
18140 }
18141
18142
18143 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18144 doc: /* Like `format', but print result to stderr.
18145 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18146 (ptrdiff_t nargs, Lisp_Object *args)
18147 {
18148 Lisp_Object s = Fformat (nargs, args);
18149 fprintf (stderr, "%s", SDATA (s));
18150 return Qnil;
18151 }
18152
18153 #endif /* GLYPH_DEBUG */
18154
18155
18156 \f
18157 /***********************************************************************
18158 Building Desired Matrix Rows
18159 ***********************************************************************/
18160
18161 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18162 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18163
18164 static struct glyph_row *
18165 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18166 {
18167 struct frame *f = XFRAME (WINDOW_FRAME (w));
18168 struct buffer *buffer = XBUFFER (w->buffer);
18169 struct buffer *old = current_buffer;
18170 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18171 int arrow_len = SCHARS (overlay_arrow_string);
18172 const unsigned char *arrow_end = arrow_string + arrow_len;
18173 const unsigned char *p;
18174 struct it it;
18175 int multibyte_p;
18176 int n_glyphs_before;
18177
18178 set_buffer_temp (buffer);
18179 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18180 it.glyph_row->used[TEXT_AREA] = 0;
18181 SET_TEXT_POS (it.position, 0, 0);
18182
18183 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18184 p = arrow_string;
18185 while (p < arrow_end)
18186 {
18187 Lisp_Object face, ilisp;
18188
18189 /* Get the next character. */
18190 if (multibyte_p)
18191 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18192 else
18193 {
18194 it.c = it.char_to_display = *p, it.len = 1;
18195 if (! ASCII_CHAR_P (it.c))
18196 it.char_to_display = BYTE8_TO_CHAR (it.c);
18197 }
18198 p += it.len;
18199
18200 /* Get its face. */
18201 ilisp = make_number (p - arrow_string);
18202 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18203 it.face_id = compute_char_face (f, it.char_to_display, face);
18204
18205 /* Compute its width, get its glyphs. */
18206 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18207 SET_TEXT_POS (it.position, -1, -1);
18208 PRODUCE_GLYPHS (&it);
18209
18210 /* If this character doesn't fit any more in the line, we have
18211 to remove some glyphs. */
18212 if (it.current_x > it.last_visible_x)
18213 {
18214 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18215 break;
18216 }
18217 }
18218
18219 set_buffer_temp (old);
18220 return it.glyph_row;
18221 }
18222
18223
18224 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18225 glyphs to insert is determined by produce_special_glyphs. */
18226
18227 static void
18228 insert_left_trunc_glyphs (struct it *it)
18229 {
18230 struct it truncate_it;
18231 struct glyph *from, *end, *to, *toend;
18232
18233 eassert (!FRAME_WINDOW_P (it->f)
18234 || (!it->glyph_row->reversed_p
18235 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18236 || (it->glyph_row->reversed_p
18237 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18238
18239 /* Get the truncation glyphs. */
18240 truncate_it = *it;
18241 truncate_it.current_x = 0;
18242 truncate_it.face_id = DEFAULT_FACE_ID;
18243 truncate_it.glyph_row = &scratch_glyph_row;
18244 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18245 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18246 truncate_it.object = make_number (0);
18247 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18248
18249 /* Overwrite glyphs from IT with truncation glyphs. */
18250 if (!it->glyph_row->reversed_p)
18251 {
18252 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18253
18254 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18255 end = from + tused;
18256 to = it->glyph_row->glyphs[TEXT_AREA];
18257 toend = to + it->glyph_row->used[TEXT_AREA];
18258 if (FRAME_WINDOW_P (it->f))
18259 {
18260 /* On GUI frames, when variable-size fonts are displayed,
18261 the truncation glyphs may need more pixels than the row's
18262 glyphs they overwrite. We overwrite more glyphs to free
18263 enough screen real estate, and enlarge the stretch glyph
18264 on the right (see display_line), if there is one, to
18265 preserve the screen position of the truncation glyphs on
18266 the right. */
18267 int w = 0;
18268 struct glyph *g = to;
18269 short used;
18270
18271 /* The first glyph could be partially visible, in which case
18272 it->glyph_row->x will be negative. But we want the left
18273 truncation glyphs to be aligned at the left margin of the
18274 window, so we override the x coordinate at which the row
18275 will begin. */
18276 it->glyph_row->x = 0;
18277 while (g < toend && w < it->truncation_pixel_width)
18278 {
18279 w += g->pixel_width;
18280 ++g;
18281 }
18282 if (g - to - tused > 0)
18283 {
18284 memmove (to + tused, g, (toend - g) * sizeof(*g));
18285 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18286 }
18287 used = it->glyph_row->used[TEXT_AREA];
18288 if (it->glyph_row->truncated_on_right_p
18289 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18290 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18291 == STRETCH_GLYPH)
18292 {
18293 int extra = w - it->truncation_pixel_width;
18294
18295 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18296 }
18297 }
18298
18299 while (from < end)
18300 *to++ = *from++;
18301
18302 /* There may be padding glyphs left over. Overwrite them too. */
18303 if (!FRAME_WINDOW_P (it->f))
18304 {
18305 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18306 {
18307 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18308 while (from < end)
18309 *to++ = *from++;
18310 }
18311 }
18312
18313 if (to > toend)
18314 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18315 }
18316 else
18317 {
18318 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18319
18320 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18321 that back to front. */
18322 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18323 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18324 toend = it->glyph_row->glyphs[TEXT_AREA];
18325 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18326 if (FRAME_WINDOW_P (it->f))
18327 {
18328 int w = 0;
18329 struct glyph *g = to;
18330
18331 while (g >= toend && w < it->truncation_pixel_width)
18332 {
18333 w += g->pixel_width;
18334 --g;
18335 }
18336 if (to - g - tused > 0)
18337 to = g + tused;
18338 if (it->glyph_row->truncated_on_right_p
18339 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18340 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18341 {
18342 int extra = w - it->truncation_pixel_width;
18343
18344 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18345 }
18346 }
18347
18348 while (from >= end && to >= toend)
18349 *to-- = *from--;
18350 if (!FRAME_WINDOW_P (it->f))
18351 {
18352 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18353 {
18354 from =
18355 truncate_it.glyph_row->glyphs[TEXT_AREA]
18356 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18357 while (from >= end && to >= toend)
18358 *to-- = *from--;
18359 }
18360 }
18361 if (from >= end)
18362 {
18363 /* Need to free some room before prepending additional
18364 glyphs. */
18365 int move_by = from - end + 1;
18366 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18367 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18368
18369 for ( ; g >= g0; g--)
18370 g[move_by] = *g;
18371 while (from >= end)
18372 *to-- = *from--;
18373 it->glyph_row->used[TEXT_AREA] += move_by;
18374 }
18375 }
18376 }
18377
18378 /* Compute the hash code for ROW. */
18379 unsigned
18380 row_hash (struct glyph_row *row)
18381 {
18382 int area, k;
18383 unsigned hashval = 0;
18384
18385 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18386 for (k = 0; k < row->used[area]; ++k)
18387 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18388 + row->glyphs[area][k].u.val
18389 + row->glyphs[area][k].face_id
18390 + row->glyphs[area][k].padding_p
18391 + (row->glyphs[area][k].type << 2));
18392
18393 return hashval;
18394 }
18395
18396 /* Compute the pixel height and width of IT->glyph_row.
18397
18398 Most of the time, ascent and height of a display line will be equal
18399 to the max_ascent and max_height values of the display iterator
18400 structure. This is not the case if
18401
18402 1. We hit ZV without displaying anything. In this case, max_ascent
18403 and max_height will be zero.
18404
18405 2. We have some glyphs that don't contribute to the line height.
18406 (The glyph row flag contributes_to_line_height_p is for future
18407 pixmap extensions).
18408
18409 The first case is easily covered by using default values because in
18410 these cases, the line height does not really matter, except that it
18411 must not be zero. */
18412
18413 static void
18414 compute_line_metrics (struct it *it)
18415 {
18416 struct glyph_row *row = it->glyph_row;
18417
18418 if (FRAME_WINDOW_P (it->f))
18419 {
18420 int i, min_y, max_y;
18421
18422 /* The line may consist of one space only, that was added to
18423 place the cursor on it. If so, the row's height hasn't been
18424 computed yet. */
18425 if (row->height == 0)
18426 {
18427 if (it->max_ascent + it->max_descent == 0)
18428 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18429 row->ascent = it->max_ascent;
18430 row->height = it->max_ascent + it->max_descent;
18431 row->phys_ascent = it->max_phys_ascent;
18432 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18433 row->extra_line_spacing = it->max_extra_line_spacing;
18434 }
18435
18436 /* Compute the width of this line. */
18437 row->pixel_width = row->x;
18438 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18439 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18440
18441 eassert (row->pixel_width >= 0);
18442 eassert (row->ascent >= 0 && row->height > 0);
18443
18444 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18445 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18446
18447 /* If first line's physical ascent is larger than its logical
18448 ascent, use the physical ascent, and make the row taller.
18449 This makes accented characters fully visible. */
18450 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18451 && row->phys_ascent > row->ascent)
18452 {
18453 row->height += row->phys_ascent - row->ascent;
18454 row->ascent = row->phys_ascent;
18455 }
18456
18457 /* Compute how much of the line is visible. */
18458 row->visible_height = row->height;
18459
18460 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18461 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18462
18463 if (row->y < min_y)
18464 row->visible_height -= min_y - row->y;
18465 if (row->y + row->height > max_y)
18466 row->visible_height -= row->y + row->height - max_y;
18467 }
18468 else
18469 {
18470 row->pixel_width = row->used[TEXT_AREA];
18471 if (row->continued_p)
18472 row->pixel_width -= it->continuation_pixel_width;
18473 else if (row->truncated_on_right_p)
18474 row->pixel_width -= it->truncation_pixel_width;
18475 row->ascent = row->phys_ascent = 0;
18476 row->height = row->phys_height = row->visible_height = 1;
18477 row->extra_line_spacing = 0;
18478 }
18479
18480 /* Compute a hash code for this row. */
18481 row->hash = row_hash (row);
18482
18483 it->max_ascent = it->max_descent = 0;
18484 it->max_phys_ascent = it->max_phys_descent = 0;
18485 }
18486
18487
18488 /* Append one space to the glyph row of iterator IT if doing a
18489 window-based redisplay. The space has the same face as
18490 IT->face_id. Value is non-zero if a space was added.
18491
18492 This function is called to make sure that there is always one glyph
18493 at the end of a glyph row that the cursor can be set on under
18494 window-systems. (If there weren't such a glyph we would not know
18495 how wide and tall a box cursor should be displayed).
18496
18497 At the same time this space let's a nicely handle clearing to the
18498 end of the line if the row ends in italic text. */
18499
18500 static int
18501 append_space_for_newline (struct it *it, int default_face_p)
18502 {
18503 if (FRAME_WINDOW_P (it->f))
18504 {
18505 int n = it->glyph_row->used[TEXT_AREA];
18506
18507 if (it->glyph_row->glyphs[TEXT_AREA] + n
18508 < it->glyph_row->glyphs[1 + TEXT_AREA])
18509 {
18510 /* Save some values that must not be changed.
18511 Must save IT->c and IT->len because otherwise
18512 ITERATOR_AT_END_P wouldn't work anymore after
18513 append_space_for_newline has been called. */
18514 enum display_element_type saved_what = it->what;
18515 int saved_c = it->c, saved_len = it->len;
18516 int saved_char_to_display = it->char_to_display;
18517 int saved_x = it->current_x;
18518 int saved_face_id = it->face_id;
18519 struct text_pos saved_pos;
18520 Lisp_Object saved_object;
18521 struct face *face;
18522
18523 saved_object = it->object;
18524 saved_pos = it->position;
18525
18526 it->what = IT_CHARACTER;
18527 memset (&it->position, 0, sizeof it->position);
18528 it->object = make_number (0);
18529 it->c = it->char_to_display = ' ';
18530 it->len = 1;
18531
18532 /* If the default face was remapped, be sure to use the
18533 remapped face for the appended newline. */
18534 if (default_face_p)
18535 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18536 else if (it->face_before_selective_p)
18537 it->face_id = it->saved_face_id;
18538 face = FACE_FROM_ID (it->f, it->face_id);
18539 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18540
18541 PRODUCE_GLYPHS (it);
18542
18543 it->override_ascent = -1;
18544 it->constrain_row_ascent_descent_p = 0;
18545 it->current_x = saved_x;
18546 it->object = saved_object;
18547 it->position = saved_pos;
18548 it->what = saved_what;
18549 it->face_id = saved_face_id;
18550 it->len = saved_len;
18551 it->c = saved_c;
18552 it->char_to_display = saved_char_to_display;
18553 return 1;
18554 }
18555 }
18556
18557 return 0;
18558 }
18559
18560
18561 /* Extend the face of the last glyph in the text area of IT->glyph_row
18562 to the end of the display line. Called from display_line. If the
18563 glyph row is empty, add a space glyph to it so that we know the
18564 face to draw. Set the glyph row flag fill_line_p. If the glyph
18565 row is R2L, prepend a stretch glyph to cover the empty space to the
18566 left of the leftmost glyph. */
18567
18568 static void
18569 extend_face_to_end_of_line (struct it *it)
18570 {
18571 struct face *face, *default_face;
18572 struct frame *f = it->f;
18573
18574 /* If line is already filled, do nothing. Non window-system frames
18575 get a grace of one more ``pixel'' because their characters are
18576 1-``pixel'' wide, so they hit the equality too early. This grace
18577 is needed only for R2L rows that are not continued, to produce
18578 one extra blank where we could display the cursor. */
18579 if (it->current_x >= it->last_visible_x
18580 + (!FRAME_WINDOW_P (f)
18581 && it->glyph_row->reversed_p
18582 && !it->glyph_row->continued_p))
18583 return;
18584
18585 /* The default face, possibly remapped. */
18586 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18587
18588 /* Face extension extends the background and box of IT->face_id
18589 to the end of the line. If the background equals the background
18590 of the frame, we don't have to do anything. */
18591 if (it->face_before_selective_p)
18592 face = FACE_FROM_ID (f, it->saved_face_id);
18593 else
18594 face = FACE_FROM_ID (f, it->face_id);
18595
18596 if (FRAME_WINDOW_P (f)
18597 && it->glyph_row->displays_text_p
18598 && face->box == FACE_NO_BOX
18599 && face->background == FRAME_BACKGROUND_PIXEL (f)
18600 && !face->stipple
18601 && !it->glyph_row->reversed_p)
18602 return;
18603
18604 /* Set the glyph row flag indicating that the face of the last glyph
18605 in the text area has to be drawn to the end of the text area. */
18606 it->glyph_row->fill_line_p = 1;
18607
18608 /* If current character of IT is not ASCII, make sure we have the
18609 ASCII face. This will be automatically undone the next time
18610 get_next_display_element returns a multibyte character. Note
18611 that the character will always be single byte in unibyte
18612 text. */
18613 if (!ASCII_CHAR_P (it->c))
18614 {
18615 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18616 }
18617
18618 if (FRAME_WINDOW_P (f))
18619 {
18620 /* If the row is empty, add a space with the current face of IT,
18621 so that we know which face to draw. */
18622 if (it->glyph_row->used[TEXT_AREA] == 0)
18623 {
18624 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18625 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18626 it->glyph_row->used[TEXT_AREA] = 1;
18627 }
18628 #ifdef HAVE_WINDOW_SYSTEM
18629 if (it->glyph_row->reversed_p)
18630 {
18631 /* Prepend a stretch glyph to the row, such that the
18632 rightmost glyph will be drawn flushed all the way to the
18633 right margin of the window. The stretch glyph that will
18634 occupy the empty space, if any, to the left of the
18635 glyphs. */
18636 struct font *font = face->font ? face->font : FRAME_FONT (f);
18637 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18638 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18639 struct glyph *g;
18640 int row_width, stretch_ascent, stretch_width;
18641 struct text_pos saved_pos;
18642 int saved_face_id, saved_avoid_cursor;
18643
18644 for (row_width = 0, g = row_start; g < row_end; g++)
18645 row_width += g->pixel_width;
18646 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18647 if (stretch_width > 0)
18648 {
18649 stretch_ascent =
18650 (((it->ascent + it->descent)
18651 * FONT_BASE (font)) / FONT_HEIGHT (font));
18652 saved_pos = it->position;
18653 memset (&it->position, 0, sizeof it->position);
18654 saved_avoid_cursor = it->avoid_cursor_p;
18655 it->avoid_cursor_p = 1;
18656 saved_face_id = it->face_id;
18657 /* The last row's stretch glyph should get the default
18658 face, to avoid painting the rest of the window with
18659 the region face, if the region ends at ZV. */
18660 if (it->glyph_row->ends_at_zv_p)
18661 it->face_id = default_face->id;
18662 else
18663 it->face_id = face->id;
18664 append_stretch_glyph (it, make_number (0), stretch_width,
18665 it->ascent + it->descent, stretch_ascent);
18666 it->position = saved_pos;
18667 it->avoid_cursor_p = saved_avoid_cursor;
18668 it->face_id = saved_face_id;
18669 }
18670 }
18671 #endif /* HAVE_WINDOW_SYSTEM */
18672 }
18673 else
18674 {
18675 /* Save some values that must not be changed. */
18676 int saved_x = it->current_x;
18677 struct text_pos saved_pos;
18678 Lisp_Object saved_object;
18679 enum display_element_type saved_what = it->what;
18680 int saved_face_id = it->face_id;
18681
18682 saved_object = it->object;
18683 saved_pos = it->position;
18684
18685 it->what = IT_CHARACTER;
18686 memset (&it->position, 0, sizeof it->position);
18687 it->object = make_number (0);
18688 it->c = it->char_to_display = ' ';
18689 it->len = 1;
18690 /* The last row's blank glyphs should get the default face, to
18691 avoid painting the rest of the window with the region face,
18692 if the region ends at ZV. */
18693 if (it->glyph_row->ends_at_zv_p)
18694 it->face_id = default_face->id;
18695 else
18696 it->face_id = face->id;
18697
18698 PRODUCE_GLYPHS (it);
18699
18700 while (it->current_x <= it->last_visible_x)
18701 PRODUCE_GLYPHS (it);
18702
18703 /* Don't count these blanks really. It would let us insert a left
18704 truncation glyph below and make us set the cursor on them, maybe. */
18705 it->current_x = saved_x;
18706 it->object = saved_object;
18707 it->position = saved_pos;
18708 it->what = saved_what;
18709 it->face_id = saved_face_id;
18710 }
18711 }
18712
18713
18714 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18715 trailing whitespace. */
18716
18717 static int
18718 trailing_whitespace_p (ptrdiff_t charpos)
18719 {
18720 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18721 int c = 0;
18722
18723 while (bytepos < ZV_BYTE
18724 && (c = FETCH_CHAR (bytepos),
18725 c == ' ' || c == '\t'))
18726 ++bytepos;
18727
18728 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18729 {
18730 if (bytepos != PT_BYTE)
18731 return 1;
18732 }
18733 return 0;
18734 }
18735
18736
18737 /* Highlight trailing whitespace, if any, in ROW. */
18738
18739 static void
18740 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18741 {
18742 int used = row->used[TEXT_AREA];
18743
18744 if (used)
18745 {
18746 struct glyph *start = row->glyphs[TEXT_AREA];
18747 struct glyph *glyph = start + used - 1;
18748
18749 if (row->reversed_p)
18750 {
18751 /* Right-to-left rows need to be processed in the opposite
18752 direction, so swap the edge pointers. */
18753 glyph = start;
18754 start = row->glyphs[TEXT_AREA] + used - 1;
18755 }
18756
18757 /* Skip over glyphs inserted to display the cursor at the
18758 end of a line, for extending the face of the last glyph
18759 to the end of the line on terminals, and for truncation
18760 and continuation glyphs. */
18761 if (!row->reversed_p)
18762 {
18763 while (glyph >= start
18764 && glyph->type == CHAR_GLYPH
18765 && INTEGERP (glyph->object))
18766 --glyph;
18767 }
18768 else
18769 {
18770 while (glyph <= start
18771 && glyph->type == CHAR_GLYPH
18772 && INTEGERP (glyph->object))
18773 ++glyph;
18774 }
18775
18776 /* If last glyph is a space or stretch, and it's trailing
18777 whitespace, set the face of all trailing whitespace glyphs in
18778 IT->glyph_row to `trailing-whitespace'. */
18779 if ((row->reversed_p ? glyph <= start : glyph >= start)
18780 && BUFFERP (glyph->object)
18781 && (glyph->type == STRETCH_GLYPH
18782 || (glyph->type == CHAR_GLYPH
18783 && glyph->u.ch == ' '))
18784 && trailing_whitespace_p (glyph->charpos))
18785 {
18786 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18787 if (face_id < 0)
18788 return;
18789
18790 if (!row->reversed_p)
18791 {
18792 while (glyph >= start
18793 && BUFFERP (glyph->object)
18794 && (glyph->type == STRETCH_GLYPH
18795 || (glyph->type == CHAR_GLYPH
18796 && glyph->u.ch == ' ')))
18797 (glyph--)->face_id = face_id;
18798 }
18799 else
18800 {
18801 while (glyph <= start
18802 && BUFFERP (glyph->object)
18803 && (glyph->type == STRETCH_GLYPH
18804 || (glyph->type == CHAR_GLYPH
18805 && glyph->u.ch == ' ')))
18806 (glyph++)->face_id = face_id;
18807 }
18808 }
18809 }
18810 }
18811
18812
18813 /* Value is non-zero if glyph row ROW should be
18814 used to hold the cursor. */
18815
18816 static int
18817 cursor_row_p (struct glyph_row *row)
18818 {
18819 int result = 1;
18820
18821 if (PT == CHARPOS (row->end.pos)
18822 || PT == MATRIX_ROW_END_CHARPOS (row))
18823 {
18824 /* Suppose the row ends on a string.
18825 Unless the row is continued, that means it ends on a newline
18826 in the string. If it's anything other than a display string
18827 (e.g., a before-string from an overlay), we don't want the
18828 cursor there. (This heuristic seems to give the optimal
18829 behavior for the various types of multi-line strings.)
18830 One exception: if the string has `cursor' property on one of
18831 its characters, we _do_ want the cursor there. */
18832 if (CHARPOS (row->end.string_pos) >= 0)
18833 {
18834 if (row->continued_p)
18835 result = 1;
18836 else
18837 {
18838 /* Check for `display' property. */
18839 struct glyph *beg = row->glyphs[TEXT_AREA];
18840 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18841 struct glyph *glyph;
18842
18843 result = 0;
18844 for (glyph = end; glyph >= beg; --glyph)
18845 if (STRINGP (glyph->object))
18846 {
18847 Lisp_Object prop
18848 = Fget_char_property (make_number (PT),
18849 Qdisplay, Qnil);
18850 result =
18851 (!NILP (prop)
18852 && display_prop_string_p (prop, glyph->object));
18853 /* If there's a `cursor' property on one of the
18854 string's characters, this row is a cursor row,
18855 even though this is not a display string. */
18856 if (!result)
18857 {
18858 Lisp_Object s = glyph->object;
18859
18860 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18861 {
18862 ptrdiff_t gpos = glyph->charpos;
18863
18864 if (!NILP (Fget_char_property (make_number (gpos),
18865 Qcursor, s)))
18866 {
18867 result = 1;
18868 break;
18869 }
18870 }
18871 }
18872 break;
18873 }
18874 }
18875 }
18876 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18877 {
18878 /* If the row ends in middle of a real character,
18879 and the line is continued, we want the cursor here.
18880 That's because CHARPOS (ROW->end.pos) would equal
18881 PT if PT is before the character. */
18882 if (!row->ends_in_ellipsis_p)
18883 result = row->continued_p;
18884 else
18885 /* If the row ends in an ellipsis, then
18886 CHARPOS (ROW->end.pos) will equal point after the
18887 invisible text. We want that position to be displayed
18888 after the ellipsis. */
18889 result = 0;
18890 }
18891 /* If the row ends at ZV, display the cursor at the end of that
18892 row instead of at the start of the row below. */
18893 else if (row->ends_at_zv_p)
18894 result = 1;
18895 else
18896 result = 0;
18897 }
18898
18899 return result;
18900 }
18901
18902 \f
18903
18904 /* Push the property PROP so that it will be rendered at the current
18905 position in IT. Return 1 if PROP was successfully pushed, 0
18906 otherwise. Called from handle_line_prefix to handle the
18907 `line-prefix' and `wrap-prefix' properties. */
18908
18909 static int
18910 push_prefix_prop (struct it *it, Lisp_Object prop)
18911 {
18912 struct text_pos pos =
18913 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18914
18915 eassert (it->method == GET_FROM_BUFFER
18916 || it->method == GET_FROM_DISPLAY_VECTOR
18917 || it->method == GET_FROM_STRING);
18918
18919 /* We need to save the current buffer/string position, so it will be
18920 restored by pop_it, because iterate_out_of_display_property
18921 depends on that being set correctly, but some situations leave
18922 it->position not yet set when this function is called. */
18923 push_it (it, &pos);
18924
18925 if (STRINGP (prop))
18926 {
18927 if (SCHARS (prop) == 0)
18928 {
18929 pop_it (it);
18930 return 0;
18931 }
18932
18933 it->string = prop;
18934 it->string_from_prefix_prop_p = 1;
18935 it->multibyte_p = STRING_MULTIBYTE (it->string);
18936 it->current.overlay_string_index = -1;
18937 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18938 it->end_charpos = it->string_nchars = SCHARS (it->string);
18939 it->method = GET_FROM_STRING;
18940 it->stop_charpos = 0;
18941 it->prev_stop = 0;
18942 it->base_level_stop = 0;
18943
18944 /* Force paragraph direction to be that of the parent
18945 buffer/string. */
18946 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18947 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18948 else
18949 it->paragraph_embedding = L2R;
18950
18951 /* Set up the bidi iterator for this display string. */
18952 if (it->bidi_p)
18953 {
18954 it->bidi_it.string.lstring = it->string;
18955 it->bidi_it.string.s = NULL;
18956 it->bidi_it.string.schars = it->end_charpos;
18957 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18958 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18959 it->bidi_it.string.unibyte = !it->multibyte_p;
18960 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18961 }
18962 }
18963 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18964 {
18965 it->method = GET_FROM_STRETCH;
18966 it->object = prop;
18967 }
18968 #ifdef HAVE_WINDOW_SYSTEM
18969 else if (IMAGEP (prop))
18970 {
18971 it->what = IT_IMAGE;
18972 it->image_id = lookup_image (it->f, prop);
18973 it->method = GET_FROM_IMAGE;
18974 }
18975 #endif /* HAVE_WINDOW_SYSTEM */
18976 else
18977 {
18978 pop_it (it); /* bogus display property, give up */
18979 return 0;
18980 }
18981
18982 return 1;
18983 }
18984
18985 /* Return the character-property PROP at the current position in IT. */
18986
18987 static Lisp_Object
18988 get_it_property (struct it *it, Lisp_Object prop)
18989 {
18990 Lisp_Object position;
18991
18992 if (STRINGP (it->object))
18993 position = make_number (IT_STRING_CHARPOS (*it));
18994 else if (BUFFERP (it->object))
18995 position = make_number (IT_CHARPOS (*it));
18996 else
18997 return Qnil;
18998
18999 return Fget_char_property (position, prop, it->object);
19000 }
19001
19002 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19003
19004 static void
19005 handle_line_prefix (struct it *it)
19006 {
19007 Lisp_Object prefix;
19008
19009 if (it->continuation_lines_width > 0)
19010 {
19011 prefix = get_it_property (it, Qwrap_prefix);
19012 if (NILP (prefix))
19013 prefix = Vwrap_prefix;
19014 }
19015 else
19016 {
19017 prefix = get_it_property (it, Qline_prefix);
19018 if (NILP (prefix))
19019 prefix = Vline_prefix;
19020 }
19021 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19022 {
19023 /* If the prefix is wider than the window, and we try to wrap
19024 it, it would acquire its own wrap prefix, and so on till the
19025 iterator stack overflows. So, don't wrap the prefix. */
19026 it->line_wrap = TRUNCATE;
19027 it->avoid_cursor_p = 1;
19028 }
19029 }
19030
19031 \f
19032
19033 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19034 only for R2L lines from display_line and display_string, when they
19035 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19036 the line/string needs to be continued on the next glyph row. */
19037 static void
19038 unproduce_glyphs (struct it *it, int n)
19039 {
19040 struct glyph *glyph, *end;
19041
19042 eassert (it->glyph_row);
19043 eassert (it->glyph_row->reversed_p);
19044 eassert (it->area == TEXT_AREA);
19045 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19046
19047 if (n > it->glyph_row->used[TEXT_AREA])
19048 n = it->glyph_row->used[TEXT_AREA];
19049 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19050 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19051 for ( ; glyph < end; glyph++)
19052 glyph[-n] = *glyph;
19053 }
19054
19055 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19056 and ROW->maxpos. */
19057 static void
19058 find_row_edges (struct it *it, struct glyph_row *row,
19059 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19060 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19061 {
19062 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19063 lines' rows is implemented for bidi-reordered rows. */
19064
19065 /* ROW->minpos is the value of min_pos, the minimal buffer position
19066 we have in ROW, or ROW->start.pos if that is smaller. */
19067 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19068 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19069 else
19070 /* We didn't find buffer positions smaller than ROW->start, or
19071 didn't find _any_ valid buffer positions in any of the glyphs,
19072 so we must trust the iterator's computed positions. */
19073 row->minpos = row->start.pos;
19074 if (max_pos <= 0)
19075 {
19076 max_pos = CHARPOS (it->current.pos);
19077 max_bpos = BYTEPOS (it->current.pos);
19078 }
19079
19080 /* Here are the various use-cases for ending the row, and the
19081 corresponding values for ROW->maxpos:
19082
19083 Line ends in a newline from buffer eol_pos + 1
19084 Line is continued from buffer max_pos + 1
19085 Line is truncated on right it->current.pos
19086 Line ends in a newline from string max_pos + 1(*)
19087 (*) + 1 only when line ends in a forward scan
19088 Line is continued from string max_pos
19089 Line is continued from display vector max_pos
19090 Line is entirely from a string min_pos == max_pos
19091 Line is entirely from a display vector min_pos == max_pos
19092 Line that ends at ZV ZV
19093
19094 If you discover other use-cases, please add them here as
19095 appropriate. */
19096 if (row->ends_at_zv_p)
19097 row->maxpos = it->current.pos;
19098 else if (row->used[TEXT_AREA])
19099 {
19100 int seen_this_string = 0;
19101 struct glyph_row *r1 = row - 1;
19102
19103 /* Did we see the same display string on the previous row? */
19104 if (STRINGP (it->object)
19105 /* this is not the first row */
19106 && row > it->w->desired_matrix->rows
19107 /* previous row is not the header line */
19108 && !r1->mode_line_p
19109 /* previous row also ends in a newline from a string */
19110 && r1->ends_in_newline_from_string_p)
19111 {
19112 struct glyph *start, *end;
19113
19114 /* Search for the last glyph of the previous row that came
19115 from buffer or string. Depending on whether the row is
19116 L2R or R2L, we need to process it front to back or the
19117 other way round. */
19118 if (!r1->reversed_p)
19119 {
19120 start = r1->glyphs[TEXT_AREA];
19121 end = start + r1->used[TEXT_AREA];
19122 /* Glyphs inserted by redisplay have an integer (zero)
19123 as their object. */
19124 while (end > start
19125 && INTEGERP ((end - 1)->object)
19126 && (end - 1)->charpos <= 0)
19127 --end;
19128 if (end > start)
19129 {
19130 if (EQ ((end - 1)->object, it->object))
19131 seen_this_string = 1;
19132 }
19133 else
19134 /* If all the glyphs of the previous row were inserted
19135 by redisplay, it means the previous row was
19136 produced from a single newline, which is only
19137 possible if that newline came from the same string
19138 as the one which produced this ROW. */
19139 seen_this_string = 1;
19140 }
19141 else
19142 {
19143 end = r1->glyphs[TEXT_AREA] - 1;
19144 start = end + r1->used[TEXT_AREA];
19145 while (end < start
19146 && INTEGERP ((end + 1)->object)
19147 && (end + 1)->charpos <= 0)
19148 ++end;
19149 if (end < start)
19150 {
19151 if (EQ ((end + 1)->object, it->object))
19152 seen_this_string = 1;
19153 }
19154 else
19155 seen_this_string = 1;
19156 }
19157 }
19158 /* Take note of each display string that covers a newline only
19159 once, the first time we see it. This is for when a display
19160 string includes more than one newline in it. */
19161 if (row->ends_in_newline_from_string_p && !seen_this_string)
19162 {
19163 /* If we were scanning the buffer forward when we displayed
19164 the string, we want to account for at least one buffer
19165 position that belongs to this row (position covered by
19166 the display string), so that cursor positioning will
19167 consider this row as a candidate when point is at the end
19168 of the visual line represented by this row. This is not
19169 required when scanning back, because max_pos will already
19170 have a much larger value. */
19171 if (CHARPOS (row->end.pos) > max_pos)
19172 INC_BOTH (max_pos, max_bpos);
19173 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19174 }
19175 else if (CHARPOS (it->eol_pos) > 0)
19176 SET_TEXT_POS (row->maxpos,
19177 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19178 else if (row->continued_p)
19179 {
19180 /* If max_pos is different from IT's current position, it
19181 means IT->method does not belong to the display element
19182 at max_pos. However, it also means that the display
19183 element at max_pos was displayed in its entirety on this
19184 line, which is equivalent to saying that the next line
19185 starts at the next buffer position. */
19186 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19187 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19188 else
19189 {
19190 INC_BOTH (max_pos, max_bpos);
19191 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19192 }
19193 }
19194 else if (row->truncated_on_right_p)
19195 /* display_line already called reseat_at_next_visible_line_start,
19196 which puts the iterator at the beginning of the next line, in
19197 the logical order. */
19198 row->maxpos = it->current.pos;
19199 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19200 /* A line that is entirely from a string/image/stretch... */
19201 row->maxpos = row->minpos;
19202 else
19203 abort ();
19204 }
19205 else
19206 row->maxpos = it->current.pos;
19207 }
19208
19209 /* Construct the glyph row IT->glyph_row in the desired matrix of
19210 IT->w from text at the current position of IT. See dispextern.h
19211 for an overview of struct it. Value is non-zero if
19212 IT->glyph_row displays text, as opposed to a line displaying ZV
19213 only. */
19214
19215 static int
19216 display_line (struct it *it)
19217 {
19218 struct glyph_row *row = it->glyph_row;
19219 Lisp_Object overlay_arrow_string;
19220 struct it wrap_it;
19221 void *wrap_data = NULL;
19222 int may_wrap = 0, wrap_x IF_LINT (= 0);
19223 int wrap_row_used = -1;
19224 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19225 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19226 int wrap_row_extra_line_spacing IF_LINT (= 0);
19227 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19228 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19229 int cvpos;
19230 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19231 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19232
19233 /* We always start displaying at hpos zero even if hscrolled. */
19234 eassert (it->hpos == 0 && it->current_x == 0);
19235
19236 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19237 >= it->w->desired_matrix->nrows)
19238 {
19239 it->w->nrows_scale_factor++;
19240 fonts_changed_p = 1;
19241 return 0;
19242 }
19243
19244 /* Is IT->w showing the region? */
19245 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19246
19247 /* Clear the result glyph row and enable it. */
19248 prepare_desired_row (row);
19249
19250 row->y = it->current_y;
19251 row->start = it->start;
19252 row->continuation_lines_width = it->continuation_lines_width;
19253 row->displays_text_p = 1;
19254 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19255 it->starts_in_middle_of_char_p = 0;
19256
19257 /* Arrange the overlays nicely for our purposes. Usually, we call
19258 display_line on only one line at a time, in which case this
19259 can't really hurt too much, or we call it on lines which appear
19260 one after another in the buffer, in which case all calls to
19261 recenter_overlay_lists but the first will be pretty cheap. */
19262 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19263
19264 /* Move over display elements that are not visible because we are
19265 hscrolled. This may stop at an x-position < IT->first_visible_x
19266 if the first glyph is partially visible or if we hit a line end. */
19267 if (it->current_x < it->first_visible_x)
19268 {
19269 enum move_it_result move_result;
19270
19271 this_line_min_pos = row->start.pos;
19272 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19273 MOVE_TO_POS | MOVE_TO_X);
19274 /* If we are under a large hscroll, move_it_in_display_line_to
19275 could hit the end of the line without reaching
19276 it->first_visible_x. Pretend that we did reach it. This is
19277 especially important on a TTY, where we will call
19278 extend_face_to_end_of_line, which needs to know how many
19279 blank glyphs to produce. */
19280 if (it->current_x < it->first_visible_x
19281 && (move_result == MOVE_NEWLINE_OR_CR
19282 || move_result == MOVE_POS_MATCH_OR_ZV))
19283 it->current_x = it->first_visible_x;
19284
19285 /* Record the smallest positions seen while we moved over
19286 display elements that are not visible. This is needed by
19287 redisplay_internal for optimizing the case where the cursor
19288 stays inside the same line. The rest of this function only
19289 considers positions that are actually displayed, so
19290 RECORD_MAX_MIN_POS will not otherwise record positions that
19291 are hscrolled to the left of the left edge of the window. */
19292 min_pos = CHARPOS (this_line_min_pos);
19293 min_bpos = BYTEPOS (this_line_min_pos);
19294 }
19295 else
19296 {
19297 /* We only do this when not calling `move_it_in_display_line_to'
19298 above, because move_it_in_display_line_to calls
19299 handle_line_prefix itself. */
19300 handle_line_prefix (it);
19301 }
19302
19303 /* Get the initial row height. This is either the height of the
19304 text hscrolled, if there is any, or zero. */
19305 row->ascent = it->max_ascent;
19306 row->height = it->max_ascent + it->max_descent;
19307 row->phys_ascent = it->max_phys_ascent;
19308 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19309 row->extra_line_spacing = it->max_extra_line_spacing;
19310
19311 /* Utility macro to record max and min buffer positions seen until now. */
19312 #define RECORD_MAX_MIN_POS(IT) \
19313 do \
19314 { \
19315 int composition_p = !STRINGP ((IT)->string) \
19316 && ((IT)->what == IT_COMPOSITION); \
19317 ptrdiff_t current_pos = \
19318 composition_p ? (IT)->cmp_it.charpos \
19319 : IT_CHARPOS (*(IT)); \
19320 ptrdiff_t current_bpos = \
19321 composition_p ? CHAR_TO_BYTE (current_pos) \
19322 : IT_BYTEPOS (*(IT)); \
19323 if (current_pos < min_pos) \
19324 { \
19325 min_pos = current_pos; \
19326 min_bpos = current_bpos; \
19327 } \
19328 if (IT_CHARPOS (*it) > max_pos) \
19329 { \
19330 max_pos = IT_CHARPOS (*it); \
19331 max_bpos = IT_BYTEPOS (*it); \
19332 } \
19333 } \
19334 while (0)
19335
19336 /* Loop generating characters. The loop is left with IT on the next
19337 character to display. */
19338 while (1)
19339 {
19340 int n_glyphs_before, hpos_before, x_before;
19341 int x, nglyphs;
19342 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19343
19344 /* Retrieve the next thing to display. Value is zero if end of
19345 buffer reached. */
19346 if (!get_next_display_element (it))
19347 {
19348 /* Maybe add a space at the end of this line that is used to
19349 display the cursor there under X. Set the charpos of the
19350 first glyph of blank lines not corresponding to any text
19351 to -1. */
19352 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19353 row->exact_window_width_line_p = 1;
19354 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19355 || row->used[TEXT_AREA] == 0)
19356 {
19357 row->glyphs[TEXT_AREA]->charpos = -1;
19358 row->displays_text_p = 0;
19359
19360 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19361 && (!MINI_WINDOW_P (it->w)
19362 || (minibuf_level && EQ (it->window, minibuf_window))))
19363 row->indicate_empty_line_p = 1;
19364 }
19365
19366 it->continuation_lines_width = 0;
19367 row->ends_at_zv_p = 1;
19368 /* A row that displays right-to-left text must always have
19369 its last face extended all the way to the end of line,
19370 even if this row ends in ZV, because we still write to
19371 the screen left to right. We also need to extend the
19372 last face if the default face is remapped to some
19373 different face, otherwise the functions that clear
19374 portions of the screen will clear with the default face's
19375 background color. */
19376 if (row->reversed_p
19377 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19378 extend_face_to_end_of_line (it);
19379 break;
19380 }
19381
19382 /* Now, get the metrics of what we want to display. This also
19383 generates glyphs in `row' (which is IT->glyph_row). */
19384 n_glyphs_before = row->used[TEXT_AREA];
19385 x = it->current_x;
19386
19387 /* Remember the line height so far in case the next element doesn't
19388 fit on the line. */
19389 if (it->line_wrap != TRUNCATE)
19390 {
19391 ascent = it->max_ascent;
19392 descent = it->max_descent;
19393 phys_ascent = it->max_phys_ascent;
19394 phys_descent = it->max_phys_descent;
19395
19396 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19397 {
19398 if (IT_DISPLAYING_WHITESPACE (it))
19399 may_wrap = 1;
19400 else if (may_wrap)
19401 {
19402 SAVE_IT (wrap_it, *it, wrap_data);
19403 wrap_x = x;
19404 wrap_row_used = row->used[TEXT_AREA];
19405 wrap_row_ascent = row->ascent;
19406 wrap_row_height = row->height;
19407 wrap_row_phys_ascent = row->phys_ascent;
19408 wrap_row_phys_height = row->phys_height;
19409 wrap_row_extra_line_spacing = row->extra_line_spacing;
19410 wrap_row_min_pos = min_pos;
19411 wrap_row_min_bpos = min_bpos;
19412 wrap_row_max_pos = max_pos;
19413 wrap_row_max_bpos = max_bpos;
19414 may_wrap = 0;
19415 }
19416 }
19417 }
19418
19419 PRODUCE_GLYPHS (it);
19420
19421 /* If this display element was in marginal areas, continue with
19422 the next one. */
19423 if (it->area != TEXT_AREA)
19424 {
19425 row->ascent = max (row->ascent, it->max_ascent);
19426 row->height = max (row->height, it->max_ascent + it->max_descent);
19427 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19428 row->phys_height = max (row->phys_height,
19429 it->max_phys_ascent + it->max_phys_descent);
19430 row->extra_line_spacing = max (row->extra_line_spacing,
19431 it->max_extra_line_spacing);
19432 set_iterator_to_next (it, 1);
19433 continue;
19434 }
19435
19436 /* Does the display element fit on the line? If we truncate
19437 lines, we should draw past the right edge of the window. If
19438 we don't truncate, we want to stop so that we can display the
19439 continuation glyph before the right margin. If lines are
19440 continued, there are two possible strategies for characters
19441 resulting in more than 1 glyph (e.g. tabs): Display as many
19442 glyphs as possible in this line and leave the rest for the
19443 continuation line, or display the whole element in the next
19444 line. Original redisplay did the former, so we do it also. */
19445 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19446 hpos_before = it->hpos;
19447 x_before = x;
19448
19449 if (/* Not a newline. */
19450 nglyphs > 0
19451 /* Glyphs produced fit entirely in the line. */
19452 && it->current_x < it->last_visible_x)
19453 {
19454 it->hpos += nglyphs;
19455 row->ascent = max (row->ascent, it->max_ascent);
19456 row->height = max (row->height, it->max_ascent + it->max_descent);
19457 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19458 row->phys_height = max (row->phys_height,
19459 it->max_phys_ascent + it->max_phys_descent);
19460 row->extra_line_spacing = max (row->extra_line_spacing,
19461 it->max_extra_line_spacing);
19462 if (it->current_x - it->pixel_width < it->first_visible_x)
19463 row->x = x - it->first_visible_x;
19464 /* Record the maximum and minimum buffer positions seen so
19465 far in glyphs that will be displayed by this row. */
19466 if (it->bidi_p)
19467 RECORD_MAX_MIN_POS (it);
19468 }
19469 else
19470 {
19471 int i, new_x;
19472 struct glyph *glyph;
19473
19474 for (i = 0; i < nglyphs; ++i, x = new_x)
19475 {
19476 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19477 new_x = x + glyph->pixel_width;
19478
19479 if (/* Lines are continued. */
19480 it->line_wrap != TRUNCATE
19481 && (/* Glyph doesn't fit on the line. */
19482 new_x > it->last_visible_x
19483 /* Or it fits exactly on a window system frame. */
19484 || (new_x == it->last_visible_x
19485 && FRAME_WINDOW_P (it->f)
19486 && (row->reversed_p
19487 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19488 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19489 {
19490 /* End of a continued line. */
19491
19492 if (it->hpos == 0
19493 || (new_x == it->last_visible_x
19494 && FRAME_WINDOW_P (it->f)
19495 && (row->reversed_p
19496 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19497 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19498 {
19499 /* Current glyph is the only one on the line or
19500 fits exactly on the line. We must continue
19501 the line because we can't draw the cursor
19502 after the glyph. */
19503 row->continued_p = 1;
19504 it->current_x = new_x;
19505 it->continuation_lines_width += new_x;
19506 ++it->hpos;
19507 if (i == nglyphs - 1)
19508 {
19509 /* If line-wrap is on, check if a previous
19510 wrap point was found. */
19511 if (wrap_row_used > 0
19512 /* Even if there is a previous wrap
19513 point, continue the line here as
19514 usual, if (i) the previous character
19515 was a space or tab AND (ii) the
19516 current character is not. */
19517 && (!may_wrap
19518 || IT_DISPLAYING_WHITESPACE (it)))
19519 goto back_to_wrap;
19520
19521 /* Record the maximum and minimum buffer
19522 positions seen so far in glyphs that will be
19523 displayed by this row. */
19524 if (it->bidi_p)
19525 RECORD_MAX_MIN_POS (it);
19526 set_iterator_to_next (it, 1);
19527 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19528 {
19529 if (!get_next_display_element (it))
19530 {
19531 row->exact_window_width_line_p = 1;
19532 it->continuation_lines_width = 0;
19533 row->continued_p = 0;
19534 row->ends_at_zv_p = 1;
19535 }
19536 else if (ITERATOR_AT_END_OF_LINE_P (it))
19537 {
19538 row->continued_p = 0;
19539 row->exact_window_width_line_p = 1;
19540 }
19541 }
19542 }
19543 else if (it->bidi_p)
19544 RECORD_MAX_MIN_POS (it);
19545 }
19546 else if (CHAR_GLYPH_PADDING_P (*glyph)
19547 && !FRAME_WINDOW_P (it->f))
19548 {
19549 /* A padding glyph that doesn't fit on this line.
19550 This means the whole character doesn't fit
19551 on the line. */
19552 if (row->reversed_p)
19553 unproduce_glyphs (it, row->used[TEXT_AREA]
19554 - n_glyphs_before);
19555 row->used[TEXT_AREA] = n_glyphs_before;
19556
19557 /* Fill the rest of the row with continuation
19558 glyphs like in 20.x. */
19559 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19560 < row->glyphs[1 + TEXT_AREA])
19561 produce_special_glyphs (it, IT_CONTINUATION);
19562
19563 row->continued_p = 1;
19564 it->current_x = x_before;
19565 it->continuation_lines_width += x_before;
19566
19567 /* Restore the height to what it was before the
19568 element not fitting on the line. */
19569 it->max_ascent = ascent;
19570 it->max_descent = descent;
19571 it->max_phys_ascent = phys_ascent;
19572 it->max_phys_descent = phys_descent;
19573 }
19574 else if (wrap_row_used > 0)
19575 {
19576 back_to_wrap:
19577 if (row->reversed_p)
19578 unproduce_glyphs (it,
19579 row->used[TEXT_AREA] - wrap_row_used);
19580 RESTORE_IT (it, &wrap_it, wrap_data);
19581 it->continuation_lines_width += wrap_x;
19582 row->used[TEXT_AREA] = wrap_row_used;
19583 row->ascent = wrap_row_ascent;
19584 row->height = wrap_row_height;
19585 row->phys_ascent = wrap_row_phys_ascent;
19586 row->phys_height = wrap_row_phys_height;
19587 row->extra_line_spacing = wrap_row_extra_line_spacing;
19588 min_pos = wrap_row_min_pos;
19589 min_bpos = wrap_row_min_bpos;
19590 max_pos = wrap_row_max_pos;
19591 max_bpos = wrap_row_max_bpos;
19592 row->continued_p = 1;
19593 row->ends_at_zv_p = 0;
19594 row->exact_window_width_line_p = 0;
19595 it->continuation_lines_width += x;
19596
19597 /* Make sure that a non-default face is extended
19598 up to the right margin of the window. */
19599 extend_face_to_end_of_line (it);
19600 }
19601 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19602 {
19603 /* A TAB that extends past the right edge of the
19604 window. This produces a single glyph on
19605 window system frames. We leave the glyph in
19606 this row and let it fill the row, but don't
19607 consume the TAB. */
19608 if ((row->reversed_p
19609 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19610 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19611 produce_special_glyphs (it, IT_CONTINUATION);
19612 it->continuation_lines_width += it->last_visible_x;
19613 row->ends_in_middle_of_char_p = 1;
19614 row->continued_p = 1;
19615 glyph->pixel_width = it->last_visible_x - x;
19616 it->starts_in_middle_of_char_p = 1;
19617 }
19618 else
19619 {
19620 /* Something other than a TAB that draws past
19621 the right edge of the window. Restore
19622 positions to values before the element. */
19623 if (row->reversed_p)
19624 unproduce_glyphs (it, row->used[TEXT_AREA]
19625 - (n_glyphs_before + i));
19626 row->used[TEXT_AREA] = n_glyphs_before + i;
19627
19628 /* Display continuation glyphs. */
19629 it->current_x = x_before;
19630 it->continuation_lines_width += x;
19631 if (!FRAME_WINDOW_P (it->f)
19632 || (row->reversed_p
19633 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19634 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19635 produce_special_glyphs (it, IT_CONTINUATION);
19636 row->continued_p = 1;
19637
19638 extend_face_to_end_of_line (it);
19639
19640 if (nglyphs > 1 && i > 0)
19641 {
19642 row->ends_in_middle_of_char_p = 1;
19643 it->starts_in_middle_of_char_p = 1;
19644 }
19645
19646 /* Restore the height to what it was before the
19647 element not fitting on the line. */
19648 it->max_ascent = ascent;
19649 it->max_descent = descent;
19650 it->max_phys_ascent = phys_ascent;
19651 it->max_phys_descent = phys_descent;
19652 }
19653
19654 break;
19655 }
19656 else if (new_x > it->first_visible_x)
19657 {
19658 /* Increment number of glyphs actually displayed. */
19659 ++it->hpos;
19660
19661 /* Record the maximum and minimum buffer positions
19662 seen so far in glyphs that will be displayed by
19663 this row. */
19664 if (it->bidi_p)
19665 RECORD_MAX_MIN_POS (it);
19666
19667 if (x < it->first_visible_x)
19668 /* Glyph is partially visible, i.e. row starts at
19669 negative X position. */
19670 row->x = x - it->first_visible_x;
19671 }
19672 else
19673 {
19674 /* Glyph is completely off the left margin of the
19675 window. This should not happen because of the
19676 move_it_in_display_line at the start of this
19677 function, unless the text display area of the
19678 window is empty. */
19679 eassert (it->first_visible_x <= it->last_visible_x);
19680 }
19681 }
19682 /* Even if this display element produced no glyphs at all,
19683 we want to record its position. */
19684 if (it->bidi_p && nglyphs == 0)
19685 RECORD_MAX_MIN_POS (it);
19686
19687 row->ascent = max (row->ascent, it->max_ascent);
19688 row->height = max (row->height, it->max_ascent + it->max_descent);
19689 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19690 row->phys_height = max (row->phys_height,
19691 it->max_phys_ascent + it->max_phys_descent);
19692 row->extra_line_spacing = max (row->extra_line_spacing,
19693 it->max_extra_line_spacing);
19694
19695 /* End of this display line if row is continued. */
19696 if (row->continued_p || row->ends_at_zv_p)
19697 break;
19698 }
19699
19700 at_end_of_line:
19701 /* Is this a line end? If yes, we're also done, after making
19702 sure that a non-default face is extended up to the right
19703 margin of the window. */
19704 if (ITERATOR_AT_END_OF_LINE_P (it))
19705 {
19706 int used_before = row->used[TEXT_AREA];
19707
19708 row->ends_in_newline_from_string_p = STRINGP (it->object);
19709
19710 /* Add a space at the end of the line that is used to
19711 display the cursor there. */
19712 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19713 append_space_for_newline (it, 0);
19714
19715 /* Extend the face to the end of the line. */
19716 extend_face_to_end_of_line (it);
19717
19718 /* Make sure we have the position. */
19719 if (used_before == 0)
19720 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19721
19722 /* Record the position of the newline, for use in
19723 find_row_edges. */
19724 it->eol_pos = it->current.pos;
19725
19726 /* Consume the line end. This skips over invisible lines. */
19727 set_iterator_to_next (it, 1);
19728 it->continuation_lines_width = 0;
19729 break;
19730 }
19731
19732 /* Proceed with next display element. Note that this skips
19733 over lines invisible because of selective display. */
19734 set_iterator_to_next (it, 1);
19735
19736 /* If we truncate lines, we are done when the last displayed
19737 glyphs reach past the right margin of the window. */
19738 if (it->line_wrap == TRUNCATE
19739 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19740 ? (it->current_x >= it->last_visible_x)
19741 : (it->current_x > it->last_visible_x)))
19742 {
19743 /* Maybe add truncation glyphs. */
19744 if (!FRAME_WINDOW_P (it->f)
19745 || (row->reversed_p
19746 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19747 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19748 {
19749 int i, n;
19750
19751 if (!row->reversed_p)
19752 {
19753 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19754 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19755 break;
19756 }
19757 else
19758 {
19759 for (i = 0; i < row->used[TEXT_AREA]; i++)
19760 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19761 break;
19762 /* Remove any padding glyphs at the front of ROW, to
19763 make room for the truncation glyphs we will be
19764 adding below. The loop below always inserts at
19765 least one truncation glyph, so also remove the
19766 last glyph added to ROW. */
19767 unproduce_glyphs (it, i + 1);
19768 /* Adjust i for the loop below. */
19769 i = row->used[TEXT_AREA] - (i + 1);
19770 }
19771
19772 it->current_x = x_before;
19773 if (!FRAME_WINDOW_P (it->f))
19774 {
19775 for (n = row->used[TEXT_AREA]; i < n; ++i)
19776 {
19777 row->used[TEXT_AREA] = i;
19778 produce_special_glyphs (it, IT_TRUNCATION);
19779 }
19780 }
19781 else
19782 {
19783 row->used[TEXT_AREA] = i;
19784 produce_special_glyphs (it, IT_TRUNCATION);
19785 }
19786 }
19787 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19788 {
19789 /* Don't truncate if we can overflow newline into fringe. */
19790 if (!get_next_display_element (it))
19791 {
19792 it->continuation_lines_width = 0;
19793 row->ends_at_zv_p = 1;
19794 row->exact_window_width_line_p = 1;
19795 break;
19796 }
19797 if (ITERATOR_AT_END_OF_LINE_P (it))
19798 {
19799 row->exact_window_width_line_p = 1;
19800 goto at_end_of_line;
19801 }
19802 it->current_x = x_before;
19803 }
19804
19805 row->truncated_on_right_p = 1;
19806 it->continuation_lines_width = 0;
19807 reseat_at_next_visible_line_start (it, 0);
19808 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19809 it->hpos = hpos_before;
19810 break;
19811 }
19812 }
19813
19814 if (wrap_data)
19815 bidi_unshelve_cache (wrap_data, 1);
19816
19817 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19818 at the left window margin. */
19819 if (it->first_visible_x
19820 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19821 {
19822 if (!FRAME_WINDOW_P (it->f)
19823 || (row->reversed_p
19824 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19825 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19826 insert_left_trunc_glyphs (it);
19827 row->truncated_on_left_p = 1;
19828 }
19829
19830 /* Remember the position at which this line ends.
19831
19832 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19833 cannot be before the call to find_row_edges below, since that is
19834 where these positions are determined. */
19835 row->end = it->current;
19836 if (!it->bidi_p)
19837 {
19838 row->minpos = row->start.pos;
19839 row->maxpos = row->end.pos;
19840 }
19841 else
19842 {
19843 /* ROW->minpos and ROW->maxpos must be the smallest and
19844 `1 + the largest' buffer positions in ROW. But if ROW was
19845 bidi-reordered, these two positions can be anywhere in the
19846 row, so we must determine them now. */
19847 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19848 }
19849
19850 /* If the start of this line is the overlay arrow-position, then
19851 mark this glyph row as the one containing the overlay arrow.
19852 This is clearly a mess with variable size fonts. It would be
19853 better to let it be displayed like cursors under X. */
19854 if ((row->displays_text_p || !overlay_arrow_seen)
19855 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19856 !NILP (overlay_arrow_string)))
19857 {
19858 /* Overlay arrow in window redisplay is a fringe bitmap. */
19859 if (STRINGP (overlay_arrow_string))
19860 {
19861 struct glyph_row *arrow_row
19862 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19863 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19864 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19865 struct glyph *p = row->glyphs[TEXT_AREA];
19866 struct glyph *p2, *end;
19867
19868 /* Copy the arrow glyphs. */
19869 while (glyph < arrow_end)
19870 *p++ = *glyph++;
19871
19872 /* Throw away padding glyphs. */
19873 p2 = p;
19874 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19875 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19876 ++p2;
19877 if (p2 > p)
19878 {
19879 while (p2 < end)
19880 *p++ = *p2++;
19881 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19882 }
19883 }
19884 else
19885 {
19886 eassert (INTEGERP (overlay_arrow_string));
19887 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19888 }
19889 overlay_arrow_seen = 1;
19890 }
19891
19892 /* Highlight trailing whitespace. */
19893 if (!NILP (Vshow_trailing_whitespace))
19894 highlight_trailing_whitespace (it->f, it->glyph_row);
19895
19896 /* Compute pixel dimensions of this line. */
19897 compute_line_metrics (it);
19898
19899 /* Implementation note: No changes in the glyphs of ROW or in their
19900 faces can be done past this point, because compute_line_metrics
19901 computes ROW's hash value and stores it within the glyph_row
19902 structure. */
19903
19904 /* Record whether this row ends inside an ellipsis. */
19905 row->ends_in_ellipsis_p
19906 = (it->method == GET_FROM_DISPLAY_VECTOR
19907 && it->ellipsis_p);
19908
19909 /* Save fringe bitmaps in this row. */
19910 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19911 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19912 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19913 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19914
19915 it->left_user_fringe_bitmap = 0;
19916 it->left_user_fringe_face_id = 0;
19917 it->right_user_fringe_bitmap = 0;
19918 it->right_user_fringe_face_id = 0;
19919
19920 /* Maybe set the cursor. */
19921 cvpos = it->w->cursor.vpos;
19922 if ((cvpos < 0
19923 /* In bidi-reordered rows, keep checking for proper cursor
19924 position even if one has been found already, because buffer
19925 positions in such rows change non-linearly with ROW->VPOS,
19926 when a line is continued. One exception: when we are at ZV,
19927 display cursor on the first suitable glyph row, since all
19928 the empty rows after that also have their position set to ZV. */
19929 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19930 lines' rows is implemented for bidi-reordered rows. */
19931 || (it->bidi_p
19932 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19933 && PT >= MATRIX_ROW_START_CHARPOS (row)
19934 && PT <= MATRIX_ROW_END_CHARPOS (row)
19935 && cursor_row_p (row))
19936 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19937
19938 /* Prepare for the next line. This line starts horizontally at (X
19939 HPOS) = (0 0). Vertical positions are incremented. As a
19940 convenience for the caller, IT->glyph_row is set to the next
19941 row to be used. */
19942 it->current_x = it->hpos = 0;
19943 it->current_y += row->height;
19944 SET_TEXT_POS (it->eol_pos, 0, 0);
19945 ++it->vpos;
19946 ++it->glyph_row;
19947 /* The next row should by default use the same value of the
19948 reversed_p flag as this one. set_iterator_to_next decides when
19949 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19950 the flag accordingly. */
19951 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19952 it->glyph_row->reversed_p = row->reversed_p;
19953 it->start = row->end;
19954 return row->displays_text_p;
19955
19956 #undef RECORD_MAX_MIN_POS
19957 }
19958
19959 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19960 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19961 doc: /* Return paragraph direction at point in BUFFER.
19962 Value is either `left-to-right' or `right-to-left'.
19963 If BUFFER is omitted or nil, it defaults to the current buffer.
19964
19965 Paragraph direction determines how the text in the paragraph is displayed.
19966 In left-to-right paragraphs, text begins at the left margin of the window
19967 and the reading direction is generally left to right. In right-to-left
19968 paragraphs, text begins at the right margin and is read from right to left.
19969
19970 See also `bidi-paragraph-direction'. */)
19971 (Lisp_Object buffer)
19972 {
19973 struct buffer *buf = current_buffer;
19974 struct buffer *old = buf;
19975
19976 if (! NILP (buffer))
19977 {
19978 CHECK_BUFFER (buffer);
19979 buf = XBUFFER (buffer);
19980 }
19981
19982 if (NILP (BVAR (buf, bidi_display_reordering))
19983 || NILP (BVAR (buf, enable_multibyte_characters))
19984 /* When we are loading loadup.el, the character property tables
19985 needed for bidi iteration are not yet available. */
19986 || !NILP (Vpurify_flag))
19987 return Qleft_to_right;
19988 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19989 return BVAR (buf, bidi_paragraph_direction);
19990 else
19991 {
19992 /* Determine the direction from buffer text. We could try to
19993 use current_matrix if it is up to date, but this seems fast
19994 enough as it is. */
19995 struct bidi_it itb;
19996 ptrdiff_t pos = BUF_PT (buf);
19997 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19998 int c;
19999 void *itb_data = bidi_shelve_cache ();
20000
20001 set_buffer_temp (buf);
20002 /* bidi_paragraph_init finds the base direction of the paragraph
20003 by searching forward from paragraph start. We need the base
20004 direction of the current or _previous_ paragraph, so we need
20005 to make sure we are within that paragraph. To that end, find
20006 the previous non-empty line. */
20007 if (pos >= ZV && pos > BEGV)
20008 {
20009 pos--;
20010 bytepos = CHAR_TO_BYTE (pos);
20011 }
20012 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20013 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20014 {
20015 while ((c = FETCH_BYTE (bytepos)) == '\n'
20016 || c == ' ' || c == '\t' || c == '\f')
20017 {
20018 if (bytepos <= BEGV_BYTE)
20019 break;
20020 bytepos--;
20021 pos--;
20022 }
20023 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20024 bytepos--;
20025 }
20026 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20027 itb.paragraph_dir = NEUTRAL_DIR;
20028 itb.string.s = NULL;
20029 itb.string.lstring = Qnil;
20030 itb.string.bufpos = 0;
20031 itb.string.unibyte = 0;
20032 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20033 bidi_unshelve_cache (itb_data, 0);
20034 set_buffer_temp (old);
20035 switch (itb.paragraph_dir)
20036 {
20037 case L2R:
20038 return Qleft_to_right;
20039 break;
20040 case R2L:
20041 return Qright_to_left;
20042 break;
20043 default:
20044 abort ();
20045 }
20046 }
20047 }
20048
20049
20050 \f
20051 /***********************************************************************
20052 Menu Bar
20053 ***********************************************************************/
20054
20055 /* Redisplay the menu bar in the frame for window W.
20056
20057 The menu bar of X frames that don't have X toolkit support is
20058 displayed in a special window W->frame->menu_bar_window.
20059
20060 The menu bar of terminal frames is treated specially as far as
20061 glyph matrices are concerned. Menu bar lines are not part of
20062 windows, so the update is done directly on the frame matrix rows
20063 for the menu bar. */
20064
20065 static void
20066 display_menu_bar (struct window *w)
20067 {
20068 struct frame *f = XFRAME (WINDOW_FRAME (w));
20069 struct it it;
20070 Lisp_Object items;
20071 int i;
20072
20073 /* Don't do all this for graphical frames. */
20074 #ifdef HAVE_NTGUI
20075 if (FRAME_W32_P (f))
20076 return;
20077 #endif
20078 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20079 if (FRAME_X_P (f))
20080 return;
20081 #endif
20082
20083 #ifdef HAVE_NS
20084 if (FRAME_NS_P (f))
20085 return;
20086 #endif /* HAVE_NS */
20087
20088 #ifdef USE_X_TOOLKIT
20089 eassert (!FRAME_WINDOW_P (f));
20090 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20091 it.first_visible_x = 0;
20092 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20093 #else /* not USE_X_TOOLKIT */
20094 if (FRAME_WINDOW_P (f))
20095 {
20096 /* Menu bar lines are displayed in the desired matrix of the
20097 dummy window menu_bar_window. */
20098 struct window *menu_w;
20099 eassert (WINDOWP (f->menu_bar_window));
20100 menu_w = XWINDOW (f->menu_bar_window);
20101 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20102 MENU_FACE_ID);
20103 it.first_visible_x = 0;
20104 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20105 }
20106 else
20107 {
20108 /* This is a TTY frame, i.e. character hpos/vpos are used as
20109 pixel x/y. */
20110 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20111 MENU_FACE_ID);
20112 it.first_visible_x = 0;
20113 it.last_visible_x = FRAME_COLS (f);
20114 }
20115 #endif /* not USE_X_TOOLKIT */
20116
20117 /* FIXME: This should be controlled by a user option. See the
20118 comments in redisplay_tool_bar and display_mode_line about
20119 this. */
20120 it.paragraph_embedding = L2R;
20121
20122 if (! mode_line_inverse_video)
20123 /* Force the menu-bar to be displayed in the default face. */
20124 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20125
20126 /* Clear all rows of the menu bar. */
20127 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20128 {
20129 struct glyph_row *row = it.glyph_row + i;
20130 clear_glyph_row (row);
20131 row->enabled_p = 1;
20132 row->full_width_p = 1;
20133 }
20134
20135 /* Display all items of the menu bar. */
20136 items = FRAME_MENU_BAR_ITEMS (it.f);
20137 for (i = 0; i < ASIZE (items); i += 4)
20138 {
20139 Lisp_Object string;
20140
20141 /* Stop at nil string. */
20142 string = AREF (items, i + 1);
20143 if (NILP (string))
20144 break;
20145
20146 /* Remember where item was displayed. */
20147 ASET (items, i + 3, make_number (it.hpos));
20148
20149 /* Display the item, pad with one space. */
20150 if (it.current_x < it.last_visible_x)
20151 display_string (NULL, string, Qnil, 0, 0, &it,
20152 SCHARS (string) + 1, 0, 0, -1);
20153 }
20154
20155 /* Fill out the line with spaces. */
20156 if (it.current_x < it.last_visible_x)
20157 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20158
20159 /* Compute the total height of the lines. */
20160 compute_line_metrics (&it);
20161 }
20162
20163
20164 \f
20165 /***********************************************************************
20166 Mode Line
20167 ***********************************************************************/
20168
20169 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20170 FORCE is non-zero, redisplay mode lines unconditionally.
20171 Otherwise, redisplay only mode lines that are garbaged. Value is
20172 the number of windows whose mode lines were redisplayed. */
20173
20174 static int
20175 redisplay_mode_lines (Lisp_Object window, int force)
20176 {
20177 int nwindows = 0;
20178
20179 while (!NILP (window))
20180 {
20181 struct window *w = XWINDOW (window);
20182
20183 if (WINDOWP (w->hchild))
20184 nwindows += redisplay_mode_lines (w->hchild, force);
20185 else if (WINDOWP (w->vchild))
20186 nwindows += redisplay_mode_lines (w->vchild, force);
20187 else if (force
20188 || FRAME_GARBAGED_P (XFRAME (w->frame))
20189 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20190 {
20191 struct text_pos lpoint;
20192 struct buffer *old = current_buffer;
20193
20194 /* Set the window's buffer for the mode line display. */
20195 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20196 set_buffer_internal_1 (XBUFFER (w->buffer));
20197
20198 /* Point refers normally to the selected window. For any
20199 other window, set up appropriate value. */
20200 if (!EQ (window, selected_window))
20201 {
20202 struct text_pos pt;
20203
20204 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20205 if (CHARPOS (pt) < BEGV)
20206 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20207 else if (CHARPOS (pt) > (ZV - 1))
20208 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20209 else
20210 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20211 }
20212
20213 /* Display mode lines. */
20214 clear_glyph_matrix (w->desired_matrix);
20215 if (display_mode_lines (w))
20216 {
20217 ++nwindows;
20218 w->must_be_updated_p = 1;
20219 }
20220
20221 /* Restore old settings. */
20222 set_buffer_internal_1 (old);
20223 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20224 }
20225
20226 window = w->next;
20227 }
20228
20229 return nwindows;
20230 }
20231
20232
20233 /* Display the mode and/or header line of window W. Value is the
20234 sum number of mode lines and header lines displayed. */
20235
20236 static int
20237 display_mode_lines (struct window *w)
20238 {
20239 Lisp_Object old_selected_window, old_selected_frame;
20240 int n = 0;
20241
20242 old_selected_frame = selected_frame;
20243 selected_frame = w->frame;
20244 old_selected_window = selected_window;
20245 XSETWINDOW (selected_window, w);
20246
20247 /* These will be set while the mode line specs are processed. */
20248 line_number_displayed = 0;
20249 w->column_number_displayed = Qnil;
20250
20251 if (WINDOW_WANTS_MODELINE_P (w))
20252 {
20253 struct window *sel_w = XWINDOW (old_selected_window);
20254
20255 /* Select mode line face based on the real selected window. */
20256 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20257 BVAR (current_buffer, mode_line_format));
20258 ++n;
20259 }
20260
20261 if (WINDOW_WANTS_HEADER_LINE_P (w))
20262 {
20263 display_mode_line (w, HEADER_LINE_FACE_ID,
20264 BVAR (current_buffer, header_line_format));
20265 ++n;
20266 }
20267
20268 selected_frame = old_selected_frame;
20269 selected_window = old_selected_window;
20270 return n;
20271 }
20272
20273
20274 /* Display mode or header line of window W. FACE_ID specifies which
20275 line to display; it is either MODE_LINE_FACE_ID or
20276 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20277 display. Value is the pixel height of the mode/header line
20278 displayed. */
20279
20280 static int
20281 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20282 {
20283 struct it it;
20284 struct face *face;
20285 ptrdiff_t count = SPECPDL_INDEX ();
20286
20287 init_iterator (&it, w, -1, -1, NULL, face_id);
20288 /* Don't extend on a previously drawn mode-line.
20289 This may happen if called from pos_visible_p. */
20290 it.glyph_row->enabled_p = 0;
20291 prepare_desired_row (it.glyph_row);
20292
20293 it.glyph_row->mode_line_p = 1;
20294
20295 if (! mode_line_inverse_video)
20296 /* Force the mode-line to be displayed in the default face. */
20297 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20298
20299 /* FIXME: This should be controlled by a user option. But
20300 supporting such an option is not trivial, since the mode line is
20301 made up of many separate strings. */
20302 it.paragraph_embedding = L2R;
20303
20304 record_unwind_protect (unwind_format_mode_line,
20305 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20306
20307 mode_line_target = MODE_LINE_DISPLAY;
20308
20309 /* Temporarily make frame's keyboard the current kboard so that
20310 kboard-local variables in the mode_line_format will get the right
20311 values. */
20312 push_kboard (FRAME_KBOARD (it.f));
20313 record_unwind_save_match_data ();
20314 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20315 pop_kboard ();
20316
20317 unbind_to (count, Qnil);
20318
20319 /* Fill up with spaces. */
20320 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20321
20322 compute_line_metrics (&it);
20323 it.glyph_row->full_width_p = 1;
20324 it.glyph_row->continued_p = 0;
20325 it.glyph_row->truncated_on_left_p = 0;
20326 it.glyph_row->truncated_on_right_p = 0;
20327
20328 /* Make a 3D mode-line have a shadow at its right end. */
20329 face = FACE_FROM_ID (it.f, face_id);
20330 extend_face_to_end_of_line (&it);
20331 if (face->box != FACE_NO_BOX)
20332 {
20333 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20334 + it.glyph_row->used[TEXT_AREA] - 1);
20335 last->right_box_line_p = 1;
20336 }
20337
20338 return it.glyph_row->height;
20339 }
20340
20341 /* Move element ELT in LIST to the front of LIST.
20342 Return the updated list. */
20343
20344 static Lisp_Object
20345 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20346 {
20347 register Lisp_Object tail, prev;
20348 register Lisp_Object tem;
20349
20350 tail = list;
20351 prev = Qnil;
20352 while (CONSP (tail))
20353 {
20354 tem = XCAR (tail);
20355
20356 if (EQ (elt, tem))
20357 {
20358 /* Splice out the link TAIL. */
20359 if (NILP (prev))
20360 list = XCDR (tail);
20361 else
20362 Fsetcdr (prev, XCDR (tail));
20363
20364 /* Now make it the first. */
20365 Fsetcdr (tail, list);
20366 return tail;
20367 }
20368 else
20369 prev = tail;
20370 tail = XCDR (tail);
20371 QUIT;
20372 }
20373
20374 /* Not found--return unchanged LIST. */
20375 return list;
20376 }
20377
20378 /* Contribute ELT to the mode line for window IT->w. How it
20379 translates into text depends on its data type.
20380
20381 IT describes the display environment in which we display, as usual.
20382
20383 DEPTH is the depth in recursion. It is used to prevent
20384 infinite recursion here.
20385
20386 FIELD_WIDTH is the number of characters the display of ELT should
20387 occupy in the mode line, and PRECISION is the maximum number of
20388 characters to display from ELT's representation. See
20389 display_string for details.
20390
20391 Returns the hpos of the end of the text generated by ELT.
20392
20393 PROPS is a property list to add to any string we encounter.
20394
20395 If RISKY is nonzero, remove (disregard) any properties in any string
20396 we encounter, and ignore :eval and :propertize.
20397
20398 The global variable `mode_line_target' determines whether the
20399 output is passed to `store_mode_line_noprop',
20400 `store_mode_line_string', or `display_string'. */
20401
20402 static int
20403 display_mode_element (struct it *it, int depth, int field_width, int precision,
20404 Lisp_Object elt, Lisp_Object props, int risky)
20405 {
20406 int n = 0, field, prec;
20407 int literal = 0;
20408
20409 tail_recurse:
20410 if (depth > 100)
20411 elt = build_string ("*too-deep*");
20412
20413 depth++;
20414
20415 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20416 {
20417 case Lisp_String:
20418 {
20419 /* A string: output it and check for %-constructs within it. */
20420 unsigned char c;
20421 ptrdiff_t offset = 0;
20422
20423 if (SCHARS (elt) > 0
20424 && (!NILP (props) || risky))
20425 {
20426 Lisp_Object oprops, aelt;
20427 oprops = Ftext_properties_at (make_number (0), elt);
20428
20429 /* If the starting string's properties are not what
20430 we want, translate the string. Also, if the string
20431 is risky, do that anyway. */
20432
20433 if (NILP (Fequal (props, oprops)) || risky)
20434 {
20435 /* If the starting string has properties,
20436 merge the specified ones onto the existing ones. */
20437 if (! NILP (oprops) && !risky)
20438 {
20439 Lisp_Object tem;
20440
20441 oprops = Fcopy_sequence (oprops);
20442 tem = props;
20443 while (CONSP (tem))
20444 {
20445 oprops = Fplist_put (oprops, XCAR (tem),
20446 XCAR (XCDR (tem)));
20447 tem = XCDR (XCDR (tem));
20448 }
20449 props = oprops;
20450 }
20451
20452 aelt = Fassoc (elt, mode_line_proptrans_alist);
20453 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20454 {
20455 /* AELT is what we want. Move it to the front
20456 without consing. */
20457 elt = XCAR (aelt);
20458 mode_line_proptrans_alist
20459 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20460 }
20461 else
20462 {
20463 Lisp_Object tem;
20464
20465 /* If AELT has the wrong props, it is useless.
20466 so get rid of it. */
20467 if (! NILP (aelt))
20468 mode_line_proptrans_alist
20469 = Fdelq (aelt, mode_line_proptrans_alist);
20470
20471 elt = Fcopy_sequence (elt);
20472 Fset_text_properties (make_number (0), Flength (elt),
20473 props, elt);
20474 /* Add this item to mode_line_proptrans_alist. */
20475 mode_line_proptrans_alist
20476 = Fcons (Fcons (elt, props),
20477 mode_line_proptrans_alist);
20478 /* Truncate mode_line_proptrans_alist
20479 to at most 50 elements. */
20480 tem = Fnthcdr (make_number (50),
20481 mode_line_proptrans_alist);
20482 if (! NILP (tem))
20483 XSETCDR (tem, Qnil);
20484 }
20485 }
20486 }
20487
20488 offset = 0;
20489
20490 if (literal)
20491 {
20492 prec = precision - n;
20493 switch (mode_line_target)
20494 {
20495 case MODE_LINE_NOPROP:
20496 case MODE_LINE_TITLE:
20497 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20498 break;
20499 case MODE_LINE_STRING:
20500 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20501 break;
20502 case MODE_LINE_DISPLAY:
20503 n += display_string (NULL, elt, Qnil, 0, 0, it,
20504 0, prec, 0, STRING_MULTIBYTE (elt));
20505 break;
20506 }
20507
20508 break;
20509 }
20510
20511 /* Handle the non-literal case. */
20512
20513 while ((precision <= 0 || n < precision)
20514 && SREF (elt, offset) != 0
20515 && (mode_line_target != MODE_LINE_DISPLAY
20516 || it->current_x < it->last_visible_x))
20517 {
20518 ptrdiff_t last_offset = offset;
20519
20520 /* Advance to end of string or next format specifier. */
20521 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20522 ;
20523
20524 if (offset - 1 != last_offset)
20525 {
20526 ptrdiff_t nchars, nbytes;
20527
20528 /* Output to end of string or up to '%'. Field width
20529 is length of string. Don't output more than
20530 PRECISION allows us. */
20531 offset--;
20532
20533 prec = c_string_width (SDATA (elt) + last_offset,
20534 offset - last_offset, precision - n,
20535 &nchars, &nbytes);
20536
20537 switch (mode_line_target)
20538 {
20539 case MODE_LINE_NOPROP:
20540 case MODE_LINE_TITLE:
20541 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20542 break;
20543 case MODE_LINE_STRING:
20544 {
20545 ptrdiff_t bytepos = last_offset;
20546 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20547 ptrdiff_t endpos = (precision <= 0
20548 ? string_byte_to_char (elt, offset)
20549 : charpos + nchars);
20550
20551 n += store_mode_line_string (NULL,
20552 Fsubstring (elt, make_number (charpos),
20553 make_number (endpos)),
20554 0, 0, 0, Qnil);
20555 }
20556 break;
20557 case MODE_LINE_DISPLAY:
20558 {
20559 ptrdiff_t bytepos = last_offset;
20560 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20561
20562 if (precision <= 0)
20563 nchars = string_byte_to_char (elt, offset) - charpos;
20564 n += display_string (NULL, elt, Qnil, 0, charpos,
20565 it, 0, nchars, 0,
20566 STRING_MULTIBYTE (elt));
20567 }
20568 break;
20569 }
20570 }
20571 else /* c == '%' */
20572 {
20573 ptrdiff_t percent_position = offset;
20574
20575 /* Get the specified minimum width. Zero means
20576 don't pad. */
20577 field = 0;
20578 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20579 field = field * 10 + c - '0';
20580
20581 /* Don't pad beyond the total padding allowed. */
20582 if (field_width - n > 0 && field > field_width - n)
20583 field = field_width - n;
20584
20585 /* Note that either PRECISION <= 0 or N < PRECISION. */
20586 prec = precision - n;
20587
20588 if (c == 'M')
20589 n += display_mode_element (it, depth, field, prec,
20590 Vglobal_mode_string, props,
20591 risky);
20592 else if (c != 0)
20593 {
20594 int multibyte;
20595 ptrdiff_t bytepos, charpos;
20596 const char *spec;
20597 Lisp_Object string;
20598
20599 bytepos = percent_position;
20600 charpos = (STRING_MULTIBYTE (elt)
20601 ? string_byte_to_char (elt, bytepos)
20602 : bytepos);
20603 spec = decode_mode_spec (it->w, c, field, &string);
20604 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20605
20606 switch (mode_line_target)
20607 {
20608 case MODE_LINE_NOPROP:
20609 case MODE_LINE_TITLE:
20610 n += store_mode_line_noprop (spec, field, prec);
20611 break;
20612 case MODE_LINE_STRING:
20613 {
20614 Lisp_Object tem = build_string (spec);
20615 props = Ftext_properties_at (make_number (charpos), elt);
20616 /* Should only keep face property in props */
20617 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20618 }
20619 break;
20620 case MODE_LINE_DISPLAY:
20621 {
20622 int nglyphs_before, nwritten;
20623
20624 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20625 nwritten = display_string (spec, string, elt,
20626 charpos, 0, it,
20627 field, prec, 0,
20628 multibyte);
20629
20630 /* Assign to the glyphs written above the
20631 string where the `%x' came from, position
20632 of the `%'. */
20633 if (nwritten > 0)
20634 {
20635 struct glyph *glyph
20636 = (it->glyph_row->glyphs[TEXT_AREA]
20637 + nglyphs_before);
20638 int i;
20639
20640 for (i = 0; i < nwritten; ++i)
20641 {
20642 glyph[i].object = elt;
20643 glyph[i].charpos = charpos;
20644 }
20645
20646 n += nwritten;
20647 }
20648 }
20649 break;
20650 }
20651 }
20652 else /* c == 0 */
20653 break;
20654 }
20655 }
20656 }
20657 break;
20658
20659 case Lisp_Symbol:
20660 /* A symbol: process the value of the symbol recursively
20661 as if it appeared here directly. Avoid error if symbol void.
20662 Special case: if value of symbol is a string, output the string
20663 literally. */
20664 {
20665 register Lisp_Object tem;
20666
20667 /* If the variable is not marked as risky to set
20668 then its contents are risky to use. */
20669 if (NILP (Fget (elt, Qrisky_local_variable)))
20670 risky = 1;
20671
20672 tem = Fboundp (elt);
20673 if (!NILP (tem))
20674 {
20675 tem = Fsymbol_value (elt);
20676 /* If value is a string, output that string literally:
20677 don't check for % within it. */
20678 if (STRINGP (tem))
20679 literal = 1;
20680
20681 if (!EQ (tem, elt))
20682 {
20683 /* Give up right away for nil or t. */
20684 elt = tem;
20685 goto tail_recurse;
20686 }
20687 }
20688 }
20689 break;
20690
20691 case Lisp_Cons:
20692 {
20693 register Lisp_Object car, tem;
20694
20695 /* A cons cell: five distinct cases.
20696 If first element is :eval or :propertize, do something special.
20697 If first element is a string or a cons, process all the elements
20698 and effectively concatenate them.
20699 If first element is a negative number, truncate displaying cdr to
20700 at most that many characters. If positive, pad (with spaces)
20701 to at least that many characters.
20702 If first element is a symbol, process the cadr or caddr recursively
20703 according to whether the symbol's value is non-nil or nil. */
20704 car = XCAR (elt);
20705 if (EQ (car, QCeval))
20706 {
20707 /* An element of the form (:eval FORM) means evaluate FORM
20708 and use the result as mode line elements. */
20709
20710 if (risky)
20711 break;
20712
20713 if (CONSP (XCDR (elt)))
20714 {
20715 Lisp_Object spec;
20716 spec = safe_eval (XCAR (XCDR (elt)));
20717 n += display_mode_element (it, depth, field_width - n,
20718 precision - n, spec, props,
20719 risky);
20720 }
20721 }
20722 else if (EQ (car, QCpropertize))
20723 {
20724 /* An element of the form (:propertize ELT PROPS...)
20725 means display ELT but applying properties PROPS. */
20726
20727 if (risky)
20728 break;
20729
20730 if (CONSP (XCDR (elt)))
20731 n += display_mode_element (it, depth, field_width - n,
20732 precision - n, XCAR (XCDR (elt)),
20733 XCDR (XCDR (elt)), risky);
20734 }
20735 else if (SYMBOLP (car))
20736 {
20737 tem = Fboundp (car);
20738 elt = XCDR (elt);
20739 if (!CONSP (elt))
20740 goto invalid;
20741 /* elt is now the cdr, and we know it is a cons cell.
20742 Use its car if CAR has a non-nil value. */
20743 if (!NILP (tem))
20744 {
20745 tem = Fsymbol_value (car);
20746 if (!NILP (tem))
20747 {
20748 elt = XCAR (elt);
20749 goto tail_recurse;
20750 }
20751 }
20752 /* Symbol's value is nil (or symbol is unbound)
20753 Get the cddr of the original list
20754 and if possible find the caddr and use that. */
20755 elt = XCDR (elt);
20756 if (NILP (elt))
20757 break;
20758 else if (!CONSP (elt))
20759 goto invalid;
20760 elt = XCAR (elt);
20761 goto tail_recurse;
20762 }
20763 else if (INTEGERP (car))
20764 {
20765 register int lim = XINT (car);
20766 elt = XCDR (elt);
20767 if (lim < 0)
20768 {
20769 /* Negative int means reduce maximum width. */
20770 if (precision <= 0)
20771 precision = -lim;
20772 else
20773 precision = min (precision, -lim);
20774 }
20775 else if (lim > 0)
20776 {
20777 /* Padding specified. Don't let it be more than
20778 current maximum. */
20779 if (precision > 0)
20780 lim = min (precision, lim);
20781
20782 /* If that's more padding than already wanted, queue it.
20783 But don't reduce padding already specified even if
20784 that is beyond the current truncation point. */
20785 field_width = max (lim, field_width);
20786 }
20787 goto tail_recurse;
20788 }
20789 else if (STRINGP (car) || CONSP (car))
20790 {
20791 Lisp_Object halftail = elt;
20792 int len = 0;
20793
20794 while (CONSP (elt)
20795 && (precision <= 0 || n < precision))
20796 {
20797 n += display_mode_element (it, depth,
20798 /* Do padding only after the last
20799 element in the list. */
20800 (! CONSP (XCDR (elt))
20801 ? field_width - n
20802 : 0),
20803 precision - n, XCAR (elt),
20804 props, risky);
20805 elt = XCDR (elt);
20806 len++;
20807 if ((len & 1) == 0)
20808 halftail = XCDR (halftail);
20809 /* Check for cycle. */
20810 if (EQ (halftail, elt))
20811 break;
20812 }
20813 }
20814 }
20815 break;
20816
20817 default:
20818 invalid:
20819 elt = build_string ("*invalid*");
20820 goto tail_recurse;
20821 }
20822
20823 /* Pad to FIELD_WIDTH. */
20824 if (field_width > 0 && n < field_width)
20825 {
20826 switch (mode_line_target)
20827 {
20828 case MODE_LINE_NOPROP:
20829 case MODE_LINE_TITLE:
20830 n += store_mode_line_noprop ("", field_width - n, 0);
20831 break;
20832 case MODE_LINE_STRING:
20833 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20834 break;
20835 case MODE_LINE_DISPLAY:
20836 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20837 0, 0, 0);
20838 break;
20839 }
20840 }
20841
20842 return n;
20843 }
20844
20845 /* Store a mode-line string element in mode_line_string_list.
20846
20847 If STRING is non-null, display that C string. Otherwise, the Lisp
20848 string LISP_STRING is displayed.
20849
20850 FIELD_WIDTH is the minimum number of output glyphs to produce.
20851 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20852 with spaces. FIELD_WIDTH <= 0 means don't pad.
20853
20854 PRECISION is the maximum number of characters to output from
20855 STRING. PRECISION <= 0 means don't truncate the string.
20856
20857 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20858 properties to the string.
20859
20860 PROPS are the properties to add to the string.
20861 The mode_line_string_face face property is always added to the string.
20862 */
20863
20864 static int
20865 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20866 int field_width, int precision, Lisp_Object props)
20867 {
20868 ptrdiff_t len;
20869 int n = 0;
20870
20871 if (string != NULL)
20872 {
20873 len = strlen (string);
20874 if (precision > 0 && len > precision)
20875 len = precision;
20876 lisp_string = make_string (string, len);
20877 if (NILP (props))
20878 props = mode_line_string_face_prop;
20879 else if (!NILP (mode_line_string_face))
20880 {
20881 Lisp_Object face = Fplist_get (props, Qface);
20882 props = Fcopy_sequence (props);
20883 if (NILP (face))
20884 face = mode_line_string_face;
20885 else
20886 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20887 props = Fplist_put (props, Qface, face);
20888 }
20889 Fadd_text_properties (make_number (0), make_number (len),
20890 props, lisp_string);
20891 }
20892 else
20893 {
20894 len = XFASTINT (Flength (lisp_string));
20895 if (precision > 0 && len > precision)
20896 {
20897 len = precision;
20898 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20899 precision = -1;
20900 }
20901 if (!NILP (mode_line_string_face))
20902 {
20903 Lisp_Object face;
20904 if (NILP (props))
20905 props = Ftext_properties_at (make_number (0), lisp_string);
20906 face = Fplist_get (props, Qface);
20907 if (NILP (face))
20908 face = mode_line_string_face;
20909 else
20910 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20911 props = Fcons (Qface, Fcons (face, Qnil));
20912 if (copy_string)
20913 lisp_string = Fcopy_sequence (lisp_string);
20914 }
20915 if (!NILP (props))
20916 Fadd_text_properties (make_number (0), make_number (len),
20917 props, lisp_string);
20918 }
20919
20920 if (len > 0)
20921 {
20922 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20923 n += len;
20924 }
20925
20926 if (field_width > len)
20927 {
20928 field_width -= len;
20929 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20930 if (!NILP (props))
20931 Fadd_text_properties (make_number (0), make_number (field_width),
20932 props, lisp_string);
20933 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20934 n += field_width;
20935 }
20936
20937 return n;
20938 }
20939
20940
20941 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20942 1, 4, 0,
20943 doc: /* Format a string out of a mode line format specification.
20944 First arg FORMAT specifies the mode line format (see `mode-line-format'
20945 for details) to use.
20946
20947 By default, the format is evaluated for the currently selected window.
20948
20949 Optional second arg FACE specifies the face property to put on all
20950 characters for which no face is specified. The value nil means the
20951 default face. The value t means whatever face the window's mode line
20952 currently uses (either `mode-line' or `mode-line-inactive',
20953 depending on whether the window is the selected window or not).
20954 An integer value means the value string has no text
20955 properties.
20956
20957 Optional third and fourth args WINDOW and BUFFER specify the window
20958 and buffer to use as the context for the formatting (defaults
20959 are the selected window and the WINDOW's buffer). */)
20960 (Lisp_Object format, Lisp_Object face,
20961 Lisp_Object window, Lisp_Object buffer)
20962 {
20963 struct it it;
20964 int len;
20965 struct window *w;
20966 struct buffer *old_buffer = NULL;
20967 int face_id;
20968 int no_props = INTEGERP (face);
20969 ptrdiff_t count = SPECPDL_INDEX ();
20970 Lisp_Object str;
20971 int string_start = 0;
20972
20973 if (NILP (window))
20974 window = selected_window;
20975 CHECK_WINDOW (window);
20976 w = XWINDOW (window);
20977
20978 if (NILP (buffer))
20979 buffer = w->buffer;
20980 CHECK_BUFFER (buffer);
20981
20982 /* Make formatting the modeline a non-op when noninteractive, otherwise
20983 there will be problems later caused by a partially initialized frame. */
20984 if (NILP (format) || noninteractive)
20985 return empty_unibyte_string;
20986
20987 if (no_props)
20988 face = Qnil;
20989
20990 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20991 : EQ (face, Qt) ? (EQ (window, selected_window)
20992 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20993 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20994 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20995 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20996 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20997 : DEFAULT_FACE_ID;
20998
20999 if (XBUFFER (buffer) != current_buffer)
21000 old_buffer = current_buffer;
21001
21002 /* Save things including mode_line_proptrans_alist,
21003 and set that to nil so that we don't alter the outer value. */
21004 record_unwind_protect (unwind_format_mode_line,
21005 format_mode_line_unwind_data
21006 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21007 old_buffer, selected_window, 1));
21008 mode_line_proptrans_alist = Qnil;
21009
21010 Fselect_window (window, Qt);
21011 if (old_buffer)
21012 set_buffer_internal_1 (XBUFFER (buffer));
21013
21014 init_iterator (&it, w, -1, -1, NULL, face_id);
21015
21016 if (no_props)
21017 {
21018 mode_line_target = MODE_LINE_NOPROP;
21019 mode_line_string_face_prop = Qnil;
21020 mode_line_string_list = Qnil;
21021 string_start = MODE_LINE_NOPROP_LEN (0);
21022 }
21023 else
21024 {
21025 mode_line_target = MODE_LINE_STRING;
21026 mode_line_string_list = Qnil;
21027 mode_line_string_face = face;
21028 mode_line_string_face_prop
21029 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21030 }
21031
21032 push_kboard (FRAME_KBOARD (it.f));
21033 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21034 pop_kboard ();
21035
21036 if (no_props)
21037 {
21038 len = MODE_LINE_NOPROP_LEN (string_start);
21039 str = make_string (mode_line_noprop_buf + string_start, len);
21040 }
21041 else
21042 {
21043 mode_line_string_list = Fnreverse (mode_line_string_list);
21044 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21045 empty_unibyte_string);
21046 }
21047
21048 unbind_to (count, Qnil);
21049 return str;
21050 }
21051
21052 /* Write a null-terminated, right justified decimal representation of
21053 the positive integer D to BUF using a minimal field width WIDTH. */
21054
21055 static void
21056 pint2str (register char *buf, register int width, register ptrdiff_t d)
21057 {
21058 register char *p = buf;
21059
21060 if (d <= 0)
21061 *p++ = '0';
21062 else
21063 {
21064 while (d > 0)
21065 {
21066 *p++ = d % 10 + '0';
21067 d /= 10;
21068 }
21069 }
21070
21071 for (width -= (int) (p - buf); width > 0; --width)
21072 *p++ = ' ';
21073 *p-- = '\0';
21074 while (p > buf)
21075 {
21076 d = *buf;
21077 *buf++ = *p;
21078 *p-- = d;
21079 }
21080 }
21081
21082 /* Write a null-terminated, right justified decimal and "human
21083 readable" representation of the nonnegative integer D to BUF using
21084 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21085
21086 static const char power_letter[] =
21087 {
21088 0, /* no letter */
21089 'k', /* kilo */
21090 'M', /* mega */
21091 'G', /* giga */
21092 'T', /* tera */
21093 'P', /* peta */
21094 'E', /* exa */
21095 'Z', /* zetta */
21096 'Y' /* yotta */
21097 };
21098
21099 static void
21100 pint2hrstr (char *buf, int width, ptrdiff_t d)
21101 {
21102 /* We aim to represent the nonnegative integer D as
21103 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21104 ptrdiff_t quotient = d;
21105 int remainder = 0;
21106 /* -1 means: do not use TENTHS. */
21107 int tenths = -1;
21108 int exponent = 0;
21109
21110 /* Length of QUOTIENT.TENTHS as a string. */
21111 int length;
21112
21113 char * psuffix;
21114 char * p;
21115
21116 if (1000 <= quotient)
21117 {
21118 /* Scale to the appropriate EXPONENT. */
21119 do
21120 {
21121 remainder = quotient % 1000;
21122 quotient /= 1000;
21123 exponent++;
21124 }
21125 while (1000 <= quotient);
21126
21127 /* Round to nearest and decide whether to use TENTHS or not. */
21128 if (quotient <= 9)
21129 {
21130 tenths = remainder / 100;
21131 if (50 <= remainder % 100)
21132 {
21133 if (tenths < 9)
21134 tenths++;
21135 else
21136 {
21137 quotient++;
21138 if (quotient == 10)
21139 tenths = -1;
21140 else
21141 tenths = 0;
21142 }
21143 }
21144 }
21145 else
21146 if (500 <= remainder)
21147 {
21148 if (quotient < 999)
21149 quotient++;
21150 else
21151 {
21152 quotient = 1;
21153 exponent++;
21154 tenths = 0;
21155 }
21156 }
21157 }
21158
21159 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21160 if (tenths == -1 && quotient <= 99)
21161 if (quotient <= 9)
21162 length = 1;
21163 else
21164 length = 2;
21165 else
21166 length = 3;
21167 p = psuffix = buf + max (width, length);
21168
21169 /* Print EXPONENT. */
21170 *psuffix++ = power_letter[exponent];
21171 *psuffix = '\0';
21172
21173 /* Print TENTHS. */
21174 if (tenths >= 0)
21175 {
21176 *--p = '0' + tenths;
21177 *--p = '.';
21178 }
21179
21180 /* Print QUOTIENT. */
21181 do
21182 {
21183 int digit = quotient % 10;
21184 *--p = '0' + digit;
21185 }
21186 while ((quotient /= 10) != 0);
21187
21188 /* Print leading spaces. */
21189 while (buf < p)
21190 *--p = ' ';
21191 }
21192
21193 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21194 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21195 type of CODING_SYSTEM. Return updated pointer into BUF. */
21196
21197 static unsigned char invalid_eol_type[] = "(*invalid*)";
21198
21199 static char *
21200 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21201 {
21202 Lisp_Object val;
21203 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21204 const unsigned char *eol_str;
21205 int eol_str_len;
21206 /* The EOL conversion we are using. */
21207 Lisp_Object eoltype;
21208
21209 val = CODING_SYSTEM_SPEC (coding_system);
21210 eoltype = Qnil;
21211
21212 if (!VECTORP (val)) /* Not yet decided. */
21213 {
21214 *buf++ = multibyte ? '-' : ' ';
21215 if (eol_flag)
21216 eoltype = eol_mnemonic_undecided;
21217 /* Don't mention EOL conversion if it isn't decided. */
21218 }
21219 else
21220 {
21221 Lisp_Object attrs;
21222 Lisp_Object eolvalue;
21223
21224 attrs = AREF (val, 0);
21225 eolvalue = AREF (val, 2);
21226
21227 *buf++ = multibyte
21228 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21229 : ' ';
21230
21231 if (eol_flag)
21232 {
21233 /* The EOL conversion that is normal on this system. */
21234
21235 if (NILP (eolvalue)) /* Not yet decided. */
21236 eoltype = eol_mnemonic_undecided;
21237 else if (VECTORP (eolvalue)) /* Not yet decided. */
21238 eoltype = eol_mnemonic_undecided;
21239 else /* eolvalue is Qunix, Qdos, or Qmac. */
21240 eoltype = (EQ (eolvalue, Qunix)
21241 ? eol_mnemonic_unix
21242 : (EQ (eolvalue, Qdos) == 1
21243 ? eol_mnemonic_dos : eol_mnemonic_mac));
21244 }
21245 }
21246
21247 if (eol_flag)
21248 {
21249 /* Mention the EOL conversion if it is not the usual one. */
21250 if (STRINGP (eoltype))
21251 {
21252 eol_str = SDATA (eoltype);
21253 eol_str_len = SBYTES (eoltype);
21254 }
21255 else if (CHARACTERP (eoltype))
21256 {
21257 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21258 int c = XFASTINT (eoltype);
21259 eol_str_len = CHAR_STRING (c, tmp);
21260 eol_str = tmp;
21261 }
21262 else
21263 {
21264 eol_str = invalid_eol_type;
21265 eol_str_len = sizeof (invalid_eol_type) - 1;
21266 }
21267 memcpy (buf, eol_str, eol_str_len);
21268 buf += eol_str_len;
21269 }
21270
21271 return buf;
21272 }
21273
21274 /* Return a string for the output of a mode line %-spec for window W,
21275 generated by character C. FIELD_WIDTH > 0 means pad the string
21276 returned with spaces to that value. Return a Lisp string in
21277 *STRING if the resulting string is taken from that Lisp string.
21278
21279 Note we operate on the current buffer for most purposes,
21280 the exception being w->base_line_pos. */
21281
21282 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21283
21284 static const char *
21285 decode_mode_spec (struct window *w, register int c, int field_width,
21286 Lisp_Object *string)
21287 {
21288 Lisp_Object obj;
21289 struct frame *f = XFRAME (WINDOW_FRAME (w));
21290 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21291 struct buffer *b = current_buffer;
21292
21293 obj = Qnil;
21294 *string = Qnil;
21295
21296 switch (c)
21297 {
21298 case '*':
21299 if (!NILP (BVAR (b, read_only)))
21300 return "%";
21301 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21302 return "*";
21303 return "-";
21304
21305 case '+':
21306 /* This differs from %* only for a modified read-only buffer. */
21307 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21308 return "*";
21309 if (!NILP (BVAR (b, read_only)))
21310 return "%";
21311 return "-";
21312
21313 case '&':
21314 /* This differs from %* in ignoring read-only-ness. */
21315 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21316 return "*";
21317 return "-";
21318
21319 case '%':
21320 return "%";
21321
21322 case '[':
21323 {
21324 int i;
21325 char *p;
21326
21327 if (command_loop_level > 5)
21328 return "[[[... ";
21329 p = decode_mode_spec_buf;
21330 for (i = 0; i < command_loop_level; i++)
21331 *p++ = '[';
21332 *p = 0;
21333 return decode_mode_spec_buf;
21334 }
21335
21336 case ']':
21337 {
21338 int i;
21339 char *p;
21340
21341 if (command_loop_level > 5)
21342 return " ...]]]";
21343 p = decode_mode_spec_buf;
21344 for (i = 0; i < command_loop_level; i++)
21345 *p++ = ']';
21346 *p = 0;
21347 return decode_mode_spec_buf;
21348 }
21349
21350 case '-':
21351 {
21352 register int i;
21353
21354 /* Let lots_of_dashes be a string of infinite length. */
21355 if (mode_line_target == MODE_LINE_NOPROP ||
21356 mode_line_target == MODE_LINE_STRING)
21357 return "--";
21358 if (field_width <= 0
21359 || field_width > sizeof (lots_of_dashes))
21360 {
21361 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21362 decode_mode_spec_buf[i] = '-';
21363 decode_mode_spec_buf[i] = '\0';
21364 return decode_mode_spec_buf;
21365 }
21366 else
21367 return lots_of_dashes;
21368 }
21369
21370 case 'b':
21371 obj = BVAR (b, name);
21372 break;
21373
21374 case 'c':
21375 /* %c and %l are ignored in `frame-title-format'.
21376 (In redisplay_internal, the frame title is drawn _before_ the
21377 windows are updated, so the stuff which depends on actual
21378 window contents (such as %l) may fail to render properly, or
21379 even crash emacs.) */
21380 if (mode_line_target == MODE_LINE_TITLE)
21381 return "";
21382 else
21383 {
21384 ptrdiff_t col = current_column ();
21385 w->column_number_displayed = make_number (col);
21386 pint2str (decode_mode_spec_buf, field_width, col);
21387 return decode_mode_spec_buf;
21388 }
21389
21390 case 'e':
21391 #ifndef SYSTEM_MALLOC
21392 {
21393 if (NILP (Vmemory_full))
21394 return "";
21395 else
21396 return "!MEM FULL! ";
21397 }
21398 #else
21399 return "";
21400 #endif
21401
21402 case 'F':
21403 /* %F displays the frame name. */
21404 if (!NILP (f->title))
21405 return SSDATA (f->title);
21406 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21407 return SSDATA (f->name);
21408 return "Emacs";
21409
21410 case 'f':
21411 obj = BVAR (b, filename);
21412 break;
21413
21414 case 'i':
21415 {
21416 ptrdiff_t size = ZV - BEGV;
21417 pint2str (decode_mode_spec_buf, field_width, size);
21418 return decode_mode_spec_buf;
21419 }
21420
21421 case 'I':
21422 {
21423 ptrdiff_t size = ZV - BEGV;
21424 pint2hrstr (decode_mode_spec_buf, field_width, size);
21425 return decode_mode_spec_buf;
21426 }
21427
21428 case 'l':
21429 {
21430 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21431 ptrdiff_t topline, nlines, height;
21432 ptrdiff_t junk;
21433
21434 /* %c and %l are ignored in `frame-title-format'. */
21435 if (mode_line_target == MODE_LINE_TITLE)
21436 return "";
21437
21438 startpos = XMARKER (w->start)->charpos;
21439 startpos_byte = marker_byte_position (w->start);
21440 height = WINDOW_TOTAL_LINES (w);
21441
21442 /* If we decided that this buffer isn't suitable for line numbers,
21443 don't forget that too fast. */
21444 if (EQ (w->base_line_pos, w->buffer))
21445 goto no_value;
21446 /* But do forget it, if the window shows a different buffer now. */
21447 else if (BUFFERP (w->base_line_pos))
21448 w->base_line_pos = Qnil;
21449
21450 /* If the buffer is very big, don't waste time. */
21451 if (INTEGERP (Vline_number_display_limit)
21452 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21453 {
21454 w->base_line_pos = Qnil;
21455 w->base_line_number = Qnil;
21456 goto no_value;
21457 }
21458
21459 if (INTEGERP (w->base_line_number)
21460 && INTEGERP (w->base_line_pos)
21461 && XFASTINT (w->base_line_pos) <= startpos)
21462 {
21463 line = XFASTINT (w->base_line_number);
21464 linepos = XFASTINT (w->base_line_pos);
21465 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21466 }
21467 else
21468 {
21469 line = 1;
21470 linepos = BUF_BEGV (b);
21471 linepos_byte = BUF_BEGV_BYTE (b);
21472 }
21473
21474 /* Count lines from base line to window start position. */
21475 nlines = display_count_lines (linepos_byte,
21476 startpos_byte,
21477 startpos, &junk);
21478
21479 topline = nlines + line;
21480
21481 /* Determine a new base line, if the old one is too close
21482 or too far away, or if we did not have one.
21483 "Too close" means it's plausible a scroll-down would
21484 go back past it. */
21485 if (startpos == BUF_BEGV (b))
21486 {
21487 w->base_line_number = make_number (topline);
21488 w->base_line_pos = make_number (BUF_BEGV (b));
21489 }
21490 else if (nlines < height + 25 || nlines > height * 3 + 50
21491 || linepos == BUF_BEGV (b))
21492 {
21493 ptrdiff_t limit = BUF_BEGV (b);
21494 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21495 ptrdiff_t position;
21496 ptrdiff_t distance =
21497 (height * 2 + 30) * line_number_display_limit_width;
21498
21499 if (startpos - distance > limit)
21500 {
21501 limit = startpos - distance;
21502 limit_byte = CHAR_TO_BYTE (limit);
21503 }
21504
21505 nlines = display_count_lines (startpos_byte,
21506 limit_byte,
21507 - (height * 2 + 30),
21508 &position);
21509 /* If we couldn't find the lines we wanted within
21510 line_number_display_limit_width chars per line,
21511 give up on line numbers for this window. */
21512 if (position == limit_byte && limit == startpos - distance)
21513 {
21514 w->base_line_pos = w->buffer;
21515 w->base_line_number = Qnil;
21516 goto no_value;
21517 }
21518
21519 w->base_line_number = make_number (topline - nlines);
21520 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21521 }
21522
21523 /* Now count lines from the start pos to point. */
21524 nlines = display_count_lines (startpos_byte,
21525 PT_BYTE, PT, &junk);
21526
21527 /* Record that we did display the line number. */
21528 line_number_displayed = 1;
21529
21530 /* Make the string to show. */
21531 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21532 return decode_mode_spec_buf;
21533 no_value:
21534 {
21535 char* p = decode_mode_spec_buf;
21536 int pad = field_width - 2;
21537 while (pad-- > 0)
21538 *p++ = ' ';
21539 *p++ = '?';
21540 *p++ = '?';
21541 *p = '\0';
21542 return decode_mode_spec_buf;
21543 }
21544 }
21545 break;
21546
21547 case 'm':
21548 obj = BVAR (b, mode_name);
21549 break;
21550
21551 case 'n':
21552 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21553 return " Narrow";
21554 break;
21555
21556 case 'p':
21557 {
21558 ptrdiff_t pos = marker_position (w->start);
21559 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21560
21561 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21562 {
21563 if (pos <= BUF_BEGV (b))
21564 return "All";
21565 else
21566 return "Bottom";
21567 }
21568 else if (pos <= BUF_BEGV (b))
21569 return "Top";
21570 else
21571 {
21572 if (total > 1000000)
21573 /* Do it differently for a large value, to avoid overflow. */
21574 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21575 else
21576 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21577 /* We can't normally display a 3-digit number,
21578 so get us a 2-digit number that is close. */
21579 if (total == 100)
21580 total = 99;
21581 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21582 return decode_mode_spec_buf;
21583 }
21584 }
21585
21586 /* Display percentage of size above the bottom of the screen. */
21587 case 'P':
21588 {
21589 ptrdiff_t toppos = marker_position (w->start);
21590 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21591 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21592
21593 if (botpos >= BUF_ZV (b))
21594 {
21595 if (toppos <= BUF_BEGV (b))
21596 return "All";
21597 else
21598 return "Bottom";
21599 }
21600 else
21601 {
21602 if (total > 1000000)
21603 /* Do it differently for a large value, to avoid overflow. */
21604 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21605 else
21606 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21607 /* We can't normally display a 3-digit number,
21608 so get us a 2-digit number that is close. */
21609 if (total == 100)
21610 total = 99;
21611 if (toppos <= BUF_BEGV (b))
21612 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21613 else
21614 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21615 return decode_mode_spec_buf;
21616 }
21617 }
21618
21619 case 's':
21620 /* status of process */
21621 obj = Fget_buffer_process (Fcurrent_buffer ());
21622 if (NILP (obj))
21623 return "no process";
21624 #ifndef MSDOS
21625 obj = Fsymbol_name (Fprocess_status (obj));
21626 #endif
21627 break;
21628
21629 case '@':
21630 {
21631 ptrdiff_t count = inhibit_garbage_collection ();
21632 Lisp_Object val = call1 (intern ("file-remote-p"),
21633 BVAR (current_buffer, directory));
21634 unbind_to (count, Qnil);
21635
21636 if (NILP (val))
21637 return "-";
21638 else
21639 return "@";
21640 }
21641
21642 case 't': /* indicate TEXT or BINARY */
21643 return "T";
21644
21645 case 'z':
21646 /* coding-system (not including end-of-line format) */
21647 case 'Z':
21648 /* coding-system (including end-of-line type) */
21649 {
21650 int eol_flag = (c == 'Z');
21651 char *p = decode_mode_spec_buf;
21652
21653 if (! FRAME_WINDOW_P (f))
21654 {
21655 /* No need to mention EOL here--the terminal never needs
21656 to do EOL conversion. */
21657 p = decode_mode_spec_coding (CODING_ID_NAME
21658 (FRAME_KEYBOARD_CODING (f)->id),
21659 p, 0);
21660 p = decode_mode_spec_coding (CODING_ID_NAME
21661 (FRAME_TERMINAL_CODING (f)->id),
21662 p, 0);
21663 }
21664 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21665 p, eol_flag);
21666
21667 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21668 #ifdef subprocesses
21669 obj = Fget_buffer_process (Fcurrent_buffer ());
21670 if (PROCESSP (obj))
21671 {
21672 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21673 p, eol_flag);
21674 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21675 p, eol_flag);
21676 }
21677 #endif /* subprocesses */
21678 #endif /* 0 */
21679 *p = 0;
21680 return decode_mode_spec_buf;
21681 }
21682 }
21683
21684 if (STRINGP (obj))
21685 {
21686 *string = obj;
21687 return SSDATA (obj);
21688 }
21689 else
21690 return "";
21691 }
21692
21693
21694 /* Count up to COUNT lines starting from START_BYTE.
21695 But don't go beyond LIMIT_BYTE.
21696 Return the number of lines thus found (always nonnegative).
21697
21698 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21699
21700 static ptrdiff_t
21701 display_count_lines (ptrdiff_t start_byte,
21702 ptrdiff_t limit_byte, ptrdiff_t count,
21703 ptrdiff_t *byte_pos_ptr)
21704 {
21705 register unsigned char *cursor;
21706 unsigned char *base;
21707
21708 register ptrdiff_t ceiling;
21709 register unsigned char *ceiling_addr;
21710 ptrdiff_t orig_count = count;
21711
21712 /* If we are not in selective display mode,
21713 check only for newlines. */
21714 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21715 && !INTEGERP (BVAR (current_buffer, selective_display)));
21716
21717 if (count > 0)
21718 {
21719 while (start_byte < limit_byte)
21720 {
21721 ceiling = BUFFER_CEILING_OF (start_byte);
21722 ceiling = min (limit_byte - 1, ceiling);
21723 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21724 base = (cursor = BYTE_POS_ADDR (start_byte));
21725 while (1)
21726 {
21727 if (selective_display)
21728 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21729 ;
21730 else
21731 while (*cursor != '\n' && ++cursor != ceiling_addr)
21732 ;
21733
21734 if (cursor != ceiling_addr)
21735 {
21736 if (--count == 0)
21737 {
21738 start_byte += cursor - base + 1;
21739 *byte_pos_ptr = start_byte;
21740 return orig_count;
21741 }
21742 else
21743 if (++cursor == ceiling_addr)
21744 break;
21745 }
21746 else
21747 break;
21748 }
21749 start_byte += cursor - base;
21750 }
21751 }
21752 else
21753 {
21754 while (start_byte > limit_byte)
21755 {
21756 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21757 ceiling = max (limit_byte, ceiling);
21758 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21759 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21760 while (1)
21761 {
21762 if (selective_display)
21763 while (--cursor != ceiling_addr
21764 && *cursor != '\n' && *cursor != 015)
21765 ;
21766 else
21767 while (--cursor != ceiling_addr && *cursor != '\n')
21768 ;
21769
21770 if (cursor != ceiling_addr)
21771 {
21772 if (++count == 0)
21773 {
21774 start_byte += cursor - base + 1;
21775 *byte_pos_ptr = start_byte;
21776 /* When scanning backwards, we should
21777 not count the newline posterior to which we stop. */
21778 return - orig_count - 1;
21779 }
21780 }
21781 else
21782 break;
21783 }
21784 /* Here we add 1 to compensate for the last decrement
21785 of CURSOR, which took it past the valid range. */
21786 start_byte += cursor - base + 1;
21787 }
21788 }
21789
21790 *byte_pos_ptr = limit_byte;
21791
21792 if (count < 0)
21793 return - orig_count + count;
21794 return orig_count - count;
21795
21796 }
21797
21798
21799 \f
21800 /***********************************************************************
21801 Displaying strings
21802 ***********************************************************************/
21803
21804 /* Display a NUL-terminated string, starting with index START.
21805
21806 If STRING is non-null, display that C string. Otherwise, the Lisp
21807 string LISP_STRING is displayed. There's a case that STRING is
21808 non-null and LISP_STRING is not nil. It means STRING is a string
21809 data of LISP_STRING. In that case, we display LISP_STRING while
21810 ignoring its text properties.
21811
21812 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21813 FACE_STRING. Display STRING or LISP_STRING with the face at
21814 FACE_STRING_POS in FACE_STRING:
21815
21816 Display the string in the environment given by IT, but use the
21817 standard display table, temporarily.
21818
21819 FIELD_WIDTH is the minimum number of output glyphs to produce.
21820 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21821 with spaces. If STRING has more characters, more than FIELD_WIDTH
21822 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21823
21824 PRECISION is the maximum number of characters to output from
21825 STRING. PRECISION < 0 means don't truncate the string.
21826
21827 This is roughly equivalent to printf format specifiers:
21828
21829 FIELD_WIDTH PRECISION PRINTF
21830 ----------------------------------------
21831 -1 -1 %s
21832 -1 10 %.10s
21833 10 -1 %10s
21834 20 10 %20.10s
21835
21836 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21837 display them, and < 0 means obey the current buffer's value of
21838 enable_multibyte_characters.
21839
21840 Value is the number of columns displayed. */
21841
21842 static int
21843 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21844 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21845 int field_width, int precision, int max_x, int multibyte)
21846 {
21847 int hpos_at_start = it->hpos;
21848 int saved_face_id = it->face_id;
21849 struct glyph_row *row = it->glyph_row;
21850 ptrdiff_t it_charpos;
21851
21852 /* Initialize the iterator IT for iteration over STRING beginning
21853 with index START. */
21854 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21855 precision, field_width, multibyte);
21856 if (string && STRINGP (lisp_string))
21857 /* LISP_STRING is the one returned by decode_mode_spec. We should
21858 ignore its text properties. */
21859 it->stop_charpos = it->end_charpos;
21860
21861 /* If displaying STRING, set up the face of the iterator from
21862 FACE_STRING, if that's given. */
21863 if (STRINGP (face_string))
21864 {
21865 ptrdiff_t endptr;
21866 struct face *face;
21867
21868 it->face_id
21869 = face_at_string_position (it->w, face_string, face_string_pos,
21870 0, it->region_beg_charpos,
21871 it->region_end_charpos,
21872 &endptr, it->base_face_id, 0);
21873 face = FACE_FROM_ID (it->f, it->face_id);
21874 it->face_box_p = face->box != FACE_NO_BOX;
21875 }
21876
21877 /* Set max_x to the maximum allowed X position. Don't let it go
21878 beyond the right edge of the window. */
21879 if (max_x <= 0)
21880 max_x = it->last_visible_x;
21881 else
21882 max_x = min (max_x, it->last_visible_x);
21883
21884 /* Skip over display elements that are not visible. because IT->w is
21885 hscrolled. */
21886 if (it->current_x < it->first_visible_x)
21887 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21888 MOVE_TO_POS | MOVE_TO_X);
21889
21890 row->ascent = it->max_ascent;
21891 row->height = it->max_ascent + it->max_descent;
21892 row->phys_ascent = it->max_phys_ascent;
21893 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21894 row->extra_line_spacing = it->max_extra_line_spacing;
21895
21896 if (STRINGP (it->string))
21897 it_charpos = IT_STRING_CHARPOS (*it);
21898 else
21899 it_charpos = IT_CHARPOS (*it);
21900
21901 /* This condition is for the case that we are called with current_x
21902 past last_visible_x. */
21903 while (it->current_x < max_x)
21904 {
21905 int x_before, x, n_glyphs_before, i, nglyphs;
21906
21907 /* Get the next display element. */
21908 if (!get_next_display_element (it))
21909 break;
21910
21911 /* Produce glyphs. */
21912 x_before = it->current_x;
21913 n_glyphs_before = row->used[TEXT_AREA];
21914 PRODUCE_GLYPHS (it);
21915
21916 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21917 i = 0;
21918 x = x_before;
21919 while (i < nglyphs)
21920 {
21921 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21922
21923 if (it->line_wrap != TRUNCATE
21924 && x + glyph->pixel_width > max_x)
21925 {
21926 /* End of continued line or max_x reached. */
21927 if (CHAR_GLYPH_PADDING_P (*glyph))
21928 {
21929 /* A wide character is unbreakable. */
21930 if (row->reversed_p)
21931 unproduce_glyphs (it, row->used[TEXT_AREA]
21932 - n_glyphs_before);
21933 row->used[TEXT_AREA] = n_glyphs_before;
21934 it->current_x = x_before;
21935 }
21936 else
21937 {
21938 if (row->reversed_p)
21939 unproduce_glyphs (it, row->used[TEXT_AREA]
21940 - (n_glyphs_before + i));
21941 row->used[TEXT_AREA] = n_glyphs_before + i;
21942 it->current_x = x;
21943 }
21944 break;
21945 }
21946 else if (x + glyph->pixel_width >= it->first_visible_x)
21947 {
21948 /* Glyph is at least partially visible. */
21949 ++it->hpos;
21950 if (x < it->first_visible_x)
21951 row->x = x - it->first_visible_x;
21952 }
21953 else
21954 {
21955 /* Glyph is off the left margin of the display area.
21956 Should not happen. */
21957 abort ();
21958 }
21959
21960 row->ascent = max (row->ascent, it->max_ascent);
21961 row->height = max (row->height, it->max_ascent + it->max_descent);
21962 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21963 row->phys_height = max (row->phys_height,
21964 it->max_phys_ascent + it->max_phys_descent);
21965 row->extra_line_spacing = max (row->extra_line_spacing,
21966 it->max_extra_line_spacing);
21967 x += glyph->pixel_width;
21968 ++i;
21969 }
21970
21971 /* Stop if max_x reached. */
21972 if (i < nglyphs)
21973 break;
21974
21975 /* Stop at line ends. */
21976 if (ITERATOR_AT_END_OF_LINE_P (it))
21977 {
21978 it->continuation_lines_width = 0;
21979 break;
21980 }
21981
21982 set_iterator_to_next (it, 1);
21983 if (STRINGP (it->string))
21984 it_charpos = IT_STRING_CHARPOS (*it);
21985 else
21986 it_charpos = IT_CHARPOS (*it);
21987
21988 /* Stop if truncating at the right edge. */
21989 if (it->line_wrap == TRUNCATE
21990 && it->current_x >= it->last_visible_x)
21991 {
21992 /* Add truncation mark, but don't do it if the line is
21993 truncated at a padding space. */
21994 if (it_charpos < it->string_nchars)
21995 {
21996 if (!FRAME_WINDOW_P (it->f))
21997 {
21998 int ii, n;
21999
22000 if (it->current_x > it->last_visible_x)
22001 {
22002 if (!row->reversed_p)
22003 {
22004 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22005 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22006 break;
22007 }
22008 else
22009 {
22010 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22011 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22012 break;
22013 unproduce_glyphs (it, ii + 1);
22014 ii = row->used[TEXT_AREA] - (ii + 1);
22015 }
22016 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22017 {
22018 row->used[TEXT_AREA] = ii;
22019 produce_special_glyphs (it, IT_TRUNCATION);
22020 }
22021 }
22022 produce_special_glyphs (it, IT_TRUNCATION);
22023 }
22024 row->truncated_on_right_p = 1;
22025 }
22026 break;
22027 }
22028 }
22029
22030 /* Maybe insert a truncation at the left. */
22031 if (it->first_visible_x
22032 && it_charpos > 0)
22033 {
22034 if (!FRAME_WINDOW_P (it->f)
22035 || (row->reversed_p
22036 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22037 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22038 insert_left_trunc_glyphs (it);
22039 row->truncated_on_left_p = 1;
22040 }
22041
22042 it->face_id = saved_face_id;
22043
22044 /* Value is number of columns displayed. */
22045 return it->hpos - hpos_at_start;
22046 }
22047
22048
22049 \f
22050 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22051 appears as an element of LIST or as the car of an element of LIST.
22052 If PROPVAL is a list, compare each element against LIST in that
22053 way, and return 1/2 if any element of PROPVAL is found in LIST.
22054 Otherwise return 0. This function cannot quit.
22055 The return value is 2 if the text is invisible but with an ellipsis
22056 and 1 if it's invisible and without an ellipsis. */
22057
22058 int
22059 invisible_p (register Lisp_Object propval, Lisp_Object list)
22060 {
22061 register Lisp_Object tail, proptail;
22062
22063 for (tail = list; CONSP (tail); tail = XCDR (tail))
22064 {
22065 register Lisp_Object tem;
22066 tem = XCAR (tail);
22067 if (EQ (propval, tem))
22068 return 1;
22069 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22070 return NILP (XCDR (tem)) ? 1 : 2;
22071 }
22072
22073 if (CONSP (propval))
22074 {
22075 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22076 {
22077 Lisp_Object propelt;
22078 propelt = XCAR (proptail);
22079 for (tail = list; CONSP (tail); tail = XCDR (tail))
22080 {
22081 register Lisp_Object tem;
22082 tem = XCAR (tail);
22083 if (EQ (propelt, tem))
22084 return 1;
22085 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22086 return NILP (XCDR (tem)) ? 1 : 2;
22087 }
22088 }
22089 }
22090
22091 return 0;
22092 }
22093
22094 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22095 doc: /* Non-nil if the property makes the text invisible.
22096 POS-OR-PROP can be a marker or number, in which case it is taken to be
22097 a position in the current buffer and the value of the `invisible' property
22098 is checked; or it can be some other value, which is then presumed to be the
22099 value of the `invisible' property of the text of interest.
22100 The non-nil value returned can be t for truly invisible text or something
22101 else if the text is replaced by an ellipsis. */)
22102 (Lisp_Object pos_or_prop)
22103 {
22104 Lisp_Object prop
22105 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22106 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22107 : pos_or_prop);
22108 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22109 return (invis == 0 ? Qnil
22110 : invis == 1 ? Qt
22111 : make_number (invis));
22112 }
22113
22114 /* Calculate a width or height in pixels from a specification using
22115 the following elements:
22116
22117 SPEC ::=
22118 NUM - a (fractional) multiple of the default font width/height
22119 (NUM) - specifies exactly NUM pixels
22120 UNIT - a fixed number of pixels, see below.
22121 ELEMENT - size of a display element in pixels, see below.
22122 (NUM . SPEC) - equals NUM * SPEC
22123 (+ SPEC SPEC ...) - add pixel values
22124 (- SPEC SPEC ...) - subtract pixel values
22125 (- SPEC) - negate pixel value
22126
22127 NUM ::=
22128 INT or FLOAT - a number constant
22129 SYMBOL - use symbol's (buffer local) variable binding.
22130
22131 UNIT ::=
22132 in - pixels per inch *)
22133 mm - pixels per 1/1000 meter *)
22134 cm - pixels per 1/100 meter *)
22135 width - width of current font in pixels.
22136 height - height of current font in pixels.
22137
22138 *) using the ratio(s) defined in display-pixels-per-inch.
22139
22140 ELEMENT ::=
22141
22142 left-fringe - left fringe width in pixels
22143 right-fringe - right fringe width in pixels
22144
22145 left-margin - left margin width in pixels
22146 right-margin - right margin width in pixels
22147
22148 scroll-bar - scroll-bar area width in pixels
22149
22150 Examples:
22151
22152 Pixels corresponding to 5 inches:
22153 (5 . in)
22154
22155 Total width of non-text areas on left side of window (if scroll-bar is on left):
22156 '(space :width (+ left-fringe left-margin scroll-bar))
22157
22158 Align to first text column (in header line):
22159 '(space :align-to 0)
22160
22161 Align to middle of text area minus half the width of variable `my-image'
22162 containing a loaded image:
22163 '(space :align-to (0.5 . (- text my-image)))
22164
22165 Width of left margin minus width of 1 character in the default font:
22166 '(space :width (- left-margin 1))
22167
22168 Width of left margin minus width of 2 characters in the current font:
22169 '(space :width (- left-margin (2 . width)))
22170
22171 Center 1 character over left-margin (in header line):
22172 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22173
22174 Different ways to express width of left fringe plus left margin minus one pixel:
22175 '(space :width (- (+ left-fringe left-margin) (1)))
22176 '(space :width (+ left-fringe left-margin (- (1))))
22177 '(space :width (+ left-fringe left-margin (-1)))
22178
22179 */
22180
22181 #define NUMVAL(X) \
22182 ((INTEGERP (X) || FLOATP (X)) \
22183 ? XFLOATINT (X) \
22184 : - 1)
22185
22186 static int
22187 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22188 struct font *font, int width_p, int *align_to)
22189 {
22190 double pixels;
22191
22192 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22193 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22194
22195 if (NILP (prop))
22196 return OK_PIXELS (0);
22197
22198 eassert (FRAME_LIVE_P (it->f));
22199
22200 if (SYMBOLP (prop))
22201 {
22202 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22203 {
22204 char *unit = SSDATA (SYMBOL_NAME (prop));
22205
22206 if (unit[0] == 'i' && unit[1] == 'n')
22207 pixels = 1.0;
22208 else if (unit[0] == 'm' && unit[1] == 'm')
22209 pixels = 25.4;
22210 else if (unit[0] == 'c' && unit[1] == 'm')
22211 pixels = 2.54;
22212 else
22213 pixels = 0;
22214 if (pixels > 0)
22215 {
22216 double ppi;
22217 #ifdef HAVE_WINDOW_SYSTEM
22218 if (FRAME_WINDOW_P (it->f)
22219 && (ppi = (width_p
22220 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22221 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22222 ppi > 0))
22223 return OK_PIXELS (ppi / pixels);
22224 #endif
22225
22226 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22227 || (CONSP (Vdisplay_pixels_per_inch)
22228 && (ppi = (width_p
22229 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22230 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22231 ppi > 0)))
22232 return OK_PIXELS (ppi / pixels);
22233
22234 return 0;
22235 }
22236 }
22237
22238 #ifdef HAVE_WINDOW_SYSTEM
22239 if (EQ (prop, Qheight))
22240 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22241 if (EQ (prop, Qwidth))
22242 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22243 #else
22244 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22245 return OK_PIXELS (1);
22246 #endif
22247
22248 if (EQ (prop, Qtext))
22249 return OK_PIXELS (width_p
22250 ? window_box_width (it->w, TEXT_AREA)
22251 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22252
22253 if (align_to && *align_to < 0)
22254 {
22255 *res = 0;
22256 if (EQ (prop, Qleft))
22257 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22258 if (EQ (prop, Qright))
22259 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22260 if (EQ (prop, Qcenter))
22261 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22262 + window_box_width (it->w, TEXT_AREA) / 2);
22263 if (EQ (prop, Qleft_fringe))
22264 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22265 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22266 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22267 if (EQ (prop, Qright_fringe))
22268 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22269 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22270 : window_box_right_offset (it->w, TEXT_AREA));
22271 if (EQ (prop, Qleft_margin))
22272 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22273 if (EQ (prop, Qright_margin))
22274 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22275 if (EQ (prop, Qscroll_bar))
22276 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22277 ? 0
22278 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22279 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22280 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22281 : 0)));
22282 }
22283 else
22284 {
22285 if (EQ (prop, Qleft_fringe))
22286 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22287 if (EQ (prop, Qright_fringe))
22288 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22289 if (EQ (prop, Qleft_margin))
22290 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22291 if (EQ (prop, Qright_margin))
22292 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22293 if (EQ (prop, Qscroll_bar))
22294 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22295 }
22296
22297 prop = buffer_local_value_1 (prop, it->w->buffer);
22298 if (EQ (prop, Qunbound))
22299 prop = Qnil;
22300 }
22301
22302 if (INTEGERP (prop) || FLOATP (prop))
22303 {
22304 int base_unit = (width_p
22305 ? FRAME_COLUMN_WIDTH (it->f)
22306 : FRAME_LINE_HEIGHT (it->f));
22307 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22308 }
22309
22310 if (CONSP (prop))
22311 {
22312 Lisp_Object car = XCAR (prop);
22313 Lisp_Object cdr = XCDR (prop);
22314
22315 if (SYMBOLP (car))
22316 {
22317 #ifdef HAVE_WINDOW_SYSTEM
22318 if (FRAME_WINDOW_P (it->f)
22319 && valid_image_p (prop))
22320 {
22321 ptrdiff_t id = lookup_image (it->f, prop);
22322 struct image *img = IMAGE_FROM_ID (it->f, id);
22323
22324 return OK_PIXELS (width_p ? img->width : img->height);
22325 }
22326 #endif
22327 if (EQ (car, Qplus) || EQ (car, Qminus))
22328 {
22329 int first = 1;
22330 double px;
22331
22332 pixels = 0;
22333 while (CONSP (cdr))
22334 {
22335 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22336 font, width_p, align_to))
22337 return 0;
22338 if (first)
22339 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22340 else
22341 pixels += px;
22342 cdr = XCDR (cdr);
22343 }
22344 if (EQ (car, Qminus))
22345 pixels = -pixels;
22346 return OK_PIXELS (pixels);
22347 }
22348
22349 car = buffer_local_value_1 (car, it->w->buffer);
22350 if (EQ (car, Qunbound))
22351 car = Qnil;
22352 }
22353
22354 if (INTEGERP (car) || FLOATP (car))
22355 {
22356 double fact;
22357 pixels = XFLOATINT (car);
22358 if (NILP (cdr))
22359 return OK_PIXELS (pixels);
22360 if (calc_pixel_width_or_height (&fact, it, cdr,
22361 font, width_p, align_to))
22362 return OK_PIXELS (pixels * fact);
22363 return 0;
22364 }
22365
22366 return 0;
22367 }
22368
22369 return 0;
22370 }
22371
22372 \f
22373 /***********************************************************************
22374 Glyph Display
22375 ***********************************************************************/
22376
22377 #ifdef HAVE_WINDOW_SYSTEM
22378
22379 #ifdef GLYPH_DEBUG
22380
22381 void
22382 dump_glyph_string (struct glyph_string *s)
22383 {
22384 fprintf (stderr, "glyph string\n");
22385 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22386 s->x, s->y, s->width, s->height);
22387 fprintf (stderr, " ybase = %d\n", s->ybase);
22388 fprintf (stderr, " hl = %d\n", s->hl);
22389 fprintf (stderr, " left overhang = %d, right = %d\n",
22390 s->left_overhang, s->right_overhang);
22391 fprintf (stderr, " nchars = %d\n", s->nchars);
22392 fprintf (stderr, " extends to end of line = %d\n",
22393 s->extends_to_end_of_line_p);
22394 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22395 fprintf (stderr, " bg width = %d\n", s->background_width);
22396 }
22397
22398 #endif /* GLYPH_DEBUG */
22399
22400 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22401 of XChar2b structures for S; it can't be allocated in
22402 init_glyph_string because it must be allocated via `alloca'. W
22403 is the window on which S is drawn. ROW and AREA are the glyph row
22404 and area within the row from which S is constructed. START is the
22405 index of the first glyph structure covered by S. HL is a
22406 face-override for drawing S. */
22407
22408 #ifdef HAVE_NTGUI
22409 #define OPTIONAL_HDC(hdc) HDC hdc,
22410 #define DECLARE_HDC(hdc) HDC hdc;
22411 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22412 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22413 #endif
22414
22415 #ifndef OPTIONAL_HDC
22416 #define OPTIONAL_HDC(hdc)
22417 #define DECLARE_HDC(hdc)
22418 #define ALLOCATE_HDC(hdc, f)
22419 #define RELEASE_HDC(hdc, f)
22420 #endif
22421
22422 static void
22423 init_glyph_string (struct glyph_string *s,
22424 OPTIONAL_HDC (hdc)
22425 XChar2b *char2b, struct window *w, struct glyph_row *row,
22426 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22427 {
22428 memset (s, 0, sizeof *s);
22429 s->w = w;
22430 s->f = XFRAME (w->frame);
22431 #ifdef HAVE_NTGUI
22432 s->hdc = hdc;
22433 #endif
22434 s->display = FRAME_X_DISPLAY (s->f);
22435 s->window = FRAME_X_WINDOW (s->f);
22436 s->char2b = char2b;
22437 s->hl = hl;
22438 s->row = row;
22439 s->area = area;
22440 s->first_glyph = row->glyphs[area] + start;
22441 s->height = row->height;
22442 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22443 s->ybase = s->y + row->ascent;
22444 }
22445
22446
22447 /* Append the list of glyph strings with head H and tail T to the list
22448 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22449
22450 static inline void
22451 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22452 struct glyph_string *h, struct glyph_string *t)
22453 {
22454 if (h)
22455 {
22456 if (*head)
22457 (*tail)->next = h;
22458 else
22459 *head = h;
22460 h->prev = *tail;
22461 *tail = t;
22462 }
22463 }
22464
22465
22466 /* Prepend the list of glyph strings with head H and tail T to the
22467 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22468 result. */
22469
22470 static inline void
22471 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22472 struct glyph_string *h, struct glyph_string *t)
22473 {
22474 if (h)
22475 {
22476 if (*head)
22477 (*head)->prev = t;
22478 else
22479 *tail = t;
22480 t->next = *head;
22481 *head = h;
22482 }
22483 }
22484
22485
22486 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22487 Set *HEAD and *TAIL to the resulting list. */
22488
22489 static inline void
22490 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22491 struct glyph_string *s)
22492 {
22493 s->next = s->prev = NULL;
22494 append_glyph_string_lists (head, tail, s, s);
22495 }
22496
22497
22498 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22499 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22500 make sure that X resources for the face returned are allocated.
22501 Value is a pointer to a realized face that is ready for display if
22502 DISPLAY_P is non-zero. */
22503
22504 static inline struct face *
22505 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22506 XChar2b *char2b, int display_p)
22507 {
22508 struct face *face = FACE_FROM_ID (f, face_id);
22509
22510 if (face->font)
22511 {
22512 unsigned code = face->font->driver->encode_char (face->font, c);
22513
22514 if (code != FONT_INVALID_CODE)
22515 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22516 else
22517 STORE_XCHAR2B (char2b, 0, 0);
22518 }
22519
22520 /* Make sure X resources of the face are allocated. */
22521 #ifdef HAVE_X_WINDOWS
22522 if (display_p)
22523 #endif
22524 {
22525 eassert (face != NULL);
22526 PREPARE_FACE_FOR_DISPLAY (f, face);
22527 }
22528
22529 return face;
22530 }
22531
22532
22533 /* Get face and two-byte form of character glyph GLYPH on frame F.
22534 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22535 a pointer to a realized face that is ready for display. */
22536
22537 static inline struct face *
22538 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22539 XChar2b *char2b, int *two_byte_p)
22540 {
22541 struct face *face;
22542
22543 eassert (glyph->type == CHAR_GLYPH);
22544 face = FACE_FROM_ID (f, glyph->face_id);
22545
22546 if (two_byte_p)
22547 *two_byte_p = 0;
22548
22549 if (face->font)
22550 {
22551 unsigned code;
22552
22553 if (CHAR_BYTE8_P (glyph->u.ch))
22554 code = CHAR_TO_BYTE8 (glyph->u.ch);
22555 else
22556 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22557
22558 if (code != FONT_INVALID_CODE)
22559 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22560 else
22561 STORE_XCHAR2B (char2b, 0, 0);
22562 }
22563
22564 /* Make sure X resources of the face are allocated. */
22565 eassert (face != NULL);
22566 PREPARE_FACE_FOR_DISPLAY (f, face);
22567 return face;
22568 }
22569
22570
22571 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22572 Return 1 if FONT has a glyph for C, otherwise return 0. */
22573
22574 static inline int
22575 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22576 {
22577 unsigned code;
22578
22579 if (CHAR_BYTE8_P (c))
22580 code = CHAR_TO_BYTE8 (c);
22581 else
22582 code = font->driver->encode_char (font, c);
22583
22584 if (code == FONT_INVALID_CODE)
22585 return 0;
22586 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22587 return 1;
22588 }
22589
22590
22591 /* Fill glyph string S with composition components specified by S->cmp.
22592
22593 BASE_FACE is the base face of the composition.
22594 S->cmp_from is the index of the first component for S.
22595
22596 OVERLAPS non-zero means S should draw the foreground only, and use
22597 its physical height for clipping. See also draw_glyphs.
22598
22599 Value is the index of a component not in S. */
22600
22601 static int
22602 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22603 int overlaps)
22604 {
22605 int i;
22606 /* For all glyphs of this composition, starting at the offset
22607 S->cmp_from, until we reach the end of the definition or encounter a
22608 glyph that requires the different face, add it to S. */
22609 struct face *face;
22610
22611 eassert (s);
22612
22613 s->for_overlaps = overlaps;
22614 s->face = NULL;
22615 s->font = NULL;
22616 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22617 {
22618 int c = COMPOSITION_GLYPH (s->cmp, i);
22619
22620 /* TAB in a composition means display glyphs with padding space
22621 on the left or right. */
22622 if (c != '\t')
22623 {
22624 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22625 -1, Qnil);
22626
22627 face = get_char_face_and_encoding (s->f, c, face_id,
22628 s->char2b + i, 1);
22629 if (face)
22630 {
22631 if (! s->face)
22632 {
22633 s->face = face;
22634 s->font = s->face->font;
22635 }
22636 else if (s->face != face)
22637 break;
22638 }
22639 }
22640 ++s->nchars;
22641 }
22642 s->cmp_to = i;
22643
22644 if (s->face == NULL)
22645 {
22646 s->face = base_face->ascii_face;
22647 s->font = s->face->font;
22648 }
22649
22650 /* All glyph strings for the same composition has the same width,
22651 i.e. the width set for the first component of the composition. */
22652 s->width = s->first_glyph->pixel_width;
22653
22654 /* If the specified font could not be loaded, use the frame's
22655 default font, but record the fact that we couldn't load it in
22656 the glyph string so that we can draw rectangles for the
22657 characters of the glyph string. */
22658 if (s->font == NULL)
22659 {
22660 s->font_not_found_p = 1;
22661 s->font = FRAME_FONT (s->f);
22662 }
22663
22664 /* Adjust base line for subscript/superscript text. */
22665 s->ybase += s->first_glyph->voffset;
22666
22667 /* This glyph string must always be drawn with 16-bit functions. */
22668 s->two_byte_p = 1;
22669
22670 return s->cmp_to;
22671 }
22672
22673 static int
22674 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22675 int start, int end, int overlaps)
22676 {
22677 struct glyph *glyph, *last;
22678 Lisp_Object lgstring;
22679 int i;
22680
22681 s->for_overlaps = overlaps;
22682 glyph = s->row->glyphs[s->area] + start;
22683 last = s->row->glyphs[s->area] + end;
22684 s->cmp_id = glyph->u.cmp.id;
22685 s->cmp_from = glyph->slice.cmp.from;
22686 s->cmp_to = glyph->slice.cmp.to + 1;
22687 s->face = FACE_FROM_ID (s->f, face_id);
22688 lgstring = composition_gstring_from_id (s->cmp_id);
22689 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22690 glyph++;
22691 while (glyph < last
22692 && glyph->u.cmp.automatic
22693 && glyph->u.cmp.id == s->cmp_id
22694 && s->cmp_to == glyph->slice.cmp.from)
22695 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22696
22697 for (i = s->cmp_from; i < s->cmp_to; i++)
22698 {
22699 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22700 unsigned code = LGLYPH_CODE (lglyph);
22701
22702 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22703 }
22704 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22705 return glyph - s->row->glyphs[s->area];
22706 }
22707
22708
22709 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22710 See the comment of fill_glyph_string for arguments.
22711 Value is the index of the first glyph not in S. */
22712
22713
22714 static int
22715 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22716 int start, int end, int overlaps)
22717 {
22718 struct glyph *glyph, *last;
22719 int voffset;
22720
22721 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22722 s->for_overlaps = overlaps;
22723 glyph = s->row->glyphs[s->area] + start;
22724 last = s->row->glyphs[s->area] + end;
22725 voffset = glyph->voffset;
22726 s->face = FACE_FROM_ID (s->f, face_id);
22727 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22728 s->nchars = 1;
22729 s->width = glyph->pixel_width;
22730 glyph++;
22731 while (glyph < last
22732 && glyph->type == GLYPHLESS_GLYPH
22733 && glyph->voffset == voffset
22734 && glyph->face_id == face_id)
22735 {
22736 s->nchars++;
22737 s->width += glyph->pixel_width;
22738 glyph++;
22739 }
22740 s->ybase += voffset;
22741 return glyph - s->row->glyphs[s->area];
22742 }
22743
22744
22745 /* Fill glyph string S from a sequence of character glyphs.
22746
22747 FACE_ID is the face id of the string. START is the index of the
22748 first glyph to consider, END is the index of the last + 1.
22749 OVERLAPS non-zero means S should draw the foreground only, and use
22750 its physical height for clipping. See also draw_glyphs.
22751
22752 Value is the index of the first glyph not in S. */
22753
22754 static int
22755 fill_glyph_string (struct glyph_string *s, int face_id,
22756 int start, int end, int overlaps)
22757 {
22758 struct glyph *glyph, *last;
22759 int voffset;
22760 int glyph_not_available_p;
22761
22762 eassert (s->f == XFRAME (s->w->frame));
22763 eassert (s->nchars == 0);
22764 eassert (start >= 0 && end > start);
22765
22766 s->for_overlaps = overlaps;
22767 glyph = s->row->glyphs[s->area] + start;
22768 last = s->row->glyphs[s->area] + end;
22769 voffset = glyph->voffset;
22770 s->padding_p = glyph->padding_p;
22771 glyph_not_available_p = glyph->glyph_not_available_p;
22772
22773 while (glyph < last
22774 && glyph->type == CHAR_GLYPH
22775 && glyph->voffset == voffset
22776 /* Same face id implies same font, nowadays. */
22777 && glyph->face_id == face_id
22778 && glyph->glyph_not_available_p == glyph_not_available_p)
22779 {
22780 int two_byte_p;
22781
22782 s->face = get_glyph_face_and_encoding (s->f, glyph,
22783 s->char2b + s->nchars,
22784 &two_byte_p);
22785 s->two_byte_p = two_byte_p;
22786 ++s->nchars;
22787 eassert (s->nchars <= end - start);
22788 s->width += glyph->pixel_width;
22789 if (glyph++->padding_p != s->padding_p)
22790 break;
22791 }
22792
22793 s->font = s->face->font;
22794
22795 /* If the specified font could not be loaded, use the frame's font,
22796 but record the fact that we couldn't load it in
22797 S->font_not_found_p so that we can draw rectangles for the
22798 characters of the glyph string. */
22799 if (s->font == NULL || glyph_not_available_p)
22800 {
22801 s->font_not_found_p = 1;
22802 s->font = FRAME_FONT (s->f);
22803 }
22804
22805 /* Adjust base line for subscript/superscript text. */
22806 s->ybase += voffset;
22807
22808 eassert (s->face && s->face->gc);
22809 return glyph - s->row->glyphs[s->area];
22810 }
22811
22812
22813 /* Fill glyph string S from image glyph S->first_glyph. */
22814
22815 static void
22816 fill_image_glyph_string (struct glyph_string *s)
22817 {
22818 eassert (s->first_glyph->type == IMAGE_GLYPH);
22819 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22820 eassert (s->img);
22821 s->slice = s->first_glyph->slice.img;
22822 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22823 s->font = s->face->font;
22824 s->width = s->first_glyph->pixel_width;
22825
22826 /* Adjust base line for subscript/superscript text. */
22827 s->ybase += s->first_glyph->voffset;
22828 }
22829
22830
22831 /* Fill glyph string S from a sequence of stretch glyphs.
22832
22833 START is the index of the first glyph to consider,
22834 END is the index of the last + 1.
22835
22836 Value is the index of the first glyph not in S. */
22837
22838 static int
22839 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22840 {
22841 struct glyph *glyph, *last;
22842 int voffset, face_id;
22843
22844 eassert (s->first_glyph->type == STRETCH_GLYPH);
22845
22846 glyph = s->row->glyphs[s->area] + start;
22847 last = s->row->glyphs[s->area] + end;
22848 face_id = glyph->face_id;
22849 s->face = FACE_FROM_ID (s->f, face_id);
22850 s->font = s->face->font;
22851 s->width = glyph->pixel_width;
22852 s->nchars = 1;
22853 voffset = glyph->voffset;
22854
22855 for (++glyph;
22856 (glyph < last
22857 && glyph->type == STRETCH_GLYPH
22858 && glyph->voffset == voffset
22859 && glyph->face_id == face_id);
22860 ++glyph)
22861 s->width += glyph->pixel_width;
22862
22863 /* Adjust base line for subscript/superscript text. */
22864 s->ybase += voffset;
22865
22866 /* The case that face->gc == 0 is handled when drawing the glyph
22867 string by calling PREPARE_FACE_FOR_DISPLAY. */
22868 eassert (s->face);
22869 return glyph - s->row->glyphs[s->area];
22870 }
22871
22872 static struct font_metrics *
22873 get_per_char_metric (struct font *font, XChar2b *char2b)
22874 {
22875 static struct font_metrics metrics;
22876 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22877
22878 if (! font || code == FONT_INVALID_CODE)
22879 return NULL;
22880 font->driver->text_extents (font, &code, 1, &metrics);
22881 return &metrics;
22882 }
22883
22884 /* EXPORT for RIF:
22885 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22886 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22887 assumed to be zero. */
22888
22889 void
22890 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22891 {
22892 *left = *right = 0;
22893
22894 if (glyph->type == CHAR_GLYPH)
22895 {
22896 struct face *face;
22897 XChar2b char2b;
22898 struct font_metrics *pcm;
22899
22900 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22901 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22902 {
22903 if (pcm->rbearing > pcm->width)
22904 *right = pcm->rbearing - pcm->width;
22905 if (pcm->lbearing < 0)
22906 *left = -pcm->lbearing;
22907 }
22908 }
22909 else if (glyph->type == COMPOSITE_GLYPH)
22910 {
22911 if (! glyph->u.cmp.automatic)
22912 {
22913 struct composition *cmp = composition_table[glyph->u.cmp.id];
22914
22915 if (cmp->rbearing > cmp->pixel_width)
22916 *right = cmp->rbearing - cmp->pixel_width;
22917 if (cmp->lbearing < 0)
22918 *left = - cmp->lbearing;
22919 }
22920 else
22921 {
22922 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22923 struct font_metrics metrics;
22924
22925 composition_gstring_width (gstring, glyph->slice.cmp.from,
22926 glyph->slice.cmp.to + 1, &metrics);
22927 if (metrics.rbearing > metrics.width)
22928 *right = metrics.rbearing - metrics.width;
22929 if (metrics.lbearing < 0)
22930 *left = - metrics.lbearing;
22931 }
22932 }
22933 }
22934
22935
22936 /* Return the index of the first glyph preceding glyph string S that
22937 is overwritten by S because of S's left overhang. Value is -1
22938 if no glyphs are overwritten. */
22939
22940 static int
22941 left_overwritten (struct glyph_string *s)
22942 {
22943 int k;
22944
22945 if (s->left_overhang)
22946 {
22947 int x = 0, i;
22948 struct glyph *glyphs = s->row->glyphs[s->area];
22949 int first = s->first_glyph - glyphs;
22950
22951 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22952 x -= glyphs[i].pixel_width;
22953
22954 k = i + 1;
22955 }
22956 else
22957 k = -1;
22958
22959 return k;
22960 }
22961
22962
22963 /* Return the index of the first glyph preceding glyph string S that
22964 is overwriting S because of its right overhang. Value is -1 if no
22965 glyph in front of S overwrites S. */
22966
22967 static int
22968 left_overwriting (struct glyph_string *s)
22969 {
22970 int i, k, x;
22971 struct glyph *glyphs = s->row->glyphs[s->area];
22972 int first = s->first_glyph - glyphs;
22973
22974 k = -1;
22975 x = 0;
22976 for (i = first - 1; i >= 0; --i)
22977 {
22978 int left, right;
22979 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22980 if (x + right > 0)
22981 k = i;
22982 x -= glyphs[i].pixel_width;
22983 }
22984
22985 return k;
22986 }
22987
22988
22989 /* Return the index of the last glyph following glyph string S that is
22990 overwritten by S because of S's right overhang. Value is -1 if
22991 no such glyph is found. */
22992
22993 static int
22994 right_overwritten (struct glyph_string *s)
22995 {
22996 int k = -1;
22997
22998 if (s->right_overhang)
22999 {
23000 int x = 0, i;
23001 struct glyph *glyphs = s->row->glyphs[s->area];
23002 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23003 int end = s->row->used[s->area];
23004
23005 for (i = first; i < end && s->right_overhang > x; ++i)
23006 x += glyphs[i].pixel_width;
23007
23008 k = i;
23009 }
23010
23011 return k;
23012 }
23013
23014
23015 /* Return the index of the last glyph following glyph string S that
23016 overwrites S because of its left overhang. Value is negative
23017 if no such glyph is found. */
23018
23019 static int
23020 right_overwriting (struct glyph_string *s)
23021 {
23022 int i, k, x;
23023 int end = s->row->used[s->area];
23024 struct glyph *glyphs = s->row->glyphs[s->area];
23025 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23026
23027 k = -1;
23028 x = 0;
23029 for (i = first; i < end; ++i)
23030 {
23031 int left, right;
23032 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23033 if (x - left < 0)
23034 k = i;
23035 x += glyphs[i].pixel_width;
23036 }
23037
23038 return k;
23039 }
23040
23041
23042 /* Set background width of glyph string S. START is the index of the
23043 first glyph following S. LAST_X is the right-most x-position + 1
23044 in the drawing area. */
23045
23046 static inline void
23047 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23048 {
23049 /* If the face of this glyph string has to be drawn to the end of
23050 the drawing area, set S->extends_to_end_of_line_p. */
23051
23052 if (start == s->row->used[s->area]
23053 && s->area == TEXT_AREA
23054 && ((s->row->fill_line_p
23055 && (s->hl == DRAW_NORMAL_TEXT
23056 || s->hl == DRAW_IMAGE_RAISED
23057 || s->hl == DRAW_IMAGE_SUNKEN))
23058 || s->hl == DRAW_MOUSE_FACE))
23059 s->extends_to_end_of_line_p = 1;
23060
23061 /* If S extends its face to the end of the line, set its
23062 background_width to the distance to the right edge of the drawing
23063 area. */
23064 if (s->extends_to_end_of_line_p)
23065 s->background_width = last_x - s->x + 1;
23066 else
23067 s->background_width = s->width;
23068 }
23069
23070
23071 /* Compute overhangs and x-positions for glyph string S and its
23072 predecessors, or successors. X is the starting x-position for S.
23073 BACKWARD_P non-zero means process predecessors. */
23074
23075 static void
23076 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23077 {
23078 if (backward_p)
23079 {
23080 while (s)
23081 {
23082 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23083 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23084 x -= s->width;
23085 s->x = x;
23086 s = s->prev;
23087 }
23088 }
23089 else
23090 {
23091 while (s)
23092 {
23093 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23094 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23095 s->x = x;
23096 x += s->width;
23097 s = s->next;
23098 }
23099 }
23100 }
23101
23102
23103
23104 /* The following macros are only called from draw_glyphs below.
23105 They reference the following parameters of that function directly:
23106 `w', `row', `area', and `overlap_p'
23107 as well as the following local variables:
23108 `s', `f', and `hdc' (in W32) */
23109
23110 #ifdef HAVE_NTGUI
23111 /* On W32, silently add local `hdc' variable to argument list of
23112 init_glyph_string. */
23113 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23114 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23115 #else
23116 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23117 init_glyph_string (s, char2b, w, row, area, start, hl)
23118 #endif
23119
23120 /* Add a glyph string for a stretch glyph to the list of strings
23121 between HEAD and TAIL. START is the index of the stretch glyph in
23122 row area AREA of glyph row ROW. END is the index of the last glyph
23123 in that glyph row area. X is the current output position assigned
23124 to the new glyph string constructed. HL overrides that face of the
23125 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23126 is the right-most x-position of the drawing area. */
23127
23128 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23129 and below -- keep them on one line. */
23130 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23131 do \
23132 { \
23133 s = alloca (sizeof *s); \
23134 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23135 START = fill_stretch_glyph_string (s, START, END); \
23136 append_glyph_string (&HEAD, &TAIL, s); \
23137 s->x = (X); \
23138 } \
23139 while (0)
23140
23141
23142 /* Add a glyph string for an image glyph to the list of strings
23143 between HEAD and TAIL. START is the index of the image glyph in
23144 row area AREA of glyph row ROW. END is the index of the last glyph
23145 in that glyph row area. X is the current output position assigned
23146 to the new glyph string constructed. HL overrides that face of the
23147 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23148 is the right-most x-position of the drawing area. */
23149
23150 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23151 do \
23152 { \
23153 s = alloca (sizeof *s); \
23154 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23155 fill_image_glyph_string (s); \
23156 append_glyph_string (&HEAD, &TAIL, s); \
23157 ++START; \
23158 s->x = (X); \
23159 } \
23160 while (0)
23161
23162
23163 /* Add a glyph string for a sequence of character glyphs to the list
23164 of strings between HEAD and TAIL. START is the index of the first
23165 glyph in row area AREA of glyph row ROW that is part of the new
23166 glyph string. END is the index of the last glyph in that glyph row
23167 area. X is the current output position assigned to the new glyph
23168 string constructed. HL overrides that face of the glyph; e.g. it
23169 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23170 right-most x-position of the drawing area. */
23171
23172 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23173 do \
23174 { \
23175 int face_id; \
23176 XChar2b *char2b; \
23177 \
23178 face_id = (row)->glyphs[area][START].face_id; \
23179 \
23180 s = alloca (sizeof *s); \
23181 char2b = alloca ((END - START) * sizeof *char2b); \
23182 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23183 append_glyph_string (&HEAD, &TAIL, s); \
23184 s->x = (X); \
23185 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23186 } \
23187 while (0)
23188
23189
23190 /* Add a glyph string for a composite sequence to the list of strings
23191 between HEAD and TAIL. START is the index of the first glyph in
23192 row area AREA of glyph row ROW that is part of the new glyph
23193 string. END is the index of the last glyph in that glyph row area.
23194 X is the current output position assigned to the new glyph string
23195 constructed. HL overrides that face of the glyph; e.g. it is
23196 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23197 x-position of the drawing area. */
23198
23199 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23200 do { \
23201 int face_id = (row)->glyphs[area][START].face_id; \
23202 struct face *base_face = FACE_FROM_ID (f, face_id); \
23203 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23204 struct composition *cmp = composition_table[cmp_id]; \
23205 XChar2b *char2b; \
23206 struct glyph_string *first_s = NULL; \
23207 int n; \
23208 \
23209 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23210 \
23211 /* Make glyph_strings for each glyph sequence that is drawable by \
23212 the same face, and append them to HEAD/TAIL. */ \
23213 for (n = 0; n < cmp->glyph_len;) \
23214 { \
23215 s = alloca (sizeof *s); \
23216 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23217 append_glyph_string (&(HEAD), &(TAIL), s); \
23218 s->cmp = cmp; \
23219 s->cmp_from = n; \
23220 s->x = (X); \
23221 if (n == 0) \
23222 first_s = s; \
23223 n = fill_composite_glyph_string (s, base_face, overlaps); \
23224 } \
23225 \
23226 ++START; \
23227 s = first_s; \
23228 } while (0)
23229
23230
23231 /* Add a glyph string for a glyph-string sequence to the list of strings
23232 between HEAD and TAIL. */
23233
23234 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23235 do { \
23236 int face_id; \
23237 XChar2b *char2b; \
23238 Lisp_Object gstring; \
23239 \
23240 face_id = (row)->glyphs[area][START].face_id; \
23241 gstring = (composition_gstring_from_id \
23242 ((row)->glyphs[area][START].u.cmp.id)); \
23243 s = alloca (sizeof *s); \
23244 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23245 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23246 append_glyph_string (&(HEAD), &(TAIL), s); \
23247 s->x = (X); \
23248 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23249 } while (0)
23250
23251
23252 /* Add a glyph string for a sequence of glyphless character's glyphs
23253 to the list of strings between HEAD and TAIL. The meanings of
23254 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23255
23256 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23257 do \
23258 { \
23259 int face_id; \
23260 \
23261 face_id = (row)->glyphs[area][START].face_id; \
23262 \
23263 s = alloca (sizeof *s); \
23264 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23265 append_glyph_string (&HEAD, &TAIL, s); \
23266 s->x = (X); \
23267 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23268 overlaps); \
23269 } \
23270 while (0)
23271
23272
23273 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23274 of AREA of glyph row ROW on window W between indices START and END.
23275 HL overrides the face for drawing glyph strings, e.g. it is
23276 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23277 x-positions of the drawing area.
23278
23279 This is an ugly monster macro construct because we must use alloca
23280 to allocate glyph strings (because draw_glyphs can be called
23281 asynchronously). */
23282
23283 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23284 do \
23285 { \
23286 HEAD = TAIL = NULL; \
23287 while (START < END) \
23288 { \
23289 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23290 switch (first_glyph->type) \
23291 { \
23292 case CHAR_GLYPH: \
23293 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23294 HL, X, LAST_X); \
23295 break; \
23296 \
23297 case COMPOSITE_GLYPH: \
23298 if (first_glyph->u.cmp.automatic) \
23299 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23300 HL, X, LAST_X); \
23301 else \
23302 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23303 HL, X, LAST_X); \
23304 break; \
23305 \
23306 case STRETCH_GLYPH: \
23307 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23308 HL, X, LAST_X); \
23309 break; \
23310 \
23311 case IMAGE_GLYPH: \
23312 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23313 HL, X, LAST_X); \
23314 break; \
23315 \
23316 case GLYPHLESS_GLYPH: \
23317 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23318 HL, X, LAST_X); \
23319 break; \
23320 \
23321 default: \
23322 abort (); \
23323 } \
23324 \
23325 if (s) \
23326 { \
23327 set_glyph_string_background_width (s, START, LAST_X); \
23328 (X) += s->width; \
23329 } \
23330 } \
23331 } while (0)
23332
23333
23334 /* Draw glyphs between START and END in AREA of ROW on window W,
23335 starting at x-position X. X is relative to AREA in W. HL is a
23336 face-override with the following meaning:
23337
23338 DRAW_NORMAL_TEXT draw normally
23339 DRAW_CURSOR draw in cursor face
23340 DRAW_MOUSE_FACE draw in mouse face.
23341 DRAW_INVERSE_VIDEO draw in mode line face
23342 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23343 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23344
23345 If OVERLAPS is non-zero, draw only the foreground of characters and
23346 clip to the physical height of ROW. Non-zero value also defines
23347 the overlapping part to be drawn:
23348
23349 OVERLAPS_PRED overlap with preceding rows
23350 OVERLAPS_SUCC overlap with succeeding rows
23351 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23352 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23353
23354 Value is the x-position reached, relative to AREA of W. */
23355
23356 static int
23357 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23358 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23359 enum draw_glyphs_face hl, int overlaps)
23360 {
23361 struct glyph_string *head, *tail;
23362 struct glyph_string *s;
23363 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23364 int i, j, x_reached, last_x, area_left = 0;
23365 struct frame *f = XFRAME (WINDOW_FRAME (w));
23366 DECLARE_HDC (hdc);
23367
23368 ALLOCATE_HDC (hdc, f);
23369
23370 /* Let's rather be paranoid than getting a SEGV. */
23371 end = min (end, row->used[area]);
23372 start = max (0, start);
23373 start = min (end, start);
23374
23375 /* Translate X to frame coordinates. Set last_x to the right
23376 end of the drawing area. */
23377 if (row->full_width_p)
23378 {
23379 /* X is relative to the left edge of W, without scroll bars
23380 or fringes. */
23381 area_left = WINDOW_LEFT_EDGE_X (w);
23382 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23383 }
23384 else
23385 {
23386 area_left = window_box_left (w, area);
23387 last_x = area_left + window_box_width (w, area);
23388 }
23389 x += area_left;
23390
23391 /* Build a doubly-linked list of glyph_string structures between
23392 head and tail from what we have to draw. Note that the macro
23393 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23394 the reason we use a separate variable `i'. */
23395 i = start;
23396 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23397 if (tail)
23398 x_reached = tail->x + tail->background_width;
23399 else
23400 x_reached = x;
23401
23402 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23403 the row, redraw some glyphs in front or following the glyph
23404 strings built above. */
23405 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23406 {
23407 struct glyph_string *h, *t;
23408 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23409 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23410 int check_mouse_face = 0;
23411 int dummy_x = 0;
23412
23413 /* If mouse highlighting is on, we may need to draw adjacent
23414 glyphs using mouse-face highlighting. */
23415 if (area == TEXT_AREA && row->mouse_face_p)
23416 {
23417 struct glyph_row *mouse_beg_row, *mouse_end_row;
23418
23419 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23420 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23421
23422 if (row >= mouse_beg_row && row <= mouse_end_row)
23423 {
23424 check_mouse_face = 1;
23425 mouse_beg_col = (row == mouse_beg_row)
23426 ? hlinfo->mouse_face_beg_col : 0;
23427 mouse_end_col = (row == mouse_end_row)
23428 ? hlinfo->mouse_face_end_col
23429 : row->used[TEXT_AREA];
23430 }
23431 }
23432
23433 /* Compute overhangs for all glyph strings. */
23434 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23435 for (s = head; s; s = s->next)
23436 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23437
23438 /* Prepend glyph strings for glyphs in front of the first glyph
23439 string that are overwritten because of the first glyph
23440 string's left overhang. The background of all strings
23441 prepended must be drawn because the first glyph string
23442 draws over it. */
23443 i = left_overwritten (head);
23444 if (i >= 0)
23445 {
23446 enum draw_glyphs_face overlap_hl;
23447
23448 /* If this row contains mouse highlighting, attempt to draw
23449 the overlapped glyphs with the correct highlight. This
23450 code fails if the overlap encompasses more than one glyph
23451 and mouse-highlight spans only some of these glyphs.
23452 However, making it work perfectly involves a lot more
23453 code, and I don't know if the pathological case occurs in
23454 practice, so we'll stick to this for now. --- cyd */
23455 if (check_mouse_face
23456 && mouse_beg_col < start && mouse_end_col > i)
23457 overlap_hl = DRAW_MOUSE_FACE;
23458 else
23459 overlap_hl = DRAW_NORMAL_TEXT;
23460
23461 j = i;
23462 BUILD_GLYPH_STRINGS (j, start, h, t,
23463 overlap_hl, dummy_x, last_x);
23464 start = i;
23465 compute_overhangs_and_x (t, head->x, 1);
23466 prepend_glyph_string_lists (&head, &tail, h, t);
23467 clip_head = head;
23468 }
23469
23470 /* Prepend glyph strings for glyphs in front of the first glyph
23471 string that overwrite that glyph string because of their
23472 right overhang. For these strings, only the foreground must
23473 be drawn, because it draws over the glyph string at `head'.
23474 The background must not be drawn because this would overwrite
23475 right overhangs of preceding glyphs for which no glyph
23476 strings exist. */
23477 i = left_overwriting (head);
23478 if (i >= 0)
23479 {
23480 enum draw_glyphs_face overlap_hl;
23481
23482 if (check_mouse_face
23483 && mouse_beg_col < start && mouse_end_col > i)
23484 overlap_hl = DRAW_MOUSE_FACE;
23485 else
23486 overlap_hl = DRAW_NORMAL_TEXT;
23487
23488 clip_head = head;
23489 BUILD_GLYPH_STRINGS (i, start, h, t,
23490 overlap_hl, dummy_x, last_x);
23491 for (s = h; s; s = s->next)
23492 s->background_filled_p = 1;
23493 compute_overhangs_and_x (t, head->x, 1);
23494 prepend_glyph_string_lists (&head, &tail, h, t);
23495 }
23496
23497 /* Append glyphs strings for glyphs following the last glyph
23498 string tail that are overwritten by tail. The background of
23499 these strings has to be drawn because tail's foreground draws
23500 over it. */
23501 i = right_overwritten (tail);
23502 if (i >= 0)
23503 {
23504 enum draw_glyphs_face overlap_hl;
23505
23506 if (check_mouse_face
23507 && mouse_beg_col < i && mouse_end_col > end)
23508 overlap_hl = DRAW_MOUSE_FACE;
23509 else
23510 overlap_hl = DRAW_NORMAL_TEXT;
23511
23512 BUILD_GLYPH_STRINGS (end, i, h, t,
23513 overlap_hl, x, last_x);
23514 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23515 we don't have `end = i;' here. */
23516 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23517 append_glyph_string_lists (&head, &tail, h, t);
23518 clip_tail = tail;
23519 }
23520
23521 /* Append glyph strings for glyphs following the last glyph
23522 string tail that overwrite tail. The foreground of such
23523 glyphs has to be drawn because it writes into the background
23524 of tail. The background must not be drawn because it could
23525 paint over the foreground of following glyphs. */
23526 i = right_overwriting (tail);
23527 if (i >= 0)
23528 {
23529 enum draw_glyphs_face overlap_hl;
23530 if (check_mouse_face
23531 && mouse_beg_col < i && mouse_end_col > end)
23532 overlap_hl = DRAW_MOUSE_FACE;
23533 else
23534 overlap_hl = DRAW_NORMAL_TEXT;
23535
23536 clip_tail = tail;
23537 i++; /* We must include the Ith glyph. */
23538 BUILD_GLYPH_STRINGS (end, i, h, t,
23539 overlap_hl, x, last_x);
23540 for (s = h; s; s = s->next)
23541 s->background_filled_p = 1;
23542 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23543 append_glyph_string_lists (&head, &tail, h, t);
23544 }
23545 if (clip_head || clip_tail)
23546 for (s = head; s; s = s->next)
23547 {
23548 s->clip_head = clip_head;
23549 s->clip_tail = clip_tail;
23550 }
23551 }
23552
23553 /* Draw all strings. */
23554 for (s = head; s; s = s->next)
23555 FRAME_RIF (f)->draw_glyph_string (s);
23556
23557 #ifndef HAVE_NS
23558 /* When focus a sole frame and move horizontally, this sets on_p to 0
23559 causing a failure to erase prev cursor position. */
23560 if (area == TEXT_AREA
23561 && !row->full_width_p
23562 /* When drawing overlapping rows, only the glyph strings'
23563 foreground is drawn, which doesn't erase a cursor
23564 completely. */
23565 && !overlaps)
23566 {
23567 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23568 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23569 : (tail ? tail->x + tail->background_width : x));
23570 x0 -= area_left;
23571 x1 -= area_left;
23572
23573 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23574 row->y, MATRIX_ROW_BOTTOM_Y (row));
23575 }
23576 #endif
23577
23578 /* Value is the x-position up to which drawn, relative to AREA of W.
23579 This doesn't include parts drawn because of overhangs. */
23580 if (row->full_width_p)
23581 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23582 else
23583 x_reached -= area_left;
23584
23585 RELEASE_HDC (hdc, f);
23586
23587 return x_reached;
23588 }
23589
23590 /* Expand row matrix if too narrow. Don't expand if area
23591 is not present. */
23592
23593 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23594 { \
23595 if (!fonts_changed_p \
23596 && (it->glyph_row->glyphs[area] \
23597 < it->glyph_row->glyphs[area + 1])) \
23598 { \
23599 it->w->ncols_scale_factor++; \
23600 fonts_changed_p = 1; \
23601 } \
23602 }
23603
23604 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23605 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23606
23607 static inline void
23608 append_glyph (struct it *it)
23609 {
23610 struct glyph *glyph;
23611 enum glyph_row_area area = it->area;
23612
23613 eassert (it->glyph_row);
23614 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23615
23616 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23617 if (glyph < it->glyph_row->glyphs[area + 1])
23618 {
23619 /* If the glyph row is reversed, we need to prepend the glyph
23620 rather than append it. */
23621 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23622 {
23623 struct glyph *g;
23624
23625 /* Make room for the additional glyph. */
23626 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23627 g[1] = *g;
23628 glyph = it->glyph_row->glyphs[area];
23629 }
23630 glyph->charpos = CHARPOS (it->position);
23631 glyph->object = it->object;
23632 if (it->pixel_width > 0)
23633 {
23634 glyph->pixel_width = it->pixel_width;
23635 glyph->padding_p = 0;
23636 }
23637 else
23638 {
23639 /* Assure at least 1-pixel width. Otherwise, cursor can't
23640 be displayed correctly. */
23641 glyph->pixel_width = 1;
23642 glyph->padding_p = 1;
23643 }
23644 glyph->ascent = it->ascent;
23645 glyph->descent = it->descent;
23646 glyph->voffset = it->voffset;
23647 glyph->type = CHAR_GLYPH;
23648 glyph->avoid_cursor_p = it->avoid_cursor_p;
23649 glyph->multibyte_p = it->multibyte_p;
23650 glyph->left_box_line_p = it->start_of_box_run_p;
23651 glyph->right_box_line_p = it->end_of_box_run_p;
23652 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23653 || it->phys_descent > it->descent);
23654 glyph->glyph_not_available_p = it->glyph_not_available_p;
23655 glyph->face_id = it->face_id;
23656 glyph->u.ch = it->char_to_display;
23657 glyph->slice.img = null_glyph_slice;
23658 glyph->font_type = FONT_TYPE_UNKNOWN;
23659 if (it->bidi_p)
23660 {
23661 glyph->resolved_level = it->bidi_it.resolved_level;
23662 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23663 abort ();
23664 glyph->bidi_type = it->bidi_it.type;
23665 }
23666 else
23667 {
23668 glyph->resolved_level = 0;
23669 glyph->bidi_type = UNKNOWN_BT;
23670 }
23671 ++it->glyph_row->used[area];
23672 }
23673 else
23674 IT_EXPAND_MATRIX_WIDTH (it, area);
23675 }
23676
23677 /* Store one glyph for the composition IT->cmp_it.id in
23678 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23679 non-null. */
23680
23681 static inline void
23682 append_composite_glyph (struct it *it)
23683 {
23684 struct glyph *glyph;
23685 enum glyph_row_area area = it->area;
23686
23687 eassert (it->glyph_row);
23688
23689 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23690 if (glyph < it->glyph_row->glyphs[area + 1])
23691 {
23692 /* If the glyph row is reversed, we need to prepend the glyph
23693 rather than append it. */
23694 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23695 {
23696 struct glyph *g;
23697
23698 /* Make room for the new glyph. */
23699 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23700 g[1] = *g;
23701 glyph = it->glyph_row->glyphs[it->area];
23702 }
23703 glyph->charpos = it->cmp_it.charpos;
23704 glyph->object = it->object;
23705 glyph->pixel_width = it->pixel_width;
23706 glyph->ascent = it->ascent;
23707 glyph->descent = it->descent;
23708 glyph->voffset = it->voffset;
23709 glyph->type = COMPOSITE_GLYPH;
23710 if (it->cmp_it.ch < 0)
23711 {
23712 glyph->u.cmp.automatic = 0;
23713 glyph->u.cmp.id = it->cmp_it.id;
23714 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23715 }
23716 else
23717 {
23718 glyph->u.cmp.automatic = 1;
23719 glyph->u.cmp.id = it->cmp_it.id;
23720 glyph->slice.cmp.from = it->cmp_it.from;
23721 glyph->slice.cmp.to = it->cmp_it.to - 1;
23722 }
23723 glyph->avoid_cursor_p = it->avoid_cursor_p;
23724 glyph->multibyte_p = it->multibyte_p;
23725 glyph->left_box_line_p = it->start_of_box_run_p;
23726 glyph->right_box_line_p = it->end_of_box_run_p;
23727 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23728 || it->phys_descent > it->descent);
23729 glyph->padding_p = 0;
23730 glyph->glyph_not_available_p = 0;
23731 glyph->face_id = it->face_id;
23732 glyph->font_type = FONT_TYPE_UNKNOWN;
23733 if (it->bidi_p)
23734 {
23735 glyph->resolved_level = it->bidi_it.resolved_level;
23736 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23737 abort ();
23738 glyph->bidi_type = it->bidi_it.type;
23739 }
23740 ++it->glyph_row->used[area];
23741 }
23742 else
23743 IT_EXPAND_MATRIX_WIDTH (it, area);
23744 }
23745
23746
23747 /* Change IT->ascent and IT->height according to the setting of
23748 IT->voffset. */
23749
23750 static inline void
23751 take_vertical_position_into_account (struct it *it)
23752 {
23753 if (it->voffset)
23754 {
23755 if (it->voffset < 0)
23756 /* Increase the ascent so that we can display the text higher
23757 in the line. */
23758 it->ascent -= it->voffset;
23759 else
23760 /* Increase the descent so that we can display the text lower
23761 in the line. */
23762 it->descent += it->voffset;
23763 }
23764 }
23765
23766
23767 /* Produce glyphs/get display metrics for the image IT is loaded with.
23768 See the description of struct display_iterator in dispextern.h for
23769 an overview of struct display_iterator. */
23770
23771 static void
23772 produce_image_glyph (struct it *it)
23773 {
23774 struct image *img;
23775 struct face *face;
23776 int glyph_ascent, crop;
23777 struct glyph_slice slice;
23778
23779 eassert (it->what == IT_IMAGE);
23780
23781 face = FACE_FROM_ID (it->f, it->face_id);
23782 eassert (face);
23783 /* Make sure X resources of the face is loaded. */
23784 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23785
23786 if (it->image_id < 0)
23787 {
23788 /* Fringe bitmap. */
23789 it->ascent = it->phys_ascent = 0;
23790 it->descent = it->phys_descent = 0;
23791 it->pixel_width = 0;
23792 it->nglyphs = 0;
23793 return;
23794 }
23795
23796 img = IMAGE_FROM_ID (it->f, it->image_id);
23797 eassert (img);
23798 /* Make sure X resources of the image is loaded. */
23799 prepare_image_for_display (it->f, img);
23800
23801 slice.x = slice.y = 0;
23802 slice.width = img->width;
23803 slice.height = img->height;
23804
23805 if (INTEGERP (it->slice.x))
23806 slice.x = XINT (it->slice.x);
23807 else if (FLOATP (it->slice.x))
23808 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23809
23810 if (INTEGERP (it->slice.y))
23811 slice.y = XINT (it->slice.y);
23812 else if (FLOATP (it->slice.y))
23813 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23814
23815 if (INTEGERP (it->slice.width))
23816 slice.width = XINT (it->slice.width);
23817 else if (FLOATP (it->slice.width))
23818 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23819
23820 if (INTEGERP (it->slice.height))
23821 slice.height = XINT (it->slice.height);
23822 else if (FLOATP (it->slice.height))
23823 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23824
23825 if (slice.x >= img->width)
23826 slice.x = img->width;
23827 if (slice.y >= img->height)
23828 slice.y = img->height;
23829 if (slice.x + slice.width >= img->width)
23830 slice.width = img->width - slice.x;
23831 if (slice.y + slice.height > img->height)
23832 slice.height = img->height - slice.y;
23833
23834 if (slice.width == 0 || slice.height == 0)
23835 return;
23836
23837 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23838
23839 it->descent = slice.height - glyph_ascent;
23840 if (slice.y == 0)
23841 it->descent += img->vmargin;
23842 if (slice.y + slice.height == img->height)
23843 it->descent += img->vmargin;
23844 it->phys_descent = it->descent;
23845
23846 it->pixel_width = slice.width;
23847 if (slice.x == 0)
23848 it->pixel_width += img->hmargin;
23849 if (slice.x + slice.width == img->width)
23850 it->pixel_width += img->hmargin;
23851
23852 /* It's quite possible for images to have an ascent greater than
23853 their height, so don't get confused in that case. */
23854 if (it->descent < 0)
23855 it->descent = 0;
23856
23857 it->nglyphs = 1;
23858
23859 if (face->box != FACE_NO_BOX)
23860 {
23861 if (face->box_line_width > 0)
23862 {
23863 if (slice.y == 0)
23864 it->ascent += face->box_line_width;
23865 if (slice.y + slice.height == img->height)
23866 it->descent += face->box_line_width;
23867 }
23868
23869 if (it->start_of_box_run_p && slice.x == 0)
23870 it->pixel_width += eabs (face->box_line_width);
23871 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23872 it->pixel_width += eabs (face->box_line_width);
23873 }
23874
23875 take_vertical_position_into_account (it);
23876
23877 /* Automatically crop wide image glyphs at right edge so we can
23878 draw the cursor on same display row. */
23879 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23880 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23881 {
23882 it->pixel_width -= crop;
23883 slice.width -= crop;
23884 }
23885
23886 if (it->glyph_row)
23887 {
23888 struct glyph *glyph;
23889 enum glyph_row_area area = it->area;
23890
23891 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23892 if (glyph < it->glyph_row->glyphs[area + 1])
23893 {
23894 glyph->charpos = CHARPOS (it->position);
23895 glyph->object = it->object;
23896 glyph->pixel_width = it->pixel_width;
23897 glyph->ascent = glyph_ascent;
23898 glyph->descent = it->descent;
23899 glyph->voffset = it->voffset;
23900 glyph->type = IMAGE_GLYPH;
23901 glyph->avoid_cursor_p = it->avoid_cursor_p;
23902 glyph->multibyte_p = it->multibyte_p;
23903 glyph->left_box_line_p = it->start_of_box_run_p;
23904 glyph->right_box_line_p = it->end_of_box_run_p;
23905 glyph->overlaps_vertically_p = 0;
23906 glyph->padding_p = 0;
23907 glyph->glyph_not_available_p = 0;
23908 glyph->face_id = it->face_id;
23909 glyph->u.img_id = img->id;
23910 glyph->slice.img = slice;
23911 glyph->font_type = FONT_TYPE_UNKNOWN;
23912 if (it->bidi_p)
23913 {
23914 glyph->resolved_level = it->bidi_it.resolved_level;
23915 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23916 abort ();
23917 glyph->bidi_type = it->bidi_it.type;
23918 }
23919 ++it->glyph_row->used[area];
23920 }
23921 else
23922 IT_EXPAND_MATRIX_WIDTH (it, area);
23923 }
23924 }
23925
23926
23927 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23928 of the glyph, WIDTH and HEIGHT are the width and height of the
23929 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23930
23931 static void
23932 append_stretch_glyph (struct it *it, Lisp_Object object,
23933 int width, int height, int ascent)
23934 {
23935 struct glyph *glyph;
23936 enum glyph_row_area area = it->area;
23937
23938 eassert (ascent >= 0 && ascent <= height);
23939
23940 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23941 if (glyph < it->glyph_row->glyphs[area + 1])
23942 {
23943 /* If the glyph row is reversed, we need to prepend the glyph
23944 rather than append it. */
23945 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23946 {
23947 struct glyph *g;
23948
23949 /* Make room for the additional glyph. */
23950 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23951 g[1] = *g;
23952 glyph = it->glyph_row->glyphs[area];
23953 }
23954 glyph->charpos = CHARPOS (it->position);
23955 glyph->object = object;
23956 glyph->pixel_width = width;
23957 glyph->ascent = ascent;
23958 glyph->descent = height - ascent;
23959 glyph->voffset = it->voffset;
23960 glyph->type = STRETCH_GLYPH;
23961 glyph->avoid_cursor_p = it->avoid_cursor_p;
23962 glyph->multibyte_p = it->multibyte_p;
23963 glyph->left_box_line_p = it->start_of_box_run_p;
23964 glyph->right_box_line_p = it->end_of_box_run_p;
23965 glyph->overlaps_vertically_p = 0;
23966 glyph->padding_p = 0;
23967 glyph->glyph_not_available_p = 0;
23968 glyph->face_id = it->face_id;
23969 glyph->u.stretch.ascent = ascent;
23970 glyph->u.stretch.height = height;
23971 glyph->slice.img = null_glyph_slice;
23972 glyph->font_type = FONT_TYPE_UNKNOWN;
23973 if (it->bidi_p)
23974 {
23975 glyph->resolved_level = it->bidi_it.resolved_level;
23976 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23977 abort ();
23978 glyph->bidi_type = it->bidi_it.type;
23979 }
23980 else
23981 {
23982 glyph->resolved_level = 0;
23983 glyph->bidi_type = UNKNOWN_BT;
23984 }
23985 ++it->glyph_row->used[area];
23986 }
23987 else
23988 IT_EXPAND_MATRIX_WIDTH (it, area);
23989 }
23990
23991 #endif /* HAVE_WINDOW_SYSTEM */
23992
23993 /* Produce a stretch glyph for iterator IT. IT->object is the value
23994 of the glyph property displayed. The value must be a list
23995 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23996 being recognized:
23997
23998 1. `:width WIDTH' specifies that the space should be WIDTH *
23999 canonical char width wide. WIDTH may be an integer or floating
24000 point number.
24001
24002 2. `:relative-width FACTOR' specifies that the width of the stretch
24003 should be computed from the width of the first character having the
24004 `glyph' property, and should be FACTOR times that width.
24005
24006 3. `:align-to HPOS' specifies that the space should be wide enough
24007 to reach HPOS, a value in canonical character units.
24008
24009 Exactly one of the above pairs must be present.
24010
24011 4. `:height HEIGHT' specifies that the height of the stretch produced
24012 should be HEIGHT, measured in canonical character units.
24013
24014 5. `:relative-height FACTOR' specifies that the height of the
24015 stretch should be FACTOR times the height of the characters having
24016 the glyph property.
24017
24018 Either none or exactly one of 4 or 5 must be present.
24019
24020 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24021 of the stretch should be used for the ascent of the stretch.
24022 ASCENT must be in the range 0 <= ASCENT <= 100. */
24023
24024 void
24025 produce_stretch_glyph (struct it *it)
24026 {
24027 /* (space :width WIDTH :height HEIGHT ...) */
24028 Lisp_Object prop, plist;
24029 int width = 0, height = 0, align_to = -1;
24030 int zero_width_ok_p = 0;
24031 int ascent = 0;
24032 double tem;
24033 struct face *face = NULL;
24034 struct font *font = NULL;
24035
24036 #ifdef HAVE_WINDOW_SYSTEM
24037 int zero_height_ok_p = 0;
24038
24039 if (FRAME_WINDOW_P (it->f))
24040 {
24041 face = FACE_FROM_ID (it->f, it->face_id);
24042 font = face->font ? face->font : FRAME_FONT (it->f);
24043 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24044 }
24045 #endif
24046
24047 /* List should start with `space'. */
24048 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24049 plist = XCDR (it->object);
24050
24051 /* Compute the width of the stretch. */
24052 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24053 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24054 {
24055 /* Absolute width `:width WIDTH' specified and valid. */
24056 zero_width_ok_p = 1;
24057 width = (int)tem;
24058 }
24059 #ifdef HAVE_WINDOW_SYSTEM
24060 else if (FRAME_WINDOW_P (it->f)
24061 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24062 {
24063 /* Relative width `:relative-width FACTOR' specified and valid.
24064 Compute the width of the characters having the `glyph'
24065 property. */
24066 struct it it2;
24067 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24068
24069 it2 = *it;
24070 if (it->multibyte_p)
24071 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24072 else
24073 {
24074 it2.c = it2.char_to_display = *p, it2.len = 1;
24075 if (! ASCII_CHAR_P (it2.c))
24076 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24077 }
24078
24079 it2.glyph_row = NULL;
24080 it2.what = IT_CHARACTER;
24081 x_produce_glyphs (&it2);
24082 width = NUMVAL (prop) * it2.pixel_width;
24083 }
24084 #endif /* HAVE_WINDOW_SYSTEM */
24085 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24086 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24087 {
24088 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24089 align_to = (align_to < 0
24090 ? 0
24091 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24092 else if (align_to < 0)
24093 align_to = window_box_left_offset (it->w, TEXT_AREA);
24094 width = max (0, (int)tem + align_to - it->current_x);
24095 zero_width_ok_p = 1;
24096 }
24097 else
24098 /* Nothing specified -> width defaults to canonical char width. */
24099 width = FRAME_COLUMN_WIDTH (it->f);
24100
24101 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24102 width = 1;
24103
24104 #ifdef HAVE_WINDOW_SYSTEM
24105 /* Compute height. */
24106 if (FRAME_WINDOW_P (it->f))
24107 {
24108 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24109 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24110 {
24111 height = (int)tem;
24112 zero_height_ok_p = 1;
24113 }
24114 else if (prop = Fplist_get (plist, QCrelative_height),
24115 NUMVAL (prop) > 0)
24116 height = FONT_HEIGHT (font) * NUMVAL (prop);
24117 else
24118 height = FONT_HEIGHT (font);
24119
24120 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24121 height = 1;
24122
24123 /* Compute percentage of height used for ascent. If
24124 `:ascent ASCENT' is present and valid, use that. Otherwise,
24125 derive the ascent from the font in use. */
24126 if (prop = Fplist_get (plist, QCascent),
24127 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24128 ascent = height * NUMVAL (prop) / 100.0;
24129 else if (!NILP (prop)
24130 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24131 ascent = min (max (0, (int)tem), height);
24132 else
24133 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24134 }
24135 else
24136 #endif /* HAVE_WINDOW_SYSTEM */
24137 height = 1;
24138
24139 if (width > 0 && it->line_wrap != TRUNCATE
24140 && it->current_x + width > it->last_visible_x)
24141 {
24142 width = it->last_visible_x - it->current_x;
24143 #ifdef HAVE_WINDOW_SYSTEM
24144 /* Subtract one more pixel from the stretch width, but only on
24145 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24146 width -= FRAME_WINDOW_P (it->f);
24147 #endif
24148 }
24149
24150 if (width > 0 && height > 0 && it->glyph_row)
24151 {
24152 Lisp_Object o_object = it->object;
24153 Lisp_Object object = it->stack[it->sp - 1].string;
24154 int n = width;
24155
24156 if (!STRINGP (object))
24157 object = it->w->buffer;
24158 #ifdef HAVE_WINDOW_SYSTEM
24159 if (FRAME_WINDOW_P (it->f))
24160 append_stretch_glyph (it, object, width, height, ascent);
24161 else
24162 #endif
24163 {
24164 it->object = object;
24165 it->char_to_display = ' ';
24166 it->pixel_width = it->len = 1;
24167 while (n--)
24168 tty_append_glyph (it);
24169 it->object = o_object;
24170 }
24171 }
24172
24173 it->pixel_width = width;
24174 #ifdef HAVE_WINDOW_SYSTEM
24175 if (FRAME_WINDOW_P (it->f))
24176 {
24177 it->ascent = it->phys_ascent = ascent;
24178 it->descent = it->phys_descent = height - it->ascent;
24179 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24180 take_vertical_position_into_account (it);
24181 }
24182 else
24183 #endif
24184 it->nglyphs = width;
24185 }
24186
24187 /* Get information about special display element WHAT in an
24188 environment described by IT. WHAT is one of IT_TRUNCATION or
24189 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24190 non-null glyph_row member. This function ensures that fields like
24191 face_id, c, len of IT are left untouched. */
24192
24193 static void
24194 produce_special_glyphs (struct it *it, enum display_element_type what)
24195 {
24196 struct it temp_it;
24197 Lisp_Object gc;
24198 GLYPH glyph;
24199
24200 temp_it = *it;
24201 temp_it.object = make_number (0);
24202 memset (&temp_it.current, 0, sizeof temp_it.current);
24203
24204 if (what == IT_CONTINUATION)
24205 {
24206 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24207 if (it->bidi_it.paragraph_dir == R2L)
24208 SET_GLYPH_FROM_CHAR (glyph, '/');
24209 else
24210 SET_GLYPH_FROM_CHAR (glyph, '\\');
24211 if (it->dp
24212 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24213 {
24214 /* FIXME: Should we mirror GC for R2L lines? */
24215 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24216 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24217 }
24218 }
24219 else if (what == IT_TRUNCATION)
24220 {
24221 /* Truncation glyph. */
24222 SET_GLYPH_FROM_CHAR (glyph, '$');
24223 if (it->dp
24224 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24225 {
24226 /* FIXME: Should we mirror GC for R2L lines? */
24227 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24228 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24229 }
24230 }
24231 else
24232 abort ();
24233
24234 #ifdef HAVE_WINDOW_SYSTEM
24235 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24236 is turned off, we precede the truncation/continuation glyphs by a
24237 stretch glyph whose width is computed such that these special
24238 glyphs are aligned at the window margin, even when very different
24239 fonts are used in different glyph rows. */
24240 if (FRAME_WINDOW_P (temp_it.f)
24241 /* init_iterator calls this with it->glyph_row == NULL, and it
24242 wants only the pixel width of the truncation/continuation
24243 glyphs. */
24244 && temp_it.glyph_row
24245 /* insert_left_trunc_glyphs calls us at the beginning of the
24246 row, and it has its own calculation of the stretch glyph
24247 width. */
24248 && temp_it.glyph_row->used[TEXT_AREA] > 0
24249 && (temp_it.glyph_row->reversed_p
24250 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24251 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24252 {
24253 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24254
24255 if (stretch_width > 0)
24256 {
24257 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24258 struct font *font =
24259 face->font ? face->font : FRAME_FONT (temp_it.f);
24260 int stretch_ascent =
24261 (((temp_it.ascent + temp_it.descent)
24262 * FONT_BASE (font)) / FONT_HEIGHT (font));
24263
24264 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24265 temp_it.ascent + temp_it.descent,
24266 stretch_ascent);
24267 }
24268 }
24269 #endif
24270
24271 temp_it.dp = NULL;
24272 temp_it.what = IT_CHARACTER;
24273 temp_it.len = 1;
24274 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24275 temp_it.face_id = GLYPH_FACE (glyph);
24276 temp_it.len = CHAR_BYTES (temp_it.c);
24277
24278 PRODUCE_GLYPHS (&temp_it);
24279 it->pixel_width = temp_it.pixel_width;
24280 it->nglyphs = temp_it.pixel_width;
24281 }
24282
24283 #ifdef HAVE_WINDOW_SYSTEM
24284
24285 /* Calculate line-height and line-spacing properties.
24286 An integer value specifies explicit pixel value.
24287 A float value specifies relative value to current face height.
24288 A cons (float . face-name) specifies relative value to
24289 height of specified face font.
24290
24291 Returns height in pixels, or nil. */
24292
24293
24294 static Lisp_Object
24295 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24296 int boff, int override)
24297 {
24298 Lisp_Object face_name = Qnil;
24299 int ascent, descent, height;
24300
24301 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24302 return val;
24303
24304 if (CONSP (val))
24305 {
24306 face_name = XCAR (val);
24307 val = XCDR (val);
24308 if (!NUMBERP (val))
24309 val = make_number (1);
24310 if (NILP (face_name))
24311 {
24312 height = it->ascent + it->descent;
24313 goto scale;
24314 }
24315 }
24316
24317 if (NILP (face_name))
24318 {
24319 font = FRAME_FONT (it->f);
24320 boff = FRAME_BASELINE_OFFSET (it->f);
24321 }
24322 else if (EQ (face_name, Qt))
24323 {
24324 override = 0;
24325 }
24326 else
24327 {
24328 int face_id;
24329 struct face *face;
24330
24331 face_id = lookup_named_face (it->f, face_name, 0);
24332 if (face_id < 0)
24333 return make_number (-1);
24334
24335 face = FACE_FROM_ID (it->f, face_id);
24336 font = face->font;
24337 if (font == NULL)
24338 return make_number (-1);
24339 boff = font->baseline_offset;
24340 if (font->vertical_centering)
24341 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24342 }
24343
24344 ascent = FONT_BASE (font) + boff;
24345 descent = FONT_DESCENT (font) - boff;
24346
24347 if (override)
24348 {
24349 it->override_ascent = ascent;
24350 it->override_descent = descent;
24351 it->override_boff = boff;
24352 }
24353
24354 height = ascent + descent;
24355
24356 scale:
24357 if (FLOATP (val))
24358 height = (int)(XFLOAT_DATA (val) * height);
24359 else if (INTEGERP (val))
24360 height *= XINT (val);
24361
24362 return make_number (height);
24363 }
24364
24365
24366 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24367 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24368 and only if this is for a character for which no font was found.
24369
24370 If the display method (it->glyphless_method) is
24371 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24372 length of the acronym or the hexadecimal string, UPPER_XOFF and
24373 UPPER_YOFF are pixel offsets for the upper part of the string,
24374 LOWER_XOFF and LOWER_YOFF are for the lower part.
24375
24376 For the other display methods, LEN through LOWER_YOFF are zero. */
24377
24378 static void
24379 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24380 short upper_xoff, short upper_yoff,
24381 short lower_xoff, short lower_yoff)
24382 {
24383 struct glyph *glyph;
24384 enum glyph_row_area area = it->area;
24385
24386 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24387 if (glyph < it->glyph_row->glyphs[area + 1])
24388 {
24389 /* If the glyph row is reversed, we need to prepend the glyph
24390 rather than append it. */
24391 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24392 {
24393 struct glyph *g;
24394
24395 /* Make room for the additional glyph. */
24396 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24397 g[1] = *g;
24398 glyph = it->glyph_row->glyphs[area];
24399 }
24400 glyph->charpos = CHARPOS (it->position);
24401 glyph->object = it->object;
24402 glyph->pixel_width = it->pixel_width;
24403 glyph->ascent = it->ascent;
24404 glyph->descent = it->descent;
24405 glyph->voffset = it->voffset;
24406 glyph->type = GLYPHLESS_GLYPH;
24407 glyph->u.glyphless.method = it->glyphless_method;
24408 glyph->u.glyphless.for_no_font = for_no_font;
24409 glyph->u.glyphless.len = len;
24410 glyph->u.glyphless.ch = it->c;
24411 glyph->slice.glyphless.upper_xoff = upper_xoff;
24412 glyph->slice.glyphless.upper_yoff = upper_yoff;
24413 glyph->slice.glyphless.lower_xoff = lower_xoff;
24414 glyph->slice.glyphless.lower_yoff = lower_yoff;
24415 glyph->avoid_cursor_p = it->avoid_cursor_p;
24416 glyph->multibyte_p = it->multibyte_p;
24417 glyph->left_box_line_p = it->start_of_box_run_p;
24418 glyph->right_box_line_p = it->end_of_box_run_p;
24419 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24420 || it->phys_descent > it->descent);
24421 glyph->padding_p = 0;
24422 glyph->glyph_not_available_p = 0;
24423 glyph->face_id = face_id;
24424 glyph->font_type = FONT_TYPE_UNKNOWN;
24425 if (it->bidi_p)
24426 {
24427 glyph->resolved_level = it->bidi_it.resolved_level;
24428 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24429 abort ();
24430 glyph->bidi_type = it->bidi_it.type;
24431 }
24432 ++it->glyph_row->used[area];
24433 }
24434 else
24435 IT_EXPAND_MATRIX_WIDTH (it, area);
24436 }
24437
24438
24439 /* Produce a glyph for a glyphless character for iterator IT.
24440 IT->glyphless_method specifies which method to use for displaying
24441 the character. See the description of enum
24442 glyphless_display_method in dispextern.h for the detail.
24443
24444 FOR_NO_FONT is nonzero if and only if this is for a character for
24445 which no font was found. ACRONYM, if non-nil, is an acronym string
24446 for the character. */
24447
24448 static void
24449 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24450 {
24451 int face_id;
24452 struct face *face;
24453 struct font *font;
24454 int base_width, base_height, width, height;
24455 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24456 int len;
24457
24458 /* Get the metrics of the base font. We always refer to the current
24459 ASCII face. */
24460 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24461 font = face->font ? face->font : FRAME_FONT (it->f);
24462 it->ascent = FONT_BASE (font) + font->baseline_offset;
24463 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24464 base_height = it->ascent + it->descent;
24465 base_width = font->average_width;
24466
24467 /* Get a face ID for the glyph by utilizing a cache (the same way as
24468 done for `escape-glyph' in get_next_display_element). */
24469 if (it->f == last_glyphless_glyph_frame
24470 && it->face_id == last_glyphless_glyph_face_id)
24471 {
24472 face_id = last_glyphless_glyph_merged_face_id;
24473 }
24474 else
24475 {
24476 /* Merge the `glyphless-char' face into the current face. */
24477 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24478 last_glyphless_glyph_frame = it->f;
24479 last_glyphless_glyph_face_id = it->face_id;
24480 last_glyphless_glyph_merged_face_id = face_id;
24481 }
24482
24483 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24484 {
24485 it->pixel_width = THIN_SPACE_WIDTH;
24486 len = 0;
24487 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24488 }
24489 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24490 {
24491 width = CHAR_WIDTH (it->c);
24492 if (width == 0)
24493 width = 1;
24494 else if (width > 4)
24495 width = 4;
24496 it->pixel_width = base_width * width;
24497 len = 0;
24498 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24499 }
24500 else
24501 {
24502 char buf[7];
24503 const char *str;
24504 unsigned int code[6];
24505 int upper_len;
24506 int ascent, descent;
24507 struct font_metrics metrics_upper, metrics_lower;
24508
24509 face = FACE_FROM_ID (it->f, face_id);
24510 font = face->font ? face->font : FRAME_FONT (it->f);
24511 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24512
24513 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24514 {
24515 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24516 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24517 if (CONSP (acronym))
24518 acronym = XCAR (acronym);
24519 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24520 }
24521 else
24522 {
24523 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24524 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24525 str = buf;
24526 }
24527 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24528 code[len] = font->driver->encode_char (font, str[len]);
24529 upper_len = (len + 1) / 2;
24530 font->driver->text_extents (font, code, upper_len,
24531 &metrics_upper);
24532 font->driver->text_extents (font, code + upper_len, len - upper_len,
24533 &metrics_lower);
24534
24535
24536
24537 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24538 width = max (metrics_upper.width, metrics_lower.width) + 4;
24539 upper_xoff = upper_yoff = 2; /* the typical case */
24540 if (base_width >= width)
24541 {
24542 /* Align the upper to the left, the lower to the right. */
24543 it->pixel_width = base_width;
24544 lower_xoff = base_width - 2 - metrics_lower.width;
24545 }
24546 else
24547 {
24548 /* Center the shorter one. */
24549 it->pixel_width = width;
24550 if (metrics_upper.width >= metrics_lower.width)
24551 lower_xoff = (width - metrics_lower.width) / 2;
24552 else
24553 {
24554 /* FIXME: This code doesn't look right. It formerly was
24555 missing the "lower_xoff = 0;", which couldn't have
24556 been right since it left lower_xoff uninitialized. */
24557 lower_xoff = 0;
24558 upper_xoff = (width - metrics_upper.width) / 2;
24559 }
24560 }
24561
24562 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24563 top, bottom, and between upper and lower strings. */
24564 height = (metrics_upper.ascent + metrics_upper.descent
24565 + metrics_lower.ascent + metrics_lower.descent) + 5;
24566 /* Center vertically.
24567 H:base_height, D:base_descent
24568 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24569
24570 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24571 descent = D - H/2 + h/2;
24572 lower_yoff = descent - 2 - ld;
24573 upper_yoff = lower_yoff - la - 1 - ud; */
24574 ascent = - (it->descent - (base_height + height + 1) / 2);
24575 descent = it->descent - (base_height - height) / 2;
24576 lower_yoff = descent - 2 - metrics_lower.descent;
24577 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24578 - metrics_upper.descent);
24579 /* Don't make the height shorter than the base height. */
24580 if (height > base_height)
24581 {
24582 it->ascent = ascent;
24583 it->descent = descent;
24584 }
24585 }
24586
24587 it->phys_ascent = it->ascent;
24588 it->phys_descent = it->descent;
24589 if (it->glyph_row)
24590 append_glyphless_glyph (it, face_id, for_no_font, len,
24591 upper_xoff, upper_yoff,
24592 lower_xoff, lower_yoff);
24593 it->nglyphs = 1;
24594 take_vertical_position_into_account (it);
24595 }
24596
24597
24598 /* RIF:
24599 Produce glyphs/get display metrics for the display element IT is
24600 loaded with. See the description of struct it in dispextern.h
24601 for an overview of struct it. */
24602
24603 void
24604 x_produce_glyphs (struct it *it)
24605 {
24606 int extra_line_spacing = it->extra_line_spacing;
24607
24608 it->glyph_not_available_p = 0;
24609
24610 if (it->what == IT_CHARACTER)
24611 {
24612 XChar2b char2b;
24613 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24614 struct font *font = face->font;
24615 struct font_metrics *pcm = NULL;
24616 int boff; /* baseline offset */
24617
24618 if (font == NULL)
24619 {
24620 /* When no suitable font is found, display this character by
24621 the method specified in the first extra slot of
24622 Vglyphless_char_display. */
24623 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24624
24625 eassert (it->what == IT_GLYPHLESS);
24626 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24627 goto done;
24628 }
24629
24630 boff = font->baseline_offset;
24631 if (font->vertical_centering)
24632 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24633
24634 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24635 {
24636 int stretched_p;
24637
24638 it->nglyphs = 1;
24639
24640 if (it->override_ascent >= 0)
24641 {
24642 it->ascent = it->override_ascent;
24643 it->descent = it->override_descent;
24644 boff = it->override_boff;
24645 }
24646 else
24647 {
24648 it->ascent = FONT_BASE (font) + boff;
24649 it->descent = FONT_DESCENT (font) - boff;
24650 }
24651
24652 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24653 {
24654 pcm = get_per_char_metric (font, &char2b);
24655 if (pcm->width == 0
24656 && pcm->rbearing == 0 && pcm->lbearing == 0)
24657 pcm = NULL;
24658 }
24659
24660 if (pcm)
24661 {
24662 it->phys_ascent = pcm->ascent + boff;
24663 it->phys_descent = pcm->descent - boff;
24664 it->pixel_width = pcm->width;
24665 }
24666 else
24667 {
24668 it->glyph_not_available_p = 1;
24669 it->phys_ascent = it->ascent;
24670 it->phys_descent = it->descent;
24671 it->pixel_width = font->space_width;
24672 }
24673
24674 if (it->constrain_row_ascent_descent_p)
24675 {
24676 if (it->descent > it->max_descent)
24677 {
24678 it->ascent += it->descent - it->max_descent;
24679 it->descent = it->max_descent;
24680 }
24681 if (it->ascent > it->max_ascent)
24682 {
24683 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24684 it->ascent = it->max_ascent;
24685 }
24686 it->phys_ascent = min (it->phys_ascent, it->ascent);
24687 it->phys_descent = min (it->phys_descent, it->descent);
24688 extra_line_spacing = 0;
24689 }
24690
24691 /* If this is a space inside a region of text with
24692 `space-width' property, change its width. */
24693 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24694 if (stretched_p)
24695 it->pixel_width *= XFLOATINT (it->space_width);
24696
24697 /* If face has a box, add the box thickness to the character
24698 height. If character has a box line to the left and/or
24699 right, add the box line width to the character's width. */
24700 if (face->box != FACE_NO_BOX)
24701 {
24702 int thick = face->box_line_width;
24703
24704 if (thick > 0)
24705 {
24706 it->ascent += thick;
24707 it->descent += thick;
24708 }
24709 else
24710 thick = -thick;
24711
24712 if (it->start_of_box_run_p)
24713 it->pixel_width += thick;
24714 if (it->end_of_box_run_p)
24715 it->pixel_width += thick;
24716 }
24717
24718 /* If face has an overline, add the height of the overline
24719 (1 pixel) and a 1 pixel margin to the character height. */
24720 if (face->overline_p)
24721 it->ascent += overline_margin;
24722
24723 if (it->constrain_row_ascent_descent_p)
24724 {
24725 if (it->ascent > it->max_ascent)
24726 it->ascent = it->max_ascent;
24727 if (it->descent > it->max_descent)
24728 it->descent = it->max_descent;
24729 }
24730
24731 take_vertical_position_into_account (it);
24732
24733 /* If we have to actually produce glyphs, do it. */
24734 if (it->glyph_row)
24735 {
24736 if (stretched_p)
24737 {
24738 /* Translate a space with a `space-width' property
24739 into a stretch glyph. */
24740 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24741 / FONT_HEIGHT (font));
24742 append_stretch_glyph (it, it->object, it->pixel_width,
24743 it->ascent + it->descent, ascent);
24744 }
24745 else
24746 append_glyph (it);
24747
24748 /* If characters with lbearing or rbearing are displayed
24749 in this line, record that fact in a flag of the
24750 glyph row. This is used to optimize X output code. */
24751 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24752 it->glyph_row->contains_overlapping_glyphs_p = 1;
24753 }
24754 if (! stretched_p && it->pixel_width == 0)
24755 /* We assure that all visible glyphs have at least 1-pixel
24756 width. */
24757 it->pixel_width = 1;
24758 }
24759 else if (it->char_to_display == '\n')
24760 {
24761 /* A newline has no width, but we need the height of the
24762 line. But if previous part of the line sets a height,
24763 don't increase that height */
24764
24765 Lisp_Object height;
24766 Lisp_Object total_height = Qnil;
24767
24768 it->override_ascent = -1;
24769 it->pixel_width = 0;
24770 it->nglyphs = 0;
24771
24772 height = get_it_property (it, Qline_height);
24773 /* Split (line-height total-height) list */
24774 if (CONSP (height)
24775 && CONSP (XCDR (height))
24776 && NILP (XCDR (XCDR (height))))
24777 {
24778 total_height = XCAR (XCDR (height));
24779 height = XCAR (height);
24780 }
24781 height = calc_line_height_property (it, height, font, boff, 1);
24782
24783 if (it->override_ascent >= 0)
24784 {
24785 it->ascent = it->override_ascent;
24786 it->descent = it->override_descent;
24787 boff = it->override_boff;
24788 }
24789 else
24790 {
24791 it->ascent = FONT_BASE (font) + boff;
24792 it->descent = FONT_DESCENT (font) - boff;
24793 }
24794
24795 if (EQ (height, Qt))
24796 {
24797 if (it->descent > it->max_descent)
24798 {
24799 it->ascent += it->descent - it->max_descent;
24800 it->descent = it->max_descent;
24801 }
24802 if (it->ascent > it->max_ascent)
24803 {
24804 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24805 it->ascent = it->max_ascent;
24806 }
24807 it->phys_ascent = min (it->phys_ascent, it->ascent);
24808 it->phys_descent = min (it->phys_descent, it->descent);
24809 it->constrain_row_ascent_descent_p = 1;
24810 extra_line_spacing = 0;
24811 }
24812 else
24813 {
24814 Lisp_Object spacing;
24815
24816 it->phys_ascent = it->ascent;
24817 it->phys_descent = it->descent;
24818
24819 if ((it->max_ascent > 0 || it->max_descent > 0)
24820 && face->box != FACE_NO_BOX
24821 && face->box_line_width > 0)
24822 {
24823 it->ascent += face->box_line_width;
24824 it->descent += face->box_line_width;
24825 }
24826 if (!NILP (height)
24827 && XINT (height) > it->ascent + it->descent)
24828 it->ascent = XINT (height) - it->descent;
24829
24830 if (!NILP (total_height))
24831 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24832 else
24833 {
24834 spacing = get_it_property (it, Qline_spacing);
24835 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24836 }
24837 if (INTEGERP (spacing))
24838 {
24839 extra_line_spacing = XINT (spacing);
24840 if (!NILP (total_height))
24841 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24842 }
24843 }
24844 }
24845 else /* i.e. (it->char_to_display == '\t') */
24846 {
24847 if (font->space_width > 0)
24848 {
24849 int tab_width = it->tab_width * font->space_width;
24850 int x = it->current_x + it->continuation_lines_width;
24851 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24852
24853 /* If the distance from the current position to the next tab
24854 stop is less than a space character width, use the
24855 tab stop after that. */
24856 if (next_tab_x - x < font->space_width)
24857 next_tab_x += tab_width;
24858
24859 it->pixel_width = next_tab_x - x;
24860 it->nglyphs = 1;
24861 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24862 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24863
24864 if (it->glyph_row)
24865 {
24866 append_stretch_glyph (it, it->object, it->pixel_width,
24867 it->ascent + it->descent, it->ascent);
24868 }
24869 }
24870 else
24871 {
24872 it->pixel_width = 0;
24873 it->nglyphs = 1;
24874 }
24875 }
24876 }
24877 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24878 {
24879 /* A static composition.
24880
24881 Note: A composition is represented as one glyph in the
24882 glyph matrix. There are no padding glyphs.
24883
24884 Important note: pixel_width, ascent, and descent are the
24885 values of what is drawn by draw_glyphs (i.e. the values of
24886 the overall glyphs composed). */
24887 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24888 int boff; /* baseline offset */
24889 struct composition *cmp = composition_table[it->cmp_it.id];
24890 int glyph_len = cmp->glyph_len;
24891 struct font *font = face->font;
24892
24893 it->nglyphs = 1;
24894
24895 /* If we have not yet calculated pixel size data of glyphs of
24896 the composition for the current face font, calculate them
24897 now. Theoretically, we have to check all fonts for the
24898 glyphs, but that requires much time and memory space. So,
24899 here we check only the font of the first glyph. This may
24900 lead to incorrect display, but it's very rare, and C-l
24901 (recenter-top-bottom) can correct the display anyway. */
24902 if (! cmp->font || cmp->font != font)
24903 {
24904 /* Ascent and descent of the font of the first character
24905 of this composition (adjusted by baseline offset).
24906 Ascent and descent of overall glyphs should not be less
24907 than these, respectively. */
24908 int font_ascent, font_descent, font_height;
24909 /* Bounding box of the overall glyphs. */
24910 int leftmost, rightmost, lowest, highest;
24911 int lbearing, rbearing;
24912 int i, width, ascent, descent;
24913 int left_padded = 0, right_padded = 0;
24914 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24915 XChar2b char2b;
24916 struct font_metrics *pcm;
24917 int font_not_found_p;
24918 ptrdiff_t pos;
24919
24920 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24921 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24922 break;
24923 if (glyph_len < cmp->glyph_len)
24924 right_padded = 1;
24925 for (i = 0; i < glyph_len; i++)
24926 {
24927 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24928 break;
24929 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24930 }
24931 if (i > 0)
24932 left_padded = 1;
24933
24934 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24935 : IT_CHARPOS (*it));
24936 /* If no suitable font is found, use the default font. */
24937 font_not_found_p = font == NULL;
24938 if (font_not_found_p)
24939 {
24940 face = face->ascii_face;
24941 font = face->font;
24942 }
24943 boff = font->baseline_offset;
24944 if (font->vertical_centering)
24945 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24946 font_ascent = FONT_BASE (font) + boff;
24947 font_descent = FONT_DESCENT (font) - boff;
24948 font_height = FONT_HEIGHT (font);
24949
24950 cmp->font = (void *) font;
24951
24952 pcm = NULL;
24953 if (! font_not_found_p)
24954 {
24955 get_char_face_and_encoding (it->f, c, it->face_id,
24956 &char2b, 0);
24957 pcm = get_per_char_metric (font, &char2b);
24958 }
24959
24960 /* Initialize the bounding box. */
24961 if (pcm)
24962 {
24963 width = cmp->glyph_len > 0 ? pcm->width : 0;
24964 ascent = pcm->ascent;
24965 descent = pcm->descent;
24966 lbearing = pcm->lbearing;
24967 rbearing = pcm->rbearing;
24968 }
24969 else
24970 {
24971 width = cmp->glyph_len > 0 ? font->space_width : 0;
24972 ascent = FONT_BASE (font);
24973 descent = FONT_DESCENT (font);
24974 lbearing = 0;
24975 rbearing = width;
24976 }
24977
24978 rightmost = width;
24979 leftmost = 0;
24980 lowest = - descent + boff;
24981 highest = ascent + boff;
24982
24983 if (! font_not_found_p
24984 && font->default_ascent
24985 && CHAR_TABLE_P (Vuse_default_ascent)
24986 && !NILP (Faref (Vuse_default_ascent,
24987 make_number (it->char_to_display))))
24988 highest = font->default_ascent + boff;
24989
24990 /* Draw the first glyph at the normal position. It may be
24991 shifted to right later if some other glyphs are drawn
24992 at the left. */
24993 cmp->offsets[i * 2] = 0;
24994 cmp->offsets[i * 2 + 1] = boff;
24995 cmp->lbearing = lbearing;
24996 cmp->rbearing = rbearing;
24997
24998 /* Set cmp->offsets for the remaining glyphs. */
24999 for (i++; i < glyph_len; i++)
25000 {
25001 int left, right, btm, top;
25002 int ch = COMPOSITION_GLYPH (cmp, i);
25003 int face_id;
25004 struct face *this_face;
25005
25006 if (ch == '\t')
25007 ch = ' ';
25008 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25009 this_face = FACE_FROM_ID (it->f, face_id);
25010 font = this_face->font;
25011
25012 if (font == NULL)
25013 pcm = NULL;
25014 else
25015 {
25016 get_char_face_and_encoding (it->f, ch, face_id,
25017 &char2b, 0);
25018 pcm = get_per_char_metric (font, &char2b);
25019 }
25020 if (! pcm)
25021 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25022 else
25023 {
25024 width = pcm->width;
25025 ascent = pcm->ascent;
25026 descent = pcm->descent;
25027 lbearing = pcm->lbearing;
25028 rbearing = pcm->rbearing;
25029 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25030 {
25031 /* Relative composition with or without
25032 alternate chars. */
25033 left = (leftmost + rightmost - width) / 2;
25034 btm = - descent + boff;
25035 if (font->relative_compose
25036 && (! CHAR_TABLE_P (Vignore_relative_composition)
25037 || NILP (Faref (Vignore_relative_composition,
25038 make_number (ch)))))
25039 {
25040
25041 if (- descent >= font->relative_compose)
25042 /* One extra pixel between two glyphs. */
25043 btm = highest + 1;
25044 else if (ascent <= 0)
25045 /* One extra pixel between two glyphs. */
25046 btm = lowest - 1 - ascent - descent;
25047 }
25048 }
25049 else
25050 {
25051 /* A composition rule is specified by an integer
25052 value that encodes global and new reference
25053 points (GREF and NREF). GREF and NREF are
25054 specified by numbers as below:
25055
25056 0---1---2 -- ascent
25057 | |
25058 | |
25059 | |
25060 9--10--11 -- center
25061 | |
25062 ---3---4---5--- baseline
25063 | |
25064 6---7---8 -- descent
25065 */
25066 int rule = COMPOSITION_RULE (cmp, i);
25067 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25068
25069 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25070 grefx = gref % 3, nrefx = nref % 3;
25071 grefy = gref / 3, nrefy = nref / 3;
25072 if (xoff)
25073 xoff = font_height * (xoff - 128) / 256;
25074 if (yoff)
25075 yoff = font_height * (yoff - 128) / 256;
25076
25077 left = (leftmost
25078 + grefx * (rightmost - leftmost) / 2
25079 - nrefx * width / 2
25080 + xoff);
25081
25082 btm = ((grefy == 0 ? highest
25083 : grefy == 1 ? 0
25084 : grefy == 2 ? lowest
25085 : (highest + lowest) / 2)
25086 - (nrefy == 0 ? ascent + descent
25087 : nrefy == 1 ? descent - boff
25088 : nrefy == 2 ? 0
25089 : (ascent + descent) / 2)
25090 + yoff);
25091 }
25092
25093 cmp->offsets[i * 2] = left;
25094 cmp->offsets[i * 2 + 1] = btm + descent;
25095
25096 /* Update the bounding box of the overall glyphs. */
25097 if (width > 0)
25098 {
25099 right = left + width;
25100 if (left < leftmost)
25101 leftmost = left;
25102 if (right > rightmost)
25103 rightmost = right;
25104 }
25105 top = btm + descent + ascent;
25106 if (top > highest)
25107 highest = top;
25108 if (btm < lowest)
25109 lowest = btm;
25110
25111 if (cmp->lbearing > left + lbearing)
25112 cmp->lbearing = left + lbearing;
25113 if (cmp->rbearing < left + rbearing)
25114 cmp->rbearing = left + rbearing;
25115 }
25116 }
25117
25118 /* If there are glyphs whose x-offsets are negative,
25119 shift all glyphs to the right and make all x-offsets
25120 non-negative. */
25121 if (leftmost < 0)
25122 {
25123 for (i = 0; i < cmp->glyph_len; i++)
25124 cmp->offsets[i * 2] -= leftmost;
25125 rightmost -= leftmost;
25126 cmp->lbearing -= leftmost;
25127 cmp->rbearing -= leftmost;
25128 }
25129
25130 if (left_padded && cmp->lbearing < 0)
25131 {
25132 for (i = 0; i < cmp->glyph_len; i++)
25133 cmp->offsets[i * 2] -= cmp->lbearing;
25134 rightmost -= cmp->lbearing;
25135 cmp->rbearing -= cmp->lbearing;
25136 cmp->lbearing = 0;
25137 }
25138 if (right_padded && rightmost < cmp->rbearing)
25139 {
25140 rightmost = cmp->rbearing;
25141 }
25142
25143 cmp->pixel_width = rightmost;
25144 cmp->ascent = highest;
25145 cmp->descent = - lowest;
25146 if (cmp->ascent < font_ascent)
25147 cmp->ascent = font_ascent;
25148 if (cmp->descent < font_descent)
25149 cmp->descent = font_descent;
25150 }
25151
25152 if (it->glyph_row
25153 && (cmp->lbearing < 0
25154 || cmp->rbearing > cmp->pixel_width))
25155 it->glyph_row->contains_overlapping_glyphs_p = 1;
25156
25157 it->pixel_width = cmp->pixel_width;
25158 it->ascent = it->phys_ascent = cmp->ascent;
25159 it->descent = it->phys_descent = cmp->descent;
25160 if (face->box != FACE_NO_BOX)
25161 {
25162 int thick = face->box_line_width;
25163
25164 if (thick > 0)
25165 {
25166 it->ascent += thick;
25167 it->descent += thick;
25168 }
25169 else
25170 thick = - thick;
25171
25172 if (it->start_of_box_run_p)
25173 it->pixel_width += thick;
25174 if (it->end_of_box_run_p)
25175 it->pixel_width += thick;
25176 }
25177
25178 /* If face has an overline, add the height of the overline
25179 (1 pixel) and a 1 pixel margin to the character height. */
25180 if (face->overline_p)
25181 it->ascent += overline_margin;
25182
25183 take_vertical_position_into_account (it);
25184 if (it->ascent < 0)
25185 it->ascent = 0;
25186 if (it->descent < 0)
25187 it->descent = 0;
25188
25189 if (it->glyph_row && cmp->glyph_len > 0)
25190 append_composite_glyph (it);
25191 }
25192 else if (it->what == IT_COMPOSITION)
25193 {
25194 /* A dynamic (automatic) composition. */
25195 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25196 Lisp_Object gstring;
25197 struct font_metrics metrics;
25198
25199 it->nglyphs = 1;
25200
25201 gstring = composition_gstring_from_id (it->cmp_it.id);
25202 it->pixel_width
25203 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25204 &metrics);
25205 if (it->glyph_row
25206 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25207 it->glyph_row->contains_overlapping_glyphs_p = 1;
25208 it->ascent = it->phys_ascent = metrics.ascent;
25209 it->descent = it->phys_descent = metrics.descent;
25210 if (face->box != FACE_NO_BOX)
25211 {
25212 int thick = face->box_line_width;
25213
25214 if (thick > 0)
25215 {
25216 it->ascent += thick;
25217 it->descent += thick;
25218 }
25219 else
25220 thick = - thick;
25221
25222 if (it->start_of_box_run_p)
25223 it->pixel_width += thick;
25224 if (it->end_of_box_run_p)
25225 it->pixel_width += thick;
25226 }
25227 /* If face has an overline, add the height of the overline
25228 (1 pixel) and a 1 pixel margin to the character height. */
25229 if (face->overline_p)
25230 it->ascent += overline_margin;
25231 take_vertical_position_into_account (it);
25232 if (it->ascent < 0)
25233 it->ascent = 0;
25234 if (it->descent < 0)
25235 it->descent = 0;
25236
25237 if (it->glyph_row)
25238 append_composite_glyph (it);
25239 }
25240 else if (it->what == IT_GLYPHLESS)
25241 produce_glyphless_glyph (it, 0, Qnil);
25242 else if (it->what == IT_IMAGE)
25243 produce_image_glyph (it);
25244 else if (it->what == IT_STRETCH)
25245 produce_stretch_glyph (it);
25246
25247 done:
25248 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25249 because this isn't true for images with `:ascent 100'. */
25250 eassert (it->ascent >= 0 && it->descent >= 0);
25251 if (it->area == TEXT_AREA)
25252 it->current_x += it->pixel_width;
25253
25254 if (extra_line_spacing > 0)
25255 {
25256 it->descent += extra_line_spacing;
25257 if (extra_line_spacing > it->max_extra_line_spacing)
25258 it->max_extra_line_spacing = extra_line_spacing;
25259 }
25260
25261 it->max_ascent = max (it->max_ascent, it->ascent);
25262 it->max_descent = max (it->max_descent, it->descent);
25263 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25264 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25265 }
25266
25267 /* EXPORT for RIF:
25268 Output LEN glyphs starting at START at the nominal cursor position.
25269 Advance the nominal cursor over the text. The global variable
25270 updated_window contains the window being updated, updated_row is
25271 the glyph row being updated, and updated_area is the area of that
25272 row being updated. */
25273
25274 void
25275 x_write_glyphs (struct glyph *start, int len)
25276 {
25277 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25278
25279 eassert (updated_window && updated_row);
25280 /* When the window is hscrolled, cursor hpos can legitimately be out
25281 of bounds, but we draw the cursor at the corresponding window
25282 margin in that case. */
25283 if (!updated_row->reversed_p && chpos < 0)
25284 chpos = 0;
25285 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25286 chpos = updated_row->used[TEXT_AREA] - 1;
25287
25288 BLOCK_INPUT;
25289
25290 /* Write glyphs. */
25291
25292 hpos = start - updated_row->glyphs[updated_area];
25293 x = draw_glyphs (updated_window, output_cursor.x,
25294 updated_row, updated_area,
25295 hpos, hpos + len,
25296 DRAW_NORMAL_TEXT, 0);
25297
25298 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25299 if (updated_area == TEXT_AREA
25300 && updated_window->phys_cursor_on_p
25301 && updated_window->phys_cursor.vpos == output_cursor.vpos
25302 && chpos >= hpos
25303 && chpos < hpos + len)
25304 updated_window->phys_cursor_on_p = 0;
25305
25306 UNBLOCK_INPUT;
25307
25308 /* Advance the output cursor. */
25309 output_cursor.hpos += len;
25310 output_cursor.x = x;
25311 }
25312
25313
25314 /* EXPORT for RIF:
25315 Insert LEN glyphs from START at the nominal cursor position. */
25316
25317 void
25318 x_insert_glyphs (struct glyph *start, int len)
25319 {
25320 struct frame *f;
25321 struct window *w;
25322 int line_height, shift_by_width, shifted_region_width;
25323 struct glyph_row *row;
25324 struct glyph *glyph;
25325 int frame_x, frame_y;
25326 ptrdiff_t hpos;
25327
25328 eassert (updated_window && updated_row);
25329 BLOCK_INPUT;
25330 w = updated_window;
25331 f = XFRAME (WINDOW_FRAME (w));
25332
25333 /* Get the height of the line we are in. */
25334 row = updated_row;
25335 line_height = row->height;
25336
25337 /* Get the width of the glyphs to insert. */
25338 shift_by_width = 0;
25339 for (glyph = start; glyph < start + len; ++glyph)
25340 shift_by_width += glyph->pixel_width;
25341
25342 /* Get the width of the region to shift right. */
25343 shifted_region_width = (window_box_width (w, updated_area)
25344 - output_cursor.x
25345 - shift_by_width);
25346
25347 /* Shift right. */
25348 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25349 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25350
25351 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25352 line_height, shift_by_width);
25353
25354 /* Write the glyphs. */
25355 hpos = start - row->glyphs[updated_area];
25356 draw_glyphs (w, output_cursor.x, row, updated_area,
25357 hpos, hpos + len,
25358 DRAW_NORMAL_TEXT, 0);
25359
25360 /* Advance the output cursor. */
25361 output_cursor.hpos += len;
25362 output_cursor.x += shift_by_width;
25363 UNBLOCK_INPUT;
25364 }
25365
25366
25367 /* EXPORT for RIF:
25368 Erase the current text line from the nominal cursor position
25369 (inclusive) to pixel column TO_X (exclusive). The idea is that
25370 everything from TO_X onward is already erased.
25371
25372 TO_X is a pixel position relative to updated_area of
25373 updated_window. TO_X == -1 means clear to the end of this area. */
25374
25375 void
25376 x_clear_end_of_line (int to_x)
25377 {
25378 struct frame *f;
25379 struct window *w = updated_window;
25380 int max_x, min_y, max_y;
25381 int from_x, from_y, to_y;
25382
25383 eassert (updated_window && updated_row);
25384 f = XFRAME (w->frame);
25385
25386 if (updated_row->full_width_p)
25387 max_x = WINDOW_TOTAL_WIDTH (w);
25388 else
25389 max_x = window_box_width (w, updated_area);
25390 max_y = window_text_bottom_y (w);
25391
25392 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25393 of window. For TO_X > 0, truncate to end of drawing area. */
25394 if (to_x == 0)
25395 return;
25396 else if (to_x < 0)
25397 to_x = max_x;
25398 else
25399 to_x = min (to_x, max_x);
25400
25401 to_y = min (max_y, output_cursor.y + updated_row->height);
25402
25403 /* Notice if the cursor will be cleared by this operation. */
25404 if (!updated_row->full_width_p)
25405 notice_overwritten_cursor (w, updated_area,
25406 output_cursor.x, -1,
25407 updated_row->y,
25408 MATRIX_ROW_BOTTOM_Y (updated_row));
25409
25410 from_x = output_cursor.x;
25411
25412 /* Translate to frame coordinates. */
25413 if (updated_row->full_width_p)
25414 {
25415 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25416 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25417 }
25418 else
25419 {
25420 int area_left = window_box_left (w, updated_area);
25421 from_x += area_left;
25422 to_x += area_left;
25423 }
25424
25425 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25426 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25427 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25428
25429 /* Prevent inadvertently clearing to end of the X window. */
25430 if (to_x > from_x && to_y > from_y)
25431 {
25432 BLOCK_INPUT;
25433 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25434 to_x - from_x, to_y - from_y);
25435 UNBLOCK_INPUT;
25436 }
25437 }
25438
25439 #endif /* HAVE_WINDOW_SYSTEM */
25440
25441
25442 \f
25443 /***********************************************************************
25444 Cursor types
25445 ***********************************************************************/
25446
25447 /* Value is the internal representation of the specified cursor type
25448 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25449 of the bar cursor. */
25450
25451 static enum text_cursor_kinds
25452 get_specified_cursor_type (Lisp_Object arg, int *width)
25453 {
25454 enum text_cursor_kinds type;
25455
25456 if (NILP (arg))
25457 return NO_CURSOR;
25458
25459 if (EQ (arg, Qbox))
25460 return FILLED_BOX_CURSOR;
25461
25462 if (EQ (arg, Qhollow))
25463 return HOLLOW_BOX_CURSOR;
25464
25465 if (EQ (arg, Qbar))
25466 {
25467 *width = 2;
25468 return BAR_CURSOR;
25469 }
25470
25471 if (CONSP (arg)
25472 && EQ (XCAR (arg), Qbar)
25473 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25474 {
25475 *width = XINT (XCDR (arg));
25476 return BAR_CURSOR;
25477 }
25478
25479 if (EQ (arg, Qhbar))
25480 {
25481 *width = 2;
25482 return HBAR_CURSOR;
25483 }
25484
25485 if (CONSP (arg)
25486 && EQ (XCAR (arg), Qhbar)
25487 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25488 {
25489 *width = XINT (XCDR (arg));
25490 return HBAR_CURSOR;
25491 }
25492
25493 /* Treat anything unknown as "hollow box cursor".
25494 It was bad to signal an error; people have trouble fixing
25495 .Xdefaults with Emacs, when it has something bad in it. */
25496 type = HOLLOW_BOX_CURSOR;
25497
25498 return type;
25499 }
25500
25501 /* Set the default cursor types for specified frame. */
25502 void
25503 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25504 {
25505 int width = 1;
25506 Lisp_Object tem;
25507
25508 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25509 FRAME_CURSOR_WIDTH (f) = width;
25510
25511 /* By default, set up the blink-off state depending on the on-state. */
25512
25513 tem = Fassoc (arg, Vblink_cursor_alist);
25514 if (!NILP (tem))
25515 {
25516 FRAME_BLINK_OFF_CURSOR (f)
25517 = get_specified_cursor_type (XCDR (tem), &width);
25518 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25519 }
25520 else
25521 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25522 }
25523
25524
25525 #ifdef HAVE_WINDOW_SYSTEM
25526
25527 /* Return the cursor we want to be displayed in window W. Return
25528 width of bar/hbar cursor through WIDTH arg. Return with
25529 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25530 (i.e. if the `system caret' should track this cursor).
25531
25532 In a mini-buffer window, we want the cursor only to appear if we
25533 are reading input from this window. For the selected window, we
25534 want the cursor type given by the frame parameter or buffer local
25535 setting of cursor-type. If explicitly marked off, draw no cursor.
25536 In all other cases, we want a hollow box cursor. */
25537
25538 static enum text_cursor_kinds
25539 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25540 int *active_cursor)
25541 {
25542 struct frame *f = XFRAME (w->frame);
25543 struct buffer *b = XBUFFER (w->buffer);
25544 int cursor_type = DEFAULT_CURSOR;
25545 Lisp_Object alt_cursor;
25546 int non_selected = 0;
25547
25548 *active_cursor = 1;
25549
25550 /* Echo area */
25551 if (cursor_in_echo_area
25552 && FRAME_HAS_MINIBUF_P (f)
25553 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25554 {
25555 if (w == XWINDOW (echo_area_window))
25556 {
25557 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25558 {
25559 *width = FRAME_CURSOR_WIDTH (f);
25560 return FRAME_DESIRED_CURSOR (f);
25561 }
25562 else
25563 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25564 }
25565
25566 *active_cursor = 0;
25567 non_selected = 1;
25568 }
25569
25570 /* Detect a nonselected window or nonselected frame. */
25571 else if (w != XWINDOW (f->selected_window)
25572 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25573 {
25574 *active_cursor = 0;
25575
25576 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25577 return NO_CURSOR;
25578
25579 non_selected = 1;
25580 }
25581
25582 /* Never display a cursor in a window in which cursor-type is nil. */
25583 if (NILP (BVAR (b, cursor_type)))
25584 return NO_CURSOR;
25585
25586 /* Get the normal cursor type for this window. */
25587 if (EQ (BVAR (b, cursor_type), Qt))
25588 {
25589 cursor_type = FRAME_DESIRED_CURSOR (f);
25590 *width = FRAME_CURSOR_WIDTH (f);
25591 }
25592 else
25593 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25594
25595 /* Use cursor-in-non-selected-windows instead
25596 for non-selected window or frame. */
25597 if (non_selected)
25598 {
25599 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25600 if (!EQ (Qt, alt_cursor))
25601 return get_specified_cursor_type (alt_cursor, width);
25602 /* t means modify the normal cursor type. */
25603 if (cursor_type == FILLED_BOX_CURSOR)
25604 cursor_type = HOLLOW_BOX_CURSOR;
25605 else if (cursor_type == BAR_CURSOR && *width > 1)
25606 --*width;
25607 return cursor_type;
25608 }
25609
25610 /* Use normal cursor if not blinked off. */
25611 if (!w->cursor_off_p)
25612 {
25613 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25614 {
25615 if (cursor_type == FILLED_BOX_CURSOR)
25616 {
25617 /* Using a block cursor on large images can be very annoying.
25618 So use a hollow cursor for "large" images.
25619 If image is not transparent (no mask), also use hollow cursor. */
25620 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25621 if (img != NULL && IMAGEP (img->spec))
25622 {
25623 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25624 where N = size of default frame font size.
25625 This should cover most of the "tiny" icons people may use. */
25626 if (!img->mask
25627 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25628 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25629 cursor_type = HOLLOW_BOX_CURSOR;
25630 }
25631 }
25632 else if (cursor_type != NO_CURSOR)
25633 {
25634 /* Display current only supports BOX and HOLLOW cursors for images.
25635 So for now, unconditionally use a HOLLOW cursor when cursor is
25636 not a solid box cursor. */
25637 cursor_type = HOLLOW_BOX_CURSOR;
25638 }
25639 }
25640 return cursor_type;
25641 }
25642
25643 /* Cursor is blinked off, so determine how to "toggle" it. */
25644
25645 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25646 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25647 return get_specified_cursor_type (XCDR (alt_cursor), width);
25648
25649 /* Then see if frame has specified a specific blink off cursor type. */
25650 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25651 {
25652 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25653 return FRAME_BLINK_OFF_CURSOR (f);
25654 }
25655
25656 #if 0
25657 /* Some people liked having a permanently visible blinking cursor,
25658 while others had very strong opinions against it. So it was
25659 decided to remove it. KFS 2003-09-03 */
25660
25661 /* Finally perform built-in cursor blinking:
25662 filled box <-> hollow box
25663 wide [h]bar <-> narrow [h]bar
25664 narrow [h]bar <-> no cursor
25665 other type <-> no cursor */
25666
25667 if (cursor_type == FILLED_BOX_CURSOR)
25668 return HOLLOW_BOX_CURSOR;
25669
25670 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25671 {
25672 *width = 1;
25673 return cursor_type;
25674 }
25675 #endif
25676
25677 return NO_CURSOR;
25678 }
25679
25680
25681 /* Notice when the text cursor of window W has been completely
25682 overwritten by a drawing operation that outputs glyphs in AREA
25683 starting at X0 and ending at X1 in the line starting at Y0 and
25684 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25685 the rest of the line after X0 has been written. Y coordinates
25686 are window-relative. */
25687
25688 static void
25689 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25690 int x0, int x1, int y0, int y1)
25691 {
25692 int cx0, cx1, cy0, cy1;
25693 struct glyph_row *row;
25694
25695 if (!w->phys_cursor_on_p)
25696 return;
25697 if (area != TEXT_AREA)
25698 return;
25699
25700 if (w->phys_cursor.vpos < 0
25701 || w->phys_cursor.vpos >= w->current_matrix->nrows
25702 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25703 !(row->enabled_p && row->displays_text_p)))
25704 return;
25705
25706 if (row->cursor_in_fringe_p)
25707 {
25708 row->cursor_in_fringe_p = 0;
25709 draw_fringe_bitmap (w, row, row->reversed_p);
25710 w->phys_cursor_on_p = 0;
25711 return;
25712 }
25713
25714 cx0 = w->phys_cursor.x;
25715 cx1 = cx0 + w->phys_cursor_width;
25716 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25717 return;
25718
25719 /* The cursor image will be completely removed from the
25720 screen if the output area intersects the cursor area in
25721 y-direction. When we draw in [y0 y1[, and some part of
25722 the cursor is at y < y0, that part must have been drawn
25723 before. When scrolling, the cursor is erased before
25724 actually scrolling, so we don't come here. When not
25725 scrolling, the rows above the old cursor row must have
25726 changed, and in this case these rows must have written
25727 over the cursor image.
25728
25729 Likewise if part of the cursor is below y1, with the
25730 exception of the cursor being in the first blank row at
25731 the buffer and window end because update_text_area
25732 doesn't draw that row. (Except when it does, but
25733 that's handled in update_text_area.) */
25734
25735 cy0 = w->phys_cursor.y;
25736 cy1 = cy0 + w->phys_cursor_height;
25737 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25738 return;
25739
25740 w->phys_cursor_on_p = 0;
25741 }
25742
25743 #endif /* HAVE_WINDOW_SYSTEM */
25744
25745 \f
25746 /************************************************************************
25747 Mouse Face
25748 ************************************************************************/
25749
25750 #ifdef HAVE_WINDOW_SYSTEM
25751
25752 /* EXPORT for RIF:
25753 Fix the display of area AREA of overlapping row ROW in window W
25754 with respect to the overlapping part OVERLAPS. */
25755
25756 void
25757 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25758 enum glyph_row_area area, int overlaps)
25759 {
25760 int i, x;
25761
25762 BLOCK_INPUT;
25763
25764 x = 0;
25765 for (i = 0; i < row->used[area];)
25766 {
25767 if (row->glyphs[area][i].overlaps_vertically_p)
25768 {
25769 int start = i, start_x = x;
25770
25771 do
25772 {
25773 x += row->glyphs[area][i].pixel_width;
25774 ++i;
25775 }
25776 while (i < row->used[area]
25777 && row->glyphs[area][i].overlaps_vertically_p);
25778
25779 draw_glyphs (w, start_x, row, area,
25780 start, i,
25781 DRAW_NORMAL_TEXT, overlaps);
25782 }
25783 else
25784 {
25785 x += row->glyphs[area][i].pixel_width;
25786 ++i;
25787 }
25788 }
25789
25790 UNBLOCK_INPUT;
25791 }
25792
25793
25794 /* EXPORT:
25795 Draw the cursor glyph of window W in glyph row ROW. See the
25796 comment of draw_glyphs for the meaning of HL. */
25797
25798 void
25799 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25800 enum draw_glyphs_face hl)
25801 {
25802 /* If cursor hpos is out of bounds, don't draw garbage. This can
25803 happen in mini-buffer windows when switching between echo area
25804 glyphs and mini-buffer. */
25805 if ((row->reversed_p
25806 ? (w->phys_cursor.hpos >= 0)
25807 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25808 {
25809 int on_p = w->phys_cursor_on_p;
25810 int x1;
25811 int hpos = w->phys_cursor.hpos;
25812
25813 /* When the window is hscrolled, cursor hpos can legitimately be
25814 out of bounds, but we draw the cursor at the corresponding
25815 window margin in that case. */
25816 if (!row->reversed_p && hpos < 0)
25817 hpos = 0;
25818 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25819 hpos = row->used[TEXT_AREA] - 1;
25820
25821 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25822 hl, 0);
25823 w->phys_cursor_on_p = on_p;
25824
25825 if (hl == DRAW_CURSOR)
25826 w->phys_cursor_width = x1 - w->phys_cursor.x;
25827 /* When we erase the cursor, and ROW is overlapped by other
25828 rows, make sure that these overlapping parts of other rows
25829 are redrawn. */
25830 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25831 {
25832 w->phys_cursor_width = x1 - w->phys_cursor.x;
25833
25834 if (row > w->current_matrix->rows
25835 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25836 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25837 OVERLAPS_ERASED_CURSOR);
25838
25839 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25840 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25841 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25842 OVERLAPS_ERASED_CURSOR);
25843 }
25844 }
25845 }
25846
25847
25848 /* EXPORT:
25849 Erase the image of a cursor of window W from the screen. */
25850
25851 void
25852 erase_phys_cursor (struct window *w)
25853 {
25854 struct frame *f = XFRAME (w->frame);
25855 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25856 int hpos = w->phys_cursor.hpos;
25857 int vpos = w->phys_cursor.vpos;
25858 int mouse_face_here_p = 0;
25859 struct glyph_matrix *active_glyphs = w->current_matrix;
25860 struct glyph_row *cursor_row;
25861 struct glyph *cursor_glyph;
25862 enum draw_glyphs_face hl;
25863
25864 /* No cursor displayed or row invalidated => nothing to do on the
25865 screen. */
25866 if (w->phys_cursor_type == NO_CURSOR)
25867 goto mark_cursor_off;
25868
25869 /* VPOS >= active_glyphs->nrows means that window has been resized.
25870 Don't bother to erase the cursor. */
25871 if (vpos >= active_glyphs->nrows)
25872 goto mark_cursor_off;
25873
25874 /* If row containing cursor is marked invalid, there is nothing we
25875 can do. */
25876 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25877 if (!cursor_row->enabled_p)
25878 goto mark_cursor_off;
25879
25880 /* If line spacing is > 0, old cursor may only be partially visible in
25881 window after split-window. So adjust visible height. */
25882 cursor_row->visible_height = min (cursor_row->visible_height,
25883 window_text_bottom_y (w) - cursor_row->y);
25884
25885 /* If row is completely invisible, don't attempt to delete a cursor which
25886 isn't there. This can happen if cursor is at top of a window, and
25887 we switch to a buffer with a header line in that window. */
25888 if (cursor_row->visible_height <= 0)
25889 goto mark_cursor_off;
25890
25891 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25892 if (cursor_row->cursor_in_fringe_p)
25893 {
25894 cursor_row->cursor_in_fringe_p = 0;
25895 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25896 goto mark_cursor_off;
25897 }
25898
25899 /* This can happen when the new row is shorter than the old one.
25900 In this case, either draw_glyphs or clear_end_of_line
25901 should have cleared the cursor. Note that we wouldn't be
25902 able to erase the cursor in this case because we don't have a
25903 cursor glyph at hand. */
25904 if ((cursor_row->reversed_p
25905 ? (w->phys_cursor.hpos < 0)
25906 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25907 goto mark_cursor_off;
25908
25909 /* When the window is hscrolled, cursor hpos can legitimately be out
25910 of bounds, but we draw the cursor at the corresponding window
25911 margin in that case. */
25912 if (!cursor_row->reversed_p && hpos < 0)
25913 hpos = 0;
25914 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25915 hpos = cursor_row->used[TEXT_AREA] - 1;
25916
25917 /* If the cursor is in the mouse face area, redisplay that when
25918 we clear the cursor. */
25919 if (! NILP (hlinfo->mouse_face_window)
25920 && coords_in_mouse_face_p (w, hpos, vpos)
25921 /* Don't redraw the cursor's spot in mouse face if it is at the
25922 end of a line (on a newline). The cursor appears there, but
25923 mouse highlighting does not. */
25924 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25925 mouse_face_here_p = 1;
25926
25927 /* Maybe clear the display under the cursor. */
25928 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25929 {
25930 int x, y, left_x;
25931 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25932 int width;
25933
25934 cursor_glyph = get_phys_cursor_glyph (w);
25935 if (cursor_glyph == NULL)
25936 goto mark_cursor_off;
25937
25938 width = cursor_glyph->pixel_width;
25939 left_x = window_box_left_offset (w, TEXT_AREA);
25940 x = w->phys_cursor.x;
25941 if (x < left_x)
25942 width -= left_x - x;
25943 width = min (width, window_box_width (w, TEXT_AREA) - x);
25944 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25945 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25946
25947 if (width > 0)
25948 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25949 }
25950
25951 /* Erase the cursor by redrawing the character underneath it. */
25952 if (mouse_face_here_p)
25953 hl = DRAW_MOUSE_FACE;
25954 else
25955 hl = DRAW_NORMAL_TEXT;
25956 draw_phys_cursor_glyph (w, cursor_row, hl);
25957
25958 mark_cursor_off:
25959 w->phys_cursor_on_p = 0;
25960 w->phys_cursor_type = NO_CURSOR;
25961 }
25962
25963
25964 /* EXPORT:
25965 Display or clear cursor of window W. If ON is zero, clear the
25966 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25967 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25968
25969 void
25970 display_and_set_cursor (struct window *w, int on,
25971 int hpos, int vpos, int x, int y)
25972 {
25973 struct frame *f = XFRAME (w->frame);
25974 int new_cursor_type;
25975 int new_cursor_width;
25976 int active_cursor;
25977 struct glyph_row *glyph_row;
25978 struct glyph *glyph;
25979
25980 /* This is pointless on invisible frames, and dangerous on garbaged
25981 windows and frames; in the latter case, the frame or window may
25982 be in the midst of changing its size, and x and y may be off the
25983 window. */
25984 if (! FRAME_VISIBLE_P (f)
25985 || FRAME_GARBAGED_P (f)
25986 || vpos >= w->current_matrix->nrows
25987 || hpos >= w->current_matrix->matrix_w)
25988 return;
25989
25990 /* If cursor is off and we want it off, return quickly. */
25991 if (!on && !w->phys_cursor_on_p)
25992 return;
25993
25994 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25995 /* If cursor row is not enabled, we don't really know where to
25996 display the cursor. */
25997 if (!glyph_row->enabled_p)
25998 {
25999 w->phys_cursor_on_p = 0;
26000 return;
26001 }
26002
26003 glyph = NULL;
26004 if (!glyph_row->exact_window_width_line_p
26005 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26006 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26007
26008 eassert (interrupt_input_blocked);
26009
26010 /* Set new_cursor_type to the cursor we want to be displayed. */
26011 new_cursor_type = get_window_cursor_type (w, glyph,
26012 &new_cursor_width, &active_cursor);
26013
26014 /* If cursor is currently being shown and we don't want it to be or
26015 it is in the wrong place, or the cursor type is not what we want,
26016 erase it. */
26017 if (w->phys_cursor_on_p
26018 && (!on
26019 || w->phys_cursor.x != x
26020 || w->phys_cursor.y != y
26021 || new_cursor_type != w->phys_cursor_type
26022 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26023 && new_cursor_width != w->phys_cursor_width)))
26024 erase_phys_cursor (w);
26025
26026 /* Don't check phys_cursor_on_p here because that flag is only set
26027 to zero in some cases where we know that the cursor has been
26028 completely erased, to avoid the extra work of erasing the cursor
26029 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26030 still not be visible, or it has only been partly erased. */
26031 if (on)
26032 {
26033 w->phys_cursor_ascent = glyph_row->ascent;
26034 w->phys_cursor_height = glyph_row->height;
26035
26036 /* Set phys_cursor_.* before x_draw_.* is called because some
26037 of them may need the information. */
26038 w->phys_cursor.x = x;
26039 w->phys_cursor.y = glyph_row->y;
26040 w->phys_cursor.hpos = hpos;
26041 w->phys_cursor.vpos = vpos;
26042 }
26043
26044 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26045 new_cursor_type, new_cursor_width,
26046 on, active_cursor);
26047 }
26048
26049
26050 /* Switch the display of W's cursor on or off, according to the value
26051 of ON. */
26052
26053 static void
26054 update_window_cursor (struct window *w, int on)
26055 {
26056 /* Don't update cursor in windows whose frame is in the process
26057 of being deleted. */
26058 if (w->current_matrix)
26059 {
26060 int hpos = w->phys_cursor.hpos;
26061 int vpos = w->phys_cursor.vpos;
26062 struct glyph_row *row;
26063
26064 if (vpos >= w->current_matrix->nrows
26065 || hpos >= w->current_matrix->matrix_w)
26066 return;
26067
26068 row = MATRIX_ROW (w->current_matrix, vpos);
26069
26070 /* When the window is hscrolled, cursor hpos can legitimately be
26071 out of bounds, but we draw the cursor at the corresponding
26072 window margin in that case. */
26073 if (!row->reversed_p && hpos < 0)
26074 hpos = 0;
26075 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26076 hpos = row->used[TEXT_AREA] - 1;
26077
26078 BLOCK_INPUT;
26079 display_and_set_cursor (w, on, hpos, vpos,
26080 w->phys_cursor.x, w->phys_cursor.y);
26081 UNBLOCK_INPUT;
26082 }
26083 }
26084
26085
26086 /* Call update_window_cursor with parameter ON_P on all leaf windows
26087 in the window tree rooted at W. */
26088
26089 static void
26090 update_cursor_in_window_tree (struct window *w, int on_p)
26091 {
26092 while (w)
26093 {
26094 if (!NILP (w->hchild))
26095 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26096 else if (!NILP (w->vchild))
26097 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26098 else
26099 update_window_cursor (w, on_p);
26100
26101 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26102 }
26103 }
26104
26105
26106 /* EXPORT:
26107 Display the cursor on window W, or clear it, according to ON_P.
26108 Don't change the cursor's position. */
26109
26110 void
26111 x_update_cursor (struct frame *f, int on_p)
26112 {
26113 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26114 }
26115
26116
26117 /* EXPORT:
26118 Clear the cursor of window W to background color, and mark the
26119 cursor as not shown. This is used when the text where the cursor
26120 is about to be rewritten. */
26121
26122 void
26123 x_clear_cursor (struct window *w)
26124 {
26125 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26126 update_window_cursor (w, 0);
26127 }
26128
26129 #endif /* HAVE_WINDOW_SYSTEM */
26130
26131 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26132 and MSDOS. */
26133 static void
26134 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26135 int start_hpos, int end_hpos,
26136 enum draw_glyphs_face draw)
26137 {
26138 #ifdef HAVE_WINDOW_SYSTEM
26139 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26140 {
26141 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26142 return;
26143 }
26144 #endif
26145 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26146 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26147 #endif
26148 }
26149
26150 /* Display the active region described by mouse_face_* according to DRAW. */
26151
26152 static void
26153 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26154 {
26155 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26156 struct frame *f = XFRAME (WINDOW_FRAME (w));
26157
26158 if (/* If window is in the process of being destroyed, don't bother
26159 to do anything. */
26160 w->current_matrix != NULL
26161 /* Don't update mouse highlight if hidden */
26162 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26163 /* Recognize when we are called to operate on rows that don't exist
26164 anymore. This can happen when a window is split. */
26165 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26166 {
26167 int phys_cursor_on_p = w->phys_cursor_on_p;
26168 struct glyph_row *row, *first, *last;
26169
26170 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26171 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26172
26173 for (row = first; row <= last && row->enabled_p; ++row)
26174 {
26175 int start_hpos, end_hpos, start_x;
26176
26177 /* For all but the first row, the highlight starts at column 0. */
26178 if (row == first)
26179 {
26180 /* R2L rows have BEG and END in reversed order, but the
26181 screen drawing geometry is always left to right. So
26182 we need to mirror the beginning and end of the
26183 highlighted area in R2L rows. */
26184 if (!row->reversed_p)
26185 {
26186 start_hpos = hlinfo->mouse_face_beg_col;
26187 start_x = hlinfo->mouse_face_beg_x;
26188 }
26189 else if (row == last)
26190 {
26191 start_hpos = hlinfo->mouse_face_end_col;
26192 start_x = hlinfo->mouse_face_end_x;
26193 }
26194 else
26195 {
26196 start_hpos = 0;
26197 start_x = 0;
26198 }
26199 }
26200 else if (row->reversed_p && row == last)
26201 {
26202 start_hpos = hlinfo->mouse_face_end_col;
26203 start_x = hlinfo->mouse_face_end_x;
26204 }
26205 else
26206 {
26207 start_hpos = 0;
26208 start_x = 0;
26209 }
26210
26211 if (row == last)
26212 {
26213 if (!row->reversed_p)
26214 end_hpos = hlinfo->mouse_face_end_col;
26215 else if (row == first)
26216 end_hpos = hlinfo->mouse_face_beg_col;
26217 else
26218 {
26219 end_hpos = row->used[TEXT_AREA];
26220 if (draw == DRAW_NORMAL_TEXT)
26221 row->fill_line_p = 1; /* Clear to end of line */
26222 }
26223 }
26224 else if (row->reversed_p && row == first)
26225 end_hpos = hlinfo->mouse_face_beg_col;
26226 else
26227 {
26228 end_hpos = row->used[TEXT_AREA];
26229 if (draw == DRAW_NORMAL_TEXT)
26230 row->fill_line_p = 1; /* Clear to end of line */
26231 }
26232
26233 if (end_hpos > start_hpos)
26234 {
26235 draw_row_with_mouse_face (w, start_x, row,
26236 start_hpos, end_hpos, draw);
26237
26238 row->mouse_face_p
26239 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26240 }
26241 }
26242
26243 #ifdef HAVE_WINDOW_SYSTEM
26244 /* When we've written over the cursor, arrange for it to
26245 be displayed again. */
26246 if (FRAME_WINDOW_P (f)
26247 && phys_cursor_on_p && !w->phys_cursor_on_p)
26248 {
26249 int hpos = w->phys_cursor.hpos;
26250
26251 /* When the window is hscrolled, cursor hpos can legitimately be
26252 out of bounds, but we draw the cursor at the corresponding
26253 window margin in that case. */
26254 if (!row->reversed_p && hpos < 0)
26255 hpos = 0;
26256 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26257 hpos = row->used[TEXT_AREA] - 1;
26258
26259 BLOCK_INPUT;
26260 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26261 w->phys_cursor.x, w->phys_cursor.y);
26262 UNBLOCK_INPUT;
26263 }
26264 #endif /* HAVE_WINDOW_SYSTEM */
26265 }
26266
26267 #ifdef HAVE_WINDOW_SYSTEM
26268 /* Change the mouse cursor. */
26269 if (FRAME_WINDOW_P (f))
26270 {
26271 if (draw == DRAW_NORMAL_TEXT
26272 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26273 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26274 else if (draw == DRAW_MOUSE_FACE)
26275 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26276 else
26277 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26278 }
26279 #endif /* HAVE_WINDOW_SYSTEM */
26280 }
26281
26282 /* EXPORT:
26283 Clear out the mouse-highlighted active region.
26284 Redraw it un-highlighted first. Value is non-zero if mouse
26285 face was actually drawn unhighlighted. */
26286
26287 int
26288 clear_mouse_face (Mouse_HLInfo *hlinfo)
26289 {
26290 int cleared = 0;
26291
26292 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26293 {
26294 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26295 cleared = 1;
26296 }
26297
26298 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26299 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26300 hlinfo->mouse_face_window = Qnil;
26301 hlinfo->mouse_face_overlay = Qnil;
26302 return cleared;
26303 }
26304
26305 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26306 within the mouse face on that window. */
26307 static int
26308 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26309 {
26310 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26311
26312 /* Quickly resolve the easy cases. */
26313 if (!(WINDOWP (hlinfo->mouse_face_window)
26314 && XWINDOW (hlinfo->mouse_face_window) == w))
26315 return 0;
26316 if (vpos < hlinfo->mouse_face_beg_row
26317 || vpos > hlinfo->mouse_face_end_row)
26318 return 0;
26319 if (vpos > hlinfo->mouse_face_beg_row
26320 && vpos < hlinfo->mouse_face_end_row)
26321 return 1;
26322
26323 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26324 {
26325 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26326 {
26327 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26328 return 1;
26329 }
26330 else if ((vpos == hlinfo->mouse_face_beg_row
26331 && hpos >= hlinfo->mouse_face_beg_col)
26332 || (vpos == hlinfo->mouse_face_end_row
26333 && hpos < hlinfo->mouse_face_end_col))
26334 return 1;
26335 }
26336 else
26337 {
26338 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26339 {
26340 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26341 return 1;
26342 }
26343 else if ((vpos == hlinfo->mouse_face_beg_row
26344 && hpos <= hlinfo->mouse_face_beg_col)
26345 || (vpos == hlinfo->mouse_face_end_row
26346 && hpos > hlinfo->mouse_face_end_col))
26347 return 1;
26348 }
26349 return 0;
26350 }
26351
26352
26353 /* EXPORT:
26354 Non-zero if physical cursor of window W is within mouse face. */
26355
26356 int
26357 cursor_in_mouse_face_p (struct window *w)
26358 {
26359 int hpos = w->phys_cursor.hpos;
26360 int vpos = w->phys_cursor.vpos;
26361 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26362
26363 /* When the window is hscrolled, cursor hpos can legitimately be out
26364 of bounds, but we draw the cursor at the corresponding window
26365 margin in that case. */
26366 if (!row->reversed_p && hpos < 0)
26367 hpos = 0;
26368 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26369 hpos = row->used[TEXT_AREA] - 1;
26370
26371 return coords_in_mouse_face_p (w, hpos, vpos);
26372 }
26373
26374
26375 \f
26376 /* Find the glyph rows START_ROW and END_ROW of window W that display
26377 characters between buffer positions START_CHARPOS and END_CHARPOS
26378 (excluding END_CHARPOS). DISP_STRING is a display string that
26379 covers these buffer positions. This is similar to
26380 row_containing_pos, but is more accurate when bidi reordering makes
26381 buffer positions change non-linearly with glyph rows. */
26382 static void
26383 rows_from_pos_range (struct window *w,
26384 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26385 Lisp_Object disp_string,
26386 struct glyph_row **start, struct glyph_row **end)
26387 {
26388 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26389 int last_y = window_text_bottom_y (w);
26390 struct glyph_row *row;
26391
26392 *start = NULL;
26393 *end = NULL;
26394
26395 while (!first->enabled_p
26396 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26397 first++;
26398
26399 /* Find the START row. */
26400 for (row = first;
26401 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26402 row++)
26403 {
26404 /* A row can potentially be the START row if the range of the
26405 characters it displays intersects the range
26406 [START_CHARPOS..END_CHARPOS). */
26407 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26408 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26409 /* See the commentary in row_containing_pos, for the
26410 explanation of the complicated way to check whether
26411 some position is beyond the end of the characters
26412 displayed by a row. */
26413 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26414 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26415 && !row->ends_at_zv_p
26416 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26417 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26418 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26419 && !row->ends_at_zv_p
26420 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26421 {
26422 /* Found a candidate row. Now make sure at least one of the
26423 glyphs it displays has a charpos from the range
26424 [START_CHARPOS..END_CHARPOS).
26425
26426 This is not obvious because bidi reordering could make
26427 buffer positions of a row be 1,2,3,102,101,100, and if we
26428 want to highlight characters in [50..60), we don't want
26429 this row, even though [50..60) does intersect [1..103),
26430 the range of character positions given by the row's start
26431 and end positions. */
26432 struct glyph *g = row->glyphs[TEXT_AREA];
26433 struct glyph *e = g + row->used[TEXT_AREA];
26434
26435 while (g < e)
26436 {
26437 if (((BUFFERP (g->object) || INTEGERP (g->object))
26438 && start_charpos <= g->charpos && g->charpos < end_charpos)
26439 /* A glyph that comes from DISP_STRING is by
26440 definition to be highlighted. */
26441 || EQ (g->object, disp_string))
26442 *start = row;
26443 g++;
26444 }
26445 if (*start)
26446 break;
26447 }
26448 }
26449
26450 /* Find the END row. */
26451 if (!*start
26452 /* If the last row is partially visible, start looking for END
26453 from that row, instead of starting from FIRST. */
26454 && !(row->enabled_p
26455 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26456 row = first;
26457 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26458 {
26459 struct glyph_row *next = row + 1;
26460 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26461
26462 if (!next->enabled_p
26463 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26464 /* The first row >= START whose range of displayed characters
26465 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26466 is the row END + 1. */
26467 || (start_charpos < next_start
26468 && end_charpos < next_start)
26469 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26470 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26471 && !next->ends_at_zv_p
26472 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26473 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26474 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26475 && !next->ends_at_zv_p
26476 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26477 {
26478 *end = row;
26479 break;
26480 }
26481 else
26482 {
26483 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26484 but none of the characters it displays are in the range, it is
26485 also END + 1. */
26486 struct glyph *g = next->glyphs[TEXT_AREA];
26487 struct glyph *s = g;
26488 struct glyph *e = g + next->used[TEXT_AREA];
26489
26490 while (g < e)
26491 {
26492 if (((BUFFERP (g->object) || INTEGERP (g->object))
26493 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26494 /* If the buffer position of the first glyph in
26495 the row is equal to END_CHARPOS, it means
26496 the last character to be highlighted is the
26497 newline of ROW, and we must consider NEXT as
26498 END, not END+1. */
26499 || (((!next->reversed_p && g == s)
26500 || (next->reversed_p && g == e - 1))
26501 && (g->charpos == end_charpos
26502 /* Special case for when NEXT is an
26503 empty line at ZV. */
26504 || (g->charpos == -1
26505 && !row->ends_at_zv_p
26506 && next_start == end_charpos)))))
26507 /* A glyph that comes from DISP_STRING is by
26508 definition to be highlighted. */
26509 || EQ (g->object, disp_string))
26510 break;
26511 g++;
26512 }
26513 if (g == e)
26514 {
26515 *end = row;
26516 break;
26517 }
26518 /* The first row that ends at ZV must be the last to be
26519 highlighted. */
26520 else if (next->ends_at_zv_p)
26521 {
26522 *end = next;
26523 break;
26524 }
26525 }
26526 }
26527 }
26528
26529 /* This function sets the mouse_face_* elements of HLINFO, assuming
26530 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26531 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26532 for the overlay or run of text properties specifying the mouse
26533 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26534 before-string and after-string that must also be highlighted.
26535 DISP_STRING, if non-nil, is a display string that may cover some
26536 or all of the highlighted text. */
26537
26538 static void
26539 mouse_face_from_buffer_pos (Lisp_Object window,
26540 Mouse_HLInfo *hlinfo,
26541 ptrdiff_t mouse_charpos,
26542 ptrdiff_t start_charpos,
26543 ptrdiff_t end_charpos,
26544 Lisp_Object before_string,
26545 Lisp_Object after_string,
26546 Lisp_Object disp_string)
26547 {
26548 struct window *w = XWINDOW (window);
26549 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26550 struct glyph_row *r1, *r2;
26551 struct glyph *glyph, *end;
26552 ptrdiff_t ignore, pos;
26553 int x;
26554
26555 eassert (NILP (disp_string) || STRINGP (disp_string));
26556 eassert (NILP (before_string) || STRINGP (before_string));
26557 eassert (NILP (after_string) || STRINGP (after_string));
26558
26559 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26560 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26561 if (r1 == NULL)
26562 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26563 /* If the before-string or display-string contains newlines,
26564 rows_from_pos_range skips to its last row. Move back. */
26565 if (!NILP (before_string) || !NILP (disp_string))
26566 {
26567 struct glyph_row *prev;
26568 while ((prev = r1 - 1, prev >= first)
26569 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26570 && prev->used[TEXT_AREA] > 0)
26571 {
26572 struct glyph *beg = prev->glyphs[TEXT_AREA];
26573 glyph = beg + prev->used[TEXT_AREA];
26574 while (--glyph >= beg && INTEGERP (glyph->object));
26575 if (glyph < beg
26576 || !(EQ (glyph->object, before_string)
26577 || EQ (glyph->object, disp_string)))
26578 break;
26579 r1 = prev;
26580 }
26581 }
26582 if (r2 == NULL)
26583 {
26584 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26585 hlinfo->mouse_face_past_end = 1;
26586 }
26587 else if (!NILP (after_string))
26588 {
26589 /* If the after-string has newlines, advance to its last row. */
26590 struct glyph_row *next;
26591 struct glyph_row *last
26592 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26593
26594 for (next = r2 + 1;
26595 next <= last
26596 && next->used[TEXT_AREA] > 0
26597 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26598 ++next)
26599 r2 = next;
26600 }
26601 /* The rest of the display engine assumes that mouse_face_beg_row is
26602 either above mouse_face_end_row or identical to it. But with
26603 bidi-reordered continued lines, the row for START_CHARPOS could
26604 be below the row for END_CHARPOS. If so, swap the rows and store
26605 them in correct order. */
26606 if (r1->y > r2->y)
26607 {
26608 struct glyph_row *tem = r2;
26609
26610 r2 = r1;
26611 r1 = tem;
26612 }
26613
26614 hlinfo->mouse_face_beg_y = r1->y;
26615 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26616 hlinfo->mouse_face_end_y = r2->y;
26617 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26618
26619 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26620 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26621 could be anywhere in the row and in any order. The strategy
26622 below is to find the leftmost and the rightmost glyph that
26623 belongs to either of these 3 strings, or whose position is
26624 between START_CHARPOS and END_CHARPOS, and highlight all the
26625 glyphs between those two. This may cover more than just the text
26626 between START_CHARPOS and END_CHARPOS if the range of characters
26627 strides the bidi level boundary, e.g. if the beginning is in R2L
26628 text while the end is in L2R text or vice versa. */
26629 if (!r1->reversed_p)
26630 {
26631 /* This row is in a left to right paragraph. Scan it left to
26632 right. */
26633 glyph = r1->glyphs[TEXT_AREA];
26634 end = glyph + r1->used[TEXT_AREA];
26635 x = r1->x;
26636
26637 /* Skip truncation glyphs at the start of the glyph row. */
26638 if (r1->displays_text_p)
26639 for (; glyph < end
26640 && INTEGERP (glyph->object)
26641 && glyph->charpos < 0;
26642 ++glyph)
26643 x += glyph->pixel_width;
26644
26645 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26646 or DISP_STRING, and the first glyph from buffer whose
26647 position is between START_CHARPOS and END_CHARPOS. */
26648 for (; glyph < end
26649 && !INTEGERP (glyph->object)
26650 && !EQ (glyph->object, disp_string)
26651 && !(BUFFERP (glyph->object)
26652 && (glyph->charpos >= start_charpos
26653 && glyph->charpos < end_charpos));
26654 ++glyph)
26655 {
26656 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26657 are present at buffer positions between START_CHARPOS and
26658 END_CHARPOS, or if they come from an overlay. */
26659 if (EQ (glyph->object, before_string))
26660 {
26661 pos = string_buffer_position (before_string,
26662 start_charpos);
26663 /* If pos == 0, it means before_string came from an
26664 overlay, not from a buffer position. */
26665 if (!pos || (pos >= start_charpos && pos < end_charpos))
26666 break;
26667 }
26668 else if (EQ (glyph->object, after_string))
26669 {
26670 pos = string_buffer_position (after_string, end_charpos);
26671 if (!pos || (pos >= start_charpos && pos < end_charpos))
26672 break;
26673 }
26674 x += glyph->pixel_width;
26675 }
26676 hlinfo->mouse_face_beg_x = x;
26677 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26678 }
26679 else
26680 {
26681 /* This row is in a right to left paragraph. Scan it right to
26682 left. */
26683 struct glyph *g;
26684
26685 end = r1->glyphs[TEXT_AREA] - 1;
26686 glyph = end + r1->used[TEXT_AREA];
26687
26688 /* Skip truncation glyphs at the start of the glyph row. */
26689 if (r1->displays_text_p)
26690 for (; glyph > end
26691 && INTEGERP (glyph->object)
26692 && glyph->charpos < 0;
26693 --glyph)
26694 ;
26695
26696 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26697 or DISP_STRING, and the first glyph from buffer whose
26698 position is between START_CHARPOS and END_CHARPOS. */
26699 for (; glyph > end
26700 && !INTEGERP (glyph->object)
26701 && !EQ (glyph->object, disp_string)
26702 && !(BUFFERP (glyph->object)
26703 && (glyph->charpos >= start_charpos
26704 && glyph->charpos < end_charpos));
26705 --glyph)
26706 {
26707 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26708 are present at buffer positions between START_CHARPOS and
26709 END_CHARPOS, or if they come from an overlay. */
26710 if (EQ (glyph->object, before_string))
26711 {
26712 pos = string_buffer_position (before_string, start_charpos);
26713 /* If pos == 0, it means before_string came from an
26714 overlay, not from a buffer position. */
26715 if (!pos || (pos >= start_charpos && pos < end_charpos))
26716 break;
26717 }
26718 else if (EQ (glyph->object, after_string))
26719 {
26720 pos = string_buffer_position (after_string, end_charpos);
26721 if (!pos || (pos >= start_charpos && pos < end_charpos))
26722 break;
26723 }
26724 }
26725
26726 glyph++; /* first glyph to the right of the highlighted area */
26727 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26728 x += g->pixel_width;
26729 hlinfo->mouse_face_beg_x = x;
26730 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26731 }
26732
26733 /* If the highlight ends in a different row, compute GLYPH and END
26734 for the end row. Otherwise, reuse the values computed above for
26735 the row where the highlight begins. */
26736 if (r2 != r1)
26737 {
26738 if (!r2->reversed_p)
26739 {
26740 glyph = r2->glyphs[TEXT_AREA];
26741 end = glyph + r2->used[TEXT_AREA];
26742 x = r2->x;
26743 }
26744 else
26745 {
26746 end = r2->glyphs[TEXT_AREA] - 1;
26747 glyph = end + r2->used[TEXT_AREA];
26748 }
26749 }
26750
26751 if (!r2->reversed_p)
26752 {
26753 /* Skip truncation and continuation glyphs near the end of the
26754 row, and also blanks and stretch glyphs inserted by
26755 extend_face_to_end_of_line. */
26756 while (end > glyph
26757 && INTEGERP ((end - 1)->object))
26758 --end;
26759 /* Scan the rest of the glyph row from the end, looking for the
26760 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26761 DISP_STRING, or whose position is between START_CHARPOS
26762 and END_CHARPOS */
26763 for (--end;
26764 end > glyph
26765 && !INTEGERP (end->object)
26766 && !EQ (end->object, disp_string)
26767 && !(BUFFERP (end->object)
26768 && (end->charpos >= start_charpos
26769 && end->charpos < end_charpos));
26770 --end)
26771 {
26772 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26773 are present at buffer positions between START_CHARPOS and
26774 END_CHARPOS, or if they come from an overlay. */
26775 if (EQ (end->object, before_string))
26776 {
26777 pos = string_buffer_position (before_string, start_charpos);
26778 if (!pos || (pos >= start_charpos && pos < end_charpos))
26779 break;
26780 }
26781 else if (EQ (end->object, after_string))
26782 {
26783 pos = string_buffer_position (after_string, end_charpos);
26784 if (!pos || (pos >= start_charpos && pos < end_charpos))
26785 break;
26786 }
26787 }
26788 /* Find the X coordinate of the last glyph to be highlighted. */
26789 for (; glyph <= end; ++glyph)
26790 x += glyph->pixel_width;
26791
26792 hlinfo->mouse_face_end_x = x;
26793 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26794 }
26795 else
26796 {
26797 /* Skip truncation and continuation glyphs near the end of the
26798 row, and also blanks and stretch glyphs inserted by
26799 extend_face_to_end_of_line. */
26800 x = r2->x;
26801 end++;
26802 while (end < glyph
26803 && INTEGERP (end->object))
26804 {
26805 x += end->pixel_width;
26806 ++end;
26807 }
26808 /* Scan the rest of the glyph row from the end, looking for the
26809 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26810 DISP_STRING, or whose position is between START_CHARPOS
26811 and END_CHARPOS */
26812 for ( ;
26813 end < glyph
26814 && !INTEGERP (end->object)
26815 && !EQ (end->object, disp_string)
26816 && !(BUFFERP (end->object)
26817 && (end->charpos >= start_charpos
26818 && end->charpos < end_charpos));
26819 ++end)
26820 {
26821 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26822 are present at buffer positions between START_CHARPOS and
26823 END_CHARPOS, or if they come from an overlay. */
26824 if (EQ (end->object, before_string))
26825 {
26826 pos = string_buffer_position (before_string, start_charpos);
26827 if (!pos || (pos >= start_charpos && pos < end_charpos))
26828 break;
26829 }
26830 else if (EQ (end->object, after_string))
26831 {
26832 pos = string_buffer_position (after_string, end_charpos);
26833 if (!pos || (pos >= start_charpos && pos < end_charpos))
26834 break;
26835 }
26836 x += end->pixel_width;
26837 }
26838 /* If we exited the above loop because we arrived at the last
26839 glyph of the row, and its buffer position is still not in
26840 range, it means the last character in range is the preceding
26841 newline. Bump the end column and x values to get past the
26842 last glyph. */
26843 if (end == glyph
26844 && BUFFERP (end->object)
26845 && (end->charpos < start_charpos
26846 || end->charpos >= end_charpos))
26847 {
26848 x += end->pixel_width;
26849 ++end;
26850 }
26851 hlinfo->mouse_face_end_x = x;
26852 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26853 }
26854
26855 hlinfo->mouse_face_window = window;
26856 hlinfo->mouse_face_face_id
26857 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26858 mouse_charpos + 1,
26859 !hlinfo->mouse_face_hidden, -1);
26860 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26861 }
26862
26863 /* The following function is not used anymore (replaced with
26864 mouse_face_from_string_pos), but I leave it here for the time
26865 being, in case someone would. */
26866
26867 #if 0 /* not used */
26868
26869 /* Find the position of the glyph for position POS in OBJECT in
26870 window W's current matrix, and return in *X, *Y the pixel
26871 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26872
26873 RIGHT_P non-zero means return the position of the right edge of the
26874 glyph, RIGHT_P zero means return the left edge position.
26875
26876 If no glyph for POS exists in the matrix, return the position of
26877 the glyph with the next smaller position that is in the matrix, if
26878 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26879 exists in the matrix, return the position of the glyph with the
26880 next larger position in OBJECT.
26881
26882 Value is non-zero if a glyph was found. */
26883
26884 static int
26885 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26886 int *hpos, int *vpos, int *x, int *y, int right_p)
26887 {
26888 int yb = window_text_bottom_y (w);
26889 struct glyph_row *r;
26890 struct glyph *best_glyph = NULL;
26891 struct glyph_row *best_row = NULL;
26892 int best_x = 0;
26893
26894 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26895 r->enabled_p && r->y < yb;
26896 ++r)
26897 {
26898 struct glyph *g = r->glyphs[TEXT_AREA];
26899 struct glyph *e = g + r->used[TEXT_AREA];
26900 int gx;
26901
26902 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26903 if (EQ (g->object, object))
26904 {
26905 if (g->charpos == pos)
26906 {
26907 best_glyph = g;
26908 best_x = gx;
26909 best_row = r;
26910 goto found;
26911 }
26912 else if (best_glyph == NULL
26913 || ((eabs (g->charpos - pos)
26914 < eabs (best_glyph->charpos - pos))
26915 && (right_p
26916 ? g->charpos < pos
26917 : g->charpos > pos)))
26918 {
26919 best_glyph = g;
26920 best_x = gx;
26921 best_row = r;
26922 }
26923 }
26924 }
26925
26926 found:
26927
26928 if (best_glyph)
26929 {
26930 *x = best_x;
26931 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26932
26933 if (right_p)
26934 {
26935 *x += best_glyph->pixel_width;
26936 ++*hpos;
26937 }
26938
26939 *y = best_row->y;
26940 *vpos = best_row - w->current_matrix->rows;
26941 }
26942
26943 return best_glyph != NULL;
26944 }
26945 #endif /* not used */
26946
26947 /* Find the positions of the first and the last glyphs in window W's
26948 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26949 (assumed to be a string), and return in HLINFO's mouse_face_*
26950 members the pixel and column/row coordinates of those glyphs. */
26951
26952 static void
26953 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26954 Lisp_Object object,
26955 ptrdiff_t startpos, ptrdiff_t endpos)
26956 {
26957 int yb = window_text_bottom_y (w);
26958 struct glyph_row *r;
26959 struct glyph *g, *e;
26960 int gx;
26961 int found = 0;
26962
26963 /* Find the glyph row with at least one position in the range
26964 [STARTPOS..ENDPOS], and the first glyph in that row whose
26965 position belongs to that range. */
26966 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26967 r->enabled_p && r->y < yb;
26968 ++r)
26969 {
26970 if (!r->reversed_p)
26971 {
26972 g = r->glyphs[TEXT_AREA];
26973 e = g + r->used[TEXT_AREA];
26974 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26975 if (EQ (g->object, object)
26976 && startpos <= g->charpos && g->charpos <= endpos)
26977 {
26978 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26979 hlinfo->mouse_face_beg_y = r->y;
26980 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26981 hlinfo->mouse_face_beg_x = gx;
26982 found = 1;
26983 break;
26984 }
26985 }
26986 else
26987 {
26988 struct glyph *g1;
26989
26990 e = r->glyphs[TEXT_AREA];
26991 g = e + r->used[TEXT_AREA];
26992 for ( ; g > e; --g)
26993 if (EQ ((g-1)->object, object)
26994 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26995 {
26996 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26997 hlinfo->mouse_face_beg_y = r->y;
26998 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26999 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27000 gx += g1->pixel_width;
27001 hlinfo->mouse_face_beg_x = gx;
27002 found = 1;
27003 break;
27004 }
27005 }
27006 if (found)
27007 break;
27008 }
27009
27010 if (!found)
27011 return;
27012
27013 /* Starting with the next row, look for the first row which does NOT
27014 include any glyphs whose positions are in the range. */
27015 for (++r; r->enabled_p && r->y < yb; ++r)
27016 {
27017 g = r->glyphs[TEXT_AREA];
27018 e = g + r->used[TEXT_AREA];
27019 found = 0;
27020 for ( ; g < e; ++g)
27021 if (EQ (g->object, object)
27022 && startpos <= g->charpos && g->charpos <= endpos)
27023 {
27024 found = 1;
27025 break;
27026 }
27027 if (!found)
27028 break;
27029 }
27030
27031 /* The highlighted region ends on the previous row. */
27032 r--;
27033
27034 /* Set the end row and its vertical pixel coordinate. */
27035 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27036 hlinfo->mouse_face_end_y = r->y;
27037
27038 /* Compute and set the end column and the end column's horizontal
27039 pixel coordinate. */
27040 if (!r->reversed_p)
27041 {
27042 g = r->glyphs[TEXT_AREA];
27043 e = g + r->used[TEXT_AREA];
27044 for ( ; e > g; --e)
27045 if (EQ ((e-1)->object, object)
27046 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27047 break;
27048 hlinfo->mouse_face_end_col = e - g;
27049
27050 for (gx = r->x; g < e; ++g)
27051 gx += g->pixel_width;
27052 hlinfo->mouse_face_end_x = gx;
27053 }
27054 else
27055 {
27056 e = r->glyphs[TEXT_AREA];
27057 g = e + r->used[TEXT_AREA];
27058 for (gx = r->x ; e < g; ++e)
27059 {
27060 if (EQ (e->object, object)
27061 && startpos <= e->charpos && e->charpos <= endpos)
27062 break;
27063 gx += e->pixel_width;
27064 }
27065 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27066 hlinfo->mouse_face_end_x = gx;
27067 }
27068 }
27069
27070 #ifdef HAVE_WINDOW_SYSTEM
27071
27072 /* See if position X, Y is within a hot-spot of an image. */
27073
27074 static int
27075 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27076 {
27077 if (!CONSP (hot_spot))
27078 return 0;
27079
27080 if (EQ (XCAR (hot_spot), Qrect))
27081 {
27082 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27083 Lisp_Object rect = XCDR (hot_spot);
27084 Lisp_Object tem;
27085 if (!CONSP (rect))
27086 return 0;
27087 if (!CONSP (XCAR (rect)))
27088 return 0;
27089 if (!CONSP (XCDR (rect)))
27090 return 0;
27091 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27092 return 0;
27093 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27094 return 0;
27095 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27096 return 0;
27097 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27098 return 0;
27099 return 1;
27100 }
27101 else if (EQ (XCAR (hot_spot), Qcircle))
27102 {
27103 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27104 Lisp_Object circ = XCDR (hot_spot);
27105 Lisp_Object lr, lx0, ly0;
27106 if (CONSP (circ)
27107 && CONSP (XCAR (circ))
27108 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27109 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27110 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27111 {
27112 double r = XFLOATINT (lr);
27113 double dx = XINT (lx0) - x;
27114 double dy = XINT (ly0) - y;
27115 return (dx * dx + dy * dy <= r * r);
27116 }
27117 }
27118 else if (EQ (XCAR (hot_spot), Qpoly))
27119 {
27120 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27121 if (VECTORP (XCDR (hot_spot)))
27122 {
27123 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27124 Lisp_Object *poly = v->contents;
27125 ptrdiff_t n = v->header.size;
27126 ptrdiff_t i;
27127 int inside = 0;
27128 Lisp_Object lx, ly;
27129 int x0, y0;
27130
27131 /* Need an even number of coordinates, and at least 3 edges. */
27132 if (n < 6 || n & 1)
27133 return 0;
27134
27135 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27136 If count is odd, we are inside polygon. Pixels on edges
27137 may or may not be included depending on actual geometry of the
27138 polygon. */
27139 if ((lx = poly[n-2], !INTEGERP (lx))
27140 || (ly = poly[n-1], !INTEGERP (lx)))
27141 return 0;
27142 x0 = XINT (lx), y0 = XINT (ly);
27143 for (i = 0; i < n; i += 2)
27144 {
27145 int x1 = x0, y1 = y0;
27146 if ((lx = poly[i], !INTEGERP (lx))
27147 || (ly = poly[i+1], !INTEGERP (ly)))
27148 return 0;
27149 x0 = XINT (lx), y0 = XINT (ly);
27150
27151 /* Does this segment cross the X line? */
27152 if (x0 >= x)
27153 {
27154 if (x1 >= x)
27155 continue;
27156 }
27157 else if (x1 < x)
27158 continue;
27159 if (y > y0 && y > y1)
27160 continue;
27161 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27162 inside = !inside;
27163 }
27164 return inside;
27165 }
27166 }
27167 return 0;
27168 }
27169
27170 Lisp_Object
27171 find_hot_spot (Lisp_Object map, int x, int y)
27172 {
27173 while (CONSP (map))
27174 {
27175 if (CONSP (XCAR (map))
27176 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27177 return XCAR (map);
27178 map = XCDR (map);
27179 }
27180
27181 return Qnil;
27182 }
27183
27184 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27185 3, 3, 0,
27186 doc: /* Lookup in image map MAP coordinates X and Y.
27187 An image map is an alist where each element has the format (AREA ID PLIST).
27188 An AREA is specified as either a rectangle, a circle, or a polygon:
27189 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27190 pixel coordinates of the upper left and bottom right corners.
27191 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27192 and the radius of the circle; r may be a float or integer.
27193 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27194 vector describes one corner in the polygon.
27195 Returns the alist element for the first matching AREA in MAP. */)
27196 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27197 {
27198 if (NILP (map))
27199 return Qnil;
27200
27201 CHECK_NUMBER (x);
27202 CHECK_NUMBER (y);
27203
27204 return find_hot_spot (map,
27205 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27206 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27207 }
27208
27209
27210 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27211 static void
27212 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27213 {
27214 /* Do not change cursor shape while dragging mouse. */
27215 if (!NILP (do_mouse_tracking))
27216 return;
27217
27218 if (!NILP (pointer))
27219 {
27220 if (EQ (pointer, Qarrow))
27221 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27222 else if (EQ (pointer, Qhand))
27223 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27224 else if (EQ (pointer, Qtext))
27225 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27226 else if (EQ (pointer, intern ("hdrag")))
27227 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27228 #ifdef HAVE_X_WINDOWS
27229 else if (EQ (pointer, intern ("vdrag")))
27230 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27231 #endif
27232 else if (EQ (pointer, intern ("hourglass")))
27233 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27234 else if (EQ (pointer, Qmodeline))
27235 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27236 else
27237 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27238 }
27239
27240 if (cursor != No_Cursor)
27241 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27242 }
27243
27244 #endif /* HAVE_WINDOW_SYSTEM */
27245
27246 /* Take proper action when mouse has moved to the mode or header line
27247 or marginal area AREA of window W, x-position X and y-position Y.
27248 X is relative to the start of the text display area of W, so the
27249 width of bitmap areas and scroll bars must be subtracted to get a
27250 position relative to the start of the mode line. */
27251
27252 static void
27253 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27254 enum window_part area)
27255 {
27256 struct window *w = XWINDOW (window);
27257 struct frame *f = XFRAME (w->frame);
27258 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27259 #ifdef HAVE_WINDOW_SYSTEM
27260 Display_Info *dpyinfo;
27261 #endif
27262 Cursor cursor = No_Cursor;
27263 Lisp_Object pointer = Qnil;
27264 int dx, dy, width, height;
27265 ptrdiff_t charpos;
27266 Lisp_Object string, object = Qnil;
27267 Lisp_Object pos IF_LINT (= Qnil), help;
27268
27269 Lisp_Object mouse_face;
27270 int original_x_pixel = x;
27271 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27272 struct glyph_row *row IF_LINT (= 0);
27273
27274 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27275 {
27276 int x0;
27277 struct glyph *end;
27278
27279 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27280 returns them in row/column units! */
27281 string = mode_line_string (w, area, &x, &y, &charpos,
27282 &object, &dx, &dy, &width, &height);
27283
27284 row = (area == ON_MODE_LINE
27285 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27286 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27287
27288 /* Find the glyph under the mouse pointer. */
27289 if (row->mode_line_p && row->enabled_p)
27290 {
27291 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27292 end = glyph + row->used[TEXT_AREA];
27293
27294 for (x0 = original_x_pixel;
27295 glyph < end && x0 >= glyph->pixel_width;
27296 ++glyph)
27297 x0 -= glyph->pixel_width;
27298
27299 if (glyph >= end)
27300 glyph = NULL;
27301 }
27302 }
27303 else
27304 {
27305 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27306 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27307 returns them in row/column units! */
27308 string = marginal_area_string (w, area, &x, &y, &charpos,
27309 &object, &dx, &dy, &width, &height);
27310 }
27311
27312 help = Qnil;
27313
27314 #ifdef HAVE_WINDOW_SYSTEM
27315 if (IMAGEP (object))
27316 {
27317 Lisp_Object image_map, hotspot;
27318 if ((image_map = Fplist_get (XCDR (object), QCmap),
27319 !NILP (image_map))
27320 && (hotspot = find_hot_spot (image_map, dx, dy),
27321 CONSP (hotspot))
27322 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27323 {
27324 Lisp_Object plist;
27325
27326 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27327 If so, we could look for mouse-enter, mouse-leave
27328 properties in PLIST (and do something...). */
27329 hotspot = XCDR (hotspot);
27330 if (CONSP (hotspot)
27331 && (plist = XCAR (hotspot), CONSP (plist)))
27332 {
27333 pointer = Fplist_get (plist, Qpointer);
27334 if (NILP (pointer))
27335 pointer = Qhand;
27336 help = Fplist_get (plist, Qhelp_echo);
27337 if (!NILP (help))
27338 {
27339 help_echo_string = help;
27340 XSETWINDOW (help_echo_window, w);
27341 help_echo_object = w->buffer;
27342 help_echo_pos = charpos;
27343 }
27344 }
27345 }
27346 if (NILP (pointer))
27347 pointer = Fplist_get (XCDR (object), QCpointer);
27348 }
27349 #endif /* HAVE_WINDOW_SYSTEM */
27350
27351 if (STRINGP (string))
27352 pos = make_number (charpos);
27353
27354 /* Set the help text and mouse pointer. If the mouse is on a part
27355 of the mode line without any text (e.g. past the right edge of
27356 the mode line text), use the default help text and pointer. */
27357 if (STRINGP (string) || area == ON_MODE_LINE)
27358 {
27359 /* Arrange to display the help by setting the global variables
27360 help_echo_string, help_echo_object, and help_echo_pos. */
27361 if (NILP (help))
27362 {
27363 if (STRINGP (string))
27364 help = Fget_text_property (pos, Qhelp_echo, string);
27365
27366 if (!NILP (help))
27367 {
27368 help_echo_string = help;
27369 XSETWINDOW (help_echo_window, w);
27370 help_echo_object = string;
27371 help_echo_pos = charpos;
27372 }
27373 else if (area == ON_MODE_LINE)
27374 {
27375 Lisp_Object default_help
27376 = buffer_local_value_1 (Qmode_line_default_help_echo,
27377 w->buffer);
27378
27379 if (STRINGP (default_help))
27380 {
27381 help_echo_string = default_help;
27382 XSETWINDOW (help_echo_window, w);
27383 help_echo_object = Qnil;
27384 help_echo_pos = -1;
27385 }
27386 }
27387 }
27388
27389 #ifdef HAVE_WINDOW_SYSTEM
27390 /* Change the mouse pointer according to what is under it. */
27391 if (FRAME_WINDOW_P (f))
27392 {
27393 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27394 if (STRINGP (string))
27395 {
27396 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27397
27398 if (NILP (pointer))
27399 pointer = Fget_text_property (pos, Qpointer, string);
27400
27401 /* Change the mouse pointer according to what is under X/Y. */
27402 if (NILP (pointer)
27403 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27404 {
27405 Lisp_Object map;
27406 map = Fget_text_property (pos, Qlocal_map, string);
27407 if (!KEYMAPP (map))
27408 map = Fget_text_property (pos, Qkeymap, string);
27409 if (!KEYMAPP (map))
27410 cursor = dpyinfo->vertical_scroll_bar_cursor;
27411 }
27412 }
27413 else
27414 /* Default mode-line pointer. */
27415 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27416 }
27417 #endif
27418 }
27419
27420 /* Change the mouse face according to what is under X/Y. */
27421 if (STRINGP (string))
27422 {
27423 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27424 if (!NILP (mouse_face)
27425 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27426 && glyph)
27427 {
27428 Lisp_Object b, e;
27429
27430 struct glyph * tmp_glyph;
27431
27432 int gpos;
27433 int gseq_length;
27434 int total_pixel_width;
27435 ptrdiff_t begpos, endpos, ignore;
27436
27437 int vpos, hpos;
27438
27439 b = Fprevious_single_property_change (make_number (charpos + 1),
27440 Qmouse_face, string, Qnil);
27441 if (NILP (b))
27442 begpos = 0;
27443 else
27444 begpos = XINT (b);
27445
27446 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27447 if (NILP (e))
27448 endpos = SCHARS (string);
27449 else
27450 endpos = XINT (e);
27451
27452 /* Calculate the glyph position GPOS of GLYPH in the
27453 displayed string, relative to the beginning of the
27454 highlighted part of the string.
27455
27456 Note: GPOS is different from CHARPOS. CHARPOS is the
27457 position of GLYPH in the internal string object. A mode
27458 line string format has structures which are converted to
27459 a flattened string by the Emacs Lisp interpreter. The
27460 internal string is an element of those structures. The
27461 displayed string is the flattened string. */
27462 tmp_glyph = row_start_glyph;
27463 while (tmp_glyph < glyph
27464 && (!(EQ (tmp_glyph->object, glyph->object)
27465 && begpos <= tmp_glyph->charpos
27466 && tmp_glyph->charpos < endpos)))
27467 tmp_glyph++;
27468 gpos = glyph - tmp_glyph;
27469
27470 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27471 the highlighted part of the displayed string to which
27472 GLYPH belongs. Note: GSEQ_LENGTH is different from
27473 SCHARS (STRING), because the latter returns the length of
27474 the internal string. */
27475 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27476 tmp_glyph > glyph
27477 && (!(EQ (tmp_glyph->object, glyph->object)
27478 && begpos <= tmp_glyph->charpos
27479 && tmp_glyph->charpos < endpos));
27480 tmp_glyph--)
27481 ;
27482 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27483
27484 /* Calculate the total pixel width of all the glyphs between
27485 the beginning of the highlighted area and GLYPH. */
27486 total_pixel_width = 0;
27487 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27488 total_pixel_width += tmp_glyph->pixel_width;
27489
27490 /* Pre calculation of re-rendering position. Note: X is in
27491 column units here, after the call to mode_line_string or
27492 marginal_area_string. */
27493 hpos = x - gpos;
27494 vpos = (area == ON_MODE_LINE
27495 ? (w->current_matrix)->nrows - 1
27496 : 0);
27497
27498 /* If GLYPH's position is included in the region that is
27499 already drawn in mouse face, we have nothing to do. */
27500 if ( EQ (window, hlinfo->mouse_face_window)
27501 && (!row->reversed_p
27502 ? (hlinfo->mouse_face_beg_col <= hpos
27503 && hpos < hlinfo->mouse_face_end_col)
27504 /* In R2L rows we swap BEG and END, see below. */
27505 : (hlinfo->mouse_face_end_col <= hpos
27506 && hpos < hlinfo->mouse_face_beg_col))
27507 && hlinfo->mouse_face_beg_row == vpos )
27508 return;
27509
27510 if (clear_mouse_face (hlinfo))
27511 cursor = No_Cursor;
27512
27513 if (!row->reversed_p)
27514 {
27515 hlinfo->mouse_face_beg_col = hpos;
27516 hlinfo->mouse_face_beg_x = original_x_pixel
27517 - (total_pixel_width + dx);
27518 hlinfo->mouse_face_end_col = hpos + gseq_length;
27519 hlinfo->mouse_face_end_x = 0;
27520 }
27521 else
27522 {
27523 /* In R2L rows, show_mouse_face expects BEG and END
27524 coordinates to be swapped. */
27525 hlinfo->mouse_face_end_col = hpos;
27526 hlinfo->mouse_face_end_x = original_x_pixel
27527 - (total_pixel_width + dx);
27528 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27529 hlinfo->mouse_face_beg_x = 0;
27530 }
27531
27532 hlinfo->mouse_face_beg_row = vpos;
27533 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27534 hlinfo->mouse_face_beg_y = 0;
27535 hlinfo->mouse_face_end_y = 0;
27536 hlinfo->mouse_face_past_end = 0;
27537 hlinfo->mouse_face_window = window;
27538
27539 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27540 charpos,
27541 0, 0, 0,
27542 &ignore,
27543 glyph->face_id,
27544 1);
27545 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27546
27547 if (NILP (pointer))
27548 pointer = Qhand;
27549 }
27550 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27551 clear_mouse_face (hlinfo);
27552 }
27553 #ifdef HAVE_WINDOW_SYSTEM
27554 if (FRAME_WINDOW_P (f))
27555 define_frame_cursor1 (f, cursor, pointer);
27556 #endif
27557 }
27558
27559
27560 /* EXPORT:
27561 Take proper action when the mouse has moved to position X, Y on
27562 frame F as regards highlighting characters that have mouse-face
27563 properties. Also de-highlighting chars where the mouse was before.
27564 X and Y can be negative or out of range. */
27565
27566 void
27567 note_mouse_highlight (struct frame *f, int x, int y)
27568 {
27569 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27570 enum window_part part = ON_NOTHING;
27571 Lisp_Object window;
27572 struct window *w;
27573 Cursor cursor = No_Cursor;
27574 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27575 struct buffer *b;
27576
27577 /* When a menu is active, don't highlight because this looks odd. */
27578 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27579 if (popup_activated ())
27580 return;
27581 #endif
27582
27583 if (NILP (Vmouse_highlight)
27584 || !f->glyphs_initialized_p
27585 || f->pointer_invisible)
27586 return;
27587
27588 hlinfo->mouse_face_mouse_x = x;
27589 hlinfo->mouse_face_mouse_y = y;
27590 hlinfo->mouse_face_mouse_frame = f;
27591
27592 if (hlinfo->mouse_face_defer)
27593 return;
27594
27595 if (gc_in_progress)
27596 {
27597 hlinfo->mouse_face_deferred_gc = 1;
27598 return;
27599 }
27600
27601 /* Which window is that in? */
27602 window = window_from_coordinates (f, x, y, &part, 1);
27603
27604 /* If displaying active text in another window, clear that. */
27605 if (! EQ (window, hlinfo->mouse_face_window)
27606 /* Also clear if we move out of text area in same window. */
27607 || (!NILP (hlinfo->mouse_face_window)
27608 && !NILP (window)
27609 && part != ON_TEXT
27610 && part != ON_MODE_LINE
27611 && part != ON_HEADER_LINE))
27612 clear_mouse_face (hlinfo);
27613
27614 /* Not on a window -> return. */
27615 if (!WINDOWP (window))
27616 return;
27617
27618 /* Reset help_echo_string. It will get recomputed below. */
27619 help_echo_string = Qnil;
27620
27621 /* Convert to window-relative pixel coordinates. */
27622 w = XWINDOW (window);
27623 frame_to_window_pixel_xy (w, &x, &y);
27624
27625 #ifdef HAVE_WINDOW_SYSTEM
27626 /* Handle tool-bar window differently since it doesn't display a
27627 buffer. */
27628 if (EQ (window, f->tool_bar_window))
27629 {
27630 note_tool_bar_highlight (f, x, y);
27631 return;
27632 }
27633 #endif
27634
27635 /* Mouse is on the mode, header line or margin? */
27636 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27637 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27638 {
27639 note_mode_line_or_margin_highlight (window, x, y, part);
27640 return;
27641 }
27642
27643 #ifdef HAVE_WINDOW_SYSTEM
27644 if (part == ON_VERTICAL_BORDER)
27645 {
27646 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27647 help_echo_string = build_string ("drag-mouse-1: resize");
27648 }
27649 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27650 || part == ON_SCROLL_BAR)
27651 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27652 else
27653 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27654 #endif
27655
27656 /* Are we in a window whose display is up to date?
27657 And verify the buffer's text has not changed. */
27658 b = XBUFFER (w->buffer);
27659 if (part == ON_TEXT
27660 && EQ (w->window_end_valid, w->buffer)
27661 && w->last_modified == BUF_MODIFF (b)
27662 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27663 {
27664 int hpos, vpos, dx, dy, area = LAST_AREA;
27665 ptrdiff_t pos;
27666 struct glyph *glyph;
27667 Lisp_Object object;
27668 Lisp_Object mouse_face = Qnil, position;
27669 Lisp_Object *overlay_vec = NULL;
27670 ptrdiff_t i, noverlays;
27671 struct buffer *obuf;
27672 ptrdiff_t obegv, ozv;
27673 int same_region;
27674
27675 /* Find the glyph under X/Y. */
27676 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27677
27678 #ifdef HAVE_WINDOW_SYSTEM
27679 /* Look for :pointer property on image. */
27680 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27681 {
27682 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27683 if (img != NULL && IMAGEP (img->spec))
27684 {
27685 Lisp_Object image_map, hotspot;
27686 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27687 !NILP (image_map))
27688 && (hotspot = find_hot_spot (image_map,
27689 glyph->slice.img.x + dx,
27690 glyph->slice.img.y + dy),
27691 CONSP (hotspot))
27692 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27693 {
27694 Lisp_Object plist;
27695
27696 /* Could check XCAR (hotspot) to see if we enter/leave
27697 this hot-spot.
27698 If so, we could look for mouse-enter, mouse-leave
27699 properties in PLIST (and do something...). */
27700 hotspot = XCDR (hotspot);
27701 if (CONSP (hotspot)
27702 && (plist = XCAR (hotspot), CONSP (plist)))
27703 {
27704 pointer = Fplist_get (plist, Qpointer);
27705 if (NILP (pointer))
27706 pointer = Qhand;
27707 help_echo_string = Fplist_get (plist, Qhelp_echo);
27708 if (!NILP (help_echo_string))
27709 {
27710 help_echo_window = window;
27711 help_echo_object = glyph->object;
27712 help_echo_pos = glyph->charpos;
27713 }
27714 }
27715 }
27716 if (NILP (pointer))
27717 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27718 }
27719 }
27720 #endif /* HAVE_WINDOW_SYSTEM */
27721
27722 /* Clear mouse face if X/Y not over text. */
27723 if (glyph == NULL
27724 || area != TEXT_AREA
27725 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27726 /* Glyph's OBJECT is an integer for glyphs inserted by the
27727 display engine for its internal purposes, like truncation
27728 and continuation glyphs and blanks beyond the end of
27729 line's text on text terminals. If we are over such a
27730 glyph, we are not over any text. */
27731 || INTEGERP (glyph->object)
27732 /* R2L rows have a stretch glyph at their front, which
27733 stands for no text, whereas L2R rows have no glyphs at
27734 all beyond the end of text. Treat such stretch glyphs
27735 like we do with NULL glyphs in L2R rows. */
27736 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27737 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27738 && glyph->type == STRETCH_GLYPH
27739 && glyph->avoid_cursor_p))
27740 {
27741 if (clear_mouse_face (hlinfo))
27742 cursor = No_Cursor;
27743 #ifdef HAVE_WINDOW_SYSTEM
27744 if (FRAME_WINDOW_P (f) && NILP (pointer))
27745 {
27746 if (area != TEXT_AREA)
27747 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27748 else
27749 pointer = Vvoid_text_area_pointer;
27750 }
27751 #endif
27752 goto set_cursor;
27753 }
27754
27755 pos = glyph->charpos;
27756 object = glyph->object;
27757 if (!STRINGP (object) && !BUFFERP (object))
27758 goto set_cursor;
27759
27760 /* If we get an out-of-range value, return now; avoid an error. */
27761 if (BUFFERP (object) && pos > BUF_Z (b))
27762 goto set_cursor;
27763
27764 /* Make the window's buffer temporarily current for
27765 overlays_at and compute_char_face. */
27766 obuf = current_buffer;
27767 current_buffer = b;
27768 obegv = BEGV;
27769 ozv = ZV;
27770 BEGV = BEG;
27771 ZV = Z;
27772
27773 /* Is this char mouse-active or does it have help-echo? */
27774 position = make_number (pos);
27775
27776 if (BUFFERP (object))
27777 {
27778 /* Put all the overlays we want in a vector in overlay_vec. */
27779 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27780 /* Sort overlays into increasing priority order. */
27781 noverlays = sort_overlays (overlay_vec, noverlays, w);
27782 }
27783 else
27784 noverlays = 0;
27785
27786 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27787
27788 if (same_region)
27789 cursor = No_Cursor;
27790
27791 /* Check mouse-face highlighting. */
27792 if (! same_region
27793 /* If there exists an overlay with mouse-face overlapping
27794 the one we are currently highlighting, we have to
27795 check if we enter the overlapping overlay, and then
27796 highlight only that. */
27797 || (OVERLAYP (hlinfo->mouse_face_overlay)
27798 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27799 {
27800 /* Find the highest priority overlay with a mouse-face. */
27801 Lisp_Object overlay = Qnil;
27802 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27803 {
27804 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27805 if (!NILP (mouse_face))
27806 overlay = overlay_vec[i];
27807 }
27808
27809 /* If we're highlighting the same overlay as before, there's
27810 no need to do that again. */
27811 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27812 goto check_help_echo;
27813 hlinfo->mouse_face_overlay = overlay;
27814
27815 /* Clear the display of the old active region, if any. */
27816 if (clear_mouse_face (hlinfo))
27817 cursor = No_Cursor;
27818
27819 /* If no overlay applies, get a text property. */
27820 if (NILP (overlay))
27821 mouse_face = Fget_text_property (position, Qmouse_face, object);
27822
27823 /* Next, compute the bounds of the mouse highlighting and
27824 display it. */
27825 if (!NILP (mouse_face) && STRINGP (object))
27826 {
27827 /* The mouse-highlighting comes from a display string
27828 with a mouse-face. */
27829 Lisp_Object s, e;
27830 ptrdiff_t ignore;
27831
27832 s = Fprevious_single_property_change
27833 (make_number (pos + 1), Qmouse_face, object, Qnil);
27834 e = Fnext_single_property_change
27835 (position, Qmouse_face, object, Qnil);
27836 if (NILP (s))
27837 s = make_number (0);
27838 if (NILP (e))
27839 e = make_number (SCHARS (object) - 1);
27840 mouse_face_from_string_pos (w, hlinfo, object,
27841 XINT (s), XINT (e));
27842 hlinfo->mouse_face_past_end = 0;
27843 hlinfo->mouse_face_window = window;
27844 hlinfo->mouse_face_face_id
27845 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27846 glyph->face_id, 1);
27847 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27848 cursor = No_Cursor;
27849 }
27850 else
27851 {
27852 /* The mouse-highlighting, if any, comes from an overlay
27853 or text property in the buffer. */
27854 Lisp_Object buffer IF_LINT (= Qnil);
27855 Lisp_Object disp_string IF_LINT (= Qnil);
27856
27857 if (STRINGP (object))
27858 {
27859 /* If we are on a display string with no mouse-face,
27860 check if the text under it has one. */
27861 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27862 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27863 pos = string_buffer_position (object, start);
27864 if (pos > 0)
27865 {
27866 mouse_face = get_char_property_and_overlay
27867 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27868 buffer = w->buffer;
27869 disp_string = object;
27870 }
27871 }
27872 else
27873 {
27874 buffer = object;
27875 disp_string = Qnil;
27876 }
27877
27878 if (!NILP (mouse_face))
27879 {
27880 Lisp_Object before, after;
27881 Lisp_Object before_string, after_string;
27882 /* To correctly find the limits of mouse highlight
27883 in a bidi-reordered buffer, we must not use the
27884 optimization of limiting the search in
27885 previous-single-property-change and
27886 next-single-property-change, because
27887 rows_from_pos_range needs the real start and end
27888 positions to DTRT in this case. That's because
27889 the first row visible in a window does not
27890 necessarily display the character whose position
27891 is the smallest. */
27892 Lisp_Object lim1 =
27893 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27894 ? Fmarker_position (w->start)
27895 : Qnil;
27896 Lisp_Object lim2 =
27897 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27898 ? make_number (BUF_Z (XBUFFER (buffer))
27899 - XFASTINT (w->window_end_pos))
27900 : Qnil;
27901
27902 if (NILP (overlay))
27903 {
27904 /* Handle the text property case. */
27905 before = Fprevious_single_property_change
27906 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27907 after = Fnext_single_property_change
27908 (make_number (pos), Qmouse_face, buffer, lim2);
27909 before_string = after_string = Qnil;
27910 }
27911 else
27912 {
27913 /* Handle the overlay case. */
27914 before = Foverlay_start (overlay);
27915 after = Foverlay_end (overlay);
27916 before_string = Foverlay_get (overlay, Qbefore_string);
27917 after_string = Foverlay_get (overlay, Qafter_string);
27918
27919 if (!STRINGP (before_string)) before_string = Qnil;
27920 if (!STRINGP (after_string)) after_string = Qnil;
27921 }
27922
27923 mouse_face_from_buffer_pos (window, hlinfo, pos,
27924 NILP (before)
27925 ? 1
27926 : XFASTINT (before),
27927 NILP (after)
27928 ? BUF_Z (XBUFFER (buffer))
27929 : XFASTINT (after),
27930 before_string, after_string,
27931 disp_string);
27932 cursor = No_Cursor;
27933 }
27934 }
27935 }
27936
27937 check_help_echo:
27938
27939 /* Look for a `help-echo' property. */
27940 if (NILP (help_echo_string)) {
27941 Lisp_Object help, overlay;
27942
27943 /* Check overlays first. */
27944 help = overlay = Qnil;
27945 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27946 {
27947 overlay = overlay_vec[i];
27948 help = Foverlay_get (overlay, Qhelp_echo);
27949 }
27950
27951 if (!NILP (help))
27952 {
27953 help_echo_string = help;
27954 help_echo_window = window;
27955 help_echo_object = overlay;
27956 help_echo_pos = pos;
27957 }
27958 else
27959 {
27960 Lisp_Object obj = glyph->object;
27961 ptrdiff_t charpos = glyph->charpos;
27962
27963 /* Try text properties. */
27964 if (STRINGP (obj)
27965 && charpos >= 0
27966 && charpos < SCHARS (obj))
27967 {
27968 help = Fget_text_property (make_number (charpos),
27969 Qhelp_echo, obj);
27970 if (NILP (help))
27971 {
27972 /* If the string itself doesn't specify a help-echo,
27973 see if the buffer text ``under'' it does. */
27974 struct glyph_row *r
27975 = MATRIX_ROW (w->current_matrix, vpos);
27976 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27977 ptrdiff_t p = string_buffer_position (obj, start);
27978 if (p > 0)
27979 {
27980 help = Fget_char_property (make_number (p),
27981 Qhelp_echo, w->buffer);
27982 if (!NILP (help))
27983 {
27984 charpos = p;
27985 obj = w->buffer;
27986 }
27987 }
27988 }
27989 }
27990 else if (BUFFERP (obj)
27991 && charpos >= BEGV
27992 && charpos < ZV)
27993 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27994 obj);
27995
27996 if (!NILP (help))
27997 {
27998 help_echo_string = help;
27999 help_echo_window = window;
28000 help_echo_object = obj;
28001 help_echo_pos = charpos;
28002 }
28003 }
28004 }
28005
28006 #ifdef HAVE_WINDOW_SYSTEM
28007 /* Look for a `pointer' property. */
28008 if (FRAME_WINDOW_P (f) && NILP (pointer))
28009 {
28010 /* Check overlays first. */
28011 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28012 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28013
28014 if (NILP (pointer))
28015 {
28016 Lisp_Object obj = glyph->object;
28017 ptrdiff_t charpos = glyph->charpos;
28018
28019 /* Try text properties. */
28020 if (STRINGP (obj)
28021 && charpos >= 0
28022 && charpos < SCHARS (obj))
28023 {
28024 pointer = Fget_text_property (make_number (charpos),
28025 Qpointer, obj);
28026 if (NILP (pointer))
28027 {
28028 /* If the string itself doesn't specify a pointer,
28029 see if the buffer text ``under'' it does. */
28030 struct glyph_row *r
28031 = MATRIX_ROW (w->current_matrix, vpos);
28032 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28033 ptrdiff_t p = string_buffer_position (obj, start);
28034 if (p > 0)
28035 pointer = Fget_char_property (make_number (p),
28036 Qpointer, w->buffer);
28037 }
28038 }
28039 else if (BUFFERP (obj)
28040 && charpos >= BEGV
28041 && charpos < ZV)
28042 pointer = Fget_text_property (make_number (charpos),
28043 Qpointer, obj);
28044 }
28045 }
28046 #endif /* HAVE_WINDOW_SYSTEM */
28047
28048 BEGV = obegv;
28049 ZV = ozv;
28050 current_buffer = obuf;
28051 }
28052
28053 set_cursor:
28054
28055 #ifdef HAVE_WINDOW_SYSTEM
28056 if (FRAME_WINDOW_P (f))
28057 define_frame_cursor1 (f, cursor, pointer);
28058 #else
28059 /* This is here to prevent a compiler error, about "label at end of
28060 compound statement". */
28061 return;
28062 #endif
28063 }
28064
28065
28066 /* EXPORT for RIF:
28067 Clear any mouse-face on window W. This function is part of the
28068 redisplay interface, and is called from try_window_id and similar
28069 functions to ensure the mouse-highlight is off. */
28070
28071 void
28072 x_clear_window_mouse_face (struct window *w)
28073 {
28074 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28075 Lisp_Object window;
28076
28077 BLOCK_INPUT;
28078 XSETWINDOW (window, w);
28079 if (EQ (window, hlinfo->mouse_face_window))
28080 clear_mouse_face (hlinfo);
28081 UNBLOCK_INPUT;
28082 }
28083
28084
28085 /* EXPORT:
28086 Just discard the mouse face information for frame F, if any.
28087 This is used when the size of F is changed. */
28088
28089 void
28090 cancel_mouse_face (struct frame *f)
28091 {
28092 Lisp_Object window;
28093 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28094
28095 window = hlinfo->mouse_face_window;
28096 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28097 {
28098 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28099 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28100 hlinfo->mouse_face_window = Qnil;
28101 }
28102 }
28103
28104
28105 \f
28106 /***********************************************************************
28107 Exposure Events
28108 ***********************************************************************/
28109
28110 #ifdef HAVE_WINDOW_SYSTEM
28111
28112 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28113 which intersects rectangle R. R is in window-relative coordinates. */
28114
28115 static void
28116 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28117 enum glyph_row_area area)
28118 {
28119 struct glyph *first = row->glyphs[area];
28120 struct glyph *end = row->glyphs[area] + row->used[area];
28121 struct glyph *last;
28122 int first_x, start_x, x;
28123
28124 if (area == TEXT_AREA && row->fill_line_p)
28125 /* If row extends face to end of line write the whole line. */
28126 draw_glyphs (w, 0, row, area,
28127 0, row->used[area],
28128 DRAW_NORMAL_TEXT, 0);
28129 else
28130 {
28131 /* Set START_X to the window-relative start position for drawing glyphs of
28132 AREA. The first glyph of the text area can be partially visible.
28133 The first glyphs of other areas cannot. */
28134 start_x = window_box_left_offset (w, area);
28135 x = start_x;
28136 if (area == TEXT_AREA)
28137 x += row->x;
28138
28139 /* Find the first glyph that must be redrawn. */
28140 while (first < end
28141 && x + first->pixel_width < r->x)
28142 {
28143 x += first->pixel_width;
28144 ++first;
28145 }
28146
28147 /* Find the last one. */
28148 last = first;
28149 first_x = x;
28150 while (last < end
28151 && x < r->x + r->width)
28152 {
28153 x += last->pixel_width;
28154 ++last;
28155 }
28156
28157 /* Repaint. */
28158 if (last > first)
28159 draw_glyphs (w, first_x - start_x, row, area,
28160 first - row->glyphs[area], last - row->glyphs[area],
28161 DRAW_NORMAL_TEXT, 0);
28162 }
28163 }
28164
28165
28166 /* Redraw the parts of the glyph row ROW on window W intersecting
28167 rectangle R. R is in window-relative coordinates. Value is
28168 non-zero if mouse-face was overwritten. */
28169
28170 static int
28171 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28172 {
28173 eassert (row->enabled_p);
28174
28175 if (row->mode_line_p || w->pseudo_window_p)
28176 draw_glyphs (w, 0, row, TEXT_AREA,
28177 0, row->used[TEXT_AREA],
28178 DRAW_NORMAL_TEXT, 0);
28179 else
28180 {
28181 if (row->used[LEFT_MARGIN_AREA])
28182 expose_area (w, row, r, LEFT_MARGIN_AREA);
28183 if (row->used[TEXT_AREA])
28184 expose_area (w, row, r, TEXT_AREA);
28185 if (row->used[RIGHT_MARGIN_AREA])
28186 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28187 draw_row_fringe_bitmaps (w, row);
28188 }
28189
28190 return row->mouse_face_p;
28191 }
28192
28193
28194 /* Redraw those parts of glyphs rows during expose event handling that
28195 overlap other rows. Redrawing of an exposed line writes over parts
28196 of lines overlapping that exposed line; this function fixes that.
28197
28198 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28199 row in W's current matrix that is exposed and overlaps other rows.
28200 LAST_OVERLAPPING_ROW is the last such row. */
28201
28202 static void
28203 expose_overlaps (struct window *w,
28204 struct glyph_row *first_overlapping_row,
28205 struct glyph_row *last_overlapping_row,
28206 XRectangle *r)
28207 {
28208 struct glyph_row *row;
28209
28210 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28211 if (row->overlapping_p)
28212 {
28213 eassert (row->enabled_p && !row->mode_line_p);
28214
28215 row->clip = r;
28216 if (row->used[LEFT_MARGIN_AREA])
28217 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28218
28219 if (row->used[TEXT_AREA])
28220 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28221
28222 if (row->used[RIGHT_MARGIN_AREA])
28223 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28224 row->clip = NULL;
28225 }
28226 }
28227
28228
28229 /* Return non-zero if W's cursor intersects rectangle R. */
28230
28231 static int
28232 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28233 {
28234 XRectangle cr, result;
28235 struct glyph *cursor_glyph;
28236 struct glyph_row *row;
28237
28238 if (w->phys_cursor.vpos >= 0
28239 && w->phys_cursor.vpos < w->current_matrix->nrows
28240 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28241 row->enabled_p)
28242 && row->cursor_in_fringe_p)
28243 {
28244 /* Cursor is in the fringe. */
28245 cr.x = window_box_right_offset (w,
28246 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28247 ? RIGHT_MARGIN_AREA
28248 : TEXT_AREA));
28249 cr.y = row->y;
28250 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28251 cr.height = row->height;
28252 return x_intersect_rectangles (&cr, r, &result);
28253 }
28254
28255 cursor_glyph = get_phys_cursor_glyph (w);
28256 if (cursor_glyph)
28257 {
28258 /* r is relative to W's box, but w->phys_cursor.x is relative
28259 to left edge of W's TEXT area. Adjust it. */
28260 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28261 cr.y = w->phys_cursor.y;
28262 cr.width = cursor_glyph->pixel_width;
28263 cr.height = w->phys_cursor_height;
28264 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28265 I assume the effect is the same -- and this is portable. */
28266 return x_intersect_rectangles (&cr, r, &result);
28267 }
28268 /* If we don't understand the format, pretend we're not in the hot-spot. */
28269 return 0;
28270 }
28271
28272
28273 /* EXPORT:
28274 Draw a vertical window border to the right of window W if W doesn't
28275 have vertical scroll bars. */
28276
28277 void
28278 x_draw_vertical_border (struct window *w)
28279 {
28280 struct frame *f = XFRAME (WINDOW_FRAME (w));
28281
28282 /* We could do better, if we knew what type of scroll-bar the adjacent
28283 windows (on either side) have... But we don't :-(
28284 However, I think this works ok. ++KFS 2003-04-25 */
28285
28286 /* Redraw borders between horizontally adjacent windows. Don't
28287 do it for frames with vertical scroll bars because either the
28288 right scroll bar of a window, or the left scroll bar of its
28289 neighbor will suffice as a border. */
28290 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28291 return;
28292
28293 if (!WINDOW_RIGHTMOST_P (w)
28294 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28295 {
28296 int x0, x1, y0, y1;
28297
28298 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28299 y1 -= 1;
28300
28301 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28302 x1 -= 1;
28303
28304 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28305 }
28306 else if (!WINDOW_LEFTMOST_P (w)
28307 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28308 {
28309 int x0, x1, y0, y1;
28310
28311 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28312 y1 -= 1;
28313
28314 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28315 x0 -= 1;
28316
28317 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28318 }
28319 }
28320
28321
28322 /* Redraw the part of window W intersection rectangle FR. Pixel
28323 coordinates in FR are frame-relative. Call this function with
28324 input blocked. Value is non-zero if the exposure overwrites
28325 mouse-face. */
28326
28327 static int
28328 expose_window (struct window *w, XRectangle *fr)
28329 {
28330 struct frame *f = XFRAME (w->frame);
28331 XRectangle wr, r;
28332 int mouse_face_overwritten_p = 0;
28333
28334 /* If window is not yet fully initialized, do nothing. This can
28335 happen when toolkit scroll bars are used and a window is split.
28336 Reconfiguring the scroll bar will generate an expose for a newly
28337 created window. */
28338 if (w->current_matrix == NULL)
28339 return 0;
28340
28341 /* When we're currently updating the window, display and current
28342 matrix usually don't agree. Arrange for a thorough display
28343 later. */
28344 if (w == updated_window)
28345 {
28346 SET_FRAME_GARBAGED (f);
28347 return 0;
28348 }
28349
28350 /* Frame-relative pixel rectangle of W. */
28351 wr.x = WINDOW_LEFT_EDGE_X (w);
28352 wr.y = WINDOW_TOP_EDGE_Y (w);
28353 wr.width = WINDOW_TOTAL_WIDTH (w);
28354 wr.height = WINDOW_TOTAL_HEIGHT (w);
28355
28356 if (x_intersect_rectangles (fr, &wr, &r))
28357 {
28358 int yb = window_text_bottom_y (w);
28359 struct glyph_row *row;
28360 int cursor_cleared_p, phys_cursor_on_p;
28361 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28362
28363 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28364 r.x, r.y, r.width, r.height));
28365
28366 /* Convert to window coordinates. */
28367 r.x -= WINDOW_LEFT_EDGE_X (w);
28368 r.y -= WINDOW_TOP_EDGE_Y (w);
28369
28370 /* Turn off the cursor. */
28371 if (!w->pseudo_window_p
28372 && phys_cursor_in_rect_p (w, &r))
28373 {
28374 x_clear_cursor (w);
28375 cursor_cleared_p = 1;
28376 }
28377 else
28378 cursor_cleared_p = 0;
28379
28380 /* If the row containing the cursor extends face to end of line,
28381 then expose_area might overwrite the cursor outside the
28382 rectangle and thus notice_overwritten_cursor might clear
28383 w->phys_cursor_on_p. We remember the original value and
28384 check later if it is changed. */
28385 phys_cursor_on_p = w->phys_cursor_on_p;
28386
28387 /* Update lines intersecting rectangle R. */
28388 first_overlapping_row = last_overlapping_row = NULL;
28389 for (row = w->current_matrix->rows;
28390 row->enabled_p;
28391 ++row)
28392 {
28393 int y0 = row->y;
28394 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28395
28396 if ((y0 >= r.y && y0 < r.y + r.height)
28397 || (y1 > r.y && y1 < r.y + r.height)
28398 || (r.y >= y0 && r.y < y1)
28399 || (r.y + r.height > y0 && r.y + r.height < y1))
28400 {
28401 /* A header line may be overlapping, but there is no need
28402 to fix overlapping areas for them. KFS 2005-02-12 */
28403 if (row->overlapping_p && !row->mode_line_p)
28404 {
28405 if (first_overlapping_row == NULL)
28406 first_overlapping_row = row;
28407 last_overlapping_row = row;
28408 }
28409
28410 row->clip = fr;
28411 if (expose_line (w, row, &r))
28412 mouse_face_overwritten_p = 1;
28413 row->clip = NULL;
28414 }
28415 else if (row->overlapping_p)
28416 {
28417 /* We must redraw a row overlapping the exposed area. */
28418 if (y0 < r.y
28419 ? y0 + row->phys_height > r.y
28420 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28421 {
28422 if (first_overlapping_row == NULL)
28423 first_overlapping_row = row;
28424 last_overlapping_row = row;
28425 }
28426 }
28427
28428 if (y1 >= yb)
28429 break;
28430 }
28431
28432 /* Display the mode line if there is one. */
28433 if (WINDOW_WANTS_MODELINE_P (w)
28434 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28435 row->enabled_p)
28436 && row->y < r.y + r.height)
28437 {
28438 if (expose_line (w, row, &r))
28439 mouse_face_overwritten_p = 1;
28440 }
28441
28442 if (!w->pseudo_window_p)
28443 {
28444 /* Fix the display of overlapping rows. */
28445 if (first_overlapping_row)
28446 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28447 fr);
28448
28449 /* Draw border between windows. */
28450 x_draw_vertical_border (w);
28451
28452 /* Turn the cursor on again. */
28453 if (cursor_cleared_p
28454 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28455 update_window_cursor (w, 1);
28456 }
28457 }
28458
28459 return mouse_face_overwritten_p;
28460 }
28461
28462
28463
28464 /* Redraw (parts) of all windows in the window tree rooted at W that
28465 intersect R. R contains frame pixel coordinates. Value is
28466 non-zero if the exposure overwrites mouse-face. */
28467
28468 static int
28469 expose_window_tree (struct window *w, XRectangle *r)
28470 {
28471 struct frame *f = XFRAME (w->frame);
28472 int mouse_face_overwritten_p = 0;
28473
28474 while (w && !FRAME_GARBAGED_P (f))
28475 {
28476 if (!NILP (w->hchild))
28477 mouse_face_overwritten_p
28478 |= expose_window_tree (XWINDOW (w->hchild), r);
28479 else if (!NILP (w->vchild))
28480 mouse_face_overwritten_p
28481 |= expose_window_tree (XWINDOW (w->vchild), r);
28482 else
28483 mouse_face_overwritten_p |= expose_window (w, r);
28484
28485 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28486 }
28487
28488 return mouse_face_overwritten_p;
28489 }
28490
28491
28492 /* EXPORT:
28493 Redisplay an exposed area of frame F. X and Y are the upper-left
28494 corner of the exposed rectangle. W and H are width and height of
28495 the exposed area. All are pixel values. W or H zero means redraw
28496 the entire frame. */
28497
28498 void
28499 expose_frame (struct frame *f, int x, int y, int w, int h)
28500 {
28501 XRectangle r;
28502 int mouse_face_overwritten_p = 0;
28503
28504 TRACE ((stderr, "expose_frame "));
28505
28506 /* No need to redraw if frame will be redrawn soon. */
28507 if (FRAME_GARBAGED_P (f))
28508 {
28509 TRACE ((stderr, " garbaged\n"));
28510 return;
28511 }
28512
28513 /* If basic faces haven't been realized yet, there is no point in
28514 trying to redraw anything. This can happen when we get an expose
28515 event while Emacs is starting, e.g. by moving another window. */
28516 if (FRAME_FACE_CACHE (f) == NULL
28517 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28518 {
28519 TRACE ((stderr, " no faces\n"));
28520 return;
28521 }
28522
28523 if (w == 0 || h == 0)
28524 {
28525 r.x = r.y = 0;
28526 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28527 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28528 }
28529 else
28530 {
28531 r.x = x;
28532 r.y = y;
28533 r.width = w;
28534 r.height = h;
28535 }
28536
28537 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28538 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28539
28540 if (WINDOWP (f->tool_bar_window))
28541 mouse_face_overwritten_p
28542 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28543
28544 #ifdef HAVE_X_WINDOWS
28545 #ifndef MSDOS
28546 #ifndef USE_X_TOOLKIT
28547 if (WINDOWP (f->menu_bar_window))
28548 mouse_face_overwritten_p
28549 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28550 #endif /* not USE_X_TOOLKIT */
28551 #endif
28552 #endif
28553
28554 /* Some window managers support a focus-follows-mouse style with
28555 delayed raising of frames. Imagine a partially obscured frame,
28556 and moving the mouse into partially obscured mouse-face on that
28557 frame. The visible part of the mouse-face will be highlighted,
28558 then the WM raises the obscured frame. With at least one WM, KDE
28559 2.1, Emacs is not getting any event for the raising of the frame
28560 (even tried with SubstructureRedirectMask), only Expose events.
28561 These expose events will draw text normally, i.e. not
28562 highlighted. Which means we must redo the highlight here.
28563 Subsume it under ``we love X''. --gerd 2001-08-15 */
28564 /* Included in Windows version because Windows most likely does not
28565 do the right thing if any third party tool offers
28566 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28567 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28568 {
28569 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28570 if (f == hlinfo->mouse_face_mouse_frame)
28571 {
28572 int mouse_x = hlinfo->mouse_face_mouse_x;
28573 int mouse_y = hlinfo->mouse_face_mouse_y;
28574 clear_mouse_face (hlinfo);
28575 note_mouse_highlight (f, mouse_x, mouse_y);
28576 }
28577 }
28578 }
28579
28580
28581 /* EXPORT:
28582 Determine the intersection of two rectangles R1 and R2. Return
28583 the intersection in *RESULT. Value is non-zero if RESULT is not
28584 empty. */
28585
28586 int
28587 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28588 {
28589 XRectangle *left, *right;
28590 XRectangle *upper, *lower;
28591 int intersection_p = 0;
28592
28593 /* Rearrange so that R1 is the left-most rectangle. */
28594 if (r1->x < r2->x)
28595 left = r1, right = r2;
28596 else
28597 left = r2, right = r1;
28598
28599 /* X0 of the intersection is right.x0, if this is inside R1,
28600 otherwise there is no intersection. */
28601 if (right->x <= left->x + left->width)
28602 {
28603 result->x = right->x;
28604
28605 /* The right end of the intersection is the minimum of
28606 the right ends of left and right. */
28607 result->width = (min (left->x + left->width, right->x + right->width)
28608 - result->x);
28609
28610 /* Same game for Y. */
28611 if (r1->y < r2->y)
28612 upper = r1, lower = r2;
28613 else
28614 upper = r2, lower = r1;
28615
28616 /* The upper end of the intersection is lower.y0, if this is inside
28617 of upper. Otherwise, there is no intersection. */
28618 if (lower->y <= upper->y + upper->height)
28619 {
28620 result->y = lower->y;
28621
28622 /* The lower end of the intersection is the minimum of the lower
28623 ends of upper and lower. */
28624 result->height = (min (lower->y + lower->height,
28625 upper->y + upper->height)
28626 - result->y);
28627 intersection_p = 1;
28628 }
28629 }
28630
28631 return intersection_p;
28632 }
28633
28634 #endif /* HAVE_WINDOW_SYSTEM */
28635
28636 \f
28637 /***********************************************************************
28638 Initialization
28639 ***********************************************************************/
28640
28641 void
28642 syms_of_xdisp (void)
28643 {
28644 Vwith_echo_area_save_vector = Qnil;
28645 staticpro (&Vwith_echo_area_save_vector);
28646
28647 Vmessage_stack = Qnil;
28648 staticpro (&Vmessage_stack);
28649
28650 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28651
28652 message_dolog_marker1 = Fmake_marker ();
28653 staticpro (&message_dolog_marker1);
28654 message_dolog_marker2 = Fmake_marker ();
28655 staticpro (&message_dolog_marker2);
28656 message_dolog_marker3 = Fmake_marker ();
28657 staticpro (&message_dolog_marker3);
28658
28659 #ifdef GLYPH_DEBUG
28660 defsubr (&Sdump_frame_glyph_matrix);
28661 defsubr (&Sdump_glyph_matrix);
28662 defsubr (&Sdump_glyph_row);
28663 defsubr (&Sdump_tool_bar_row);
28664 defsubr (&Strace_redisplay);
28665 defsubr (&Strace_to_stderr);
28666 #endif
28667 #ifdef HAVE_WINDOW_SYSTEM
28668 defsubr (&Stool_bar_lines_needed);
28669 defsubr (&Slookup_image_map);
28670 #endif
28671 defsubr (&Sformat_mode_line);
28672 defsubr (&Sinvisible_p);
28673 defsubr (&Scurrent_bidi_paragraph_direction);
28674
28675 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28676 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28677 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28678 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28679 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28680 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28681 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28682 DEFSYM (Qeval, "eval");
28683 DEFSYM (QCdata, ":data");
28684 DEFSYM (Qdisplay, "display");
28685 DEFSYM (Qspace_width, "space-width");
28686 DEFSYM (Qraise, "raise");
28687 DEFSYM (Qslice, "slice");
28688 DEFSYM (Qspace, "space");
28689 DEFSYM (Qmargin, "margin");
28690 DEFSYM (Qpointer, "pointer");
28691 DEFSYM (Qleft_margin, "left-margin");
28692 DEFSYM (Qright_margin, "right-margin");
28693 DEFSYM (Qcenter, "center");
28694 DEFSYM (Qline_height, "line-height");
28695 DEFSYM (QCalign_to, ":align-to");
28696 DEFSYM (QCrelative_width, ":relative-width");
28697 DEFSYM (QCrelative_height, ":relative-height");
28698 DEFSYM (QCeval, ":eval");
28699 DEFSYM (QCpropertize, ":propertize");
28700 DEFSYM (QCfile, ":file");
28701 DEFSYM (Qfontified, "fontified");
28702 DEFSYM (Qfontification_functions, "fontification-functions");
28703 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28704 DEFSYM (Qescape_glyph, "escape-glyph");
28705 DEFSYM (Qnobreak_space, "nobreak-space");
28706 DEFSYM (Qimage, "image");
28707 DEFSYM (Qtext, "text");
28708 DEFSYM (Qboth, "both");
28709 DEFSYM (Qboth_horiz, "both-horiz");
28710 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28711 DEFSYM (QCmap, ":map");
28712 DEFSYM (QCpointer, ":pointer");
28713 DEFSYM (Qrect, "rect");
28714 DEFSYM (Qcircle, "circle");
28715 DEFSYM (Qpoly, "poly");
28716 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28717 DEFSYM (Qgrow_only, "grow-only");
28718 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28719 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28720 DEFSYM (Qposition, "position");
28721 DEFSYM (Qbuffer_position, "buffer-position");
28722 DEFSYM (Qobject, "object");
28723 DEFSYM (Qbar, "bar");
28724 DEFSYM (Qhbar, "hbar");
28725 DEFSYM (Qbox, "box");
28726 DEFSYM (Qhollow, "hollow");
28727 DEFSYM (Qhand, "hand");
28728 DEFSYM (Qarrow, "arrow");
28729 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28730
28731 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28732 Fcons (intern_c_string ("void-variable"), Qnil)),
28733 Qnil);
28734 staticpro (&list_of_error);
28735
28736 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28737 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28738 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28739 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28740
28741 echo_buffer[0] = echo_buffer[1] = Qnil;
28742 staticpro (&echo_buffer[0]);
28743 staticpro (&echo_buffer[1]);
28744
28745 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28746 staticpro (&echo_area_buffer[0]);
28747 staticpro (&echo_area_buffer[1]);
28748
28749 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28750 staticpro (&Vmessages_buffer_name);
28751
28752 mode_line_proptrans_alist = Qnil;
28753 staticpro (&mode_line_proptrans_alist);
28754 mode_line_string_list = Qnil;
28755 staticpro (&mode_line_string_list);
28756 mode_line_string_face = Qnil;
28757 staticpro (&mode_line_string_face);
28758 mode_line_string_face_prop = Qnil;
28759 staticpro (&mode_line_string_face_prop);
28760 Vmode_line_unwind_vector = Qnil;
28761 staticpro (&Vmode_line_unwind_vector);
28762
28763 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28764
28765 help_echo_string = Qnil;
28766 staticpro (&help_echo_string);
28767 help_echo_object = Qnil;
28768 staticpro (&help_echo_object);
28769 help_echo_window = Qnil;
28770 staticpro (&help_echo_window);
28771 previous_help_echo_string = Qnil;
28772 staticpro (&previous_help_echo_string);
28773 help_echo_pos = -1;
28774
28775 DEFSYM (Qright_to_left, "right-to-left");
28776 DEFSYM (Qleft_to_right, "left-to-right");
28777
28778 #ifdef HAVE_WINDOW_SYSTEM
28779 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28780 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28781 For example, if a block cursor is over a tab, it will be drawn as
28782 wide as that tab on the display. */);
28783 x_stretch_cursor_p = 0;
28784 #endif
28785
28786 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28787 doc: /* Non-nil means highlight trailing whitespace.
28788 The face used for trailing whitespace is `trailing-whitespace'. */);
28789 Vshow_trailing_whitespace = Qnil;
28790
28791 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28792 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28793 If the value is t, Emacs highlights non-ASCII chars which have the
28794 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28795 or `escape-glyph' face respectively.
28796
28797 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28798 U+2011 (non-breaking hyphen) are affected.
28799
28800 Any other non-nil value means to display these characters as a escape
28801 glyph followed by an ordinary space or hyphen.
28802
28803 A value of nil means no special handling of these characters. */);
28804 Vnobreak_char_display = Qt;
28805
28806 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28807 doc: /* The pointer shape to show in void text areas.
28808 A value of nil means to show the text pointer. Other options are `arrow',
28809 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28810 Vvoid_text_area_pointer = Qarrow;
28811
28812 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28813 doc: /* Non-nil means don't actually do any redisplay.
28814 This is used for internal purposes. */);
28815 Vinhibit_redisplay = Qnil;
28816
28817 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28818 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28819 Vglobal_mode_string = Qnil;
28820
28821 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28822 doc: /* Marker for where to display an arrow on top of the buffer text.
28823 This must be the beginning of a line in order to work.
28824 See also `overlay-arrow-string'. */);
28825 Voverlay_arrow_position = Qnil;
28826
28827 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28828 doc: /* String to display as an arrow in non-window frames.
28829 See also `overlay-arrow-position'. */);
28830 Voverlay_arrow_string = build_pure_c_string ("=>");
28831
28832 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28833 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28834 The symbols on this list are examined during redisplay to determine
28835 where to display overlay arrows. */);
28836 Voverlay_arrow_variable_list
28837 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28838
28839 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28840 doc: /* The number of lines to try scrolling a window by when point moves out.
28841 If that fails to bring point back on frame, point is centered instead.
28842 If this is zero, point is always centered after it moves off frame.
28843 If you want scrolling to always be a line at a time, you should set
28844 `scroll-conservatively' to a large value rather than set this to 1. */);
28845
28846 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28847 doc: /* Scroll up to this many lines, to bring point back on screen.
28848 If point moves off-screen, redisplay will scroll by up to
28849 `scroll-conservatively' lines in order to bring point just barely
28850 onto the screen again. If that cannot be done, then redisplay
28851 recenters point as usual.
28852
28853 If the value is greater than 100, redisplay will never recenter point,
28854 but will always scroll just enough text to bring point into view, even
28855 if you move far away.
28856
28857 A value of zero means always recenter point if it moves off screen. */);
28858 scroll_conservatively = 0;
28859
28860 DEFVAR_INT ("scroll-margin", scroll_margin,
28861 doc: /* Number of lines of margin at the top and bottom of a window.
28862 Recenter the window whenever point gets within this many lines
28863 of the top or bottom of the window. */);
28864 scroll_margin = 0;
28865
28866 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28867 doc: /* Pixels per inch value for non-window system displays.
28868 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28869 Vdisplay_pixels_per_inch = make_float (72.0);
28870
28871 #ifdef GLYPH_DEBUG
28872 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28873 #endif
28874
28875 DEFVAR_LISP ("truncate-partial-width-windows",
28876 Vtruncate_partial_width_windows,
28877 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28878 For an integer value, truncate lines in each window narrower than the
28879 full frame width, provided the window width is less than that integer;
28880 otherwise, respect the value of `truncate-lines'.
28881
28882 For any other non-nil value, truncate lines in all windows that do
28883 not span the full frame width.
28884
28885 A value of nil means to respect the value of `truncate-lines'.
28886
28887 If `word-wrap' is enabled, you might want to reduce this. */);
28888 Vtruncate_partial_width_windows = make_number (50);
28889
28890 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28891 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28892 Any other value means to use the appropriate face, `mode-line',
28893 `header-line', or `menu' respectively. */);
28894 mode_line_inverse_video = 1;
28895
28896 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28897 doc: /* Maximum buffer size for which line number should be displayed.
28898 If the buffer is bigger than this, the line number does not appear
28899 in the mode line. A value of nil means no limit. */);
28900 Vline_number_display_limit = Qnil;
28901
28902 DEFVAR_INT ("line-number-display-limit-width",
28903 line_number_display_limit_width,
28904 doc: /* Maximum line width (in characters) for line number display.
28905 If the average length of the lines near point is bigger than this, then the
28906 line number may be omitted from the mode line. */);
28907 line_number_display_limit_width = 200;
28908
28909 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28910 doc: /* Non-nil means highlight region even in nonselected windows. */);
28911 highlight_nonselected_windows = 0;
28912
28913 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28914 doc: /* Non-nil if more than one frame is visible on this display.
28915 Minibuffer-only frames don't count, but iconified frames do.
28916 This variable is not guaranteed to be accurate except while processing
28917 `frame-title-format' and `icon-title-format'. */);
28918
28919 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28920 doc: /* Template for displaying the title bar of visible frames.
28921 \(Assuming the window manager supports this feature.)
28922
28923 This variable has the same structure as `mode-line-format', except that
28924 the %c and %l constructs are ignored. It is used only on frames for
28925 which no explicit name has been set \(see `modify-frame-parameters'). */);
28926
28927 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28928 doc: /* Template for displaying the title bar of an iconified frame.
28929 \(Assuming the window manager supports this feature.)
28930 This variable has the same structure as `mode-line-format' (which see),
28931 and is used only on frames for which no explicit name has been set
28932 \(see `modify-frame-parameters'). */);
28933 Vicon_title_format
28934 = Vframe_title_format
28935 = listn (CONSTYPE_PURE, 3,
28936 intern_c_string ("multiple-frames"),
28937 build_pure_c_string ("%b"),
28938 listn (CONSTYPE_PURE, 4,
28939 empty_unibyte_string,
28940 intern_c_string ("invocation-name"),
28941 build_pure_c_string ("@"),
28942 intern_c_string ("system-name")));
28943
28944 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28945 doc: /* Maximum number of lines to keep in the message log buffer.
28946 If nil, disable message logging. If t, log messages but don't truncate
28947 the buffer when it becomes large. */);
28948 Vmessage_log_max = make_number (100);
28949
28950 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28951 doc: /* Functions called before redisplay, if window sizes have changed.
28952 The value should be a list of functions that take one argument.
28953 Just before redisplay, for each frame, if any of its windows have changed
28954 size since the last redisplay, or have been split or deleted,
28955 all the functions in the list are called, with the frame as argument. */);
28956 Vwindow_size_change_functions = Qnil;
28957
28958 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28959 doc: /* List of functions to call before redisplaying a window with scrolling.
28960 Each function is called with two arguments, the window and its new
28961 display-start position. Note that these functions are also called by
28962 `set-window-buffer'. Also note that the value of `window-end' is not
28963 valid when these functions are called.
28964
28965 Warning: Do not use this feature to alter the way the window
28966 is scrolled. It is not designed for that, and such use probably won't
28967 work. */);
28968 Vwindow_scroll_functions = Qnil;
28969
28970 DEFVAR_LISP ("window-text-change-functions",
28971 Vwindow_text_change_functions,
28972 doc: /* Functions to call in redisplay when text in the window might change. */);
28973 Vwindow_text_change_functions = Qnil;
28974
28975 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28976 doc: /* Functions called when redisplay of a window reaches the end trigger.
28977 Each function is called with two arguments, the window and the end trigger value.
28978 See `set-window-redisplay-end-trigger'. */);
28979 Vredisplay_end_trigger_functions = Qnil;
28980
28981 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28982 doc: /* Non-nil means autoselect window with mouse pointer.
28983 If nil, do not autoselect windows.
28984 A positive number means delay autoselection by that many seconds: a
28985 window is autoselected only after the mouse has remained in that
28986 window for the duration of the delay.
28987 A negative number has a similar effect, but causes windows to be
28988 autoselected only after the mouse has stopped moving. \(Because of
28989 the way Emacs compares mouse events, you will occasionally wait twice
28990 that time before the window gets selected.\)
28991 Any other value means to autoselect window instantaneously when the
28992 mouse pointer enters it.
28993
28994 Autoselection selects the minibuffer only if it is active, and never
28995 unselects the minibuffer if it is active.
28996
28997 When customizing this variable make sure that the actual value of
28998 `focus-follows-mouse' matches the behavior of your window manager. */);
28999 Vmouse_autoselect_window = Qnil;
29000
29001 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29002 doc: /* Non-nil means automatically resize tool-bars.
29003 This dynamically changes the tool-bar's height to the minimum height
29004 that is needed to make all tool-bar items visible.
29005 If value is `grow-only', the tool-bar's height is only increased
29006 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29007 Vauto_resize_tool_bars = Qt;
29008
29009 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29010 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29011 auto_raise_tool_bar_buttons_p = 1;
29012
29013 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29014 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29015 make_cursor_line_fully_visible_p = 1;
29016
29017 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29018 doc: /* Border below tool-bar in pixels.
29019 If an integer, use it as the height of the border.
29020 If it is one of `internal-border-width' or `border-width', use the
29021 value of the corresponding frame parameter.
29022 Otherwise, no border is added below the tool-bar. */);
29023 Vtool_bar_border = Qinternal_border_width;
29024
29025 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29026 doc: /* Margin around tool-bar buttons in pixels.
29027 If an integer, use that for both horizontal and vertical margins.
29028 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29029 HORZ specifying the horizontal margin, and VERT specifying the
29030 vertical margin. */);
29031 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29032
29033 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29034 doc: /* Relief thickness of tool-bar buttons. */);
29035 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29036
29037 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29038 doc: /* Tool bar style to use.
29039 It can be one of
29040 image - show images only
29041 text - show text only
29042 both - show both, text below image
29043 both-horiz - show text to the right of the image
29044 text-image-horiz - show text to the left of the image
29045 any other - use system default or image if no system default.
29046
29047 This variable only affects the GTK+ toolkit version of Emacs. */);
29048 Vtool_bar_style = Qnil;
29049
29050 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29051 doc: /* Maximum number of characters a label can have to be shown.
29052 The tool bar style must also show labels for this to have any effect, see
29053 `tool-bar-style'. */);
29054 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29055
29056 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29057 doc: /* List of functions to call to fontify regions of text.
29058 Each function is called with one argument POS. Functions must
29059 fontify a region starting at POS in the current buffer, and give
29060 fontified regions the property `fontified'. */);
29061 Vfontification_functions = Qnil;
29062 Fmake_variable_buffer_local (Qfontification_functions);
29063
29064 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29065 unibyte_display_via_language_environment,
29066 doc: /* Non-nil means display unibyte text according to language environment.
29067 Specifically, this means that raw bytes in the range 160-255 decimal
29068 are displayed by converting them to the equivalent multibyte characters
29069 according to the current language environment. As a result, they are
29070 displayed according to the current fontset.
29071
29072 Note that this variable affects only how these bytes are displayed,
29073 but does not change the fact they are interpreted as raw bytes. */);
29074 unibyte_display_via_language_environment = 0;
29075
29076 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29077 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29078 If a float, it specifies a fraction of the mini-window frame's height.
29079 If an integer, it specifies a number of lines. */);
29080 Vmax_mini_window_height = make_float (0.25);
29081
29082 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29083 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29084 A value of nil means don't automatically resize mini-windows.
29085 A value of t means resize them to fit the text displayed in them.
29086 A value of `grow-only', the default, means let mini-windows grow only;
29087 they return to their normal size when the minibuffer is closed, or the
29088 echo area becomes empty. */);
29089 Vresize_mini_windows = Qgrow_only;
29090
29091 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29092 doc: /* Alist specifying how to blink the cursor off.
29093 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29094 `cursor-type' frame-parameter or variable equals ON-STATE,
29095 comparing using `equal', Emacs uses OFF-STATE to specify
29096 how to blink it off. ON-STATE and OFF-STATE are values for
29097 the `cursor-type' frame parameter.
29098
29099 If a frame's ON-STATE has no entry in this list,
29100 the frame's other specifications determine how to blink the cursor off. */);
29101 Vblink_cursor_alist = Qnil;
29102
29103 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29104 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29105 If non-nil, windows are automatically scrolled horizontally to make
29106 point visible. */);
29107 automatic_hscrolling_p = 1;
29108 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29109
29110 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29111 doc: /* How many columns away from the window edge point is allowed to get
29112 before automatic hscrolling will horizontally scroll the window. */);
29113 hscroll_margin = 5;
29114
29115 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29116 doc: /* How many columns to scroll the window when point gets too close to the edge.
29117 When point is less than `hscroll-margin' columns from the window
29118 edge, automatic hscrolling will scroll the window by the amount of columns
29119 determined by this variable. If its value is a positive integer, scroll that
29120 many columns. If it's a positive floating-point number, it specifies the
29121 fraction of the window's width to scroll. If it's nil or zero, point will be
29122 centered horizontally after the scroll. Any other value, including negative
29123 numbers, are treated as if the value were zero.
29124
29125 Automatic hscrolling always moves point outside the scroll margin, so if
29126 point was more than scroll step columns inside the margin, the window will
29127 scroll more than the value given by the scroll step.
29128
29129 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29130 and `scroll-right' overrides this variable's effect. */);
29131 Vhscroll_step = make_number (0);
29132
29133 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29134 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29135 Bind this around calls to `message' to let it take effect. */);
29136 message_truncate_lines = 0;
29137
29138 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29139 doc: /* Normal hook run to update the menu bar definitions.
29140 Redisplay runs this hook before it redisplays the menu bar.
29141 This is used to update submenus such as Buffers,
29142 whose contents depend on various data. */);
29143 Vmenu_bar_update_hook = Qnil;
29144
29145 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29146 doc: /* Frame for which we are updating a menu.
29147 The enable predicate for a menu binding should check this variable. */);
29148 Vmenu_updating_frame = Qnil;
29149
29150 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29151 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29152 inhibit_menubar_update = 0;
29153
29154 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29155 doc: /* Prefix prepended to all continuation lines at display time.
29156 The value may be a string, an image, or a stretch-glyph; it is
29157 interpreted in the same way as the value of a `display' text property.
29158
29159 This variable is overridden by any `wrap-prefix' text or overlay
29160 property.
29161
29162 To add a prefix to non-continuation lines, use `line-prefix'. */);
29163 Vwrap_prefix = Qnil;
29164 DEFSYM (Qwrap_prefix, "wrap-prefix");
29165 Fmake_variable_buffer_local (Qwrap_prefix);
29166
29167 DEFVAR_LISP ("line-prefix", Vline_prefix,
29168 doc: /* Prefix prepended to all non-continuation lines at display time.
29169 The value may be a string, an image, or a stretch-glyph; it is
29170 interpreted in the same way as the value of a `display' text property.
29171
29172 This variable is overridden by any `line-prefix' text or overlay
29173 property.
29174
29175 To add a prefix to continuation lines, use `wrap-prefix'. */);
29176 Vline_prefix = Qnil;
29177 DEFSYM (Qline_prefix, "line-prefix");
29178 Fmake_variable_buffer_local (Qline_prefix);
29179
29180 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29181 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29182 inhibit_eval_during_redisplay = 0;
29183
29184 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29185 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29186 inhibit_free_realized_faces = 0;
29187
29188 #ifdef GLYPH_DEBUG
29189 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29190 doc: /* Inhibit try_window_id display optimization. */);
29191 inhibit_try_window_id = 0;
29192
29193 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29194 doc: /* Inhibit try_window_reusing display optimization. */);
29195 inhibit_try_window_reusing = 0;
29196
29197 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29198 doc: /* Inhibit try_cursor_movement display optimization. */);
29199 inhibit_try_cursor_movement = 0;
29200 #endif /* GLYPH_DEBUG */
29201
29202 DEFVAR_INT ("overline-margin", overline_margin,
29203 doc: /* Space between overline and text, in pixels.
29204 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29205 margin to the character height. */);
29206 overline_margin = 2;
29207
29208 DEFVAR_INT ("underline-minimum-offset",
29209 underline_minimum_offset,
29210 doc: /* Minimum distance between baseline and underline.
29211 This can improve legibility of underlined text at small font sizes,
29212 particularly when using variable `x-use-underline-position-properties'
29213 with fonts that specify an UNDERLINE_POSITION relatively close to the
29214 baseline. The default value is 1. */);
29215 underline_minimum_offset = 1;
29216
29217 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29218 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29219 This feature only works when on a window system that can change
29220 cursor shapes. */);
29221 display_hourglass_p = 1;
29222
29223 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29224 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29225 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29226
29227 hourglass_atimer = NULL;
29228 hourglass_shown_p = 0;
29229
29230 DEFSYM (Qglyphless_char, "glyphless-char");
29231 DEFSYM (Qhex_code, "hex-code");
29232 DEFSYM (Qempty_box, "empty-box");
29233 DEFSYM (Qthin_space, "thin-space");
29234 DEFSYM (Qzero_width, "zero-width");
29235
29236 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29237 /* Intern this now in case it isn't already done.
29238 Setting this variable twice is harmless.
29239 But don't staticpro it here--that is done in alloc.c. */
29240 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29241 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29242
29243 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29244 doc: /* Char-table defining glyphless characters.
29245 Each element, if non-nil, should be one of the following:
29246 an ASCII acronym string: display this string in a box
29247 `hex-code': display the hexadecimal code of a character in a box
29248 `empty-box': display as an empty box
29249 `thin-space': display as 1-pixel width space
29250 `zero-width': don't display
29251 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29252 display method for graphical terminals and text terminals respectively.
29253 GRAPHICAL and TEXT should each have one of the values listed above.
29254
29255 The char-table has one extra slot to control the display of a character for
29256 which no font is found. This slot only takes effect on graphical terminals.
29257 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29258 `thin-space'. The default is `empty-box'. */);
29259 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29260 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29261 Qempty_box);
29262 }
29263
29264
29265 /* Initialize this module when Emacs starts. */
29266
29267 void
29268 init_xdisp (void)
29269 {
29270 current_header_line_height = current_mode_line_height = -1;
29271
29272 CHARPOS (this_line_start_pos) = 0;
29273
29274 if (!noninteractive)
29275 {
29276 struct window *m = XWINDOW (minibuf_window);
29277 Lisp_Object frame = m->frame;
29278 struct frame *f = XFRAME (frame);
29279 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29280 struct window *r = XWINDOW (root);
29281 int i;
29282
29283 echo_area_window = minibuf_window;
29284
29285 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29286 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29287 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29288 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29289 XSETFASTINT (m->total_lines, 1);
29290 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29291
29292 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29293 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29294 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29295
29296 /* The default ellipsis glyphs `...'. */
29297 for (i = 0; i < 3; ++i)
29298 default_invis_vector[i] = make_number ('.');
29299 }
29300
29301 {
29302 /* Allocate the buffer for frame titles.
29303 Also used for `format-mode-line'. */
29304 int size = 100;
29305 mode_line_noprop_buf = xmalloc (size);
29306 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29307 mode_line_noprop_ptr = mode_line_noprop_buf;
29308 mode_line_target = MODE_LINE_DISPLAY;
29309 }
29310
29311 help_echo_showing_p = 0;
29312 }
29313
29314 /* Since w32 does not support atimers, it defines its own implementation of
29315 the following three functions in w32fns.c. */
29316 #ifndef WINDOWSNT
29317
29318 /* Platform-independent portion of hourglass implementation. */
29319
29320 /* Cancel a currently active hourglass timer, and start a new one. */
29321 void
29322 start_hourglass (void)
29323 {
29324 #if defined (HAVE_WINDOW_SYSTEM)
29325 EMACS_TIME delay;
29326
29327 cancel_hourglass ();
29328
29329 if (INTEGERP (Vhourglass_delay)
29330 && XINT (Vhourglass_delay) > 0)
29331 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29332 TYPE_MAXIMUM (time_t)),
29333 0);
29334 else if (FLOATP (Vhourglass_delay)
29335 && XFLOAT_DATA (Vhourglass_delay) > 0)
29336 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29337 else
29338 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29339
29340 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29341 show_hourglass, NULL);
29342 #endif
29343 }
29344
29345
29346 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29347 shown. */
29348 void
29349 cancel_hourglass (void)
29350 {
29351 #if defined (HAVE_WINDOW_SYSTEM)
29352 if (hourglass_atimer)
29353 {
29354 cancel_atimer (hourglass_atimer);
29355 hourglass_atimer = NULL;
29356 }
29357
29358 if (hourglass_shown_p)
29359 hide_hourglass ();
29360 #endif
29361 }
29362 #endif /* ! WINDOWSNT */