Cleanup xmalloc.
[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 show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
955 static int coords_in_mouse_face_p (struct window *, int, int);
956
957
958 \f
959 /***********************************************************************
960 Window display dimensions
961 ***********************************************************************/
962
963 /* Return the bottom boundary y-position for text lines in window W.
964 This is the first y position at which a line cannot start.
965 It is relative to the top of the window.
966
967 This is the height of W minus the height of a mode line, if any. */
968
969 int
970 window_text_bottom_y (struct window *w)
971 {
972 int height = WINDOW_TOTAL_HEIGHT (w);
973
974 if (WINDOW_WANTS_MODELINE_P (w))
975 height -= CURRENT_MODE_LINE_HEIGHT (w);
976 return height;
977 }
978
979 /* Return the pixel width of display area AREA of window W. AREA < 0
980 means return the total width of W, not including fringes to
981 the left and right of the window. */
982
983 int
984 window_box_width (struct window *w, int area)
985 {
986 int cols = XFASTINT (w->total_cols);
987 int pixels = 0;
988
989 if (!w->pseudo_window_p)
990 {
991 cols -= WINDOW_SCROLL_BAR_COLS (w);
992
993 if (area == TEXT_AREA)
994 {
995 if (INTEGERP (w->left_margin_cols))
996 cols -= XFASTINT (w->left_margin_cols);
997 if (INTEGERP (w->right_margin_cols))
998 cols -= XFASTINT (w->right_margin_cols);
999 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1000 }
1001 else if (area == LEFT_MARGIN_AREA)
1002 {
1003 cols = (INTEGERP (w->left_margin_cols)
1004 ? XFASTINT (w->left_margin_cols) : 0);
1005 pixels = 0;
1006 }
1007 else if (area == RIGHT_MARGIN_AREA)
1008 {
1009 cols = (INTEGERP (w->right_margin_cols)
1010 ? XFASTINT (w->right_margin_cols) : 0);
1011 pixels = 0;
1012 }
1013 }
1014
1015 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1016 }
1017
1018
1019 /* Return the pixel height of the display area of window W, not
1020 including mode lines of W, if any. */
1021
1022 int
1023 window_box_height (struct window *w)
1024 {
1025 struct frame *f = XFRAME (w->frame);
1026 int height = WINDOW_TOTAL_HEIGHT (w);
1027
1028 eassert (height >= 0);
1029
1030 /* Note: the code below that determines the mode-line/header-line
1031 height is essentially the same as that contained in the macro
1032 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1033 the appropriate glyph row has its `mode_line_p' flag set,
1034 and if it doesn't, uses estimate_mode_line_height instead. */
1035
1036 if (WINDOW_WANTS_MODELINE_P (w))
1037 {
1038 struct glyph_row *ml_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (ml_row && ml_row->mode_line_p)
1043 height -= ml_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1046 }
1047
1048 if (WINDOW_WANTS_HEADER_LINE_P (w))
1049 {
1050 struct glyph_row *hl_row
1051 = (w->current_matrix && w->current_matrix->rows
1052 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1053 : 0);
1054 if (hl_row && hl_row->mode_line_p)
1055 height -= hl_row->height;
1056 else
1057 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1058 }
1059
1060 /* With a very small font and a mode-line that's taller than
1061 default, we might end up with a negative height. */
1062 return max (0, height);
1063 }
1064
1065 /* Return the window-relative coordinate of the left edge of display
1066 area AREA of window W. AREA < 0 means return the left edge of the
1067 whole window, to the right of the left fringe of W. */
1068
1069 int
1070 window_box_left_offset (struct window *w, int area)
1071 {
1072 int x;
1073
1074 if (w->pseudo_window_p)
1075 return 0;
1076
1077 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1078
1079 if (area == TEXT_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA));
1082 else if (area == RIGHT_MARGIN_AREA)
1083 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1084 + window_box_width (w, LEFT_MARGIN_AREA)
1085 + window_box_width (w, TEXT_AREA)
1086 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1087 ? 0
1088 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1089 else if (area == LEFT_MARGIN_AREA
1090 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1091 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1092
1093 return x;
1094 }
1095
1096
1097 /* Return the window-relative coordinate of the right edge of display
1098 area AREA of window W. AREA < 0 means return the right edge of the
1099 whole window, to the left of the right fringe of W. */
1100
1101 int
1102 window_box_right_offset (struct window *w, int area)
1103 {
1104 return window_box_left_offset (w, area) + window_box_width (w, area);
1105 }
1106
1107 /* Return the frame-relative coordinate of the left edge of display
1108 area AREA of window W. AREA < 0 means return the left edge of the
1109 whole window, to the right of the left fringe of W. */
1110
1111 int
1112 window_box_left (struct window *w, int area)
1113 {
1114 struct frame *f = XFRAME (w->frame);
1115 int x;
1116
1117 if (w->pseudo_window_p)
1118 return FRAME_INTERNAL_BORDER_WIDTH (f);
1119
1120 x = (WINDOW_LEFT_EDGE_X (w)
1121 + window_box_left_offset (w, area));
1122
1123 return x;
1124 }
1125
1126
1127 /* Return the frame-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the right edge of the
1129 whole window, to the left of the right fringe of W. */
1130
1131 int
1132 window_box_right (struct window *w, int area)
1133 {
1134 return window_box_left (w, area) + window_box_width (w, area);
1135 }
1136
1137 /* Get the bounding box of the display area AREA of window W, without
1138 mode lines, in frame-relative coordinates. AREA < 0 means the
1139 whole window, not including the left and right fringes of
1140 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1141 coordinates of the upper-left corner of the box. Return in
1142 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1143
1144 void
1145 window_box (struct window *w, int area, int *box_x, int *box_y,
1146 int *box_width, int *box_height)
1147 {
1148 if (box_width)
1149 *box_width = window_box_width (w, area);
1150 if (box_height)
1151 *box_height = window_box_height (w);
1152 if (box_x)
1153 *box_x = window_box_left (w, area);
1154 if (box_y)
1155 {
1156 *box_y = WINDOW_TOP_EDGE_Y (w);
1157 if (WINDOW_WANTS_HEADER_LINE_P (w))
1158 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1159 }
1160 }
1161
1162
1163 /* Get the bounding box of the display area AREA of window W, without
1164 mode lines. AREA < 0 means the whole window, not including the
1165 left and right fringe of the window. Return in *TOP_LEFT_X
1166 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1167 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1168 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1169 box. */
1170
1171 static inline void
1172 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1173 int *bottom_right_x, int *bottom_right_y)
1174 {
1175 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1176 bottom_right_y);
1177 *bottom_right_x += *top_left_x;
1178 *bottom_right_y += *top_left_y;
1179 }
1180
1181
1182 \f
1183 /***********************************************************************
1184 Utilities
1185 ***********************************************************************/
1186
1187 /* Return the bottom y-position of the line the iterator IT is in.
1188 This can modify IT's settings. */
1189
1190 int
1191 line_bottom_y (struct it *it)
1192 {
1193 int line_height = it->max_ascent + it->max_descent;
1194 int line_top_y = it->current_y;
1195
1196 if (line_height == 0)
1197 {
1198 if (last_height)
1199 line_height = last_height;
1200 else if (IT_CHARPOS (*it) < ZV)
1201 {
1202 move_it_by_lines (it, 1);
1203 line_height = (it->max_ascent || it->max_descent
1204 ? it->max_ascent + it->max_descent
1205 : last_height);
1206 }
1207 else
1208 {
1209 struct glyph_row *row = it->glyph_row;
1210
1211 /* Use the default character height. */
1212 it->glyph_row = NULL;
1213 it->what = IT_CHARACTER;
1214 it->c = ' ';
1215 it->len = 1;
1216 PRODUCE_GLYPHS (it);
1217 line_height = it->ascent + it->descent;
1218 it->glyph_row = row;
1219 }
1220 }
1221
1222 return line_top_y + line_height;
1223 }
1224
1225 /* Subroutine of pos_visible_p below. Extracts a display string, if
1226 any, from the display spec given as its argument. */
1227 static Lisp_Object
1228 string_from_display_spec (Lisp_Object spec)
1229 {
1230 if (CONSP (spec))
1231 {
1232 while (CONSP (spec))
1233 {
1234 if (STRINGP (XCAR (spec)))
1235 return XCAR (spec);
1236 spec = XCDR (spec);
1237 }
1238 }
1239 else if (VECTORP (spec))
1240 {
1241 ptrdiff_t i;
1242
1243 for (i = 0; i < ASIZE (spec); i++)
1244 {
1245 if (STRINGP (AREF (spec, i)))
1246 return AREF (spec, i);
1247 }
1248 return Qnil;
1249 }
1250
1251 return spec;
1252 }
1253
1254 /* Return 1 if position CHARPOS is visible in window W.
1255 CHARPOS < 0 means return info about WINDOW_END position.
1256 If visible, set *X and *Y to pixel coordinates of top left corner.
1257 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1258 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1259
1260 int
1261 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1262 int *rtop, int *rbot, int *rowh, int *vpos)
1263 {
1264 struct it it;
1265 void *itdata = bidi_shelve_cache ();
1266 struct text_pos top;
1267 int visible_p = 0;
1268 struct buffer *old_buffer = NULL;
1269
1270 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1271 return visible_p;
1272
1273 if (XBUFFER (w->buffer) != current_buffer)
1274 {
1275 old_buffer = current_buffer;
1276 set_buffer_internal_1 (XBUFFER (w->buffer));
1277 }
1278
1279 SET_TEXT_POS_FROM_MARKER (top, w->start);
1280 /* Scrolling a minibuffer window via scroll bar when the echo area
1281 shows long text sometimes resets the minibuffer contents behind
1282 our backs. */
1283 if (CHARPOS (top) > ZV)
1284 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1285
1286 /* Compute exact mode line heights. */
1287 if (WINDOW_WANTS_MODELINE_P (w))
1288 current_mode_line_height
1289 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1290 BVAR (current_buffer, mode_line_format));
1291
1292 if (WINDOW_WANTS_HEADER_LINE_P (w))
1293 current_header_line_height
1294 = display_mode_line (w, HEADER_LINE_FACE_ID,
1295 BVAR (current_buffer, header_line_format));
1296
1297 start_display (&it, w, top);
1298 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1299 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1300
1301 if (charpos >= 0
1302 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1303 && IT_CHARPOS (it) >= charpos)
1304 /* When scanning backwards under bidi iteration, move_it_to
1305 stops at or _before_ CHARPOS, because it stops at or to
1306 the _right_ of the character at CHARPOS. */
1307 || (it.bidi_p && it.bidi_it.scan_dir == -1
1308 && IT_CHARPOS (it) <= charpos)))
1309 {
1310 /* We have reached CHARPOS, or passed it. How the call to
1311 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1312 or covered by a display property, move_it_to stops at the end
1313 of the invisible text, to the right of CHARPOS. (ii) If
1314 CHARPOS is in a display vector, move_it_to stops on its last
1315 glyph. */
1316 int top_x = it.current_x;
1317 int top_y = it.current_y;
1318 /* Calling line_bottom_y may change it.method, it.position, etc. */
1319 enum it_method it_method = it.method;
1320 int bottom_y = (last_height = 0, line_bottom_y (&it));
1321 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1322
1323 if (top_y < window_top_y)
1324 visible_p = bottom_y > window_top_y;
1325 else if (top_y < it.last_visible_y)
1326 visible_p = 1;
1327 if (bottom_y >= it.last_visible_y
1328 && it.bidi_p && it.bidi_it.scan_dir == -1
1329 && IT_CHARPOS (it) < charpos)
1330 {
1331 /* When the last line of the window is scanned backwards
1332 under bidi iteration, we could be duped into thinking
1333 that we have passed CHARPOS, when in fact move_it_to
1334 simply stopped short of CHARPOS because it reached
1335 last_visible_y. To see if that's what happened, we call
1336 move_it_to again with a slightly larger vertical limit,
1337 and see if it actually moved vertically; if it did, we
1338 didn't really reach CHARPOS, which is beyond window end. */
1339 struct it save_it = it;
1340 /* Why 10? because we don't know how many canonical lines
1341 will the height of the next line(s) be. So we guess. */
1342 int ten_more_lines =
1343 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1344
1345 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1346 MOVE_TO_POS | MOVE_TO_Y);
1347 if (it.current_y > top_y)
1348 visible_p = 0;
1349
1350 it = save_it;
1351 }
1352 if (visible_p)
1353 {
1354 if (it_method == GET_FROM_DISPLAY_VECTOR)
1355 {
1356 /* We stopped on the last glyph of a display vector.
1357 Try and recompute. Hack alert! */
1358 if (charpos < 2 || top.charpos >= charpos)
1359 top_x = it.glyph_row->x;
1360 else
1361 {
1362 struct it it2;
1363 start_display (&it2, w, top);
1364 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1365 get_next_display_element (&it2);
1366 PRODUCE_GLYPHS (&it2);
1367 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1368 || it2.current_x > it2.last_visible_x)
1369 top_x = it.glyph_row->x;
1370 else
1371 {
1372 top_x = it2.current_x;
1373 top_y = it2.current_y;
1374 }
1375 }
1376 }
1377 else if (IT_CHARPOS (it) != charpos)
1378 {
1379 Lisp_Object cpos = make_number (charpos);
1380 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1381 Lisp_Object string = string_from_display_spec (spec);
1382 int newline_in_string = 0;
1383
1384 if (STRINGP (string))
1385 {
1386 const char *s = SSDATA (string);
1387 const char *e = s + SBYTES (string);
1388 while (s < e)
1389 {
1390 if (*s++ == '\n')
1391 {
1392 newline_in_string = 1;
1393 break;
1394 }
1395 }
1396 }
1397 /* The tricky code below is needed because there's a
1398 discrepancy between move_it_to and how we set cursor
1399 when the display line ends in a newline from a
1400 display string. move_it_to will stop _after_ such
1401 display strings, whereas set_cursor_from_row
1402 conspires with cursor_row_p to place the cursor on
1403 the first glyph produced from the display string. */
1404
1405 /* We have overshoot PT because it is covered by a
1406 display property whose value is a string. If the
1407 string includes embedded newlines, we are also in the
1408 wrong display line. Backtrack to the correct line,
1409 where the display string begins. */
1410 if (newline_in_string)
1411 {
1412 Lisp_Object startpos, endpos;
1413 EMACS_INT start, end;
1414 struct it it3;
1415 int it3_moved;
1416
1417 /* Find the first and the last buffer positions
1418 covered by the display string. */
1419 endpos =
1420 Fnext_single_char_property_change (cpos, Qdisplay,
1421 Qnil, Qnil);
1422 startpos =
1423 Fprevious_single_char_property_change (endpos, Qdisplay,
1424 Qnil, Qnil);
1425 start = XFASTINT (startpos);
1426 end = XFASTINT (endpos);
1427 /* Move to the last buffer position before the
1428 display property. */
1429 start_display (&it3, w, top);
1430 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1431 /* Move forward one more line if the position before
1432 the display string is a newline or if it is the
1433 rightmost character on a line that is
1434 continued or word-wrapped. */
1435 if (it3.method == GET_FROM_BUFFER
1436 && it3.c == '\n')
1437 move_it_by_lines (&it3, 1);
1438 else if (move_it_in_display_line_to (&it3, -1,
1439 it3.current_x
1440 + it3.pixel_width,
1441 MOVE_TO_X)
1442 == MOVE_LINE_CONTINUED)
1443 {
1444 move_it_by_lines (&it3, 1);
1445 /* When we are under word-wrap, the #$@%!
1446 move_it_by_lines moves 2 lines, so we need to
1447 fix that up. */
1448 if (it3.line_wrap == WORD_WRAP)
1449 move_it_by_lines (&it3, -1);
1450 }
1451
1452 /* Record the vertical coordinate of the display
1453 line where we wound up. */
1454 top_y = it3.current_y;
1455 if (it3.bidi_p)
1456 {
1457 /* When characters are reordered for display,
1458 the character displayed to the left of the
1459 display string could be _after_ the display
1460 property in the logical order. Use the
1461 smallest vertical position of these two. */
1462 start_display (&it3, w, top);
1463 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1464 if (it3.current_y < top_y)
1465 top_y = it3.current_y;
1466 }
1467 /* Move from the top of the window to the beginning
1468 of the display line where the display string
1469 begins. */
1470 start_display (&it3, w, top);
1471 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1472 /* If it3_moved stays zero after the 'while' loop
1473 below, that means we already were at a newline
1474 before the loop (e.g., the display string begins
1475 with a newline), so we don't need to (and cannot)
1476 inspect the glyphs of it3.glyph_row, because
1477 PRODUCE_GLYPHS will not produce anything for a
1478 newline, and thus it3.glyph_row stays at its
1479 stale content it got at top of the window. */
1480 it3_moved = 0;
1481 /* Finally, advance the iterator until we hit the
1482 first display element whose character position is
1483 CHARPOS, or until the first newline from the
1484 display string, which signals the end of the
1485 display line. */
1486 while (get_next_display_element (&it3))
1487 {
1488 PRODUCE_GLYPHS (&it3);
1489 if (IT_CHARPOS (it3) == charpos
1490 || ITERATOR_AT_END_OF_LINE_P (&it3))
1491 break;
1492 it3_moved = 1;
1493 set_iterator_to_next (&it3, 0);
1494 }
1495 top_x = it3.current_x - it3.pixel_width;
1496 /* Normally, we would exit the above loop because we
1497 found the display element whose character
1498 position is CHARPOS. For the contingency that we
1499 didn't, and stopped at the first newline from the
1500 display string, move back over the glyphs
1501 produced from the string, until we find the
1502 rightmost glyph not from the string. */
1503 if (it3_moved
1504 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1505 {
1506 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1507 + it3.glyph_row->used[TEXT_AREA];
1508
1509 while (EQ ((g - 1)->object, string))
1510 {
1511 --g;
1512 top_x -= g->pixel_width;
1513 }
1514 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1515 + it3.glyph_row->used[TEXT_AREA]);
1516 }
1517 }
1518 }
1519
1520 *x = top_x;
1521 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1522 *rtop = max (0, window_top_y - top_y);
1523 *rbot = max (0, bottom_y - it.last_visible_y);
1524 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1525 - max (top_y, window_top_y)));
1526 *vpos = it.vpos;
1527 }
1528 }
1529 else
1530 {
1531 /* We were asked to provide info about WINDOW_END. */
1532 struct it it2;
1533 void *it2data = NULL;
1534
1535 SAVE_IT (it2, it, it2data);
1536 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1537 move_it_by_lines (&it, 1);
1538 if (charpos < IT_CHARPOS (it)
1539 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1540 {
1541 visible_p = 1;
1542 RESTORE_IT (&it2, &it2, it2data);
1543 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1544 *x = it2.current_x;
1545 *y = it2.current_y + it2.max_ascent - it2.ascent;
1546 *rtop = max (0, -it2.current_y);
1547 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1548 - it.last_visible_y));
1549 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1550 it.last_visible_y)
1551 - max (it2.current_y,
1552 WINDOW_HEADER_LINE_HEIGHT (w))));
1553 *vpos = it2.vpos;
1554 }
1555 else
1556 bidi_unshelve_cache (it2data, 1);
1557 }
1558 bidi_unshelve_cache (itdata, 0);
1559
1560 if (old_buffer)
1561 set_buffer_internal_1 (old_buffer);
1562
1563 current_header_line_height = current_mode_line_height = -1;
1564
1565 if (visible_p && w->hscroll > 0)
1566 *x -= w->hscroll * WINDOW_FRAME_COLUMN_WIDTH (w);
1567
1568 #if 0
1569 /* Debugging code. */
1570 if (visible_p)
1571 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1572 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1573 else
1574 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1575 #endif
1576
1577 return visible_p;
1578 }
1579
1580
1581 /* Return the next character from STR. Return in *LEN the length of
1582 the character. This is like STRING_CHAR_AND_LENGTH but never
1583 returns an invalid character. If we find one, we return a `?', but
1584 with the length of the invalid character. */
1585
1586 static inline int
1587 string_char_and_length (const unsigned char *str, int *len)
1588 {
1589 int c;
1590
1591 c = STRING_CHAR_AND_LENGTH (str, *len);
1592 if (!CHAR_VALID_P (c))
1593 /* We may not change the length here because other places in Emacs
1594 don't use this function, i.e. they silently accept invalid
1595 characters. */
1596 c = '?';
1597
1598 return c;
1599 }
1600
1601
1602
1603 /* Given a position POS containing a valid character and byte position
1604 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1605
1606 static struct text_pos
1607 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1608 {
1609 eassert (STRINGP (string) && nchars >= 0);
1610
1611 if (STRING_MULTIBYTE (string))
1612 {
1613 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1614 int len;
1615
1616 while (nchars--)
1617 {
1618 string_char_and_length (p, &len);
1619 p += len;
1620 CHARPOS (pos) += 1;
1621 BYTEPOS (pos) += len;
1622 }
1623 }
1624 else
1625 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1626
1627 return pos;
1628 }
1629
1630
1631 /* Value is the text position, i.e. character and byte position,
1632 for character position CHARPOS in STRING. */
1633
1634 static inline struct text_pos
1635 string_pos (ptrdiff_t charpos, Lisp_Object string)
1636 {
1637 struct text_pos pos;
1638 eassert (STRINGP (string));
1639 eassert (charpos >= 0);
1640 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1641 return pos;
1642 }
1643
1644
1645 /* Value is a text position, i.e. character and byte position, for
1646 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1647 means recognize multibyte characters. */
1648
1649 static struct text_pos
1650 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1651 {
1652 struct text_pos pos;
1653
1654 eassert (s != NULL);
1655 eassert (charpos >= 0);
1656
1657 if (multibyte_p)
1658 {
1659 int len;
1660
1661 SET_TEXT_POS (pos, 0, 0);
1662 while (charpos--)
1663 {
1664 string_char_and_length ((const unsigned char *) s, &len);
1665 s += len;
1666 CHARPOS (pos) += 1;
1667 BYTEPOS (pos) += len;
1668 }
1669 }
1670 else
1671 SET_TEXT_POS (pos, charpos, charpos);
1672
1673 return pos;
1674 }
1675
1676
1677 /* Value is the number of characters in C string S. MULTIBYTE_P
1678 non-zero means recognize multibyte characters. */
1679
1680 static ptrdiff_t
1681 number_of_chars (const char *s, int multibyte_p)
1682 {
1683 ptrdiff_t nchars;
1684
1685 if (multibyte_p)
1686 {
1687 ptrdiff_t rest = strlen (s);
1688 int len;
1689 const unsigned char *p = (const unsigned char *) s;
1690
1691 for (nchars = 0; rest > 0; ++nchars)
1692 {
1693 string_char_and_length (p, &len);
1694 rest -= len, p += len;
1695 }
1696 }
1697 else
1698 nchars = strlen (s);
1699
1700 return nchars;
1701 }
1702
1703
1704 /* Compute byte position NEWPOS->bytepos corresponding to
1705 NEWPOS->charpos. POS is a known position in string STRING.
1706 NEWPOS->charpos must be >= POS.charpos. */
1707
1708 static void
1709 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1710 {
1711 eassert (STRINGP (string));
1712 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1713
1714 if (STRING_MULTIBYTE (string))
1715 *newpos = string_pos_nchars_ahead (pos, string,
1716 CHARPOS (*newpos) - CHARPOS (pos));
1717 else
1718 BYTEPOS (*newpos) = CHARPOS (*newpos);
1719 }
1720
1721 /* EXPORT:
1722 Return an estimation of the pixel height of mode or header lines on
1723 frame F. FACE_ID specifies what line's height to estimate. */
1724
1725 int
1726 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1727 {
1728 #ifdef HAVE_WINDOW_SYSTEM
1729 if (FRAME_WINDOW_P (f))
1730 {
1731 int height = FONT_HEIGHT (FRAME_FONT (f));
1732
1733 /* This function is called so early when Emacs starts that the face
1734 cache and mode line face are not yet initialized. */
1735 if (FRAME_FACE_CACHE (f))
1736 {
1737 struct face *face = FACE_FROM_ID (f, face_id);
1738 if (face)
1739 {
1740 if (face->font)
1741 height = FONT_HEIGHT (face->font);
1742 if (face->box_line_width > 0)
1743 height += 2 * face->box_line_width;
1744 }
1745 }
1746
1747 return height;
1748 }
1749 #endif
1750
1751 return 1;
1752 }
1753
1754 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1755 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1756 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1757 not force the value into range. */
1758
1759 void
1760 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1761 int *x, int *y, NativeRectangle *bounds, int noclip)
1762 {
1763
1764 #ifdef HAVE_WINDOW_SYSTEM
1765 if (FRAME_WINDOW_P (f))
1766 {
1767 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1768 even for negative values. */
1769 if (pix_x < 0)
1770 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1771 if (pix_y < 0)
1772 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1773
1774 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1775 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1776
1777 if (bounds)
1778 STORE_NATIVE_RECT (*bounds,
1779 FRAME_COL_TO_PIXEL_X (f, pix_x),
1780 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1781 FRAME_COLUMN_WIDTH (f) - 1,
1782 FRAME_LINE_HEIGHT (f) - 1);
1783
1784 if (!noclip)
1785 {
1786 if (pix_x < 0)
1787 pix_x = 0;
1788 else if (pix_x > FRAME_TOTAL_COLS (f))
1789 pix_x = FRAME_TOTAL_COLS (f);
1790
1791 if (pix_y < 0)
1792 pix_y = 0;
1793 else if (pix_y > FRAME_LINES (f))
1794 pix_y = FRAME_LINES (f);
1795 }
1796 }
1797 #endif
1798
1799 *x = pix_x;
1800 *y = pix_y;
1801 }
1802
1803
1804 /* Find the glyph under window-relative coordinates X/Y in window W.
1805 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1806 strings. Return in *HPOS and *VPOS the row and column number of
1807 the glyph found. Return in *AREA the glyph area containing X.
1808 Value is a pointer to the glyph found or null if X/Y is not on
1809 text, or we can't tell because W's current matrix is not up to
1810 date. */
1811
1812 static
1813 struct glyph *
1814 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1815 int *dx, int *dy, int *area)
1816 {
1817 struct glyph *glyph, *end;
1818 struct glyph_row *row = NULL;
1819 int x0, i;
1820
1821 /* Find row containing Y. Give up if some row is not enabled. */
1822 for (i = 0; i < w->current_matrix->nrows; ++i)
1823 {
1824 row = MATRIX_ROW (w->current_matrix, i);
1825 if (!row->enabled_p)
1826 return NULL;
1827 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1828 break;
1829 }
1830
1831 *vpos = i;
1832 *hpos = 0;
1833
1834 /* Give up if Y is not in the window. */
1835 if (i == w->current_matrix->nrows)
1836 return NULL;
1837
1838 /* Get the glyph area containing X. */
1839 if (w->pseudo_window_p)
1840 {
1841 *area = TEXT_AREA;
1842 x0 = 0;
1843 }
1844 else
1845 {
1846 if (x < window_box_left_offset (w, TEXT_AREA))
1847 {
1848 *area = LEFT_MARGIN_AREA;
1849 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1850 }
1851 else if (x < window_box_right_offset (w, TEXT_AREA))
1852 {
1853 *area = TEXT_AREA;
1854 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1855 }
1856 else
1857 {
1858 *area = RIGHT_MARGIN_AREA;
1859 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1860 }
1861 }
1862
1863 /* Find glyph containing X. */
1864 glyph = row->glyphs[*area];
1865 end = glyph + row->used[*area];
1866 x -= x0;
1867 while (glyph < end && x >= glyph->pixel_width)
1868 {
1869 x -= glyph->pixel_width;
1870 ++glyph;
1871 }
1872
1873 if (glyph == end)
1874 return NULL;
1875
1876 if (dx)
1877 {
1878 *dx = x;
1879 *dy = y - (row->y + row->ascent - glyph->ascent);
1880 }
1881
1882 *hpos = glyph - row->glyphs[*area];
1883 return glyph;
1884 }
1885
1886 /* Convert frame-relative x/y to coordinates relative to window W.
1887 Takes pseudo-windows into account. */
1888
1889 static void
1890 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1891 {
1892 if (w->pseudo_window_p)
1893 {
1894 /* A pseudo-window is always full-width, and starts at the
1895 left edge of the frame, plus a frame border. */
1896 struct frame *f = XFRAME (w->frame);
1897 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1898 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1899 }
1900 else
1901 {
1902 *x -= WINDOW_LEFT_EDGE_X (w);
1903 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1904 }
1905 }
1906
1907 #ifdef HAVE_WINDOW_SYSTEM
1908
1909 /* EXPORT:
1910 Return in RECTS[] at most N clipping rectangles for glyph string S.
1911 Return the number of stored rectangles. */
1912
1913 int
1914 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1915 {
1916 XRectangle r;
1917
1918 if (n <= 0)
1919 return 0;
1920
1921 if (s->row->full_width_p)
1922 {
1923 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1924 r.x = WINDOW_LEFT_EDGE_X (s->w);
1925 r.width = WINDOW_TOTAL_WIDTH (s->w);
1926
1927 /* Unless displaying a mode or menu bar line, which are always
1928 fully visible, clip to the visible part of the row. */
1929 if (s->w->pseudo_window_p)
1930 r.height = s->row->visible_height;
1931 else
1932 r.height = s->height;
1933 }
1934 else
1935 {
1936 /* This is a text line that may be partially visible. */
1937 r.x = window_box_left (s->w, s->area);
1938 r.width = window_box_width (s->w, s->area);
1939 r.height = s->row->visible_height;
1940 }
1941
1942 if (s->clip_head)
1943 if (r.x < s->clip_head->x)
1944 {
1945 if (r.width >= s->clip_head->x - r.x)
1946 r.width -= s->clip_head->x - r.x;
1947 else
1948 r.width = 0;
1949 r.x = s->clip_head->x;
1950 }
1951 if (s->clip_tail)
1952 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1953 {
1954 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1955 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1956 else
1957 r.width = 0;
1958 }
1959
1960 /* If S draws overlapping rows, it's sufficient to use the top and
1961 bottom of the window for clipping because this glyph string
1962 intentionally draws over other lines. */
1963 if (s->for_overlaps)
1964 {
1965 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1966 r.height = window_text_bottom_y (s->w) - r.y;
1967
1968 /* Alas, the above simple strategy does not work for the
1969 environments with anti-aliased text: if the same text is
1970 drawn onto the same place multiple times, it gets thicker.
1971 If the overlap we are processing is for the erased cursor, we
1972 take the intersection with the rectangle of the cursor. */
1973 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1974 {
1975 XRectangle rc, r_save = r;
1976
1977 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1978 rc.y = s->w->phys_cursor.y;
1979 rc.width = s->w->phys_cursor_width;
1980 rc.height = s->w->phys_cursor_height;
1981
1982 x_intersect_rectangles (&r_save, &rc, &r);
1983 }
1984 }
1985 else
1986 {
1987 /* Don't use S->y for clipping because it doesn't take partially
1988 visible lines into account. For example, it can be negative for
1989 partially visible lines at the top of a window. */
1990 if (!s->row->full_width_p
1991 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1992 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1993 else
1994 r.y = max (0, s->row->y);
1995 }
1996
1997 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1998
1999 /* If drawing the cursor, don't let glyph draw outside its
2000 advertised boundaries. Cleartype does this under some circumstances. */
2001 if (s->hl == DRAW_CURSOR)
2002 {
2003 struct glyph *glyph = s->first_glyph;
2004 int height, max_y;
2005
2006 if (s->x > r.x)
2007 {
2008 r.width -= s->x - r.x;
2009 r.x = s->x;
2010 }
2011 r.width = min (r.width, glyph->pixel_width);
2012
2013 /* If r.y is below window bottom, ensure that we still see a cursor. */
2014 height = min (glyph->ascent + glyph->descent,
2015 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2016 max_y = window_text_bottom_y (s->w) - height;
2017 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2018 if (s->ybase - glyph->ascent > max_y)
2019 {
2020 r.y = max_y;
2021 r.height = height;
2022 }
2023 else
2024 {
2025 /* Don't draw cursor glyph taller than our actual glyph. */
2026 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2027 if (height < r.height)
2028 {
2029 max_y = r.y + r.height;
2030 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2031 r.height = min (max_y - r.y, height);
2032 }
2033 }
2034 }
2035
2036 if (s->row->clip)
2037 {
2038 XRectangle r_save = r;
2039
2040 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2041 r.width = 0;
2042 }
2043
2044 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2045 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2046 {
2047 #ifdef CONVERT_FROM_XRECT
2048 CONVERT_FROM_XRECT (r, *rects);
2049 #else
2050 *rects = r;
2051 #endif
2052 return 1;
2053 }
2054 else
2055 {
2056 /* If we are processing overlapping and allowed to return
2057 multiple clipping rectangles, we exclude the row of the glyph
2058 string from the clipping rectangle. This is to avoid drawing
2059 the same text on the environment with anti-aliasing. */
2060 #ifdef CONVERT_FROM_XRECT
2061 XRectangle rs[2];
2062 #else
2063 XRectangle *rs = rects;
2064 #endif
2065 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2066
2067 if (s->for_overlaps & OVERLAPS_PRED)
2068 {
2069 rs[i] = r;
2070 if (r.y + r.height > row_y)
2071 {
2072 if (r.y < row_y)
2073 rs[i].height = row_y - r.y;
2074 else
2075 rs[i].height = 0;
2076 }
2077 i++;
2078 }
2079 if (s->for_overlaps & OVERLAPS_SUCC)
2080 {
2081 rs[i] = r;
2082 if (r.y < row_y + s->row->visible_height)
2083 {
2084 if (r.y + r.height > row_y + s->row->visible_height)
2085 {
2086 rs[i].y = row_y + s->row->visible_height;
2087 rs[i].height = r.y + r.height - rs[i].y;
2088 }
2089 else
2090 rs[i].height = 0;
2091 }
2092 i++;
2093 }
2094
2095 n = i;
2096 #ifdef CONVERT_FROM_XRECT
2097 for (i = 0; i < n; i++)
2098 CONVERT_FROM_XRECT (rs[i], rects[i]);
2099 #endif
2100 return n;
2101 }
2102 }
2103
2104 /* EXPORT:
2105 Return in *NR the clipping rectangle for glyph string S. */
2106
2107 void
2108 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2109 {
2110 get_glyph_string_clip_rects (s, nr, 1);
2111 }
2112
2113
2114 /* EXPORT:
2115 Return the position and height of the phys cursor in window W.
2116 Set w->phys_cursor_width to width of phys cursor.
2117 */
2118
2119 void
2120 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2121 struct glyph *glyph, int *xp, int *yp, int *heightp)
2122 {
2123 struct frame *f = XFRAME (WINDOW_FRAME (w));
2124 int x, y, wd, h, h0, y0;
2125
2126 /* Compute the width of the rectangle to draw. If on a stretch
2127 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2128 rectangle as wide as the glyph, but use a canonical character
2129 width instead. */
2130 wd = glyph->pixel_width - 1;
2131 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2132 wd++; /* Why? */
2133 #endif
2134
2135 x = w->phys_cursor.x;
2136 if (x < 0)
2137 {
2138 wd += x;
2139 x = 0;
2140 }
2141
2142 if (glyph->type == STRETCH_GLYPH
2143 && !x_stretch_cursor_p)
2144 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2145 w->phys_cursor_width = wd;
2146
2147 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2148
2149 /* If y is below window bottom, ensure that we still see a cursor. */
2150 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2151
2152 h = max (h0, glyph->ascent + glyph->descent);
2153 h0 = min (h0, glyph->ascent + glyph->descent);
2154
2155 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2156 if (y < y0)
2157 {
2158 h = max (h - (y0 - y) + 1, h0);
2159 y = y0 - 1;
2160 }
2161 else
2162 {
2163 y0 = window_text_bottom_y (w) - h0;
2164 if (y > y0)
2165 {
2166 h += y - y0;
2167 y = y0;
2168 }
2169 }
2170
2171 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2172 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2173 *heightp = h;
2174 }
2175
2176 /*
2177 * Remember which glyph the mouse is over.
2178 */
2179
2180 void
2181 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2182 {
2183 Lisp_Object window;
2184 struct window *w;
2185 struct glyph_row *r, *gr, *end_row;
2186 enum window_part part;
2187 enum glyph_row_area area;
2188 int x, y, width, height;
2189
2190 /* Try to determine frame pixel position and size of the glyph under
2191 frame pixel coordinates X/Y on frame F. */
2192
2193 if (!f->glyphs_initialized_p
2194 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2195 NILP (window)))
2196 {
2197 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2198 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2199 goto virtual_glyph;
2200 }
2201
2202 w = XWINDOW (window);
2203 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2204 height = WINDOW_FRAME_LINE_HEIGHT (w);
2205
2206 x = window_relative_x_coord (w, part, gx);
2207 y = gy - WINDOW_TOP_EDGE_Y (w);
2208
2209 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2210 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2211
2212 if (w->pseudo_window_p)
2213 {
2214 area = TEXT_AREA;
2215 part = ON_MODE_LINE; /* Don't adjust margin. */
2216 goto text_glyph;
2217 }
2218
2219 switch (part)
2220 {
2221 case ON_LEFT_MARGIN:
2222 area = LEFT_MARGIN_AREA;
2223 goto text_glyph;
2224
2225 case ON_RIGHT_MARGIN:
2226 area = RIGHT_MARGIN_AREA;
2227 goto text_glyph;
2228
2229 case ON_HEADER_LINE:
2230 case ON_MODE_LINE:
2231 gr = (part == ON_HEADER_LINE
2232 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2233 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2234 gy = gr->y;
2235 area = TEXT_AREA;
2236 goto text_glyph_row_found;
2237
2238 case ON_TEXT:
2239 area = TEXT_AREA;
2240
2241 text_glyph:
2242 gr = 0; gy = 0;
2243 for (; r <= end_row && r->enabled_p; ++r)
2244 if (r->y + r->height > y)
2245 {
2246 gr = r; gy = r->y;
2247 break;
2248 }
2249
2250 text_glyph_row_found:
2251 if (gr && gy <= y)
2252 {
2253 struct glyph *g = gr->glyphs[area];
2254 struct glyph *end = g + gr->used[area];
2255
2256 height = gr->height;
2257 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2258 if (gx + g->pixel_width > x)
2259 break;
2260
2261 if (g < end)
2262 {
2263 if (g->type == IMAGE_GLYPH)
2264 {
2265 /* Don't remember when mouse is over image, as
2266 image may have hot-spots. */
2267 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2268 return;
2269 }
2270 width = g->pixel_width;
2271 }
2272 else
2273 {
2274 /* Use nominal char spacing at end of line. */
2275 x -= gx;
2276 gx += (x / width) * width;
2277 }
2278
2279 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2280 gx += window_box_left_offset (w, area);
2281 }
2282 else
2283 {
2284 /* Use nominal line height at end of window. */
2285 gx = (x / width) * width;
2286 y -= gy;
2287 gy += (y / height) * height;
2288 }
2289 break;
2290
2291 case ON_LEFT_FRINGE:
2292 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2293 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2294 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2295 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2296 goto row_glyph;
2297
2298 case ON_RIGHT_FRINGE:
2299 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2300 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2301 : window_box_right_offset (w, TEXT_AREA));
2302 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2303 goto row_glyph;
2304
2305 case ON_SCROLL_BAR:
2306 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2307 ? 0
2308 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2309 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2310 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2311 : 0)));
2312 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2313
2314 row_glyph:
2315 gr = 0, gy = 0;
2316 for (; r <= end_row && r->enabled_p; ++r)
2317 if (r->y + r->height > y)
2318 {
2319 gr = r; gy = r->y;
2320 break;
2321 }
2322
2323 if (gr && gy <= y)
2324 height = gr->height;
2325 else
2326 {
2327 /* Use nominal line height at end of window. */
2328 y -= gy;
2329 gy += (y / height) * height;
2330 }
2331 break;
2332
2333 default:
2334 ;
2335 virtual_glyph:
2336 /* If there is no glyph under the mouse, then we divide the screen
2337 into a grid of the smallest glyph in the frame, and use that
2338 as our "glyph". */
2339
2340 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2341 round down even for negative values. */
2342 if (gx < 0)
2343 gx -= width - 1;
2344 if (gy < 0)
2345 gy -= height - 1;
2346
2347 gx = (gx / width) * width;
2348 gy = (gy / height) * height;
2349
2350 goto store_rect;
2351 }
2352
2353 gx += WINDOW_LEFT_EDGE_X (w);
2354 gy += WINDOW_TOP_EDGE_Y (w);
2355
2356 store_rect:
2357 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2358
2359 /* Visible feedback for debugging. */
2360 #if 0
2361 #if HAVE_X_WINDOWS
2362 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2363 f->output_data.x->normal_gc,
2364 gx, gy, width, height);
2365 #endif
2366 #endif
2367 }
2368
2369
2370 #endif /* HAVE_WINDOW_SYSTEM */
2371
2372 \f
2373 /***********************************************************************
2374 Lisp form evaluation
2375 ***********************************************************************/
2376
2377 /* Error handler for safe_eval and safe_call. */
2378
2379 static Lisp_Object
2380 safe_eval_handler (Lisp_Object arg)
2381 {
2382 add_to_log ("Error during redisplay: %S", arg, Qnil);
2383 return Qnil;
2384 }
2385
2386
2387 /* Evaluate SEXPR and return the result, or nil if something went
2388 wrong. Prevent redisplay during the evaluation. */
2389
2390 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2391 Return the result, or nil if something went wrong. Prevent
2392 redisplay during the evaluation. */
2393
2394 Lisp_Object
2395 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2396 {
2397 Lisp_Object val;
2398
2399 if (inhibit_eval_during_redisplay)
2400 val = Qnil;
2401 else
2402 {
2403 ptrdiff_t count = SPECPDL_INDEX ();
2404 struct gcpro gcpro1;
2405
2406 GCPRO1 (args[0]);
2407 gcpro1.nvars = nargs;
2408 specbind (Qinhibit_redisplay, Qt);
2409 /* Use Qt to ensure debugger does not run,
2410 so there is no possibility of wanting to redisplay. */
2411 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2412 safe_eval_handler);
2413 UNGCPRO;
2414 val = unbind_to (count, val);
2415 }
2416
2417 return val;
2418 }
2419
2420
2421 /* Call function FN with one argument ARG.
2422 Return the result, or nil if something went wrong. */
2423
2424 Lisp_Object
2425 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2426 {
2427 Lisp_Object args[2];
2428 args[0] = fn;
2429 args[1] = arg;
2430 return safe_call (2, args);
2431 }
2432
2433 static Lisp_Object Qeval;
2434
2435 Lisp_Object
2436 safe_eval (Lisp_Object sexpr)
2437 {
2438 return safe_call1 (Qeval, sexpr);
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_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2446 {
2447 Lisp_Object args[3];
2448 args[0] = fn;
2449 args[1] = arg1;
2450 args[2] = arg2;
2451 return safe_call (3, args);
2452 }
2453
2454
2455 \f
2456 /***********************************************************************
2457 Debugging
2458 ***********************************************************************/
2459
2460 #if 0
2461
2462 /* Define CHECK_IT to perform sanity checks on iterators.
2463 This is for debugging. It is too slow to do unconditionally. */
2464
2465 static void
2466 check_it (struct it *it)
2467 {
2468 if (it->method == GET_FROM_STRING)
2469 {
2470 eassert (STRINGP (it->string));
2471 eassert (IT_STRING_CHARPOS (*it) >= 0);
2472 }
2473 else
2474 {
2475 eassert (IT_STRING_CHARPOS (*it) < 0);
2476 if (it->method == GET_FROM_BUFFER)
2477 {
2478 /* Check that character and byte positions agree. */
2479 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2480 }
2481 }
2482
2483 if (it->dpvec)
2484 eassert (it->current.dpvec_index >= 0);
2485 else
2486 eassert (it->current.dpvec_index < 0);
2487 }
2488
2489 #define CHECK_IT(IT) check_it ((IT))
2490
2491 #else /* not 0 */
2492
2493 #define CHECK_IT(IT) (void) 0
2494
2495 #endif /* not 0 */
2496
2497
2498 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2499
2500 /* Check that the window end of window W is what we expect it
2501 to be---the last row in the current matrix displaying text. */
2502
2503 static void
2504 check_window_end (struct window *w)
2505 {
2506 if (!MINI_WINDOW_P (w)
2507 && !NILP (w->window_end_valid))
2508 {
2509 struct glyph_row *row;
2510 eassert ((row = MATRIX_ROW (w->current_matrix,
2511 XFASTINT (w->window_end_vpos)),
2512 !row->enabled_p
2513 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2514 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2515 }
2516 }
2517
2518 #define CHECK_WINDOW_END(W) check_window_end ((W))
2519
2520 #else
2521
2522 #define CHECK_WINDOW_END(W) (void) 0
2523
2524 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2525
2526
2527 \f
2528 /***********************************************************************
2529 Iterator initialization
2530 ***********************************************************************/
2531
2532 /* Initialize IT for displaying current_buffer in window W, starting
2533 at character position CHARPOS. CHARPOS < 0 means that no buffer
2534 position is specified which is useful when the iterator is assigned
2535 a position later. BYTEPOS is the byte position corresponding to
2536 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2537
2538 If ROW is not null, calls to produce_glyphs with IT as parameter
2539 will produce glyphs in that row.
2540
2541 BASE_FACE_ID is the id of a base face to use. It must be one of
2542 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2543 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2544 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2545
2546 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2547 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2548 will be initialized to use the corresponding mode line glyph row of
2549 the desired matrix of W. */
2550
2551 void
2552 init_iterator (struct it *it, struct window *w,
2553 ptrdiff_t charpos, ptrdiff_t bytepos,
2554 struct glyph_row *row, enum face_id base_face_id)
2555 {
2556 int highlight_region_p;
2557 enum face_id remapped_base_face_id = base_face_id;
2558
2559 /* Some precondition checks. */
2560 eassert (w != NULL && it != NULL);
2561 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2562 && charpos <= ZV));
2563
2564 /* If face attributes have been changed since the last redisplay,
2565 free realized faces now because they depend on face definitions
2566 that might have changed. Don't free faces while there might be
2567 desired matrices pending which reference these faces. */
2568 if (face_change_count && !inhibit_free_realized_faces)
2569 {
2570 face_change_count = 0;
2571 free_all_realized_faces (Qnil);
2572 }
2573
2574 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2575 if (! NILP (Vface_remapping_alist))
2576 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2577
2578 /* Use one of the mode line rows of W's desired matrix if
2579 appropriate. */
2580 if (row == NULL)
2581 {
2582 if (base_face_id == MODE_LINE_FACE_ID
2583 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2584 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2585 else if (base_face_id == HEADER_LINE_FACE_ID)
2586 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2587 }
2588
2589 /* Clear IT. */
2590 memset (it, 0, sizeof *it);
2591 it->current.overlay_string_index = -1;
2592 it->current.dpvec_index = -1;
2593 it->base_face_id = remapped_base_face_id;
2594 it->string = Qnil;
2595 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2596 it->paragraph_embedding = L2R;
2597 it->bidi_it.string.lstring = Qnil;
2598 it->bidi_it.string.s = NULL;
2599 it->bidi_it.string.bufpos = 0;
2600
2601 /* The window in which we iterate over current_buffer: */
2602 XSETWINDOW (it->window, w);
2603 it->w = w;
2604 it->f = XFRAME (w->frame);
2605
2606 it->cmp_it.id = -1;
2607
2608 /* Extra space between lines (on window systems only). */
2609 if (base_face_id == DEFAULT_FACE_ID
2610 && FRAME_WINDOW_P (it->f))
2611 {
2612 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2613 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2614 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2615 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2616 * FRAME_LINE_HEIGHT (it->f));
2617 else if (it->f->extra_line_spacing > 0)
2618 it->extra_line_spacing = it->f->extra_line_spacing;
2619 it->max_extra_line_spacing = 0;
2620 }
2621
2622 /* If realized faces have been removed, e.g. because of face
2623 attribute changes of named faces, recompute them. When running
2624 in batch mode, the face cache of the initial frame is null. If
2625 we happen to get called, make a dummy face cache. */
2626 if (FRAME_FACE_CACHE (it->f) == NULL)
2627 init_frame_faces (it->f);
2628 if (FRAME_FACE_CACHE (it->f)->used == 0)
2629 recompute_basic_faces (it->f);
2630
2631 /* Current value of the `slice', `space-width', and 'height' properties. */
2632 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2633 it->space_width = Qnil;
2634 it->font_height = Qnil;
2635 it->override_ascent = -1;
2636
2637 /* Are control characters displayed as `^C'? */
2638 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2639
2640 /* -1 means everything between a CR and the following line end
2641 is invisible. >0 means lines indented more than this value are
2642 invisible. */
2643 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2644 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2645 selective_display)),
2646 PTRDIFF_MAX)
2647 : (!NILP (BVAR (current_buffer, selective_display))
2648 ? -1 : 0));
2649 it->selective_display_ellipsis_p
2650 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2651
2652 /* Display table to use. */
2653 it->dp = window_display_table (w);
2654
2655 /* Are multibyte characters enabled in current_buffer? */
2656 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2657
2658 /* Non-zero if we should highlight the region. */
2659 highlight_region_p
2660 = (!NILP (Vtransient_mark_mode)
2661 && !NILP (BVAR (current_buffer, mark_active))
2662 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2663
2664 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2665 start and end of a visible region in window IT->w. Set both to
2666 -1 to indicate no region. */
2667 if (highlight_region_p
2668 /* Maybe highlight only in selected window. */
2669 && (/* Either show region everywhere. */
2670 highlight_nonselected_windows
2671 /* Or show region in the selected window. */
2672 || w == XWINDOW (selected_window)
2673 /* Or show the region if we are in the mini-buffer and W is
2674 the window the mini-buffer refers to. */
2675 || (MINI_WINDOW_P (XWINDOW (selected_window))
2676 && WINDOWP (minibuf_selected_window)
2677 && w == XWINDOW (minibuf_selected_window))))
2678 {
2679 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2680 it->region_beg_charpos = min (PT, markpos);
2681 it->region_end_charpos = max (PT, markpos);
2682 }
2683 else
2684 it->region_beg_charpos = it->region_end_charpos = -1;
2685
2686 /* Get the position at which the redisplay_end_trigger hook should
2687 be run, if it is to be run at all. */
2688 if (MARKERP (w->redisplay_end_trigger)
2689 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2690 it->redisplay_end_trigger_charpos
2691 = marker_position (w->redisplay_end_trigger);
2692 else if (INTEGERP (w->redisplay_end_trigger))
2693 it->redisplay_end_trigger_charpos =
2694 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2695
2696 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2697
2698 /* Are lines in the display truncated? */
2699 if (base_face_id != DEFAULT_FACE_ID
2700 || it->w->hscroll
2701 || (! WINDOW_FULL_WIDTH_P (it->w)
2702 && ((!NILP (Vtruncate_partial_width_windows)
2703 && !INTEGERP (Vtruncate_partial_width_windows))
2704 || (INTEGERP (Vtruncate_partial_width_windows)
2705 && (WINDOW_TOTAL_COLS (it->w)
2706 < XINT (Vtruncate_partial_width_windows))))))
2707 it->line_wrap = TRUNCATE;
2708 else if (NILP (BVAR (current_buffer, truncate_lines)))
2709 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2710 ? WINDOW_WRAP : WORD_WRAP;
2711 else
2712 it->line_wrap = TRUNCATE;
2713
2714 /* Get dimensions of truncation and continuation glyphs. These are
2715 displayed as fringe bitmaps under X, so we don't need them for such
2716 frames. */
2717 if (!FRAME_WINDOW_P (it->f))
2718 {
2719 if (it->line_wrap == TRUNCATE)
2720 {
2721 /* We will need the truncation glyph. */
2722 eassert (it->glyph_row == NULL);
2723 produce_special_glyphs (it, IT_TRUNCATION);
2724 it->truncation_pixel_width = it->pixel_width;
2725 }
2726 else
2727 {
2728 /* We will need the continuation glyph. */
2729 eassert (it->glyph_row == NULL);
2730 produce_special_glyphs (it, IT_CONTINUATION);
2731 it->continuation_pixel_width = it->pixel_width;
2732 }
2733
2734 /* Reset these values to zero because the produce_special_glyphs
2735 above has changed them. */
2736 it->pixel_width = it->ascent = it->descent = 0;
2737 it->phys_ascent = it->phys_descent = 0;
2738 }
2739
2740 /* Set this after getting the dimensions of truncation and
2741 continuation glyphs, so that we don't produce glyphs when calling
2742 produce_special_glyphs, above. */
2743 it->glyph_row = row;
2744 it->area = TEXT_AREA;
2745
2746 /* Forget any previous info about this row being reversed. */
2747 if (it->glyph_row)
2748 it->glyph_row->reversed_p = 0;
2749
2750 /* Get the dimensions of the display area. The display area
2751 consists of the visible window area plus a horizontally scrolled
2752 part to the left of the window. All x-values are relative to the
2753 start of this total display area. */
2754 if (base_face_id != DEFAULT_FACE_ID)
2755 {
2756 /* Mode lines, menu bar in terminal frames. */
2757 it->first_visible_x = 0;
2758 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2759 }
2760 else
2761 {
2762 it->first_visible_x
2763 = it->w->hscroll * FRAME_COLUMN_WIDTH (it->f);
2764 it->last_visible_x = (it->first_visible_x
2765 + window_box_width (w, TEXT_AREA));
2766
2767 /* If we truncate lines, leave room for the truncator glyph(s) at
2768 the right margin. Otherwise, leave room for the continuation
2769 glyph(s). Truncation and continuation glyphs are not inserted
2770 for window-based redisplay. */
2771 if (!FRAME_WINDOW_P (it->f))
2772 {
2773 if (it->line_wrap == TRUNCATE)
2774 it->last_visible_x -= it->truncation_pixel_width;
2775 else
2776 it->last_visible_x -= it->continuation_pixel_width;
2777 }
2778
2779 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2780 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2781 }
2782
2783 /* Leave room for a border glyph. */
2784 if (!FRAME_WINDOW_P (it->f)
2785 && !WINDOW_RIGHTMOST_P (it->w))
2786 it->last_visible_x -= 1;
2787
2788 it->last_visible_y = window_text_bottom_y (w);
2789
2790 /* For mode lines and alike, arrange for the first glyph having a
2791 left box line if the face specifies a box. */
2792 if (base_face_id != DEFAULT_FACE_ID)
2793 {
2794 struct face *face;
2795
2796 it->face_id = remapped_base_face_id;
2797
2798 /* If we have a boxed mode line, make the first character appear
2799 with a left box line. */
2800 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2801 if (face->box != FACE_NO_BOX)
2802 it->start_of_box_run_p = 1;
2803 }
2804
2805 /* If a buffer position was specified, set the iterator there,
2806 getting overlays and face properties from that position. */
2807 if (charpos >= BUF_BEG (current_buffer))
2808 {
2809 it->end_charpos = ZV;
2810 IT_CHARPOS (*it) = charpos;
2811
2812 /* We will rely on `reseat' to set this up properly, via
2813 handle_face_prop. */
2814 it->face_id = it->base_face_id;
2815
2816 /* Compute byte position if not specified. */
2817 if (bytepos < charpos)
2818 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2819 else
2820 IT_BYTEPOS (*it) = bytepos;
2821
2822 it->start = it->current;
2823 /* Do we need to reorder bidirectional text? Not if this is a
2824 unibyte buffer: by definition, none of the single-byte
2825 characters are strong R2L, so no reordering is needed. And
2826 bidi.c doesn't support unibyte buffers anyway. Also, don't
2827 reorder while we are loading loadup.el, since the tables of
2828 character properties needed for reordering are not yet
2829 available. */
2830 it->bidi_p =
2831 NILP (Vpurify_flag)
2832 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2833 && it->multibyte_p;
2834
2835 /* If we are to reorder bidirectional text, init the bidi
2836 iterator. */
2837 if (it->bidi_p)
2838 {
2839 /* Note the paragraph direction that this buffer wants to
2840 use. */
2841 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2842 Qleft_to_right))
2843 it->paragraph_embedding = L2R;
2844 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2845 Qright_to_left))
2846 it->paragraph_embedding = R2L;
2847 else
2848 it->paragraph_embedding = NEUTRAL_DIR;
2849 bidi_unshelve_cache (NULL, 0);
2850 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2851 &it->bidi_it);
2852 }
2853
2854 /* Compute faces etc. */
2855 reseat (it, it->current.pos, 1);
2856 }
2857
2858 CHECK_IT (it);
2859 }
2860
2861
2862 /* Initialize IT for the display of window W with window start POS. */
2863
2864 void
2865 start_display (struct it *it, struct window *w, struct text_pos pos)
2866 {
2867 struct glyph_row *row;
2868 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2869
2870 row = w->desired_matrix->rows + first_vpos;
2871 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2872 it->first_vpos = first_vpos;
2873
2874 /* Don't reseat to previous visible line start if current start
2875 position is in a string or image. */
2876 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2877 {
2878 int start_at_line_beg_p;
2879 int first_y = it->current_y;
2880
2881 /* If window start is not at a line start, skip forward to POS to
2882 get the correct continuation lines width. */
2883 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2884 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2885 if (!start_at_line_beg_p)
2886 {
2887 int new_x;
2888
2889 reseat_at_previous_visible_line_start (it);
2890 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2891
2892 new_x = it->current_x + it->pixel_width;
2893
2894 /* If lines are continued, this line may end in the middle
2895 of a multi-glyph character (e.g. a control character
2896 displayed as \003, or in the middle of an overlay
2897 string). In this case move_it_to above will not have
2898 taken us to the start of the continuation line but to the
2899 end of the continued line. */
2900 if (it->current_x > 0
2901 && it->line_wrap != TRUNCATE /* Lines are continued. */
2902 && (/* And glyph doesn't fit on the line. */
2903 new_x > it->last_visible_x
2904 /* Or it fits exactly and we're on a window
2905 system frame. */
2906 || (new_x == it->last_visible_x
2907 && FRAME_WINDOW_P (it->f))))
2908 {
2909 if ((it->current.dpvec_index >= 0
2910 || it->current.overlay_string_index >= 0)
2911 /* If we are on a newline from a display vector or
2912 overlay string, then we are already at the end of
2913 a screen line; no need to go to the next line in
2914 that case, as this line is not really continued.
2915 (If we do go to the next line, C-e will not DTRT.) */
2916 && it->c != '\n')
2917 {
2918 set_iterator_to_next (it, 1);
2919 move_it_in_display_line_to (it, -1, -1, 0);
2920 }
2921
2922 it->continuation_lines_width += it->current_x;
2923 }
2924 /* If the character at POS is displayed via a display
2925 vector, move_it_to above stops at the final glyph of
2926 IT->dpvec. To make the caller redisplay that character
2927 again (a.k.a. start at POS), we need to reset the
2928 dpvec_index to the beginning of IT->dpvec. */
2929 else if (it->current.dpvec_index >= 0)
2930 it->current.dpvec_index = 0;
2931
2932 /* We're starting a new display line, not affected by the
2933 height of the continued line, so clear the appropriate
2934 fields in the iterator structure. */
2935 it->max_ascent = it->max_descent = 0;
2936 it->max_phys_ascent = it->max_phys_descent = 0;
2937
2938 it->current_y = first_y;
2939 it->vpos = 0;
2940 it->current_x = it->hpos = 0;
2941 }
2942 }
2943 }
2944
2945
2946 /* Return 1 if POS is a position in ellipses displayed for invisible
2947 text. W is the window we display, for text property lookup. */
2948
2949 static int
2950 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2951 {
2952 Lisp_Object prop, window;
2953 int ellipses_p = 0;
2954 ptrdiff_t charpos = CHARPOS (pos->pos);
2955
2956 /* If POS specifies a position in a display vector, this might
2957 be for an ellipsis displayed for invisible text. We won't
2958 get the iterator set up for delivering that ellipsis unless
2959 we make sure that it gets aware of the invisible text. */
2960 if (pos->dpvec_index >= 0
2961 && pos->overlay_string_index < 0
2962 && CHARPOS (pos->string_pos) < 0
2963 && charpos > BEGV
2964 && (XSETWINDOW (window, w),
2965 prop = Fget_char_property (make_number (charpos),
2966 Qinvisible, window),
2967 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2968 {
2969 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2970 window);
2971 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2972 }
2973
2974 return ellipses_p;
2975 }
2976
2977
2978 /* Initialize IT for stepping through current_buffer in window W,
2979 starting at position POS that includes overlay string and display
2980 vector/ control character translation position information. Value
2981 is zero if there are overlay strings with newlines at POS. */
2982
2983 static int
2984 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2985 {
2986 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2987 int i, overlay_strings_with_newlines = 0;
2988
2989 /* If POS specifies a position in a display vector, this might
2990 be for an ellipsis displayed for invisible text. We won't
2991 get the iterator set up for delivering that ellipsis unless
2992 we make sure that it gets aware of the invisible text. */
2993 if (in_ellipses_for_invisible_text_p (pos, w))
2994 {
2995 --charpos;
2996 bytepos = 0;
2997 }
2998
2999 /* Keep in mind: the call to reseat in init_iterator skips invisible
3000 text, so we might end up at a position different from POS. This
3001 is only a problem when POS is a row start after a newline and an
3002 overlay starts there with an after-string, and the overlay has an
3003 invisible property. Since we don't skip invisible text in
3004 display_line and elsewhere immediately after consuming the
3005 newline before the row start, such a POS will not be in a string,
3006 but the call to init_iterator below will move us to the
3007 after-string. */
3008 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3009
3010 /* This only scans the current chunk -- it should scan all chunks.
3011 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3012 to 16 in 22.1 to make this a lesser problem. */
3013 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3014 {
3015 const char *s = SSDATA (it->overlay_strings[i]);
3016 const char *e = s + SBYTES (it->overlay_strings[i]);
3017
3018 while (s < e && *s != '\n')
3019 ++s;
3020
3021 if (s < e)
3022 {
3023 overlay_strings_with_newlines = 1;
3024 break;
3025 }
3026 }
3027
3028 /* If position is within an overlay string, set up IT to the right
3029 overlay string. */
3030 if (pos->overlay_string_index >= 0)
3031 {
3032 int relative_index;
3033
3034 /* If the first overlay string happens to have a `display'
3035 property for an image, the iterator will be set up for that
3036 image, and we have to undo that setup first before we can
3037 correct the overlay string index. */
3038 if (it->method == GET_FROM_IMAGE)
3039 pop_it (it);
3040
3041 /* We already have the first chunk of overlay strings in
3042 IT->overlay_strings. Load more until the one for
3043 pos->overlay_string_index is in IT->overlay_strings. */
3044 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3045 {
3046 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3047 it->current.overlay_string_index = 0;
3048 while (n--)
3049 {
3050 load_overlay_strings (it, 0);
3051 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3052 }
3053 }
3054
3055 it->current.overlay_string_index = pos->overlay_string_index;
3056 relative_index = (it->current.overlay_string_index
3057 % OVERLAY_STRING_CHUNK_SIZE);
3058 it->string = it->overlay_strings[relative_index];
3059 eassert (STRINGP (it->string));
3060 it->current.string_pos = pos->string_pos;
3061 it->method = GET_FROM_STRING;
3062 }
3063
3064 if (CHARPOS (pos->string_pos) >= 0)
3065 {
3066 /* Recorded position is not in an overlay string, but in another
3067 string. This can only be a string from a `display' property.
3068 IT should already be filled with that string. */
3069 it->current.string_pos = pos->string_pos;
3070 eassert (STRINGP (it->string));
3071 }
3072
3073 /* Restore position in display vector translations, control
3074 character translations or ellipses. */
3075 if (pos->dpvec_index >= 0)
3076 {
3077 if (it->dpvec == NULL)
3078 get_next_display_element (it);
3079 eassert (it->dpvec && it->current.dpvec_index == 0);
3080 it->current.dpvec_index = pos->dpvec_index;
3081 }
3082
3083 CHECK_IT (it);
3084 return !overlay_strings_with_newlines;
3085 }
3086
3087
3088 /* Initialize IT for stepping through current_buffer in window W
3089 starting at ROW->start. */
3090
3091 static void
3092 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3093 {
3094 init_from_display_pos (it, w, &row->start);
3095 it->start = row->start;
3096 it->continuation_lines_width = row->continuation_lines_width;
3097 CHECK_IT (it);
3098 }
3099
3100
3101 /* Initialize IT for stepping through current_buffer in window W
3102 starting in the line following ROW, i.e. starting at ROW->end.
3103 Value is zero if there are overlay strings with newlines at ROW's
3104 end position. */
3105
3106 static int
3107 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3108 {
3109 int success = 0;
3110
3111 if (init_from_display_pos (it, w, &row->end))
3112 {
3113 if (row->continued_p)
3114 it->continuation_lines_width
3115 = row->continuation_lines_width + row->pixel_width;
3116 CHECK_IT (it);
3117 success = 1;
3118 }
3119
3120 return success;
3121 }
3122
3123
3124
3125 \f
3126 /***********************************************************************
3127 Text properties
3128 ***********************************************************************/
3129
3130 /* Called when IT reaches IT->stop_charpos. Handle text property and
3131 overlay changes. Set IT->stop_charpos to the next position where
3132 to stop. */
3133
3134 static void
3135 handle_stop (struct it *it)
3136 {
3137 enum prop_handled handled;
3138 int handle_overlay_change_p;
3139 struct props *p;
3140
3141 it->dpvec = NULL;
3142 it->current.dpvec_index = -1;
3143 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3144 it->ignore_overlay_strings_at_pos_p = 0;
3145 it->ellipsis_p = 0;
3146
3147 /* Use face of preceding text for ellipsis (if invisible) */
3148 if (it->selective_display_ellipsis_p)
3149 it->saved_face_id = it->face_id;
3150
3151 do
3152 {
3153 handled = HANDLED_NORMALLY;
3154
3155 /* Call text property handlers. */
3156 for (p = it_props; p->handler; ++p)
3157 {
3158 handled = p->handler (it);
3159
3160 if (handled == HANDLED_RECOMPUTE_PROPS)
3161 break;
3162 else if (handled == HANDLED_RETURN)
3163 {
3164 /* We still want to show before and after strings from
3165 overlays even if the actual buffer text is replaced. */
3166 if (!handle_overlay_change_p
3167 || it->sp > 1
3168 /* Don't call get_overlay_strings_1 if we already
3169 have overlay strings loaded, because doing so
3170 will load them again and push the iterator state
3171 onto the stack one more time, which is not
3172 expected by the rest of the code that processes
3173 overlay strings. */
3174 || (it->current.overlay_string_index < 0
3175 ? !get_overlay_strings_1 (it, 0, 0)
3176 : 0))
3177 {
3178 if (it->ellipsis_p)
3179 setup_for_ellipsis (it, 0);
3180 /* When handling a display spec, we might load an
3181 empty string. In that case, discard it here. We
3182 used to discard it in handle_single_display_spec,
3183 but that causes get_overlay_strings_1, above, to
3184 ignore overlay strings that we must check. */
3185 if (STRINGP (it->string) && !SCHARS (it->string))
3186 pop_it (it);
3187 return;
3188 }
3189 else if (STRINGP (it->string) && !SCHARS (it->string))
3190 pop_it (it);
3191 else
3192 {
3193 it->ignore_overlay_strings_at_pos_p = 1;
3194 it->string_from_display_prop_p = 0;
3195 it->from_disp_prop_p = 0;
3196 handle_overlay_change_p = 0;
3197 }
3198 handled = HANDLED_RECOMPUTE_PROPS;
3199 break;
3200 }
3201 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3202 handle_overlay_change_p = 0;
3203 }
3204
3205 if (handled != HANDLED_RECOMPUTE_PROPS)
3206 {
3207 /* Don't check for overlay strings below when set to deliver
3208 characters from a display vector. */
3209 if (it->method == GET_FROM_DISPLAY_VECTOR)
3210 handle_overlay_change_p = 0;
3211
3212 /* Handle overlay changes.
3213 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3214 if it finds overlays. */
3215 if (handle_overlay_change_p)
3216 handled = handle_overlay_change (it);
3217 }
3218
3219 if (it->ellipsis_p)
3220 {
3221 setup_for_ellipsis (it, 0);
3222 break;
3223 }
3224 }
3225 while (handled == HANDLED_RECOMPUTE_PROPS);
3226
3227 /* Determine where to stop next. */
3228 if (handled == HANDLED_NORMALLY)
3229 compute_stop_pos (it);
3230 }
3231
3232
3233 /* Compute IT->stop_charpos from text property and overlay change
3234 information for IT's current position. */
3235
3236 static void
3237 compute_stop_pos (struct it *it)
3238 {
3239 register INTERVAL iv, next_iv;
3240 Lisp_Object object, limit, position;
3241 ptrdiff_t charpos, bytepos;
3242
3243 if (STRINGP (it->string))
3244 {
3245 /* Strings are usually short, so don't limit the search for
3246 properties. */
3247 it->stop_charpos = it->end_charpos;
3248 object = it->string;
3249 limit = Qnil;
3250 charpos = IT_STRING_CHARPOS (*it);
3251 bytepos = IT_STRING_BYTEPOS (*it);
3252 }
3253 else
3254 {
3255 ptrdiff_t pos;
3256
3257 /* If end_charpos is out of range for some reason, such as a
3258 misbehaving display function, rationalize it (Bug#5984). */
3259 if (it->end_charpos > ZV)
3260 it->end_charpos = ZV;
3261 it->stop_charpos = it->end_charpos;
3262
3263 /* If next overlay change is in front of the current stop pos
3264 (which is IT->end_charpos), stop there. Note: value of
3265 next_overlay_change is point-max if no overlay change
3266 follows. */
3267 charpos = IT_CHARPOS (*it);
3268 bytepos = IT_BYTEPOS (*it);
3269 pos = next_overlay_change (charpos);
3270 if (pos < it->stop_charpos)
3271 it->stop_charpos = pos;
3272
3273 /* If showing the region, we have to stop at the region
3274 start or end because the face might change there. */
3275 if (it->region_beg_charpos > 0)
3276 {
3277 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3278 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3279 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3280 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3281 }
3282
3283 /* Set up variables for computing the stop position from text
3284 property changes. */
3285 XSETBUFFER (object, current_buffer);
3286 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3287 }
3288
3289 /* Get the interval containing IT's position. Value is a null
3290 interval if there isn't such an interval. */
3291 position = make_number (charpos);
3292 iv = validate_interval_range (object, &position, &position, 0);
3293 if (!NULL_INTERVAL_P (iv))
3294 {
3295 Lisp_Object values_here[LAST_PROP_IDX];
3296 struct props *p;
3297
3298 /* Get properties here. */
3299 for (p = it_props; p->handler; ++p)
3300 values_here[p->idx] = textget (iv->plist, *p->name);
3301
3302 /* Look for an interval following iv that has different
3303 properties. */
3304 for (next_iv = next_interval (iv);
3305 (!NULL_INTERVAL_P (next_iv)
3306 && (NILP (limit)
3307 || XFASTINT (limit) > next_iv->position));
3308 next_iv = next_interval (next_iv))
3309 {
3310 for (p = it_props; p->handler; ++p)
3311 {
3312 Lisp_Object new_value;
3313
3314 new_value = textget (next_iv->plist, *p->name);
3315 if (!EQ (values_here[p->idx], new_value))
3316 break;
3317 }
3318
3319 if (p->handler)
3320 break;
3321 }
3322
3323 if (!NULL_INTERVAL_P (next_iv))
3324 {
3325 if (INTEGERP (limit)
3326 && next_iv->position >= XFASTINT (limit))
3327 /* No text property change up to limit. */
3328 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3329 else
3330 /* Text properties change in next_iv. */
3331 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3332 }
3333 }
3334
3335 if (it->cmp_it.id < 0)
3336 {
3337 ptrdiff_t stoppos = it->end_charpos;
3338
3339 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3340 stoppos = -1;
3341 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3342 stoppos, it->string);
3343 }
3344
3345 eassert (STRINGP (it->string)
3346 || (it->stop_charpos >= BEGV
3347 && it->stop_charpos >= IT_CHARPOS (*it)));
3348 }
3349
3350
3351 /* Return the position of the next overlay change after POS in
3352 current_buffer. Value is point-max if no overlay change
3353 follows. This is like `next-overlay-change' but doesn't use
3354 xmalloc. */
3355
3356 static ptrdiff_t
3357 next_overlay_change (ptrdiff_t pos)
3358 {
3359 ptrdiff_t i, noverlays;
3360 ptrdiff_t endpos;
3361 Lisp_Object *overlays;
3362
3363 /* Get all overlays at the given position. */
3364 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3365
3366 /* If any of these overlays ends before endpos,
3367 use its ending point instead. */
3368 for (i = 0; i < noverlays; ++i)
3369 {
3370 Lisp_Object oend;
3371 ptrdiff_t oendpos;
3372
3373 oend = OVERLAY_END (overlays[i]);
3374 oendpos = OVERLAY_POSITION (oend);
3375 endpos = min (endpos, oendpos);
3376 }
3377
3378 return endpos;
3379 }
3380
3381 /* How many characters forward to search for a display property or
3382 display string. Searching too far forward makes the bidi display
3383 sluggish, especially in small windows. */
3384 #define MAX_DISP_SCAN 250
3385
3386 /* Return the character position of a display string at or after
3387 position specified by POSITION. If no display string exists at or
3388 after POSITION, return ZV. A display string is either an overlay
3389 with `display' property whose value is a string, or a `display'
3390 text property whose value is a string. STRING is data about the
3391 string to iterate; if STRING->lstring is nil, we are iterating a
3392 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3393 on a GUI frame. DISP_PROP is set to zero if we searched
3394 MAX_DISP_SCAN characters forward without finding any display
3395 strings, non-zero otherwise. It is set to 2 if the display string
3396 uses any kind of `(space ...)' spec that will produce a stretch of
3397 white space in the text area. */
3398 ptrdiff_t
3399 compute_display_string_pos (struct text_pos *position,
3400 struct bidi_string_data *string,
3401 int frame_window_p, int *disp_prop)
3402 {
3403 /* OBJECT = nil means current buffer. */
3404 Lisp_Object object =
3405 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3406 Lisp_Object pos, spec, limpos;
3407 int string_p = (string && (STRINGP (string->lstring) || string->s));
3408 ptrdiff_t eob = string_p ? string->schars : ZV;
3409 ptrdiff_t begb = string_p ? 0 : BEGV;
3410 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3411 ptrdiff_t lim =
3412 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3413 struct text_pos tpos;
3414 int rv = 0;
3415
3416 *disp_prop = 1;
3417
3418 if (charpos >= eob
3419 /* We don't support display properties whose values are strings
3420 that have display string properties. */
3421 || string->from_disp_str
3422 /* C strings cannot have display properties. */
3423 || (string->s && !STRINGP (object)))
3424 {
3425 *disp_prop = 0;
3426 return eob;
3427 }
3428
3429 /* If the character at CHARPOS is where the display string begins,
3430 return CHARPOS. */
3431 pos = make_number (charpos);
3432 if (STRINGP (object))
3433 bufpos = string->bufpos;
3434 else
3435 bufpos = charpos;
3436 tpos = *position;
3437 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3438 && (charpos <= begb
3439 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3440 object),
3441 spec))
3442 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3443 frame_window_p)))
3444 {
3445 if (rv == 2)
3446 *disp_prop = 2;
3447 return charpos;
3448 }
3449
3450 /* Look forward for the first character with a `display' property
3451 that will replace the underlying text when displayed. */
3452 limpos = make_number (lim);
3453 do {
3454 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3455 CHARPOS (tpos) = XFASTINT (pos);
3456 if (CHARPOS (tpos) >= lim)
3457 {
3458 *disp_prop = 0;
3459 break;
3460 }
3461 if (STRINGP (object))
3462 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3463 else
3464 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3465 spec = Fget_char_property (pos, Qdisplay, object);
3466 if (!STRINGP (object))
3467 bufpos = CHARPOS (tpos);
3468 } while (NILP (spec)
3469 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3470 bufpos, frame_window_p)));
3471 if (rv == 2)
3472 *disp_prop = 2;
3473
3474 return CHARPOS (tpos);
3475 }
3476
3477 /* Return the character position of the end of the display string that
3478 started at CHARPOS. If there's no display string at CHARPOS,
3479 return -1. A display string is either an overlay with `display'
3480 property whose value is a string or a `display' text property whose
3481 value is a string. */
3482 ptrdiff_t
3483 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3484 {
3485 /* OBJECT = nil means current buffer. */
3486 Lisp_Object object =
3487 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3488 Lisp_Object pos = make_number (charpos);
3489 ptrdiff_t eob =
3490 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3491
3492 if (charpos >= eob || (string->s && !STRINGP (object)))
3493 return eob;
3494
3495 /* It could happen that the display property or overlay was removed
3496 since we found it in compute_display_string_pos above. One way
3497 this can happen is if JIT font-lock was called (through
3498 handle_fontified_prop), and jit-lock-functions remove text
3499 properties or overlays from the portion of buffer that includes
3500 CHARPOS. Muse mode is known to do that, for example. In this
3501 case, we return -1 to the caller, to signal that no display
3502 string is actually present at CHARPOS. See bidi_fetch_char for
3503 how this is handled.
3504
3505 An alternative would be to never look for display properties past
3506 it->stop_charpos. But neither compute_display_string_pos nor
3507 bidi_fetch_char that calls it know or care where the next
3508 stop_charpos is. */
3509 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3510 return -1;
3511
3512 /* Look forward for the first character where the `display' property
3513 changes. */
3514 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3515
3516 return XFASTINT (pos);
3517 }
3518
3519
3520 \f
3521 /***********************************************************************
3522 Fontification
3523 ***********************************************************************/
3524
3525 /* Handle changes in the `fontified' property of the current buffer by
3526 calling hook functions from Qfontification_functions to fontify
3527 regions of text. */
3528
3529 static enum prop_handled
3530 handle_fontified_prop (struct it *it)
3531 {
3532 Lisp_Object prop, pos;
3533 enum prop_handled handled = HANDLED_NORMALLY;
3534
3535 if (!NILP (Vmemory_full))
3536 return handled;
3537
3538 /* Get the value of the `fontified' property at IT's current buffer
3539 position. (The `fontified' property doesn't have a special
3540 meaning in strings.) If the value is nil, call functions from
3541 Qfontification_functions. */
3542 if (!STRINGP (it->string)
3543 && it->s == NULL
3544 && !NILP (Vfontification_functions)
3545 && !NILP (Vrun_hooks)
3546 && (pos = make_number (IT_CHARPOS (*it)),
3547 prop = Fget_char_property (pos, Qfontified, Qnil),
3548 /* Ignore the special cased nil value always present at EOB since
3549 no amount of fontifying will be able to change it. */
3550 NILP (prop) && IT_CHARPOS (*it) < Z))
3551 {
3552 ptrdiff_t count = SPECPDL_INDEX ();
3553 Lisp_Object val;
3554 struct buffer *obuf = current_buffer;
3555 int begv = BEGV, zv = ZV;
3556 int old_clip_changed = current_buffer->clip_changed;
3557
3558 val = Vfontification_functions;
3559 specbind (Qfontification_functions, Qnil);
3560
3561 eassert (it->end_charpos == ZV);
3562
3563 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3564 safe_call1 (val, pos);
3565 else
3566 {
3567 Lisp_Object fns, fn;
3568 struct gcpro gcpro1, gcpro2;
3569
3570 fns = Qnil;
3571 GCPRO2 (val, fns);
3572
3573 for (; CONSP (val); val = XCDR (val))
3574 {
3575 fn = XCAR (val);
3576
3577 if (EQ (fn, Qt))
3578 {
3579 /* A value of t indicates this hook has a local
3580 binding; it means to run the global binding too.
3581 In a global value, t should not occur. If it
3582 does, we must ignore it to avoid an endless
3583 loop. */
3584 for (fns = Fdefault_value (Qfontification_functions);
3585 CONSP (fns);
3586 fns = XCDR (fns))
3587 {
3588 fn = XCAR (fns);
3589 if (!EQ (fn, Qt))
3590 safe_call1 (fn, pos);
3591 }
3592 }
3593 else
3594 safe_call1 (fn, pos);
3595 }
3596
3597 UNGCPRO;
3598 }
3599
3600 unbind_to (count, Qnil);
3601
3602 /* Fontification functions routinely call `save-restriction'.
3603 Normally, this tags clip_changed, which can confuse redisplay
3604 (see discussion in Bug#6671). Since we don't perform any
3605 special handling of fontification changes in the case where
3606 `save-restriction' isn't called, there's no point doing so in
3607 this case either. So, if the buffer's restrictions are
3608 actually left unchanged, reset clip_changed. */
3609 if (obuf == current_buffer)
3610 {
3611 if (begv == BEGV && zv == ZV)
3612 current_buffer->clip_changed = old_clip_changed;
3613 }
3614 /* There isn't much we can reasonably do to protect against
3615 misbehaving fontification, but here's a fig leaf. */
3616 else if (!NILP (BVAR (obuf, name)))
3617 set_buffer_internal_1 (obuf);
3618
3619 /* The fontification code may have added/removed text.
3620 It could do even a lot worse, but let's at least protect against
3621 the most obvious case where only the text past `pos' gets changed',
3622 as is/was done in grep.el where some escapes sequences are turned
3623 into face properties (bug#7876). */
3624 it->end_charpos = ZV;
3625
3626 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3627 something. This avoids an endless loop if they failed to
3628 fontify the text for which reason ever. */
3629 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3630 handled = HANDLED_RECOMPUTE_PROPS;
3631 }
3632
3633 return handled;
3634 }
3635
3636
3637 \f
3638 /***********************************************************************
3639 Faces
3640 ***********************************************************************/
3641
3642 /* Set up iterator IT from face properties at its current position.
3643 Called from handle_stop. */
3644
3645 static enum prop_handled
3646 handle_face_prop (struct it *it)
3647 {
3648 int new_face_id;
3649 ptrdiff_t next_stop;
3650
3651 if (!STRINGP (it->string))
3652 {
3653 new_face_id
3654 = face_at_buffer_position (it->w,
3655 IT_CHARPOS (*it),
3656 it->region_beg_charpos,
3657 it->region_end_charpos,
3658 &next_stop,
3659 (IT_CHARPOS (*it)
3660 + TEXT_PROP_DISTANCE_LIMIT),
3661 0, it->base_face_id);
3662
3663 /* Is this a start of a run of characters with box face?
3664 Caveat: this can be called for a freshly initialized
3665 iterator; face_id is -1 in this case. We know that the new
3666 face will not change until limit, i.e. if the new face has a
3667 box, all characters up to limit will have one. But, as
3668 usual, we don't know whether limit is really the end. */
3669 if (new_face_id != it->face_id)
3670 {
3671 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3672
3673 /* If new face has a box but old face has not, this is
3674 the start of a run of characters with box, i.e. it has
3675 a shadow on the left side. The value of face_id of the
3676 iterator will be -1 if this is the initial call that gets
3677 the face. In this case, we have to look in front of IT's
3678 position and see whether there is a face != new_face_id. */
3679 it->start_of_box_run_p
3680 = (new_face->box != FACE_NO_BOX
3681 && (it->face_id >= 0
3682 || IT_CHARPOS (*it) == BEG
3683 || new_face_id != face_before_it_pos (it)));
3684 it->face_box_p = new_face->box != FACE_NO_BOX;
3685 }
3686 }
3687 else
3688 {
3689 int base_face_id;
3690 ptrdiff_t bufpos;
3691 int i;
3692 Lisp_Object from_overlay
3693 = (it->current.overlay_string_index >= 0
3694 ? it->string_overlays[it->current.overlay_string_index
3695 % OVERLAY_STRING_CHUNK_SIZE]
3696 : Qnil);
3697
3698 /* See if we got to this string directly or indirectly from
3699 an overlay property. That includes the before-string or
3700 after-string of an overlay, strings in display properties
3701 provided by an overlay, their text properties, etc.
3702
3703 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3704 if (! NILP (from_overlay))
3705 for (i = it->sp - 1; i >= 0; i--)
3706 {
3707 if (it->stack[i].current.overlay_string_index >= 0)
3708 from_overlay
3709 = it->string_overlays[it->stack[i].current.overlay_string_index
3710 % OVERLAY_STRING_CHUNK_SIZE];
3711 else if (! NILP (it->stack[i].from_overlay))
3712 from_overlay = it->stack[i].from_overlay;
3713
3714 if (!NILP (from_overlay))
3715 break;
3716 }
3717
3718 if (! NILP (from_overlay))
3719 {
3720 bufpos = IT_CHARPOS (*it);
3721 /* For a string from an overlay, the base face depends
3722 only on text properties and ignores overlays. */
3723 base_face_id
3724 = face_for_overlay_string (it->w,
3725 IT_CHARPOS (*it),
3726 it->region_beg_charpos,
3727 it->region_end_charpos,
3728 &next_stop,
3729 (IT_CHARPOS (*it)
3730 + TEXT_PROP_DISTANCE_LIMIT),
3731 0,
3732 from_overlay);
3733 }
3734 else
3735 {
3736 bufpos = 0;
3737
3738 /* For strings from a `display' property, use the face at
3739 IT's current buffer position as the base face to merge
3740 with, so that overlay strings appear in the same face as
3741 surrounding text, unless they specify their own
3742 faces. */
3743 base_face_id = it->string_from_prefix_prop_p
3744 ? DEFAULT_FACE_ID
3745 : underlying_face_id (it);
3746 }
3747
3748 new_face_id = face_at_string_position (it->w,
3749 it->string,
3750 IT_STRING_CHARPOS (*it),
3751 bufpos,
3752 it->region_beg_charpos,
3753 it->region_end_charpos,
3754 &next_stop,
3755 base_face_id, 0);
3756
3757 /* Is this a start of a run of characters with box? Caveat:
3758 this can be called for a freshly allocated iterator; face_id
3759 is -1 is this case. We know that the new face will not
3760 change until the next check pos, i.e. if the new face has a
3761 box, all characters up to that position will have a
3762 box. But, as usual, we don't know whether that position
3763 is really the end. */
3764 if (new_face_id != it->face_id)
3765 {
3766 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3767 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3768
3769 /* If new face has a box but old face hasn't, this is the
3770 start of a run of characters with box, i.e. it has a
3771 shadow on the left side. */
3772 it->start_of_box_run_p
3773 = new_face->box && (old_face == NULL || !old_face->box);
3774 it->face_box_p = new_face->box != FACE_NO_BOX;
3775 }
3776 }
3777
3778 it->face_id = new_face_id;
3779 return HANDLED_NORMALLY;
3780 }
3781
3782
3783 /* Return the ID of the face ``underlying'' IT's current position,
3784 which is in a string. If the iterator is associated with a
3785 buffer, return the face at IT's current buffer position.
3786 Otherwise, use the iterator's base_face_id. */
3787
3788 static int
3789 underlying_face_id (struct it *it)
3790 {
3791 int face_id = it->base_face_id, i;
3792
3793 eassert (STRINGP (it->string));
3794
3795 for (i = it->sp - 1; i >= 0; --i)
3796 if (NILP (it->stack[i].string))
3797 face_id = it->stack[i].face_id;
3798
3799 return face_id;
3800 }
3801
3802
3803 /* Compute the face one character before or after the current position
3804 of IT, in the visual order. BEFORE_P non-zero means get the face
3805 in front (to the left in L2R paragraphs, to the right in R2L
3806 paragraphs) of IT's screen position. Value is the ID of the face. */
3807
3808 static int
3809 face_before_or_after_it_pos (struct it *it, int before_p)
3810 {
3811 int face_id, limit;
3812 ptrdiff_t next_check_charpos;
3813 struct it it_copy;
3814 void *it_copy_data = NULL;
3815
3816 eassert (it->s == NULL);
3817
3818 if (STRINGP (it->string))
3819 {
3820 ptrdiff_t bufpos, charpos;
3821 int base_face_id;
3822
3823 /* No face change past the end of the string (for the case
3824 we are padding with spaces). No face change before the
3825 string start. */
3826 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3827 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3828 return it->face_id;
3829
3830 if (!it->bidi_p)
3831 {
3832 /* Set charpos to the position before or after IT's current
3833 position, in the logical order, which in the non-bidi
3834 case is the same as the visual order. */
3835 if (before_p)
3836 charpos = IT_STRING_CHARPOS (*it) - 1;
3837 else if (it->what == IT_COMPOSITION)
3838 /* For composition, we must check the character after the
3839 composition. */
3840 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3841 else
3842 charpos = IT_STRING_CHARPOS (*it) + 1;
3843 }
3844 else
3845 {
3846 if (before_p)
3847 {
3848 /* With bidi iteration, the character before the current
3849 in the visual order cannot be found by simple
3850 iteration, because "reverse" reordering is not
3851 supported. Instead, we need to use the move_it_*
3852 family of functions. */
3853 /* Ignore face changes before the first visible
3854 character on this display line. */
3855 if (it->current_x <= it->first_visible_x)
3856 return it->face_id;
3857 SAVE_IT (it_copy, *it, it_copy_data);
3858 /* Implementation note: Since move_it_in_display_line
3859 works in the iterator geometry, and thinks the first
3860 character is always the leftmost, even in R2L lines,
3861 we don't need to distinguish between the R2L and L2R
3862 cases here. */
3863 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3864 it_copy.current_x - 1, MOVE_TO_X);
3865 charpos = IT_STRING_CHARPOS (it_copy);
3866 RESTORE_IT (it, it, it_copy_data);
3867 }
3868 else
3869 {
3870 /* Set charpos to the string position of the character
3871 that comes after IT's current position in the visual
3872 order. */
3873 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3874
3875 it_copy = *it;
3876 while (n--)
3877 bidi_move_to_visually_next (&it_copy.bidi_it);
3878
3879 charpos = it_copy.bidi_it.charpos;
3880 }
3881 }
3882 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3883
3884 if (it->current.overlay_string_index >= 0)
3885 bufpos = IT_CHARPOS (*it);
3886 else
3887 bufpos = 0;
3888
3889 base_face_id = underlying_face_id (it);
3890
3891 /* Get the face for ASCII, or unibyte. */
3892 face_id = face_at_string_position (it->w,
3893 it->string,
3894 charpos,
3895 bufpos,
3896 it->region_beg_charpos,
3897 it->region_end_charpos,
3898 &next_check_charpos,
3899 base_face_id, 0);
3900
3901 /* Correct the face for charsets different from ASCII. Do it
3902 for the multibyte case only. The face returned above is
3903 suitable for unibyte text if IT->string is unibyte. */
3904 if (STRING_MULTIBYTE (it->string))
3905 {
3906 struct text_pos pos1 = string_pos (charpos, it->string);
3907 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3908 int c, len;
3909 struct face *face = FACE_FROM_ID (it->f, face_id);
3910
3911 c = string_char_and_length (p, &len);
3912 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3913 }
3914 }
3915 else
3916 {
3917 struct text_pos pos;
3918
3919 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3920 || (IT_CHARPOS (*it) <= BEGV && before_p))
3921 return it->face_id;
3922
3923 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3924 pos = it->current.pos;
3925
3926 if (!it->bidi_p)
3927 {
3928 if (before_p)
3929 DEC_TEXT_POS (pos, it->multibyte_p);
3930 else
3931 {
3932 if (it->what == IT_COMPOSITION)
3933 {
3934 /* For composition, we must check the position after
3935 the composition. */
3936 pos.charpos += it->cmp_it.nchars;
3937 pos.bytepos += it->len;
3938 }
3939 else
3940 INC_TEXT_POS (pos, it->multibyte_p);
3941 }
3942 }
3943 else
3944 {
3945 if (before_p)
3946 {
3947 /* With bidi iteration, the character before the current
3948 in the visual order cannot be found by simple
3949 iteration, because "reverse" reordering is not
3950 supported. Instead, we need to use the move_it_*
3951 family of functions. */
3952 /* Ignore face changes before the first visible
3953 character on this display line. */
3954 if (it->current_x <= it->first_visible_x)
3955 return it->face_id;
3956 SAVE_IT (it_copy, *it, it_copy_data);
3957 /* Implementation note: Since move_it_in_display_line
3958 works in the iterator geometry, and thinks the first
3959 character is always the leftmost, even in R2L lines,
3960 we don't need to distinguish between the R2L and L2R
3961 cases here. */
3962 move_it_in_display_line (&it_copy, ZV,
3963 it_copy.current_x - 1, MOVE_TO_X);
3964 pos = it_copy.current.pos;
3965 RESTORE_IT (it, it, it_copy_data);
3966 }
3967 else
3968 {
3969 /* Set charpos to the buffer position of the character
3970 that comes after IT's current position in the visual
3971 order. */
3972 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3973
3974 it_copy = *it;
3975 while (n--)
3976 bidi_move_to_visually_next (&it_copy.bidi_it);
3977
3978 SET_TEXT_POS (pos,
3979 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3980 }
3981 }
3982 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3983
3984 /* Determine face for CHARSET_ASCII, or unibyte. */
3985 face_id = face_at_buffer_position (it->w,
3986 CHARPOS (pos),
3987 it->region_beg_charpos,
3988 it->region_end_charpos,
3989 &next_check_charpos,
3990 limit, 0, -1);
3991
3992 /* Correct the face for charsets different from ASCII. Do it
3993 for the multibyte case only. The face returned above is
3994 suitable for unibyte text if current_buffer is unibyte. */
3995 if (it->multibyte_p)
3996 {
3997 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3998 struct face *face = FACE_FROM_ID (it->f, face_id);
3999 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4000 }
4001 }
4002
4003 return face_id;
4004 }
4005
4006
4007 \f
4008 /***********************************************************************
4009 Invisible text
4010 ***********************************************************************/
4011
4012 /* Set up iterator IT from invisible properties at its current
4013 position. Called from handle_stop. */
4014
4015 static enum prop_handled
4016 handle_invisible_prop (struct it *it)
4017 {
4018 enum prop_handled handled = HANDLED_NORMALLY;
4019
4020 if (STRINGP (it->string))
4021 {
4022 Lisp_Object prop, end_charpos, limit, charpos;
4023
4024 /* Get the value of the invisible text property at the
4025 current position. Value will be nil if there is no such
4026 property. */
4027 charpos = make_number (IT_STRING_CHARPOS (*it));
4028 prop = Fget_text_property (charpos, Qinvisible, it->string);
4029
4030 if (!NILP (prop)
4031 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4032 {
4033 ptrdiff_t endpos;
4034
4035 handled = HANDLED_RECOMPUTE_PROPS;
4036
4037 /* Get the position at which the next change of the
4038 invisible text property can be found in IT->string.
4039 Value will be nil if the property value is the same for
4040 all the rest of IT->string. */
4041 XSETINT (limit, SCHARS (it->string));
4042 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4043 it->string, limit);
4044
4045 /* Text at current position is invisible. The next
4046 change in the property is at position end_charpos.
4047 Move IT's current position to that position. */
4048 if (INTEGERP (end_charpos)
4049 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4050 {
4051 struct text_pos old;
4052 ptrdiff_t oldpos;
4053
4054 old = it->current.string_pos;
4055 oldpos = CHARPOS (old);
4056 if (it->bidi_p)
4057 {
4058 if (it->bidi_it.first_elt
4059 && it->bidi_it.charpos < SCHARS (it->string))
4060 bidi_paragraph_init (it->paragraph_embedding,
4061 &it->bidi_it, 1);
4062 /* Bidi-iterate out of the invisible text. */
4063 do
4064 {
4065 bidi_move_to_visually_next (&it->bidi_it);
4066 }
4067 while (oldpos <= it->bidi_it.charpos
4068 && it->bidi_it.charpos < endpos);
4069
4070 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4071 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4072 if (IT_CHARPOS (*it) >= endpos)
4073 it->prev_stop = endpos;
4074 }
4075 else
4076 {
4077 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4078 compute_string_pos (&it->current.string_pos, old, it->string);
4079 }
4080 }
4081 else
4082 {
4083 /* The rest of the string is invisible. If this is an
4084 overlay string, proceed with the next overlay string
4085 or whatever comes and return a character from there. */
4086 if (it->current.overlay_string_index >= 0)
4087 {
4088 next_overlay_string (it);
4089 /* Don't check for overlay strings when we just
4090 finished processing them. */
4091 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4092 }
4093 else
4094 {
4095 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4096 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4097 }
4098 }
4099 }
4100 }
4101 else
4102 {
4103 int invis_p;
4104 ptrdiff_t newpos, next_stop, start_charpos, tem;
4105 Lisp_Object pos, prop, overlay;
4106
4107 /* First of all, is there invisible text at this position? */
4108 tem = start_charpos = IT_CHARPOS (*it);
4109 pos = make_number (tem);
4110 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4111 &overlay);
4112 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4113
4114 /* If we are on invisible text, skip over it. */
4115 if (invis_p && start_charpos < it->end_charpos)
4116 {
4117 /* Record whether we have to display an ellipsis for the
4118 invisible text. */
4119 int display_ellipsis_p = invis_p == 2;
4120
4121 handled = HANDLED_RECOMPUTE_PROPS;
4122
4123 /* Loop skipping over invisible text. The loop is left at
4124 ZV or with IT on the first char being visible again. */
4125 do
4126 {
4127 /* Try to skip some invisible text. Return value is the
4128 position reached which can be equal to where we start
4129 if there is nothing invisible there. This skips both
4130 over invisible text properties and overlays with
4131 invisible property. */
4132 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4133
4134 /* If we skipped nothing at all we weren't at invisible
4135 text in the first place. If everything to the end of
4136 the buffer was skipped, end the loop. */
4137 if (newpos == tem || newpos >= ZV)
4138 invis_p = 0;
4139 else
4140 {
4141 /* We skipped some characters but not necessarily
4142 all there are. Check if we ended up on visible
4143 text. Fget_char_property returns the property of
4144 the char before the given position, i.e. if we
4145 get invis_p = 0, this means that the char at
4146 newpos is visible. */
4147 pos = make_number (newpos);
4148 prop = Fget_char_property (pos, Qinvisible, it->window);
4149 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4150 }
4151
4152 /* If we ended up on invisible text, proceed to
4153 skip starting with next_stop. */
4154 if (invis_p)
4155 tem = next_stop;
4156
4157 /* If there are adjacent invisible texts, don't lose the
4158 second one's ellipsis. */
4159 if (invis_p == 2)
4160 display_ellipsis_p = 1;
4161 }
4162 while (invis_p);
4163
4164 /* The position newpos is now either ZV or on visible text. */
4165 if (it->bidi_p)
4166 {
4167 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4168 int on_newline =
4169 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4170 int after_newline =
4171 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4172
4173 /* If the invisible text ends on a newline or on a
4174 character after a newline, we can avoid the costly,
4175 character by character, bidi iteration to NEWPOS, and
4176 instead simply reseat the iterator there. That's
4177 because all bidi reordering information is tossed at
4178 the newline. This is a big win for modes that hide
4179 complete lines, like Outline, Org, etc. */
4180 if (on_newline || after_newline)
4181 {
4182 struct text_pos tpos;
4183 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4184
4185 SET_TEXT_POS (tpos, newpos, bpos);
4186 reseat_1 (it, tpos, 0);
4187 /* If we reseat on a newline/ZV, we need to prep the
4188 bidi iterator for advancing to the next character
4189 after the newline/EOB, keeping the current paragraph
4190 direction (so that PRODUCE_GLYPHS does TRT wrt
4191 prepending/appending glyphs to a glyph row). */
4192 if (on_newline)
4193 {
4194 it->bidi_it.first_elt = 0;
4195 it->bidi_it.paragraph_dir = pdir;
4196 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4197 it->bidi_it.nchars = 1;
4198 it->bidi_it.ch_len = 1;
4199 }
4200 }
4201 else /* Must use the slow method. */
4202 {
4203 /* With bidi iteration, the region of invisible text
4204 could start and/or end in the middle of a
4205 non-base embedding level. Therefore, we need to
4206 skip invisible text using the bidi iterator,
4207 starting at IT's current position, until we find
4208 ourselves outside of the invisible text.
4209 Skipping invisible text _after_ bidi iteration
4210 avoids affecting the visual order of the
4211 displayed text when invisible properties are
4212 added or removed. */
4213 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4214 {
4215 /* If we were `reseat'ed to a new paragraph,
4216 determine the paragraph base direction. We
4217 need to do it now because
4218 next_element_from_buffer may not have a
4219 chance to do it, if we are going to skip any
4220 text at the beginning, which resets the
4221 FIRST_ELT flag. */
4222 bidi_paragraph_init (it->paragraph_embedding,
4223 &it->bidi_it, 1);
4224 }
4225 do
4226 {
4227 bidi_move_to_visually_next (&it->bidi_it);
4228 }
4229 while (it->stop_charpos <= it->bidi_it.charpos
4230 && it->bidi_it.charpos < newpos);
4231 IT_CHARPOS (*it) = it->bidi_it.charpos;
4232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4233 /* If we overstepped NEWPOS, record its position in
4234 the iterator, so that we skip invisible text if
4235 later the bidi iteration lands us in the
4236 invisible region again. */
4237 if (IT_CHARPOS (*it) >= newpos)
4238 it->prev_stop = newpos;
4239 }
4240 }
4241 else
4242 {
4243 IT_CHARPOS (*it) = newpos;
4244 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4245 }
4246
4247 /* If there are before-strings at the start of invisible
4248 text, and the text is invisible because of a text
4249 property, arrange to show before-strings because 20.x did
4250 it that way. (If the text is invisible because of an
4251 overlay property instead of a text property, this is
4252 already handled in the overlay code.) */
4253 if (NILP (overlay)
4254 && get_overlay_strings (it, it->stop_charpos))
4255 {
4256 handled = HANDLED_RECOMPUTE_PROPS;
4257 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4258 }
4259 else if (display_ellipsis_p)
4260 {
4261 /* Make sure that the glyphs of the ellipsis will get
4262 correct `charpos' values. If we would not update
4263 it->position here, the glyphs would belong to the
4264 last visible character _before_ the invisible
4265 text, which confuses `set_cursor_from_row'.
4266
4267 We use the last invisible position instead of the
4268 first because this way the cursor is always drawn on
4269 the first "." of the ellipsis, whenever PT is inside
4270 the invisible text. Otherwise the cursor would be
4271 placed _after_ the ellipsis when the point is after the
4272 first invisible character. */
4273 if (!STRINGP (it->object))
4274 {
4275 it->position.charpos = newpos - 1;
4276 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4277 }
4278 it->ellipsis_p = 1;
4279 /* Let the ellipsis display before
4280 considering any properties of the following char.
4281 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4282 handled = HANDLED_RETURN;
4283 }
4284 }
4285 }
4286
4287 return handled;
4288 }
4289
4290
4291 /* Make iterator IT return `...' next.
4292 Replaces LEN characters from buffer. */
4293
4294 static void
4295 setup_for_ellipsis (struct it *it, int len)
4296 {
4297 /* Use the display table definition for `...'. Invalid glyphs
4298 will be handled by the method returning elements from dpvec. */
4299 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4300 {
4301 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4302 it->dpvec = v->contents;
4303 it->dpend = v->contents + v->header.size;
4304 }
4305 else
4306 {
4307 /* Default `...'. */
4308 it->dpvec = default_invis_vector;
4309 it->dpend = default_invis_vector + 3;
4310 }
4311
4312 it->dpvec_char_len = len;
4313 it->current.dpvec_index = 0;
4314 it->dpvec_face_id = -1;
4315
4316 /* Remember the current face id in case glyphs specify faces.
4317 IT's face is restored in set_iterator_to_next.
4318 saved_face_id was set to preceding char's face in handle_stop. */
4319 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4320 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4321
4322 it->method = GET_FROM_DISPLAY_VECTOR;
4323 it->ellipsis_p = 1;
4324 }
4325
4326
4327 \f
4328 /***********************************************************************
4329 'display' property
4330 ***********************************************************************/
4331
4332 /* Set up iterator IT from `display' property at its current position.
4333 Called from handle_stop.
4334 We return HANDLED_RETURN if some part of the display property
4335 overrides the display of the buffer text itself.
4336 Otherwise we return HANDLED_NORMALLY. */
4337
4338 static enum prop_handled
4339 handle_display_prop (struct it *it)
4340 {
4341 Lisp_Object propval, object, overlay;
4342 struct text_pos *position;
4343 ptrdiff_t bufpos;
4344 /* Nonzero if some property replaces the display of the text itself. */
4345 int display_replaced_p = 0;
4346
4347 if (STRINGP (it->string))
4348 {
4349 object = it->string;
4350 position = &it->current.string_pos;
4351 bufpos = CHARPOS (it->current.pos);
4352 }
4353 else
4354 {
4355 XSETWINDOW (object, it->w);
4356 position = &it->current.pos;
4357 bufpos = CHARPOS (*position);
4358 }
4359
4360 /* Reset those iterator values set from display property values. */
4361 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4362 it->space_width = Qnil;
4363 it->font_height = Qnil;
4364 it->voffset = 0;
4365
4366 /* We don't support recursive `display' properties, i.e. string
4367 values that have a string `display' property, that have a string
4368 `display' property etc. */
4369 if (!it->string_from_display_prop_p)
4370 it->area = TEXT_AREA;
4371
4372 propval = get_char_property_and_overlay (make_number (position->charpos),
4373 Qdisplay, object, &overlay);
4374 if (NILP (propval))
4375 return HANDLED_NORMALLY;
4376 /* Now OVERLAY is the overlay that gave us this property, or nil
4377 if it was a text property. */
4378
4379 if (!STRINGP (it->string))
4380 object = it->w->buffer;
4381
4382 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4383 position, bufpos,
4384 FRAME_WINDOW_P (it->f));
4385
4386 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4387 }
4388
4389 /* Subroutine of handle_display_prop. Returns non-zero if the display
4390 specification in SPEC is a replacing specification, i.e. it would
4391 replace the text covered by `display' property with something else,
4392 such as an image or a display string. If SPEC includes any kind or
4393 `(space ...) specification, the value is 2; this is used by
4394 compute_display_string_pos, which see.
4395
4396 See handle_single_display_spec for documentation of arguments.
4397 frame_window_p is non-zero if the window being redisplayed is on a
4398 GUI frame; this argument is used only if IT is NULL, see below.
4399
4400 IT can be NULL, if this is called by the bidi reordering code
4401 through compute_display_string_pos, which see. In that case, this
4402 function only examines SPEC, but does not otherwise "handle" it, in
4403 the sense that it doesn't set up members of IT from the display
4404 spec. */
4405 static int
4406 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4407 Lisp_Object overlay, struct text_pos *position,
4408 ptrdiff_t bufpos, int frame_window_p)
4409 {
4410 int replacing_p = 0;
4411 int rv;
4412
4413 if (CONSP (spec)
4414 /* Simple specifications. */
4415 && !EQ (XCAR (spec), Qimage)
4416 && !EQ (XCAR (spec), Qspace)
4417 && !EQ (XCAR (spec), Qwhen)
4418 && !EQ (XCAR (spec), Qslice)
4419 && !EQ (XCAR (spec), Qspace_width)
4420 && !EQ (XCAR (spec), Qheight)
4421 && !EQ (XCAR (spec), Qraise)
4422 /* Marginal area specifications. */
4423 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4424 && !EQ (XCAR (spec), Qleft_fringe)
4425 && !EQ (XCAR (spec), Qright_fringe)
4426 && !NILP (XCAR (spec)))
4427 {
4428 for (; CONSP (spec); spec = XCDR (spec))
4429 {
4430 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4431 overlay, position, bufpos,
4432 replacing_p, frame_window_p)))
4433 {
4434 replacing_p = rv;
4435 /* If some text in a string is replaced, `position' no
4436 longer points to the position of `object'. */
4437 if (!it || STRINGP (object))
4438 break;
4439 }
4440 }
4441 }
4442 else if (VECTORP (spec))
4443 {
4444 ptrdiff_t i;
4445 for (i = 0; i < ASIZE (spec); ++i)
4446 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4447 overlay, position, bufpos,
4448 replacing_p, frame_window_p)))
4449 {
4450 replacing_p = rv;
4451 /* If some text in a string is replaced, `position' no
4452 longer points to the position of `object'. */
4453 if (!it || STRINGP (object))
4454 break;
4455 }
4456 }
4457 else
4458 {
4459 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4460 position, bufpos, 0,
4461 frame_window_p)))
4462 replacing_p = rv;
4463 }
4464
4465 return replacing_p;
4466 }
4467
4468 /* Value is the position of the end of the `display' property starting
4469 at START_POS in OBJECT. */
4470
4471 static struct text_pos
4472 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4473 {
4474 Lisp_Object end;
4475 struct text_pos end_pos;
4476
4477 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4478 Qdisplay, object, Qnil);
4479 CHARPOS (end_pos) = XFASTINT (end);
4480 if (STRINGP (object))
4481 compute_string_pos (&end_pos, start_pos, it->string);
4482 else
4483 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4484
4485 return end_pos;
4486 }
4487
4488
4489 /* Set up IT from a single `display' property specification SPEC. OBJECT
4490 is the object in which the `display' property was found. *POSITION
4491 is the position in OBJECT at which the `display' property was found.
4492 BUFPOS is the buffer position of OBJECT (different from POSITION if
4493 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4494 previously saw a display specification which already replaced text
4495 display with something else, for example an image; we ignore such
4496 properties after the first one has been processed.
4497
4498 OVERLAY is the overlay this `display' property came from,
4499 or nil if it was a text property.
4500
4501 If SPEC is a `space' or `image' specification, and in some other
4502 cases too, set *POSITION to the position where the `display'
4503 property ends.
4504
4505 If IT is NULL, only examine the property specification in SPEC, but
4506 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4507 is intended to be displayed in a window on a GUI frame.
4508
4509 Value is non-zero if something was found which replaces the display
4510 of buffer or string text. */
4511
4512 static int
4513 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4514 Lisp_Object overlay, struct text_pos *position,
4515 ptrdiff_t bufpos, int display_replaced_p,
4516 int frame_window_p)
4517 {
4518 Lisp_Object form;
4519 Lisp_Object location, value;
4520 struct text_pos start_pos = *position;
4521 int valid_p;
4522
4523 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4524 If the result is non-nil, use VALUE instead of SPEC. */
4525 form = Qt;
4526 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4527 {
4528 spec = XCDR (spec);
4529 if (!CONSP (spec))
4530 return 0;
4531 form = XCAR (spec);
4532 spec = XCDR (spec);
4533 }
4534
4535 if (!NILP (form) && !EQ (form, Qt))
4536 {
4537 ptrdiff_t count = SPECPDL_INDEX ();
4538 struct gcpro gcpro1;
4539
4540 /* Bind `object' to the object having the `display' property, a
4541 buffer or string. Bind `position' to the position in the
4542 object where the property was found, and `buffer-position'
4543 to the current position in the buffer. */
4544
4545 if (NILP (object))
4546 XSETBUFFER (object, current_buffer);
4547 specbind (Qobject, object);
4548 specbind (Qposition, make_number (CHARPOS (*position)));
4549 specbind (Qbuffer_position, make_number (bufpos));
4550 GCPRO1 (form);
4551 form = safe_eval (form);
4552 UNGCPRO;
4553 unbind_to (count, Qnil);
4554 }
4555
4556 if (NILP (form))
4557 return 0;
4558
4559 /* Handle `(height HEIGHT)' specifications. */
4560 if (CONSP (spec)
4561 && EQ (XCAR (spec), Qheight)
4562 && CONSP (XCDR (spec)))
4563 {
4564 if (it)
4565 {
4566 if (!FRAME_WINDOW_P (it->f))
4567 return 0;
4568
4569 it->font_height = XCAR (XCDR (spec));
4570 if (!NILP (it->font_height))
4571 {
4572 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4573 int new_height = -1;
4574
4575 if (CONSP (it->font_height)
4576 && (EQ (XCAR (it->font_height), Qplus)
4577 || EQ (XCAR (it->font_height), Qminus))
4578 && CONSP (XCDR (it->font_height))
4579 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4580 {
4581 /* `(+ N)' or `(- N)' where N is an integer. */
4582 int steps = XINT (XCAR (XCDR (it->font_height)));
4583 if (EQ (XCAR (it->font_height), Qplus))
4584 steps = - steps;
4585 it->face_id = smaller_face (it->f, it->face_id, steps);
4586 }
4587 else if (FUNCTIONP (it->font_height))
4588 {
4589 /* Call function with current height as argument.
4590 Value is the new height. */
4591 Lisp_Object height;
4592 height = safe_call1 (it->font_height,
4593 face->lface[LFACE_HEIGHT_INDEX]);
4594 if (NUMBERP (height))
4595 new_height = XFLOATINT (height);
4596 }
4597 else if (NUMBERP (it->font_height))
4598 {
4599 /* Value is a multiple of the canonical char height. */
4600 struct face *f;
4601
4602 f = FACE_FROM_ID (it->f,
4603 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4604 new_height = (XFLOATINT (it->font_height)
4605 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4606 }
4607 else
4608 {
4609 /* Evaluate IT->font_height with `height' bound to the
4610 current specified height to get the new height. */
4611 ptrdiff_t count = SPECPDL_INDEX ();
4612
4613 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4614 value = safe_eval (it->font_height);
4615 unbind_to (count, Qnil);
4616
4617 if (NUMBERP (value))
4618 new_height = XFLOATINT (value);
4619 }
4620
4621 if (new_height > 0)
4622 it->face_id = face_with_height (it->f, it->face_id, new_height);
4623 }
4624 }
4625
4626 return 0;
4627 }
4628
4629 /* Handle `(space-width WIDTH)'. */
4630 if (CONSP (spec)
4631 && EQ (XCAR (spec), Qspace_width)
4632 && CONSP (XCDR (spec)))
4633 {
4634 if (it)
4635 {
4636 if (!FRAME_WINDOW_P (it->f))
4637 return 0;
4638
4639 value = XCAR (XCDR (spec));
4640 if (NUMBERP (value) && XFLOATINT (value) > 0)
4641 it->space_width = value;
4642 }
4643
4644 return 0;
4645 }
4646
4647 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4648 if (CONSP (spec)
4649 && EQ (XCAR (spec), Qslice))
4650 {
4651 Lisp_Object tem;
4652
4653 if (it)
4654 {
4655 if (!FRAME_WINDOW_P (it->f))
4656 return 0;
4657
4658 if (tem = XCDR (spec), CONSP (tem))
4659 {
4660 it->slice.x = XCAR (tem);
4661 if (tem = XCDR (tem), CONSP (tem))
4662 {
4663 it->slice.y = XCAR (tem);
4664 if (tem = XCDR (tem), CONSP (tem))
4665 {
4666 it->slice.width = XCAR (tem);
4667 if (tem = XCDR (tem), CONSP (tem))
4668 it->slice.height = XCAR (tem);
4669 }
4670 }
4671 }
4672 }
4673
4674 return 0;
4675 }
4676
4677 /* Handle `(raise FACTOR)'. */
4678 if (CONSP (spec)
4679 && EQ (XCAR (spec), Qraise)
4680 && CONSP (XCDR (spec)))
4681 {
4682 if (it)
4683 {
4684 if (!FRAME_WINDOW_P (it->f))
4685 return 0;
4686
4687 #ifdef HAVE_WINDOW_SYSTEM
4688 value = XCAR (XCDR (spec));
4689 if (NUMBERP (value))
4690 {
4691 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4692 it->voffset = - (XFLOATINT (value)
4693 * (FONT_HEIGHT (face->font)));
4694 }
4695 #endif /* HAVE_WINDOW_SYSTEM */
4696 }
4697
4698 return 0;
4699 }
4700
4701 /* Don't handle the other kinds of display specifications
4702 inside a string that we got from a `display' property. */
4703 if (it && it->string_from_display_prop_p)
4704 return 0;
4705
4706 /* Characters having this form of property are not displayed, so
4707 we have to find the end of the property. */
4708 if (it)
4709 {
4710 start_pos = *position;
4711 *position = display_prop_end (it, object, start_pos);
4712 }
4713 value = Qnil;
4714
4715 /* Stop the scan at that end position--we assume that all
4716 text properties change there. */
4717 if (it)
4718 it->stop_charpos = position->charpos;
4719
4720 /* Handle `(left-fringe BITMAP [FACE])'
4721 and `(right-fringe BITMAP [FACE])'. */
4722 if (CONSP (spec)
4723 && (EQ (XCAR (spec), Qleft_fringe)
4724 || EQ (XCAR (spec), Qright_fringe))
4725 && CONSP (XCDR (spec)))
4726 {
4727 int fringe_bitmap;
4728
4729 if (it)
4730 {
4731 if (!FRAME_WINDOW_P (it->f))
4732 /* If we return here, POSITION has been advanced
4733 across the text with this property. */
4734 {
4735 /* Synchronize the bidi iterator with POSITION. This is
4736 needed because we are not going to push the iterator
4737 on behalf of this display property, so there will be
4738 no pop_it call to do this synchronization for us. */
4739 if (it->bidi_p)
4740 {
4741 it->position = *position;
4742 iterate_out_of_display_property (it);
4743 *position = it->position;
4744 }
4745 return 1;
4746 }
4747 }
4748 else if (!frame_window_p)
4749 return 1;
4750
4751 #ifdef HAVE_WINDOW_SYSTEM
4752 value = XCAR (XCDR (spec));
4753 if (!SYMBOLP (value)
4754 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4755 /* If we return here, POSITION has been advanced
4756 across the text with this property. */
4757 {
4758 if (it && it->bidi_p)
4759 {
4760 it->position = *position;
4761 iterate_out_of_display_property (it);
4762 *position = it->position;
4763 }
4764 return 1;
4765 }
4766
4767 if (it)
4768 {
4769 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4770
4771 if (CONSP (XCDR (XCDR (spec))))
4772 {
4773 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4774 int face_id2 = lookup_derived_face (it->f, face_name,
4775 FRINGE_FACE_ID, 0);
4776 if (face_id2 >= 0)
4777 face_id = face_id2;
4778 }
4779
4780 /* Save current settings of IT so that we can restore them
4781 when we are finished with the glyph property value. */
4782 push_it (it, position);
4783
4784 it->area = TEXT_AREA;
4785 it->what = IT_IMAGE;
4786 it->image_id = -1; /* no image */
4787 it->position = start_pos;
4788 it->object = NILP (object) ? it->w->buffer : object;
4789 it->method = GET_FROM_IMAGE;
4790 it->from_overlay = Qnil;
4791 it->face_id = face_id;
4792 it->from_disp_prop_p = 1;
4793
4794 /* Say that we haven't consumed the characters with
4795 `display' property yet. The call to pop_it in
4796 set_iterator_to_next will clean this up. */
4797 *position = start_pos;
4798
4799 if (EQ (XCAR (spec), Qleft_fringe))
4800 {
4801 it->left_user_fringe_bitmap = fringe_bitmap;
4802 it->left_user_fringe_face_id = face_id;
4803 }
4804 else
4805 {
4806 it->right_user_fringe_bitmap = fringe_bitmap;
4807 it->right_user_fringe_face_id = face_id;
4808 }
4809 }
4810 #endif /* HAVE_WINDOW_SYSTEM */
4811 return 1;
4812 }
4813
4814 /* Prepare to handle `((margin left-margin) ...)',
4815 `((margin right-margin) ...)' and `((margin nil) ...)'
4816 prefixes for display specifications. */
4817 location = Qunbound;
4818 if (CONSP (spec) && CONSP (XCAR (spec)))
4819 {
4820 Lisp_Object tem;
4821
4822 value = XCDR (spec);
4823 if (CONSP (value))
4824 value = XCAR (value);
4825
4826 tem = XCAR (spec);
4827 if (EQ (XCAR (tem), Qmargin)
4828 && (tem = XCDR (tem),
4829 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4830 (NILP (tem)
4831 || EQ (tem, Qleft_margin)
4832 || EQ (tem, Qright_margin))))
4833 location = tem;
4834 }
4835
4836 if (EQ (location, Qunbound))
4837 {
4838 location = Qnil;
4839 value = spec;
4840 }
4841
4842 /* After this point, VALUE is the property after any
4843 margin prefix has been stripped. It must be a string,
4844 an image specification, or `(space ...)'.
4845
4846 LOCATION specifies where to display: `left-margin',
4847 `right-margin' or nil. */
4848
4849 valid_p = (STRINGP (value)
4850 #ifdef HAVE_WINDOW_SYSTEM
4851 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4852 && valid_image_p (value))
4853 #endif /* not HAVE_WINDOW_SYSTEM */
4854 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4855
4856 if (valid_p && !display_replaced_p)
4857 {
4858 int retval = 1;
4859
4860 if (!it)
4861 {
4862 /* Callers need to know whether the display spec is any kind
4863 of `(space ...)' spec that is about to affect text-area
4864 display. */
4865 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4866 retval = 2;
4867 return retval;
4868 }
4869
4870 /* Save current settings of IT so that we can restore them
4871 when we are finished with the glyph property value. */
4872 push_it (it, position);
4873 it->from_overlay = overlay;
4874 it->from_disp_prop_p = 1;
4875
4876 if (NILP (location))
4877 it->area = TEXT_AREA;
4878 else if (EQ (location, Qleft_margin))
4879 it->area = LEFT_MARGIN_AREA;
4880 else
4881 it->area = RIGHT_MARGIN_AREA;
4882
4883 if (STRINGP (value))
4884 {
4885 it->string = value;
4886 it->multibyte_p = STRING_MULTIBYTE (it->string);
4887 it->current.overlay_string_index = -1;
4888 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4889 it->end_charpos = it->string_nchars = SCHARS (it->string);
4890 it->method = GET_FROM_STRING;
4891 it->stop_charpos = 0;
4892 it->prev_stop = 0;
4893 it->base_level_stop = 0;
4894 it->string_from_display_prop_p = 1;
4895 /* Say that we haven't consumed the characters with
4896 `display' property yet. The call to pop_it in
4897 set_iterator_to_next will clean this up. */
4898 if (BUFFERP (object))
4899 *position = start_pos;
4900
4901 /* Force paragraph direction to be that of the parent
4902 object. If the parent object's paragraph direction is
4903 not yet determined, default to L2R. */
4904 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4905 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4906 else
4907 it->paragraph_embedding = L2R;
4908
4909 /* Set up the bidi iterator for this display string. */
4910 if (it->bidi_p)
4911 {
4912 it->bidi_it.string.lstring = it->string;
4913 it->bidi_it.string.s = NULL;
4914 it->bidi_it.string.schars = it->end_charpos;
4915 it->bidi_it.string.bufpos = bufpos;
4916 it->bidi_it.string.from_disp_str = 1;
4917 it->bidi_it.string.unibyte = !it->multibyte_p;
4918 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4919 }
4920 }
4921 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4922 {
4923 it->method = GET_FROM_STRETCH;
4924 it->object = value;
4925 *position = it->position = start_pos;
4926 retval = 1 + (it->area == TEXT_AREA);
4927 }
4928 #ifdef HAVE_WINDOW_SYSTEM
4929 else
4930 {
4931 it->what = IT_IMAGE;
4932 it->image_id = lookup_image (it->f, value);
4933 it->position = start_pos;
4934 it->object = NILP (object) ? it->w->buffer : object;
4935 it->method = GET_FROM_IMAGE;
4936
4937 /* Say that we haven't consumed the characters with
4938 `display' property yet. The call to pop_it in
4939 set_iterator_to_next will clean this up. */
4940 *position = start_pos;
4941 }
4942 #endif /* HAVE_WINDOW_SYSTEM */
4943
4944 return retval;
4945 }
4946
4947 /* Invalid property or property not supported. Restore
4948 POSITION to what it was before. */
4949 *position = start_pos;
4950 return 0;
4951 }
4952
4953 /* Check if PROP is a display property value whose text should be
4954 treated as intangible. OVERLAY is the overlay from which PROP
4955 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4956 specify the buffer position covered by PROP. */
4957
4958 int
4959 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4960 ptrdiff_t charpos, ptrdiff_t bytepos)
4961 {
4962 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4963 struct text_pos position;
4964
4965 SET_TEXT_POS (position, charpos, bytepos);
4966 return handle_display_spec (NULL, prop, Qnil, overlay,
4967 &position, charpos, frame_window_p);
4968 }
4969
4970
4971 /* Return 1 if PROP is a display sub-property value containing STRING.
4972
4973 Implementation note: this and the following function are really
4974 special cases of handle_display_spec and
4975 handle_single_display_spec, and should ideally use the same code.
4976 Until they do, these two pairs must be consistent and must be
4977 modified in sync. */
4978
4979 static int
4980 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4981 {
4982 if (EQ (string, prop))
4983 return 1;
4984
4985 /* Skip over `when FORM'. */
4986 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4987 {
4988 prop = XCDR (prop);
4989 if (!CONSP (prop))
4990 return 0;
4991 /* Actually, the condition following `when' should be eval'ed,
4992 like handle_single_display_spec does, and we should return
4993 zero if it evaluates to nil. However, this function is
4994 called only when the buffer was already displayed and some
4995 glyph in the glyph matrix was found to come from a display
4996 string. Therefore, the condition was already evaluated, and
4997 the result was non-nil, otherwise the display string wouldn't
4998 have been displayed and we would have never been called for
4999 this property. Thus, we can skip the evaluation and assume
5000 its result is non-nil. */
5001 prop = XCDR (prop);
5002 }
5003
5004 if (CONSP (prop))
5005 /* Skip over `margin LOCATION'. */
5006 if (EQ (XCAR (prop), Qmargin))
5007 {
5008 prop = XCDR (prop);
5009 if (!CONSP (prop))
5010 return 0;
5011
5012 prop = XCDR (prop);
5013 if (!CONSP (prop))
5014 return 0;
5015 }
5016
5017 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5018 }
5019
5020
5021 /* Return 1 if STRING appears in the `display' property PROP. */
5022
5023 static int
5024 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5025 {
5026 if (CONSP (prop)
5027 && !EQ (XCAR (prop), Qwhen)
5028 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5029 {
5030 /* A list of sub-properties. */
5031 while (CONSP (prop))
5032 {
5033 if (single_display_spec_string_p (XCAR (prop), string))
5034 return 1;
5035 prop = XCDR (prop);
5036 }
5037 }
5038 else if (VECTORP (prop))
5039 {
5040 /* A vector of sub-properties. */
5041 ptrdiff_t i;
5042 for (i = 0; i < ASIZE (prop); ++i)
5043 if (single_display_spec_string_p (AREF (prop, i), string))
5044 return 1;
5045 }
5046 else
5047 return single_display_spec_string_p (prop, string);
5048
5049 return 0;
5050 }
5051
5052 /* Look for STRING in overlays and text properties in the current
5053 buffer, between character positions FROM and TO (excluding TO).
5054 BACK_P non-zero means look back (in this case, TO is supposed to be
5055 less than FROM).
5056 Value is the first character position where STRING was found, or
5057 zero if it wasn't found before hitting TO.
5058
5059 This function may only use code that doesn't eval because it is
5060 called asynchronously from note_mouse_highlight. */
5061
5062 static ptrdiff_t
5063 string_buffer_position_lim (Lisp_Object string,
5064 ptrdiff_t from, ptrdiff_t to, int back_p)
5065 {
5066 Lisp_Object limit, prop, pos;
5067 int found = 0;
5068
5069 pos = make_number (max (from, BEGV));
5070
5071 if (!back_p) /* looking forward */
5072 {
5073 limit = make_number (min (to, ZV));
5074 while (!found && !EQ (pos, limit))
5075 {
5076 prop = Fget_char_property (pos, Qdisplay, Qnil);
5077 if (!NILP (prop) && display_prop_string_p (prop, string))
5078 found = 1;
5079 else
5080 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5081 limit);
5082 }
5083 }
5084 else /* looking back */
5085 {
5086 limit = make_number (max (to, BEGV));
5087 while (!found && !EQ (pos, limit))
5088 {
5089 prop = Fget_char_property (pos, Qdisplay, Qnil);
5090 if (!NILP (prop) && display_prop_string_p (prop, string))
5091 found = 1;
5092 else
5093 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5094 limit);
5095 }
5096 }
5097
5098 return found ? XINT (pos) : 0;
5099 }
5100
5101 /* Determine which buffer position in current buffer STRING comes from.
5102 AROUND_CHARPOS is an approximate position where it could come from.
5103 Value is the buffer position or 0 if it couldn't be determined.
5104
5105 This function is necessary because we don't record buffer positions
5106 in glyphs generated from strings (to keep struct glyph small).
5107 This function may only use code that doesn't eval because it is
5108 called asynchronously from note_mouse_highlight. */
5109
5110 static ptrdiff_t
5111 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5112 {
5113 const int MAX_DISTANCE = 1000;
5114 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5115 around_charpos + MAX_DISTANCE,
5116 0);
5117
5118 if (!found)
5119 found = string_buffer_position_lim (string, around_charpos,
5120 around_charpos - MAX_DISTANCE, 1);
5121 return found;
5122 }
5123
5124
5125 \f
5126 /***********************************************************************
5127 `composition' property
5128 ***********************************************************************/
5129
5130 /* Set up iterator IT from `composition' property at its current
5131 position. Called from handle_stop. */
5132
5133 static enum prop_handled
5134 handle_composition_prop (struct it *it)
5135 {
5136 Lisp_Object prop, string;
5137 ptrdiff_t pos, pos_byte, start, end;
5138
5139 if (STRINGP (it->string))
5140 {
5141 unsigned char *s;
5142
5143 pos = IT_STRING_CHARPOS (*it);
5144 pos_byte = IT_STRING_BYTEPOS (*it);
5145 string = it->string;
5146 s = SDATA (string) + pos_byte;
5147 it->c = STRING_CHAR (s);
5148 }
5149 else
5150 {
5151 pos = IT_CHARPOS (*it);
5152 pos_byte = IT_BYTEPOS (*it);
5153 string = Qnil;
5154 it->c = FETCH_CHAR (pos_byte);
5155 }
5156
5157 /* If there's a valid composition and point is not inside of the
5158 composition (in the case that the composition is from the current
5159 buffer), draw a glyph composed from the composition components. */
5160 if (find_composition (pos, -1, &start, &end, &prop, string)
5161 && COMPOSITION_VALID_P (start, end, prop)
5162 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5163 {
5164 if (start < pos)
5165 /* As we can't handle this situation (perhaps font-lock added
5166 a new composition), we just return here hoping that next
5167 redisplay will detect this composition much earlier. */
5168 return HANDLED_NORMALLY;
5169 if (start != pos)
5170 {
5171 if (STRINGP (it->string))
5172 pos_byte = string_char_to_byte (it->string, start);
5173 else
5174 pos_byte = CHAR_TO_BYTE (start);
5175 }
5176 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5177 prop, string);
5178
5179 if (it->cmp_it.id >= 0)
5180 {
5181 it->cmp_it.ch = -1;
5182 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5183 it->cmp_it.nglyphs = -1;
5184 }
5185 }
5186
5187 return HANDLED_NORMALLY;
5188 }
5189
5190
5191 \f
5192 /***********************************************************************
5193 Overlay strings
5194 ***********************************************************************/
5195
5196 /* The following structure is used to record overlay strings for
5197 later sorting in load_overlay_strings. */
5198
5199 struct overlay_entry
5200 {
5201 Lisp_Object overlay;
5202 Lisp_Object string;
5203 EMACS_INT priority;
5204 int after_string_p;
5205 };
5206
5207
5208 /* Set up iterator IT from overlay strings at its current position.
5209 Called from handle_stop. */
5210
5211 static enum prop_handled
5212 handle_overlay_change (struct it *it)
5213 {
5214 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5215 return HANDLED_RECOMPUTE_PROPS;
5216 else
5217 return HANDLED_NORMALLY;
5218 }
5219
5220
5221 /* Set up the next overlay string for delivery by IT, if there is an
5222 overlay string to deliver. Called by set_iterator_to_next when the
5223 end of the current overlay string is reached. If there are more
5224 overlay strings to display, IT->string and
5225 IT->current.overlay_string_index are set appropriately here.
5226 Otherwise IT->string is set to nil. */
5227
5228 static void
5229 next_overlay_string (struct it *it)
5230 {
5231 ++it->current.overlay_string_index;
5232 if (it->current.overlay_string_index == it->n_overlay_strings)
5233 {
5234 /* No more overlay strings. Restore IT's settings to what
5235 they were before overlay strings were processed, and
5236 continue to deliver from current_buffer. */
5237
5238 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5239 pop_it (it);
5240 eassert (it->sp > 0
5241 || (NILP (it->string)
5242 && it->method == GET_FROM_BUFFER
5243 && it->stop_charpos >= BEGV
5244 && it->stop_charpos <= it->end_charpos));
5245 it->current.overlay_string_index = -1;
5246 it->n_overlay_strings = 0;
5247 it->overlay_strings_charpos = -1;
5248 /* If there's an empty display string on the stack, pop the
5249 stack, to resync the bidi iterator with IT's position. Such
5250 empty strings are pushed onto the stack in
5251 get_overlay_strings_1. */
5252 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5253 pop_it (it);
5254
5255 /* If we're at the end of the buffer, record that we have
5256 processed the overlay strings there already, so that
5257 next_element_from_buffer doesn't try it again. */
5258 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5259 it->overlay_strings_at_end_processed_p = 1;
5260 }
5261 else
5262 {
5263 /* There are more overlay strings to process. If
5264 IT->current.overlay_string_index has advanced to a position
5265 where we must load IT->overlay_strings with more strings, do
5266 it. We must load at the IT->overlay_strings_charpos where
5267 IT->n_overlay_strings was originally computed; when invisible
5268 text is present, this might not be IT_CHARPOS (Bug#7016). */
5269 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5270
5271 if (it->current.overlay_string_index && i == 0)
5272 load_overlay_strings (it, it->overlay_strings_charpos);
5273
5274 /* Initialize IT to deliver display elements from the overlay
5275 string. */
5276 it->string = it->overlay_strings[i];
5277 it->multibyte_p = STRING_MULTIBYTE (it->string);
5278 SET_TEXT_POS (it->current.string_pos, 0, 0);
5279 it->method = GET_FROM_STRING;
5280 it->stop_charpos = 0;
5281 if (it->cmp_it.stop_pos >= 0)
5282 it->cmp_it.stop_pos = 0;
5283 it->prev_stop = 0;
5284 it->base_level_stop = 0;
5285
5286 /* Set up the bidi iterator for this overlay string. */
5287 if (it->bidi_p)
5288 {
5289 it->bidi_it.string.lstring = it->string;
5290 it->bidi_it.string.s = NULL;
5291 it->bidi_it.string.schars = SCHARS (it->string);
5292 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5293 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5294 it->bidi_it.string.unibyte = !it->multibyte_p;
5295 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5296 }
5297 }
5298
5299 CHECK_IT (it);
5300 }
5301
5302
5303 /* Compare two overlay_entry structures E1 and E2. Used as a
5304 comparison function for qsort in load_overlay_strings. Overlay
5305 strings for the same position are sorted so that
5306
5307 1. All after-strings come in front of before-strings, except
5308 when they come from the same overlay.
5309
5310 2. Within after-strings, strings are sorted so that overlay strings
5311 from overlays with higher priorities come first.
5312
5313 2. Within before-strings, strings are sorted so that overlay
5314 strings from overlays with higher priorities come last.
5315
5316 Value is analogous to strcmp. */
5317
5318
5319 static int
5320 compare_overlay_entries (const void *e1, const void *e2)
5321 {
5322 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5323 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5324 int result;
5325
5326 if (entry1->after_string_p != entry2->after_string_p)
5327 {
5328 /* Let after-strings appear in front of before-strings if
5329 they come from different overlays. */
5330 if (EQ (entry1->overlay, entry2->overlay))
5331 result = entry1->after_string_p ? 1 : -1;
5332 else
5333 result = entry1->after_string_p ? -1 : 1;
5334 }
5335 else if (entry1->priority != entry2->priority)
5336 {
5337 if (entry1->after_string_p)
5338 /* After-strings sorted in order of decreasing priority. */
5339 result = entry2->priority < entry1->priority ? -1 : 1;
5340 else
5341 /* Before-strings sorted in order of increasing priority. */
5342 result = entry1->priority < entry2->priority ? -1 : 1;
5343 }
5344 else
5345 result = 0;
5346
5347 return result;
5348 }
5349
5350
5351 /* Load the vector IT->overlay_strings with overlay strings from IT's
5352 current buffer position, or from CHARPOS if that is > 0. Set
5353 IT->n_overlays to the total number of overlay strings found.
5354
5355 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5356 a time. On entry into load_overlay_strings,
5357 IT->current.overlay_string_index gives the number of overlay
5358 strings that have already been loaded by previous calls to this
5359 function.
5360
5361 IT->add_overlay_start contains an additional overlay start
5362 position to consider for taking overlay strings from, if non-zero.
5363 This position comes into play when the overlay has an `invisible'
5364 property, and both before and after-strings. When we've skipped to
5365 the end of the overlay, because of its `invisible' property, we
5366 nevertheless want its before-string to appear.
5367 IT->add_overlay_start will contain the overlay start position
5368 in this case.
5369
5370 Overlay strings are sorted so that after-string strings come in
5371 front of before-string strings. Within before and after-strings,
5372 strings are sorted by overlay priority. See also function
5373 compare_overlay_entries. */
5374
5375 static void
5376 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5377 {
5378 Lisp_Object overlay, window, str, invisible;
5379 struct Lisp_Overlay *ov;
5380 ptrdiff_t start, end;
5381 ptrdiff_t size = 20;
5382 ptrdiff_t n = 0, i, j;
5383 int invis_p;
5384 struct overlay_entry *entries
5385 = (struct overlay_entry *) alloca (size * sizeof *entries);
5386 USE_SAFE_ALLOCA;
5387
5388 if (charpos <= 0)
5389 charpos = IT_CHARPOS (*it);
5390
5391 /* Append the overlay string STRING of overlay OVERLAY to vector
5392 `entries' which has size `size' and currently contains `n'
5393 elements. AFTER_P non-zero means STRING is an after-string of
5394 OVERLAY. */
5395 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5396 do \
5397 { \
5398 Lisp_Object priority; \
5399 \
5400 if (n == size) \
5401 { \
5402 struct overlay_entry *old = entries; \
5403 SAFE_NALLOCA (entries, 2, size); \
5404 memcpy (entries, old, size * sizeof *entries); \
5405 size *= 2; \
5406 } \
5407 \
5408 entries[n].string = (STRING); \
5409 entries[n].overlay = (OVERLAY); \
5410 priority = Foverlay_get ((OVERLAY), Qpriority); \
5411 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5412 entries[n].after_string_p = (AFTER_P); \
5413 ++n; \
5414 } \
5415 while (0)
5416
5417 /* Process overlay before the overlay center. */
5418 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5419 {
5420 XSETMISC (overlay, ov);
5421 eassert (OVERLAYP (overlay));
5422 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5423 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5424
5425 if (end < charpos)
5426 break;
5427
5428 /* Skip this overlay if it doesn't start or end at IT's current
5429 position. */
5430 if (end != charpos && start != charpos)
5431 continue;
5432
5433 /* Skip this overlay if it doesn't apply to IT->w. */
5434 window = Foverlay_get (overlay, Qwindow);
5435 if (WINDOWP (window) && XWINDOW (window) != it->w)
5436 continue;
5437
5438 /* If the text ``under'' the overlay is invisible, both before-
5439 and after-strings from this overlay are visible; start and
5440 end position are indistinguishable. */
5441 invisible = Foverlay_get (overlay, Qinvisible);
5442 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5443
5444 /* If overlay has a non-empty before-string, record it. */
5445 if ((start == charpos || (end == charpos && invis_p))
5446 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5447 && SCHARS (str))
5448 RECORD_OVERLAY_STRING (overlay, str, 0);
5449
5450 /* If overlay has a non-empty after-string, record it. */
5451 if ((end == charpos || (start == charpos && invis_p))
5452 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5453 && SCHARS (str))
5454 RECORD_OVERLAY_STRING (overlay, str, 1);
5455 }
5456
5457 /* Process overlays after the overlay center. */
5458 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5459 {
5460 XSETMISC (overlay, ov);
5461 eassert (OVERLAYP (overlay));
5462 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5463 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5464
5465 if (start > charpos)
5466 break;
5467
5468 /* Skip this overlay if it doesn't start or end at IT's current
5469 position. */
5470 if (end != charpos && start != charpos)
5471 continue;
5472
5473 /* Skip this overlay if it doesn't apply to IT->w. */
5474 window = Foverlay_get (overlay, Qwindow);
5475 if (WINDOWP (window) && XWINDOW (window) != it->w)
5476 continue;
5477
5478 /* If the text ``under'' the overlay is invisible, it has a zero
5479 dimension, and both before- and after-strings apply. */
5480 invisible = Foverlay_get (overlay, Qinvisible);
5481 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5482
5483 /* If overlay has a non-empty before-string, record it. */
5484 if ((start == charpos || (end == charpos && invis_p))
5485 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5486 && SCHARS (str))
5487 RECORD_OVERLAY_STRING (overlay, str, 0);
5488
5489 /* If overlay has a non-empty after-string, record it. */
5490 if ((end == charpos || (start == charpos && invis_p))
5491 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5492 && SCHARS (str))
5493 RECORD_OVERLAY_STRING (overlay, str, 1);
5494 }
5495
5496 #undef RECORD_OVERLAY_STRING
5497
5498 /* Sort entries. */
5499 if (n > 1)
5500 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5501
5502 /* Record number of overlay strings, and where we computed it. */
5503 it->n_overlay_strings = n;
5504 it->overlay_strings_charpos = charpos;
5505
5506 /* IT->current.overlay_string_index is the number of overlay strings
5507 that have already been consumed by IT. Copy some of the
5508 remaining overlay strings to IT->overlay_strings. */
5509 i = 0;
5510 j = it->current.overlay_string_index;
5511 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5512 {
5513 it->overlay_strings[i] = entries[j].string;
5514 it->string_overlays[i++] = entries[j++].overlay;
5515 }
5516
5517 CHECK_IT (it);
5518 SAFE_FREE ();
5519 }
5520
5521
5522 /* Get the first chunk of overlay strings at IT's current buffer
5523 position, or at CHARPOS if that is > 0. Value is non-zero if at
5524 least one overlay string was found. */
5525
5526 static int
5527 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5528 {
5529 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5530 process. This fills IT->overlay_strings with strings, and sets
5531 IT->n_overlay_strings to the total number of strings to process.
5532 IT->pos.overlay_string_index has to be set temporarily to zero
5533 because load_overlay_strings needs this; it must be set to -1
5534 when no overlay strings are found because a zero value would
5535 indicate a position in the first overlay string. */
5536 it->current.overlay_string_index = 0;
5537 load_overlay_strings (it, charpos);
5538
5539 /* If we found overlay strings, set up IT to deliver display
5540 elements from the first one. Otherwise set up IT to deliver
5541 from current_buffer. */
5542 if (it->n_overlay_strings)
5543 {
5544 /* Make sure we know settings in current_buffer, so that we can
5545 restore meaningful values when we're done with the overlay
5546 strings. */
5547 if (compute_stop_p)
5548 compute_stop_pos (it);
5549 eassert (it->face_id >= 0);
5550
5551 /* Save IT's settings. They are restored after all overlay
5552 strings have been processed. */
5553 eassert (!compute_stop_p || it->sp == 0);
5554
5555 /* When called from handle_stop, there might be an empty display
5556 string loaded. In that case, don't bother saving it. But
5557 don't use this optimization with the bidi iterator, since we
5558 need the corresponding pop_it call to resync the bidi
5559 iterator's position with IT's position, after we are done
5560 with the overlay strings. (The corresponding call to pop_it
5561 in case of an empty display string is in
5562 next_overlay_string.) */
5563 if (!(!it->bidi_p
5564 && STRINGP (it->string) && !SCHARS (it->string)))
5565 push_it (it, NULL);
5566
5567 /* Set up IT to deliver display elements from the first overlay
5568 string. */
5569 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5570 it->string = it->overlay_strings[0];
5571 it->from_overlay = Qnil;
5572 it->stop_charpos = 0;
5573 eassert (STRINGP (it->string));
5574 it->end_charpos = SCHARS (it->string);
5575 it->prev_stop = 0;
5576 it->base_level_stop = 0;
5577 it->multibyte_p = STRING_MULTIBYTE (it->string);
5578 it->method = GET_FROM_STRING;
5579 it->from_disp_prop_p = 0;
5580
5581 /* Force paragraph direction to be that of the parent
5582 buffer. */
5583 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5584 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5585 else
5586 it->paragraph_embedding = L2R;
5587
5588 /* Set up the bidi iterator for this overlay string. */
5589 if (it->bidi_p)
5590 {
5591 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5592
5593 it->bidi_it.string.lstring = it->string;
5594 it->bidi_it.string.s = NULL;
5595 it->bidi_it.string.schars = SCHARS (it->string);
5596 it->bidi_it.string.bufpos = pos;
5597 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5598 it->bidi_it.string.unibyte = !it->multibyte_p;
5599 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5600 }
5601 return 1;
5602 }
5603
5604 it->current.overlay_string_index = -1;
5605 return 0;
5606 }
5607
5608 static int
5609 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5610 {
5611 it->string = Qnil;
5612 it->method = GET_FROM_BUFFER;
5613
5614 (void) get_overlay_strings_1 (it, charpos, 1);
5615
5616 CHECK_IT (it);
5617
5618 /* Value is non-zero if we found at least one overlay string. */
5619 return STRINGP (it->string);
5620 }
5621
5622
5623 \f
5624 /***********************************************************************
5625 Saving and restoring state
5626 ***********************************************************************/
5627
5628 /* Save current settings of IT on IT->stack. Called, for example,
5629 before setting up IT for an overlay string, to be able to restore
5630 IT's settings to what they were after the overlay string has been
5631 processed. If POSITION is non-NULL, it is the position to save on
5632 the stack instead of IT->position. */
5633
5634 static void
5635 push_it (struct it *it, struct text_pos *position)
5636 {
5637 struct iterator_stack_entry *p;
5638
5639 eassert (it->sp < IT_STACK_SIZE);
5640 p = it->stack + it->sp;
5641
5642 p->stop_charpos = it->stop_charpos;
5643 p->prev_stop = it->prev_stop;
5644 p->base_level_stop = it->base_level_stop;
5645 p->cmp_it = it->cmp_it;
5646 eassert (it->face_id >= 0);
5647 p->face_id = it->face_id;
5648 p->string = it->string;
5649 p->method = it->method;
5650 p->from_overlay = it->from_overlay;
5651 switch (p->method)
5652 {
5653 case GET_FROM_IMAGE:
5654 p->u.image.object = it->object;
5655 p->u.image.image_id = it->image_id;
5656 p->u.image.slice = it->slice;
5657 break;
5658 case GET_FROM_STRETCH:
5659 p->u.stretch.object = it->object;
5660 break;
5661 }
5662 p->position = position ? *position : it->position;
5663 p->current = it->current;
5664 p->end_charpos = it->end_charpos;
5665 p->string_nchars = it->string_nchars;
5666 p->area = it->area;
5667 p->multibyte_p = it->multibyte_p;
5668 p->avoid_cursor_p = it->avoid_cursor_p;
5669 p->space_width = it->space_width;
5670 p->font_height = it->font_height;
5671 p->voffset = it->voffset;
5672 p->string_from_display_prop_p = it->string_from_display_prop_p;
5673 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5674 p->display_ellipsis_p = 0;
5675 p->line_wrap = it->line_wrap;
5676 p->bidi_p = it->bidi_p;
5677 p->paragraph_embedding = it->paragraph_embedding;
5678 p->from_disp_prop_p = it->from_disp_prop_p;
5679 ++it->sp;
5680
5681 /* Save the state of the bidi iterator as well. */
5682 if (it->bidi_p)
5683 bidi_push_it (&it->bidi_it);
5684 }
5685
5686 static void
5687 iterate_out_of_display_property (struct it *it)
5688 {
5689 int buffer_p = !STRINGP (it->string);
5690 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5691 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5692
5693 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5694
5695 /* Maybe initialize paragraph direction. If we are at the beginning
5696 of a new paragraph, next_element_from_buffer may not have a
5697 chance to do that. */
5698 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5699 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5700 /* prev_stop can be zero, so check against BEGV as well. */
5701 while (it->bidi_it.charpos >= bob
5702 && it->prev_stop <= it->bidi_it.charpos
5703 && it->bidi_it.charpos < CHARPOS (it->position)
5704 && it->bidi_it.charpos < eob)
5705 bidi_move_to_visually_next (&it->bidi_it);
5706 /* Record the stop_pos we just crossed, for when we cross it
5707 back, maybe. */
5708 if (it->bidi_it.charpos > CHARPOS (it->position))
5709 it->prev_stop = CHARPOS (it->position);
5710 /* If we ended up not where pop_it put us, resync IT's
5711 positional members with the bidi iterator. */
5712 if (it->bidi_it.charpos != CHARPOS (it->position))
5713 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5714 if (buffer_p)
5715 it->current.pos = it->position;
5716 else
5717 it->current.string_pos = it->position;
5718 }
5719
5720 /* Restore IT's settings from IT->stack. Called, for example, when no
5721 more overlay strings must be processed, and we return to delivering
5722 display elements from a buffer, or when the end of a string from a
5723 `display' property is reached and we return to delivering display
5724 elements from an overlay string, or from a buffer. */
5725
5726 static void
5727 pop_it (struct it *it)
5728 {
5729 struct iterator_stack_entry *p;
5730 int from_display_prop = it->from_disp_prop_p;
5731
5732 eassert (it->sp > 0);
5733 --it->sp;
5734 p = it->stack + it->sp;
5735 it->stop_charpos = p->stop_charpos;
5736 it->prev_stop = p->prev_stop;
5737 it->base_level_stop = p->base_level_stop;
5738 it->cmp_it = p->cmp_it;
5739 it->face_id = p->face_id;
5740 it->current = p->current;
5741 it->position = p->position;
5742 it->string = p->string;
5743 it->from_overlay = p->from_overlay;
5744 if (NILP (it->string))
5745 SET_TEXT_POS (it->current.string_pos, -1, -1);
5746 it->method = p->method;
5747 switch (it->method)
5748 {
5749 case GET_FROM_IMAGE:
5750 it->image_id = p->u.image.image_id;
5751 it->object = p->u.image.object;
5752 it->slice = p->u.image.slice;
5753 break;
5754 case GET_FROM_STRETCH:
5755 it->object = p->u.stretch.object;
5756 break;
5757 case GET_FROM_BUFFER:
5758 it->object = it->w->buffer;
5759 break;
5760 case GET_FROM_STRING:
5761 it->object = it->string;
5762 break;
5763 case GET_FROM_DISPLAY_VECTOR:
5764 if (it->s)
5765 it->method = GET_FROM_C_STRING;
5766 else if (STRINGP (it->string))
5767 it->method = GET_FROM_STRING;
5768 else
5769 {
5770 it->method = GET_FROM_BUFFER;
5771 it->object = it->w->buffer;
5772 }
5773 }
5774 it->end_charpos = p->end_charpos;
5775 it->string_nchars = p->string_nchars;
5776 it->area = p->area;
5777 it->multibyte_p = p->multibyte_p;
5778 it->avoid_cursor_p = p->avoid_cursor_p;
5779 it->space_width = p->space_width;
5780 it->font_height = p->font_height;
5781 it->voffset = p->voffset;
5782 it->string_from_display_prop_p = p->string_from_display_prop_p;
5783 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5784 it->line_wrap = p->line_wrap;
5785 it->bidi_p = p->bidi_p;
5786 it->paragraph_embedding = p->paragraph_embedding;
5787 it->from_disp_prop_p = p->from_disp_prop_p;
5788 if (it->bidi_p)
5789 {
5790 bidi_pop_it (&it->bidi_it);
5791 /* Bidi-iterate until we get out of the portion of text, if any,
5792 covered by a `display' text property or by an overlay with
5793 `display' property. (We cannot just jump there, because the
5794 internal coherency of the bidi iterator state can not be
5795 preserved across such jumps.) We also must determine the
5796 paragraph base direction if the overlay we just processed is
5797 at the beginning of a new paragraph. */
5798 if (from_display_prop
5799 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5800 iterate_out_of_display_property (it);
5801
5802 eassert ((BUFFERP (it->object)
5803 && IT_CHARPOS (*it) == it->bidi_it.charpos
5804 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5805 || (STRINGP (it->object)
5806 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5807 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5808 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5809 }
5810 }
5811
5812
5813 \f
5814 /***********************************************************************
5815 Moving over lines
5816 ***********************************************************************/
5817
5818 /* Set IT's current position to the previous line start. */
5819
5820 static void
5821 back_to_previous_line_start (struct it *it)
5822 {
5823 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5824 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5825 }
5826
5827
5828 /* Move IT to the next line start.
5829
5830 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5831 we skipped over part of the text (as opposed to moving the iterator
5832 continuously over the text). Otherwise, don't change the value
5833 of *SKIPPED_P.
5834
5835 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5836 iterator on the newline, if it was found.
5837
5838 Newlines may come from buffer text, overlay strings, or strings
5839 displayed via the `display' property. That's the reason we can't
5840 simply use find_next_newline_no_quit.
5841
5842 Note that this function may not skip over invisible text that is so
5843 because of text properties and immediately follows a newline. If
5844 it would, function reseat_at_next_visible_line_start, when called
5845 from set_iterator_to_next, would effectively make invisible
5846 characters following a newline part of the wrong glyph row, which
5847 leads to wrong cursor motion. */
5848
5849 static int
5850 forward_to_next_line_start (struct it *it, int *skipped_p,
5851 struct bidi_it *bidi_it_prev)
5852 {
5853 ptrdiff_t old_selective;
5854 int newline_found_p, n;
5855 const int MAX_NEWLINE_DISTANCE = 500;
5856
5857 /* If already on a newline, just consume it to avoid unintended
5858 skipping over invisible text below. */
5859 if (it->what == IT_CHARACTER
5860 && it->c == '\n'
5861 && CHARPOS (it->position) == IT_CHARPOS (*it))
5862 {
5863 if (it->bidi_p && bidi_it_prev)
5864 *bidi_it_prev = it->bidi_it;
5865 set_iterator_to_next (it, 0);
5866 it->c = 0;
5867 return 1;
5868 }
5869
5870 /* Don't handle selective display in the following. It's (a)
5871 unnecessary because it's done by the caller, and (b) leads to an
5872 infinite recursion because next_element_from_ellipsis indirectly
5873 calls this function. */
5874 old_selective = it->selective;
5875 it->selective = 0;
5876
5877 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5878 from buffer text. */
5879 for (n = newline_found_p = 0;
5880 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5881 n += STRINGP (it->string) ? 0 : 1)
5882 {
5883 if (!get_next_display_element (it))
5884 return 0;
5885 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5886 if (newline_found_p && it->bidi_p && bidi_it_prev)
5887 *bidi_it_prev = it->bidi_it;
5888 set_iterator_to_next (it, 0);
5889 }
5890
5891 /* If we didn't find a newline near enough, see if we can use a
5892 short-cut. */
5893 if (!newline_found_p)
5894 {
5895 ptrdiff_t start = IT_CHARPOS (*it);
5896 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5897 Lisp_Object pos;
5898
5899 eassert (!STRINGP (it->string));
5900
5901 /* If there isn't any `display' property in sight, and no
5902 overlays, we can just use the position of the newline in
5903 buffer text. */
5904 if (it->stop_charpos >= limit
5905 || ((pos = Fnext_single_property_change (make_number (start),
5906 Qdisplay, Qnil,
5907 make_number (limit)),
5908 NILP (pos))
5909 && next_overlay_change (start) == ZV))
5910 {
5911 if (!it->bidi_p)
5912 {
5913 IT_CHARPOS (*it) = limit;
5914 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5915 }
5916 else
5917 {
5918 struct bidi_it bprev;
5919
5920 /* Help bidi.c avoid expensive searches for display
5921 properties and overlays, by telling it that there are
5922 none up to `limit'. */
5923 if (it->bidi_it.disp_pos < limit)
5924 {
5925 it->bidi_it.disp_pos = limit;
5926 it->bidi_it.disp_prop = 0;
5927 }
5928 do {
5929 bprev = it->bidi_it;
5930 bidi_move_to_visually_next (&it->bidi_it);
5931 } while (it->bidi_it.charpos != limit);
5932 IT_CHARPOS (*it) = limit;
5933 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5934 if (bidi_it_prev)
5935 *bidi_it_prev = bprev;
5936 }
5937 *skipped_p = newline_found_p = 1;
5938 }
5939 else
5940 {
5941 while (get_next_display_element (it)
5942 && !newline_found_p)
5943 {
5944 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5945 if (newline_found_p && it->bidi_p && bidi_it_prev)
5946 *bidi_it_prev = it->bidi_it;
5947 set_iterator_to_next (it, 0);
5948 }
5949 }
5950 }
5951
5952 it->selective = old_selective;
5953 return newline_found_p;
5954 }
5955
5956
5957 /* Set IT's current position to the previous visible line start. Skip
5958 invisible text that is so either due to text properties or due to
5959 selective display. Caution: this does not change IT->current_x and
5960 IT->hpos. */
5961
5962 static void
5963 back_to_previous_visible_line_start (struct it *it)
5964 {
5965 while (IT_CHARPOS (*it) > BEGV)
5966 {
5967 back_to_previous_line_start (it);
5968
5969 if (IT_CHARPOS (*it) <= BEGV)
5970 break;
5971
5972 /* If selective > 0, then lines indented more than its value are
5973 invisible. */
5974 if (it->selective > 0
5975 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5976 it->selective))
5977 continue;
5978
5979 /* Check the newline before point for invisibility. */
5980 {
5981 Lisp_Object prop;
5982 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5983 Qinvisible, it->window);
5984 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5985 continue;
5986 }
5987
5988 if (IT_CHARPOS (*it) <= BEGV)
5989 break;
5990
5991 {
5992 struct it it2;
5993 void *it2data = NULL;
5994 ptrdiff_t pos;
5995 ptrdiff_t beg, end;
5996 Lisp_Object val, overlay;
5997
5998 SAVE_IT (it2, *it, it2data);
5999
6000 /* If newline is part of a composition, continue from start of composition */
6001 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6002 && beg < IT_CHARPOS (*it))
6003 goto replaced;
6004
6005 /* If newline is replaced by a display property, find start of overlay
6006 or interval and continue search from that point. */
6007 pos = --IT_CHARPOS (it2);
6008 --IT_BYTEPOS (it2);
6009 it2.sp = 0;
6010 bidi_unshelve_cache (NULL, 0);
6011 it2.string_from_display_prop_p = 0;
6012 it2.from_disp_prop_p = 0;
6013 if (handle_display_prop (&it2) == HANDLED_RETURN
6014 && !NILP (val = get_char_property_and_overlay
6015 (make_number (pos), Qdisplay, Qnil, &overlay))
6016 && (OVERLAYP (overlay)
6017 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6018 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6019 {
6020 RESTORE_IT (it, it, it2data);
6021 goto replaced;
6022 }
6023
6024 /* Newline is not replaced by anything -- so we are done. */
6025 RESTORE_IT (it, it, it2data);
6026 break;
6027
6028 replaced:
6029 if (beg < BEGV)
6030 beg = BEGV;
6031 IT_CHARPOS (*it) = beg;
6032 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6033 }
6034 }
6035
6036 it->continuation_lines_width = 0;
6037
6038 eassert (IT_CHARPOS (*it) >= BEGV);
6039 eassert (IT_CHARPOS (*it) == BEGV
6040 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6041 CHECK_IT (it);
6042 }
6043
6044
6045 /* Reseat iterator IT at the previous visible line start. Skip
6046 invisible text that is so either due to text properties or due to
6047 selective display. At the end, update IT's overlay information,
6048 face information etc. */
6049
6050 void
6051 reseat_at_previous_visible_line_start (struct it *it)
6052 {
6053 back_to_previous_visible_line_start (it);
6054 reseat (it, it->current.pos, 1);
6055 CHECK_IT (it);
6056 }
6057
6058
6059 /* Reseat iterator IT on the next visible line start in the current
6060 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6061 preceding the line start. Skip over invisible text that is so
6062 because of selective display. Compute faces, overlays etc at the
6063 new position. Note that this function does not skip over text that
6064 is invisible because of text properties. */
6065
6066 static void
6067 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6068 {
6069 int newline_found_p, skipped_p = 0;
6070 struct bidi_it bidi_it_prev;
6071
6072 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6073
6074 /* Skip over lines that are invisible because they are indented
6075 more than the value of IT->selective. */
6076 if (it->selective > 0)
6077 while (IT_CHARPOS (*it) < ZV
6078 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6079 it->selective))
6080 {
6081 eassert (IT_BYTEPOS (*it) == BEGV
6082 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6083 newline_found_p =
6084 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6085 }
6086
6087 /* Position on the newline if that's what's requested. */
6088 if (on_newline_p && newline_found_p)
6089 {
6090 if (STRINGP (it->string))
6091 {
6092 if (IT_STRING_CHARPOS (*it) > 0)
6093 {
6094 if (!it->bidi_p)
6095 {
6096 --IT_STRING_CHARPOS (*it);
6097 --IT_STRING_BYTEPOS (*it);
6098 }
6099 else
6100 {
6101 /* We need to restore the bidi iterator to the state
6102 it had on the newline, and resync the IT's
6103 position with that. */
6104 it->bidi_it = bidi_it_prev;
6105 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6106 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6107 }
6108 }
6109 }
6110 else if (IT_CHARPOS (*it) > BEGV)
6111 {
6112 if (!it->bidi_p)
6113 {
6114 --IT_CHARPOS (*it);
6115 --IT_BYTEPOS (*it);
6116 }
6117 else
6118 {
6119 /* We need to restore the bidi iterator to the state it
6120 had on the newline and resync IT with that. */
6121 it->bidi_it = bidi_it_prev;
6122 IT_CHARPOS (*it) = it->bidi_it.charpos;
6123 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6124 }
6125 reseat (it, it->current.pos, 0);
6126 }
6127 }
6128 else if (skipped_p)
6129 reseat (it, it->current.pos, 0);
6130
6131 CHECK_IT (it);
6132 }
6133
6134
6135 \f
6136 /***********************************************************************
6137 Changing an iterator's position
6138 ***********************************************************************/
6139
6140 /* Change IT's current position to POS in current_buffer. If FORCE_P
6141 is non-zero, always check for text properties at the new position.
6142 Otherwise, text properties are only looked up if POS >=
6143 IT->check_charpos of a property. */
6144
6145 static void
6146 reseat (struct it *it, struct text_pos pos, int force_p)
6147 {
6148 ptrdiff_t original_pos = IT_CHARPOS (*it);
6149
6150 reseat_1 (it, pos, 0);
6151
6152 /* Determine where to check text properties. Avoid doing it
6153 where possible because text property lookup is very expensive. */
6154 if (force_p
6155 || CHARPOS (pos) > it->stop_charpos
6156 || CHARPOS (pos) < original_pos)
6157 {
6158 if (it->bidi_p)
6159 {
6160 /* For bidi iteration, we need to prime prev_stop and
6161 base_level_stop with our best estimations. */
6162 /* Implementation note: Of course, POS is not necessarily a
6163 stop position, so assigning prev_pos to it is a lie; we
6164 should have called compute_stop_backwards. However, if
6165 the current buffer does not include any R2L characters,
6166 that call would be a waste of cycles, because the
6167 iterator will never move back, and thus never cross this
6168 "fake" stop position. So we delay that backward search
6169 until the time we really need it, in next_element_from_buffer. */
6170 if (CHARPOS (pos) != it->prev_stop)
6171 it->prev_stop = CHARPOS (pos);
6172 if (CHARPOS (pos) < it->base_level_stop)
6173 it->base_level_stop = 0; /* meaning it's unknown */
6174 handle_stop (it);
6175 }
6176 else
6177 {
6178 handle_stop (it);
6179 it->prev_stop = it->base_level_stop = 0;
6180 }
6181
6182 }
6183
6184 CHECK_IT (it);
6185 }
6186
6187
6188 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6189 IT->stop_pos to POS, also. */
6190
6191 static void
6192 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6193 {
6194 /* Don't call this function when scanning a C string. */
6195 eassert (it->s == NULL);
6196
6197 /* POS must be a reasonable value. */
6198 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6199
6200 it->current.pos = it->position = pos;
6201 it->end_charpos = ZV;
6202 it->dpvec = NULL;
6203 it->current.dpvec_index = -1;
6204 it->current.overlay_string_index = -1;
6205 IT_STRING_CHARPOS (*it) = -1;
6206 IT_STRING_BYTEPOS (*it) = -1;
6207 it->string = Qnil;
6208 it->method = GET_FROM_BUFFER;
6209 it->object = it->w->buffer;
6210 it->area = TEXT_AREA;
6211 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6212 it->sp = 0;
6213 it->string_from_display_prop_p = 0;
6214 it->string_from_prefix_prop_p = 0;
6215
6216 it->from_disp_prop_p = 0;
6217 it->face_before_selective_p = 0;
6218 if (it->bidi_p)
6219 {
6220 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6221 &it->bidi_it);
6222 bidi_unshelve_cache (NULL, 0);
6223 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6224 it->bidi_it.string.s = NULL;
6225 it->bidi_it.string.lstring = Qnil;
6226 it->bidi_it.string.bufpos = 0;
6227 it->bidi_it.string.unibyte = 0;
6228 }
6229
6230 if (set_stop_p)
6231 {
6232 it->stop_charpos = CHARPOS (pos);
6233 it->base_level_stop = CHARPOS (pos);
6234 }
6235 }
6236
6237
6238 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6239 If S is non-null, it is a C string to iterate over. Otherwise,
6240 STRING gives a Lisp string to iterate over.
6241
6242 If PRECISION > 0, don't return more then PRECISION number of
6243 characters from the string.
6244
6245 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6246 characters have been returned. FIELD_WIDTH < 0 means an infinite
6247 field width.
6248
6249 MULTIBYTE = 0 means disable processing of multibyte characters,
6250 MULTIBYTE > 0 means enable it,
6251 MULTIBYTE < 0 means use IT->multibyte_p.
6252
6253 IT must be initialized via a prior call to init_iterator before
6254 calling this function. */
6255
6256 static void
6257 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6258 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6259 int multibyte)
6260 {
6261 /* No region in strings. */
6262 it->region_beg_charpos = it->region_end_charpos = -1;
6263
6264 /* No text property checks performed by default, but see below. */
6265 it->stop_charpos = -1;
6266
6267 /* Set iterator position and end position. */
6268 memset (&it->current, 0, sizeof it->current);
6269 it->current.overlay_string_index = -1;
6270 it->current.dpvec_index = -1;
6271 eassert (charpos >= 0);
6272
6273 /* If STRING is specified, use its multibyteness, otherwise use the
6274 setting of MULTIBYTE, if specified. */
6275 if (multibyte >= 0)
6276 it->multibyte_p = multibyte > 0;
6277
6278 /* Bidirectional reordering of strings is controlled by the default
6279 value of bidi-display-reordering. Don't try to reorder while
6280 loading loadup.el, as the necessary character property tables are
6281 not yet available. */
6282 it->bidi_p =
6283 NILP (Vpurify_flag)
6284 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6285
6286 if (s == NULL)
6287 {
6288 eassert (STRINGP (string));
6289 it->string = string;
6290 it->s = NULL;
6291 it->end_charpos = it->string_nchars = SCHARS (string);
6292 it->method = GET_FROM_STRING;
6293 it->current.string_pos = string_pos (charpos, string);
6294
6295 if (it->bidi_p)
6296 {
6297 it->bidi_it.string.lstring = string;
6298 it->bidi_it.string.s = NULL;
6299 it->bidi_it.string.schars = it->end_charpos;
6300 it->bidi_it.string.bufpos = 0;
6301 it->bidi_it.string.from_disp_str = 0;
6302 it->bidi_it.string.unibyte = !it->multibyte_p;
6303 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6304 FRAME_WINDOW_P (it->f), &it->bidi_it);
6305 }
6306 }
6307 else
6308 {
6309 it->s = (const unsigned char *) s;
6310 it->string = Qnil;
6311
6312 /* Note that we use IT->current.pos, not it->current.string_pos,
6313 for displaying C strings. */
6314 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6315 if (it->multibyte_p)
6316 {
6317 it->current.pos = c_string_pos (charpos, s, 1);
6318 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6319 }
6320 else
6321 {
6322 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6323 it->end_charpos = it->string_nchars = strlen (s);
6324 }
6325
6326 if (it->bidi_p)
6327 {
6328 it->bidi_it.string.lstring = Qnil;
6329 it->bidi_it.string.s = (const unsigned char *) s;
6330 it->bidi_it.string.schars = it->end_charpos;
6331 it->bidi_it.string.bufpos = 0;
6332 it->bidi_it.string.from_disp_str = 0;
6333 it->bidi_it.string.unibyte = !it->multibyte_p;
6334 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6335 &it->bidi_it);
6336 }
6337 it->method = GET_FROM_C_STRING;
6338 }
6339
6340 /* PRECISION > 0 means don't return more than PRECISION characters
6341 from the string. */
6342 if (precision > 0 && it->end_charpos - charpos > precision)
6343 {
6344 it->end_charpos = it->string_nchars = charpos + precision;
6345 if (it->bidi_p)
6346 it->bidi_it.string.schars = it->end_charpos;
6347 }
6348
6349 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6350 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6351 FIELD_WIDTH < 0 means infinite field width. This is useful for
6352 padding with `-' at the end of a mode line. */
6353 if (field_width < 0)
6354 field_width = INFINITY;
6355 /* Implementation note: We deliberately don't enlarge
6356 it->bidi_it.string.schars here to fit it->end_charpos, because
6357 the bidi iterator cannot produce characters out of thin air. */
6358 if (field_width > it->end_charpos - charpos)
6359 it->end_charpos = charpos + field_width;
6360
6361 /* Use the standard display table for displaying strings. */
6362 if (DISP_TABLE_P (Vstandard_display_table))
6363 it->dp = XCHAR_TABLE (Vstandard_display_table);
6364
6365 it->stop_charpos = charpos;
6366 it->prev_stop = charpos;
6367 it->base_level_stop = 0;
6368 if (it->bidi_p)
6369 {
6370 it->bidi_it.first_elt = 1;
6371 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6372 it->bidi_it.disp_pos = -1;
6373 }
6374 if (s == NULL && it->multibyte_p)
6375 {
6376 ptrdiff_t endpos = SCHARS (it->string);
6377 if (endpos > it->end_charpos)
6378 endpos = it->end_charpos;
6379 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6380 it->string);
6381 }
6382 CHECK_IT (it);
6383 }
6384
6385
6386 \f
6387 /***********************************************************************
6388 Iteration
6389 ***********************************************************************/
6390
6391 /* Map enum it_method value to corresponding next_element_from_* function. */
6392
6393 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6394 {
6395 next_element_from_buffer,
6396 next_element_from_display_vector,
6397 next_element_from_string,
6398 next_element_from_c_string,
6399 next_element_from_image,
6400 next_element_from_stretch
6401 };
6402
6403 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6404
6405
6406 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6407 (possibly with the following characters). */
6408
6409 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6410 ((IT)->cmp_it.id >= 0 \
6411 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6412 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6413 END_CHARPOS, (IT)->w, \
6414 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6415 (IT)->string)))
6416
6417
6418 /* Lookup the char-table Vglyphless_char_display for character C (-1
6419 if we want information for no-font case), and return the display
6420 method symbol. By side-effect, update it->what and
6421 it->glyphless_method. This function is called from
6422 get_next_display_element for each character element, and from
6423 x_produce_glyphs when no suitable font was found. */
6424
6425 Lisp_Object
6426 lookup_glyphless_char_display (int c, struct it *it)
6427 {
6428 Lisp_Object glyphless_method = Qnil;
6429
6430 if (CHAR_TABLE_P (Vglyphless_char_display)
6431 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6432 {
6433 if (c >= 0)
6434 {
6435 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6436 if (CONSP (glyphless_method))
6437 glyphless_method = FRAME_WINDOW_P (it->f)
6438 ? XCAR (glyphless_method)
6439 : XCDR (glyphless_method);
6440 }
6441 else
6442 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6443 }
6444
6445 retry:
6446 if (NILP (glyphless_method))
6447 {
6448 if (c >= 0)
6449 /* The default is to display the character by a proper font. */
6450 return Qnil;
6451 /* The default for the no-font case is to display an empty box. */
6452 glyphless_method = Qempty_box;
6453 }
6454 if (EQ (glyphless_method, Qzero_width))
6455 {
6456 if (c >= 0)
6457 return glyphless_method;
6458 /* This method can't be used for the no-font case. */
6459 glyphless_method = Qempty_box;
6460 }
6461 if (EQ (glyphless_method, Qthin_space))
6462 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6463 else if (EQ (glyphless_method, Qempty_box))
6464 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6465 else if (EQ (glyphless_method, Qhex_code))
6466 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6467 else if (STRINGP (glyphless_method))
6468 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6469 else
6470 {
6471 /* Invalid value. We use the default method. */
6472 glyphless_method = Qnil;
6473 goto retry;
6474 }
6475 it->what = IT_GLYPHLESS;
6476 return glyphless_method;
6477 }
6478
6479 /* Load IT's display element fields with information about the next
6480 display element from the current position of IT. Value is zero if
6481 end of buffer (or C string) is reached. */
6482
6483 static struct frame *last_escape_glyph_frame = NULL;
6484 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6485 static int last_escape_glyph_merged_face_id = 0;
6486
6487 struct frame *last_glyphless_glyph_frame = NULL;
6488 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6489 int last_glyphless_glyph_merged_face_id = 0;
6490
6491 static int
6492 get_next_display_element (struct it *it)
6493 {
6494 /* Non-zero means that we found a display element. Zero means that
6495 we hit the end of what we iterate over. Performance note: the
6496 function pointer `method' used here turns out to be faster than
6497 using a sequence of if-statements. */
6498 int success_p;
6499
6500 get_next:
6501 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6502
6503 if (it->what == IT_CHARACTER)
6504 {
6505 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6506 and only if (a) the resolved directionality of that character
6507 is R..." */
6508 /* FIXME: Do we need an exception for characters from display
6509 tables? */
6510 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6511 it->c = bidi_mirror_char (it->c);
6512 /* Map via display table or translate control characters.
6513 IT->c, IT->len etc. have been set to the next character by
6514 the function call above. If we have a display table, and it
6515 contains an entry for IT->c, translate it. Don't do this if
6516 IT->c itself comes from a display table, otherwise we could
6517 end up in an infinite recursion. (An alternative could be to
6518 count the recursion depth of this function and signal an
6519 error when a certain maximum depth is reached.) Is it worth
6520 it? */
6521 if (success_p && it->dpvec == NULL)
6522 {
6523 Lisp_Object dv;
6524 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6525 int nonascii_space_p = 0;
6526 int nonascii_hyphen_p = 0;
6527 int c = it->c; /* This is the character to display. */
6528
6529 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6530 {
6531 eassert (SINGLE_BYTE_CHAR_P (c));
6532 if (unibyte_display_via_language_environment)
6533 {
6534 c = DECODE_CHAR (unibyte, c);
6535 if (c < 0)
6536 c = BYTE8_TO_CHAR (it->c);
6537 }
6538 else
6539 c = BYTE8_TO_CHAR (it->c);
6540 }
6541
6542 if (it->dp
6543 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6544 VECTORP (dv)))
6545 {
6546 struct Lisp_Vector *v = XVECTOR (dv);
6547
6548 /* Return the first character from the display table
6549 entry, if not empty. If empty, don't display the
6550 current character. */
6551 if (v->header.size)
6552 {
6553 it->dpvec_char_len = it->len;
6554 it->dpvec = v->contents;
6555 it->dpend = v->contents + v->header.size;
6556 it->current.dpvec_index = 0;
6557 it->dpvec_face_id = -1;
6558 it->saved_face_id = it->face_id;
6559 it->method = GET_FROM_DISPLAY_VECTOR;
6560 it->ellipsis_p = 0;
6561 }
6562 else
6563 {
6564 set_iterator_to_next (it, 0);
6565 }
6566 goto get_next;
6567 }
6568
6569 if (! NILP (lookup_glyphless_char_display (c, it)))
6570 {
6571 if (it->what == IT_GLYPHLESS)
6572 goto done;
6573 /* Don't display this character. */
6574 set_iterator_to_next (it, 0);
6575 goto get_next;
6576 }
6577
6578 /* If `nobreak-char-display' is non-nil, we display
6579 non-ASCII spaces and hyphens specially. */
6580 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6581 {
6582 if (c == 0xA0)
6583 nonascii_space_p = 1;
6584 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6585 nonascii_hyphen_p = 1;
6586 }
6587
6588 /* Translate control characters into `\003' or `^C' form.
6589 Control characters coming from a display table entry are
6590 currently not translated because we use IT->dpvec to hold
6591 the translation. This could easily be changed but I
6592 don't believe that it is worth doing.
6593
6594 The characters handled by `nobreak-char-display' must be
6595 translated too.
6596
6597 Non-printable characters and raw-byte characters are also
6598 translated to octal form. */
6599 if (((c < ' ' || c == 127) /* ASCII control chars */
6600 ? (it->area != TEXT_AREA
6601 /* In mode line, treat \n, \t like other crl chars. */
6602 || (c != '\t'
6603 && it->glyph_row
6604 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6605 || (c != '\n' && c != '\t'))
6606 : (nonascii_space_p
6607 || nonascii_hyphen_p
6608 || CHAR_BYTE8_P (c)
6609 || ! CHAR_PRINTABLE_P (c))))
6610 {
6611 /* C is a control character, non-ASCII space/hyphen,
6612 raw-byte, or a non-printable character which must be
6613 displayed either as '\003' or as `^C' where the '\\'
6614 and '^' can be defined in the display table. Fill
6615 IT->ctl_chars with glyphs for what we have to
6616 display. Then, set IT->dpvec to these glyphs. */
6617 Lisp_Object gc;
6618 int ctl_len;
6619 int face_id;
6620 int lface_id = 0;
6621 int escape_glyph;
6622
6623 /* Handle control characters with ^. */
6624
6625 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6626 {
6627 int g;
6628
6629 g = '^'; /* default glyph for Control */
6630 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6631 if (it->dp
6632 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6633 {
6634 g = GLYPH_CODE_CHAR (gc);
6635 lface_id = GLYPH_CODE_FACE (gc);
6636 }
6637 if (lface_id)
6638 {
6639 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6640 }
6641 else if (it->f == last_escape_glyph_frame
6642 && it->face_id == last_escape_glyph_face_id)
6643 {
6644 face_id = last_escape_glyph_merged_face_id;
6645 }
6646 else
6647 {
6648 /* Merge the escape-glyph face into the current face. */
6649 face_id = merge_faces (it->f, Qescape_glyph, 0,
6650 it->face_id);
6651 last_escape_glyph_frame = it->f;
6652 last_escape_glyph_face_id = it->face_id;
6653 last_escape_glyph_merged_face_id = face_id;
6654 }
6655
6656 XSETINT (it->ctl_chars[0], g);
6657 XSETINT (it->ctl_chars[1], c ^ 0100);
6658 ctl_len = 2;
6659 goto display_control;
6660 }
6661
6662 /* Handle non-ascii space in the mode where it only gets
6663 highlighting. */
6664
6665 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6666 {
6667 /* Merge `nobreak-space' into the current face. */
6668 face_id = merge_faces (it->f, Qnobreak_space, 0,
6669 it->face_id);
6670 XSETINT (it->ctl_chars[0], ' ');
6671 ctl_len = 1;
6672 goto display_control;
6673 }
6674
6675 /* Handle sequences that start with the "escape glyph". */
6676
6677 /* the default escape glyph is \. */
6678 escape_glyph = '\\';
6679
6680 if (it->dp
6681 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6682 {
6683 escape_glyph = GLYPH_CODE_CHAR (gc);
6684 lface_id = GLYPH_CODE_FACE (gc);
6685 }
6686 if (lface_id)
6687 {
6688 /* The display table specified a face.
6689 Merge it into face_id and also into escape_glyph. */
6690 face_id = merge_faces (it->f, Qt, lface_id,
6691 it->face_id);
6692 }
6693 else if (it->f == last_escape_glyph_frame
6694 && it->face_id == last_escape_glyph_face_id)
6695 {
6696 face_id = last_escape_glyph_merged_face_id;
6697 }
6698 else
6699 {
6700 /* Merge the escape-glyph face into the current face. */
6701 face_id = merge_faces (it->f, Qescape_glyph, 0,
6702 it->face_id);
6703 last_escape_glyph_frame = it->f;
6704 last_escape_glyph_face_id = it->face_id;
6705 last_escape_glyph_merged_face_id = face_id;
6706 }
6707
6708 /* Draw non-ASCII hyphen with just highlighting: */
6709
6710 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6711 {
6712 XSETINT (it->ctl_chars[0], '-');
6713 ctl_len = 1;
6714 goto display_control;
6715 }
6716
6717 /* Draw non-ASCII space/hyphen with escape glyph: */
6718
6719 if (nonascii_space_p || nonascii_hyphen_p)
6720 {
6721 XSETINT (it->ctl_chars[0], escape_glyph);
6722 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6723 ctl_len = 2;
6724 goto display_control;
6725 }
6726
6727 {
6728 char str[10];
6729 int len, i;
6730
6731 if (CHAR_BYTE8_P (c))
6732 /* Display \200 instead of \17777600. */
6733 c = CHAR_TO_BYTE8 (c);
6734 len = sprintf (str, "%03o", c);
6735
6736 XSETINT (it->ctl_chars[0], escape_glyph);
6737 for (i = 0; i < len; i++)
6738 XSETINT (it->ctl_chars[i + 1], str[i]);
6739 ctl_len = len + 1;
6740 }
6741
6742 display_control:
6743 /* Set up IT->dpvec and return first character from it. */
6744 it->dpvec_char_len = it->len;
6745 it->dpvec = it->ctl_chars;
6746 it->dpend = it->dpvec + ctl_len;
6747 it->current.dpvec_index = 0;
6748 it->dpvec_face_id = face_id;
6749 it->saved_face_id = it->face_id;
6750 it->method = GET_FROM_DISPLAY_VECTOR;
6751 it->ellipsis_p = 0;
6752 goto get_next;
6753 }
6754 it->char_to_display = c;
6755 }
6756 else if (success_p)
6757 {
6758 it->char_to_display = it->c;
6759 }
6760 }
6761
6762 /* Adjust face id for a multibyte character. There are no multibyte
6763 character in unibyte text. */
6764 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6765 && it->multibyte_p
6766 && success_p
6767 && FRAME_WINDOW_P (it->f))
6768 {
6769 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6770
6771 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6772 {
6773 /* Automatic composition with glyph-string. */
6774 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6775
6776 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6777 }
6778 else
6779 {
6780 ptrdiff_t pos = (it->s ? -1
6781 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6782 : IT_CHARPOS (*it));
6783 int c;
6784
6785 if (it->what == IT_CHARACTER)
6786 c = it->char_to_display;
6787 else
6788 {
6789 struct composition *cmp = composition_table[it->cmp_it.id];
6790 int i;
6791
6792 c = ' ';
6793 for (i = 0; i < cmp->glyph_len; i++)
6794 /* TAB in a composition means display glyphs with
6795 padding space on the left or right. */
6796 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6797 break;
6798 }
6799 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6800 }
6801 }
6802
6803 done:
6804 /* Is this character the last one of a run of characters with
6805 box? If yes, set IT->end_of_box_run_p to 1. */
6806 if (it->face_box_p
6807 && it->s == NULL)
6808 {
6809 if (it->method == GET_FROM_STRING && it->sp)
6810 {
6811 int face_id = underlying_face_id (it);
6812 struct face *face = FACE_FROM_ID (it->f, face_id);
6813
6814 if (face)
6815 {
6816 if (face->box == FACE_NO_BOX)
6817 {
6818 /* If the box comes from face properties in a
6819 display string, check faces in that string. */
6820 int string_face_id = face_after_it_pos (it);
6821 it->end_of_box_run_p
6822 = (FACE_FROM_ID (it->f, string_face_id)->box
6823 == FACE_NO_BOX);
6824 }
6825 /* Otherwise, the box comes from the underlying face.
6826 If this is the last string character displayed, check
6827 the next buffer location. */
6828 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6829 && (it->current.overlay_string_index
6830 == it->n_overlay_strings - 1))
6831 {
6832 ptrdiff_t ignore;
6833 int next_face_id;
6834 struct text_pos pos = it->current.pos;
6835 INC_TEXT_POS (pos, it->multibyte_p);
6836
6837 next_face_id = face_at_buffer_position
6838 (it->w, CHARPOS (pos), it->region_beg_charpos,
6839 it->region_end_charpos, &ignore,
6840 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6841 -1);
6842 it->end_of_box_run_p
6843 = (FACE_FROM_ID (it->f, next_face_id)->box
6844 == FACE_NO_BOX);
6845 }
6846 }
6847 }
6848 else
6849 {
6850 int face_id = face_after_it_pos (it);
6851 it->end_of_box_run_p
6852 = (face_id != it->face_id
6853 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6854 }
6855 }
6856 /* If we reached the end of the object we've been iterating (e.g., a
6857 display string or an overlay string), and there's something on
6858 IT->stack, proceed with what's on the stack. It doesn't make
6859 sense to return zero if there's unprocessed stuff on the stack,
6860 because otherwise that stuff will never be displayed. */
6861 if (!success_p && it->sp > 0)
6862 {
6863 set_iterator_to_next (it, 0);
6864 success_p = get_next_display_element (it);
6865 }
6866
6867 /* Value is 0 if end of buffer or string reached. */
6868 return success_p;
6869 }
6870
6871
6872 /* Move IT to the next display element.
6873
6874 RESEAT_P non-zero means if called on a newline in buffer text,
6875 skip to the next visible line start.
6876
6877 Functions get_next_display_element and set_iterator_to_next are
6878 separate because I find this arrangement easier to handle than a
6879 get_next_display_element function that also increments IT's
6880 position. The way it is we can first look at an iterator's current
6881 display element, decide whether it fits on a line, and if it does,
6882 increment the iterator position. The other way around we probably
6883 would either need a flag indicating whether the iterator has to be
6884 incremented the next time, or we would have to implement a
6885 decrement position function which would not be easy to write. */
6886
6887 void
6888 set_iterator_to_next (struct it *it, int reseat_p)
6889 {
6890 /* Reset flags indicating start and end of a sequence of characters
6891 with box. Reset them at the start of this function because
6892 moving the iterator to a new position might set them. */
6893 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6894
6895 switch (it->method)
6896 {
6897 case GET_FROM_BUFFER:
6898 /* The current display element of IT is a character from
6899 current_buffer. Advance in the buffer, and maybe skip over
6900 invisible lines that are so because of selective display. */
6901 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6902 reseat_at_next_visible_line_start (it, 0);
6903 else if (it->cmp_it.id >= 0)
6904 {
6905 /* We are currently getting glyphs from a composition. */
6906 int i;
6907
6908 if (! it->bidi_p)
6909 {
6910 IT_CHARPOS (*it) += it->cmp_it.nchars;
6911 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6912 if (it->cmp_it.to < it->cmp_it.nglyphs)
6913 {
6914 it->cmp_it.from = it->cmp_it.to;
6915 }
6916 else
6917 {
6918 it->cmp_it.id = -1;
6919 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6920 IT_BYTEPOS (*it),
6921 it->end_charpos, Qnil);
6922 }
6923 }
6924 else if (! it->cmp_it.reversed_p)
6925 {
6926 /* Composition created while scanning forward. */
6927 /* Update IT's char/byte positions to point to the first
6928 character of the next grapheme cluster, or to the
6929 character visually after the current composition. */
6930 for (i = 0; i < it->cmp_it.nchars; i++)
6931 bidi_move_to_visually_next (&it->bidi_it);
6932 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6933 IT_CHARPOS (*it) = it->bidi_it.charpos;
6934
6935 if (it->cmp_it.to < it->cmp_it.nglyphs)
6936 {
6937 /* Proceed to the next grapheme cluster. */
6938 it->cmp_it.from = it->cmp_it.to;
6939 }
6940 else
6941 {
6942 /* No more grapheme clusters in this composition.
6943 Find the next stop position. */
6944 ptrdiff_t stop = it->end_charpos;
6945 if (it->bidi_it.scan_dir < 0)
6946 /* Now we are scanning backward and don't know
6947 where to stop. */
6948 stop = -1;
6949 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6950 IT_BYTEPOS (*it), stop, Qnil);
6951 }
6952 }
6953 else
6954 {
6955 /* Composition created while scanning backward. */
6956 /* Update IT's char/byte positions to point to the last
6957 character of the previous grapheme cluster, or the
6958 character visually after the current composition. */
6959 for (i = 0; i < it->cmp_it.nchars; i++)
6960 bidi_move_to_visually_next (&it->bidi_it);
6961 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6962 IT_CHARPOS (*it) = it->bidi_it.charpos;
6963 if (it->cmp_it.from > 0)
6964 {
6965 /* Proceed to the previous grapheme cluster. */
6966 it->cmp_it.to = it->cmp_it.from;
6967 }
6968 else
6969 {
6970 /* No more grapheme clusters in this composition.
6971 Find the next stop position. */
6972 ptrdiff_t stop = it->end_charpos;
6973 if (it->bidi_it.scan_dir < 0)
6974 /* Now we are scanning backward and don't know
6975 where to stop. */
6976 stop = -1;
6977 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6978 IT_BYTEPOS (*it), stop, Qnil);
6979 }
6980 }
6981 }
6982 else
6983 {
6984 eassert (it->len != 0);
6985
6986 if (!it->bidi_p)
6987 {
6988 IT_BYTEPOS (*it) += it->len;
6989 IT_CHARPOS (*it) += 1;
6990 }
6991 else
6992 {
6993 int prev_scan_dir = it->bidi_it.scan_dir;
6994 /* If this is a new paragraph, determine its base
6995 direction (a.k.a. its base embedding level). */
6996 if (it->bidi_it.new_paragraph)
6997 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6998 bidi_move_to_visually_next (&it->bidi_it);
6999 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7000 IT_CHARPOS (*it) = it->bidi_it.charpos;
7001 if (prev_scan_dir != it->bidi_it.scan_dir)
7002 {
7003 /* As the scan direction was changed, we must
7004 re-compute the stop position for composition. */
7005 ptrdiff_t stop = it->end_charpos;
7006 if (it->bidi_it.scan_dir < 0)
7007 stop = -1;
7008 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7009 IT_BYTEPOS (*it), stop, Qnil);
7010 }
7011 }
7012 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7013 }
7014 break;
7015
7016 case GET_FROM_C_STRING:
7017 /* Current display element of IT is from a C string. */
7018 if (!it->bidi_p
7019 /* If the string position is beyond string's end, it means
7020 next_element_from_c_string is padding the string with
7021 blanks, in which case we bypass the bidi iterator,
7022 because it cannot deal with such virtual characters. */
7023 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7024 {
7025 IT_BYTEPOS (*it) += it->len;
7026 IT_CHARPOS (*it) += 1;
7027 }
7028 else
7029 {
7030 bidi_move_to_visually_next (&it->bidi_it);
7031 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7032 IT_CHARPOS (*it) = it->bidi_it.charpos;
7033 }
7034 break;
7035
7036 case GET_FROM_DISPLAY_VECTOR:
7037 /* Current display element of IT is from a display table entry.
7038 Advance in the display table definition. Reset it to null if
7039 end reached, and continue with characters from buffers/
7040 strings. */
7041 ++it->current.dpvec_index;
7042
7043 /* Restore face of the iterator to what they were before the
7044 display vector entry (these entries may contain faces). */
7045 it->face_id = it->saved_face_id;
7046
7047 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7048 {
7049 int recheck_faces = it->ellipsis_p;
7050
7051 if (it->s)
7052 it->method = GET_FROM_C_STRING;
7053 else if (STRINGP (it->string))
7054 it->method = GET_FROM_STRING;
7055 else
7056 {
7057 it->method = GET_FROM_BUFFER;
7058 it->object = it->w->buffer;
7059 }
7060
7061 it->dpvec = NULL;
7062 it->current.dpvec_index = -1;
7063
7064 /* Skip over characters which were displayed via IT->dpvec. */
7065 if (it->dpvec_char_len < 0)
7066 reseat_at_next_visible_line_start (it, 1);
7067 else if (it->dpvec_char_len > 0)
7068 {
7069 if (it->method == GET_FROM_STRING
7070 && it->n_overlay_strings > 0)
7071 it->ignore_overlay_strings_at_pos_p = 1;
7072 it->len = it->dpvec_char_len;
7073 set_iterator_to_next (it, reseat_p);
7074 }
7075
7076 /* Maybe recheck faces after display vector */
7077 if (recheck_faces)
7078 it->stop_charpos = IT_CHARPOS (*it);
7079 }
7080 break;
7081
7082 case GET_FROM_STRING:
7083 /* Current display element is a character from a Lisp string. */
7084 eassert (it->s == NULL && STRINGP (it->string));
7085 /* Don't advance past string end. These conditions are true
7086 when set_iterator_to_next is called at the end of
7087 get_next_display_element, in which case the Lisp string is
7088 already exhausted, and all we want is pop the iterator
7089 stack. */
7090 if (it->current.overlay_string_index >= 0)
7091 {
7092 /* This is an overlay string, so there's no padding with
7093 spaces, and the number of characters in the string is
7094 where the string ends. */
7095 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7096 goto consider_string_end;
7097 }
7098 else
7099 {
7100 /* Not an overlay string. There could be padding, so test
7101 against it->end_charpos . */
7102 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7103 goto consider_string_end;
7104 }
7105 if (it->cmp_it.id >= 0)
7106 {
7107 int i;
7108
7109 if (! it->bidi_p)
7110 {
7111 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7112 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7113 if (it->cmp_it.to < it->cmp_it.nglyphs)
7114 it->cmp_it.from = it->cmp_it.to;
7115 else
7116 {
7117 it->cmp_it.id = -1;
7118 composition_compute_stop_pos (&it->cmp_it,
7119 IT_STRING_CHARPOS (*it),
7120 IT_STRING_BYTEPOS (*it),
7121 it->end_charpos, it->string);
7122 }
7123 }
7124 else if (! it->cmp_it.reversed_p)
7125 {
7126 for (i = 0; i < it->cmp_it.nchars; i++)
7127 bidi_move_to_visually_next (&it->bidi_it);
7128 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7129 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7130
7131 if (it->cmp_it.to < it->cmp_it.nglyphs)
7132 it->cmp_it.from = it->cmp_it.to;
7133 else
7134 {
7135 ptrdiff_t stop = it->end_charpos;
7136 if (it->bidi_it.scan_dir < 0)
7137 stop = -1;
7138 composition_compute_stop_pos (&it->cmp_it,
7139 IT_STRING_CHARPOS (*it),
7140 IT_STRING_BYTEPOS (*it), stop,
7141 it->string);
7142 }
7143 }
7144 else
7145 {
7146 for (i = 0; i < it->cmp_it.nchars; i++)
7147 bidi_move_to_visually_next (&it->bidi_it);
7148 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7149 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7150 if (it->cmp_it.from > 0)
7151 it->cmp_it.to = it->cmp_it.from;
7152 else
7153 {
7154 ptrdiff_t stop = it->end_charpos;
7155 if (it->bidi_it.scan_dir < 0)
7156 stop = -1;
7157 composition_compute_stop_pos (&it->cmp_it,
7158 IT_STRING_CHARPOS (*it),
7159 IT_STRING_BYTEPOS (*it), stop,
7160 it->string);
7161 }
7162 }
7163 }
7164 else
7165 {
7166 if (!it->bidi_p
7167 /* If the string position is beyond string's end, it
7168 means next_element_from_string is padding the string
7169 with blanks, in which case we bypass the bidi
7170 iterator, because it cannot deal with such virtual
7171 characters. */
7172 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7173 {
7174 IT_STRING_BYTEPOS (*it) += it->len;
7175 IT_STRING_CHARPOS (*it) += 1;
7176 }
7177 else
7178 {
7179 int prev_scan_dir = it->bidi_it.scan_dir;
7180
7181 bidi_move_to_visually_next (&it->bidi_it);
7182 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7183 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7184 if (prev_scan_dir != it->bidi_it.scan_dir)
7185 {
7186 ptrdiff_t stop = it->end_charpos;
7187
7188 if (it->bidi_it.scan_dir < 0)
7189 stop = -1;
7190 composition_compute_stop_pos (&it->cmp_it,
7191 IT_STRING_CHARPOS (*it),
7192 IT_STRING_BYTEPOS (*it), stop,
7193 it->string);
7194 }
7195 }
7196 }
7197
7198 consider_string_end:
7199
7200 if (it->current.overlay_string_index >= 0)
7201 {
7202 /* IT->string is an overlay string. Advance to the
7203 next, if there is one. */
7204 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7205 {
7206 it->ellipsis_p = 0;
7207 next_overlay_string (it);
7208 if (it->ellipsis_p)
7209 setup_for_ellipsis (it, 0);
7210 }
7211 }
7212 else
7213 {
7214 /* IT->string is not an overlay string. If we reached
7215 its end, and there is something on IT->stack, proceed
7216 with what is on the stack. This can be either another
7217 string, this time an overlay string, or a buffer. */
7218 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7219 && it->sp > 0)
7220 {
7221 pop_it (it);
7222 if (it->method == GET_FROM_STRING)
7223 goto consider_string_end;
7224 }
7225 }
7226 break;
7227
7228 case GET_FROM_IMAGE:
7229 case GET_FROM_STRETCH:
7230 /* The position etc with which we have to proceed are on
7231 the stack. The position may be at the end of a string,
7232 if the `display' property takes up the whole string. */
7233 eassert (it->sp > 0);
7234 pop_it (it);
7235 if (it->method == GET_FROM_STRING)
7236 goto consider_string_end;
7237 break;
7238
7239 default:
7240 /* There are no other methods defined, so this should be a bug. */
7241 abort ();
7242 }
7243
7244 eassert (it->method != GET_FROM_STRING
7245 || (STRINGP (it->string)
7246 && IT_STRING_CHARPOS (*it) >= 0));
7247 }
7248
7249 /* Load IT's display element fields with information about the next
7250 display element which comes from a display table entry or from the
7251 result of translating a control character to one of the forms `^C'
7252 or `\003'.
7253
7254 IT->dpvec holds the glyphs to return as characters.
7255 IT->saved_face_id holds the face id before the display vector--it
7256 is restored into IT->face_id in set_iterator_to_next. */
7257
7258 static int
7259 next_element_from_display_vector (struct it *it)
7260 {
7261 Lisp_Object gc;
7262
7263 /* Precondition. */
7264 eassert (it->dpvec && it->current.dpvec_index >= 0);
7265
7266 it->face_id = it->saved_face_id;
7267
7268 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7269 That seemed totally bogus - so I changed it... */
7270 gc = it->dpvec[it->current.dpvec_index];
7271
7272 if (GLYPH_CODE_P (gc))
7273 {
7274 it->c = GLYPH_CODE_CHAR (gc);
7275 it->len = CHAR_BYTES (it->c);
7276
7277 /* The entry may contain a face id to use. Such a face id is
7278 the id of a Lisp face, not a realized face. A face id of
7279 zero means no face is specified. */
7280 if (it->dpvec_face_id >= 0)
7281 it->face_id = it->dpvec_face_id;
7282 else
7283 {
7284 int lface_id = GLYPH_CODE_FACE (gc);
7285 if (lface_id > 0)
7286 it->face_id = merge_faces (it->f, Qt, lface_id,
7287 it->saved_face_id);
7288 }
7289 }
7290 else
7291 /* Display table entry is invalid. Return a space. */
7292 it->c = ' ', it->len = 1;
7293
7294 /* Don't change position and object of the iterator here. They are
7295 still the values of the character that had this display table
7296 entry or was translated, and that's what we want. */
7297 it->what = IT_CHARACTER;
7298 return 1;
7299 }
7300
7301 /* Get the first element of string/buffer in the visual order, after
7302 being reseated to a new position in a string or a buffer. */
7303 static void
7304 get_visually_first_element (struct it *it)
7305 {
7306 int string_p = STRINGP (it->string) || it->s;
7307 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7308 ptrdiff_t bob = (string_p ? 0 : BEGV);
7309
7310 if (STRINGP (it->string))
7311 {
7312 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7313 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7314 }
7315 else
7316 {
7317 it->bidi_it.charpos = IT_CHARPOS (*it);
7318 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7319 }
7320
7321 if (it->bidi_it.charpos == eob)
7322 {
7323 /* Nothing to do, but reset the FIRST_ELT flag, like
7324 bidi_paragraph_init does, because we are not going to
7325 call it. */
7326 it->bidi_it.first_elt = 0;
7327 }
7328 else if (it->bidi_it.charpos == bob
7329 || (!string_p
7330 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7331 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7332 {
7333 /* If we are at the beginning of a line/string, we can produce
7334 the next element right away. */
7335 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7336 bidi_move_to_visually_next (&it->bidi_it);
7337 }
7338 else
7339 {
7340 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7341
7342 /* We need to prime the bidi iterator starting at the line's or
7343 string's beginning, before we will be able to produce the
7344 next element. */
7345 if (string_p)
7346 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7347 else
7348 {
7349 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7350 -1);
7351 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7352 }
7353 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7354 do
7355 {
7356 /* Now return to buffer/string position where we were asked
7357 to get the next display element, and produce that. */
7358 bidi_move_to_visually_next (&it->bidi_it);
7359 }
7360 while (it->bidi_it.bytepos != orig_bytepos
7361 && it->bidi_it.charpos < eob);
7362 }
7363
7364 /* Adjust IT's position information to where we ended up. */
7365 if (STRINGP (it->string))
7366 {
7367 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7368 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7369 }
7370 else
7371 {
7372 IT_CHARPOS (*it) = it->bidi_it.charpos;
7373 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7374 }
7375
7376 if (STRINGP (it->string) || !it->s)
7377 {
7378 ptrdiff_t stop, charpos, bytepos;
7379
7380 if (STRINGP (it->string))
7381 {
7382 eassert (!it->s);
7383 stop = SCHARS (it->string);
7384 if (stop > it->end_charpos)
7385 stop = it->end_charpos;
7386 charpos = IT_STRING_CHARPOS (*it);
7387 bytepos = IT_STRING_BYTEPOS (*it);
7388 }
7389 else
7390 {
7391 stop = it->end_charpos;
7392 charpos = IT_CHARPOS (*it);
7393 bytepos = IT_BYTEPOS (*it);
7394 }
7395 if (it->bidi_it.scan_dir < 0)
7396 stop = -1;
7397 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7398 it->string);
7399 }
7400 }
7401
7402 /* Load IT with the next display element from Lisp string IT->string.
7403 IT->current.string_pos is the current position within the string.
7404 If IT->current.overlay_string_index >= 0, the Lisp string is an
7405 overlay string. */
7406
7407 static int
7408 next_element_from_string (struct it *it)
7409 {
7410 struct text_pos position;
7411
7412 eassert (STRINGP (it->string));
7413 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7414 eassert (IT_STRING_CHARPOS (*it) >= 0);
7415 position = it->current.string_pos;
7416
7417 /* With bidi reordering, the character to display might not be the
7418 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7419 that we were reseat()ed to a new string, whose paragraph
7420 direction is not known. */
7421 if (it->bidi_p && it->bidi_it.first_elt)
7422 {
7423 get_visually_first_element (it);
7424 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7425 }
7426
7427 /* Time to check for invisible text? */
7428 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7429 {
7430 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7431 {
7432 if (!(!it->bidi_p
7433 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7434 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7435 {
7436 /* With bidi non-linear iteration, we could find
7437 ourselves far beyond the last computed stop_charpos,
7438 with several other stop positions in between that we
7439 missed. Scan them all now, in buffer's logical
7440 order, until we find and handle the last stop_charpos
7441 that precedes our current position. */
7442 handle_stop_backwards (it, it->stop_charpos);
7443 return GET_NEXT_DISPLAY_ELEMENT (it);
7444 }
7445 else
7446 {
7447 if (it->bidi_p)
7448 {
7449 /* Take note of the stop position we just moved
7450 across, for when we will move back across it. */
7451 it->prev_stop = it->stop_charpos;
7452 /* If we are at base paragraph embedding level, take
7453 note of the last stop position seen at this
7454 level. */
7455 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7456 it->base_level_stop = it->stop_charpos;
7457 }
7458 handle_stop (it);
7459
7460 /* Since a handler may have changed IT->method, we must
7461 recurse here. */
7462 return GET_NEXT_DISPLAY_ELEMENT (it);
7463 }
7464 }
7465 else if (it->bidi_p
7466 /* If we are before prev_stop, we may have overstepped
7467 on our way backwards a stop_pos, and if so, we need
7468 to handle that stop_pos. */
7469 && IT_STRING_CHARPOS (*it) < it->prev_stop
7470 /* We can sometimes back up for reasons that have nothing
7471 to do with bidi reordering. E.g., compositions. The
7472 code below is only needed when we are above the base
7473 embedding level, so test for that explicitly. */
7474 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7475 {
7476 /* If we lost track of base_level_stop, we have no better
7477 place for handle_stop_backwards to start from than string
7478 beginning. This happens, e.g., when we were reseated to
7479 the previous screenful of text by vertical-motion. */
7480 if (it->base_level_stop <= 0
7481 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7482 it->base_level_stop = 0;
7483 handle_stop_backwards (it, it->base_level_stop);
7484 return GET_NEXT_DISPLAY_ELEMENT (it);
7485 }
7486 }
7487
7488 if (it->current.overlay_string_index >= 0)
7489 {
7490 /* Get the next character from an overlay string. In overlay
7491 strings, there is no field width or padding with spaces to
7492 do. */
7493 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7494 {
7495 it->what = IT_EOB;
7496 return 0;
7497 }
7498 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7499 IT_STRING_BYTEPOS (*it),
7500 it->bidi_it.scan_dir < 0
7501 ? -1
7502 : SCHARS (it->string))
7503 && next_element_from_composition (it))
7504 {
7505 return 1;
7506 }
7507 else if (STRING_MULTIBYTE (it->string))
7508 {
7509 const unsigned char *s = (SDATA (it->string)
7510 + IT_STRING_BYTEPOS (*it));
7511 it->c = string_char_and_length (s, &it->len);
7512 }
7513 else
7514 {
7515 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7516 it->len = 1;
7517 }
7518 }
7519 else
7520 {
7521 /* Get the next character from a Lisp string that is not an
7522 overlay string. Such strings come from the mode line, for
7523 example. We may have to pad with spaces, or truncate the
7524 string. See also next_element_from_c_string. */
7525 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7526 {
7527 it->what = IT_EOB;
7528 return 0;
7529 }
7530 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7531 {
7532 /* Pad with spaces. */
7533 it->c = ' ', it->len = 1;
7534 CHARPOS (position) = BYTEPOS (position) = -1;
7535 }
7536 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7537 IT_STRING_BYTEPOS (*it),
7538 it->bidi_it.scan_dir < 0
7539 ? -1
7540 : it->string_nchars)
7541 && next_element_from_composition (it))
7542 {
7543 return 1;
7544 }
7545 else if (STRING_MULTIBYTE (it->string))
7546 {
7547 const unsigned char *s = (SDATA (it->string)
7548 + IT_STRING_BYTEPOS (*it));
7549 it->c = string_char_and_length (s, &it->len);
7550 }
7551 else
7552 {
7553 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7554 it->len = 1;
7555 }
7556 }
7557
7558 /* Record what we have and where it came from. */
7559 it->what = IT_CHARACTER;
7560 it->object = it->string;
7561 it->position = position;
7562 return 1;
7563 }
7564
7565
7566 /* Load IT with next display element from C string IT->s.
7567 IT->string_nchars is the maximum number of characters to return
7568 from the string. IT->end_charpos may be greater than
7569 IT->string_nchars when this function is called, in which case we
7570 may have to return padding spaces. Value is zero if end of string
7571 reached, including padding spaces. */
7572
7573 static int
7574 next_element_from_c_string (struct it *it)
7575 {
7576 int success_p = 1;
7577
7578 eassert (it->s);
7579 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7580 it->what = IT_CHARACTER;
7581 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7582 it->object = Qnil;
7583
7584 /* With bidi reordering, the character to display might not be the
7585 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7586 we were reseated to a new string, whose paragraph direction is
7587 not known. */
7588 if (it->bidi_p && it->bidi_it.first_elt)
7589 get_visually_first_element (it);
7590
7591 /* IT's position can be greater than IT->string_nchars in case a
7592 field width or precision has been specified when the iterator was
7593 initialized. */
7594 if (IT_CHARPOS (*it) >= it->end_charpos)
7595 {
7596 /* End of the game. */
7597 it->what = IT_EOB;
7598 success_p = 0;
7599 }
7600 else if (IT_CHARPOS (*it) >= it->string_nchars)
7601 {
7602 /* Pad with spaces. */
7603 it->c = ' ', it->len = 1;
7604 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7605 }
7606 else if (it->multibyte_p)
7607 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7608 else
7609 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7610
7611 return success_p;
7612 }
7613
7614
7615 /* Set up IT to return characters from an ellipsis, if appropriate.
7616 The definition of the ellipsis glyphs may come from a display table
7617 entry. This function fills IT with the first glyph from the
7618 ellipsis if an ellipsis is to be displayed. */
7619
7620 static int
7621 next_element_from_ellipsis (struct it *it)
7622 {
7623 if (it->selective_display_ellipsis_p)
7624 setup_for_ellipsis (it, it->len);
7625 else
7626 {
7627 /* The face at the current position may be different from the
7628 face we find after the invisible text. Remember what it
7629 was in IT->saved_face_id, and signal that it's there by
7630 setting face_before_selective_p. */
7631 it->saved_face_id = it->face_id;
7632 it->method = GET_FROM_BUFFER;
7633 it->object = it->w->buffer;
7634 reseat_at_next_visible_line_start (it, 1);
7635 it->face_before_selective_p = 1;
7636 }
7637
7638 return GET_NEXT_DISPLAY_ELEMENT (it);
7639 }
7640
7641
7642 /* Deliver an image display element. The iterator IT is already
7643 filled with image information (done in handle_display_prop). Value
7644 is always 1. */
7645
7646
7647 static int
7648 next_element_from_image (struct it *it)
7649 {
7650 it->what = IT_IMAGE;
7651 it->ignore_overlay_strings_at_pos_p = 0;
7652 return 1;
7653 }
7654
7655
7656 /* Fill iterator IT with next display element from a stretch glyph
7657 property. IT->object is the value of the text property. Value is
7658 always 1. */
7659
7660 static int
7661 next_element_from_stretch (struct it *it)
7662 {
7663 it->what = IT_STRETCH;
7664 return 1;
7665 }
7666
7667 /* Scan backwards from IT's current position until we find a stop
7668 position, or until BEGV. This is called when we find ourself
7669 before both the last known prev_stop and base_level_stop while
7670 reordering bidirectional text. */
7671
7672 static void
7673 compute_stop_pos_backwards (struct it *it)
7674 {
7675 const int SCAN_BACK_LIMIT = 1000;
7676 struct text_pos pos;
7677 struct display_pos save_current = it->current;
7678 struct text_pos save_position = it->position;
7679 ptrdiff_t charpos = IT_CHARPOS (*it);
7680 ptrdiff_t where_we_are = charpos;
7681 ptrdiff_t save_stop_pos = it->stop_charpos;
7682 ptrdiff_t save_end_pos = it->end_charpos;
7683
7684 eassert (NILP (it->string) && !it->s);
7685 eassert (it->bidi_p);
7686 it->bidi_p = 0;
7687 do
7688 {
7689 it->end_charpos = min (charpos + 1, ZV);
7690 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7691 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7692 reseat_1 (it, pos, 0);
7693 compute_stop_pos (it);
7694 /* We must advance forward, right? */
7695 if (it->stop_charpos <= charpos)
7696 abort ();
7697 }
7698 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7699
7700 if (it->stop_charpos <= where_we_are)
7701 it->prev_stop = it->stop_charpos;
7702 else
7703 it->prev_stop = BEGV;
7704 it->bidi_p = 1;
7705 it->current = save_current;
7706 it->position = save_position;
7707 it->stop_charpos = save_stop_pos;
7708 it->end_charpos = save_end_pos;
7709 }
7710
7711 /* Scan forward from CHARPOS in the current buffer/string, until we
7712 find a stop position > current IT's position. Then handle the stop
7713 position before that. This is called when we bump into a stop
7714 position while reordering bidirectional text. CHARPOS should be
7715 the last previously processed stop_pos (or BEGV/0, if none were
7716 processed yet) whose position is less that IT's current
7717 position. */
7718
7719 static void
7720 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7721 {
7722 int bufp = !STRINGP (it->string);
7723 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7724 struct display_pos save_current = it->current;
7725 struct text_pos save_position = it->position;
7726 struct text_pos pos1;
7727 ptrdiff_t next_stop;
7728
7729 /* Scan in strict logical order. */
7730 eassert (it->bidi_p);
7731 it->bidi_p = 0;
7732 do
7733 {
7734 it->prev_stop = charpos;
7735 if (bufp)
7736 {
7737 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7738 reseat_1 (it, pos1, 0);
7739 }
7740 else
7741 it->current.string_pos = string_pos (charpos, it->string);
7742 compute_stop_pos (it);
7743 /* We must advance forward, right? */
7744 if (it->stop_charpos <= it->prev_stop)
7745 abort ();
7746 charpos = it->stop_charpos;
7747 }
7748 while (charpos <= where_we_are);
7749
7750 it->bidi_p = 1;
7751 it->current = save_current;
7752 it->position = save_position;
7753 next_stop = it->stop_charpos;
7754 it->stop_charpos = it->prev_stop;
7755 handle_stop (it);
7756 it->stop_charpos = next_stop;
7757 }
7758
7759 /* Load IT with the next display element from current_buffer. Value
7760 is zero if end of buffer reached. IT->stop_charpos is the next
7761 position at which to stop and check for text properties or buffer
7762 end. */
7763
7764 static int
7765 next_element_from_buffer (struct it *it)
7766 {
7767 int success_p = 1;
7768
7769 eassert (IT_CHARPOS (*it) >= BEGV);
7770 eassert (NILP (it->string) && !it->s);
7771 eassert (!it->bidi_p
7772 || (EQ (it->bidi_it.string.lstring, Qnil)
7773 && it->bidi_it.string.s == NULL));
7774
7775 /* With bidi reordering, the character to display might not be the
7776 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7777 we were reseat()ed to a new buffer position, which is potentially
7778 a different paragraph. */
7779 if (it->bidi_p && it->bidi_it.first_elt)
7780 {
7781 get_visually_first_element (it);
7782 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7783 }
7784
7785 if (IT_CHARPOS (*it) >= it->stop_charpos)
7786 {
7787 if (IT_CHARPOS (*it) >= it->end_charpos)
7788 {
7789 int overlay_strings_follow_p;
7790
7791 /* End of the game, except when overlay strings follow that
7792 haven't been returned yet. */
7793 if (it->overlay_strings_at_end_processed_p)
7794 overlay_strings_follow_p = 0;
7795 else
7796 {
7797 it->overlay_strings_at_end_processed_p = 1;
7798 overlay_strings_follow_p = get_overlay_strings (it, 0);
7799 }
7800
7801 if (overlay_strings_follow_p)
7802 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7803 else
7804 {
7805 it->what = IT_EOB;
7806 it->position = it->current.pos;
7807 success_p = 0;
7808 }
7809 }
7810 else if (!(!it->bidi_p
7811 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7812 || IT_CHARPOS (*it) == it->stop_charpos))
7813 {
7814 /* With bidi non-linear iteration, we could find ourselves
7815 far beyond the last computed stop_charpos, with several
7816 other stop positions in between that we missed. Scan
7817 them all now, in buffer's logical order, until we find
7818 and handle the last stop_charpos that precedes our
7819 current position. */
7820 handle_stop_backwards (it, it->stop_charpos);
7821 return GET_NEXT_DISPLAY_ELEMENT (it);
7822 }
7823 else
7824 {
7825 if (it->bidi_p)
7826 {
7827 /* Take note of the stop position we just moved across,
7828 for when we will move back across it. */
7829 it->prev_stop = it->stop_charpos;
7830 /* If we are at base paragraph embedding level, take
7831 note of the last stop position seen at this
7832 level. */
7833 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7834 it->base_level_stop = it->stop_charpos;
7835 }
7836 handle_stop (it);
7837 return GET_NEXT_DISPLAY_ELEMENT (it);
7838 }
7839 }
7840 else if (it->bidi_p
7841 /* If we are before prev_stop, we may have overstepped on
7842 our way backwards a stop_pos, and if so, we need to
7843 handle that stop_pos. */
7844 && IT_CHARPOS (*it) < it->prev_stop
7845 /* We can sometimes back up for reasons that have nothing
7846 to do with bidi reordering. E.g., compositions. The
7847 code below is only needed when we are above the base
7848 embedding level, so test for that explicitly. */
7849 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7850 {
7851 if (it->base_level_stop <= 0
7852 || IT_CHARPOS (*it) < it->base_level_stop)
7853 {
7854 /* If we lost track of base_level_stop, we need to find
7855 prev_stop by looking backwards. This happens, e.g., when
7856 we were reseated to the previous screenful of text by
7857 vertical-motion. */
7858 it->base_level_stop = BEGV;
7859 compute_stop_pos_backwards (it);
7860 handle_stop_backwards (it, it->prev_stop);
7861 }
7862 else
7863 handle_stop_backwards (it, it->base_level_stop);
7864 return GET_NEXT_DISPLAY_ELEMENT (it);
7865 }
7866 else
7867 {
7868 /* No face changes, overlays etc. in sight, so just return a
7869 character from current_buffer. */
7870 unsigned char *p;
7871 ptrdiff_t stop;
7872
7873 /* Maybe run the redisplay end trigger hook. Performance note:
7874 This doesn't seem to cost measurable time. */
7875 if (it->redisplay_end_trigger_charpos
7876 && it->glyph_row
7877 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7878 run_redisplay_end_trigger_hook (it);
7879
7880 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7881 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7882 stop)
7883 && next_element_from_composition (it))
7884 {
7885 return 1;
7886 }
7887
7888 /* Get the next character, maybe multibyte. */
7889 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7890 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7891 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7892 else
7893 it->c = *p, it->len = 1;
7894
7895 /* Record what we have and where it came from. */
7896 it->what = IT_CHARACTER;
7897 it->object = it->w->buffer;
7898 it->position = it->current.pos;
7899
7900 /* Normally we return the character found above, except when we
7901 really want to return an ellipsis for selective display. */
7902 if (it->selective)
7903 {
7904 if (it->c == '\n')
7905 {
7906 /* A value of selective > 0 means hide lines indented more
7907 than that number of columns. */
7908 if (it->selective > 0
7909 && IT_CHARPOS (*it) + 1 < ZV
7910 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7911 IT_BYTEPOS (*it) + 1,
7912 it->selective))
7913 {
7914 success_p = next_element_from_ellipsis (it);
7915 it->dpvec_char_len = -1;
7916 }
7917 }
7918 else if (it->c == '\r' && it->selective == -1)
7919 {
7920 /* A value of selective == -1 means that everything from the
7921 CR to the end of the line is invisible, with maybe an
7922 ellipsis displayed for it. */
7923 success_p = next_element_from_ellipsis (it);
7924 it->dpvec_char_len = -1;
7925 }
7926 }
7927 }
7928
7929 /* Value is zero if end of buffer reached. */
7930 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7931 return success_p;
7932 }
7933
7934
7935 /* Run the redisplay end trigger hook for IT. */
7936
7937 static void
7938 run_redisplay_end_trigger_hook (struct it *it)
7939 {
7940 Lisp_Object args[3];
7941
7942 /* IT->glyph_row should be non-null, i.e. we should be actually
7943 displaying something, or otherwise we should not run the hook. */
7944 eassert (it->glyph_row);
7945
7946 /* Set up hook arguments. */
7947 args[0] = Qredisplay_end_trigger_functions;
7948 args[1] = it->window;
7949 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7950 it->redisplay_end_trigger_charpos = 0;
7951
7952 /* Since we are *trying* to run these functions, don't try to run
7953 them again, even if they get an error. */
7954 it->w->redisplay_end_trigger = Qnil;
7955 Frun_hook_with_args (3, args);
7956
7957 /* Notice if it changed the face of the character we are on. */
7958 handle_face_prop (it);
7959 }
7960
7961
7962 /* Deliver a composition display element. Unlike the other
7963 next_element_from_XXX, this function is not registered in the array
7964 get_next_element[]. It is called from next_element_from_buffer and
7965 next_element_from_string when necessary. */
7966
7967 static int
7968 next_element_from_composition (struct it *it)
7969 {
7970 it->what = IT_COMPOSITION;
7971 it->len = it->cmp_it.nbytes;
7972 if (STRINGP (it->string))
7973 {
7974 if (it->c < 0)
7975 {
7976 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7977 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7978 return 0;
7979 }
7980 it->position = it->current.string_pos;
7981 it->object = it->string;
7982 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7983 IT_STRING_BYTEPOS (*it), it->string);
7984 }
7985 else
7986 {
7987 if (it->c < 0)
7988 {
7989 IT_CHARPOS (*it) += it->cmp_it.nchars;
7990 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7991 if (it->bidi_p)
7992 {
7993 if (it->bidi_it.new_paragraph)
7994 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7995 /* Resync the bidi iterator with IT's new position.
7996 FIXME: this doesn't support bidirectional text. */
7997 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7998 bidi_move_to_visually_next (&it->bidi_it);
7999 }
8000 return 0;
8001 }
8002 it->position = it->current.pos;
8003 it->object = it->w->buffer;
8004 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8005 IT_BYTEPOS (*it), Qnil);
8006 }
8007 return 1;
8008 }
8009
8010
8011 \f
8012 /***********************************************************************
8013 Moving an iterator without producing glyphs
8014 ***********************************************************************/
8015
8016 /* Check if iterator is at a position corresponding to a valid buffer
8017 position after some move_it_ call. */
8018
8019 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8020 ((it)->method == GET_FROM_STRING \
8021 ? IT_STRING_CHARPOS (*it) == 0 \
8022 : 1)
8023
8024
8025 /* Move iterator IT to a specified buffer or X position within one
8026 line on the display without producing glyphs.
8027
8028 OP should be a bit mask including some or all of these bits:
8029 MOVE_TO_X: Stop upon reaching x-position TO_X.
8030 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8031 Regardless of OP's value, stop upon reaching the end of the display line.
8032
8033 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8034 This means, in particular, that TO_X includes window's horizontal
8035 scroll amount.
8036
8037 The return value has several possible values that
8038 say what condition caused the scan to stop:
8039
8040 MOVE_POS_MATCH_OR_ZV
8041 - when TO_POS or ZV was reached.
8042
8043 MOVE_X_REACHED
8044 -when TO_X was reached before TO_POS or ZV were reached.
8045
8046 MOVE_LINE_CONTINUED
8047 - when we reached the end of the display area and the line must
8048 be continued.
8049
8050 MOVE_LINE_TRUNCATED
8051 - when we reached the end of the display area and the line is
8052 truncated.
8053
8054 MOVE_NEWLINE_OR_CR
8055 - when we stopped at a line end, i.e. a newline or a CR and selective
8056 display is on. */
8057
8058 static enum move_it_result
8059 move_it_in_display_line_to (struct it *it,
8060 ptrdiff_t to_charpos, int to_x,
8061 enum move_operation_enum op)
8062 {
8063 enum move_it_result result = MOVE_UNDEFINED;
8064 struct glyph_row *saved_glyph_row;
8065 struct it wrap_it, atpos_it, atx_it, ppos_it;
8066 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8067 void *ppos_data = NULL;
8068 int may_wrap = 0;
8069 enum it_method prev_method = it->method;
8070 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8071 int saw_smaller_pos = prev_pos < to_charpos;
8072
8073 /* Don't produce glyphs in produce_glyphs. */
8074 saved_glyph_row = it->glyph_row;
8075 it->glyph_row = NULL;
8076
8077 /* Use wrap_it to save a copy of IT wherever a word wrap could
8078 occur. Use atpos_it to save a copy of IT at the desired buffer
8079 position, if found, so that we can scan ahead and check if the
8080 word later overshoots the window edge. Use atx_it similarly, for
8081 pixel positions. */
8082 wrap_it.sp = -1;
8083 atpos_it.sp = -1;
8084 atx_it.sp = -1;
8085
8086 /* Use ppos_it under bidi reordering to save a copy of IT for the
8087 position > CHARPOS that is the closest to CHARPOS. We restore
8088 that position in IT when we have scanned the entire display line
8089 without finding a match for CHARPOS and all the character
8090 positions are greater than CHARPOS. */
8091 if (it->bidi_p)
8092 {
8093 SAVE_IT (ppos_it, *it, ppos_data);
8094 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8095 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8096 SAVE_IT (ppos_it, *it, ppos_data);
8097 }
8098
8099 #define BUFFER_POS_REACHED_P() \
8100 ((op & MOVE_TO_POS) != 0 \
8101 && BUFFERP (it->object) \
8102 && (IT_CHARPOS (*it) == to_charpos \
8103 || ((!it->bidi_p \
8104 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8105 && IT_CHARPOS (*it) > to_charpos) \
8106 || (it->what == IT_COMPOSITION \
8107 && ((IT_CHARPOS (*it) > to_charpos \
8108 && to_charpos >= it->cmp_it.charpos) \
8109 || (IT_CHARPOS (*it) < to_charpos \
8110 && to_charpos <= it->cmp_it.charpos)))) \
8111 && (it->method == GET_FROM_BUFFER \
8112 || (it->method == GET_FROM_DISPLAY_VECTOR \
8113 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8114
8115 /* If there's a line-/wrap-prefix, handle it. */
8116 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8117 && it->current_y < it->last_visible_y)
8118 handle_line_prefix (it);
8119
8120 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8121 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8122
8123 while (1)
8124 {
8125 int x, i, ascent = 0, descent = 0;
8126
8127 /* Utility macro to reset an iterator with x, ascent, and descent. */
8128 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8129 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8130 (IT)->max_descent = descent)
8131
8132 /* Stop if we move beyond TO_CHARPOS (after an image or a
8133 display string or stretch glyph). */
8134 if ((op & MOVE_TO_POS) != 0
8135 && BUFFERP (it->object)
8136 && it->method == GET_FROM_BUFFER
8137 && (((!it->bidi_p
8138 /* When the iterator is at base embedding level, we
8139 are guaranteed that characters are delivered for
8140 display in strictly increasing order of their
8141 buffer positions. */
8142 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8143 && IT_CHARPOS (*it) > to_charpos)
8144 || (it->bidi_p
8145 && (prev_method == GET_FROM_IMAGE
8146 || prev_method == GET_FROM_STRETCH
8147 || prev_method == GET_FROM_STRING)
8148 /* Passed TO_CHARPOS from left to right. */
8149 && ((prev_pos < to_charpos
8150 && IT_CHARPOS (*it) > to_charpos)
8151 /* Passed TO_CHARPOS from right to left. */
8152 || (prev_pos > to_charpos
8153 && IT_CHARPOS (*it) < to_charpos)))))
8154 {
8155 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8156 {
8157 result = MOVE_POS_MATCH_OR_ZV;
8158 break;
8159 }
8160 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8161 /* If wrap_it is valid, the current position might be in a
8162 word that is wrapped. So, save the iterator in
8163 atpos_it and continue to see if wrapping happens. */
8164 SAVE_IT (atpos_it, *it, atpos_data);
8165 }
8166
8167 /* Stop when ZV reached.
8168 We used to stop here when TO_CHARPOS reached as well, but that is
8169 too soon if this glyph does not fit on this line. So we handle it
8170 explicitly below. */
8171 if (!get_next_display_element (it))
8172 {
8173 result = MOVE_POS_MATCH_OR_ZV;
8174 break;
8175 }
8176
8177 if (it->line_wrap == TRUNCATE)
8178 {
8179 if (BUFFER_POS_REACHED_P ())
8180 {
8181 result = MOVE_POS_MATCH_OR_ZV;
8182 break;
8183 }
8184 }
8185 else
8186 {
8187 if (it->line_wrap == WORD_WRAP)
8188 {
8189 if (IT_DISPLAYING_WHITESPACE (it))
8190 may_wrap = 1;
8191 else if (may_wrap)
8192 {
8193 /* We have reached a glyph that follows one or more
8194 whitespace characters. If the position is
8195 already found, we are done. */
8196 if (atpos_it.sp >= 0)
8197 {
8198 RESTORE_IT (it, &atpos_it, atpos_data);
8199 result = MOVE_POS_MATCH_OR_ZV;
8200 goto done;
8201 }
8202 if (atx_it.sp >= 0)
8203 {
8204 RESTORE_IT (it, &atx_it, atx_data);
8205 result = MOVE_X_REACHED;
8206 goto done;
8207 }
8208 /* Otherwise, we can wrap here. */
8209 SAVE_IT (wrap_it, *it, wrap_data);
8210 may_wrap = 0;
8211 }
8212 }
8213 }
8214
8215 /* Remember the line height for the current line, in case
8216 the next element doesn't fit on the line. */
8217 ascent = it->max_ascent;
8218 descent = it->max_descent;
8219
8220 /* The call to produce_glyphs will get the metrics of the
8221 display element IT is loaded with. Record the x-position
8222 before this display element, in case it doesn't fit on the
8223 line. */
8224 x = it->current_x;
8225
8226 PRODUCE_GLYPHS (it);
8227
8228 if (it->area != TEXT_AREA)
8229 {
8230 prev_method = it->method;
8231 if (it->method == GET_FROM_BUFFER)
8232 prev_pos = IT_CHARPOS (*it);
8233 set_iterator_to_next (it, 1);
8234 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8235 SET_TEXT_POS (this_line_min_pos,
8236 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8237 if (it->bidi_p
8238 && (op & MOVE_TO_POS)
8239 && IT_CHARPOS (*it) > to_charpos
8240 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8241 SAVE_IT (ppos_it, *it, ppos_data);
8242 continue;
8243 }
8244
8245 /* The number of glyphs we get back in IT->nglyphs will normally
8246 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8247 character on a terminal frame, or (iii) a line end. For the
8248 second case, IT->nglyphs - 1 padding glyphs will be present.
8249 (On X frames, there is only one glyph produced for a
8250 composite character.)
8251
8252 The behavior implemented below means, for continuation lines,
8253 that as many spaces of a TAB as fit on the current line are
8254 displayed there. For terminal frames, as many glyphs of a
8255 multi-glyph character are displayed in the current line, too.
8256 This is what the old redisplay code did, and we keep it that
8257 way. Under X, the whole shape of a complex character must
8258 fit on the line or it will be completely displayed in the
8259 next line.
8260
8261 Note that both for tabs and padding glyphs, all glyphs have
8262 the same width. */
8263 if (it->nglyphs)
8264 {
8265 /* More than one glyph or glyph doesn't fit on line. All
8266 glyphs have the same width. */
8267 int single_glyph_width = it->pixel_width / it->nglyphs;
8268 int new_x;
8269 int x_before_this_char = x;
8270 int hpos_before_this_char = it->hpos;
8271
8272 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8273 {
8274 new_x = x + single_glyph_width;
8275
8276 /* We want to leave anything reaching TO_X to the caller. */
8277 if ((op & MOVE_TO_X) && new_x > to_x)
8278 {
8279 if (BUFFER_POS_REACHED_P ())
8280 {
8281 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8282 goto buffer_pos_reached;
8283 if (atpos_it.sp < 0)
8284 {
8285 SAVE_IT (atpos_it, *it, atpos_data);
8286 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8287 }
8288 }
8289 else
8290 {
8291 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8292 {
8293 it->current_x = x;
8294 result = MOVE_X_REACHED;
8295 break;
8296 }
8297 if (atx_it.sp < 0)
8298 {
8299 SAVE_IT (atx_it, *it, atx_data);
8300 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8301 }
8302 }
8303 }
8304
8305 if (/* Lines are continued. */
8306 it->line_wrap != TRUNCATE
8307 && (/* And glyph doesn't fit on the line. */
8308 new_x > it->last_visible_x
8309 /* Or it fits exactly and we're on a window
8310 system frame. */
8311 || (new_x == it->last_visible_x
8312 && FRAME_WINDOW_P (it->f))))
8313 {
8314 if (/* IT->hpos == 0 means the very first glyph
8315 doesn't fit on the line, e.g. a wide image. */
8316 it->hpos == 0
8317 || (new_x == it->last_visible_x
8318 && FRAME_WINDOW_P (it->f)))
8319 {
8320 ++it->hpos;
8321 it->current_x = new_x;
8322
8323 /* The character's last glyph just barely fits
8324 in this row. */
8325 if (i == it->nglyphs - 1)
8326 {
8327 /* If this is the destination position,
8328 return a position *before* it in this row,
8329 now that we know it fits in this row. */
8330 if (BUFFER_POS_REACHED_P ())
8331 {
8332 if (it->line_wrap != WORD_WRAP
8333 || wrap_it.sp < 0)
8334 {
8335 it->hpos = hpos_before_this_char;
8336 it->current_x = x_before_this_char;
8337 result = MOVE_POS_MATCH_OR_ZV;
8338 break;
8339 }
8340 if (it->line_wrap == WORD_WRAP
8341 && atpos_it.sp < 0)
8342 {
8343 SAVE_IT (atpos_it, *it, atpos_data);
8344 atpos_it.current_x = x_before_this_char;
8345 atpos_it.hpos = hpos_before_this_char;
8346 }
8347 }
8348
8349 prev_method = it->method;
8350 if (it->method == GET_FROM_BUFFER)
8351 prev_pos = IT_CHARPOS (*it);
8352 set_iterator_to_next (it, 1);
8353 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8354 SET_TEXT_POS (this_line_min_pos,
8355 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8356 /* On graphical terminals, newlines may
8357 "overflow" into the fringe if
8358 overflow-newline-into-fringe is non-nil.
8359 On text terminals, newlines may overflow
8360 into the last glyph on the display
8361 line.*/
8362 if (!FRAME_WINDOW_P (it->f)
8363 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8364 {
8365 if (!get_next_display_element (it))
8366 {
8367 result = MOVE_POS_MATCH_OR_ZV;
8368 break;
8369 }
8370 if (BUFFER_POS_REACHED_P ())
8371 {
8372 if (ITERATOR_AT_END_OF_LINE_P (it))
8373 result = MOVE_POS_MATCH_OR_ZV;
8374 else
8375 result = MOVE_LINE_CONTINUED;
8376 break;
8377 }
8378 if (ITERATOR_AT_END_OF_LINE_P (it))
8379 {
8380 result = MOVE_NEWLINE_OR_CR;
8381 break;
8382 }
8383 }
8384 }
8385 }
8386 else
8387 IT_RESET_X_ASCENT_DESCENT (it);
8388
8389 if (wrap_it.sp >= 0)
8390 {
8391 RESTORE_IT (it, &wrap_it, wrap_data);
8392 atpos_it.sp = -1;
8393 atx_it.sp = -1;
8394 }
8395
8396 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8397 IT_CHARPOS (*it)));
8398 result = MOVE_LINE_CONTINUED;
8399 break;
8400 }
8401
8402 if (BUFFER_POS_REACHED_P ())
8403 {
8404 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8405 goto buffer_pos_reached;
8406 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8407 {
8408 SAVE_IT (atpos_it, *it, atpos_data);
8409 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8410 }
8411 }
8412
8413 if (new_x > it->first_visible_x)
8414 {
8415 /* Glyph is visible. Increment number of glyphs that
8416 would be displayed. */
8417 ++it->hpos;
8418 }
8419 }
8420
8421 if (result != MOVE_UNDEFINED)
8422 break;
8423 }
8424 else if (BUFFER_POS_REACHED_P ())
8425 {
8426 buffer_pos_reached:
8427 IT_RESET_X_ASCENT_DESCENT (it);
8428 result = MOVE_POS_MATCH_OR_ZV;
8429 break;
8430 }
8431 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8432 {
8433 /* Stop when TO_X specified and reached. This check is
8434 necessary here because of lines consisting of a line end,
8435 only. The line end will not produce any glyphs and we
8436 would never get MOVE_X_REACHED. */
8437 eassert (it->nglyphs == 0);
8438 result = MOVE_X_REACHED;
8439 break;
8440 }
8441
8442 /* Is this a line end? If yes, we're done. */
8443 if (ITERATOR_AT_END_OF_LINE_P (it))
8444 {
8445 /* If we are past TO_CHARPOS, but never saw any character
8446 positions smaller than TO_CHARPOS, return
8447 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8448 did. */
8449 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8450 {
8451 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8452 {
8453 if (IT_CHARPOS (ppos_it) < ZV)
8454 {
8455 RESTORE_IT (it, &ppos_it, ppos_data);
8456 result = MOVE_POS_MATCH_OR_ZV;
8457 }
8458 else
8459 goto buffer_pos_reached;
8460 }
8461 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8462 && IT_CHARPOS (*it) > to_charpos)
8463 goto buffer_pos_reached;
8464 else
8465 result = MOVE_NEWLINE_OR_CR;
8466 }
8467 else
8468 result = MOVE_NEWLINE_OR_CR;
8469 break;
8470 }
8471
8472 prev_method = it->method;
8473 if (it->method == GET_FROM_BUFFER)
8474 prev_pos = IT_CHARPOS (*it);
8475 /* The current display element has been consumed. Advance
8476 to the next. */
8477 set_iterator_to_next (it, 1);
8478 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8479 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8480 if (IT_CHARPOS (*it) < to_charpos)
8481 saw_smaller_pos = 1;
8482 if (it->bidi_p
8483 && (op & MOVE_TO_POS)
8484 && IT_CHARPOS (*it) >= to_charpos
8485 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8486 SAVE_IT (ppos_it, *it, ppos_data);
8487
8488 /* Stop if lines are truncated and IT's current x-position is
8489 past the right edge of the window now. */
8490 if (it->line_wrap == TRUNCATE
8491 && it->current_x >= it->last_visible_x)
8492 {
8493 if (!FRAME_WINDOW_P (it->f)
8494 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8495 {
8496 int at_eob_p = 0;
8497
8498 if ((at_eob_p = !get_next_display_element (it))
8499 || BUFFER_POS_REACHED_P ()
8500 /* If we are past TO_CHARPOS, but never saw any
8501 character positions smaller than TO_CHARPOS,
8502 return MOVE_POS_MATCH_OR_ZV, like the
8503 unidirectional display did. */
8504 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8505 && !saw_smaller_pos
8506 && IT_CHARPOS (*it) > to_charpos))
8507 {
8508 if (it->bidi_p
8509 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8510 RESTORE_IT (it, &ppos_it, ppos_data);
8511 result = MOVE_POS_MATCH_OR_ZV;
8512 break;
8513 }
8514 if (ITERATOR_AT_END_OF_LINE_P (it))
8515 {
8516 result = MOVE_NEWLINE_OR_CR;
8517 break;
8518 }
8519 }
8520 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8521 && !saw_smaller_pos
8522 && IT_CHARPOS (*it) > to_charpos)
8523 {
8524 if (IT_CHARPOS (ppos_it) < ZV)
8525 RESTORE_IT (it, &ppos_it, ppos_data);
8526 result = MOVE_POS_MATCH_OR_ZV;
8527 break;
8528 }
8529 result = MOVE_LINE_TRUNCATED;
8530 break;
8531 }
8532 #undef IT_RESET_X_ASCENT_DESCENT
8533 }
8534
8535 #undef BUFFER_POS_REACHED_P
8536
8537 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8538 restore the saved iterator. */
8539 if (atpos_it.sp >= 0)
8540 RESTORE_IT (it, &atpos_it, atpos_data);
8541 else if (atx_it.sp >= 0)
8542 RESTORE_IT (it, &atx_it, atx_data);
8543
8544 done:
8545
8546 if (atpos_data)
8547 bidi_unshelve_cache (atpos_data, 1);
8548 if (atx_data)
8549 bidi_unshelve_cache (atx_data, 1);
8550 if (wrap_data)
8551 bidi_unshelve_cache (wrap_data, 1);
8552 if (ppos_data)
8553 bidi_unshelve_cache (ppos_data, 1);
8554
8555 /* Restore the iterator settings altered at the beginning of this
8556 function. */
8557 it->glyph_row = saved_glyph_row;
8558 return result;
8559 }
8560
8561 /* For external use. */
8562 void
8563 move_it_in_display_line (struct it *it,
8564 ptrdiff_t to_charpos, int to_x,
8565 enum move_operation_enum op)
8566 {
8567 if (it->line_wrap == WORD_WRAP
8568 && (op & MOVE_TO_X))
8569 {
8570 struct it save_it;
8571 void *save_data = NULL;
8572 int skip;
8573
8574 SAVE_IT (save_it, *it, save_data);
8575 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8576 /* When word-wrap is on, TO_X may lie past the end
8577 of a wrapped line. Then it->current is the
8578 character on the next line, so backtrack to the
8579 space before the wrap point. */
8580 if (skip == MOVE_LINE_CONTINUED)
8581 {
8582 int prev_x = max (it->current_x - 1, 0);
8583 RESTORE_IT (it, &save_it, save_data);
8584 move_it_in_display_line_to
8585 (it, -1, prev_x, MOVE_TO_X);
8586 }
8587 else
8588 bidi_unshelve_cache (save_data, 1);
8589 }
8590 else
8591 move_it_in_display_line_to (it, to_charpos, to_x, op);
8592 }
8593
8594
8595 /* Move IT forward until it satisfies one or more of the criteria in
8596 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8597
8598 OP is a bit-mask that specifies where to stop, and in particular,
8599 which of those four position arguments makes a difference. See the
8600 description of enum move_operation_enum.
8601
8602 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8603 screen line, this function will set IT to the next position that is
8604 displayed to the right of TO_CHARPOS on the screen. */
8605
8606 void
8607 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8608 {
8609 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8610 int line_height, line_start_x = 0, reached = 0;
8611 void *backup_data = NULL;
8612
8613 for (;;)
8614 {
8615 if (op & MOVE_TO_VPOS)
8616 {
8617 /* If no TO_CHARPOS and no TO_X specified, stop at the
8618 start of the line TO_VPOS. */
8619 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8620 {
8621 if (it->vpos == to_vpos)
8622 {
8623 reached = 1;
8624 break;
8625 }
8626 else
8627 skip = move_it_in_display_line_to (it, -1, -1, 0);
8628 }
8629 else
8630 {
8631 /* TO_VPOS >= 0 means stop at TO_X in the line at
8632 TO_VPOS, or at TO_POS, whichever comes first. */
8633 if (it->vpos == to_vpos)
8634 {
8635 reached = 2;
8636 break;
8637 }
8638
8639 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8640
8641 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8642 {
8643 reached = 3;
8644 break;
8645 }
8646 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8647 {
8648 /* We have reached TO_X but not in the line we want. */
8649 skip = move_it_in_display_line_to (it, to_charpos,
8650 -1, MOVE_TO_POS);
8651 if (skip == MOVE_POS_MATCH_OR_ZV)
8652 {
8653 reached = 4;
8654 break;
8655 }
8656 }
8657 }
8658 }
8659 else if (op & MOVE_TO_Y)
8660 {
8661 struct it it_backup;
8662
8663 if (it->line_wrap == WORD_WRAP)
8664 SAVE_IT (it_backup, *it, backup_data);
8665
8666 /* TO_Y specified means stop at TO_X in the line containing
8667 TO_Y---or at TO_CHARPOS if this is reached first. The
8668 problem is that we can't really tell whether the line
8669 contains TO_Y before we have completely scanned it, and
8670 this may skip past TO_X. What we do is to first scan to
8671 TO_X.
8672
8673 If TO_X is not specified, use a TO_X of zero. The reason
8674 is to make the outcome of this function more predictable.
8675 If we didn't use TO_X == 0, we would stop at the end of
8676 the line which is probably not what a caller would expect
8677 to happen. */
8678 skip = move_it_in_display_line_to
8679 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8680 (MOVE_TO_X | (op & MOVE_TO_POS)));
8681
8682 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8683 if (skip == MOVE_POS_MATCH_OR_ZV)
8684 reached = 5;
8685 else if (skip == MOVE_X_REACHED)
8686 {
8687 /* If TO_X was reached, we want to know whether TO_Y is
8688 in the line. We know this is the case if the already
8689 scanned glyphs make the line tall enough. Otherwise,
8690 we must check by scanning the rest of the line. */
8691 line_height = it->max_ascent + it->max_descent;
8692 if (to_y >= it->current_y
8693 && to_y < it->current_y + line_height)
8694 {
8695 reached = 6;
8696 break;
8697 }
8698 SAVE_IT (it_backup, *it, backup_data);
8699 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8700 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8701 op & MOVE_TO_POS);
8702 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8703 line_height = it->max_ascent + it->max_descent;
8704 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8705
8706 if (to_y >= it->current_y
8707 && to_y < it->current_y + line_height)
8708 {
8709 /* If TO_Y is in this line and TO_X was reached
8710 above, we scanned too far. We have to restore
8711 IT's settings to the ones before skipping. But
8712 keep the more accurate values of max_ascent and
8713 max_descent we've found while skipping the rest
8714 of the line, for the sake of callers, such as
8715 pos_visible_p, that need to know the line
8716 height. */
8717 int max_ascent = it->max_ascent;
8718 int max_descent = it->max_descent;
8719
8720 RESTORE_IT (it, &it_backup, backup_data);
8721 it->max_ascent = max_ascent;
8722 it->max_descent = max_descent;
8723 reached = 6;
8724 }
8725 else
8726 {
8727 skip = skip2;
8728 if (skip == MOVE_POS_MATCH_OR_ZV)
8729 reached = 7;
8730 }
8731 }
8732 else
8733 {
8734 /* Check whether TO_Y is in this line. */
8735 line_height = it->max_ascent + it->max_descent;
8736 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8737
8738 if (to_y >= it->current_y
8739 && to_y < it->current_y + line_height)
8740 {
8741 /* When word-wrap is on, TO_X may lie past the end
8742 of a wrapped line. Then it->current is the
8743 character on the next line, so backtrack to the
8744 space before the wrap point. */
8745 if (skip == MOVE_LINE_CONTINUED
8746 && it->line_wrap == WORD_WRAP)
8747 {
8748 int prev_x = max (it->current_x - 1, 0);
8749 RESTORE_IT (it, &it_backup, backup_data);
8750 skip = move_it_in_display_line_to
8751 (it, -1, prev_x, MOVE_TO_X);
8752 }
8753 reached = 6;
8754 }
8755 }
8756
8757 if (reached)
8758 break;
8759 }
8760 else if (BUFFERP (it->object)
8761 && (it->method == GET_FROM_BUFFER
8762 || it->method == GET_FROM_STRETCH)
8763 && IT_CHARPOS (*it) >= to_charpos
8764 /* Under bidi iteration, a call to set_iterator_to_next
8765 can scan far beyond to_charpos if the initial
8766 portion of the next line needs to be reordered. In
8767 that case, give move_it_in_display_line_to another
8768 chance below. */
8769 && !(it->bidi_p
8770 && it->bidi_it.scan_dir == -1))
8771 skip = MOVE_POS_MATCH_OR_ZV;
8772 else
8773 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8774
8775 switch (skip)
8776 {
8777 case MOVE_POS_MATCH_OR_ZV:
8778 reached = 8;
8779 goto out;
8780
8781 case MOVE_NEWLINE_OR_CR:
8782 set_iterator_to_next (it, 1);
8783 it->continuation_lines_width = 0;
8784 break;
8785
8786 case MOVE_LINE_TRUNCATED:
8787 it->continuation_lines_width = 0;
8788 reseat_at_next_visible_line_start (it, 0);
8789 if ((op & MOVE_TO_POS) != 0
8790 && IT_CHARPOS (*it) > to_charpos)
8791 {
8792 reached = 9;
8793 goto out;
8794 }
8795 break;
8796
8797 case MOVE_LINE_CONTINUED:
8798 /* For continued lines ending in a tab, some of the glyphs
8799 associated with the tab are displayed on the current
8800 line. Since it->current_x does not include these glyphs,
8801 we use it->last_visible_x instead. */
8802 if (it->c == '\t')
8803 {
8804 it->continuation_lines_width += it->last_visible_x;
8805 /* When moving by vpos, ensure that the iterator really
8806 advances to the next line (bug#847, bug#969). Fixme:
8807 do we need to do this in other circumstances? */
8808 if (it->current_x != it->last_visible_x
8809 && (op & MOVE_TO_VPOS)
8810 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8811 {
8812 line_start_x = it->current_x + it->pixel_width
8813 - it->last_visible_x;
8814 set_iterator_to_next (it, 0);
8815 }
8816 }
8817 else
8818 it->continuation_lines_width += it->current_x;
8819 break;
8820
8821 default:
8822 abort ();
8823 }
8824
8825 /* Reset/increment for the next run. */
8826 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8827 it->current_x = line_start_x;
8828 line_start_x = 0;
8829 it->hpos = 0;
8830 it->current_y += it->max_ascent + it->max_descent;
8831 ++it->vpos;
8832 last_height = it->max_ascent + it->max_descent;
8833 last_max_ascent = it->max_ascent;
8834 it->max_ascent = it->max_descent = 0;
8835 }
8836
8837 out:
8838
8839 /* On text terminals, we may stop at the end of a line in the middle
8840 of a multi-character glyph. If the glyph itself is continued,
8841 i.e. it is actually displayed on the next line, don't treat this
8842 stopping point as valid; move to the next line instead (unless
8843 that brings us offscreen). */
8844 if (!FRAME_WINDOW_P (it->f)
8845 && op & MOVE_TO_POS
8846 && IT_CHARPOS (*it) == to_charpos
8847 && it->what == IT_CHARACTER
8848 && it->nglyphs > 1
8849 && it->line_wrap == WINDOW_WRAP
8850 && it->current_x == it->last_visible_x - 1
8851 && it->c != '\n'
8852 && it->c != '\t'
8853 && it->vpos < XFASTINT (it->w->window_end_vpos))
8854 {
8855 it->continuation_lines_width += it->current_x;
8856 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8857 it->current_y += it->max_ascent + it->max_descent;
8858 ++it->vpos;
8859 last_height = it->max_ascent + it->max_descent;
8860 last_max_ascent = it->max_ascent;
8861 }
8862
8863 if (backup_data)
8864 bidi_unshelve_cache (backup_data, 1);
8865
8866 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8867 }
8868
8869
8870 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8871
8872 If DY > 0, move IT backward at least that many pixels. DY = 0
8873 means move IT backward to the preceding line start or BEGV. This
8874 function may move over more than DY pixels if IT->current_y - DY
8875 ends up in the middle of a line; in this case IT->current_y will be
8876 set to the top of the line moved to. */
8877
8878 void
8879 move_it_vertically_backward (struct it *it, int dy)
8880 {
8881 int nlines, h;
8882 struct it it2, it3;
8883 void *it2data = NULL, *it3data = NULL;
8884 ptrdiff_t start_pos;
8885
8886 move_further_back:
8887 eassert (dy >= 0);
8888
8889 start_pos = IT_CHARPOS (*it);
8890
8891 /* Estimate how many newlines we must move back. */
8892 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8893
8894 /* Set the iterator's position that many lines back. */
8895 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8896 back_to_previous_visible_line_start (it);
8897
8898 /* Reseat the iterator here. When moving backward, we don't want
8899 reseat to skip forward over invisible text, set up the iterator
8900 to deliver from overlay strings at the new position etc. So,
8901 use reseat_1 here. */
8902 reseat_1 (it, it->current.pos, 1);
8903
8904 /* We are now surely at a line start. */
8905 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8906 reordering is in effect. */
8907 it->continuation_lines_width = 0;
8908
8909 /* Move forward and see what y-distance we moved. First move to the
8910 start of the next line so that we get its height. We need this
8911 height to be able to tell whether we reached the specified
8912 y-distance. */
8913 SAVE_IT (it2, *it, it2data);
8914 it2.max_ascent = it2.max_descent = 0;
8915 do
8916 {
8917 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8918 MOVE_TO_POS | MOVE_TO_VPOS);
8919 }
8920 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8921 /* If we are in a display string which starts at START_POS,
8922 and that display string includes a newline, and we are
8923 right after that newline (i.e. at the beginning of a
8924 display line), exit the loop, because otherwise we will
8925 infloop, since move_it_to will see that it is already at
8926 START_POS and will not move. */
8927 || (it2.method == GET_FROM_STRING
8928 && IT_CHARPOS (it2) == start_pos
8929 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8930 eassert (IT_CHARPOS (*it) >= BEGV);
8931 SAVE_IT (it3, it2, it3data);
8932
8933 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8934 eassert (IT_CHARPOS (*it) >= BEGV);
8935 /* H is the actual vertical distance from the position in *IT
8936 and the starting position. */
8937 h = it2.current_y - it->current_y;
8938 /* NLINES is the distance in number of lines. */
8939 nlines = it2.vpos - it->vpos;
8940
8941 /* Correct IT's y and vpos position
8942 so that they are relative to the starting point. */
8943 it->vpos -= nlines;
8944 it->current_y -= h;
8945
8946 if (dy == 0)
8947 {
8948 /* DY == 0 means move to the start of the screen line. The
8949 value of nlines is > 0 if continuation lines were involved,
8950 or if the original IT position was at start of a line. */
8951 RESTORE_IT (it, it, it2data);
8952 if (nlines > 0)
8953 move_it_by_lines (it, nlines);
8954 /* The above code moves us to some position NLINES down,
8955 usually to its first glyph (leftmost in an L2R line), but
8956 that's not necessarily the start of the line, under bidi
8957 reordering. We want to get to the character position
8958 that is immediately after the newline of the previous
8959 line. */
8960 if (it->bidi_p
8961 && !it->continuation_lines_width
8962 && !STRINGP (it->string)
8963 && IT_CHARPOS (*it) > BEGV
8964 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8965 {
8966 ptrdiff_t nl_pos =
8967 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8968
8969 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8970 }
8971 bidi_unshelve_cache (it3data, 1);
8972 }
8973 else
8974 {
8975 /* The y-position we try to reach, relative to *IT.
8976 Note that H has been subtracted in front of the if-statement. */
8977 int target_y = it->current_y + h - dy;
8978 int y0 = it3.current_y;
8979 int y1;
8980 int line_height;
8981
8982 RESTORE_IT (&it3, &it3, it3data);
8983 y1 = line_bottom_y (&it3);
8984 line_height = y1 - y0;
8985 RESTORE_IT (it, it, it2data);
8986 /* If we did not reach target_y, try to move further backward if
8987 we can. If we moved too far backward, try to move forward. */
8988 if (target_y < it->current_y
8989 /* This is heuristic. In a window that's 3 lines high, with
8990 a line height of 13 pixels each, recentering with point
8991 on the bottom line will try to move -39/2 = 19 pixels
8992 backward. Try to avoid moving into the first line. */
8993 && (it->current_y - target_y
8994 > min (window_box_height (it->w), line_height * 2 / 3))
8995 && IT_CHARPOS (*it) > BEGV)
8996 {
8997 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8998 target_y - it->current_y));
8999 dy = it->current_y - target_y;
9000 goto move_further_back;
9001 }
9002 else if (target_y >= it->current_y + line_height
9003 && IT_CHARPOS (*it) < ZV)
9004 {
9005 /* Should move forward by at least one line, maybe more.
9006
9007 Note: Calling move_it_by_lines can be expensive on
9008 terminal frames, where compute_motion is used (via
9009 vmotion) to do the job, when there are very long lines
9010 and truncate-lines is nil. That's the reason for
9011 treating terminal frames specially here. */
9012
9013 if (!FRAME_WINDOW_P (it->f))
9014 move_it_vertically (it, target_y - (it->current_y + line_height));
9015 else
9016 {
9017 do
9018 {
9019 move_it_by_lines (it, 1);
9020 }
9021 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9022 }
9023 }
9024 }
9025 }
9026
9027
9028 /* Move IT by a specified amount of pixel lines DY. DY negative means
9029 move backwards. DY = 0 means move to start of screen line. At the
9030 end, IT will be on the start of a screen line. */
9031
9032 void
9033 move_it_vertically (struct it *it, int dy)
9034 {
9035 if (dy <= 0)
9036 move_it_vertically_backward (it, -dy);
9037 else
9038 {
9039 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9040 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9041 MOVE_TO_POS | MOVE_TO_Y);
9042 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9043
9044 /* If buffer ends in ZV without a newline, move to the start of
9045 the line to satisfy the post-condition. */
9046 if (IT_CHARPOS (*it) == ZV
9047 && ZV > BEGV
9048 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9049 move_it_by_lines (it, 0);
9050 }
9051 }
9052
9053
9054 /* Move iterator IT past the end of the text line it is in. */
9055
9056 void
9057 move_it_past_eol (struct it *it)
9058 {
9059 enum move_it_result rc;
9060
9061 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9062 if (rc == MOVE_NEWLINE_OR_CR)
9063 set_iterator_to_next (it, 0);
9064 }
9065
9066
9067 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9068 negative means move up. DVPOS == 0 means move to the start of the
9069 screen line.
9070
9071 Optimization idea: If we would know that IT->f doesn't use
9072 a face with proportional font, we could be faster for
9073 truncate-lines nil. */
9074
9075 void
9076 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9077 {
9078
9079 /* The commented-out optimization uses vmotion on terminals. This
9080 gives bad results, because elements like it->what, on which
9081 callers such as pos_visible_p rely, aren't updated. */
9082 /* struct position pos;
9083 if (!FRAME_WINDOW_P (it->f))
9084 {
9085 struct text_pos textpos;
9086
9087 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9088 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9089 reseat (it, textpos, 1);
9090 it->vpos += pos.vpos;
9091 it->current_y += pos.vpos;
9092 }
9093 else */
9094
9095 if (dvpos == 0)
9096 {
9097 /* DVPOS == 0 means move to the start of the screen line. */
9098 move_it_vertically_backward (it, 0);
9099 /* Let next call to line_bottom_y calculate real line height */
9100 last_height = 0;
9101 }
9102 else if (dvpos > 0)
9103 {
9104 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9105 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9106 {
9107 /* Only move to the next buffer position if we ended up in a
9108 string from display property, not in an overlay string
9109 (before-string or after-string). That is because the
9110 latter don't conceal the underlying buffer position, so
9111 we can ask to move the iterator to the exact position we
9112 are interested in. Note that, even if we are already at
9113 IT_CHARPOS (*it), the call below is not a no-op, as it
9114 will detect that we are at the end of the string, pop the
9115 iterator, and compute it->current_x and it->hpos
9116 correctly. */
9117 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9118 -1, -1, -1, MOVE_TO_POS);
9119 }
9120 }
9121 else
9122 {
9123 struct it it2;
9124 void *it2data = NULL;
9125 ptrdiff_t start_charpos, i;
9126
9127 /* Start at the beginning of the screen line containing IT's
9128 position. This may actually move vertically backwards,
9129 in case of overlays, so adjust dvpos accordingly. */
9130 dvpos += it->vpos;
9131 move_it_vertically_backward (it, 0);
9132 dvpos -= it->vpos;
9133
9134 /* Go back -DVPOS visible lines and reseat the iterator there. */
9135 start_charpos = IT_CHARPOS (*it);
9136 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9137 back_to_previous_visible_line_start (it);
9138 reseat (it, it->current.pos, 1);
9139
9140 /* Move further back if we end up in a string or an image. */
9141 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9142 {
9143 /* First try to move to start of display line. */
9144 dvpos += it->vpos;
9145 move_it_vertically_backward (it, 0);
9146 dvpos -= it->vpos;
9147 if (IT_POS_VALID_AFTER_MOVE_P (it))
9148 break;
9149 /* If start of line is still in string or image,
9150 move further back. */
9151 back_to_previous_visible_line_start (it);
9152 reseat (it, it->current.pos, 1);
9153 dvpos--;
9154 }
9155
9156 it->current_x = it->hpos = 0;
9157
9158 /* Above call may have moved too far if continuation lines
9159 are involved. Scan forward and see if it did. */
9160 SAVE_IT (it2, *it, it2data);
9161 it2.vpos = it2.current_y = 0;
9162 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9163 it->vpos -= it2.vpos;
9164 it->current_y -= it2.current_y;
9165 it->current_x = it->hpos = 0;
9166
9167 /* If we moved too far back, move IT some lines forward. */
9168 if (it2.vpos > -dvpos)
9169 {
9170 int delta = it2.vpos + dvpos;
9171
9172 RESTORE_IT (&it2, &it2, it2data);
9173 SAVE_IT (it2, *it, it2data);
9174 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9175 /* Move back again if we got too far ahead. */
9176 if (IT_CHARPOS (*it) >= start_charpos)
9177 RESTORE_IT (it, &it2, it2data);
9178 else
9179 bidi_unshelve_cache (it2data, 1);
9180 }
9181 else
9182 RESTORE_IT (it, it, it2data);
9183 }
9184 }
9185
9186 /* Return 1 if IT points into the middle of a display vector. */
9187
9188 int
9189 in_display_vector_p (struct it *it)
9190 {
9191 return (it->method == GET_FROM_DISPLAY_VECTOR
9192 && it->current.dpvec_index > 0
9193 && it->dpvec + it->current.dpvec_index != it->dpend);
9194 }
9195
9196 \f
9197 /***********************************************************************
9198 Messages
9199 ***********************************************************************/
9200
9201
9202 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9203 to *Messages*. */
9204
9205 void
9206 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9207 {
9208 Lisp_Object args[3];
9209 Lisp_Object msg, fmt;
9210 char *buffer;
9211 ptrdiff_t len;
9212 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9213 USE_SAFE_ALLOCA;
9214
9215 /* Do nothing if called asynchronously. Inserting text into
9216 a buffer may call after-change-functions and alike and
9217 that would means running Lisp asynchronously. */
9218 if (handling_signal)
9219 return;
9220
9221 fmt = msg = Qnil;
9222 GCPRO4 (fmt, msg, arg1, arg2);
9223
9224 args[0] = fmt = build_string (format);
9225 args[1] = arg1;
9226 args[2] = arg2;
9227 msg = Fformat (3, args);
9228
9229 len = SBYTES (msg) + 1;
9230 SAFE_ALLOCA (buffer, char *, len);
9231 memcpy (buffer, SDATA (msg), len);
9232
9233 message_dolog (buffer, len - 1, 1, 0);
9234 SAFE_FREE ();
9235
9236 UNGCPRO;
9237 }
9238
9239
9240 /* Output a newline in the *Messages* buffer if "needs" one. */
9241
9242 void
9243 message_log_maybe_newline (void)
9244 {
9245 if (message_log_need_newline)
9246 message_dolog ("", 0, 1, 0);
9247 }
9248
9249
9250 /* Add a string M of length NBYTES to the message log, optionally
9251 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9252 nonzero, means interpret the contents of M as multibyte. This
9253 function calls low-level routines in order to bypass text property
9254 hooks, etc. which might not be safe to run.
9255
9256 This may GC (insert may run before/after change hooks),
9257 so the buffer M must NOT point to a Lisp string. */
9258
9259 void
9260 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9261 {
9262 const unsigned char *msg = (const unsigned char *) m;
9263
9264 if (!NILP (Vmemory_full))
9265 return;
9266
9267 if (!NILP (Vmessage_log_max))
9268 {
9269 struct buffer *oldbuf;
9270 Lisp_Object oldpoint, oldbegv, oldzv;
9271 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9272 ptrdiff_t point_at_end = 0;
9273 ptrdiff_t zv_at_end = 0;
9274 Lisp_Object old_deactivate_mark, tem;
9275 struct gcpro gcpro1;
9276
9277 old_deactivate_mark = Vdeactivate_mark;
9278 oldbuf = current_buffer;
9279 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9280 BVAR (current_buffer, undo_list) = Qt;
9281
9282 oldpoint = message_dolog_marker1;
9283 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9284 oldbegv = message_dolog_marker2;
9285 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9286 oldzv = message_dolog_marker3;
9287 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9288 GCPRO1 (old_deactivate_mark);
9289
9290 if (PT == Z)
9291 point_at_end = 1;
9292 if (ZV == Z)
9293 zv_at_end = 1;
9294
9295 BEGV = BEG;
9296 BEGV_BYTE = BEG_BYTE;
9297 ZV = Z;
9298 ZV_BYTE = Z_BYTE;
9299 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9300
9301 /* Insert the string--maybe converting multibyte to single byte
9302 or vice versa, so that all the text fits the buffer. */
9303 if (multibyte
9304 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9305 {
9306 ptrdiff_t i;
9307 int c, char_bytes;
9308 char work[1];
9309
9310 /* Convert a multibyte string to single-byte
9311 for the *Message* buffer. */
9312 for (i = 0; i < nbytes; i += char_bytes)
9313 {
9314 c = string_char_and_length (msg + i, &char_bytes);
9315 work[0] = (ASCII_CHAR_P (c)
9316 ? c
9317 : multibyte_char_to_unibyte (c));
9318 insert_1_both (work, 1, 1, 1, 0, 0);
9319 }
9320 }
9321 else if (! multibyte
9322 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9323 {
9324 ptrdiff_t i;
9325 int c, char_bytes;
9326 unsigned char str[MAX_MULTIBYTE_LENGTH];
9327 /* Convert a single-byte string to multibyte
9328 for the *Message* buffer. */
9329 for (i = 0; i < nbytes; i++)
9330 {
9331 c = msg[i];
9332 MAKE_CHAR_MULTIBYTE (c);
9333 char_bytes = CHAR_STRING (c, str);
9334 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9335 }
9336 }
9337 else if (nbytes)
9338 insert_1 (m, nbytes, 1, 0, 0);
9339
9340 if (nlflag)
9341 {
9342 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9343 printmax_t dups;
9344 insert_1 ("\n", 1, 1, 0, 0);
9345
9346 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9347 this_bol = PT;
9348 this_bol_byte = PT_BYTE;
9349
9350 /* See if this line duplicates the previous one.
9351 If so, combine duplicates. */
9352 if (this_bol > BEG)
9353 {
9354 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9355 prev_bol = PT;
9356 prev_bol_byte = PT_BYTE;
9357
9358 dups = message_log_check_duplicate (prev_bol_byte,
9359 this_bol_byte);
9360 if (dups)
9361 {
9362 del_range_both (prev_bol, prev_bol_byte,
9363 this_bol, this_bol_byte, 0);
9364 if (dups > 1)
9365 {
9366 char dupstr[sizeof " [ times]"
9367 + INT_STRLEN_BOUND (printmax_t)];
9368
9369 /* If you change this format, don't forget to also
9370 change message_log_check_duplicate. */
9371 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9372 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9373 insert_1 (dupstr, duplen, 1, 0, 1);
9374 }
9375 }
9376 }
9377
9378 /* If we have more than the desired maximum number of lines
9379 in the *Messages* buffer now, delete the oldest ones.
9380 This is safe because we don't have undo in this buffer. */
9381
9382 if (NATNUMP (Vmessage_log_max))
9383 {
9384 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9385 -XFASTINT (Vmessage_log_max) - 1, 0);
9386 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9387 }
9388 }
9389 BEGV = XMARKER (oldbegv)->charpos;
9390 BEGV_BYTE = marker_byte_position (oldbegv);
9391
9392 if (zv_at_end)
9393 {
9394 ZV = Z;
9395 ZV_BYTE = Z_BYTE;
9396 }
9397 else
9398 {
9399 ZV = XMARKER (oldzv)->charpos;
9400 ZV_BYTE = marker_byte_position (oldzv);
9401 }
9402
9403 if (point_at_end)
9404 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9405 else
9406 /* We can't do Fgoto_char (oldpoint) because it will run some
9407 Lisp code. */
9408 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9409 XMARKER (oldpoint)->bytepos);
9410
9411 UNGCPRO;
9412 unchain_marker (XMARKER (oldpoint));
9413 unchain_marker (XMARKER (oldbegv));
9414 unchain_marker (XMARKER (oldzv));
9415
9416 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9417 set_buffer_internal (oldbuf);
9418 if (NILP (tem))
9419 windows_or_buffers_changed = old_windows_or_buffers_changed;
9420 message_log_need_newline = !nlflag;
9421 Vdeactivate_mark = old_deactivate_mark;
9422 }
9423 }
9424
9425
9426 /* We are at the end of the buffer after just having inserted a newline.
9427 (Note: We depend on the fact we won't be crossing the gap.)
9428 Check to see if the most recent message looks a lot like the previous one.
9429 Return 0 if different, 1 if the new one should just replace it, or a
9430 value N > 1 if we should also append " [N times]". */
9431
9432 static intmax_t
9433 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9434 {
9435 ptrdiff_t i;
9436 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9437 int seen_dots = 0;
9438 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9439 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9440
9441 for (i = 0; i < len; i++)
9442 {
9443 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9444 seen_dots = 1;
9445 if (p1[i] != p2[i])
9446 return seen_dots;
9447 }
9448 p1 += len;
9449 if (*p1 == '\n')
9450 return 2;
9451 if (*p1++ == ' ' && *p1++ == '[')
9452 {
9453 char *pend;
9454 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9455 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9456 return n+1;
9457 }
9458 return 0;
9459 }
9460 \f
9461
9462 /* Display an echo area message M with a specified length of NBYTES
9463 bytes. The string may include null characters. If M is 0, clear
9464 out any existing message, and let the mini-buffer text show
9465 through.
9466
9467 This may GC, so the buffer M must NOT point to a Lisp string. */
9468
9469 void
9470 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9471 {
9472 /* First flush out any partial line written with print. */
9473 message_log_maybe_newline ();
9474 if (m)
9475 message_dolog (m, nbytes, 1, multibyte);
9476 message2_nolog (m, nbytes, multibyte);
9477 }
9478
9479
9480 /* The non-logging counterpart of message2. */
9481
9482 void
9483 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9484 {
9485 struct frame *sf = SELECTED_FRAME ();
9486 message_enable_multibyte = multibyte;
9487
9488 if (FRAME_INITIAL_P (sf))
9489 {
9490 if (noninteractive_need_newline)
9491 putc ('\n', stderr);
9492 noninteractive_need_newline = 0;
9493 if (m)
9494 fwrite (m, nbytes, 1, stderr);
9495 if (cursor_in_echo_area == 0)
9496 fprintf (stderr, "\n");
9497 fflush (stderr);
9498 }
9499 /* A null message buffer means that the frame hasn't really been
9500 initialized yet. Error messages get reported properly by
9501 cmd_error, so this must be just an informative message; toss it. */
9502 else if (INTERACTIVE
9503 && sf->glyphs_initialized_p
9504 && FRAME_MESSAGE_BUF (sf))
9505 {
9506 Lisp_Object mini_window;
9507 struct frame *f;
9508
9509 /* Get the frame containing the mini-buffer
9510 that the selected frame is using. */
9511 mini_window = FRAME_MINIBUF_WINDOW (sf);
9512 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9513
9514 FRAME_SAMPLE_VISIBILITY (f);
9515 if (FRAME_VISIBLE_P (sf)
9516 && ! FRAME_VISIBLE_P (f))
9517 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9518
9519 if (m)
9520 {
9521 set_message (m, Qnil, nbytes, multibyte);
9522 if (minibuffer_auto_raise)
9523 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9524 }
9525 else
9526 clear_message (1, 1);
9527
9528 do_pending_window_change (0);
9529 echo_area_display (1);
9530 do_pending_window_change (0);
9531 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9532 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9533 }
9534 }
9535
9536
9537 /* Display an echo area message M with a specified length of NBYTES
9538 bytes. The string may include null characters. If M is not a
9539 string, clear out any existing message, and let the mini-buffer
9540 text show through.
9541
9542 This function cancels echoing. */
9543
9544 void
9545 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9546 {
9547 struct gcpro gcpro1;
9548
9549 GCPRO1 (m);
9550 clear_message (1,1);
9551 cancel_echoing ();
9552
9553 /* First flush out any partial line written with print. */
9554 message_log_maybe_newline ();
9555 if (STRINGP (m))
9556 {
9557 char *buffer;
9558 USE_SAFE_ALLOCA;
9559
9560 SAFE_ALLOCA (buffer, char *, nbytes);
9561 memcpy (buffer, SDATA (m), nbytes);
9562 message_dolog (buffer, nbytes, 1, multibyte);
9563 SAFE_FREE ();
9564 }
9565 message3_nolog (m, nbytes, multibyte);
9566
9567 UNGCPRO;
9568 }
9569
9570
9571 /* The non-logging version of message3.
9572 This does not cancel echoing, because it is used for echoing.
9573 Perhaps we need to make a separate function for echoing
9574 and make this cancel echoing. */
9575
9576 void
9577 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9578 {
9579 struct frame *sf = SELECTED_FRAME ();
9580 message_enable_multibyte = multibyte;
9581
9582 if (FRAME_INITIAL_P (sf))
9583 {
9584 if (noninteractive_need_newline)
9585 putc ('\n', stderr);
9586 noninteractive_need_newline = 0;
9587 if (STRINGP (m))
9588 fwrite (SDATA (m), nbytes, 1, stderr);
9589 if (cursor_in_echo_area == 0)
9590 fprintf (stderr, "\n");
9591 fflush (stderr);
9592 }
9593 /* A null message buffer means that the frame hasn't really been
9594 initialized yet. Error messages get reported properly by
9595 cmd_error, so this must be just an informative message; toss it. */
9596 else if (INTERACTIVE
9597 && sf->glyphs_initialized_p
9598 && FRAME_MESSAGE_BUF (sf))
9599 {
9600 Lisp_Object mini_window;
9601 Lisp_Object frame;
9602 struct frame *f;
9603
9604 /* Get the frame containing the mini-buffer
9605 that the selected frame is using. */
9606 mini_window = FRAME_MINIBUF_WINDOW (sf);
9607 frame = XWINDOW (mini_window)->frame;
9608 f = XFRAME (frame);
9609
9610 FRAME_SAMPLE_VISIBILITY (f);
9611 if (FRAME_VISIBLE_P (sf)
9612 && !FRAME_VISIBLE_P (f))
9613 Fmake_frame_visible (frame);
9614
9615 if (STRINGP (m) && SCHARS (m) > 0)
9616 {
9617 set_message (NULL, m, nbytes, multibyte);
9618 if (minibuffer_auto_raise)
9619 Fraise_frame (frame);
9620 /* Assume we are not echoing.
9621 (If we are, echo_now will override this.) */
9622 echo_message_buffer = Qnil;
9623 }
9624 else
9625 clear_message (1, 1);
9626
9627 do_pending_window_change (0);
9628 echo_area_display (1);
9629 do_pending_window_change (0);
9630 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9631 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9632 }
9633 }
9634
9635
9636 /* Display a null-terminated echo area message M. If M is 0, clear
9637 out any existing message, and let the mini-buffer text show through.
9638
9639 The buffer M must continue to exist until after the echo area gets
9640 cleared or some other message gets displayed there. Do not pass
9641 text that is stored in a Lisp string. Do not pass text in a buffer
9642 that was alloca'd. */
9643
9644 void
9645 message1 (const char *m)
9646 {
9647 message2 (m, (m ? strlen (m) : 0), 0);
9648 }
9649
9650
9651 /* The non-logging counterpart of message1. */
9652
9653 void
9654 message1_nolog (const char *m)
9655 {
9656 message2_nolog (m, (m ? strlen (m) : 0), 0);
9657 }
9658
9659 /* Display a message M which contains a single %s
9660 which gets replaced with STRING. */
9661
9662 void
9663 message_with_string (const char *m, Lisp_Object string, int log)
9664 {
9665 CHECK_STRING (string);
9666
9667 if (noninteractive)
9668 {
9669 if (m)
9670 {
9671 if (noninteractive_need_newline)
9672 putc ('\n', stderr);
9673 noninteractive_need_newline = 0;
9674 fprintf (stderr, m, SDATA (string));
9675 if (!cursor_in_echo_area)
9676 fprintf (stderr, "\n");
9677 fflush (stderr);
9678 }
9679 }
9680 else if (INTERACTIVE)
9681 {
9682 /* The frame whose minibuffer we're going to display the message on.
9683 It may be larger than the selected frame, so we need
9684 to use its buffer, not the selected frame's buffer. */
9685 Lisp_Object mini_window;
9686 struct frame *f, *sf = SELECTED_FRAME ();
9687
9688 /* Get the frame containing the minibuffer
9689 that the selected frame is using. */
9690 mini_window = FRAME_MINIBUF_WINDOW (sf);
9691 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9692
9693 /* A null message buffer means that the frame hasn't really been
9694 initialized yet. Error messages get reported properly by
9695 cmd_error, so this must be just an informative message; toss it. */
9696 if (FRAME_MESSAGE_BUF (f))
9697 {
9698 Lisp_Object args[2], msg;
9699 struct gcpro gcpro1, gcpro2;
9700
9701 args[0] = build_string (m);
9702 args[1] = msg = string;
9703 GCPRO2 (args[0], msg);
9704 gcpro1.nvars = 2;
9705
9706 msg = Fformat (2, args);
9707
9708 if (log)
9709 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9710 else
9711 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9712
9713 UNGCPRO;
9714
9715 /* Print should start at the beginning of the message
9716 buffer next time. */
9717 message_buf_print = 0;
9718 }
9719 }
9720 }
9721
9722
9723 /* Dump an informative message to the minibuf. If M is 0, clear out
9724 any existing message, and let the mini-buffer text show through. */
9725
9726 static void
9727 vmessage (const char *m, va_list ap)
9728 {
9729 if (noninteractive)
9730 {
9731 if (m)
9732 {
9733 if (noninteractive_need_newline)
9734 putc ('\n', stderr);
9735 noninteractive_need_newline = 0;
9736 vfprintf (stderr, m, ap);
9737 if (cursor_in_echo_area == 0)
9738 fprintf (stderr, "\n");
9739 fflush (stderr);
9740 }
9741 }
9742 else if (INTERACTIVE)
9743 {
9744 /* The frame whose mini-buffer we're going to display the message
9745 on. It may be larger than the selected frame, so we need to
9746 use its buffer, not the selected frame's buffer. */
9747 Lisp_Object mini_window;
9748 struct frame *f, *sf = SELECTED_FRAME ();
9749
9750 /* Get the frame containing the mini-buffer
9751 that the selected frame is using. */
9752 mini_window = FRAME_MINIBUF_WINDOW (sf);
9753 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9754
9755 /* A null message buffer means that the frame hasn't really been
9756 initialized yet. Error messages get reported properly by
9757 cmd_error, so this must be just an informative message; toss
9758 it. */
9759 if (FRAME_MESSAGE_BUF (f))
9760 {
9761 if (m)
9762 {
9763 ptrdiff_t len;
9764
9765 len = doprnt (FRAME_MESSAGE_BUF (f),
9766 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9767
9768 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9769 }
9770 else
9771 message1 (0);
9772
9773 /* Print should start at the beginning of the message
9774 buffer next time. */
9775 message_buf_print = 0;
9776 }
9777 }
9778 }
9779
9780 void
9781 message (const char *m, ...)
9782 {
9783 va_list ap;
9784 va_start (ap, m);
9785 vmessage (m, ap);
9786 va_end (ap);
9787 }
9788
9789
9790 #if 0
9791 /* The non-logging version of message. */
9792
9793 void
9794 message_nolog (const char *m, ...)
9795 {
9796 Lisp_Object old_log_max;
9797 va_list ap;
9798 va_start (ap, m);
9799 old_log_max = Vmessage_log_max;
9800 Vmessage_log_max = Qnil;
9801 vmessage (m, ap);
9802 Vmessage_log_max = old_log_max;
9803 va_end (ap);
9804 }
9805 #endif
9806
9807
9808 /* Display the current message in the current mini-buffer. This is
9809 only called from error handlers in process.c, and is not time
9810 critical. */
9811
9812 void
9813 update_echo_area (void)
9814 {
9815 if (!NILP (echo_area_buffer[0]))
9816 {
9817 Lisp_Object string;
9818 string = Fcurrent_message ();
9819 message3 (string, SBYTES (string),
9820 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9821 }
9822 }
9823
9824
9825 /* Make sure echo area buffers in `echo_buffers' are live.
9826 If they aren't, make new ones. */
9827
9828 static void
9829 ensure_echo_area_buffers (void)
9830 {
9831 int i;
9832
9833 for (i = 0; i < 2; ++i)
9834 if (!BUFFERP (echo_buffer[i])
9835 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9836 {
9837 char name[30];
9838 Lisp_Object old_buffer;
9839 int j;
9840
9841 old_buffer = echo_buffer[i];
9842 sprintf (name, " *Echo Area %d*", i);
9843 echo_buffer[i] = Fget_buffer_create (build_string (name));
9844 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9845 /* to force word wrap in echo area -
9846 it was decided to postpone this*/
9847 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9848
9849 for (j = 0; j < 2; ++j)
9850 if (EQ (old_buffer, echo_area_buffer[j]))
9851 echo_area_buffer[j] = echo_buffer[i];
9852 }
9853 }
9854
9855
9856 /* Call FN with args A1..A4 with either the current or last displayed
9857 echo_area_buffer as current buffer.
9858
9859 WHICH zero means use the current message buffer
9860 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9861 from echo_buffer[] and clear it.
9862
9863 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9864 suitable buffer from echo_buffer[] and clear it.
9865
9866 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9867 that the current message becomes the last displayed one, make
9868 choose a suitable buffer for echo_area_buffer[0], and clear it.
9869
9870 Value is what FN returns. */
9871
9872 static int
9873 with_echo_area_buffer (struct window *w, int which,
9874 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9875 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9876 {
9877 Lisp_Object buffer;
9878 int this_one, the_other, clear_buffer_p, rc;
9879 ptrdiff_t count = SPECPDL_INDEX ();
9880
9881 /* If buffers aren't live, make new ones. */
9882 ensure_echo_area_buffers ();
9883
9884 clear_buffer_p = 0;
9885
9886 if (which == 0)
9887 this_one = 0, the_other = 1;
9888 else if (which > 0)
9889 this_one = 1, the_other = 0;
9890 else
9891 {
9892 this_one = 0, the_other = 1;
9893 clear_buffer_p = 1;
9894
9895 /* We need a fresh one in case the current echo buffer equals
9896 the one containing the last displayed echo area message. */
9897 if (!NILP (echo_area_buffer[this_one])
9898 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9899 echo_area_buffer[this_one] = Qnil;
9900 }
9901
9902 /* Choose a suitable buffer from echo_buffer[] is we don't
9903 have one. */
9904 if (NILP (echo_area_buffer[this_one]))
9905 {
9906 echo_area_buffer[this_one]
9907 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9908 ? echo_buffer[the_other]
9909 : echo_buffer[this_one]);
9910 clear_buffer_p = 1;
9911 }
9912
9913 buffer = echo_area_buffer[this_one];
9914
9915 /* Don't get confused by reusing the buffer used for echoing
9916 for a different purpose. */
9917 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9918 cancel_echoing ();
9919
9920 record_unwind_protect (unwind_with_echo_area_buffer,
9921 with_echo_area_buffer_unwind_data (w));
9922
9923 /* Make the echo area buffer current. Note that for display
9924 purposes, it is not necessary that the displayed window's buffer
9925 == current_buffer, except for text property lookup. So, let's
9926 only set that buffer temporarily here without doing a full
9927 Fset_window_buffer. We must also change w->pointm, though,
9928 because otherwise an assertions in unshow_buffer fails, and Emacs
9929 aborts. */
9930 set_buffer_internal_1 (XBUFFER (buffer));
9931 if (w)
9932 {
9933 w->buffer = buffer;
9934 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9935 }
9936
9937 BVAR (current_buffer, undo_list) = Qt;
9938 BVAR (current_buffer, read_only) = Qnil;
9939 specbind (Qinhibit_read_only, Qt);
9940 specbind (Qinhibit_modification_hooks, Qt);
9941
9942 if (clear_buffer_p && Z > BEG)
9943 del_range (BEG, Z);
9944
9945 eassert (BEGV >= BEG);
9946 eassert (ZV <= Z && ZV >= BEGV);
9947
9948 rc = fn (a1, a2, a3, a4);
9949
9950 eassert (BEGV >= BEG);
9951 eassert (ZV <= Z && ZV >= BEGV);
9952
9953 unbind_to (count, Qnil);
9954 return rc;
9955 }
9956
9957
9958 /* Save state that should be preserved around the call to the function
9959 FN called in with_echo_area_buffer. */
9960
9961 static Lisp_Object
9962 with_echo_area_buffer_unwind_data (struct window *w)
9963 {
9964 int i = 0;
9965 Lisp_Object vector, tmp;
9966
9967 /* Reduce consing by keeping one vector in
9968 Vwith_echo_area_save_vector. */
9969 vector = Vwith_echo_area_save_vector;
9970 Vwith_echo_area_save_vector = Qnil;
9971
9972 if (NILP (vector))
9973 vector = Fmake_vector (make_number (7), Qnil);
9974
9975 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9976 ASET (vector, i, Vdeactivate_mark); ++i;
9977 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9978
9979 if (w)
9980 {
9981 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9982 ASET (vector, i, w->buffer); ++i;
9983 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9984 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9985 }
9986 else
9987 {
9988 int end = i + 4;
9989 for (; i < end; ++i)
9990 ASET (vector, i, Qnil);
9991 }
9992
9993 eassert (i == ASIZE (vector));
9994 return vector;
9995 }
9996
9997
9998 /* Restore global state from VECTOR which was created by
9999 with_echo_area_buffer_unwind_data. */
10000
10001 static Lisp_Object
10002 unwind_with_echo_area_buffer (Lisp_Object vector)
10003 {
10004 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10005 Vdeactivate_mark = AREF (vector, 1);
10006 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10007
10008 if (WINDOWP (AREF (vector, 3)))
10009 {
10010 struct window *w;
10011 Lisp_Object buffer, charpos, bytepos;
10012
10013 w = XWINDOW (AREF (vector, 3));
10014 buffer = AREF (vector, 4);
10015 charpos = AREF (vector, 5);
10016 bytepos = AREF (vector, 6);
10017
10018 w->buffer = buffer;
10019 set_marker_both (w->pointm, buffer,
10020 XFASTINT (charpos), XFASTINT (bytepos));
10021 }
10022
10023 Vwith_echo_area_save_vector = vector;
10024 return Qnil;
10025 }
10026
10027
10028 /* Set up the echo area for use by print functions. MULTIBYTE_P
10029 non-zero means we will print multibyte. */
10030
10031 void
10032 setup_echo_area_for_printing (int multibyte_p)
10033 {
10034 /* If we can't find an echo area any more, exit. */
10035 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10036 Fkill_emacs (Qnil);
10037
10038 ensure_echo_area_buffers ();
10039
10040 if (!message_buf_print)
10041 {
10042 /* A message has been output since the last time we printed.
10043 Choose a fresh echo area buffer. */
10044 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10045 echo_area_buffer[0] = echo_buffer[1];
10046 else
10047 echo_area_buffer[0] = echo_buffer[0];
10048
10049 /* Switch to that buffer and clear it. */
10050 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10051 BVAR (current_buffer, truncate_lines) = Qnil;
10052
10053 if (Z > BEG)
10054 {
10055 ptrdiff_t count = SPECPDL_INDEX ();
10056 specbind (Qinhibit_read_only, Qt);
10057 /* Note that undo recording is always disabled. */
10058 del_range (BEG, Z);
10059 unbind_to (count, Qnil);
10060 }
10061 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10062
10063 /* Set up the buffer for the multibyteness we need. */
10064 if (multibyte_p
10065 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10066 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10067
10068 /* Raise the frame containing the echo area. */
10069 if (minibuffer_auto_raise)
10070 {
10071 struct frame *sf = SELECTED_FRAME ();
10072 Lisp_Object mini_window;
10073 mini_window = FRAME_MINIBUF_WINDOW (sf);
10074 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10075 }
10076
10077 message_log_maybe_newline ();
10078 message_buf_print = 1;
10079 }
10080 else
10081 {
10082 if (NILP (echo_area_buffer[0]))
10083 {
10084 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10085 echo_area_buffer[0] = echo_buffer[1];
10086 else
10087 echo_area_buffer[0] = echo_buffer[0];
10088 }
10089
10090 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10091 {
10092 /* Someone switched buffers between print requests. */
10093 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10094 BVAR (current_buffer, truncate_lines) = Qnil;
10095 }
10096 }
10097 }
10098
10099
10100 /* Display an echo area message in window W. Value is non-zero if W's
10101 height is changed. If display_last_displayed_message_p is
10102 non-zero, display the message that was last displayed, otherwise
10103 display the current message. */
10104
10105 static int
10106 display_echo_area (struct window *w)
10107 {
10108 int i, no_message_p, window_height_changed_p;
10109
10110 /* Temporarily disable garbage collections while displaying the echo
10111 area. This is done because a GC can print a message itself.
10112 That message would modify the echo area buffer's contents while a
10113 redisplay of the buffer is going on, and seriously confuse
10114 redisplay. */
10115 ptrdiff_t count = inhibit_garbage_collection ();
10116
10117 /* If there is no message, we must call display_echo_area_1
10118 nevertheless because it resizes the window. But we will have to
10119 reset the echo_area_buffer in question to nil at the end because
10120 with_echo_area_buffer will sets it to an empty buffer. */
10121 i = display_last_displayed_message_p ? 1 : 0;
10122 no_message_p = NILP (echo_area_buffer[i]);
10123
10124 window_height_changed_p
10125 = with_echo_area_buffer (w, display_last_displayed_message_p,
10126 display_echo_area_1,
10127 (intptr_t) w, Qnil, 0, 0);
10128
10129 if (no_message_p)
10130 echo_area_buffer[i] = Qnil;
10131
10132 unbind_to (count, Qnil);
10133 return window_height_changed_p;
10134 }
10135
10136
10137 /* Helper for display_echo_area. Display the current buffer which
10138 contains the current echo area message in window W, a mini-window,
10139 a pointer to which is passed in A1. A2..A4 are currently not used.
10140 Change the height of W so that all of the message is displayed.
10141 Value is non-zero if height of W was changed. */
10142
10143 static int
10144 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10145 {
10146 intptr_t i1 = a1;
10147 struct window *w = (struct window *) i1;
10148 Lisp_Object window;
10149 struct text_pos start;
10150 int window_height_changed_p = 0;
10151
10152 /* Do this before displaying, so that we have a large enough glyph
10153 matrix for the display. If we can't get enough space for the
10154 whole text, display the last N lines. That works by setting w->start. */
10155 window_height_changed_p = resize_mini_window (w, 0);
10156
10157 /* Use the starting position chosen by resize_mini_window. */
10158 SET_TEXT_POS_FROM_MARKER (start, w->start);
10159
10160 /* Display. */
10161 clear_glyph_matrix (w->desired_matrix);
10162 XSETWINDOW (window, w);
10163 try_window (window, start, 0);
10164
10165 return window_height_changed_p;
10166 }
10167
10168
10169 /* Resize the echo area window to exactly the size needed for the
10170 currently displayed message, if there is one. If a mini-buffer
10171 is active, don't shrink it. */
10172
10173 void
10174 resize_echo_area_exactly (void)
10175 {
10176 if (BUFFERP (echo_area_buffer[0])
10177 && WINDOWP (echo_area_window))
10178 {
10179 struct window *w = XWINDOW (echo_area_window);
10180 int resized_p;
10181 Lisp_Object resize_exactly;
10182
10183 if (minibuf_level == 0)
10184 resize_exactly = Qt;
10185 else
10186 resize_exactly = Qnil;
10187
10188 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10189 (intptr_t) w, resize_exactly,
10190 0, 0);
10191 if (resized_p)
10192 {
10193 ++windows_or_buffers_changed;
10194 ++update_mode_lines;
10195 redisplay_internal ();
10196 }
10197 }
10198 }
10199
10200
10201 /* Callback function for with_echo_area_buffer, when used from
10202 resize_echo_area_exactly. A1 contains a pointer to the window to
10203 resize, EXACTLY non-nil means resize the mini-window exactly to the
10204 size of the text displayed. A3 and A4 are not used. Value is what
10205 resize_mini_window returns. */
10206
10207 static int
10208 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10209 {
10210 intptr_t i1 = a1;
10211 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10212 }
10213
10214
10215 /* Resize mini-window W to fit the size of its contents. EXACT_P
10216 means size the window exactly to the size needed. Otherwise, it's
10217 only enlarged until W's buffer is empty.
10218
10219 Set W->start to the right place to begin display. If the whole
10220 contents fit, start at the beginning. Otherwise, start so as
10221 to make the end of the contents appear. This is particularly
10222 important for y-or-n-p, but seems desirable generally.
10223
10224 Value is non-zero if the window height has been changed. */
10225
10226 int
10227 resize_mini_window (struct window *w, int exact_p)
10228 {
10229 struct frame *f = XFRAME (w->frame);
10230 int window_height_changed_p = 0;
10231
10232 eassert (MINI_WINDOW_P (w));
10233
10234 /* By default, start display at the beginning. */
10235 set_marker_both (w->start, w->buffer,
10236 BUF_BEGV (XBUFFER (w->buffer)),
10237 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10238
10239 /* Don't resize windows while redisplaying a window; it would
10240 confuse redisplay functions when the size of the window they are
10241 displaying changes from under them. Such a resizing can happen,
10242 for instance, when which-func prints a long message while
10243 we are running fontification-functions. We're running these
10244 functions with safe_call which binds inhibit-redisplay to t. */
10245 if (!NILP (Vinhibit_redisplay))
10246 return 0;
10247
10248 /* Nil means don't try to resize. */
10249 if (NILP (Vresize_mini_windows)
10250 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10251 return 0;
10252
10253 if (!FRAME_MINIBUF_ONLY_P (f))
10254 {
10255 struct it it;
10256 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10257 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10258 int height;
10259 EMACS_INT max_height;
10260 int unit = FRAME_LINE_HEIGHT (f);
10261 struct text_pos start;
10262 struct buffer *old_current_buffer = NULL;
10263
10264 if (current_buffer != XBUFFER (w->buffer))
10265 {
10266 old_current_buffer = current_buffer;
10267 set_buffer_internal (XBUFFER (w->buffer));
10268 }
10269
10270 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10271
10272 /* Compute the max. number of lines specified by the user. */
10273 if (FLOATP (Vmax_mini_window_height))
10274 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10275 else if (INTEGERP (Vmax_mini_window_height))
10276 max_height = XINT (Vmax_mini_window_height);
10277 else
10278 max_height = total_height / 4;
10279
10280 /* Correct that max. height if it's bogus. */
10281 max_height = max (1, max_height);
10282 max_height = min (total_height, max_height);
10283
10284 /* Find out the height of the text in the window. */
10285 if (it.line_wrap == TRUNCATE)
10286 height = 1;
10287 else
10288 {
10289 last_height = 0;
10290 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10291 if (it.max_ascent == 0 && it.max_descent == 0)
10292 height = it.current_y + last_height;
10293 else
10294 height = it.current_y + it.max_ascent + it.max_descent;
10295 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10296 height = (height + unit - 1) / unit;
10297 }
10298
10299 /* Compute a suitable window start. */
10300 if (height > max_height)
10301 {
10302 height = max_height;
10303 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10304 move_it_vertically_backward (&it, (height - 1) * unit);
10305 start = it.current.pos;
10306 }
10307 else
10308 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10309 SET_MARKER_FROM_TEXT_POS (w->start, start);
10310
10311 if (EQ (Vresize_mini_windows, Qgrow_only))
10312 {
10313 /* Let it grow only, until we display an empty message, in which
10314 case the window shrinks again. */
10315 if (height > WINDOW_TOTAL_LINES (w))
10316 {
10317 int old_height = WINDOW_TOTAL_LINES (w);
10318 freeze_window_starts (f, 1);
10319 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10320 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10321 }
10322 else if (height < WINDOW_TOTAL_LINES (w)
10323 && (exact_p || BEGV == ZV))
10324 {
10325 int old_height = WINDOW_TOTAL_LINES (w);
10326 freeze_window_starts (f, 0);
10327 shrink_mini_window (w);
10328 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10329 }
10330 }
10331 else
10332 {
10333 /* Always resize to exact size needed. */
10334 if (height > WINDOW_TOTAL_LINES (w))
10335 {
10336 int old_height = WINDOW_TOTAL_LINES (w);
10337 freeze_window_starts (f, 1);
10338 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10339 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10340 }
10341 else if (height < WINDOW_TOTAL_LINES (w))
10342 {
10343 int old_height = WINDOW_TOTAL_LINES (w);
10344 freeze_window_starts (f, 0);
10345 shrink_mini_window (w);
10346
10347 if (height)
10348 {
10349 freeze_window_starts (f, 1);
10350 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10351 }
10352
10353 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10354 }
10355 }
10356
10357 if (old_current_buffer)
10358 set_buffer_internal (old_current_buffer);
10359 }
10360
10361 return window_height_changed_p;
10362 }
10363
10364
10365 /* Value is the current message, a string, or nil if there is no
10366 current message. */
10367
10368 Lisp_Object
10369 current_message (void)
10370 {
10371 Lisp_Object msg;
10372
10373 if (!BUFFERP (echo_area_buffer[0]))
10374 msg = Qnil;
10375 else
10376 {
10377 with_echo_area_buffer (0, 0, current_message_1,
10378 (intptr_t) &msg, Qnil, 0, 0);
10379 if (NILP (msg))
10380 echo_area_buffer[0] = Qnil;
10381 }
10382
10383 return msg;
10384 }
10385
10386
10387 static int
10388 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10389 {
10390 intptr_t i1 = a1;
10391 Lisp_Object *msg = (Lisp_Object *) i1;
10392
10393 if (Z > BEG)
10394 *msg = make_buffer_string (BEG, Z, 1);
10395 else
10396 *msg = Qnil;
10397 return 0;
10398 }
10399
10400
10401 /* Push the current message on Vmessage_stack for later restoration
10402 by restore_message. Value is non-zero if the current message isn't
10403 empty. This is a relatively infrequent operation, so it's not
10404 worth optimizing. */
10405
10406 int
10407 push_message (void)
10408 {
10409 Lisp_Object msg;
10410 msg = current_message ();
10411 Vmessage_stack = Fcons (msg, Vmessage_stack);
10412 return STRINGP (msg);
10413 }
10414
10415
10416 /* Restore message display from the top of Vmessage_stack. */
10417
10418 void
10419 restore_message (void)
10420 {
10421 Lisp_Object msg;
10422
10423 eassert (CONSP (Vmessage_stack));
10424 msg = XCAR (Vmessage_stack);
10425 if (STRINGP (msg))
10426 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10427 else
10428 message3_nolog (msg, 0, 0);
10429 }
10430
10431
10432 /* Handler for record_unwind_protect calling pop_message. */
10433
10434 Lisp_Object
10435 pop_message_unwind (Lisp_Object dummy)
10436 {
10437 pop_message ();
10438 return Qnil;
10439 }
10440
10441 /* Pop the top-most entry off Vmessage_stack. */
10442
10443 static void
10444 pop_message (void)
10445 {
10446 eassert (CONSP (Vmessage_stack));
10447 Vmessage_stack = XCDR (Vmessage_stack);
10448 }
10449
10450
10451 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10452 exits. If the stack is not empty, we have a missing pop_message
10453 somewhere. */
10454
10455 void
10456 check_message_stack (void)
10457 {
10458 if (!NILP (Vmessage_stack))
10459 abort ();
10460 }
10461
10462
10463 /* Truncate to NCHARS what will be displayed in the echo area the next
10464 time we display it---but don't redisplay it now. */
10465
10466 void
10467 truncate_echo_area (ptrdiff_t nchars)
10468 {
10469 if (nchars == 0)
10470 echo_area_buffer[0] = Qnil;
10471 /* A null message buffer means that the frame hasn't really been
10472 initialized yet. Error messages get reported properly by
10473 cmd_error, so this must be just an informative message; toss it. */
10474 else if (!noninteractive
10475 && INTERACTIVE
10476 && !NILP (echo_area_buffer[0]))
10477 {
10478 struct frame *sf = SELECTED_FRAME ();
10479 if (FRAME_MESSAGE_BUF (sf))
10480 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10481 }
10482 }
10483
10484
10485 /* Helper function for truncate_echo_area. Truncate the current
10486 message to at most NCHARS characters. */
10487
10488 static int
10489 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10490 {
10491 if (BEG + nchars < Z)
10492 del_range (BEG + nchars, Z);
10493 if (Z == BEG)
10494 echo_area_buffer[0] = Qnil;
10495 return 0;
10496 }
10497
10498
10499 /* Set the current message to a substring of S or STRING.
10500
10501 If STRING is a Lisp string, set the message to the first NBYTES
10502 bytes from STRING. NBYTES zero means use the whole string. If
10503 STRING is multibyte, the message will be displayed multibyte.
10504
10505 If S is not null, set the message to the first LEN bytes of S. LEN
10506 zero means use the whole string. MULTIBYTE_P non-zero means S is
10507 multibyte. Display the message multibyte in that case.
10508
10509 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10510 to t before calling set_message_1 (which calls insert).
10511 */
10512
10513 static void
10514 set_message (const char *s, Lisp_Object string,
10515 ptrdiff_t nbytes, int multibyte_p)
10516 {
10517 message_enable_multibyte
10518 = ((s && multibyte_p)
10519 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10520
10521 with_echo_area_buffer (0, -1, set_message_1,
10522 (intptr_t) s, string, nbytes, multibyte_p);
10523 message_buf_print = 0;
10524 help_echo_showing_p = 0;
10525 }
10526
10527
10528 /* Helper function for set_message. Arguments have the same meaning
10529 as there, with A1 corresponding to S and A2 corresponding to STRING
10530 This function is called with the echo area buffer being
10531 current. */
10532
10533 static int
10534 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10535 {
10536 intptr_t i1 = a1;
10537 const char *s = (const char *) i1;
10538 const unsigned char *msg = (const unsigned char *) s;
10539 Lisp_Object string = a2;
10540
10541 /* Change multibyteness of the echo buffer appropriately. */
10542 if (message_enable_multibyte
10543 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10544 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10545
10546 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10547 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10548 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10549
10550 /* Insert new message at BEG. */
10551 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10552
10553 if (STRINGP (string))
10554 {
10555 ptrdiff_t nchars;
10556
10557 if (nbytes == 0)
10558 nbytes = SBYTES (string);
10559 nchars = string_byte_to_char (string, nbytes);
10560
10561 /* This function takes care of single/multibyte conversion. We
10562 just have to ensure that the echo area buffer has the right
10563 setting of enable_multibyte_characters. */
10564 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10565 }
10566 else if (s)
10567 {
10568 if (nbytes == 0)
10569 nbytes = strlen (s);
10570
10571 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10572 {
10573 /* Convert from multi-byte to single-byte. */
10574 ptrdiff_t i;
10575 int c, n;
10576 char work[1];
10577
10578 /* Convert a multibyte string to single-byte. */
10579 for (i = 0; i < nbytes; i += n)
10580 {
10581 c = string_char_and_length (msg + i, &n);
10582 work[0] = (ASCII_CHAR_P (c)
10583 ? c
10584 : multibyte_char_to_unibyte (c));
10585 insert_1_both (work, 1, 1, 1, 0, 0);
10586 }
10587 }
10588 else if (!multibyte_p
10589 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10590 {
10591 /* Convert from single-byte to multi-byte. */
10592 ptrdiff_t i;
10593 int c, n;
10594 unsigned char str[MAX_MULTIBYTE_LENGTH];
10595
10596 /* Convert a single-byte string to multibyte. */
10597 for (i = 0; i < nbytes; i++)
10598 {
10599 c = msg[i];
10600 MAKE_CHAR_MULTIBYTE (c);
10601 n = CHAR_STRING (c, str);
10602 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10603 }
10604 }
10605 else
10606 insert_1 (s, nbytes, 1, 0, 0);
10607 }
10608
10609 return 0;
10610 }
10611
10612
10613 /* Clear messages. CURRENT_P non-zero means clear the current
10614 message. LAST_DISPLAYED_P non-zero means clear the message
10615 last displayed. */
10616
10617 void
10618 clear_message (int current_p, int last_displayed_p)
10619 {
10620 if (current_p)
10621 {
10622 echo_area_buffer[0] = Qnil;
10623 message_cleared_p = 1;
10624 }
10625
10626 if (last_displayed_p)
10627 echo_area_buffer[1] = Qnil;
10628
10629 message_buf_print = 0;
10630 }
10631
10632 /* Clear garbaged frames.
10633
10634 This function is used where the old redisplay called
10635 redraw_garbaged_frames which in turn called redraw_frame which in
10636 turn called clear_frame. The call to clear_frame was a source of
10637 flickering. I believe a clear_frame is not necessary. It should
10638 suffice in the new redisplay to invalidate all current matrices,
10639 and ensure a complete redisplay of all windows. */
10640
10641 static void
10642 clear_garbaged_frames (void)
10643 {
10644 if (frame_garbaged)
10645 {
10646 Lisp_Object tail, frame;
10647 int changed_count = 0;
10648
10649 FOR_EACH_FRAME (tail, frame)
10650 {
10651 struct frame *f = XFRAME (frame);
10652
10653 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10654 {
10655 if (f->resized_p)
10656 {
10657 Fredraw_frame (frame);
10658 f->force_flush_display_p = 1;
10659 }
10660 clear_current_matrices (f);
10661 changed_count++;
10662 f->garbaged = 0;
10663 f->resized_p = 0;
10664 }
10665 }
10666
10667 frame_garbaged = 0;
10668 if (changed_count)
10669 ++windows_or_buffers_changed;
10670 }
10671 }
10672
10673
10674 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10675 is non-zero update selected_frame. Value is non-zero if the
10676 mini-windows height has been changed. */
10677
10678 static int
10679 echo_area_display (int update_frame_p)
10680 {
10681 Lisp_Object mini_window;
10682 struct window *w;
10683 struct frame *f;
10684 int window_height_changed_p = 0;
10685 struct frame *sf = SELECTED_FRAME ();
10686
10687 mini_window = FRAME_MINIBUF_WINDOW (sf);
10688 w = XWINDOW (mini_window);
10689 f = XFRAME (WINDOW_FRAME (w));
10690
10691 /* Don't display if frame is invisible or not yet initialized. */
10692 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10693 return 0;
10694
10695 #ifdef HAVE_WINDOW_SYSTEM
10696 /* When Emacs starts, selected_frame may be the initial terminal
10697 frame. If we let this through, a message would be displayed on
10698 the terminal. */
10699 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10700 return 0;
10701 #endif /* HAVE_WINDOW_SYSTEM */
10702
10703 /* Redraw garbaged frames. */
10704 if (frame_garbaged)
10705 clear_garbaged_frames ();
10706
10707 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10708 {
10709 echo_area_window = mini_window;
10710 window_height_changed_p = display_echo_area (w);
10711 w->must_be_updated_p = 1;
10712
10713 /* Update the display, unless called from redisplay_internal.
10714 Also don't update the screen during redisplay itself. The
10715 update will happen at the end of redisplay, and an update
10716 here could cause confusion. */
10717 if (update_frame_p && !redisplaying_p)
10718 {
10719 int n = 0;
10720
10721 /* If the display update has been interrupted by pending
10722 input, update mode lines in the frame. Due to the
10723 pending input, it might have been that redisplay hasn't
10724 been called, so that mode lines above the echo area are
10725 garbaged. This looks odd, so we prevent it here. */
10726 if (!display_completed)
10727 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10728
10729 if (window_height_changed_p
10730 /* Don't do this if Emacs is shutting down. Redisplay
10731 needs to run hooks. */
10732 && !NILP (Vrun_hooks))
10733 {
10734 /* Must update other windows. Likewise as in other
10735 cases, don't let this update be interrupted by
10736 pending input. */
10737 ptrdiff_t count = SPECPDL_INDEX ();
10738 specbind (Qredisplay_dont_pause, Qt);
10739 windows_or_buffers_changed = 1;
10740 redisplay_internal ();
10741 unbind_to (count, Qnil);
10742 }
10743 else if (FRAME_WINDOW_P (f) && n == 0)
10744 {
10745 /* Window configuration is the same as before.
10746 Can do with a display update of the echo area,
10747 unless we displayed some mode lines. */
10748 update_single_window (w, 1);
10749 FRAME_RIF (f)->flush_display (f);
10750 }
10751 else
10752 update_frame (f, 1, 1);
10753
10754 /* If cursor is in the echo area, make sure that the next
10755 redisplay displays the minibuffer, so that the cursor will
10756 be replaced with what the minibuffer wants. */
10757 if (cursor_in_echo_area)
10758 ++windows_or_buffers_changed;
10759 }
10760 }
10761 else if (!EQ (mini_window, selected_window))
10762 windows_or_buffers_changed++;
10763
10764 /* Last displayed message is now the current message. */
10765 echo_area_buffer[1] = echo_area_buffer[0];
10766 /* Inform read_char that we're not echoing. */
10767 echo_message_buffer = Qnil;
10768
10769 /* Prevent redisplay optimization in redisplay_internal by resetting
10770 this_line_start_pos. This is done because the mini-buffer now
10771 displays the message instead of its buffer text. */
10772 if (EQ (mini_window, selected_window))
10773 CHARPOS (this_line_start_pos) = 0;
10774
10775 return window_height_changed_p;
10776 }
10777
10778
10779 \f
10780 /***********************************************************************
10781 Mode Lines and Frame Titles
10782 ***********************************************************************/
10783
10784 /* A buffer for constructing non-propertized mode-line strings and
10785 frame titles in it; allocated from the heap in init_xdisp and
10786 resized as needed in store_mode_line_noprop_char. */
10787
10788 static char *mode_line_noprop_buf;
10789
10790 /* The buffer's end, and a current output position in it. */
10791
10792 static char *mode_line_noprop_buf_end;
10793 static char *mode_line_noprop_ptr;
10794
10795 #define MODE_LINE_NOPROP_LEN(start) \
10796 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10797
10798 static enum {
10799 MODE_LINE_DISPLAY = 0,
10800 MODE_LINE_TITLE,
10801 MODE_LINE_NOPROP,
10802 MODE_LINE_STRING
10803 } mode_line_target;
10804
10805 /* Alist that caches the results of :propertize.
10806 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10807 static Lisp_Object mode_line_proptrans_alist;
10808
10809 /* List of strings making up the mode-line. */
10810 static Lisp_Object mode_line_string_list;
10811
10812 /* Base face property when building propertized mode line string. */
10813 static Lisp_Object mode_line_string_face;
10814 static Lisp_Object mode_line_string_face_prop;
10815
10816
10817 /* Unwind data for mode line strings */
10818
10819 static Lisp_Object Vmode_line_unwind_vector;
10820
10821 static Lisp_Object
10822 format_mode_line_unwind_data (struct frame *target_frame,
10823 struct buffer *obuf,
10824 Lisp_Object owin,
10825 int save_proptrans)
10826 {
10827 Lisp_Object vector, tmp;
10828
10829 /* Reduce consing by keeping one vector in
10830 Vwith_echo_area_save_vector. */
10831 vector = Vmode_line_unwind_vector;
10832 Vmode_line_unwind_vector = Qnil;
10833
10834 if (NILP (vector))
10835 vector = Fmake_vector (make_number (10), Qnil);
10836
10837 ASET (vector, 0, make_number (mode_line_target));
10838 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10839 ASET (vector, 2, mode_line_string_list);
10840 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10841 ASET (vector, 4, mode_line_string_face);
10842 ASET (vector, 5, mode_line_string_face_prop);
10843
10844 if (obuf)
10845 XSETBUFFER (tmp, obuf);
10846 else
10847 tmp = Qnil;
10848 ASET (vector, 6, tmp);
10849 ASET (vector, 7, owin);
10850 if (target_frame)
10851 {
10852 /* Similarly to `with-selected-window', if the operation selects
10853 a window on another frame, we must restore that frame's
10854 selected window, and (for a tty) the top-frame. */
10855 ASET (vector, 8, target_frame->selected_window);
10856 if (FRAME_TERMCAP_P (target_frame))
10857 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10858 }
10859
10860 return vector;
10861 }
10862
10863 static Lisp_Object
10864 unwind_format_mode_line (Lisp_Object vector)
10865 {
10866 Lisp_Object old_window = AREF (vector, 7);
10867 Lisp_Object target_frame_window = AREF (vector, 8);
10868 Lisp_Object old_top_frame = AREF (vector, 9);
10869
10870 mode_line_target = XINT (AREF (vector, 0));
10871 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10872 mode_line_string_list = AREF (vector, 2);
10873 if (! EQ (AREF (vector, 3), Qt))
10874 mode_line_proptrans_alist = AREF (vector, 3);
10875 mode_line_string_face = AREF (vector, 4);
10876 mode_line_string_face_prop = AREF (vector, 5);
10877
10878 /* Select window before buffer, since it may change the buffer. */
10879 if (!NILP (old_window))
10880 {
10881 /* If the operation that we are unwinding had selected a window
10882 on a different frame, reset its frame-selected-window. For a
10883 text terminal, reset its top-frame if necessary. */
10884 if (!NILP (target_frame_window))
10885 {
10886 Lisp_Object frame
10887 = WINDOW_FRAME (XWINDOW (target_frame_window));
10888
10889 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10890 Fselect_window (target_frame_window, Qt);
10891
10892 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10893 Fselect_frame (old_top_frame, Qt);
10894 }
10895
10896 Fselect_window (old_window, Qt);
10897 }
10898
10899 if (!NILP (AREF (vector, 6)))
10900 {
10901 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10902 ASET (vector, 6, Qnil);
10903 }
10904
10905 Vmode_line_unwind_vector = vector;
10906 return Qnil;
10907 }
10908
10909
10910 /* Store a single character C for the frame title in mode_line_noprop_buf.
10911 Re-allocate mode_line_noprop_buf if necessary. */
10912
10913 static void
10914 store_mode_line_noprop_char (char c)
10915 {
10916 /* If output position has reached the end of the allocated buffer,
10917 increase the buffer's size. */
10918 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10919 {
10920 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10921 ptrdiff_t size = len;
10922 mode_line_noprop_buf =
10923 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10924 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10925 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10926 }
10927
10928 *mode_line_noprop_ptr++ = c;
10929 }
10930
10931
10932 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10933 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10934 characters that yield more columns than PRECISION; PRECISION <= 0
10935 means copy the whole string. Pad with spaces until FIELD_WIDTH
10936 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10937 pad. Called from display_mode_element when it is used to build a
10938 frame title. */
10939
10940 static int
10941 store_mode_line_noprop (const char *string, int field_width, int precision)
10942 {
10943 const unsigned char *str = (const unsigned char *) string;
10944 int n = 0;
10945 ptrdiff_t dummy, nbytes;
10946
10947 /* Copy at most PRECISION chars from STR. */
10948 nbytes = strlen (string);
10949 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10950 while (nbytes--)
10951 store_mode_line_noprop_char (*str++);
10952
10953 /* Fill up with spaces until FIELD_WIDTH reached. */
10954 while (field_width > 0
10955 && n < field_width)
10956 {
10957 store_mode_line_noprop_char (' ');
10958 ++n;
10959 }
10960
10961 return n;
10962 }
10963
10964 /***********************************************************************
10965 Frame Titles
10966 ***********************************************************************/
10967
10968 #ifdef HAVE_WINDOW_SYSTEM
10969
10970 /* Set the title of FRAME, if it has changed. The title format is
10971 Vicon_title_format if FRAME is iconified, otherwise it is
10972 frame_title_format. */
10973
10974 static void
10975 x_consider_frame_title (Lisp_Object frame)
10976 {
10977 struct frame *f = XFRAME (frame);
10978
10979 if (FRAME_WINDOW_P (f)
10980 || FRAME_MINIBUF_ONLY_P (f)
10981 || f->explicit_name)
10982 {
10983 /* Do we have more than one visible frame on this X display? */
10984 Lisp_Object tail;
10985 Lisp_Object fmt;
10986 ptrdiff_t title_start;
10987 char *title;
10988 ptrdiff_t len;
10989 struct it it;
10990 ptrdiff_t count = SPECPDL_INDEX ();
10991
10992 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10993 {
10994 Lisp_Object other_frame = XCAR (tail);
10995 struct frame *tf = XFRAME (other_frame);
10996
10997 if (tf != f
10998 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10999 && !FRAME_MINIBUF_ONLY_P (tf)
11000 && !EQ (other_frame, tip_frame)
11001 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11002 break;
11003 }
11004
11005 /* Set global variable indicating that multiple frames exist. */
11006 multiple_frames = CONSP (tail);
11007
11008 /* Switch to the buffer of selected window of the frame. Set up
11009 mode_line_target so that display_mode_element will output into
11010 mode_line_noprop_buf; then display the title. */
11011 record_unwind_protect (unwind_format_mode_line,
11012 format_mode_line_unwind_data
11013 (f, current_buffer, selected_window, 0));
11014
11015 Fselect_window (f->selected_window, Qt);
11016 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11017 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11018
11019 mode_line_target = MODE_LINE_TITLE;
11020 title_start = MODE_LINE_NOPROP_LEN (0);
11021 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11022 NULL, DEFAULT_FACE_ID);
11023 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11024 len = MODE_LINE_NOPROP_LEN (title_start);
11025 title = mode_line_noprop_buf + title_start;
11026 unbind_to (count, Qnil);
11027
11028 /* Set the title only if it's changed. This avoids consing in
11029 the common case where it hasn't. (If it turns out that we've
11030 already wasted too much time by walking through the list with
11031 display_mode_element, then we might need to optimize at a
11032 higher level than this.) */
11033 if (! STRINGP (f->name)
11034 || SBYTES (f->name) != len
11035 || memcmp (title, SDATA (f->name), len) != 0)
11036 x_implicitly_set_name (f, make_string (title, len), Qnil);
11037 }
11038 }
11039
11040 #endif /* not HAVE_WINDOW_SYSTEM */
11041
11042 \f
11043 /***********************************************************************
11044 Menu Bars
11045 ***********************************************************************/
11046
11047
11048 /* Prepare for redisplay by updating menu-bar item lists when
11049 appropriate. This can call eval. */
11050
11051 void
11052 prepare_menu_bars (void)
11053 {
11054 int all_windows;
11055 struct gcpro gcpro1, gcpro2;
11056 struct frame *f;
11057 Lisp_Object tooltip_frame;
11058
11059 #ifdef HAVE_WINDOW_SYSTEM
11060 tooltip_frame = tip_frame;
11061 #else
11062 tooltip_frame = Qnil;
11063 #endif
11064
11065 /* Update all frame titles based on their buffer names, etc. We do
11066 this before the menu bars so that the buffer-menu will show the
11067 up-to-date frame titles. */
11068 #ifdef HAVE_WINDOW_SYSTEM
11069 if (windows_or_buffers_changed || update_mode_lines)
11070 {
11071 Lisp_Object tail, frame;
11072
11073 FOR_EACH_FRAME (tail, frame)
11074 {
11075 f = XFRAME (frame);
11076 if (!EQ (frame, tooltip_frame)
11077 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11078 x_consider_frame_title (frame);
11079 }
11080 }
11081 #endif /* HAVE_WINDOW_SYSTEM */
11082
11083 /* Update the menu bar item lists, if appropriate. This has to be
11084 done before any actual redisplay or generation of display lines. */
11085 all_windows = (update_mode_lines
11086 || buffer_shared > 1
11087 || windows_or_buffers_changed);
11088 if (all_windows)
11089 {
11090 Lisp_Object tail, frame;
11091 ptrdiff_t count = SPECPDL_INDEX ();
11092 /* 1 means that update_menu_bar has run its hooks
11093 so any further calls to update_menu_bar shouldn't do so again. */
11094 int menu_bar_hooks_run = 0;
11095
11096 record_unwind_save_match_data ();
11097
11098 FOR_EACH_FRAME (tail, frame)
11099 {
11100 f = XFRAME (frame);
11101
11102 /* Ignore tooltip frame. */
11103 if (EQ (frame, tooltip_frame))
11104 continue;
11105
11106 /* If a window on this frame changed size, report that to
11107 the user and clear the size-change flag. */
11108 if (FRAME_WINDOW_SIZES_CHANGED (f))
11109 {
11110 Lisp_Object functions;
11111
11112 /* Clear flag first in case we get an error below. */
11113 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11114 functions = Vwindow_size_change_functions;
11115 GCPRO2 (tail, functions);
11116
11117 while (CONSP (functions))
11118 {
11119 if (!EQ (XCAR (functions), Qt))
11120 call1 (XCAR (functions), frame);
11121 functions = XCDR (functions);
11122 }
11123 UNGCPRO;
11124 }
11125
11126 GCPRO1 (tail);
11127 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11128 #ifdef HAVE_WINDOW_SYSTEM
11129 update_tool_bar (f, 0);
11130 #endif
11131 #ifdef HAVE_NS
11132 if (windows_or_buffers_changed
11133 && FRAME_NS_P (f))
11134 ns_set_doc_edited (f, Fbuffer_modified_p
11135 (XWINDOW (f->selected_window)->buffer));
11136 #endif
11137 UNGCPRO;
11138 }
11139
11140 unbind_to (count, Qnil);
11141 }
11142 else
11143 {
11144 struct frame *sf = SELECTED_FRAME ();
11145 update_menu_bar (sf, 1, 0);
11146 #ifdef HAVE_WINDOW_SYSTEM
11147 update_tool_bar (sf, 1);
11148 #endif
11149 }
11150 }
11151
11152
11153 /* Update the menu bar item list for frame F. This has to be done
11154 before we start to fill in any display lines, because it can call
11155 eval.
11156
11157 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11158
11159 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11160 already ran the menu bar hooks for this redisplay, so there
11161 is no need to run them again. The return value is the
11162 updated value of this flag, to pass to the next call. */
11163
11164 static int
11165 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11166 {
11167 Lisp_Object window;
11168 register struct window *w;
11169
11170 /* If called recursively during a menu update, do nothing. This can
11171 happen when, for instance, an activate-menubar-hook causes a
11172 redisplay. */
11173 if (inhibit_menubar_update)
11174 return hooks_run;
11175
11176 window = FRAME_SELECTED_WINDOW (f);
11177 w = XWINDOW (window);
11178
11179 if (FRAME_WINDOW_P (f)
11180 ?
11181 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11182 || defined (HAVE_NS) || defined (USE_GTK)
11183 FRAME_EXTERNAL_MENU_BAR (f)
11184 #else
11185 FRAME_MENU_BAR_LINES (f) > 0
11186 #endif
11187 : FRAME_MENU_BAR_LINES (f) > 0)
11188 {
11189 /* If the user has switched buffers or windows, we need to
11190 recompute to reflect the new bindings. But we'll
11191 recompute when update_mode_lines is set too; that means
11192 that people can use force-mode-line-update to request
11193 that the menu bar be recomputed. The adverse effect on
11194 the rest of the redisplay algorithm is about the same as
11195 windows_or_buffers_changed anyway. */
11196 if (windows_or_buffers_changed
11197 /* This used to test w->update_mode_line, but we believe
11198 there is no need to recompute the menu in that case. */
11199 || update_mode_lines
11200 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11201 < BUF_MODIFF (XBUFFER (w->buffer)))
11202 != w->last_had_star)
11203 || ((!NILP (Vtransient_mark_mode)
11204 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11205 != !NILP (w->region_showing)))
11206 {
11207 struct buffer *prev = current_buffer;
11208 ptrdiff_t count = SPECPDL_INDEX ();
11209
11210 specbind (Qinhibit_menubar_update, Qt);
11211
11212 set_buffer_internal_1 (XBUFFER (w->buffer));
11213 if (save_match_data)
11214 record_unwind_save_match_data ();
11215 if (NILP (Voverriding_local_map_menu_flag))
11216 {
11217 specbind (Qoverriding_terminal_local_map, Qnil);
11218 specbind (Qoverriding_local_map, Qnil);
11219 }
11220
11221 if (!hooks_run)
11222 {
11223 /* Run the Lucid hook. */
11224 safe_run_hooks (Qactivate_menubar_hook);
11225
11226 /* If it has changed current-menubar from previous value,
11227 really recompute the menu-bar from the value. */
11228 if (! NILP (Vlucid_menu_bar_dirty_flag))
11229 call0 (Qrecompute_lucid_menubar);
11230
11231 safe_run_hooks (Qmenu_bar_update_hook);
11232
11233 hooks_run = 1;
11234 }
11235
11236 XSETFRAME (Vmenu_updating_frame, f);
11237 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11238
11239 /* Redisplay the menu bar in case we changed it. */
11240 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11241 || defined (HAVE_NS) || defined (USE_GTK)
11242 if (FRAME_WINDOW_P (f))
11243 {
11244 #if defined (HAVE_NS)
11245 /* All frames on Mac OS share the same menubar. So only
11246 the selected frame should be allowed to set it. */
11247 if (f == SELECTED_FRAME ())
11248 #endif
11249 set_frame_menubar (f, 0, 0);
11250 }
11251 else
11252 /* On a terminal screen, the menu bar is an ordinary screen
11253 line, and this makes it get updated. */
11254 w->update_mode_line = 1;
11255 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11256 /* In the non-toolkit version, the menu bar is an ordinary screen
11257 line, and this makes it get updated. */
11258 w->update_mode_line = 1;
11259 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11260
11261 unbind_to (count, Qnil);
11262 set_buffer_internal_1 (prev);
11263 }
11264 }
11265
11266 return hooks_run;
11267 }
11268
11269
11270 \f
11271 /***********************************************************************
11272 Output Cursor
11273 ***********************************************************************/
11274
11275 #ifdef HAVE_WINDOW_SYSTEM
11276
11277 /* EXPORT:
11278 Nominal cursor position -- where to draw output.
11279 HPOS and VPOS are window relative glyph matrix coordinates.
11280 X and Y are window relative pixel coordinates. */
11281
11282 struct cursor_pos output_cursor;
11283
11284
11285 /* EXPORT:
11286 Set the global variable output_cursor to CURSOR. All cursor
11287 positions are relative to updated_window. */
11288
11289 void
11290 set_output_cursor (struct cursor_pos *cursor)
11291 {
11292 output_cursor.hpos = cursor->hpos;
11293 output_cursor.vpos = cursor->vpos;
11294 output_cursor.x = cursor->x;
11295 output_cursor.y = cursor->y;
11296 }
11297
11298
11299 /* EXPORT for RIF:
11300 Set a nominal cursor position.
11301
11302 HPOS and VPOS are column/row positions in a window glyph matrix. X
11303 and Y are window text area relative pixel positions.
11304
11305 If this is done during an update, updated_window will contain the
11306 window that is being updated and the position is the future output
11307 cursor position for that window. If updated_window is null, use
11308 selected_window and display the cursor at the given position. */
11309
11310 void
11311 x_cursor_to (int vpos, int hpos, int y, int x)
11312 {
11313 struct window *w;
11314
11315 /* If updated_window is not set, work on selected_window. */
11316 if (updated_window)
11317 w = updated_window;
11318 else
11319 w = XWINDOW (selected_window);
11320
11321 /* Set the output cursor. */
11322 output_cursor.hpos = hpos;
11323 output_cursor.vpos = vpos;
11324 output_cursor.x = x;
11325 output_cursor.y = y;
11326
11327 /* If not called as part of an update, really display the cursor.
11328 This will also set the cursor position of W. */
11329 if (updated_window == NULL)
11330 {
11331 BLOCK_INPUT;
11332 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11333 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11334 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11335 UNBLOCK_INPUT;
11336 }
11337 }
11338
11339 #endif /* HAVE_WINDOW_SYSTEM */
11340
11341 \f
11342 /***********************************************************************
11343 Tool-bars
11344 ***********************************************************************/
11345
11346 #ifdef HAVE_WINDOW_SYSTEM
11347
11348 /* Where the mouse was last time we reported a mouse event. */
11349
11350 FRAME_PTR last_mouse_frame;
11351
11352 /* Tool-bar item index of the item on which a mouse button was pressed
11353 or -1. */
11354
11355 int last_tool_bar_item;
11356
11357
11358 static Lisp_Object
11359 update_tool_bar_unwind (Lisp_Object frame)
11360 {
11361 selected_frame = frame;
11362 return Qnil;
11363 }
11364
11365 /* Update the tool-bar item list for frame F. This has to be done
11366 before we start to fill in any display lines. Called from
11367 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11368 and restore it here. */
11369
11370 static void
11371 update_tool_bar (struct frame *f, int save_match_data)
11372 {
11373 #if defined (USE_GTK) || defined (HAVE_NS)
11374 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11375 #else
11376 int do_update = WINDOWP (f->tool_bar_window)
11377 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11378 #endif
11379
11380 if (do_update)
11381 {
11382 Lisp_Object window;
11383 struct window *w;
11384
11385 window = FRAME_SELECTED_WINDOW (f);
11386 w = XWINDOW (window);
11387
11388 /* If the user has switched buffers or windows, we need to
11389 recompute to reflect the new bindings. But we'll
11390 recompute when update_mode_lines is set too; that means
11391 that people can use force-mode-line-update to request
11392 that the menu bar be recomputed. The adverse effect on
11393 the rest of the redisplay algorithm is about the same as
11394 windows_or_buffers_changed anyway. */
11395 if (windows_or_buffers_changed
11396 || w->update_mode_line
11397 || update_mode_lines
11398 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11399 < BUF_MODIFF (XBUFFER (w->buffer)))
11400 != w->last_had_star)
11401 || ((!NILP (Vtransient_mark_mode)
11402 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11403 != !NILP (w->region_showing)))
11404 {
11405 struct buffer *prev = current_buffer;
11406 ptrdiff_t count = SPECPDL_INDEX ();
11407 Lisp_Object frame, new_tool_bar;
11408 int new_n_tool_bar;
11409 struct gcpro gcpro1;
11410
11411 /* Set current_buffer to the buffer of the selected
11412 window of the frame, so that we get the right local
11413 keymaps. */
11414 set_buffer_internal_1 (XBUFFER (w->buffer));
11415
11416 /* Save match data, if we must. */
11417 if (save_match_data)
11418 record_unwind_save_match_data ();
11419
11420 /* Make sure that we don't accidentally use bogus keymaps. */
11421 if (NILP (Voverriding_local_map_menu_flag))
11422 {
11423 specbind (Qoverriding_terminal_local_map, Qnil);
11424 specbind (Qoverriding_local_map, Qnil);
11425 }
11426
11427 GCPRO1 (new_tool_bar);
11428
11429 /* We must temporarily set the selected frame to this frame
11430 before calling tool_bar_items, because the calculation of
11431 the tool-bar keymap uses the selected frame (see
11432 `tool-bar-make-keymap' in tool-bar.el). */
11433 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11434 XSETFRAME (frame, f);
11435 selected_frame = frame;
11436
11437 /* Build desired tool-bar items from keymaps. */
11438 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11439 &new_n_tool_bar);
11440
11441 /* Redisplay the tool-bar if we changed it. */
11442 if (new_n_tool_bar != f->n_tool_bar_items
11443 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11444 {
11445 /* Redisplay that happens asynchronously due to an expose event
11446 may access f->tool_bar_items. Make sure we update both
11447 variables within BLOCK_INPUT so no such event interrupts. */
11448 BLOCK_INPUT;
11449 f->tool_bar_items = new_tool_bar;
11450 f->n_tool_bar_items = new_n_tool_bar;
11451 w->update_mode_line = 1;
11452 UNBLOCK_INPUT;
11453 }
11454
11455 UNGCPRO;
11456
11457 unbind_to (count, Qnil);
11458 set_buffer_internal_1 (prev);
11459 }
11460 }
11461 }
11462
11463
11464 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11465 F's desired tool-bar contents. F->tool_bar_items must have
11466 been set up previously by calling prepare_menu_bars. */
11467
11468 static void
11469 build_desired_tool_bar_string (struct frame *f)
11470 {
11471 int i, size, size_needed;
11472 struct gcpro gcpro1, gcpro2, gcpro3;
11473 Lisp_Object image, plist, props;
11474
11475 image = plist = props = Qnil;
11476 GCPRO3 (image, plist, props);
11477
11478 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11479 Otherwise, make a new string. */
11480
11481 /* The size of the string we might be able to reuse. */
11482 size = (STRINGP (f->desired_tool_bar_string)
11483 ? SCHARS (f->desired_tool_bar_string)
11484 : 0);
11485
11486 /* We need one space in the string for each image. */
11487 size_needed = f->n_tool_bar_items;
11488
11489 /* Reuse f->desired_tool_bar_string, if possible. */
11490 if (size < size_needed || NILP (f->desired_tool_bar_string))
11491 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11492 make_number (' '));
11493 else
11494 {
11495 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11496 Fremove_text_properties (make_number (0), make_number (size),
11497 props, f->desired_tool_bar_string);
11498 }
11499
11500 /* Put a `display' property on the string for the images to display,
11501 put a `menu_item' property on tool-bar items with a value that
11502 is the index of the item in F's tool-bar item vector. */
11503 for (i = 0; i < f->n_tool_bar_items; ++i)
11504 {
11505 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11506
11507 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11508 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11509 int hmargin, vmargin, relief, idx, end;
11510
11511 /* If image is a vector, choose the image according to the
11512 button state. */
11513 image = PROP (TOOL_BAR_ITEM_IMAGES);
11514 if (VECTORP (image))
11515 {
11516 if (enabled_p)
11517 idx = (selected_p
11518 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11519 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11520 else
11521 idx = (selected_p
11522 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11523 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11524
11525 eassert (ASIZE (image) >= idx);
11526 image = AREF (image, idx);
11527 }
11528 else
11529 idx = -1;
11530
11531 /* Ignore invalid image specifications. */
11532 if (!valid_image_p (image))
11533 continue;
11534
11535 /* Display the tool-bar button pressed, or depressed. */
11536 plist = Fcopy_sequence (XCDR (image));
11537
11538 /* Compute margin and relief to draw. */
11539 relief = (tool_bar_button_relief >= 0
11540 ? tool_bar_button_relief
11541 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11542 hmargin = vmargin = relief;
11543
11544 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11545 INT_MAX - max (hmargin, vmargin)))
11546 {
11547 hmargin += XFASTINT (Vtool_bar_button_margin);
11548 vmargin += XFASTINT (Vtool_bar_button_margin);
11549 }
11550 else if (CONSP (Vtool_bar_button_margin))
11551 {
11552 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11553 INT_MAX - hmargin))
11554 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11555
11556 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11557 INT_MAX - vmargin))
11558 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11559 }
11560
11561 if (auto_raise_tool_bar_buttons_p)
11562 {
11563 /* Add a `:relief' property to the image spec if the item is
11564 selected. */
11565 if (selected_p)
11566 {
11567 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11568 hmargin -= relief;
11569 vmargin -= relief;
11570 }
11571 }
11572 else
11573 {
11574 /* If image is selected, display it pressed, i.e. with a
11575 negative relief. If it's not selected, display it with a
11576 raised relief. */
11577 plist = Fplist_put (plist, QCrelief,
11578 (selected_p
11579 ? make_number (-relief)
11580 : make_number (relief)));
11581 hmargin -= relief;
11582 vmargin -= relief;
11583 }
11584
11585 /* Put a margin around the image. */
11586 if (hmargin || vmargin)
11587 {
11588 if (hmargin == vmargin)
11589 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11590 else
11591 plist = Fplist_put (plist, QCmargin,
11592 Fcons (make_number (hmargin),
11593 make_number (vmargin)));
11594 }
11595
11596 /* If button is not enabled, and we don't have special images
11597 for the disabled state, make the image appear disabled by
11598 applying an appropriate algorithm to it. */
11599 if (!enabled_p && idx < 0)
11600 plist = Fplist_put (plist, QCconversion, Qdisabled);
11601
11602 /* Put a `display' text property on the string for the image to
11603 display. Put a `menu-item' property on the string that gives
11604 the start of this item's properties in the tool-bar items
11605 vector. */
11606 image = Fcons (Qimage, plist);
11607 props = list4 (Qdisplay, image,
11608 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11609
11610 /* Let the last image hide all remaining spaces in the tool bar
11611 string. The string can be longer than needed when we reuse a
11612 previous string. */
11613 if (i + 1 == f->n_tool_bar_items)
11614 end = SCHARS (f->desired_tool_bar_string);
11615 else
11616 end = i + 1;
11617 Fadd_text_properties (make_number (i), make_number (end),
11618 props, f->desired_tool_bar_string);
11619 #undef PROP
11620 }
11621
11622 UNGCPRO;
11623 }
11624
11625
11626 /* Display one line of the tool-bar of frame IT->f.
11627
11628 HEIGHT specifies the desired height of the tool-bar line.
11629 If the actual height of the glyph row is less than HEIGHT, the
11630 row's height is increased to HEIGHT, and the icons are centered
11631 vertically in the new height.
11632
11633 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11634 count a final empty row in case the tool-bar width exactly matches
11635 the window width.
11636 */
11637
11638 static void
11639 display_tool_bar_line (struct it *it, int height)
11640 {
11641 struct glyph_row *row = it->glyph_row;
11642 int max_x = it->last_visible_x;
11643 struct glyph *last;
11644
11645 prepare_desired_row (row);
11646 row->y = it->current_y;
11647
11648 /* Note that this isn't made use of if the face hasn't a box,
11649 so there's no need to check the face here. */
11650 it->start_of_box_run_p = 1;
11651
11652 while (it->current_x < max_x)
11653 {
11654 int x, n_glyphs_before, i, nglyphs;
11655 struct it it_before;
11656
11657 /* Get the next display element. */
11658 if (!get_next_display_element (it))
11659 {
11660 /* Don't count empty row if we are counting needed tool-bar lines. */
11661 if (height < 0 && !it->hpos)
11662 return;
11663 break;
11664 }
11665
11666 /* Produce glyphs. */
11667 n_glyphs_before = row->used[TEXT_AREA];
11668 it_before = *it;
11669
11670 PRODUCE_GLYPHS (it);
11671
11672 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11673 i = 0;
11674 x = it_before.current_x;
11675 while (i < nglyphs)
11676 {
11677 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11678
11679 if (x + glyph->pixel_width > max_x)
11680 {
11681 /* Glyph doesn't fit on line. Backtrack. */
11682 row->used[TEXT_AREA] = n_glyphs_before;
11683 *it = it_before;
11684 /* If this is the only glyph on this line, it will never fit on the
11685 tool-bar, so skip it. But ensure there is at least one glyph,
11686 so we don't accidentally disable the tool-bar. */
11687 if (n_glyphs_before == 0
11688 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11689 break;
11690 goto out;
11691 }
11692
11693 ++it->hpos;
11694 x += glyph->pixel_width;
11695 ++i;
11696 }
11697
11698 /* Stop at line end. */
11699 if (ITERATOR_AT_END_OF_LINE_P (it))
11700 break;
11701
11702 set_iterator_to_next (it, 1);
11703 }
11704
11705 out:;
11706
11707 row->displays_text_p = row->used[TEXT_AREA] != 0;
11708
11709 /* Use default face for the border below the tool bar.
11710
11711 FIXME: When auto-resize-tool-bars is grow-only, there is
11712 no additional border below the possibly empty tool-bar lines.
11713 So to make the extra empty lines look "normal", we have to
11714 use the tool-bar face for the border too. */
11715 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11716 it->face_id = DEFAULT_FACE_ID;
11717
11718 extend_face_to_end_of_line (it);
11719 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11720 last->right_box_line_p = 1;
11721 if (last == row->glyphs[TEXT_AREA])
11722 last->left_box_line_p = 1;
11723
11724 /* Make line the desired height and center it vertically. */
11725 if ((height -= it->max_ascent + it->max_descent) > 0)
11726 {
11727 /* Don't add more than one line height. */
11728 height %= FRAME_LINE_HEIGHT (it->f);
11729 it->max_ascent += height / 2;
11730 it->max_descent += (height + 1) / 2;
11731 }
11732
11733 compute_line_metrics (it);
11734
11735 /* If line is empty, make it occupy the rest of the tool-bar. */
11736 if (!row->displays_text_p)
11737 {
11738 row->height = row->phys_height = it->last_visible_y - row->y;
11739 row->visible_height = row->height;
11740 row->ascent = row->phys_ascent = 0;
11741 row->extra_line_spacing = 0;
11742 }
11743
11744 row->full_width_p = 1;
11745 row->continued_p = 0;
11746 row->truncated_on_left_p = 0;
11747 row->truncated_on_right_p = 0;
11748
11749 it->current_x = it->hpos = 0;
11750 it->current_y += row->height;
11751 ++it->vpos;
11752 ++it->glyph_row;
11753 }
11754
11755
11756 /* Max tool-bar height. */
11757
11758 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11759 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11760
11761 /* Value is the number of screen lines needed to make all tool-bar
11762 items of frame F visible. The number of actual rows needed is
11763 returned in *N_ROWS if non-NULL. */
11764
11765 static int
11766 tool_bar_lines_needed (struct frame *f, int *n_rows)
11767 {
11768 struct window *w = XWINDOW (f->tool_bar_window);
11769 struct it it;
11770 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11771 the desired matrix, so use (unused) mode-line row as temporary row to
11772 avoid destroying the first tool-bar row. */
11773 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11774
11775 /* Initialize an iterator for iteration over
11776 F->desired_tool_bar_string in the tool-bar window of frame F. */
11777 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11778 it.first_visible_x = 0;
11779 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11780 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11781 it.paragraph_embedding = L2R;
11782
11783 while (!ITERATOR_AT_END_P (&it))
11784 {
11785 clear_glyph_row (temp_row);
11786 it.glyph_row = temp_row;
11787 display_tool_bar_line (&it, -1);
11788 }
11789 clear_glyph_row (temp_row);
11790
11791 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11792 if (n_rows)
11793 *n_rows = it.vpos > 0 ? it.vpos : -1;
11794
11795 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11796 }
11797
11798
11799 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11800 0, 1, 0,
11801 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11802 (Lisp_Object frame)
11803 {
11804 struct frame *f;
11805 struct window *w;
11806 int nlines = 0;
11807
11808 if (NILP (frame))
11809 frame = selected_frame;
11810 else
11811 CHECK_FRAME (frame);
11812 f = XFRAME (frame);
11813
11814 if (WINDOWP (f->tool_bar_window)
11815 && (w = XWINDOW (f->tool_bar_window),
11816 WINDOW_TOTAL_LINES (w) > 0))
11817 {
11818 update_tool_bar (f, 1);
11819 if (f->n_tool_bar_items)
11820 {
11821 build_desired_tool_bar_string (f);
11822 nlines = tool_bar_lines_needed (f, NULL);
11823 }
11824 }
11825
11826 return make_number (nlines);
11827 }
11828
11829
11830 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11831 height should be changed. */
11832
11833 static int
11834 redisplay_tool_bar (struct frame *f)
11835 {
11836 struct window *w;
11837 struct it it;
11838 struct glyph_row *row;
11839
11840 #if defined (USE_GTK) || defined (HAVE_NS)
11841 if (FRAME_EXTERNAL_TOOL_BAR (f))
11842 update_frame_tool_bar (f);
11843 return 0;
11844 #endif
11845
11846 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11847 do anything. This means you must start with tool-bar-lines
11848 non-zero to get the auto-sizing effect. Or in other words, you
11849 can turn off tool-bars by specifying tool-bar-lines zero. */
11850 if (!WINDOWP (f->tool_bar_window)
11851 || (w = XWINDOW (f->tool_bar_window),
11852 WINDOW_TOTAL_LINES (w) == 0))
11853 return 0;
11854
11855 /* Set up an iterator for the tool-bar window. */
11856 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11857 it.first_visible_x = 0;
11858 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11859 row = it.glyph_row;
11860
11861 /* Build a string that represents the contents of the tool-bar. */
11862 build_desired_tool_bar_string (f);
11863 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11864 /* FIXME: This should be controlled by a user option. But it
11865 doesn't make sense to have an R2L tool bar if the menu bar cannot
11866 be drawn also R2L, and making the menu bar R2L is tricky due
11867 toolkit-specific code that implements it. If an R2L tool bar is
11868 ever supported, display_tool_bar_line should also be augmented to
11869 call unproduce_glyphs like display_line and display_string
11870 do. */
11871 it.paragraph_embedding = L2R;
11872
11873 if (f->n_tool_bar_rows == 0)
11874 {
11875 int nlines;
11876
11877 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11878 nlines != WINDOW_TOTAL_LINES (w)))
11879 {
11880 Lisp_Object frame;
11881 int old_height = WINDOW_TOTAL_LINES (w);
11882
11883 XSETFRAME (frame, f);
11884 Fmodify_frame_parameters (frame,
11885 Fcons (Fcons (Qtool_bar_lines,
11886 make_number (nlines)),
11887 Qnil));
11888 if (WINDOW_TOTAL_LINES (w) != old_height)
11889 {
11890 clear_glyph_matrix (w->desired_matrix);
11891 fonts_changed_p = 1;
11892 return 1;
11893 }
11894 }
11895 }
11896
11897 /* Display as many lines as needed to display all tool-bar items. */
11898
11899 if (f->n_tool_bar_rows > 0)
11900 {
11901 int border, rows, height, extra;
11902
11903 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11904 border = XINT (Vtool_bar_border);
11905 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11906 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11907 else if (EQ (Vtool_bar_border, Qborder_width))
11908 border = f->border_width;
11909 else
11910 border = 0;
11911 if (border < 0)
11912 border = 0;
11913
11914 rows = f->n_tool_bar_rows;
11915 height = max (1, (it.last_visible_y - border) / rows);
11916 extra = it.last_visible_y - border - height * rows;
11917
11918 while (it.current_y < it.last_visible_y)
11919 {
11920 int h = 0;
11921 if (extra > 0 && rows-- > 0)
11922 {
11923 h = (extra + rows - 1) / rows;
11924 extra -= h;
11925 }
11926 display_tool_bar_line (&it, height + h);
11927 }
11928 }
11929 else
11930 {
11931 while (it.current_y < it.last_visible_y)
11932 display_tool_bar_line (&it, 0);
11933 }
11934
11935 /* It doesn't make much sense to try scrolling in the tool-bar
11936 window, so don't do it. */
11937 w->desired_matrix->no_scrolling_p = 1;
11938 w->must_be_updated_p = 1;
11939
11940 if (!NILP (Vauto_resize_tool_bars))
11941 {
11942 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11943 int change_height_p = 0;
11944
11945 /* If we couldn't display everything, change the tool-bar's
11946 height if there is room for more. */
11947 if (IT_STRING_CHARPOS (it) < it.end_charpos
11948 && it.current_y < max_tool_bar_height)
11949 change_height_p = 1;
11950
11951 row = it.glyph_row - 1;
11952
11953 /* If there are blank lines at the end, except for a partially
11954 visible blank line at the end that is smaller than
11955 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11956 if (!row->displays_text_p
11957 && row->height >= FRAME_LINE_HEIGHT (f))
11958 change_height_p = 1;
11959
11960 /* If row displays tool-bar items, but is partially visible,
11961 change the tool-bar's height. */
11962 if (row->displays_text_p
11963 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11964 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11965 change_height_p = 1;
11966
11967 /* Resize windows as needed by changing the `tool-bar-lines'
11968 frame parameter. */
11969 if (change_height_p)
11970 {
11971 Lisp_Object frame;
11972 int old_height = WINDOW_TOTAL_LINES (w);
11973 int nrows;
11974 int nlines = tool_bar_lines_needed (f, &nrows);
11975
11976 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11977 && !f->minimize_tool_bar_window_p)
11978 ? (nlines > old_height)
11979 : (nlines != old_height));
11980 f->minimize_tool_bar_window_p = 0;
11981
11982 if (change_height_p)
11983 {
11984 XSETFRAME (frame, f);
11985 Fmodify_frame_parameters (frame,
11986 Fcons (Fcons (Qtool_bar_lines,
11987 make_number (nlines)),
11988 Qnil));
11989 if (WINDOW_TOTAL_LINES (w) != old_height)
11990 {
11991 clear_glyph_matrix (w->desired_matrix);
11992 f->n_tool_bar_rows = nrows;
11993 fonts_changed_p = 1;
11994 return 1;
11995 }
11996 }
11997 }
11998 }
11999
12000 f->minimize_tool_bar_window_p = 0;
12001 return 0;
12002 }
12003
12004
12005 /* Get information about the tool-bar item which is displayed in GLYPH
12006 on frame F. Return in *PROP_IDX the index where tool-bar item
12007 properties start in F->tool_bar_items. Value is zero if
12008 GLYPH doesn't display a tool-bar item. */
12009
12010 static int
12011 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12012 {
12013 Lisp_Object prop;
12014 int success_p;
12015 int charpos;
12016
12017 /* This function can be called asynchronously, which means we must
12018 exclude any possibility that Fget_text_property signals an
12019 error. */
12020 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12021 charpos = max (0, charpos);
12022
12023 /* Get the text property `menu-item' at pos. The value of that
12024 property is the start index of this item's properties in
12025 F->tool_bar_items. */
12026 prop = Fget_text_property (make_number (charpos),
12027 Qmenu_item, f->current_tool_bar_string);
12028 if (INTEGERP (prop))
12029 {
12030 *prop_idx = XINT (prop);
12031 success_p = 1;
12032 }
12033 else
12034 success_p = 0;
12035
12036 return success_p;
12037 }
12038
12039 \f
12040 /* Get information about the tool-bar item at position X/Y on frame F.
12041 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12042 the current matrix of the tool-bar window of F, or NULL if not
12043 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12044 item in F->tool_bar_items. Value is
12045
12046 -1 if X/Y is not on a tool-bar item
12047 0 if X/Y is on the same item that was highlighted before.
12048 1 otherwise. */
12049
12050 static int
12051 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12052 int *hpos, int *vpos, int *prop_idx)
12053 {
12054 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12055 struct window *w = XWINDOW (f->tool_bar_window);
12056 int area;
12057
12058 /* Find the glyph under X/Y. */
12059 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12060 if (*glyph == NULL)
12061 return -1;
12062
12063 /* Get the start of this tool-bar item's properties in
12064 f->tool_bar_items. */
12065 if (!tool_bar_item_info (f, *glyph, prop_idx))
12066 return -1;
12067
12068 /* Is mouse on the highlighted item? */
12069 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12070 && *vpos >= hlinfo->mouse_face_beg_row
12071 && *vpos <= hlinfo->mouse_face_end_row
12072 && (*vpos > hlinfo->mouse_face_beg_row
12073 || *hpos >= hlinfo->mouse_face_beg_col)
12074 && (*vpos < hlinfo->mouse_face_end_row
12075 || *hpos < hlinfo->mouse_face_end_col
12076 || hlinfo->mouse_face_past_end))
12077 return 0;
12078
12079 return 1;
12080 }
12081
12082
12083 /* EXPORT:
12084 Handle mouse button event on the tool-bar of frame F, at
12085 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12086 0 for button release. MODIFIERS is event modifiers for button
12087 release. */
12088
12089 void
12090 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12091 int modifiers)
12092 {
12093 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12094 struct window *w = XWINDOW (f->tool_bar_window);
12095 int hpos, vpos, prop_idx;
12096 struct glyph *glyph;
12097 Lisp_Object enabled_p;
12098
12099 /* If not on the highlighted tool-bar item, return. */
12100 frame_to_window_pixel_xy (w, &x, &y);
12101 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12102 return;
12103
12104 /* If item is disabled, do nothing. */
12105 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12106 if (NILP (enabled_p))
12107 return;
12108
12109 if (down_p)
12110 {
12111 /* Show item in pressed state. */
12112 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12113 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12114 last_tool_bar_item = prop_idx;
12115 }
12116 else
12117 {
12118 Lisp_Object key, frame;
12119 struct input_event event;
12120 EVENT_INIT (event);
12121
12122 /* Show item in released state. */
12123 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12124 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12125
12126 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12127
12128 XSETFRAME (frame, f);
12129 event.kind = TOOL_BAR_EVENT;
12130 event.frame_or_window = frame;
12131 event.arg = frame;
12132 kbd_buffer_store_event (&event);
12133
12134 event.kind = TOOL_BAR_EVENT;
12135 event.frame_or_window = frame;
12136 event.arg = key;
12137 event.modifiers = modifiers;
12138 kbd_buffer_store_event (&event);
12139 last_tool_bar_item = -1;
12140 }
12141 }
12142
12143
12144 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12145 tool-bar window-relative coordinates X/Y. Called from
12146 note_mouse_highlight. */
12147
12148 static void
12149 note_tool_bar_highlight (struct frame *f, int x, int y)
12150 {
12151 Lisp_Object window = f->tool_bar_window;
12152 struct window *w = XWINDOW (window);
12153 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12154 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12155 int hpos, vpos;
12156 struct glyph *glyph;
12157 struct glyph_row *row;
12158 int i;
12159 Lisp_Object enabled_p;
12160 int prop_idx;
12161 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12162 int mouse_down_p, rc;
12163
12164 /* Function note_mouse_highlight is called with negative X/Y
12165 values when mouse moves outside of the frame. */
12166 if (x <= 0 || y <= 0)
12167 {
12168 clear_mouse_face (hlinfo);
12169 return;
12170 }
12171
12172 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12173 if (rc < 0)
12174 {
12175 /* Not on tool-bar item. */
12176 clear_mouse_face (hlinfo);
12177 return;
12178 }
12179 else if (rc == 0)
12180 /* On same tool-bar item as before. */
12181 goto set_help_echo;
12182
12183 clear_mouse_face (hlinfo);
12184
12185 /* Mouse is down, but on different tool-bar item? */
12186 mouse_down_p = (dpyinfo->grabbed
12187 && f == last_mouse_frame
12188 && FRAME_LIVE_P (f));
12189 if (mouse_down_p
12190 && last_tool_bar_item != prop_idx)
12191 return;
12192
12193 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12194 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12195
12196 /* If tool-bar item is not enabled, don't highlight it. */
12197 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12198 if (!NILP (enabled_p))
12199 {
12200 /* Compute the x-position of the glyph. In front and past the
12201 image is a space. We include this in the highlighted area. */
12202 row = MATRIX_ROW (w->current_matrix, vpos);
12203 for (i = x = 0; i < hpos; ++i)
12204 x += row->glyphs[TEXT_AREA][i].pixel_width;
12205
12206 /* Record this as the current active region. */
12207 hlinfo->mouse_face_beg_col = hpos;
12208 hlinfo->mouse_face_beg_row = vpos;
12209 hlinfo->mouse_face_beg_x = x;
12210 hlinfo->mouse_face_beg_y = row->y;
12211 hlinfo->mouse_face_past_end = 0;
12212
12213 hlinfo->mouse_face_end_col = hpos + 1;
12214 hlinfo->mouse_face_end_row = vpos;
12215 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12216 hlinfo->mouse_face_end_y = row->y;
12217 hlinfo->mouse_face_window = window;
12218 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12219
12220 /* Display it as active. */
12221 show_mouse_face (hlinfo, draw);
12222 hlinfo->mouse_face_image_state = draw;
12223 }
12224
12225 set_help_echo:
12226
12227 /* Set help_echo_string to a help string to display for this tool-bar item.
12228 XTread_socket does the rest. */
12229 help_echo_object = help_echo_window = Qnil;
12230 help_echo_pos = -1;
12231 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12232 if (NILP (help_echo_string))
12233 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12234 }
12235
12236 #endif /* HAVE_WINDOW_SYSTEM */
12237
12238
12239 \f
12240 /************************************************************************
12241 Horizontal scrolling
12242 ************************************************************************/
12243
12244 static int hscroll_window_tree (Lisp_Object);
12245 static int hscroll_windows (Lisp_Object);
12246
12247 /* For all leaf windows in the window tree rooted at WINDOW, set their
12248 hscroll value so that PT is (i) visible in the window, and (ii) so
12249 that it is not within a certain margin at the window's left and
12250 right border. Value is non-zero if any window's hscroll has been
12251 changed. */
12252
12253 static int
12254 hscroll_window_tree (Lisp_Object window)
12255 {
12256 int hscrolled_p = 0;
12257 int hscroll_relative_p = FLOATP (Vhscroll_step);
12258 int hscroll_step_abs = 0;
12259 double hscroll_step_rel = 0;
12260
12261 if (hscroll_relative_p)
12262 {
12263 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12264 if (hscroll_step_rel < 0)
12265 {
12266 hscroll_relative_p = 0;
12267 hscroll_step_abs = 0;
12268 }
12269 }
12270 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12271 {
12272 hscroll_step_abs = XINT (Vhscroll_step);
12273 if (hscroll_step_abs < 0)
12274 hscroll_step_abs = 0;
12275 }
12276 else
12277 hscroll_step_abs = 0;
12278
12279 while (WINDOWP (window))
12280 {
12281 struct window *w = XWINDOW (window);
12282
12283 if (WINDOWP (w->hchild))
12284 hscrolled_p |= hscroll_window_tree (w->hchild);
12285 else if (WINDOWP (w->vchild))
12286 hscrolled_p |= hscroll_window_tree (w->vchild);
12287 else if (w->cursor.vpos >= 0)
12288 {
12289 int h_margin;
12290 int text_area_width;
12291 struct glyph_row *current_cursor_row
12292 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12293 struct glyph_row *desired_cursor_row
12294 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12295 struct glyph_row *cursor_row
12296 = (desired_cursor_row->enabled_p
12297 ? desired_cursor_row
12298 : current_cursor_row);
12299 int row_r2l_p = cursor_row->reversed_p;
12300
12301 text_area_width = window_box_width (w, TEXT_AREA);
12302
12303 /* Scroll when cursor is inside this scroll margin. */
12304 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12305
12306 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12307 /* For left-to-right rows, hscroll when cursor is either
12308 (i) inside the right hscroll margin, or (ii) if it is
12309 inside the left margin and the window is already
12310 hscrolled. */
12311 && ((!row_r2l_p
12312 && ((w->hscroll
12313 && w->cursor.x <= h_margin)
12314 || (cursor_row->enabled_p
12315 && cursor_row->truncated_on_right_p
12316 && (w->cursor.x >= text_area_width - h_margin))))
12317 /* For right-to-left rows, the logic is similar,
12318 except that rules for scrolling to left and right
12319 are reversed. E.g., if cursor.x <= h_margin, we
12320 need to hscroll "to the right" unconditionally,
12321 and that will scroll the screen to the left so as
12322 to reveal the next portion of the row. */
12323 || (row_r2l_p
12324 && ((cursor_row->enabled_p
12325 /* FIXME: It is confusing to set the
12326 truncated_on_right_p flag when R2L rows
12327 are actually truncated on the left. */
12328 && cursor_row->truncated_on_right_p
12329 && w->cursor.x <= h_margin)
12330 || (w->hscroll
12331 && (w->cursor.x >= text_area_width - h_margin))))))
12332 {
12333 struct it it;
12334 ptrdiff_t hscroll;
12335 struct buffer *saved_current_buffer;
12336 ptrdiff_t pt;
12337 int wanted_x;
12338
12339 /* Find point in a display of infinite width. */
12340 saved_current_buffer = current_buffer;
12341 current_buffer = XBUFFER (w->buffer);
12342
12343 if (w == XWINDOW (selected_window))
12344 pt = PT;
12345 else
12346 {
12347 pt = marker_position (w->pointm);
12348 pt = max (BEGV, pt);
12349 pt = min (ZV, pt);
12350 }
12351
12352 /* Move iterator to pt starting at cursor_row->start in
12353 a line with infinite width. */
12354 init_to_row_start (&it, w, cursor_row);
12355 it.last_visible_x = INFINITY;
12356 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12357 current_buffer = saved_current_buffer;
12358
12359 /* Position cursor in window. */
12360 if (!hscroll_relative_p && hscroll_step_abs == 0)
12361 hscroll = max (0, (it.current_x
12362 - (ITERATOR_AT_END_OF_LINE_P (&it)
12363 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12364 : (text_area_width / 2))))
12365 / FRAME_COLUMN_WIDTH (it.f);
12366 else if ((!row_r2l_p
12367 && w->cursor.x >= text_area_width - h_margin)
12368 || (row_r2l_p && w->cursor.x <= h_margin))
12369 {
12370 if (hscroll_relative_p)
12371 wanted_x = text_area_width * (1 - hscroll_step_rel)
12372 - h_margin;
12373 else
12374 wanted_x = text_area_width
12375 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12376 - h_margin;
12377 hscroll
12378 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12379 }
12380 else
12381 {
12382 if (hscroll_relative_p)
12383 wanted_x = text_area_width * hscroll_step_rel
12384 + h_margin;
12385 else
12386 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12387 + h_margin;
12388 hscroll
12389 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12390 }
12391 hscroll = max (hscroll, w->min_hscroll);
12392
12393 /* Don't prevent redisplay optimizations if hscroll
12394 hasn't changed, as it will unnecessarily slow down
12395 redisplay. */
12396 if (w->hscroll != hscroll)
12397 {
12398 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12399 w->hscroll = hscroll;
12400 hscrolled_p = 1;
12401 }
12402 }
12403 }
12404
12405 window = w->next;
12406 }
12407
12408 /* Value is non-zero if hscroll of any leaf window has been changed. */
12409 return hscrolled_p;
12410 }
12411
12412
12413 /* Set hscroll so that cursor is visible and not inside horizontal
12414 scroll margins for all windows in the tree rooted at WINDOW. See
12415 also hscroll_window_tree above. Value is non-zero if any window's
12416 hscroll has been changed. If it has, desired matrices on the frame
12417 of WINDOW are cleared. */
12418
12419 static int
12420 hscroll_windows (Lisp_Object window)
12421 {
12422 int hscrolled_p = hscroll_window_tree (window);
12423 if (hscrolled_p)
12424 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12425 return hscrolled_p;
12426 }
12427
12428
12429 \f
12430 /************************************************************************
12431 Redisplay
12432 ************************************************************************/
12433
12434 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12435 to a non-zero value. This is sometimes handy to have in a debugger
12436 session. */
12437
12438 #ifdef GLYPH_DEBUG
12439
12440 /* First and last unchanged row for try_window_id. */
12441
12442 static int debug_first_unchanged_at_end_vpos;
12443 static int debug_last_unchanged_at_beg_vpos;
12444
12445 /* Delta vpos and y. */
12446
12447 static int debug_dvpos, debug_dy;
12448
12449 /* Delta in characters and bytes for try_window_id. */
12450
12451 static ptrdiff_t debug_delta, debug_delta_bytes;
12452
12453 /* Values of window_end_pos and window_end_vpos at the end of
12454 try_window_id. */
12455
12456 static ptrdiff_t debug_end_vpos;
12457
12458 /* Append a string to W->desired_matrix->method. FMT is a printf
12459 format string. If trace_redisplay_p is non-zero also printf the
12460 resulting string to stderr. */
12461
12462 static void debug_method_add (struct window *, char const *, ...)
12463 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12464
12465 static void
12466 debug_method_add (struct window *w, char const *fmt, ...)
12467 {
12468 char buffer[512];
12469 char *method = w->desired_matrix->method;
12470 int len = strlen (method);
12471 int size = sizeof w->desired_matrix->method;
12472 int remaining = size - len - 1;
12473 va_list ap;
12474
12475 va_start (ap, fmt);
12476 vsprintf (buffer, fmt, ap);
12477 va_end (ap);
12478 if (len && remaining)
12479 {
12480 method[len] = '|';
12481 --remaining, ++len;
12482 }
12483
12484 strncpy (method + len, buffer, remaining);
12485
12486 if (trace_redisplay_p)
12487 fprintf (stderr, "%p (%s): %s\n",
12488 w,
12489 ((BUFFERP (w->buffer)
12490 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12491 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12492 : "no buffer"),
12493 buffer);
12494 }
12495
12496 #endif /* GLYPH_DEBUG */
12497
12498
12499 /* Value is non-zero if all changes in window W, which displays
12500 current_buffer, are in the text between START and END. START is a
12501 buffer position, END is given as a distance from Z. Used in
12502 redisplay_internal for display optimization. */
12503
12504 static inline int
12505 text_outside_line_unchanged_p (struct window *w,
12506 ptrdiff_t start, ptrdiff_t end)
12507 {
12508 int unchanged_p = 1;
12509
12510 /* If text or overlays have changed, see where. */
12511 if (w->last_modified < MODIFF
12512 || w->last_overlay_modified < OVERLAY_MODIFF)
12513 {
12514 /* Gap in the line? */
12515 if (GPT < start || Z - GPT < end)
12516 unchanged_p = 0;
12517
12518 /* Changes start in front of the line, or end after it? */
12519 if (unchanged_p
12520 && (BEG_UNCHANGED < start - 1
12521 || END_UNCHANGED < end))
12522 unchanged_p = 0;
12523
12524 /* If selective display, can't optimize if changes start at the
12525 beginning of the line. */
12526 if (unchanged_p
12527 && INTEGERP (BVAR (current_buffer, selective_display))
12528 && XINT (BVAR (current_buffer, selective_display)) > 0
12529 && (BEG_UNCHANGED < start || GPT <= start))
12530 unchanged_p = 0;
12531
12532 /* If there are overlays at the start or end of the line, these
12533 may have overlay strings with newlines in them. A change at
12534 START, for instance, may actually concern the display of such
12535 overlay strings as well, and they are displayed on different
12536 lines. So, quickly rule out this case. (For the future, it
12537 might be desirable to implement something more telling than
12538 just BEG/END_UNCHANGED.) */
12539 if (unchanged_p)
12540 {
12541 if (BEG + BEG_UNCHANGED == start
12542 && overlay_touches_p (start))
12543 unchanged_p = 0;
12544 if (END_UNCHANGED == end
12545 && overlay_touches_p (Z - end))
12546 unchanged_p = 0;
12547 }
12548
12549 /* Under bidi reordering, adding or deleting a character in the
12550 beginning of a paragraph, before the first strong directional
12551 character, can change the base direction of the paragraph (unless
12552 the buffer specifies a fixed paragraph direction), which will
12553 require to redisplay the whole paragraph. It might be worthwhile
12554 to find the paragraph limits and widen the range of redisplayed
12555 lines to that, but for now just give up this optimization. */
12556 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12557 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12558 unchanged_p = 0;
12559 }
12560
12561 return unchanged_p;
12562 }
12563
12564
12565 /* Do a frame update, taking possible shortcuts into account. This is
12566 the main external entry point for redisplay.
12567
12568 If the last redisplay displayed an echo area message and that message
12569 is no longer requested, we clear the echo area or bring back the
12570 mini-buffer if that is in use. */
12571
12572 void
12573 redisplay (void)
12574 {
12575 redisplay_internal ();
12576 }
12577
12578
12579 static Lisp_Object
12580 overlay_arrow_string_or_property (Lisp_Object var)
12581 {
12582 Lisp_Object val;
12583
12584 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12585 return val;
12586
12587 return Voverlay_arrow_string;
12588 }
12589
12590 /* Return 1 if there are any overlay-arrows in current_buffer. */
12591 static int
12592 overlay_arrow_in_current_buffer_p (void)
12593 {
12594 Lisp_Object vlist;
12595
12596 for (vlist = Voverlay_arrow_variable_list;
12597 CONSP (vlist);
12598 vlist = XCDR (vlist))
12599 {
12600 Lisp_Object var = XCAR (vlist);
12601 Lisp_Object val;
12602
12603 if (!SYMBOLP (var))
12604 continue;
12605 val = find_symbol_value (var);
12606 if (MARKERP (val)
12607 && current_buffer == XMARKER (val)->buffer)
12608 return 1;
12609 }
12610 return 0;
12611 }
12612
12613
12614 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12615 has changed. */
12616
12617 static int
12618 overlay_arrows_changed_p (void)
12619 {
12620 Lisp_Object vlist;
12621
12622 for (vlist = Voverlay_arrow_variable_list;
12623 CONSP (vlist);
12624 vlist = XCDR (vlist))
12625 {
12626 Lisp_Object var = XCAR (vlist);
12627 Lisp_Object val, pstr;
12628
12629 if (!SYMBOLP (var))
12630 continue;
12631 val = find_symbol_value (var);
12632 if (!MARKERP (val))
12633 continue;
12634 if (! EQ (COERCE_MARKER (val),
12635 Fget (var, Qlast_arrow_position))
12636 || ! (pstr = overlay_arrow_string_or_property (var),
12637 EQ (pstr, Fget (var, Qlast_arrow_string))))
12638 return 1;
12639 }
12640 return 0;
12641 }
12642
12643 /* Mark overlay arrows to be updated on next redisplay. */
12644
12645 static void
12646 update_overlay_arrows (int up_to_date)
12647 {
12648 Lisp_Object vlist;
12649
12650 for (vlist = Voverlay_arrow_variable_list;
12651 CONSP (vlist);
12652 vlist = XCDR (vlist))
12653 {
12654 Lisp_Object var = XCAR (vlist);
12655
12656 if (!SYMBOLP (var))
12657 continue;
12658
12659 if (up_to_date > 0)
12660 {
12661 Lisp_Object val = find_symbol_value (var);
12662 Fput (var, Qlast_arrow_position,
12663 COERCE_MARKER (val));
12664 Fput (var, Qlast_arrow_string,
12665 overlay_arrow_string_or_property (var));
12666 }
12667 else if (up_to_date < 0
12668 || !NILP (Fget (var, Qlast_arrow_position)))
12669 {
12670 Fput (var, Qlast_arrow_position, Qt);
12671 Fput (var, Qlast_arrow_string, Qt);
12672 }
12673 }
12674 }
12675
12676
12677 /* Return overlay arrow string to display at row.
12678 Return integer (bitmap number) for arrow bitmap in left fringe.
12679 Return nil if no overlay arrow. */
12680
12681 static Lisp_Object
12682 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
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 Lisp_Object val;
12692
12693 if (!SYMBOLP (var))
12694 continue;
12695
12696 val = find_symbol_value (var);
12697
12698 if (MARKERP (val)
12699 && current_buffer == XMARKER (val)->buffer
12700 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12701 {
12702 if (FRAME_WINDOW_P (it->f)
12703 /* FIXME: if ROW->reversed_p is set, this should test
12704 the right fringe, not the left one. */
12705 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12706 {
12707 #ifdef HAVE_WINDOW_SYSTEM
12708 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12709 {
12710 int fringe_bitmap;
12711 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12712 return make_number (fringe_bitmap);
12713 }
12714 #endif
12715 return make_number (-1); /* Use default arrow bitmap */
12716 }
12717 return overlay_arrow_string_or_property (var);
12718 }
12719 }
12720
12721 return Qnil;
12722 }
12723
12724 /* Return 1 if point moved out of or into a composition. Otherwise
12725 return 0. PREV_BUF and PREV_PT are the last point buffer and
12726 position. BUF and PT are the current point buffer and position. */
12727
12728 static int
12729 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12730 struct buffer *buf, ptrdiff_t pt)
12731 {
12732 ptrdiff_t start, end;
12733 Lisp_Object prop;
12734 Lisp_Object buffer;
12735
12736 XSETBUFFER (buffer, buf);
12737 /* Check a composition at the last point if point moved within the
12738 same buffer. */
12739 if (prev_buf == buf)
12740 {
12741 if (prev_pt == pt)
12742 /* Point didn't move. */
12743 return 0;
12744
12745 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12746 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12747 && COMPOSITION_VALID_P (start, end, prop)
12748 && start < prev_pt && end > prev_pt)
12749 /* The last point was within the composition. Return 1 iff
12750 point moved out of the composition. */
12751 return (pt <= start || pt >= end);
12752 }
12753
12754 /* Check a composition at the current point. */
12755 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12756 && find_composition (pt, -1, &start, &end, &prop, buffer)
12757 && COMPOSITION_VALID_P (start, end, prop)
12758 && start < pt && end > pt);
12759 }
12760
12761
12762 /* Reconsider the setting of B->clip_changed which is displayed
12763 in window W. */
12764
12765 static inline void
12766 reconsider_clip_changes (struct window *w, struct buffer *b)
12767 {
12768 if (b->clip_changed
12769 && !NILP (w->window_end_valid)
12770 && w->current_matrix->buffer == b
12771 && w->current_matrix->zv == BUF_ZV (b)
12772 && w->current_matrix->begv == BUF_BEGV (b))
12773 b->clip_changed = 0;
12774
12775 /* If display wasn't paused, and W is not a tool bar window, see if
12776 point has been moved into or out of a composition. In that case,
12777 we set b->clip_changed to 1 to force updating the screen. If
12778 b->clip_changed has already been set to 1, we can skip this
12779 check. */
12780 if (!b->clip_changed
12781 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12782 {
12783 ptrdiff_t pt;
12784
12785 if (w == XWINDOW (selected_window))
12786 pt = PT;
12787 else
12788 pt = marker_position (w->pointm);
12789
12790 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12791 || pt != w->last_point)
12792 && check_point_in_composition (w->current_matrix->buffer,
12793 w->last_point,
12794 XBUFFER (w->buffer), pt))
12795 b->clip_changed = 1;
12796 }
12797 }
12798 \f
12799
12800 /* Select FRAME to forward the values of frame-local variables into C
12801 variables so that the redisplay routines can access those values
12802 directly. */
12803
12804 static void
12805 select_frame_for_redisplay (Lisp_Object frame)
12806 {
12807 Lisp_Object tail, tem;
12808 Lisp_Object old = selected_frame;
12809 struct Lisp_Symbol *sym;
12810
12811 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12812
12813 selected_frame = frame;
12814
12815 do {
12816 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12817 if (CONSP (XCAR (tail))
12818 && (tem = XCAR (XCAR (tail)),
12819 SYMBOLP (tem))
12820 && (sym = indirect_variable (XSYMBOL (tem)),
12821 sym->redirect == SYMBOL_LOCALIZED)
12822 && sym->val.blv->frame_local)
12823 /* Use find_symbol_value rather than Fsymbol_value
12824 to avoid an error if it is void. */
12825 find_symbol_value (tem);
12826 } while (!EQ (frame, old) && (frame = old, 1));
12827 }
12828
12829
12830 #define STOP_POLLING \
12831 do { if (! polling_stopped_here) stop_polling (); \
12832 polling_stopped_here = 1; } while (0)
12833
12834 #define RESUME_POLLING \
12835 do { if (polling_stopped_here) start_polling (); \
12836 polling_stopped_here = 0; } while (0)
12837
12838
12839 /* Perhaps in the future avoid recentering windows if it
12840 is not necessary; currently that causes some problems. */
12841
12842 static void
12843 redisplay_internal (void)
12844 {
12845 struct window *w = XWINDOW (selected_window);
12846 struct window *sw;
12847 struct frame *fr;
12848 int pending;
12849 int must_finish = 0;
12850 struct text_pos tlbufpos, tlendpos;
12851 int number_of_visible_frames;
12852 ptrdiff_t count, count1;
12853 struct frame *sf;
12854 int polling_stopped_here = 0;
12855 Lisp_Object old_frame = selected_frame;
12856
12857 /* Non-zero means redisplay has to consider all windows on all
12858 frames. Zero means, only selected_window is considered. */
12859 int consider_all_windows_p;
12860
12861 /* Non-zero means redisplay has to redisplay the miniwindow */
12862 int update_miniwindow_p = 0;
12863
12864 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12865
12866 /* No redisplay if running in batch mode or frame is not yet fully
12867 initialized, or redisplay is explicitly turned off by setting
12868 Vinhibit_redisplay. */
12869 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12870 || !NILP (Vinhibit_redisplay))
12871 return;
12872
12873 /* Don't examine these until after testing Vinhibit_redisplay.
12874 When Emacs is shutting down, perhaps because its connection to
12875 X has dropped, we should not look at them at all. */
12876 fr = XFRAME (w->frame);
12877 sf = SELECTED_FRAME ();
12878
12879 if (!fr->glyphs_initialized_p)
12880 return;
12881
12882 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12883 if (popup_activated ())
12884 return;
12885 #endif
12886
12887 /* I don't think this happens but let's be paranoid. */
12888 if (redisplaying_p)
12889 return;
12890
12891 /* Record a function that resets redisplaying_p to its old value
12892 when we leave this function. */
12893 count = SPECPDL_INDEX ();
12894 record_unwind_protect (unwind_redisplay,
12895 Fcons (make_number (redisplaying_p), selected_frame));
12896 ++redisplaying_p;
12897 specbind (Qinhibit_free_realized_faces, Qnil);
12898
12899 {
12900 Lisp_Object tail, frame;
12901
12902 FOR_EACH_FRAME (tail, frame)
12903 {
12904 struct frame *f = XFRAME (frame);
12905 f->already_hscrolled_p = 0;
12906 }
12907 }
12908
12909 retry:
12910 /* Remember the currently selected window. */
12911 sw = w;
12912
12913 if (!EQ (old_frame, selected_frame)
12914 && FRAME_LIVE_P (XFRAME (old_frame)))
12915 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12916 selected_frame and selected_window to be temporarily out-of-sync so
12917 when we come back here via `goto retry', we need to resync because we
12918 may need to run Elisp code (via prepare_menu_bars). */
12919 select_frame_for_redisplay (old_frame);
12920
12921 pending = 0;
12922 reconsider_clip_changes (w, current_buffer);
12923 last_escape_glyph_frame = NULL;
12924 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12925 last_glyphless_glyph_frame = NULL;
12926 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12927
12928 /* If new fonts have been loaded that make a glyph matrix adjustment
12929 necessary, do it. */
12930 if (fonts_changed_p)
12931 {
12932 adjust_glyphs (NULL);
12933 ++windows_or_buffers_changed;
12934 fonts_changed_p = 0;
12935 }
12936
12937 /* If face_change_count is non-zero, init_iterator will free all
12938 realized faces, which includes the faces referenced from current
12939 matrices. So, we can't reuse current matrices in this case. */
12940 if (face_change_count)
12941 ++windows_or_buffers_changed;
12942
12943 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12944 && FRAME_TTY (sf)->previous_frame != sf)
12945 {
12946 /* Since frames on a single ASCII terminal share the same
12947 display area, displaying a different frame means redisplay
12948 the whole thing. */
12949 windows_or_buffers_changed++;
12950 SET_FRAME_GARBAGED (sf);
12951 #ifndef DOS_NT
12952 set_tty_color_mode (FRAME_TTY (sf), sf);
12953 #endif
12954 FRAME_TTY (sf)->previous_frame = sf;
12955 }
12956
12957 /* Set the visible flags for all frames. Do this before checking
12958 for resized or garbaged frames; they want to know if their frames
12959 are visible. See the comment in frame.h for
12960 FRAME_SAMPLE_VISIBILITY. */
12961 {
12962 Lisp_Object tail, frame;
12963
12964 number_of_visible_frames = 0;
12965
12966 FOR_EACH_FRAME (tail, frame)
12967 {
12968 struct frame *f = XFRAME (frame);
12969
12970 FRAME_SAMPLE_VISIBILITY (f);
12971 if (FRAME_VISIBLE_P (f))
12972 ++number_of_visible_frames;
12973 clear_desired_matrices (f);
12974 }
12975 }
12976
12977 /* Notice any pending interrupt request to change frame size. */
12978 do_pending_window_change (1);
12979
12980 /* do_pending_window_change could change the selected_window due to
12981 frame resizing which makes the selected window too small. */
12982 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12983 {
12984 sw = w;
12985 reconsider_clip_changes (w, current_buffer);
12986 }
12987
12988 /* Clear frames marked as garbaged. */
12989 if (frame_garbaged)
12990 clear_garbaged_frames ();
12991
12992 /* Build menubar and tool-bar items. */
12993 if (NILP (Vmemory_full))
12994 prepare_menu_bars ();
12995
12996 if (windows_or_buffers_changed)
12997 update_mode_lines++;
12998
12999 /* Detect case that we need to write or remove a star in the mode line. */
13000 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13001 {
13002 w->update_mode_line = 1;
13003 if (buffer_shared > 1)
13004 update_mode_lines++;
13005 }
13006
13007 /* Avoid invocation of point motion hooks by `current_column' below. */
13008 count1 = SPECPDL_INDEX ();
13009 specbind (Qinhibit_point_motion_hooks, Qt);
13010
13011 /* If %c is in the mode line, update it if needed. */
13012 if (!NILP (w->column_number_displayed)
13013 /* This alternative quickly identifies a common case
13014 where no change is needed. */
13015 && !(PT == w->last_point
13016 && w->last_modified >= MODIFF
13017 && w->last_overlay_modified >= OVERLAY_MODIFF)
13018 && (XFASTINT (w->column_number_displayed) != current_column ()))
13019 w->update_mode_line = 1;
13020
13021 unbind_to (count1, Qnil);
13022
13023 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13024
13025 /* The variable buffer_shared is set in redisplay_window and
13026 indicates that we redisplay a buffer in different windows. See
13027 there. */
13028 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13029 || cursor_type_changed);
13030
13031 /* If specs for an arrow have changed, do thorough redisplay
13032 to ensure we remove any arrow that should no longer exist. */
13033 if (overlay_arrows_changed_p ())
13034 consider_all_windows_p = windows_or_buffers_changed = 1;
13035
13036 /* Normally the message* functions will have already displayed and
13037 updated the echo area, but the frame may have been trashed, or
13038 the update may have been preempted, so display the echo area
13039 again here. Checking message_cleared_p captures the case that
13040 the echo area should be cleared. */
13041 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13042 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13043 || (message_cleared_p
13044 && minibuf_level == 0
13045 /* If the mini-window is currently selected, this means the
13046 echo-area doesn't show through. */
13047 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13048 {
13049 int window_height_changed_p = echo_area_display (0);
13050
13051 if (message_cleared_p)
13052 update_miniwindow_p = 1;
13053
13054 must_finish = 1;
13055
13056 /* If we don't display the current message, don't clear the
13057 message_cleared_p flag, because, if we did, we wouldn't clear
13058 the echo area in the next redisplay which doesn't preserve
13059 the echo area. */
13060 if (!display_last_displayed_message_p)
13061 message_cleared_p = 0;
13062
13063 if (fonts_changed_p)
13064 goto retry;
13065 else if (window_height_changed_p)
13066 {
13067 consider_all_windows_p = 1;
13068 ++update_mode_lines;
13069 ++windows_or_buffers_changed;
13070
13071 /* If window configuration was changed, frames may have been
13072 marked garbaged. Clear them or we will experience
13073 surprises wrt scrolling. */
13074 if (frame_garbaged)
13075 clear_garbaged_frames ();
13076 }
13077 }
13078 else if (EQ (selected_window, minibuf_window)
13079 && (current_buffer->clip_changed
13080 || w->last_modified < MODIFF
13081 || w->last_overlay_modified < OVERLAY_MODIFF)
13082 && resize_mini_window (w, 0))
13083 {
13084 /* Resized active mini-window to fit the size of what it is
13085 showing if its contents might have changed. */
13086 must_finish = 1;
13087 /* FIXME: this causes all frames to be updated, which seems unnecessary
13088 since only the current frame needs to be considered. This function needs
13089 to be rewritten with two variables, consider_all_windows and
13090 consider_all_frames. */
13091 consider_all_windows_p = 1;
13092 ++windows_or_buffers_changed;
13093 ++update_mode_lines;
13094
13095 /* If window configuration was changed, frames may have been
13096 marked garbaged. Clear them or we will experience
13097 surprises wrt scrolling. */
13098 if (frame_garbaged)
13099 clear_garbaged_frames ();
13100 }
13101
13102
13103 /* If showing the region, and mark has changed, we must redisplay
13104 the whole window. The assignment to this_line_start_pos prevents
13105 the optimization directly below this if-statement. */
13106 if (((!NILP (Vtransient_mark_mode)
13107 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13108 != !NILP (w->region_showing))
13109 || (!NILP (w->region_showing)
13110 && !EQ (w->region_showing,
13111 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13112 CHARPOS (this_line_start_pos) = 0;
13113
13114 /* Optimize the case that only the line containing the cursor in the
13115 selected window has changed. Variables starting with this_ are
13116 set in display_line and record information about the line
13117 containing the cursor. */
13118 tlbufpos = this_line_start_pos;
13119 tlendpos = this_line_end_pos;
13120 if (!consider_all_windows_p
13121 && CHARPOS (tlbufpos) > 0
13122 && !w->update_mode_line
13123 && !current_buffer->clip_changed
13124 && !current_buffer->prevent_redisplay_optimizations_p
13125 && FRAME_VISIBLE_P (XFRAME (w->frame))
13126 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13127 /* Make sure recorded data applies to current buffer, etc. */
13128 && this_line_buffer == current_buffer
13129 && current_buffer == XBUFFER (w->buffer)
13130 && !w->force_start
13131 && !w->optional_new_start
13132 /* Point must be on the line that we have info recorded about. */
13133 && PT >= CHARPOS (tlbufpos)
13134 && PT <= Z - CHARPOS (tlendpos)
13135 /* All text outside that line, including its final newline,
13136 must be unchanged. */
13137 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13138 CHARPOS (tlendpos)))
13139 {
13140 if (CHARPOS (tlbufpos) > BEGV
13141 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13142 && (CHARPOS (tlbufpos) == ZV
13143 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13144 /* Former continuation line has disappeared by becoming empty. */
13145 goto cancel;
13146 else if (w->last_modified < MODIFF
13147 || w->last_overlay_modified < OVERLAY_MODIFF
13148 || MINI_WINDOW_P (w))
13149 {
13150 /* We have to handle the case of continuation around a
13151 wide-column character (see the comment in indent.c around
13152 line 1340).
13153
13154 For instance, in the following case:
13155
13156 -------- Insert --------
13157 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13158 J_I_ ==> J_I_ `^^' are cursors.
13159 ^^ ^^
13160 -------- --------
13161
13162 As we have to redraw the line above, we cannot use this
13163 optimization. */
13164
13165 struct it it;
13166 int line_height_before = this_line_pixel_height;
13167
13168 /* Note that start_display will handle the case that the
13169 line starting at tlbufpos is a continuation line. */
13170 start_display (&it, w, tlbufpos);
13171
13172 /* Implementation note: It this still necessary? */
13173 if (it.current_x != this_line_start_x)
13174 goto cancel;
13175
13176 TRACE ((stderr, "trying display optimization 1\n"));
13177 w->cursor.vpos = -1;
13178 overlay_arrow_seen = 0;
13179 it.vpos = this_line_vpos;
13180 it.current_y = this_line_y;
13181 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13182 display_line (&it);
13183
13184 /* If line contains point, is not continued,
13185 and ends at same distance from eob as before, we win. */
13186 if (w->cursor.vpos >= 0
13187 /* Line is not continued, otherwise this_line_start_pos
13188 would have been set to 0 in display_line. */
13189 && CHARPOS (this_line_start_pos)
13190 /* Line ends as before. */
13191 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13192 /* Line has same height as before. Otherwise other lines
13193 would have to be shifted up or down. */
13194 && this_line_pixel_height == line_height_before)
13195 {
13196 /* If this is not the window's last line, we must adjust
13197 the charstarts of the lines below. */
13198 if (it.current_y < it.last_visible_y)
13199 {
13200 struct glyph_row *row
13201 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13202 ptrdiff_t delta, delta_bytes;
13203
13204 /* We used to distinguish between two cases here,
13205 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13206 when the line ends in a newline or the end of the
13207 buffer's accessible portion. But both cases did
13208 the same, so they were collapsed. */
13209 delta = (Z
13210 - CHARPOS (tlendpos)
13211 - MATRIX_ROW_START_CHARPOS (row));
13212 delta_bytes = (Z_BYTE
13213 - BYTEPOS (tlendpos)
13214 - MATRIX_ROW_START_BYTEPOS (row));
13215
13216 increment_matrix_positions (w->current_matrix,
13217 this_line_vpos + 1,
13218 w->current_matrix->nrows,
13219 delta, delta_bytes);
13220 }
13221
13222 /* If this row displays text now but previously didn't,
13223 or vice versa, w->window_end_vpos may have to be
13224 adjusted. */
13225 if ((it.glyph_row - 1)->displays_text_p)
13226 {
13227 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13228 XSETINT (w->window_end_vpos, this_line_vpos);
13229 }
13230 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13231 && this_line_vpos > 0)
13232 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13233 w->window_end_valid = Qnil;
13234
13235 /* Update hint: No need to try to scroll in update_window. */
13236 w->desired_matrix->no_scrolling_p = 1;
13237
13238 #ifdef GLYPH_DEBUG
13239 *w->desired_matrix->method = 0;
13240 debug_method_add (w, "optimization 1");
13241 #endif
13242 #ifdef HAVE_WINDOW_SYSTEM
13243 update_window_fringes (w, 0);
13244 #endif
13245 goto update;
13246 }
13247 else
13248 goto cancel;
13249 }
13250 else if (/* Cursor position hasn't changed. */
13251 PT == w->last_point
13252 /* Make sure the cursor was last displayed
13253 in this window. Otherwise we have to reposition it. */
13254 && 0 <= w->cursor.vpos
13255 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13256 {
13257 if (!must_finish)
13258 {
13259 do_pending_window_change (1);
13260 /* If selected_window changed, redisplay again. */
13261 if (WINDOWP (selected_window)
13262 && (w = XWINDOW (selected_window)) != sw)
13263 goto retry;
13264
13265 /* We used to always goto end_of_redisplay here, but this
13266 isn't enough if we have a blinking cursor. */
13267 if (w->cursor_off_p == w->last_cursor_off_p)
13268 goto end_of_redisplay;
13269 }
13270 goto update;
13271 }
13272 /* If highlighting the region, or if the cursor is in the echo area,
13273 then we can't just move the cursor. */
13274 else if (! (!NILP (Vtransient_mark_mode)
13275 && !NILP (BVAR (current_buffer, mark_active)))
13276 && (EQ (selected_window,
13277 BVAR (current_buffer, last_selected_window))
13278 || highlight_nonselected_windows)
13279 && NILP (w->region_showing)
13280 && NILP (Vshow_trailing_whitespace)
13281 && !cursor_in_echo_area)
13282 {
13283 struct it it;
13284 struct glyph_row *row;
13285
13286 /* Skip from tlbufpos to PT and see where it is. Note that
13287 PT may be in invisible text. If so, we will end at the
13288 next visible position. */
13289 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13290 NULL, DEFAULT_FACE_ID);
13291 it.current_x = this_line_start_x;
13292 it.current_y = this_line_y;
13293 it.vpos = this_line_vpos;
13294
13295 /* The call to move_it_to stops in front of PT, but
13296 moves over before-strings. */
13297 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13298
13299 if (it.vpos == this_line_vpos
13300 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13301 row->enabled_p))
13302 {
13303 eassert (this_line_vpos == it.vpos);
13304 eassert (this_line_y == it.current_y);
13305 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13306 #ifdef GLYPH_DEBUG
13307 *w->desired_matrix->method = 0;
13308 debug_method_add (w, "optimization 3");
13309 #endif
13310 goto update;
13311 }
13312 else
13313 goto cancel;
13314 }
13315
13316 cancel:
13317 /* Text changed drastically or point moved off of line. */
13318 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13319 }
13320
13321 CHARPOS (this_line_start_pos) = 0;
13322 consider_all_windows_p |= buffer_shared > 1;
13323 ++clear_face_cache_count;
13324 #ifdef HAVE_WINDOW_SYSTEM
13325 ++clear_image_cache_count;
13326 #endif
13327
13328 /* Build desired matrices, and update the display. If
13329 consider_all_windows_p is non-zero, do it for all windows on all
13330 frames. Otherwise do it for selected_window, only. */
13331
13332 if (consider_all_windows_p)
13333 {
13334 Lisp_Object tail, frame;
13335
13336 FOR_EACH_FRAME (tail, frame)
13337 XFRAME (frame)->updated_p = 0;
13338
13339 /* Recompute # windows showing selected buffer. This will be
13340 incremented each time such a window is displayed. */
13341 buffer_shared = 0;
13342
13343 FOR_EACH_FRAME (tail, frame)
13344 {
13345 struct frame *f = XFRAME (frame);
13346
13347 /* We don't have to do anything for unselected terminal
13348 frames. */
13349 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13350 && !EQ (FRAME_TTY (f)->top_frame, frame))
13351 continue;
13352
13353 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13354 {
13355 if (! EQ (frame, selected_frame))
13356 /* Select the frame, for the sake of frame-local
13357 variables. */
13358 select_frame_for_redisplay (frame);
13359
13360 /* Mark all the scroll bars to be removed; we'll redeem
13361 the ones we want when we redisplay their windows. */
13362 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13363 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13364
13365 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13366 redisplay_windows (FRAME_ROOT_WINDOW (f));
13367
13368 /* The X error handler may have deleted that frame. */
13369 if (!FRAME_LIVE_P (f))
13370 continue;
13371
13372 /* Any scroll bars which redisplay_windows should have
13373 nuked should now go away. */
13374 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13375 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13376
13377 /* If fonts changed, display again. */
13378 /* ??? rms: I suspect it is a mistake to jump all the way
13379 back to retry here. It should just retry this frame. */
13380 if (fonts_changed_p)
13381 goto retry;
13382
13383 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13384 {
13385 /* See if we have to hscroll. */
13386 if (!f->already_hscrolled_p)
13387 {
13388 f->already_hscrolled_p = 1;
13389 if (hscroll_windows (f->root_window))
13390 goto retry;
13391 }
13392
13393 /* Prevent various kinds of signals during display
13394 update. stdio is not robust about handling
13395 signals, which can cause an apparent I/O
13396 error. */
13397 if (interrupt_input)
13398 unrequest_sigio ();
13399 STOP_POLLING;
13400
13401 /* Update the display. */
13402 set_window_update_flags (XWINDOW (f->root_window), 1);
13403 pending |= update_frame (f, 0, 0);
13404 f->updated_p = 1;
13405 }
13406 }
13407 }
13408
13409 if (!EQ (old_frame, selected_frame)
13410 && FRAME_LIVE_P (XFRAME (old_frame)))
13411 /* We played a bit fast-and-loose above and allowed selected_frame
13412 and selected_window to be temporarily out-of-sync but let's make
13413 sure this stays contained. */
13414 select_frame_for_redisplay (old_frame);
13415 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13416
13417 if (!pending)
13418 {
13419 /* Do the mark_window_display_accurate after all windows have
13420 been redisplayed because this call resets flags in buffers
13421 which are needed for proper redisplay. */
13422 FOR_EACH_FRAME (tail, frame)
13423 {
13424 struct frame *f = XFRAME (frame);
13425 if (f->updated_p)
13426 {
13427 mark_window_display_accurate (f->root_window, 1);
13428 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13429 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13430 }
13431 }
13432 }
13433 }
13434 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13435 {
13436 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13437 struct frame *mini_frame;
13438
13439 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13440 /* Use list_of_error, not Qerror, so that
13441 we catch only errors and don't run the debugger. */
13442 internal_condition_case_1 (redisplay_window_1, selected_window,
13443 list_of_error,
13444 redisplay_window_error);
13445 if (update_miniwindow_p)
13446 internal_condition_case_1 (redisplay_window_1, mini_window,
13447 list_of_error,
13448 redisplay_window_error);
13449
13450 /* Compare desired and current matrices, perform output. */
13451
13452 update:
13453 /* If fonts changed, display again. */
13454 if (fonts_changed_p)
13455 goto retry;
13456
13457 /* Prevent various kinds of signals during display update.
13458 stdio is not robust about handling signals,
13459 which can cause an apparent I/O error. */
13460 if (interrupt_input)
13461 unrequest_sigio ();
13462 STOP_POLLING;
13463
13464 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13465 {
13466 if (hscroll_windows (selected_window))
13467 goto retry;
13468
13469 XWINDOW (selected_window)->must_be_updated_p = 1;
13470 pending = update_frame (sf, 0, 0);
13471 }
13472
13473 /* We may have called echo_area_display at the top of this
13474 function. If the echo area is on another frame, that may
13475 have put text on a frame other than the selected one, so the
13476 above call to update_frame would not have caught it. Catch
13477 it here. */
13478 mini_window = FRAME_MINIBUF_WINDOW (sf);
13479 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13480
13481 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13482 {
13483 XWINDOW (mini_window)->must_be_updated_p = 1;
13484 pending |= update_frame (mini_frame, 0, 0);
13485 if (!pending && hscroll_windows (mini_window))
13486 goto retry;
13487 }
13488 }
13489
13490 /* If display was paused because of pending input, make sure we do a
13491 thorough update the next time. */
13492 if (pending)
13493 {
13494 /* Prevent the optimization at the beginning of
13495 redisplay_internal that tries a single-line update of the
13496 line containing the cursor in the selected window. */
13497 CHARPOS (this_line_start_pos) = 0;
13498
13499 /* Let the overlay arrow be updated the next time. */
13500 update_overlay_arrows (0);
13501
13502 /* If we pause after scrolling, some rows in the current
13503 matrices of some windows are not valid. */
13504 if (!WINDOW_FULL_WIDTH_P (w)
13505 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13506 update_mode_lines = 1;
13507 }
13508 else
13509 {
13510 if (!consider_all_windows_p)
13511 {
13512 /* This has already been done above if
13513 consider_all_windows_p is set. */
13514 mark_window_display_accurate_1 (w, 1);
13515
13516 /* Say overlay arrows are up to date. */
13517 update_overlay_arrows (1);
13518
13519 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13520 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13521 }
13522
13523 update_mode_lines = 0;
13524 windows_or_buffers_changed = 0;
13525 cursor_type_changed = 0;
13526 }
13527
13528 /* Start SIGIO interrupts coming again. Having them off during the
13529 code above makes it less likely one will discard output, but not
13530 impossible, since there might be stuff in the system buffer here.
13531 But it is much hairier to try to do anything about that. */
13532 if (interrupt_input)
13533 request_sigio ();
13534 RESUME_POLLING;
13535
13536 /* If a frame has become visible which was not before, redisplay
13537 again, so that we display it. Expose events for such a frame
13538 (which it gets when becoming visible) don't call the parts of
13539 redisplay constructing glyphs, so simply exposing a frame won't
13540 display anything in this case. So, we have to display these
13541 frames here explicitly. */
13542 if (!pending)
13543 {
13544 Lisp_Object tail, frame;
13545 int new_count = 0;
13546
13547 FOR_EACH_FRAME (tail, frame)
13548 {
13549 int this_is_visible = 0;
13550
13551 if (XFRAME (frame)->visible)
13552 this_is_visible = 1;
13553 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13554 if (XFRAME (frame)->visible)
13555 this_is_visible = 1;
13556
13557 if (this_is_visible)
13558 new_count++;
13559 }
13560
13561 if (new_count != number_of_visible_frames)
13562 windows_or_buffers_changed++;
13563 }
13564
13565 /* Change frame size now if a change is pending. */
13566 do_pending_window_change (1);
13567
13568 /* If we just did a pending size change, or have additional
13569 visible frames, or selected_window changed, redisplay again. */
13570 if ((windows_or_buffers_changed && !pending)
13571 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13572 goto retry;
13573
13574 /* Clear the face and image caches.
13575
13576 We used to do this only if consider_all_windows_p. But the cache
13577 needs to be cleared if a timer creates images in the current
13578 buffer (e.g. the test case in Bug#6230). */
13579
13580 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13581 {
13582 clear_face_cache (0);
13583 clear_face_cache_count = 0;
13584 }
13585
13586 #ifdef HAVE_WINDOW_SYSTEM
13587 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13588 {
13589 clear_image_caches (Qnil);
13590 clear_image_cache_count = 0;
13591 }
13592 #endif /* HAVE_WINDOW_SYSTEM */
13593
13594 end_of_redisplay:
13595 unbind_to (count, Qnil);
13596 RESUME_POLLING;
13597 }
13598
13599
13600 /* Redisplay, but leave alone any recent echo area message unless
13601 another message has been requested in its place.
13602
13603 This is useful in situations where you need to redisplay but no
13604 user action has occurred, making it inappropriate for the message
13605 area to be cleared. See tracking_off and
13606 wait_reading_process_output for examples of these situations.
13607
13608 FROM_WHERE is an integer saying from where this function was
13609 called. This is useful for debugging. */
13610
13611 void
13612 redisplay_preserve_echo_area (int from_where)
13613 {
13614 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13615
13616 if (!NILP (echo_area_buffer[1]))
13617 {
13618 /* We have a previously displayed message, but no current
13619 message. Redisplay the previous message. */
13620 display_last_displayed_message_p = 1;
13621 redisplay_internal ();
13622 display_last_displayed_message_p = 0;
13623 }
13624 else
13625 redisplay_internal ();
13626
13627 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13628 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13629 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13630 }
13631
13632
13633 /* Function registered with record_unwind_protect in
13634 redisplay_internal. Reset redisplaying_p to the value it had
13635 before redisplay_internal was called, and clear
13636 prevent_freeing_realized_faces_p. It also selects the previously
13637 selected frame, unless it has been deleted (by an X connection
13638 failure during redisplay, for example). */
13639
13640 static Lisp_Object
13641 unwind_redisplay (Lisp_Object val)
13642 {
13643 Lisp_Object old_redisplaying_p, old_frame;
13644
13645 old_redisplaying_p = XCAR (val);
13646 redisplaying_p = XFASTINT (old_redisplaying_p);
13647 old_frame = XCDR (val);
13648 if (! EQ (old_frame, selected_frame)
13649 && FRAME_LIVE_P (XFRAME (old_frame)))
13650 select_frame_for_redisplay (old_frame);
13651 return Qnil;
13652 }
13653
13654
13655 /* Mark the display of window W as accurate or inaccurate. If
13656 ACCURATE_P is non-zero mark display of W as accurate. If
13657 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13658 redisplay_internal is called. */
13659
13660 static void
13661 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13662 {
13663 if (BUFFERP (w->buffer))
13664 {
13665 struct buffer *b = XBUFFER (w->buffer);
13666
13667 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13668 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13669 w->last_had_star
13670 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13671
13672 if (accurate_p)
13673 {
13674 b->clip_changed = 0;
13675 b->prevent_redisplay_optimizations_p = 0;
13676
13677 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13678 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13679 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13680 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13681
13682 w->current_matrix->buffer = b;
13683 w->current_matrix->begv = BUF_BEGV (b);
13684 w->current_matrix->zv = BUF_ZV (b);
13685
13686 w->last_cursor = w->cursor;
13687 w->last_cursor_off_p = w->cursor_off_p;
13688
13689 if (w == XWINDOW (selected_window))
13690 w->last_point = BUF_PT (b);
13691 else
13692 w->last_point = XMARKER (w->pointm)->charpos;
13693 }
13694 }
13695
13696 if (accurate_p)
13697 {
13698 w->window_end_valid = w->buffer;
13699 w->update_mode_line = 0;
13700 }
13701 }
13702
13703
13704 /* Mark the display of windows in the window tree rooted at WINDOW as
13705 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13706 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13707 be redisplayed the next time redisplay_internal is called. */
13708
13709 void
13710 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13711 {
13712 struct window *w;
13713
13714 for (; !NILP (window); window = w->next)
13715 {
13716 w = XWINDOW (window);
13717 mark_window_display_accurate_1 (w, accurate_p);
13718
13719 if (!NILP (w->vchild))
13720 mark_window_display_accurate (w->vchild, accurate_p);
13721 if (!NILP (w->hchild))
13722 mark_window_display_accurate (w->hchild, accurate_p);
13723 }
13724
13725 if (accurate_p)
13726 {
13727 update_overlay_arrows (1);
13728 }
13729 else
13730 {
13731 /* Force a thorough redisplay the next time by setting
13732 last_arrow_position and last_arrow_string to t, which is
13733 unequal to any useful value of Voverlay_arrow_... */
13734 update_overlay_arrows (-1);
13735 }
13736 }
13737
13738
13739 /* Return value in display table DP (Lisp_Char_Table *) for character
13740 C. Since a display table doesn't have any parent, we don't have to
13741 follow parent. Do not call this function directly but use the
13742 macro DISP_CHAR_VECTOR. */
13743
13744 Lisp_Object
13745 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13746 {
13747 Lisp_Object val;
13748
13749 if (ASCII_CHAR_P (c))
13750 {
13751 val = dp->ascii;
13752 if (SUB_CHAR_TABLE_P (val))
13753 val = XSUB_CHAR_TABLE (val)->contents[c];
13754 }
13755 else
13756 {
13757 Lisp_Object table;
13758
13759 XSETCHAR_TABLE (table, dp);
13760 val = char_table_ref (table, c);
13761 }
13762 if (NILP (val))
13763 val = dp->defalt;
13764 return val;
13765 }
13766
13767
13768 \f
13769 /***********************************************************************
13770 Window Redisplay
13771 ***********************************************************************/
13772
13773 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13774
13775 static void
13776 redisplay_windows (Lisp_Object window)
13777 {
13778 while (!NILP (window))
13779 {
13780 struct window *w = XWINDOW (window);
13781
13782 if (!NILP (w->hchild))
13783 redisplay_windows (w->hchild);
13784 else if (!NILP (w->vchild))
13785 redisplay_windows (w->vchild);
13786 else if (!NILP (w->buffer))
13787 {
13788 displayed_buffer = XBUFFER (w->buffer);
13789 /* Use list_of_error, not Qerror, so that
13790 we catch only errors and don't run the debugger. */
13791 internal_condition_case_1 (redisplay_window_0, window,
13792 list_of_error,
13793 redisplay_window_error);
13794 }
13795
13796 window = w->next;
13797 }
13798 }
13799
13800 static Lisp_Object
13801 redisplay_window_error (Lisp_Object ignore)
13802 {
13803 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13804 return Qnil;
13805 }
13806
13807 static Lisp_Object
13808 redisplay_window_0 (Lisp_Object window)
13809 {
13810 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13811 redisplay_window (window, 0);
13812 return Qnil;
13813 }
13814
13815 static Lisp_Object
13816 redisplay_window_1 (Lisp_Object window)
13817 {
13818 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13819 redisplay_window (window, 1);
13820 return Qnil;
13821 }
13822 \f
13823
13824 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13825 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13826 which positions recorded in ROW differ from current buffer
13827 positions.
13828
13829 Return 0 if cursor is not on this row, 1 otherwise. */
13830
13831 static int
13832 set_cursor_from_row (struct window *w, struct glyph_row *row,
13833 struct glyph_matrix *matrix,
13834 ptrdiff_t delta, ptrdiff_t delta_bytes,
13835 int dy, int dvpos)
13836 {
13837 struct glyph *glyph = row->glyphs[TEXT_AREA];
13838 struct glyph *end = glyph + row->used[TEXT_AREA];
13839 struct glyph *cursor = NULL;
13840 /* The last known character position in row. */
13841 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13842 int x = row->x;
13843 ptrdiff_t pt_old = PT - delta;
13844 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13845 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13846 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13847 /* A glyph beyond the edge of TEXT_AREA which we should never
13848 touch. */
13849 struct glyph *glyphs_end = end;
13850 /* Non-zero means we've found a match for cursor position, but that
13851 glyph has the avoid_cursor_p flag set. */
13852 int match_with_avoid_cursor = 0;
13853 /* Non-zero means we've seen at least one glyph that came from a
13854 display string. */
13855 int string_seen = 0;
13856 /* Largest and smallest buffer positions seen so far during scan of
13857 glyph row. */
13858 ptrdiff_t bpos_max = pos_before;
13859 ptrdiff_t bpos_min = pos_after;
13860 /* Last buffer position covered by an overlay string with an integer
13861 `cursor' property. */
13862 ptrdiff_t bpos_covered = 0;
13863 /* Non-zero means the display string on which to display the cursor
13864 comes from a text property, not from an overlay. */
13865 int string_from_text_prop = 0;
13866
13867 /* Don't even try doing anything if called for a mode-line or
13868 header-line row, since the rest of the code isn't prepared to
13869 deal with such calamities. */
13870 eassert (!row->mode_line_p);
13871 if (row->mode_line_p)
13872 return 0;
13873
13874 /* Skip over glyphs not having an object at the start and the end of
13875 the row. These are special glyphs like truncation marks on
13876 terminal frames. */
13877 if (row->displays_text_p)
13878 {
13879 if (!row->reversed_p)
13880 {
13881 while (glyph < end
13882 && INTEGERP (glyph->object)
13883 && glyph->charpos < 0)
13884 {
13885 x += glyph->pixel_width;
13886 ++glyph;
13887 }
13888 while (end > glyph
13889 && INTEGERP ((end - 1)->object)
13890 /* CHARPOS is zero for blanks and stretch glyphs
13891 inserted by extend_face_to_end_of_line. */
13892 && (end - 1)->charpos <= 0)
13893 --end;
13894 glyph_before = glyph - 1;
13895 glyph_after = end;
13896 }
13897 else
13898 {
13899 struct glyph *g;
13900
13901 /* If the glyph row is reversed, we need to process it from back
13902 to front, so swap the edge pointers. */
13903 glyphs_end = end = glyph - 1;
13904 glyph += row->used[TEXT_AREA] - 1;
13905
13906 while (glyph > end + 1
13907 && INTEGERP (glyph->object)
13908 && glyph->charpos < 0)
13909 {
13910 --glyph;
13911 x -= glyph->pixel_width;
13912 }
13913 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13914 --glyph;
13915 /* By default, in reversed rows we put the cursor on the
13916 rightmost (first in the reading order) glyph. */
13917 for (g = end + 1; g < glyph; g++)
13918 x += g->pixel_width;
13919 while (end < glyph
13920 && INTEGERP ((end + 1)->object)
13921 && (end + 1)->charpos <= 0)
13922 ++end;
13923 glyph_before = glyph + 1;
13924 glyph_after = end;
13925 }
13926 }
13927 else if (row->reversed_p)
13928 {
13929 /* In R2L rows that don't display text, put the cursor on the
13930 rightmost glyph. Case in point: an empty last line that is
13931 part of an R2L paragraph. */
13932 cursor = end - 1;
13933 /* Avoid placing the cursor on the last glyph of the row, where
13934 on terminal frames we hold the vertical border between
13935 adjacent windows. */
13936 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13937 && !WINDOW_RIGHTMOST_P (w)
13938 && cursor == row->glyphs[LAST_AREA] - 1)
13939 cursor--;
13940 x = -1; /* will be computed below, at label compute_x */
13941 }
13942
13943 /* Step 1: Try to find the glyph whose character position
13944 corresponds to point. If that's not possible, find 2 glyphs
13945 whose character positions are the closest to point, one before
13946 point, the other after it. */
13947 if (!row->reversed_p)
13948 while (/* not marched to end of glyph row */
13949 glyph < end
13950 /* glyph was not inserted by redisplay for internal purposes */
13951 && !INTEGERP (glyph->object))
13952 {
13953 if (BUFFERP (glyph->object))
13954 {
13955 ptrdiff_t dpos = glyph->charpos - pt_old;
13956
13957 if (glyph->charpos > bpos_max)
13958 bpos_max = glyph->charpos;
13959 if (glyph->charpos < bpos_min)
13960 bpos_min = glyph->charpos;
13961 if (!glyph->avoid_cursor_p)
13962 {
13963 /* If we hit point, we've found the glyph on which to
13964 display the cursor. */
13965 if (dpos == 0)
13966 {
13967 match_with_avoid_cursor = 0;
13968 break;
13969 }
13970 /* See if we've found a better approximation to
13971 POS_BEFORE or to POS_AFTER. */
13972 if (0 > dpos && dpos > pos_before - pt_old)
13973 {
13974 pos_before = glyph->charpos;
13975 glyph_before = glyph;
13976 }
13977 else if (0 < dpos && dpos < pos_after - pt_old)
13978 {
13979 pos_after = glyph->charpos;
13980 glyph_after = glyph;
13981 }
13982 }
13983 else if (dpos == 0)
13984 match_with_avoid_cursor = 1;
13985 }
13986 else if (STRINGP (glyph->object))
13987 {
13988 Lisp_Object chprop;
13989 ptrdiff_t glyph_pos = glyph->charpos;
13990
13991 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13992 glyph->object);
13993 if (!NILP (chprop))
13994 {
13995 /* If the string came from a `display' text property,
13996 look up the buffer position of that property and
13997 use that position to update bpos_max, as if we
13998 actually saw such a position in one of the row's
13999 glyphs. This helps with supporting integer values
14000 of `cursor' property on the display string in
14001 situations where most or all of the row's buffer
14002 text is completely covered by display properties,
14003 so that no glyph with valid buffer positions is
14004 ever seen in the row. */
14005 ptrdiff_t prop_pos =
14006 string_buffer_position_lim (glyph->object, pos_before,
14007 pos_after, 0);
14008
14009 if (prop_pos >= pos_before)
14010 bpos_max = prop_pos - 1;
14011 }
14012 if (INTEGERP (chprop))
14013 {
14014 bpos_covered = bpos_max + XINT (chprop);
14015 /* If the `cursor' property covers buffer positions up
14016 to and including point, we should display cursor on
14017 this glyph. Note that, if a `cursor' property on one
14018 of the string's characters has an integer value, we
14019 will break out of the loop below _before_ we get to
14020 the position match above. IOW, integer values of
14021 the `cursor' property override the "exact match for
14022 point" strategy of positioning the cursor. */
14023 /* Implementation note: bpos_max == pt_old when, e.g.,
14024 we are in an empty line, where bpos_max is set to
14025 MATRIX_ROW_START_CHARPOS, see above. */
14026 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14027 {
14028 cursor = glyph;
14029 break;
14030 }
14031 }
14032
14033 string_seen = 1;
14034 }
14035 x += glyph->pixel_width;
14036 ++glyph;
14037 }
14038 else if (glyph > end) /* row is reversed */
14039 while (!INTEGERP (glyph->object))
14040 {
14041 if (BUFFERP (glyph->object))
14042 {
14043 ptrdiff_t dpos = glyph->charpos - pt_old;
14044
14045 if (glyph->charpos > bpos_max)
14046 bpos_max = glyph->charpos;
14047 if (glyph->charpos < bpos_min)
14048 bpos_min = glyph->charpos;
14049 if (!glyph->avoid_cursor_p)
14050 {
14051 if (dpos == 0)
14052 {
14053 match_with_avoid_cursor = 0;
14054 break;
14055 }
14056 if (0 > dpos && dpos > pos_before - pt_old)
14057 {
14058 pos_before = glyph->charpos;
14059 glyph_before = glyph;
14060 }
14061 else if (0 < dpos && dpos < pos_after - pt_old)
14062 {
14063 pos_after = glyph->charpos;
14064 glyph_after = glyph;
14065 }
14066 }
14067 else if (dpos == 0)
14068 match_with_avoid_cursor = 1;
14069 }
14070 else if (STRINGP (glyph->object))
14071 {
14072 Lisp_Object chprop;
14073 ptrdiff_t glyph_pos = glyph->charpos;
14074
14075 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14076 glyph->object);
14077 if (!NILP (chprop))
14078 {
14079 ptrdiff_t prop_pos =
14080 string_buffer_position_lim (glyph->object, pos_before,
14081 pos_after, 0);
14082
14083 if (prop_pos >= pos_before)
14084 bpos_max = prop_pos - 1;
14085 }
14086 if (INTEGERP (chprop))
14087 {
14088 bpos_covered = bpos_max + XINT (chprop);
14089 /* If the `cursor' property covers buffer positions up
14090 to and including point, we should display cursor on
14091 this glyph. */
14092 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14093 {
14094 cursor = glyph;
14095 break;
14096 }
14097 }
14098 string_seen = 1;
14099 }
14100 --glyph;
14101 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14102 {
14103 x--; /* can't use any pixel_width */
14104 break;
14105 }
14106 x -= glyph->pixel_width;
14107 }
14108
14109 /* Step 2: If we didn't find an exact match for point, we need to
14110 look for a proper place to put the cursor among glyphs between
14111 GLYPH_BEFORE and GLYPH_AFTER. */
14112 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14113 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14114 && bpos_covered < pt_old)
14115 {
14116 /* An empty line has a single glyph whose OBJECT is zero and
14117 whose CHARPOS is the position of a newline on that line.
14118 Note that on a TTY, there are more glyphs after that, which
14119 were produced by extend_face_to_end_of_line, but their
14120 CHARPOS is zero or negative. */
14121 int empty_line_p =
14122 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14123 && INTEGERP (glyph->object) && glyph->charpos > 0;
14124
14125 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14126 {
14127 ptrdiff_t ellipsis_pos;
14128
14129 /* Scan back over the ellipsis glyphs. */
14130 if (!row->reversed_p)
14131 {
14132 ellipsis_pos = (glyph - 1)->charpos;
14133 while (glyph > row->glyphs[TEXT_AREA]
14134 && (glyph - 1)->charpos == ellipsis_pos)
14135 glyph--, x -= glyph->pixel_width;
14136 /* That loop always goes one position too far, including
14137 the glyph before the ellipsis. So scan forward over
14138 that one. */
14139 x += glyph->pixel_width;
14140 glyph++;
14141 }
14142 else /* row is reversed */
14143 {
14144 ellipsis_pos = (glyph + 1)->charpos;
14145 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14146 && (glyph + 1)->charpos == ellipsis_pos)
14147 glyph++, x += glyph->pixel_width;
14148 x -= glyph->pixel_width;
14149 glyph--;
14150 }
14151 }
14152 else if (match_with_avoid_cursor)
14153 {
14154 cursor = glyph_after;
14155 x = -1;
14156 }
14157 else if (string_seen)
14158 {
14159 int incr = row->reversed_p ? -1 : +1;
14160
14161 /* Need to find the glyph that came out of a string which is
14162 present at point. That glyph is somewhere between
14163 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14164 positioned between POS_BEFORE and POS_AFTER in the
14165 buffer. */
14166 struct glyph *start, *stop;
14167 ptrdiff_t pos = pos_before;
14168
14169 x = -1;
14170
14171 /* If the row ends in a newline from a display string,
14172 reordering could have moved the glyphs belonging to the
14173 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14174 in this case we extend the search to the last glyph in
14175 the row that was not inserted by redisplay. */
14176 if (row->ends_in_newline_from_string_p)
14177 {
14178 glyph_after = end;
14179 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14180 }
14181
14182 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14183 correspond to POS_BEFORE and POS_AFTER, respectively. We
14184 need START and STOP in the order that corresponds to the
14185 row's direction as given by its reversed_p flag. If the
14186 directionality of characters between POS_BEFORE and
14187 POS_AFTER is the opposite of the row's base direction,
14188 these characters will have been reordered for display,
14189 and we need to reverse START and STOP. */
14190 if (!row->reversed_p)
14191 {
14192 start = min (glyph_before, glyph_after);
14193 stop = max (glyph_before, glyph_after);
14194 }
14195 else
14196 {
14197 start = max (glyph_before, glyph_after);
14198 stop = min (glyph_before, glyph_after);
14199 }
14200 for (glyph = start + incr;
14201 row->reversed_p ? glyph > stop : glyph < stop; )
14202 {
14203
14204 /* Any glyphs that come from the buffer are here because
14205 of bidi reordering. Skip them, and only pay
14206 attention to glyphs that came from some string. */
14207 if (STRINGP (glyph->object))
14208 {
14209 Lisp_Object str;
14210 ptrdiff_t tem;
14211 /* If the display property covers the newline, we
14212 need to search for it one position farther. */
14213 ptrdiff_t lim = pos_after
14214 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14215
14216 string_from_text_prop = 0;
14217 str = glyph->object;
14218 tem = string_buffer_position_lim (str, pos, lim, 0);
14219 if (tem == 0 /* from overlay */
14220 || pos <= tem)
14221 {
14222 /* If the string from which this glyph came is
14223 found in the buffer at point, or at position
14224 that is closer to point than pos_after, then
14225 we've found the glyph we've been looking for.
14226 If it comes from an overlay (tem == 0), and
14227 it has the `cursor' property on one of its
14228 glyphs, record that glyph as a candidate for
14229 displaying the cursor. (As in the
14230 unidirectional version, we will display the
14231 cursor on the last candidate we find.) */
14232 if (tem == 0
14233 || tem == pt_old
14234 || (tem - pt_old > 0 && tem < pos_after))
14235 {
14236 /* The glyphs from this string could have
14237 been reordered. Find the one with the
14238 smallest string position. Or there could
14239 be a character in the string with the
14240 `cursor' property, which means display
14241 cursor on that character's glyph. */
14242 ptrdiff_t strpos = glyph->charpos;
14243
14244 if (tem)
14245 {
14246 cursor = glyph;
14247 string_from_text_prop = 1;
14248 }
14249 for ( ;
14250 (row->reversed_p ? glyph > stop : glyph < stop)
14251 && EQ (glyph->object, str);
14252 glyph += incr)
14253 {
14254 Lisp_Object cprop;
14255 ptrdiff_t gpos = glyph->charpos;
14256
14257 cprop = Fget_char_property (make_number (gpos),
14258 Qcursor,
14259 glyph->object);
14260 if (!NILP (cprop))
14261 {
14262 cursor = glyph;
14263 break;
14264 }
14265 if (tem && glyph->charpos < strpos)
14266 {
14267 strpos = glyph->charpos;
14268 cursor = glyph;
14269 }
14270 }
14271
14272 if (tem == pt_old
14273 || (tem - pt_old > 0 && tem < pos_after))
14274 goto compute_x;
14275 }
14276 if (tem)
14277 pos = tem + 1; /* don't find previous instances */
14278 }
14279 /* This string is not what we want; skip all of the
14280 glyphs that came from it. */
14281 while ((row->reversed_p ? glyph > stop : glyph < stop)
14282 && EQ (glyph->object, str))
14283 glyph += incr;
14284 }
14285 else
14286 glyph += incr;
14287 }
14288
14289 /* If we reached the end of the line, and END was from a string,
14290 the cursor is not on this line. */
14291 if (cursor == NULL
14292 && (row->reversed_p ? glyph <= end : glyph >= end)
14293 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14294 && STRINGP (end->object)
14295 && row->continued_p)
14296 return 0;
14297 }
14298 /* A truncated row may not include PT among its character positions.
14299 Setting the cursor inside the scroll margin will trigger
14300 recalculation of hscroll in hscroll_window_tree. But if a
14301 display string covers point, defer to the string-handling
14302 code below to figure this out. */
14303 else if (row->truncated_on_left_p && pt_old < bpos_min)
14304 {
14305 cursor = glyph_before;
14306 x = -1;
14307 }
14308 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14309 /* Zero-width characters produce no glyphs. */
14310 || (!empty_line_p
14311 && (row->reversed_p
14312 ? glyph_after > glyphs_end
14313 : glyph_after < glyphs_end)))
14314 {
14315 cursor = glyph_after;
14316 x = -1;
14317 }
14318 }
14319
14320 compute_x:
14321 if (cursor != NULL)
14322 glyph = cursor;
14323 else if (glyph == glyphs_end
14324 && pos_before == pos_after
14325 && STRINGP ((row->reversed_p
14326 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14327 : row->glyphs[TEXT_AREA])->object))
14328 {
14329 /* If all the glyphs of this row came from strings, put the
14330 cursor on the first glyph of the row. This avoids having the
14331 cursor outside of the text area in this very rare and hard
14332 use case. */
14333 glyph =
14334 row->reversed_p
14335 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14336 : row->glyphs[TEXT_AREA];
14337 }
14338 if (x < 0)
14339 {
14340 struct glyph *g;
14341
14342 /* Need to compute x that corresponds to GLYPH. */
14343 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14344 {
14345 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14346 abort ();
14347 x += g->pixel_width;
14348 }
14349 }
14350
14351 /* ROW could be part of a continued line, which, under bidi
14352 reordering, might have other rows whose start and end charpos
14353 occlude point. Only set w->cursor if we found a better
14354 approximation to the cursor position than we have from previously
14355 examined candidate rows belonging to the same continued line. */
14356 if (/* we already have a candidate row */
14357 w->cursor.vpos >= 0
14358 /* that candidate is not the row we are processing */
14359 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14360 /* Make sure cursor.vpos specifies a row whose start and end
14361 charpos occlude point, and it is valid candidate for being a
14362 cursor-row. This is because some callers of this function
14363 leave cursor.vpos at the row where the cursor was displayed
14364 during the last redisplay cycle. */
14365 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14366 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14367 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14368 {
14369 struct glyph *g1 =
14370 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14371
14372 /* Don't consider glyphs that are outside TEXT_AREA. */
14373 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14374 return 0;
14375 /* Keep the candidate whose buffer position is the closest to
14376 point or has the `cursor' property. */
14377 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14378 w->cursor.hpos >= 0
14379 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14380 && ((BUFFERP (g1->object)
14381 && (g1->charpos == pt_old /* an exact match always wins */
14382 || (BUFFERP (glyph->object)
14383 && eabs (g1->charpos - pt_old)
14384 < eabs (glyph->charpos - pt_old))))
14385 /* previous candidate is a glyph from a string that has
14386 a non-nil `cursor' property */
14387 || (STRINGP (g1->object)
14388 && (!NILP (Fget_char_property (make_number (g1->charpos),
14389 Qcursor, g1->object))
14390 /* previous candidate is from the same display
14391 string as this one, and the display string
14392 came from a text property */
14393 || (EQ (g1->object, glyph->object)
14394 && string_from_text_prop)
14395 /* this candidate is from newline and its
14396 position is not an exact match */
14397 || (INTEGERP (glyph->object)
14398 && glyph->charpos != pt_old)))))
14399 return 0;
14400 /* If this candidate gives an exact match, use that. */
14401 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14402 /* If this candidate is a glyph created for the
14403 terminating newline of a line, and point is on that
14404 newline, it wins because it's an exact match. */
14405 || (!row->continued_p
14406 && INTEGERP (glyph->object)
14407 && glyph->charpos == 0
14408 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14409 /* Otherwise, keep the candidate that comes from a row
14410 spanning less buffer positions. This may win when one or
14411 both candidate positions are on glyphs that came from
14412 display strings, for which we cannot compare buffer
14413 positions. */
14414 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14415 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14416 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14417 return 0;
14418 }
14419 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14420 w->cursor.x = x;
14421 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14422 w->cursor.y = row->y + dy;
14423
14424 if (w == XWINDOW (selected_window))
14425 {
14426 if (!row->continued_p
14427 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14428 && row->x == 0)
14429 {
14430 this_line_buffer = XBUFFER (w->buffer);
14431
14432 CHARPOS (this_line_start_pos)
14433 = MATRIX_ROW_START_CHARPOS (row) + delta;
14434 BYTEPOS (this_line_start_pos)
14435 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14436
14437 CHARPOS (this_line_end_pos)
14438 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14439 BYTEPOS (this_line_end_pos)
14440 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14441
14442 this_line_y = w->cursor.y;
14443 this_line_pixel_height = row->height;
14444 this_line_vpos = w->cursor.vpos;
14445 this_line_start_x = row->x;
14446 }
14447 else
14448 CHARPOS (this_line_start_pos) = 0;
14449 }
14450
14451 return 1;
14452 }
14453
14454
14455 /* Run window scroll functions, if any, for WINDOW with new window
14456 start STARTP. Sets the window start of WINDOW to that position.
14457
14458 We assume that the window's buffer is really current. */
14459
14460 static inline struct text_pos
14461 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14462 {
14463 struct window *w = XWINDOW (window);
14464 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14465
14466 if (current_buffer != XBUFFER (w->buffer))
14467 abort ();
14468
14469 if (!NILP (Vwindow_scroll_functions))
14470 {
14471 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14472 make_number (CHARPOS (startp)));
14473 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14474 /* In case the hook functions switch buffers. */
14475 if (current_buffer != XBUFFER (w->buffer))
14476 set_buffer_internal_1 (XBUFFER (w->buffer));
14477 }
14478
14479 return startp;
14480 }
14481
14482
14483 /* Make sure the line containing the cursor is fully visible.
14484 A value of 1 means there is nothing to be done.
14485 (Either the line is fully visible, or it cannot be made so,
14486 or we cannot tell.)
14487
14488 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14489 is higher than window.
14490
14491 A value of 0 means the caller should do scrolling
14492 as if point had gone off the screen. */
14493
14494 static int
14495 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14496 {
14497 struct glyph_matrix *matrix;
14498 struct glyph_row *row;
14499 int window_height;
14500
14501 if (!make_cursor_line_fully_visible_p)
14502 return 1;
14503
14504 /* It's not always possible to find the cursor, e.g, when a window
14505 is full of overlay strings. Don't do anything in that case. */
14506 if (w->cursor.vpos < 0)
14507 return 1;
14508
14509 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14510 row = MATRIX_ROW (matrix, w->cursor.vpos);
14511
14512 /* If the cursor row is not partially visible, there's nothing to do. */
14513 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14514 return 1;
14515
14516 /* If the row the cursor is in is taller than the window's height,
14517 it's not clear what to do, so do nothing. */
14518 window_height = window_box_height (w);
14519 if (row->height >= window_height)
14520 {
14521 if (!force_p || MINI_WINDOW_P (w)
14522 || w->vscroll || w->cursor.vpos == 0)
14523 return 1;
14524 }
14525 return 0;
14526 }
14527
14528
14529 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14530 non-zero means only WINDOW is redisplayed in redisplay_internal.
14531 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14532 in redisplay_window to bring a partially visible line into view in
14533 the case that only the cursor has moved.
14534
14535 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14536 last screen line's vertical height extends past the end of the screen.
14537
14538 Value is
14539
14540 1 if scrolling succeeded
14541
14542 0 if scrolling didn't find point.
14543
14544 -1 if new fonts have been loaded so that we must interrupt
14545 redisplay, adjust glyph matrices, and try again. */
14546
14547 enum
14548 {
14549 SCROLLING_SUCCESS,
14550 SCROLLING_FAILED,
14551 SCROLLING_NEED_LARGER_MATRICES
14552 };
14553
14554 /* If scroll-conservatively is more than this, never recenter.
14555
14556 If you change this, don't forget to update the doc string of
14557 `scroll-conservatively' and the Emacs manual. */
14558 #define SCROLL_LIMIT 100
14559
14560 static int
14561 try_scrolling (Lisp_Object window, int just_this_one_p,
14562 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14563 int temp_scroll_step, int last_line_misfit)
14564 {
14565 struct window *w = XWINDOW (window);
14566 struct frame *f = XFRAME (w->frame);
14567 struct text_pos pos, startp;
14568 struct it it;
14569 int this_scroll_margin, scroll_max, rc, height;
14570 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14571 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14572 Lisp_Object aggressive;
14573 /* We will never try scrolling more than this number of lines. */
14574 int scroll_limit = SCROLL_LIMIT;
14575
14576 #ifdef GLYPH_DEBUG
14577 debug_method_add (w, "try_scrolling");
14578 #endif
14579
14580 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14581
14582 /* Compute scroll margin height in pixels. We scroll when point is
14583 within this distance from the top or bottom of the window. */
14584 if (scroll_margin > 0)
14585 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14586 * FRAME_LINE_HEIGHT (f);
14587 else
14588 this_scroll_margin = 0;
14589
14590 /* Force arg_scroll_conservatively to have a reasonable value, to
14591 avoid scrolling too far away with slow move_it_* functions. Note
14592 that the user can supply scroll-conservatively equal to
14593 `most-positive-fixnum', which can be larger than INT_MAX. */
14594 if (arg_scroll_conservatively > scroll_limit)
14595 {
14596 arg_scroll_conservatively = scroll_limit + 1;
14597 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14598 }
14599 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14600 /* Compute how much we should try to scroll maximally to bring
14601 point into view. */
14602 scroll_max = (max (scroll_step,
14603 max (arg_scroll_conservatively, temp_scroll_step))
14604 * FRAME_LINE_HEIGHT (f));
14605 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14606 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14607 /* We're trying to scroll because of aggressive scrolling but no
14608 scroll_step is set. Choose an arbitrary one. */
14609 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14610 else
14611 scroll_max = 0;
14612
14613 too_near_end:
14614
14615 /* Decide whether to scroll down. */
14616 if (PT > CHARPOS (startp))
14617 {
14618 int scroll_margin_y;
14619
14620 /* Compute the pixel ypos of the scroll margin, then move IT to
14621 either that ypos or PT, whichever comes first. */
14622 start_display (&it, w, startp);
14623 scroll_margin_y = it.last_visible_y - this_scroll_margin
14624 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14625 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14626 (MOVE_TO_POS | MOVE_TO_Y));
14627
14628 if (PT > CHARPOS (it.current.pos))
14629 {
14630 int y0 = line_bottom_y (&it);
14631 /* Compute how many pixels below window bottom to stop searching
14632 for PT. This avoids costly search for PT that is far away if
14633 the user limited scrolling by a small number of lines, but
14634 always finds PT if scroll_conservatively is set to a large
14635 number, such as most-positive-fixnum. */
14636 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14637 int y_to_move = it.last_visible_y + slack;
14638
14639 /* Compute the distance from the scroll margin to PT or to
14640 the scroll limit, whichever comes first. This should
14641 include the height of the cursor line, to make that line
14642 fully visible. */
14643 move_it_to (&it, PT, -1, y_to_move,
14644 -1, MOVE_TO_POS | MOVE_TO_Y);
14645 dy = line_bottom_y (&it) - y0;
14646
14647 if (dy > scroll_max)
14648 return SCROLLING_FAILED;
14649
14650 if (dy > 0)
14651 scroll_down_p = 1;
14652 }
14653 }
14654
14655 if (scroll_down_p)
14656 {
14657 /* Point is in or below the bottom scroll margin, so move the
14658 window start down. If scrolling conservatively, move it just
14659 enough down to make point visible. If scroll_step is set,
14660 move it down by scroll_step. */
14661 if (arg_scroll_conservatively)
14662 amount_to_scroll
14663 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14664 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14665 else if (scroll_step || temp_scroll_step)
14666 amount_to_scroll = scroll_max;
14667 else
14668 {
14669 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14670 height = WINDOW_BOX_TEXT_HEIGHT (w);
14671 if (NUMBERP (aggressive))
14672 {
14673 double float_amount = XFLOATINT (aggressive) * height;
14674 amount_to_scroll = float_amount;
14675 if (amount_to_scroll == 0 && float_amount > 0)
14676 amount_to_scroll = 1;
14677 /* Don't let point enter the scroll margin near top of
14678 the window. */
14679 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14680 amount_to_scroll = height - 2*this_scroll_margin + dy;
14681 }
14682 }
14683
14684 if (amount_to_scroll <= 0)
14685 return SCROLLING_FAILED;
14686
14687 start_display (&it, w, startp);
14688 if (arg_scroll_conservatively <= scroll_limit)
14689 move_it_vertically (&it, amount_to_scroll);
14690 else
14691 {
14692 /* Extra precision for users who set scroll-conservatively
14693 to a large number: make sure the amount we scroll
14694 the window start is never less than amount_to_scroll,
14695 which was computed as distance from window bottom to
14696 point. This matters when lines at window top and lines
14697 below window bottom have different height. */
14698 struct it it1;
14699 void *it1data = NULL;
14700 /* We use a temporary it1 because line_bottom_y can modify
14701 its argument, if it moves one line down; see there. */
14702 int start_y;
14703
14704 SAVE_IT (it1, it, it1data);
14705 start_y = line_bottom_y (&it1);
14706 do {
14707 RESTORE_IT (&it, &it, it1data);
14708 move_it_by_lines (&it, 1);
14709 SAVE_IT (it1, it, it1data);
14710 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14711 }
14712
14713 /* If STARTP is unchanged, move it down another screen line. */
14714 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14715 move_it_by_lines (&it, 1);
14716 startp = it.current.pos;
14717 }
14718 else
14719 {
14720 struct text_pos scroll_margin_pos = startp;
14721
14722 /* See if point is inside the scroll margin at the top of the
14723 window. */
14724 if (this_scroll_margin)
14725 {
14726 start_display (&it, w, startp);
14727 move_it_vertically (&it, this_scroll_margin);
14728 scroll_margin_pos = it.current.pos;
14729 }
14730
14731 if (PT < CHARPOS (scroll_margin_pos))
14732 {
14733 /* Point is in the scroll margin at the top of the window or
14734 above what is displayed in the window. */
14735 int y0, y_to_move;
14736
14737 /* Compute the vertical distance from PT to the scroll
14738 margin position. Move as far as scroll_max allows, or
14739 one screenful, or 10 screen lines, whichever is largest.
14740 Give up if distance is greater than scroll_max. */
14741 SET_TEXT_POS (pos, PT, PT_BYTE);
14742 start_display (&it, w, pos);
14743 y0 = it.current_y;
14744 y_to_move = max (it.last_visible_y,
14745 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14746 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14747 y_to_move, -1,
14748 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14749 dy = it.current_y - y0;
14750 if (dy > scroll_max)
14751 return SCROLLING_FAILED;
14752
14753 /* Compute new window start. */
14754 start_display (&it, w, startp);
14755
14756 if (arg_scroll_conservatively)
14757 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14758 max (scroll_step, temp_scroll_step));
14759 else if (scroll_step || temp_scroll_step)
14760 amount_to_scroll = scroll_max;
14761 else
14762 {
14763 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14764 height = WINDOW_BOX_TEXT_HEIGHT (w);
14765 if (NUMBERP (aggressive))
14766 {
14767 double float_amount = XFLOATINT (aggressive) * height;
14768 amount_to_scroll = float_amount;
14769 if (amount_to_scroll == 0 && float_amount > 0)
14770 amount_to_scroll = 1;
14771 amount_to_scroll -=
14772 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14773 /* Don't let point enter the scroll margin near
14774 bottom of the window. */
14775 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14776 amount_to_scroll = height - 2*this_scroll_margin + dy;
14777 }
14778 }
14779
14780 if (amount_to_scroll <= 0)
14781 return SCROLLING_FAILED;
14782
14783 move_it_vertically_backward (&it, amount_to_scroll);
14784 startp = it.current.pos;
14785 }
14786 }
14787
14788 /* Run window scroll functions. */
14789 startp = run_window_scroll_functions (window, startp);
14790
14791 /* Display the window. Give up if new fonts are loaded, or if point
14792 doesn't appear. */
14793 if (!try_window (window, startp, 0))
14794 rc = SCROLLING_NEED_LARGER_MATRICES;
14795 else if (w->cursor.vpos < 0)
14796 {
14797 clear_glyph_matrix (w->desired_matrix);
14798 rc = SCROLLING_FAILED;
14799 }
14800 else
14801 {
14802 /* Maybe forget recorded base line for line number display. */
14803 if (!just_this_one_p
14804 || current_buffer->clip_changed
14805 || BEG_UNCHANGED < CHARPOS (startp))
14806 w->base_line_number = Qnil;
14807
14808 /* If cursor ends up on a partially visible line,
14809 treat that as being off the bottom of the screen. */
14810 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14811 /* It's possible that the cursor is on the first line of the
14812 buffer, which is partially obscured due to a vscroll
14813 (Bug#7537). In that case, avoid looping forever . */
14814 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14815 {
14816 clear_glyph_matrix (w->desired_matrix);
14817 ++extra_scroll_margin_lines;
14818 goto too_near_end;
14819 }
14820 rc = SCROLLING_SUCCESS;
14821 }
14822
14823 return rc;
14824 }
14825
14826
14827 /* Compute a suitable window start for window W if display of W starts
14828 on a continuation line. Value is non-zero if a new window start
14829 was computed.
14830
14831 The new window start will be computed, based on W's width, starting
14832 from the start of the continued line. It is the start of the
14833 screen line with the minimum distance from the old start W->start. */
14834
14835 static int
14836 compute_window_start_on_continuation_line (struct window *w)
14837 {
14838 struct text_pos pos, start_pos;
14839 int window_start_changed_p = 0;
14840
14841 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14842
14843 /* If window start is on a continuation line... Window start may be
14844 < BEGV in case there's invisible text at the start of the
14845 buffer (M-x rmail, for example). */
14846 if (CHARPOS (start_pos) > BEGV
14847 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14848 {
14849 struct it it;
14850 struct glyph_row *row;
14851
14852 /* Handle the case that the window start is out of range. */
14853 if (CHARPOS (start_pos) < BEGV)
14854 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14855 else if (CHARPOS (start_pos) > ZV)
14856 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14857
14858 /* Find the start of the continued line. This should be fast
14859 because scan_buffer is fast (newline cache). */
14860 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14861 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14862 row, DEFAULT_FACE_ID);
14863 reseat_at_previous_visible_line_start (&it);
14864
14865 /* If the line start is "too far" away from the window start,
14866 say it takes too much time to compute a new window start. */
14867 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14868 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14869 {
14870 int min_distance, distance;
14871
14872 /* Move forward by display lines to find the new window
14873 start. If window width was enlarged, the new start can
14874 be expected to be > the old start. If window width was
14875 decreased, the new window start will be < the old start.
14876 So, we're looking for the display line start with the
14877 minimum distance from the old window start. */
14878 pos = it.current.pos;
14879 min_distance = INFINITY;
14880 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14881 distance < min_distance)
14882 {
14883 min_distance = distance;
14884 pos = it.current.pos;
14885 move_it_by_lines (&it, 1);
14886 }
14887
14888 /* Set the window start there. */
14889 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14890 window_start_changed_p = 1;
14891 }
14892 }
14893
14894 return window_start_changed_p;
14895 }
14896
14897
14898 /* Try cursor movement in case text has not changed in window WINDOW,
14899 with window start STARTP. Value is
14900
14901 CURSOR_MOVEMENT_SUCCESS if successful
14902
14903 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14904
14905 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14906 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14907 we want to scroll as if scroll-step were set to 1. See the code.
14908
14909 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14910 which case we have to abort this redisplay, and adjust matrices
14911 first. */
14912
14913 enum
14914 {
14915 CURSOR_MOVEMENT_SUCCESS,
14916 CURSOR_MOVEMENT_CANNOT_BE_USED,
14917 CURSOR_MOVEMENT_MUST_SCROLL,
14918 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14919 };
14920
14921 static int
14922 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14923 {
14924 struct window *w = XWINDOW (window);
14925 struct frame *f = XFRAME (w->frame);
14926 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14927
14928 #ifdef GLYPH_DEBUG
14929 if (inhibit_try_cursor_movement)
14930 return rc;
14931 #endif
14932
14933 /* Previously, there was a check for Lisp integer in the
14934 if-statement below. Now, this field is converted to
14935 ptrdiff_t, thus zero means invalid position in a buffer. */
14936 eassert (w->last_point > 0);
14937
14938 /* Handle case where text has not changed, only point, and it has
14939 not moved off the frame. */
14940 if (/* Point may be in this window. */
14941 PT >= CHARPOS (startp)
14942 /* Selective display hasn't changed. */
14943 && !current_buffer->clip_changed
14944 /* Function force-mode-line-update is used to force a thorough
14945 redisplay. It sets either windows_or_buffers_changed or
14946 update_mode_lines. So don't take a shortcut here for these
14947 cases. */
14948 && !update_mode_lines
14949 && !windows_or_buffers_changed
14950 && !cursor_type_changed
14951 /* Can't use this case if highlighting a region. When a
14952 region exists, cursor movement has to do more than just
14953 set the cursor. */
14954 && !(!NILP (Vtransient_mark_mode)
14955 && !NILP (BVAR (current_buffer, mark_active)))
14956 && NILP (w->region_showing)
14957 && NILP (Vshow_trailing_whitespace)
14958 /* This code is not used for mini-buffer for the sake of the case
14959 of redisplaying to replace an echo area message; since in
14960 that case the mini-buffer contents per se are usually
14961 unchanged. This code is of no real use in the mini-buffer
14962 since the handling of this_line_start_pos, etc., in redisplay
14963 handles the same cases. */
14964 && !EQ (window, minibuf_window)
14965 /* When splitting windows or for new windows, it happens that
14966 redisplay is called with a nil window_end_vpos or one being
14967 larger than the window. This should really be fixed in
14968 window.c. I don't have this on my list, now, so we do
14969 approximately the same as the old redisplay code. --gerd. */
14970 && INTEGERP (w->window_end_vpos)
14971 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14972 && (FRAME_WINDOW_P (f)
14973 || !overlay_arrow_in_current_buffer_p ()))
14974 {
14975 int this_scroll_margin, top_scroll_margin;
14976 struct glyph_row *row = NULL;
14977
14978 #ifdef GLYPH_DEBUG
14979 debug_method_add (w, "cursor movement");
14980 #endif
14981
14982 /* Scroll if point within this distance from the top or bottom
14983 of the window. This is a pixel value. */
14984 if (scroll_margin > 0)
14985 {
14986 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14987 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14988 }
14989 else
14990 this_scroll_margin = 0;
14991
14992 top_scroll_margin = this_scroll_margin;
14993 if (WINDOW_WANTS_HEADER_LINE_P (w))
14994 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14995
14996 /* Start with the row the cursor was displayed during the last
14997 not paused redisplay. Give up if that row is not valid. */
14998 if (w->last_cursor.vpos < 0
14999 || w->last_cursor.vpos >= w->current_matrix->nrows)
15000 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15001 else
15002 {
15003 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15004 if (row->mode_line_p)
15005 ++row;
15006 if (!row->enabled_p)
15007 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15008 }
15009
15010 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15011 {
15012 int scroll_p = 0, must_scroll = 0;
15013 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15014
15015 if (PT > w->last_point)
15016 {
15017 /* Point has moved forward. */
15018 while (MATRIX_ROW_END_CHARPOS (row) < PT
15019 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15020 {
15021 eassert (row->enabled_p);
15022 ++row;
15023 }
15024
15025 /* If the end position of a row equals the start
15026 position of the next row, and PT is at that position,
15027 we would rather display cursor in the next line. */
15028 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15029 && MATRIX_ROW_END_CHARPOS (row) == PT
15030 && row < w->current_matrix->rows
15031 + w->current_matrix->nrows - 1
15032 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15033 && !cursor_row_p (row))
15034 ++row;
15035
15036 /* If within the scroll margin, scroll. Note that
15037 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15038 the next line would be drawn, and that
15039 this_scroll_margin can be zero. */
15040 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15041 || PT > MATRIX_ROW_END_CHARPOS (row)
15042 /* Line is completely visible last line in window
15043 and PT is to be set in the next line. */
15044 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15045 && PT == MATRIX_ROW_END_CHARPOS (row)
15046 && !row->ends_at_zv_p
15047 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15048 scroll_p = 1;
15049 }
15050 else if (PT < w->last_point)
15051 {
15052 /* Cursor has to be moved backward. Note that PT >=
15053 CHARPOS (startp) because of the outer if-statement. */
15054 while (!row->mode_line_p
15055 && (MATRIX_ROW_START_CHARPOS (row) > PT
15056 || (MATRIX_ROW_START_CHARPOS (row) == PT
15057 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15058 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15059 row > w->current_matrix->rows
15060 && (row-1)->ends_in_newline_from_string_p))))
15061 && (row->y > top_scroll_margin
15062 || CHARPOS (startp) == BEGV))
15063 {
15064 eassert (row->enabled_p);
15065 --row;
15066 }
15067
15068 /* Consider the following case: Window starts at BEGV,
15069 there is invisible, intangible text at BEGV, so that
15070 display starts at some point START > BEGV. It can
15071 happen that we are called with PT somewhere between
15072 BEGV and START. Try to handle that case. */
15073 if (row < w->current_matrix->rows
15074 || row->mode_line_p)
15075 {
15076 row = w->current_matrix->rows;
15077 if (row->mode_line_p)
15078 ++row;
15079 }
15080
15081 /* Due to newlines in overlay strings, we may have to
15082 skip forward over overlay strings. */
15083 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15084 && MATRIX_ROW_END_CHARPOS (row) == PT
15085 && !cursor_row_p (row))
15086 ++row;
15087
15088 /* If within the scroll margin, scroll. */
15089 if (row->y < top_scroll_margin
15090 && CHARPOS (startp) != BEGV)
15091 scroll_p = 1;
15092 }
15093 else
15094 {
15095 /* Cursor did not move. So don't scroll even if cursor line
15096 is partially visible, as it was so before. */
15097 rc = CURSOR_MOVEMENT_SUCCESS;
15098 }
15099
15100 if (PT < MATRIX_ROW_START_CHARPOS (row)
15101 || PT > MATRIX_ROW_END_CHARPOS (row))
15102 {
15103 /* if PT is not in the glyph row, give up. */
15104 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15105 must_scroll = 1;
15106 }
15107 else if (rc != CURSOR_MOVEMENT_SUCCESS
15108 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15109 {
15110 struct glyph_row *row1;
15111
15112 /* If rows are bidi-reordered and point moved, back up
15113 until we find a row that does not belong to a
15114 continuation line. This is because we must consider
15115 all rows of a continued line as candidates for the
15116 new cursor positioning, since row start and end
15117 positions change non-linearly with vertical position
15118 in such rows. */
15119 /* FIXME: Revisit this when glyph ``spilling'' in
15120 continuation lines' rows is implemented for
15121 bidi-reordered rows. */
15122 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15123 MATRIX_ROW_CONTINUATION_LINE_P (row);
15124 --row)
15125 {
15126 /* If we hit the beginning of the displayed portion
15127 without finding the first row of a continued
15128 line, give up. */
15129 if (row <= row1)
15130 {
15131 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15132 break;
15133 }
15134 eassert (row->enabled_p);
15135 }
15136 }
15137 if (must_scroll)
15138 ;
15139 else if (rc != CURSOR_MOVEMENT_SUCCESS
15140 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15141 /* Make sure this isn't a header line by any chance, since
15142 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15143 && !row->mode_line_p
15144 && make_cursor_line_fully_visible_p)
15145 {
15146 if (PT == MATRIX_ROW_END_CHARPOS (row)
15147 && !row->ends_at_zv_p
15148 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15149 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15150 else if (row->height > window_box_height (w))
15151 {
15152 /* If we end up in a partially visible line, let's
15153 make it fully visible, except when it's taller
15154 than the window, in which case we can't do much
15155 about it. */
15156 *scroll_step = 1;
15157 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15158 }
15159 else
15160 {
15161 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15162 if (!cursor_row_fully_visible_p (w, 0, 1))
15163 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15164 else
15165 rc = CURSOR_MOVEMENT_SUCCESS;
15166 }
15167 }
15168 else if (scroll_p)
15169 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15170 else if (rc != CURSOR_MOVEMENT_SUCCESS
15171 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15172 {
15173 /* With bidi-reordered rows, there could be more than
15174 one candidate row whose start and end positions
15175 occlude point. We need to let set_cursor_from_row
15176 find the best candidate. */
15177 /* FIXME: Revisit this when glyph ``spilling'' in
15178 continuation lines' rows is implemented for
15179 bidi-reordered rows. */
15180 int rv = 0;
15181
15182 do
15183 {
15184 int at_zv_p = 0, exact_match_p = 0;
15185
15186 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15187 && PT <= MATRIX_ROW_END_CHARPOS (row)
15188 && cursor_row_p (row))
15189 rv |= set_cursor_from_row (w, row, w->current_matrix,
15190 0, 0, 0, 0);
15191 /* As soon as we've found the exact match for point,
15192 or the first suitable row whose ends_at_zv_p flag
15193 is set, we are done. */
15194 at_zv_p =
15195 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15196 if (rv && !at_zv_p
15197 && w->cursor.hpos >= 0
15198 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15199 w->cursor.vpos))
15200 {
15201 struct glyph_row *candidate =
15202 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15203 struct glyph *g =
15204 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15205 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15206
15207 exact_match_p =
15208 (BUFFERP (g->object) && g->charpos == PT)
15209 || (INTEGERP (g->object)
15210 && (g->charpos == PT
15211 || (g->charpos == 0 && endpos - 1 == PT)));
15212 }
15213 if (rv && (at_zv_p || exact_match_p))
15214 {
15215 rc = CURSOR_MOVEMENT_SUCCESS;
15216 break;
15217 }
15218 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15219 break;
15220 ++row;
15221 }
15222 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15223 || row->continued_p)
15224 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15225 || (MATRIX_ROW_START_CHARPOS (row) == PT
15226 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15227 /* If we didn't find any candidate rows, or exited the
15228 loop before all the candidates were examined, signal
15229 to the caller that this method failed. */
15230 if (rc != CURSOR_MOVEMENT_SUCCESS
15231 && !(rv
15232 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15233 && !row->continued_p))
15234 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15235 else if (rv)
15236 rc = CURSOR_MOVEMENT_SUCCESS;
15237 }
15238 else
15239 {
15240 do
15241 {
15242 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15243 {
15244 rc = CURSOR_MOVEMENT_SUCCESS;
15245 break;
15246 }
15247 ++row;
15248 }
15249 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15250 && MATRIX_ROW_START_CHARPOS (row) == PT
15251 && cursor_row_p (row));
15252 }
15253 }
15254 }
15255
15256 return rc;
15257 }
15258
15259 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15260 static
15261 #endif
15262 void
15263 set_vertical_scroll_bar (struct window *w)
15264 {
15265 ptrdiff_t start, end, whole;
15266
15267 /* Calculate the start and end positions for the current window.
15268 At some point, it would be nice to choose between scrollbars
15269 which reflect the whole buffer size, with special markers
15270 indicating narrowing, and scrollbars which reflect only the
15271 visible region.
15272
15273 Note that mini-buffers sometimes aren't displaying any text. */
15274 if (!MINI_WINDOW_P (w)
15275 || (w == XWINDOW (minibuf_window)
15276 && NILP (echo_area_buffer[0])))
15277 {
15278 struct buffer *buf = XBUFFER (w->buffer);
15279 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15280 start = marker_position (w->start) - BUF_BEGV (buf);
15281 /* I don't think this is guaranteed to be right. For the
15282 moment, we'll pretend it is. */
15283 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15284
15285 if (end < start)
15286 end = start;
15287 if (whole < (end - start))
15288 whole = end - start;
15289 }
15290 else
15291 start = end = whole = 0;
15292
15293 /* Indicate what this scroll bar ought to be displaying now. */
15294 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15295 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15296 (w, end - start, whole, start);
15297 }
15298
15299
15300 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15301 selected_window is redisplayed.
15302
15303 We can return without actually redisplaying the window if
15304 fonts_changed_p is nonzero. In that case, redisplay_internal will
15305 retry. */
15306
15307 static void
15308 redisplay_window (Lisp_Object window, int just_this_one_p)
15309 {
15310 struct window *w = XWINDOW (window);
15311 struct frame *f = XFRAME (w->frame);
15312 struct buffer *buffer = XBUFFER (w->buffer);
15313 struct buffer *old = current_buffer;
15314 struct text_pos lpoint, opoint, startp;
15315 int update_mode_line;
15316 int tem;
15317 struct it it;
15318 /* Record it now because it's overwritten. */
15319 int current_matrix_up_to_date_p = 0;
15320 int used_current_matrix_p = 0;
15321 /* This is less strict than current_matrix_up_to_date_p.
15322 It indicates that the buffer contents and narrowing are unchanged. */
15323 int buffer_unchanged_p = 0;
15324 int temp_scroll_step = 0;
15325 ptrdiff_t count = SPECPDL_INDEX ();
15326 int rc;
15327 int centering_position = -1;
15328 int last_line_misfit = 0;
15329 ptrdiff_t beg_unchanged, end_unchanged;
15330
15331 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15332 opoint = lpoint;
15333
15334 /* W must be a leaf window here. */
15335 eassert (!NILP (w->buffer));
15336 #ifdef GLYPH_DEBUG
15337 *w->desired_matrix->method = 0;
15338 #endif
15339
15340 restart:
15341 reconsider_clip_changes (w, buffer);
15342
15343 /* Has the mode line to be updated? */
15344 update_mode_line = (w->update_mode_line
15345 || update_mode_lines
15346 || buffer->clip_changed
15347 || buffer->prevent_redisplay_optimizations_p);
15348
15349 if (MINI_WINDOW_P (w))
15350 {
15351 if (w == XWINDOW (echo_area_window)
15352 && !NILP (echo_area_buffer[0]))
15353 {
15354 if (update_mode_line)
15355 /* We may have to update a tty frame's menu bar or a
15356 tool-bar. Example `M-x C-h C-h C-g'. */
15357 goto finish_menu_bars;
15358 else
15359 /* We've already displayed the echo area glyphs in this window. */
15360 goto finish_scroll_bars;
15361 }
15362 else if ((w != XWINDOW (minibuf_window)
15363 || minibuf_level == 0)
15364 /* When buffer is nonempty, redisplay window normally. */
15365 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15366 /* Quail displays non-mini buffers in minibuffer window.
15367 In that case, redisplay the window normally. */
15368 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15369 {
15370 /* W is a mini-buffer window, but it's not active, so clear
15371 it. */
15372 int yb = window_text_bottom_y (w);
15373 struct glyph_row *row;
15374 int y;
15375
15376 for (y = 0, row = w->desired_matrix->rows;
15377 y < yb;
15378 y += row->height, ++row)
15379 blank_row (w, row, y);
15380 goto finish_scroll_bars;
15381 }
15382
15383 clear_glyph_matrix (w->desired_matrix);
15384 }
15385
15386 /* Otherwise set up data on this window; select its buffer and point
15387 value. */
15388 /* Really select the buffer, for the sake of buffer-local
15389 variables. */
15390 set_buffer_internal_1 (XBUFFER (w->buffer));
15391
15392 current_matrix_up_to_date_p
15393 = (!NILP (w->window_end_valid)
15394 && !current_buffer->clip_changed
15395 && !current_buffer->prevent_redisplay_optimizations_p
15396 && w->last_modified >= MODIFF
15397 && w->last_overlay_modified >= OVERLAY_MODIFF);
15398
15399 /* Run the window-bottom-change-functions
15400 if it is possible that the text on the screen has changed
15401 (either due to modification of the text, or any other reason). */
15402 if (!current_matrix_up_to_date_p
15403 && !NILP (Vwindow_text_change_functions))
15404 {
15405 safe_run_hooks (Qwindow_text_change_functions);
15406 goto restart;
15407 }
15408
15409 beg_unchanged = BEG_UNCHANGED;
15410 end_unchanged = END_UNCHANGED;
15411
15412 SET_TEXT_POS (opoint, PT, PT_BYTE);
15413
15414 specbind (Qinhibit_point_motion_hooks, Qt);
15415
15416 buffer_unchanged_p
15417 = (!NILP (w->window_end_valid)
15418 && !current_buffer->clip_changed
15419 && w->last_modified >= MODIFF
15420 && w->last_overlay_modified >= OVERLAY_MODIFF);
15421
15422 /* When windows_or_buffers_changed is non-zero, we can't rely on
15423 the window end being valid, so set it to nil there. */
15424 if (windows_or_buffers_changed)
15425 {
15426 /* If window starts on a continuation line, maybe adjust the
15427 window start in case the window's width changed. */
15428 if (XMARKER (w->start)->buffer == current_buffer)
15429 compute_window_start_on_continuation_line (w);
15430
15431 w->window_end_valid = Qnil;
15432 }
15433
15434 /* Some sanity checks. */
15435 CHECK_WINDOW_END (w);
15436 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15437 abort ();
15438 if (BYTEPOS (opoint) < CHARPOS (opoint))
15439 abort ();
15440
15441 /* If %c is in mode line, update it if needed. */
15442 if (!NILP (w->column_number_displayed)
15443 /* This alternative quickly identifies a common case
15444 where no change is needed. */
15445 && !(PT == w->last_point
15446 && w->last_modified >= MODIFF
15447 && w->last_overlay_modified >= OVERLAY_MODIFF)
15448 && (XFASTINT (w->column_number_displayed) != current_column ()))
15449 update_mode_line = 1;
15450
15451 /* Count number of windows showing the selected buffer. An indirect
15452 buffer counts as its base buffer. */
15453 if (!just_this_one_p)
15454 {
15455 struct buffer *current_base, *window_base;
15456 current_base = current_buffer;
15457 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15458 if (current_base->base_buffer)
15459 current_base = current_base->base_buffer;
15460 if (window_base->base_buffer)
15461 window_base = window_base->base_buffer;
15462 if (current_base == window_base)
15463 buffer_shared++;
15464 }
15465
15466 /* Point refers normally to the selected window. For any other
15467 window, set up appropriate value. */
15468 if (!EQ (window, selected_window))
15469 {
15470 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15471 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15472 if (new_pt < BEGV)
15473 {
15474 new_pt = BEGV;
15475 new_pt_byte = BEGV_BYTE;
15476 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15477 }
15478 else if (new_pt > (ZV - 1))
15479 {
15480 new_pt = ZV;
15481 new_pt_byte = ZV_BYTE;
15482 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15483 }
15484
15485 /* We don't use SET_PT so that the point-motion hooks don't run. */
15486 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15487 }
15488
15489 /* If any of the character widths specified in the display table
15490 have changed, invalidate the width run cache. It's true that
15491 this may be a bit late to catch such changes, but the rest of
15492 redisplay goes (non-fatally) haywire when the display table is
15493 changed, so why should we worry about doing any better? */
15494 if (current_buffer->width_run_cache)
15495 {
15496 struct Lisp_Char_Table *disptab = buffer_display_table ();
15497
15498 if (! disptab_matches_widthtab (disptab,
15499 XVECTOR (BVAR (current_buffer, width_table))))
15500 {
15501 invalidate_region_cache (current_buffer,
15502 current_buffer->width_run_cache,
15503 BEG, Z);
15504 recompute_width_table (current_buffer, disptab);
15505 }
15506 }
15507
15508 /* If window-start is screwed up, choose a new one. */
15509 if (XMARKER (w->start)->buffer != current_buffer)
15510 goto recenter;
15511
15512 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15513
15514 /* If someone specified a new starting point but did not insist,
15515 check whether it can be used. */
15516 if (w->optional_new_start
15517 && CHARPOS (startp) >= BEGV
15518 && CHARPOS (startp) <= ZV)
15519 {
15520 w->optional_new_start = 0;
15521 start_display (&it, w, startp);
15522 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15523 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15524 if (IT_CHARPOS (it) == PT)
15525 w->force_start = 1;
15526 /* IT may overshoot PT if text at PT is invisible. */
15527 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15528 w->force_start = 1;
15529 }
15530
15531 force_start:
15532
15533 /* Handle case where place to start displaying has been specified,
15534 unless the specified location is outside the accessible range. */
15535 if (w->force_start || w->frozen_window_start_p)
15536 {
15537 /* We set this later on if we have to adjust point. */
15538 int new_vpos = -1;
15539
15540 w->force_start = 0;
15541 w->vscroll = 0;
15542 w->window_end_valid = Qnil;
15543
15544 /* Forget any recorded base line for line number display. */
15545 if (!buffer_unchanged_p)
15546 w->base_line_number = Qnil;
15547
15548 /* Redisplay the mode line. Select the buffer properly for that.
15549 Also, run the hook window-scroll-functions
15550 because we have scrolled. */
15551 /* Note, we do this after clearing force_start because
15552 if there's an error, it is better to forget about force_start
15553 than to get into an infinite loop calling the hook functions
15554 and having them get more errors. */
15555 if (!update_mode_line
15556 || ! NILP (Vwindow_scroll_functions))
15557 {
15558 update_mode_line = 1;
15559 w->update_mode_line = 1;
15560 startp = run_window_scroll_functions (window, startp);
15561 }
15562
15563 w->last_modified = 0;
15564 w->last_overlay_modified = 0;
15565 if (CHARPOS (startp) < BEGV)
15566 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15567 else if (CHARPOS (startp) > ZV)
15568 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15569
15570 /* Redisplay, then check if cursor has been set during the
15571 redisplay. Give up if new fonts were loaded. */
15572 /* We used to issue a CHECK_MARGINS argument to try_window here,
15573 but this causes scrolling to fail when point begins inside
15574 the scroll margin (bug#148) -- cyd */
15575 if (!try_window (window, startp, 0))
15576 {
15577 w->force_start = 1;
15578 clear_glyph_matrix (w->desired_matrix);
15579 goto need_larger_matrices;
15580 }
15581
15582 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15583 {
15584 /* If point does not appear, try to move point so it does
15585 appear. The desired matrix has been built above, so we
15586 can use it here. */
15587 new_vpos = window_box_height (w) / 2;
15588 }
15589
15590 if (!cursor_row_fully_visible_p (w, 0, 0))
15591 {
15592 /* Point does appear, but on a line partly visible at end of window.
15593 Move it back to a fully-visible line. */
15594 new_vpos = window_box_height (w);
15595 }
15596
15597 /* If we need to move point for either of the above reasons,
15598 now actually do it. */
15599 if (new_vpos >= 0)
15600 {
15601 struct glyph_row *row;
15602
15603 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15604 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15605 ++row;
15606
15607 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15608 MATRIX_ROW_START_BYTEPOS (row));
15609
15610 if (w != XWINDOW (selected_window))
15611 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15612 else if (current_buffer == old)
15613 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15614
15615 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15616
15617 /* If we are highlighting the region, then we just changed
15618 the region, so redisplay to show it. */
15619 if (!NILP (Vtransient_mark_mode)
15620 && !NILP (BVAR (current_buffer, mark_active)))
15621 {
15622 clear_glyph_matrix (w->desired_matrix);
15623 if (!try_window (window, startp, 0))
15624 goto need_larger_matrices;
15625 }
15626 }
15627
15628 #ifdef GLYPH_DEBUG
15629 debug_method_add (w, "forced window start");
15630 #endif
15631 goto done;
15632 }
15633
15634 /* Handle case where text has not changed, only point, and it has
15635 not moved off the frame, and we are not retrying after hscroll.
15636 (current_matrix_up_to_date_p is nonzero when retrying.) */
15637 if (current_matrix_up_to_date_p
15638 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15639 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15640 {
15641 switch (rc)
15642 {
15643 case CURSOR_MOVEMENT_SUCCESS:
15644 used_current_matrix_p = 1;
15645 goto done;
15646
15647 case CURSOR_MOVEMENT_MUST_SCROLL:
15648 goto try_to_scroll;
15649
15650 default:
15651 abort ();
15652 }
15653 }
15654 /* If current starting point was originally the beginning of a line
15655 but no longer is, find a new starting point. */
15656 else if (w->start_at_line_beg
15657 && !(CHARPOS (startp) <= BEGV
15658 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15659 {
15660 #ifdef GLYPH_DEBUG
15661 debug_method_add (w, "recenter 1");
15662 #endif
15663 goto recenter;
15664 }
15665
15666 /* Try scrolling with try_window_id. Value is > 0 if update has
15667 been done, it is -1 if we know that the same window start will
15668 not work. It is 0 if unsuccessful for some other reason. */
15669 else if ((tem = try_window_id (w)) != 0)
15670 {
15671 #ifdef GLYPH_DEBUG
15672 debug_method_add (w, "try_window_id %d", tem);
15673 #endif
15674
15675 if (fonts_changed_p)
15676 goto need_larger_matrices;
15677 if (tem > 0)
15678 goto done;
15679
15680 /* Otherwise try_window_id has returned -1 which means that we
15681 don't want the alternative below this comment to execute. */
15682 }
15683 else if (CHARPOS (startp) >= BEGV
15684 && CHARPOS (startp) <= ZV
15685 && PT >= CHARPOS (startp)
15686 && (CHARPOS (startp) < ZV
15687 /* Avoid starting at end of buffer. */
15688 || CHARPOS (startp) == BEGV
15689 || (w->last_modified >= MODIFF
15690 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15691 {
15692 int d1, d2, d3, d4, d5, d6;
15693
15694 /* If first window line is a continuation line, and window start
15695 is inside the modified region, but the first change is before
15696 current window start, we must select a new window start.
15697
15698 However, if this is the result of a down-mouse event (e.g. by
15699 extending the mouse-drag-overlay), we don't want to select a
15700 new window start, since that would change the position under
15701 the mouse, resulting in an unwanted mouse-movement rather
15702 than a simple mouse-click. */
15703 if (!w->start_at_line_beg
15704 && NILP (do_mouse_tracking)
15705 && CHARPOS (startp) > BEGV
15706 && CHARPOS (startp) > BEG + beg_unchanged
15707 && CHARPOS (startp) <= Z - end_unchanged
15708 /* Even if w->start_at_line_beg is nil, a new window may
15709 start at a line_beg, since that's how set_buffer_window
15710 sets it. So, we need to check the return value of
15711 compute_window_start_on_continuation_line. (See also
15712 bug#197). */
15713 && XMARKER (w->start)->buffer == current_buffer
15714 && compute_window_start_on_continuation_line (w)
15715 /* It doesn't make sense to force the window start like we
15716 do at label force_start if it is already known that point
15717 will not be visible in the resulting window, because
15718 doing so will move point from its correct position
15719 instead of scrolling the window to bring point into view.
15720 See bug#9324. */
15721 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15722 {
15723 w->force_start = 1;
15724 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15725 goto force_start;
15726 }
15727
15728 #ifdef GLYPH_DEBUG
15729 debug_method_add (w, "same window start");
15730 #endif
15731
15732 /* Try to redisplay starting at same place as before.
15733 If point has not moved off frame, accept the results. */
15734 if (!current_matrix_up_to_date_p
15735 /* Don't use try_window_reusing_current_matrix in this case
15736 because a window scroll function can have changed the
15737 buffer. */
15738 || !NILP (Vwindow_scroll_functions)
15739 || MINI_WINDOW_P (w)
15740 || !(used_current_matrix_p
15741 = try_window_reusing_current_matrix (w)))
15742 {
15743 IF_DEBUG (debug_method_add (w, "1"));
15744 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15745 /* -1 means we need to scroll.
15746 0 means we need new matrices, but fonts_changed_p
15747 is set in that case, so we will detect it below. */
15748 goto try_to_scroll;
15749 }
15750
15751 if (fonts_changed_p)
15752 goto need_larger_matrices;
15753
15754 if (w->cursor.vpos >= 0)
15755 {
15756 if (!just_this_one_p
15757 || current_buffer->clip_changed
15758 || BEG_UNCHANGED < CHARPOS (startp))
15759 /* Forget any recorded base line for line number display. */
15760 w->base_line_number = Qnil;
15761
15762 if (!cursor_row_fully_visible_p (w, 1, 0))
15763 {
15764 clear_glyph_matrix (w->desired_matrix);
15765 last_line_misfit = 1;
15766 }
15767 /* Drop through and scroll. */
15768 else
15769 goto done;
15770 }
15771 else
15772 clear_glyph_matrix (w->desired_matrix);
15773 }
15774
15775 try_to_scroll:
15776
15777 w->last_modified = 0;
15778 w->last_overlay_modified = 0;
15779
15780 /* Redisplay the mode line. Select the buffer properly for that. */
15781 if (!update_mode_line)
15782 {
15783 update_mode_line = 1;
15784 w->update_mode_line = 1;
15785 }
15786
15787 /* Try to scroll by specified few lines. */
15788 if ((scroll_conservatively
15789 || emacs_scroll_step
15790 || temp_scroll_step
15791 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15792 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15793 && CHARPOS (startp) >= BEGV
15794 && CHARPOS (startp) <= ZV)
15795 {
15796 /* The function returns -1 if new fonts were loaded, 1 if
15797 successful, 0 if not successful. */
15798 int ss = try_scrolling (window, just_this_one_p,
15799 scroll_conservatively,
15800 emacs_scroll_step,
15801 temp_scroll_step, last_line_misfit);
15802 switch (ss)
15803 {
15804 case SCROLLING_SUCCESS:
15805 goto done;
15806
15807 case SCROLLING_NEED_LARGER_MATRICES:
15808 goto need_larger_matrices;
15809
15810 case SCROLLING_FAILED:
15811 break;
15812
15813 default:
15814 abort ();
15815 }
15816 }
15817
15818 /* Finally, just choose a place to start which positions point
15819 according to user preferences. */
15820
15821 recenter:
15822
15823 #ifdef GLYPH_DEBUG
15824 debug_method_add (w, "recenter");
15825 #endif
15826
15827 /* w->vscroll = 0; */
15828
15829 /* Forget any previously recorded base line for line number display. */
15830 if (!buffer_unchanged_p)
15831 w->base_line_number = Qnil;
15832
15833 /* Determine the window start relative to point. */
15834 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15835 it.current_y = it.last_visible_y;
15836 if (centering_position < 0)
15837 {
15838 int margin =
15839 scroll_margin > 0
15840 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15841 : 0;
15842 ptrdiff_t margin_pos = CHARPOS (startp);
15843 Lisp_Object aggressive;
15844 int scrolling_up;
15845
15846 /* If there is a scroll margin at the top of the window, find
15847 its character position. */
15848 if (margin
15849 /* Cannot call start_display if startp is not in the
15850 accessible region of the buffer. This can happen when we
15851 have just switched to a different buffer and/or changed
15852 its restriction. In that case, startp is initialized to
15853 the character position 1 (BEGV) because we did not yet
15854 have chance to display the buffer even once. */
15855 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15856 {
15857 struct it it1;
15858 void *it1data = NULL;
15859
15860 SAVE_IT (it1, it, it1data);
15861 start_display (&it1, w, startp);
15862 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15863 margin_pos = IT_CHARPOS (it1);
15864 RESTORE_IT (&it, &it, it1data);
15865 }
15866 scrolling_up = PT > margin_pos;
15867 aggressive =
15868 scrolling_up
15869 ? BVAR (current_buffer, scroll_up_aggressively)
15870 : BVAR (current_buffer, scroll_down_aggressively);
15871
15872 if (!MINI_WINDOW_P (w)
15873 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15874 {
15875 int pt_offset = 0;
15876
15877 /* Setting scroll-conservatively overrides
15878 scroll-*-aggressively. */
15879 if (!scroll_conservatively && NUMBERP (aggressive))
15880 {
15881 double float_amount = XFLOATINT (aggressive);
15882
15883 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15884 if (pt_offset == 0 && float_amount > 0)
15885 pt_offset = 1;
15886 if (pt_offset && margin > 0)
15887 margin -= 1;
15888 }
15889 /* Compute how much to move the window start backward from
15890 point so that point will be displayed where the user
15891 wants it. */
15892 if (scrolling_up)
15893 {
15894 centering_position = it.last_visible_y;
15895 if (pt_offset)
15896 centering_position -= pt_offset;
15897 centering_position -=
15898 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15899 + WINDOW_HEADER_LINE_HEIGHT (w);
15900 /* Don't let point enter the scroll margin near top of
15901 the window. */
15902 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15903 centering_position = margin * FRAME_LINE_HEIGHT (f);
15904 }
15905 else
15906 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15907 }
15908 else
15909 /* Set the window start half the height of the window backward
15910 from point. */
15911 centering_position = window_box_height (w) / 2;
15912 }
15913 move_it_vertically_backward (&it, centering_position);
15914
15915 eassert (IT_CHARPOS (it) >= BEGV);
15916
15917 /* The function move_it_vertically_backward may move over more
15918 than the specified y-distance. If it->w is small, e.g. a
15919 mini-buffer window, we may end up in front of the window's
15920 display area. Start displaying at the start of the line
15921 containing PT in this case. */
15922 if (it.current_y <= 0)
15923 {
15924 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15925 move_it_vertically_backward (&it, 0);
15926 it.current_y = 0;
15927 }
15928
15929 it.current_x = it.hpos = 0;
15930
15931 /* Set the window start position here explicitly, to avoid an
15932 infinite loop in case the functions in window-scroll-functions
15933 get errors. */
15934 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15935
15936 /* Run scroll hooks. */
15937 startp = run_window_scroll_functions (window, it.current.pos);
15938
15939 /* Redisplay the window. */
15940 if (!current_matrix_up_to_date_p
15941 || windows_or_buffers_changed
15942 || cursor_type_changed
15943 /* Don't use try_window_reusing_current_matrix in this case
15944 because it can have changed the buffer. */
15945 || !NILP (Vwindow_scroll_functions)
15946 || !just_this_one_p
15947 || MINI_WINDOW_P (w)
15948 || !(used_current_matrix_p
15949 = try_window_reusing_current_matrix (w)))
15950 try_window (window, startp, 0);
15951
15952 /* If new fonts have been loaded (due to fontsets), give up. We
15953 have to start a new redisplay since we need to re-adjust glyph
15954 matrices. */
15955 if (fonts_changed_p)
15956 goto need_larger_matrices;
15957
15958 /* If cursor did not appear assume that the middle of the window is
15959 in the first line of the window. Do it again with the next line.
15960 (Imagine a window of height 100, displaying two lines of height
15961 60. Moving back 50 from it->last_visible_y will end in the first
15962 line.) */
15963 if (w->cursor.vpos < 0)
15964 {
15965 if (!NILP (w->window_end_valid)
15966 && PT >= Z - XFASTINT (w->window_end_pos))
15967 {
15968 clear_glyph_matrix (w->desired_matrix);
15969 move_it_by_lines (&it, 1);
15970 try_window (window, it.current.pos, 0);
15971 }
15972 else if (PT < IT_CHARPOS (it))
15973 {
15974 clear_glyph_matrix (w->desired_matrix);
15975 move_it_by_lines (&it, -1);
15976 try_window (window, it.current.pos, 0);
15977 }
15978 else
15979 {
15980 /* Not much we can do about it. */
15981 }
15982 }
15983
15984 /* Consider the following case: Window starts at BEGV, there is
15985 invisible, intangible text at BEGV, so that display starts at
15986 some point START > BEGV. It can happen that we are called with
15987 PT somewhere between BEGV and START. Try to handle that case. */
15988 if (w->cursor.vpos < 0)
15989 {
15990 struct glyph_row *row = w->current_matrix->rows;
15991 if (row->mode_line_p)
15992 ++row;
15993 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15994 }
15995
15996 if (!cursor_row_fully_visible_p (w, 0, 0))
15997 {
15998 /* If vscroll is enabled, disable it and try again. */
15999 if (w->vscroll)
16000 {
16001 w->vscroll = 0;
16002 clear_glyph_matrix (w->desired_matrix);
16003 goto recenter;
16004 }
16005
16006 /* Users who set scroll-conservatively to a large number want
16007 point just above/below the scroll margin. If we ended up
16008 with point's row partially visible, move the window start to
16009 make that row fully visible and out of the margin. */
16010 if (scroll_conservatively > SCROLL_LIMIT)
16011 {
16012 int margin =
16013 scroll_margin > 0
16014 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16015 : 0;
16016 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16017
16018 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16019 clear_glyph_matrix (w->desired_matrix);
16020 if (1 == try_window (window, it.current.pos,
16021 TRY_WINDOW_CHECK_MARGINS))
16022 goto done;
16023 }
16024
16025 /* If centering point failed to make the whole line visible,
16026 put point at the top instead. That has to make the whole line
16027 visible, if it can be done. */
16028 if (centering_position == 0)
16029 goto done;
16030
16031 clear_glyph_matrix (w->desired_matrix);
16032 centering_position = 0;
16033 goto recenter;
16034 }
16035
16036 done:
16037
16038 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16039 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16040 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16041
16042 /* Display the mode line, if we must. */
16043 if ((update_mode_line
16044 /* If window not full width, must redo its mode line
16045 if (a) the window to its side is being redone and
16046 (b) we do a frame-based redisplay. This is a consequence
16047 of how inverted lines are drawn in frame-based redisplay. */
16048 || (!just_this_one_p
16049 && !FRAME_WINDOW_P (f)
16050 && !WINDOW_FULL_WIDTH_P (w))
16051 /* Line number to display. */
16052 || INTEGERP (w->base_line_pos)
16053 /* Column number is displayed and different from the one displayed. */
16054 || (!NILP (w->column_number_displayed)
16055 && (XFASTINT (w->column_number_displayed) != current_column ())))
16056 /* This means that the window has a mode line. */
16057 && (WINDOW_WANTS_MODELINE_P (w)
16058 || WINDOW_WANTS_HEADER_LINE_P (w)))
16059 {
16060 display_mode_lines (w);
16061
16062 /* If mode line height has changed, arrange for a thorough
16063 immediate redisplay using the correct mode line height. */
16064 if (WINDOW_WANTS_MODELINE_P (w)
16065 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16066 {
16067 fonts_changed_p = 1;
16068 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16069 = DESIRED_MODE_LINE_HEIGHT (w);
16070 }
16071
16072 /* If header line height has changed, arrange for a thorough
16073 immediate redisplay using the correct header line height. */
16074 if (WINDOW_WANTS_HEADER_LINE_P (w)
16075 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16076 {
16077 fonts_changed_p = 1;
16078 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16079 = DESIRED_HEADER_LINE_HEIGHT (w);
16080 }
16081
16082 if (fonts_changed_p)
16083 goto need_larger_matrices;
16084 }
16085
16086 if (!line_number_displayed
16087 && !BUFFERP (w->base_line_pos))
16088 {
16089 w->base_line_pos = Qnil;
16090 w->base_line_number = Qnil;
16091 }
16092
16093 finish_menu_bars:
16094
16095 /* When we reach a frame's selected window, redo the frame's menu bar. */
16096 if (update_mode_line
16097 && EQ (FRAME_SELECTED_WINDOW (f), window))
16098 {
16099 int redisplay_menu_p = 0;
16100
16101 if (FRAME_WINDOW_P (f))
16102 {
16103 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16104 || defined (HAVE_NS) || defined (USE_GTK)
16105 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16106 #else
16107 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16108 #endif
16109 }
16110 else
16111 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16112
16113 if (redisplay_menu_p)
16114 display_menu_bar (w);
16115
16116 #ifdef HAVE_WINDOW_SYSTEM
16117 if (FRAME_WINDOW_P (f))
16118 {
16119 #if defined (USE_GTK) || defined (HAVE_NS)
16120 if (FRAME_EXTERNAL_TOOL_BAR (f))
16121 redisplay_tool_bar (f);
16122 #else
16123 if (WINDOWP (f->tool_bar_window)
16124 && (FRAME_TOOL_BAR_LINES (f) > 0
16125 || !NILP (Vauto_resize_tool_bars))
16126 && redisplay_tool_bar (f))
16127 ignore_mouse_drag_p = 1;
16128 #endif
16129 }
16130 #endif
16131 }
16132
16133 #ifdef HAVE_WINDOW_SYSTEM
16134 if (FRAME_WINDOW_P (f)
16135 && update_window_fringes (w, (just_this_one_p
16136 || (!used_current_matrix_p && !overlay_arrow_seen)
16137 || w->pseudo_window_p)))
16138 {
16139 update_begin (f);
16140 BLOCK_INPUT;
16141 if (draw_window_fringes (w, 1))
16142 x_draw_vertical_border (w);
16143 UNBLOCK_INPUT;
16144 update_end (f);
16145 }
16146 #endif /* HAVE_WINDOW_SYSTEM */
16147
16148 /* We go to this label, with fonts_changed_p nonzero,
16149 if it is necessary to try again using larger glyph matrices.
16150 We have to redeem the scroll bar even in this case,
16151 because the loop in redisplay_internal expects that. */
16152 need_larger_matrices:
16153 ;
16154 finish_scroll_bars:
16155
16156 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16157 {
16158 /* Set the thumb's position and size. */
16159 set_vertical_scroll_bar (w);
16160
16161 /* Note that we actually used the scroll bar attached to this
16162 window, so it shouldn't be deleted at the end of redisplay. */
16163 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16164 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16165 }
16166
16167 /* Restore current_buffer and value of point in it. The window
16168 update may have changed the buffer, so first make sure `opoint'
16169 is still valid (Bug#6177). */
16170 if (CHARPOS (opoint) < BEGV)
16171 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16172 else if (CHARPOS (opoint) > ZV)
16173 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16174 else
16175 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16176
16177 set_buffer_internal_1 (old);
16178 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16179 shorter. This can be caused by log truncation in *Messages*. */
16180 if (CHARPOS (lpoint) <= ZV)
16181 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16182
16183 unbind_to (count, Qnil);
16184 }
16185
16186
16187 /* Build the complete desired matrix of WINDOW with a window start
16188 buffer position POS.
16189
16190 Value is 1 if successful. It is zero if fonts were loaded during
16191 redisplay which makes re-adjusting glyph matrices necessary, and -1
16192 if point would appear in the scroll margins.
16193 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16194 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16195 set in FLAGS.) */
16196
16197 int
16198 try_window (Lisp_Object window, struct text_pos pos, int flags)
16199 {
16200 struct window *w = XWINDOW (window);
16201 struct it it;
16202 struct glyph_row *last_text_row = NULL;
16203 struct frame *f = XFRAME (w->frame);
16204
16205 /* Make POS the new window start. */
16206 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16207
16208 /* Mark cursor position as unknown. No overlay arrow seen. */
16209 w->cursor.vpos = -1;
16210 overlay_arrow_seen = 0;
16211
16212 /* Initialize iterator and info to start at POS. */
16213 start_display (&it, w, pos);
16214
16215 /* Display all lines of W. */
16216 while (it.current_y < it.last_visible_y)
16217 {
16218 if (display_line (&it))
16219 last_text_row = it.glyph_row - 1;
16220 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16221 return 0;
16222 }
16223
16224 /* Don't let the cursor end in the scroll margins. */
16225 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16226 && !MINI_WINDOW_P (w))
16227 {
16228 int this_scroll_margin;
16229
16230 if (scroll_margin > 0)
16231 {
16232 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16233 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16234 }
16235 else
16236 this_scroll_margin = 0;
16237
16238 if ((w->cursor.y >= 0 /* not vscrolled */
16239 && w->cursor.y < this_scroll_margin
16240 && CHARPOS (pos) > BEGV
16241 && IT_CHARPOS (it) < ZV)
16242 /* rms: considering make_cursor_line_fully_visible_p here
16243 seems to give wrong results. We don't want to recenter
16244 when the last line is partly visible, we want to allow
16245 that case to be handled in the usual way. */
16246 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16247 {
16248 w->cursor.vpos = -1;
16249 clear_glyph_matrix (w->desired_matrix);
16250 return -1;
16251 }
16252 }
16253
16254 /* If bottom moved off end of frame, change mode line percentage. */
16255 if (XFASTINT (w->window_end_pos) <= 0
16256 && Z != IT_CHARPOS (it))
16257 w->update_mode_line = 1;
16258
16259 /* Set window_end_pos to the offset of the last character displayed
16260 on the window from the end of current_buffer. Set
16261 window_end_vpos to its row number. */
16262 if (last_text_row)
16263 {
16264 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16265 w->window_end_bytepos
16266 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16267 w->window_end_pos
16268 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16269 w->window_end_vpos
16270 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16271 eassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16272 ->displays_text_p);
16273 }
16274 else
16275 {
16276 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16277 w->window_end_pos = make_number (Z - ZV);
16278 w->window_end_vpos = make_number (0);
16279 }
16280
16281 /* But that is not valid info until redisplay finishes. */
16282 w->window_end_valid = Qnil;
16283 return 1;
16284 }
16285
16286
16287 \f
16288 /************************************************************************
16289 Window redisplay reusing current matrix when buffer has not changed
16290 ************************************************************************/
16291
16292 /* Try redisplay of window W showing an unchanged buffer with a
16293 different window start than the last time it was displayed by
16294 reusing its current matrix. Value is non-zero if successful.
16295 W->start is the new window start. */
16296
16297 static int
16298 try_window_reusing_current_matrix (struct window *w)
16299 {
16300 struct frame *f = XFRAME (w->frame);
16301 struct glyph_row *bottom_row;
16302 struct it it;
16303 struct run run;
16304 struct text_pos start, new_start;
16305 int nrows_scrolled, i;
16306 struct glyph_row *last_text_row;
16307 struct glyph_row *last_reused_text_row;
16308 struct glyph_row *start_row;
16309 int start_vpos, min_y, max_y;
16310
16311 #ifdef GLYPH_DEBUG
16312 if (inhibit_try_window_reusing)
16313 return 0;
16314 #endif
16315
16316 if (/* This function doesn't handle terminal frames. */
16317 !FRAME_WINDOW_P (f)
16318 /* Don't try to reuse the display if windows have been split
16319 or such. */
16320 || windows_or_buffers_changed
16321 || cursor_type_changed)
16322 return 0;
16323
16324 /* Can't do this if region may have changed. */
16325 if ((!NILP (Vtransient_mark_mode)
16326 && !NILP (BVAR (current_buffer, mark_active)))
16327 || !NILP (w->region_showing)
16328 || !NILP (Vshow_trailing_whitespace))
16329 return 0;
16330
16331 /* If top-line visibility has changed, give up. */
16332 if (WINDOW_WANTS_HEADER_LINE_P (w)
16333 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16334 return 0;
16335
16336 /* Give up if old or new display is scrolled vertically. We could
16337 make this function handle this, but right now it doesn't. */
16338 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16339 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16340 return 0;
16341
16342 /* The variable new_start now holds the new window start. The old
16343 start `start' can be determined from the current matrix. */
16344 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16345 start = start_row->minpos;
16346 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16347
16348 /* Clear the desired matrix for the display below. */
16349 clear_glyph_matrix (w->desired_matrix);
16350
16351 if (CHARPOS (new_start) <= CHARPOS (start))
16352 {
16353 /* Don't use this method if the display starts with an ellipsis
16354 displayed for invisible text. It's not easy to handle that case
16355 below, and it's certainly not worth the effort since this is
16356 not a frequent case. */
16357 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16358 return 0;
16359
16360 IF_DEBUG (debug_method_add (w, "twu1"));
16361
16362 /* Display up to a row that can be reused. The variable
16363 last_text_row is set to the last row displayed that displays
16364 text. Note that it.vpos == 0 if or if not there is a
16365 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16366 start_display (&it, w, new_start);
16367 w->cursor.vpos = -1;
16368 last_text_row = last_reused_text_row = NULL;
16369
16370 while (it.current_y < it.last_visible_y
16371 && !fonts_changed_p)
16372 {
16373 /* If we have reached into the characters in the START row,
16374 that means the line boundaries have changed. So we
16375 can't start copying with the row START. Maybe it will
16376 work to start copying with the following row. */
16377 while (IT_CHARPOS (it) > CHARPOS (start))
16378 {
16379 /* Advance to the next row as the "start". */
16380 start_row++;
16381 start = start_row->minpos;
16382 /* If there are no more rows to try, or just one, give up. */
16383 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16384 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16385 || CHARPOS (start) == ZV)
16386 {
16387 clear_glyph_matrix (w->desired_matrix);
16388 return 0;
16389 }
16390
16391 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16392 }
16393 /* If we have reached alignment, we can copy the rest of the
16394 rows. */
16395 if (IT_CHARPOS (it) == CHARPOS (start)
16396 /* Don't accept "alignment" inside a display vector,
16397 since start_row could have started in the middle of
16398 that same display vector (thus their character
16399 positions match), and we have no way of telling if
16400 that is the case. */
16401 && it.current.dpvec_index < 0)
16402 break;
16403
16404 if (display_line (&it))
16405 last_text_row = it.glyph_row - 1;
16406
16407 }
16408
16409 /* A value of current_y < last_visible_y means that we stopped
16410 at the previous window start, which in turn means that we
16411 have at least one reusable row. */
16412 if (it.current_y < it.last_visible_y)
16413 {
16414 struct glyph_row *row;
16415
16416 /* IT.vpos always starts from 0; it counts text lines. */
16417 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16418
16419 /* Find PT if not already found in the lines displayed. */
16420 if (w->cursor.vpos < 0)
16421 {
16422 int dy = it.current_y - start_row->y;
16423
16424 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16425 row = row_containing_pos (w, PT, row, NULL, dy);
16426 if (row)
16427 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16428 dy, nrows_scrolled);
16429 else
16430 {
16431 clear_glyph_matrix (w->desired_matrix);
16432 return 0;
16433 }
16434 }
16435
16436 /* Scroll the display. Do it before the current matrix is
16437 changed. The problem here is that update has not yet
16438 run, i.e. part of the current matrix is not up to date.
16439 scroll_run_hook will clear the cursor, and use the
16440 current matrix to get the height of the row the cursor is
16441 in. */
16442 run.current_y = start_row->y;
16443 run.desired_y = it.current_y;
16444 run.height = it.last_visible_y - it.current_y;
16445
16446 if (run.height > 0 && run.current_y != run.desired_y)
16447 {
16448 update_begin (f);
16449 FRAME_RIF (f)->update_window_begin_hook (w);
16450 FRAME_RIF (f)->clear_window_mouse_face (w);
16451 FRAME_RIF (f)->scroll_run_hook (w, &run);
16452 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16453 update_end (f);
16454 }
16455
16456 /* Shift current matrix down by nrows_scrolled lines. */
16457 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16458 rotate_matrix (w->current_matrix,
16459 start_vpos,
16460 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16461 nrows_scrolled);
16462
16463 /* Disable lines that must be updated. */
16464 for (i = 0; i < nrows_scrolled; ++i)
16465 (start_row + i)->enabled_p = 0;
16466
16467 /* Re-compute Y positions. */
16468 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16469 max_y = it.last_visible_y;
16470 for (row = start_row + nrows_scrolled;
16471 row < bottom_row;
16472 ++row)
16473 {
16474 row->y = it.current_y;
16475 row->visible_height = row->height;
16476
16477 if (row->y < min_y)
16478 row->visible_height -= min_y - row->y;
16479 if (row->y + row->height > max_y)
16480 row->visible_height -= row->y + row->height - max_y;
16481 if (row->fringe_bitmap_periodic_p)
16482 row->redraw_fringe_bitmaps_p = 1;
16483
16484 it.current_y += row->height;
16485
16486 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16487 last_reused_text_row = row;
16488 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16489 break;
16490 }
16491
16492 /* Disable lines in the current matrix which are now
16493 below the window. */
16494 for (++row; row < bottom_row; ++row)
16495 row->enabled_p = row->mode_line_p = 0;
16496 }
16497
16498 /* Update window_end_pos etc.; last_reused_text_row is the last
16499 reused row from the current matrix containing text, if any.
16500 The value of last_text_row is the last displayed line
16501 containing text. */
16502 if (last_reused_text_row)
16503 {
16504 w->window_end_bytepos
16505 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16506 w->window_end_pos
16507 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16508 w->window_end_vpos
16509 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16510 w->current_matrix));
16511 }
16512 else if (last_text_row)
16513 {
16514 w->window_end_bytepos
16515 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16516 w->window_end_pos
16517 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16518 w->window_end_vpos
16519 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16520 }
16521 else
16522 {
16523 /* This window must be completely empty. */
16524 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16525 w->window_end_pos = make_number (Z - ZV);
16526 w->window_end_vpos = make_number (0);
16527 }
16528 w->window_end_valid = Qnil;
16529
16530 /* Update hint: don't try scrolling again in update_window. */
16531 w->desired_matrix->no_scrolling_p = 1;
16532
16533 #ifdef GLYPH_DEBUG
16534 debug_method_add (w, "try_window_reusing_current_matrix 1");
16535 #endif
16536 return 1;
16537 }
16538 else if (CHARPOS (new_start) > CHARPOS (start))
16539 {
16540 struct glyph_row *pt_row, *row;
16541 struct glyph_row *first_reusable_row;
16542 struct glyph_row *first_row_to_display;
16543 int dy;
16544 int yb = window_text_bottom_y (w);
16545
16546 /* Find the row starting at new_start, if there is one. Don't
16547 reuse a partially visible line at the end. */
16548 first_reusable_row = start_row;
16549 while (first_reusable_row->enabled_p
16550 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16551 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16552 < CHARPOS (new_start)))
16553 ++first_reusable_row;
16554
16555 /* Give up if there is no row to reuse. */
16556 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16557 || !first_reusable_row->enabled_p
16558 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16559 != CHARPOS (new_start)))
16560 return 0;
16561
16562 /* We can reuse fully visible rows beginning with
16563 first_reusable_row to the end of the window. Set
16564 first_row_to_display to the first row that cannot be reused.
16565 Set pt_row to the row containing point, if there is any. */
16566 pt_row = NULL;
16567 for (first_row_to_display = first_reusable_row;
16568 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16569 ++first_row_to_display)
16570 {
16571 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16572 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16573 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16574 && first_row_to_display->ends_at_zv_p
16575 && pt_row == NULL)))
16576 pt_row = first_row_to_display;
16577 }
16578
16579 /* Start displaying at the start of first_row_to_display. */
16580 eassert (first_row_to_display->y < yb);
16581 init_to_row_start (&it, w, first_row_to_display);
16582
16583 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16584 - start_vpos);
16585 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16586 - nrows_scrolled);
16587 it.current_y = (first_row_to_display->y - first_reusable_row->y
16588 + WINDOW_HEADER_LINE_HEIGHT (w));
16589
16590 /* Display lines beginning with first_row_to_display in the
16591 desired matrix. Set last_text_row to the last row displayed
16592 that displays text. */
16593 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16594 if (pt_row == NULL)
16595 w->cursor.vpos = -1;
16596 last_text_row = NULL;
16597 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16598 if (display_line (&it))
16599 last_text_row = it.glyph_row - 1;
16600
16601 /* If point is in a reused row, adjust y and vpos of the cursor
16602 position. */
16603 if (pt_row)
16604 {
16605 w->cursor.vpos -= nrows_scrolled;
16606 w->cursor.y -= first_reusable_row->y - start_row->y;
16607 }
16608
16609 /* Give up if point isn't in a row displayed or reused. (This
16610 also handles the case where w->cursor.vpos < nrows_scrolled
16611 after the calls to display_line, which can happen with scroll
16612 margins. See bug#1295.) */
16613 if (w->cursor.vpos < 0)
16614 {
16615 clear_glyph_matrix (w->desired_matrix);
16616 return 0;
16617 }
16618
16619 /* Scroll the display. */
16620 run.current_y = first_reusable_row->y;
16621 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16622 run.height = it.last_visible_y - run.current_y;
16623 dy = run.current_y - run.desired_y;
16624
16625 if (run.height)
16626 {
16627 update_begin (f);
16628 FRAME_RIF (f)->update_window_begin_hook (w);
16629 FRAME_RIF (f)->clear_window_mouse_face (w);
16630 FRAME_RIF (f)->scroll_run_hook (w, &run);
16631 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16632 update_end (f);
16633 }
16634
16635 /* Adjust Y positions of reused rows. */
16636 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16637 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16638 max_y = it.last_visible_y;
16639 for (row = first_reusable_row; row < first_row_to_display; ++row)
16640 {
16641 row->y -= dy;
16642 row->visible_height = row->height;
16643 if (row->y < min_y)
16644 row->visible_height -= min_y - row->y;
16645 if (row->y + row->height > max_y)
16646 row->visible_height -= row->y + row->height - max_y;
16647 if (row->fringe_bitmap_periodic_p)
16648 row->redraw_fringe_bitmaps_p = 1;
16649 }
16650
16651 /* Scroll the current matrix. */
16652 eassert (nrows_scrolled > 0);
16653 rotate_matrix (w->current_matrix,
16654 start_vpos,
16655 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16656 -nrows_scrolled);
16657
16658 /* Disable rows not reused. */
16659 for (row -= nrows_scrolled; row < bottom_row; ++row)
16660 row->enabled_p = 0;
16661
16662 /* Point may have moved to a different line, so we cannot assume that
16663 the previous cursor position is valid; locate the correct row. */
16664 if (pt_row)
16665 {
16666 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16667 row < bottom_row
16668 && PT >= MATRIX_ROW_END_CHARPOS (row)
16669 && !row->ends_at_zv_p;
16670 row++)
16671 {
16672 w->cursor.vpos++;
16673 w->cursor.y = row->y;
16674 }
16675 if (row < bottom_row)
16676 {
16677 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16678 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16679
16680 /* Can't use this optimization with bidi-reordered glyph
16681 rows, unless cursor is already at point. */
16682 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16683 {
16684 if (!(w->cursor.hpos >= 0
16685 && w->cursor.hpos < row->used[TEXT_AREA]
16686 && BUFFERP (glyph->object)
16687 && glyph->charpos == PT))
16688 return 0;
16689 }
16690 else
16691 for (; glyph < end
16692 && (!BUFFERP (glyph->object)
16693 || glyph->charpos < PT);
16694 glyph++)
16695 {
16696 w->cursor.hpos++;
16697 w->cursor.x += glyph->pixel_width;
16698 }
16699 }
16700 }
16701
16702 /* Adjust window end. A null value of last_text_row means that
16703 the window end is in reused rows which in turn means that
16704 only its vpos can have changed. */
16705 if (last_text_row)
16706 {
16707 w->window_end_bytepos
16708 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16709 w->window_end_pos
16710 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16711 w->window_end_vpos
16712 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16713 }
16714 else
16715 {
16716 w->window_end_vpos
16717 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16718 }
16719
16720 w->window_end_valid = Qnil;
16721 w->desired_matrix->no_scrolling_p = 1;
16722
16723 #ifdef GLYPH_DEBUG
16724 debug_method_add (w, "try_window_reusing_current_matrix 2");
16725 #endif
16726 return 1;
16727 }
16728
16729 return 0;
16730 }
16731
16732
16733 \f
16734 /************************************************************************
16735 Window redisplay reusing current matrix when buffer has changed
16736 ************************************************************************/
16737
16738 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16739 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16740 ptrdiff_t *, ptrdiff_t *);
16741 static struct glyph_row *
16742 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16743 struct glyph_row *);
16744
16745
16746 /* Return the last row in MATRIX displaying text. If row START is
16747 non-null, start searching with that row. IT gives the dimensions
16748 of the display. Value is null if matrix is empty; otherwise it is
16749 a pointer to the row found. */
16750
16751 static struct glyph_row *
16752 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16753 struct glyph_row *start)
16754 {
16755 struct glyph_row *row, *row_found;
16756
16757 /* Set row_found to the last row in IT->w's current matrix
16758 displaying text. The loop looks funny but think of partially
16759 visible lines. */
16760 row_found = NULL;
16761 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16762 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16763 {
16764 eassert (row->enabled_p);
16765 row_found = row;
16766 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16767 break;
16768 ++row;
16769 }
16770
16771 return row_found;
16772 }
16773
16774
16775 /* Return the last row in the current matrix of W that is not affected
16776 by changes at the start of current_buffer that occurred since W's
16777 current matrix was built. Value is null if no such row exists.
16778
16779 BEG_UNCHANGED us the number of characters unchanged at the start of
16780 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16781 first changed character in current_buffer. Characters at positions <
16782 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16783 when the current matrix was built. */
16784
16785 static struct glyph_row *
16786 find_last_unchanged_at_beg_row (struct window *w)
16787 {
16788 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16789 struct glyph_row *row;
16790 struct glyph_row *row_found = NULL;
16791 int yb = window_text_bottom_y (w);
16792
16793 /* Find the last row displaying unchanged text. */
16794 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16795 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16796 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16797 ++row)
16798 {
16799 if (/* If row ends before first_changed_pos, it is unchanged,
16800 except in some case. */
16801 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16802 /* When row ends in ZV and we write at ZV it is not
16803 unchanged. */
16804 && !row->ends_at_zv_p
16805 /* When first_changed_pos is the end of a continued line,
16806 row is not unchanged because it may be no longer
16807 continued. */
16808 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16809 && (row->continued_p
16810 || row->exact_window_width_line_p))
16811 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16812 needs to be recomputed, so don't consider this row as
16813 unchanged. This happens when the last line was
16814 bidi-reordered and was killed immediately before this
16815 redisplay cycle. In that case, ROW->end stores the
16816 buffer position of the first visual-order character of
16817 the killed text, which is now beyond ZV. */
16818 && CHARPOS (row->end.pos) <= ZV)
16819 row_found = row;
16820
16821 /* Stop if last visible row. */
16822 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16823 break;
16824 }
16825
16826 return row_found;
16827 }
16828
16829
16830 /* Find the first glyph row in the current matrix of W that is not
16831 affected by changes at the end of current_buffer since the
16832 time W's current matrix was built.
16833
16834 Return in *DELTA the number of chars by which buffer positions in
16835 unchanged text at the end of current_buffer must be adjusted.
16836
16837 Return in *DELTA_BYTES the corresponding number of bytes.
16838
16839 Value is null if no such row exists, i.e. all rows are affected by
16840 changes. */
16841
16842 static struct glyph_row *
16843 find_first_unchanged_at_end_row (struct window *w,
16844 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16845 {
16846 struct glyph_row *row;
16847 struct glyph_row *row_found = NULL;
16848
16849 *delta = *delta_bytes = 0;
16850
16851 /* Display must not have been paused, otherwise the current matrix
16852 is not up to date. */
16853 eassert (!NILP (w->window_end_valid));
16854
16855 /* A value of window_end_pos >= END_UNCHANGED means that the window
16856 end is in the range of changed text. If so, there is no
16857 unchanged row at the end of W's current matrix. */
16858 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16859 return NULL;
16860
16861 /* Set row to the last row in W's current matrix displaying text. */
16862 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16863
16864 /* If matrix is entirely empty, no unchanged row exists. */
16865 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16866 {
16867 /* The value of row is the last glyph row in the matrix having a
16868 meaningful buffer position in it. The end position of row
16869 corresponds to window_end_pos. This allows us to translate
16870 buffer positions in the current matrix to current buffer
16871 positions for characters not in changed text. */
16872 ptrdiff_t Z_old =
16873 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16874 ptrdiff_t Z_BYTE_old =
16875 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16876 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16877 struct glyph_row *first_text_row
16878 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16879
16880 *delta = Z - Z_old;
16881 *delta_bytes = Z_BYTE - Z_BYTE_old;
16882
16883 /* Set last_unchanged_pos to the buffer position of the last
16884 character in the buffer that has not been changed. Z is the
16885 index + 1 of the last character in current_buffer, i.e. by
16886 subtracting END_UNCHANGED we get the index of the last
16887 unchanged character, and we have to add BEG to get its buffer
16888 position. */
16889 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16890 last_unchanged_pos_old = last_unchanged_pos - *delta;
16891
16892 /* Search backward from ROW for a row displaying a line that
16893 starts at a minimum position >= last_unchanged_pos_old. */
16894 for (; row > first_text_row; --row)
16895 {
16896 /* This used to abort, but it can happen.
16897 It is ok to just stop the search instead here. KFS. */
16898 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16899 break;
16900
16901 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16902 row_found = row;
16903 }
16904 }
16905
16906 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16907
16908 return row_found;
16909 }
16910
16911
16912 /* Make sure that glyph rows in the current matrix of window W
16913 reference the same glyph memory as corresponding rows in the
16914 frame's frame matrix. This function is called after scrolling W's
16915 current matrix on a terminal frame in try_window_id and
16916 try_window_reusing_current_matrix. */
16917
16918 static void
16919 sync_frame_with_window_matrix_rows (struct window *w)
16920 {
16921 struct frame *f = XFRAME (w->frame);
16922 struct glyph_row *window_row, *window_row_end, *frame_row;
16923
16924 /* Preconditions: W must be a leaf window and full-width. Its frame
16925 must have a frame matrix. */
16926 eassert (NILP (w->hchild) && NILP (w->vchild));
16927 eassert (WINDOW_FULL_WIDTH_P (w));
16928 eassert (!FRAME_WINDOW_P (f));
16929
16930 /* If W is a full-width window, glyph pointers in W's current matrix
16931 have, by definition, to be the same as glyph pointers in the
16932 corresponding frame matrix. Note that frame matrices have no
16933 marginal areas (see build_frame_matrix). */
16934 window_row = w->current_matrix->rows;
16935 window_row_end = window_row + w->current_matrix->nrows;
16936 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16937 while (window_row < window_row_end)
16938 {
16939 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16940 struct glyph *end = window_row->glyphs[LAST_AREA];
16941
16942 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16943 frame_row->glyphs[TEXT_AREA] = start;
16944 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16945 frame_row->glyphs[LAST_AREA] = end;
16946
16947 /* Disable frame rows whose corresponding window rows have
16948 been disabled in try_window_id. */
16949 if (!window_row->enabled_p)
16950 frame_row->enabled_p = 0;
16951
16952 ++window_row, ++frame_row;
16953 }
16954 }
16955
16956
16957 /* Find the glyph row in window W containing CHARPOS. Consider all
16958 rows between START and END (not inclusive). END null means search
16959 all rows to the end of the display area of W. Value is the row
16960 containing CHARPOS or null. */
16961
16962 struct glyph_row *
16963 row_containing_pos (struct window *w, ptrdiff_t charpos,
16964 struct glyph_row *start, struct glyph_row *end, int dy)
16965 {
16966 struct glyph_row *row = start;
16967 struct glyph_row *best_row = NULL;
16968 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16969 int last_y;
16970
16971 /* If we happen to start on a header-line, skip that. */
16972 if (row->mode_line_p)
16973 ++row;
16974
16975 if ((end && row >= end) || !row->enabled_p)
16976 return NULL;
16977
16978 last_y = window_text_bottom_y (w) - dy;
16979
16980 while (1)
16981 {
16982 /* Give up if we have gone too far. */
16983 if (end && row >= end)
16984 return NULL;
16985 /* This formerly returned if they were equal.
16986 I think that both quantities are of a "last plus one" type;
16987 if so, when they are equal, the row is within the screen. -- rms. */
16988 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16989 return NULL;
16990
16991 /* If it is in this row, return this row. */
16992 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16993 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16994 /* The end position of a row equals the start
16995 position of the next row. If CHARPOS is there, we
16996 would rather display it in the next line, except
16997 when this line ends in ZV. */
16998 && !row->ends_at_zv_p
16999 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17000 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17001 {
17002 struct glyph *g;
17003
17004 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17005 || (!best_row && !row->continued_p))
17006 return row;
17007 /* In bidi-reordered rows, there could be several rows
17008 occluding point, all of them belonging to the same
17009 continued line. We need to find the row which fits
17010 CHARPOS the best. */
17011 for (g = row->glyphs[TEXT_AREA];
17012 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17013 g++)
17014 {
17015 if (!STRINGP (g->object))
17016 {
17017 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17018 {
17019 mindif = eabs (g->charpos - charpos);
17020 best_row = row;
17021 /* Exact match always wins. */
17022 if (mindif == 0)
17023 return best_row;
17024 }
17025 }
17026 }
17027 }
17028 else if (best_row && !row->continued_p)
17029 return best_row;
17030 ++row;
17031 }
17032 }
17033
17034
17035 /* Try to redisplay window W by reusing its existing display. W's
17036 current matrix must be up to date when this function is called,
17037 i.e. window_end_valid must not be nil.
17038
17039 Value is
17040
17041 1 if display has been updated
17042 0 if otherwise unsuccessful
17043 -1 if redisplay with same window start is known not to succeed
17044
17045 The following steps are performed:
17046
17047 1. Find the last row in the current matrix of W that is not
17048 affected by changes at the start of current_buffer. If no such row
17049 is found, give up.
17050
17051 2. Find the first row in W's current matrix that is not affected by
17052 changes at the end of current_buffer. Maybe there is no such row.
17053
17054 3. Display lines beginning with the row + 1 found in step 1 to the
17055 row found in step 2 or, if step 2 didn't find a row, to the end of
17056 the window.
17057
17058 4. If cursor is not known to appear on the window, give up.
17059
17060 5. If display stopped at the row found in step 2, scroll the
17061 display and current matrix as needed.
17062
17063 6. Maybe display some lines at the end of W, if we must. This can
17064 happen under various circumstances, like a partially visible line
17065 becoming fully visible, or because newly displayed lines are displayed
17066 in smaller font sizes.
17067
17068 7. Update W's window end information. */
17069
17070 static int
17071 try_window_id (struct window *w)
17072 {
17073 struct frame *f = XFRAME (w->frame);
17074 struct glyph_matrix *current_matrix = w->current_matrix;
17075 struct glyph_matrix *desired_matrix = w->desired_matrix;
17076 struct glyph_row *last_unchanged_at_beg_row;
17077 struct glyph_row *first_unchanged_at_end_row;
17078 struct glyph_row *row;
17079 struct glyph_row *bottom_row;
17080 int bottom_vpos;
17081 struct it it;
17082 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17083 int dvpos, dy;
17084 struct text_pos start_pos;
17085 struct run run;
17086 int first_unchanged_at_end_vpos = 0;
17087 struct glyph_row *last_text_row, *last_text_row_at_end;
17088 struct text_pos start;
17089 ptrdiff_t first_changed_charpos, last_changed_charpos;
17090
17091 #ifdef GLYPH_DEBUG
17092 if (inhibit_try_window_id)
17093 return 0;
17094 #endif
17095
17096 /* This is handy for debugging. */
17097 #if 0
17098 #define GIVE_UP(X) \
17099 do { \
17100 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17101 return 0; \
17102 } while (0)
17103 #else
17104 #define GIVE_UP(X) return 0
17105 #endif
17106
17107 SET_TEXT_POS_FROM_MARKER (start, w->start);
17108
17109 /* Don't use this for mini-windows because these can show
17110 messages and mini-buffers, and we don't handle that here. */
17111 if (MINI_WINDOW_P (w))
17112 GIVE_UP (1);
17113
17114 /* This flag is used to prevent redisplay optimizations. */
17115 if (windows_or_buffers_changed || cursor_type_changed)
17116 GIVE_UP (2);
17117
17118 /* Verify that narrowing has not changed.
17119 Also verify that we were not told to prevent redisplay optimizations.
17120 It would be nice to further
17121 reduce the number of cases where this prevents try_window_id. */
17122 if (current_buffer->clip_changed
17123 || current_buffer->prevent_redisplay_optimizations_p)
17124 GIVE_UP (3);
17125
17126 /* Window must either use window-based redisplay or be full width. */
17127 if (!FRAME_WINDOW_P (f)
17128 && (!FRAME_LINE_INS_DEL_OK (f)
17129 || !WINDOW_FULL_WIDTH_P (w)))
17130 GIVE_UP (4);
17131
17132 /* Give up if point is known NOT to appear in W. */
17133 if (PT < CHARPOS (start))
17134 GIVE_UP (5);
17135
17136 /* Another way to prevent redisplay optimizations. */
17137 if (w->last_modified == 0)
17138 GIVE_UP (6);
17139
17140 /* Verify that window is not hscrolled. */
17141 if (w->hscroll != 0)
17142 GIVE_UP (7);
17143
17144 /* Verify that display wasn't paused. */
17145 if (NILP (w->window_end_valid))
17146 GIVE_UP (8);
17147
17148 /* Can't use this if highlighting a region because a cursor movement
17149 will do more than just set the cursor. */
17150 if (!NILP (Vtransient_mark_mode)
17151 && !NILP (BVAR (current_buffer, mark_active)))
17152 GIVE_UP (9);
17153
17154 /* Likewise if highlighting trailing whitespace. */
17155 if (!NILP (Vshow_trailing_whitespace))
17156 GIVE_UP (11);
17157
17158 /* Likewise if showing a region. */
17159 if (!NILP (w->region_showing))
17160 GIVE_UP (10);
17161
17162 /* Can't use this if overlay arrow position and/or string have
17163 changed. */
17164 if (overlay_arrows_changed_p ())
17165 GIVE_UP (12);
17166
17167 /* When word-wrap is on, adding a space to the first word of a
17168 wrapped line can change the wrap position, altering the line
17169 above it. It might be worthwhile to handle this more
17170 intelligently, but for now just redisplay from scratch. */
17171 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17172 GIVE_UP (21);
17173
17174 /* Under bidi reordering, adding or deleting a character in the
17175 beginning of a paragraph, before the first strong directional
17176 character, can change the base direction of the paragraph (unless
17177 the buffer specifies a fixed paragraph direction), which will
17178 require to redisplay the whole paragraph. It might be worthwhile
17179 to find the paragraph limits and widen the range of redisplayed
17180 lines to that, but for now just give up this optimization and
17181 redisplay from scratch. */
17182 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17183 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17184 GIVE_UP (22);
17185
17186 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17187 only if buffer has really changed. The reason is that the gap is
17188 initially at Z for freshly visited files. The code below would
17189 set end_unchanged to 0 in that case. */
17190 if (MODIFF > SAVE_MODIFF
17191 /* This seems to happen sometimes after saving a buffer. */
17192 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17193 {
17194 if (GPT - BEG < BEG_UNCHANGED)
17195 BEG_UNCHANGED = GPT - BEG;
17196 if (Z - GPT < END_UNCHANGED)
17197 END_UNCHANGED = Z - GPT;
17198 }
17199
17200 /* The position of the first and last character that has been changed. */
17201 first_changed_charpos = BEG + BEG_UNCHANGED;
17202 last_changed_charpos = Z - END_UNCHANGED;
17203
17204 /* If window starts after a line end, and the last change is in
17205 front of that newline, then changes don't affect the display.
17206 This case happens with stealth-fontification. Note that although
17207 the display is unchanged, glyph positions in the matrix have to
17208 be adjusted, of course. */
17209 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17210 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17211 && ((last_changed_charpos < CHARPOS (start)
17212 && CHARPOS (start) == BEGV)
17213 || (last_changed_charpos < CHARPOS (start) - 1
17214 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17215 {
17216 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17217 struct glyph_row *r0;
17218
17219 /* Compute how many chars/bytes have been added to or removed
17220 from the buffer. */
17221 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17222 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17223 Z_delta = Z - Z_old;
17224 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17225
17226 /* Give up if PT is not in the window. Note that it already has
17227 been checked at the start of try_window_id that PT is not in
17228 front of the window start. */
17229 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17230 GIVE_UP (13);
17231
17232 /* If window start is unchanged, we can reuse the whole matrix
17233 as is, after adjusting glyph positions. No need to compute
17234 the window end again, since its offset from Z hasn't changed. */
17235 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17236 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17237 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17238 /* PT must not be in a partially visible line. */
17239 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17240 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17241 {
17242 /* Adjust positions in the glyph matrix. */
17243 if (Z_delta || Z_delta_bytes)
17244 {
17245 struct glyph_row *r1
17246 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17247 increment_matrix_positions (w->current_matrix,
17248 MATRIX_ROW_VPOS (r0, current_matrix),
17249 MATRIX_ROW_VPOS (r1, current_matrix),
17250 Z_delta, Z_delta_bytes);
17251 }
17252
17253 /* Set the cursor. */
17254 row = row_containing_pos (w, PT, r0, NULL, 0);
17255 if (row)
17256 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17257 else
17258 abort ();
17259 return 1;
17260 }
17261 }
17262
17263 /* Handle the case that changes are all below what is displayed in
17264 the window, and that PT is in the window. This shortcut cannot
17265 be taken if ZV is visible in the window, and text has been added
17266 there that is visible in the window. */
17267 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17268 /* ZV is not visible in the window, or there are no
17269 changes at ZV, actually. */
17270 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17271 || first_changed_charpos == last_changed_charpos))
17272 {
17273 struct glyph_row *r0;
17274
17275 /* Give up if PT is not in the window. Note that it already has
17276 been checked at the start of try_window_id that PT is not in
17277 front of the window start. */
17278 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17279 GIVE_UP (14);
17280
17281 /* If window start is unchanged, we can reuse the whole matrix
17282 as is, without changing glyph positions since no text has
17283 been added/removed in front of the window end. */
17284 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17285 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17286 /* PT must not be in a partially visible line. */
17287 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17288 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17289 {
17290 /* We have to compute the window end anew since text
17291 could have been added/removed after it. */
17292 w->window_end_pos
17293 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17294 w->window_end_bytepos
17295 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17296
17297 /* Set the cursor. */
17298 row = row_containing_pos (w, PT, r0, NULL, 0);
17299 if (row)
17300 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17301 else
17302 abort ();
17303 return 2;
17304 }
17305 }
17306
17307 /* Give up if window start is in the changed area.
17308
17309 The condition used to read
17310
17311 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17312
17313 but why that was tested escapes me at the moment. */
17314 if (CHARPOS (start) >= first_changed_charpos
17315 && CHARPOS (start) <= last_changed_charpos)
17316 GIVE_UP (15);
17317
17318 /* Check that window start agrees with the start of the first glyph
17319 row in its current matrix. Check this after we know the window
17320 start is not in changed text, otherwise positions would not be
17321 comparable. */
17322 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17323 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17324 GIVE_UP (16);
17325
17326 /* Give up if the window ends in strings. Overlay strings
17327 at the end are difficult to handle, so don't try. */
17328 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17329 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17330 GIVE_UP (20);
17331
17332 /* Compute the position at which we have to start displaying new
17333 lines. Some of the lines at the top of the window might be
17334 reusable because they are not displaying changed text. Find the
17335 last row in W's current matrix not affected by changes at the
17336 start of current_buffer. Value is null if changes start in the
17337 first line of window. */
17338 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17339 if (last_unchanged_at_beg_row)
17340 {
17341 /* Avoid starting to display in the middle of a character, a TAB
17342 for instance. This is easier than to set up the iterator
17343 exactly, and it's not a frequent case, so the additional
17344 effort wouldn't really pay off. */
17345 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17346 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17347 && last_unchanged_at_beg_row > w->current_matrix->rows)
17348 --last_unchanged_at_beg_row;
17349
17350 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17351 GIVE_UP (17);
17352
17353 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17354 GIVE_UP (18);
17355 start_pos = it.current.pos;
17356
17357 /* Start displaying new lines in the desired matrix at the same
17358 vpos we would use in the current matrix, i.e. below
17359 last_unchanged_at_beg_row. */
17360 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17361 current_matrix);
17362 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17363 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17364
17365 eassert (it.hpos == 0 && it.current_x == 0);
17366 }
17367 else
17368 {
17369 /* There are no reusable lines at the start of the window.
17370 Start displaying in the first text line. */
17371 start_display (&it, w, start);
17372 it.vpos = it.first_vpos;
17373 start_pos = it.current.pos;
17374 }
17375
17376 /* Find the first row that is not affected by changes at the end of
17377 the buffer. Value will be null if there is no unchanged row, in
17378 which case we must redisplay to the end of the window. delta
17379 will be set to the value by which buffer positions beginning with
17380 first_unchanged_at_end_row have to be adjusted due to text
17381 changes. */
17382 first_unchanged_at_end_row
17383 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17384 IF_DEBUG (debug_delta = delta);
17385 IF_DEBUG (debug_delta_bytes = delta_bytes);
17386
17387 /* Set stop_pos to the buffer position up to which we will have to
17388 display new lines. If first_unchanged_at_end_row != NULL, this
17389 is the buffer position of the start of the line displayed in that
17390 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17391 that we don't stop at a buffer position. */
17392 stop_pos = 0;
17393 if (first_unchanged_at_end_row)
17394 {
17395 eassert (last_unchanged_at_beg_row == NULL
17396 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17397
17398 /* If this is a continuation line, move forward to the next one
17399 that isn't. Changes in lines above affect this line.
17400 Caution: this may move first_unchanged_at_end_row to a row
17401 not displaying text. */
17402 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17403 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17404 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17405 < it.last_visible_y))
17406 ++first_unchanged_at_end_row;
17407
17408 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17409 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17410 >= it.last_visible_y))
17411 first_unchanged_at_end_row = NULL;
17412 else
17413 {
17414 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17415 + delta);
17416 first_unchanged_at_end_vpos
17417 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17418 eassert (stop_pos >= Z - END_UNCHANGED);
17419 }
17420 }
17421 else if (last_unchanged_at_beg_row == NULL)
17422 GIVE_UP (19);
17423
17424
17425 #ifdef GLYPH_DEBUG
17426
17427 /* Either there is no unchanged row at the end, or the one we have
17428 now displays text. This is a necessary condition for the window
17429 end pos calculation at the end of this function. */
17430 eassert (first_unchanged_at_end_row == NULL
17431 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17432
17433 debug_last_unchanged_at_beg_vpos
17434 = (last_unchanged_at_beg_row
17435 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17436 : -1);
17437 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17438
17439 #endif /* GLYPH_DEBUG */
17440
17441
17442 /* Display new lines. Set last_text_row to the last new line
17443 displayed which has text on it, i.e. might end up as being the
17444 line where the window_end_vpos is. */
17445 w->cursor.vpos = -1;
17446 last_text_row = NULL;
17447 overlay_arrow_seen = 0;
17448 while (it.current_y < it.last_visible_y
17449 && !fonts_changed_p
17450 && (first_unchanged_at_end_row == NULL
17451 || IT_CHARPOS (it) < stop_pos))
17452 {
17453 if (display_line (&it))
17454 last_text_row = it.glyph_row - 1;
17455 }
17456
17457 if (fonts_changed_p)
17458 return -1;
17459
17460
17461 /* Compute differences in buffer positions, y-positions etc. for
17462 lines reused at the bottom of the window. Compute what we can
17463 scroll. */
17464 if (first_unchanged_at_end_row
17465 /* No lines reused because we displayed everything up to the
17466 bottom of the window. */
17467 && it.current_y < it.last_visible_y)
17468 {
17469 dvpos = (it.vpos
17470 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17471 current_matrix));
17472 dy = it.current_y - first_unchanged_at_end_row->y;
17473 run.current_y = first_unchanged_at_end_row->y;
17474 run.desired_y = run.current_y + dy;
17475 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17476 }
17477 else
17478 {
17479 delta = delta_bytes = dvpos = dy
17480 = run.current_y = run.desired_y = run.height = 0;
17481 first_unchanged_at_end_row = NULL;
17482 }
17483 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17484
17485
17486 /* Find the cursor if not already found. We have to decide whether
17487 PT will appear on this window (it sometimes doesn't, but this is
17488 not a very frequent case.) This decision has to be made before
17489 the current matrix is altered. A value of cursor.vpos < 0 means
17490 that PT is either in one of the lines beginning at
17491 first_unchanged_at_end_row or below the window. Don't care for
17492 lines that might be displayed later at the window end; as
17493 mentioned, this is not a frequent case. */
17494 if (w->cursor.vpos < 0)
17495 {
17496 /* Cursor in unchanged rows at the top? */
17497 if (PT < CHARPOS (start_pos)
17498 && last_unchanged_at_beg_row)
17499 {
17500 row = row_containing_pos (w, PT,
17501 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17502 last_unchanged_at_beg_row + 1, 0);
17503 if (row)
17504 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17505 }
17506
17507 /* Start from first_unchanged_at_end_row looking for PT. */
17508 else if (first_unchanged_at_end_row)
17509 {
17510 row = row_containing_pos (w, PT - delta,
17511 first_unchanged_at_end_row, NULL, 0);
17512 if (row)
17513 set_cursor_from_row (w, row, w->current_matrix, delta,
17514 delta_bytes, dy, dvpos);
17515 }
17516
17517 /* Give up if cursor was not found. */
17518 if (w->cursor.vpos < 0)
17519 {
17520 clear_glyph_matrix (w->desired_matrix);
17521 return -1;
17522 }
17523 }
17524
17525 /* Don't let the cursor end in the scroll margins. */
17526 {
17527 int this_scroll_margin, cursor_height;
17528
17529 this_scroll_margin =
17530 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17531 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17532 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17533
17534 if ((w->cursor.y < this_scroll_margin
17535 && CHARPOS (start) > BEGV)
17536 /* Old redisplay didn't take scroll margin into account at the bottom,
17537 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17538 || (w->cursor.y + (make_cursor_line_fully_visible_p
17539 ? cursor_height + this_scroll_margin
17540 : 1)) > it.last_visible_y)
17541 {
17542 w->cursor.vpos = -1;
17543 clear_glyph_matrix (w->desired_matrix);
17544 return -1;
17545 }
17546 }
17547
17548 /* Scroll the display. Do it before changing the current matrix so
17549 that xterm.c doesn't get confused about where the cursor glyph is
17550 found. */
17551 if (dy && run.height)
17552 {
17553 update_begin (f);
17554
17555 if (FRAME_WINDOW_P (f))
17556 {
17557 FRAME_RIF (f)->update_window_begin_hook (w);
17558 FRAME_RIF (f)->clear_window_mouse_face (w);
17559 FRAME_RIF (f)->scroll_run_hook (w, &run);
17560 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17561 }
17562 else
17563 {
17564 /* Terminal frame. In this case, dvpos gives the number of
17565 lines to scroll by; dvpos < 0 means scroll up. */
17566 int from_vpos
17567 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17568 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17569 int end = (WINDOW_TOP_EDGE_LINE (w)
17570 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17571 + window_internal_height (w));
17572
17573 #if defined (HAVE_GPM) || defined (MSDOS)
17574 x_clear_window_mouse_face (w);
17575 #endif
17576 /* Perform the operation on the screen. */
17577 if (dvpos > 0)
17578 {
17579 /* Scroll last_unchanged_at_beg_row to the end of the
17580 window down dvpos lines. */
17581 set_terminal_window (f, end);
17582
17583 /* On dumb terminals delete dvpos lines at the end
17584 before inserting dvpos empty lines. */
17585 if (!FRAME_SCROLL_REGION_OK (f))
17586 ins_del_lines (f, end - dvpos, -dvpos);
17587
17588 /* Insert dvpos empty lines in front of
17589 last_unchanged_at_beg_row. */
17590 ins_del_lines (f, from, dvpos);
17591 }
17592 else if (dvpos < 0)
17593 {
17594 /* Scroll up last_unchanged_at_beg_vpos to the end of
17595 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17596 set_terminal_window (f, end);
17597
17598 /* Delete dvpos lines in front of
17599 last_unchanged_at_beg_vpos. ins_del_lines will set
17600 the cursor to the given vpos and emit |dvpos| delete
17601 line sequences. */
17602 ins_del_lines (f, from + dvpos, dvpos);
17603
17604 /* On a dumb terminal insert dvpos empty lines at the
17605 end. */
17606 if (!FRAME_SCROLL_REGION_OK (f))
17607 ins_del_lines (f, end + dvpos, -dvpos);
17608 }
17609
17610 set_terminal_window (f, 0);
17611 }
17612
17613 update_end (f);
17614 }
17615
17616 /* Shift reused rows of the current matrix to the right position.
17617 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17618 text. */
17619 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17620 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17621 if (dvpos < 0)
17622 {
17623 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17624 bottom_vpos, dvpos);
17625 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17626 bottom_vpos, 0);
17627 }
17628 else if (dvpos > 0)
17629 {
17630 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17631 bottom_vpos, dvpos);
17632 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17633 first_unchanged_at_end_vpos + dvpos, 0);
17634 }
17635
17636 /* For frame-based redisplay, make sure that current frame and window
17637 matrix are in sync with respect to glyph memory. */
17638 if (!FRAME_WINDOW_P (f))
17639 sync_frame_with_window_matrix_rows (w);
17640
17641 /* Adjust buffer positions in reused rows. */
17642 if (delta || delta_bytes)
17643 increment_matrix_positions (current_matrix,
17644 first_unchanged_at_end_vpos + dvpos,
17645 bottom_vpos, delta, delta_bytes);
17646
17647 /* Adjust Y positions. */
17648 if (dy)
17649 shift_glyph_matrix (w, current_matrix,
17650 first_unchanged_at_end_vpos + dvpos,
17651 bottom_vpos, dy);
17652
17653 if (first_unchanged_at_end_row)
17654 {
17655 first_unchanged_at_end_row += dvpos;
17656 if (first_unchanged_at_end_row->y >= it.last_visible_y
17657 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17658 first_unchanged_at_end_row = NULL;
17659 }
17660
17661 /* If scrolling up, there may be some lines to display at the end of
17662 the window. */
17663 last_text_row_at_end = NULL;
17664 if (dy < 0)
17665 {
17666 /* Scrolling up can leave for example a partially visible line
17667 at the end of the window to be redisplayed. */
17668 /* Set last_row to the glyph row in the current matrix where the
17669 window end line is found. It has been moved up or down in
17670 the matrix by dvpos. */
17671 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17672 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17673
17674 /* If last_row is the window end line, it should display text. */
17675 eassert (last_row->displays_text_p);
17676
17677 /* If window end line was partially visible before, begin
17678 displaying at that line. Otherwise begin displaying with the
17679 line following it. */
17680 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17681 {
17682 init_to_row_start (&it, w, last_row);
17683 it.vpos = last_vpos;
17684 it.current_y = last_row->y;
17685 }
17686 else
17687 {
17688 init_to_row_end (&it, w, last_row);
17689 it.vpos = 1 + last_vpos;
17690 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17691 ++last_row;
17692 }
17693
17694 /* We may start in a continuation line. If so, we have to
17695 get the right continuation_lines_width and current_x. */
17696 it.continuation_lines_width = last_row->continuation_lines_width;
17697 it.hpos = it.current_x = 0;
17698
17699 /* Display the rest of the lines at the window end. */
17700 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17701 while (it.current_y < it.last_visible_y
17702 && !fonts_changed_p)
17703 {
17704 /* Is it always sure that the display agrees with lines in
17705 the current matrix? I don't think so, so we mark rows
17706 displayed invalid in the current matrix by setting their
17707 enabled_p flag to zero. */
17708 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17709 if (display_line (&it))
17710 last_text_row_at_end = it.glyph_row - 1;
17711 }
17712 }
17713
17714 /* Update window_end_pos and window_end_vpos. */
17715 if (first_unchanged_at_end_row
17716 && !last_text_row_at_end)
17717 {
17718 /* Window end line if one of the preserved rows from the current
17719 matrix. Set row to the last row displaying text in current
17720 matrix starting at first_unchanged_at_end_row, after
17721 scrolling. */
17722 eassert (first_unchanged_at_end_row->displays_text_p);
17723 row = find_last_row_displaying_text (w->current_matrix, &it,
17724 first_unchanged_at_end_row);
17725 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17726
17727 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17728 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17729 w->window_end_vpos
17730 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17731 eassert (w->window_end_bytepos >= 0);
17732 IF_DEBUG (debug_method_add (w, "A"));
17733 }
17734 else if (last_text_row_at_end)
17735 {
17736 w->window_end_pos
17737 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17738 w->window_end_bytepos
17739 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17740 w->window_end_vpos
17741 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17742 eassert (w->window_end_bytepos >= 0);
17743 IF_DEBUG (debug_method_add (w, "B"));
17744 }
17745 else if (last_text_row)
17746 {
17747 /* We have displayed either to the end of the window or at the
17748 end of the window, i.e. the last row with text is to be found
17749 in the desired matrix. */
17750 w->window_end_pos
17751 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17752 w->window_end_bytepos
17753 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17754 w->window_end_vpos
17755 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17756 eassert (w->window_end_bytepos >= 0);
17757 }
17758 else if (first_unchanged_at_end_row == NULL
17759 && last_text_row == NULL
17760 && last_text_row_at_end == NULL)
17761 {
17762 /* Displayed to end of window, but no line containing text was
17763 displayed. Lines were deleted at the end of the window. */
17764 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17765 int vpos = XFASTINT (w->window_end_vpos);
17766 struct glyph_row *current_row = current_matrix->rows + vpos;
17767 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17768
17769 for (row = NULL;
17770 row == NULL && vpos >= first_vpos;
17771 --vpos, --current_row, --desired_row)
17772 {
17773 if (desired_row->enabled_p)
17774 {
17775 if (desired_row->displays_text_p)
17776 row = desired_row;
17777 }
17778 else if (current_row->displays_text_p)
17779 row = current_row;
17780 }
17781
17782 eassert (row != NULL);
17783 w->window_end_vpos = make_number (vpos + 1);
17784 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17785 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17786 eassert (w->window_end_bytepos >= 0);
17787 IF_DEBUG (debug_method_add (w, "C"));
17788 }
17789 else
17790 abort ();
17791
17792 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17793 debug_end_vpos = XFASTINT (w->window_end_vpos));
17794
17795 /* Record that display has not been completed. */
17796 w->window_end_valid = Qnil;
17797 w->desired_matrix->no_scrolling_p = 1;
17798 return 3;
17799
17800 #undef GIVE_UP
17801 }
17802
17803
17804 \f
17805 /***********************************************************************
17806 More debugging support
17807 ***********************************************************************/
17808
17809 #ifdef GLYPH_DEBUG
17810
17811 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17812 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17813 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17814
17815
17816 /* Dump the contents of glyph matrix MATRIX on stderr.
17817
17818 GLYPHS 0 means don't show glyph contents.
17819 GLYPHS 1 means show glyphs in short form
17820 GLYPHS > 1 means show glyphs in long form. */
17821
17822 void
17823 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17824 {
17825 int i;
17826 for (i = 0; i < matrix->nrows; ++i)
17827 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17828 }
17829
17830
17831 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17832 the glyph row and area where the glyph comes from. */
17833
17834 void
17835 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17836 {
17837 if (glyph->type == CHAR_GLYPH)
17838 {
17839 fprintf (stderr,
17840 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17841 glyph - row->glyphs[TEXT_AREA],
17842 'C',
17843 glyph->charpos,
17844 (BUFFERP (glyph->object)
17845 ? 'B'
17846 : (STRINGP (glyph->object)
17847 ? 'S'
17848 : '-')),
17849 glyph->pixel_width,
17850 glyph->u.ch,
17851 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17852 ? glyph->u.ch
17853 : '.'),
17854 glyph->face_id,
17855 glyph->left_box_line_p,
17856 glyph->right_box_line_p);
17857 }
17858 else if (glyph->type == STRETCH_GLYPH)
17859 {
17860 fprintf (stderr,
17861 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17862 glyph - row->glyphs[TEXT_AREA],
17863 'S',
17864 glyph->charpos,
17865 (BUFFERP (glyph->object)
17866 ? 'B'
17867 : (STRINGP (glyph->object)
17868 ? 'S'
17869 : '-')),
17870 glyph->pixel_width,
17871 0,
17872 '.',
17873 glyph->face_id,
17874 glyph->left_box_line_p,
17875 glyph->right_box_line_p);
17876 }
17877 else if (glyph->type == IMAGE_GLYPH)
17878 {
17879 fprintf (stderr,
17880 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17881 glyph - row->glyphs[TEXT_AREA],
17882 'I',
17883 glyph->charpos,
17884 (BUFFERP (glyph->object)
17885 ? 'B'
17886 : (STRINGP (glyph->object)
17887 ? 'S'
17888 : '-')),
17889 glyph->pixel_width,
17890 glyph->u.img_id,
17891 '.',
17892 glyph->face_id,
17893 glyph->left_box_line_p,
17894 glyph->right_box_line_p);
17895 }
17896 else if (glyph->type == COMPOSITE_GLYPH)
17897 {
17898 fprintf (stderr,
17899 " %5td %4c %6"pI"d %c %3d 0x%05x",
17900 glyph - row->glyphs[TEXT_AREA],
17901 '+',
17902 glyph->charpos,
17903 (BUFFERP (glyph->object)
17904 ? 'B'
17905 : (STRINGP (glyph->object)
17906 ? 'S'
17907 : '-')),
17908 glyph->pixel_width,
17909 glyph->u.cmp.id);
17910 if (glyph->u.cmp.automatic)
17911 fprintf (stderr,
17912 "[%d-%d]",
17913 glyph->slice.cmp.from, glyph->slice.cmp.to);
17914 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17915 glyph->face_id,
17916 glyph->left_box_line_p,
17917 glyph->right_box_line_p);
17918 }
17919 }
17920
17921
17922 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17923 GLYPHS 0 means don't show glyph contents.
17924 GLYPHS 1 means show glyphs in short form
17925 GLYPHS > 1 means show glyphs in long form. */
17926
17927 void
17928 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17929 {
17930 if (glyphs != 1)
17931 {
17932 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17933 fprintf (stderr, "======================================================================\n");
17934
17935 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17936 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17937 vpos,
17938 MATRIX_ROW_START_CHARPOS (row),
17939 MATRIX_ROW_END_CHARPOS (row),
17940 row->used[TEXT_AREA],
17941 row->contains_overlapping_glyphs_p,
17942 row->enabled_p,
17943 row->truncated_on_left_p,
17944 row->truncated_on_right_p,
17945 row->continued_p,
17946 MATRIX_ROW_CONTINUATION_LINE_P (row),
17947 row->displays_text_p,
17948 row->ends_at_zv_p,
17949 row->fill_line_p,
17950 row->ends_in_middle_of_char_p,
17951 row->starts_in_middle_of_char_p,
17952 row->mouse_face_p,
17953 row->x,
17954 row->y,
17955 row->pixel_width,
17956 row->height,
17957 row->visible_height,
17958 row->ascent,
17959 row->phys_ascent);
17960 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17961 row->end.overlay_string_index,
17962 row->continuation_lines_width);
17963 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17964 CHARPOS (row->start.string_pos),
17965 CHARPOS (row->end.string_pos));
17966 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17967 row->end.dpvec_index);
17968 }
17969
17970 if (glyphs > 1)
17971 {
17972 int area;
17973
17974 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17975 {
17976 struct glyph *glyph = row->glyphs[area];
17977 struct glyph *glyph_end = glyph + row->used[area];
17978
17979 /* Glyph for a line end in text. */
17980 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17981 ++glyph_end;
17982
17983 if (glyph < glyph_end)
17984 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17985
17986 for (; glyph < glyph_end; ++glyph)
17987 dump_glyph (row, glyph, area);
17988 }
17989 }
17990 else if (glyphs == 1)
17991 {
17992 int area;
17993
17994 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17995 {
17996 char *s = (char *) alloca (row->used[area] + 1);
17997 int i;
17998
17999 for (i = 0; i < row->used[area]; ++i)
18000 {
18001 struct glyph *glyph = row->glyphs[area] + i;
18002 if (glyph->type == CHAR_GLYPH
18003 && glyph->u.ch < 0x80
18004 && glyph->u.ch >= ' ')
18005 s[i] = glyph->u.ch;
18006 else
18007 s[i] = '.';
18008 }
18009
18010 s[i] = '\0';
18011 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18012 }
18013 }
18014 }
18015
18016
18017 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18018 Sdump_glyph_matrix, 0, 1, "p",
18019 doc: /* Dump the current matrix of the selected window to stderr.
18020 Shows contents of glyph row structures. With non-nil
18021 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18022 glyphs in short form, otherwise show glyphs in long form. */)
18023 (Lisp_Object glyphs)
18024 {
18025 struct window *w = XWINDOW (selected_window);
18026 struct buffer *buffer = XBUFFER (w->buffer);
18027
18028 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18029 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18030 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18031 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18032 fprintf (stderr, "=============================================\n");
18033 dump_glyph_matrix (w->current_matrix,
18034 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18035 return Qnil;
18036 }
18037
18038
18039 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18040 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18041 (void)
18042 {
18043 struct frame *f = XFRAME (selected_frame);
18044 dump_glyph_matrix (f->current_matrix, 1);
18045 return Qnil;
18046 }
18047
18048
18049 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18050 doc: /* Dump glyph row ROW to stderr.
18051 GLYPH 0 means don't dump glyphs.
18052 GLYPH 1 means dump glyphs in short form.
18053 GLYPH > 1 or omitted means dump glyphs in long form. */)
18054 (Lisp_Object row, Lisp_Object glyphs)
18055 {
18056 struct glyph_matrix *matrix;
18057 EMACS_INT vpos;
18058
18059 CHECK_NUMBER (row);
18060 matrix = XWINDOW (selected_window)->current_matrix;
18061 vpos = XINT (row);
18062 if (vpos >= 0 && vpos < matrix->nrows)
18063 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18064 vpos,
18065 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18066 return Qnil;
18067 }
18068
18069
18070 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18071 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18072 GLYPH 0 means don't dump glyphs.
18073 GLYPH 1 means dump glyphs in short form.
18074 GLYPH > 1 or omitted means dump glyphs in long form. */)
18075 (Lisp_Object row, Lisp_Object glyphs)
18076 {
18077 struct frame *sf = SELECTED_FRAME ();
18078 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18079 EMACS_INT vpos;
18080
18081 CHECK_NUMBER (row);
18082 vpos = XINT (row);
18083 if (vpos >= 0 && vpos < m->nrows)
18084 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18085 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18086 return Qnil;
18087 }
18088
18089
18090 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18091 doc: /* Toggle tracing of redisplay.
18092 With ARG, turn tracing on if and only if ARG is positive. */)
18093 (Lisp_Object arg)
18094 {
18095 if (NILP (arg))
18096 trace_redisplay_p = !trace_redisplay_p;
18097 else
18098 {
18099 arg = Fprefix_numeric_value (arg);
18100 trace_redisplay_p = XINT (arg) > 0;
18101 }
18102
18103 return Qnil;
18104 }
18105
18106
18107 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18108 doc: /* Like `format', but print result to stderr.
18109 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18110 (ptrdiff_t nargs, Lisp_Object *args)
18111 {
18112 Lisp_Object s = Fformat (nargs, args);
18113 fprintf (stderr, "%s", SDATA (s));
18114 return Qnil;
18115 }
18116
18117 #endif /* GLYPH_DEBUG */
18118
18119
18120 \f
18121 /***********************************************************************
18122 Building Desired Matrix Rows
18123 ***********************************************************************/
18124
18125 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18126 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18127
18128 static struct glyph_row *
18129 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18130 {
18131 struct frame *f = XFRAME (WINDOW_FRAME (w));
18132 struct buffer *buffer = XBUFFER (w->buffer);
18133 struct buffer *old = current_buffer;
18134 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18135 int arrow_len = SCHARS (overlay_arrow_string);
18136 const unsigned char *arrow_end = arrow_string + arrow_len;
18137 const unsigned char *p;
18138 struct it it;
18139 int multibyte_p;
18140 int n_glyphs_before;
18141
18142 set_buffer_temp (buffer);
18143 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18144 it.glyph_row->used[TEXT_AREA] = 0;
18145 SET_TEXT_POS (it.position, 0, 0);
18146
18147 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18148 p = arrow_string;
18149 while (p < arrow_end)
18150 {
18151 Lisp_Object face, ilisp;
18152
18153 /* Get the next character. */
18154 if (multibyte_p)
18155 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18156 else
18157 {
18158 it.c = it.char_to_display = *p, it.len = 1;
18159 if (! ASCII_CHAR_P (it.c))
18160 it.char_to_display = BYTE8_TO_CHAR (it.c);
18161 }
18162 p += it.len;
18163
18164 /* Get its face. */
18165 ilisp = make_number (p - arrow_string);
18166 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18167 it.face_id = compute_char_face (f, it.char_to_display, face);
18168
18169 /* Compute its width, get its glyphs. */
18170 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18171 SET_TEXT_POS (it.position, -1, -1);
18172 PRODUCE_GLYPHS (&it);
18173
18174 /* If this character doesn't fit any more in the line, we have
18175 to remove some glyphs. */
18176 if (it.current_x > it.last_visible_x)
18177 {
18178 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18179 break;
18180 }
18181 }
18182
18183 set_buffer_temp (old);
18184 return it.glyph_row;
18185 }
18186
18187
18188 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18189 glyphs are only inserted for terminal frames since we can't really
18190 win with truncation glyphs when partially visible glyphs are
18191 involved. Which glyphs to insert is determined by
18192 produce_special_glyphs. */
18193
18194 static void
18195 insert_left_trunc_glyphs (struct it *it)
18196 {
18197 struct it truncate_it;
18198 struct glyph *from, *end, *to, *toend;
18199
18200 eassert (!FRAME_WINDOW_P (it->f));
18201
18202 /* Get the truncation glyphs. */
18203 truncate_it = *it;
18204 truncate_it.current_x = 0;
18205 truncate_it.face_id = DEFAULT_FACE_ID;
18206 truncate_it.glyph_row = &scratch_glyph_row;
18207 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18208 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18209 truncate_it.object = make_number (0);
18210 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18211
18212 /* Overwrite glyphs from IT with truncation glyphs. */
18213 if (!it->glyph_row->reversed_p)
18214 {
18215 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18216 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18217 to = it->glyph_row->glyphs[TEXT_AREA];
18218 toend = to + it->glyph_row->used[TEXT_AREA];
18219
18220 while (from < end)
18221 *to++ = *from++;
18222
18223 /* There may be padding glyphs left over. Overwrite them too. */
18224 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18225 {
18226 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18227 while (from < end)
18228 *to++ = *from++;
18229 }
18230
18231 if (to > toend)
18232 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18233 }
18234 else
18235 {
18236 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18237 that back to front. */
18238 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18239 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18240 toend = it->glyph_row->glyphs[TEXT_AREA];
18241 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18242
18243 while (from >= end && to >= toend)
18244 *to-- = *from--;
18245 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18246 {
18247 from =
18248 truncate_it.glyph_row->glyphs[TEXT_AREA]
18249 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18250 while (from >= end && to >= toend)
18251 *to-- = *from--;
18252 }
18253 if (from >= end)
18254 {
18255 /* Need to free some room before prepending additional
18256 glyphs. */
18257 int move_by = from - end + 1;
18258 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18259 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18260
18261 for ( ; g >= g0; g--)
18262 g[move_by] = *g;
18263 while (from >= end)
18264 *to-- = *from--;
18265 it->glyph_row->used[TEXT_AREA] += move_by;
18266 }
18267 }
18268 }
18269
18270 /* Compute the hash code for ROW. */
18271 unsigned
18272 row_hash (struct glyph_row *row)
18273 {
18274 int area, k;
18275 unsigned hashval = 0;
18276
18277 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18278 for (k = 0; k < row->used[area]; ++k)
18279 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18280 + row->glyphs[area][k].u.val
18281 + row->glyphs[area][k].face_id
18282 + row->glyphs[area][k].padding_p
18283 + (row->glyphs[area][k].type << 2));
18284
18285 return hashval;
18286 }
18287
18288 /* Compute the pixel height and width of IT->glyph_row.
18289
18290 Most of the time, ascent and height of a display line will be equal
18291 to the max_ascent and max_height values of the display iterator
18292 structure. This is not the case if
18293
18294 1. We hit ZV without displaying anything. In this case, max_ascent
18295 and max_height will be zero.
18296
18297 2. We have some glyphs that don't contribute to the line height.
18298 (The glyph row flag contributes_to_line_height_p is for future
18299 pixmap extensions).
18300
18301 The first case is easily covered by using default values because in
18302 these cases, the line height does not really matter, except that it
18303 must not be zero. */
18304
18305 static void
18306 compute_line_metrics (struct it *it)
18307 {
18308 struct glyph_row *row = it->glyph_row;
18309
18310 if (FRAME_WINDOW_P (it->f))
18311 {
18312 int i, min_y, max_y;
18313
18314 /* The line may consist of one space only, that was added to
18315 place the cursor on it. If so, the row's height hasn't been
18316 computed yet. */
18317 if (row->height == 0)
18318 {
18319 if (it->max_ascent + it->max_descent == 0)
18320 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18321 row->ascent = it->max_ascent;
18322 row->height = it->max_ascent + it->max_descent;
18323 row->phys_ascent = it->max_phys_ascent;
18324 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18325 row->extra_line_spacing = it->max_extra_line_spacing;
18326 }
18327
18328 /* Compute the width of this line. */
18329 row->pixel_width = row->x;
18330 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18331 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18332
18333 eassert (row->pixel_width >= 0);
18334 eassert (row->ascent >= 0 && row->height > 0);
18335
18336 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18337 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18338
18339 /* If first line's physical ascent is larger than its logical
18340 ascent, use the physical ascent, and make the row taller.
18341 This makes accented characters fully visible. */
18342 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18343 && row->phys_ascent > row->ascent)
18344 {
18345 row->height += row->phys_ascent - row->ascent;
18346 row->ascent = row->phys_ascent;
18347 }
18348
18349 /* Compute how much of the line is visible. */
18350 row->visible_height = row->height;
18351
18352 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18353 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18354
18355 if (row->y < min_y)
18356 row->visible_height -= min_y - row->y;
18357 if (row->y + row->height > max_y)
18358 row->visible_height -= row->y + row->height - max_y;
18359 }
18360 else
18361 {
18362 row->pixel_width = row->used[TEXT_AREA];
18363 if (row->continued_p)
18364 row->pixel_width -= it->continuation_pixel_width;
18365 else if (row->truncated_on_right_p)
18366 row->pixel_width -= it->truncation_pixel_width;
18367 row->ascent = row->phys_ascent = 0;
18368 row->height = row->phys_height = row->visible_height = 1;
18369 row->extra_line_spacing = 0;
18370 }
18371
18372 /* Compute a hash code for this row. */
18373 row->hash = row_hash (row);
18374
18375 it->max_ascent = it->max_descent = 0;
18376 it->max_phys_ascent = it->max_phys_descent = 0;
18377 }
18378
18379
18380 /* Append one space to the glyph row of iterator IT if doing a
18381 window-based redisplay. The space has the same face as
18382 IT->face_id. Value is non-zero if a space was added.
18383
18384 This function is called to make sure that there is always one glyph
18385 at the end of a glyph row that the cursor can be set on under
18386 window-systems. (If there weren't such a glyph we would not know
18387 how wide and tall a box cursor should be displayed).
18388
18389 At the same time this space let's a nicely handle clearing to the
18390 end of the line if the row ends in italic text. */
18391
18392 static int
18393 append_space_for_newline (struct it *it, int default_face_p)
18394 {
18395 if (FRAME_WINDOW_P (it->f))
18396 {
18397 int n = it->glyph_row->used[TEXT_AREA];
18398
18399 if (it->glyph_row->glyphs[TEXT_AREA] + n
18400 < it->glyph_row->glyphs[1 + TEXT_AREA])
18401 {
18402 /* Save some values that must not be changed.
18403 Must save IT->c and IT->len because otherwise
18404 ITERATOR_AT_END_P wouldn't work anymore after
18405 append_space_for_newline has been called. */
18406 enum display_element_type saved_what = it->what;
18407 int saved_c = it->c, saved_len = it->len;
18408 int saved_char_to_display = it->char_to_display;
18409 int saved_x = it->current_x;
18410 int saved_face_id = it->face_id;
18411 struct text_pos saved_pos;
18412 Lisp_Object saved_object;
18413 struct face *face;
18414
18415 saved_object = it->object;
18416 saved_pos = it->position;
18417
18418 it->what = IT_CHARACTER;
18419 memset (&it->position, 0, sizeof it->position);
18420 it->object = make_number (0);
18421 it->c = it->char_to_display = ' ';
18422 it->len = 1;
18423
18424 /* If the default face was remapped, be sure to use the
18425 remapped face for the appended newline. */
18426 if (default_face_p)
18427 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18428 else if (it->face_before_selective_p)
18429 it->face_id = it->saved_face_id;
18430 face = FACE_FROM_ID (it->f, it->face_id);
18431 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18432
18433 PRODUCE_GLYPHS (it);
18434
18435 it->override_ascent = -1;
18436 it->constrain_row_ascent_descent_p = 0;
18437 it->current_x = saved_x;
18438 it->object = saved_object;
18439 it->position = saved_pos;
18440 it->what = saved_what;
18441 it->face_id = saved_face_id;
18442 it->len = saved_len;
18443 it->c = saved_c;
18444 it->char_to_display = saved_char_to_display;
18445 return 1;
18446 }
18447 }
18448
18449 return 0;
18450 }
18451
18452
18453 /* Extend the face of the last glyph in the text area of IT->glyph_row
18454 to the end of the display line. Called from display_line. If the
18455 glyph row is empty, add a space glyph to it so that we know the
18456 face to draw. Set the glyph row flag fill_line_p. If the glyph
18457 row is R2L, prepend a stretch glyph to cover the empty space to the
18458 left of the leftmost glyph. */
18459
18460 static void
18461 extend_face_to_end_of_line (struct it *it)
18462 {
18463 struct face *face, *default_face;
18464 struct frame *f = it->f;
18465
18466 /* If line is already filled, do nothing. Non window-system frames
18467 get a grace of one more ``pixel'' because their characters are
18468 1-``pixel'' wide, so they hit the equality too early. This grace
18469 is needed only for R2L rows that are not continued, to produce
18470 one extra blank where we could display the cursor. */
18471 if (it->current_x >= it->last_visible_x
18472 + (!FRAME_WINDOW_P (f)
18473 && it->glyph_row->reversed_p
18474 && !it->glyph_row->continued_p))
18475 return;
18476
18477 /* The default face, possibly remapped. */
18478 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18479
18480 /* Face extension extends the background and box of IT->face_id
18481 to the end of the line. If the background equals the background
18482 of the frame, we don't have to do anything. */
18483 if (it->face_before_selective_p)
18484 face = FACE_FROM_ID (f, it->saved_face_id);
18485 else
18486 face = FACE_FROM_ID (f, it->face_id);
18487
18488 if (FRAME_WINDOW_P (f)
18489 && it->glyph_row->displays_text_p
18490 && face->box == FACE_NO_BOX
18491 && face->background == FRAME_BACKGROUND_PIXEL (f)
18492 && !face->stipple
18493 && !it->glyph_row->reversed_p)
18494 return;
18495
18496 /* Set the glyph row flag indicating that the face of the last glyph
18497 in the text area has to be drawn to the end of the text area. */
18498 it->glyph_row->fill_line_p = 1;
18499
18500 /* If current character of IT is not ASCII, make sure we have the
18501 ASCII face. This will be automatically undone the next time
18502 get_next_display_element returns a multibyte character. Note
18503 that the character will always be single byte in unibyte
18504 text. */
18505 if (!ASCII_CHAR_P (it->c))
18506 {
18507 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18508 }
18509
18510 if (FRAME_WINDOW_P (f))
18511 {
18512 /* If the row is empty, add a space with the current face of IT,
18513 so that we know which face to draw. */
18514 if (it->glyph_row->used[TEXT_AREA] == 0)
18515 {
18516 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18517 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18518 it->glyph_row->used[TEXT_AREA] = 1;
18519 }
18520 #ifdef HAVE_WINDOW_SYSTEM
18521 if (it->glyph_row->reversed_p)
18522 {
18523 /* Prepend a stretch glyph to the row, such that the
18524 rightmost glyph will be drawn flushed all the way to the
18525 right margin of the window. The stretch glyph that will
18526 occupy the empty space, if any, to the left of the
18527 glyphs. */
18528 struct font *font = face->font ? face->font : FRAME_FONT (f);
18529 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18530 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18531 struct glyph *g;
18532 int row_width, stretch_ascent, stretch_width;
18533 struct text_pos saved_pos;
18534 int saved_face_id, saved_avoid_cursor;
18535
18536 for (row_width = 0, g = row_start; g < row_end; g++)
18537 row_width += g->pixel_width;
18538 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18539 if (stretch_width > 0)
18540 {
18541 stretch_ascent =
18542 (((it->ascent + it->descent)
18543 * FONT_BASE (font)) / FONT_HEIGHT (font));
18544 saved_pos = it->position;
18545 memset (&it->position, 0, sizeof it->position);
18546 saved_avoid_cursor = it->avoid_cursor_p;
18547 it->avoid_cursor_p = 1;
18548 saved_face_id = it->face_id;
18549 /* The last row's stretch glyph should get the default
18550 face, to avoid painting the rest of the window with
18551 the region face, if the region ends at ZV. */
18552 if (it->glyph_row->ends_at_zv_p)
18553 it->face_id = default_face->id;
18554 else
18555 it->face_id = face->id;
18556 append_stretch_glyph (it, make_number (0), stretch_width,
18557 it->ascent + it->descent, stretch_ascent);
18558 it->position = saved_pos;
18559 it->avoid_cursor_p = saved_avoid_cursor;
18560 it->face_id = saved_face_id;
18561 }
18562 }
18563 #endif /* HAVE_WINDOW_SYSTEM */
18564 }
18565 else
18566 {
18567 /* Save some values that must not be changed. */
18568 int saved_x = it->current_x;
18569 struct text_pos saved_pos;
18570 Lisp_Object saved_object;
18571 enum display_element_type saved_what = it->what;
18572 int saved_face_id = it->face_id;
18573
18574 saved_object = it->object;
18575 saved_pos = it->position;
18576
18577 it->what = IT_CHARACTER;
18578 memset (&it->position, 0, sizeof it->position);
18579 it->object = make_number (0);
18580 it->c = it->char_to_display = ' ';
18581 it->len = 1;
18582 /* The last row's blank glyphs should get the default face, to
18583 avoid painting the rest of the window with the region face,
18584 if the region ends at ZV. */
18585 if (it->glyph_row->ends_at_zv_p)
18586 it->face_id = default_face->id;
18587 else
18588 it->face_id = face->id;
18589
18590 PRODUCE_GLYPHS (it);
18591
18592 while (it->current_x <= it->last_visible_x)
18593 PRODUCE_GLYPHS (it);
18594
18595 /* Don't count these blanks really. It would let us insert a left
18596 truncation glyph below and make us set the cursor on them, maybe. */
18597 it->current_x = saved_x;
18598 it->object = saved_object;
18599 it->position = saved_pos;
18600 it->what = saved_what;
18601 it->face_id = saved_face_id;
18602 }
18603 }
18604
18605
18606 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18607 trailing whitespace. */
18608
18609 static int
18610 trailing_whitespace_p (ptrdiff_t charpos)
18611 {
18612 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18613 int c = 0;
18614
18615 while (bytepos < ZV_BYTE
18616 && (c = FETCH_CHAR (bytepos),
18617 c == ' ' || c == '\t'))
18618 ++bytepos;
18619
18620 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18621 {
18622 if (bytepos != PT_BYTE)
18623 return 1;
18624 }
18625 return 0;
18626 }
18627
18628
18629 /* Highlight trailing whitespace, if any, in ROW. */
18630
18631 static void
18632 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18633 {
18634 int used = row->used[TEXT_AREA];
18635
18636 if (used)
18637 {
18638 struct glyph *start = row->glyphs[TEXT_AREA];
18639 struct glyph *glyph = start + used - 1;
18640
18641 if (row->reversed_p)
18642 {
18643 /* Right-to-left rows need to be processed in the opposite
18644 direction, so swap the edge pointers. */
18645 glyph = start;
18646 start = row->glyphs[TEXT_AREA] + used - 1;
18647 }
18648
18649 /* Skip over glyphs inserted to display the cursor at the
18650 end of a line, for extending the face of the last glyph
18651 to the end of the line on terminals, and for truncation
18652 and continuation glyphs. */
18653 if (!row->reversed_p)
18654 {
18655 while (glyph >= start
18656 && glyph->type == CHAR_GLYPH
18657 && INTEGERP (glyph->object))
18658 --glyph;
18659 }
18660 else
18661 {
18662 while (glyph <= start
18663 && glyph->type == CHAR_GLYPH
18664 && INTEGERP (glyph->object))
18665 ++glyph;
18666 }
18667
18668 /* If last glyph is a space or stretch, and it's trailing
18669 whitespace, set the face of all trailing whitespace glyphs in
18670 IT->glyph_row to `trailing-whitespace'. */
18671 if ((row->reversed_p ? glyph <= start : glyph >= start)
18672 && BUFFERP (glyph->object)
18673 && (glyph->type == STRETCH_GLYPH
18674 || (glyph->type == CHAR_GLYPH
18675 && glyph->u.ch == ' '))
18676 && trailing_whitespace_p (glyph->charpos))
18677 {
18678 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18679 if (face_id < 0)
18680 return;
18681
18682 if (!row->reversed_p)
18683 {
18684 while (glyph >= start
18685 && BUFFERP (glyph->object)
18686 && (glyph->type == STRETCH_GLYPH
18687 || (glyph->type == CHAR_GLYPH
18688 && glyph->u.ch == ' ')))
18689 (glyph--)->face_id = face_id;
18690 }
18691 else
18692 {
18693 while (glyph <= start
18694 && BUFFERP (glyph->object)
18695 && (glyph->type == STRETCH_GLYPH
18696 || (glyph->type == CHAR_GLYPH
18697 && glyph->u.ch == ' ')))
18698 (glyph++)->face_id = face_id;
18699 }
18700 }
18701 }
18702 }
18703
18704
18705 /* Value is non-zero if glyph row ROW should be
18706 used to hold the cursor. */
18707
18708 static int
18709 cursor_row_p (struct glyph_row *row)
18710 {
18711 int result = 1;
18712
18713 if (PT == CHARPOS (row->end.pos)
18714 || PT == MATRIX_ROW_END_CHARPOS (row))
18715 {
18716 /* Suppose the row ends on a string.
18717 Unless the row is continued, that means it ends on a newline
18718 in the string. If it's anything other than a display string
18719 (e.g., a before-string from an overlay), we don't want the
18720 cursor there. (This heuristic seems to give the optimal
18721 behavior for the various types of multi-line strings.)
18722 One exception: if the string has `cursor' property on one of
18723 its characters, we _do_ want the cursor there. */
18724 if (CHARPOS (row->end.string_pos) >= 0)
18725 {
18726 if (row->continued_p)
18727 result = 1;
18728 else
18729 {
18730 /* Check for `display' property. */
18731 struct glyph *beg = row->glyphs[TEXT_AREA];
18732 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18733 struct glyph *glyph;
18734
18735 result = 0;
18736 for (glyph = end; glyph >= beg; --glyph)
18737 if (STRINGP (glyph->object))
18738 {
18739 Lisp_Object prop
18740 = Fget_char_property (make_number (PT),
18741 Qdisplay, Qnil);
18742 result =
18743 (!NILP (prop)
18744 && display_prop_string_p (prop, glyph->object));
18745 /* If there's a `cursor' property on one of the
18746 string's characters, this row is a cursor row,
18747 even though this is not a display string. */
18748 if (!result)
18749 {
18750 Lisp_Object s = glyph->object;
18751
18752 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18753 {
18754 ptrdiff_t gpos = glyph->charpos;
18755
18756 if (!NILP (Fget_char_property (make_number (gpos),
18757 Qcursor, s)))
18758 {
18759 result = 1;
18760 break;
18761 }
18762 }
18763 }
18764 break;
18765 }
18766 }
18767 }
18768 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18769 {
18770 /* If the row ends in middle of a real character,
18771 and the line is continued, we want the cursor here.
18772 That's because CHARPOS (ROW->end.pos) would equal
18773 PT if PT is before the character. */
18774 if (!row->ends_in_ellipsis_p)
18775 result = row->continued_p;
18776 else
18777 /* If the row ends in an ellipsis, then
18778 CHARPOS (ROW->end.pos) will equal point after the
18779 invisible text. We want that position to be displayed
18780 after the ellipsis. */
18781 result = 0;
18782 }
18783 /* If the row ends at ZV, display the cursor at the end of that
18784 row instead of at the start of the row below. */
18785 else if (row->ends_at_zv_p)
18786 result = 1;
18787 else
18788 result = 0;
18789 }
18790
18791 return result;
18792 }
18793
18794 \f
18795
18796 /* Push the property PROP so that it will be rendered at the current
18797 position in IT. Return 1 if PROP was successfully pushed, 0
18798 otherwise. Called from handle_line_prefix to handle the
18799 `line-prefix' and `wrap-prefix' properties. */
18800
18801 static int
18802 push_prefix_prop (struct it *it, Lisp_Object prop)
18803 {
18804 struct text_pos pos =
18805 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18806
18807 eassert (it->method == GET_FROM_BUFFER
18808 || it->method == GET_FROM_DISPLAY_VECTOR
18809 || it->method == GET_FROM_STRING);
18810
18811 /* We need to save the current buffer/string position, so it will be
18812 restored by pop_it, because iterate_out_of_display_property
18813 depends on that being set correctly, but some situations leave
18814 it->position not yet set when this function is called. */
18815 push_it (it, &pos);
18816
18817 if (STRINGP (prop))
18818 {
18819 if (SCHARS (prop) == 0)
18820 {
18821 pop_it (it);
18822 return 0;
18823 }
18824
18825 it->string = prop;
18826 it->string_from_prefix_prop_p = 1;
18827 it->multibyte_p = STRING_MULTIBYTE (it->string);
18828 it->current.overlay_string_index = -1;
18829 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18830 it->end_charpos = it->string_nchars = SCHARS (it->string);
18831 it->method = GET_FROM_STRING;
18832 it->stop_charpos = 0;
18833 it->prev_stop = 0;
18834 it->base_level_stop = 0;
18835
18836 /* Force paragraph direction to be that of the parent
18837 buffer/string. */
18838 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18839 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18840 else
18841 it->paragraph_embedding = L2R;
18842
18843 /* Set up the bidi iterator for this display string. */
18844 if (it->bidi_p)
18845 {
18846 it->bidi_it.string.lstring = it->string;
18847 it->bidi_it.string.s = NULL;
18848 it->bidi_it.string.schars = it->end_charpos;
18849 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18850 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18851 it->bidi_it.string.unibyte = !it->multibyte_p;
18852 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18853 }
18854 }
18855 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18856 {
18857 it->method = GET_FROM_STRETCH;
18858 it->object = prop;
18859 }
18860 #ifdef HAVE_WINDOW_SYSTEM
18861 else if (IMAGEP (prop))
18862 {
18863 it->what = IT_IMAGE;
18864 it->image_id = lookup_image (it->f, prop);
18865 it->method = GET_FROM_IMAGE;
18866 }
18867 #endif /* HAVE_WINDOW_SYSTEM */
18868 else
18869 {
18870 pop_it (it); /* bogus display property, give up */
18871 return 0;
18872 }
18873
18874 return 1;
18875 }
18876
18877 /* Return the character-property PROP at the current position in IT. */
18878
18879 static Lisp_Object
18880 get_it_property (struct it *it, Lisp_Object prop)
18881 {
18882 Lisp_Object position;
18883
18884 if (STRINGP (it->object))
18885 position = make_number (IT_STRING_CHARPOS (*it));
18886 else if (BUFFERP (it->object))
18887 position = make_number (IT_CHARPOS (*it));
18888 else
18889 return Qnil;
18890
18891 return Fget_char_property (position, prop, it->object);
18892 }
18893
18894 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18895
18896 static void
18897 handle_line_prefix (struct it *it)
18898 {
18899 Lisp_Object prefix;
18900
18901 if (it->continuation_lines_width > 0)
18902 {
18903 prefix = get_it_property (it, Qwrap_prefix);
18904 if (NILP (prefix))
18905 prefix = Vwrap_prefix;
18906 }
18907 else
18908 {
18909 prefix = get_it_property (it, Qline_prefix);
18910 if (NILP (prefix))
18911 prefix = Vline_prefix;
18912 }
18913 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18914 {
18915 /* If the prefix is wider than the window, and we try to wrap
18916 it, it would acquire its own wrap prefix, and so on till the
18917 iterator stack overflows. So, don't wrap the prefix. */
18918 it->line_wrap = TRUNCATE;
18919 it->avoid_cursor_p = 1;
18920 }
18921 }
18922
18923 \f
18924
18925 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18926 only for R2L lines from display_line and display_string, when they
18927 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18928 the line/string needs to be continued on the next glyph row. */
18929 static void
18930 unproduce_glyphs (struct it *it, int n)
18931 {
18932 struct glyph *glyph, *end;
18933
18934 eassert (it->glyph_row);
18935 eassert (it->glyph_row->reversed_p);
18936 eassert (it->area == TEXT_AREA);
18937 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18938
18939 if (n > it->glyph_row->used[TEXT_AREA])
18940 n = it->glyph_row->used[TEXT_AREA];
18941 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18942 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18943 for ( ; glyph < end; glyph++)
18944 glyph[-n] = *glyph;
18945 }
18946
18947 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18948 and ROW->maxpos. */
18949 static void
18950 find_row_edges (struct it *it, struct glyph_row *row,
18951 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18952 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18953 {
18954 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18955 lines' rows is implemented for bidi-reordered rows. */
18956
18957 /* ROW->minpos is the value of min_pos, the minimal buffer position
18958 we have in ROW, or ROW->start.pos if that is smaller. */
18959 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18960 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18961 else
18962 /* We didn't find buffer positions smaller than ROW->start, or
18963 didn't find _any_ valid buffer positions in any of the glyphs,
18964 so we must trust the iterator's computed positions. */
18965 row->minpos = row->start.pos;
18966 if (max_pos <= 0)
18967 {
18968 max_pos = CHARPOS (it->current.pos);
18969 max_bpos = BYTEPOS (it->current.pos);
18970 }
18971
18972 /* Here are the various use-cases for ending the row, and the
18973 corresponding values for ROW->maxpos:
18974
18975 Line ends in a newline from buffer eol_pos + 1
18976 Line is continued from buffer max_pos + 1
18977 Line is truncated on right it->current.pos
18978 Line ends in a newline from string max_pos + 1(*)
18979 (*) + 1 only when line ends in a forward scan
18980 Line is continued from string max_pos
18981 Line is continued from display vector max_pos
18982 Line is entirely from a string min_pos == max_pos
18983 Line is entirely from a display vector min_pos == max_pos
18984 Line that ends at ZV ZV
18985
18986 If you discover other use-cases, please add them here as
18987 appropriate. */
18988 if (row->ends_at_zv_p)
18989 row->maxpos = it->current.pos;
18990 else if (row->used[TEXT_AREA])
18991 {
18992 int seen_this_string = 0;
18993 struct glyph_row *r1 = row - 1;
18994
18995 /* Did we see the same display string on the previous row? */
18996 if (STRINGP (it->object)
18997 /* this is not the first row */
18998 && row > it->w->desired_matrix->rows
18999 /* previous row is not the header line */
19000 && !r1->mode_line_p
19001 /* previous row also ends in a newline from a string */
19002 && r1->ends_in_newline_from_string_p)
19003 {
19004 struct glyph *start, *end;
19005
19006 /* Search for the last glyph of the previous row that came
19007 from buffer or string. Depending on whether the row is
19008 L2R or R2L, we need to process it front to back or the
19009 other way round. */
19010 if (!r1->reversed_p)
19011 {
19012 start = r1->glyphs[TEXT_AREA];
19013 end = start + r1->used[TEXT_AREA];
19014 /* Glyphs inserted by redisplay have an integer (zero)
19015 as their object. */
19016 while (end > start
19017 && INTEGERP ((end - 1)->object)
19018 && (end - 1)->charpos <= 0)
19019 --end;
19020 if (end > start)
19021 {
19022 if (EQ ((end - 1)->object, it->object))
19023 seen_this_string = 1;
19024 }
19025 else
19026 /* If all the glyphs of the previous row were inserted
19027 by redisplay, it means the previous row was
19028 produced from a single newline, which is only
19029 possible if that newline came from the same string
19030 as the one which produced this ROW. */
19031 seen_this_string = 1;
19032 }
19033 else
19034 {
19035 end = r1->glyphs[TEXT_AREA] - 1;
19036 start = end + r1->used[TEXT_AREA];
19037 while (end < start
19038 && INTEGERP ((end + 1)->object)
19039 && (end + 1)->charpos <= 0)
19040 ++end;
19041 if (end < start)
19042 {
19043 if (EQ ((end + 1)->object, it->object))
19044 seen_this_string = 1;
19045 }
19046 else
19047 seen_this_string = 1;
19048 }
19049 }
19050 /* Take note of each display string that covers a newline only
19051 once, the first time we see it. This is for when a display
19052 string includes more than one newline in it. */
19053 if (row->ends_in_newline_from_string_p && !seen_this_string)
19054 {
19055 /* If we were scanning the buffer forward when we displayed
19056 the string, we want to account for at least one buffer
19057 position that belongs to this row (position covered by
19058 the display string), so that cursor positioning will
19059 consider this row as a candidate when point is at the end
19060 of the visual line represented by this row. This is not
19061 required when scanning back, because max_pos will already
19062 have a much larger value. */
19063 if (CHARPOS (row->end.pos) > max_pos)
19064 INC_BOTH (max_pos, max_bpos);
19065 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19066 }
19067 else if (CHARPOS (it->eol_pos) > 0)
19068 SET_TEXT_POS (row->maxpos,
19069 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19070 else if (row->continued_p)
19071 {
19072 /* If max_pos is different from IT's current position, it
19073 means IT->method does not belong to the display element
19074 at max_pos. However, it also means that the display
19075 element at max_pos was displayed in its entirety on this
19076 line, which is equivalent to saying that the next line
19077 starts at the next buffer position. */
19078 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19079 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19080 else
19081 {
19082 INC_BOTH (max_pos, max_bpos);
19083 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19084 }
19085 }
19086 else if (row->truncated_on_right_p)
19087 /* display_line already called reseat_at_next_visible_line_start,
19088 which puts the iterator at the beginning of the next line, in
19089 the logical order. */
19090 row->maxpos = it->current.pos;
19091 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19092 /* A line that is entirely from a string/image/stretch... */
19093 row->maxpos = row->minpos;
19094 else
19095 abort ();
19096 }
19097 else
19098 row->maxpos = it->current.pos;
19099 }
19100
19101 /* Construct the glyph row IT->glyph_row in the desired matrix of
19102 IT->w from text at the current position of IT. See dispextern.h
19103 for an overview of struct it. Value is non-zero if
19104 IT->glyph_row displays text, as opposed to a line displaying ZV
19105 only. */
19106
19107 static int
19108 display_line (struct it *it)
19109 {
19110 struct glyph_row *row = it->glyph_row;
19111 Lisp_Object overlay_arrow_string;
19112 struct it wrap_it;
19113 void *wrap_data = NULL;
19114 int may_wrap = 0, wrap_x IF_LINT (= 0);
19115 int wrap_row_used = -1;
19116 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19117 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19118 int wrap_row_extra_line_spacing IF_LINT (= 0);
19119 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19120 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19121 int cvpos;
19122 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19123 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19124
19125 /* We always start displaying at hpos zero even if hscrolled. */
19126 eassert (it->hpos == 0 && it->current_x == 0);
19127
19128 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19129 >= it->w->desired_matrix->nrows)
19130 {
19131 it->w->nrows_scale_factor++;
19132 fonts_changed_p = 1;
19133 return 0;
19134 }
19135
19136 /* Is IT->w showing the region? */
19137 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19138
19139 /* Clear the result glyph row and enable it. */
19140 prepare_desired_row (row);
19141
19142 row->y = it->current_y;
19143 row->start = it->start;
19144 row->continuation_lines_width = it->continuation_lines_width;
19145 row->displays_text_p = 1;
19146 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19147 it->starts_in_middle_of_char_p = 0;
19148
19149 /* Arrange the overlays nicely for our purposes. Usually, we call
19150 display_line on only one line at a time, in which case this
19151 can't really hurt too much, or we call it on lines which appear
19152 one after another in the buffer, in which case all calls to
19153 recenter_overlay_lists but the first will be pretty cheap. */
19154 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19155
19156 /* Move over display elements that are not visible because we are
19157 hscrolled. This may stop at an x-position < IT->first_visible_x
19158 if the first glyph is partially visible or if we hit a line end. */
19159 if (it->current_x < it->first_visible_x)
19160 {
19161 this_line_min_pos = row->start.pos;
19162 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19163 MOVE_TO_POS | MOVE_TO_X);
19164 /* Record the smallest positions seen while we moved over
19165 display elements that are not visible. This is needed by
19166 redisplay_internal for optimizing the case where the cursor
19167 stays inside the same line. The rest of this function only
19168 considers positions that are actually displayed, so
19169 RECORD_MAX_MIN_POS will not otherwise record positions that
19170 are hscrolled to the left of the left edge of the window. */
19171 min_pos = CHARPOS (this_line_min_pos);
19172 min_bpos = BYTEPOS (this_line_min_pos);
19173 }
19174 else
19175 {
19176 /* We only do this when not calling `move_it_in_display_line_to'
19177 above, because move_it_in_display_line_to calls
19178 handle_line_prefix itself. */
19179 handle_line_prefix (it);
19180 }
19181
19182 /* Get the initial row height. This is either the height of the
19183 text hscrolled, if there is any, or zero. */
19184 row->ascent = it->max_ascent;
19185 row->height = it->max_ascent + it->max_descent;
19186 row->phys_ascent = it->max_phys_ascent;
19187 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19188 row->extra_line_spacing = it->max_extra_line_spacing;
19189
19190 /* Utility macro to record max and min buffer positions seen until now. */
19191 #define RECORD_MAX_MIN_POS(IT) \
19192 do \
19193 { \
19194 int composition_p = !STRINGP ((IT)->string) \
19195 && ((IT)->what == IT_COMPOSITION); \
19196 ptrdiff_t current_pos = \
19197 composition_p ? (IT)->cmp_it.charpos \
19198 : IT_CHARPOS (*(IT)); \
19199 ptrdiff_t current_bpos = \
19200 composition_p ? CHAR_TO_BYTE (current_pos) \
19201 : IT_BYTEPOS (*(IT)); \
19202 if (current_pos < min_pos) \
19203 { \
19204 min_pos = current_pos; \
19205 min_bpos = current_bpos; \
19206 } \
19207 if (IT_CHARPOS (*it) > max_pos) \
19208 { \
19209 max_pos = IT_CHARPOS (*it); \
19210 max_bpos = IT_BYTEPOS (*it); \
19211 } \
19212 } \
19213 while (0)
19214
19215 /* Loop generating characters. The loop is left with IT on the next
19216 character to display. */
19217 while (1)
19218 {
19219 int n_glyphs_before, hpos_before, x_before;
19220 int x, nglyphs;
19221 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19222
19223 /* Retrieve the next thing to display. Value is zero if end of
19224 buffer reached. */
19225 if (!get_next_display_element (it))
19226 {
19227 /* Maybe add a space at the end of this line that is used to
19228 display the cursor there under X. Set the charpos of the
19229 first glyph of blank lines not corresponding to any text
19230 to -1. */
19231 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19232 row->exact_window_width_line_p = 1;
19233 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19234 || row->used[TEXT_AREA] == 0)
19235 {
19236 row->glyphs[TEXT_AREA]->charpos = -1;
19237 row->displays_text_p = 0;
19238
19239 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19240 && (!MINI_WINDOW_P (it->w)
19241 || (minibuf_level && EQ (it->window, minibuf_window))))
19242 row->indicate_empty_line_p = 1;
19243 }
19244
19245 it->continuation_lines_width = 0;
19246 row->ends_at_zv_p = 1;
19247 /* A row that displays right-to-left text must always have
19248 its last face extended all the way to the end of line,
19249 even if this row ends in ZV, because we still write to
19250 the screen left to right. We also need to extend the
19251 last face if the default face is remapped to some
19252 different face, otherwise the functions that clear
19253 portions of the screen will clear with the default face's
19254 background color. */
19255 if (row->reversed_p
19256 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19257 extend_face_to_end_of_line (it);
19258 break;
19259 }
19260
19261 /* Now, get the metrics of what we want to display. This also
19262 generates glyphs in `row' (which is IT->glyph_row). */
19263 n_glyphs_before = row->used[TEXT_AREA];
19264 x = it->current_x;
19265
19266 /* Remember the line height so far in case the next element doesn't
19267 fit on the line. */
19268 if (it->line_wrap != TRUNCATE)
19269 {
19270 ascent = it->max_ascent;
19271 descent = it->max_descent;
19272 phys_ascent = it->max_phys_ascent;
19273 phys_descent = it->max_phys_descent;
19274
19275 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19276 {
19277 if (IT_DISPLAYING_WHITESPACE (it))
19278 may_wrap = 1;
19279 else if (may_wrap)
19280 {
19281 SAVE_IT (wrap_it, *it, wrap_data);
19282 wrap_x = x;
19283 wrap_row_used = row->used[TEXT_AREA];
19284 wrap_row_ascent = row->ascent;
19285 wrap_row_height = row->height;
19286 wrap_row_phys_ascent = row->phys_ascent;
19287 wrap_row_phys_height = row->phys_height;
19288 wrap_row_extra_line_spacing = row->extra_line_spacing;
19289 wrap_row_min_pos = min_pos;
19290 wrap_row_min_bpos = min_bpos;
19291 wrap_row_max_pos = max_pos;
19292 wrap_row_max_bpos = max_bpos;
19293 may_wrap = 0;
19294 }
19295 }
19296 }
19297
19298 PRODUCE_GLYPHS (it);
19299
19300 /* If this display element was in marginal areas, continue with
19301 the next one. */
19302 if (it->area != TEXT_AREA)
19303 {
19304 row->ascent = max (row->ascent, it->max_ascent);
19305 row->height = max (row->height, it->max_ascent + it->max_descent);
19306 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19307 row->phys_height = max (row->phys_height,
19308 it->max_phys_ascent + it->max_phys_descent);
19309 row->extra_line_spacing = max (row->extra_line_spacing,
19310 it->max_extra_line_spacing);
19311 set_iterator_to_next (it, 1);
19312 continue;
19313 }
19314
19315 /* Does the display element fit on the line? If we truncate
19316 lines, we should draw past the right edge of the window. If
19317 we don't truncate, we want to stop so that we can display the
19318 continuation glyph before the right margin. If lines are
19319 continued, there are two possible strategies for characters
19320 resulting in more than 1 glyph (e.g. tabs): Display as many
19321 glyphs as possible in this line and leave the rest for the
19322 continuation line, or display the whole element in the next
19323 line. Original redisplay did the former, so we do it also. */
19324 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19325 hpos_before = it->hpos;
19326 x_before = x;
19327
19328 if (/* Not a newline. */
19329 nglyphs > 0
19330 /* Glyphs produced fit entirely in the line. */
19331 && it->current_x < it->last_visible_x)
19332 {
19333 it->hpos += nglyphs;
19334 row->ascent = max (row->ascent, it->max_ascent);
19335 row->height = max (row->height, it->max_ascent + it->max_descent);
19336 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19337 row->phys_height = max (row->phys_height,
19338 it->max_phys_ascent + it->max_phys_descent);
19339 row->extra_line_spacing = max (row->extra_line_spacing,
19340 it->max_extra_line_spacing);
19341 if (it->current_x - it->pixel_width < it->first_visible_x)
19342 row->x = x - it->first_visible_x;
19343 /* Record the maximum and minimum buffer positions seen so
19344 far in glyphs that will be displayed by this row. */
19345 if (it->bidi_p)
19346 RECORD_MAX_MIN_POS (it);
19347 }
19348 else
19349 {
19350 int i, new_x;
19351 struct glyph *glyph;
19352
19353 for (i = 0; i < nglyphs; ++i, x = new_x)
19354 {
19355 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19356 new_x = x + glyph->pixel_width;
19357
19358 if (/* Lines are continued. */
19359 it->line_wrap != TRUNCATE
19360 && (/* Glyph doesn't fit on the line. */
19361 new_x > it->last_visible_x
19362 /* Or it fits exactly on a window system frame. */
19363 || (new_x == it->last_visible_x
19364 && FRAME_WINDOW_P (it->f))))
19365 {
19366 /* End of a continued line. */
19367
19368 if (it->hpos == 0
19369 || (new_x == it->last_visible_x
19370 && FRAME_WINDOW_P (it->f)))
19371 {
19372 /* Current glyph is the only one on the line or
19373 fits exactly on the line. We must continue
19374 the line because we can't draw the cursor
19375 after the glyph. */
19376 row->continued_p = 1;
19377 it->current_x = new_x;
19378 it->continuation_lines_width += new_x;
19379 ++it->hpos;
19380 if (i == nglyphs - 1)
19381 {
19382 /* If line-wrap is on, check if a previous
19383 wrap point was found. */
19384 if (wrap_row_used > 0
19385 /* Even if there is a previous wrap
19386 point, continue the line here as
19387 usual, if (i) the previous character
19388 was a space or tab AND (ii) the
19389 current character is not. */
19390 && (!may_wrap
19391 || IT_DISPLAYING_WHITESPACE (it)))
19392 goto back_to_wrap;
19393
19394 /* Record the maximum and minimum buffer
19395 positions seen so far in glyphs that will be
19396 displayed by this row. */
19397 if (it->bidi_p)
19398 RECORD_MAX_MIN_POS (it);
19399 set_iterator_to_next (it, 1);
19400 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19401 {
19402 if (!get_next_display_element (it))
19403 {
19404 row->exact_window_width_line_p = 1;
19405 it->continuation_lines_width = 0;
19406 row->continued_p = 0;
19407 row->ends_at_zv_p = 1;
19408 }
19409 else if (ITERATOR_AT_END_OF_LINE_P (it))
19410 {
19411 row->continued_p = 0;
19412 row->exact_window_width_line_p = 1;
19413 }
19414 }
19415 }
19416 else if (it->bidi_p)
19417 RECORD_MAX_MIN_POS (it);
19418 }
19419 else if (CHAR_GLYPH_PADDING_P (*glyph)
19420 && !FRAME_WINDOW_P (it->f))
19421 {
19422 /* A padding glyph that doesn't fit on this line.
19423 This means the whole character doesn't fit
19424 on the line. */
19425 if (row->reversed_p)
19426 unproduce_glyphs (it, row->used[TEXT_AREA]
19427 - n_glyphs_before);
19428 row->used[TEXT_AREA] = n_glyphs_before;
19429
19430 /* Fill the rest of the row with continuation
19431 glyphs like in 20.x. */
19432 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19433 < row->glyphs[1 + TEXT_AREA])
19434 produce_special_glyphs (it, IT_CONTINUATION);
19435
19436 row->continued_p = 1;
19437 it->current_x = x_before;
19438 it->continuation_lines_width += x_before;
19439
19440 /* Restore the height to what it was before the
19441 element not fitting on the line. */
19442 it->max_ascent = ascent;
19443 it->max_descent = descent;
19444 it->max_phys_ascent = phys_ascent;
19445 it->max_phys_descent = phys_descent;
19446 }
19447 else if (wrap_row_used > 0)
19448 {
19449 back_to_wrap:
19450 if (row->reversed_p)
19451 unproduce_glyphs (it,
19452 row->used[TEXT_AREA] - wrap_row_used);
19453 RESTORE_IT (it, &wrap_it, wrap_data);
19454 it->continuation_lines_width += wrap_x;
19455 row->used[TEXT_AREA] = wrap_row_used;
19456 row->ascent = wrap_row_ascent;
19457 row->height = wrap_row_height;
19458 row->phys_ascent = wrap_row_phys_ascent;
19459 row->phys_height = wrap_row_phys_height;
19460 row->extra_line_spacing = wrap_row_extra_line_spacing;
19461 min_pos = wrap_row_min_pos;
19462 min_bpos = wrap_row_min_bpos;
19463 max_pos = wrap_row_max_pos;
19464 max_bpos = wrap_row_max_bpos;
19465 row->continued_p = 1;
19466 row->ends_at_zv_p = 0;
19467 row->exact_window_width_line_p = 0;
19468 it->continuation_lines_width += x;
19469
19470 /* Make sure that a non-default face is extended
19471 up to the right margin of the window. */
19472 extend_face_to_end_of_line (it);
19473 }
19474 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19475 {
19476 /* A TAB that extends past the right edge of the
19477 window. This produces a single glyph on
19478 window system frames. We leave the glyph in
19479 this row and let it fill the row, but don't
19480 consume the TAB. */
19481 it->continuation_lines_width += it->last_visible_x;
19482 row->ends_in_middle_of_char_p = 1;
19483 row->continued_p = 1;
19484 glyph->pixel_width = it->last_visible_x - x;
19485 it->starts_in_middle_of_char_p = 1;
19486 }
19487 else
19488 {
19489 /* Something other than a TAB that draws past
19490 the right edge of the window. Restore
19491 positions to values before the element. */
19492 if (row->reversed_p)
19493 unproduce_glyphs (it, row->used[TEXT_AREA]
19494 - (n_glyphs_before + i));
19495 row->used[TEXT_AREA] = n_glyphs_before + i;
19496
19497 /* Display continuation glyphs. */
19498 if (!FRAME_WINDOW_P (it->f))
19499 produce_special_glyphs (it, IT_CONTINUATION);
19500 row->continued_p = 1;
19501
19502 it->current_x = x_before;
19503 it->continuation_lines_width += x;
19504 extend_face_to_end_of_line (it);
19505
19506 if (nglyphs > 1 && i > 0)
19507 {
19508 row->ends_in_middle_of_char_p = 1;
19509 it->starts_in_middle_of_char_p = 1;
19510 }
19511
19512 /* Restore the height to what it was before the
19513 element not fitting on the line. */
19514 it->max_ascent = ascent;
19515 it->max_descent = descent;
19516 it->max_phys_ascent = phys_ascent;
19517 it->max_phys_descent = phys_descent;
19518 }
19519
19520 break;
19521 }
19522 else if (new_x > it->first_visible_x)
19523 {
19524 /* Increment number of glyphs actually displayed. */
19525 ++it->hpos;
19526
19527 /* Record the maximum and minimum buffer positions
19528 seen so far in glyphs that will be displayed by
19529 this row. */
19530 if (it->bidi_p)
19531 RECORD_MAX_MIN_POS (it);
19532
19533 if (x < it->first_visible_x)
19534 /* Glyph is partially visible, i.e. row starts at
19535 negative X position. */
19536 row->x = x - it->first_visible_x;
19537 }
19538 else
19539 {
19540 /* Glyph is completely off the left margin of the
19541 window. This should not happen because of the
19542 move_it_in_display_line at the start of this
19543 function, unless the text display area of the
19544 window is empty. */
19545 eassert (it->first_visible_x <= it->last_visible_x);
19546 }
19547 }
19548 /* Even if this display element produced no glyphs at all,
19549 we want to record its position. */
19550 if (it->bidi_p && nglyphs == 0)
19551 RECORD_MAX_MIN_POS (it);
19552
19553 row->ascent = max (row->ascent, it->max_ascent);
19554 row->height = max (row->height, it->max_ascent + it->max_descent);
19555 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19556 row->phys_height = max (row->phys_height,
19557 it->max_phys_ascent + it->max_phys_descent);
19558 row->extra_line_spacing = max (row->extra_line_spacing,
19559 it->max_extra_line_spacing);
19560
19561 /* End of this display line if row is continued. */
19562 if (row->continued_p || row->ends_at_zv_p)
19563 break;
19564 }
19565
19566 at_end_of_line:
19567 /* Is this a line end? If yes, we're also done, after making
19568 sure that a non-default face is extended up to the right
19569 margin of the window. */
19570 if (ITERATOR_AT_END_OF_LINE_P (it))
19571 {
19572 int used_before = row->used[TEXT_AREA];
19573
19574 row->ends_in_newline_from_string_p = STRINGP (it->object);
19575
19576 /* Add a space at the end of the line that is used to
19577 display the cursor there. */
19578 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19579 append_space_for_newline (it, 0);
19580
19581 /* Extend the face to the end of the line. */
19582 extend_face_to_end_of_line (it);
19583
19584 /* Make sure we have the position. */
19585 if (used_before == 0)
19586 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19587
19588 /* Record the position of the newline, for use in
19589 find_row_edges. */
19590 it->eol_pos = it->current.pos;
19591
19592 /* Consume the line end. This skips over invisible lines. */
19593 set_iterator_to_next (it, 1);
19594 it->continuation_lines_width = 0;
19595 break;
19596 }
19597
19598 /* Proceed with next display element. Note that this skips
19599 over lines invisible because of selective display. */
19600 set_iterator_to_next (it, 1);
19601
19602 /* If we truncate lines, we are done when the last displayed
19603 glyphs reach past the right margin of the window. */
19604 if (it->line_wrap == TRUNCATE
19605 && (FRAME_WINDOW_P (it->f)
19606 ? (it->current_x >= it->last_visible_x)
19607 : (it->current_x > it->last_visible_x)))
19608 {
19609 /* Maybe add truncation glyphs. */
19610 if (!FRAME_WINDOW_P (it->f))
19611 {
19612 int i, n;
19613
19614 if (!row->reversed_p)
19615 {
19616 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19617 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19618 break;
19619 }
19620 else
19621 {
19622 for (i = 0; i < row->used[TEXT_AREA]; i++)
19623 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19624 break;
19625 /* Remove any padding glyphs at the front of ROW, to
19626 make room for the truncation glyphs we will be
19627 adding below. The loop below always inserts at
19628 least one truncation glyph, so also remove the
19629 last glyph added to ROW. */
19630 unproduce_glyphs (it, i + 1);
19631 /* Adjust i for the loop below. */
19632 i = row->used[TEXT_AREA] - (i + 1);
19633 }
19634
19635 for (n = row->used[TEXT_AREA]; i < n; ++i)
19636 {
19637 row->used[TEXT_AREA] = i;
19638 produce_special_glyphs (it, IT_TRUNCATION);
19639 }
19640 }
19641 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19642 {
19643 /* Don't truncate if we can overflow newline into fringe. */
19644 if (!get_next_display_element (it))
19645 {
19646 it->continuation_lines_width = 0;
19647 row->ends_at_zv_p = 1;
19648 row->exact_window_width_line_p = 1;
19649 break;
19650 }
19651 if (ITERATOR_AT_END_OF_LINE_P (it))
19652 {
19653 row->exact_window_width_line_p = 1;
19654 goto at_end_of_line;
19655 }
19656 }
19657
19658 row->truncated_on_right_p = 1;
19659 it->continuation_lines_width = 0;
19660 reseat_at_next_visible_line_start (it, 0);
19661 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19662 it->hpos = hpos_before;
19663 it->current_x = x_before;
19664 break;
19665 }
19666 }
19667
19668 if (wrap_data)
19669 bidi_unshelve_cache (wrap_data, 1);
19670
19671 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19672 at the left window margin. */
19673 if (it->first_visible_x
19674 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19675 {
19676 if (!FRAME_WINDOW_P (it->f))
19677 insert_left_trunc_glyphs (it);
19678 row->truncated_on_left_p = 1;
19679 }
19680
19681 /* Remember the position at which this line ends.
19682
19683 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19684 cannot be before the call to find_row_edges below, since that is
19685 where these positions are determined. */
19686 row->end = it->current;
19687 if (!it->bidi_p)
19688 {
19689 row->minpos = row->start.pos;
19690 row->maxpos = row->end.pos;
19691 }
19692 else
19693 {
19694 /* ROW->minpos and ROW->maxpos must be the smallest and
19695 `1 + the largest' buffer positions in ROW. But if ROW was
19696 bidi-reordered, these two positions can be anywhere in the
19697 row, so we must determine them now. */
19698 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19699 }
19700
19701 /* If the start of this line is the overlay arrow-position, then
19702 mark this glyph row as the one containing the overlay arrow.
19703 This is clearly a mess with variable size fonts. It would be
19704 better to let it be displayed like cursors under X. */
19705 if ((row->displays_text_p || !overlay_arrow_seen)
19706 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19707 !NILP (overlay_arrow_string)))
19708 {
19709 /* Overlay arrow in window redisplay is a fringe bitmap. */
19710 if (STRINGP (overlay_arrow_string))
19711 {
19712 struct glyph_row *arrow_row
19713 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19714 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19715 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19716 struct glyph *p = row->glyphs[TEXT_AREA];
19717 struct glyph *p2, *end;
19718
19719 /* Copy the arrow glyphs. */
19720 while (glyph < arrow_end)
19721 *p++ = *glyph++;
19722
19723 /* Throw away padding glyphs. */
19724 p2 = p;
19725 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19726 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19727 ++p2;
19728 if (p2 > p)
19729 {
19730 while (p2 < end)
19731 *p++ = *p2++;
19732 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19733 }
19734 }
19735 else
19736 {
19737 eassert (INTEGERP (overlay_arrow_string));
19738 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19739 }
19740 overlay_arrow_seen = 1;
19741 }
19742
19743 /* Highlight trailing whitespace. */
19744 if (!NILP (Vshow_trailing_whitespace))
19745 highlight_trailing_whitespace (it->f, it->glyph_row);
19746
19747 /* Compute pixel dimensions of this line. */
19748 compute_line_metrics (it);
19749
19750 /* Implementation note: No changes in the glyphs of ROW or in their
19751 faces can be done past this point, because compute_line_metrics
19752 computes ROW's hash value and stores it within the glyph_row
19753 structure. */
19754
19755 /* Record whether this row ends inside an ellipsis. */
19756 row->ends_in_ellipsis_p
19757 = (it->method == GET_FROM_DISPLAY_VECTOR
19758 && it->ellipsis_p);
19759
19760 /* Save fringe bitmaps in this row. */
19761 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19762 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19763 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19764 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19765
19766 it->left_user_fringe_bitmap = 0;
19767 it->left_user_fringe_face_id = 0;
19768 it->right_user_fringe_bitmap = 0;
19769 it->right_user_fringe_face_id = 0;
19770
19771 /* Maybe set the cursor. */
19772 cvpos = it->w->cursor.vpos;
19773 if ((cvpos < 0
19774 /* In bidi-reordered rows, keep checking for proper cursor
19775 position even if one has been found already, because buffer
19776 positions in such rows change non-linearly with ROW->VPOS,
19777 when a line is continued. One exception: when we are at ZV,
19778 display cursor on the first suitable glyph row, since all
19779 the empty rows after that also have their position set to ZV. */
19780 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19781 lines' rows is implemented for bidi-reordered rows. */
19782 || (it->bidi_p
19783 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19784 && PT >= MATRIX_ROW_START_CHARPOS (row)
19785 && PT <= MATRIX_ROW_END_CHARPOS (row)
19786 && cursor_row_p (row))
19787 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19788
19789 /* Prepare for the next line. This line starts horizontally at (X
19790 HPOS) = (0 0). Vertical positions are incremented. As a
19791 convenience for the caller, IT->glyph_row is set to the next
19792 row to be used. */
19793 it->current_x = it->hpos = 0;
19794 it->current_y += row->height;
19795 SET_TEXT_POS (it->eol_pos, 0, 0);
19796 ++it->vpos;
19797 ++it->glyph_row;
19798 /* The next row should by default use the same value of the
19799 reversed_p flag as this one. set_iterator_to_next decides when
19800 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19801 the flag accordingly. */
19802 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19803 it->glyph_row->reversed_p = row->reversed_p;
19804 it->start = row->end;
19805 return row->displays_text_p;
19806
19807 #undef RECORD_MAX_MIN_POS
19808 }
19809
19810 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19811 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19812 doc: /* Return paragraph direction at point in BUFFER.
19813 Value is either `left-to-right' or `right-to-left'.
19814 If BUFFER is omitted or nil, it defaults to the current buffer.
19815
19816 Paragraph direction determines how the text in the paragraph is displayed.
19817 In left-to-right paragraphs, text begins at the left margin of the window
19818 and the reading direction is generally left to right. In right-to-left
19819 paragraphs, text begins at the right margin and is read from right to left.
19820
19821 See also `bidi-paragraph-direction'. */)
19822 (Lisp_Object buffer)
19823 {
19824 struct buffer *buf = current_buffer;
19825 struct buffer *old = buf;
19826
19827 if (! NILP (buffer))
19828 {
19829 CHECK_BUFFER (buffer);
19830 buf = XBUFFER (buffer);
19831 }
19832
19833 if (NILP (BVAR (buf, bidi_display_reordering))
19834 || NILP (BVAR (buf, enable_multibyte_characters))
19835 /* When we are loading loadup.el, the character property tables
19836 needed for bidi iteration are not yet available. */
19837 || !NILP (Vpurify_flag))
19838 return Qleft_to_right;
19839 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19840 return BVAR (buf, bidi_paragraph_direction);
19841 else
19842 {
19843 /* Determine the direction from buffer text. We could try to
19844 use current_matrix if it is up to date, but this seems fast
19845 enough as it is. */
19846 struct bidi_it itb;
19847 ptrdiff_t pos = BUF_PT (buf);
19848 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19849 int c;
19850 void *itb_data = bidi_shelve_cache ();
19851
19852 set_buffer_temp (buf);
19853 /* bidi_paragraph_init finds the base direction of the paragraph
19854 by searching forward from paragraph start. We need the base
19855 direction of the current or _previous_ paragraph, so we need
19856 to make sure we are within that paragraph. To that end, find
19857 the previous non-empty line. */
19858 if (pos >= ZV && pos > BEGV)
19859 {
19860 pos--;
19861 bytepos = CHAR_TO_BYTE (pos);
19862 }
19863 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19864 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19865 {
19866 while ((c = FETCH_BYTE (bytepos)) == '\n'
19867 || c == ' ' || c == '\t' || c == '\f')
19868 {
19869 if (bytepos <= BEGV_BYTE)
19870 break;
19871 bytepos--;
19872 pos--;
19873 }
19874 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19875 bytepos--;
19876 }
19877 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19878 itb.paragraph_dir = NEUTRAL_DIR;
19879 itb.string.s = NULL;
19880 itb.string.lstring = Qnil;
19881 itb.string.bufpos = 0;
19882 itb.string.unibyte = 0;
19883 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19884 bidi_unshelve_cache (itb_data, 0);
19885 set_buffer_temp (old);
19886 switch (itb.paragraph_dir)
19887 {
19888 case L2R:
19889 return Qleft_to_right;
19890 break;
19891 case R2L:
19892 return Qright_to_left;
19893 break;
19894 default:
19895 abort ();
19896 }
19897 }
19898 }
19899
19900
19901 \f
19902 /***********************************************************************
19903 Menu Bar
19904 ***********************************************************************/
19905
19906 /* Redisplay the menu bar in the frame for window W.
19907
19908 The menu bar of X frames that don't have X toolkit support is
19909 displayed in a special window W->frame->menu_bar_window.
19910
19911 The menu bar of terminal frames is treated specially as far as
19912 glyph matrices are concerned. Menu bar lines are not part of
19913 windows, so the update is done directly on the frame matrix rows
19914 for the menu bar. */
19915
19916 static void
19917 display_menu_bar (struct window *w)
19918 {
19919 struct frame *f = XFRAME (WINDOW_FRAME (w));
19920 struct it it;
19921 Lisp_Object items;
19922 int i;
19923
19924 /* Don't do all this for graphical frames. */
19925 #ifdef HAVE_NTGUI
19926 if (FRAME_W32_P (f))
19927 return;
19928 #endif
19929 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19930 if (FRAME_X_P (f))
19931 return;
19932 #endif
19933
19934 #ifdef HAVE_NS
19935 if (FRAME_NS_P (f))
19936 return;
19937 #endif /* HAVE_NS */
19938
19939 #ifdef USE_X_TOOLKIT
19940 eassert (!FRAME_WINDOW_P (f));
19941 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19942 it.first_visible_x = 0;
19943 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19944 #else /* not USE_X_TOOLKIT */
19945 if (FRAME_WINDOW_P (f))
19946 {
19947 /* Menu bar lines are displayed in the desired matrix of the
19948 dummy window menu_bar_window. */
19949 struct window *menu_w;
19950 eassert (WINDOWP (f->menu_bar_window));
19951 menu_w = XWINDOW (f->menu_bar_window);
19952 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19953 MENU_FACE_ID);
19954 it.first_visible_x = 0;
19955 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19956 }
19957 else
19958 {
19959 /* This is a TTY frame, i.e. character hpos/vpos are used as
19960 pixel x/y. */
19961 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19962 MENU_FACE_ID);
19963 it.first_visible_x = 0;
19964 it.last_visible_x = FRAME_COLS (f);
19965 }
19966 #endif /* not USE_X_TOOLKIT */
19967
19968 /* FIXME: This should be controlled by a user option. See the
19969 comments in redisplay_tool_bar and display_mode_line about
19970 this. */
19971 it.paragraph_embedding = L2R;
19972
19973 if (! mode_line_inverse_video)
19974 /* Force the menu-bar to be displayed in the default face. */
19975 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19976
19977 /* Clear all rows of the menu bar. */
19978 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19979 {
19980 struct glyph_row *row = it.glyph_row + i;
19981 clear_glyph_row (row);
19982 row->enabled_p = 1;
19983 row->full_width_p = 1;
19984 }
19985
19986 /* Display all items of the menu bar. */
19987 items = FRAME_MENU_BAR_ITEMS (it.f);
19988 for (i = 0; i < ASIZE (items); i += 4)
19989 {
19990 Lisp_Object string;
19991
19992 /* Stop at nil string. */
19993 string = AREF (items, i + 1);
19994 if (NILP (string))
19995 break;
19996
19997 /* Remember where item was displayed. */
19998 ASET (items, i + 3, make_number (it.hpos));
19999
20000 /* Display the item, pad with one space. */
20001 if (it.current_x < it.last_visible_x)
20002 display_string (NULL, string, Qnil, 0, 0, &it,
20003 SCHARS (string) + 1, 0, 0, -1);
20004 }
20005
20006 /* Fill out the line with spaces. */
20007 if (it.current_x < it.last_visible_x)
20008 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20009
20010 /* Compute the total height of the lines. */
20011 compute_line_metrics (&it);
20012 }
20013
20014
20015 \f
20016 /***********************************************************************
20017 Mode Line
20018 ***********************************************************************/
20019
20020 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20021 FORCE is non-zero, redisplay mode lines unconditionally.
20022 Otherwise, redisplay only mode lines that are garbaged. Value is
20023 the number of windows whose mode lines were redisplayed. */
20024
20025 static int
20026 redisplay_mode_lines (Lisp_Object window, int force)
20027 {
20028 int nwindows = 0;
20029
20030 while (!NILP (window))
20031 {
20032 struct window *w = XWINDOW (window);
20033
20034 if (WINDOWP (w->hchild))
20035 nwindows += redisplay_mode_lines (w->hchild, force);
20036 else if (WINDOWP (w->vchild))
20037 nwindows += redisplay_mode_lines (w->vchild, force);
20038 else if (force
20039 || FRAME_GARBAGED_P (XFRAME (w->frame))
20040 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20041 {
20042 struct text_pos lpoint;
20043 struct buffer *old = current_buffer;
20044
20045 /* Set the window's buffer for the mode line display. */
20046 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20047 set_buffer_internal_1 (XBUFFER (w->buffer));
20048
20049 /* Point refers normally to the selected window. For any
20050 other window, set up appropriate value. */
20051 if (!EQ (window, selected_window))
20052 {
20053 struct text_pos pt;
20054
20055 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20056 if (CHARPOS (pt) < BEGV)
20057 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20058 else if (CHARPOS (pt) > (ZV - 1))
20059 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20060 else
20061 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20062 }
20063
20064 /* Display mode lines. */
20065 clear_glyph_matrix (w->desired_matrix);
20066 if (display_mode_lines (w))
20067 {
20068 ++nwindows;
20069 w->must_be_updated_p = 1;
20070 }
20071
20072 /* Restore old settings. */
20073 set_buffer_internal_1 (old);
20074 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20075 }
20076
20077 window = w->next;
20078 }
20079
20080 return nwindows;
20081 }
20082
20083
20084 /* Display the mode and/or header line of window W. Value is the
20085 sum number of mode lines and header lines displayed. */
20086
20087 static int
20088 display_mode_lines (struct window *w)
20089 {
20090 Lisp_Object old_selected_window, old_selected_frame;
20091 int n = 0;
20092
20093 old_selected_frame = selected_frame;
20094 selected_frame = w->frame;
20095 old_selected_window = selected_window;
20096 XSETWINDOW (selected_window, w);
20097
20098 /* These will be set while the mode line specs are processed. */
20099 line_number_displayed = 0;
20100 w->column_number_displayed = Qnil;
20101
20102 if (WINDOW_WANTS_MODELINE_P (w))
20103 {
20104 struct window *sel_w = XWINDOW (old_selected_window);
20105
20106 /* Select mode line face based on the real selected window. */
20107 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20108 BVAR (current_buffer, mode_line_format));
20109 ++n;
20110 }
20111
20112 if (WINDOW_WANTS_HEADER_LINE_P (w))
20113 {
20114 display_mode_line (w, HEADER_LINE_FACE_ID,
20115 BVAR (current_buffer, header_line_format));
20116 ++n;
20117 }
20118
20119 selected_frame = old_selected_frame;
20120 selected_window = old_selected_window;
20121 return n;
20122 }
20123
20124
20125 /* Display mode or header line of window W. FACE_ID specifies which
20126 line to display; it is either MODE_LINE_FACE_ID or
20127 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20128 display. Value is the pixel height of the mode/header line
20129 displayed. */
20130
20131 static int
20132 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20133 {
20134 struct it it;
20135 struct face *face;
20136 ptrdiff_t count = SPECPDL_INDEX ();
20137
20138 init_iterator (&it, w, -1, -1, NULL, face_id);
20139 /* Don't extend on a previously drawn mode-line.
20140 This may happen if called from pos_visible_p. */
20141 it.glyph_row->enabled_p = 0;
20142 prepare_desired_row (it.glyph_row);
20143
20144 it.glyph_row->mode_line_p = 1;
20145
20146 if (! mode_line_inverse_video)
20147 /* Force the mode-line to be displayed in the default face. */
20148 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20149
20150 /* FIXME: This should be controlled by a user option. But
20151 supporting such an option is not trivial, since the mode line is
20152 made up of many separate strings. */
20153 it.paragraph_embedding = L2R;
20154
20155 record_unwind_protect (unwind_format_mode_line,
20156 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20157
20158 mode_line_target = MODE_LINE_DISPLAY;
20159
20160 /* Temporarily make frame's keyboard the current kboard so that
20161 kboard-local variables in the mode_line_format will get the right
20162 values. */
20163 push_kboard (FRAME_KBOARD (it.f));
20164 record_unwind_save_match_data ();
20165 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20166 pop_kboard ();
20167
20168 unbind_to (count, Qnil);
20169
20170 /* Fill up with spaces. */
20171 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20172
20173 compute_line_metrics (&it);
20174 it.glyph_row->full_width_p = 1;
20175 it.glyph_row->continued_p = 0;
20176 it.glyph_row->truncated_on_left_p = 0;
20177 it.glyph_row->truncated_on_right_p = 0;
20178
20179 /* Make a 3D mode-line have a shadow at its right end. */
20180 face = FACE_FROM_ID (it.f, face_id);
20181 extend_face_to_end_of_line (&it);
20182 if (face->box != FACE_NO_BOX)
20183 {
20184 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20185 + it.glyph_row->used[TEXT_AREA] - 1);
20186 last->right_box_line_p = 1;
20187 }
20188
20189 return it.glyph_row->height;
20190 }
20191
20192 /* Move element ELT in LIST to the front of LIST.
20193 Return the updated list. */
20194
20195 static Lisp_Object
20196 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20197 {
20198 register Lisp_Object tail, prev;
20199 register Lisp_Object tem;
20200
20201 tail = list;
20202 prev = Qnil;
20203 while (CONSP (tail))
20204 {
20205 tem = XCAR (tail);
20206
20207 if (EQ (elt, tem))
20208 {
20209 /* Splice out the link TAIL. */
20210 if (NILP (prev))
20211 list = XCDR (tail);
20212 else
20213 Fsetcdr (prev, XCDR (tail));
20214
20215 /* Now make it the first. */
20216 Fsetcdr (tail, list);
20217 return tail;
20218 }
20219 else
20220 prev = tail;
20221 tail = XCDR (tail);
20222 QUIT;
20223 }
20224
20225 /* Not found--return unchanged LIST. */
20226 return list;
20227 }
20228
20229 /* Contribute ELT to the mode line for window IT->w. How it
20230 translates into text depends on its data type.
20231
20232 IT describes the display environment in which we display, as usual.
20233
20234 DEPTH is the depth in recursion. It is used to prevent
20235 infinite recursion here.
20236
20237 FIELD_WIDTH is the number of characters the display of ELT should
20238 occupy in the mode line, and PRECISION is the maximum number of
20239 characters to display from ELT's representation. See
20240 display_string for details.
20241
20242 Returns the hpos of the end of the text generated by ELT.
20243
20244 PROPS is a property list to add to any string we encounter.
20245
20246 If RISKY is nonzero, remove (disregard) any properties in any string
20247 we encounter, and ignore :eval and :propertize.
20248
20249 The global variable `mode_line_target' determines whether the
20250 output is passed to `store_mode_line_noprop',
20251 `store_mode_line_string', or `display_string'. */
20252
20253 static int
20254 display_mode_element (struct it *it, int depth, int field_width, int precision,
20255 Lisp_Object elt, Lisp_Object props, int risky)
20256 {
20257 int n = 0, field, prec;
20258 int literal = 0;
20259
20260 tail_recurse:
20261 if (depth > 100)
20262 elt = build_string ("*too-deep*");
20263
20264 depth++;
20265
20266 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20267 {
20268 case Lisp_String:
20269 {
20270 /* A string: output it and check for %-constructs within it. */
20271 unsigned char c;
20272 ptrdiff_t offset = 0;
20273
20274 if (SCHARS (elt) > 0
20275 && (!NILP (props) || risky))
20276 {
20277 Lisp_Object oprops, aelt;
20278 oprops = Ftext_properties_at (make_number (0), elt);
20279
20280 /* If the starting string's properties are not what
20281 we want, translate the string. Also, if the string
20282 is risky, do that anyway. */
20283
20284 if (NILP (Fequal (props, oprops)) || risky)
20285 {
20286 /* If the starting string has properties,
20287 merge the specified ones onto the existing ones. */
20288 if (! NILP (oprops) && !risky)
20289 {
20290 Lisp_Object tem;
20291
20292 oprops = Fcopy_sequence (oprops);
20293 tem = props;
20294 while (CONSP (tem))
20295 {
20296 oprops = Fplist_put (oprops, XCAR (tem),
20297 XCAR (XCDR (tem)));
20298 tem = XCDR (XCDR (tem));
20299 }
20300 props = oprops;
20301 }
20302
20303 aelt = Fassoc (elt, mode_line_proptrans_alist);
20304 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20305 {
20306 /* AELT is what we want. Move it to the front
20307 without consing. */
20308 elt = XCAR (aelt);
20309 mode_line_proptrans_alist
20310 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20311 }
20312 else
20313 {
20314 Lisp_Object tem;
20315
20316 /* If AELT has the wrong props, it is useless.
20317 so get rid of it. */
20318 if (! NILP (aelt))
20319 mode_line_proptrans_alist
20320 = Fdelq (aelt, mode_line_proptrans_alist);
20321
20322 elt = Fcopy_sequence (elt);
20323 Fset_text_properties (make_number (0), Flength (elt),
20324 props, elt);
20325 /* Add this item to mode_line_proptrans_alist. */
20326 mode_line_proptrans_alist
20327 = Fcons (Fcons (elt, props),
20328 mode_line_proptrans_alist);
20329 /* Truncate mode_line_proptrans_alist
20330 to at most 50 elements. */
20331 tem = Fnthcdr (make_number (50),
20332 mode_line_proptrans_alist);
20333 if (! NILP (tem))
20334 XSETCDR (tem, Qnil);
20335 }
20336 }
20337 }
20338
20339 offset = 0;
20340
20341 if (literal)
20342 {
20343 prec = precision - n;
20344 switch (mode_line_target)
20345 {
20346 case MODE_LINE_NOPROP:
20347 case MODE_LINE_TITLE:
20348 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20349 break;
20350 case MODE_LINE_STRING:
20351 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20352 break;
20353 case MODE_LINE_DISPLAY:
20354 n += display_string (NULL, elt, Qnil, 0, 0, it,
20355 0, prec, 0, STRING_MULTIBYTE (elt));
20356 break;
20357 }
20358
20359 break;
20360 }
20361
20362 /* Handle the non-literal case. */
20363
20364 while ((precision <= 0 || n < precision)
20365 && SREF (elt, offset) != 0
20366 && (mode_line_target != MODE_LINE_DISPLAY
20367 || it->current_x < it->last_visible_x))
20368 {
20369 ptrdiff_t last_offset = offset;
20370
20371 /* Advance to end of string or next format specifier. */
20372 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20373 ;
20374
20375 if (offset - 1 != last_offset)
20376 {
20377 ptrdiff_t nchars, nbytes;
20378
20379 /* Output to end of string or up to '%'. Field width
20380 is length of string. Don't output more than
20381 PRECISION allows us. */
20382 offset--;
20383
20384 prec = c_string_width (SDATA (elt) + last_offset,
20385 offset - last_offset, precision - n,
20386 &nchars, &nbytes);
20387
20388 switch (mode_line_target)
20389 {
20390 case MODE_LINE_NOPROP:
20391 case MODE_LINE_TITLE:
20392 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20393 break;
20394 case MODE_LINE_STRING:
20395 {
20396 ptrdiff_t bytepos = last_offset;
20397 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20398 ptrdiff_t endpos = (precision <= 0
20399 ? string_byte_to_char (elt, offset)
20400 : charpos + nchars);
20401
20402 n += store_mode_line_string (NULL,
20403 Fsubstring (elt, make_number (charpos),
20404 make_number (endpos)),
20405 0, 0, 0, Qnil);
20406 }
20407 break;
20408 case MODE_LINE_DISPLAY:
20409 {
20410 ptrdiff_t bytepos = last_offset;
20411 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20412
20413 if (precision <= 0)
20414 nchars = string_byte_to_char (elt, offset) - charpos;
20415 n += display_string (NULL, elt, Qnil, 0, charpos,
20416 it, 0, nchars, 0,
20417 STRING_MULTIBYTE (elt));
20418 }
20419 break;
20420 }
20421 }
20422 else /* c == '%' */
20423 {
20424 ptrdiff_t percent_position = offset;
20425
20426 /* Get the specified minimum width. Zero means
20427 don't pad. */
20428 field = 0;
20429 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20430 field = field * 10 + c - '0';
20431
20432 /* Don't pad beyond the total padding allowed. */
20433 if (field_width - n > 0 && field > field_width - n)
20434 field = field_width - n;
20435
20436 /* Note that either PRECISION <= 0 or N < PRECISION. */
20437 prec = precision - n;
20438
20439 if (c == 'M')
20440 n += display_mode_element (it, depth, field, prec,
20441 Vglobal_mode_string, props,
20442 risky);
20443 else if (c != 0)
20444 {
20445 int multibyte;
20446 ptrdiff_t bytepos, charpos;
20447 const char *spec;
20448 Lisp_Object string;
20449
20450 bytepos = percent_position;
20451 charpos = (STRING_MULTIBYTE (elt)
20452 ? string_byte_to_char (elt, bytepos)
20453 : bytepos);
20454 spec = decode_mode_spec (it->w, c, field, &string);
20455 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20456
20457 switch (mode_line_target)
20458 {
20459 case MODE_LINE_NOPROP:
20460 case MODE_LINE_TITLE:
20461 n += store_mode_line_noprop (spec, field, prec);
20462 break;
20463 case MODE_LINE_STRING:
20464 {
20465 Lisp_Object tem = build_string (spec);
20466 props = Ftext_properties_at (make_number (charpos), elt);
20467 /* Should only keep face property in props */
20468 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20469 }
20470 break;
20471 case MODE_LINE_DISPLAY:
20472 {
20473 int nglyphs_before, nwritten;
20474
20475 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20476 nwritten = display_string (spec, string, elt,
20477 charpos, 0, it,
20478 field, prec, 0,
20479 multibyte);
20480
20481 /* Assign to the glyphs written above the
20482 string where the `%x' came from, position
20483 of the `%'. */
20484 if (nwritten > 0)
20485 {
20486 struct glyph *glyph
20487 = (it->glyph_row->glyphs[TEXT_AREA]
20488 + nglyphs_before);
20489 int i;
20490
20491 for (i = 0; i < nwritten; ++i)
20492 {
20493 glyph[i].object = elt;
20494 glyph[i].charpos = charpos;
20495 }
20496
20497 n += nwritten;
20498 }
20499 }
20500 break;
20501 }
20502 }
20503 else /* c == 0 */
20504 break;
20505 }
20506 }
20507 }
20508 break;
20509
20510 case Lisp_Symbol:
20511 /* A symbol: process the value of the symbol recursively
20512 as if it appeared here directly. Avoid error if symbol void.
20513 Special case: if value of symbol is a string, output the string
20514 literally. */
20515 {
20516 register Lisp_Object tem;
20517
20518 /* If the variable is not marked as risky to set
20519 then its contents are risky to use. */
20520 if (NILP (Fget (elt, Qrisky_local_variable)))
20521 risky = 1;
20522
20523 tem = Fboundp (elt);
20524 if (!NILP (tem))
20525 {
20526 tem = Fsymbol_value (elt);
20527 /* If value is a string, output that string literally:
20528 don't check for % within it. */
20529 if (STRINGP (tem))
20530 literal = 1;
20531
20532 if (!EQ (tem, elt))
20533 {
20534 /* Give up right away for nil or t. */
20535 elt = tem;
20536 goto tail_recurse;
20537 }
20538 }
20539 }
20540 break;
20541
20542 case Lisp_Cons:
20543 {
20544 register Lisp_Object car, tem;
20545
20546 /* A cons cell: five distinct cases.
20547 If first element is :eval or :propertize, do something special.
20548 If first element is a string or a cons, process all the elements
20549 and effectively concatenate them.
20550 If first element is a negative number, truncate displaying cdr to
20551 at most that many characters. If positive, pad (with spaces)
20552 to at least that many characters.
20553 If first element is a symbol, process the cadr or caddr recursively
20554 according to whether the symbol's value is non-nil or nil. */
20555 car = XCAR (elt);
20556 if (EQ (car, QCeval))
20557 {
20558 /* An element of the form (:eval FORM) means evaluate FORM
20559 and use the result as mode line elements. */
20560
20561 if (risky)
20562 break;
20563
20564 if (CONSP (XCDR (elt)))
20565 {
20566 Lisp_Object spec;
20567 spec = safe_eval (XCAR (XCDR (elt)));
20568 n += display_mode_element (it, depth, field_width - n,
20569 precision - n, spec, props,
20570 risky);
20571 }
20572 }
20573 else if (EQ (car, QCpropertize))
20574 {
20575 /* An element of the form (:propertize ELT PROPS...)
20576 means display ELT but applying properties PROPS. */
20577
20578 if (risky)
20579 break;
20580
20581 if (CONSP (XCDR (elt)))
20582 n += display_mode_element (it, depth, field_width - n,
20583 precision - n, XCAR (XCDR (elt)),
20584 XCDR (XCDR (elt)), risky);
20585 }
20586 else if (SYMBOLP (car))
20587 {
20588 tem = Fboundp (car);
20589 elt = XCDR (elt);
20590 if (!CONSP (elt))
20591 goto invalid;
20592 /* elt is now the cdr, and we know it is a cons cell.
20593 Use its car if CAR has a non-nil value. */
20594 if (!NILP (tem))
20595 {
20596 tem = Fsymbol_value (car);
20597 if (!NILP (tem))
20598 {
20599 elt = XCAR (elt);
20600 goto tail_recurse;
20601 }
20602 }
20603 /* Symbol's value is nil (or symbol is unbound)
20604 Get the cddr of the original list
20605 and if possible find the caddr and use that. */
20606 elt = XCDR (elt);
20607 if (NILP (elt))
20608 break;
20609 else if (!CONSP (elt))
20610 goto invalid;
20611 elt = XCAR (elt);
20612 goto tail_recurse;
20613 }
20614 else if (INTEGERP (car))
20615 {
20616 register int lim = XINT (car);
20617 elt = XCDR (elt);
20618 if (lim < 0)
20619 {
20620 /* Negative int means reduce maximum width. */
20621 if (precision <= 0)
20622 precision = -lim;
20623 else
20624 precision = min (precision, -lim);
20625 }
20626 else if (lim > 0)
20627 {
20628 /* Padding specified. Don't let it be more than
20629 current maximum. */
20630 if (precision > 0)
20631 lim = min (precision, lim);
20632
20633 /* If that's more padding than already wanted, queue it.
20634 But don't reduce padding already specified even if
20635 that is beyond the current truncation point. */
20636 field_width = max (lim, field_width);
20637 }
20638 goto tail_recurse;
20639 }
20640 else if (STRINGP (car) || CONSP (car))
20641 {
20642 Lisp_Object halftail = elt;
20643 int len = 0;
20644
20645 while (CONSP (elt)
20646 && (precision <= 0 || n < precision))
20647 {
20648 n += display_mode_element (it, depth,
20649 /* Do padding only after the last
20650 element in the list. */
20651 (! CONSP (XCDR (elt))
20652 ? field_width - n
20653 : 0),
20654 precision - n, XCAR (elt),
20655 props, risky);
20656 elt = XCDR (elt);
20657 len++;
20658 if ((len & 1) == 0)
20659 halftail = XCDR (halftail);
20660 /* Check for cycle. */
20661 if (EQ (halftail, elt))
20662 break;
20663 }
20664 }
20665 }
20666 break;
20667
20668 default:
20669 invalid:
20670 elt = build_string ("*invalid*");
20671 goto tail_recurse;
20672 }
20673
20674 /* Pad to FIELD_WIDTH. */
20675 if (field_width > 0 && n < field_width)
20676 {
20677 switch (mode_line_target)
20678 {
20679 case MODE_LINE_NOPROP:
20680 case MODE_LINE_TITLE:
20681 n += store_mode_line_noprop ("", field_width - n, 0);
20682 break;
20683 case MODE_LINE_STRING:
20684 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20685 break;
20686 case MODE_LINE_DISPLAY:
20687 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20688 0, 0, 0);
20689 break;
20690 }
20691 }
20692
20693 return n;
20694 }
20695
20696 /* Store a mode-line string element in mode_line_string_list.
20697
20698 If STRING is non-null, display that C string. Otherwise, the Lisp
20699 string LISP_STRING is displayed.
20700
20701 FIELD_WIDTH is the minimum number of output glyphs to produce.
20702 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20703 with spaces. FIELD_WIDTH <= 0 means don't pad.
20704
20705 PRECISION is the maximum number of characters to output from
20706 STRING. PRECISION <= 0 means don't truncate the string.
20707
20708 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20709 properties to the string.
20710
20711 PROPS are the properties to add to the string.
20712 The mode_line_string_face face property is always added to the string.
20713 */
20714
20715 static int
20716 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20717 int field_width, int precision, Lisp_Object props)
20718 {
20719 ptrdiff_t len;
20720 int n = 0;
20721
20722 if (string != NULL)
20723 {
20724 len = strlen (string);
20725 if (precision > 0 && len > precision)
20726 len = precision;
20727 lisp_string = make_string (string, len);
20728 if (NILP (props))
20729 props = mode_line_string_face_prop;
20730 else if (!NILP (mode_line_string_face))
20731 {
20732 Lisp_Object face = Fplist_get (props, Qface);
20733 props = Fcopy_sequence (props);
20734 if (NILP (face))
20735 face = mode_line_string_face;
20736 else
20737 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20738 props = Fplist_put (props, Qface, face);
20739 }
20740 Fadd_text_properties (make_number (0), make_number (len),
20741 props, lisp_string);
20742 }
20743 else
20744 {
20745 len = XFASTINT (Flength (lisp_string));
20746 if (precision > 0 && len > precision)
20747 {
20748 len = precision;
20749 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20750 precision = -1;
20751 }
20752 if (!NILP (mode_line_string_face))
20753 {
20754 Lisp_Object face;
20755 if (NILP (props))
20756 props = Ftext_properties_at (make_number (0), lisp_string);
20757 face = Fplist_get (props, Qface);
20758 if (NILP (face))
20759 face = mode_line_string_face;
20760 else
20761 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20762 props = Fcons (Qface, Fcons (face, Qnil));
20763 if (copy_string)
20764 lisp_string = Fcopy_sequence (lisp_string);
20765 }
20766 if (!NILP (props))
20767 Fadd_text_properties (make_number (0), make_number (len),
20768 props, lisp_string);
20769 }
20770
20771 if (len > 0)
20772 {
20773 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20774 n += len;
20775 }
20776
20777 if (field_width > len)
20778 {
20779 field_width -= len;
20780 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20781 if (!NILP (props))
20782 Fadd_text_properties (make_number (0), make_number (field_width),
20783 props, lisp_string);
20784 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20785 n += field_width;
20786 }
20787
20788 return n;
20789 }
20790
20791
20792 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20793 1, 4, 0,
20794 doc: /* Format a string out of a mode line format specification.
20795 First arg FORMAT specifies the mode line format (see `mode-line-format'
20796 for details) to use.
20797
20798 By default, the format is evaluated for the currently selected window.
20799
20800 Optional second arg FACE specifies the face property to put on all
20801 characters for which no face is specified. The value nil means the
20802 default face. The value t means whatever face the window's mode line
20803 currently uses (either `mode-line' or `mode-line-inactive',
20804 depending on whether the window is the selected window or not).
20805 An integer value means the value string has no text
20806 properties.
20807
20808 Optional third and fourth args WINDOW and BUFFER specify the window
20809 and buffer to use as the context for the formatting (defaults
20810 are the selected window and the WINDOW's buffer). */)
20811 (Lisp_Object format, Lisp_Object face,
20812 Lisp_Object window, Lisp_Object buffer)
20813 {
20814 struct it it;
20815 int len;
20816 struct window *w;
20817 struct buffer *old_buffer = NULL;
20818 int face_id;
20819 int no_props = INTEGERP (face);
20820 ptrdiff_t count = SPECPDL_INDEX ();
20821 Lisp_Object str;
20822 int string_start = 0;
20823
20824 if (NILP (window))
20825 window = selected_window;
20826 CHECK_WINDOW (window);
20827 w = XWINDOW (window);
20828
20829 if (NILP (buffer))
20830 buffer = w->buffer;
20831 CHECK_BUFFER (buffer);
20832
20833 /* Make formatting the modeline a non-op when noninteractive, otherwise
20834 there will be problems later caused by a partially initialized frame. */
20835 if (NILP (format) || noninteractive)
20836 return empty_unibyte_string;
20837
20838 if (no_props)
20839 face = Qnil;
20840
20841 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20842 : EQ (face, Qt) ? (EQ (window, selected_window)
20843 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20844 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20845 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20846 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20847 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20848 : DEFAULT_FACE_ID;
20849
20850 if (XBUFFER (buffer) != current_buffer)
20851 old_buffer = current_buffer;
20852
20853 /* Save things including mode_line_proptrans_alist,
20854 and set that to nil so that we don't alter the outer value. */
20855 record_unwind_protect (unwind_format_mode_line,
20856 format_mode_line_unwind_data
20857 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
20858 old_buffer, selected_window, 1));
20859 mode_line_proptrans_alist = Qnil;
20860
20861 Fselect_window (window, Qt);
20862 if (old_buffer)
20863 set_buffer_internal_1 (XBUFFER (buffer));
20864
20865 init_iterator (&it, w, -1, -1, NULL, face_id);
20866
20867 if (no_props)
20868 {
20869 mode_line_target = MODE_LINE_NOPROP;
20870 mode_line_string_face_prop = Qnil;
20871 mode_line_string_list = Qnil;
20872 string_start = MODE_LINE_NOPROP_LEN (0);
20873 }
20874 else
20875 {
20876 mode_line_target = MODE_LINE_STRING;
20877 mode_line_string_list = Qnil;
20878 mode_line_string_face = face;
20879 mode_line_string_face_prop
20880 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20881 }
20882
20883 push_kboard (FRAME_KBOARD (it.f));
20884 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20885 pop_kboard ();
20886
20887 if (no_props)
20888 {
20889 len = MODE_LINE_NOPROP_LEN (string_start);
20890 str = make_string (mode_line_noprop_buf + string_start, len);
20891 }
20892 else
20893 {
20894 mode_line_string_list = Fnreverse (mode_line_string_list);
20895 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20896 empty_unibyte_string);
20897 }
20898
20899 unbind_to (count, Qnil);
20900 return str;
20901 }
20902
20903 /* Write a null-terminated, right justified decimal representation of
20904 the positive integer D to BUF using a minimal field width WIDTH. */
20905
20906 static void
20907 pint2str (register char *buf, register int width, register ptrdiff_t d)
20908 {
20909 register char *p = buf;
20910
20911 if (d <= 0)
20912 *p++ = '0';
20913 else
20914 {
20915 while (d > 0)
20916 {
20917 *p++ = d % 10 + '0';
20918 d /= 10;
20919 }
20920 }
20921
20922 for (width -= (int) (p - buf); width > 0; --width)
20923 *p++ = ' ';
20924 *p-- = '\0';
20925 while (p > buf)
20926 {
20927 d = *buf;
20928 *buf++ = *p;
20929 *p-- = d;
20930 }
20931 }
20932
20933 /* Write a null-terminated, right justified decimal and "human
20934 readable" representation of the nonnegative integer D to BUF using
20935 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20936
20937 static const char power_letter[] =
20938 {
20939 0, /* no letter */
20940 'k', /* kilo */
20941 'M', /* mega */
20942 'G', /* giga */
20943 'T', /* tera */
20944 'P', /* peta */
20945 'E', /* exa */
20946 'Z', /* zetta */
20947 'Y' /* yotta */
20948 };
20949
20950 static void
20951 pint2hrstr (char *buf, int width, ptrdiff_t d)
20952 {
20953 /* We aim to represent the nonnegative integer D as
20954 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20955 ptrdiff_t quotient = d;
20956 int remainder = 0;
20957 /* -1 means: do not use TENTHS. */
20958 int tenths = -1;
20959 int exponent = 0;
20960
20961 /* Length of QUOTIENT.TENTHS as a string. */
20962 int length;
20963
20964 char * psuffix;
20965 char * p;
20966
20967 if (1000 <= quotient)
20968 {
20969 /* Scale to the appropriate EXPONENT. */
20970 do
20971 {
20972 remainder = quotient % 1000;
20973 quotient /= 1000;
20974 exponent++;
20975 }
20976 while (1000 <= quotient);
20977
20978 /* Round to nearest and decide whether to use TENTHS or not. */
20979 if (quotient <= 9)
20980 {
20981 tenths = remainder / 100;
20982 if (50 <= remainder % 100)
20983 {
20984 if (tenths < 9)
20985 tenths++;
20986 else
20987 {
20988 quotient++;
20989 if (quotient == 10)
20990 tenths = -1;
20991 else
20992 tenths = 0;
20993 }
20994 }
20995 }
20996 else
20997 if (500 <= remainder)
20998 {
20999 if (quotient < 999)
21000 quotient++;
21001 else
21002 {
21003 quotient = 1;
21004 exponent++;
21005 tenths = 0;
21006 }
21007 }
21008 }
21009
21010 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21011 if (tenths == -1 && quotient <= 99)
21012 if (quotient <= 9)
21013 length = 1;
21014 else
21015 length = 2;
21016 else
21017 length = 3;
21018 p = psuffix = buf + max (width, length);
21019
21020 /* Print EXPONENT. */
21021 *psuffix++ = power_letter[exponent];
21022 *psuffix = '\0';
21023
21024 /* Print TENTHS. */
21025 if (tenths >= 0)
21026 {
21027 *--p = '0' + tenths;
21028 *--p = '.';
21029 }
21030
21031 /* Print QUOTIENT. */
21032 do
21033 {
21034 int digit = quotient % 10;
21035 *--p = '0' + digit;
21036 }
21037 while ((quotient /= 10) != 0);
21038
21039 /* Print leading spaces. */
21040 while (buf < p)
21041 *--p = ' ';
21042 }
21043
21044 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21045 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21046 type of CODING_SYSTEM. Return updated pointer into BUF. */
21047
21048 static unsigned char invalid_eol_type[] = "(*invalid*)";
21049
21050 static char *
21051 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21052 {
21053 Lisp_Object val;
21054 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21055 const unsigned char *eol_str;
21056 int eol_str_len;
21057 /* The EOL conversion we are using. */
21058 Lisp_Object eoltype;
21059
21060 val = CODING_SYSTEM_SPEC (coding_system);
21061 eoltype = Qnil;
21062
21063 if (!VECTORP (val)) /* Not yet decided. */
21064 {
21065 *buf++ = multibyte ? '-' : ' ';
21066 if (eol_flag)
21067 eoltype = eol_mnemonic_undecided;
21068 /* Don't mention EOL conversion if it isn't decided. */
21069 }
21070 else
21071 {
21072 Lisp_Object attrs;
21073 Lisp_Object eolvalue;
21074
21075 attrs = AREF (val, 0);
21076 eolvalue = AREF (val, 2);
21077
21078 *buf++ = multibyte
21079 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21080 : ' ';
21081
21082 if (eol_flag)
21083 {
21084 /* The EOL conversion that is normal on this system. */
21085
21086 if (NILP (eolvalue)) /* Not yet decided. */
21087 eoltype = eol_mnemonic_undecided;
21088 else if (VECTORP (eolvalue)) /* Not yet decided. */
21089 eoltype = eol_mnemonic_undecided;
21090 else /* eolvalue is Qunix, Qdos, or Qmac. */
21091 eoltype = (EQ (eolvalue, Qunix)
21092 ? eol_mnemonic_unix
21093 : (EQ (eolvalue, Qdos) == 1
21094 ? eol_mnemonic_dos : eol_mnemonic_mac));
21095 }
21096 }
21097
21098 if (eol_flag)
21099 {
21100 /* Mention the EOL conversion if it is not the usual one. */
21101 if (STRINGP (eoltype))
21102 {
21103 eol_str = SDATA (eoltype);
21104 eol_str_len = SBYTES (eoltype);
21105 }
21106 else if (CHARACTERP (eoltype))
21107 {
21108 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
21109 int c = XFASTINT (eoltype);
21110 eol_str_len = CHAR_STRING (c, tmp);
21111 eol_str = tmp;
21112 }
21113 else
21114 {
21115 eol_str = invalid_eol_type;
21116 eol_str_len = sizeof (invalid_eol_type) - 1;
21117 }
21118 memcpy (buf, eol_str, eol_str_len);
21119 buf += eol_str_len;
21120 }
21121
21122 return buf;
21123 }
21124
21125 /* Return a string for the output of a mode line %-spec for window W,
21126 generated by character C. FIELD_WIDTH > 0 means pad the string
21127 returned with spaces to that value. Return a Lisp string in
21128 *STRING if the resulting string is taken from that Lisp string.
21129
21130 Note we operate on the current buffer for most purposes,
21131 the exception being w->base_line_pos. */
21132
21133 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21134
21135 static const char *
21136 decode_mode_spec (struct window *w, register int c, int field_width,
21137 Lisp_Object *string)
21138 {
21139 Lisp_Object obj;
21140 struct frame *f = XFRAME (WINDOW_FRAME (w));
21141 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21142 struct buffer *b = current_buffer;
21143
21144 obj = Qnil;
21145 *string = Qnil;
21146
21147 switch (c)
21148 {
21149 case '*':
21150 if (!NILP (BVAR (b, read_only)))
21151 return "%";
21152 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21153 return "*";
21154 return "-";
21155
21156 case '+':
21157 /* This differs from %* only for a modified read-only buffer. */
21158 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21159 return "*";
21160 if (!NILP (BVAR (b, read_only)))
21161 return "%";
21162 return "-";
21163
21164 case '&':
21165 /* This differs from %* in ignoring read-only-ness. */
21166 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21167 return "*";
21168 return "-";
21169
21170 case '%':
21171 return "%";
21172
21173 case '[':
21174 {
21175 int i;
21176 char *p;
21177
21178 if (command_loop_level > 5)
21179 return "[[[... ";
21180 p = decode_mode_spec_buf;
21181 for (i = 0; i < command_loop_level; i++)
21182 *p++ = '[';
21183 *p = 0;
21184 return decode_mode_spec_buf;
21185 }
21186
21187 case ']':
21188 {
21189 int i;
21190 char *p;
21191
21192 if (command_loop_level > 5)
21193 return " ...]]]";
21194 p = decode_mode_spec_buf;
21195 for (i = 0; i < command_loop_level; i++)
21196 *p++ = ']';
21197 *p = 0;
21198 return decode_mode_spec_buf;
21199 }
21200
21201 case '-':
21202 {
21203 register int i;
21204
21205 /* Let lots_of_dashes be a string of infinite length. */
21206 if (mode_line_target == MODE_LINE_NOPROP ||
21207 mode_line_target == MODE_LINE_STRING)
21208 return "--";
21209 if (field_width <= 0
21210 || field_width > sizeof (lots_of_dashes))
21211 {
21212 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21213 decode_mode_spec_buf[i] = '-';
21214 decode_mode_spec_buf[i] = '\0';
21215 return decode_mode_spec_buf;
21216 }
21217 else
21218 return lots_of_dashes;
21219 }
21220
21221 case 'b':
21222 obj = BVAR (b, name);
21223 break;
21224
21225 case 'c':
21226 /* %c and %l are ignored in `frame-title-format'.
21227 (In redisplay_internal, the frame title is drawn _before_ the
21228 windows are updated, so the stuff which depends on actual
21229 window contents (such as %l) may fail to render properly, or
21230 even crash emacs.) */
21231 if (mode_line_target == MODE_LINE_TITLE)
21232 return "";
21233 else
21234 {
21235 ptrdiff_t col = current_column ();
21236 w->column_number_displayed = make_number (col);
21237 pint2str (decode_mode_spec_buf, field_width, col);
21238 return decode_mode_spec_buf;
21239 }
21240
21241 case 'e':
21242 #ifndef SYSTEM_MALLOC
21243 {
21244 if (NILP (Vmemory_full))
21245 return "";
21246 else
21247 return "!MEM FULL! ";
21248 }
21249 #else
21250 return "";
21251 #endif
21252
21253 case 'F':
21254 /* %F displays the frame name. */
21255 if (!NILP (f->title))
21256 return SSDATA (f->title);
21257 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21258 return SSDATA (f->name);
21259 return "Emacs";
21260
21261 case 'f':
21262 obj = BVAR (b, filename);
21263 break;
21264
21265 case 'i':
21266 {
21267 ptrdiff_t size = ZV - BEGV;
21268 pint2str (decode_mode_spec_buf, field_width, size);
21269 return decode_mode_spec_buf;
21270 }
21271
21272 case 'I':
21273 {
21274 ptrdiff_t size = ZV - BEGV;
21275 pint2hrstr (decode_mode_spec_buf, field_width, size);
21276 return decode_mode_spec_buf;
21277 }
21278
21279 case 'l':
21280 {
21281 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21282 ptrdiff_t topline, nlines, height;
21283 ptrdiff_t junk;
21284
21285 /* %c and %l are ignored in `frame-title-format'. */
21286 if (mode_line_target == MODE_LINE_TITLE)
21287 return "";
21288
21289 startpos = XMARKER (w->start)->charpos;
21290 startpos_byte = marker_byte_position (w->start);
21291 height = WINDOW_TOTAL_LINES (w);
21292
21293 /* If we decided that this buffer isn't suitable for line numbers,
21294 don't forget that too fast. */
21295 if (EQ (w->base_line_pos, w->buffer))
21296 goto no_value;
21297 /* But do forget it, if the window shows a different buffer now. */
21298 else if (BUFFERP (w->base_line_pos))
21299 w->base_line_pos = Qnil;
21300
21301 /* If the buffer is very big, don't waste time. */
21302 if (INTEGERP (Vline_number_display_limit)
21303 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21304 {
21305 w->base_line_pos = Qnil;
21306 w->base_line_number = Qnil;
21307 goto no_value;
21308 }
21309
21310 if (INTEGERP (w->base_line_number)
21311 && INTEGERP (w->base_line_pos)
21312 && XFASTINT (w->base_line_pos) <= startpos)
21313 {
21314 line = XFASTINT (w->base_line_number);
21315 linepos = XFASTINT (w->base_line_pos);
21316 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21317 }
21318 else
21319 {
21320 line = 1;
21321 linepos = BUF_BEGV (b);
21322 linepos_byte = BUF_BEGV_BYTE (b);
21323 }
21324
21325 /* Count lines from base line to window start position. */
21326 nlines = display_count_lines (linepos_byte,
21327 startpos_byte,
21328 startpos, &junk);
21329
21330 topline = nlines + line;
21331
21332 /* Determine a new base line, if the old one is too close
21333 or too far away, or if we did not have one.
21334 "Too close" means it's plausible a scroll-down would
21335 go back past it. */
21336 if (startpos == BUF_BEGV (b))
21337 {
21338 w->base_line_number = make_number (topline);
21339 w->base_line_pos = make_number (BUF_BEGV (b));
21340 }
21341 else if (nlines < height + 25 || nlines > height * 3 + 50
21342 || linepos == BUF_BEGV (b))
21343 {
21344 ptrdiff_t limit = BUF_BEGV (b);
21345 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21346 ptrdiff_t position;
21347 ptrdiff_t distance =
21348 (height * 2 + 30) * line_number_display_limit_width;
21349
21350 if (startpos - distance > limit)
21351 {
21352 limit = startpos - distance;
21353 limit_byte = CHAR_TO_BYTE (limit);
21354 }
21355
21356 nlines = display_count_lines (startpos_byte,
21357 limit_byte,
21358 - (height * 2 + 30),
21359 &position);
21360 /* If we couldn't find the lines we wanted within
21361 line_number_display_limit_width chars per line,
21362 give up on line numbers for this window. */
21363 if (position == limit_byte && limit == startpos - distance)
21364 {
21365 w->base_line_pos = w->buffer;
21366 w->base_line_number = Qnil;
21367 goto no_value;
21368 }
21369
21370 w->base_line_number = make_number (topline - nlines);
21371 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21372 }
21373
21374 /* Now count lines from the start pos to point. */
21375 nlines = display_count_lines (startpos_byte,
21376 PT_BYTE, PT, &junk);
21377
21378 /* Record that we did display the line number. */
21379 line_number_displayed = 1;
21380
21381 /* Make the string to show. */
21382 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21383 return decode_mode_spec_buf;
21384 no_value:
21385 {
21386 char* p = decode_mode_spec_buf;
21387 int pad = field_width - 2;
21388 while (pad-- > 0)
21389 *p++ = ' ';
21390 *p++ = '?';
21391 *p++ = '?';
21392 *p = '\0';
21393 return decode_mode_spec_buf;
21394 }
21395 }
21396 break;
21397
21398 case 'm':
21399 obj = BVAR (b, mode_name);
21400 break;
21401
21402 case 'n':
21403 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21404 return " Narrow";
21405 break;
21406
21407 case 'p':
21408 {
21409 ptrdiff_t pos = marker_position (w->start);
21410 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21411
21412 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21413 {
21414 if (pos <= BUF_BEGV (b))
21415 return "All";
21416 else
21417 return "Bottom";
21418 }
21419 else if (pos <= BUF_BEGV (b))
21420 return "Top";
21421 else
21422 {
21423 if (total > 1000000)
21424 /* Do it differently for a large value, to avoid overflow. */
21425 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21426 else
21427 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21428 /* We can't normally display a 3-digit number,
21429 so get us a 2-digit number that is close. */
21430 if (total == 100)
21431 total = 99;
21432 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21433 return decode_mode_spec_buf;
21434 }
21435 }
21436
21437 /* Display percentage of size above the bottom of the screen. */
21438 case 'P':
21439 {
21440 ptrdiff_t toppos = marker_position (w->start);
21441 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21442 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21443
21444 if (botpos >= BUF_ZV (b))
21445 {
21446 if (toppos <= BUF_BEGV (b))
21447 return "All";
21448 else
21449 return "Bottom";
21450 }
21451 else
21452 {
21453 if (total > 1000000)
21454 /* Do it differently for a large value, to avoid overflow. */
21455 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21456 else
21457 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21458 /* We can't normally display a 3-digit number,
21459 so get us a 2-digit number that is close. */
21460 if (total == 100)
21461 total = 99;
21462 if (toppos <= BUF_BEGV (b))
21463 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21464 else
21465 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21466 return decode_mode_spec_buf;
21467 }
21468 }
21469
21470 case 's':
21471 /* status of process */
21472 obj = Fget_buffer_process (Fcurrent_buffer ());
21473 if (NILP (obj))
21474 return "no process";
21475 #ifndef MSDOS
21476 obj = Fsymbol_name (Fprocess_status (obj));
21477 #endif
21478 break;
21479
21480 case '@':
21481 {
21482 ptrdiff_t count = inhibit_garbage_collection ();
21483 Lisp_Object val = call1 (intern ("file-remote-p"),
21484 BVAR (current_buffer, directory));
21485 unbind_to (count, Qnil);
21486
21487 if (NILP (val))
21488 return "-";
21489 else
21490 return "@";
21491 }
21492
21493 case 't': /* indicate TEXT or BINARY */
21494 return "T";
21495
21496 case 'z':
21497 /* coding-system (not including end-of-line format) */
21498 case 'Z':
21499 /* coding-system (including end-of-line type) */
21500 {
21501 int eol_flag = (c == 'Z');
21502 char *p = decode_mode_spec_buf;
21503
21504 if (! FRAME_WINDOW_P (f))
21505 {
21506 /* No need to mention EOL here--the terminal never needs
21507 to do EOL conversion. */
21508 p = decode_mode_spec_coding (CODING_ID_NAME
21509 (FRAME_KEYBOARD_CODING (f)->id),
21510 p, 0);
21511 p = decode_mode_spec_coding (CODING_ID_NAME
21512 (FRAME_TERMINAL_CODING (f)->id),
21513 p, 0);
21514 }
21515 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21516 p, eol_flag);
21517
21518 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21519 #ifdef subprocesses
21520 obj = Fget_buffer_process (Fcurrent_buffer ());
21521 if (PROCESSP (obj))
21522 {
21523 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21524 p, eol_flag);
21525 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21526 p, eol_flag);
21527 }
21528 #endif /* subprocesses */
21529 #endif /* 0 */
21530 *p = 0;
21531 return decode_mode_spec_buf;
21532 }
21533 }
21534
21535 if (STRINGP (obj))
21536 {
21537 *string = obj;
21538 return SSDATA (obj);
21539 }
21540 else
21541 return "";
21542 }
21543
21544
21545 /* Count up to COUNT lines starting from START_BYTE.
21546 But don't go beyond LIMIT_BYTE.
21547 Return the number of lines thus found (always nonnegative).
21548
21549 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21550
21551 static ptrdiff_t
21552 display_count_lines (ptrdiff_t start_byte,
21553 ptrdiff_t limit_byte, ptrdiff_t count,
21554 ptrdiff_t *byte_pos_ptr)
21555 {
21556 register unsigned char *cursor;
21557 unsigned char *base;
21558
21559 register ptrdiff_t ceiling;
21560 register unsigned char *ceiling_addr;
21561 ptrdiff_t orig_count = count;
21562
21563 /* If we are not in selective display mode,
21564 check only for newlines. */
21565 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21566 && !INTEGERP (BVAR (current_buffer, selective_display)));
21567
21568 if (count > 0)
21569 {
21570 while (start_byte < limit_byte)
21571 {
21572 ceiling = BUFFER_CEILING_OF (start_byte);
21573 ceiling = min (limit_byte - 1, ceiling);
21574 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21575 base = (cursor = BYTE_POS_ADDR (start_byte));
21576 while (1)
21577 {
21578 if (selective_display)
21579 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21580 ;
21581 else
21582 while (*cursor != '\n' && ++cursor != ceiling_addr)
21583 ;
21584
21585 if (cursor != ceiling_addr)
21586 {
21587 if (--count == 0)
21588 {
21589 start_byte += cursor - base + 1;
21590 *byte_pos_ptr = start_byte;
21591 return orig_count;
21592 }
21593 else
21594 if (++cursor == ceiling_addr)
21595 break;
21596 }
21597 else
21598 break;
21599 }
21600 start_byte += cursor - base;
21601 }
21602 }
21603 else
21604 {
21605 while (start_byte > limit_byte)
21606 {
21607 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21608 ceiling = max (limit_byte, ceiling);
21609 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21610 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21611 while (1)
21612 {
21613 if (selective_display)
21614 while (--cursor != ceiling_addr
21615 && *cursor != '\n' && *cursor != 015)
21616 ;
21617 else
21618 while (--cursor != ceiling_addr && *cursor != '\n')
21619 ;
21620
21621 if (cursor != ceiling_addr)
21622 {
21623 if (++count == 0)
21624 {
21625 start_byte += cursor - base + 1;
21626 *byte_pos_ptr = start_byte;
21627 /* When scanning backwards, we should
21628 not count the newline posterior to which we stop. */
21629 return - orig_count - 1;
21630 }
21631 }
21632 else
21633 break;
21634 }
21635 /* Here we add 1 to compensate for the last decrement
21636 of CURSOR, which took it past the valid range. */
21637 start_byte += cursor - base + 1;
21638 }
21639 }
21640
21641 *byte_pos_ptr = limit_byte;
21642
21643 if (count < 0)
21644 return - orig_count + count;
21645 return orig_count - count;
21646
21647 }
21648
21649
21650 \f
21651 /***********************************************************************
21652 Displaying strings
21653 ***********************************************************************/
21654
21655 /* Display a NUL-terminated string, starting with index START.
21656
21657 If STRING is non-null, display that C string. Otherwise, the Lisp
21658 string LISP_STRING is displayed. There's a case that STRING is
21659 non-null and LISP_STRING is not nil. It means STRING is a string
21660 data of LISP_STRING. In that case, we display LISP_STRING while
21661 ignoring its text properties.
21662
21663 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21664 FACE_STRING. Display STRING or LISP_STRING with the face at
21665 FACE_STRING_POS in FACE_STRING:
21666
21667 Display the string in the environment given by IT, but use the
21668 standard display table, temporarily.
21669
21670 FIELD_WIDTH is the minimum number of output glyphs to produce.
21671 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21672 with spaces. If STRING has more characters, more than FIELD_WIDTH
21673 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21674
21675 PRECISION is the maximum number of characters to output from
21676 STRING. PRECISION < 0 means don't truncate the string.
21677
21678 This is roughly equivalent to printf format specifiers:
21679
21680 FIELD_WIDTH PRECISION PRINTF
21681 ----------------------------------------
21682 -1 -1 %s
21683 -1 10 %.10s
21684 10 -1 %10s
21685 20 10 %20.10s
21686
21687 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21688 display them, and < 0 means obey the current buffer's value of
21689 enable_multibyte_characters.
21690
21691 Value is the number of columns displayed. */
21692
21693 static int
21694 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21695 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21696 int field_width, int precision, int max_x, int multibyte)
21697 {
21698 int hpos_at_start = it->hpos;
21699 int saved_face_id = it->face_id;
21700 struct glyph_row *row = it->glyph_row;
21701 ptrdiff_t it_charpos;
21702
21703 /* Initialize the iterator IT for iteration over STRING beginning
21704 with index START. */
21705 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21706 precision, field_width, multibyte);
21707 if (string && STRINGP (lisp_string))
21708 /* LISP_STRING is the one returned by decode_mode_spec. We should
21709 ignore its text properties. */
21710 it->stop_charpos = it->end_charpos;
21711
21712 /* If displaying STRING, set up the face of the iterator from
21713 FACE_STRING, if that's given. */
21714 if (STRINGP (face_string))
21715 {
21716 ptrdiff_t endptr;
21717 struct face *face;
21718
21719 it->face_id
21720 = face_at_string_position (it->w, face_string, face_string_pos,
21721 0, it->region_beg_charpos,
21722 it->region_end_charpos,
21723 &endptr, it->base_face_id, 0);
21724 face = FACE_FROM_ID (it->f, it->face_id);
21725 it->face_box_p = face->box != FACE_NO_BOX;
21726 }
21727
21728 /* Set max_x to the maximum allowed X position. Don't let it go
21729 beyond the right edge of the window. */
21730 if (max_x <= 0)
21731 max_x = it->last_visible_x;
21732 else
21733 max_x = min (max_x, it->last_visible_x);
21734
21735 /* Skip over display elements that are not visible. because IT->w is
21736 hscrolled. */
21737 if (it->current_x < it->first_visible_x)
21738 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21739 MOVE_TO_POS | MOVE_TO_X);
21740
21741 row->ascent = it->max_ascent;
21742 row->height = it->max_ascent + it->max_descent;
21743 row->phys_ascent = it->max_phys_ascent;
21744 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21745 row->extra_line_spacing = it->max_extra_line_spacing;
21746
21747 if (STRINGP (it->string))
21748 it_charpos = IT_STRING_CHARPOS (*it);
21749 else
21750 it_charpos = IT_CHARPOS (*it);
21751
21752 /* This condition is for the case that we are called with current_x
21753 past last_visible_x. */
21754 while (it->current_x < max_x)
21755 {
21756 int x_before, x, n_glyphs_before, i, nglyphs;
21757
21758 /* Get the next display element. */
21759 if (!get_next_display_element (it))
21760 break;
21761
21762 /* Produce glyphs. */
21763 x_before = it->current_x;
21764 n_glyphs_before = row->used[TEXT_AREA];
21765 PRODUCE_GLYPHS (it);
21766
21767 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21768 i = 0;
21769 x = x_before;
21770 while (i < nglyphs)
21771 {
21772 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21773
21774 if (it->line_wrap != TRUNCATE
21775 && x + glyph->pixel_width > max_x)
21776 {
21777 /* End of continued line or max_x reached. */
21778 if (CHAR_GLYPH_PADDING_P (*glyph))
21779 {
21780 /* A wide character is unbreakable. */
21781 if (row->reversed_p)
21782 unproduce_glyphs (it, row->used[TEXT_AREA]
21783 - n_glyphs_before);
21784 row->used[TEXT_AREA] = n_glyphs_before;
21785 it->current_x = x_before;
21786 }
21787 else
21788 {
21789 if (row->reversed_p)
21790 unproduce_glyphs (it, row->used[TEXT_AREA]
21791 - (n_glyphs_before + i));
21792 row->used[TEXT_AREA] = n_glyphs_before + i;
21793 it->current_x = x;
21794 }
21795 break;
21796 }
21797 else if (x + glyph->pixel_width >= it->first_visible_x)
21798 {
21799 /* Glyph is at least partially visible. */
21800 ++it->hpos;
21801 if (x < it->first_visible_x)
21802 row->x = x - it->first_visible_x;
21803 }
21804 else
21805 {
21806 /* Glyph is off the left margin of the display area.
21807 Should not happen. */
21808 abort ();
21809 }
21810
21811 row->ascent = max (row->ascent, it->max_ascent);
21812 row->height = max (row->height, it->max_ascent + it->max_descent);
21813 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21814 row->phys_height = max (row->phys_height,
21815 it->max_phys_ascent + it->max_phys_descent);
21816 row->extra_line_spacing = max (row->extra_line_spacing,
21817 it->max_extra_line_spacing);
21818 x += glyph->pixel_width;
21819 ++i;
21820 }
21821
21822 /* Stop if max_x reached. */
21823 if (i < nglyphs)
21824 break;
21825
21826 /* Stop at line ends. */
21827 if (ITERATOR_AT_END_OF_LINE_P (it))
21828 {
21829 it->continuation_lines_width = 0;
21830 break;
21831 }
21832
21833 set_iterator_to_next (it, 1);
21834 if (STRINGP (it->string))
21835 it_charpos = IT_STRING_CHARPOS (*it);
21836 else
21837 it_charpos = IT_CHARPOS (*it);
21838
21839 /* Stop if truncating at the right edge. */
21840 if (it->line_wrap == TRUNCATE
21841 && it->current_x >= it->last_visible_x)
21842 {
21843 /* Add truncation mark, but don't do it if the line is
21844 truncated at a padding space. */
21845 if (it_charpos < it->string_nchars)
21846 {
21847 if (!FRAME_WINDOW_P (it->f))
21848 {
21849 int ii, n;
21850
21851 if (it->current_x > it->last_visible_x)
21852 {
21853 if (!row->reversed_p)
21854 {
21855 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21856 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21857 break;
21858 }
21859 else
21860 {
21861 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21862 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21863 break;
21864 unproduce_glyphs (it, ii + 1);
21865 ii = row->used[TEXT_AREA] - (ii + 1);
21866 }
21867 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21868 {
21869 row->used[TEXT_AREA] = ii;
21870 produce_special_glyphs (it, IT_TRUNCATION);
21871 }
21872 }
21873 produce_special_glyphs (it, IT_TRUNCATION);
21874 }
21875 row->truncated_on_right_p = 1;
21876 }
21877 break;
21878 }
21879 }
21880
21881 /* Maybe insert a truncation at the left. */
21882 if (it->first_visible_x
21883 && it_charpos > 0)
21884 {
21885 if (!FRAME_WINDOW_P (it->f))
21886 insert_left_trunc_glyphs (it);
21887 row->truncated_on_left_p = 1;
21888 }
21889
21890 it->face_id = saved_face_id;
21891
21892 /* Value is number of columns displayed. */
21893 return it->hpos - hpos_at_start;
21894 }
21895
21896
21897 \f
21898 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21899 appears as an element of LIST or as the car of an element of LIST.
21900 If PROPVAL is a list, compare each element against LIST in that
21901 way, and return 1/2 if any element of PROPVAL is found in LIST.
21902 Otherwise return 0. This function cannot quit.
21903 The return value is 2 if the text is invisible but with an ellipsis
21904 and 1 if it's invisible and without an ellipsis. */
21905
21906 int
21907 invisible_p (register Lisp_Object propval, Lisp_Object list)
21908 {
21909 register Lisp_Object tail, proptail;
21910
21911 for (tail = list; CONSP (tail); tail = XCDR (tail))
21912 {
21913 register Lisp_Object tem;
21914 tem = XCAR (tail);
21915 if (EQ (propval, tem))
21916 return 1;
21917 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21918 return NILP (XCDR (tem)) ? 1 : 2;
21919 }
21920
21921 if (CONSP (propval))
21922 {
21923 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21924 {
21925 Lisp_Object propelt;
21926 propelt = XCAR (proptail);
21927 for (tail = list; CONSP (tail); tail = XCDR (tail))
21928 {
21929 register Lisp_Object tem;
21930 tem = XCAR (tail);
21931 if (EQ (propelt, tem))
21932 return 1;
21933 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21934 return NILP (XCDR (tem)) ? 1 : 2;
21935 }
21936 }
21937 }
21938
21939 return 0;
21940 }
21941
21942 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21943 doc: /* Non-nil if the property makes the text invisible.
21944 POS-OR-PROP can be a marker or number, in which case it is taken to be
21945 a position in the current buffer and the value of the `invisible' property
21946 is checked; or it can be some other value, which is then presumed to be the
21947 value of the `invisible' property of the text of interest.
21948 The non-nil value returned can be t for truly invisible text or something
21949 else if the text is replaced by an ellipsis. */)
21950 (Lisp_Object pos_or_prop)
21951 {
21952 Lisp_Object prop
21953 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21954 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21955 : pos_or_prop);
21956 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21957 return (invis == 0 ? Qnil
21958 : invis == 1 ? Qt
21959 : make_number (invis));
21960 }
21961
21962 /* Calculate a width or height in pixels from a specification using
21963 the following elements:
21964
21965 SPEC ::=
21966 NUM - a (fractional) multiple of the default font width/height
21967 (NUM) - specifies exactly NUM pixels
21968 UNIT - a fixed number of pixels, see below.
21969 ELEMENT - size of a display element in pixels, see below.
21970 (NUM . SPEC) - equals NUM * SPEC
21971 (+ SPEC SPEC ...) - add pixel values
21972 (- SPEC SPEC ...) - subtract pixel values
21973 (- SPEC) - negate pixel value
21974
21975 NUM ::=
21976 INT or FLOAT - a number constant
21977 SYMBOL - use symbol's (buffer local) variable binding.
21978
21979 UNIT ::=
21980 in - pixels per inch *)
21981 mm - pixels per 1/1000 meter *)
21982 cm - pixels per 1/100 meter *)
21983 width - width of current font in pixels.
21984 height - height of current font in pixels.
21985
21986 *) using the ratio(s) defined in display-pixels-per-inch.
21987
21988 ELEMENT ::=
21989
21990 left-fringe - left fringe width in pixels
21991 right-fringe - right fringe width in pixels
21992
21993 left-margin - left margin width in pixels
21994 right-margin - right margin width in pixels
21995
21996 scroll-bar - scroll-bar area width in pixels
21997
21998 Examples:
21999
22000 Pixels corresponding to 5 inches:
22001 (5 . in)
22002
22003 Total width of non-text areas on left side of window (if scroll-bar is on left):
22004 '(space :width (+ left-fringe left-margin scroll-bar))
22005
22006 Align to first text column (in header line):
22007 '(space :align-to 0)
22008
22009 Align to middle of text area minus half the width of variable `my-image'
22010 containing a loaded image:
22011 '(space :align-to (0.5 . (- text my-image)))
22012
22013 Width of left margin minus width of 1 character in the default font:
22014 '(space :width (- left-margin 1))
22015
22016 Width of left margin minus width of 2 characters in the current font:
22017 '(space :width (- left-margin (2 . width)))
22018
22019 Center 1 character over left-margin (in header line):
22020 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22021
22022 Different ways to express width of left fringe plus left margin minus one pixel:
22023 '(space :width (- (+ left-fringe left-margin) (1)))
22024 '(space :width (+ left-fringe left-margin (- (1))))
22025 '(space :width (+ left-fringe left-margin (-1)))
22026
22027 */
22028
22029 #define NUMVAL(X) \
22030 ((INTEGERP (X) || FLOATP (X)) \
22031 ? XFLOATINT (X) \
22032 : - 1)
22033
22034 static int
22035 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22036 struct font *font, int width_p, int *align_to)
22037 {
22038 double pixels;
22039
22040 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22041 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22042
22043 if (NILP (prop))
22044 return OK_PIXELS (0);
22045
22046 eassert (FRAME_LIVE_P (it->f));
22047
22048 if (SYMBOLP (prop))
22049 {
22050 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22051 {
22052 char *unit = SSDATA (SYMBOL_NAME (prop));
22053
22054 if (unit[0] == 'i' && unit[1] == 'n')
22055 pixels = 1.0;
22056 else if (unit[0] == 'm' && unit[1] == 'm')
22057 pixels = 25.4;
22058 else if (unit[0] == 'c' && unit[1] == 'm')
22059 pixels = 2.54;
22060 else
22061 pixels = 0;
22062 if (pixels > 0)
22063 {
22064 double ppi;
22065 #ifdef HAVE_WINDOW_SYSTEM
22066 if (FRAME_WINDOW_P (it->f)
22067 && (ppi = (width_p
22068 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22069 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22070 ppi > 0))
22071 return OK_PIXELS (ppi / pixels);
22072 #endif
22073
22074 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22075 || (CONSP (Vdisplay_pixels_per_inch)
22076 && (ppi = (width_p
22077 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22078 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22079 ppi > 0)))
22080 return OK_PIXELS (ppi / pixels);
22081
22082 return 0;
22083 }
22084 }
22085
22086 #ifdef HAVE_WINDOW_SYSTEM
22087 if (EQ (prop, Qheight))
22088 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22089 if (EQ (prop, Qwidth))
22090 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22091 #else
22092 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22093 return OK_PIXELS (1);
22094 #endif
22095
22096 if (EQ (prop, Qtext))
22097 return OK_PIXELS (width_p
22098 ? window_box_width (it->w, TEXT_AREA)
22099 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22100
22101 if (align_to && *align_to < 0)
22102 {
22103 *res = 0;
22104 if (EQ (prop, Qleft))
22105 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22106 if (EQ (prop, Qright))
22107 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22108 if (EQ (prop, Qcenter))
22109 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22110 + window_box_width (it->w, TEXT_AREA) / 2);
22111 if (EQ (prop, Qleft_fringe))
22112 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22113 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22114 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22115 if (EQ (prop, Qright_fringe))
22116 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22117 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22118 : window_box_right_offset (it->w, TEXT_AREA));
22119 if (EQ (prop, Qleft_margin))
22120 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22121 if (EQ (prop, Qright_margin))
22122 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22123 if (EQ (prop, Qscroll_bar))
22124 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22125 ? 0
22126 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22127 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22128 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22129 : 0)));
22130 }
22131 else
22132 {
22133 if (EQ (prop, Qleft_fringe))
22134 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22135 if (EQ (prop, Qright_fringe))
22136 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22137 if (EQ (prop, Qleft_margin))
22138 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22139 if (EQ (prop, Qright_margin))
22140 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22141 if (EQ (prop, Qscroll_bar))
22142 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22143 }
22144
22145 prop = buffer_local_value_1 (prop, it->w->buffer);
22146 if (EQ (prop, Qunbound))
22147 prop = Qnil;
22148 }
22149
22150 if (INTEGERP (prop) || FLOATP (prop))
22151 {
22152 int base_unit = (width_p
22153 ? FRAME_COLUMN_WIDTH (it->f)
22154 : FRAME_LINE_HEIGHT (it->f));
22155 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22156 }
22157
22158 if (CONSP (prop))
22159 {
22160 Lisp_Object car = XCAR (prop);
22161 Lisp_Object cdr = XCDR (prop);
22162
22163 if (SYMBOLP (car))
22164 {
22165 #ifdef HAVE_WINDOW_SYSTEM
22166 if (FRAME_WINDOW_P (it->f)
22167 && valid_image_p (prop))
22168 {
22169 ptrdiff_t id = lookup_image (it->f, prop);
22170 struct image *img = IMAGE_FROM_ID (it->f, id);
22171
22172 return OK_PIXELS (width_p ? img->width : img->height);
22173 }
22174 #endif
22175 if (EQ (car, Qplus) || EQ (car, Qminus))
22176 {
22177 int first = 1;
22178 double px;
22179
22180 pixels = 0;
22181 while (CONSP (cdr))
22182 {
22183 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22184 font, width_p, align_to))
22185 return 0;
22186 if (first)
22187 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22188 else
22189 pixels += px;
22190 cdr = XCDR (cdr);
22191 }
22192 if (EQ (car, Qminus))
22193 pixels = -pixels;
22194 return OK_PIXELS (pixels);
22195 }
22196
22197 car = buffer_local_value_1 (car, it->w->buffer);
22198 if (EQ (car, Qunbound))
22199 car = Qnil;
22200 }
22201
22202 if (INTEGERP (car) || FLOATP (car))
22203 {
22204 double fact;
22205 pixels = XFLOATINT (car);
22206 if (NILP (cdr))
22207 return OK_PIXELS (pixels);
22208 if (calc_pixel_width_or_height (&fact, it, cdr,
22209 font, width_p, align_to))
22210 return OK_PIXELS (pixels * fact);
22211 return 0;
22212 }
22213
22214 return 0;
22215 }
22216
22217 return 0;
22218 }
22219
22220 \f
22221 /***********************************************************************
22222 Glyph Display
22223 ***********************************************************************/
22224
22225 #ifdef HAVE_WINDOW_SYSTEM
22226
22227 #ifdef GLYPH_DEBUG
22228
22229 void
22230 dump_glyph_string (struct glyph_string *s)
22231 {
22232 fprintf (stderr, "glyph string\n");
22233 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22234 s->x, s->y, s->width, s->height);
22235 fprintf (stderr, " ybase = %d\n", s->ybase);
22236 fprintf (stderr, " hl = %d\n", s->hl);
22237 fprintf (stderr, " left overhang = %d, right = %d\n",
22238 s->left_overhang, s->right_overhang);
22239 fprintf (stderr, " nchars = %d\n", s->nchars);
22240 fprintf (stderr, " extends to end of line = %d\n",
22241 s->extends_to_end_of_line_p);
22242 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22243 fprintf (stderr, " bg width = %d\n", s->background_width);
22244 }
22245
22246 #endif /* GLYPH_DEBUG */
22247
22248 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22249 of XChar2b structures for S; it can't be allocated in
22250 init_glyph_string because it must be allocated via `alloca'. W
22251 is the window on which S is drawn. ROW and AREA are the glyph row
22252 and area within the row from which S is constructed. START is the
22253 index of the first glyph structure covered by S. HL is a
22254 face-override for drawing S. */
22255
22256 #ifdef HAVE_NTGUI
22257 #define OPTIONAL_HDC(hdc) HDC hdc,
22258 #define DECLARE_HDC(hdc) HDC hdc;
22259 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22260 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22261 #endif
22262
22263 #ifndef OPTIONAL_HDC
22264 #define OPTIONAL_HDC(hdc)
22265 #define DECLARE_HDC(hdc)
22266 #define ALLOCATE_HDC(hdc, f)
22267 #define RELEASE_HDC(hdc, f)
22268 #endif
22269
22270 static void
22271 init_glyph_string (struct glyph_string *s,
22272 OPTIONAL_HDC (hdc)
22273 XChar2b *char2b, struct window *w, struct glyph_row *row,
22274 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22275 {
22276 memset (s, 0, sizeof *s);
22277 s->w = w;
22278 s->f = XFRAME (w->frame);
22279 #ifdef HAVE_NTGUI
22280 s->hdc = hdc;
22281 #endif
22282 s->display = FRAME_X_DISPLAY (s->f);
22283 s->window = FRAME_X_WINDOW (s->f);
22284 s->char2b = char2b;
22285 s->hl = hl;
22286 s->row = row;
22287 s->area = area;
22288 s->first_glyph = row->glyphs[area] + start;
22289 s->height = row->height;
22290 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22291 s->ybase = s->y + row->ascent;
22292 }
22293
22294
22295 /* Append the list of glyph strings with head H and tail T to the list
22296 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22297
22298 static inline void
22299 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22300 struct glyph_string *h, struct glyph_string *t)
22301 {
22302 if (h)
22303 {
22304 if (*head)
22305 (*tail)->next = h;
22306 else
22307 *head = h;
22308 h->prev = *tail;
22309 *tail = t;
22310 }
22311 }
22312
22313
22314 /* Prepend the list of glyph strings with head H and tail T to the
22315 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22316 result. */
22317
22318 static inline void
22319 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22320 struct glyph_string *h, struct glyph_string *t)
22321 {
22322 if (h)
22323 {
22324 if (*head)
22325 (*head)->prev = t;
22326 else
22327 *tail = t;
22328 t->next = *head;
22329 *head = h;
22330 }
22331 }
22332
22333
22334 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22335 Set *HEAD and *TAIL to the resulting list. */
22336
22337 static inline void
22338 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22339 struct glyph_string *s)
22340 {
22341 s->next = s->prev = NULL;
22342 append_glyph_string_lists (head, tail, s, s);
22343 }
22344
22345
22346 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22347 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22348 make sure that X resources for the face returned are allocated.
22349 Value is a pointer to a realized face that is ready for display if
22350 DISPLAY_P is non-zero. */
22351
22352 static inline struct face *
22353 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22354 XChar2b *char2b, int display_p)
22355 {
22356 struct face *face = FACE_FROM_ID (f, face_id);
22357
22358 if (face->font)
22359 {
22360 unsigned code = face->font->driver->encode_char (face->font, c);
22361
22362 if (code != FONT_INVALID_CODE)
22363 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22364 else
22365 STORE_XCHAR2B (char2b, 0, 0);
22366 }
22367
22368 /* Make sure X resources of the face are allocated. */
22369 #ifdef HAVE_X_WINDOWS
22370 if (display_p)
22371 #endif
22372 {
22373 eassert (face != NULL);
22374 PREPARE_FACE_FOR_DISPLAY (f, face);
22375 }
22376
22377 return face;
22378 }
22379
22380
22381 /* Get face and two-byte form of character glyph GLYPH on frame F.
22382 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22383 a pointer to a realized face that is ready for display. */
22384
22385 static inline struct face *
22386 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22387 XChar2b *char2b, int *two_byte_p)
22388 {
22389 struct face *face;
22390
22391 eassert (glyph->type == CHAR_GLYPH);
22392 face = FACE_FROM_ID (f, glyph->face_id);
22393
22394 if (two_byte_p)
22395 *two_byte_p = 0;
22396
22397 if (face->font)
22398 {
22399 unsigned code;
22400
22401 if (CHAR_BYTE8_P (glyph->u.ch))
22402 code = CHAR_TO_BYTE8 (glyph->u.ch);
22403 else
22404 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22405
22406 if (code != FONT_INVALID_CODE)
22407 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22408 else
22409 STORE_XCHAR2B (char2b, 0, 0);
22410 }
22411
22412 /* Make sure X resources of the face are allocated. */
22413 eassert (face != NULL);
22414 PREPARE_FACE_FOR_DISPLAY (f, face);
22415 return face;
22416 }
22417
22418
22419 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22420 Return 1 if FONT has a glyph for C, otherwise return 0. */
22421
22422 static inline int
22423 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22424 {
22425 unsigned code;
22426
22427 if (CHAR_BYTE8_P (c))
22428 code = CHAR_TO_BYTE8 (c);
22429 else
22430 code = font->driver->encode_char (font, c);
22431
22432 if (code == FONT_INVALID_CODE)
22433 return 0;
22434 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22435 return 1;
22436 }
22437
22438
22439 /* Fill glyph string S with composition components specified by S->cmp.
22440
22441 BASE_FACE is the base face of the composition.
22442 S->cmp_from is the index of the first component for S.
22443
22444 OVERLAPS non-zero means S should draw the foreground only, and use
22445 its physical height for clipping. See also draw_glyphs.
22446
22447 Value is the index of a component not in S. */
22448
22449 static int
22450 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22451 int overlaps)
22452 {
22453 int i;
22454 /* For all glyphs of this composition, starting at the offset
22455 S->cmp_from, until we reach the end of the definition or encounter a
22456 glyph that requires the different face, add it to S. */
22457 struct face *face;
22458
22459 eassert (s);
22460
22461 s->for_overlaps = overlaps;
22462 s->face = NULL;
22463 s->font = NULL;
22464 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22465 {
22466 int c = COMPOSITION_GLYPH (s->cmp, i);
22467
22468 /* TAB in a composition means display glyphs with padding space
22469 on the left or right. */
22470 if (c != '\t')
22471 {
22472 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22473 -1, Qnil);
22474
22475 face = get_char_face_and_encoding (s->f, c, face_id,
22476 s->char2b + i, 1);
22477 if (face)
22478 {
22479 if (! s->face)
22480 {
22481 s->face = face;
22482 s->font = s->face->font;
22483 }
22484 else if (s->face != face)
22485 break;
22486 }
22487 }
22488 ++s->nchars;
22489 }
22490 s->cmp_to = i;
22491
22492 if (s->face == NULL)
22493 {
22494 s->face = base_face->ascii_face;
22495 s->font = s->face->font;
22496 }
22497
22498 /* All glyph strings for the same composition has the same width,
22499 i.e. the width set for the first component of the composition. */
22500 s->width = s->first_glyph->pixel_width;
22501
22502 /* If the specified font could not be loaded, use the frame's
22503 default font, but record the fact that we couldn't load it in
22504 the glyph string so that we can draw rectangles for the
22505 characters of the glyph string. */
22506 if (s->font == NULL)
22507 {
22508 s->font_not_found_p = 1;
22509 s->font = FRAME_FONT (s->f);
22510 }
22511
22512 /* Adjust base line for subscript/superscript text. */
22513 s->ybase += s->first_glyph->voffset;
22514
22515 /* This glyph string must always be drawn with 16-bit functions. */
22516 s->two_byte_p = 1;
22517
22518 return s->cmp_to;
22519 }
22520
22521 static int
22522 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22523 int start, int end, int overlaps)
22524 {
22525 struct glyph *glyph, *last;
22526 Lisp_Object lgstring;
22527 int i;
22528
22529 s->for_overlaps = overlaps;
22530 glyph = s->row->glyphs[s->area] + start;
22531 last = s->row->glyphs[s->area] + end;
22532 s->cmp_id = glyph->u.cmp.id;
22533 s->cmp_from = glyph->slice.cmp.from;
22534 s->cmp_to = glyph->slice.cmp.to + 1;
22535 s->face = FACE_FROM_ID (s->f, face_id);
22536 lgstring = composition_gstring_from_id (s->cmp_id);
22537 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22538 glyph++;
22539 while (glyph < last
22540 && glyph->u.cmp.automatic
22541 && glyph->u.cmp.id == s->cmp_id
22542 && s->cmp_to == glyph->slice.cmp.from)
22543 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22544
22545 for (i = s->cmp_from; i < s->cmp_to; i++)
22546 {
22547 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22548 unsigned code = LGLYPH_CODE (lglyph);
22549
22550 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22551 }
22552 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22553 return glyph - s->row->glyphs[s->area];
22554 }
22555
22556
22557 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22558 See the comment of fill_glyph_string for arguments.
22559 Value is the index of the first glyph not in S. */
22560
22561
22562 static int
22563 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22564 int start, int end, int overlaps)
22565 {
22566 struct glyph *glyph, *last;
22567 int voffset;
22568
22569 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22570 s->for_overlaps = overlaps;
22571 glyph = s->row->glyphs[s->area] + start;
22572 last = s->row->glyphs[s->area] + end;
22573 voffset = glyph->voffset;
22574 s->face = FACE_FROM_ID (s->f, face_id);
22575 s->font = s->face->font;
22576 s->nchars = 1;
22577 s->width = glyph->pixel_width;
22578 glyph++;
22579 while (glyph < last
22580 && glyph->type == GLYPHLESS_GLYPH
22581 && glyph->voffset == voffset
22582 && glyph->face_id == face_id)
22583 {
22584 s->nchars++;
22585 s->width += glyph->pixel_width;
22586 glyph++;
22587 }
22588 s->ybase += voffset;
22589 return glyph - s->row->glyphs[s->area];
22590 }
22591
22592
22593 /* Fill glyph string S from a sequence of character glyphs.
22594
22595 FACE_ID is the face id of the string. START is the index of the
22596 first glyph to consider, END is the index of the last + 1.
22597 OVERLAPS non-zero means S should draw the foreground only, and use
22598 its physical height for clipping. See also draw_glyphs.
22599
22600 Value is the index of the first glyph not in S. */
22601
22602 static int
22603 fill_glyph_string (struct glyph_string *s, int face_id,
22604 int start, int end, int overlaps)
22605 {
22606 struct glyph *glyph, *last;
22607 int voffset;
22608 int glyph_not_available_p;
22609
22610 eassert (s->f == XFRAME (s->w->frame));
22611 eassert (s->nchars == 0);
22612 eassert (start >= 0 && end > start);
22613
22614 s->for_overlaps = overlaps;
22615 glyph = s->row->glyphs[s->area] + start;
22616 last = s->row->glyphs[s->area] + end;
22617 voffset = glyph->voffset;
22618 s->padding_p = glyph->padding_p;
22619 glyph_not_available_p = glyph->glyph_not_available_p;
22620
22621 while (glyph < last
22622 && glyph->type == CHAR_GLYPH
22623 && glyph->voffset == voffset
22624 /* Same face id implies same font, nowadays. */
22625 && glyph->face_id == face_id
22626 && glyph->glyph_not_available_p == glyph_not_available_p)
22627 {
22628 int two_byte_p;
22629
22630 s->face = get_glyph_face_and_encoding (s->f, glyph,
22631 s->char2b + s->nchars,
22632 &two_byte_p);
22633 s->two_byte_p = two_byte_p;
22634 ++s->nchars;
22635 eassert (s->nchars <= end - start);
22636 s->width += glyph->pixel_width;
22637 if (glyph++->padding_p != s->padding_p)
22638 break;
22639 }
22640
22641 s->font = s->face->font;
22642
22643 /* If the specified font could not be loaded, use the frame's font,
22644 but record the fact that we couldn't load it in
22645 S->font_not_found_p so that we can draw rectangles for the
22646 characters of the glyph string. */
22647 if (s->font == NULL || glyph_not_available_p)
22648 {
22649 s->font_not_found_p = 1;
22650 s->font = FRAME_FONT (s->f);
22651 }
22652
22653 /* Adjust base line for subscript/superscript text. */
22654 s->ybase += voffset;
22655
22656 eassert (s->face && s->face->gc);
22657 return glyph - s->row->glyphs[s->area];
22658 }
22659
22660
22661 /* Fill glyph string S from image glyph S->first_glyph. */
22662
22663 static void
22664 fill_image_glyph_string (struct glyph_string *s)
22665 {
22666 eassert (s->first_glyph->type == IMAGE_GLYPH);
22667 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22668 eassert (s->img);
22669 s->slice = s->first_glyph->slice.img;
22670 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22671 s->font = s->face->font;
22672 s->width = s->first_glyph->pixel_width;
22673
22674 /* Adjust base line for subscript/superscript text. */
22675 s->ybase += s->first_glyph->voffset;
22676 }
22677
22678
22679 /* Fill glyph string S from a sequence of stretch glyphs.
22680
22681 START is the index of the first glyph to consider,
22682 END is the index of the last + 1.
22683
22684 Value is the index of the first glyph not in S. */
22685
22686 static int
22687 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22688 {
22689 struct glyph *glyph, *last;
22690 int voffset, face_id;
22691
22692 eassert (s->first_glyph->type == STRETCH_GLYPH);
22693
22694 glyph = s->row->glyphs[s->area] + start;
22695 last = s->row->glyphs[s->area] + end;
22696 face_id = glyph->face_id;
22697 s->face = FACE_FROM_ID (s->f, face_id);
22698 s->font = s->face->font;
22699 s->width = glyph->pixel_width;
22700 s->nchars = 1;
22701 voffset = glyph->voffset;
22702
22703 for (++glyph;
22704 (glyph < last
22705 && glyph->type == STRETCH_GLYPH
22706 && glyph->voffset == voffset
22707 && glyph->face_id == face_id);
22708 ++glyph)
22709 s->width += glyph->pixel_width;
22710
22711 /* Adjust base line for subscript/superscript text. */
22712 s->ybase += voffset;
22713
22714 /* The case that face->gc == 0 is handled when drawing the glyph
22715 string by calling PREPARE_FACE_FOR_DISPLAY. */
22716 eassert (s->face);
22717 return glyph - s->row->glyphs[s->area];
22718 }
22719
22720 static struct font_metrics *
22721 get_per_char_metric (struct font *font, XChar2b *char2b)
22722 {
22723 static struct font_metrics metrics;
22724 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22725
22726 if (! font || code == FONT_INVALID_CODE)
22727 return NULL;
22728 font->driver->text_extents (font, &code, 1, &metrics);
22729 return &metrics;
22730 }
22731
22732 /* EXPORT for RIF:
22733 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22734 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22735 assumed to be zero. */
22736
22737 void
22738 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22739 {
22740 *left = *right = 0;
22741
22742 if (glyph->type == CHAR_GLYPH)
22743 {
22744 struct face *face;
22745 XChar2b char2b;
22746 struct font_metrics *pcm;
22747
22748 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22749 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22750 {
22751 if (pcm->rbearing > pcm->width)
22752 *right = pcm->rbearing - pcm->width;
22753 if (pcm->lbearing < 0)
22754 *left = -pcm->lbearing;
22755 }
22756 }
22757 else if (glyph->type == COMPOSITE_GLYPH)
22758 {
22759 if (! glyph->u.cmp.automatic)
22760 {
22761 struct composition *cmp = composition_table[glyph->u.cmp.id];
22762
22763 if (cmp->rbearing > cmp->pixel_width)
22764 *right = cmp->rbearing - cmp->pixel_width;
22765 if (cmp->lbearing < 0)
22766 *left = - cmp->lbearing;
22767 }
22768 else
22769 {
22770 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22771 struct font_metrics metrics;
22772
22773 composition_gstring_width (gstring, glyph->slice.cmp.from,
22774 glyph->slice.cmp.to + 1, &metrics);
22775 if (metrics.rbearing > metrics.width)
22776 *right = metrics.rbearing - metrics.width;
22777 if (metrics.lbearing < 0)
22778 *left = - metrics.lbearing;
22779 }
22780 }
22781 }
22782
22783
22784 /* Return the index of the first glyph preceding glyph string S that
22785 is overwritten by S because of S's left overhang. Value is -1
22786 if no glyphs are overwritten. */
22787
22788 static int
22789 left_overwritten (struct glyph_string *s)
22790 {
22791 int k;
22792
22793 if (s->left_overhang)
22794 {
22795 int x = 0, i;
22796 struct glyph *glyphs = s->row->glyphs[s->area];
22797 int first = s->first_glyph - glyphs;
22798
22799 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22800 x -= glyphs[i].pixel_width;
22801
22802 k = i + 1;
22803 }
22804 else
22805 k = -1;
22806
22807 return k;
22808 }
22809
22810
22811 /* Return the index of the first glyph preceding glyph string S that
22812 is overwriting S because of its right overhang. Value is -1 if no
22813 glyph in front of S overwrites S. */
22814
22815 static int
22816 left_overwriting (struct glyph_string *s)
22817 {
22818 int i, k, x;
22819 struct glyph *glyphs = s->row->glyphs[s->area];
22820 int first = s->first_glyph - glyphs;
22821
22822 k = -1;
22823 x = 0;
22824 for (i = first - 1; i >= 0; --i)
22825 {
22826 int left, right;
22827 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22828 if (x + right > 0)
22829 k = i;
22830 x -= glyphs[i].pixel_width;
22831 }
22832
22833 return k;
22834 }
22835
22836
22837 /* Return the index of the last glyph following glyph string S that is
22838 overwritten by S because of S's right overhang. Value is -1 if
22839 no such glyph is found. */
22840
22841 static int
22842 right_overwritten (struct glyph_string *s)
22843 {
22844 int k = -1;
22845
22846 if (s->right_overhang)
22847 {
22848 int x = 0, i;
22849 struct glyph *glyphs = s->row->glyphs[s->area];
22850 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22851 int end = s->row->used[s->area];
22852
22853 for (i = first; i < end && s->right_overhang > x; ++i)
22854 x += glyphs[i].pixel_width;
22855
22856 k = i;
22857 }
22858
22859 return k;
22860 }
22861
22862
22863 /* Return the index of the last glyph following glyph string S that
22864 overwrites S because of its left overhang. Value is negative
22865 if no such glyph is found. */
22866
22867 static int
22868 right_overwriting (struct glyph_string *s)
22869 {
22870 int i, k, x;
22871 int end = s->row->used[s->area];
22872 struct glyph *glyphs = s->row->glyphs[s->area];
22873 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22874
22875 k = -1;
22876 x = 0;
22877 for (i = first; i < end; ++i)
22878 {
22879 int left, right;
22880 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22881 if (x - left < 0)
22882 k = i;
22883 x += glyphs[i].pixel_width;
22884 }
22885
22886 return k;
22887 }
22888
22889
22890 /* Set background width of glyph string S. START is the index of the
22891 first glyph following S. LAST_X is the right-most x-position + 1
22892 in the drawing area. */
22893
22894 static inline void
22895 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22896 {
22897 /* If the face of this glyph string has to be drawn to the end of
22898 the drawing area, set S->extends_to_end_of_line_p. */
22899
22900 if (start == s->row->used[s->area]
22901 && s->area == TEXT_AREA
22902 && ((s->row->fill_line_p
22903 && (s->hl == DRAW_NORMAL_TEXT
22904 || s->hl == DRAW_IMAGE_RAISED
22905 || s->hl == DRAW_IMAGE_SUNKEN))
22906 || s->hl == DRAW_MOUSE_FACE))
22907 s->extends_to_end_of_line_p = 1;
22908
22909 /* If S extends its face to the end of the line, set its
22910 background_width to the distance to the right edge of the drawing
22911 area. */
22912 if (s->extends_to_end_of_line_p)
22913 s->background_width = last_x - s->x + 1;
22914 else
22915 s->background_width = s->width;
22916 }
22917
22918
22919 /* Compute overhangs and x-positions for glyph string S and its
22920 predecessors, or successors. X is the starting x-position for S.
22921 BACKWARD_P non-zero means process predecessors. */
22922
22923 static void
22924 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22925 {
22926 if (backward_p)
22927 {
22928 while (s)
22929 {
22930 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22931 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22932 x -= s->width;
22933 s->x = x;
22934 s = s->prev;
22935 }
22936 }
22937 else
22938 {
22939 while (s)
22940 {
22941 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22942 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22943 s->x = x;
22944 x += s->width;
22945 s = s->next;
22946 }
22947 }
22948 }
22949
22950
22951
22952 /* The following macros are only called from draw_glyphs below.
22953 They reference the following parameters of that function directly:
22954 `w', `row', `area', and `overlap_p'
22955 as well as the following local variables:
22956 `s', `f', and `hdc' (in W32) */
22957
22958 #ifdef HAVE_NTGUI
22959 /* On W32, silently add local `hdc' variable to argument list of
22960 init_glyph_string. */
22961 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22962 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22963 #else
22964 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22965 init_glyph_string (s, char2b, w, row, area, start, hl)
22966 #endif
22967
22968 /* Add a glyph string for a stretch glyph to the list of strings
22969 between HEAD and TAIL. START is the index of the stretch glyph in
22970 row area AREA of glyph row ROW. END is the index of the last glyph
22971 in that glyph row area. X is the current output position assigned
22972 to the new glyph string constructed. HL overrides that face of the
22973 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22974 is the right-most x-position of the drawing area. */
22975
22976 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22977 and below -- keep them on one line. */
22978 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22979 do \
22980 { \
22981 s = (struct glyph_string *) alloca (sizeof *s); \
22982 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22983 START = fill_stretch_glyph_string (s, START, END); \
22984 append_glyph_string (&HEAD, &TAIL, s); \
22985 s->x = (X); \
22986 } \
22987 while (0)
22988
22989
22990 /* Add a glyph string for an image glyph to the list of strings
22991 between HEAD and TAIL. START is the index of the image glyph in
22992 row area AREA of glyph row ROW. END is the index of the last glyph
22993 in that glyph row area. X is the current output position assigned
22994 to the new glyph string constructed. HL overrides that face of the
22995 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22996 is the right-most x-position of the drawing area. */
22997
22998 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22999 do \
23000 { \
23001 s = (struct glyph_string *) alloca (sizeof *s); \
23002 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23003 fill_image_glyph_string (s); \
23004 append_glyph_string (&HEAD, &TAIL, s); \
23005 ++START; \
23006 s->x = (X); \
23007 } \
23008 while (0)
23009
23010
23011 /* Add a glyph string for a sequence of character glyphs to the list
23012 of strings between HEAD and TAIL. START is the index of the first
23013 glyph in row area AREA of glyph row ROW that is part of the new
23014 glyph string. END is the index of the last glyph in that glyph row
23015 area. X is the current output position assigned to the new glyph
23016 string constructed. HL overrides that face of the glyph; e.g. it
23017 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23018 right-most x-position of the drawing area. */
23019
23020 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23021 do \
23022 { \
23023 int face_id; \
23024 XChar2b *char2b; \
23025 \
23026 face_id = (row)->glyphs[area][START].face_id; \
23027 \
23028 s = (struct glyph_string *) alloca (sizeof *s); \
23029 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
23030 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23031 append_glyph_string (&HEAD, &TAIL, s); \
23032 s->x = (X); \
23033 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23034 } \
23035 while (0)
23036
23037
23038 /* Add a glyph string for a composite sequence to the list of strings
23039 between HEAD and TAIL. START is the index of the first glyph in
23040 row area AREA of glyph row ROW that is part of the new glyph
23041 string. END is the index of the last glyph in that glyph row area.
23042 X is the current output position assigned to the new glyph string
23043 constructed. HL overrides that face of the glyph; e.g. it is
23044 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23045 x-position of the drawing area. */
23046
23047 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23048 do { \
23049 int face_id = (row)->glyphs[area][START].face_id; \
23050 struct face *base_face = FACE_FROM_ID (f, face_id); \
23051 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23052 struct composition *cmp = composition_table[cmp_id]; \
23053 XChar2b *char2b; \
23054 struct glyph_string *first_s = NULL; \
23055 int n; \
23056 \
23057 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
23058 \
23059 /* Make glyph_strings for each glyph sequence that is drawable by \
23060 the same face, and append them to HEAD/TAIL. */ \
23061 for (n = 0; n < cmp->glyph_len;) \
23062 { \
23063 s = (struct glyph_string *) alloca (sizeof *s); \
23064 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23065 append_glyph_string (&(HEAD), &(TAIL), s); \
23066 s->cmp = cmp; \
23067 s->cmp_from = n; \
23068 s->x = (X); \
23069 if (n == 0) \
23070 first_s = s; \
23071 n = fill_composite_glyph_string (s, base_face, overlaps); \
23072 } \
23073 \
23074 ++START; \
23075 s = first_s; \
23076 } while (0)
23077
23078
23079 /* Add a glyph string for a glyph-string sequence to the list of strings
23080 between HEAD and TAIL. */
23081
23082 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23083 do { \
23084 int face_id; \
23085 XChar2b *char2b; \
23086 Lisp_Object gstring; \
23087 \
23088 face_id = (row)->glyphs[area][START].face_id; \
23089 gstring = (composition_gstring_from_id \
23090 ((row)->glyphs[area][START].u.cmp.id)); \
23091 s = (struct glyph_string *) alloca (sizeof *s); \
23092 char2b = (XChar2b *) alloca ((sizeof *char2b) \
23093 * LGSTRING_GLYPH_LEN (gstring)); \
23094 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23095 append_glyph_string (&(HEAD), &(TAIL), s); \
23096 s->x = (X); \
23097 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23098 } while (0)
23099
23100
23101 /* Add a glyph string for a sequence of glyphless character's glyphs
23102 to the list of strings between HEAD and TAIL. The meanings of
23103 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23104
23105 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23106 do \
23107 { \
23108 int face_id; \
23109 \
23110 face_id = (row)->glyphs[area][START].face_id; \
23111 \
23112 s = (struct glyph_string *) alloca (sizeof *s); \
23113 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23114 append_glyph_string (&HEAD, &TAIL, s); \
23115 s->x = (X); \
23116 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23117 overlaps); \
23118 } \
23119 while (0)
23120
23121
23122 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23123 of AREA of glyph row ROW on window W between indices START and END.
23124 HL overrides the face for drawing glyph strings, e.g. it is
23125 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23126 x-positions of the drawing area.
23127
23128 This is an ugly monster macro construct because we must use alloca
23129 to allocate glyph strings (because draw_glyphs can be called
23130 asynchronously). */
23131
23132 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23133 do \
23134 { \
23135 HEAD = TAIL = NULL; \
23136 while (START < END) \
23137 { \
23138 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23139 switch (first_glyph->type) \
23140 { \
23141 case CHAR_GLYPH: \
23142 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23143 HL, X, LAST_X); \
23144 break; \
23145 \
23146 case COMPOSITE_GLYPH: \
23147 if (first_glyph->u.cmp.automatic) \
23148 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23149 HL, X, LAST_X); \
23150 else \
23151 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23152 HL, X, LAST_X); \
23153 break; \
23154 \
23155 case STRETCH_GLYPH: \
23156 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23157 HL, X, LAST_X); \
23158 break; \
23159 \
23160 case IMAGE_GLYPH: \
23161 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23162 HL, X, LAST_X); \
23163 break; \
23164 \
23165 case GLYPHLESS_GLYPH: \
23166 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23167 HL, X, LAST_X); \
23168 break; \
23169 \
23170 default: \
23171 abort (); \
23172 } \
23173 \
23174 if (s) \
23175 { \
23176 set_glyph_string_background_width (s, START, LAST_X); \
23177 (X) += s->width; \
23178 } \
23179 } \
23180 } while (0)
23181
23182
23183 /* Draw glyphs between START and END in AREA of ROW on window W,
23184 starting at x-position X. X is relative to AREA in W. HL is a
23185 face-override with the following meaning:
23186
23187 DRAW_NORMAL_TEXT draw normally
23188 DRAW_CURSOR draw in cursor face
23189 DRAW_MOUSE_FACE draw in mouse face.
23190 DRAW_INVERSE_VIDEO draw in mode line face
23191 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23192 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23193
23194 If OVERLAPS is non-zero, draw only the foreground of characters and
23195 clip to the physical height of ROW. Non-zero value also defines
23196 the overlapping part to be drawn:
23197
23198 OVERLAPS_PRED overlap with preceding rows
23199 OVERLAPS_SUCC overlap with succeeding rows
23200 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23201 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23202
23203 Value is the x-position reached, relative to AREA of W. */
23204
23205 static int
23206 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23207 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23208 enum draw_glyphs_face hl, int overlaps)
23209 {
23210 struct glyph_string *head, *tail;
23211 struct glyph_string *s;
23212 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23213 int i, j, x_reached, last_x, area_left = 0;
23214 struct frame *f = XFRAME (WINDOW_FRAME (w));
23215 DECLARE_HDC (hdc);
23216
23217 ALLOCATE_HDC (hdc, f);
23218
23219 /* Let's rather be paranoid than getting a SEGV. */
23220 end = min (end, row->used[area]);
23221 start = max (0, start);
23222 start = min (end, start);
23223
23224 /* Translate X to frame coordinates. Set last_x to the right
23225 end of the drawing area. */
23226 if (row->full_width_p)
23227 {
23228 /* X is relative to the left edge of W, without scroll bars
23229 or fringes. */
23230 area_left = WINDOW_LEFT_EDGE_X (w);
23231 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23232 }
23233 else
23234 {
23235 area_left = window_box_left (w, area);
23236 last_x = area_left + window_box_width (w, area);
23237 }
23238 x += area_left;
23239
23240 /* Build a doubly-linked list of glyph_string structures between
23241 head and tail from what we have to draw. Note that the macro
23242 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23243 the reason we use a separate variable `i'. */
23244 i = start;
23245 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23246 if (tail)
23247 x_reached = tail->x + tail->background_width;
23248 else
23249 x_reached = x;
23250
23251 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23252 the row, redraw some glyphs in front or following the glyph
23253 strings built above. */
23254 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23255 {
23256 struct glyph_string *h, *t;
23257 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23258 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23259 int check_mouse_face = 0;
23260 int dummy_x = 0;
23261
23262 /* If mouse highlighting is on, we may need to draw adjacent
23263 glyphs using mouse-face highlighting. */
23264 if (area == TEXT_AREA && row->mouse_face_p)
23265 {
23266 struct glyph_row *mouse_beg_row, *mouse_end_row;
23267
23268 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23269 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23270
23271 if (row >= mouse_beg_row && row <= mouse_end_row)
23272 {
23273 check_mouse_face = 1;
23274 mouse_beg_col = (row == mouse_beg_row)
23275 ? hlinfo->mouse_face_beg_col : 0;
23276 mouse_end_col = (row == mouse_end_row)
23277 ? hlinfo->mouse_face_end_col
23278 : row->used[TEXT_AREA];
23279 }
23280 }
23281
23282 /* Compute overhangs for all glyph strings. */
23283 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23284 for (s = head; s; s = s->next)
23285 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23286
23287 /* Prepend glyph strings for glyphs in front of the first glyph
23288 string that are overwritten because of the first glyph
23289 string's left overhang. The background of all strings
23290 prepended must be drawn because the first glyph string
23291 draws over it. */
23292 i = left_overwritten (head);
23293 if (i >= 0)
23294 {
23295 enum draw_glyphs_face overlap_hl;
23296
23297 /* If this row contains mouse highlighting, attempt to draw
23298 the overlapped glyphs with the correct highlight. This
23299 code fails if the overlap encompasses more than one glyph
23300 and mouse-highlight spans only some of these glyphs.
23301 However, making it work perfectly involves a lot more
23302 code, and I don't know if the pathological case occurs in
23303 practice, so we'll stick to this for now. --- cyd */
23304 if (check_mouse_face
23305 && mouse_beg_col < start && mouse_end_col > i)
23306 overlap_hl = DRAW_MOUSE_FACE;
23307 else
23308 overlap_hl = DRAW_NORMAL_TEXT;
23309
23310 j = i;
23311 BUILD_GLYPH_STRINGS (j, start, h, t,
23312 overlap_hl, dummy_x, last_x);
23313 start = i;
23314 compute_overhangs_and_x (t, head->x, 1);
23315 prepend_glyph_string_lists (&head, &tail, h, t);
23316 clip_head = head;
23317 }
23318
23319 /* Prepend glyph strings for glyphs in front of the first glyph
23320 string that overwrite that glyph string because of their
23321 right overhang. For these strings, only the foreground must
23322 be drawn, because it draws over the glyph string at `head'.
23323 The background must not be drawn because this would overwrite
23324 right overhangs of preceding glyphs for which no glyph
23325 strings exist. */
23326 i = left_overwriting (head);
23327 if (i >= 0)
23328 {
23329 enum draw_glyphs_face overlap_hl;
23330
23331 if (check_mouse_face
23332 && mouse_beg_col < start && mouse_end_col > i)
23333 overlap_hl = DRAW_MOUSE_FACE;
23334 else
23335 overlap_hl = DRAW_NORMAL_TEXT;
23336
23337 clip_head = head;
23338 BUILD_GLYPH_STRINGS (i, start, h, t,
23339 overlap_hl, dummy_x, last_x);
23340 for (s = h; s; s = s->next)
23341 s->background_filled_p = 1;
23342 compute_overhangs_and_x (t, head->x, 1);
23343 prepend_glyph_string_lists (&head, &tail, h, t);
23344 }
23345
23346 /* Append glyphs strings for glyphs following the last glyph
23347 string tail that are overwritten by tail. The background of
23348 these strings has to be drawn because tail's foreground draws
23349 over it. */
23350 i = right_overwritten (tail);
23351 if (i >= 0)
23352 {
23353 enum draw_glyphs_face overlap_hl;
23354
23355 if (check_mouse_face
23356 && mouse_beg_col < i && mouse_end_col > end)
23357 overlap_hl = DRAW_MOUSE_FACE;
23358 else
23359 overlap_hl = DRAW_NORMAL_TEXT;
23360
23361 BUILD_GLYPH_STRINGS (end, i, h, t,
23362 overlap_hl, x, last_x);
23363 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23364 we don't have `end = i;' here. */
23365 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23366 append_glyph_string_lists (&head, &tail, h, t);
23367 clip_tail = tail;
23368 }
23369
23370 /* Append glyph strings for glyphs following the last glyph
23371 string tail that overwrite tail. The foreground of such
23372 glyphs has to be drawn because it writes into the background
23373 of tail. The background must not be drawn because it could
23374 paint over the foreground of following glyphs. */
23375 i = right_overwriting (tail);
23376 if (i >= 0)
23377 {
23378 enum draw_glyphs_face overlap_hl;
23379 if (check_mouse_face
23380 && mouse_beg_col < i && mouse_end_col > end)
23381 overlap_hl = DRAW_MOUSE_FACE;
23382 else
23383 overlap_hl = DRAW_NORMAL_TEXT;
23384
23385 clip_tail = tail;
23386 i++; /* We must include the Ith glyph. */
23387 BUILD_GLYPH_STRINGS (end, i, h, t,
23388 overlap_hl, x, last_x);
23389 for (s = h; s; s = s->next)
23390 s->background_filled_p = 1;
23391 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23392 append_glyph_string_lists (&head, &tail, h, t);
23393 }
23394 if (clip_head || clip_tail)
23395 for (s = head; s; s = s->next)
23396 {
23397 s->clip_head = clip_head;
23398 s->clip_tail = clip_tail;
23399 }
23400 }
23401
23402 /* Draw all strings. */
23403 for (s = head; s; s = s->next)
23404 FRAME_RIF (f)->draw_glyph_string (s);
23405
23406 #ifndef HAVE_NS
23407 /* When focus a sole frame and move horizontally, this sets on_p to 0
23408 causing a failure to erase prev cursor position. */
23409 if (area == TEXT_AREA
23410 && !row->full_width_p
23411 /* When drawing overlapping rows, only the glyph strings'
23412 foreground is drawn, which doesn't erase a cursor
23413 completely. */
23414 && !overlaps)
23415 {
23416 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23417 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23418 : (tail ? tail->x + tail->background_width : x));
23419 x0 -= area_left;
23420 x1 -= area_left;
23421
23422 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23423 row->y, MATRIX_ROW_BOTTOM_Y (row));
23424 }
23425 #endif
23426
23427 /* Value is the x-position up to which drawn, relative to AREA of W.
23428 This doesn't include parts drawn because of overhangs. */
23429 if (row->full_width_p)
23430 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23431 else
23432 x_reached -= area_left;
23433
23434 RELEASE_HDC (hdc, f);
23435
23436 return x_reached;
23437 }
23438
23439 /* Expand row matrix if too narrow. Don't expand if area
23440 is not present. */
23441
23442 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23443 { \
23444 if (!fonts_changed_p \
23445 && (it->glyph_row->glyphs[area] \
23446 < it->glyph_row->glyphs[area + 1])) \
23447 { \
23448 it->w->ncols_scale_factor++; \
23449 fonts_changed_p = 1; \
23450 } \
23451 }
23452
23453 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23454 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23455
23456 static inline void
23457 append_glyph (struct it *it)
23458 {
23459 struct glyph *glyph;
23460 enum glyph_row_area area = it->area;
23461
23462 eassert (it->glyph_row);
23463 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23464
23465 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23466 if (glyph < it->glyph_row->glyphs[area + 1])
23467 {
23468 /* If the glyph row is reversed, we need to prepend the glyph
23469 rather than append it. */
23470 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23471 {
23472 struct glyph *g;
23473
23474 /* Make room for the additional glyph. */
23475 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23476 g[1] = *g;
23477 glyph = it->glyph_row->glyphs[area];
23478 }
23479 glyph->charpos = CHARPOS (it->position);
23480 glyph->object = it->object;
23481 if (it->pixel_width > 0)
23482 {
23483 glyph->pixel_width = it->pixel_width;
23484 glyph->padding_p = 0;
23485 }
23486 else
23487 {
23488 /* Assure at least 1-pixel width. Otherwise, cursor can't
23489 be displayed correctly. */
23490 glyph->pixel_width = 1;
23491 glyph->padding_p = 1;
23492 }
23493 glyph->ascent = it->ascent;
23494 glyph->descent = it->descent;
23495 glyph->voffset = it->voffset;
23496 glyph->type = CHAR_GLYPH;
23497 glyph->avoid_cursor_p = it->avoid_cursor_p;
23498 glyph->multibyte_p = it->multibyte_p;
23499 glyph->left_box_line_p = it->start_of_box_run_p;
23500 glyph->right_box_line_p = it->end_of_box_run_p;
23501 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23502 || it->phys_descent > it->descent);
23503 glyph->glyph_not_available_p = it->glyph_not_available_p;
23504 glyph->face_id = it->face_id;
23505 glyph->u.ch = it->char_to_display;
23506 glyph->slice.img = null_glyph_slice;
23507 glyph->font_type = FONT_TYPE_UNKNOWN;
23508 if (it->bidi_p)
23509 {
23510 glyph->resolved_level = it->bidi_it.resolved_level;
23511 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23512 abort ();
23513 glyph->bidi_type = it->bidi_it.type;
23514 }
23515 else
23516 {
23517 glyph->resolved_level = 0;
23518 glyph->bidi_type = UNKNOWN_BT;
23519 }
23520 ++it->glyph_row->used[area];
23521 }
23522 else
23523 IT_EXPAND_MATRIX_WIDTH (it, area);
23524 }
23525
23526 /* Store one glyph for the composition IT->cmp_it.id in
23527 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23528 non-null. */
23529
23530 static inline void
23531 append_composite_glyph (struct it *it)
23532 {
23533 struct glyph *glyph;
23534 enum glyph_row_area area = it->area;
23535
23536 eassert (it->glyph_row);
23537
23538 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23539 if (glyph < it->glyph_row->glyphs[area + 1])
23540 {
23541 /* If the glyph row is reversed, we need to prepend the glyph
23542 rather than append it. */
23543 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23544 {
23545 struct glyph *g;
23546
23547 /* Make room for the new glyph. */
23548 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23549 g[1] = *g;
23550 glyph = it->glyph_row->glyphs[it->area];
23551 }
23552 glyph->charpos = it->cmp_it.charpos;
23553 glyph->object = it->object;
23554 glyph->pixel_width = it->pixel_width;
23555 glyph->ascent = it->ascent;
23556 glyph->descent = it->descent;
23557 glyph->voffset = it->voffset;
23558 glyph->type = COMPOSITE_GLYPH;
23559 if (it->cmp_it.ch < 0)
23560 {
23561 glyph->u.cmp.automatic = 0;
23562 glyph->u.cmp.id = it->cmp_it.id;
23563 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23564 }
23565 else
23566 {
23567 glyph->u.cmp.automatic = 1;
23568 glyph->u.cmp.id = it->cmp_it.id;
23569 glyph->slice.cmp.from = it->cmp_it.from;
23570 glyph->slice.cmp.to = it->cmp_it.to - 1;
23571 }
23572 glyph->avoid_cursor_p = it->avoid_cursor_p;
23573 glyph->multibyte_p = it->multibyte_p;
23574 glyph->left_box_line_p = it->start_of_box_run_p;
23575 glyph->right_box_line_p = it->end_of_box_run_p;
23576 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23577 || it->phys_descent > it->descent);
23578 glyph->padding_p = 0;
23579 glyph->glyph_not_available_p = 0;
23580 glyph->face_id = it->face_id;
23581 glyph->font_type = FONT_TYPE_UNKNOWN;
23582 if (it->bidi_p)
23583 {
23584 glyph->resolved_level = it->bidi_it.resolved_level;
23585 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23586 abort ();
23587 glyph->bidi_type = it->bidi_it.type;
23588 }
23589 ++it->glyph_row->used[area];
23590 }
23591 else
23592 IT_EXPAND_MATRIX_WIDTH (it, area);
23593 }
23594
23595
23596 /* Change IT->ascent and IT->height according to the setting of
23597 IT->voffset. */
23598
23599 static inline void
23600 take_vertical_position_into_account (struct it *it)
23601 {
23602 if (it->voffset)
23603 {
23604 if (it->voffset < 0)
23605 /* Increase the ascent so that we can display the text higher
23606 in the line. */
23607 it->ascent -= it->voffset;
23608 else
23609 /* Increase the descent so that we can display the text lower
23610 in the line. */
23611 it->descent += it->voffset;
23612 }
23613 }
23614
23615
23616 /* Produce glyphs/get display metrics for the image IT is loaded with.
23617 See the description of struct display_iterator in dispextern.h for
23618 an overview of struct display_iterator. */
23619
23620 static void
23621 produce_image_glyph (struct it *it)
23622 {
23623 struct image *img;
23624 struct face *face;
23625 int glyph_ascent, crop;
23626 struct glyph_slice slice;
23627
23628 eassert (it->what == IT_IMAGE);
23629
23630 face = FACE_FROM_ID (it->f, it->face_id);
23631 eassert (face);
23632 /* Make sure X resources of the face is loaded. */
23633 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23634
23635 if (it->image_id < 0)
23636 {
23637 /* Fringe bitmap. */
23638 it->ascent = it->phys_ascent = 0;
23639 it->descent = it->phys_descent = 0;
23640 it->pixel_width = 0;
23641 it->nglyphs = 0;
23642 return;
23643 }
23644
23645 img = IMAGE_FROM_ID (it->f, it->image_id);
23646 eassert (img);
23647 /* Make sure X resources of the image is loaded. */
23648 prepare_image_for_display (it->f, img);
23649
23650 slice.x = slice.y = 0;
23651 slice.width = img->width;
23652 slice.height = img->height;
23653
23654 if (INTEGERP (it->slice.x))
23655 slice.x = XINT (it->slice.x);
23656 else if (FLOATP (it->slice.x))
23657 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23658
23659 if (INTEGERP (it->slice.y))
23660 slice.y = XINT (it->slice.y);
23661 else if (FLOATP (it->slice.y))
23662 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23663
23664 if (INTEGERP (it->slice.width))
23665 slice.width = XINT (it->slice.width);
23666 else if (FLOATP (it->slice.width))
23667 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23668
23669 if (INTEGERP (it->slice.height))
23670 slice.height = XINT (it->slice.height);
23671 else if (FLOATP (it->slice.height))
23672 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23673
23674 if (slice.x >= img->width)
23675 slice.x = img->width;
23676 if (slice.y >= img->height)
23677 slice.y = img->height;
23678 if (slice.x + slice.width >= img->width)
23679 slice.width = img->width - slice.x;
23680 if (slice.y + slice.height > img->height)
23681 slice.height = img->height - slice.y;
23682
23683 if (slice.width == 0 || slice.height == 0)
23684 return;
23685
23686 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23687
23688 it->descent = slice.height - glyph_ascent;
23689 if (slice.y == 0)
23690 it->descent += img->vmargin;
23691 if (slice.y + slice.height == img->height)
23692 it->descent += img->vmargin;
23693 it->phys_descent = it->descent;
23694
23695 it->pixel_width = slice.width;
23696 if (slice.x == 0)
23697 it->pixel_width += img->hmargin;
23698 if (slice.x + slice.width == img->width)
23699 it->pixel_width += img->hmargin;
23700
23701 /* It's quite possible for images to have an ascent greater than
23702 their height, so don't get confused in that case. */
23703 if (it->descent < 0)
23704 it->descent = 0;
23705
23706 it->nglyphs = 1;
23707
23708 if (face->box != FACE_NO_BOX)
23709 {
23710 if (face->box_line_width > 0)
23711 {
23712 if (slice.y == 0)
23713 it->ascent += face->box_line_width;
23714 if (slice.y + slice.height == img->height)
23715 it->descent += face->box_line_width;
23716 }
23717
23718 if (it->start_of_box_run_p && slice.x == 0)
23719 it->pixel_width += eabs (face->box_line_width);
23720 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23721 it->pixel_width += eabs (face->box_line_width);
23722 }
23723
23724 take_vertical_position_into_account (it);
23725
23726 /* Automatically crop wide image glyphs at right edge so we can
23727 draw the cursor on same display row. */
23728 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23729 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23730 {
23731 it->pixel_width -= crop;
23732 slice.width -= crop;
23733 }
23734
23735 if (it->glyph_row)
23736 {
23737 struct glyph *glyph;
23738 enum glyph_row_area area = it->area;
23739
23740 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23741 if (glyph < it->glyph_row->glyphs[area + 1])
23742 {
23743 glyph->charpos = CHARPOS (it->position);
23744 glyph->object = it->object;
23745 glyph->pixel_width = it->pixel_width;
23746 glyph->ascent = glyph_ascent;
23747 glyph->descent = it->descent;
23748 glyph->voffset = it->voffset;
23749 glyph->type = IMAGE_GLYPH;
23750 glyph->avoid_cursor_p = it->avoid_cursor_p;
23751 glyph->multibyte_p = it->multibyte_p;
23752 glyph->left_box_line_p = it->start_of_box_run_p;
23753 glyph->right_box_line_p = it->end_of_box_run_p;
23754 glyph->overlaps_vertically_p = 0;
23755 glyph->padding_p = 0;
23756 glyph->glyph_not_available_p = 0;
23757 glyph->face_id = it->face_id;
23758 glyph->u.img_id = img->id;
23759 glyph->slice.img = slice;
23760 glyph->font_type = FONT_TYPE_UNKNOWN;
23761 if (it->bidi_p)
23762 {
23763 glyph->resolved_level = it->bidi_it.resolved_level;
23764 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23765 abort ();
23766 glyph->bidi_type = it->bidi_it.type;
23767 }
23768 ++it->glyph_row->used[area];
23769 }
23770 else
23771 IT_EXPAND_MATRIX_WIDTH (it, area);
23772 }
23773 }
23774
23775
23776 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23777 of the glyph, WIDTH and HEIGHT are the width and height of the
23778 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23779
23780 static void
23781 append_stretch_glyph (struct it *it, Lisp_Object object,
23782 int width, int height, int ascent)
23783 {
23784 struct glyph *glyph;
23785 enum glyph_row_area area = it->area;
23786
23787 eassert (ascent >= 0 && ascent <= height);
23788
23789 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23790 if (glyph < it->glyph_row->glyphs[area + 1])
23791 {
23792 /* If the glyph row is reversed, we need to prepend the glyph
23793 rather than append it. */
23794 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23795 {
23796 struct glyph *g;
23797
23798 /* Make room for the additional glyph. */
23799 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23800 g[1] = *g;
23801 glyph = it->glyph_row->glyphs[area];
23802 }
23803 glyph->charpos = CHARPOS (it->position);
23804 glyph->object = object;
23805 glyph->pixel_width = width;
23806 glyph->ascent = ascent;
23807 glyph->descent = height - ascent;
23808 glyph->voffset = it->voffset;
23809 glyph->type = STRETCH_GLYPH;
23810 glyph->avoid_cursor_p = it->avoid_cursor_p;
23811 glyph->multibyte_p = it->multibyte_p;
23812 glyph->left_box_line_p = it->start_of_box_run_p;
23813 glyph->right_box_line_p = it->end_of_box_run_p;
23814 glyph->overlaps_vertically_p = 0;
23815 glyph->padding_p = 0;
23816 glyph->glyph_not_available_p = 0;
23817 glyph->face_id = it->face_id;
23818 glyph->u.stretch.ascent = ascent;
23819 glyph->u.stretch.height = height;
23820 glyph->slice.img = null_glyph_slice;
23821 glyph->font_type = FONT_TYPE_UNKNOWN;
23822 if (it->bidi_p)
23823 {
23824 glyph->resolved_level = it->bidi_it.resolved_level;
23825 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23826 abort ();
23827 glyph->bidi_type = it->bidi_it.type;
23828 }
23829 else
23830 {
23831 glyph->resolved_level = 0;
23832 glyph->bidi_type = UNKNOWN_BT;
23833 }
23834 ++it->glyph_row->used[area];
23835 }
23836 else
23837 IT_EXPAND_MATRIX_WIDTH (it, area);
23838 }
23839
23840 #endif /* HAVE_WINDOW_SYSTEM */
23841
23842 /* Produce a stretch glyph for iterator IT. IT->object is the value
23843 of the glyph property displayed. The value must be a list
23844 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23845 being recognized:
23846
23847 1. `:width WIDTH' specifies that the space should be WIDTH *
23848 canonical char width wide. WIDTH may be an integer or floating
23849 point number.
23850
23851 2. `:relative-width FACTOR' specifies that the width of the stretch
23852 should be computed from the width of the first character having the
23853 `glyph' property, and should be FACTOR times that width.
23854
23855 3. `:align-to HPOS' specifies that the space should be wide enough
23856 to reach HPOS, a value in canonical character units.
23857
23858 Exactly one of the above pairs must be present.
23859
23860 4. `:height HEIGHT' specifies that the height of the stretch produced
23861 should be HEIGHT, measured in canonical character units.
23862
23863 5. `:relative-height FACTOR' specifies that the height of the
23864 stretch should be FACTOR times the height of the characters having
23865 the glyph property.
23866
23867 Either none or exactly one of 4 or 5 must be present.
23868
23869 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23870 of the stretch should be used for the ascent of the stretch.
23871 ASCENT must be in the range 0 <= ASCENT <= 100. */
23872
23873 void
23874 produce_stretch_glyph (struct it *it)
23875 {
23876 /* (space :width WIDTH :height HEIGHT ...) */
23877 Lisp_Object prop, plist;
23878 int width = 0, height = 0, align_to = -1;
23879 int zero_width_ok_p = 0;
23880 int ascent = 0;
23881 double tem;
23882 struct face *face = NULL;
23883 struct font *font = NULL;
23884
23885 #ifdef HAVE_WINDOW_SYSTEM
23886 int zero_height_ok_p = 0;
23887
23888 if (FRAME_WINDOW_P (it->f))
23889 {
23890 face = FACE_FROM_ID (it->f, it->face_id);
23891 font = face->font ? face->font : FRAME_FONT (it->f);
23892 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23893 }
23894 #endif
23895
23896 /* List should start with `space'. */
23897 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23898 plist = XCDR (it->object);
23899
23900 /* Compute the width of the stretch. */
23901 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23902 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23903 {
23904 /* Absolute width `:width WIDTH' specified and valid. */
23905 zero_width_ok_p = 1;
23906 width = (int)tem;
23907 }
23908 #ifdef HAVE_WINDOW_SYSTEM
23909 else if (FRAME_WINDOW_P (it->f)
23910 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23911 {
23912 /* Relative width `:relative-width FACTOR' specified and valid.
23913 Compute the width of the characters having the `glyph'
23914 property. */
23915 struct it it2;
23916 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23917
23918 it2 = *it;
23919 if (it->multibyte_p)
23920 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23921 else
23922 {
23923 it2.c = it2.char_to_display = *p, it2.len = 1;
23924 if (! ASCII_CHAR_P (it2.c))
23925 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23926 }
23927
23928 it2.glyph_row = NULL;
23929 it2.what = IT_CHARACTER;
23930 x_produce_glyphs (&it2);
23931 width = NUMVAL (prop) * it2.pixel_width;
23932 }
23933 #endif /* HAVE_WINDOW_SYSTEM */
23934 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23935 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23936 {
23937 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23938 align_to = (align_to < 0
23939 ? 0
23940 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23941 else if (align_to < 0)
23942 align_to = window_box_left_offset (it->w, TEXT_AREA);
23943 width = max (0, (int)tem + align_to - it->current_x);
23944 zero_width_ok_p = 1;
23945 }
23946 else
23947 /* Nothing specified -> width defaults to canonical char width. */
23948 width = FRAME_COLUMN_WIDTH (it->f);
23949
23950 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23951 width = 1;
23952
23953 #ifdef HAVE_WINDOW_SYSTEM
23954 /* Compute height. */
23955 if (FRAME_WINDOW_P (it->f))
23956 {
23957 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23958 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23959 {
23960 height = (int)tem;
23961 zero_height_ok_p = 1;
23962 }
23963 else if (prop = Fplist_get (plist, QCrelative_height),
23964 NUMVAL (prop) > 0)
23965 height = FONT_HEIGHT (font) * NUMVAL (prop);
23966 else
23967 height = FONT_HEIGHT (font);
23968
23969 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23970 height = 1;
23971
23972 /* Compute percentage of height used for ascent. If
23973 `:ascent ASCENT' is present and valid, use that. Otherwise,
23974 derive the ascent from the font in use. */
23975 if (prop = Fplist_get (plist, QCascent),
23976 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23977 ascent = height * NUMVAL (prop) / 100.0;
23978 else if (!NILP (prop)
23979 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23980 ascent = min (max (0, (int)tem), height);
23981 else
23982 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23983 }
23984 else
23985 #endif /* HAVE_WINDOW_SYSTEM */
23986 height = 1;
23987
23988 if (width > 0 && it->line_wrap != TRUNCATE
23989 && it->current_x + width > it->last_visible_x)
23990 {
23991 width = it->last_visible_x - it->current_x;
23992 #ifdef HAVE_WINDOW_SYSTEM
23993 /* Subtract one more pixel from the stretch width, but only on
23994 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23995 width -= FRAME_WINDOW_P (it->f);
23996 #endif
23997 }
23998
23999 if (width > 0 && height > 0 && it->glyph_row)
24000 {
24001 Lisp_Object o_object = it->object;
24002 Lisp_Object object = it->stack[it->sp - 1].string;
24003 int n = width;
24004
24005 if (!STRINGP (object))
24006 object = it->w->buffer;
24007 #ifdef HAVE_WINDOW_SYSTEM
24008 if (FRAME_WINDOW_P (it->f))
24009 append_stretch_glyph (it, object, width, height, ascent);
24010 else
24011 #endif
24012 {
24013 it->object = object;
24014 it->char_to_display = ' ';
24015 it->pixel_width = it->len = 1;
24016 while (n--)
24017 tty_append_glyph (it);
24018 it->object = o_object;
24019 }
24020 }
24021
24022 it->pixel_width = width;
24023 #ifdef HAVE_WINDOW_SYSTEM
24024 if (FRAME_WINDOW_P (it->f))
24025 {
24026 it->ascent = it->phys_ascent = ascent;
24027 it->descent = it->phys_descent = height - it->ascent;
24028 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24029 take_vertical_position_into_account (it);
24030 }
24031 else
24032 #endif
24033 it->nglyphs = width;
24034 }
24035
24036 #ifdef HAVE_WINDOW_SYSTEM
24037
24038 /* Calculate line-height and line-spacing properties.
24039 An integer value specifies explicit pixel value.
24040 A float value specifies relative value to current face height.
24041 A cons (float . face-name) specifies relative value to
24042 height of specified face font.
24043
24044 Returns height in pixels, or nil. */
24045
24046
24047 static Lisp_Object
24048 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24049 int boff, int override)
24050 {
24051 Lisp_Object face_name = Qnil;
24052 int ascent, descent, height;
24053
24054 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24055 return val;
24056
24057 if (CONSP (val))
24058 {
24059 face_name = XCAR (val);
24060 val = XCDR (val);
24061 if (!NUMBERP (val))
24062 val = make_number (1);
24063 if (NILP (face_name))
24064 {
24065 height = it->ascent + it->descent;
24066 goto scale;
24067 }
24068 }
24069
24070 if (NILP (face_name))
24071 {
24072 font = FRAME_FONT (it->f);
24073 boff = FRAME_BASELINE_OFFSET (it->f);
24074 }
24075 else if (EQ (face_name, Qt))
24076 {
24077 override = 0;
24078 }
24079 else
24080 {
24081 int face_id;
24082 struct face *face;
24083
24084 face_id = lookup_named_face (it->f, face_name, 0);
24085 if (face_id < 0)
24086 return make_number (-1);
24087
24088 face = FACE_FROM_ID (it->f, face_id);
24089 font = face->font;
24090 if (font == NULL)
24091 return make_number (-1);
24092 boff = font->baseline_offset;
24093 if (font->vertical_centering)
24094 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24095 }
24096
24097 ascent = FONT_BASE (font) + boff;
24098 descent = FONT_DESCENT (font) - boff;
24099
24100 if (override)
24101 {
24102 it->override_ascent = ascent;
24103 it->override_descent = descent;
24104 it->override_boff = boff;
24105 }
24106
24107 height = ascent + descent;
24108
24109 scale:
24110 if (FLOATP (val))
24111 height = (int)(XFLOAT_DATA (val) * height);
24112 else if (INTEGERP (val))
24113 height *= XINT (val);
24114
24115 return make_number (height);
24116 }
24117
24118
24119 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24120 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24121 and only if this is for a character for which no font was found.
24122
24123 If the display method (it->glyphless_method) is
24124 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24125 length of the acronym or the hexadecimal string, UPPER_XOFF and
24126 UPPER_YOFF are pixel offsets for the upper part of the string,
24127 LOWER_XOFF and LOWER_YOFF are for the lower part.
24128
24129 For the other display methods, LEN through LOWER_YOFF are zero. */
24130
24131 static void
24132 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24133 short upper_xoff, short upper_yoff,
24134 short lower_xoff, short lower_yoff)
24135 {
24136 struct glyph *glyph;
24137 enum glyph_row_area area = it->area;
24138
24139 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24140 if (glyph < it->glyph_row->glyphs[area + 1])
24141 {
24142 /* If the glyph row is reversed, we need to prepend the glyph
24143 rather than append it. */
24144 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24145 {
24146 struct glyph *g;
24147
24148 /* Make room for the additional glyph. */
24149 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24150 g[1] = *g;
24151 glyph = it->glyph_row->glyphs[area];
24152 }
24153 glyph->charpos = CHARPOS (it->position);
24154 glyph->object = it->object;
24155 glyph->pixel_width = it->pixel_width;
24156 glyph->ascent = it->ascent;
24157 glyph->descent = it->descent;
24158 glyph->voffset = it->voffset;
24159 glyph->type = GLYPHLESS_GLYPH;
24160 glyph->u.glyphless.method = it->glyphless_method;
24161 glyph->u.glyphless.for_no_font = for_no_font;
24162 glyph->u.glyphless.len = len;
24163 glyph->u.glyphless.ch = it->c;
24164 glyph->slice.glyphless.upper_xoff = upper_xoff;
24165 glyph->slice.glyphless.upper_yoff = upper_yoff;
24166 glyph->slice.glyphless.lower_xoff = lower_xoff;
24167 glyph->slice.glyphless.lower_yoff = lower_yoff;
24168 glyph->avoid_cursor_p = it->avoid_cursor_p;
24169 glyph->multibyte_p = it->multibyte_p;
24170 glyph->left_box_line_p = it->start_of_box_run_p;
24171 glyph->right_box_line_p = it->end_of_box_run_p;
24172 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24173 || it->phys_descent > it->descent);
24174 glyph->padding_p = 0;
24175 glyph->glyph_not_available_p = 0;
24176 glyph->face_id = face_id;
24177 glyph->font_type = FONT_TYPE_UNKNOWN;
24178 if (it->bidi_p)
24179 {
24180 glyph->resolved_level = it->bidi_it.resolved_level;
24181 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24182 abort ();
24183 glyph->bidi_type = it->bidi_it.type;
24184 }
24185 ++it->glyph_row->used[area];
24186 }
24187 else
24188 IT_EXPAND_MATRIX_WIDTH (it, area);
24189 }
24190
24191
24192 /* Produce a glyph for a glyphless character for iterator IT.
24193 IT->glyphless_method specifies which method to use for displaying
24194 the character. See the description of enum
24195 glyphless_display_method in dispextern.h for the detail.
24196
24197 FOR_NO_FONT is nonzero if and only if this is for a character for
24198 which no font was found. ACRONYM, if non-nil, is an acronym string
24199 for the character. */
24200
24201 static void
24202 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24203 {
24204 int face_id;
24205 struct face *face;
24206 struct font *font;
24207 int base_width, base_height, width, height;
24208 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24209 int len;
24210
24211 /* Get the metrics of the base font. We always refer to the current
24212 ASCII face. */
24213 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24214 font = face->font ? face->font : FRAME_FONT (it->f);
24215 it->ascent = FONT_BASE (font) + font->baseline_offset;
24216 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24217 base_height = it->ascent + it->descent;
24218 base_width = font->average_width;
24219
24220 /* Get a face ID for the glyph by utilizing a cache (the same way as
24221 done for `escape-glyph' in get_next_display_element). */
24222 if (it->f == last_glyphless_glyph_frame
24223 && it->face_id == last_glyphless_glyph_face_id)
24224 {
24225 face_id = last_glyphless_glyph_merged_face_id;
24226 }
24227 else
24228 {
24229 /* Merge the `glyphless-char' face into the current face. */
24230 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24231 last_glyphless_glyph_frame = it->f;
24232 last_glyphless_glyph_face_id = it->face_id;
24233 last_glyphless_glyph_merged_face_id = face_id;
24234 }
24235
24236 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24237 {
24238 it->pixel_width = THIN_SPACE_WIDTH;
24239 len = 0;
24240 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24241 }
24242 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24243 {
24244 width = CHAR_WIDTH (it->c);
24245 if (width == 0)
24246 width = 1;
24247 else if (width > 4)
24248 width = 4;
24249 it->pixel_width = base_width * width;
24250 len = 0;
24251 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24252 }
24253 else
24254 {
24255 char buf[7];
24256 const char *str;
24257 unsigned int code[6];
24258 int upper_len;
24259 int ascent, descent;
24260 struct font_metrics metrics_upper, metrics_lower;
24261
24262 face = FACE_FROM_ID (it->f, face_id);
24263 font = face->font ? face->font : FRAME_FONT (it->f);
24264 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24265
24266 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24267 {
24268 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24269 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24270 if (CONSP (acronym))
24271 acronym = XCAR (acronym);
24272 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24273 }
24274 else
24275 {
24276 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24277 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24278 str = buf;
24279 }
24280 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24281 code[len] = font->driver->encode_char (font, str[len]);
24282 upper_len = (len + 1) / 2;
24283 font->driver->text_extents (font, code, upper_len,
24284 &metrics_upper);
24285 font->driver->text_extents (font, code + upper_len, len - upper_len,
24286 &metrics_lower);
24287
24288
24289
24290 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24291 width = max (metrics_upper.width, metrics_lower.width) + 4;
24292 upper_xoff = upper_yoff = 2; /* the typical case */
24293 if (base_width >= width)
24294 {
24295 /* Align the upper to the left, the lower to the right. */
24296 it->pixel_width = base_width;
24297 lower_xoff = base_width - 2 - metrics_lower.width;
24298 }
24299 else
24300 {
24301 /* Center the shorter one. */
24302 it->pixel_width = width;
24303 if (metrics_upper.width >= metrics_lower.width)
24304 lower_xoff = (width - metrics_lower.width) / 2;
24305 else
24306 {
24307 /* FIXME: This code doesn't look right. It formerly was
24308 missing the "lower_xoff = 0;", which couldn't have
24309 been right since it left lower_xoff uninitialized. */
24310 lower_xoff = 0;
24311 upper_xoff = (width - metrics_upper.width) / 2;
24312 }
24313 }
24314
24315 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24316 top, bottom, and between upper and lower strings. */
24317 height = (metrics_upper.ascent + metrics_upper.descent
24318 + metrics_lower.ascent + metrics_lower.descent) + 5;
24319 /* Center vertically.
24320 H:base_height, D:base_descent
24321 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24322
24323 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24324 descent = D - H/2 + h/2;
24325 lower_yoff = descent - 2 - ld;
24326 upper_yoff = lower_yoff - la - 1 - ud; */
24327 ascent = - (it->descent - (base_height + height + 1) / 2);
24328 descent = it->descent - (base_height - height) / 2;
24329 lower_yoff = descent - 2 - metrics_lower.descent;
24330 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24331 - metrics_upper.descent);
24332 /* Don't make the height shorter than the base height. */
24333 if (height > base_height)
24334 {
24335 it->ascent = ascent;
24336 it->descent = descent;
24337 }
24338 }
24339
24340 it->phys_ascent = it->ascent;
24341 it->phys_descent = it->descent;
24342 if (it->glyph_row)
24343 append_glyphless_glyph (it, face_id, for_no_font, len,
24344 upper_xoff, upper_yoff,
24345 lower_xoff, lower_yoff);
24346 it->nglyphs = 1;
24347 take_vertical_position_into_account (it);
24348 }
24349
24350
24351 /* RIF:
24352 Produce glyphs/get display metrics for the display element IT is
24353 loaded with. See the description of struct it in dispextern.h
24354 for an overview of struct it. */
24355
24356 void
24357 x_produce_glyphs (struct it *it)
24358 {
24359 int extra_line_spacing = it->extra_line_spacing;
24360
24361 it->glyph_not_available_p = 0;
24362
24363 if (it->what == IT_CHARACTER)
24364 {
24365 XChar2b char2b;
24366 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24367 struct font *font = face->font;
24368 struct font_metrics *pcm = NULL;
24369 int boff; /* baseline offset */
24370
24371 if (font == NULL)
24372 {
24373 /* When no suitable font is found, display this character by
24374 the method specified in the first extra slot of
24375 Vglyphless_char_display. */
24376 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24377
24378 eassert (it->what == IT_GLYPHLESS);
24379 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24380 goto done;
24381 }
24382
24383 boff = font->baseline_offset;
24384 if (font->vertical_centering)
24385 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24386
24387 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24388 {
24389 int stretched_p;
24390
24391 it->nglyphs = 1;
24392
24393 if (it->override_ascent >= 0)
24394 {
24395 it->ascent = it->override_ascent;
24396 it->descent = it->override_descent;
24397 boff = it->override_boff;
24398 }
24399 else
24400 {
24401 it->ascent = FONT_BASE (font) + boff;
24402 it->descent = FONT_DESCENT (font) - boff;
24403 }
24404
24405 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24406 {
24407 pcm = get_per_char_metric (font, &char2b);
24408 if (pcm->width == 0
24409 && pcm->rbearing == 0 && pcm->lbearing == 0)
24410 pcm = NULL;
24411 }
24412
24413 if (pcm)
24414 {
24415 it->phys_ascent = pcm->ascent + boff;
24416 it->phys_descent = pcm->descent - boff;
24417 it->pixel_width = pcm->width;
24418 }
24419 else
24420 {
24421 it->glyph_not_available_p = 1;
24422 it->phys_ascent = it->ascent;
24423 it->phys_descent = it->descent;
24424 it->pixel_width = font->space_width;
24425 }
24426
24427 if (it->constrain_row_ascent_descent_p)
24428 {
24429 if (it->descent > it->max_descent)
24430 {
24431 it->ascent += it->descent - it->max_descent;
24432 it->descent = it->max_descent;
24433 }
24434 if (it->ascent > it->max_ascent)
24435 {
24436 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24437 it->ascent = it->max_ascent;
24438 }
24439 it->phys_ascent = min (it->phys_ascent, it->ascent);
24440 it->phys_descent = min (it->phys_descent, it->descent);
24441 extra_line_spacing = 0;
24442 }
24443
24444 /* If this is a space inside a region of text with
24445 `space-width' property, change its width. */
24446 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24447 if (stretched_p)
24448 it->pixel_width *= XFLOATINT (it->space_width);
24449
24450 /* If face has a box, add the box thickness to the character
24451 height. If character has a box line to the left and/or
24452 right, add the box line width to the character's width. */
24453 if (face->box != FACE_NO_BOX)
24454 {
24455 int thick = face->box_line_width;
24456
24457 if (thick > 0)
24458 {
24459 it->ascent += thick;
24460 it->descent += thick;
24461 }
24462 else
24463 thick = -thick;
24464
24465 if (it->start_of_box_run_p)
24466 it->pixel_width += thick;
24467 if (it->end_of_box_run_p)
24468 it->pixel_width += thick;
24469 }
24470
24471 /* If face has an overline, add the height of the overline
24472 (1 pixel) and a 1 pixel margin to the character height. */
24473 if (face->overline_p)
24474 it->ascent += overline_margin;
24475
24476 if (it->constrain_row_ascent_descent_p)
24477 {
24478 if (it->ascent > it->max_ascent)
24479 it->ascent = it->max_ascent;
24480 if (it->descent > it->max_descent)
24481 it->descent = it->max_descent;
24482 }
24483
24484 take_vertical_position_into_account (it);
24485
24486 /* If we have to actually produce glyphs, do it. */
24487 if (it->glyph_row)
24488 {
24489 if (stretched_p)
24490 {
24491 /* Translate a space with a `space-width' property
24492 into a stretch glyph. */
24493 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24494 / FONT_HEIGHT (font));
24495 append_stretch_glyph (it, it->object, it->pixel_width,
24496 it->ascent + it->descent, ascent);
24497 }
24498 else
24499 append_glyph (it);
24500
24501 /* If characters with lbearing or rbearing are displayed
24502 in this line, record that fact in a flag of the
24503 glyph row. This is used to optimize X output code. */
24504 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24505 it->glyph_row->contains_overlapping_glyphs_p = 1;
24506 }
24507 if (! stretched_p && it->pixel_width == 0)
24508 /* We assure that all visible glyphs have at least 1-pixel
24509 width. */
24510 it->pixel_width = 1;
24511 }
24512 else if (it->char_to_display == '\n')
24513 {
24514 /* A newline has no width, but we need the height of the
24515 line. But if previous part of the line sets a height,
24516 don't increase that height */
24517
24518 Lisp_Object height;
24519 Lisp_Object total_height = Qnil;
24520
24521 it->override_ascent = -1;
24522 it->pixel_width = 0;
24523 it->nglyphs = 0;
24524
24525 height = get_it_property (it, Qline_height);
24526 /* Split (line-height total-height) list */
24527 if (CONSP (height)
24528 && CONSP (XCDR (height))
24529 && NILP (XCDR (XCDR (height))))
24530 {
24531 total_height = XCAR (XCDR (height));
24532 height = XCAR (height);
24533 }
24534 height = calc_line_height_property (it, height, font, boff, 1);
24535
24536 if (it->override_ascent >= 0)
24537 {
24538 it->ascent = it->override_ascent;
24539 it->descent = it->override_descent;
24540 boff = it->override_boff;
24541 }
24542 else
24543 {
24544 it->ascent = FONT_BASE (font) + boff;
24545 it->descent = FONT_DESCENT (font) - boff;
24546 }
24547
24548 if (EQ (height, Qt))
24549 {
24550 if (it->descent > it->max_descent)
24551 {
24552 it->ascent += it->descent - it->max_descent;
24553 it->descent = it->max_descent;
24554 }
24555 if (it->ascent > it->max_ascent)
24556 {
24557 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24558 it->ascent = it->max_ascent;
24559 }
24560 it->phys_ascent = min (it->phys_ascent, it->ascent);
24561 it->phys_descent = min (it->phys_descent, it->descent);
24562 it->constrain_row_ascent_descent_p = 1;
24563 extra_line_spacing = 0;
24564 }
24565 else
24566 {
24567 Lisp_Object spacing;
24568
24569 it->phys_ascent = it->ascent;
24570 it->phys_descent = it->descent;
24571
24572 if ((it->max_ascent > 0 || it->max_descent > 0)
24573 && face->box != FACE_NO_BOX
24574 && face->box_line_width > 0)
24575 {
24576 it->ascent += face->box_line_width;
24577 it->descent += face->box_line_width;
24578 }
24579 if (!NILP (height)
24580 && XINT (height) > it->ascent + it->descent)
24581 it->ascent = XINT (height) - it->descent;
24582
24583 if (!NILP (total_height))
24584 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24585 else
24586 {
24587 spacing = get_it_property (it, Qline_spacing);
24588 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24589 }
24590 if (INTEGERP (spacing))
24591 {
24592 extra_line_spacing = XINT (spacing);
24593 if (!NILP (total_height))
24594 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24595 }
24596 }
24597 }
24598 else /* i.e. (it->char_to_display == '\t') */
24599 {
24600 if (font->space_width > 0)
24601 {
24602 int tab_width = it->tab_width * font->space_width;
24603 int x = it->current_x + it->continuation_lines_width;
24604 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24605
24606 /* If the distance from the current position to the next tab
24607 stop is less than a space character width, use the
24608 tab stop after that. */
24609 if (next_tab_x - x < font->space_width)
24610 next_tab_x += tab_width;
24611
24612 it->pixel_width = next_tab_x - x;
24613 it->nglyphs = 1;
24614 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24615 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24616
24617 if (it->glyph_row)
24618 {
24619 append_stretch_glyph (it, it->object, it->pixel_width,
24620 it->ascent + it->descent, it->ascent);
24621 }
24622 }
24623 else
24624 {
24625 it->pixel_width = 0;
24626 it->nglyphs = 1;
24627 }
24628 }
24629 }
24630 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24631 {
24632 /* A static composition.
24633
24634 Note: A composition is represented as one glyph in the
24635 glyph matrix. There are no padding glyphs.
24636
24637 Important note: pixel_width, ascent, and descent are the
24638 values of what is drawn by draw_glyphs (i.e. the values of
24639 the overall glyphs composed). */
24640 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24641 int boff; /* baseline offset */
24642 struct composition *cmp = composition_table[it->cmp_it.id];
24643 int glyph_len = cmp->glyph_len;
24644 struct font *font = face->font;
24645
24646 it->nglyphs = 1;
24647
24648 /* If we have not yet calculated pixel size data of glyphs of
24649 the composition for the current face font, calculate them
24650 now. Theoretically, we have to check all fonts for the
24651 glyphs, but that requires much time and memory space. So,
24652 here we check only the font of the first glyph. This may
24653 lead to incorrect display, but it's very rare, and C-l
24654 (recenter-top-bottom) can correct the display anyway. */
24655 if (! cmp->font || cmp->font != font)
24656 {
24657 /* Ascent and descent of the font of the first character
24658 of this composition (adjusted by baseline offset).
24659 Ascent and descent of overall glyphs should not be less
24660 than these, respectively. */
24661 int font_ascent, font_descent, font_height;
24662 /* Bounding box of the overall glyphs. */
24663 int leftmost, rightmost, lowest, highest;
24664 int lbearing, rbearing;
24665 int i, width, ascent, descent;
24666 int left_padded = 0, right_padded = 0;
24667 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24668 XChar2b char2b;
24669 struct font_metrics *pcm;
24670 int font_not_found_p;
24671 ptrdiff_t pos;
24672
24673 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24674 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24675 break;
24676 if (glyph_len < cmp->glyph_len)
24677 right_padded = 1;
24678 for (i = 0; i < glyph_len; i++)
24679 {
24680 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24681 break;
24682 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24683 }
24684 if (i > 0)
24685 left_padded = 1;
24686
24687 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24688 : IT_CHARPOS (*it));
24689 /* If no suitable font is found, use the default font. */
24690 font_not_found_p = font == NULL;
24691 if (font_not_found_p)
24692 {
24693 face = face->ascii_face;
24694 font = face->font;
24695 }
24696 boff = font->baseline_offset;
24697 if (font->vertical_centering)
24698 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24699 font_ascent = FONT_BASE (font) + boff;
24700 font_descent = FONT_DESCENT (font) - boff;
24701 font_height = FONT_HEIGHT (font);
24702
24703 cmp->font = (void *) font;
24704
24705 pcm = NULL;
24706 if (! font_not_found_p)
24707 {
24708 get_char_face_and_encoding (it->f, c, it->face_id,
24709 &char2b, 0);
24710 pcm = get_per_char_metric (font, &char2b);
24711 }
24712
24713 /* Initialize the bounding box. */
24714 if (pcm)
24715 {
24716 width = cmp->glyph_len > 0 ? pcm->width : 0;
24717 ascent = pcm->ascent;
24718 descent = pcm->descent;
24719 lbearing = pcm->lbearing;
24720 rbearing = pcm->rbearing;
24721 }
24722 else
24723 {
24724 width = cmp->glyph_len > 0 ? font->space_width : 0;
24725 ascent = FONT_BASE (font);
24726 descent = FONT_DESCENT (font);
24727 lbearing = 0;
24728 rbearing = width;
24729 }
24730
24731 rightmost = width;
24732 leftmost = 0;
24733 lowest = - descent + boff;
24734 highest = ascent + boff;
24735
24736 if (! font_not_found_p
24737 && font->default_ascent
24738 && CHAR_TABLE_P (Vuse_default_ascent)
24739 && !NILP (Faref (Vuse_default_ascent,
24740 make_number (it->char_to_display))))
24741 highest = font->default_ascent + boff;
24742
24743 /* Draw the first glyph at the normal position. It may be
24744 shifted to right later if some other glyphs are drawn
24745 at the left. */
24746 cmp->offsets[i * 2] = 0;
24747 cmp->offsets[i * 2 + 1] = boff;
24748 cmp->lbearing = lbearing;
24749 cmp->rbearing = rbearing;
24750
24751 /* Set cmp->offsets for the remaining glyphs. */
24752 for (i++; i < glyph_len; i++)
24753 {
24754 int left, right, btm, top;
24755 int ch = COMPOSITION_GLYPH (cmp, i);
24756 int face_id;
24757 struct face *this_face;
24758
24759 if (ch == '\t')
24760 ch = ' ';
24761 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24762 this_face = FACE_FROM_ID (it->f, face_id);
24763 font = this_face->font;
24764
24765 if (font == NULL)
24766 pcm = NULL;
24767 else
24768 {
24769 get_char_face_and_encoding (it->f, ch, face_id,
24770 &char2b, 0);
24771 pcm = get_per_char_metric (font, &char2b);
24772 }
24773 if (! pcm)
24774 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24775 else
24776 {
24777 width = pcm->width;
24778 ascent = pcm->ascent;
24779 descent = pcm->descent;
24780 lbearing = pcm->lbearing;
24781 rbearing = pcm->rbearing;
24782 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24783 {
24784 /* Relative composition with or without
24785 alternate chars. */
24786 left = (leftmost + rightmost - width) / 2;
24787 btm = - descent + boff;
24788 if (font->relative_compose
24789 && (! CHAR_TABLE_P (Vignore_relative_composition)
24790 || NILP (Faref (Vignore_relative_composition,
24791 make_number (ch)))))
24792 {
24793
24794 if (- descent >= font->relative_compose)
24795 /* One extra pixel between two glyphs. */
24796 btm = highest + 1;
24797 else if (ascent <= 0)
24798 /* One extra pixel between two glyphs. */
24799 btm = lowest - 1 - ascent - descent;
24800 }
24801 }
24802 else
24803 {
24804 /* A composition rule is specified by an integer
24805 value that encodes global and new reference
24806 points (GREF and NREF). GREF and NREF are
24807 specified by numbers as below:
24808
24809 0---1---2 -- ascent
24810 | |
24811 | |
24812 | |
24813 9--10--11 -- center
24814 | |
24815 ---3---4---5--- baseline
24816 | |
24817 6---7---8 -- descent
24818 */
24819 int rule = COMPOSITION_RULE (cmp, i);
24820 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24821
24822 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24823 grefx = gref % 3, nrefx = nref % 3;
24824 grefy = gref / 3, nrefy = nref / 3;
24825 if (xoff)
24826 xoff = font_height * (xoff - 128) / 256;
24827 if (yoff)
24828 yoff = font_height * (yoff - 128) / 256;
24829
24830 left = (leftmost
24831 + grefx * (rightmost - leftmost) / 2
24832 - nrefx * width / 2
24833 + xoff);
24834
24835 btm = ((grefy == 0 ? highest
24836 : grefy == 1 ? 0
24837 : grefy == 2 ? lowest
24838 : (highest + lowest) / 2)
24839 - (nrefy == 0 ? ascent + descent
24840 : nrefy == 1 ? descent - boff
24841 : nrefy == 2 ? 0
24842 : (ascent + descent) / 2)
24843 + yoff);
24844 }
24845
24846 cmp->offsets[i * 2] = left;
24847 cmp->offsets[i * 2 + 1] = btm + descent;
24848
24849 /* Update the bounding box of the overall glyphs. */
24850 if (width > 0)
24851 {
24852 right = left + width;
24853 if (left < leftmost)
24854 leftmost = left;
24855 if (right > rightmost)
24856 rightmost = right;
24857 }
24858 top = btm + descent + ascent;
24859 if (top > highest)
24860 highest = top;
24861 if (btm < lowest)
24862 lowest = btm;
24863
24864 if (cmp->lbearing > left + lbearing)
24865 cmp->lbearing = left + lbearing;
24866 if (cmp->rbearing < left + rbearing)
24867 cmp->rbearing = left + rbearing;
24868 }
24869 }
24870
24871 /* If there are glyphs whose x-offsets are negative,
24872 shift all glyphs to the right and make all x-offsets
24873 non-negative. */
24874 if (leftmost < 0)
24875 {
24876 for (i = 0; i < cmp->glyph_len; i++)
24877 cmp->offsets[i * 2] -= leftmost;
24878 rightmost -= leftmost;
24879 cmp->lbearing -= leftmost;
24880 cmp->rbearing -= leftmost;
24881 }
24882
24883 if (left_padded && cmp->lbearing < 0)
24884 {
24885 for (i = 0; i < cmp->glyph_len; i++)
24886 cmp->offsets[i * 2] -= cmp->lbearing;
24887 rightmost -= cmp->lbearing;
24888 cmp->rbearing -= cmp->lbearing;
24889 cmp->lbearing = 0;
24890 }
24891 if (right_padded && rightmost < cmp->rbearing)
24892 {
24893 rightmost = cmp->rbearing;
24894 }
24895
24896 cmp->pixel_width = rightmost;
24897 cmp->ascent = highest;
24898 cmp->descent = - lowest;
24899 if (cmp->ascent < font_ascent)
24900 cmp->ascent = font_ascent;
24901 if (cmp->descent < font_descent)
24902 cmp->descent = font_descent;
24903 }
24904
24905 if (it->glyph_row
24906 && (cmp->lbearing < 0
24907 || cmp->rbearing > cmp->pixel_width))
24908 it->glyph_row->contains_overlapping_glyphs_p = 1;
24909
24910 it->pixel_width = cmp->pixel_width;
24911 it->ascent = it->phys_ascent = cmp->ascent;
24912 it->descent = it->phys_descent = cmp->descent;
24913 if (face->box != FACE_NO_BOX)
24914 {
24915 int thick = face->box_line_width;
24916
24917 if (thick > 0)
24918 {
24919 it->ascent += thick;
24920 it->descent += thick;
24921 }
24922 else
24923 thick = - thick;
24924
24925 if (it->start_of_box_run_p)
24926 it->pixel_width += thick;
24927 if (it->end_of_box_run_p)
24928 it->pixel_width += thick;
24929 }
24930
24931 /* If face has an overline, add the height of the overline
24932 (1 pixel) and a 1 pixel margin to the character height. */
24933 if (face->overline_p)
24934 it->ascent += overline_margin;
24935
24936 take_vertical_position_into_account (it);
24937 if (it->ascent < 0)
24938 it->ascent = 0;
24939 if (it->descent < 0)
24940 it->descent = 0;
24941
24942 if (it->glyph_row && cmp->glyph_len > 0)
24943 append_composite_glyph (it);
24944 }
24945 else if (it->what == IT_COMPOSITION)
24946 {
24947 /* A dynamic (automatic) composition. */
24948 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24949 Lisp_Object gstring;
24950 struct font_metrics metrics;
24951
24952 it->nglyphs = 1;
24953
24954 gstring = composition_gstring_from_id (it->cmp_it.id);
24955 it->pixel_width
24956 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24957 &metrics);
24958 if (it->glyph_row
24959 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24960 it->glyph_row->contains_overlapping_glyphs_p = 1;
24961 it->ascent = it->phys_ascent = metrics.ascent;
24962 it->descent = it->phys_descent = metrics.descent;
24963 if (face->box != FACE_NO_BOX)
24964 {
24965 int thick = face->box_line_width;
24966
24967 if (thick > 0)
24968 {
24969 it->ascent += thick;
24970 it->descent += thick;
24971 }
24972 else
24973 thick = - thick;
24974
24975 if (it->start_of_box_run_p)
24976 it->pixel_width += thick;
24977 if (it->end_of_box_run_p)
24978 it->pixel_width += thick;
24979 }
24980 /* If face has an overline, add the height of the overline
24981 (1 pixel) and a 1 pixel margin to the character height. */
24982 if (face->overline_p)
24983 it->ascent += overline_margin;
24984 take_vertical_position_into_account (it);
24985 if (it->ascent < 0)
24986 it->ascent = 0;
24987 if (it->descent < 0)
24988 it->descent = 0;
24989
24990 if (it->glyph_row)
24991 append_composite_glyph (it);
24992 }
24993 else if (it->what == IT_GLYPHLESS)
24994 produce_glyphless_glyph (it, 0, Qnil);
24995 else if (it->what == IT_IMAGE)
24996 produce_image_glyph (it);
24997 else if (it->what == IT_STRETCH)
24998 produce_stretch_glyph (it);
24999
25000 done:
25001 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25002 because this isn't true for images with `:ascent 100'. */
25003 eassert (it->ascent >= 0 && it->descent >= 0);
25004 if (it->area == TEXT_AREA)
25005 it->current_x += it->pixel_width;
25006
25007 if (extra_line_spacing > 0)
25008 {
25009 it->descent += extra_line_spacing;
25010 if (extra_line_spacing > it->max_extra_line_spacing)
25011 it->max_extra_line_spacing = extra_line_spacing;
25012 }
25013
25014 it->max_ascent = max (it->max_ascent, it->ascent);
25015 it->max_descent = max (it->max_descent, it->descent);
25016 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25017 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25018 }
25019
25020 /* EXPORT for RIF:
25021 Output LEN glyphs starting at START at the nominal cursor position.
25022 Advance the nominal cursor over the text. The global variable
25023 updated_window contains the window being updated, updated_row is
25024 the glyph row being updated, and updated_area is the area of that
25025 row being updated. */
25026
25027 void
25028 x_write_glyphs (struct glyph *start, int len)
25029 {
25030 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25031
25032 eassert (updated_window && updated_row);
25033 /* When the window is hscrolled, cursor hpos can legitimately be out
25034 of bounds, but we draw the cursor at the corresponding window
25035 margin in that case. */
25036 if (!updated_row->reversed_p && chpos < 0)
25037 chpos = 0;
25038 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25039 chpos = updated_row->used[TEXT_AREA] - 1;
25040
25041 BLOCK_INPUT;
25042
25043 /* Write glyphs. */
25044
25045 hpos = start - updated_row->glyphs[updated_area];
25046 x = draw_glyphs (updated_window, output_cursor.x,
25047 updated_row, updated_area,
25048 hpos, hpos + len,
25049 DRAW_NORMAL_TEXT, 0);
25050
25051 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25052 if (updated_area == TEXT_AREA
25053 && updated_window->phys_cursor_on_p
25054 && updated_window->phys_cursor.vpos == output_cursor.vpos
25055 && chpos >= hpos
25056 && chpos < hpos + len)
25057 updated_window->phys_cursor_on_p = 0;
25058
25059 UNBLOCK_INPUT;
25060
25061 /* Advance the output cursor. */
25062 output_cursor.hpos += len;
25063 output_cursor.x = x;
25064 }
25065
25066
25067 /* EXPORT for RIF:
25068 Insert LEN glyphs from START at the nominal cursor position. */
25069
25070 void
25071 x_insert_glyphs (struct glyph *start, int len)
25072 {
25073 struct frame *f;
25074 struct window *w;
25075 int line_height, shift_by_width, shifted_region_width;
25076 struct glyph_row *row;
25077 struct glyph *glyph;
25078 int frame_x, frame_y;
25079 ptrdiff_t hpos;
25080
25081 eassert (updated_window && updated_row);
25082 BLOCK_INPUT;
25083 w = updated_window;
25084 f = XFRAME (WINDOW_FRAME (w));
25085
25086 /* Get the height of the line we are in. */
25087 row = updated_row;
25088 line_height = row->height;
25089
25090 /* Get the width of the glyphs to insert. */
25091 shift_by_width = 0;
25092 for (glyph = start; glyph < start + len; ++glyph)
25093 shift_by_width += glyph->pixel_width;
25094
25095 /* Get the width of the region to shift right. */
25096 shifted_region_width = (window_box_width (w, updated_area)
25097 - output_cursor.x
25098 - shift_by_width);
25099
25100 /* Shift right. */
25101 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25102 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25103
25104 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25105 line_height, shift_by_width);
25106
25107 /* Write the glyphs. */
25108 hpos = start - row->glyphs[updated_area];
25109 draw_glyphs (w, output_cursor.x, row, updated_area,
25110 hpos, hpos + len,
25111 DRAW_NORMAL_TEXT, 0);
25112
25113 /* Advance the output cursor. */
25114 output_cursor.hpos += len;
25115 output_cursor.x += shift_by_width;
25116 UNBLOCK_INPUT;
25117 }
25118
25119
25120 /* EXPORT for RIF:
25121 Erase the current text line from the nominal cursor position
25122 (inclusive) to pixel column TO_X (exclusive). The idea is that
25123 everything from TO_X onward is already erased.
25124
25125 TO_X is a pixel position relative to updated_area of
25126 updated_window. TO_X == -1 means clear to the end of this area. */
25127
25128 void
25129 x_clear_end_of_line (int to_x)
25130 {
25131 struct frame *f;
25132 struct window *w = updated_window;
25133 int max_x, min_y, max_y;
25134 int from_x, from_y, to_y;
25135
25136 eassert (updated_window && updated_row);
25137 f = XFRAME (w->frame);
25138
25139 if (updated_row->full_width_p)
25140 max_x = WINDOW_TOTAL_WIDTH (w);
25141 else
25142 max_x = window_box_width (w, updated_area);
25143 max_y = window_text_bottom_y (w);
25144
25145 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25146 of window. For TO_X > 0, truncate to end of drawing area. */
25147 if (to_x == 0)
25148 return;
25149 else if (to_x < 0)
25150 to_x = max_x;
25151 else
25152 to_x = min (to_x, max_x);
25153
25154 to_y = min (max_y, output_cursor.y + updated_row->height);
25155
25156 /* Notice if the cursor will be cleared by this operation. */
25157 if (!updated_row->full_width_p)
25158 notice_overwritten_cursor (w, updated_area,
25159 output_cursor.x, -1,
25160 updated_row->y,
25161 MATRIX_ROW_BOTTOM_Y (updated_row));
25162
25163 from_x = output_cursor.x;
25164
25165 /* Translate to frame coordinates. */
25166 if (updated_row->full_width_p)
25167 {
25168 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25169 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25170 }
25171 else
25172 {
25173 int area_left = window_box_left (w, updated_area);
25174 from_x += area_left;
25175 to_x += area_left;
25176 }
25177
25178 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25179 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25180 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25181
25182 /* Prevent inadvertently clearing to end of the X window. */
25183 if (to_x > from_x && to_y > from_y)
25184 {
25185 BLOCK_INPUT;
25186 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25187 to_x - from_x, to_y - from_y);
25188 UNBLOCK_INPUT;
25189 }
25190 }
25191
25192 #endif /* HAVE_WINDOW_SYSTEM */
25193
25194
25195 \f
25196 /***********************************************************************
25197 Cursor types
25198 ***********************************************************************/
25199
25200 /* Value is the internal representation of the specified cursor type
25201 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25202 of the bar cursor. */
25203
25204 static enum text_cursor_kinds
25205 get_specified_cursor_type (Lisp_Object arg, int *width)
25206 {
25207 enum text_cursor_kinds type;
25208
25209 if (NILP (arg))
25210 return NO_CURSOR;
25211
25212 if (EQ (arg, Qbox))
25213 return FILLED_BOX_CURSOR;
25214
25215 if (EQ (arg, Qhollow))
25216 return HOLLOW_BOX_CURSOR;
25217
25218 if (EQ (arg, Qbar))
25219 {
25220 *width = 2;
25221 return BAR_CURSOR;
25222 }
25223
25224 if (CONSP (arg)
25225 && EQ (XCAR (arg), Qbar)
25226 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25227 {
25228 *width = XINT (XCDR (arg));
25229 return BAR_CURSOR;
25230 }
25231
25232 if (EQ (arg, Qhbar))
25233 {
25234 *width = 2;
25235 return HBAR_CURSOR;
25236 }
25237
25238 if (CONSP (arg)
25239 && EQ (XCAR (arg), Qhbar)
25240 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25241 {
25242 *width = XINT (XCDR (arg));
25243 return HBAR_CURSOR;
25244 }
25245
25246 /* Treat anything unknown as "hollow box cursor".
25247 It was bad to signal an error; people have trouble fixing
25248 .Xdefaults with Emacs, when it has something bad in it. */
25249 type = HOLLOW_BOX_CURSOR;
25250
25251 return type;
25252 }
25253
25254 /* Set the default cursor types for specified frame. */
25255 void
25256 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25257 {
25258 int width = 1;
25259 Lisp_Object tem;
25260
25261 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25262 FRAME_CURSOR_WIDTH (f) = width;
25263
25264 /* By default, set up the blink-off state depending on the on-state. */
25265
25266 tem = Fassoc (arg, Vblink_cursor_alist);
25267 if (!NILP (tem))
25268 {
25269 FRAME_BLINK_OFF_CURSOR (f)
25270 = get_specified_cursor_type (XCDR (tem), &width);
25271 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25272 }
25273 else
25274 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25275 }
25276
25277
25278 #ifdef HAVE_WINDOW_SYSTEM
25279
25280 /* Return the cursor we want to be displayed in window W. Return
25281 width of bar/hbar cursor through WIDTH arg. Return with
25282 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25283 (i.e. if the `system caret' should track this cursor).
25284
25285 In a mini-buffer window, we want the cursor only to appear if we
25286 are reading input from this window. For the selected window, we
25287 want the cursor type given by the frame parameter or buffer local
25288 setting of cursor-type. If explicitly marked off, draw no cursor.
25289 In all other cases, we want a hollow box cursor. */
25290
25291 static enum text_cursor_kinds
25292 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25293 int *active_cursor)
25294 {
25295 struct frame *f = XFRAME (w->frame);
25296 struct buffer *b = XBUFFER (w->buffer);
25297 int cursor_type = DEFAULT_CURSOR;
25298 Lisp_Object alt_cursor;
25299 int non_selected = 0;
25300
25301 *active_cursor = 1;
25302
25303 /* Echo area */
25304 if (cursor_in_echo_area
25305 && FRAME_HAS_MINIBUF_P (f)
25306 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25307 {
25308 if (w == XWINDOW (echo_area_window))
25309 {
25310 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25311 {
25312 *width = FRAME_CURSOR_WIDTH (f);
25313 return FRAME_DESIRED_CURSOR (f);
25314 }
25315 else
25316 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25317 }
25318
25319 *active_cursor = 0;
25320 non_selected = 1;
25321 }
25322
25323 /* Detect a nonselected window or nonselected frame. */
25324 else if (w != XWINDOW (f->selected_window)
25325 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25326 {
25327 *active_cursor = 0;
25328
25329 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25330 return NO_CURSOR;
25331
25332 non_selected = 1;
25333 }
25334
25335 /* Never display a cursor in a window in which cursor-type is nil. */
25336 if (NILP (BVAR (b, cursor_type)))
25337 return NO_CURSOR;
25338
25339 /* Get the normal cursor type for this window. */
25340 if (EQ (BVAR (b, cursor_type), Qt))
25341 {
25342 cursor_type = FRAME_DESIRED_CURSOR (f);
25343 *width = FRAME_CURSOR_WIDTH (f);
25344 }
25345 else
25346 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25347
25348 /* Use cursor-in-non-selected-windows instead
25349 for non-selected window or frame. */
25350 if (non_selected)
25351 {
25352 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25353 if (!EQ (Qt, alt_cursor))
25354 return get_specified_cursor_type (alt_cursor, width);
25355 /* t means modify the normal cursor type. */
25356 if (cursor_type == FILLED_BOX_CURSOR)
25357 cursor_type = HOLLOW_BOX_CURSOR;
25358 else if (cursor_type == BAR_CURSOR && *width > 1)
25359 --*width;
25360 return cursor_type;
25361 }
25362
25363 /* Use normal cursor if not blinked off. */
25364 if (!w->cursor_off_p)
25365 {
25366 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25367 {
25368 if (cursor_type == FILLED_BOX_CURSOR)
25369 {
25370 /* Using a block cursor on large images can be very annoying.
25371 So use a hollow cursor for "large" images.
25372 If image is not transparent (no mask), also use hollow cursor. */
25373 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25374 if (img != NULL && IMAGEP (img->spec))
25375 {
25376 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25377 where N = size of default frame font size.
25378 This should cover most of the "tiny" icons people may use. */
25379 if (!img->mask
25380 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25381 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25382 cursor_type = HOLLOW_BOX_CURSOR;
25383 }
25384 }
25385 else if (cursor_type != NO_CURSOR)
25386 {
25387 /* Display current only supports BOX and HOLLOW cursors for images.
25388 So for now, unconditionally use a HOLLOW cursor when cursor is
25389 not a solid box cursor. */
25390 cursor_type = HOLLOW_BOX_CURSOR;
25391 }
25392 }
25393 return cursor_type;
25394 }
25395
25396 /* Cursor is blinked off, so determine how to "toggle" it. */
25397
25398 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25399 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25400 return get_specified_cursor_type (XCDR (alt_cursor), width);
25401
25402 /* Then see if frame has specified a specific blink off cursor type. */
25403 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25404 {
25405 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25406 return FRAME_BLINK_OFF_CURSOR (f);
25407 }
25408
25409 #if 0
25410 /* Some people liked having a permanently visible blinking cursor,
25411 while others had very strong opinions against it. So it was
25412 decided to remove it. KFS 2003-09-03 */
25413
25414 /* Finally perform built-in cursor blinking:
25415 filled box <-> hollow box
25416 wide [h]bar <-> narrow [h]bar
25417 narrow [h]bar <-> no cursor
25418 other type <-> no cursor */
25419
25420 if (cursor_type == FILLED_BOX_CURSOR)
25421 return HOLLOW_BOX_CURSOR;
25422
25423 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25424 {
25425 *width = 1;
25426 return cursor_type;
25427 }
25428 #endif
25429
25430 return NO_CURSOR;
25431 }
25432
25433
25434 /* Notice when the text cursor of window W has been completely
25435 overwritten by a drawing operation that outputs glyphs in AREA
25436 starting at X0 and ending at X1 in the line starting at Y0 and
25437 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25438 the rest of the line after X0 has been written. Y coordinates
25439 are window-relative. */
25440
25441 static void
25442 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25443 int x0, int x1, int y0, int y1)
25444 {
25445 int cx0, cx1, cy0, cy1;
25446 struct glyph_row *row;
25447
25448 if (!w->phys_cursor_on_p)
25449 return;
25450 if (area != TEXT_AREA)
25451 return;
25452
25453 if (w->phys_cursor.vpos < 0
25454 || w->phys_cursor.vpos >= w->current_matrix->nrows
25455 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25456 !(row->enabled_p && row->displays_text_p)))
25457 return;
25458
25459 if (row->cursor_in_fringe_p)
25460 {
25461 row->cursor_in_fringe_p = 0;
25462 draw_fringe_bitmap (w, row, row->reversed_p);
25463 w->phys_cursor_on_p = 0;
25464 return;
25465 }
25466
25467 cx0 = w->phys_cursor.x;
25468 cx1 = cx0 + w->phys_cursor_width;
25469 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25470 return;
25471
25472 /* The cursor image will be completely removed from the
25473 screen if the output area intersects the cursor area in
25474 y-direction. When we draw in [y0 y1[, and some part of
25475 the cursor is at y < y0, that part must have been drawn
25476 before. When scrolling, the cursor is erased before
25477 actually scrolling, so we don't come here. When not
25478 scrolling, the rows above the old cursor row must have
25479 changed, and in this case these rows must have written
25480 over the cursor image.
25481
25482 Likewise if part of the cursor is below y1, with the
25483 exception of the cursor being in the first blank row at
25484 the buffer and window end because update_text_area
25485 doesn't draw that row. (Except when it does, but
25486 that's handled in update_text_area.) */
25487
25488 cy0 = w->phys_cursor.y;
25489 cy1 = cy0 + w->phys_cursor_height;
25490 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25491 return;
25492
25493 w->phys_cursor_on_p = 0;
25494 }
25495
25496 #endif /* HAVE_WINDOW_SYSTEM */
25497
25498 \f
25499 /************************************************************************
25500 Mouse Face
25501 ************************************************************************/
25502
25503 #ifdef HAVE_WINDOW_SYSTEM
25504
25505 /* EXPORT for RIF:
25506 Fix the display of area AREA of overlapping row ROW in window W
25507 with respect to the overlapping part OVERLAPS. */
25508
25509 void
25510 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25511 enum glyph_row_area area, int overlaps)
25512 {
25513 int i, x;
25514
25515 BLOCK_INPUT;
25516
25517 x = 0;
25518 for (i = 0; i < row->used[area];)
25519 {
25520 if (row->glyphs[area][i].overlaps_vertically_p)
25521 {
25522 int start = i, start_x = x;
25523
25524 do
25525 {
25526 x += row->glyphs[area][i].pixel_width;
25527 ++i;
25528 }
25529 while (i < row->used[area]
25530 && row->glyphs[area][i].overlaps_vertically_p);
25531
25532 draw_glyphs (w, start_x, row, area,
25533 start, i,
25534 DRAW_NORMAL_TEXT, overlaps);
25535 }
25536 else
25537 {
25538 x += row->glyphs[area][i].pixel_width;
25539 ++i;
25540 }
25541 }
25542
25543 UNBLOCK_INPUT;
25544 }
25545
25546
25547 /* EXPORT:
25548 Draw the cursor glyph of window W in glyph row ROW. See the
25549 comment of draw_glyphs for the meaning of HL. */
25550
25551 void
25552 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25553 enum draw_glyphs_face hl)
25554 {
25555 /* If cursor hpos is out of bounds, don't draw garbage. This can
25556 happen in mini-buffer windows when switching between echo area
25557 glyphs and mini-buffer. */
25558 if ((row->reversed_p
25559 ? (w->phys_cursor.hpos >= 0)
25560 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25561 {
25562 int on_p = w->phys_cursor_on_p;
25563 int x1;
25564 int hpos = w->phys_cursor.hpos;
25565
25566 /* When the window is hscrolled, cursor hpos can legitimately be
25567 out of bounds, but we draw the cursor at the corresponding
25568 window margin in that case. */
25569 if (!row->reversed_p && hpos < 0)
25570 hpos = 0;
25571 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25572 hpos = row->used[TEXT_AREA] - 1;
25573
25574 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25575 hl, 0);
25576 w->phys_cursor_on_p = on_p;
25577
25578 if (hl == DRAW_CURSOR)
25579 w->phys_cursor_width = x1 - w->phys_cursor.x;
25580 /* When we erase the cursor, and ROW is overlapped by other
25581 rows, make sure that these overlapping parts of other rows
25582 are redrawn. */
25583 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25584 {
25585 w->phys_cursor_width = x1 - w->phys_cursor.x;
25586
25587 if (row > w->current_matrix->rows
25588 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25589 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25590 OVERLAPS_ERASED_CURSOR);
25591
25592 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25593 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25594 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25595 OVERLAPS_ERASED_CURSOR);
25596 }
25597 }
25598 }
25599
25600
25601 /* EXPORT:
25602 Erase the image of a cursor of window W from the screen. */
25603
25604 void
25605 erase_phys_cursor (struct window *w)
25606 {
25607 struct frame *f = XFRAME (w->frame);
25608 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25609 int hpos = w->phys_cursor.hpos;
25610 int vpos = w->phys_cursor.vpos;
25611 int mouse_face_here_p = 0;
25612 struct glyph_matrix *active_glyphs = w->current_matrix;
25613 struct glyph_row *cursor_row;
25614 struct glyph *cursor_glyph;
25615 enum draw_glyphs_face hl;
25616
25617 /* No cursor displayed or row invalidated => nothing to do on the
25618 screen. */
25619 if (w->phys_cursor_type == NO_CURSOR)
25620 goto mark_cursor_off;
25621
25622 /* VPOS >= active_glyphs->nrows means that window has been resized.
25623 Don't bother to erase the cursor. */
25624 if (vpos >= active_glyphs->nrows)
25625 goto mark_cursor_off;
25626
25627 /* If row containing cursor is marked invalid, there is nothing we
25628 can do. */
25629 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25630 if (!cursor_row->enabled_p)
25631 goto mark_cursor_off;
25632
25633 /* If line spacing is > 0, old cursor may only be partially visible in
25634 window after split-window. So adjust visible height. */
25635 cursor_row->visible_height = min (cursor_row->visible_height,
25636 window_text_bottom_y (w) - cursor_row->y);
25637
25638 /* If row is completely invisible, don't attempt to delete a cursor which
25639 isn't there. This can happen if cursor is at top of a window, and
25640 we switch to a buffer with a header line in that window. */
25641 if (cursor_row->visible_height <= 0)
25642 goto mark_cursor_off;
25643
25644 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25645 if (cursor_row->cursor_in_fringe_p)
25646 {
25647 cursor_row->cursor_in_fringe_p = 0;
25648 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25649 goto mark_cursor_off;
25650 }
25651
25652 /* This can happen when the new row is shorter than the old one.
25653 In this case, either draw_glyphs or clear_end_of_line
25654 should have cleared the cursor. Note that we wouldn't be
25655 able to erase the cursor in this case because we don't have a
25656 cursor glyph at hand. */
25657 if ((cursor_row->reversed_p
25658 ? (w->phys_cursor.hpos < 0)
25659 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25660 goto mark_cursor_off;
25661
25662 /* When the window is hscrolled, cursor hpos can legitimately be out
25663 of bounds, but we draw the cursor at the corresponding window
25664 margin in that case. */
25665 if (!cursor_row->reversed_p && hpos < 0)
25666 hpos = 0;
25667 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25668 hpos = cursor_row->used[TEXT_AREA] - 1;
25669
25670 /* If the cursor is in the mouse face area, redisplay that when
25671 we clear the cursor. */
25672 if (! NILP (hlinfo->mouse_face_window)
25673 && coords_in_mouse_face_p (w, hpos, vpos)
25674 /* Don't redraw the cursor's spot in mouse face if it is at the
25675 end of a line (on a newline). The cursor appears there, but
25676 mouse highlighting does not. */
25677 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25678 mouse_face_here_p = 1;
25679
25680 /* Maybe clear the display under the cursor. */
25681 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25682 {
25683 int x, y, left_x;
25684 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25685 int width;
25686
25687 cursor_glyph = get_phys_cursor_glyph (w);
25688 if (cursor_glyph == NULL)
25689 goto mark_cursor_off;
25690
25691 width = cursor_glyph->pixel_width;
25692 left_x = window_box_left_offset (w, TEXT_AREA);
25693 x = w->phys_cursor.x;
25694 if (x < left_x)
25695 width -= left_x - x;
25696 width = min (width, window_box_width (w, TEXT_AREA) - x);
25697 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25698 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25699
25700 if (width > 0)
25701 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25702 }
25703
25704 /* Erase the cursor by redrawing the character underneath it. */
25705 if (mouse_face_here_p)
25706 hl = DRAW_MOUSE_FACE;
25707 else
25708 hl = DRAW_NORMAL_TEXT;
25709 draw_phys_cursor_glyph (w, cursor_row, hl);
25710
25711 mark_cursor_off:
25712 w->phys_cursor_on_p = 0;
25713 w->phys_cursor_type = NO_CURSOR;
25714 }
25715
25716
25717 /* EXPORT:
25718 Display or clear cursor of window W. If ON is zero, clear the
25719 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25720 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25721
25722 void
25723 display_and_set_cursor (struct window *w, int on,
25724 int hpos, int vpos, int x, int y)
25725 {
25726 struct frame *f = XFRAME (w->frame);
25727 int new_cursor_type;
25728 int new_cursor_width;
25729 int active_cursor;
25730 struct glyph_row *glyph_row;
25731 struct glyph *glyph;
25732
25733 /* This is pointless on invisible frames, and dangerous on garbaged
25734 windows and frames; in the latter case, the frame or window may
25735 be in the midst of changing its size, and x and y may be off the
25736 window. */
25737 if (! FRAME_VISIBLE_P (f)
25738 || FRAME_GARBAGED_P (f)
25739 || vpos >= w->current_matrix->nrows
25740 || hpos >= w->current_matrix->matrix_w)
25741 return;
25742
25743 /* If cursor is off and we want it off, return quickly. */
25744 if (!on && !w->phys_cursor_on_p)
25745 return;
25746
25747 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25748 /* If cursor row is not enabled, we don't really know where to
25749 display the cursor. */
25750 if (!glyph_row->enabled_p)
25751 {
25752 w->phys_cursor_on_p = 0;
25753 return;
25754 }
25755
25756 glyph = NULL;
25757 if (!glyph_row->exact_window_width_line_p
25758 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25759 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25760
25761 eassert (interrupt_input_blocked);
25762
25763 /* Set new_cursor_type to the cursor we want to be displayed. */
25764 new_cursor_type = get_window_cursor_type (w, glyph,
25765 &new_cursor_width, &active_cursor);
25766
25767 /* If cursor is currently being shown and we don't want it to be or
25768 it is in the wrong place, or the cursor type is not what we want,
25769 erase it. */
25770 if (w->phys_cursor_on_p
25771 && (!on
25772 || w->phys_cursor.x != x
25773 || w->phys_cursor.y != y
25774 || new_cursor_type != w->phys_cursor_type
25775 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25776 && new_cursor_width != w->phys_cursor_width)))
25777 erase_phys_cursor (w);
25778
25779 /* Don't check phys_cursor_on_p here because that flag is only set
25780 to zero in some cases where we know that the cursor has been
25781 completely erased, to avoid the extra work of erasing the cursor
25782 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25783 still not be visible, or it has only been partly erased. */
25784 if (on)
25785 {
25786 w->phys_cursor_ascent = glyph_row->ascent;
25787 w->phys_cursor_height = glyph_row->height;
25788
25789 /* Set phys_cursor_.* before x_draw_.* is called because some
25790 of them may need the information. */
25791 w->phys_cursor.x = x;
25792 w->phys_cursor.y = glyph_row->y;
25793 w->phys_cursor.hpos = hpos;
25794 w->phys_cursor.vpos = vpos;
25795 }
25796
25797 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25798 new_cursor_type, new_cursor_width,
25799 on, active_cursor);
25800 }
25801
25802
25803 /* Switch the display of W's cursor on or off, according to the value
25804 of ON. */
25805
25806 static void
25807 update_window_cursor (struct window *w, int on)
25808 {
25809 /* Don't update cursor in windows whose frame is in the process
25810 of being deleted. */
25811 if (w->current_matrix)
25812 {
25813 int hpos = w->phys_cursor.hpos;
25814 int vpos = w->phys_cursor.vpos;
25815 struct glyph_row *row;
25816
25817 if (vpos >= w->current_matrix->nrows
25818 || hpos >= w->current_matrix->matrix_w)
25819 return;
25820
25821 row = MATRIX_ROW (w->current_matrix, vpos);
25822
25823 /* When the window is hscrolled, cursor hpos can legitimately be
25824 out of bounds, but we draw the cursor at the corresponding
25825 window margin in that case. */
25826 if (!row->reversed_p && hpos < 0)
25827 hpos = 0;
25828 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25829 hpos = row->used[TEXT_AREA] - 1;
25830
25831 BLOCK_INPUT;
25832 display_and_set_cursor (w, on, hpos, vpos,
25833 w->phys_cursor.x, w->phys_cursor.y);
25834 UNBLOCK_INPUT;
25835 }
25836 }
25837
25838
25839 /* Call update_window_cursor with parameter ON_P on all leaf windows
25840 in the window tree rooted at W. */
25841
25842 static void
25843 update_cursor_in_window_tree (struct window *w, int on_p)
25844 {
25845 while (w)
25846 {
25847 if (!NILP (w->hchild))
25848 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25849 else if (!NILP (w->vchild))
25850 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25851 else
25852 update_window_cursor (w, on_p);
25853
25854 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25855 }
25856 }
25857
25858
25859 /* EXPORT:
25860 Display the cursor on window W, or clear it, according to ON_P.
25861 Don't change the cursor's position. */
25862
25863 void
25864 x_update_cursor (struct frame *f, int on_p)
25865 {
25866 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25867 }
25868
25869
25870 /* EXPORT:
25871 Clear the cursor of window W to background color, and mark the
25872 cursor as not shown. This is used when the text where the cursor
25873 is about to be rewritten. */
25874
25875 void
25876 x_clear_cursor (struct window *w)
25877 {
25878 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25879 update_window_cursor (w, 0);
25880 }
25881
25882 #endif /* HAVE_WINDOW_SYSTEM */
25883
25884 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25885 and MSDOS. */
25886 static void
25887 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25888 int start_hpos, int end_hpos,
25889 enum draw_glyphs_face draw)
25890 {
25891 #ifdef HAVE_WINDOW_SYSTEM
25892 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25893 {
25894 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25895 return;
25896 }
25897 #endif
25898 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
25899 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25900 #endif
25901 }
25902
25903 /* Display the active region described by mouse_face_* according to DRAW. */
25904
25905 static void
25906 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25907 {
25908 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25909 struct frame *f = XFRAME (WINDOW_FRAME (w));
25910
25911 if (/* If window is in the process of being destroyed, don't bother
25912 to do anything. */
25913 w->current_matrix != NULL
25914 /* Don't update mouse highlight if hidden */
25915 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25916 /* Recognize when we are called to operate on rows that don't exist
25917 anymore. This can happen when a window is split. */
25918 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25919 {
25920 int phys_cursor_on_p = w->phys_cursor_on_p;
25921 struct glyph_row *row, *first, *last;
25922
25923 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25924 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25925
25926 for (row = first; row <= last && row->enabled_p; ++row)
25927 {
25928 int start_hpos, end_hpos, start_x;
25929
25930 /* For all but the first row, the highlight starts at column 0. */
25931 if (row == first)
25932 {
25933 /* R2L rows have BEG and END in reversed order, but the
25934 screen drawing geometry is always left to right. So
25935 we need to mirror the beginning and end of the
25936 highlighted area in R2L rows. */
25937 if (!row->reversed_p)
25938 {
25939 start_hpos = hlinfo->mouse_face_beg_col;
25940 start_x = hlinfo->mouse_face_beg_x;
25941 }
25942 else if (row == last)
25943 {
25944 start_hpos = hlinfo->mouse_face_end_col;
25945 start_x = hlinfo->mouse_face_end_x;
25946 }
25947 else
25948 {
25949 start_hpos = 0;
25950 start_x = 0;
25951 }
25952 }
25953 else if (row->reversed_p && row == last)
25954 {
25955 start_hpos = hlinfo->mouse_face_end_col;
25956 start_x = hlinfo->mouse_face_end_x;
25957 }
25958 else
25959 {
25960 start_hpos = 0;
25961 start_x = 0;
25962 }
25963
25964 if (row == last)
25965 {
25966 if (!row->reversed_p)
25967 end_hpos = hlinfo->mouse_face_end_col;
25968 else if (row == first)
25969 end_hpos = hlinfo->mouse_face_beg_col;
25970 else
25971 {
25972 end_hpos = row->used[TEXT_AREA];
25973 if (draw == DRAW_NORMAL_TEXT)
25974 row->fill_line_p = 1; /* Clear to end of line */
25975 }
25976 }
25977 else if (row->reversed_p && row == first)
25978 end_hpos = hlinfo->mouse_face_beg_col;
25979 else
25980 {
25981 end_hpos = row->used[TEXT_AREA];
25982 if (draw == DRAW_NORMAL_TEXT)
25983 row->fill_line_p = 1; /* Clear to end of line */
25984 }
25985
25986 if (end_hpos > start_hpos)
25987 {
25988 draw_row_with_mouse_face (w, start_x, row,
25989 start_hpos, end_hpos, draw);
25990
25991 row->mouse_face_p
25992 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25993 }
25994 }
25995
25996 #ifdef HAVE_WINDOW_SYSTEM
25997 /* When we've written over the cursor, arrange for it to
25998 be displayed again. */
25999 if (FRAME_WINDOW_P (f)
26000 && phys_cursor_on_p && !w->phys_cursor_on_p)
26001 {
26002 int hpos = w->phys_cursor.hpos;
26003
26004 /* When the window is hscrolled, cursor hpos can legitimately be
26005 out of bounds, but we draw the cursor at the corresponding
26006 window margin in that case. */
26007 if (!row->reversed_p && hpos < 0)
26008 hpos = 0;
26009 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26010 hpos = row->used[TEXT_AREA] - 1;
26011
26012 BLOCK_INPUT;
26013 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26014 w->phys_cursor.x, w->phys_cursor.y);
26015 UNBLOCK_INPUT;
26016 }
26017 #endif /* HAVE_WINDOW_SYSTEM */
26018 }
26019
26020 #ifdef HAVE_WINDOW_SYSTEM
26021 /* Change the mouse cursor. */
26022 if (FRAME_WINDOW_P (f))
26023 {
26024 if (draw == DRAW_NORMAL_TEXT
26025 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26026 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26027 else if (draw == DRAW_MOUSE_FACE)
26028 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26029 else
26030 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26031 }
26032 #endif /* HAVE_WINDOW_SYSTEM */
26033 }
26034
26035 /* EXPORT:
26036 Clear out the mouse-highlighted active region.
26037 Redraw it un-highlighted first. Value is non-zero if mouse
26038 face was actually drawn unhighlighted. */
26039
26040 int
26041 clear_mouse_face (Mouse_HLInfo *hlinfo)
26042 {
26043 int cleared = 0;
26044
26045 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26046 {
26047 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26048 cleared = 1;
26049 }
26050
26051 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26052 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26053 hlinfo->mouse_face_window = Qnil;
26054 hlinfo->mouse_face_overlay = Qnil;
26055 return cleared;
26056 }
26057
26058 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26059 within the mouse face on that window. */
26060 static int
26061 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26062 {
26063 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26064
26065 /* Quickly resolve the easy cases. */
26066 if (!(WINDOWP (hlinfo->mouse_face_window)
26067 && XWINDOW (hlinfo->mouse_face_window) == w))
26068 return 0;
26069 if (vpos < hlinfo->mouse_face_beg_row
26070 || vpos > hlinfo->mouse_face_end_row)
26071 return 0;
26072 if (vpos > hlinfo->mouse_face_beg_row
26073 && vpos < hlinfo->mouse_face_end_row)
26074 return 1;
26075
26076 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26077 {
26078 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26079 {
26080 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26081 return 1;
26082 }
26083 else if ((vpos == hlinfo->mouse_face_beg_row
26084 && hpos >= hlinfo->mouse_face_beg_col)
26085 || (vpos == hlinfo->mouse_face_end_row
26086 && hpos < hlinfo->mouse_face_end_col))
26087 return 1;
26088 }
26089 else
26090 {
26091 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26092 {
26093 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26094 return 1;
26095 }
26096 else if ((vpos == hlinfo->mouse_face_beg_row
26097 && hpos <= hlinfo->mouse_face_beg_col)
26098 || (vpos == hlinfo->mouse_face_end_row
26099 && hpos > hlinfo->mouse_face_end_col))
26100 return 1;
26101 }
26102 return 0;
26103 }
26104
26105
26106 /* EXPORT:
26107 Non-zero if physical cursor of window W is within mouse face. */
26108
26109 int
26110 cursor_in_mouse_face_p (struct window *w)
26111 {
26112 int hpos = w->phys_cursor.hpos;
26113 int vpos = w->phys_cursor.vpos;
26114 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26115
26116 /* When the window is hscrolled, cursor hpos can legitimately be out
26117 of bounds, but we draw the cursor at the corresponding window
26118 margin in that case. */
26119 if (!row->reversed_p && hpos < 0)
26120 hpos = 0;
26121 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26122 hpos = row->used[TEXT_AREA] - 1;
26123
26124 return coords_in_mouse_face_p (w, hpos, vpos);
26125 }
26126
26127
26128 \f
26129 /* Find the glyph rows START_ROW and END_ROW of window W that display
26130 characters between buffer positions START_CHARPOS and END_CHARPOS
26131 (excluding END_CHARPOS). DISP_STRING is a display string that
26132 covers these buffer positions. This is similar to
26133 row_containing_pos, but is more accurate when bidi reordering makes
26134 buffer positions change non-linearly with glyph rows. */
26135 static void
26136 rows_from_pos_range (struct window *w,
26137 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26138 Lisp_Object disp_string,
26139 struct glyph_row **start, struct glyph_row **end)
26140 {
26141 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26142 int last_y = window_text_bottom_y (w);
26143 struct glyph_row *row;
26144
26145 *start = NULL;
26146 *end = NULL;
26147
26148 while (!first->enabled_p
26149 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26150 first++;
26151
26152 /* Find the START row. */
26153 for (row = first;
26154 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26155 row++)
26156 {
26157 /* A row can potentially be the START row if the range of the
26158 characters it displays intersects the range
26159 [START_CHARPOS..END_CHARPOS). */
26160 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26161 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26162 /* See the commentary in row_containing_pos, for the
26163 explanation of the complicated way to check whether
26164 some position is beyond the end of the characters
26165 displayed by a row. */
26166 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26167 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26168 && !row->ends_at_zv_p
26169 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26170 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26171 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26172 && !row->ends_at_zv_p
26173 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26174 {
26175 /* Found a candidate row. Now make sure at least one of the
26176 glyphs it displays has a charpos from the range
26177 [START_CHARPOS..END_CHARPOS).
26178
26179 This is not obvious because bidi reordering could make
26180 buffer positions of a row be 1,2,3,102,101,100, and if we
26181 want to highlight characters in [50..60), we don't want
26182 this row, even though [50..60) does intersect [1..103),
26183 the range of character positions given by the row's start
26184 and end positions. */
26185 struct glyph *g = row->glyphs[TEXT_AREA];
26186 struct glyph *e = g + row->used[TEXT_AREA];
26187
26188 while (g < e)
26189 {
26190 if (((BUFFERP (g->object) || INTEGERP (g->object))
26191 && start_charpos <= g->charpos && g->charpos < end_charpos)
26192 /* A glyph that comes from DISP_STRING is by
26193 definition to be highlighted. */
26194 || EQ (g->object, disp_string))
26195 *start = row;
26196 g++;
26197 }
26198 if (*start)
26199 break;
26200 }
26201 }
26202
26203 /* Find the END row. */
26204 if (!*start
26205 /* If the last row is partially visible, start looking for END
26206 from that row, instead of starting from FIRST. */
26207 && !(row->enabled_p
26208 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26209 row = first;
26210 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26211 {
26212 struct glyph_row *next = row + 1;
26213 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26214
26215 if (!next->enabled_p
26216 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26217 /* The first row >= START whose range of displayed characters
26218 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26219 is the row END + 1. */
26220 || (start_charpos < next_start
26221 && end_charpos < next_start)
26222 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26223 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26224 && !next->ends_at_zv_p
26225 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26226 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26227 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26228 && !next->ends_at_zv_p
26229 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26230 {
26231 *end = row;
26232 break;
26233 }
26234 else
26235 {
26236 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26237 but none of the characters it displays are in the range, it is
26238 also END + 1. */
26239 struct glyph *g = next->glyphs[TEXT_AREA];
26240 struct glyph *s = g;
26241 struct glyph *e = g + next->used[TEXT_AREA];
26242
26243 while (g < e)
26244 {
26245 if (((BUFFERP (g->object) || INTEGERP (g->object))
26246 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26247 /* If the buffer position of the first glyph in
26248 the row is equal to END_CHARPOS, it means
26249 the last character to be highlighted is the
26250 newline of ROW, and we must consider NEXT as
26251 END, not END+1. */
26252 || (((!next->reversed_p && g == s)
26253 || (next->reversed_p && g == e - 1))
26254 && (g->charpos == end_charpos
26255 /* Special case for when NEXT is an
26256 empty line at ZV. */
26257 || (g->charpos == -1
26258 && !row->ends_at_zv_p
26259 && next_start == end_charpos)))))
26260 /* A glyph that comes from DISP_STRING is by
26261 definition to be highlighted. */
26262 || EQ (g->object, disp_string))
26263 break;
26264 g++;
26265 }
26266 if (g == e)
26267 {
26268 *end = row;
26269 break;
26270 }
26271 /* The first row that ends at ZV must be the last to be
26272 highlighted. */
26273 else if (next->ends_at_zv_p)
26274 {
26275 *end = next;
26276 break;
26277 }
26278 }
26279 }
26280 }
26281
26282 /* This function sets the mouse_face_* elements of HLINFO, assuming
26283 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26284 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26285 for the overlay or run of text properties specifying the mouse
26286 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26287 before-string and after-string that must also be highlighted.
26288 DISP_STRING, if non-nil, is a display string that may cover some
26289 or all of the highlighted text. */
26290
26291 static void
26292 mouse_face_from_buffer_pos (Lisp_Object window,
26293 Mouse_HLInfo *hlinfo,
26294 ptrdiff_t mouse_charpos,
26295 ptrdiff_t start_charpos,
26296 ptrdiff_t end_charpos,
26297 Lisp_Object before_string,
26298 Lisp_Object after_string,
26299 Lisp_Object disp_string)
26300 {
26301 struct window *w = XWINDOW (window);
26302 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26303 struct glyph_row *r1, *r2;
26304 struct glyph *glyph, *end;
26305 ptrdiff_t ignore, pos;
26306 int x;
26307
26308 eassert (NILP (disp_string) || STRINGP (disp_string));
26309 eassert (NILP (before_string) || STRINGP (before_string));
26310 eassert (NILP (after_string) || STRINGP (after_string));
26311
26312 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26313 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26314 if (r1 == NULL)
26315 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26316 /* If the before-string or display-string contains newlines,
26317 rows_from_pos_range skips to its last row. Move back. */
26318 if (!NILP (before_string) || !NILP (disp_string))
26319 {
26320 struct glyph_row *prev;
26321 while ((prev = r1 - 1, prev >= first)
26322 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26323 && prev->used[TEXT_AREA] > 0)
26324 {
26325 struct glyph *beg = prev->glyphs[TEXT_AREA];
26326 glyph = beg + prev->used[TEXT_AREA];
26327 while (--glyph >= beg && INTEGERP (glyph->object));
26328 if (glyph < beg
26329 || !(EQ (glyph->object, before_string)
26330 || EQ (glyph->object, disp_string)))
26331 break;
26332 r1 = prev;
26333 }
26334 }
26335 if (r2 == NULL)
26336 {
26337 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26338 hlinfo->mouse_face_past_end = 1;
26339 }
26340 else if (!NILP (after_string))
26341 {
26342 /* If the after-string has newlines, advance to its last row. */
26343 struct glyph_row *next;
26344 struct glyph_row *last
26345 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26346
26347 for (next = r2 + 1;
26348 next <= last
26349 && next->used[TEXT_AREA] > 0
26350 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26351 ++next)
26352 r2 = next;
26353 }
26354 /* The rest of the display engine assumes that mouse_face_beg_row is
26355 either above mouse_face_end_row or identical to it. But with
26356 bidi-reordered continued lines, the row for START_CHARPOS could
26357 be below the row for END_CHARPOS. If so, swap the rows and store
26358 them in correct order. */
26359 if (r1->y > r2->y)
26360 {
26361 struct glyph_row *tem = r2;
26362
26363 r2 = r1;
26364 r1 = tem;
26365 }
26366
26367 hlinfo->mouse_face_beg_y = r1->y;
26368 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26369 hlinfo->mouse_face_end_y = r2->y;
26370 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26371
26372 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26373 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26374 could be anywhere in the row and in any order. The strategy
26375 below is to find the leftmost and the rightmost glyph that
26376 belongs to either of these 3 strings, or whose position is
26377 between START_CHARPOS and END_CHARPOS, and highlight all the
26378 glyphs between those two. This may cover more than just the text
26379 between START_CHARPOS and END_CHARPOS if the range of characters
26380 strides the bidi level boundary, e.g. if the beginning is in R2L
26381 text while the end is in L2R text or vice versa. */
26382 if (!r1->reversed_p)
26383 {
26384 /* This row is in a left to right paragraph. Scan it left to
26385 right. */
26386 glyph = r1->glyphs[TEXT_AREA];
26387 end = glyph + r1->used[TEXT_AREA];
26388 x = r1->x;
26389
26390 /* Skip truncation glyphs at the start of the glyph row. */
26391 if (r1->displays_text_p)
26392 for (; glyph < end
26393 && INTEGERP (glyph->object)
26394 && glyph->charpos < 0;
26395 ++glyph)
26396 x += glyph->pixel_width;
26397
26398 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26399 or DISP_STRING, and the first glyph from buffer whose
26400 position is between START_CHARPOS and END_CHARPOS. */
26401 for (; glyph < end
26402 && !INTEGERP (glyph->object)
26403 && !EQ (glyph->object, disp_string)
26404 && !(BUFFERP (glyph->object)
26405 && (glyph->charpos >= start_charpos
26406 && glyph->charpos < end_charpos));
26407 ++glyph)
26408 {
26409 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26410 are present at buffer positions between START_CHARPOS and
26411 END_CHARPOS, or if they come from an overlay. */
26412 if (EQ (glyph->object, before_string))
26413 {
26414 pos = string_buffer_position (before_string,
26415 start_charpos);
26416 /* If pos == 0, it means before_string came from an
26417 overlay, not from a buffer position. */
26418 if (!pos || (pos >= start_charpos && pos < end_charpos))
26419 break;
26420 }
26421 else if (EQ (glyph->object, after_string))
26422 {
26423 pos = string_buffer_position (after_string, end_charpos);
26424 if (!pos || (pos >= start_charpos && pos < end_charpos))
26425 break;
26426 }
26427 x += glyph->pixel_width;
26428 }
26429 hlinfo->mouse_face_beg_x = x;
26430 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26431 }
26432 else
26433 {
26434 /* This row is in a right to left paragraph. Scan it right to
26435 left. */
26436 struct glyph *g;
26437
26438 end = r1->glyphs[TEXT_AREA] - 1;
26439 glyph = end + r1->used[TEXT_AREA];
26440
26441 /* Skip truncation glyphs at the start of the glyph row. */
26442 if (r1->displays_text_p)
26443 for (; glyph > end
26444 && INTEGERP (glyph->object)
26445 && glyph->charpos < 0;
26446 --glyph)
26447 ;
26448
26449 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26450 or DISP_STRING, and the first glyph from buffer whose
26451 position is between START_CHARPOS and END_CHARPOS. */
26452 for (; glyph > end
26453 && !INTEGERP (glyph->object)
26454 && !EQ (glyph->object, disp_string)
26455 && !(BUFFERP (glyph->object)
26456 && (glyph->charpos >= start_charpos
26457 && glyph->charpos < end_charpos));
26458 --glyph)
26459 {
26460 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26461 are present at buffer positions between START_CHARPOS and
26462 END_CHARPOS, or if they come from an overlay. */
26463 if (EQ (glyph->object, before_string))
26464 {
26465 pos = string_buffer_position (before_string, start_charpos);
26466 /* If pos == 0, it means before_string came from an
26467 overlay, not from a buffer position. */
26468 if (!pos || (pos >= start_charpos && pos < end_charpos))
26469 break;
26470 }
26471 else if (EQ (glyph->object, after_string))
26472 {
26473 pos = string_buffer_position (after_string, end_charpos);
26474 if (!pos || (pos >= start_charpos && pos < end_charpos))
26475 break;
26476 }
26477 }
26478
26479 glyph++; /* first glyph to the right of the highlighted area */
26480 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26481 x += g->pixel_width;
26482 hlinfo->mouse_face_beg_x = x;
26483 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26484 }
26485
26486 /* If the highlight ends in a different row, compute GLYPH and END
26487 for the end row. Otherwise, reuse the values computed above for
26488 the row where the highlight begins. */
26489 if (r2 != r1)
26490 {
26491 if (!r2->reversed_p)
26492 {
26493 glyph = r2->glyphs[TEXT_AREA];
26494 end = glyph + r2->used[TEXT_AREA];
26495 x = r2->x;
26496 }
26497 else
26498 {
26499 end = r2->glyphs[TEXT_AREA] - 1;
26500 glyph = end + r2->used[TEXT_AREA];
26501 }
26502 }
26503
26504 if (!r2->reversed_p)
26505 {
26506 /* Skip truncation and continuation glyphs near the end of the
26507 row, and also blanks and stretch glyphs inserted by
26508 extend_face_to_end_of_line. */
26509 while (end > glyph
26510 && INTEGERP ((end - 1)->object))
26511 --end;
26512 /* Scan the rest of the glyph row from the end, looking for the
26513 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26514 DISP_STRING, or whose position is between START_CHARPOS
26515 and END_CHARPOS */
26516 for (--end;
26517 end > glyph
26518 && !INTEGERP (end->object)
26519 && !EQ (end->object, disp_string)
26520 && !(BUFFERP (end->object)
26521 && (end->charpos >= start_charpos
26522 && end->charpos < end_charpos));
26523 --end)
26524 {
26525 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26526 are present at buffer positions between START_CHARPOS and
26527 END_CHARPOS, or if they come from an overlay. */
26528 if (EQ (end->object, before_string))
26529 {
26530 pos = string_buffer_position (before_string, start_charpos);
26531 if (!pos || (pos >= start_charpos && pos < end_charpos))
26532 break;
26533 }
26534 else if (EQ (end->object, after_string))
26535 {
26536 pos = string_buffer_position (after_string, end_charpos);
26537 if (!pos || (pos >= start_charpos && pos < end_charpos))
26538 break;
26539 }
26540 }
26541 /* Find the X coordinate of the last glyph to be highlighted. */
26542 for (; glyph <= end; ++glyph)
26543 x += glyph->pixel_width;
26544
26545 hlinfo->mouse_face_end_x = x;
26546 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26547 }
26548 else
26549 {
26550 /* Skip truncation and continuation glyphs near the end of the
26551 row, and also blanks and stretch glyphs inserted by
26552 extend_face_to_end_of_line. */
26553 x = r2->x;
26554 end++;
26555 while (end < glyph
26556 && INTEGERP (end->object))
26557 {
26558 x += end->pixel_width;
26559 ++end;
26560 }
26561 /* Scan the rest of the glyph row from the end, looking for the
26562 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26563 DISP_STRING, or whose position is between START_CHARPOS
26564 and END_CHARPOS */
26565 for ( ;
26566 end < glyph
26567 && !INTEGERP (end->object)
26568 && !EQ (end->object, disp_string)
26569 && !(BUFFERP (end->object)
26570 && (end->charpos >= start_charpos
26571 && end->charpos < end_charpos));
26572 ++end)
26573 {
26574 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26575 are present at buffer positions between START_CHARPOS and
26576 END_CHARPOS, or if they come from an overlay. */
26577 if (EQ (end->object, before_string))
26578 {
26579 pos = string_buffer_position (before_string, start_charpos);
26580 if (!pos || (pos >= start_charpos && pos < end_charpos))
26581 break;
26582 }
26583 else if (EQ (end->object, after_string))
26584 {
26585 pos = string_buffer_position (after_string, end_charpos);
26586 if (!pos || (pos >= start_charpos && pos < end_charpos))
26587 break;
26588 }
26589 x += end->pixel_width;
26590 }
26591 /* If we exited the above loop because we arrived at the last
26592 glyph of the row, and its buffer position is still not in
26593 range, it means the last character in range is the preceding
26594 newline. Bump the end column and x values to get past the
26595 last glyph. */
26596 if (end == glyph
26597 && BUFFERP (end->object)
26598 && (end->charpos < start_charpos
26599 || end->charpos >= end_charpos))
26600 {
26601 x += end->pixel_width;
26602 ++end;
26603 }
26604 hlinfo->mouse_face_end_x = x;
26605 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26606 }
26607
26608 hlinfo->mouse_face_window = window;
26609 hlinfo->mouse_face_face_id
26610 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26611 mouse_charpos + 1,
26612 !hlinfo->mouse_face_hidden, -1);
26613 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26614 }
26615
26616 /* The following function is not used anymore (replaced with
26617 mouse_face_from_string_pos), but I leave it here for the time
26618 being, in case someone would. */
26619
26620 #if 0 /* not used */
26621
26622 /* Find the position of the glyph for position POS in OBJECT in
26623 window W's current matrix, and return in *X, *Y the pixel
26624 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26625
26626 RIGHT_P non-zero means return the position of the right edge of the
26627 glyph, RIGHT_P zero means return the left edge position.
26628
26629 If no glyph for POS exists in the matrix, return the position of
26630 the glyph with the next smaller position that is in the matrix, if
26631 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26632 exists in the matrix, return the position of the glyph with the
26633 next larger position in OBJECT.
26634
26635 Value is non-zero if a glyph was found. */
26636
26637 static int
26638 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26639 int *hpos, int *vpos, int *x, int *y, int right_p)
26640 {
26641 int yb = window_text_bottom_y (w);
26642 struct glyph_row *r;
26643 struct glyph *best_glyph = NULL;
26644 struct glyph_row *best_row = NULL;
26645 int best_x = 0;
26646
26647 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26648 r->enabled_p && r->y < yb;
26649 ++r)
26650 {
26651 struct glyph *g = r->glyphs[TEXT_AREA];
26652 struct glyph *e = g + r->used[TEXT_AREA];
26653 int gx;
26654
26655 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26656 if (EQ (g->object, object))
26657 {
26658 if (g->charpos == pos)
26659 {
26660 best_glyph = g;
26661 best_x = gx;
26662 best_row = r;
26663 goto found;
26664 }
26665 else if (best_glyph == NULL
26666 || ((eabs (g->charpos - pos)
26667 < eabs (best_glyph->charpos - pos))
26668 && (right_p
26669 ? g->charpos < pos
26670 : g->charpos > pos)))
26671 {
26672 best_glyph = g;
26673 best_x = gx;
26674 best_row = r;
26675 }
26676 }
26677 }
26678
26679 found:
26680
26681 if (best_glyph)
26682 {
26683 *x = best_x;
26684 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26685
26686 if (right_p)
26687 {
26688 *x += best_glyph->pixel_width;
26689 ++*hpos;
26690 }
26691
26692 *y = best_row->y;
26693 *vpos = best_row - w->current_matrix->rows;
26694 }
26695
26696 return best_glyph != NULL;
26697 }
26698 #endif /* not used */
26699
26700 /* Find the positions of the first and the last glyphs in window W's
26701 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26702 (assumed to be a string), and return in HLINFO's mouse_face_*
26703 members the pixel and column/row coordinates of those glyphs. */
26704
26705 static void
26706 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26707 Lisp_Object object,
26708 ptrdiff_t startpos, ptrdiff_t endpos)
26709 {
26710 int yb = window_text_bottom_y (w);
26711 struct glyph_row *r;
26712 struct glyph *g, *e;
26713 int gx;
26714 int found = 0;
26715
26716 /* Find the glyph row with at least one position in the range
26717 [STARTPOS..ENDPOS], and the first glyph in that row whose
26718 position belongs to that range. */
26719 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26720 r->enabled_p && r->y < yb;
26721 ++r)
26722 {
26723 if (!r->reversed_p)
26724 {
26725 g = r->glyphs[TEXT_AREA];
26726 e = g + r->used[TEXT_AREA];
26727 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26728 if (EQ (g->object, object)
26729 && startpos <= g->charpos && g->charpos <= endpos)
26730 {
26731 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26732 hlinfo->mouse_face_beg_y = r->y;
26733 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26734 hlinfo->mouse_face_beg_x = gx;
26735 found = 1;
26736 break;
26737 }
26738 }
26739 else
26740 {
26741 struct glyph *g1;
26742
26743 e = r->glyphs[TEXT_AREA];
26744 g = e + r->used[TEXT_AREA];
26745 for ( ; g > e; --g)
26746 if (EQ ((g-1)->object, object)
26747 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26748 {
26749 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26750 hlinfo->mouse_face_beg_y = r->y;
26751 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26752 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26753 gx += g1->pixel_width;
26754 hlinfo->mouse_face_beg_x = gx;
26755 found = 1;
26756 break;
26757 }
26758 }
26759 if (found)
26760 break;
26761 }
26762
26763 if (!found)
26764 return;
26765
26766 /* Starting with the next row, look for the first row which does NOT
26767 include any glyphs whose positions are in the range. */
26768 for (++r; r->enabled_p && r->y < yb; ++r)
26769 {
26770 g = r->glyphs[TEXT_AREA];
26771 e = g + r->used[TEXT_AREA];
26772 found = 0;
26773 for ( ; g < e; ++g)
26774 if (EQ (g->object, object)
26775 && startpos <= g->charpos && g->charpos <= endpos)
26776 {
26777 found = 1;
26778 break;
26779 }
26780 if (!found)
26781 break;
26782 }
26783
26784 /* The highlighted region ends on the previous row. */
26785 r--;
26786
26787 /* Set the end row and its vertical pixel coordinate. */
26788 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26789 hlinfo->mouse_face_end_y = r->y;
26790
26791 /* Compute and set the end column and the end column's horizontal
26792 pixel coordinate. */
26793 if (!r->reversed_p)
26794 {
26795 g = r->glyphs[TEXT_AREA];
26796 e = g + r->used[TEXT_AREA];
26797 for ( ; e > g; --e)
26798 if (EQ ((e-1)->object, object)
26799 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26800 break;
26801 hlinfo->mouse_face_end_col = e - g;
26802
26803 for (gx = r->x; g < e; ++g)
26804 gx += g->pixel_width;
26805 hlinfo->mouse_face_end_x = gx;
26806 }
26807 else
26808 {
26809 e = r->glyphs[TEXT_AREA];
26810 g = e + r->used[TEXT_AREA];
26811 for (gx = r->x ; e < g; ++e)
26812 {
26813 if (EQ (e->object, object)
26814 && startpos <= e->charpos && e->charpos <= endpos)
26815 break;
26816 gx += e->pixel_width;
26817 }
26818 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26819 hlinfo->mouse_face_end_x = gx;
26820 }
26821 }
26822
26823 #ifdef HAVE_WINDOW_SYSTEM
26824
26825 /* See if position X, Y is within a hot-spot of an image. */
26826
26827 static int
26828 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26829 {
26830 if (!CONSP (hot_spot))
26831 return 0;
26832
26833 if (EQ (XCAR (hot_spot), Qrect))
26834 {
26835 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26836 Lisp_Object rect = XCDR (hot_spot);
26837 Lisp_Object tem;
26838 if (!CONSP (rect))
26839 return 0;
26840 if (!CONSP (XCAR (rect)))
26841 return 0;
26842 if (!CONSP (XCDR (rect)))
26843 return 0;
26844 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26845 return 0;
26846 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26847 return 0;
26848 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26849 return 0;
26850 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26851 return 0;
26852 return 1;
26853 }
26854 else if (EQ (XCAR (hot_spot), Qcircle))
26855 {
26856 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26857 Lisp_Object circ = XCDR (hot_spot);
26858 Lisp_Object lr, lx0, ly0;
26859 if (CONSP (circ)
26860 && CONSP (XCAR (circ))
26861 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26862 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26863 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26864 {
26865 double r = XFLOATINT (lr);
26866 double dx = XINT (lx0) - x;
26867 double dy = XINT (ly0) - y;
26868 return (dx * dx + dy * dy <= r * r);
26869 }
26870 }
26871 else if (EQ (XCAR (hot_spot), Qpoly))
26872 {
26873 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26874 if (VECTORP (XCDR (hot_spot)))
26875 {
26876 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26877 Lisp_Object *poly = v->contents;
26878 ptrdiff_t n = v->header.size;
26879 ptrdiff_t i;
26880 int inside = 0;
26881 Lisp_Object lx, ly;
26882 int x0, y0;
26883
26884 /* Need an even number of coordinates, and at least 3 edges. */
26885 if (n < 6 || n & 1)
26886 return 0;
26887
26888 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26889 If count is odd, we are inside polygon. Pixels on edges
26890 may or may not be included depending on actual geometry of the
26891 polygon. */
26892 if ((lx = poly[n-2], !INTEGERP (lx))
26893 || (ly = poly[n-1], !INTEGERP (lx)))
26894 return 0;
26895 x0 = XINT (lx), y0 = XINT (ly);
26896 for (i = 0; i < n; i += 2)
26897 {
26898 int x1 = x0, y1 = y0;
26899 if ((lx = poly[i], !INTEGERP (lx))
26900 || (ly = poly[i+1], !INTEGERP (ly)))
26901 return 0;
26902 x0 = XINT (lx), y0 = XINT (ly);
26903
26904 /* Does this segment cross the X line? */
26905 if (x0 >= x)
26906 {
26907 if (x1 >= x)
26908 continue;
26909 }
26910 else if (x1 < x)
26911 continue;
26912 if (y > y0 && y > y1)
26913 continue;
26914 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26915 inside = !inside;
26916 }
26917 return inside;
26918 }
26919 }
26920 return 0;
26921 }
26922
26923 Lisp_Object
26924 find_hot_spot (Lisp_Object map, int x, int y)
26925 {
26926 while (CONSP (map))
26927 {
26928 if (CONSP (XCAR (map))
26929 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26930 return XCAR (map);
26931 map = XCDR (map);
26932 }
26933
26934 return Qnil;
26935 }
26936
26937 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26938 3, 3, 0,
26939 doc: /* Lookup in image map MAP coordinates X and Y.
26940 An image map is an alist where each element has the format (AREA ID PLIST).
26941 An AREA is specified as either a rectangle, a circle, or a polygon:
26942 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26943 pixel coordinates of the upper left and bottom right corners.
26944 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26945 and the radius of the circle; r may be a float or integer.
26946 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26947 vector describes one corner in the polygon.
26948 Returns the alist element for the first matching AREA in MAP. */)
26949 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26950 {
26951 if (NILP (map))
26952 return Qnil;
26953
26954 CHECK_NUMBER (x);
26955 CHECK_NUMBER (y);
26956
26957 return find_hot_spot (map,
26958 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
26959 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
26960 }
26961
26962
26963 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26964 static void
26965 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26966 {
26967 /* Do not change cursor shape while dragging mouse. */
26968 if (!NILP (do_mouse_tracking))
26969 return;
26970
26971 if (!NILP (pointer))
26972 {
26973 if (EQ (pointer, Qarrow))
26974 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26975 else if (EQ (pointer, Qhand))
26976 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26977 else if (EQ (pointer, Qtext))
26978 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26979 else if (EQ (pointer, intern ("hdrag")))
26980 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26981 #ifdef HAVE_X_WINDOWS
26982 else if (EQ (pointer, intern ("vdrag")))
26983 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26984 #endif
26985 else if (EQ (pointer, intern ("hourglass")))
26986 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26987 else if (EQ (pointer, Qmodeline))
26988 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26989 else
26990 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26991 }
26992
26993 if (cursor != No_Cursor)
26994 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26995 }
26996
26997 #endif /* HAVE_WINDOW_SYSTEM */
26998
26999 /* Take proper action when mouse has moved to the mode or header line
27000 or marginal area AREA of window W, x-position X and y-position Y.
27001 X is relative to the start of the text display area of W, so the
27002 width of bitmap areas and scroll bars must be subtracted to get a
27003 position relative to the start of the mode line. */
27004
27005 static void
27006 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27007 enum window_part area)
27008 {
27009 struct window *w = XWINDOW (window);
27010 struct frame *f = XFRAME (w->frame);
27011 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27012 #ifdef HAVE_WINDOW_SYSTEM
27013 Display_Info *dpyinfo;
27014 #endif
27015 Cursor cursor = No_Cursor;
27016 Lisp_Object pointer = Qnil;
27017 int dx, dy, width, height;
27018 ptrdiff_t charpos;
27019 Lisp_Object string, object = Qnil;
27020 Lisp_Object pos IF_LINT (= Qnil), help;
27021
27022 Lisp_Object mouse_face;
27023 int original_x_pixel = x;
27024 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27025 struct glyph_row *row IF_LINT (= 0);
27026
27027 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27028 {
27029 int x0;
27030 struct glyph *end;
27031
27032 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27033 returns them in row/column units! */
27034 string = mode_line_string (w, area, &x, &y, &charpos,
27035 &object, &dx, &dy, &width, &height);
27036
27037 row = (area == ON_MODE_LINE
27038 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27039 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27040
27041 /* Find the glyph under the mouse pointer. */
27042 if (row->mode_line_p && row->enabled_p)
27043 {
27044 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27045 end = glyph + row->used[TEXT_AREA];
27046
27047 for (x0 = original_x_pixel;
27048 glyph < end && x0 >= glyph->pixel_width;
27049 ++glyph)
27050 x0 -= glyph->pixel_width;
27051
27052 if (glyph >= end)
27053 glyph = NULL;
27054 }
27055 }
27056 else
27057 {
27058 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27059 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27060 returns them in row/column units! */
27061 string = marginal_area_string (w, area, &x, &y, &charpos,
27062 &object, &dx, &dy, &width, &height);
27063 }
27064
27065 help = Qnil;
27066
27067 #ifdef HAVE_WINDOW_SYSTEM
27068 if (IMAGEP (object))
27069 {
27070 Lisp_Object image_map, hotspot;
27071 if ((image_map = Fplist_get (XCDR (object), QCmap),
27072 !NILP (image_map))
27073 && (hotspot = find_hot_spot (image_map, dx, dy),
27074 CONSP (hotspot))
27075 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27076 {
27077 Lisp_Object plist;
27078
27079 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27080 If so, we could look for mouse-enter, mouse-leave
27081 properties in PLIST (and do something...). */
27082 hotspot = XCDR (hotspot);
27083 if (CONSP (hotspot)
27084 && (plist = XCAR (hotspot), CONSP (plist)))
27085 {
27086 pointer = Fplist_get (plist, Qpointer);
27087 if (NILP (pointer))
27088 pointer = Qhand;
27089 help = Fplist_get (plist, Qhelp_echo);
27090 if (!NILP (help))
27091 {
27092 help_echo_string = help;
27093 XSETWINDOW (help_echo_window, w);
27094 help_echo_object = w->buffer;
27095 help_echo_pos = charpos;
27096 }
27097 }
27098 }
27099 if (NILP (pointer))
27100 pointer = Fplist_get (XCDR (object), QCpointer);
27101 }
27102 #endif /* HAVE_WINDOW_SYSTEM */
27103
27104 if (STRINGP (string))
27105 pos = make_number (charpos);
27106
27107 /* Set the help text and mouse pointer. If the mouse is on a part
27108 of the mode line without any text (e.g. past the right edge of
27109 the mode line text), use the default help text and pointer. */
27110 if (STRINGP (string) || area == ON_MODE_LINE)
27111 {
27112 /* Arrange to display the help by setting the global variables
27113 help_echo_string, help_echo_object, and help_echo_pos. */
27114 if (NILP (help))
27115 {
27116 if (STRINGP (string))
27117 help = Fget_text_property (pos, Qhelp_echo, string);
27118
27119 if (!NILP (help))
27120 {
27121 help_echo_string = help;
27122 XSETWINDOW (help_echo_window, w);
27123 help_echo_object = string;
27124 help_echo_pos = charpos;
27125 }
27126 else if (area == ON_MODE_LINE)
27127 {
27128 Lisp_Object default_help
27129 = buffer_local_value_1 (Qmode_line_default_help_echo,
27130 w->buffer);
27131
27132 if (STRINGP (default_help))
27133 {
27134 help_echo_string = default_help;
27135 XSETWINDOW (help_echo_window, w);
27136 help_echo_object = Qnil;
27137 help_echo_pos = -1;
27138 }
27139 }
27140 }
27141
27142 #ifdef HAVE_WINDOW_SYSTEM
27143 /* Change the mouse pointer according to what is under it. */
27144 if (FRAME_WINDOW_P (f))
27145 {
27146 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27147 if (STRINGP (string))
27148 {
27149 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27150
27151 if (NILP (pointer))
27152 pointer = Fget_text_property (pos, Qpointer, string);
27153
27154 /* Change the mouse pointer according to what is under X/Y. */
27155 if (NILP (pointer)
27156 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27157 {
27158 Lisp_Object map;
27159 map = Fget_text_property (pos, Qlocal_map, string);
27160 if (!KEYMAPP (map))
27161 map = Fget_text_property (pos, Qkeymap, string);
27162 if (!KEYMAPP (map))
27163 cursor = dpyinfo->vertical_scroll_bar_cursor;
27164 }
27165 }
27166 else
27167 /* Default mode-line pointer. */
27168 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27169 }
27170 #endif
27171 }
27172
27173 /* Change the mouse face according to what is under X/Y. */
27174 if (STRINGP (string))
27175 {
27176 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27177 if (!NILP (mouse_face)
27178 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27179 && glyph)
27180 {
27181 Lisp_Object b, e;
27182
27183 struct glyph * tmp_glyph;
27184
27185 int gpos;
27186 int gseq_length;
27187 int total_pixel_width;
27188 ptrdiff_t begpos, endpos, ignore;
27189
27190 int vpos, hpos;
27191
27192 b = Fprevious_single_property_change (make_number (charpos + 1),
27193 Qmouse_face, string, Qnil);
27194 if (NILP (b))
27195 begpos = 0;
27196 else
27197 begpos = XINT (b);
27198
27199 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27200 if (NILP (e))
27201 endpos = SCHARS (string);
27202 else
27203 endpos = XINT (e);
27204
27205 /* Calculate the glyph position GPOS of GLYPH in the
27206 displayed string, relative to the beginning of the
27207 highlighted part of the string.
27208
27209 Note: GPOS is different from CHARPOS. CHARPOS is the
27210 position of GLYPH in the internal string object. A mode
27211 line string format has structures which are converted to
27212 a flattened string by the Emacs Lisp interpreter. The
27213 internal string is an element of those structures. The
27214 displayed string is the flattened string. */
27215 tmp_glyph = row_start_glyph;
27216 while (tmp_glyph < glyph
27217 && (!(EQ (tmp_glyph->object, glyph->object)
27218 && begpos <= tmp_glyph->charpos
27219 && tmp_glyph->charpos < endpos)))
27220 tmp_glyph++;
27221 gpos = glyph - tmp_glyph;
27222
27223 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27224 the highlighted part of the displayed string to which
27225 GLYPH belongs. Note: GSEQ_LENGTH is different from
27226 SCHARS (STRING), because the latter returns the length of
27227 the internal string. */
27228 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27229 tmp_glyph > glyph
27230 && (!(EQ (tmp_glyph->object, glyph->object)
27231 && begpos <= tmp_glyph->charpos
27232 && tmp_glyph->charpos < endpos));
27233 tmp_glyph--)
27234 ;
27235 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27236
27237 /* Calculate the total pixel width of all the glyphs between
27238 the beginning of the highlighted area and GLYPH. */
27239 total_pixel_width = 0;
27240 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27241 total_pixel_width += tmp_glyph->pixel_width;
27242
27243 /* Pre calculation of re-rendering position. Note: X is in
27244 column units here, after the call to mode_line_string or
27245 marginal_area_string. */
27246 hpos = x - gpos;
27247 vpos = (area == ON_MODE_LINE
27248 ? (w->current_matrix)->nrows - 1
27249 : 0);
27250
27251 /* If GLYPH's position is included in the region that is
27252 already drawn in mouse face, we have nothing to do. */
27253 if ( EQ (window, hlinfo->mouse_face_window)
27254 && (!row->reversed_p
27255 ? (hlinfo->mouse_face_beg_col <= hpos
27256 && hpos < hlinfo->mouse_face_end_col)
27257 /* In R2L rows we swap BEG and END, see below. */
27258 : (hlinfo->mouse_face_end_col <= hpos
27259 && hpos < hlinfo->mouse_face_beg_col))
27260 && hlinfo->mouse_face_beg_row == vpos )
27261 return;
27262
27263 if (clear_mouse_face (hlinfo))
27264 cursor = No_Cursor;
27265
27266 if (!row->reversed_p)
27267 {
27268 hlinfo->mouse_face_beg_col = hpos;
27269 hlinfo->mouse_face_beg_x = original_x_pixel
27270 - (total_pixel_width + dx);
27271 hlinfo->mouse_face_end_col = hpos + gseq_length;
27272 hlinfo->mouse_face_end_x = 0;
27273 }
27274 else
27275 {
27276 /* In R2L rows, show_mouse_face expects BEG and END
27277 coordinates to be swapped. */
27278 hlinfo->mouse_face_end_col = hpos;
27279 hlinfo->mouse_face_end_x = original_x_pixel
27280 - (total_pixel_width + dx);
27281 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27282 hlinfo->mouse_face_beg_x = 0;
27283 }
27284
27285 hlinfo->mouse_face_beg_row = vpos;
27286 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27287 hlinfo->mouse_face_beg_y = 0;
27288 hlinfo->mouse_face_end_y = 0;
27289 hlinfo->mouse_face_past_end = 0;
27290 hlinfo->mouse_face_window = window;
27291
27292 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27293 charpos,
27294 0, 0, 0,
27295 &ignore,
27296 glyph->face_id,
27297 1);
27298 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27299
27300 if (NILP (pointer))
27301 pointer = Qhand;
27302 }
27303 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27304 clear_mouse_face (hlinfo);
27305 }
27306 #ifdef HAVE_WINDOW_SYSTEM
27307 if (FRAME_WINDOW_P (f))
27308 define_frame_cursor1 (f, cursor, pointer);
27309 #endif
27310 }
27311
27312
27313 /* EXPORT:
27314 Take proper action when the mouse has moved to position X, Y on
27315 frame F as regards highlighting characters that have mouse-face
27316 properties. Also de-highlighting chars where the mouse was before.
27317 X and Y can be negative or out of range. */
27318
27319 void
27320 note_mouse_highlight (struct frame *f, int x, int y)
27321 {
27322 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27323 enum window_part part = ON_NOTHING;
27324 Lisp_Object window;
27325 struct window *w;
27326 Cursor cursor = No_Cursor;
27327 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27328 struct buffer *b;
27329
27330 /* When a menu is active, don't highlight because this looks odd. */
27331 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27332 if (popup_activated ())
27333 return;
27334 #endif
27335
27336 if (NILP (Vmouse_highlight)
27337 || !f->glyphs_initialized_p
27338 || f->pointer_invisible)
27339 return;
27340
27341 hlinfo->mouse_face_mouse_x = x;
27342 hlinfo->mouse_face_mouse_y = y;
27343 hlinfo->mouse_face_mouse_frame = f;
27344
27345 if (hlinfo->mouse_face_defer)
27346 return;
27347
27348 if (gc_in_progress)
27349 {
27350 hlinfo->mouse_face_deferred_gc = 1;
27351 return;
27352 }
27353
27354 /* Which window is that in? */
27355 window = window_from_coordinates (f, x, y, &part, 1);
27356
27357 /* If displaying active text in another window, clear that. */
27358 if (! EQ (window, hlinfo->mouse_face_window)
27359 /* Also clear if we move out of text area in same window. */
27360 || (!NILP (hlinfo->mouse_face_window)
27361 && !NILP (window)
27362 && part != ON_TEXT
27363 && part != ON_MODE_LINE
27364 && part != ON_HEADER_LINE))
27365 clear_mouse_face (hlinfo);
27366
27367 /* Not on a window -> return. */
27368 if (!WINDOWP (window))
27369 return;
27370
27371 /* Reset help_echo_string. It will get recomputed below. */
27372 help_echo_string = Qnil;
27373
27374 /* Convert to window-relative pixel coordinates. */
27375 w = XWINDOW (window);
27376 frame_to_window_pixel_xy (w, &x, &y);
27377
27378 #ifdef HAVE_WINDOW_SYSTEM
27379 /* Handle tool-bar window differently since it doesn't display a
27380 buffer. */
27381 if (EQ (window, f->tool_bar_window))
27382 {
27383 note_tool_bar_highlight (f, x, y);
27384 return;
27385 }
27386 #endif
27387
27388 /* Mouse is on the mode, header line or margin? */
27389 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27390 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27391 {
27392 note_mode_line_or_margin_highlight (window, x, y, part);
27393 return;
27394 }
27395
27396 #ifdef HAVE_WINDOW_SYSTEM
27397 if (part == ON_VERTICAL_BORDER)
27398 {
27399 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27400 help_echo_string = build_string ("drag-mouse-1: resize");
27401 }
27402 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27403 || part == ON_SCROLL_BAR)
27404 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27405 else
27406 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27407 #endif
27408
27409 /* Are we in a window whose display is up to date?
27410 And verify the buffer's text has not changed. */
27411 b = XBUFFER (w->buffer);
27412 if (part == ON_TEXT
27413 && EQ (w->window_end_valid, w->buffer)
27414 && w->last_modified == BUF_MODIFF (b)
27415 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27416 {
27417 int hpos, vpos, dx, dy, area = LAST_AREA;
27418 ptrdiff_t pos;
27419 struct glyph *glyph;
27420 Lisp_Object object;
27421 Lisp_Object mouse_face = Qnil, position;
27422 Lisp_Object *overlay_vec = NULL;
27423 ptrdiff_t i, noverlays;
27424 struct buffer *obuf;
27425 ptrdiff_t obegv, ozv;
27426 int same_region;
27427
27428 /* Find the glyph under X/Y. */
27429 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27430
27431 #ifdef HAVE_WINDOW_SYSTEM
27432 /* Look for :pointer property on image. */
27433 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27434 {
27435 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27436 if (img != NULL && IMAGEP (img->spec))
27437 {
27438 Lisp_Object image_map, hotspot;
27439 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27440 !NILP (image_map))
27441 && (hotspot = find_hot_spot (image_map,
27442 glyph->slice.img.x + dx,
27443 glyph->slice.img.y + dy),
27444 CONSP (hotspot))
27445 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27446 {
27447 Lisp_Object plist;
27448
27449 /* Could check XCAR (hotspot) to see if we enter/leave
27450 this hot-spot.
27451 If so, we could look for mouse-enter, mouse-leave
27452 properties in PLIST (and do something...). */
27453 hotspot = XCDR (hotspot);
27454 if (CONSP (hotspot)
27455 && (plist = XCAR (hotspot), CONSP (plist)))
27456 {
27457 pointer = Fplist_get (plist, Qpointer);
27458 if (NILP (pointer))
27459 pointer = Qhand;
27460 help_echo_string = Fplist_get (plist, Qhelp_echo);
27461 if (!NILP (help_echo_string))
27462 {
27463 help_echo_window = window;
27464 help_echo_object = glyph->object;
27465 help_echo_pos = glyph->charpos;
27466 }
27467 }
27468 }
27469 if (NILP (pointer))
27470 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27471 }
27472 }
27473 #endif /* HAVE_WINDOW_SYSTEM */
27474
27475 /* Clear mouse face if X/Y not over text. */
27476 if (glyph == NULL
27477 || area != TEXT_AREA
27478 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27479 /* Glyph's OBJECT is an integer for glyphs inserted by the
27480 display engine for its internal purposes, like truncation
27481 and continuation glyphs and blanks beyond the end of
27482 line's text on text terminals. If we are over such a
27483 glyph, we are not over any text. */
27484 || INTEGERP (glyph->object)
27485 /* R2L rows have a stretch glyph at their front, which
27486 stands for no text, whereas L2R rows have no glyphs at
27487 all beyond the end of text. Treat such stretch glyphs
27488 like we do with NULL glyphs in L2R rows. */
27489 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27490 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27491 && glyph->type == STRETCH_GLYPH
27492 && glyph->avoid_cursor_p))
27493 {
27494 if (clear_mouse_face (hlinfo))
27495 cursor = No_Cursor;
27496 #ifdef HAVE_WINDOW_SYSTEM
27497 if (FRAME_WINDOW_P (f) && NILP (pointer))
27498 {
27499 if (area != TEXT_AREA)
27500 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27501 else
27502 pointer = Vvoid_text_area_pointer;
27503 }
27504 #endif
27505 goto set_cursor;
27506 }
27507
27508 pos = glyph->charpos;
27509 object = glyph->object;
27510 if (!STRINGP (object) && !BUFFERP (object))
27511 goto set_cursor;
27512
27513 /* If we get an out-of-range value, return now; avoid an error. */
27514 if (BUFFERP (object) && pos > BUF_Z (b))
27515 goto set_cursor;
27516
27517 /* Make the window's buffer temporarily current for
27518 overlays_at and compute_char_face. */
27519 obuf = current_buffer;
27520 current_buffer = b;
27521 obegv = BEGV;
27522 ozv = ZV;
27523 BEGV = BEG;
27524 ZV = Z;
27525
27526 /* Is this char mouse-active or does it have help-echo? */
27527 position = make_number (pos);
27528
27529 if (BUFFERP (object))
27530 {
27531 /* Put all the overlays we want in a vector in overlay_vec. */
27532 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27533 /* Sort overlays into increasing priority order. */
27534 noverlays = sort_overlays (overlay_vec, noverlays, w);
27535 }
27536 else
27537 noverlays = 0;
27538
27539 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27540
27541 if (same_region)
27542 cursor = No_Cursor;
27543
27544 /* Check mouse-face highlighting. */
27545 if (! same_region
27546 /* If there exists an overlay with mouse-face overlapping
27547 the one we are currently highlighting, we have to
27548 check if we enter the overlapping overlay, and then
27549 highlight only that. */
27550 || (OVERLAYP (hlinfo->mouse_face_overlay)
27551 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27552 {
27553 /* Find the highest priority overlay with a mouse-face. */
27554 Lisp_Object overlay = Qnil;
27555 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27556 {
27557 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27558 if (!NILP (mouse_face))
27559 overlay = overlay_vec[i];
27560 }
27561
27562 /* If we're highlighting the same overlay as before, there's
27563 no need to do that again. */
27564 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27565 goto check_help_echo;
27566 hlinfo->mouse_face_overlay = overlay;
27567
27568 /* Clear the display of the old active region, if any. */
27569 if (clear_mouse_face (hlinfo))
27570 cursor = No_Cursor;
27571
27572 /* If no overlay applies, get a text property. */
27573 if (NILP (overlay))
27574 mouse_face = Fget_text_property (position, Qmouse_face, object);
27575
27576 /* Next, compute the bounds of the mouse highlighting and
27577 display it. */
27578 if (!NILP (mouse_face) && STRINGP (object))
27579 {
27580 /* The mouse-highlighting comes from a display string
27581 with a mouse-face. */
27582 Lisp_Object s, e;
27583 ptrdiff_t ignore;
27584
27585 s = Fprevious_single_property_change
27586 (make_number (pos + 1), Qmouse_face, object, Qnil);
27587 e = Fnext_single_property_change
27588 (position, Qmouse_face, object, Qnil);
27589 if (NILP (s))
27590 s = make_number (0);
27591 if (NILP (e))
27592 e = make_number (SCHARS (object) - 1);
27593 mouse_face_from_string_pos (w, hlinfo, object,
27594 XINT (s), XINT (e));
27595 hlinfo->mouse_face_past_end = 0;
27596 hlinfo->mouse_face_window = window;
27597 hlinfo->mouse_face_face_id
27598 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27599 glyph->face_id, 1);
27600 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27601 cursor = No_Cursor;
27602 }
27603 else
27604 {
27605 /* The mouse-highlighting, if any, comes from an overlay
27606 or text property in the buffer. */
27607 Lisp_Object buffer IF_LINT (= Qnil);
27608 Lisp_Object disp_string IF_LINT (= Qnil);
27609
27610 if (STRINGP (object))
27611 {
27612 /* If we are on a display string with no mouse-face,
27613 check if the text under it has one. */
27614 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27615 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27616 pos = string_buffer_position (object, start);
27617 if (pos > 0)
27618 {
27619 mouse_face = get_char_property_and_overlay
27620 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27621 buffer = w->buffer;
27622 disp_string = object;
27623 }
27624 }
27625 else
27626 {
27627 buffer = object;
27628 disp_string = Qnil;
27629 }
27630
27631 if (!NILP (mouse_face))
27632 {
27633 Lisp_Object before, after;
27634 Lisp_Object before_string, after_string;
27635 /* To correctly find the limits of mouse highlight
27636 in a bidi-reordered buffer, we must not use the
27637 optimization of limiting the search in
27638 previous-single-property-change and
27639 next-single-property-change, because
27640 rows_from_pos_range needs the real start and end
27641 positions to DTRT in this case. That's because
27642 the first row visible in a window does not
27643 necessarily display the character whose position
27644 is the smallest. */
27645 Lisp_Object lim1 =
27646 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27647 ? Fmarker_position (w->start)
27648 : Qnil;
27649 Lisp_Object lim2 =
27650 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27651 ? make_number (BUF_Z (XBUFFER (buffer))
27652 - XFASTINT (w->window_end_pos))
27653 : Qnil;
27654
27655 if (NILP (overlay))
27656 {
27657 /* Handle the text property case. */
27658 before = Fprevious_single_property_change
27659 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27660 after = Fnext_single_property_change
27661 (make_number (pos), Qmouse_face, buffer, lim2);
27662 before_string = after_string = Qnil;
27663 }
27664 else
27665 {
27666 /* Handle the overlay case. */
27667 before = Foverlay_start (overlay);
27668 after = Foverlay_end (overlay);
27669 before_string = Foverlay_get (overlay, Qbefore_string);
27670 after_string = Foverlay_get (overlay, Qafter_string);
27671
27672 if (!STRINGP (before_string)) before_string = Qnil;
27673 if (!STRINGP (after_string)) after_string = Qnil;
27674 }
27675
27676 mouse_face_from_buffer_pos (window, hlinfo, pos,
27677 NILP (before)
27678 ? 1
27679 : XFASTINT (before),
27680 NILP (after)
27681 ? BUF_Z (XBUFFER (buffer))
27682 : XFASTINT (after),
27683 before_string, after_string,
27684 disp_string);
27685 cursor = No_Cursor;
27686 }
27687 }
27688 }
27689
27690 check_help_echo:
27691
27692 /* Look for a `help-echo' property. */
27693 if (NILP (help_echo_string)) {
27694 Lisp_Object help, overlay;
27695
27696 /* Check overlays first. */
27697 help = overlay = Qnil;
27698 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27699 {
27700 overlay = overlay_vec[i];
27701 help = Foverlay_get (overlay, Qhelp_echo);
27702 }
27703
27704 if (!NILP (help))
27705 {
27706 help_echo_string = help;
27707 help_echo_window = window;
27708 help_echo_object = overlay;
27709 help_echo_pos = pos;
27710 }
27711 else
27712 {
27713 Lisp_Object obj = glyph->object;
27714 ptrdiff_t charpos = glyph->charpos;
27715
27716 /* Try text properties. */
27717 if (STRINGP (obj)
27718 && charpos >= 0
27719 && charpos < SCHARS (obj))
27720 {
27721 help = Fget_text_property (make_number (charpos),
27722 Qhelp_echo, obj);
27723 if (NILP (help))
27724 {
27725 /* If the string itself doesn't specify a help-echo,
27726 see if the buffer text ``under'' it does. */
27727 struct glyph_row *r
27728 = MATRIX_ROW (w->current_matrix, vpos);
27729 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27730 ptrdiff_t p = string_buffer_position (obj, start);
27731 if (p > 0)
27732 {
27733 help = Fget_char_property (make_number (p),
27734 Qhelp_echo, w->buffer);
27735 if (!NILP (help))
27736 {
27737 charpos = p;
27738 obj = w->buffer;
27739 }
27740 }
27741 }
27742 }
27743 else if (BUFFERP (obj)
27744 && charpos >= BEGV
27745 && charpos < ZV)
27746 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27747 obj);
27748
27749 if (!NILP (help))
27750 {
27751 help_echo_string = help;
27752 help_echo_window = window;
27753 help_echo_object = obj;
27754 help_echo_pos = charpos;
27755 }
27756 }
27757 }
27758
27759 #ifdef HAVE_WINDOW_SYSTEM
27760 /* Look for a `pointer' property. */
27761 if (FRAME_WINDOW_P (f) && NILP (pointer))
27762 {
27763 /* Check overlays first. */
27764 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27765 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27766
27767 if (NILP (pointer))
27768 {
27769 Lisp_Object obj = glyph->object;
27770 ptrdiff_t charpos = glyph->charpos;
27771
27772 /* Try text properties. */
27773 if (STRINGP (obj)
27774 && charpos >= 0
27775 && charpos < SCHARS (obj))
27776 {
27777 pointer = Fget_text_property (make_number (charpos),
27778 Qpointer, obj);
27779 if (NILP (pointer))
27780 {
27781 /* If the string itself doesn't specify a pointer,
27782 see if the buffer text ``under'' it does. */
27783 struct glyph_row *r
27784 = MATRIX_ROW (w->current_matrix, vpos);
27785 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27786 ptrdiff_t p = string_buffer_position (obj, start);
27787 if (p > 0)
27788 pointer = Fget_char_property (make_number (p),
27789 Qpointer, w->buffer);
27790 }
27791 }
27792 else if (BUFFERP (obj)
27793 && charpos >= BEGV
27794 && charpos < ZV)
27795 pointer = Fget_text_property (make_number (charpos),
27796 Qpointer, obj);
27797 }
27798 }
27799 #endif /* HAVE_WINDOW_SYSTEM */
27800
27801 BEGV = obegv;
27802 ZV = ozv;
27803 current_buffer = obuf;
27804 }
27805
27806 set_cursor:
27807
27808 #ifdef HAVE_WINDOW_SYSTEM
27809 if (FRAME_WINDOW_P (f))
27810 define_frame_cursor1 (f, cursor, pointer);
27811 #else
27812 /* This is here to prevent a compiler error, about "label at end of
27813 compound statement". */
27814 return;
27815 #endif
27816 }
27817
27818
27819 /* EXPORT for RIF:
27820 Clear any mouse-face on window W. This function is part of the
27821 redisplay interface, and is called from try_window_id and similar
27822 functions to ensure the mouse-highlight is off. */
27823
27824 void
27825 x_clear_window_mouse_face (struct window *w)
27826 {
27827 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27828 Lisp_Object window;
27829
27830 BLOCK_INPUT;
27831 XSETWINDOW (window, w);
27832 if (EQ (window, hlinfo->mouse_face_window))
27833 clear_mouse_face (hlinfo);
27834 UNBLOCK_INPUT;
27835 }
27836
27837
27838 /* EXPORT:
27839 Just discard the mouse face information for frame F, if any.
27840 This is used when the size of F is changed. */
27841
27842 void
27843 cancel_mouse_face (struct frame *f)
27844 {
27845 Lisp_Object window;
27846 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27847
27848 window = hlinfo->mouse_face_window;
27849 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27850 {
27851 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27852 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27853 hlinfo->mouse_face_window = Qnil;
27854 }
27855 }
27856
27857
27858 \f
27859 /***********************************************************************
27860 Exposure Events
27861 ***********************************************************************/
27862
27863 #ifdef HAVE_WINDOW_SYSTEM
27864
27865 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27866 which intersects rectangle R. R is in window-relative coordinates. */
27867
27868 static void
27869 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27870 enum glyph_row_area area)
27871 {
27872 struct glyph *first = row->glyphs[area];
27873 struct glyph *end = row->glyphs[area] + row->used[area];
27874 struct glyph *last;
27875 int first_x, start_x, x;
27876
27877 if (area == TEXT_AREA && row->fill_line_p)
27878 /* If row extends face to end of line write the whole line. */
27879 draw_glyphs (w, 0, row, area,
27880 0, row->used[area],
27881 DRAW_NORMAL_TEXT, 0);
27882 else
27883 {
27884 /* Set START_X to the window-relative start position for drawing glyphs of
27885 AREA. The first glyph of the text area can be partially visible.
27886 The first glyphs of other areas cannot. */
27887 start_x = window_box_left_offset (w, area);
27888 x = start_x;
27889 if (area == TEXT_AREA)
27890 x += row->x;
27891
27892 /* Find the first glyph that must be redrawn. */
27893 while (first < end
27894 && x + first->pixel_width < r->x)
27895 {
27896 x += first->pixel_width;
27897 ++first;
27898 }
27899
27900 /* Find the last one. */
27901 last = first;
27902 first_x = x;
27903 while (last < end
27904 && x < r->x + r->width)
27905 {
27906 x += last->pixel_width;
27907 ++last;
27908 }
27909
27910 /* Repaint. */
27911 if (last > first)
27912 draw_glyphs (w, first_x - start_x, row, area,
27913 first - row->glyphs[area], last - row->glyphs[area],
27914 DRAW_NORMAL_TEXT, 0);
27915 }
27916 }
27917
27918
27919 /* Redraw the parts of the glyph row ROW on window W intersecting
27920 rectangle R. R is in window-relative coordinates. Value is
27921 non-zero if mouse-face was overwritten. */
27922
27923 static int
27924 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27925 {
27926 eassert (row->enabled_p);
27927
27928 if (row->mode_line_p || w->pseudo_window_p)
27929 draw_glyphs (w, 0, row, TEXT_AREA,
27930 0, row->used[TEXT_AREA],
27931 DRAW_NORMAL_TEXT, 0);
27932 else
27933 {
27934 if (row->used[LEFT_MARGIN_AREA])
27935 expose_area (w, row, r, LEFT_MARGIN_AREA);
27936 if (row->used[TEXT_AREA])
27937 expose_area (w, row, r, TEXT_AREA);
27938 if (row->used[RIGHT_MARGIN_AREA])
27939 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27940 draw_row_fringe_bitmaps (w, row);
27941 }
27942
27943 return row->mouse_face_p;
27944 }
27945
27946
27947 /* Redraw those parts of glyphs rows during expose event handling that
27948 overlap other rows. Redrawing of an exposed line writes over parts
27949 of lines overlapping that exposed line; this function fixes that.
27950
27951 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27952 row in W's current matrix that is exposed and overlaps other rows.
27953 LAST_OVERLAPPING_ROW is the last such row. */
27954
27955 static void
27956 expose_overlaps (struct window *w,
27957 struct glyph_row *first_overlapping_row,
27958 struct glyph_row *last_overlapping_row,
27959 XRectangle *r)
27960 {
27961 struct glyph_row *row;
27962
27963 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27964 if (row->overlapping_p)
27965 {
27966 eassert (row->enabled_p && !row->mode_line_p);
27967
27968 row->clip = r;
27969 if (row->used[LEFT_MARGIN_AREA])
27970 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27971
27972 if (row->used[TEXT_AREA])
27973 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27974
27975 if (row->used[RIGHT_MARGIN_AREA])
27976 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27977 row->clip = NULL;
27978 }
27979 }
27980
27981
27982 /* Return non-zero if W's cursor intersects rectangle R. */
27983
27984 static int
27985 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27986 {
27987 XRectangle cr, result;
27988 struct glyph *cursor_glyph;
27989 struct glyph_row *row;
27990
27991 if (w->phys_cursor.vpos >= 0
27992 && w->phys_cursor.vpos < w->current_matrix->nrows
27993 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27994 row->enabled_p)
27995 && row->cursor_in_fringe_p)
27996 {
27997 /* Cursor is in the fringe. */
27998 cr.x = window_box_right_offset (w,
27999 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28000 ? RIGHT_MARGIN_AREA
28001 : TEXT_AREA));
28002 cr.y = row->y;
28003 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28004 cr.height = row->height;
28005 return x_intersect_rectangles (&cr, r, &result);
28006 }
28007
28008 cursor_glyph = get_phys_cursor_glyph (w);
28009 if (cursor_glyph)
28010 {
28011 /* r is relative to W's box, but w->phys_cursor.x is relative
28012 to left edge of W's TEXT area. Adjust it. */
28013 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28014 cr.y = w->phys_cursor.y;
28015 cr.width = cursor_glyph->pixel_width;
28016 cr.height = w->phys_cursor_height;
28017 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28018 I assume the effect is the same -- and this is portable. */
28019 return x_intersect_rectangles (&cr, r, &result);
28020 }
28021 /* If we don't understand the format, pretend we're not in the hot-spot. */
28022 return 0;
28023 }
28024
28025
28026 /* EXPORT:
28027 Draw a vertical window border to the right of window W if W doesn't
28028 have vertical scroll bars. */
28029
28030 void
28031 x_draw_vertical_border (struct window *w)
28032 {
28033 struct frame *f = XFRAME (WINDOW_FRAME (w));
28034
28035 /* We could do better, if we knew what type of scroll-bar the adjacent
28036 windows (on either side) have... But we don't :-(
28037 However, I think this works ok. ++KFS 2003-04-25 */
28038
28039 /* Redraw borders between horizontally adjacent windows. Don't
28040 do it for frames with vertical scroll bars because either the
28041 right scroll bar of a window, or the left scroll bar of its
28042 neighbor will suffice as a border. */
28043 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28044 return;
28045
28046 if (!WINDOW_RIGHTMOST_P (w)
28047 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28048 {
28049 int x0, x1, y0, y1;
28050
28051 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28052 y1 -= 1;
28053
28054 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28055 x1 -= 1;
28056
28057 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28058 }
28059 else if (!WINDOW_LEFTMOST_P (w)
28060 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28061 {
28062 int x0, x1, y0, y1;
28063
28064 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28065 y1 -= 1;
28066
28067 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28068 x0 -= 1;
28069
28070 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28071 }
28072 }
28073
28074
28075 /* Redraw the part of window W intersection rectangle FR. Pixel
28076 coordinates in FR are frame-relative. Call this function with
28077 input blocked. Value is non-zero if the exposure overwrites
28078 mouse-face. */
28079
28080 static int
28081 expose_window (struct window *w, XRectangle *fr)
28082 {
28083 struct frame *f = XFRAME (w->frame);
28084 XRectangle wr, r;
28085 int mouse_face_overwritten_p = 0;
28086
28087 /* If window is not yet fully initialized, do nothing. This can
28088 happen when toolkit scroll bars are used and a window is split.
28089 Reconfiguring the scroll bar will generate an expose for a newly
28090 created window. */
28091 if (w->current_matrix == NULL)
28092 return 0;
28093
28094 /* When we're currently updating the window, display and current
28095 matrix usually don't agree. Arrange for a thorough display
28096 later. */
28097 if (w == updated_window)
28098 {
28099 SET_FRAME_GARBAGED (f);
28100 return 0;
28101 }
28102
28103 /* Frame-relative pixel rectangle of W. */
28104 wr.x = WINDOW_LEFT_EDGE_X (w);
28105 wr.y = WINDOW_TOP_EDGE_Y (w);
28106 wr.width = WINDOW_TOTAL_WIDTH (w);
28107 wr.height = WINDOW_TOTAL_HEIGHT (w);
28108
28109 if (x_intersect_rectangles (fr, &wr, &r))
28110 {
28111 int yb = window_text_bottom_y (w);
28112 struct glyph_row *row;
28113 int cursor_cleared_p, phys_cursor_on_p;
28114 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28115
28116 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28117 r.x, r.y, r.width, r.height));
28118
28119 /* Convert to window coordinates. */
28120 r.x -= WINDOW_LEFT_EDGE_X (w);
28121 r.y -= WINDOW_TOP_EDGE_Y (w);
28122
28123 /* Turn off the cursor. */
28124 if (!w->pseudo_window_p
28125 && phys_cursor_in_rect_p (w, &r))
28126 {
28127 x_clear_cursor (w);
28128 cursor_cleared_p = 1;
28129 }
28130 else
28131 cursor_cleared_p = 0;
28132
28133 /* If the row containing the cursor extends face to end of line,
28134 then expose_area might overwrite the cursor outside the
28135 rectangle and thus notice_overwritten_cursor might clear
28136 w->phys_cursor_on_p. We remember the original value and
28137 check later if it is changed. */
28138 phys_cursor_on_p = w->phys_cursor_on_p;
28139
28140 /* Update lines intersecting rectangle R. */
28141 first_overlapping_row = last_overlapping_row = NULL;
28142 for (row = w->current_matrix->rows;
28143 row->enabled_p;
28144 ++row)
28145 {
28146 int y0 = row->y;
28147 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28148
28149 if ((y0 >= r.y && y0 < r.y + r.height)
28150 || (y1 > r.y && y1 < r.y + r.height)
28151 || (r.y >= y0 && r.y < y1)
28152 || (r.y + r.height > y0 && r.y + r.height < y1))
28153 {
28154 /* A header line may be overlapping, but there is no need
28155 to fix overlapping areas for them. KFS 2005-02-12 */
28156 if (row->overlapping_p && !row->mode_line_p)
28157 {
28158 if (first_overlapping_row == NULL)
28159 first_overlapping_row = row;
28160 last_overlapping_row = row;
28161 }
28162
28163 row->clip = fr;
28164 if (expose_line (w, row, &r))
28165 mouse_face_overwritten_p = 1;
28166 row->clip = NULL;
28167 }
28168 else if (row->overlapping_p)
28169 {
28170 /* We must redraw a row overlapping the exposed area. */
28171 if (y0 < r.y
28172 ? y0 + row->phys_height > r.y
28173 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28174 {
28175 if (first_overlapping_row == NULL)
28176 first_overlapping_row = row;
28177 last_overlapping_row = row;
28178 }
28179 }
28180
28181 if (y1 >= yb)
28182 break;
28183 }
28184
28185 /* Display the mode line if there is one. */
28186 if (WINDOW_WANTS_MODELINE_P (w)
28187 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28188 row->enabled_p)
28189 && row->y < r.y + r.height)
28190 {
28191 if (expose_line (w, row, &r))
28192 mouse_face_overwritten_p = 1;
28193 }
28194
28195 if (!w->pseudo_window_p)
28196 {
28197 /* Fix the display of overlapping rows. */
28198 if (first_overlapping_row)
28199 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28200 fr);
28201
28202 /* Draw border between windows. */
28203 x_draw_vertical_border (w);
28204
28205 /* Turn the cursor on again. */
28206 if (cursor_cleared_p
28207 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28208 update_window_cursor (w, 1);
28209 }
28210 }
28211
28212 return mouse_face_overwritten_p;
28213 }
28214
28215
28216
28217 /* Redraw (parts) of all windows in the window tree rooted at W that
28218 intersect R. R contains frame pixel coordinates. Value is
28219 non-zero if the exposure overwrites mouse-face. */
28220
28221 static int
28222 expose_window_tree (struct window *w, XRectangle *r)
28223 {
28224 struct frame *f = XFRAME (w->frame);
28225 int mouse_face_overwritten_p = 0;
28226
28227 while (w && !FRAME_GARBAGED_P (f))
28228 {
28229 if (!NILP (w->hchild))
28230 mouse_face_overwritten_p
28231 |= expose_window_tree (XWINDOW (w->hchild), r);
28232 else if (!NILP (w->vchild))
28233 mouse_face_overwritten_p
28234 |= expose_window_tree (XWINDOW (w->vchild), r);
28235 else
28236 mouse_face_overwritten_p |= expose_window (w, r);
28237
28238 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28239 }
28240
28241 return mouse_face_overwritten_p;
28242 }
28243
28244
28245 /* EXPORT:
28246 Redisplay an exposed area of frame F. X and Y are the upper-left
28247 corner of the exposed rectangle. W and H are width and height of
28248 the exposed area. All are pixel values. W or H zero means redraw
28249 the entire frame. */
28250
28251 void
28252 expose_frame (struct frame *f, int x, int y, int w, int h)
28253 {
28254 XRectangle r;
28255 int mouse_face_overwritten_p = 0;
28256
28257 TRACE ((stderr, "expose_frame "));
28258
28259 /* No need to redraw if frame will be redrawn soon. */
28260 if (FRAME_GARBAGED_P (f))
28261 {
28262 TRACE ((stderr, " garbaged\n"));
28263 return;
28264 }
28265
28266 /* If basic faces haven't been realized yet, there is no point in
28267 trying to redraw anything. This can happen when we get an expose
28268 event while Emacs is starting, e.g. by moving another window. */
28269 if (FRAME_FACE_CACHE (f) == NULL
28270 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28271 {
28272 TRACE ((stderr, " no faces\n"));
28273 return;
28274 }
28275
28276 if (w == 0 || h == 0)
28277 {
28278 r.x = r.y = 0;
28279 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28280 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28281 }
28282 else
28283 {
28284 r.x = x;
28285 r.y = y;
28286 r.width = w;
28287 r.height = h;
28288 }
28289
28290 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28291 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28292
28293 if (WINDOWP (f->tool_bar_window))
28294 mouse_face_overwritten_p
28295 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28296
28297 #ifdef HAVE_X_WINDOWS
28298 #ifndef MSDOS
28299 #ifndef USE_X_TOOLKIT
28300 if (WINDOWP (f->menu_bar_window))
28301 mouse_face_overwritten_p
28302 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28303 #endif /* not USE_X_TOOLKIT */
28304 #endif
28305 #endif
28306
28307 /* Some window managers support a focus-follows-mouse style with
28308 delayed raising of frames. Imagine a partially obscured frame,
28309 and moving the mouse into partially obscured mouse-face on that
28310 frame. The visible part of the mouse-face will be highlighted,
28311 then the WM raises the obscured frame. With at least one WM, KDE
28312 2.1, Emacs is not getting any event for the raising of the frame
28313 (even tried with SubstructureRedirectMask), only Expose events.
28314 These expose events will draw text normally, i.e. not
28315 highlighted. Which means we must redo the highlight here.
28316 Subsume it under ``we love X''. --gerd 2001-08-15 */
28317 /* Included in Windows version because Windows most likely does not
28318 do the right thing if any third party tool offers
28319 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28320 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28321 {
28322 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28323 if (f == hlinfo->mouse_face_mouse_frame)
28324 {
28325 int mouse_x = hlinfo->mouse_face_mouse_x;
28326 int mouse_y = hlinfo->mouse_face_mouse_y;
28327 clear_mouse_face (hlinfo);
28328 note_mouse_highlight (f, mouse_x, mouse_y);
28329 }
28330 }
28331 }
28332
28333
28334 /* EXPORT:
28335 Determine the intersection of two rectangles R1 and R2. Return
28336 the intersection in *RESULT. Value is non-zero if RESULT is not
28337 empty. */
28338
28339 int
28340 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28341 {
28342 XRectangle *left, *right;
28343 XRectangle *upper, *lower;
28344 int intersection_p = 0;
28345
28346 /* Rearrange so that R1 is the left-most rectangle. */
28347 if (r1->x < r2->x)
28348 left = r1, right = r2;
28349 else
28350 left = r2, right = r1;
28351
28352 /* X0 of the intersection is right.x0, if this is inside R1,
28353 otherwise there is no intersection. */
28354 if (right->x <= left->x + left->width)
28355 {
28356 result->x = right->x;
28357
28358 /* The right end of the intersection is the minimum of
28359 the right ends of left and right. */
28360 result->width = (min (left->x + left->width, right->x + right->width)
28361 - result->x);
28362
28363 /* Same game for Y. */
28364 if (r1->y < r2->y)
28365 upper = r1, lower = r2;
28366 else
28367 upper = r2, lower = r1;
28368
28369 /* The upper end of the intersection is lower.y0, if this is inside
28370 of upper. Otherwise, there is no intersection. */
28371 if (lower->y <= upper->y + upper->height)
28372 {
28373 result->y = lower->y;
28374
28375 /* The lower end of the intersection is the minimum of the lower
28376 ends of upper and lower. */
28377 result->height = (min (lower->y + lower->height,
28378 upper->y + upper->height)
28379 - result->y);
28380 intersection_p = 1;
28381 }
28382 }
28383
28384 return intersection_p;
28385 }
28386
28387 #endif /* HAVE_WINDOW_SYSTEM */
28388
28389 \f
28390 /***********************************************************************
28391 Initialization
28392 ***********************************************************************/
28393
28394 void
28395 syms_of_xdisp (void)
28396 {
28397 Vwith_echo_area_save_vector = Qnil;
28398 staticpro (&Vwith_echo_area_save_vector);
28399
28400 Vmessage_stack = Qnil;
28401 staticpro (&Vmessage_stack);
28402
28403 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28404
28405 message_dolog_marker1 = Fmake_marker ();
28406 staticpro (&message_dolog_marker1);
28407 message_dolog_marker2 = Fmake_marker ();
28408 staticpro (&message_dolog_marker2);
28409 message_dolog_marker3 = Fmake_marker ();
28410 staticpro (&message_dolog_marker3);
28411
28412 #ifdef GLYPH_DEBUG
28413 defsubr (&Sdump_frame_glyph_matrix);
28414 defsubr (&Sdump_glyph_matrix);
28415 defsubr (&Sdump_glyph_row);
28416 defsubr (&Sdump_tool_bar_row);
28417 defsubr (&Strace_redisplay);
28418 defsubr (&Strace_to_stderr);
28419 #endif
28420 #ifdef HAVE_WINDOW_SYSTEM
28421 defsubr (&Stool_bar_lines_needed);
28422 defsubr (&Slookup_image_map);
28423 #endif
28424 defsubr (&Sformat_mode_line);
28425 defsubr (&Sinvisible_p);
28426 defsubr (&Scurrent_bidi_paragraph_direction);
28427
28428 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28429 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28430 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28431 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28432 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28433 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28434 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28435 DEFSYM (Qeval, "eval");
28436 DEFSYM (QCdata, ":data");
28437 DEFSYM (Qdisplay, "display");
28438 DEFSYM (Qspace_width, "space-width");
28439 DEFSYM (Qraise, "raise");
28440 DEFSYM (Qslice, "slice");
28441 DEFSYM (Qspace, "space");
28442 DEFSYM (Qmargin, "margin");
28443 DEFSYM (Qpointer, "pointer");
28444 DEFSYM (Qleft_margin, "left-margin");
28445 DEFSYM (Qright_margin, "right-margin");
28446 DEFSYM (Qcenter, "center");
28447 DEFSYM (Qline_height, "line-height");
28448 DEFSYM (QCalign_to, ":align-to");
28449 DEFSYM (QCrelative_width, ":relative-width");
28450 DEFSYM (QCrelative_height, ":relative-height");
28451 DEFSYM (QCeval, ":eval");
28452 DEFSYM (QCpropertize, ":propertize");
28453 DEFSYM (QCfile, ":file");
28454 DEFSYM (Qfontified, "fontified");
28455 DEFSYM (Qfontification_functions, "fontification-functions");
28456 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28457 DEFSYM (Qescape_glyph, "escape-glyph");
28458 DEFSYM (Qnobreak_space, "nobreak-space");
28459 DEFSYM (Qimage, "image");
28460 DEFSYM (Qtext, "text");
28461 DEFSYM (Qboth, "both");
28462 DEFSYM (Qboth_horiz, "both-horiz");
28463 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28464 DEFSYM (QCmap, ":map");
28465 DEFSYM (QCpointer, ":pointer");
28466 DEFSYM (Qrect, "rect");
28467 DEFSYM (Qcircle, "circle");
28468 DEFSYM (Qpoly, "poly");
28469 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28470 DEFSYM (Qgrow_only, "grow-only");
28471 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28472 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28473 DEFSYM (Qposition, "position");
28474 DEFSYM (Qbuffer_position, "buffer-position");
28475 DEFSYM (Qobject, "object");
28476 DEFSYM (Qbar, "bar");
28477 DEFSYM (Qhbar, "hbar");
28478 DEFSYM (Qbox, "box");
28479 DEFSYM (Qhollow, "hollow");
28480 DEFSYM (Qhand, "hand");
28481 DEFSYM (Qarrow, "arrow");
28482 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28483
28484 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28485 Fcons (intern_c_string ("void-variable"), Qnil)),
28486 Qnil);
28487 staticpro (&list_of_error);
28488
28489 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28490 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28491 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28492 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28493
28494 echo_buffer[0] = echo_buffer[1] = Qnil;
28495 staticpro (&echo_buffer[0]);
28496 staticpro (&echo_buffer[1]);
28497
28498 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28499 staticpro (&echo_area_buffer[0]);
28500 staticpro (&echo_area_buffer[1]);
28501
28502 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28503 staticpro (&Vmessages_buffer_name);
28504
28505 mode_line_proptrans_alist = Qnil;
28506 staticpro (&mode_line_proptrans_alist);
28507 mode_line_string_list = Qnil;
28508 staticpro (&mode_line_string_list);
28509 mode_line_string_face = Qnil;
28510 staticpro (&mode_line_string_face);
28511 mode_line_string_face_prop = Qnil;
28512 staticpro (&mode_line_string_face_prop);
28513 Vmode_line_unwind_vector = Qnil;
28514 staticpro (&Vmode_line_unwind_vector);
28515
28516 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28517
28518 help_echo_string = Qnil;
28519 staticpro (&help_echo_string);
28520 help_echo_object = Qnil;
28521 staticpro (&help_echo_object);
28522 help_echo_window = Qnil;
28523 staticpro (&help_echo_window);
28524 previous_help_echo_string = Qnil;
28525 staticpro (&previous_help_echo_string);
28526 help_echo_pos = -1;
28527
28528 DEFSYM (Qright_to_left, "right-to-left");
28529 DEFSYM (Qleft_to_right, "left-to-right");
28530
28531 #ifdef HAVE_WINDOW_SYSTEM
28532 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28533 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28534 For example, if a block cursor is over a tab, it will be drawn as
28535 wide as that tab on the display. */);
28536 x_stretch_cursor_p = 0;
28537 #endif
28538
28539 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28540 doc: /* Non-nil means highlight trailing whitespace.
28541 The face used for trailing whitespace is `trailing-whitespace'. */);
28542 Vshow_trailing_whitespace = Qnil;
28543
28544 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28545 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28546 If the value is t, Emacs highlights non-ASCII chars which have the
28547 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28548 or `escape-glyph' face respectively.
28549
28550 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28551 U+2011 (non-breaking hyphen) are affected.
28552
28553 Any other non-nil value means to display these characters as a escape
28554 glyph followed by an ordinary space or hyphen.
28555
28556 A value of nil means no special handling of these characters. */);
28557 Vnobreak_char_display = Qt;
28558
28559 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28560 doc: /* The pointer shape to show in void text areas.
28561 A value of nil means to show the text pointer. Other options are `arrow',
28562 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28563 Vvoid_text_area_pointer = Qarrow;
28564
28565 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28566 doc: /* Non-nil means don't actually do any redisplay.
28567 This is used for internal purposes. */);
28568 Vinhibit_redisplay = Qnil;
28569
28570 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28571 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28572 Vglobal_mode_string = Qnil;
28573
28574 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28575 doc: /* Marker for where to display an arrow on top of the buffer text.
28576 This must be the beginning of a line in order to work.
28577 See also `overlay-arrow-string'. */);
28578 Voverlay_arrow_position = Qnil;
28579
28580 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28581 doc: /* String to display as an arrow in non-window frames.
28582 See also `overlay-arrow-position'. */);
28583 Voverlay_arrow_string = make_pure_c_string ("=>");
28584
28585 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28586 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28587 The symbols on this list are examined during redisplay to determine
28588 where to display overlay arrows. */);
28589 Voverlay_arrow_variable_list
28590 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28591
28592 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28593 doc: /* The number of lines to try scrolling a window by when point moves out.
28594 If that fails to bring point back on frame, point is centered instead.
28595 If this is zero, point is always centered after it moves off frame.
28596 If you want scrolling to always be a line at a time, you should set
28597 `scroll-conservatively' to a large value rather than set this to 1. */);
28598
28599 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28600 doc: /* Scroll up to this many lines, to bring point back on screen.
28601 If point moves off-screen, redisplay will scroll by up to
28602 `scroll-conservatively' lines in order to bring point just barely
28603 onto the screen again. If that cannot be done, then redisplay
28604 recenters point as usual.
28605
28606 If the value is greater than 100, redisplay will never recenter point,
28607 but will always scroll just enough text to bring point into view, even
28608 if you move far away.
28609
28610 A value of zero means always recenter point if it moves off screen. */);
28611 scroll_conservatively = 0;
28612
28613 DEFVAR_INT ("scroll-margin", scroll_margin,
28614 doc: /* Number of lines of margin at the top and bottom of a window.
28615 Recenter the window whenever point gets within this many lines
28616 of the top or bottom of the window. */);
28617 scroll_margin = 0;
28618
28619 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28620 doc: /* Pixels per inch value for non-window system displays.
28621 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28622 Vdisplay_pixels_per_inch = make_float (72.0);
28623
28624 #ifdef GLYPH_DEBUG
28625 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28626 #endif
28627
28628 DEFVAR_LISP ("truncate-partial-width-windows",
28629 Vtruncate_partial_width_windows,
28630 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28631 For an integer value, truncate lines in each window narrower than the
28632 full frame width, provided the window width is less than that integer;
28633 otherwise, respect the value of `truncate-lines'.
28634
28635 For any other non-nil value, truncate lines in all windows that do
28636 not span the full frame width.
28637
28638 A value of nil means to respect the value of `truncate-lines'.
28639
28640 If `word-wrap' is enabled, you might want to reduce this. */);
28641 Vtruncate_partial_width_windows = make_number (50);
28642
28643 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28644 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28645 Any other value means to use the appropriate face, `mode-line',
28646 `header-line', or `menu' respectively. */);
28647 mode_line_inverse_video = 1;
28648
28649 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28650 doc: /* Maximum buffer size for which line number should be displayed.
28651 If the buffer is bigger than this, the line number does not appear
28652 in the mode line. A value of nil means no limit. */);
28653 Vline_number_display_limit = Qnil;
28654
28655 DEFVAR_INT ("line-number-display-limit-width",
28656 line_number_display_limit_width,
28657 doc: /* Maximum line width (in characters) for line number display.
28658 If the average length of the lines near point is bigger than this, then the
28659 line number may be omitted from the mode line. */);
28660 line_number_display_limit_width = 200;
28661
28662 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28663 doc: /* Non-nil means highlight region even in nonselected windows. */);
28664 highlight_nonselected_windows = 0;
28665
28666 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28667 doc: /* Non-nil if more than one frame is visible on this display.
28668 Minibuffer-only frames don't count, but iconified frames do.
28669 This variable is not guaranteed to be accurate except while processing
28670 `frame-title-format' and `icon-title-format'. */);
28671
28672 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28673 doc: /* Template for displaying the title bar of visible frames.
28674 \(Assuming the window manager supports this feature.)
28675
28676 This variable has the same structure as `mode-line-format', except that
28677 the %c and %l constructs are ignored. It is used only on frames for
28678 which no explicit name has been set \(see `modify-frame-parameters'). */);
28679
28680 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28681 doc: /* Template for displaying the title bar of an iconified frame.
28682 \(Assuming the window manager supports this feature.)
28683 This variable has the same structure as `mode-line-format' (which see),
28684 and is used only on frames for which no explicit name has been set
28685 \(see `modify-frame-parameters'). */);
28686 Vicon_title_format
28687 = Vframe_title_format
28688 = pure_cons (intern_c_string ("multiple-frames"),
28689 pure_cons (make_pure_c_string ("%b"),
28690 pure_cons (pure_cons (empty_unibyte_string,
28691 pure_cons (intern_c_string ("invocation-name"),
28692 pure_cons (make_pure_c_string ("@"),
28693 pure_cons (intern_c_string ("system-name"),
28694 Qnil)))),
28695 Qnil)));
28696
28697 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28698 doc: /* Maximum number of lines to keep in the message log buffer.
28699 If nil, disable message logging. If t, log messages but don't truncate
28700 the buffer when it becomes large. */);
28701 Vmessage_log_max = make_number (100);
28702
28703 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28704 doc: /* Functions called before redisplay, if window sizes have changed.
28705 The value should be a list of functions that take one argument.
28706 Just before redisplay, for each frame, if any of its windows have changed
28707 size since the last redisplay, or have been split or deleted,
28708 all the functions in the list are called, with the frame as argument. */);
28709 Vwindow_size_change_functions = Qnil;
28710
28711 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28712 doc: /* List of functions to call before redisplaying a window with scrolling.
28713 Each function is called with two arguments, the window and its new
28714 display-start position. Note that these functions are also called by
28715 `set-window-buffer'. Also note that the value of `window-end' is not
28716 valid when these functions are called.
28717
28718 Warning: Do not use this feature to alter the way the window
28719 is scrolled. It is not designed for that, and such use probably won't
28720 work. */);
28721 Vwindow_scroll_functions = Qnil;
28722
28723 DEFVAR_LISP ("window-text-change-functions",
28724 Vwindow_text_change_functions,
28725 doc: /* Functions to call in redisplay when text in the window might change. */);
28726 Vwindow_text_change_functions = Qnil;
28727
28728 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28729 doc: /* Functions called when redisplay of a window reaches the end trigger.
28730 Each function is called with two arguments, the window and the end trigger value.
28731 See `set-window-redisplay-end-trigger'. */);
28732 Vredisplay_end_trigger_functions = Qnil;
28733
28734 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28735 doc: /* Non-nil means autoselect window with mouse pointer.
28736 If nil, do not autoselect windows.
28737 A positive number means delay autoselection by that many seconds: a
28738 window is autoselected only after the mouse has remained in that
28739 window for the duration of the delay.
28740 A negative number has a similar effect, but causes windows to be
28741 autoselected only after the mouse has stopped moving. \(Because of
28742 the way Emacs compares mouse events, you will occasionally wait twice
28743 that time before the window gets selected.\)
28744 Any other value means to autoselect window instantaneously when the
28745 mouse pointer enters it.
28746
28747 Autoselection selects the minibuffer only if it is active, and never
28748 unselects the minibuffer if it is active.
28749
28750 When customizing this variable make sure that the actual value of
28751 `focus-follows-mouse' matches the behavior of your window manager. */);
28752 Vmouse_autoselect_window = Qnil;
28753
28754 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28755 doc: /* Non-nil means automatically resize tool-bars.
28756 This dynamically changes the tool-bar's height to the minimum height
28757 that is needed to make all tool-bar items visible.
28758 If value is `grow-only', the tool-bar's height is only increased
28759 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28760 Vauto_resize_tool_bars = Qt;
28761
28762 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28763 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28764 auto_raise_tool_bar_buttons_p = 1;
28765
28766 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28767 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28768 make_cursor_line_fully_visible_p = 1;
28769
28770 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28771 doc: /* Border below tool-bar in pixels.
28772 If an integer, use it as the height of the border.
28773 If it is one of `internal-border-width' or `border-width', use the
28774 value of the corresponding frame parameter.
28775 Otherwise, no border is added below the tool-bar. */);
28776 Vtool_bar_border = Qinternal_border_width;
28777
28778 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28779 doc: /* Margin around tool-bar buttons in pixels.
28780 If an integer, use that for both horizontal and vertical margins.
28781 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28782 HORZ specifying the horizontal margin, and VERT specifying the
28783 vertical margin. */);
28784 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28785
28786 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28787 doc: /* Relief thickness of tool-bar buttons. */);
28788 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28789
28790 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28791 doc: /* Tool bar style to use.
28792 It can be one of
28793 image - show images only
28794 text - show text only
28795 both - show both, text below image
28796 both-horiz - show text to the right of the image
28797 text-image-horiz - show text to the left of the image
28798 any other - use system default or image if no system default.
28799
28800 This variable only affects the GTK+ toolkit version of Emacs. */);
28801 Vtool_bar_style = Qnil;
28802
28803 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28804 doc: /* Maximum number of characters a label can have to be shown.
28805 The tool bar style must also show labels for this to have any effect, see
28806 `tool-bar-style'. */);
28807 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28808
28809 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28810 doc: /* List of functions to call to fontify regions of text.
28811 Each function is called with one argument POS. Functions must
28812 fontify a region starting at POS in the current buffer, and give
28813 fontified regions the property `fontified'. */);
28814 Vfontification_functions = Qnil;
28815 Fmake_variable_buffer_local (Qfontification_functions);
28816
28817 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28818 unibyte_display_via_language_environment,
28819 doc: /* Non-nil means display unibyte text according to language environment.
28820 Specifically, this means that raw bytes in the range 160-255 decimal
28821 are displayed by converting them to the equivalent multibyte characters
28822 according to the current language environment. As a result, they are
28823 displayed according to the current fontset.
28824
28825 Note that this variable affects only how these bytes are displayed,
28826 but does not change the fact they are interpreted as raw bytes. */);
28827 unibyte_display_via_language_environment = 0;
28828
28829 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28830 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28831 If a float, it specifies a fraction of the mini-window frame's height.
28832 If an integer, it specifies a number of lines. */);
28833 Vmax_mini_window_height = make_float (0.25);
28834
28835 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28836 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28837 A value of nil means don't automatically resize mini-windows.
28838 A value of t means resize them to fit the text displayed in them.
28839 A value of `grow-only', the default, means let mini-windows grow only;
28840 they return to their normal size when the minibuffer is closed, or the
28841 echo area becomes empty. */);
28842 Vresize_mini_windows = Qgrow_only;
28843
28844 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28845 doc: /* Alist specifying how to blink the cursor off.
28846 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28847 `cursor-type' frame-parameter or variable equals ON-STATE,
28848 comparing using `equal', Emacs uses OFF-STATE to specify
28849 how to blink it off. ON-STATE and OFF-STATE are values for
28850 the `cursor-type' frame parameter.
28851
28852 If a frame's ON-STATE has no entry in this list,
28853 the frame's other specifications determine how to blink the cursor off. */);
28854 Vblink_cursor_alist = Qnil;
28855
28856 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28857 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28858 If non-nil, windows are automatically scrolled horizontally to make
28859 point visible. */);
28860 automatic_hscrolling_p = 1;
28861 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28862
28863 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28864 doc: /* How many columns away from the window edge point is allowed to get
28865 before automatic hscrolling will horizontally scroll the window. */);
28866 hscroll_margin = 5;
28867
28868 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28869 doc: /* How many columns to scroll the window when point gets too close to the edge.
28870 When point is less than `hscroll-margin' columns from the window
28871 edge, automatic hscrolling will scroll the window by the amount of columns
28872 determined by this variable. If its value is a positive integer, scroll that
28873 many columns. If it's a positive floating-point number, it specifies the
28874 fraction of the window's width to scroll. If it's nil or zero, point will be
28875 centered horizontally after the scroll. Any other value, including negative
28876 numbers, are treated as if the value were zero.
28877
28878 Automatic hscrolling always moves point outside the scroll margin, so if
28879 point was more than scroll step columns inside the margin, the window will
28880 scroll more than the value given by the scroll step.
28881
28882 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28883 and `scroll-right' overrides this variable's effect. */);
28884 Vhscroll_step = make_number (0);
28885
28886 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28887 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28888 Bind this around calls to `message' to let it take effect. */);
28889 message_truncate_lines = 0;
28890
28891 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28892 doc: /* Normal hook run to update the menu bar definitions.
28893 Redisplay runs this hook before it redisplays the menu bar.
28894 This is used to update submenus such as Buffers,
28895 whose contents depend on various data. */);
28896 Vmenu_bar_update_hook = Qnil;
28897
28898 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28899 doc: /* Frame for which we are updating a menu.
28900 The enable predicate for a menu binding should check this variable. */);
28901 Vmenu_updating_frame = Qnil;
28902
28903 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28904 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28905 inhibit_menubar_update = 0;
28906
28907 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28908 doc: /* Prefix prepended to all continuation lines at display time.
28909 The value may be a string, an image, or a stretch-glyph; it is
28910 interpreted in the same way as the value of a `display' text property.
28911
28912 This variable is overridden by any `wrap-prefix' text or overlay
28913 property.
28914
28915 To add a prefix to non-continuation lines, use `line-prefix'. */);
28916 Vwrap_prefix = Qnil;
28917 DEFSYM (Qwrap_prefix, "wrap-prefix");
28918 Fmake_variable_buffer_local (Qwrap_prefix);
28919
28920 DEFVAR_LISP ("line-prefix", Vline_prefix,
28921 doc: /* Prefix prepended to all non-continuation lines at display time.
28922 The value may be a string, an image, or a stretch-glyph; it is
28923 interpreted in the same way as the value of a `display' text property.
28924
28925 This variable is overridden by any `line-prefix' text or overlay
28926 property.
28927
28928 To add a prefix to continuation lines, use `wrap-prefix'. */);
28929 Vline_prefix = Qnil;
28930 DEFSYM (Qline_prefix, "line-prefix");
28931 Fmake_variable_buffer_local (Qline_prefix);
28932
28933 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28934 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28935 inhibit_eval_during_redisplay = 0;
28936
28937 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28938 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28939 inhibit_free_realized_faces = 0;
28940
28941 #ifdef GLYPH_DEBUG
28942 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28943 doc: /* Inhibit try_window_id display optimization. */);
28944 inhibit_try_window_id = 0;
28945
28946 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28947 doc: /* Inhibit try_window_reusing display optimization. */);
28948 inhibit_try_window_reusing = 0;
28949
28950 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28951 doc: /* Inhibit try_cursor_movement display optimization. */);
28952 inhibit_try_cursor_movement = 0;
28953 #endif /* GLYPH_DEBUG */
28954
28955 DEFVAR_INT ("overline-margin", overline_margin,
28956 doc: /* Space between overline and text, in pixels.
28957 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28958 margin to the character height. */);
28959 overline_margin = 2;
28960
28961 DEFVAR_INT ("underline-minimum-offset",
28962 underline_minimum_offset,
28963 doc: /* Minimum distance between baseline and underline.
28964 This can improve legibility of underlined text at small font sizes,
28965 particularly when using variable `x-use-underline-position-properties'
28966 with fonts that specify an UNDERLINE_POSITION relatively close to the
28967 baseline. The default value is 1. */);
28968 underline_minimum_offset = 1;
28969
28970 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28971 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28972 This feature only works when on a window system that can change
28973 cursor shapes. */);
28974 display_hourglass_p = 1;
28975
28976 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28977 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28978 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28979
28980 hourglass_atimer = NULL;
28981 hourglass_shown_p = 0;
28982
28983 DEFSYM (Qglyphless_char, "glyphless-char");
28984 DEFSYM (Qhex_code, "hex-code");
28985 DEFSYM (Qempty_box, "empty-box");
28986 DEFSYM (Qthin_space, "thin-space");
28987 DEFSYM (Qzero_width, "zero-width");
28988
28989 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28990 /* Intern this now in case it isn't already done.
28991 Setting this variable twice is harmless.
28992 But don't staticpro it here--that is done in alloc.c. */
28993 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28994 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28995
28996 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28997 doc: /* Char-table defining glyphless characters.
28998 Each element, if non-nil, should be one of the following:
28999 an ASCII acronym string: display this string in a box
29000 `hex-code': display the hexadecimal code of a character in a box
29001 `empty-box': display as an empty box
29002 `thin-space': display as 1-pixel width space
29003 `zero-width': don't display
29004 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29005 display method for graphical terminals and text terminals respectively.
29006 GRAPHICAL and TEXT should each have one of the values listed above.
29007
29008 The char-table has one extra slot to control the display of a character for
29009 which no font is found. This slot only takes effect on graphical terminals.
29010 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29011 `thin-space'. The default is `empty-box'. */);
29012 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29013 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29014 Qempty_box);
29015 }
29016
29017
29018 /* Initialize this module when Emacs starts. */
29019
29020 void
29021 init_xdisp (void)
29022 {
29023 current_header_line_height = current_mode_line_height = -1;
29024
29025 CHARPOS (this_line_start_pos) = 0;
29026
29027 if (!noninteractive)
29028 {
29029 struct window *m = XWINDOW (minibuf_window);
29030 Lisp_Object frame = m->frame;
29031 struct frame *f = XFRAME (frame);
29032 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29033 struct window *r = XWINDOW (root);
29034 int i;
29035
29036 echo_area_window = minibuf_window;
29037
29038 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29039 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29040 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29041 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29042 XSETFASTINT (m->total_lines, 1);
29043 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29044
29045 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29046 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29047 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29048
29049 /* The default ellipsis glyphs `...'. */
29050 for (i = 0; i < 3; ++i)
29051 default_invis_vector[i] = make_number ('.');
29052 }
29053
29054 {
29055 /* Allocate the buffer for frame titles.
29056 Also used for `format-mode-line'. */
29057 int size = 100;
29058 mode_line_noprop_buf = xmalloc (size);
29059 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29060 mode_line_noprop_ptr = mode_line_noprop_buf;
29061 mode_line_target = MODE_LINE_DISPLAY;
29062 }
29063
29064 help_echo_showing_p = 0;
29065 }
29066
29067 /* Since w32 does not support atimers, it defines its own implementation of
29068 the following three functions in w32fns.c. */
29069 #ifndef WINDOWSNT
29070
29071 /* Platform-independent portion of hourglass implementation. */
29072
29073 /* Cancel a currently active hourglass timer, and start a new one. */
29074 void
29075 start_hourglass (void)
29076 {
29077 #if defined (HAVE_WINDOW_SYSTEM)
29078 EMACS_TIME delay;
29079
29080 cancel_hourglass ();
29081
29082 if (INTEGERP (Vhourglass_delay)
29083 && XINT (Vhourglass_delay) > 0)
29084 EMACS_SET_SECS_NSECS (delay,
29085 min (XINT (Vhourglass_delay), TYPE_MAXIMUM (time_t)),
29086 0);
29087 else if (FLOATP (Vhourglass_delay)
29088 && XFLOAT_DATA (Vhourglass_delay) > 0)
29089 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29090 else
29091 EMACS_SET_SECS_NSECS (delay, DEFAULT_HOURGLASS_DELAY, 0);
29092
29093 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29094 show_hourglass, NULL);
29095 #endif
29096 }
29097
29098
29099 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29100 shown. */
29101 void
29102 cancel_hourglass (void)
29103 {
29104 #if defined (HAVE_WINDOW_SYSTEM)
29105 if (hourglass_atimer)
29106 {
29107 cancel_atimer (hourglass_atimer);
29108 hourglass_atimer = NULL;
29109 }
29110
29111 if (hourglass_shown_p)
29112 hide_hourglass ();
29113 #endif
29114 }
29115 #endif /* ! WINDOWSNT */