Drop FGET and revert read access to Lisp_Objects slots of struct frame.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT, or the underlying buffer
387 or string character, is a space or a TAB character. This is used
388 to determine where word wrapping can occur. */
389
390 #define IT_DISPLAYING_WHITESPACE(it) \
391 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
392 || ((STRINGP (it->string) \
393 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
394 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
395 || (it->s \
396 && (it->s[IT_BYTEPOS (*it)] == ' ' \
397 || it->s[IT_BYTEPOS (*it)] == '\t')) \
398 || (IT_BYTEPOS (*it) < ZV_BYTE \
399 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
400 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
401
402 /* Name of the face used to highlight trailing whitespace. */
403
404 static Lisp_Object Qtrailing_whitespace;
405
406 /* Name and number of the face used to highlight escape glyphs. */
407
408 static Lisp_Object Qescape_glyph;
409
410 /* Name and number of the face used to highlight non-breaking spaces. */
411
412 static Lisp_Object Qnobreak_space;
413
414 /* The symbol `image' which is the car of the lists used to represent
415 images in Lisp. Also a tool bar style. */
416
417 Lisp_Object Qimage;
418
419 /* The image map types. */
420 Lisp_Object QCmap;
421 static Lisp_Object QCpointer;
422 static Lisp_Object Qrect, Qcircle, Qpoly;
423
424 /* Tool bar styles */
425 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
426
427 /* Non-zero means print newline to stdout before next mini-buffer
428 message. */
429
430 int noninteractive_need_newline;
431
432 /* Non-zero means print newline to message log before next message. */
433
434 static int message_log_need_newline;
435
436 /* Three markers that message_dolog uses.
437 It could allocate them itself, but that causes trouble
438 in handling memory-full errors. */
439 static Lisp_Object message_dolog_marker1;
440 static Lisp_Object message_dolog_marker2;
441 static Lisp_Object message_dolog_marker3;
442 \f
443 /* The buffer position of the first character appearing entirely or
444 partially on the line of the selected window which contains the
445 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
446 redisplay optimization in redisplay_internal. */
447
448 static struct text_pos this_line_start_pos;
449
450 /* Number of characters past the end of the line above, including the
451 terminating newline. */
452
453 static struct text_pos this_line_end_pos;
454
455 /* The vertical positions and the height of this line. */
456
457 static int this_line_vpos;
458 static int this_line_y;
459 static int this_line_pixel_height;
460
461 /* X position at which this display line starts. Usually zero;
462 negative if first character is partially visible. */
463
464 static int this_line_start_x;
465
466 /* The smallest character position seen by move_it_* functions as they
467 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
468 hscrolled lines, see display_line. */
469
470 static struct text_pos this_line_min_pos;
471
472 /* Buffer that this_line_.* variables are referring to. */
473
474 static struct buffer *this_line_buffer;
475
476
477 /* Values of those variables at last redisplay are stored as
478 properties on `overlay-arrow-position' symbol. However, if
479 Voverlay_arrow_position is a marker, last-arrow-position is its
480 numerical position. */
481
482 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
483
484 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
485 properties on a symbol in overlay-arrow-variable-list. */
486
487 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
488
489 Lisp_Object Qmenu_bar_update_hook;
490
491 /* Nonzero if an overlay arrow has been displayed in this window. */
492
493 static int overlay_arrow_seen;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
508
509 Lisp_Object echo_area_window;
510
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
515
516 static Lisp_Object Vmessage_stack;
517
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
520
521 static int message_enable_multibyte;
522
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
524
525 int update_mode_lines;
526
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
529
530 int windows_or_buffers_changed;
531
532 /* Nonzero means a frame's cursor type has been changed. */
533
534 int cursor_type_changed;
535
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
538
539 static int line_number_displayed;
540
541 /* The name of the *Messages* buffer, a string. */
542
543 static Lisp_Object Vmessages_buffer_name;
544
545 /* Current, index 0, and last displayed echo area message. Either
546 buffers from echo_buffers, or nil to indicate no message. */
547
548 Lisp_Object echo_area_buffer[2];
549
550 /* The buffers referenced from echo_area_buffer. */
551
552 static Lisp_Object echo_buffer[2];
553
554 /* A vector saved used in with_area_buffer to reduce consing. */
555
556 static Lisp_Object Vwith_echo_area_save_vector;
557
558 /* Non-zero means display_echo_area should display the last echo area
559 message again. Set by redisplay_preserve_echo_area. */
560
561 static int display_last_displayed_message_p;
562
563 /* Nonzero if echo area is being used by print; zero if being used by
564 message. */
565
566 static int message_buf_print;
567
568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
569
570 static Lisp_Object Qinhibit_menubar_update;
571 static Lisp_Object Qmessage_truncate_lines;
572
573 /* Set to 1 in clear_message to make redisplay_internal aware
574 of an emptied echo area. */
575
576 static int message_cleared_p;
577
578 /* A scratch glyph row with contents used for generating truncation
579 glyphs. Also used in direct_output_for_insert. */
580
581 #define MAX_SCRATCH_GLYPHS 100
582 static struct glyph_row scratch_glyph_row;
583 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
584
585 /* Ascent and height of the last line processed by move_it_to. */
586
587 static int last_max_ascent, last_height;
588
589 /* Non-zero if there's a help-echo in the echo area. */
590
591 int help_echo_showing_p;
592
593 /* If >= 0, computed, exact values of mode-line and header-line height
594 to use in the macros CURRENT_MODE_LINE_HEIGHT and
595 CURRENT_HEADER_LINE_HEIGHT. */
596
597 int current_mode_line_height, current_header_line_height;
598
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
604
605 #define TEXT_PROP_DISTANCE_LIMIT 100
606
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
621
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
629
630 #ifdef GLYPH_DEBUG
631
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG defined. */
634
635 int trace_redisplay_p;
636
637 #endif /* GLYPH_DEBUG */
638
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
642
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
647
648 static Lisp_Object Qauto_hscroll_mode;
649
650 /* Buffer being redisplayed -- for redisplay_window_error. */
651
652 static struct buffer *displayed_buffer;
653
654 /* Value returned from text property handlers (see below). */
655
656 enum prop_handled
657 {
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
662 };
663
664 /* A description of text properties that redisplay is interested
665 in. */
666
667 struct props
668 {
669 /* The name of the property. */
670 Lisp_Object *name;
671
672 /* A unique index for the property. */
673 enum prop_idx idx;
674
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) (struct it *it);
678 };
679
680 static enum prop_handled handle_face_prop (struct it *);
681 static enum prop_handled handle_invisible_prop (struct it *);
682 static enum prop_handled handle_display_prop (struct it *);
683 static enum prop_handled handle_composition_prop (struct it *);
684 static enum prop_handled handle_overlay_change (struct it *);
685 static enum prop_handled handle_fontified_prop (struct it *);
686
687 /* Properties handled by iterators. */
688
689 static struct props it_props[] =
690 {
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
698 {NULL, 0, NULL}
699 };
700
701 /* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
703
704 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
705
706 /* Enumeration returned by some move_it_.* functions internally. */
707
708 enum move_it_result
709 {
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
712
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
715
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
718
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
722
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
726
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
729 };
730
731 /* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
735
736 #define CLEAR_FACE_CACHE_COUNT 500
737 static int clear_face_cache_count;
738
739 /* Similarly for the image cache. */
740
741 #ifdef HAVE_WINDOW_SYSTEM
742 #define CLEAR_IMAGE_CACHE_COUNT 101
743 static int clear_image_cache_count;
744
745 /* Null glyph slice */
746 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
747 #endif
748
749 /* Non-zero while redisplay_internal is in progress. */
750
751 int redisplaying_p;
752
753 static Lisp_Object Qinhibit_free_realized_faces;
754 static Lisp_Object Qmode_line_default_help_echo;
755
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
758
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
763
764 /* Temporary variable for XTread_socket. */
765
766 Lisp_Object previous_help_echo_string;
767
768 /* Platform-independent portion of hourglass implementation. */
769
770 /* Non-zero means an hourglass cursor is currently shown. */
771 int hourglass_shown_p;
772
773 /* If non-null, an asynchronous timer that, when it expires, displays
774 an hourglass cursor on all frames. */
775 struct atimer *hourglass_atimer;
776
777 /* Name of the face used to display glyphless characters. */
778 Lisp_Object Qglyphless_char;
779
780 /* Symbol for the purpose of Vglyphless_char_display. */
781 static Lisp_Object Qglyphless_char_display;
782
783 /* Method symbols for Vglyphless_char_display. */
784 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
785
786 /* Default pixel width of `thin-space' display method. */
787 #define THIN_SPACE_WIDTH 1
788
789 /* Default number of seconds to wait before displaying an hourglass
790 cursor. */
791 #define DEFAULT_HOURGLASS_DELAY 1
792
793 \f
794 /* Function prototypes. */
795
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, int);
798 static void mark_window_display_accurate_1 (struct window *, int);
799 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
800 static int display_prop_string_p (Lisp_Object, Lisp_Object);
801 static int cursor_row_p (struct glyph_row *);
802 static int redisplay_mode_lines (Lisp_Object, int);
803 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
804
805 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
806
807 static void handle_line_prefix (struct it *);
808
809 static void pint2str (char *, int, ptrdiff_t);
810 static void pint2hrstr (char *, int, ptrdiff_t);
811 static struct text_pos run_window_scroll_functions (Lisp_Object,
812 struct text_pos);
813 static void reconsider_clip_changes (struct window *, struct buffer *);
814 static int text_outside_line_unchanged_p (struct window *,
815 ptrdiff_t, ptrdiff_t);
816 static void store_mode_line_noprop_char (char);
817 static int store_mode_line_noprop (const char *, int, int);
818 static void handle_stop (struct it *);
819 static void handle_stop_backwards (struct it *, ptrdiff_t);
820 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
821 static void ensure_echo_area_buffers (void);
822 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
823 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
824 static int with_echo_area_buffer (struct window *, int,
825 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
826 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
827 static void clear_garbaged_frames (void);
828 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
829 static void pop_message (void);
830 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
831 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
832 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
833 static int display_echo_area (struct window *);
834 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
835 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
836 static Lisp_Object unwind_redisplay (Lisp_Object);
837 static int string_char_and_length (const unsigned char *, int *);
838 static struct text_pos display_prop_end (struct it *, Lisp_Object,
839 struct text_pos);
840 static int compute_window_start_on_continuation_line (struct window *);
841 static Lisp_Object safe_eval_handler (Lisp_Object);
842 static void insert_left_trunc_glyphs (struct it *);
843 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
844 Lisp_Object);
845 static void extend_face_to_end_of_line (struct it *);
846 static int append_space_for_newline (struct it *, int);
847 static int cursor_row_fully_visible_p (struct window *, int, int);
848 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
849 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
850 static int trailing_whitespace_p (ptrdiff_t);
851 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
852 static void push_it (struct it *, struct text_pos *);
853 static void iterate_out_of_display_property (struct it *);
854 static void pop_it (struct it *);
855 static void sync_frame_with_window_matrix_rows (struct window *);
856 static void select_frame_for_redisplay (Lisp_Object);
857 static void redisplay_internal (void);
858 static int echo_area_display (int);
859 static void redisplay_windows (Lisp_Object);
860 static void redisplay_window (Lisp_Object, int);
861 static Lisp_Object redisplay_window_error (Lisp_Object);
862 static Lisp_Object redisplay_window_0 (Lisp_Object);
863 static Lisp_Object redisplay_window_1 (Lisp_Object);
864 static int set_cursor_from_row (struct window *, struct glyph_row *,
865 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
866 int, int);
867 static int update_menu_bar (struct frame *, int, int);
868 static int try_window_reusing_current_matrix (struct window *);
869 static int try_window_id (struct window *);
870 static int display_line (struct it *);
871 static int display_mode_lines (struct window *);
872 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
873 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
874 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
875 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
876 static void display_menu_bar (struct window *);
877 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
878 ptrdiff_t *);
879 static int display_string (const char *, Lisp_Object, Lisp_Object,
880 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
881 static void compute_line_metrics (struct it *);
882 static void run_redisplay_end_trigger_hook (struct it *);
883 static int get_overlay_strings (struct it *, ptrdiff_t);
884 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
885 static void next_overlay_string (struct it *);
886 static void reseat (struct it *, struct text_pos, int);
887 static void reseat_1 (struct it *, struct text_pos, int);
888 static void back_to_previous_visible_line_start (struct it *);
889 void reseat_at_previous_visible_line_start (struct it *);
890 static void reseat_at_next_visible_line_start (struct it *, int);
891 static int next_element_from_ellipsis (struct it *);
892 static int next_element_from_display_vector (struct it *);
893 static int next_element_from_string (struct it *);
894 static int next_element_from_c_string (struct it *);
895 static int next_element_from_buffer (struct it *);
896 static int next_element_from_composition (struct it *);
897 static int next_element_from_image (struct it *);
898 static int next_element_from_stretch (struct it *);
899 static void load_overlay_strings (struct it *, ptrdiff_t);
900 static int init_from_display_pos (struct it *, struct window *,
901 struct display_pos *);
902 static void reseat_to_string (struct it *, const char *,
903 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
904 static int get_next_display_element (struct it *);
905 static enum move_it_result
906 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
907 enum move_operation_enum);
908 void move_it_vertically_backward (struct it *, int);
909 static void init_to_row_start (struct it *, struct window *,
910 struct glyph_row *);
911 static int init_to_row_end (struct it *, struct window *,
912 struct glyph_row *);
913 static void back_to_previous_line_start (struct it *);
914 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
915 static struct text_pos string_pos_nchars_ahead (struct text_pos,
916 Lisp_Object, ptrdiff_t);
917 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
918 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
919 static ptrdiff_t number_of_chars (const char *, int);
920 static void compute_stop_pos (struct it *);
921 static void compute_string_pos (struct text_pos *, struct text_pos,
922 Lisp_Object);
923 static int face_before_or_after_it_pos (struct it *, int);
924 static ptrdiff_t next_overlay_change (ptrdiff_t);
925 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
926 Lisp_Object, struct text_pos *, ptrdiff_t, int);
927 static int handle_single_display_spec (struct it *, Lisp_Object,
928 Lisp_Object, Lisp_Object,
929 struct text_pos *, ptrdiff_t, int, int);
930 static int underlying_face_id (struct it *);
931 static int in_ellipses_for_invisible_text_p (struct display_pos *,
932 struct window *);
933
934 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
935 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
936
937 #ifdef HAVE_WINDOW_SYSTEM
938
939 static void x_consider_frame_title (Lisp_Object);
940 static int tool_bar_lines_needed (struct frame *, int *);
941 static void update_tool_bar (struct frame *, int);
942 static void build_desired_tool_bar_string (struct frame *f);
943 static int redisplay_tool_bar (struct frame *);
944 static void display_tool_bar_line (struct it *, int);
945 static void notice_overwritten_cursor (struct window *,
946 enum glyph_row_area,
947 int, int, int, int);
948 static void append_stretch_glyph (struct it *, Lisp_Object,
949 int, int, int);
950
951
952 #endif /* HAVE_WINDOW_SYSTEM */
953
954 static void produce_special_glyphs (struct it *, enum display_element_type);
955 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
956 static int coords_in_mouse_face_p (struct window *, int, int);
957
958
959 \f
960 /***********************************************************************
961 Window display dimensions
962 ***********************************************************************/
963
964 /* Return the bottom boundary y-position for text lines in window W.
965 This is the first y position at which a line cannot start.
966 It is relative to the top of the window.
967
968 This is the height of W minus the height of a mode line, if any. */
969
970 int
971 window_text_bottom_y (struct window *w)
972 {
973 int height = WINDOW_TOTAL_HEIGHT (w);
974
975 if (WINDOW_WANTS_MODELINE_P (w))
976 height -= CURRENT_MODE_LINE_HEIGHT (w);
977 return height;
978 }
979
980 /* Return the pixel width of display area AREA of window W. AREA < 0
981 means return the total width of W, not including fringes to
982 the left and right of the window. */
983
984 int
985 window_box_width (struct window *w, int area)
986 {
987 int cols = XFASTINT (WGET (w, total_cols));
988 int pixels = 0;
989
990 if (!w->pseudo_window_p)
991 {
992 cols -= WINDOW_SCROLL_BAR_COLS (w);
993
994 if (area == TEXT_AREA)
995 {
996 if (INTEGERP (WGET (w, left_margin_cols)))
997 cols -= XFASTINT (WGET (w, left_margin_cols));
998 if (INTEGERP (WGET (w, right_margin_cols)))
999 cols -= XFASTINT (WGET (w, right_margin_cols));
1000 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1001 }
1002 else if (area == LEFT_MARGIN_AREA)
1003 {
1004 cols = (INTEGERP (WGET (w, left_margin_cols))
1005 ? XFASTINT (WGET (w, left_margin_cols)) : 0);
1006 pixels = 0;
1007 }
1008 else if (area == RIGHT_MARGIN_AREA)
1009 {
1010 cols = (INTEGERP (WGET (w, right_margin_cols))
1011 ? XFASTINT (WGET (w, right_margin_cols)) : 0);
1012 pixels = 0;
1013 }
1014 }
1015
1016 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1017 }
1018
1019
1020 /* Return the pixel height of the display area of window W, not
1021 including mode lines of W, if any. */
1022
1023 int
1024 window_box_height (struct window *w)
1025 {
1026 struct frame *f = XFRAME (WGET (w, frame));
1027 int height = WINDOW_TOTAL_HEIGHT (w);
1028
1029 eassert (height >= 0);
1030
1031 /* Note: the code below that determines the mode-line/header-line
1032 height is essentially the same as that contained in the macro
1033 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1034 the appropriate glyph row has its `mode_line_p' flag set,
1035 and if it doesn't, uses estimate_mode_line_height instead. */
1036
1037 if (WINDOW_WANTS_MODELINE_P (w))
1038 {
1039 struct glyph_row *ml_row
1040 = (w->current_matrix && w->current_matrix->rows
1041 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1042 : 0);
1043 if (ml_row && ml_row->mode_line_p)
1044 height -= ml_row->height;
1045 else
1046 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1047 }
1048
1049 if (WINDOW_WANTS_HEADER_LINE_P (w))
1050 {
1051 struct glyph_row *hl_row
1052 = (w->current_matrix && w->current_matrix->rows
1053 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1054 : 0);
1055 if (hl_row && hl_row->mode_line_p)
1056 height -= hl_row->height;
1057 else
1058 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1059 }
1060
1061 /* With a very small font and a mode-line that's taller than
1062 default, we might end up with a negative height. */
1063 return max (0, height);
1064 }
1065
1066 /* Return the window-relative coordinate of the left edge of display
1067 area AREA of window W. AREA < 0 means return the left edge of the
1068 whole window, to the right of the left fringe of W. */
1069
1070 int
1071 window_box_left_offset (struct window *w, int area)
1072 {
1073 int x;
1074
1075 if (w->pseudo_window_p)
1076 return 0;
1077
1078 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1079
1080 if (area == TEXT_AREA)
1081 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1082 + window_box_width (w, LEFT_MARGIN_AREA));
1083 else if (area == RIGHT_MARGIN_AREA)
1084 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1085 + window_box_width (w, LEFT_MARGIN_AREA)
1086 + window_box_width (w, TEXT_AREA)
1087 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1088 ? 0
1089 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1090 else if (area == LEFT_MARGIN_AREA
1091 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1092 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1093
1094 return x;
1095 }
1096
1097
1098 /* Return the window-relative coordinate of the right edge of display
1099 area AREA of window W. AREA < 0 means return the right edge of the
1100 whole window, to the left of the right fringe of W. */
1101
1102 int
1103 window_box_right_offset (struct window *w, int area)
1104 {
1105 return window_box_left_offset (w, area) + window_box_width (w, area);
1106 }
1107
1108 /* Return the frame-relative coordinate of the left edge of display
1109 area AREA of window W. AREA < 0 means return the left edge of the
1110 whole window, to the right of the left fringe of W. */
1111
1112 int
1113 window_box_left (struct window *w, int area)
1114 {
1115 struct frame *f = XFRAME (WGET (w, frame));
1116 int x;
1117
1118 if (w->pseudo_window_p)
1119 return FRAME_INTERNAL_BORDER_WIDTH (f);
1120
1121 x = (WINDOW_LEFT_EDGE_X (w)
1122 + window_box_left_offset (w, area));
1123
1124 return x;
1125 }
1126
1127
1128 /* Return the frame-relative coordinate of the right edge of display
1129 area AREA of window W. AREA < 0 means return the right edge of the
1130 whole window, to the left of the right fringe of W. */
1131
1132 int
1133 window_box_right (struct window *w, int area)
1134 {
1135 return window_box_left (w, area) + window_box_width (w, area);
1136 }
1137
1138 /* Get the bounding box of the display area AREA of window W, without
1139 mode lines, in frame-relative coordinates. AREA < 0 means the
1140 whole window, not including the left and right fringes of
1141 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1142 coordinates of the upper-left corner of the box. Return in
1143 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1144
1145 void
1146 window_box (struct window *w, int area, int *box_x, int *box_y,
1147 int *box_width, int *box_height)
1148 {
1149 if (box_width)
1150 *box_width = window_box_width (w, area);
1151 if (box_height)
1152 *box_height = window_box_height (w);
1153 if (box_x)
1154 *box_x = window_box_left (w, area);
1155 if (box_y)
1156 {
1157 *box_y = WINDOW_TOP_EDGE_Y (w);
1158 if (WINDOW_WANTS_HEADER_LINE_P (w))
1159 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1160 }
1161 }
1162
1163
1164 /* Get the bounding box of the display area AREA of window W, without
1165 mode lines. AREA < 0 means the whole window, not including the
1166 left and right fringe of the window. Return in *TOP_LEFT_X
1167 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1168 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1169 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1170 box. */
1171
1172 static inline void
1173 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1174 int *bottom_right_x, int *bottom_right_y)
1175 {
1176 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1177 bottom_right_y);
1178 *bottom_right_x += *top_left_x;
1179 *bottom_right_y += *top_left_y;
1180 }
1181
1182
1183 \f
1184 /***********************************************************************
1185 Utilities
1186 ***********************************************************************/
1187
1188 /* Return the bottom y-position of the line the iterator IT is in.
1189 This can modify IT's settings. */
1190
1191 int
1192 line_bottom_y (struct it *it)
1193 {
1194 int line_height = it->max_ascent + it->max_descent;
1195 int line_top_y = it->current_y;
1196
1197 if (line_height == 0)
1198 {
1199 if (last_height)
1200 line_height = last_height;
1201 else if (IT_CHARPOS (*it) < ZV)
1202 {
1203 move_it_by_lines (it, 1);
1204 line_height = (it->max_ascent || it->max_descent
1205 ? it->max_ascent + it->max_descent
1206 : last_height);
1207 }
1208 else
1209 {
1210 struct glyph_row *row = it->glyph_row;
1211
1212 /* Use the default character height. */
1213 it->glyph_row = NULL;
1214 it->what = IT_CHARACTER;
1215 it->c = ' ';
1216 it->len = 1;
1217 PRODUCE_GLYPHS (it);
1218 line_height = it->ascent + it->descent;
1219 it->glyph_row = row;
1220 }
1221 }
1222
1223 return line_top_y + line_height;
1224 }
1225
1226 /* Subroutine of pos_visible_p below. Extracts a display string, if
1227 any, from the display spec given as its argument. */
1228 static Lisp_Object
1229 string_from_display_spec (Lisp_Object spec)
1230 {
1231 if (CONSP (spec))
1232 {
1233 while (CONSP (spec))
1234 {
1235 if (STRINGP (XCAR (spec)))
1236 return XCAR (spec);
1237 spec = XCDR (spec);
1238 }
1239 }
1240 else if (VECTORP (spec))
1241 {
1242 ptrdiff_t i;
1243
1244 for (i = 0; i < ASIZE (spec); i++)
1245 {
1246 if (STRINGP (AREF (spec, i)))
1247 return AREF (spec, i);
1248 }
1249 return Qnil;
1250 }
1251
1252 return spec;
1253 }
1254
1255
1256 /* Limit insanely large values of W->hscroll on frame F to the largest
1257 value that will still prevent first_visible_x and last_visible_x of
1258 'struct it' from overflowing an int. */
1259 static inline int
1260 window_hscroll_limited (struct window *w, struct frame *f)
1261 {
1262 ptrdiff_t window_hscroll = w->hscroll;
1263 int window_text_width = window_box_width (w, TEXT_AREA);
1264 int colwidth = FRAME_COLUMN_WIDTH (f);
1265
1266 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1267 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1268
1269 return window_hscroll;
1270 }
1271
1272 /* Return 1 if position CHARPOS is visible in window W.
1273 CHARPOS < 0 means return info about WINDOW_END position.
1274 If visible, set *X and *Y to pixel coordinates of top left corner.
1275 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1276 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1277
1278 int
1279 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1280 int *rtop, int *rbot, int *rowh, int *vpos)
1281 {
1282 struct it it;
1283 void *itdata = bidi_shelve_cache ();
1284 struct text_pos top;
1285 int visible_p = 0;
1286 struct buffer *old_buffer = NULL;
1287
1288 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1289 return visible_p;
1290
1291 if (XBUFFER (WGET (w, buffer)) != current_buffer)
1292 {
1293 old_buffer = current_buffer;
1294 set_buffer_internal_1 (XBUFFER (WGET (w, buffer)));
1295 }
1296
1297 SET_TEXT_POS_FROM_MARKER (top, WGET (w, start));
1298 /* Scrolling a minibuffer window via scroll bar when the echo area
1299 shows long text sometimes resets the minibuffer contents behind
1300 our backs. */
1301 if (CHARPOS (top) > ZV)
1302 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1303
1304 /* Compute exact mode line heights. */
1305 if (WINDOW_WANTS_MODELINE_P (w))
1306 current_mode_line_height
1307 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1308 BVAR (current_buffer, mode_line_format));
1309
1310 if (WINDOW_WANTS_HEADER_LINE_P (w))
1311 current_header_line_height
1312 = display_mode_line (w, HEADER_LINE_FACE_ID,
1313 BVAR (current_buffer, header_line_format));
1314
1315 start_display (&it, w, top);
1316 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1317 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1318
1319 if (charpos >= 0
1320 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1321 && IT_CHARPOS (it) >= charpos)
1322 /* When scanning backwards under bidi iteration, move_it_to
1323 stops at or _before_ CHARPOS, because it stops at or to
1324 the _right_ of the character at CHARPOS. */
1325 || (it.bidi_p && it.bidi_it.scan_dir == -1
1326 && IT_CHARPOS (it) <= charpos)))
1327 {
1328 /* We have reached CHARPOS, or passed it. How the call to
1329 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1330 or covered by a display property, move_it_to stops at the end
1331 of the invisible text, to the right of CHARPOS. (ii) If
1332 CHARPOS is in a display vector, move_it_to stops on its last
1333 glyph. */
1334 int top_x = it.current_x;
1335 int top_y = it.current_y;
1336 /* Calling line_bottom_y may change it.method, it.position, etc. */
1337 enum it_method it_method = it.method;
1338 int bottom_y = (last_height = 0, line_bottom_y (&it));
1339 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1340
1341 if (top_y < window_top_y)
1342 visible_p = bottom_y > window_top_y;
1343 else if (top_y < it.last_visible_y)
1344 visible_p = 1;
1345 if (bottom_y >= it.last_visible_y
1346 && it.bidi_p && it.bidi_it.scan_dir == -1
1347 && IT_CHARPOS (it) < charpos)
1348 {
1349 /* When the last line of the window is scanned backwards
1350 under bidi iteration, we could be duped into thinking
1351 that we have passed CHARPOS, when in fact move_it_to
1352 simply stopped short of CHARPOS because it reached
1353 last_visible_y. To see if that's what happened, we call
1354 move_it_to again with a slightly larger vertical limit,
1355 and see if it actually moved vertically; if it did, we
1356 didn't really reach CHARPOS, which is beyond window end. */
1357 struct it save_it = it;
1358 /* Why 10? because we don't know how many canonical lines
1359 will the height of the next line(s) be. So we guess. */
1360 int ten_more_lines =
1361 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1362
1363 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1364 MOVE_TO_POS | MOVE_TO_Y);
1365 if (it.current_y > top_y)
1366 visible_p = 0;
1367
1368 it = save_it;
1369 }
1370 if (visible_p)
1371 {
1372 if (it_method == GET_FROM_DISPLAY_VECTOR)
1373 {
1374 /* We stopped on the last glyph of a display vector.
1375 Try and recompute. Hack alert! */
1376 if (charpos < 2 || top.charpos >= charpos)
1377 top_x = it.glyph_row->x;
1378 else
1379 {
1380 struct it it2;
1381 start_display (&it2, w, top);
1382 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1383 get_next_display_element (&it2);
1384 PRODUCE_GLYPHS (&it2);
1385 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1386 || it2.current_x > it2.last_visible_x)
1387 top_x = it.glyph_row->x;
1388 else
1389 {
1390 top_x = it2.current_x;
1391 top_y = it2.current_y;
1392 }
1393 }
1394 }
1395 else if (IT_CHARPOS (it) != charpos)
1396 {
1397 Lisp_Object cpos = make_number (charpos);
1398 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1399 Lisp_Object string = string_from_display_spec (spec);
1400 int newline_in_string = 0;
1401
1402 if (STRINGP (string))
1403 {
1404 const char *s = SSDATA (string);
1405 const char *e = s + SBYTES (string);
1406 while (s < e)
1407 {
1408 if (*s++ == '\n')
1409 {
1410 newline_in_string = 1;
1411 break;
1412 }
1413 }
1414 }
1415 /* The tricky code below is needed because there's a
1416 discrepancy between move_it_to and how we set cursor
1417 when the display line ends in a newline from a
1418 display string. move_it_to will stop _after_ such
1419 display strings, whereas set_cursor_from_row
1420 conspires with cursor_row_p to place the cursor on
1421 the first glyph produced from the display string. */
1422
1423 /* We have overshoot PT because it is covered by a
1424 display property whose value is a string. If the
1425 string includes embedded newlines, we are also in the
1426 wrong display line. Backtrack to the correct line,
1427 where the display string begins. */
1428 if (newline_in_string)
1429 {
1430 Lisp_Object startpos, endpos;
1431 EMACS_INT start, end;
1432 struct it it3;
1433 int it3_moved;
1434
1435 /* Find the first and the last buffer positions
1436 covered by the display string. */
1437 endpos =
1438 Fnext_single_char_property_change (cpos, Qdisplay,
1439 Qnil, Qnil);
1440 startpos =
1441 Fprevious_single_char_property_change (endpos, Qdisplay,
1442 Qnil, Qnil);
1443 start = XFASTINT (startpos);
1444 end = XFASTINT (endpos);
1445 /* Move to the last buffer position before the
1446 display property. */
1447 start_display (&it3, w, top);
1448 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1449 /* Move forward one more line if the position before
1450 the display string is a newline or if it is the
1451 rightmost character on a line that is
1452 continued or word-wrapped. */
1453 if (it3.method == GET_FROM_BUFFER
1454 && it3.c == '\n')
1455 move_it_by_lines (&it3, 1);
1456 else if (move_it_in_display_line_to (&it3, -1,
1457 it3.current_x
1458 + it3.pixel_width,
1459 MOVE_TO_X)
1460 == MOVE_LINE_CONTINUED)
1461 {
1462 move_it_by_lines (&it3, 1);
1463 /* When we are under word-wrap, the #$@%!
1464 move_it_by_lines moves 2 lines, so we need to
1465 fix that up. */
1466 if (it3.line_wrap == WORD_WRAP)
1467 move_it_by_lines (&it3, -1);
1468 }
1469
1470 /* Record the vertical coordinate of the display
1471 line where we wound up. */
1472 top_y = it3.current_y;
1473 if (it3.bidi_p)
1474 {
1475 /* When characters are reordered for display,
1476 the character displayed to the left of the
1477 display string could be _after_ the display
1478 property in the logical order. Use the
1479 smallest vertical position of these two. */
1480 start_display (&it3, w, top);
1481 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1482 if (it3.current_y < top_y)
1483 top_y = it3.current_y;
1484 }
1485 /* Move from the top of the window to the beginning
1486 of the display line where the display string
1487 begins. */
1488 start_display (&it3, w, top);
1489 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1490 /* If it3_moved stays zero after the 'while' loop
1491 below, that means we already were at a newline
1492 before the loop (e.g., the display string begins
1493 with a newline), so we don't need to (and cannot)
1494 inspect the glyphs of it3.glyph_row, because
1495 PRODUCE_GLYPHS will not produce anything for a
1496 newline, and thus it3.glyph_row stays at its
1497 stale content it got at top of the window. */
1498 it3_moved = 0;
1499 /* Finally, advance the iterator until we hit the
1500 first display element whose character position is
1501 CHARPOS, or until the first newline from the
1502 display string, which signals the end of the
1503 display line. */
1504 while (get_next_display_element (&it3))
1505 {
1506 PRODUCE_GLYPHS (&it3);
1507 if (IT_CHARPOS (it3) == charpos
1508 || ITERATOR_AT_END_OF_LINE_P (&it3))
1509 break;
1510 it3_moved = 1;
1511 set_iterator_to_next (&it3, 0);
1512 }
1513 top_x = it3.current_x - it3.pixel_width;
1514 /* Normally, we would exit the above loop because we
1515 found the display element whose character
1516 position is CHARPOS. For the contingency that we
1517 didn't, and stopped at the first newline from the
1518 display string, move back over the glyphs
1519 produced from the string, until we find the
1520 rightmost glyph not from the string. */
1521 if (it3_moved
1522 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1523 {
1524 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1525 + it3.glyph_row->used[TEXT_AREA];
1526
1527 while (EQ ((g - 1)->object, string))
1528 {
1529 --g;
1530 top_x -= g->pixel_width;
1531 }
1532 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1533 + it3.glyph_row->used[TEXT_AREA]);
1534 }
1535 }
1536 }
1537
1538 *x = top_x;
1539 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1540 *rtop = max (0, window_top_y - top_y);
1541 *rbot = max (0, bottom_y - it.last_visible_y);
1542 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1543 - max (top_y, window_top_y)));
1544 *vpos = it.vpos;
1545 }
1546 }
1547 else
1548 {
1549 /* We were asked to provide info about WINDOW_END. */
1550 struct it it2;
1551 void *it2data = NULL;
1552
1553 SAVE_IT (it2, it, it2data);
1554 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1555 move_it_by_lines (&it, 1);
1556 if (charpos < IT_CHARPOS (it)
1557 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1558 {
1559 visible_p = 1;
1560 RESTORE_IT (&it2, &it2, it2data);
1561 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1562 *x = it2.current_x;
1563 *y = it2.current_y + it2.max_ascent - it2.ascent;
1564 *rtop = max (0, -it2.current_y);
1565 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1566 - it.last_visible_y));
1567 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1568 it.last_visible_y)
1569 - max (it2.current_y,
1570 WINDOW_HEADER_LINE_HEIGHT (w))));
1571 *vpos = it2.vpos;
1572 }
1573 else
1574 bidi_unshelve_cache (it2data, 1);
1575 }
1576 bidi_unshelve_cache (itdata, 0);
1577
1578 if (old_buffer)
1579 set_buffer_internal_1 (old_buffer);
1580
1581 current_header_line_height = current_mode_line_height = -1;
1582
1583 if (visible_p && w->hscroll > 0)
1584 *x -=
1585 window_hscroll_limited (w, WINDOW_XFRAME (w))
1586 * WINDOW_FRAME_COLUMN_WIDTH (w);
1587
1588 #if 0
1589 /* Debugging code. */
1590 if (visible_p)
1591 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1592 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1593 else
1594 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1595 #endif
1596
1597 return visible_p;
1598 }
1599
1600
1601 /* Return the next character from STR. Return in *LEN the length of
1602 the character. This is like STRING_CHAR_AND_LENGTH but never
1603 returns an invalid character. If we find one, we return a `?', but
1604 with the length of the invalid character. */
1605
1606 static inline int
1607 string_char_and_length (const unsigned char *str, int *len)
1608 {
1609 int c;
1610
1611 c = STRING_CHAR_AND_LENGTH (str, *len);
1612 if (!CHAR_VALID_P (c))
1613 /* We may not change the length here because other places in Emacs
1614 don't use this function, i.e. they silently accept invalid
1615 characters. */
1616 c = '?';
1617
1618 return c;
1619 }
1620
1621
1622
1623 /* Given a position POS containing a valid character and byte position
1624 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1625
1626 static struct text_pos
1627 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1628 {
1629 eassert (STRINGP (string) && nchars >= 0);
1630
1631 if (STRING_MULTIBYTE (string))
1632 {
1633 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1634 int len;
1635
1636 while (nchars--)
1637 {
1638 string_char_and_length (p, &len);
1639 p += len;
1640 CHARPOS (pos) += 1;
1641 BYTEPOS (pos) += len;
1642 }
1643 }
1644 else
1645 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1646
1647 return pos;
1648 }
1649
1650
1651 /* Value is the text position, i.e. character and byte position,
1652 for character position CHARPOS in STRING. */
1653
1654 static inline struct text_pos
1655 string_pos (ptrdiff_t charpos, Lisp_Object string)
1656 {
1657 struct text_pos pos;
1658 eassert (STRINGP (string));
1659 eassert (charpos >= 0);
1660 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1661 return pos;
1662 }
1663
1664
1665 /* Value is a text position, i.e. character and byte position, for
1666 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1667 means recognize multibyte characters. */
1668
1669 static struct text_pos
1670 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1671 {
1672 struct text_pos pos;
1673
1674 eassert (s != NULL);
1675 eassert (charpos >= 0);
1676
1677 if (multibyte_p)
1678 {
1679 int len;
1680
1681 SET_TEXT_POS (pos, 0, 0);
1682 while (charpos--)
1683 {
1684 string_char_and_length ((const unsigned char *) s, &len);
1685 s += len;
1686 CHARPOS (pos) += 1;
1687 BYTEPOS (pos) += len;
1688 }
1689 }
1690 else
1691 SET_TEXT_POS (pos, charpos, charpos);
1692
1693 return pos;
1694 }
1695
1696
1697 /* Value is the number of characters in C string S. MULTIBYTE_P
1698 non-zero means recognize multibyte characters. */
1699
1700 static ptrdiff_t
1701 number_of_chars (const char *s, int multibyte_p)
1702 {
1703 ptrdiff_t nchars;
1704
1705 if (multibyte_p)
1706 {
1707 ptrdiff_t rest = strlen (s);
1708 int len;
1709 const unsigned char *p = (const unsigned char *) s;
1710
1711 for (nchars = 0; rest > 0; ++nchars)
1712 {
1713 string_char_and_length (p, &len);
1714 rest -= len, p += len;
1715 }
1716 }
1717 else
1718 nchars = strlen (s);
1719
1720 return nchars;
1721 }
1722
1723
1724 /* Compute byte position NEWPOS->bytepos corresponding to
1725 NEWPOS->charpos. POS is a known position in string STRING.
1726 NEWPOS->charpos must be >= POS.charpos. */
1727
1728 static void
1729 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1730 {
1731 eassert (STRINGP (string));
1732 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1733
1734 if (STRING_MULTIBYTE (string))
1735 *newpos = string_pos_nchars_ahead (pos, string,
1736 CHARPOS (*newpos) - CHARPOS (pos));
1737 else
1738 BYTEPOS (*newpos) = CHARPOS (*newpos);
1739 }
1740
1741 /* EXPORT:
1742 Return an estimation of the pixel height of mode or header lines on
1743 frame F. FACE_ID specifies what line's height to estimate. */
1744
1745 int
1746 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1747 {
1748 #ifdef HAVE_WINDOW_SYSTEM
1749 if (FRAME_WINDOW_P (f))
1750 {
1751 int height = FONT_HEIGHT (FRAME_FONT (f));
1752
1753 /* This function is called so early when Emacs starts that the face
1754 cache and mode line face are not yet initialized. */
1755 if (FRAME_FACE_CACHE (f))
1756 {
1757 struct face *face = FACE_FROM_ID (f, face_id);
1758 if (face)
1759 {
1760 if (face->font)
1761 height = FONT_HEIGHT (face->font);
1762 if (face->box_line_width > 0)
1763 height += 2 * face->box_line_width;
1764 }
1765 }
1766
1767 return height;
1768 }
1769 #endif
1770
1771 return 1;
1772 }
1773
1774 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1775 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1776 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1777 not force the value into range. */
1778
1779 void
1780 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1781 int *x, int *y, NativeRectangle *bounds, int noclip)
1782 {
1783
1784 #ifdef HAVE_WINDOW_SYSTEM
1785 if (FRAME_WINDOW_P (f))
1786 {
1787 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1788 even for negative values. */
1789 if (pix_x < 0)
1790 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1791 if (pix_y < 0)
1792 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1793
1794 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1795 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1796
1797 if (bounds)
1798 STORE_NATIVE_RECT (*bounds,
1799 FRAME_COL_TO_PIXEL_X (f, pix_x),
1800 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1801 FRAME_COLUMN_WIDTH (f) - 1,
1802 FRAME_LINE_HEIGHT (f) - 1);
1803
1804 if (!noclip)
1805 {
1806 if (pix_x < 0)
1807 pix_x = 0;
1808 else if (pix_x > FRAME_TOTAL_COLS (f))
1809 pix_x = FRAME_TOTAL_COLS (f);
1810
1811 if (pix_y < 0)
1812 pix_y = 0;
1813 else if (pix_y > FRAME_LINES (f))
1814 pix_y = FRAME_LINES (f);
1815 }
1816 }
1817 #endif
1818
1819 *x = pix_x;
1820 *y = pix_y;
1821 }
1822
1823
1824 /* Find the glyph under window-relative coordinates X/Y in window W.
1825 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1826 strings. Return in *HPOS and *VPOS the row and column number of
1827 the glyph found. Return in *AREA the glyph area containing X.
1828 Value is a pointer to the glyph found or null if X/Y is not on
1829 text, or we can't tell because W's current matrix is not up to
1830 date. */
1831
1832 static
1833 struct glyph *
1834 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1835 int *dx, int *dy, int *area)
1836 {
1837 struct glyph *glyph, *end;
1838 struct glyph_row *row = NULL;
1839 int x0, i;
1840
1841 /* Find row containing Y. Give up if some row is not enabled. */
1842 for (i = 0; i < w->current_matrix->nrows; ++i)
1843 {
1844 row = MATRIX_ROW (w->current_matrix, i);
1845 if (!row->enabled_p)
1846 return NULL;
1847 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1848 break;
1849 }
1850
1851 *vpos = i;
1852 *hpos = 0;
1853
1854 /* Give up if Y is not in the window. */
1855 if (i == w->current_matrix->nrows)
1856 return NULL;
1857
1858 /* Get the glyph area containing X. */
1859 if (w->pseudo_window_p)
1860 {
1861 *area = TEXT_AREA;
1862 x0 = 0;
1863 }
1864 else
1865 {
1866 if (x < window_box_left_offset (w, TEXT_AREA))
1867 {
1868 *area = LEFT_MARGIN_AREA;
1869 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1870 }
1871 else if (x < window_box_right_offset (w, TEXT_AREA))
1872 {
1873 *area = TEXT_AREA;
1874 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1875 }
1876 else
1877 {
1878 *area = RIGHT_MARGIN_AREA;
1879 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1880 }
1881 }
1882
1883 /* Find glyph containing X. */
1884 glyph = row->glyphs[*area];
1885 end = glyph + row->used[*area];
1886 x -= x0;
1887 while (glyph < end && x >= glyph->pixel_width)
1888 {
1889 x -= glyph->pixel_width;
1890 ++glyph;
1891 }
1892
1893 if (glyph == end)
1894 return NULL;
1895
1896 if (dx)
1897 {
1898 *dx = x;
1899 *dy = y - (row->y + row->ascent - glyph->ascent);
1900 }
1901
1902 *hpos = glyph - row->glyphs[*area];
1903 return glyph;
1904 }
1905
1906 /* Convert frame-relative x/y to coordinates relative to window W.
1907 Takes pseudo-windows into account. */
1908
1909 static void
1910 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1911 {
1912 if (w->pseudo_window_p)
1913 {
1914 /* A pseudo-window is always full-width, and starts at the
1915 left edge of the frame, plus a frame border. */
1916 struct frame *f = XFRAME (WGET (w, frame));
1917 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1918 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1919 }
1920 else
1921 {
1922 *x -= WINDOW_LEFT_EDGE_X (w);
1923 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1924 }
1925 }
1926
1927 #ifdef HAVE_WINDOW_SYSTEM
1928
1929 /* EXPORT:
1930 Return in RECTS[] at most N clipping rectangles for glyph string S.
1931 Return the number of stored rectangles. */
1932
1933 int
1934 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1935 {
1936 XRectangle r;
1937
1938 if (n <= 0)
1939 return 0;
1940
1941 if (s->row->full_width_p)
1942 {
1943 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1944 r.x = WINDOW_LEFT_EDGE_X (s->w);
1945 r.width = WINDOW_TOTAL_WIDTH (s->w);
1946
1947 /* Unless displaying a mode or menu bar line, which are always
1948 fully visible, clip to the visible part of the row. */
1949 if (s->w->pseudo_window_p)
1950 r.height = s->row->visible_height;
1951 else
1952 r.height = s->height;
1953 }
1954 else
1955 {
1956 /* This is a text line that may be partially visible. */
1957 r.x = window_box_left (s->w, s->area);
1958 r.width = window_box_width (s->w, s->area);
1959 r.height = s->row->visible_height;
1960 }
1961
1962 if (s->clip_head)
1963 if (r.x < s->clip_head->x)
1964 {
1965 if (r.width >= s->clip_head->x - r.x)
1966 r.width -= s->clip_head->x - r.x;
1967 else
1968 r.width = 0;
1969 r.x = s->clip_head->x;
1970 }
1971 if (s->clip_tail)
1972 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1973 {
1974 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1975 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1976 else
1977 r.width = 0;
1978 }
1979
1980 /* If S draws overlapping rows, it's sufficient to use the top and
1981 bottom of the window for clipping because this glyph string
1982 intentionally draws over other lines. */
1983 if (s->for_overlaps)
1984 {
1985 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1986 r.height = window_text_bottom_y (s->w) - r.y;
1987
1988 /* Alas, the above simple strategy does not work for the
1989 environments with anti-aliased text: if the same text is
1990 drawn onto the same place multiple times, it gets thicker.
1991 If the overlap we are processing is for the erased cursor, we
1992 take the intersection with the rectangle of the cursor. */
1993 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1994 {
1995 XRectangle rc, r_save = r;
1996
1997 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1998 rc.y = s->w->phys_cursor.y;
1999 rc.width = s->w->phys_cursor_width;
2000 rc.height = s->w->phys_cursor_height;
2001
2002 x_intersect_rectangles (&r_save, &rc, &r);
2003 }
2004 }
2005 else
2006 {
2007 /* Don't use S->y for clipping because it doesn't take partially
2008 visible lines into account. For example, it can be negative for
2009 partially visible lines at the top of a window. */
2010 if (!s->row->full_width_p
2011 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2012 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2013 else
2014 r.y = max (0, s->row->y);
2015 }
2016
2017 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2018
2019 /* If drawing the cursor, don't let glyph draw outside its
2020 advertised boundaries. Cleartype does this under some circumstances. */
2021 if (s->hl == DRAW_CURSOR)
2022 {
2023 struct glyph *glyph = s->first_glyph;
2024 int height, max_y;
2025
2026 if (s->x > r.x)
2027 {
2028 r.width -= s->x - r.x;
2029 r.x = s->x;
2030 }
2031 r.width = min (r.width, glyph->pixel_width);
2032
2033 /* If r.y is below window bottom, ensure that we still see a cursor. */
2034 height = min (glyph->ascent + glyph->descent,
2035 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2036 max_y = window_text_bottom_y (s->w) - height;
2037 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2038 if (s->ybase - glyph->ascent > max_y)
2039 {
2040 r.y = max_y;
2041 r.height = height;
2042 }
2043 else
2044 {
2045 /* Don't draw cursor glyph taller than our actual glyph. */
2046 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2047 if (height < r.height)
2048 {
2049 max_y = r.y + r.height;
2050 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2051 r.height = min (max_y - r.y, height);
2052 }
2053 }
2054 }
2055
2056 if (s->row->clip)
2057 {
2058 XRectangle r_save = r;
2059
2060 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2061 r.width = 0;
2062 }
2063
2064 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2065 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2066 {
2067 #ifdef CONVERT_FROM_XRECT
2068 CONVERT_FROM_XRECT (r, *rects);
2069 #else
2070 *rects = r;
2071 #endif
2072 return 1;
2073 }
2074 else
2075 {
2076 /* If we are processing overlapping and allowed to return
2077 multiple clipping rectangles, we exclude the row of the glyph
2078 string from the clipping rectangle. This is to avoid drawing
2079 the same text on the environment with anti-aliasing. */
2080 #ifdef CONVERT_FROM_XRECT
2081 XRectangle rs[2];
2082 #else
2083 XRectangle *rs = rects;
2084 #endif
2085 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2086
2087 if (s->for_overlaps & OVERLAPS_PRED)
2088 {
2089 rs[i] = r;
2090 if (r.y + r.height > row_y)
2091 {
2092 if (r.y < row_y)
2093 rs[i].height = row_y - r.y;
2094 else
2095 rs[i].height = 0;
2096 }
2097 i++;
2098 }
2099 if (s->for_overlaps & OVERLAPS_SUCC)
2100 {
2101 rs[i] = r;
2102 if (r.y < row_y + s->row->visible_height)
2103 {
2104 if (r.y + r.height > row_y + s->row->visible_height)
2105 {
2106 rs[i].y = row_y + s->row->visible_height;
2107 rs[i].height = r.y + r.height - rs[i].y;
2108 }
2109 else
2110 rs[i].height = 0;
2111 }
2112 i++;
2113 }
2114
2115 n = i;
2116 #ifdef CONVERT_FROM_XRECT
2117 for (i = 0; i < n; i++)
2118 CONVERT_FROM_XRECT (rs[i], rects[i]);
2119 #endif
2120 return n;
2121 }
2122 }
2123
2124 /* EXPORT:
2125 Return in *NR the clipping rectangle for glyph string S. */
2126
2127 void
2128 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2129 {
2130 get_glyph_string_clip_rects (s, nr, 1);
2131 }
2132
2133
2134 /* EXPORT:
2135 Return the position and height of the phys cursor in window W.
2136 Set w->phys_cursor_width to width of phys cursor.
2137 */
2138
2139 void
2140 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2141 struct glyph *glyph, int *xp, int *yp, int *heightp)
2142 {
2143 struct frame *f = XFRAME (WINDOW_FRAME (w));
2144 int x, y, wd, h, h0, y0;
2145
2146 /* Compute the width of the rectangle to draw. If on a stretch
2147 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2148 rectangle as wide as the glyph, but use a canonical character
2149 width instead. */
2150 wd = glyph->pixel_width - 1;
2151 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2152 wd++; /* Why? */
2153 #endif
2154
2155 x = w->phys_cursor.x;
2156 if (x < 0)
2157 {
2158 wd += x;
2159 x = 0;
2160 }
2161
2162 if (glyph->type == STRETCH_GLYPH
2163 && !x_stretch_cursor_p)
2164 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2165 w->phys_cursor_width = wd;
2166
2167 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2168
2169 /* If y is below window bottom, ensure that we still see a cursor. */
2170 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2171
2172 h = max (h0, glyph->ascent + glyph->descent);
2173 h0 = min (h0, glyph->ascent + glyph->descent);
2174
2175 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2176 if (y < y0)
2177 {
2178 h = max (h - (y0 - y) + 1, h0);
2179 y = y0 - 1;
2180 }
2181 else
2182 {
2183 y0 = window_text_bottom_y (w) - h0;
2184 if (y > y0)
2185 {
2186 h += y - y0;
2187 y = y0;
2188 }
2189 }
2190
2191 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2192 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2193 *heightp = h;
2194 }
2195
2196 /*
2197 * Remember which glyph the mouse is over.
2198 */
2199
2200 void
2201 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2202 {
2203 Lisp_Object window;
2204 struct window *w;
2205 struct glyph_row *r, *gr, *end_row;
2206 enum window_part part;
2207 enum glyph_row_area area;
2208 int x, y, width, height;
2209
2210 /* Try to determine frame pixel position and size of the glyph under
2211 frame pixel coordinates X/Y on frame F. */
2212
2213 if (!f->glyphs_initialized_p
2214 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2215 NILP (window)))
2216 {
2217 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2218 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2219 goto virtual_glyph;
2220 }
2221
2222 w = XWINDOW (window);
2223 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2224 height = WINDOW_FRAME_LINE_HEIGHT (w);
2225
2226 x = window_relative_x_coord (w, part, gx);
2227 y = gy - WINDOW_TOP_EDGE_Y (w);
2228
2229 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2230 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2231
2232 if (w->pseudo_window_p)
2233 {
2234 area = TEXT_AREA;
2235 part = ON_MODE_LINE; /* Don't adjust margin. */
2236 goto text_glyph;
2237 }
2238
2239 switch (part)
2240 {
2241 case ON_LEFT_MARGIN:
2242 area = LEFT_MARGIN_AREA;
2243 goto text_glyph;
2244
2245 case ON_RIGHT_MARGIN:
2246 area = RIGHT_MARGIN_AREA;
2247 goto text_glyph;
2248
2249 case ON_HEADER_LINE:
2250 case ON_MODE_LINE:
2251 gr = (part == ON_HEADER_LINE
2252 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2253 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2254 gy = gr->y;
2255 area = TEXT_AREA;
2256 goto text_glyph_row_found;
2257
2258 case ON_TEXT:
2259 area = TEXT_AREA;
2260
2261 text_glyph:
2262 gr = 0; gy = 0;
2263 for (; r <= end_row && r->enabled_p; ++r)
2264 if (r->y + r->height > y)
2265 {
2266 gr = r; gy = r->y;
2267 break;
2268 }
2269
2270 text_glyph_row_found:
2271 if (gr && gy <= y)
2272 {
2273 struct glyph *g = gr->glyphs[area];
2274 struct glyph *end = g + gr->used[area];
2275
2276 height = gr->height;
2277 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2278 if (gx + g->pixel_width > x)
2279 break;
2280
2281 if (g < end)
2282 {
2283 if (g->type == IMAGE_GLYPH)
2284 {
2285 /* Don't remember when mouse is over image, as
2286 image may have hot-spots. */
2287 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2288 return;
2289 }
2290 width = g->pixel_width;
2291 }
2292 else
2293 {
2294 /* Use nominal char spacing at end of line. */
2295 x -= gx;
2296 gx += (x / width) * width;
2297 }
2298
2299 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2300 gx += window_box_left_offset (w, area);
2301 }
2302 else
2303 {
2304 /* Use nominal line height at end of window. */
2305 gx = (x / width) * width;
2306 y -= gy;
2307 gy += (y / height) * height;
2308 }
2309 break;
2310
2311 case ON_LEFT_FRINGE:
2312 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2313 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2314 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2315 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2316 goto row_glyph;
2317
2318 case ON_RIGHT_FRINGE:
2319 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2320 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2321 : window_box_right_offset (w, TEXT_AREA));
2322 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2323 goto row_glyph;
2324
2325 case ON_SCROLL_BAR:
2326 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2327 ? 0
2328 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2329 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2330 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2331 : 0)));
2332 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2333
2334 row_glyph:
2335 gr = 0, gy = 0;
2336 for (; r <= end_row && r->enabled_p; ++r)
2337 if (r->y + r->height > y)
2338 {
2339 gr = r; gy = r->y;
2340 break;
2341 }
2342
2343 if (gr && gy <= y)
2344 height = gr->height;
2345 else
2346 {
2347 /* Use nominal line height at end of window. */
2348 y -= gy;
2349 gy += (y / height) * height;
2350 }
2351 break;
2352
2353 default:
2354 ;
2355 virtual_glyph:
2356 /* If there is no glyph under the mouse, then we divide the screen
2357 into a grid of the smallest glyph in the frame, and use that
2358 as our "glyph". */
2359
2360 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2361 round down even for negative values. */
2362 if (gx < 0)
2363 gx -= width - 1;
2364 if (gy < 0)
2365 gy -= height - 1;
2366
2367 gx = (gx / width) * width;
2368 gy = (gy / height) * height;
2369
2370 goto store_rect;
2371 }
2372
2373 gx += WINDOW_LEFT_EDGE_X (w);
2374 gy += WINDOW_TOP_EDGE_Y (w);
2375
2376 store_rect:
2377 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2378
2379 /* Visible feedback for debugging. */
2380 #if 0
2381 #if HAVE_X_WINDOWS
2382 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2383 f->output_data.x->normal_gc,
2384 gx, gy, width, height);
2385 #endif
2386 #endif
2387 }
2388
2389
2390 #endif /* HAVE_WINDOW_SYSTEM */
2391
2392 \f
2393 /***********************************************************************
2394 Lisp form evaluation
2395 ***********************************************************************/
2396
2397 /* Error handler for safe_eval and safe_call. */
2398
2399 static Lisp_Object
2400 safe_eval_handler (Lisp_Object arg)
2401 {
2402 add_to_log ("Error during redisplay: %S", arg, Qnil);
2403 return Qnil;
2404 }
2405
2406 /* Call function FUNC with the rest of NARGS - 1 arguments
2407 following. Return the result, or nil if something went
2408 wrong. Prevent redisplay during the evaluation. */
2409
2410 Lisp_Object
2411 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2412 {
2413 Lisp_Object val;
2414
2415 if (inhibit_eval_during_redisplay)
2416 val = Qnil;
2417 else
2418 {
2419 va_list ap;
2420 ptrdiff_t i;
2421 ptrdiff_t count = SPECPDL_INDEX ();
2422 struct gcpro gcpro1;
2423 Lisp_Object *args = alloca (nargs * word_size);
2424
2425 args[0] = func;
2426 va_start (ap, func);
2427 for (i = 1; i < nargs; i++)
2428 args[i] = va_arg (ap, Lisp_Object);
2429 va_end (ap);
2430
2431 GCPRO1 (args[0]);
2432 gcpro1.nvars = nargs;
2433 specbind (Qinhibit_redisplay, Qt);
2434 /* Use Qt to ensure debugger does not run,
2435 so there is no possibility of wanting to redisplay. */
2436 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2437 safe_eval_handler);
2438 UNGCPRO;
2439 val = unbind_to (count, val);
2440 }
2441
2442 return val;
2443 }
2444
2445
2446 /* Call function FN with one argument ARG.
2447 Return the result, or nil if something went wrong. */
2448
2449 Lisp_Object
2450 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2451 {
2452 return safe_call (2, fn, arg);
2453 }
2454
2455 static Lisp_Object Qeval;
2456
2457 Lisp_Object
2458 safe_eval (Lisp_Object sexpr)
2459 {
2460 return safe_call1 (Qeval, sexpr);
2461 }
2462
2463 /* Call function FN with two arguments ARG1 and ARG2.
2464 Return the result, or nil if something went wrong. */
2465
2466 Lisp_Object
2467 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2468 {
2469 return safe_call (3, fn, arg1, arg2);
2470 }
2471
2472
2473 \f
2474 /***********************************************************************
2475 Debugging
2476 ***********************************************************************/
2477
2478 #if 0
2479
2480 /* Define CHECK_IT to perform sanity checks on iterators.
2481 This is for debugging. It is too slow to do unconditionally. */
2482
2483 static void
2484 check_it (struct it *it)
2485 {
2486 if (it->method == GET_FROM_STRING)
2487 {
2488 eassert (STRINGP (it->string));
2489 eassert (IT_STRING_CHARPOS (*it) >= 0);
2490 }
2491 else
2492 {
2493 eassert (IT_STRING_CHARPOS (*it) < 0);
2494 if (it->method == GET_FROM_BUFFER)
2495 {
2496 /* Check that character and byte positions agree. */
2497 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2498 }
2499 }
2500
2501 if (it->dpvec)
2502 eassert (it->current.dpvec_index >= 0);
2503 else
2504 eassert (it->current.dpvec_index < 0);
2505 }
2506
2507 #define CHECK_IT(IT) check_it ((IT))
2508
2509 #else /* not 0 */
2510
2511 #define CHECK_IT(IT) (void) 0
2512
2513 #endif /* not 0 */
2514
2515
2516 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2517
2518 /* Check that the window end of window W is what we expect it
2519 to be---the last row in the current matrix displaying text. */
2520
2521 static void
2522 check_window_end (struct window *w)
2523 {
2524 if (!MINI_WINDOW_P (w)
2525 && !NILP (WGET (w, window_end_valid)))
2526 {
2527 struct glyph_row *row;
2528 eassert ((row = MATRIX_ROW (w->current_matrix,
2529 XFASTINT (WGET (w, window_end_vpos))),
2530 !row->enabled_p
2531 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2532 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2533 }
2534 }
2535
2536 #define CHECK_WINDOW_END(W) check_window_end ((W))
2537
2538 #else
2539
2540 #define CHECK_WINDOW_END(W) (void) 0
2541
2542 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2543
2544
2545 \f
2546 /***********************************************************************
2547 Iterator initialization
2548 ***********************************************************************/
2549
2550 /* Initialize IT for displaying current_buffer in window W, starting
2551 at character position CHARPOS. CHARPOS < 0 means that no buffer
2552 position is specified which is useful when the iterator is assigned
2553 a position later. BYTEPOS is the byte position corresponding to
2554 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2555
2556 If ROW is not null, calls to produce_glyphs with IT as parameter
2557 will produce glyphs in that row.
2558
2559 BASE_FACE_ID is the id of a base face to use. It must be one of
2560 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2561 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2562 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2563
2564 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2565 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2566 will be initialized to use the corresponding mode line glyph row of
2567 the desired matrix of W. */
2568
2569 void
2570 init_iterator (struct it *it, struct window *w,
2571 ptrdiff_t charpos, ptrdiff_t bytepos,
2572 struct glyph_row *row, enum face_id base_face_id)
2573 {
2574 int highlight_region_p;
2575 enum face_id remapped_base_face_id = base_face_id;
2576
2577 /* Some precondition checks. */
2578 eassert (w != NULL && it != NULL);
2579 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2580 && charpos <= ZV));
2581
2582 /* If face attributes have been changed since the last redisplay,
2583 free realized faces now because they depend on face definitions
2584 that might have changed. Don't free faces while there might be
2585 desired matrices pending which reference these faces. */
2586 if (face_change_count && !inhibit_free_realized_faces)
2587 {
2588 face_change_count = 0;
2589 free_all_realized_faces (Qnil);
2590 }
2591
2592 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2593 if (! NILP (Vface_remapping_alist))
2594 remapped_base_face_id
2595 = lookup_basic_face (XFRAME (WGET (w, frame)), base_face_id);
2596
2597 /* Use one of the mode line rows of W's desired matrix if
2598 appropriate. */
2599 if (row == NULL)
2600 {
2601 if (base_face_id == MODE_LINE_FACE_ID
2602 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2603 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2604 else if (base_face_id == HEADER_LINE_FACE_ID)
2605 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2606 }
2607
2608 /* Clear IT. */
2609 memset (it, 0, sizeof *it);
2610 it->current.overlay_string_index = -1;
2611 it->current.dpvec_index = -1;
2612 it->base_face_id = remapped_base_face_id;
2613 it->string = Qnil;
2614 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2615 it->paragraph_embedding = L2R;
2616 it->bidi_it.string.lstring = Qnil;
2617 it->bidi_it.string.s = NULL;
2618 it->bidi_it.string.bufpos = 0;
2619
2620 /* The window in which we iterate over current_buffer: */
2621 XSETWINDOW (it->window, w);
2622 it->w = w;
2623 it->f = XFRAME (WGET (w, frame));
2624
2625 it->cmp_it.id = -1;
2626
2627 /* Extra space between lines (on window systems only). */
2628 if (base_face_id == DEFAULT_FACE_ID
2629 && FRAME_WINDOW_P (it->f))
2630 {
2631 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2632 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2633 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2634 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2635 * FRAME_LINE_HEIGHT (it->f));
2636 else if (it->f->extra_line_spacing > 0)
2637 it->extra_line_spacing = it->f->extra_line_spacing;
2638 it->max_extra_line_spacing = 0;
2639 }
2640
2641 /* If realized faces have been removed, e.g. because of face
2642 attribute changes of named faces, recompute them. When running
2643 in batch mode, the face cache of the initial frame is null. If
2644 we happen to get called, make a dummy face cache. */
2645 if (FRAME_FACE_CACHE (it->f) == NULL)
2646 init_frame_faces (it->f);
2647 if (FRAME_FACE_CACHE (it->f)->used == 0)
2648 recompute_basic_faces (it->f);
2649
2650 /* Current value of the `slice', `space-width', and 'height' properties. */
2651 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2652 it->space_width = Qnil;
2653 it->font_height = Qnil;
2654 it->override_ascent = -1;
2655
2656 /* Are control characters displayed as `^C'? */
2657 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2658
2659 /* -1 means everything between a CR and the following line end
2660 is invisible. >0 means lines indented more than this value are
2661 invisible. */
2662 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2663 ? (clip_to_bounds
2664 (-1, XINT (BVAR (current_buffer, selective_display)),
2665 PTRDIFF_MAX))
2666 : (!NILP (BVAR (current_buffer, selective_display))
2667 ? -1 : 0));
2668 it->selective_display_ellipsis_p
2669 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2670
2671 /* Display table to use. */
2672 it->dp = window_display_table (w);
2673
2674 /* Are multibyte characters enabled in current_buffer? */
2675 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2676
2677 /* Non-zero if we should highlight the region. */
2678 highlight_region_p
2679 = (!NILP (Vtransient_mark_mode)
2680 && !NILP (BVAR (current_buffer, mark_active))
2681 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2682
2683 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2684 start and end of a visible region in window IT->w. Set both to
2685 -1 to indicate no region. */
2686 if (highlight_region_p
2687 /* Maybe highlight only in selected window. */
2688 && (/* Either show region everywhere. */
2689 highlight_nonselected_windows
2690 /* Or show region in the selected window. */
2691 || w == XWINDOW (selected_window)
2692 /* Or show the region if we are in the mini-buffer and W is
2693 the window the mini-buffer refers to. */
2694 || (MINI_WINDOW_P (XWINDOW (selected_window))
2695 && WINDOWP (minibuf_selected_window)
2696 && w == XWINDOW (minibuf_selected_window))))
2697 {
2698 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2699 it->region_beg_charpos = min (PT, markpos);
2700 it->region_end_charpos = max (PT, markpos);
2701 }
2702 else
2703 it->region_beg_charpos = it->region_end_charpos = -1;
2704
2705 /* Get the position at which the redisplay_end_trigger hook should
2706 be run, if it is to be run at all. */
2707 if (MARKERP (WGET (w, redisplay_end_trigger))
2708 && XMARKER (WGET (w, redisplay_end_trigger))->buffer != 0)
2709 it->redisplay_end_trigger_charpos
2710 = marker_position (WGET (w, redisplay_end_trigger));
2711 else if (INTEGERP (WGET (w, redisplay_end_trigger)))
2712 it->redisplay_end_trigger_charpos =
2713 clip_to_bounds (PTRDIFF_MIN, XINT (WGET (w, redisplay_end_trigger)), PTRDIFF_MAX);
2714
2715 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2716
2717 /* Are lines in the display truncated? */
2718 if (base_face_id != DEFAULT_FACE_ID
2719 || it->w->hscroll
2720 || (! WINDOW_FULL_WIDTH_P (it->w)
2721 && ((!NILP (Vtruncate_partial_width_windows)
2722 && !INTEGERP (Vtruncate_partial_width_windows))
2723 || (INTEGERP (Vtruncate_partial_width_windows)
2724 && (WINDOW_TOTAL_COLS (it->w)
2725 < XINT (Vtruncate_partial_width_windows))))))
2726 it->line_wrap = TRUNCATE;
2727 else if (NILP (BVAR (current_buffer, truncate_lines)))
2728 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2729 ? WINDOW_WRAP : WORD_WRAP;
2730 else
2731 it->line_wrap = TRUNCATE;
2732
2733 /* Get dimensions of truncation and continuation glyphs. These are
2734 displayed as fringe bitmaps under X, but we need them for such
2735 frames when the fringes are turned off. But leave the dimensions
2736 zero for tooltip frames, as these glyphs look ugly there and also
2737 sabotage calculations of tooltip dimensions in x-show-tip. */
2738 #ifdef HAVE_WINDOW_SYSTEM
2739 if (!(FRAME_WINDOW_P (it->f)
2740 && FRAMEP (tip_frame)
2741 && it->f == XFRAME (tip_frame)))
2742 #endif
2743 {
2744 if (it->line_wrap == TRUNCATE)
2745 {
2746 /* We will need the truncation glyph. */
2747 eassert (it->glyph_row == NULL);
2748 produce_special_glyphs (it, IT_TRUNCATION);
2749 it->truncation_pixel_width = it->pixel_width;
2750 }
2751 else
2752 {
2753 /* We will need the continuation glyph. */
2754 eassert (it->glyph_row == NULL);
2755 produce_special_glyphs (it, IT_CONTINUATION);
2756 it->continuation_pixel_width = it->pixel_width;
2757 }
2758 }
2759
2760 /* Reset these values to zero because the produce_special_glyphs
2761 above has changed them. */
2762 it->pixel_width = it->ascent = it->descent = 0;
2763 it->phys_ascent = it->phys_descent = 0;
2764
2765 /* Set this after getting the dimensions of truncation and
2766 continuation glyphs, so that we don't produce glyphs when calling
2767 produce_special_glyphs, above. */
2768 it->glyph_row = row;
2769 it->area = TEXT_AREA;
2770
2771 /* Forget any previous info about this row being reversed. */
2772 if (it->glyph_row)
2773 it->glyph_row->reversed_p = 0;
2774
2775 /* Get the dimensions of the display area. The display area
2776 consists of the visible window area plus a horizontally scrolled
2777 part to the left of the window. All x-values are relative to the
2778 start of this total display area. */
2779 if (base_face_id != DEFAULT_FACE_ID)
2780 {
2781 /* Mode lines, menu bar in terminal frames. */
2782 it->first_visible_x = 0;
2783 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2784 }
2785 else
2786 {
2787 it->first_visible_x =
2788 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2789 it->last_visible_x = (it->first_visible_x
2790 + window_box_width (w, TEXT_AREA));
2791
2792 /* If we truncate lines, leave room for the truncation glyph(s) at
2793 the right margin. Otherwise, leave room for the continuation
2794 glyph(s). Done only if the window has no fringes. Since we
2795 don't know at this point whether there will be any R2L lines in
2796 the window, we reserve space for truncation/continuation glyphs
2797 even if only one of the fringes is absent. */
2798 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2799 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2800 {
2801 if (it->line_wrap == TRUNCATE)
2802 it->last_visible_x -= it->truncation_pixel_width;
2803 else
2804 it->last_visible_x -= it->continuation_pixel_width;
2805 }
2806
2807 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2808 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2809 }
2810
2811 /* Leave room for a border glyph. */
2812 if (!FRAME_WINDOW_P (it->f)
2813 && !WINDOW_RIGHTMOST_P (it->w))
2814 it->last_visible_x -= 1;
2815
2816 it->last_visible_y = window_text_bottom_y (w);
2817
2818 /* For mode lines and alike, arrange for the first glyph having a
2819 left box line if the face specifies a box. */
2820 if (base_face_id != DEFAULT_FACE_ID)
2821 {
2822 struct face *face;
2823
2824 it->face_id = remapped_base_face_id;
2825
2826 /* If we have a boxed mode line, make the first character appear
2827 with a left box line. */
2828 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2829 if (face->box != FACE_NO_BOX)
2830 it->start_of_box_run_p = 1;
2831 }
2832
2833 /* If a buffer position was specified, set the iterator there,
2834 getting overlays and face properties from that position. */
2835 if (charpos >= BUF_BEG (current_buffer))
2836 {
2837 it->end_charpos = ZV;
2838 IT_CHARPOS (*it) = charpos;
2839
2840 /* We will rely on `reseat' to set this up properly, via
2841 handle_face_prop. */
2842 it->face_id = it->base_face_id;
2843
2844 /* Compute byte position if not specified. */
2845 if (bytepos < charpos)
2846 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2847 else
2848 IT_BYTEPOS (*it) = bytepos;
2849
2850 it->start = it->current;
2851 /* Do we need to reorder bidirectional text? Not if this is a
2852 unibyte buffer: by definition, none of the single-byte
2853 characters are strong R2L, so no reordering is needed. And
2854 bidi.c doesn't support unibyte buffers anyway. Also, don't
2855 reorder while we are loading loadup.el, since the tables of
2856 character properties needed for reordering are not yet
2857 available. */
2858 it->bidi_p =
2859 NILP (Vpurify_flag)
2860 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2861 && it->multibyte_p;
2862
2863 /* If we are to reorder bidirectional text, init the bidi
2864 iterator. */
2865 if (it->bidi_p)
2866 {
2867 /* Note the paragraph direction that this buffer wants to
2868 use. */
2869 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2870 Qleft_to_right))
2871 it->paragraph_embedding = L2R;
2872 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2873 Qright_to_left))
2874 it->paragraph_embedding = R2L;
2875 else
2876 it->paragraph_embedding = NEUTRAL_DIR;
2877 bidi_unshelve_cache (NULL, 0);
2878 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2879 &it->bidi_it);
2880 }
2881
2882 /* Compute faces etc. */
2883 reseat (it, it->current.pos, 1);
2884 }
2885
2886 CHECK_IT (it);
2887 }
2888
2889
2890 /* Initialize IT for the display of window W with window start POS. */
2891
2892 void
2893 start_display (struct it *it, struct window *w, struct text_pos pos)
2894 {
2895 struct glyph_row *row;
2896 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2897
2898 row = w->desired_matrix->rows + first_vpos;
2899 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2900 it->first_vpos = first_vpos;
2901
2902 /* Don't reseat to previous visible line start if current start
2903 position is in a string or image. */
2904 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2905 {
2906 int start_at_line_beg_p;
2907 int first_y = it->current_y;
2908
2909 /* If window start is not at a line start, skip forward to POS to
2910 get the correct continuation lines width. */
2911 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2912 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2913 if (!start_at_line_beg_p)
2914 {
2915 int new_x;
2916
2917 reseat_at_previous_visible_line_start (it);
2918 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2919
2920 new_x = it->current_x + it->pixel_width;
2921
2922 /* If lines are continued, this line may end in the middle
2923 of a multi-glyph character (e.g. a control character
2924 displayed as \003, or in the middle of an overlay
2925 string). In this case move_it_to above will not have
2926 taken us to the start of the continuation line but to the
2927 end of the continued line. */
2928 if (it->current_x > 0
2929 && it->line_wrap != TRUNCATE /* Lines are continued. */
2930 && (/* And glyph doesn't fit on the line. */
2931 new_x > it->last_visible_x
2932 /* Or it fits exactly and we're on a window
2933 system frame. */
2934 || (new_x == it->last_visible_x
2935 && FRAME_WINDOW_P (it->f)
2936 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2937 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2938 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2939 {
2940 if ((it->current.dpvec_index >= 0
2941 || it->current.overlay_string_index >= 0)
2942 /* If we are on a newline from a display vector or
2943 overlay string, then we are already at the end of
2944 a screen line; no need to go to the next line in
2945 that case, as this line is not really continued.
2946 (If we do go to the next line, C-e will not DTRT.) */
2947 && it->c != '\n')
2948 {
2949 set_iterator_to_next (it, 1);
2950 move_it_in_display_line_to (it, -1, -1, 0);
2951 }
2952
2953 it->continuation_lines_width += it->current_x;
2954 }
2955 /* If the character at POS is displayed via a display
2956 vector, move_it_to above stops at the final glyph of
2957 IT->dpvec. To make the caller redisplay that character
2958 again (a.k.a. start at POS), we need to reset the
2959 dpvec_index to the beginning of IT->dpvec. */
2960 else if (it->current.dpvec_index >= 0)
2961 it->current.dpvec_index = 0;
2962
2963 /* We're starting a new display line, not affected by the
2964 height of the continued line, so clear the appropriate
2965 fields in the iterator structure. */
2966 it->max_ascent = it->max_descent = 0;
2967 it->max_phys_ascent = it->max_phys_descent = 0;
2968
2969 it->current_y = first_y;
2970 it->vpos = 0;
2971 it->current_x = it->hpos = 0;
2972 }
2973 }
2974 }
2975
2976
2977 /* Return 1 if POS is a position in ellipses displayed for invisible
2978 text. W is the window we display, for text property lookup. */
2979
2980 static int
2981 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2982 {
2983 Lisp_Object prop, window;
2984 int ellipses_p = 0;
2985 ptrdiff_t charpos = CHARPOS (pos->pos);
2986
2987 /* If POS specifies a position in a display vector, this might
2988 be for an ellipsis displayed for invisible text. We won't
2989 get the iterator set up for delivering that ellipsis unless
2990 we make sure that it gets aware of the invisible text. */
2991 if (pos->dpvec_index >= 0
2992 && pos->overlay_string_index < 0
2993 && CHARPOS (pos->string_pos) < 0
2994 && charpos > BEGV
2995 && (XSETWINDOW (window, w),
2996 prop = Fget_char_property (make_number (charpos),
2997 Qinvisible, window),
2998 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2999 {
3000 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3001 window);
3002 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3003 }
3004
3005 return ellipses_p;
3006 }
3007
3008
3009 /* Initialize IT for stepping through current_buffer in window W,
3010 starting at position POS that includes overlay string and display
3011 vector/ control character translation position information. Value
3012 is zero if there are overlay strings with newlines at POS. */
3013
3014 static int
3015 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3016 {
3017 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3018 int i, overlay_strings_with_newlines = 0;
3019
3020 /* If POS specifies a position in a display vector, this might
3021 be for an ellipsis displayed for invisible text. We won't
3022 get the iterator set up for delivering that ellipsis unless
3023 we make sure that it gets aware of the invisible text. */
3024 if (in_ellipses_for_invisible_text_p (pos, w))
3025 {
3026 --charpos;
3027 bytepos = 0;
3028 }
3029
3030 /* Keep in mind: the call to reseat in init_iterator skips invisible
3031 text, so we might end up at a position different from POS. This
3032 is only a problem when POS is a row start after a newline and an
3033 overlay starts there with an after-string, and the overlay has an
3034 invisible property. Since we don't skip invisible text in
3035 display_line and elsewhere immediately after consuming the
3036 newline before the row start, such a POS will not be in a string,
3037 but the call to init_iterator below will move us to the
3038 after-string. */
3039 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3040
3041 /* This only scans the current chunk -- it should scan all chunks.
3042 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3043 to 16 in 22.1 to make this a lesser problem. */
3044 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3045 {
3046 const char *s = SSDATA (it->overlay_strings[i]);
3047 const char *e = s + SBYTES (it->overlay_strings[i]);
3048
3049 while (s < e && *s != '\n')
3050 ++s;
3051
3052 if (s < e)
3053 {
3054 overlay_strings_with_newlines = 1;
3055 break;
3056 }
3057 }
3058
3059 /* If position is within an overlay string, set up IT to the right
3060 overlay string. */
3061 if (pos->overlay_string_index >= 0)
3062 {
3063 int relative_index;
3064
3065 /* If the first overlay string happens to have a `display'
3066 property for an image, the iterator will be set up for that
3067 image, and we have to undo that setup first before we can
3068 correct the overlay string index. */
3069 if (it->method == GET_FROM_IMAGE)
3070 pop_it (it);
3071
3072 /* We already have the first chunk of overlay strings in
3073 IT->overlay_strings. Load more until the one for
3074 pos->overlay_string_index is in IT->overlay_strings. */
3075 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3076 {
3077 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3078 it->current.overlay_string_index = 0;
3079 while (n--)
3080 {
3081 load_overlay_strings (it, 0);
3082 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3083 }
3084 }
3085
3086 it->current.overlay_string_index = pos->overlay_string_index;
3087 relative_index = (it->current.overlay_string_index
3088 % OVERLAY_STRING_CHUNK_SIZE);
3089 it->string = it->overlay_strings[relative_index];
3090 eassert (STRINGP (it->string));
3091 it->current.string_pos = pos->string_pos;
3092 it->method = GET_FROM_STRING;
3093 }
3094
3095 if (CHARPOS (pos->string_pos) >= 0)
3096 {
3097 /* Recorded position is not in an overlay string, but in another
3098 string. This can only be a string from a `display' property.
3099 IT should already be filled with that string. */
3100 it->current.string_pos = pos->string_pos;
3101 eassert (STRINGP (it->string));
3102 }
3103
3104 /* Restore position in display vector translations, control
3105 character translations or ellipses. */
3106 if (pos->dpvec_index >= 0)
3107 {
3108 if (it->dpvec == NULL)
3109 get_next_display_element (it);
3110 eassert (it->dpvec && it->current.dpvec_index == 0);
3111 it->current.dpvec_index = pos->dpvec_index;
3112 }
3113
3114 CHECK_IT (it);
3115 return !overlay_strings_with_newlines;
3116 }
3117
3118
3119 /* Initialize IT for stepping through current_buffer in window W
3120 starting at ROW->start. */
3121
3122 static void
3123 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3124 {
3125 init_from_display_pos (it, w, &row->start);
3126 it->start = row->start;
3127 it->continuation_lines_width = row->continuation_lines_width;
3128 CHECK_IT (it);
3129 }
3130
3131
3132 /* Initialize IT for stepping through current_buffer in window W
3133 starting in the line following ROW, i.e. starting at ROW->end.
3134 Value is zero if there are overlay strings with newlines at ROW's
3135 end position. */
3136
3137 static int
3138 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3139 {
3140 int success = 0;
3141
3142 if (init_from_display_pos (it, w, &row->end))
3143 {
3144 if (row->continued_p)
3145 it->continuation_lines_width
3146 = row->continuation_lines_width + row->pixel_width;
3147 CHECK_IT (it);
3148 success = 1;
3149 }
3150
3151 return success;
3152 }
3153
3154
3155
3156 \f
3157 /***********************************************************************
3158 Text properties
3159 ***********************************************************************/
3160
3161 /* Called when IT reaches IT->stop_charpos. Handle text property and
3162 overlay changes. Set IT->stop_charpos to the next position where
3163 to stop. */
3164
3165 static void
3166 handle_stop (struct it *it)
3167 {
3168 enum prop_handled handled;
3169 int handle_overlay_change_p;
3170 struct props *p;
3171
3172 it->dpvec = NULL;
3173 it->current.dpvec_index = -1;
3174 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3175 it->ignore_overlay_strings_at_pos_p = 0;
3176 it->ellipsis_p = 0;
3177
3178 /* Use face of preceding text for ellipsis (if invisible) */
3179 if (it->selective_display_ellipsis_p)
3180 it->saved_face_id = it->face_id;
3181
3182 do
3183 {
3184 handled = HANDLED_NORMALLY;
3185
3186 /* Call text property handlers. */
3187 for (p = it_props; p->handler; ++p)
3188 {
3189 handled = p->handler (it);
3190
3191 if (handled == HANDLED_RECOMPUTE_PROPS)
3192 break;
3193 else if (handled == HANDLED_RETURN)
3194 {
3195 /* We still want to show before and after strings from
3196 overlays even if the actual buffer text is replaced. */
3197 if (!handle_overlay_change_p
3198 || it->sp > 1
3199 /* Don't call get_overlay_strings_1 if we already
3200 have overlay strings loaded, because doing so
3201 will load them again and push the iterator state
3202 onto the stack one more time, which is not
3203 expected by the rest of the code that processes
3204 overlay strings. */
3205 || (it->current.overlay_string_index < 0
3206 ? !get_overlay_strings_1 (it, 0, 0)
3207 : 0))
3208 {
3209 if (it->ellipsis_p)
3210 setup_for_ellipsis (it, 0);
3211 /* When handling a display spec, we might load an
3212 empty string. In that case, discard it here. We
3213 used to discard it in handle_single_display_spec,
3214 but that causes get_overlay_strings_1, above, to
3215 ignore overlay strings that we must check. */
3216 if (STRINGP (it->string) && !SCHARS (it->string))
3217 pop_it (it);
3218 return;
3219 }
3220 else if (STRINGP (it->string) && !SCHARS (it->string))
3221 pop_it (it);
3222 else
3223 {
3224 it->ignore_overlay_strings_at_pos_p = 1;
3225 it->string_from_display_prop_p = 0;
3226 it->from_disp_prop_p = 0;
3227 handle_overlay_change_p = 0;
3228 }
3229 handled = HANDLED_RECOMPUTE_PROPS;
3230 break;
3231 }
3232 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3233 handle_overlay_change_p = 0;
3234 }
3235
3236 if (handled != HANDLED_RECOMPUTE_PROPS)
3237 {
3238 /* Don't check for overlay strings below when set to deliver
3239 characters from a display vector. */
3240 if (it->method == GET_FROM_DISPLAY_VECTOR)
3241 handle_overlay_change_p = 0;
3242
3243 /* Handle overlay changes.
3244 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3245 if it finds overlays. */
3246 if (handle_overlay_change_p)
3247 handled = handle_overlay_change (it);
3248 }
3249
3250 if (it->ellipsis_p)
3251 {
3252 setup_for_ellipsis (it, 0);
3253 break;
3254 }
3255 }
3256 while (handled == HANDLED_RECOMPUTE_PROPS);
3257
3258 /* Determine where to stop next. */
3259 if (handled == HANDLED_NORMALLY)
3260 compute_stop_pos (it);
3261 }
3262
3263
3264 /* Compute IT->stop_charpos from text property and overlay change
3265 information for IT's current position. */
3266
3267 static void
3268 compute_stop_pos (struct it *it)
3269 {
3270 register INTERVAL iv, next_iv;
3271 Lisp_Object object, limit, position;
3272 ptrdiff_t charpos, bytepos;
3273
3274 if (STRINGP (it->string))
3275 {
3276 /* Strings are usually short, so don't limit the search for
3277 properties. */
3278 it->stop_charpos = it->end_charpos;
3279 object = it->string;
3280 limit = Qnil;
3281 charpos = IT_STRING_CHARPOS (*it);
3282 bytepos = IT_STRING_BYTEPOS (*it);
3283 }
3284 else
3285 {
3286 ptrdiff_t pos;
3287
3288 /* If end_charpos is out of range for some reason, such as a
3289 misbehaving display function, rationalize it (Bug#5984). */
3290 if (it->end_charpos > ZV)
3291 it->end_charpos = ZV;
3292 it->stop_charpos = it->end_charpos;
3293
3294 /* If next overlay change is in front of the current stop pos
3295 (which is IT->end_charpos), stop there. Note: value of
3296 next_overlay_change is point-max if no overlay change
3297 follows. */
3298 charpos = IT_CHARPOS (*it);
3299 bytepos = IT_BYTEPOS (*it);
3300 pos = next_overlay_change (charpos);
3301 if (pos < it->stop_charpos)
3302 it->stop_charpos = pos;
3303
3304 /* If showing the region, we have to stop at the region
3305 start or end because the face might change there. */
3306 if (it->region_beg_charpos > 0)
3307 {
3308 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3309 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3310 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3311 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3312 }
3313
3314 /* Set up variables for computing the stop position from text
3315 property changes. */
3316 XSETBUFFER (object, current_buffer);
3317 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3318 }
3319
3320 /* Get the interval containing IT's position. Value is a null
3321 interval if there isn't such an interval. */
3322 position = make_number (charpos);
3323 iv = validate_interval_range (object, &position, &position, 0);
3324 if (!NULL_INTERVAL_P (iv))
3325 {
3326 Lisp_Object values_here[LAST_PROP_IDX];
3327 struct props *p;
3328
3329 /* Get properties here. */
3330 for (p = it_props; p->handler; ++p)
3331 values_here[p->idx] = textget (iv->plist, *p->name);
3332
3333 /* Look for an interval following iv that has different
3334 properties. */
3335 for (next_iv = next_interval (iv);
3336 (!NULL_INTERVAL_P (next_iv)
3337 && (NILP (limit)
3338 || XFASTINT (limit) > next_iv->position));
3339 next_iv = next_interval (next_iv))
3340 {
3341 for (p = it_props; p->handler; ++p)
3342 {
3343 Lisp_Object new_value;
3344
3345 new_value = textget (next_iv->plist, *p->name);
3346 if (!EQ (values_here[p->idx], new_value))
3347 break;
3348 }
3349
3350 if (p->handler)
3351 break;
3352 }
3353
3354 if (!NULL_INTERVAL_P (next_iv))
3355 {
3356 if (INTEGERP (limit)
3357 && next_iv->position >= XFASTINT (limit))
3358 /* No text property change up to limit. */
3359 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3360 else
3361 /* Text properties change in next_iv. */
3362 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3363 }
3364 }
3365
3366 if (it->cmp_it.id < 0)
3367 {
3368 ptrdiff_t stoppos = it->end_charpos;
3369
3370 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3371 stoppos = -1;
3372 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3373 stoppos, it->string);
3374 }
3375
3376 eassert (STRINGP (it->string)
3377 || (it->stop_charpos >= BEGV
3378 && it->stop_charpos >= IT_CHARPOS (*it)));
3379 }
3380
3381
3382 /* Return the position of the next overlay change after POS in
3383 current_buffer. Value is point-max if no overlay change
3384 follows. This is like `next-overlay-change' but doesn't use
3385 xmalloc. */
3386
3387 static ptrdiff_t
3388 next_overlay_change (ptrdiff_t pos)
3389 {
3390 ptrdiff_t i, noverlays;
3391 ptrdiff_t endpos;
3392 Lisp_Object *overlays;
3393
3394 /* Get all overlays at the given position. */
3395 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3396
3397 /* If any of these overlays ends before endpos,
3398 use its ending point instead. */
3399 for (i = 0; i < noverlays; ++i)
3400 {
3401 Lisp_Object oend;
3402 ptrdiff_t oendpos;
3403
3404 oend = OVERLAY_END (overlays[i]);
3405 oendpos = OVERLAY_POSITION (oend);
3406 endpos = min (endpos, oendpos);
3407 }
3408
3409 return endpos;
3410 }
3411
3412 /* How many characters forward to search for a display property or
3413 display string. Searching too far forward makes the bidi display
3414 sluggish, especially in small windows. */
3415 #define MAX_DISP_SCAN 250
3416
3417 /* Return the character position of a display string at or after
3418 position specified by POSITION. If no display string exists at or
3419 after POSITION, return ZV. A display string is either an overlay
3420 with `display' property whose value is a string, or a `display'
3421 text property whose value is a string. STRING is data about the
3422 string to iterate; if STRING->lstring is nil, we are iterating a
3423 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3424 on a GUI frame. DISP_PROP is set to zero if we searched
3425 MAX_DISP_SCAN characters forward without finding any display
3426 strings, non-zero otherwise. It is set to 2 if the display string
3427 uses any kind of `(space ...)' spec that will produce a stretch of
3428 white space in the text area. */
3429 ptrdiff_t
3430 compute_display_string_pos (struct text_pos *position,
3431 struct bidi_string_data *string,
3432 int frame_window_p, int *disp_prop)
3433 {
3434 /* OBJECT = nil means current buffer. */
3435 Lisp_Object object =
3436 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3437 Lisp_Object pos, spec, limpos;
3438 int string_p = (string && (STRINGP (string->lstring) || string->s));
3439 ptrdiff_t eob = string_p ? string->schars : ZV;
3440 ptrdiff_t begb = string_p ? 0 : BEGV;
3441 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3442 ptrdiff_t lim =
3443 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3444 struct text_pos tpos;
3445 int rv = 0;
3446
3447 *disp_prop = 1;
3448
3449 if (charpos >= eob
3450 /* We don't support display properties whose values are strings
3451 that have display string properties. */
3452 || string->from_disp_str
3453 /* C strings cannot have display properties. */
3454 || (string->s && !STRINGP (object)))
3455 {
3456 *disp_prop = 0;
3457 return eob;
3458 }
3459
3460 /* If the character at CHARPOS is where the display string begins,
3461 return CHARPOS. */
3462 pos = make_number (charpos);
3463 if (STRINGP (object))
3464 bufpos = string->bufpos;
3465 else
3466 bufpos = charpos;
3467 tpos = *position;
3468 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3469 && (charpos <= begb
3470 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3471 object),
3472 spec))
3473 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3474 frame_window_p)))
3475 {
3476 if (rv == 2)
3477 *disp_prop = 2;
3478 return charpos;
3479 }
3480
3481 /* Look forward for the first character with a `display' property
3482 that will replace the underlying text when displayed. */
3483 limpos = make_number (lim);
3484 do {
3485 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3486 CHARPOS (tpos) = XFASTINT (pos);
3487 if (CHARPOS (tpos) >= lim)
3488 {
3489 *disp_prop = 0;
3490 break;
3491 }
3492 if (STRINGP (object))
3493 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3494 else
3495 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3496 spec = Fget_char_property (pos, Qdisplay, object);
3497 if (!STRINGP (object))
3498 bufpos = CHARPOS (tpos);
3499 } while (NILP (spec)
3500 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3501 bufpos, frame_window_p)));
3502 if (rv == 2)
3503 *disp_prop = 2;
3504
3505 return CHARPOS (tpos);
3506 }
3507
3508 /* Return the character position of the end of the display string that
3509 started at CHARPOS. If there's no display string at CHARPOS,
3510 return -1. A display string is either an overlay with `display'
3511 property whose value is a string or a `display' text property whose
3512 value is a string. */
3513 ptrdiff_t
3514 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3515 {
3516 /* OBJECT = nil means current buffer. */
3517 Lisp_Object object =
3518 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3519 Lisp_Object pos = make_number (charpos);
3520 ptrdiff_t eob =
3521 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3522
3523 if (charpos >= eob || (string->s && !STRINGP (object)))
3524 return eob;
3525
3526 /* It could happen that the display property or overlay was removed
3527 since we found it in compute_display_string_pos above. One way
3528 this can happen is if JIT font-lock was called (through
3529 handle_fontified_prop), and jit-lock-functions remove text
3530 properties or overlays from the portion of buffer that includes
3531 CHARPOS. Muse mode is known to do that, for example. In this
3532 case, we return -1 to the caller, to signal that no display
3533 string is actually present at CHARPOS. See bidi_fetch_char for
3534 how this is handled.
3535
3536 An alternative would be to never look for display properties past
3537 it->stop_charpos. But neither compute_display_string_pos nor
3538 bidi_fetch_char that calls it know or care where the next
3539 stop_charpos is. */
3540 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3541 return -1;
3542
3543 /* Look forward for the first character where the `display' property
3544 changes. */
3545 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3546
3547 return XFASTINT (pos);
3548 }
3549
3550
3551 \f
3552 /***********************************************************************
3553 Fontification
3554 ***********************************************************************/
3555
3556 /* Handle changes in the `fontified' property of the current buffer by
3557 calling hook functions from Qfontification_functions to fontify
3558 regions of text. */
3559
3560 static enum prop_handled
3561 handle_fontified_prop (struct it *it)
3562 {
3563 Lisp_Object prop, pos;
3564 enum prop_handled handled = HANDLED_NORMALLY;
3565
3566 if (!NILP (Vmemory_full))
3567 return handled;
3568
3569 /* Get the value of the `fontified' property at IT's current buffer
3570 position. (The `fontified' property doesn't have a special
3571 meaning in strings.) If the value is nil, call functions from
3572 Qfontification_functions. */
3573 if (!STRINGP (it->string)
3574 && it->s == NULL
3575 && !NILP (Vfontification_functions)
3576 && !NILP (Vrun_hooks)
3577 && (pos = make_number (IT_CHARPOS (*it)),
3578 prop = Fget_char_property (pos, Qfontified, Qnil),
3579 /* Ignore the special cased nil value always present at EOB since
3580 no amount of fontifying will be able to change it. */
3581 NILP (prop) && IT_CHARPOS (*it) < Z))
3582 {
3583 ptrdiff_t count = SPECPDL_INDEX ();
3584 Lisp_Object val;
3585 struct buffer *obuf = current_buffer;
3586 int begv = BEGV, zv = ZV;
3587 int old_clip_changed = current_buffer->clip_changed;
3588
3589 val = Vfontification_functions;
3590 specbind (Qfontification_functions, Qnil);
3591
3592 eassert (it->end_charpos == ZV);
3593
3594 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3595 safe_call1 (val, pos);
3596 else
3597 {
3598 Lisp_Object fns, fn;
3599 struct gcpro gcpro1, gcpro2;
3600
3601 fns = Qnil;
3602 GCPRO2 (val, fns);
3603
3604 for (; CONSP (val); val = XCDR (val))
3605 {
3606 fn = XCAR (val);
3607
3608 if (EQ (fn, Qt))
3609 {
3610 /* A value of t indicates this hook has a local
3611 binding; it means to run the global binding too.
3612 In a global value, t should not occur. If it
3613 does, we must ignore it to avoid an endless
3614 loop. */
3615 for (fns = Fdefault_value (Qfontification_functions);
3616 CONSP (fns);
3617 fns = XCDR (fns))
3618 {
3619 fn = XCAR (fns);
3620 if (!EQ (fn, Qt))
3621 safe_call1 (fn, pos);
3622 }
3623 }
3624 else
3625 safe_call1 (fn, pos);
3626 }
3627
3628 UNGCPRO;
3629 }
3630
3631 unbind_to (count, Qnil);
3632
3633 /* Fontification functions routinely call `save-restriction'.
3634 Normally, this tags clip_changed, which can confuse redisplay
3635 (see discussion in Bug#6671). Since we don't perform any
3636 special handling of fontification changes in the case where
3637 `save-restriction' isn't called, there's no point doing so in
3638 this case either. So, if the buffer's restrictions are
3639 actually left unchanged, reset clip_changed. */
3640 if (obuf == current_buffer)
3641 {
3642 if (begv == BEGV && zv == ZV)
3643 current_buffer->clip_changed = old_clip_changed;
3644 }
3645 /* There isn't much we can reasonably do to protect against
3646 misbehaving fontification, but here's a fig leaf. */
3647 else if (!NILP (BVAR (obuf, name)))
3648 set_buffer_internal_1 (obuf);
3649
3650 /* The fontification code may have added/removed text.
3651 It could do even a lot worse, but let's at least protect against
3652 the most obvious case where only the text past `pos' gets changed',
3653 as is/was done in grep.el where some escapes sequences are turned
3654 into face properties (bug#7876). */
3655 it->end_charpos = ZV;
3656
3657 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3658 something. This avoids an endless loop if they failed to
3659 fontify the text for which reason ever. */
3660 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3661 handled = HANDLED_RECOMPUTE_PROPS;
3662 }
3663
3664 return handled;
3665 }
3666
3667
3668 \f
3669 /***********************************************************************
3670 Faces
3671 ***********************************************************************/
3672
3673 /* Set up iterator IT from face properties at its current position.
3674 Called from handle_stop. */
3675
3676 static enum prop_handled
3677 handle_face_prop (struct it *it)
3678 {
3679 int new_face_id;
3680 ptrdiff_t next_stop;
3681
3682 if (!STRINGP (it->string))
3683 {
3684 new_face_id
3685 = face_at_buffer_position (it->w,
3686 IT_CHARPOS (*it),
3687 it->region_beg_charpos,
3688 it->region_end_charpos,
3689 &next_stop,
3690 (IT_CHARPOS (*it)
3691 + TEXT_PROP_DISTANCE_LIMIT),
3692 0, it->base_face_id);
3693
3694 /* Is this a start of a run of characters with box face?
3695 Caveat: this can be called for a freshly initialized
3696 iterator; face_id is -1 in this case. We know that the new
3697 face will not change until limit, i.e. if the new face has a
3698 box, all characters up to limit will have one. But, as
3699 usual, we don't know whether limit is really the end. */
3700 if (new_face_id != it->face_id)
3701 {
3702 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3703
3704 /* If new face has a box but old face has not, this is
3705 the start of a run of characters with box, i.e. it has
3706 a shadow on the left side. The value of face_id of the
3707 iterator will be -1 if this is the initial call that gets
3708 the face. In this case, we have to look in front of IT's
3709 position and see whether there is a face != new_face_id. */
3710 it->start_of_box_run_p
3711 = (new_face->box != FACE_NO_BOX
3712 && (it->face_id >= 0
3713 || IT_CHARPOS (*it) == BEG
3714 || new_face_id != face_before_it_pos (it)));
3715 it->face_box_p = new_face->box != FACE_NO_BOX;
3716 }
3717 }
3718 else
3719 {
3720 int base_face_id;
3721 ptrdiff_t bufpos;
3722 int i;
3723 Lisp_Object from_overlay
3724 = (it->current.overlay_string_index >= 0
3725 ? it->string_overlays[it->current.overlay_string_index
3726 % OVERLAY_STRING_CHUNK_SIZE]
3727 : Qnil);
3728
3729 /* See if we got to this string directly or indirectly from
3730 an overlay property. That includes the before-string or
3731 after-string of an overlay, strings in display properties
3732 provided by an overlay, their text properties, etc.
3733
3734 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3735 if (! NILP (from_overlay))
3736 for (i = it->sp - 1; i >= 0; i--)
3737 {
3738 if (it->stack[i].current.overlay_string_index >= 0)
3739 from_overlay
3740 = it->string_overlays[it->stack[i].current.overlay_string_index
3741 % OVERLAY_STRING_CHUNK_SIZE];
3742 else if (! NILP (it->stack[i].from_overlay))
3743 from_overlay = it->stack[i].from_overlay;
3744
3745 if (!NILP (from_overlay))
3746 break;
3747 }
3748
3749 if (! NILP (from_overlay))
3750 {
3751 bufpos = IT_CHARPOS (*it);
3752 /* For a string from an overlay, the base face depends
3753 only on text properties and ignores overlays. */
3754 base_face_id
3755 = face_for_overlay_string (it->w,
3756 IT_CHARPOS (*it),
3757 it->region_beg_charpos,
3758 it->region_end_charpos,
3759 &next_stop,
3760 (IT_CHARPOS (*it)
3761 + TEXT_PROP_DISTANCE_LIMIT),
3762 0,
3763 from_overlay);
3764 }
3765 else
3766 {
3767 bufpos = 0;
3768
3769 /* For strings from a `display' property, use the face at
3770 IT's current buffer position as the base face to merge
3771 with, so that overlay strings appear in the same face as
3772 surrounding text, unless they specify their own
3773 faces. */
3774 base_face_id = it->string_from_prefix_prop_p
3775 ? DEFAULT_FACE_ID
3776 : underlying_face_id (it);
3777 }
3778
3779 new_face_id = face_at_string_position (it->w,
3780 it->string,
3781 IT_STRING_CHARPOS (*it),
3782 bufpos,
3783 it->region_beg_charpos,
3784 it->region_end_charpos,
3785 &next_stop,
3786 base_face_id, 0);
3787
3788 /* Is this a start of a run of characters with box? Caveat:
3789 this can be called for a freshly allocated iterator; face_id
3790 is -1 is this case. We know that the new face will not
3791 change until the next check pos, i.e. if the new face has a
3792 box, all characters up to that position will have a
3793 box. But, as usual, we don't know whether that position
3794 is really the end. */
3795 if (new_face_id != it->face_id)
3796 {
3797 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3798 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3799
3800 /* If new face has a box but old face hasn't, this is the
3801 start of a run of characters with box, i.e. it has a
3802 shadow on the left side. */
3803 it->start_of_box_run_p
3804 = new_face->box && (old_face == NULL || !old_face->box);
3805 it->face_box_p = new_face->box != FACE_NO_BOX;
3806 }
3807 }
3808
3809 it->face_id = new_face_id;
3810 return HANDLED_NORMALLY;
3811 }
3812
3813
3814 /* Return the ID of the face ``underlying'' IT's current position,
3815 which is in a string. If the iterator is associated with a
3816 buffer, return the face at IT's current buffer position.
3817 Otherwise, use the iterator's base_face_id. */
3818
3819 static int
3820 underlying_face_id (struct it *it)
3821 {
3822 int face_id = it->base_face_id, i;
3823
3824 eassert (STRINGP (it->string));
3825
3826 for (i = it->sp - 1; i >= 0; --i)
3827 if (NILP (it->stack[i].string))
3828 face_id = it->stack[i].face_id;
3829
3830 return face_id;
3831 }
3832
3833
3834 /* Compute the face one character before or after the current position
3835 of IT, in the visual order. BEFORE_P non-zero means get the face
3836 in front (to the left in L2R paragraphs, to the right in R2L
3837 paragraphs) of IT's screen position. Value is the ID of the face. */
3838
3839 static int
3840 face_before_or_after_it_pos (struct it *it, int before_p)
3841 {
3842 int face_id, limit;
3843 ptrdiff_t next_check_charpos;
3844 struct it it_copy;
3845 void *it_copy_data = NULL;
3846
3847 eassert (it->s == NULL);
3848
3849 if (STRINGP (it->string))
3850 {
3851 ptrdiff_t bufpos, charpos;
3852 int base_face_id;
3853
3854 /* No face change past the end of the string (for the case
3855 we are padding with spaces). No face change before the
3856 string start. */
3857 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3858 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3859 return it->face_id;
3860
3861 if (!it->bidi_p)
3862 {
3863 /* Set charpos to the position before or after IT's current
3864 position, in the logical order, which in the non-bidi
3865 case is the same as the visual order. */
3866 if (before_p)
3867 charpos = IT_STRING_CHARPOS (*it) - 1;
3868 else if (it->what == IT_COMPOSITION)
3869 /* For composition, we must check the character after the
3870 composition. */
3871 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3872 else
3873 charpos = IT_STRING_CHARPOS (*it) + 1;
3874 }
3875 else
3876 {
3877 if (before_p)
3878 {
3879 /* With bidi iteration, the character before the current
3880 in the visual order cannot be found by simple
3881 iteration, because "reverse" reordering is not
3882 supported. Instead, we need to use the move_it_*
3883 family of functions. */
3884 /* Ignore face changes before the first visible
3885 character on this display line. */
3886 if (it->current_x <= it->first_visible_x)
3887 return it->face_id;
3888 SAVE_IT (it_copy, *it, it_copy_data);
3889 /* Implementation note: Since move_it_in_display_line
3890 works in the iterator geometry, and thinks the first
3891 character is always the leftmost, even in R2L lines,
3892 we don't need to distinguish between the R2L and L2R
3893 cases here. */
3894 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3895 it_copy.current_x - 1, MOVE_TO_X);
3896 charpos = IT_STRING_CHARPOS (it_copy);
3897 RESTORE_IT (it, it, it_copy_data);
3898 }
3899 else
3900 {
3901 /* Set charpos to the string position of the character
3902 that comes after IT's current position in the visual
3903 order. */
3904 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3905
3906 it_copy = *it;
3907 while (n--)
3908 bidi_move_to_visually_next (&it_copy.bidi_it);
3909
3910 charpos = it_copy.bidi_it.charpos;
3911 }
3912 }
3913 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3914
3915 if (it->current.overlay_string_index >= 0)
3916 bufpos = IT_CHARPOS (*it);
3917 else
3918 bufpos = 0;
3919
3920 base_face_id = underlying_face_id (it);
3921
3922 /* Get the face for ASCII, or unibyte. */
3923 face_id = face_at_string_position (it->w,
3924 it->string,
3925 charpos,
3926 bufpos,
3927 it->region_beg_charpos,
3928 it->region_end_charpos,
3929 &next_check_charpos,
3930 base_face_id, 0);
3931
3932 /* Correct the face for charsets different from ASCII. Do it
3933 for the multibyte case only. The face returned above is
3934 suitable for unibyte text if IT->string is unibyte. */
3935 if (STRING_MULTIBYTE (it->string))
3936 {
3937 struct text_pos pos1 = string_pos (charpos, it->string);
3938 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3939 int c, len;
3940 struct face *face = FACE_FROM_ID (it->f, face_id);
3941
3942 c = string_char_and_length (p, &len);
3943 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3944 }
3945 }
3946 else
3947 {
3948 struct text_pos pos;
3949
3950 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3951 || (IT_CHARPOS (*it) <= BEGV && before_p))
3952 return it->face_id;
3953
3954 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3955 pos = it->current.pos;
3956
3957 if (!it->bidi_p)
3958 {
3959 if (before_p)
3960 DEC_TEXT_POS (pos, it->multibyte_p);
3961 else
3962 {
3963 if (it->what == IT_COMPOSITION)
3964 {
3965 /* For composition, we must check the position after
3966 the composition. */
3967 pos.charpos += it->cmp_it.nchars;
3968 pos.bytepos += it->len;
3969 }
3970 else
3971 INC_TEXT_POS (pos, it->multibyte_p);
3972 }
3973 }
3974 else
3975 {
3976 if (before_p)
3977 {
3978 /* With bidi iteration, the character before the current
3979 in the visual order cannot be found by simple
3980 iteration, because "reverse" reordering is not
3981 supported. Instead, we need to use the move_it_*
3982 family of functions. */
3983 /* Ignore face changes before the first visible
3984 character on this display line. */
3985 if (it->current_x <= it->first_visible_x)
3986 return it->face_id;
3987 SAVE_IT (it_copy, *it, it_copy_data);
3988 /* Implementation note: Since move_it_in_display_line
3989 works in the iterator geometry, and thinks the first
3990 character is always the leftmost, even in R2L lines,
3991 we don't need to distinguish between the R2L and L2R
3992 cases here. */
3993 move_it_in_display_line (&it_copy, ZV,
3994 it_copy.current_x - 1, MOVE_TO_X);
3995 pos = it_copy.current.pos;
3996 RESTORE_IT (it, it, it_copy_data);
3997 }
3998 else
3999 {
4000 /* Set charpos to the buffer position of the character
4001 that comes after IT's current position in the visual
4002 order. */
4003 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4004
4005 it_copy = *it;
4006 while (n--)
4007 bidi_move_to_visually_next (&it_copy.bidi_it);
4008
4009 SET_TEXT_POS (pos,
4010 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4011 }
4012 }
4013 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4014
4015 /* Determine face for CHARSET_ASCII, or unibyte. */
4016 face_id = face_at_buffer_position (it->w,
4017 CHARPOS (pos),
4018 it->region_beg_charpos,
4019 it->region_end_charpos,
4020 &next_check_charpos,
4021 limit, 0, -1);
4022
4023 /* Correct the face for charsets different from ASCII. Do it
4024 for the multibyte case only. The face returned above is
4025 suitable for unibyte text if current_buffer is unibyte. */
4026 if (it->multibyte_p)
4027 {
4028 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4029 struct face *face = FACE_FROM_ID (it->f, face_id);
4030 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4031 }
4032 }
4033
4034 return face_id;
4035 }
4036
4037
4038 \f
4039 /***********************************************************************
4040 Invisible text
4041 ***********************************************************************/
4042
4043 /* Set up iterator IT from invisible properties at its current
4044 position. Called from handle_stop. */
4045
4046 static enum prop_handled
4047 handle_invisible_prop (struct it *it)
4048 {
4049 enum prop_handled handled = HANDLED_NORMALLY;
4050
4051 if (STRINGP (it->string))
4052 {
4053 Lisp_Object prop, end_charpos, limit, charpos;
4054
4055 /* Get the value of the invisible text property at the
4056 current position. Value will be nil if there is no such
4057 property. */
4058 charpos = make_number (IT_STRING_CHARPOS (*it));
4059 prop = Fget_text_property (charpos, Qinvisible, it->string);
4060
4061 if (!NILP (prop)
4062 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4063 {
4064 ptrdiff_t endpos;
4065
4066 handled = HANDLED_RECOMPUTE_PROPS;
4067
4068 /* Get the position at which the next change of the
4069 invisible text property can be found in IT->string.
4070 Value will be nil if the property value is the same for
4071 all the rest of IT->string. */
4072 XSETINT (limit, SCHARS (it->string));
4073 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4074 it->string, limit);
4075
4076 /* Text at current position is invisible. The next
4077 change in the property is at position end_charpos.
4078 Move IT's current position to that position. */
4079 if (INTEGERP (end_charpos)
4080 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4081 {
4082 struct text_pos old;
4083 ptrdiff_t oldpos;
4084
4085 old = it->current.string_pos;
4086 oldpos = CHARPOS (old);
4087 if (it->bidi_p)
4088 {
4089 if (it->bidi_it.first_elt
4090 && it->bidi_it.charpos < SCHARS (it->string))
4091 bidi_paragraph_init (it->paragraph_embedding,
4092 &it->bidi_it, 1);
4093 /* Bidi-iterate out of the invisible text. */
4094 do
4095 {
4096 bidi_move_to_visually_next (&it->bidi_it);
4097 }
4098 while (oldpos <= it->bidi_it.charpos
4099 && it->bidi_it.charpos < endpos);
4100
4101 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4102 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4103 if (IT_CHARPOS (*it) >= endpos)
4104 it->prev_stop = endpos;
4105 }
4106 else
4107 {
4108 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4109 compute_string_pos (&it->current.string_pos, old, it->string);
4110 }
4111 }
4112 else
4113 {
4114 /* The rest of the string is invisible. If this is an
4115 overlay string, proceed with the next overlay string
4116 or whatever comes and return a character from there. */
4117 if (it->current.overlay_string_index >= 0)
4118 {
4119 next_overlay_string (it);
4120 /* Don't check for overlay strings when we just
4121 finished processing them. */
4122 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4123 }
4124 else
4125 {
4126 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4127 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4128 }
4129 }
4130 }
4131 }
4132 else
4133 {
4134 int invis_p;
4135 ptrdiff_t newpos, next_stop, start_charpos, tem;
4136 Lisp_Object pos, prop, overlay;
4137
4138 /* First of all, is there invisible text at this position? */
4139 tem = start_charpos = IT_CHARPOS (*it);
4140 pos = make_number (tem);
4141 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4142 &overlay);
4143 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4144
4145 /* If we are on invisible text, skip over it. */
4146 if (invis_p && start_charpos < it->end_charpos)
4147 {
4148 /* Record whether we have to display an ellipsis for the
4149 invisible text. */
4150 int display_ellipsis_p = invis_p == 2;
4151
4152 handled = HANDLED_RECOMPUTE_PROPS;
4153
4154 /* Loop skipping over invisible text. The loop is left at
4155 ZV or with IT on the first char being visible again. */
4156 do
4157 {
4158 /* Try to skip some invisible text. Return value is the
4159 position reached which can be equal to where we start
4160 if there is nothing invisible there. This skips both
4161 over invisible text properties and overlays with
4162 invisible property. */
4163 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4164
4165 /* If we skipped nothing at all we weren't at invisible
4166 text in the first place. If everything to the end of
4167 the buffer was skipped, end the loop. */
4168 if (newpos == tem || newpos >= ZV)
4169 invis_p = 0;
4170 else
4171 {
4172 /* We skipped some characters but not necessarily
4173 all there are. Check if we ended up on visible
4174 text. Fget_char_property returns the property of
4175 the char before the given position, i.e. if we
4176 get invis_p = 0, this means that the char at
4177 newpos is visible. */
4178 pos = make_number (newpos);
4179 prop = Fget_char_property (pos, Qinvisible, it->window);
4180 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4181 }
4182
4183 /* If we ended up on invisible text, proceed to
4184 skip starting with next_stop. */
4185 if (invis_p)
4186 tem = next_stop;
4187
4188 /* If there are adjacent invisible texts, don't lose the
4189 second one's ellipsis. */
4190 if (invis_p == 2)
4191 display_ellipsis_p = 1;
4192 }
4193 while (invis_p);
4194
4195 /* The position newpos is now either ZV or on visible text. */
4196 if (it->bidi_p)
4197 {
4198 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4199 int on_newline =
4200 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4201 int after_newline =
4202 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4203
4204 /* If the invisible text ends on a newline or on a
4205 character after a newline, we can avoid the costly,
4206 character by character, bidi iteration to NEWPOS, and
4207 instead simply reseat the iterator there. That's
4208 because all bidi reordering information is tossed at
4209 the newline. This is a big win for modes that hide
4210 complete lines, like Outline, Org, etc. */
4211 if (on_newline || after_newline)
4212 {
4213 struct text_pos tpos;
4214 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4215
4216 SET_TEXT_POS (tpos, newpos, bpos);
4217 reseat_1 (it, tpos, 0);
4218 /* If we reseat on a newline/ZV, we need to prep the
4219 bidi iterator for advancing to the next character
4220 after the newline/EOB, keeping the current paragraph
4221 direction (so that PRODUCE_GLYPHS does TRT wrt
4222 prepending/appending glyphs to a glyph row). */
4223 if (on_newline)
4224 {
4225 it->bidi_it.first_elt = 0;
4226 it->bidi_it.paragraph_dir = pdir;
4227 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4228 it->bidi_it.nchars = 1;
4229 it->bidi_it.ch_len = 1;
4230 }
4231 }
4232 else /* Must use the slow method. */
4233 {
4234 /* With bidi iteration, the region of invisible text
4235 could start and/or end in the middle of a
4236 non-base embedding level. Therefore, we need to
4237 skip invisible text using the bidi iterator,
4238 starting at IT's current position, until we find
4239 ourselves outside of the invisible text.
4240 Skipping invisible text _after_ bidi iteration
4241 avoids affecting the visual order of the
4242 displayed text when invisible properties are
4243 added or removed. */
4244 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4245 {
4246 /* If we were `reseat'ed to a new paragraph,
4247 determine the paragraph base direction. We
4248 need to do it now because
4249 next_element_from_buffer may not have a
4250 chance to do it, if we are going to skip any
4251 text at the beginning, which resets the
4252 FIRST_ELT flag. */
4253 bidi_paragraph_init (it->paragraph_embedding,
4254 &it->bidi_it, 1);
4255 }
4256 do
4257 {
4258 bidi_move_to_visually_next (&it->bidi_it);
4259 }
4260 while (it->stop_charpos <= it->bidi_it.charpos
4261 && it->bidi_it.charpos < newpos);
4262 IT_CHARPOS (*it) = it->bidi_it.charpos;
4263 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4264 /* If we overstepped NEWPOS, record its position in
4265 the iterator, so that we skip invisible text if
4266 later the bidi iteration lands us in the
4267 invisible region again. */
4268 if (IT_CHARPOS (*it) >= newpos)
4269 it->prev_stop = newpos;
4270 }
4271 }
4272 else
4273 {
4274 IT_CHARPOS (*it) = newpos;
4275 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4276 }
4277
4278 /* If there are before-strings at the start of invisible
4279 text, and the text is invisible because of a text
4280 property, arrange to show before-strings because 20.x did
4281 it that way. (If the text is invisible because of an
4282 overlay property instead of a text property, this is
4283 already handled in the overlay code.) */
4284 if (NILP (overlay)
4285 && get_overlay_strings (it, it->stop_charpos))
4286 {
4287 handled = HANDLED_RECOMPUTE_PROPS;
4288 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4289 }
4290 else if (display_ellipsis_p)
4291 {
4292 /* Make sure that the glyphs of the ellipsis will get
4293 correct `charpos' values. If we would not update
4294 it->position here, the glyphs would belong to the
4295 last visible character _before_ the invisible
4296 text, which confuses `set_cursor_from_row'.
4297
4298 We use the last invisible position instead of the
4299 first because this way the cursor is always drawn on
4300 the first "." of the ellipsis, whenever PT is inside
4301 the invisible text. Otherwise the cursor would be
4302 placed _after_ the ellipsis when the point is after the
4303 first invisible character. */
4304 if (!STRINGP (it->object))
4305 {
4306 it->position.charpos = newpos - 1;
4307 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4308 }
4309 it->ellipsis_p = 1;
4310 /* Let the ellipsis display before
4311 considering any properties of the following char.
4312 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4313 handled = HANDLED_RETURN;
4314 }
4315 }
4316 }
4317
4318 return handled;
4319 }
4320
4321
4322 /* Make iterator IT return `...' next.
4323 Replaces LEN characters from buffer. */
4324
4325 static void
4326 setup_for_ellipsis (struct it *it, int len)
4327 {
4328 /* Use the display table definition for `...'. Invalid glyphs
4329 will be handled by the method returning elements from dpvec. */
4330 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4331 {
4332 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4333 it->dpvec = v->contents;
4334 it->dpend = v->contents + v->header.size;
4335 }
4336 else
4337 {
4338 /* Default `...'. */
4339 it->dpvec = default_invis_vector;
4340 it->dpend = default_invis_vector + 3;
4341 }
4342
4343 it->dpvec_char_len = len;
4344 it->current.dpvec_index = 0;
4345 it->dpvec_face_id = -1;
4346
4347 /* Remember the current face id in case glyphs specify faces.
4348 IT's face is restored in set_iterator_to_next.
4349 saved_face_id was set to preceding char's face in handle_stop. */
4350 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4351 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4352
4353 it->method = GET_FROM_DISPLAY_VECTOR;
4354 it->ellipsis_p = 1;
4355 }
4356
4357
4358 \f
4359 /***********************************************************************
4360 'display' property
4361 ***********************************************************************/
4362
4363 /* Set up iterator IT from `display' property at its current position.
4364 Called from handle_stop.
4365 We return HANDLED_RETURN if some part of the display property
4366 overrides the display of the buffer text itself.
4367 Otherwise we return HANDLED_NORMALLY. */
4368
4369 static enum prop_handled
4370 handle_display_prop (struct it *it)
4371 {
4372 Lisp_Object propval, object, overlay;
4373 struct text_pos *position;
4374 ptrdiff_t bufpos;
4375 /* Nonzero if some property replaces the display of the text itself. */
4376 int display_replaced_p = 0;
4377
4378 if (STRINGP (it->string))
4379 {
4380 object = it->string;
4381 position = &it->current.string_pos;
4382 bufpos = CHARPOS (it->current.pos);
4383 }
4384 else
4385 {
4386 XSETWINDOW (object, it->w);
4387 position = &it->current.pos;
4388 bufpos = CHARPOS (*position);
4389 }
4390
4391 /* Reset those iterator values set from display property values. */
4392 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4393 it->space_width = Qnil;
4394 it->font_height = Qnil;
4395 it->voffset = 0;
4396
4397 /* We don't support recursive `display' properties, i.e. string
4398 values that have a string `display' property, that have a string
4399 `display' property etc. */
4400 if (!it->string_from_display_prop_p)
4401 it->area = TEXT_AREA;
4402
4403 propval = get_char_property_and_overlay (make_number (position->charpos),
4404 Qdisplay, object, &overlay);
4405 if (NILP (propval))
4406 return HANDLED_NORMALLY;
4407 /* Now OVERLAY is the overlay that gave us this property, or nil
4408 if it was a text property. */
4409
4410 if (!STRINGP (it->string))
4411 object = WGET (it->w, buffer);
4412
4413 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4414 position, bufpos,
4415 FRAME_WINDOW_P (it->f));
4416
4417 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4418 }
4419
4420 /* Subroutine of handle_display_prop. Returns non-zero if the display
4421 specification in SPEC is a replacing specification, i.e. it would
4422 replace the text covered by `display' property with something else,
4423 such as an image or a display string. If SPEC includes any kind or
4424 `(space ...) specification, the value is 2; this is used by
4425 compute_display_string_pos, which see.
4426
4427 See handle_single_display_spec for documentation of arguments.
4428 frame_window_p is non-zero if the window being redisplayed is on a
4429 GUI frame; this argument is used only if IT is NULL, see below.
4430
4431 IT can be NULL, if this is called by the bidi reordering code
4432 through compute_display_string_pos, which see. In that case, this
4433 function only examines SPEC, but does not otherwise "handle" it, in
4434 the sense that it doesn't set up members of IT from the display
4435 spec. */
4436 static int
4437 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4438 Lisp_Object overlay, struct text_pos *position,
4439 ptrdiff_t bufpos, int frame_window_p)
4440 {
4441 int replacing_p = 0;
4442 int rv;
4443
4444 if (CONSP (spec)
4445 /* Simple specifications. */
4446 && !EQ (XCAR (spec), Qimage)
4447 && !EQ (XCAR (spec), Qspace)
4448 && !EQ (XCAR (spec), Qwhen)
4449 && !EQ (XCAR (spec), Qslice)
4450 && !EQ (XCAR (spec), Qspace_width)
4451 && !EQ (XCAR (spec), Qheight)
4452 && !EQ (XCAR (spec), Qraise)
4453 /* Marginal area specifications. */
4454 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4455 && !EQ (XCAR (spec), Qleft_fringe)
4456 && !EQ (XCAR (spec), Qright_fringe)
4457 && !NILP (XCAR (spec)))
4458 {
4459 for (; CONSP (spec); spec = XCDR (spec))
4460 {
4461 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4462 overlay, position, bufpos,
4463 replacing_p, frame_window_p)))
4464 {
4465 replacing_p = rv;
4466 /* If some text in a string is replaced, `position' no
4467 longer points to the position of `object'. */
4468 if (!it || STRINGP (object))
4469 break;
4470 }
4471 }
4472 }
4473 else if (VECTORP (spec))
4474 {
4475 ptrdiff_t i;
4476 for (i = 0; i < ASIZE (spec); ++i)
4477 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4478 overlay, position, bufpos,
4479 replacing_p, frame_window_p)))
4480 {
4481 replacing_p = rv;
4482 /* If some text in a string is replaced, `position' no
4483 longer points to the position of `object'. */
4484 if (!it || STRINGP (object))
4485 break;
4486 }
4487 }
4488 else
4489 {
4490 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4491 position, bufpos, 0,
4492 frame_window_p)))
4493 replacing_p = rv;
4494 }
4495
4496 return replacing_p;
4497 }
4498
4499 /* Value is the position of the end of the `display' property starting
4500 at START_POS in OBJECT. */
4501
4502 static struct text_pos
4503 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4504 {
4505 Lisp_Object end;
4506 struct text_pos end_pos;
4507
4508 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4509 Qdisplay, object, Qnil);
4510 CHARPOS (end_pos) = XFASTINT (end);
4511 if (STRINGP (object))
4512 compute_string_pos (&end_pos, start_pos, it->string);
4513 else
4514 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4515
4516 return end_pos;
4517 }
4518
4519
4520 /* Set up IT from a single `display' property specification SPEC. OBJECT
4521 is the object in which the `display' property was found. *POSITION
4522 is the position in OBJECT at which the `display' property was found.
4523 BUFPOS is the buffer position of OBJECT (different from POSITION if
4524 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4525 previously saw a display specification which already replaced text
4526 display with something else, for example an image; we ignore such
4527 properties after the first one has been processed.
4528
4529 OVERLAY is the overlay this `display' property came from,
4530 or nil if it was a text property.
4531
4532 If SPEC is a `space' or `image' specification, and in some other
4533 cases too, set *POSITION to the position where the `display'
4534 property ends.
4535
4536 If IT is NULL, only examine the property specification in SPEC, but
4537 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4538 is intended to be displayed in a window on a GUI frame.
4539
4540 Value is non-zero if something was found which replaces the display
4541 of buffer or string text. */
4542
4543 static int
4544 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4545 Lisp_Object overlay, struct text_pos *position,
4546 ptrdiff_t bufpos, int display_replaced_p,
4547 int frame_window_p)
4548 {
4549 Lisp_Object form;
4550 Lisp_Object location, value;
4551 struct text_pos start_pos = *position;
4552 int valid_p;
4553
4554 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4555 If the result is non-nil, use VALUE instead of SPEC. */
4556 form = Qt;
4557 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4558 {
4559 spec = XCDR (spec);
4560 if (!CONSP (spec))
4561 return 0;
4562 form = XCAR (spec);
4563 spec = XCDR (spec);
4564 }
4565
4566 if (!NILP (form) && !EQ (form, Qt))
4567 {
4568 ptrdiff_t count = SPECPDL_INDEX ();
4569 struct gcpro gcpro1;
4570
4571 /* Bind `object' to the object having the `display' property, a
4572 buffer or string. Bind `position' to the position in the
4573 object where the property was found, and `buffer-position'
4574 to the current position in the buffer. */
4575
4576 if (NILP (object))
4577 XSETBUFFER (object, current_buffer);
4578 specbind (Qobject, object);
4579 specbind (Qposition, make_number (CHARPOS (*position)));
4580 specbind (Qbuffer_position, make_number (bufpos));
4581 GCPRO1 (form);
4582 form = safe_eval (form);
4583 UNGCPRO;
4584 unbind_to (count, Qnil);
4585 }
4586
4587 if (NILP (form))
4588 return 0;
4589
4590 /* Handle `(height HEIGHT)' specifications. */
4591 if (CONSP (spec)
4592 && EQ (XCAR (spec), Qheight)
4593 && CONSP (XCDR (spec)))
4594 {
4595 if (it)
4596 {
4597 if (!FRAME_WINDOW_P (it->f))
4598 return 0;
4599
4600 it->font_height = XCAR (XCDR (spec));
4601 if (!NILP (it->font_height))
4602 {
4603 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4604 int new_height = -1;
4605
4606 if (CONSP (it->font_height)
4607 && (EQ (XCAR (it->font_height), Qplus)
4608 || EQ (XCAR (it->font_height), Qminus))
4609 && CONSP (XCDR (it->font_height))
4610 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4611 {
4612 /* `(+ N)' or `(- N)' where N is an integer. */
4613 int steps = XINT (XCAR (XCDR (it->font_height)));
4614 if (EQ (XCAR (it->font_height), Qplus))
4615 steps = - steps;
4616 it->face_id = smaller_face (it->f, it->face_id, steps);
4617 }
4618 else if (FUNCTIONP (it->font_height))
4619 {
4620 /* Call function with current height as argument.
4621 Value is the new height. */
4622 Lisp_Object height;
4623 height = safe_call1 (it->font_height,
4624 face->lface[LFACE_HEIGHT_INDEX]);
4625 if (NUMBERP (height))
4626 new_height = XFLOATINT (height);
4627 }
4628 else if (NUMBERP (it->font_height))
4629 {
4630 /* Value is a multiple of the canonical char height. */
4631 struct face *f;
4632
4633 f = FACE_FROM_ID (it->f,
4634 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4635 new_height = (XFLOATINT (it->font_height)
4636 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4637 }
4638 else
4639 {
4640 /* Evaluate IT->font_height with `height' bound to the
4641 current specified height to get the new height. */
4642 ptrdiff_t count = SPECPDL_INDEX ();
4643
4644 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4645 value = safe_eval (it->font_height);
4646 unbind_to (count, Qnil);
4647
4648 if (NUMBERP (value))
4649 new_height = XFLOATINT (value);
4650 }
4651
4652 if (new_height > 0)
4653 it->face_id = face_with_height (it->f, it->face_id, new_height);
4654 }
4655 }
4656
4657 return 0;
4658 }
4659
4660 /* Handle `(space-width WIDTH)'. */
4661 if (CONSP (spec)
4662 && EQ (XCAR (spec), Qspace_width)
4663 && CONSP (XCDR (spec)))
4664 {
4665 if (it)
4666 {
4667 if (!FRAME_WINDOW_P (it->f))
4668 return 0;
4669
4670 value = XCAR (XCDR (spec));
4671 if (NUMBERP (value) && XFLOATINT (value) > 0)
4672 it->space_width = value;
4673 }
4674
4675 return 0;
4676 }
4677
4678 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4679 if (CONSP (spec)
4680 && EQ (XCAR (spec), Qslice))
4681 {
4682 Lisp_Object tem;
4683
4684 if (it)
4685 {
4686 if (!FRAME_WINDOW_P (it->f))
4687 return 0;
4688
4689 if (tem = XCDR (spec), CONSP (tem))
4690 {
4691 it->slice.x = XCAR (tem);
4692 if (tem = XCDR (tem), CONSP (tem))
4693 {
4694 it->slice.y = XCAR (tem);
4695 if (tem = XCDR (tem), CONSP (tem))
4696 {
4697 it->slice.width = XCAR (tem);
4698 if (tem = XCDR (tem), CONSP (tem))
4699 it->slice.height = XCAR (tem);
4700 }
4701 }
4702 }
4703 }
4704
4705 return 0;
4706 }
4707
4708 /* Handle `(raise FACTOR)'. */
4709 if (CONSP (spec)
4710 && EQ (XCAR (spec), Qraise)
4711 && CONSP (XCDR (spec)))
4712 {
4713 if (it)
4714 {
4715 if (!FRAME_WINDOW_P (it->f))
4716 return 0;
4717
4718 #ifdef HAVE_WINDOW_SYSTEM
4719 value = XCAR (XCDR (spec));
4720 if (NUMBERP (value))
4721 {
4722 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4723 it->voffset = - (XFLOATINT (value)
4724 * (FONT_HEIGHT (face->font)));
4725 }
4726 #endif /* HAVE_WINDOW_SYSTEM */
4727 }
4728
4729 return 0;
4730 }
4731
4732 /* Don't handle the other kinds of display specifications
4733 inside a string that we got from a `display' property. */
4734 if (it && it->string_from_display_prop_p)
4735 return 0;
4736
4737 /* Characters having this form of property are not displayed, so
4738 we have to find the end of the property. */
4739 if (it)
4740 {
4741 start_pos = *position;
4742 *position = display_prop_end (it, object, start_pos);
4743 }
4744 value = Qnil;
4745
4746 /* Stop the scan at that end position--we assume that all
4747 text properties change there. */
4748 if (it)
4749 it->stop_charpos = position->charpos;
4750
4751 /* Handle `(left-fringe BITMAP [FACE])'
4752 and `(right-fringe BITMAP [FACE])'. */
4753 if (CONSP (spec)
4754 && (EQ (XCAR (spec), Qleft_fringe)
4755 || EQ (XCAR (spec), Qright_fringe))
4756 && CONSP (XCDR (spec)))
4757 {
4758 int fringe_bitmap;
4759
4760 if (it)
4761 {
4762 if (!FRAME_WINDOW_P (it->f))
4763 /* If we return here, POSITION has been advanced
4764 across the text with this property. */
4765 {
4766 /* Synchronize the bidi iterator with POSITION. This is
4767 needed because we are not going to push the iterator
4768 on behalf of this display property, so there will be
4769 no pop_it call to do this synchronization for us. */
4770 if (it->bidi_p)
4771 {
4772 it->position = *position;
4773 iterate_out_of_display_property (it);
4774 *position = it->position;
4775 }
4776 return 1;
4777 }
4778 }
4779 else if (!frame_window_p)
4780 return 1;
4781
4782 #ifdef HAVE_WINDOW_SYSTEM
4783 value = XCAR (XCDR (spec));
4784 if (!SYMBOLP (value)
4785 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4786 /* If we return here, POSITION has been advanced
4787 across the text with this property. */
4788 {
4789 if (it && it->bidi_p)
4790 {
4791 it->position = *position;
4792 iterate_out_of_display_property (it);
4793 *position = it->position;
4794 }
4795 return 1;
4796 }
4797
4798 if (it)
4799 {
4800 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4801
4802 if (CONSP (XCDR (XCDR (spec))))
4803 {
4804 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4805 int face_id2 = lookup_derived_face (it->f, face_name,
4806 FRINGE_FACE_ID, 0);
4807 if (face_id2 >= 0)
4808 face_id = face_id2;
4809 }
4810
4811 /* Save current settings of IT so that we can restore them
4812 when we are finished with the glyph property value. */
4813 push_it (it, position);
4814
4815 it->area = TEXT_AREA;
4816 it->what = IT_IMAGE;
4817 it->image_id = -1; /* no image */
4818 it->position = start_pos;
4819 it->object = NILP (object) ? WGET (it->w, buffer) : object;
4820 it->method = GET_FROM_IMAGE;
4821 it->from_overlay = Qnil;
4822 it->face_id = face_id;
4823 it->from_disp_prop_p = 1;
4824
4825 /* Say that we haven't consumed the characters with
4826 `display' property yet. The call to pop_it in
4827 set_iterator_to_next will clean this up. */
4828 *position = start_pos;
4829
4830 if (EQ (XCAR (spec), Qleft_fringe))
4831 {
4832 it->left_user_fringe_bitmap = fringe_bitmap;
4833 it->left_user_fringe_face_id = face_id;
4834 }
4835 else
4836 {
4837 it->right_user_fringe_bitmap = fringe_bitmap;
4838 it->right_user_fringe_face_id = face_id;
4839 }
4840 }
4841 #endif /* HAVE_WINDOW_SYSTEM */
4842 return 1;
4843 }
4844
4845 /* Prepare to handle `((margin left-margin) ...)',
4846 `((margin right-margin) ...)' and `((margin nil) ...)'
4847 prefixes for display specifications. */
4848 location = Qunbound;
4849 if (CONSP (spec) && CONSP (XCAR (spec)))
4850 {
4851 Lisp_Object tem;
4852
4853 value = XCDR (spec);
4854 if (CONSP (value))
4855 value = XCAR (value);
4856
4857 tem = XCAR (spec);
4858 if (EQ (XCAR (tem), Qmargin)
4859 && (tem = XCDR (tem),
4860 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4861 (NILP (tem)
4862 || EQ (tem, Qleft_margin)
4863 || EQ (tem, Qright_margin))))
4864 location = tem;
4865 }
4866
4867 if (EQ (location, Qunbound))
4868 {
4869 location = Qnil;
4870 value = spec;
4871 }
4872
4873 /* After this point, VALUE is the property after any
4874 margin prefix has been stripped. It must be a string,
4875 an image specification, or `(space ...)'.
4876
4877 LOCATION specifies where to display: `left-margin',
4878 `right-margin' or nil. */
4879
4880 valid_p = (STRINGP (value)
4881 #ifdef HAVE_WINDOW_SYSTEM
4882 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4883 && valid_image_p (value))
4884 #endif /* not HAVE_WINDOW_SYSTEM */
4885 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4886
4887 if (valid_p && !display_replaced_p)
4888 {
4889 int retval = 1;
4890
4891 if (!it)
4892 {
4893 /* Callers need to know whether the display spec is any kind
4894 of `(space ...)' spec that is about to affect text-area
4895 display. */
4896 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4897 retval = 2;
4898 return retval;
4899 }
4900
4901 /* Save current settings of IT so that we can restore them
4902 when we are finished with the glyph property value. */
4903 push_it (it, position);
4904 it->from_overlay = overlay;
4905 it->from_disp_prop_p = 1;
4906
4907 if (NILP (location))
4908 it->area = TEXT_AREA;
4909 else if (EQ (location, Qleft_margin))
4910 it->area = LEFT_MARGIN_AREA;
4911 else
4912 it->area = RIGHT_MARGIN_AREA;
4913
4914 if (STRINGP (value))
4915 {
4916 it->string = value;
4917 it->multibyte_p = STRING_MULTIBYTE (it->string);
4918 it->current.overlay_string_index = -1;
4919 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4920 it->end_charpos = it->string_nchars = SCHARS (it->string);
4921 it->method = GET_FROM_STRING;
4922 it->stop_charpos = 0;
4923 it->prev_stop = 0;
4924 it->base_level_stop = 0;
4925 it->string_from_display_prop_p = 1;
4926 /* Say that we haven't consumed the characters with
4927 `display' property yet. The call to pop_it in
4928 set_iterator_to_next will clean this up. */
4929 if (BUFFERP (object))
4930 *position = start_pos;
4931
4932 /* Force paragraph direction to be that of the parent
4933 object. If the parent object's paragraph direction is
4934 not yet determined, default to L2R. */
4935 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4936 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4937 else
4938 it->paragraph_embedding = L2R;
4939
4940 /* Set up the bidi iterator for this display string. */
4941 if (it->bidi_p)
4942 {
4943 it->bidi_it.string.lstring = it->string;
4944 it->bidi_it.string.s = NULL;
4945 it->bidi_it.string.schars = it->end_charpos;
4946 it->bidi_it.string.bufpos = bufpos;
4947 it->bidi_it.string.from_disp_str = 1;
4948 it->bidi_it.string.unibyte = !it->multibyte_p;
4949 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4950 }
4951 }
4952 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4953 {
4954 it->method = GET_FROM_STRETCH;
4955 it->object = value;
4956 *position = it->position = start_pos;
4957 retval = 1 + (it->area == TEXT_AREA);
4958 }
4959 #ifdef HAVE_WINDOW_SYSTEM
4960 else
4961 {
4962 it->what = IT_IMAGE;
4963 it->image_id = lookup_image (it->f, value);
4964 it->position = start_pos;
4965 it->object = NILP (object) ? WGET (it->w, buffer) : object;
4966 it->method = GET_FROM_IMAGE;
4967
4968 /* Say that we haven't consumed the characters with
4969 `display' property yet. The call to pop_it in
4970 set_iterator_to_next will clean this up. */
4971 *position = start_pos;
4972 }
4973 #endif /* HAVE_WINDOW_SYSTEM */
4974
4975 return retval;
4976 }
4977
4978 /* Invalid property or property not supported. Restore
4979 POSITION to what it was before. */
4980 *position = start_pos;
4981 return 0;
4982 }
4983
4984 /* Check if PROP is a display property value whose text should be
4985 treated as intangible. OVERLAY is the overlay from which PROP
4986 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4987 specify the buffer position covered by PROP. */
4988
4989 int
4990 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4991 ptrdiff_t charpos, ptrdiff_t bytepos)
4992 {
4993 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4994 struct text_pos position;
4995
4996 SET_TEXT_POS (position, charpos, bytepos);
4997 return handle_display_spec (NULL, prop, Qnil, overlay,
4998 &position, charpos, frame_window_p);
4999 }
5000
5001
5002 /* Return 1 if PROP is a display sub-property value containing STRING.
5003
5004 Implementation note: this and the following function are really
5005 special cases of handle_display_spec and
5006 handle_single_display_spec, and should ideally use the same code.
5007 Until they do, these two pairs must be consistent and must be
5008 modified in sync. */
5009
5010 static int
5011 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5012 {
5013 if (EQ (string, prop))
5014 return 1;
5015
5016 /* Skip over `when FORM'. */
5017 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5018 {
5019 prop = XCDR (prop);
5020 if (!CONSP (prop))
5021 return 0;
5022 /* Actually, the condition following `when' should be eval'ed,
5023 like handle_single_display_spec does, and we should return
5024 zero if it evaluates to nil. However, this function is
5025 called only when the buffer was already displayed and some
5026 glyph in the glyph matrix was found to come from a display
5027 string. Therefore, the condition was already evaluated, and
5028 the result was non-nil, otherwise the display string wouldn't
5029 have been displayed and we would have never been called for
5030 this property. Thus, we can skip the evaluation and assume
5031 its result is non-nil. */
5032 prop = XCDR (prop);
5033 }
5034
5035 if (CONSP (prop))
5036 /* Skip over `margin LOCATION'. */
5037 if (EQ (XCAR (prop), Qmargin))
5038 {
5039 prop = XCDR (prop);
5040 if (!CONSP (prop))
5041 return 0;
5042
5043 prop = XCDR (prop);
5044 if (!CONSP (prop))
5045 return 0;
5046 }
5047
5048 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5049 }
5050
5051
5052 /* Return 1 if STRING appears in the `display' property PROP. */
5053
5054 static int
5055 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5056 {
5057 if (CONSP (prop)
5058 && !EQ (XCAR (prop), Qwhen)
5059 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5060 {
5061 /* A list of sub-properties. */
5062 while (CONSP (prop))
5063 {
5064 if (single_display_spec_string_p (XCAR (prop), string))
5065 return 1;
5066 prop = XCDR (prop);
5067 }
5068 }
5069 else if (VECTORP (prop))
5070 {
5071 /* A vector of sub-properties. */
5072 ptrdiff_t i;
5073 for (i = 0; i < ASIZE (prop); ++i)
5074 if (single_display_spec_string_p (AREF (prop, i), string))
5075 return 1;
5076 }
5077 else
5078 return single_display_spec_string_p (prop, string);
5079
5080 return 0;
5081 }
5082
5083 /* Look for STRING in overlays and text properties in the current
5084 buffer, between character positions FROM and TO (excluding TO).
5085 BACK_P non-zero means look back (in this case, TO is supposed to be
5086 less than FROM).
5087 Value is the first character position where STRING was found, or
5088 zero if it wasn't found before hitting TO.
5089
5090 This function may only use code that doesn't eval because it is
5091 called asynchronously from note_mouse_highlight. */
5092
5093 static ptrdiff_t
5094 string_buffer_position_lim (Lisp_Object string,
5095 ptrdiff_t from, ptrdiff_t to, int back_p)
5096 {
5097 Lisp_Object limit, prop, pos;
5098 int found = 0;
5099
5100 pos = make_number (max (from, BEGV));
5101
5102 if (!back_p) /* looking forward */
5103 {
5104 limit = make_number (min (to, ZV));
5105 while (!found && !EQ (pos, limit))
5106 {
5107 prop = Fget_char_property (pos, Qdisplay, Qnil);
5108 if (!NILP (prop) && display_prop_string_p (prop, string))
5109 found = 1;
5110 else
5111 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5112 limit);
5113 }
5114 }
5115 else /* looking back */
5116 {
5117 limit = make_number (max (to, BEGV));
5118 while (!found && !EQ (pos, limit))
5119 {
5120 prop = Fget_char_property (pos, Qdisplay, Qnil);
5121 if (!NILP (prop) && display_prop_string_p (prop, string))
5122 found = 1;
5123 else
5124 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5125 limit);
5126 }
5127 }
5128
5129 return found ? XINT (pos) : 0;
5130 }
5131
5132 /* Determine which buffer position in current buffer STRING comes from.
5133 AROUND_CHARPOS is an approximate position where it could come from.
5134 Value is the buffer position or 0 if it couldn't be determined.
5135
5136 This function is necessary because we don't record buffer positions
5137 in glyphs generated from strings (to keep struct glyph small).
5138 This function may only use code that doesn't eval because it is
5139 called asynchronously from note_mouse_highlight. */
5140
5141 static ptrdiff_t
5142 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5143 {
5144 const int MAX_DISTANCE = 1000;
5145 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5146 around_charpos + MAX_DISTANCE,
5147 0);
5148
5149 if (!found)
5150 found = string_buffer_position_lim (string, around_charpos,
5151 around_charpos - MAX_DISTANCE, 1);
5152 return found;
5153 }
5154
5155
5156 \f
5157 /***********************************************************************
5158 `composition' property
5159 ***********************************************************************/
5160
5161 /* Set up iterator IT from `composition' property at its current
5162 position. Called from handle_stop. */
5163
5164 static enum prop_handled
5165 handle_composition_prop (struct it *it)
5166 {
5167 Lisp_Object prop, string;
5168 ptrdiff_t pos, pos_byte, start, end;
5169
5170 if (STRINGP (it->string))
5171 {
5172 unsigned char *s;
5173
5174 pos = IT_STRING_CHARPOS (*it);
5175 pos_byte = IT_STRING_BYTEPOS (*it);
5176 string = it->string;
5177 s = SDATA (string) + pos_byte;
5178 it->c = STRING_CHAR (s);
5179 }
5180 else
5181 {
5182 pos = IT_CHARPOS (*it);
5183 pos_byte = IT_BYTEPOS (*it);
5184 string = Qnil;
5185 it->c = FETCH_CHAR (pos_byte);
5186 }
5187
5188 /* If there's a valid composition and point is not inside of the
5189 composition (in the case that the composition is from the current
5190 buffer), draw a glyph composed from the composition components. */
5191 if (find_composition (pos, -1, &start, &end, &prop, string)
5192 && COMPOSITION_VALID_P (start, end, prop)
5193 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5194 {
5195 if (start < pos)
5196 /* As we can't handle this situation (perhaps font-lock added
5197 a new composition), we just return here hoping that next
5198 redisplay will detect this composition much earlier. */
5199 return HANDLED_NORMALLY;
5200 if (start != pos)
5201 {
5202 if (STRINGP (it->string))
5203 pos_byte = string_char_to_byte (it->string, start);
5204 else
5205 pos_byte = CHAR_TO_BYTE (start);
5206 }
5207 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5208 prop, string);
5209
5210 if (it->cmp_it.id >= 0)
5211 {
5212 it->cmp_it.ch = -1;
5213 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5214 it->cmp_it.nglyphs = -1;
5215 }
5216 }
5217
5218 return HANDLED_NORMALLY;
5219 }
5220
5221
5222 \f
5223 /***********************************************************************
5224 Overlay strings
5225 ***********************************************************************/
5226
5227 /* The following structure is used to record overlay strings for
5228 later sorting in load_overlay_strings. */
5229
5230 struct overlay_entry
5231 {
5232 Lisp_Object overlay;
5233 Lisp_Object string;
5234 EMACS_INT priority;
5235 int after_string_p;
5236 };
5237
5238
5239 /* Set up iterator IT from overlay strings at its current position.
5240 Called from handle_stop. */
5241
5242 static enum prop_handled
5243 handle_overlay_change (struct it *it)
5244 {
5245 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5246 return HANDLED_RECOMPUTE_PROPS;
5247 else
5248 return HANDLED_NORMALLY;
5249 }
5250
5251
5252 /* Set up the next overlay string for delivery by IT, if there is an
5253 overlay string to deliver. Called by set_iterator_to_next when the
5254 end of the current overlay string is reached. If there are more
5255 overlay strings to display, IT->string and
5256 IT->current.overlay_string_index are set appropriately here.
5257 Otherwise IT->string is set to nil. */
5258
5259 static void
5260 next_overlay_string (struct it *it)
5261 {
5262 ++it->current.overlay_string_index;
5263 if (it->current.overlay_string_index == it->n_overlay_strings)
5264 {
5265 /* No more overlay strings. Restore IT's settings to what
5266 they were before overlay strings were processed, and
5267 continue to deliver from current_buffer. */
5268
5269 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5270 pop_it (it);
5271 eassert (it->sp > 0
5272 || (NILP (it->string)
5273 && it->method == GET_FROM_BUFFER
5274 && it->stop_charpos >= BEGV
5275 && it->stop_charpos <= it->end_charpos));
5276 it->current.overlay_string_index = -1;
5277 it->n_overlay_strings = 0;
5278 it->overlay_strings_charpos = -1;
5279 /* If there's an empty display string on the stack, pop the
5280 stack, to resync the bidi iterator with IT's position. Such
5281 empty strings are pushed onto the stack in
5282 get_overlay_strings_1. */
5283 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5284 pop_it (it);
5285
5286 /* If we're at the end of the buffer, record that we have
5287 processed the overlay strings there already, so that
5288 next_element_from_buffer doesn't try it again. */
5289 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5290 it->overlay_strings_at_end_processed_p = 1;
5291 }
5292 else
5293 {
5294 /* There are more overlay strings to process. If
5295 IT->current.overlay_string_index has advanced to a position
5296 where we must load IT->overlay_strings with more strings, do
5297 it. We must load at the IT->overlay_strings_charpos where
5298 IT->n_overlay_strings was originally computed; when invisible
5299 text is present, this might not be IT_CHARPOS (Bug#7016). */
5300 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5301
5302 if (it->current.overlay_string_index && i == 0)
5303 load_overlay_strings (it, it->overlay_strings_charpos);
5304
5305 /* Initialize IT to deliver display elements from the overlay
5306 string. */
5307 it->string = it->overlay_strings[i];
5308 it->multibyte_p = STRING_MULTIBYTE (it->string);
5309 SET_TEXT_POS (it->current.string_pos, 0, 0);
5310 it->method = GET_FROM_STRING;
5311 it->stop_charpos = 0;
5312 if (it->cmp_it.stop_pos >= 0)
5313 it->cmp_it.stop_pos = 0;
5314 it->prev_stop = 0;
5315 it->base_level_stop = 0;
5316
5317 /* Set up the bidi iterator for this overlay string. */
5318 if (it->bidi_p)
5319 {
5320 it->bidi_it.string.lstring = it->string;
5321 it->bidi_it.string.s = NULL;
5322 it->bidi_it.string.schars = SCHARS (it->string);
5323 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5324 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5325 it->bidi_it.string.unibyte = !it->multibyte_p;
5326 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5327 }
5328 }
5329
5330 CHECK_IT (it);
5331 }
5332
5333
5334 /* Compare two overlay_entry structures E1 and E2. Used as a
5335 comparison function for qsort in load_overlay_strings. Overlay
5336 strings for the same position are sorted so that
5337
5338 1. All after-strings come in front of before-strings, except
5339 when they come from the same overlay.
5340
5341 2. Within after-strings, strings are sorted so that overlay strings
5342 from overlays with higher priorities come first.
5343
5344 2. Within before-strings, strings are sorted so that overlay
5345 strings from overlays with higher priorities come last.
5346
5347 Value is analogous to strcmp. */
5348
5349
5350 static int
5351 compare_overlay_entries (const void *e1, const void *e2)
5352 {
5353 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5354 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5355 int result;
5356
5357 if (entry1->after_string_p != entry2->after_string_p)
5358 {
5359 /* Let after-strings appear in front of before-strings if
5360 they come from different overlays. */
5361 if (EQ (entry1->overlay, entry2->overlay))
5362 result = entry1->after_string_p ? 1 : -1;
5363 else
5364 result = entry1->after_string_p ? -1 : 1;
5365 }
5366 else if (entry1->priority != entry2->priority)
5367 {
5368 if (entry1->after_string_p)
5369 /* After-strings sorted in order of decreasing priority. */
5370 result = entry2->priority < entry1->priority ? -1 : 1;
5371 else
5372 /* Before-strings sorted in order of increasing priority. */
5373 result = entry1->priority < entry2->priority ? -1 : 1;
5374 }
5375 else
5376 result = 0;
5377
5378 return result;
5379 }
5380
5381
5382 /* Load the vector IT->overlay_strings with overlay strings from IT's
5383 current buffer position, or from CHARPOS if that is > 0. Set
5384 IT->n_overlays to the total number of overlay strings found.
5385
5386 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5387 a time. On entry into load_overlay_strings,
5388 IT->current.overlay_string_index gives the number of overlay
5389 strings that have already been loaded by previous calls to this
5390 function.
5391
5392 IT->add_overlay_start contains an additional overlay start
5393 position to consider for taking overlay strings from, if non-zero.
5394 This position comes into play when the overlay has an `invisible'
5395 property, and both before and after-strings. When we've skipped to
5396 the end of the overlay, because of its `invisible' property, we
5397 nevertheless want its before-string to appear.
5398 IT->add_overlay_start will contain the overlay start position
5399 in this case.
5400
5401 Overlay strings are sorted so that after-string strings come in
5402 front of before-string strings. Within before and after-strings,
5403 strings are sorted by overlay priority. See also function
5404 compare_overlay_entries. */
5405
5406 static void
5407 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5408 {
5409 Lisp_Object overlay, window, str, invisible;
5410 struct Lisp_Overlay *ov;
5411 ptrdiff_t start, end;
5412 ptrdiff_t size = 20;
5413 ptrdiff_t n = 0, i, j;
5414 int invis_p;
5415 struct overlay_entry *entries = alloca (size * sizeof *entries);
5416 USE_SAFE_ALLOCA;
5417
5418 if (charpos <= 0)
5419 charpos = IT_CHARPOS (*it);
5420
5421 /* Append the overlay string STRING of overlay OVERLAY to vector
5422 `entries' which has size `size' and currently contains `n'
5423 elements. AFTER_P non-zero means STRING is an after-string of
5424 OVERLAY. */
5425 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5426 do \
5427 { \
5428 Lisp_Object priority; \
5429 \
5430 if (n == size) \
5431 { \
5432 struct overlay_entry *old = entries; \
5433 SAFE_NALLOCA (entries, 2, size); \
5434 memcpy (entries, old, size * sizeof *entries); \
5435 size *= 2; \
5436 } \
5437 \
5438 entries[n].string = (STRING); \
5439 entries[n].overlay = (OVERLAY); \
5440 priority = Foverlay_get ((OVERLAY), Qpriority); \
5441 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5442 entries[n].after_string_p = (AFTER_P); \
5443 ++n; \
5444 } \
5445 while (0)
5446
5447 /* Process overlay before the overlay center. */
5448 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5449 {
5450 XSETMISC (overlay, ov);
5451 eassert (OVERLAYP (overlay));
5452 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5453 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5454
5455 if (end < charpos)
5456 break;
5457
5458 /* Skip this overlay if it doesn't start or end at IT's current
5459 position. */
5460 if (end != charpos && start != charpos)
5461 continue;
5462
5463 /* Skip this overlay if it doesn't apply to IT->w. */
5464 window = Foverlay_get (overlay, Qwindow);
5465 if (WINDOWP (window) && XWINDOW (window) != it->w)
5466 continue;
5467
5468 /* If the text ``under'' the overlay is invisible, both before-
5469 and after-strings from this overlay are visible; start and
5470 end position are indistinguishable. */
5471 invisible = Foverlay_get (overlay, Qinvisible);
5472 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5473
5474 /* If overlay has a non-empty before-string, record it. */
5475 if ((start == charpos || (end == charpos && invis_p))
5476 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5477 && SCHARS (str))
5478 RECORD_OVERLAY_STRING (overlay, str, 0);
5479
5480 /* If overlay has a non-empty after-string, record it. */
5481 if ((end == charpos || (start == charpos && invis_p))
5482 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5483 && SCHARS (str))
5484 RECORD_OVERLAY_STRING (overlay, str, 1);
5485 }
5486
5487 /* Process overlays after the overlay center. */
5488 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5489 {
5490 XSETMISC (overlay, ov);
5491 eassert (OVERLAYP (overlay));
5492 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5493 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5494
5495 if (start > charpos)
5496 break;
5497
5498 /* Skip this overlay if it doesn't start or end at IT's current
5499 position. */
5500 if (end != charpos && start != charpos)
5501 continue;
5502
5503 /* Skip this overlay if it doesn't apply to IT->w. */
5504 window = Foverlay_get (overlay, Qwindow);
5505 if (WINDOWP (window) && XWINDOW (window) != it->w)
5506 continue;
5507
5508 /* If the text ``under'' the overlay is invisible, it has a zero
5509 dimension, and both before- and after-strings apply. */
5510 invisible = Foverlay_get (overlay, Qinvisible);
5511 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5512
5513 /* If overlay has a non-empty before-string, record it. */
5514 if ((start == charpos || (end == charpos && invis_p))
5515 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5516 && SCHARS (str))
5517 RECORD_OVERLAY_STRING (overlay, str, 0);
5518
5519 /* If overlay has a non-empty after-string, record it. */
5520 if ((end == charpos || (start == charpos && invis_p))
5521 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5522 && SCHARS (str))
5523 RECORD_OVERLAY_STRING (overlay, str, 1);
5524 }
5525
5526 #undef RECORD_OVERLAY_STRING
5527
5528 /* Sort entries. */
5529 if (n > 1)
5530 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5531
5532 /* Record number of overlay strings, and where we computed it. */
5533 it->n_overlay_strings = n;
5534 it->overlay_strings_charpos = charpos;
5535
5536 /* IT->current.overlay_string_index is the number of overlay strings
5537 that have already been consumed by IT. Copy some of the
5538 remaining overlay strings to IT->overlay_strings. */
5539 i = 0;
5540 j = it->current.overlay_string_index;
5541 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5542 {
5543 it->overlay_strings[i] = entries[j].string;
5544 it->string_overlays[i++] = entries[j++].overlay;
5545 }
5546
5547 CHECK_IT (it);
5548 SAFE_FREE ();
5549 }
5550
5551
5552 /* Get the first chunk of overlay strings at IT's current buffer
5553 position, or at CHARPOS if that is > 0. Value is non-zero if at
5554 least one overlay string was found. */
5555
5556 static int
5557 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5558 {
5559 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5560 process. This fills IT->overlay_strings with strings, and sets
5561 IT->n_overlay_strings to the total number of strings to process.
5562 IT->pos.overlay_string_index has to be set temporarily to zero
5563 because load_overlay_strings needs this; it must be set to -1
5564 when no overlay strings are found because a zero value would
5565 indicate a position in the first overlay string. */
5566 it->current.overlay_string_index = 0;
5567 load_overlay_strings (it, charpos);
5568
5569 /* If we found overlay strings, set up IT to deliver display
5570 elements from the first one. Otherwise set up IT to deliver
5571 from current_buffer. */
5572 if (it->n_overlay_strings)
5573 {
5574 /* Make sure we know settings in current_buffer, so that we can
5575 restore meaningful values when we're done with the overlay
5576 strings. */
5577 if (compute_stop_p)
5578 compute_stop_pos (it);
5579 eassert (it->face_id >= 0);
5580
5581 /* Save IT's settings. They are restored after all overlay
5582 strings have been processed. */
5583 eassert (!compute_stop_p || it->sp == 0);
5584
5585 /* When called from handle_stop, there might be an empty display
5586 string loaded. In that case, don't bother saving it. But
5587 don't use this optimization with the bidi iterator, since we
5588 need the corresponding pop_it call to resync the bidi
5589 iterator's position with IT's position, after we are done
5590 with the overlay strings. (The corresponding call to pop_it
5591 in case of an empty display string is in
5592 next_overlay_string.) */
5593 if (!(!it->bidi_p
5594 && STRINGP (it->string) && !SCHARS (it->string)))
5595 push_it (it, NULL);
5596
5597 /* Set up IT to deliver display elements from the first overlay
5598 string. */
5599 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5600 it->string = it->overlay_strings[0];
5601 it->from_overlay = Qnil;
5602 it->stop_charpos = 0;
5603 eassert (STRINGP (it->string));
5604 it->end_charpos = SCHARS (it->string);
5605 it->prev_stop = 0;
5606 it->base_level_stop = 0;
5607 it->multibyte_p = STRING_MULTIBYTE (it->string);
5608 it->method = GET_FROM_STRING;
5609 it->from_disp_prop_p = 0;
5610
5611 /* Force paragraph direction to be that of the parent
5612 buffer. */
5613 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5614 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5615 else
5616 it->paragraph_embedding = L2R;
5617
5618 /* Set up the bidi iterator for this overlay string. */
5619 if (it->bidi_p)
5620 {
5621 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5622
5623 it->bidi_it.string.lstring = it->string;
5624 it->bidi_it.string.s = NULL;
5625 it->bidi_it.string.schars = SCHARS (it->string);
5626 it->bidi_it.string.bufpos = pos;
5627 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5628 it->bidi_it.string.unibyte = !it->multibyte_p;
5629 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5630 }
5631 return 1;
5632 }
5633
5634 it->current.overlay_string_index = -1;
5635 return 0;
5636 }
5637
5638 static int
5639 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5640 {
5641 it->string = Qnil;
5642 it->method = GET_FROM_BUFFER;
5643
5644 (void) get_overlay_strings_1 (it, charpos, 1);
5645
5646 CHECK_IT (it);
5647
5648 /* Value is non-zero if we found at least one overlay string. */
5649 return STRINGP (it->string);
5650 }
5651
5652
5653 \f
5654 /***********************************************************************
5655 Saving and restoring state
5656 ***********************************************************************/
5657
5658 /* Save current settings of IT on IT->stack. Called, for example,
5659 before setting up IT for an overlay string, to be able to restore
5660 IT's settings to what they were after the overlay string has been
5661 processed. If POSITION is non-NULL, it is the position to save on
5662 the stack instead of IT->position. */
5663
5664 static void
5665 push_it (struct it *it, struct text_pos *position)
5666 {
5667 struct iterator_stack_entry *p;
5668
5669 eassert (it->sp < IT_STACK_SIZE);
5670 p = it->stack + it->sp;
5671
5672 p->stop_charpos = it->stop_charpos;
5673 p->prev_stop = it->prev_stop;
5674 p->base_level_stop = it->base_level_stop;
5675 p->cmp_it = it->cmp_it;
5676 eassert (it->face_id >= 0);
5677 p->face_id = it->face_id;
5678 p->string = it->string;
5679 p->method = it->method;
5680 p->from_overlay = it->from_overlay;
5681 switch (p->method)
5682 {
5683 case GET_FROM_IMAGE:
5684 p->u.image.object = it->object;
5685 p->u.image.image_id = it->image_id;
5686 p->u.image.slice = it->slice;
5687 break;
5688 case GET_FROM_STRETCH:
5689 p->u.stretch.object = it->object;
5690 break;
5691 }
5692 p->position = position ? *position : it->position;
5693 p->current = it->current;
5694 p->end_charpos = it->end_charpos;
5695 p->string_nchars = it->string_nchars;
5696 p->area = it->area;
5697 p->multibyte_p = it->multibyte_p;
5698 p->avoid_cursor_p = it->avoid_cursor_p;
5699 p->space_width = it->space_width;
5700 p->font_height = it->font_height;
5701 p->voffset = it->voffset;
5702 p->string_from_display_prop_p = it->string_from_display_prop_p;
5703 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5704 p->display_ellipsis_p = 0;
5705 p->line_wrap = it->line_wrap;
5706 p->bidi_p = it->bidi_p;
5707 p->paragraph_embedding = it->paragraph_embedding;
5708 p->from_disp_prop_p = it->from_disp_prop_p;
5709 ++it->sp;
5710
5711 /* Save the state of the bidi iterator as well. */
5712 if (it->bidi_p)
5713 bidi_push_it (&it->bidi_it);
5714 }
5715
5716 static void
5717 iterate_out_of_display_property (struct it *it)
5718 {
5719 int buffer_p = !STRINGP (it->string);
5720 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5721 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5722
5723 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5724
5725 /* Maybe initialize paragraph direction. If we are at the beginning
5726 of a new paragraph, next_element_from_buffer may not have a
5727 chance to do that. */
5728 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5729 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5730 /* prev_stop can be zero, so check against BEGV as well. */
5731 while (it->bidi_it.charpos >= bob
5732 && it->prev_stop <= it->bidi_it.charpos
5733 && it->bidi_it.charpos < CHARPOS (it->position)
5734 && it->bidi_it.charpos < eob)
5735 bidi_move_to_visually_next (&it->bidi_it);
5736 /* Record the stop_pos we just crossed, for when we cross it
5737 back, maybe. */
5738 if (it->bidi_it.charpos > CHARPOS (it->position))
5739 it->prev_stop = CHARPOS (it->position);
5740 /* If we ended up not where pop_it put us, resync IT's
5741 positional members with the bidi iterator. */
5742 if (it->bidi_it.charpos != CHARPOS (it->position))
5743 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5744 if (buffer_p)
5745 it->current.pos = it->position;
5746 else
5747 it->current.string_pos = it->position;
5748 }
5749
5750 /* Restore IT's settings from IT->stack. Called, for example, when no
5751 more overlay strings must be processed, and we return to delivering
5752 display elements from a buffer, or when the end of a string from a
5753 `display' property is reached and we return to delivering display
5754 elements from an overlay string, or from a buffer. */
5755
5756 static void
5757 pop_it (struct it *it)
5758 {
5759 struct iterator_stack_entry *p;
5760 int from_display_prop = it->from_disp_prop_p;
5761
5762 eassert (it->sp > 0);
5763 --it->sp;
5764 p = it->stack + it->sp;
5765 it->stop_charpos = p->stop_charpos;
5766 it->prev_stop = p->prev_stop;
5767 it->base_level_stop = p->base_level_stop;
5768 it->cmp_it = p->cmp_it;
5769 it->face_id = p->face_id;
5770 it->current = p->current;
5771 it->position = p->position;
5772 it->string = p->string;
5773 it->from_overlay = p->from_overlay;
5774 if (NILP (it->string))
5775 SET_TEXT_POS (it->current.string_pos, -1, -1);
5776 it->method = p->method;
5777 switch (it->method)
5778 {
5779 case GET_FROM_IMAGE:
5780 it->image_id = p->u.image.image_id;
5781 it->object = p->u.image.object;
5782 it->slice = p->u.image.slice;
5783 break;
5784 case GET_FROM_STRETCH:
5785 it->object = p->u.stretch.object;
5786 break;
5787 case GET_FROM_BUFFER:
5788 it->object = WGET (it->w, buffer);
5789 break;
5790 case GET_FROM_STRING:
5791 it->object = it->string;
5792 break;
5793 case GET_FROM_DISPLAY_VECTOR:
5794 if (it->s)
5795 it->method = GET_FROM_C_STRING;
5796 else if (STRINGP (it->string))
5797 it->method = GET_FROM_STRING;
5798 else
5799 {
5800 it->method = GET_FROM_BUFFER;
5801 it->object = WGET (it->w, buffer);
5802 }
5803 }
5804 it->end_charpos = p->end_charpos;
5805 it->string_nchars = p->string_nchars;
5806 it->area = p->area;
5807 it->multibyte_p = p->multibyte_p;
5808 it->avoid_cursor_p = p->avoid_cursor_p;
5809 it->space_width = p->space_width;
5810 it->font_height = p->font_height;
5811 it->voffset = p->voffset;
5812 it->string_from_display_prop_p = p->string_from_display_prop_p;
5813 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5814 it->line_wrap = p->line_wrap;
5815 it->bidi_p = p->bidi_p;
5816 it->paragraph_embedding = p->paragraph_embedding;
5817 it->from_disp_prop_p = p->from_disp_prop_p;
5818 if (it->bidi_p)
5819 {
5820 bidi_pop_it (&it->bidi_it);
5821 /* Bidi-iterate until we get out of the portion of text, if any,
5822 covered by a `display' text property or by an overlay with
5823 `display' property. (We cannot just jump there, because the
5824 internal coherency of the bidi iterator state can not be
5825 preserved across such jumps.) We also must determine the
5826 paragraph base direction if the overlay we just processed is
5827 at the beginning of a new paragraph. */
5828 if (from_display_prop
5829 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5830 iterate_out_of_display_property (it);
5831
5832 eassert ((BUFFERP (it->object)
5833 && IT_CHARPOS (*it) == it->bidi_it.charpos
5834 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5835 || (STRINGP (it->object)
5836 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5837 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5838 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5839 }
5840 }
5841
5842
5843 \f
5844 /***********************************************************************
5845 Moving over lines
5846 ***********************************************************************/
5847
5848 /* Set IT's current position to the previous line start. */
5849
5850 static void
5851 back_to_previous_line_start (struct it *it)
5852 {
5853 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5854 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5855 }
5856
5857
5858 /* Move IT to the next line start.
5859
5860 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5861 we skipped over part of the text (as opposed to moving the iterator
5862 continuously over the text). Otherwise, don't change the value
5863 of *SKIPPED_P.
5864
5865 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5866 iterator on the newline, if it was found.
5867
5868 Newlines may come from buffer text, overlay strings, or strings
5869 displayed via the `display' property. That's the reason we can't
5870 simply use find_next_newline_no_quit.
5871
5872 Note that this function may not skip over invisible text that is so
5873 because of text properties and immediately follows a newline. If
5874 it would, function reseat_at_next_visible_line_start, when called
5875 from set_iterator_to_next, would effectively make invisible
5876 characters following a newline part of the wrong glyph row, which
5877 leads to wrong cursor motion. */
5878
5879 static int
5880 forward_to_next_line_start (struct it *it, int *skipped_p,
5881 struct bidi_it *bidi_it_prev)
5882 {
5883 ptrdiff_t old_selective;
5884 int newline_found_p, n;
5885 const int MAX_NEWLINE_DISTANCE = 500;
5886
5887 /* If already on a newline, just consume it to avoid unintended
5888 skipping over invisible text below. */
5889 if (it->what == IT_CHARACTER
5890 && it->c == '\n'
5891 && CHARPOS (it->position) == IT_CHARPOS (*it))
5892 {
5893 if (it->bidi_p && bidi_it_prev)
5894 *bidi_it_prev = it->bidi_it;
5895 set_iterator_to_next (it, 0);
5896 it->c = 0;
5897 return 1;
5898 }
5899
5900 /* Don't handle selective display in the following. It's (a)
5901 unnecessary because it's done by the caller, and (b) leads to an
5902 infinite recursion because next_element_from_ellipsis indirectly
5903 calls this function. */
5904 old_selective = it->selective;
5905 it->selective = 0;
5906
5907 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5908 from buffer text. */
5909 for (n = newline_found_p = 0;
5910 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5911 n += STRINGP (it->string) ? 0 : 1)
5912 {
5913 if (!get_next_display_element (it))
5914 return 0;
5915 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5916 if (newline_found_p && it->bidi_p && bidi_it_prev)
5917 *bidi_it_prev = it->bidi_it;
5918 set_iterator_to_next (it, 0);
5919 }
5920
5921 /* If we didn't find a newline near enough, see if we can use a
5922 short-cut. */
5923 if (!newline_found_p)
5924 {
5925 ptrdiff_t start = IT_CHARPOS (*it);
5926 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5927 Lisp_Object pos;
5928
5929 eassert (!STRINGP (it->string));
5930
5931 /* If there isn't any `display' property in sight, and no
5932 overlays, we can just use the position of the newline in
5933 buffer text. */
5934 if (it->stop_charpos >= limit
5935 || ((pos = Fnext_single_property_change (make_number (start),
5936 Qdisplay, Qnil,
5937 make_number (limit)),
5938 NILP (pos))
5939 && next_overlay_change (start) == ZV))
5940 {
5941 if (!it->bidi_p)
5942 {
5943 IT_CHARPOS (*it) = limit;
5944 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5945 }
5946 else
5947 {
5948 struct bidi_it bprev;
5949
5950 /* Help bidi.c avoid expensive searches for display
5951 properties and overlays, by telling it that there are
5952 none up to `limit'. */
5953 if (it->bidi_it.disp_pos < limit)
5954 {
5955 it->bidi_it.disp_pos = limit;
5956 it->bidi_it.disp_prop = 0;
5957 }
5958 do {
5959 bprev = it->bidi_it;
5960 bidi_move_to_visually_next (&it->bidi_it);
5961 } while (it->bidi_it.charpos != limit);
5962 IT_CHARPOS (*it) = limit;
5963 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5964 if (bidi_it_prev)
5965 *bidi_it_prev = bprev;
5966 }
5967 *skipped_p = newline_found_p = 1;
5968 }
5969 else
5970 {
5971 while (get_next_display_element (it)
5972 && !newline_found_p)
5973 {
5974 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5975 if (newline_found_p && it->bidi_p && bidi_it_prev)
5976 *bidi_it_prev = it->bidi_it;
5977 set_iterator_to_next (it, 0);
5978 }
5979 }
5980 }
5981
5982 it->selective = old_selective;
5983 return newline_found_p;
5984 }
5985
5986
5987 /* Set IT's current position to the previous visible line start. Skip
5988 invisible text that is so either due to text properties or due to
5989 selective display. Caution: this does not change IT->current_x and
5990 IT->hpos. */
5991
5992 static void
5993 back_to_previous_visible_line_start (struct it *it)
5994 {
5995 while (IT_CHARPOS (*it) > BEGV)
5996 {
5997 back_to_previous_line_start (it);
5998
5999 if (IT_CHARPOS (*it) <= BEGV)
6000 break;
6001
6002 /* If selective > 0, then lines indented more than its value are
6003 invisible. */
6004 if (it->selective > 0
6005 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6006 it->selective))
6007 continue;
6008
6009 /* Check the newline before point for invisibility. */
6010 {
6011 Lisp_Object prop;
6012 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6013 Qinvisible, it->window);
6014 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6015 continue;
6016 }
6017
6018 if (IT_CHARPOS (*it) <= BEGV)
6019 break;
6020
6021 {
6022 struct it it2;
6023 void *it2data = NULL;
6024 ptrdiff_t pos;
6025 ptrdiff_t beg, end;
6026 Lisp_Object val, overlay;
6027
6028 SAVE_IT (it2, *it, it2data);
6029
6030 /* If newline is part of a composition, continue from start of composition */
6031 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6032 && beg < IT_CHARPOS (*it))
6033 goto replaced;
6034
6035 /* If newline is replaced by a display property, find start of overlay
6036 or interval and continue search from that point. */
6037 pos = --IT_CHARPOS (it2);
6038 --IT_BYTEPOS (it2);
6039 it2.sp = 0;
6040 bidi_unshelve_cache (NULL, 0);
6041 it2.string_from_display_prop_p = 0;
6042 it2.from_disp_prop_p = 0;
6043 if (handle_display_prop (&it2) == HANDLED_RETURN
6044 && !NILP (val = get_char_property_and_overlay
6045 (make_number (pos), Qdisplay, Qnil, &overlay))
6046 && (OVERLAYP (overlay)
6047 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6048 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6049 {
6050 RESTORE_IT (it, it, it2data);
6051 goto replaced;
6052 }
6053
6054 /* Newline is not replaced by anything -- so we are done. */
6055 RESTORE_IT (it, it, it2data);
6056 break;
6057
6058 replaced:
6059 if (beg < BEGV)
6060 beg = BEGV;
6061 IT_CHARPOS (*it) = beg;
6062 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6063 }
6064 }
6065
6066 it->continuation_lines_width = 0;
6067
6068 eassert (IT_CHARPOS (*it) >= BEGV);
6069 eassert (IT_CHARPOS (*it) == BEGV
6070 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6071 CHECK_IT (it);
6072 }
6073
6074
6075 /* Reseat iterator IT at the previous visible line start. Skip
6076 invisible text that is so either due to text properties or due to
6077 selective display. At the end, update IT's overlay information,
6078 face information etc. */
6079
6080 void
6081 reseat_at_previous_visible_line_start (struct it *it)
6082 {
6083 back_to_previous_visible_line_start (it);
6084 reseat (it, it->current.pos, 1);
6085 CHECK_IT (it);
6086 }
6087
6088
6089 /* Reseat iterator IT on the next visible line start in the current
6090 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6091 preceding the line start. Skip over invisible text that is so
6092 because of selective display. Compute faces, overlays etc at the
6093 new position. Note that this function does not skip over text that
6094 is invisible because of text properties. */
6095
6096 static void
6097 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6098 {
6099 int newline_found_p, skipped_p = 0;
6100 struct bidi_it bidi_it_prev;
6101
6102 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6103
6104 /* Skip over lines that are invisible because they are indented
6105 more than the value of IT->selective. */
6106 if (it->selective > 0)
6107 while (IT_CHARPOS (*it) < ZV
6108 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6109 it->selective))
6110 {
6111 eassert (IT_BYTEPOS (*it) == BEGV
6112 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6113 newline_found_p =
6114 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6115 }
6116
6117 /* Position on the newline if that's what's requested. */
6118 if (on_newline_p && newline_found_p)
6119 {
6120 if (STRINGP (it->string))
6121 {
6122 if (IT_STRING_CHARPOS (*it) > 0)
6123 {
6124 if (!it->bidi_p)
6125 {
6126 --IT_STRING_CHARPOS (*it);
6127 --IT_STRING_BYTEPOS (*it);
6128 }
6129 else
6130 {
6131 /* We need to restore the bidi iterator to the state
6132 it had on the newline, and resync the IT's
6133 position with that. */
6134 it->bidi_it = bidi_it_prev;
6135 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6136 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6137 }
6138 }
6139 }
6140 else if (IT_CHARPOS (*it) > BEGV)
6141 {
6142 if (!it->bidi_p)
6143 {
6144 --IT_CHARPOS (*it);
6145 --IT_BYTEPOS (*it);
6146 }
6147 else
6148 {
6149 /* We need to restore the bidi iterator to the state it
6150 had on the newline and resync IT with that. */
6151 it->bidi_it = bidi_it_prev;
6152 IT_CHARPOS (*it) = it->bidi_it.charpos;
6153 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6154 }
6155 reseat (it, it->current.pos, 0);
6156 }
6157 }
6158 else if (skipped_p)
6159 reseat (it, it->current.pos, 0);
6160
6161 CHECK_IT (it);
6162 }
6163
6164
6165 \f
6166 /***********************************************************************
6167 Changing an iterator's position
6168 ***********************************************************************/
6169
6170 /* Change IT's current position to POS in current_buffer. If FORCE_P
6171 is non-zero, always check for text properties at the new position.
6172 Otherwise, text properties are only looked up if POS >=
6173 IT->check_charpos of a property. */
6174
6175 static void
6176 reseat (struct it *it, struct text_pos pos, int force_p)
6177 {
6178 ptrdiff_t original_pos = IT_CHARPOS (*it);
6179
6180 reseat_1 (it, pos, 0);
6181
6182 /* Determine where to check text properties. Avoid doing it
6183 where possible because text property lookup is very expensive. */
6184 if (force_p
6185 || CHARPOS (pos) > it->stop_charpos
6186 || CHARPOS (pos) < original_pos)
6187 {
6188 if (it->bidi_p)
6189 {
6190 /* For bidi iteration, we need to prime prev_stop and
6191 base_level_stop with our best estimations. */
6192 /* Implementation note: Of course, POS is not necessarily a
6193 stop position, so assigning prev_pos to it is a lie; we
6194 should have called compute_stop_backwards. However, if
6195 the current buffer does not include any R2L characters,
6196 that call would be a waste of cycles, because the
6197 iterator will never move back, and thus never cross this
6198 "fake" stop position. So we delay that backward search
6199 until the time we really need it, in next_element_from_buffer. */
6200 if (CHARPOS (pos) != it->prev_stop)
6201 it->prev_stop = CHARPOS (pos);
6202 if (CHARPOS (pos) < it->base_level_stop)
6203 it->base_level_stop = 0; /* meaning it's unknown */
6204 handle_stop (it);
6205 }
6206 else
6207 {
6208 handle_stop (it);
6209 it->prev_stop = it->base_level_stop = 0;
6210 }
6211
6212 }
6213
6214 CHECK_IT (it);
6215 }
6216
6217
6218 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6219 IT->stop_pos to POS, also. */
6220
6221 static void
6222 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6223 {
6224 /* Don't call this function when scanning a C string. */
6225 eassert (it->s == NULL);
6226
6227 /* POS must be a reasonable value. */
6228 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6229
6230 it->current.pos = it->position = pos;
6231 it->end_charpos = ZV;
6232 it->dpvec = NULL;
6233 it->current.dpvec_index = -1;
6234 it->current.overlay_string_index = -1;
6235 IT_STRING_CHARPOS (*it) = -1;
6236 IT_STRING_BYTEPOS (*it) = -1;
6237 it->string = Qnil;
6238 it->method = GET_FROM_BUFFER;
6239 it->object = WGET (it->w, buffer);
6240 it->area = TEXT_AREA;
6241 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6242 it->sp = 0;
6243 it->string_from_display_prop_p = 0;
6244 it->string_from_prefix_prop_p = 0;
6245
6246 it->from_disp_prop_p = 0;
6247 it->face_before_selective_p = 0;
6248 if (it->bidi_p)
6249 {
6250 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6251 &it->bidi_it);
6252 bidi_unshelve_cache (NULL, 0);
6253 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6254 it->bidi_it.string.s = NULL;
6255 it->bidi_it.string.lstring = Qnil;
6256 it->bidi_it.string.bufpos = 0;
6257 it->bidi_it.string.unibyte = 0;
6258 }
6259
6260 if (set_stop_p)
6261 {
6262 it->stop_charpos = CHARPOS (pos);
6263 it->base_level_stop = CHARPOS (pos);
6264 }
6265 }
6266
6267
6268 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6269 If S is non-null, it is a C string to iterate over. Otherwise,
6270 STRING gives a Lisp string to iterate over.
6271
6272 If PRECISION > 0, don't return more then PRECISION number of
6273 characters from the string.
6274
6275 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6276 characters have been returned. FIELD_WIDTH < 0 means an infinite
6277 field width.
6278
6279 MULTIBYTE = 0 means disable processing of multibyte characters,
6280 MULTIBYTE > 0 means enable it,
6281 MULTIBYTE < 0 means use IT->multibyte_p.
6282
6283 IT must be initialized via a prior call to init_iterator before
6284 calling this function. */
6285
6286 static void
6287 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6288 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6289 int multibyte)
6290 {
6291 /* No region in strings. */
6292 it->region_beg_charpos = it->region_end_charpos = -1;
6293
6294 /* No text property checks performed by default, but see below. */
6295 it->stop_charpos = -1;
6296
6297 /* Set iterator position and end position. */
6298 memset (&it->current, 0, sizeof it->current);
6299 it->current.overlay_string_index = -1;
6300 it->current.dpvec_index = -1;
6301 eassert (charpos >= 0);
6302
6303 /* If STRING is specified, use its multibyteness, otherwise use the
6304 setting of MULTIBYTE, if specified. */
6305 if (multibyte >= 0)
6306 it->multibyte_p = multibyte > 0;
6307
6308 /* Bidirectional reordering of strings is controlled by the default
6309 value of bidi-display-reordering. Don't try to reorder while
6310 loading loadup.el, as the necessary character property tables are
6311 not yet available. */
6312 it->bidi_p =
6313 NILP (Vpurify_flag)
6314 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6315
6316 if (s == NULL)
6317 {
6318 eassert (STRINGP (string));
6319 it->string = string;
6320 it->s = NULL;
6321 it->end_charpos = it->string_nchars = SCHARS (string);
6322 it->method = GET_FROM_STRING;
6323 it->current.string_pos = string_pos (charpos, string);
6324
6325 if (it->bidi_p)
6326 {
6327 it->bidi_it.string.lstring = string;
6328 it->bidi_it.string.s = NULL;
6329 it->bidi_it.string.schars = it->end_charpos;
6330 it->bidi_it.string.bufpos = 0;
6331 it->bidi_it.string.from_disp_str = 0;
6332 it->bidi_it.string.unibyte = !it->multibyte_p;
6333 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6334 FRAME_WINDOW_P (it->f), &it->bidi_it);
6335 }
6336 }
6337 else
6338 {
6339 it->s = (const unsigned char *) s;
6340 it->string = Qnil;
6341
6342 /* Note that we use IT->current.pos, not it->current.string_pos,
6343 for displaying C strings. */
6344 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6345 if (it->multibyte_p)
6346 {
6347 it->current.pos = c_string_pos (charpos, s, 1);
6348 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6349 }
6350 else
6351 {
6352 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6353 it->end_charpos = it->string_nchars = strlen (s);
6354 }
6355
6356 if (it->bidi_p)
6357 {
6358 it->bidi_it.string.lstring = Qnil;
6359 it->bidi_it.string.s = (const unsigned char *) s;
6360 it->bidi_it.string.schars = it->end_charpos;
6361 it->bidi_it.string.bufpos = 0;
6362 it->bidi_it.string.from_disp_str = 0;
6363 it->bidi_it.string.unibyte = !it->multibyte_p;
6364 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6365 &it->bidi_it);
6366 }
6367 it->method = GET_FROM_C_STRING;
6368 }
6369
6370 /* PRECISION > 0 means don't return more than PRECISION characters
6371 from the string. */
6372 if (precision > 0 && it->end_charpos - charpos > precision)
6373 {
6374 it->end_charpos = it->string_nchars = charpos + precision;
6375 if (it->bidi_p)
6376 it->bidi_it.string.schars = it->end_charpos;
6377 }
6378
6379 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6380 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6381 FIELD_WIDTH < 0 means infinite field width. This is useful for
6382 padding with `-' at the end of a mode line. */
6383 if (field_width < 0)
6384 field_width = INFINITY;
6385 /* Implementation note: We deliberately don't enlarge
6386 it->bidi_it.string.schars here to fit it->end_charpos, because
6387 the bidi iterator cannot produce characters out of thin air. */
6388 if (field_width > it->end_charpos - charpos)
6389 it->end_charpos = charpos + field_width;
6390
6391 /* Use the standard display table for displaying strings. */
6392 if (DISP_TABLE_P (Vstandard_display_table))
6393 it->dp = XCHAR_TABLE (Vstandard_display_table);
6394
6395 it->stop_charpos = charpos;
6396 it->prev_stop = charpos;
6397 it->base_level_stop = 0;
6398 if (it->bidi_p)
6399 {
6400 it->bidi_it.first_elt = 1;
6401 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6402 it->bidi_it.disp_pos = -1;
6403 }
6404 if (s == NULL && it->multibyte_p)
6405 {
6406 ptrdiff_t endpos = SCHARS (it->string);
6407 if (endpos > it->end_charpos)
6408 endpos = it->end_charpos;
6409 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6410 it->string);
6411 }
6412 CHECK_IT (it);
6413 }
6414
6415
6416 \f
6417 /***********************************************************************
6418 Iteration
6419 ***********************************************************************/
6420
6421 /* Map enum it_method value to corresponding next_element_from_* function. */
6422
6423 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6424 {
6425 next_element_from_buffer,
6426 next_element_from_display_vector,
6427 next_element_from_string,
6428 next_element_from_c_string,
6429 next_element_from_image,
6430 next_element_from_stretch
6431 };
6432
6433 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6434
6435
6436 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6437 (possibly with the following characters). */
6438
6439 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6440 ((IT)->cmp_it.id >= 0 \
6441 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6442 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6443 END_CHARPOS, (IT)->w, \
6444 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6445 (IT)->string)))
6446
6447
6448 /* Lookup the char-table Vglyphless_char_display for character C (-1
6449 if we want information for no-font case), and return the display
6450 method symbol. By side-effect, update it->what and
6451 it->glyphless_method. This function is called from
6452 get_next_display_element for each character element, and from
6453 x_produce_glyphs when no suitable font was found. */
6454
6455 Lisp_Object
6456 lookup_glyphless_char_display (int c, struct it *it)
6457 {
6458 Lisp_Object glyphless_method = Qnil;
6459
6460 if (CHAR_TABLE_P (Vglyphless_char_display)
6461 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6462 {
6463 if (c >= 0)
6464 {
6465 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6466 if (CONSP (glyphless_method))
6467 glyphless_method = FRAME_WINDOW_P (it->f)
6468 ? XCAR (glyphless_method)
6469 : XCDR (glyphless_method);
6470 }
6471 else
6472 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6473 }
6474
6475 retry:
6476 if (NILP (glyphless_method))
6477 {
6478 if (c >= 0)
6479 /* The default is to display the character by a proper font. */
6480 return Qnil;
6481 /* The default for the no-font case is to display an empty box. */
6482 glyphless_method = Qempty_box;
6483 }
6484 if (EQ (glyphless_method, Qzero_width))
6485 {
6486 if (c >= 0)
6487 return glyphless_method;
6488 /* This method can't be used for the no-font case. */
6489 glyphless_method = Qempty_box;
6490 }
6491 if (EQ (glyphless_method, Qthin_space))
6492 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6493 else if (EQ (glyphless_method, Qempty_box))
6494 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6495 else if (EQ (glyphless_method, Qhex_code))
6496 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6497 else if (STRINGP (glyphless_method))
6498 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6499 else
6500 {
6501 /* Invalid value. We use the default method. */
6502 glyphless_method = Qnil;
6503 goto retry;
6504 }
6505 it->what = IT_GLYPHLESS;
6506 return glyphless_method;
6507 }
6508
6509 /* Load IT's display element fields with information about the next
6510 display element from the current position of IT. Value is zero if
6511 end of buffer (or C string) is reached. */
6512
6513 static struct frame *last_escape_glyph_frame = NULL;
6514 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6515 static int last_escape_glyph_merged_face_id = 0;
6516
6517 struct frame *last_glyphless_glyph_frame = NULL;
6518 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6519 int last_glyphless_glyph_merged_face_id = 0;
6520
6521 static int
6522 get_next_display_element (struct it *it)
6523 {
6524 /* Non-zero means that we found a display element. Zero means that
6525 we hit the end of what we iterate over. Performance note: the
6526 function pointer `method' used here turns out to be faster than
6527 using a sequence of if-statements. */
6528 int success_p;
6529
6530 get_next:
6531 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6532
6533 if (it->what == IT_CHARACTER)
6534 {
6535 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6536 and only if (a) the resolved directionality of that character
6537 is R..." */
6538 /* FIXME: Do we need an exception for characters from display
6539 tables? */
6540 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6541 it->c = bidi_mirror_char (it->c);
6542 /* Map via display table or translate control characters.
6543 IT->c, IT->len etc. have been set to the next character by
6544 the function call above. If we have a display table, and it
6545 contains an entry for IT->c, translate it. Don't do this if
6546 IT->c itself comes from a display table, otherwise we could
6547 end up in an infinite recursion. (An alternative could be to
6548 count the recursion depth of this function and signal an
6549 error when a certain maximum depth is reached.) Is it worth
6550 it? */
6551 if (success_p && it->dpvec == NULL)
6552 {
6553 Lisp_Object dv;
6554 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6555 int nonascii_space_p = 0;
6556 int nonascii_hyphen_p = 0;
6557 int c = it->c; /* This is the character to display. */
6558
6559 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6560 {
6561 eassert (SINGLE_BYTE_CHAR_P (c));
6562 if (unibyte_display_via_language_environment)
6563 {
6564 c = DECODE_CHAR (unibyte, c);
6565 if (c < 0)
6566 c = BYTE8_TO_CHAR (it->c);
6567 }
6568 else
6569 c = BYTE8_TO_CHAR (it->c);
6570 }
6571
6572 if (it->dp
6573 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6574 VECTORP (dv)))
6575 {
6576 struct Lisp_Vector *v = XVECTOR (dv);
6577
6578 /* Return the first character from the display table
6579 entry, if not empty. If empty, don't display the
6580 current character. */
6581 if (v->header.size)
6582 {
6583 it->dpvec_char_len = it->len;
6584 it->dpvec = v->contents;
6585 it->dpend = v->contents + v->header.size;
6586 it->current.dpvec_index = 0;
6587 it->dpvec_face_id = -1;
6588 it->saved_face_id = it->face_id;
6589 it->method = GET_FROM_DISPLAY_VECTOR;
6590 it->ellipsis_p = 0;
6591 }
6592 else
6593 {
6594 set_iterator_to_next (it, 0);
6595 }
6596 goto get_next;
6597 }
6598
6599 if (! NILP (lookup_glyphless_char_display (c, it)))
6600 {
6601 if (it->what == IT_GLYPHLESS)
6602 goto done;
6603 /* Don't display this character. */
6604 set_iterator_to_next (it, 0);
6605 goto get_next;
6606 }
6607
6608 /* If `nobreak-char-display' is non-nil, we display
6609 non-ASCII spaces and hyphens specially. */
6610 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6611 {
6612 if (c == 0xA0)
6613 nonascii_space_p = 1;
6614 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6615 nonascii_hyphen_p = 1;
6616 }
6617
6618 /* Translate control characters into `\003' or `^C' form.
6619 Control characters coming from a display table entry are
6620 currently not translated because we use IT->dpvec to hold
6621 the translation. This could easily be changed but I
6622 don't believe that it is worth doing.
6623
6624 The characters handled by `nobreak-char-display' must be
6625 translated too.
6626
6627 Non-printable characters and raw-byte characters are also
6628 translated to octal form. */
6629 if (((c < ' ' || c == 127) /* ASCII control chars */
6630 ? (it->area != TEXT_AREA
6631 /* In mode line, treat \n, \t like other crl chars. */
6632 || (c != '\t'
6633 && it->glyph_row
6634 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6635 || (c != '\n' && c != '\t'))
6636 : (nonascii_space_p
6637 || nonascii_hyphen_p
6638 || CHAR_BYTE8_P (c)
6639 || ! CHAR_PRINTABLE_P (c))))
6640 {
6641 /* C is a control character, non-ASCII space/hyphen,
6642 raw-byte, or a non-printable character which must be
6643 displayed either as '\003' or as `^C' where the '\\'
6644 and '^' can be defined in the display table. Fill
6645 IT->ctl_chars with glyphs for what we have to
6646 display. Then, set IT->dpvec to these glyphs. */
6647 Lisp_Object gc;
6648 int ctl_len;
6649 int face_id;
6650 int lface_id = 0;
6651 int escape_glyph;
6652
6653 /* Handle control characters with ^. */
6654
6655 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6656 {
6657 int g;
6658
6659 g = '^'; /* default glyph for Control */
6660 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6661 if (it->dp
6662 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6663 {
6664 g = GLYPH_CODE_CHAR (gc);
6665 lface_id = GLYPH_CODE_FACE (gc);
6666 }
6667 if (lface_id)
6668 {
6669 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6670 }
6671 else if (it->f == last_escape_glyph_frame
6672 && it->face_id == last_escape_glyph_face_id)
6673 {
6674 face_id = last_escape_glyph_merged_face_id;
6675 }
6676 else
6677 {
6678 /* Merge the escape-glyph face into the current face. */
6679 face_id = merge_faces (it->f, Qescape_glyph, 0,
6680 it->face_id);
6681 last_escape_glyph_frame = it->f;
6682 last_escape_glyph_face_id = it->face_id;
6683 last_escape_glyph_merged_face_id = face_id;
6684 }
6685
6686 XSETINT (it->ctl_chars[0], g);
6687 XSETINT (it->ctl_chars[1], c ^ 0100);
6688 ctl_len = 2;
6689 goto display_control;
6690 }
6691
6692 /* Handle non-ascii space in the mode where it only gets
6693 highlighting. */
6694
6695 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6696 {
6697 /* Merge `nobreak-space' into the current face. */
6698 face_id = merge_faces (it->f, Qnobreak_space, 0,
6699 it->face_id);
6700 XSETINT (it->ctl_chars[0], ' ');
6701 ctl_len = 1;
6702 goto display_control;
6703 }
6704
6705 /* Handle sequences that start with the "escape glyph". */
6706
6707 /* the default escape glyph is \. */
6708 escape_glyph = '\\';
6709
6710 if (it->dp
6711 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6712 {
6713 escape_glyph = GLYPH_CODE_CHAR (gc);
6714 lface_id = GLYPH_CODE_FACE (gc);
6715 }
6716 if (lface_id)
6717 {
6718 /* The display table specified a face.
6719 Merge it into face_id and also into escape_glyph. */
6720 face_id = merge_faces (it->f, Qt, lface_id,
6721 it->face_id);
6722 }
6723 else if (it->f == last_escape_glyph_frame
6724 && it->face_id == last_escape_glyph_face_id)
6725 {
6726 face_id = last_escape_glyph_merged_face_id;
6727 }
6728 else
6729 {
6730 /* Merge the escape-glyph face into the current face. */
6731 face_id = merge_faces (it->f, Qescape_glyph, 0,
6732 it->face_id);
6733 last_escape_glyph_frame = it->f;
6734 last_escape_glyph_face_id = it->face_id;
6735 last_escape_glyph_merged_face_id = face_id;
6736 }
6737
6738 /* Draw non-ASCII hyphen with just highlighting: */
6739
6740 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6741 {
6742 XSETINT (it->ctl_chars[0], '-');
6743 ctl_len = 1;
6744 goto display_control;
6745 }
6746
6747 /* Draw non-ASCII space/hyphen with escape glyph: */
6748
6749 if (nonascii_space_p || nonascii_hyphen_p)
6750 {
6751 XSETINT (it->ctl_chars[0], escape_glyph);
6752 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6753 ctl_len = 2;
6754 goto display_control;
6755 }
6756
6757 {
6758 char str[10];
6759 int len, i;
6760
6761 if (CHAR_BYTE8_P (c))
6762 /* Display \200 instead of \17777600. */
6763 c = CHAR_TO_BYTE8 (c);
6764 len = sprintf (str, "%03o", c);
6765
6766 XSETINT (it->ctl_chars[0], escape_glyph);
6767 for (i = 0; i < len; i++)
6768 XSETINT (it->ctl_chars[i + 1], str[i]);
6769 ctl_len = len + 1;
6770 }
6771
6772 display_control:
6773 /* Set up IT->dpvec and return first character from it. */
6774 it->dpvec_char_len = it->len;
6775 it->dpvec = it->ctl_chars;
6776 it->dpend = it->dpvec + ctl_len;
6777 it->current.dpvec_index = 0;
6778 it->dpvec_face_id = face_id;
6779 it->saved_face_id = it->face_id;
6780 it->method = GET_FROM_DISPLAY_VECTOR;
6781 it->ellipsis_p = 0;
6782 goto get_next;
6783 }
6784 it->char_to_display = c;
6785 }
6786 else if (success_p)
6787 {
6788 it->char_to_display = it->c;
6789 }
6790 }
6791
6792 /* Adjust face id for a multibyte character. There are no multibyte
6793 character in unibyte text. */
6794 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6795 && it->multibyte_p
6796 && success_p
6797 && FRAME_WINDOW_P (it->f))
6798 {
6799 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6800
6801 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6802 {
6803 /* Automatic composition with glyph-string. */
6804 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6805
6806 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6807 }
6808 else
6809 {
6810 ptrdiff_t pos = (it->s ? -1
6811 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6812 : IT_CHARPOS (*it));
6813 int c;
6814
6815 if (it->what == IT_CHARACTER)
6816 c = it->char_to_display;
6817 else
6818 {
6819 struct composition *cmp = composition_table[it->cmp_it.id];
6820 int i;
6821
6822 c = ' ';
6823 for (i = 0; i < cmp->glyph_len; i++)
6824 /* TAB in a composition means display glyphs with
6825 padding space on the left or right. */
6826 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6827 break;
6828 }
6829 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6830 }
6831 }
6832
6833 done:
6834 /* Is this character the last one of a run of characters with
6835 box? If yes, set IT->end_of_box_run_p to 1. */
6836 if (it->face_box_p
6837 && it->s == NULL)
6838 {
6839 if (it->method == GET_FROM_STRING && it->sp)
6840 {
6841 int face_id = underlying_face_id (it);
6842 struct face *face = FACE_FROM_ID (it->f, face_id);
6843
6844 if (face)
6845 {
6846 if (face->box == FACE_NO_BOX)
6847 {
6848 /* If the box comes from face properties in a
6849 display string, check faces in that string. */
6850 int string_face_id = face_after_it_pos (it);
6851 it->end_of_box_run_p
6852 = (FACE_FROM_ID (it->f, string_face_id)->box
6853 == FACE_NO_BOX);
6854 }
6855 /* Otherwise, the box comes from the underlying face.
6856 If this is the last string character displayed, check
6857 the next buffer location. */
6858 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6859 && (it->current.overlay_string_index
6860 == it->n_overlay_strings - 1))
6861 {
6862 ptrdiff_t ignore;
6863 int next_face_id;
6864 struct text_pos pos = it->current.pos;
6865 INC_TEXT_POS (pos, it->multibyte_p);
6866
6867 next_face_id = face_at_buffer_position
6868 (it->w, CHARPOS (pos), it->region_beg_charpos,
6869 it->region_end_charpos, &ignore,
6870 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6871 -1);
6872 it->end_of_box_run_p
6873 = (FACE_FROM_ID (it->f, next_face_id)->box
6874 == FACE_NO_BOX);
6875 }
6876 }
6877 }
6878 else
6879 {
6880 int face_id = face_after_it_pos (it);
6881 it->end_of_box_run_p
6882 = (face_id != it->face_id
6883 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6884 }
6885 }
6886 /* If we reached the end of the object we've been iterating (e.g., a
6887 display string or an overlay string), and there's something on
6888 IT->stack, proceed with what's on the stack. It doesn't make
6889 sense to return zero if there's unprocessed stuff on the stack,
6890 because otherwise that stuff will never be displayed. */
6891 if (!success_p && it->sp > 0)
6892 {
6893 set_iterator_to_next (it, 0);
6894 success_p = get_next_display_element (it);
6895 }
6896
6897 /* Value is 0 if end of buffer or string reached. */
6898 return success_p;
6899 }
6900
6901
6902 /* Move IT to the next display element.
6903
6904 RESEAT_P non-zero means if called on a newline in buffer text,
6905 skip to the next visible line start.
6906
6907 Functions get_next_display_element and set_iterator_to_next are
6908 separate because I find this arrangement easier to handle than a
6909 get_next_display_element function that also increments IT's
6910 position. The way it is we can first look at an iterator's current
6911 display element, decide whether it fits on a line, and if it does,
6912 increment the iterator position. The other way around we probably
6913 would either need a flag indicating whether the iterator has to be
6914 incremented the next time, or we would have to implement a
6915 decrement position function which would not be easy to write. */
6916
6917 void
6918 set_iterator_to_next (struct it *it, int reseat_p)
6919 {
6920 /* Reset flags indicating start and end of a sequence of characters
6921 with box. Reset them at the start of this function because
6922 moving the iterator to a new position might set them. */
6923 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6924
6925 switch (it->method)
6926 {
6927 case GET_FROM_BUFFER:
6928 /* The current display element of IT is a character from
6929 current_buffer. Advance in the buffer, and maybe skip over
6930 invisible lines that are so because of selective display. */
6931 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6932 reseat_at_next_visible_line_start (it, 0);
6933 else if (it->cmp_it.id >= 0)
6934 {
6935 /* We are currently getting glyphs from a composition. */
6936 int i;
6937
6938 if (! it->bidi_p)
6939 {
6940 IT_CHARPOS (*it) += it->cmp_it.nchars;
6941 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6942 if (it->cmp_it.to < it->cmp_it.nglyphs)
6943 {
6944 it->cmp_it.from = it->cmp_it.to;
6945 }
6946 else
6947 {
6948 it->cmp_it.id = -1;
6949 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6950 IT_BYTEPOS (*it),
6951 it->end_charpos, Qnil);
6952 }
6953 }
6954 else if (! it->cmp_it.reversed_p)
6955 {
6956 /* Composition created while scanning forward. */
6957 /* Update IT's char/byte positions to point to the first
6958 character of the next grapheme cluster, or to the
6959 character visually after the current composition. */
6960 for (i = 0; i < it->cmp_it.nchars; i++)
6961 bidi_move_to_visually_next (&it->bidi_it);
6962 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6963 IT_CHARPOS (*it) = it->bidi_it.charpos;
6964
6965 if (it->cmp_it.to < it->cmp_it.nglyphs)
6966 {
6967 /* Proceed to the next grapheme cluster. */
6968 it->cmp_it.from = it->cmp_it.to;
6969 }
6970 else
6971 {
6972 /* No more grapheme clusters in this composition.
6973 Find the next stop position. */
6974 ptrdiff_t stop = it->end_charpos;
6975 if (it->bidi_it.scan_dir < 0)
6976 /* Now we are scanning backward and don't know
6977 where to stop. */
6978 stop = -1;
6979 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6980 IT_BYTEPOS (*it), stop, Qnil);
6981 }
6982 }
6983 else
6984 {
6985 /* Composition created while scanning backward. */
6986 /* Update IT's char/byte positions to point to the last
6987 character of the previous grapheme cluster, or the
6988 character visually after the current composition. */
6989 for (i = 0; i < it->cmp_it.nchars; i++)
6990 bidi_move_to_visually_next (&it->bidi_it);
6991 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6992 IT_CHARPOS (*it) = it->bidi_it.charpos;
6993 if (it->cmp_it.from > 0)
6994 {
6995 /* Proceed to the previous grapheme cluster. */
6996 it->cmp_it.to = it->cmp_it.from;
6997 }
6998 else
6999 {
7000 /* No more grapheme clusters in this composition.
7001 Find the next stop position. */
7002 ptrdiff_t stop = it->end_charpos;
7003 if (it->bidi_it.scan_dir < 0)
7004 /* Now we are scanning backward and don't know
7005 where to stop. */
7006 stop = -1;
7007 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7008 IT_BYTEPOS (*it), stop, Qnil);
7009 }
7010 }
7011 }
7012 else
7013 {
7014 eassert (it->len != 0);
7015
7016 if (!it->bidi_p)
7017 {
7018 IT_BYTEPOS (*it) += it->len;
7019 IT_CHARPOS (*it) += 1;
7020 }
7021 else
7022 {
7023 int prev_scan_dir = it->bidi_it.scan_dir;
7024 /* If this is a new paragraph, determine its base
7025 direction (a.k.a. its base embedding level). */
7026 if (it->bidi_it.new_paragraph)
7027 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7028 bidi_move_to_visually_next (&it->bidi_it);
7029 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7030 IT_CHARPOS (*it) = it->bidi_it.charpos;
7031 if (prev_scan_dir != it->bidi_it.scan_dir)
7032 {
7033 /* As the scan direction was changed, we must
7034 re-compute the stop position for composition. */
7035 ptrdiff_t stop = it->end_charpos;
7036 if (it->bidi_it.scan_dir < 0)
7037 stop = -1;
7038 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7039 IT_BYTEPOS (*it), stop, Qnil);
7040 }
7041 }
7042 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7043 }
7044 break;
7045
7046 case GET_FROM_C_STRING:
7047 /* Current display element of IT is from a C string. */
7048 if (!it->bidi_p
7049 /* If the string position is beyond string's end, it means
7050 next_element_from_c_string is padding the string with
7051 blanks, in which case we bypass the bidi iterator,
7052 because it cannot deal with such virtual characters. */
7053 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7054 {
7055 IT_BYTEPOS (*it) += it->len;
7056 IT_CHARPOS (*it) += 1;
7057 }
7058 else
7059 {
7060 bidi_move_to_visually_next (&it->bidi_it);
7061 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7062 IT_CHARPOS (*it) = it->bidi_it.charpos;
7063 }
7064 break;
7065
7066 case GET_FROM_DISPLAY_VECTOR:
7067 /* Current display element of IT is from a display table entry.
7068 Advance in the display table definition. Reset it to null if
7069 end reached, and continue with characters from buffers/
7070 strings. */
7071 ++it->current.dpvec_index;
7072
7073 /* Restore face of the iterator to what they were before the
7074 display vector entry (these entries may contain faces). */
7075 it->face_id = it->saved_face_id;
7076
7077 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7078 {
7079 int recheck_faces = it->ellipsis_p;
7080
7081 if (it->s)
7082 it->method = GET_FROM_C_STRING;
7083 else if (STRINGP (it->string))
7084 it->method = GET_FROM_STRING;
7085 else
7086 {
7087 it->method = GET_FROM_BUFFER;
7088 it->object = WGET (it->w, buffer);
7089 }
7090
7091 it->dpvec = NULL;
7092 it->current.dpvec_index = -1;
7093
7094 /* Skip over characters which were displayed via IT->dpvec. */
7095 if (it->dpvec_char_len < 0)
7096 reseat_at_next_visible_line_start (it, 1);
7097 else if (it->dpvec_char_len > 0)
7098 {
7099 if (it->method == GET_FROM_STRING
7100 && it->n_overlay_strings > 0)
7101 it->ignore_overlay_strings_at_pos_p = 1;
7102 it->len = it->dpvec_char_len;
7103 set_iterator_to_next (it, reseat_p);
7104 }
7105
7106 /* Maybe recheck faces after display vector */
7107 if (recheck_faces)
7108 it->stop_charpos = IT_CHARPOS (*it);
7109 }
7110 break;
7111
7112 case GET_FROM_STRING:
7113 /* Current display element is a character from a Lisp string. */
7114 eassert (it->s == NULL && STRINGP (it->string));
7115 /* Don't advance past string end. These conditions are true
7116 when set_iterator_to_next is called at the end of
7117 get_next_display_element, in which case the Lisp string is
7118 already exhausted, and all we want is pop the iterator
7119 stack. */
7120 if (it->current.overlay_string_index >= 0)
7121 {
7122 /* This is an overlay string, so there's no padding with
7123 spaces, and the number of characters in the string is
7124 where the string ends. */
7125 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7126 goto consider_string_end;
7127 }
7128 else
7129 {
7130 /* Not an overlay string. There could be padding, so test
7131 against it->end_charpos . */
7132 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7133 goto consider_string_end;
7134 }
7135 if (it->cmp_it.id >= 0)
7136 {
7137 int i;
7138
7139 if (! it->bidi_p)
7140 {
7141 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7142 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7143 if (it->cmp_it.to < it->cmp_it.nglyphs)
7144 it->cmp_it.from = it->cmp_it.to;
7145 else
7146 {
7147 it->cmp_it.id = -1;
7148 composition_compute_stop_pos (&it->cmp_it,
7149 IT_STRING_CHARPOS (*it),
7150 IT_STRING_BYTEPOS (*it),
7151 it->end_charpos, it->string);
7152 }
7153 }
7154 else if (! it->cmp_it.reversed_p)
7155 {
7156 for (i = 0; i < it->cmp_it.nchars; i++)
7157 bidi_move_to_visually_next (&it->bidi_it);
7158 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7159 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7160
7161 if (it->cmp_it.to < it->cmp_it.nglyphs)
7162 it->cmp_it.from = it->cmp_it.to;
7163 else
7164 {
7165 ptrdiff_t stop = it->end_charpos;
7166 if (it->bidi_it.scan_dir < 0)
7167 stop = -1;
7168 composition_compute_stop_pos (&it->cmp_it,
7169 IT_STRING_CHARPOS (*it),
7170 IT_STRING_BYTEPOS (*it), stop,
7171 it->string);
7172 }
7173 }
7174 else
7175 {
7176 for (i = 0; i < it->cmp_it.nchars; i++)
7177 bidi_move_to_visually_next (&it->bidi_it);
7178 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7179 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7180 if (it->cmp_it.from > 0)
7181 it->cmp_it.to = it->cmp_it.from;
7182 else
7183 {
7184 ptrdiff_t stop = it->end_charpos;
7185 if (it->bidi_it.scan_dir < 0)
7186 stop = -1;
7187 composition_compute_stop_pos (&it->cmp_it,
7188 IT_STRING_CHARPOS (*it),
7189 IT_STRING_BYTEPOS (*it), stop,
7190 it->string);
7191 }
7192 }
7193 }
7194 else
7195 {
7196 if (!it->bidi_p
7197 /* If the string position is beyond string's end, it
7198 means next_element_from_string is padding the string
7199 with blanks, in which case we bypass the bidi
7200 iterator, because it cannot deal with such virtual
7201 characters. */
7202 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7203 {
7204 IT_STRING_BYTEPOS (*it) += it->len;
7205 IT_STRING_CHARPOS (*it) += 1;
7206 }
7207 else
7208 {
7209 int prev_scan_dir = it->bidi_it.scan_dir;
7210
7211 bidi_move_to_visually_next (&it->bidi_it);
7212 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7213 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7214 if (prev_scan_dir != it->bidi_it.scan_dir)
7215 {
7216 ptrdiff_t stop = it->end_charpos;
7217
7218 if (it->bidi_it.scan_dir < 0)
7219 stop = -1;
7220 composition_compute_stop_pos (&it->cmp_it,
7221 IT_STRING_CHARPOS (*it),
7222 IT_STRING_BYTEPOS (*it), stop,
7223 it->string);
7224 }
7225 }
7226 }
7227
7228 consider_string_end:
7229
7230 if (it->current.overlay_string_index >= 0)
7231 {
7232 /* IT->string is an overlay string. Advance to the
7233 next, if there is one. */
7234 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7235 {
7236 it->ellipsis_p = 0;
7237 next_overlay_string (it);
7238 if (it->ellipsis_p)
7239 setup_for_ellipsis (it, 0);
7240 }
7241 }
7242 else
7243 {
7244 /* IT->string is not an overlay string. If we reached
7245 its end, and there is something on IT->stack, proceed
7246 with what is on the stack. This can be either another
7247 string, this time an overlay string, or a buffer. */
7248 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7249 && it->sp > 0)
7250 {
7251 pop_it (it);
7252 if (it->method == GET_FROM_STRING)
7253 goto consider_string_end;
7254 }
7255 }
7256 break;
7257
7258 case GET_FROM_IMAGE:
7259 case GET_FROM_STRETCH:
7260 /* The position etc with which we have to proceed are on
7261 the stack. The position may be at the end of a string,
7262 if the `display' property takes up the whole string. */
7263 eassert (it->sp > 0);
7264 pop_it (it);
7265 if (it->method == GET_FROM_STRING)
7266 goto consider_string_end;
7267 break;
7268
7269 default:
7270 /* There are no other methods defined, so this should be a bug. */
7271 abort ();
7272 }
7273
7274 eassert (it->method != GET_FROM_STRING
7275 || (STRINGP (it->string)
7276 && IT_STRING_CHARPOS (*it) >= 0));
7277 }
7278
7279 /* Load IT's display element fields with information about the next
7280 display element which comes from a display table entry or from the
7281 result of translating a control character to one of the forms `^C'
7282 or `\003'.
7283
7284 IT->dpvec holds the glyphs to return as characters.
7285 IT->saved_face_id holds the face id before the display vector--it
7286 is restored into IT->face_id in set_iterator_to_next. */
7287
7288 static int
7289 next_element_from_display_vector (struct it *it)
7290 {
7291 Lisp_Object gc;
7292
7293 /* Precondition. */
7294 eassert (it->dpvec && it->current.dpvec_index >= 0);
7295
7296 it->face_id = it->saved_face_id;
7297
7298 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7299 That seemed totally bogus - so I changed it... */
7300 gc = it->dpvec[it->current.dpvec_index];
7301
7302 if (GLYPH_CODE_P (gc))
7303 {
7304 it->c = GLYPH_CODE_CHAR (gc);
7305 it->len = CHAR_BYTES (it->c);
7306
7307 /* The entry may contain a face id to use. Such a face id is
7308 the id of a Lisp face, not a realized face. A face id of
7309 zero means no face is specified. */
7310 if (it->dpvec_face_id >= 0)
7311 it->face_id = it->dpvec_face_id;
7312 else
7313 {
7314 int lface_id = GLYPH_CODE_FACE (gc);
7315 if (lface_id > 0)
7316 it->face_id = merge_faces (it->f, Qt, lface_id,
7317 it->saved_face_id);
7318 }
7319 }
7320 else
7321 /* Display table entry is invalid. Return a space. */
7322 it->c = ' ', it->len = 1;
7323
7324 /* Don't change position and object of the iterator here. They are
7325 still the values of the character that had this display table
7326 entry or was translated, and that's what we want. */
7327 it->what = IT_CHARACTER;
7328 return 1;
7329 }
7330
7331 /* Get the first element of string/buffer in the visual order, after
7332 being reseated to a new position in a string or a buffer. */
7333 static void
7334 get_visually_first_element (struct it *it)
7335 {
7336 int string_p = STRINGP (it->string) || it->s;
7337 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7338 ptrdiff_t bob = (string_p ? 0 : BEGV);
7339
7340 if (STRINGP (it->string))
7341 {
7342 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7343 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7344 }
7345 else
7346 {
7347 it->bidi_it.charpos = IT_CHARPOS (*it);
7348 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7349 }
7350
7351 if (it->bidi_it.charpos == eob)
7352 {
7353 /* Nothing to do, but reset the FIRST_ELT flag, like
7354 bidi_paragraph_init does, because we are not going to
7355 call it. */
7356 it->bidi_it.first_elt = 0;
7357 }
7358 else if (it->bidi_it.charpos == bob
7359 || (!string_p
7360 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7361 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7362 {
7363 /* If we are at the beginning of a line/string, we can produce
7364 the next element right away. */
7365 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7366 bidi_move_to_visually_next (&it->bidi_it);
7367 }
7368 else
7369 {
7370 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7371
7372 /* We need to prime the bidi iterator starting at the line's or
7373 string's beginning, before we will be able to produce the
7374 next element. */
7375 if (string_p)
7376 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7377 else
7378 {
7379 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7380 -1);
7381 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7382 }
7383 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7384 do
7385 {
7386 /* Now return to buffer/string position where we were asked
7387 to get the next display element, and produce that. */
7388 bidi_move_to_visually_next (&it->bidi_it);
7389 }
7390 while (it->bidi_it.bytepos != orig_bytepos
7391 && it->bidi_it.charpos < eob);
7392 }
7393
7394 /* Adjust IT's position information to where we ended up. */
7395 if (STRINGP (it->string))
7396 {
7397 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7398 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7399 }
7400 else
7401 {
7402 IT_CHARPOS (*it) = it->bidi_it.charpos;
7403 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7404 }
7405
7406 if (STRINGP (it->string) || !it->s)
7407 {
7408 ptrdiff_t stop, charpos, bytepos;
7409
7410 if (STRINGP (it->string))
7411 {
7412 eassert (!it->s);
7413 stop = SCHARS (it->string);
7414 if (stop > it->end_charpos)
7415 stop = it->end_charpos;
7416 charpos = IT_STRING_CHARPOS (*it);
7417 bytepos = IT_STRING_BYTEPOS (*it);
7418 }
7419 else
7420 {
7421 stop = it->end_charpos;
7422 charpos = IT_CHARPOS (*it);
7423 bytepos = IT_BYTEPOS (*it);
7424 }
7425 if (it->bidi_it.scan_dir < 0)
7426 stop = -1;
7427 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7428 it->string);
7429 }
7430 }
7431
7432 /* Load IT with the next display element from Lisp string IT->string.
7433 IT->current.string_pos is the current position within the string.
7434 If IT->current.overlay_string_index >= 0, the Lisp string is an
7435 overlay string. */
7436
7437 static int
7438 next_element_from_string (struct it *it)
7439 {
7440 struct text_pos position;
7441
7442 eassert (STRINGP (it->string));
7443 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7444 eassert (IT_STRING_CHARPOS (*it) >= 0);
7445 position = it->current.string_pos;
7446
7447 /* With bidi reordering, the character to display might not be the
7448 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7449 that we were reseat()ed to a new string, whose paragraph
7450 direction is not known. */
7451 if (it->bidi_p && it->bidi_it.first_elt)
7452 {
7453 get_visually_first_element (it);
7454 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7455 }
7456
7457 /* Time to check for invisible text? */
7458 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7459 {
7460 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7461 {
7462 if (!(!it->bidi_p
7463 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7464 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7465 {
7466 /* With bidi non-linear iteration, we could find
7467 ourselves far beyond the last computed stop_charpos,
7468 with several other stop positions in between that we
7469 missed. Scan them all now, in buffer's logical
7470 order, until we find and handle the last stop_charpos
7471 that precedes our current position. */
7472 handle_stop_backwards (it, it->stop_charpos);
7473 return GET_NEXT_DISPLAY_ELEMENT (it);
7474 }
7475 else
7476 {
7477 if (it->bidi_p)
7478 {
7479 /* Take note of the stop position we just moved
7480 across, for when we will move back across it. */
7481 it->prev_stop = it->stop_charpos;
7482 /* If we are at base paragraph embedding level, take
7483 note of the last stop position seen at this
7484 level. */
7485 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7486 it->base_level_stop = it->stop_charpos;
7487 }
7488 handle_stop (it);
7489
7490 /* Since a handler may have changed IT->method, we must
7491 recurse here. */
7492 return GET_NEXT_DISPLAY_ELEMENT (it);
7493 }
7494 }
7495 else if (it->bidi_p
7496 /* If we are before prev_stop, we may have overstepped
7497 on our way backwards a stop_pos, and if so, we need
7498 to handle that stop_pos. */
7499 && IT_STRING_CHARPOS (*it) < it->prev_stop
7500 /* We can sometimes back up for reasons that have nothing
7501 to do with bidi reordering. E.g., compositions. The
7502 code below is only needed when we are above the base
7503 embedding level, so test for that explicitly. */
7504 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7505 {
7506 /* If we lost track of base_level_stop, we have no better
7507 place for handle_stop_backwards to start from than string
7508 beginning. This happens, e.g., when we were reseated to
7509 the previous screenful of text by vertical-motion. */
7510 if (it->base_level_stop <= 0
7511 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7512 it->base_level_stop = 0;
7513 handle_stop_backwards (it, it->base_level_stop);
7514 return GET_NEXT_DISPLAY_ELEMENT (it);
7515 }
7516 }
7517
7518 if (it->current.overlay_string_index >= 0)
7519 {
7520 /* Get the next character from an overlay string. In overlay
7521 strings, there is no field width or padding with spaces to
7522 do. */
7523 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7524 {
7525 it->what = IT_EOB;
7526 return 0;
7527 }
7528 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7529 IT_STRING_BYTEPOS (*it),
7530 it->bidi_it.scan_dir < 0
7531 ? -1
7532 : SCHARS (it->string))
7533 && next_element_from_composition (it))
7534 {
7535 return 1;
7536 }
7537 else if (STRING_MULTIBYTE (it->string))
7538 {
7539 const unsigned char *s = (SDATA (it->string)
7540 + IT_STRING_BYTEPOS (*it));
7541 it->c = string_char_and_length (s, &it->len);
7542 }
7543 else
7544 {
7545 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7546 it->len = 1;
7547 }
7548 }
7549 else
7550 {
7551 /* Get the next character from a Lisp string that is not an
7552 overlay string. Such strings come from the mode line, for
7553 example. We may have to pad with spaces, or truncate the
7554 string. See also next_element_from_c_string. */
7555 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7556 {
7557 it->what = IT_EOB;
7558 return 0;
7559 }
7560 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7561 {
7562 /* Pad with spaces. */
7563 it->c = ' ', it->len = 1;
7564 CHARPOS (position) = BYTEPOS (position) = -1;
7565 }
7566 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7567 IT_STRING_BYTEPOS (*it),
7568 it->bidi_it.scan_dir < 0
7569 ? -1
7570 : it->string_nchars)
7571 && next_element_from_composition (it))
7572 {
7573 return 1;
7574 }
7575 else if (STRING_MULTIBYTE (it->string))
7576 {
7577 const unsigned char *s = (SDATA (it->string)
7578 + IT_STRING_BYTEPOS (*it));
7579 it->c = string_char_and_length (s, &it->len);
7580 }
7581 else
7582 {
7583 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7584 it->len = 1;
7585 }
7586 }
7587
7588 /* Record what we have and where it came from. */
7589 it->what = IT_CHARACTER;
7590 it->object = it->string;
7591 it->position = position;
7592 return 1;
7593 }
7594
7595
7596 /* Load IT with next display element from C string IT->s.
7597 IT->string_nchars is the maximum number of characters to return
7598 from the string. IT->end_charpos may be greater than
7599 IT->string_nchars when this function is called, in which case we
7600 may have to return padding spaces. Value is zero if end of string
7601 reached, including padding spaces. */
7602
7603 static int
7604 next_element_from_c_string (struct it *it)
7605 {
7606 int success_p = 1;
7607
7608 eassert (it->s);
7609 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7610 it->what = IT_CHARACTER;
7611 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7612 it->object = Qnil;
7613
7614 /* With bidi reordering, the character to display might not be the
7615 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7616 we were reseated to a new string, whose paragraph direction is
7617 not known. */
7618 if (it->bidi_p && it->bidi_it.first_elt)
7619 get_visually_first_element (it);
7620
7621 /* IT's position can be greater than IT->string_nchars in case a
7622 field width or precision has been specified when the iterator was
7623 initialized. */
7624 if (IT_CHARPOS (*it) >= it->end_charpos)
7625 {
7626 /* End of the game. */
7627 it->what = IT_EOB;
7628 success_p = 0;
7629 }
7630 else if (IT_CHARPOS (*it) >= it->string_nchars)
7631 {
7632 /* Pad with spaces. */
7633 it->c = ' ', it->len = 1;
7634 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7635 }
7636 else if (it->multibyte_p)
7637 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7638 else
7639 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7640
7641 return success_p;
7642 }
7643
7644
7645 /* Set up IT to return characters from an ellipsis, if appropriate.
7646 The definition of the ellipsis glyphs may come from a display table
7647 entry. This function fills IT with the first glyph from the
7648 ellipsis if an ellipsis is to be displayed. */
7649
7650 static int
7651 next_element_from_ellipsis (struct it *it)
7652 {
7653 if (it->selective_display_ellipsis_p)
7654 setup_for_ellipsis (it, it->len);
7655 else
7656 {
7657 /* The face at the current position may be different from the
7658 face we find after the invisible text. Remember what it
7659 was in IT->saved_face_id, and signal that it's there by
7660 setting face_before_selective_p. */
7661 it->saved_face_id = it->face_id;
7662 it->method = GET_FROM_BUFFER;
7663 it->object = WGET (it->w, buffer);
7664 reseat_at_next_visible_line_start (it, 1);
7665 it->face_before_selective_p = 1;
7666 }
7667
7668 return GET_NEXT_DISPLAY_ELEMENT (it);
7669 }
7670
7671
7672 /* Deliver an image display element. The iterator IT is already
7673 filled with image information (done in handle_display_prop). Value
7674 is always 1. */
7675
7676
7677 static int
7678 next_element_from_image (struct it *it)
7679 {
7680 it->what = IT_IMAGE;
7681 it->ignore_overlay_strings_at_pos_p = 0;
7682 return 1;
7683 }
7684
7685
7686 /* Fill iterator IT with next display element from a stretch glyph
7687 property. IT->object is the value of the text property. Value is
7688 always 1. */
7689
7690 static int
7691 next_element_from_stretch (struct it *it)
7692 {
7693 it->what = IT_STRETCH;
7694 return 1;
7695 }
7696
7697 /* Scan backwards from IT's current position until we find a stop
7698 position, or until BEGV. This is called when we find ourself
7699 before both the last known prev_stop and base_level_stop while
7700 reordering bidirectional text. */
7701
7702 static void
7703 compute_stop_pos_backwards (struct it *it)
7704 {
7705 const int SCAN_BACK_LIMIT = 1000;
7706 struct text_pos pos;
7707 struct display_pos save_current = it->current;
7708 struct text_pos save_position = it->position;
7709 ptrdiff_t charpos = IT_CHARPOS (*it);
7710 ptrdiff_t where_we_are = charpos;
7711 ptrdiff_t save_stop_pos = it->stop_charpos;
7712 ptrdiff_t save_end_pos = it->end_charpos;
7713
7714 eassert (NILP (it->string) && !it->s);
7715 eassert (it->bidi_p);
7716 it->bidi_p = 0;
7717 do
7718 {
7719 it->end_charpos = min (charpos + 1, ZV);
7720 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7721 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7722 reseat_1 (it, pos, 0);
7723 compute_stop_pos (it);
7724 /* We must advance forward, right? */
7725 if (it->stop_charpos <= charpos)
7726 abort ();
7727 }
7728 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7729
7730 if (it->stop_charpos <= where_we_are)
7731 it->prev_stop = it->stop_charpos;
7732 else
7733 it->prev_stop = BEGV;
7734 it->bidi_p = 1;
7735 it->current = save_current;
7736 it->position = save_position;
7737 it->stop_charpos = save_stop_pos;
7738 it->end_charpos = save_end_pos;
7739 }
7740
7741 /* Scan forward from CHARPOS in the current buffer/string, until we
7742 find a stop position > current IT's position. Then handle the stop
7743 position before that. This is called when we bump into a stop
7744 position while reordering bidirectional text. CHARPOS should be
7745 the last previously processed stop_pos (or BEGV/0, if none were
7746 processed yet) whose position is less that IT's current
7747 position. */
7748
7749 static void
7750 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7751 {
7752 int bufp = !STRINGP (it->string);
7753 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7754 struct display_pos save_current = it->current;
7755 struct text_pos save_position = it->position;
7756 struct text_pos pos1;
7757 ptrdiff_t next_stop;
7758
7759 /* Scan in strict logical order. */
7760 eassert (it->bidi_p);
7761 it->bidi_p = 0;
7762 do
7763 {
7764 it->prev_stop = charpos;
7765 if (bufp)
7766 {
7767 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7768 reseat_1 (it, pos1, 0);
7769 }
7770 else
7771 it->current.string_pos = string_pos (charpos, it->string);
7772 compute_stop_pos (it);
7773 /* We must advance forward, right? */
7774 if (it->stop_charpos <= it->prev_stop)
7775 abort ();
7776 charpos = it->stop_charpos;
7777 }
7778 while (charpos <= where_we_are);
7779
7780 it->bidi_p = 1;
7781 it->current = save_current;
7782 it->position = save_position;
7783 next_stop = it->stop_charpos;
7784 it->stop_charpos = it->prev_stop;
7785 handle_stop (it);
7786 it->stop_charpos = next_stop;
7787 }
7788
7789 /* Load IT with the next display element from current_buffer. Value
7790 is zero if end of buffer reached. IT->stop_charpos is the next
7791 position at which to stop and check for text properties or buffer
7792 end. */
7793
7794 static int
7795 next_element_from_buffer (struct it *it)
7796 {
7797 int success_p = 1;
7798
7799 eassert (IT_CHARPOS (*it) >= BEGV);
7800 eassert (NILP (it->string) && !it->s);
7801 eassert (!it->bidi_p
7802 || (EQ (it->bidi_it.string.lstring, Qnil)
7803 && it->bidi_it.string.s == NULL));
7804
7805 /* With bidi reordering, the character to display might not be the
7806 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7807 we were reseat()ed to a new buffer position, which is potentially
7808 a different paragraph. */
7809 if (it->bidi_p && it->bidi_it.first_elt)
7810 {
7811 get_visually_first_element (it);
7812 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7813 }
7814
7815 if (IT_CHARPOS (*it) >= it->stop_charpos)
7816 {
7817 if (IT_CHARPOS (*it) >= it->end_charpos)
7818 {
7819 int overlay_strings_follow_p;
7820
7821 /* End of the game, except when overlay strings follow that
7822 haven't been returned yet. */
7823 if (it->overlay_strings_at_end_processed_p)
7824 overlay_strings_follow_p = 0;
7825 else
7826 {
7827 it->overlay_strings_at_end_processed_p = 1;
7828 overlay_strings_follow_p = get_overlay_strings (it, 0);
7829 }
7830
7831 if (overlay_strings_follow_p)
7832 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7833 else
7834 {
7835 it->what = IT_EOB;
7836 it->position = it->current.pos;
7837 success_p = 0;
7838 }
7839 }
7840 else if (!(!it->bidi_p
7841 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7842 || IT_CHARPOS (*it) == it->stop_charpos))
7843 {
7844 /* With bidi non-linear iteration, we could find ourselves
7845 far beyond the last computed stop_charpos, with several
7846 other stop positions in between that we missed. Scan
7847 them all now, in buffer's logical order, until we find
7848 and handle the last stop_charpos that precedes our
7849 current position. */
7850 handle_stop_backwards (it, it->stop_charpos);
7851 return GET_NEXT_DISPLAY_ELEMENT (it);
7852 }
7853 else
7854 {
7855 if (it->bidi_p)
7856 {
7857 /* Take note of the stop position we just moved across,
7858 for when we will move back across it. */
7859 it->prev_stop = it->stop_charpos;
7860 /* If we are at base paragraph embedding level, take
7861 note of the last stop position seen at this
7862 level. */
7863 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7864 it->base_level_stop = it->stop_charpos;
7865 }
7866 handle_stop (it);
7867 return GET_NEXT_DISPLAY_ELEMENT (it);
7868 }
7869 }
7870 else if (it->bidi_p
7871 /* If we are before prev_stop, we may have overstepped on
7872 our way backwards a stop_pos, and if so, we need to
7873 handle that stop_pos. */
7874 && IT_CHARPOS (*it) < it->prev_stop
7875 /* We can sometimes back up for reasons that have nothing
7876 to do with bidi reordering. E.g., compositions. The
7877 code below is only needed when we are above the base
7878 embedding level, so test for that explicitly. */
7879 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7880 {
7881 if (it->base_level_stop <= 0
7882 || IT_CHARPOS (*it) < it->base_level_stop)
7883 {
7884 /* If we lost track of base_level_stop, we need to find
7885 prev_stop by looking backwards. This happens, e.g., when
7886 we were reseated to the previous screenful of text by
7887 vertical-motion. */
7888 it->base_level_stop = BEGV;
7889 compute_stop_pos_backwards (it);
7890 handle_stop_backwards (it, it->prev_stop);
7891 }
7892 else
7893 handle_stop_backwards (it, it->base_level_stop);
7894 return GET_NEXT_DISPLAY_ELEMENT (it);
7895 }
7896 else
7897 {
7898 /* No face changes, overlays etc. in sight, so just return a
7899 character from current_buffer. */
7900 unsigned char *p;
7901 ptrdiff_t stop;
7902
7903 /* Maybe run the redisplay end trigger hook. Performance note:
7904 This doesn't seem to cost measurable time. */
7905 if (it->redisplay_end_trigger_charpos
7906 && it->glyph_row
7907 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7908 run_redisplay_end_trigger_hook (it);
7909
7910 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7911 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7912 stop)
7913 && next_element_from_composition (it))
7914 {
7915 return 1;
7916 }
7917
7918 /* Get the next character, maybe multibyte. */
7919 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7920 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7921 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7922 else
7923 it->c = *p, it->len = 1;
7924
7925 /* Record what we have and where it came from. */
7926 it->what = IT_CHARACTER;
7927 it->object = WGET (it->w, buffer);
7928 it->position = it->current.pos;
7929
7930 /* Normally we return the character found above, except when we
7931 really want to return an ellipsis for selective display. */
7932 if (it->selective)
7933 {
7934 if (it->c == '\n')
7935 {
7936 /* A value of selective > 0 means hide lines indented more
7937 than that number of columns. */
7938 if (it->selective > 0
7939 && IT_CHARPOS (*it) + 1 < ZV
7940 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7941 IT_BYTEPOS (*it) + 1,
7942 it->selective))
7943 {
7944 success_p = next_element_from_ellipsis (it);
7945 it->dpvec_char_len = -1;
7946 }
7947 }
7948 else if (it->c == '\r' && it->selective == -1)
7949 {
7950 /* A value of selective == -1 means that everything from the
7951 CR to the end of the line is invisible, with maybe an
7952 ellipsis displayed for it. */
7953 success_p = next_element_from_ellipsis (it);
7954 it->dpvec_char_len = -1;
7955 }
7956 }
7957 }
7958
7959 /* Value is zero if end of buffer reached. */
7960 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7961 return success_p;
7962 }
7963
7964
7965 /* Run the redisplay end trigger hook for IT. */
7966
7967 static void
7968 run_redisplay_end_trigger_hook (struct it *it)
7969 {
7970 Lisp_Object args[3];
7971
7972 /* IT->glyph_row should be non-null, i.e. we should be actually
7973 displaying something, or otherwise we should not run the hook. */
7974 eassert (it->glyph_row);
7975
7976 /* Set up hook arguments. */
7977 args[0] = Qredisplay_end_trigger_functions;
7978 args[1] = it->window;
7979 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7980 it->redisplay_end_trigger_charpos = 0;
7981
7982 /* Since we are *trying* to run these functions, don't try to run
7983 them again, even if they get an error. */
7984 WSET (it->w, redisplay_end_trigger, Qnil);
7985 Frun_hook_with_args (3, args);
7986
7987 /* Notice if it changed the face of the character we are on. */
7988 handle_face_prop (it);
7989 }
7990
7991
7992 /* Deliver a composition display element. Unlike the other
7993 next_element_from_XXX, this function is not registered in the array
7994 get_next_element[]. It is called from next_element_from_buffer and
7995 next_element_from_string when necessary. */
7996
7997 static int
7998 next_element_from_composition (struct it *it)
7999 {
8000 it->what = IT_COMPOSITION;
8001 it->len = it->cmp_it.nbytes;
8002 if (STRINGP (it->string))
8003 {
8004 if (it->c < 0)
8005 {
8006 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8007 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8008 return 0;
8009 }
8010 it->position = it->current.string_pos;
8011 it->object = it->string;
8012 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8013 IT_STRING_BYTEPOS (*it), it->string);
8014 }
8015 else
8016 {
8017 if (it->c < 0)
8018 {
8019 IT_CHARPOS (*it) += it->cmp_it.nchars;
8020 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8021 if (it->bidi_p)
8022 {
8023 if (it->bidi_it.new_paragraph)
8024 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8025 /* Resync the bidi iterator with IT's new position.
8026 FIXME: this doesn't support bidirectional text. */
8027 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8028 bidi_move_to_visually_next (&it->bidi_it);
8029 }
8030 return 0;
8031 }
8032 it->position = it->current.pos;
8033 it->object = WGET (it->w, buffer);
8034 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8035 IT_BYTEPOS (*it), Qnil);
8036 }
8037 return 1;
8038 }
8039
8040
8041 \f
8042 /***********************************************************************
8043 Moving an iterator without producing glyphs
8044 ***********************************************************************/
8045
8046 /* Check if iterator is at a position corresponding to a valid buffer
8047 position after some move_it_ call. */
8048
8049 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8050 ((it)->method == GET_FROM_STRING \
8051 ? IT_STRING_CHARPOS (*it) == 0 \
8052 : 1)
8053
8054
8055 /* Move iterator IT to a specified buffer or X position within one
8056 line on the display without producing glyphs.
8057
8058 OP should be a bit mask including some or all of these bits:
8059 MOVE_TO_X: Stop upon reaching x-position TO_X.
8060 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8061 Regardless of OP's value, stop upon reaching the end of the display line.
8062
8063 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8064 This means, in particular, that TO_X includes window's horizontal
8065 scroll amount.
8066
8067 The return value has several possible values that
8068 say what condition caused the scan to stop:
8069
8070 MOVE_POS_MATCH_OR_ZV
8071 - when TO_POS or ZV was reached.
8072
8073 MOVE_X_REACHED
8074 -when TO_X was reached before TO_POS or ZV were reached.
8075
8076 MOVE_LINE_CONTINUED
8077 - when we reached the end of the display area and the line must
8078 be continued.
8079
8080 MOVE_LINE_TRUNCATED
8081 - when we reached the end of the display area and the line is
8082 truncated.
8083
8084 MOVE_NEWLINE_OR_CR
8085 - when we stopped at a line end, i.e. a newline or a CR and selective
8086 display is on. */
8087
8088 static enum move_it_result
8089 move_it_in_display_line_to (struct it *it,
8090 ptrdiff_t to_charpos, int to_x,
8091 enum move_operation_enum op)
8092 {
8093 enum move_it_result result = MOVE_UNDEFINED;
8094 struct glyph_row *saved_glyph_row;
8095 struct it wrap_it, atpos_it, atx_it, ppos_it;
8096 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8097 void *ppos_data = NULL;
8098 int may_wrap = 0;
8099 enum it_method prev_method = it->method;
8100 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8101 int saw_smaller_pos = prev_pos < to_charpos;
8102
8103 /* Don't produce glyphs in produce_glyphs. */
8104 saved_glyph_row = it->glyph_row;
8105 it->glyph_row = NULL;
8106
8107 /* Use wrap_it to save a copy of IT wherever a word wrap could
8108 occur. Use atpos_it to save a copy of IT at the desired buffer
8109 position, if found, so that we can scan ahead and check if the
8110 word later overshoots the window edge. Use atx_it similarly, for
8111 pixel positions. */
8112 wrap_it.sp = -1;
8113 atpos_it.sp = -1;
8114 atx_it.sp = -1;
8115
8116 /* Use ppos_it under bidi reordering to save a copy of IT for the
8117 position > CHARPOS that is the closest to CHARPOS. We restore
8118 that position in IT when we have scanned the entire display line
8119 without finding a match for CHARPOS and all the character
8120 positions are greater than CHARPOS. */
8121 if (it->bidi_p)
8122 {
8123 SAVE_IT (ppos_it, *it, ppos_data);
8124 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8125 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8126 SAVE_IT (ppos_it, *it, ppos_data);
8127 }
8128
8129 #define BUFFER_POS_REACHED_P() \
8130 ((op & MOVE_TO_POS) != 0 \
8131 && BUFFERP (it->object) \
8132 && (IT_CHARPOS (*it) == to_charpos \
8133 || ((!it->bidi_p \
8134 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8135 && IT_CHARPOS (*it) > to_charpos) \
8136 || (it->what == IT_COMPOSITION \
8137 && ((IT_CHARPOS (*it) > to_charpos \
8138 && to_charpos >= it->cmp_it.charpos) \
8139 || (IT_CHARPOS (*it) < to_charpos \
8140 && to_charpos <= it->cmp_it.charpos)))) \
8141 && (it->method == GET_FROM_BUFFER \
8142 || (it->method == GET_FROM_DISPLAY_VECTOR \
8143 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8144
8145 /* If there's a line-/wrap-prefix, handle it. */
8146 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8147 && it->current_y < it->last_visible_y)
8148 handle_line_prefix (it);
8149
8150 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8151 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8152
8153 while (1)
8154 {
8155 int x, i, ascent = 0, descent = 0;
8156
8157 /* Utility macro to reset an iterator with x, ascent, and descent. */
8158 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8159 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8160 (IT)->max_descent = descent)
8161
8162 /* Stop if we move beyond TO_CHARPOS (after an image or a
8163 display string or stretch glyph). */
8164 if ((op & MOVE_TO_POS) != 0
8165 && BUFFERP (it->object)
8166 && it->method == GET_FROM_BUFFER
8167 && (((!it->bidi_p
8168 /* When the iterator is at base embedding level, we
8169 are guaranteed that characters are delivered for
8170 display in strictly increasing order of their
8171 buffer positions. */
8172 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8173 && IT_CHARPOS (*it) > to_charpos)
8174 || (it->bidi_p
8175 && (prev_method == GET_FROM_IMAGE
8176 || prev_method == GET_FROM_STRETCH
8177 || prev_method == GET_FROM_STRING)
8178 /* Passed TO_CHARPOS from left to right. */
8179 && ((prev_pos < to_charpos
8180 && IT_CHARPOS (*it) > to_charpos)
8181 /* Passed TO_CHARPOS from right to left. */
8182 || (prev_pos > to_charpos
8183 && IT_CHARPOS (*it) < to_charpos)))))
8184 {
8185 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8186 {
8187 result = MOVE_POS_MATCH_OR_ZV;
8188 break;
8189 }
8190 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8191 /* If wrap_it is valid, the current position might be in a
8192 word that is wrapped. So, save the iterator in
8193 atpos_it and continue to see if wrapping happens. */
8194 SAVE_IT (atpos_it, *it, atpos_data);
8195 }
8196
8197 /* Stop when ZV reached.
8198 We used to stop here when TO_CHARPOS reached as well, but that is
8199 too soon if this glyph does not fit on this line. So we handle it
8200 explicitly below. */
8201 if (!get_next_display_element (it))
8202 {
8203 result = MOVE_POS_MATCH_OR_ZV;
8204 break;
8205 }
8206
8207 if (it->line_wrap == TRUNCATE)
8208 {
8209 if (BUFFER_POS_REACHED_P ())
8210 {
8211 result = MOVE_POS_MATCH_OR_ZV;
8212 break;
8213 }
8214 }
8215 else
8216 {
8217 if (it->line_wrap == WORD_WRAP)
8218 {
8219 if (IT_DISPLAYING_WHITESPACE (it))
8220 may_wrap = 1;
8221 else if (may_wrap)
8222 {
8223 /* We have reached a glyph that follows one or more
8224 whitespace characters. If the position is
8225 already found, we are done. */
8226 if (atpos_it.sp >= 0)
8227 {
8228 RESTORE_IT (it, &atpos_it, atpos_data);
8229 result = MOVE_POS_MATCH_OR_ZV;
8230 goto done;
8231 }
8232 if (atx_it.sp >= 0)
8233 {
8234 RESTORE_IT (it, &atx_it, atx_data);
8235 result = MOVE_X_REACHED;
8236 goto done;
8237 }
8238 /* Otherwise, we can wrap here. */
8239 SAVE_IT (wrap_it, *it, wrap_data);
8240 may_wrap = 0;
8241 }
8242 }
8243 }
8244
8245 /* Remember the line height for the current line, in case
8246 the next element doesn't fit on the line. */
8247 ascent = it->max_ascent;
8248 descent = it->max_descent;
8249
8250 /* The call to produce_glyphs will get the metrics of the
8251 display element IT is loaded with. Record the x-position
8252 before this display element, in case it doesn't fit on the
8253 line. */
8254 x = it->current_x;
8255
8256 PRODUCE_GLYPHS (it);
8257
8258 if (it->area != TEXT_AREA)
8259 {
8260 prev_method = it->method;
8261 if (it->method == GET_FROM_BUFFER)
8262 prev_pos = IT_CHARPOS (*it);
8263 set_iterator_to_next (it, 1);
8264 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8265 SET_TEXT_POS (this_line_min_pos,
8266 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8267 if (it->bidi_p
8268 && (op & MOVE_TO_POS)
8269 && IT_CHARPOS (*it) > to_charpos
8270 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8271 SAVE_IT (ppos_it, *it, ppos_data);
8272 continue;
8273 }
8274
8275 /* The number of glyphs we get back in IT->nglyphs will normally
8276 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8277 character on a terminal frame, or (iii) a line end. For the
8278 second case, IT->nglyphs - 1 padding glyphs will be present.
8279 (On X frames, there is only one glyph produced for a
8280 composite character.)
8281
8282 The behavior implemented below means, for continuation lines,
8283 that as many spaces of a TAB as fit on the current line are
8284 displayed there. For terminal frames, as many glyphs of a
8285 multi-glyph character are displayed in the current line, too.
8286 This is what the old redisplay code did, and we keep it that
8287 way. Under X, the whole shape of a complex character must
8288 fit on the line or it will be completely displayed in the
8289 next line.
8290
8291 Note that both for tabs and padding glyphs, all glyphs have
8292 the same width. */
8293 if (it->nglyphs)
8294 {
8295 /* More than one glyph or glyph doesn't fit on line. All
8296 glyphs have the same width. */
8297 int single_glyph_width = it->pixel_width / it->nglyphs;
8298 int new_x;
8299 int x_before_this_char = x;
8300 int hpos_before_this_char = it->hpos;
8301
8302 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8303 {
8304 new_x = x + single_glyph_width;
8305
8306 /* We want to leave anything reaching TO_X to the caller. */
8307 if ((op & MOVE_TO_X) && new_x > to_x)
8308 {
8309 if (BUFFER_POS_REACHED_P ())
8310 {
8311 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8312 goto buffer_pos_reached;
8313 if (atpos_it.sp < 0)
8314 {
8315 SAVE_IT (atpos_it, *it, atpos_data);
8316 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8317 }
8318 }
8319 else
8320 {
8321 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8322 {
8323 it->current_x = x;
8324 result = MOVE_X_REACHED;
8325 break;
8326 }
8327 if (atx_it.sp < 0)
8328 {
8329 SAVE_IT (atx_it, *it, atx_data);
8330 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8331 }
8332 }
8333 }
8334
8335 if (/* Lines are continued. */
8336 it->line_wrap != TRUNCATE
8337 && (/* And glyph doesn't fit on the line. */
8338 new_x > it->last_visible_x
8339 /* Or it fits exactly and we're on a window
8340 system frame. */
8341 || (new_x == it->last_visible_x
8342 && FRAME_WINDOW_P (it->f)
8343 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8344 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8345 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8346 {
8347 if (/* IT->hpos == 0 means the very first glyph
8348 doesn't fit on the line, e.g. a wide image. */
8349 it->hpos == 0
8350 || (new_x == it->last_visible_x
8351 && FRAME_WINDOW_P (it->f)))
8352 {
8353 ++it->hpos;
8354 it->current_x = new_x;
8355
8356 /* The character's last glyph just barely fits
8357 in this row. */
8358 if (i == it->nglyphs - 1)
8359 {
8360 /* If this is the destination position,
8361 return a position *before* it in this row,
8362 now that we know it fits in this row. */
8363 if (BUFFER_POS_REACHED_P ())
8364 {
8365 if (it->line_wrap != WORD_WRAP
8366 || wrap_it.sp < 0)
8367 {
8368 it->hpos = hpos_before_this_char;
8369 it->current_x = x_before_this_char;
8370 result = MOVE_POS_MATCH_OR_ZV;
8371 break;
8372 }
8373 if (it->line_wrap == WORD_WRAP
8374 && atpos_it.sp < 0)
8375 {
8376 SAVE_IT (atpos_it, *it, atpos_data);
8377 atpos_it.current_x = x_before_this_char;
8378 atpos_it.hpos = hpos_before_this_char;
8379 }
8380 }
8381
8382 prev_method = it->method;
8383 if (it->method == GET_FROM_BUFFER)
8384 prev_pos = IT_CHARPOS (*it);
8385 set_iterator_to_next (it, 1);
8386 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8387 SET_TEXT_POS (this_line_min_pos,
8388 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8389 /* On graphical terminals, newlines may
8390 "overflow" into the fringe if
8391 overflow-newline-into-fringe is non-nil.
8392 On text terminals, and on graphical
8393 terminals with no right margin, newlines
8394 may overflow into the last glyph on the
8395 display line.*/
8396 if (!FRAME_WINDOW_P (it->f)
8397 || ((it->bidi_p
8398 && it->bidi_it.paragraph_dir == R2L)
8399 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8400 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8401 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8402 {
8403 if (!get_next_display_element (it))
8404 {
8405 result = MOVE_POS_MATCH_OR_ZV;
8406 break;
8407 }
8408 if (BUFFER_POS_REACHED_P ())
8409 {
8410 if (ITERATOR_AT_END_OF_LINE_P (it))
8411 result = MOVE_POS_MATCH_OR_ZV;
8412 else
8413 result = MOVE_LINE_CONTINUED;
8414 break;
8415 }
8416 if (ITERATOR_AT_END_OF_LINE_P (it))
8417 {
8418 result = MOVE_NEWLINE_OR_CR;
8419 break;
8420 }
8421 }
8422 }
8423 }
8424 else
8425 IT_RESET_X_ASCENT_DESCENT (it);
8426
8427 if (wrap_it.sp >= 0)
8428 {
8429 RESTORE_IT (it, &wrap_it, wrap_data);
8430 atpos_it.sp = -1;
8431 atx_it.sp = -1;
8432 }
8433
8434 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8435 IT_CHARPOS (*it)));
8436 result = MOVE_LINE_CONTINUED;
8437 break;
8438 }
8439
8440 if (BUFFER_POS_REACHED_P ())
8441 {
8442 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8443 goto buffer_pos_reached;
8444 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8445 {
8446 SAVE_IT (atpos_it, *it, atpos_data);
8447 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8448 }
8449 }
8450
8451 if (new_x > it->first_visible_x)
8452 {
8453 /* Glyph is visible. Increment number of glyphs that
8454 would be displayed. */
8455 ++it->hpos;
8456 }
8457 }
8458
8459 if (result != MOVE_UNDEFINED)
8460 break;
8461 }
8462 else if (BUFFER_POS_REACHED_P ())
8463 {
8464 buffer_pos_reached:
8465 IT_RESET_X_ASCENT_DESCENT (it);
8466 result = MOVE_POS_MATCH_OR_ZV;
8467 break;
8468 }
8469 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8470 {
8471 /* Stop when TO_X specified and reached. This check is
8472 necessary here because of lines consisting of a line end,
8473 only. The line end will not produce any glyphs and we
8474 would never get MOVE_X_REACHED. */
8475 eassert (it->nglyphs == 0);
8476 result = MOVE_X_REACHED;
8477 break;
8478 }
8479
8480 /* Is this a line end? If yes, we're done. */
8481 if (ITERATOR_AT_END_OF_LINE_P (it))
8482 {
8483 /* If we are past TO_CHARPOS, but never saw any character
8484 positions smaller than TO_CHARPOS, return
8485 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8486 did. */
8487 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8488 {
8489 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8490 {
8491 if (IT_CHARPOS (ppos_it) < ZV)
8492 {
8493 RESTORE_IT (it, &ppos_it, ppos_data);
8494 result = MOVE_POS_MATCH_OR_ZV;
8495 }
8496 else
8497 goto buffer_pos_reached;
8498 }
8499 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8500 && IT_CHARPOS (*it) > to_charpos)
8501 goto buffer_pos_reached;
8502 else
8503 result = MOVE_NEWLINE_OR_CR;
8504 }
8505 else
8506 result = MOVE_NEWLINE_OR_CR;
8507 break;
8508 }
8509
8510 prev_method = it->method;
8511 if (it->method == GET_FROM_BUFFER)
8512 prev_pos = IT_CHARPOS (*it);
8513 /* The current display element has been consumed. Advance
8514 to the next. */
8515 set_iterator_to_next (it, 1);
8516 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8517 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8518 if (IT_CHARPOS (*it) < to_charpos)
8519 saw_smaller_pos = 1;
8520 if (it->bidi_p
8521 && (op & MOVE_TO_POS)
8522 && IT_CHARPOS (*it) >= to_charpos
8523 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8524 SAVE_IT (ppos_it, *it, ppos_data);
8525
8526 /* Stop if lines are truncated and IT's current x-position is
8527 past the right edge of the window now. */
8528 if (it->line_wrap == TRUNCATE
8529 && it->current_x >= it->last_visible_x)
8530 {
8531 if (!FRAME_WINDOW_P (it->f)
8532 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8533 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8534 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8535 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8536 {
8537 int at_eob_p = 0;
8538
8539 if ((at_eob_p = !get_next_display_element (it))
8540 || BUFFER_POS_REACHED_P ()
8541 /* If we are past TO_CHARPOS, but never saw any
8542 character positions smaller than TO_CHARPOS,
8543 return MOVE_POS_MATCH_OR_ZV, like the
8544 unidirectional display did. */
8545 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8546 && !saw_smaller_pos
8547 && IT_CHARPOS (*it) > to_charpos))
8548 {
8549 if (it->bidi_p
8550 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8551 RESTORE_IT (it, &ppos_it, ppos_data);
8552 result = MOVE_POS_MATCH_OR_ZV;
8553 break;
8554 }
8555 if (ITERATOR_AT_END_OF_LINE_P (it))
8556 {
8557 result = MOVE_NEWLINE_OR_CR;
8558 break;
8559 }
8560 }
8561 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8562 && !saw_smaller_pos
8563 && IT_CHARPOS (*it) > to_charpos)
8564 {
8565 if (IT_CHARPOS (ppos_it) < ZV)
8566 RESTORE_IT (it, &ppos_it, ppos_data);
8567 result = MOVE_POS_MATCH_OR_ZV;
8568 break;
8569 }
8570 result = MOVE_LINE_TRUNCATED;
8571 break;
8572 }
8573 #undef IT_RESET_X_ASCENT_DESCENT
8574 }
8575
8576 #undef BUFFER_POS_REACHED_P
8577
8578 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8579 restore the saved iterator. */
8580 if (atpos_it.sp >= 0)
8581 RESTORE_IT (it, &atpos_it, atpos_data);
8582 else if (atx_it.sp >= 0)
8583 RESTORE_IT (it, &atx_it, atx_data);
8584
8585 done:
8586
8587 if (atpos_data)
8588 bidi_unshelve_cache (atpos_data, 1);
8589 if (atx_data)
8590 bidi_unshelve_cache (atx_data, 1);
8591 if (wrap_data)
8592 bidi_unshelve_cache (wrap_data, 1);
8593 if (ppos_data)
8594 bidi_unshelve_cache (ppos_data, 1);
8595
8596 /* Restore the iterator settings altered at the beginning of this
8597 function. */
8598 it->glyph_row = saved_glyph_row;
8599 return result;
8600 }
8601
8602 /* For external use. */
8603 void
8604 move_it_in_display_line (struct it *it,
8605 ptrdiff_t to_charpos, int to_x,
8606 enum move_operation_enum op)
8607 {
8608 if (it->line_wrap == WORD_WRAP
8609 && (op & MOVE_TO_X))
8610 {
8611 struct it save_it;
8612 void *save_data = NULL;
8613 int skip;
8614
8615 SAVE_IT (save_it, *it, save_data);
8616 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8617 /* When word-wrap is on, TO_X may lie past the end
8618 of a wrapped line. Then it->current is the
8619 character on the next line, so backtrack to the
8620 space before the wrap point. */
8621 if (skip == MOVE_LINE_CONTINUED)
8622 {
8623 int prev_x = max (it->current_x - 1, 0);
8624 RESTORE_IT (it, &save_it, save_data);
8625 move_it_in_display_line_to
8626 (it, -1, prev_x, MOVE_TO_X);
8627 }
8628 else
8629 bidi_unshelve_cache (save_data, 1);
8630 }
8631 else
8632 move_it_in_display_line_to (it, to_charpos, to_x, op);
8633 }
8634
8635
8636 /* Move IT forward until it satisfies one or more of the criteria in
8637 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8638
8639 OP is a bit-mask that specifies where to stop, and in particular,
8640 which of those four position arguments makes a difference. See the
8641 description of enum move_operation_enum.
8642
8643 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8644 screen line, this function will set IT to the next position that is
8645 displayed to the right of TO_CHARPOS on the screen. */
8646
8647 void
8648 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8649 {
8650 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8651 int line_height, line_start_x = 0, reached = 0;
8652 void *backup_data = NULL;
8653
8654 for (;;)
8655 {
8656 if (op & MOVE_TO_VPOS)
8657 {
8658 /* If no TO_CHARPOS and no TO_X specified, stop at the
8659 start of the line TO_VPOS. */
8660 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8661 {
8662 if (it->vpos == to_vpos)
8663 {
8664 reached = 1;
8665 break;
8666 }
8667 else
8668 skip = move_it_in_display_line_to (it, -1, -1, 0);
8669 }
8670 else
8671 {
8672 /* TO_VPOS >= 0 means stop at TO_X in the line at
8673 TO_VPOS, or at TO_POS, whichever comes first. */
8674 if (it->vpos == to_vpos)
8675 {
8676 reached = 2;
8677 break;
8678 }
8679
8680 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8681
8682 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8683 {
8684 reached = 3;
8685 break;
8686 }
8687 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8688 {
8689 /* We have reached TO_X but not in the line we want. */
8690 skip = move_it_in_display_line_to (it, to_charpos,
8691 -1, MOVE_TO_POS);
8692 if (skip == MOVE_POS_MATCH_OR_ZV)
8693 {
8694 reached = 4;
8695 break;
8696 }
8697 }
8698 }
8699 }
8700 else if (op & MOVE_TO_Y)
8701 {
8702 struct it it_backup;
8703
8704 if (it->line_wrap == WORD_WRAP)
8705 SAVE_IT (it_backup, *it, backup_data);
8706
8707 /* TO_Y specified means stop at TO_X in the line containing
8708 TO_Y---or at TO_CHARPOS if this is reached first. The
8709 problem is that we can't really tell whether the line
8710 contains TO_Y before we have completely scanned it, and
8711 this may skip past TO_X. What we do is to first scan to
8712 TO_X.
8713
8714 If TO_X is not specified, use a TO_X of zero. The reason
8715 is to make the outcome of this function more predictable.
8716 If we didn't use TO_X == 0, we would stop at the end of
8717 the line which is probably not what a caller would expect
8718 to happen. */
8719 skip = move_it_in_display_line_to
8720 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8721 (MOVE_TO_X | (op & MOVE_TO_POS)));
8722
8723 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8724 if (skip == MOVE_POS_MATCH_OR_ZV)
8725 reached = 5;
8726 else if (skip == MOVE_X_REACHED)
8727 {
8728 /* If TO_X was reached, we want to know whether TO_Y is
8729 in the line. We know this is the case if the already
8730 scanned glyphs make the line tall enough. Otherwise,
8731 we must check by scanning the rest of the line. */
8732 line_height = it->max_ascent + it->max_descent;
8733 if (to_y >= it->current_y
8734 && to_y < it->current_y + line_height)
8735 {
8736 reached = 6;
8737 break;
8738 }
8739 SAVE_IT (it_backup, *it, backup_data);
8740 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8741 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8742 op & MOVE_TO_POS);
8743 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8744 line_height = it->max_ascent + it->max_descent;
8745 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8746
8747 if (to_y >= it->current_y
8748 && to_y < it->current_y + line_height)
8749 {
8750 /* If TO_Y is in this line and TO_X was reached
8751 above, we scanned too far. We have to restore
8752 IT's settings to the ones before skipping. But
8753 keep the more accurate values of max_ascent and
8754 max_descent we've found while skipping the rest
8755 of the line, for the sake of callers, such as
8756 pos_visible_p, that need to know the line
8757 height. */
8758 int max_ascent = it->max_ascent;
8759 int max_descent = it->max_descent;
8760
8761 RESTORE_IT (it, &it_backup, backup_data);
8762 it->max_ascent = max_ascent;
8763 it->max_descent = max_descent;
8764 reached = 6;
8765 }
8766 else
8767 {
8768 skip = skip2;
8769 if (skip == MOVE_POS_MATCH_OR_ZV)
8770 reached = 7;
8771 }
8772 }
8773 else
8774 {
8775 /* Check whether TO_Y is in this line. */
8776 line_height = it->max_ascent + it->max_descent;
8777 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8778
8779 if (to_y >= it->current_y
8780 && to_y < it->current_y + line_height)
8781 {
8782 /* When word-wrap is on, TO_X may lie past the end
8783 of a wrapped line. Then it->current is the
8784 character on the next line, so backtrack to the
8785 space before the wrap point. */
8786 if (skip == MOVE_LINE_CONTINUED
8787 && it->line_wrap == WORD_WRAP)
8788 {
8789 int prev_x = max (it->current_x - 1, 0);
8790 RESTORE_IT (it, &it_backup, backup_data);
8791 skip = move_it_in_display_line_to
8792 (it, -1, prev_x, MOVE_TO_X);
8793 }
8794 reached = 6;
8795 }
8796 }
8797
8798 if (reached)
8799 break;
8800 }
8801 else if (BUFFERP (it->object)
8802 && (it->method == GET_FROM_BUFFER
8803 || it->method == GET_FROM_STRETCH)
8804 && IT_CHARPOS (*it) >= to_charpos
8805 /* Under bidi iteration, a call to set_iterator_to_next
8806 can scan far beyond to_charpos if the initial
8807 portion of the next line needs to be reordered. In
8808 that case, give move_it_in_display_line_to another
8809 chance below. */
8810 && !(it->bidi_p
8811 && it->bidi_it.scan_dir == -1))
8812 skip = MOVE_POS_MATCH_OR_ZV;
8813 else
8814 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8815
8816 switch (skip)
8817 {
8818 case MOVE_POS_MATCH_OR_ZV:
8819 reached = 8;
8820 goto out;
8821
8822 case MOVE_NEWLINE_OR_CR:
8823 set_iterator_to_next (it, 1);
8824 it->continuation_lines_width = 0;
8825 break;
8826
8827 case MOVE_LINE_TRUNCATED:
8828 it->continuation_lines_width = 0;
8829 reseat_at_next_visible_line_start (it, 0);
8830 if ((op & MOVE_TO_POS) != 0
8831 && IT_CHARPOS (*it) > to_charpos)
8832 {
8833 reached = 9;
8834 goto out;
8835 }
8836 break;
8837
8838 case MOVE_LINE_CONTINUED:
8839 /* For continued lines ending in a tab, some of the glyphs
8840 associated with the tab are displayed on the current
8841 line. Since it->current_x does not include these glyphs,
8842 we use it->last_visible_x instead. */
8843 if (it->c == '\t')
8844 {
8845 it->continuation_lines_width += it->last_visible_x;
8846 /* When moving by vpos, ensure that the iterator really
8847 advances to the next line (bug#847, bug#969). Fixme:
8848 do we need to do this in other circumstances? */
8849 if (it->current_x != it->last_visible_x
8850 && (op & MOVE_TO_VPOS)
8851 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8852 {
8853 line_start_x = it->current_x + it->pixel_width
8854 - it->last_visible_x;
8855 set_iterator_to_next (it, 0);
8856 }
8857 }
8858 else
8859 it->continuation_lines_width += it->current_x;
8860 break;
8861
8862 default:
8863 abort ();
8864 }
8865
8866 /* Reset/increment for the next run. */
8867 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8868 it->current_x = line_start_x;
8869 line_start_x = 0;
8870 it->hpos = 0;
8871 it->current_y += it->max_ascent + it->max_descent;
8872 ++it->vpos;
8873 last_height = it->max_ascent + it->max_descent;
8874 last_max_ascent = it->max_ascent;
8875 it->max_ascent = it->max_descent = 0;
8876 }
8877
8878 out:
8879
8880 /* On text terminals, we may stop at the end of a line in the middle
8881 of a multi-character glyph. If the glyph itself is continued,
8882 i.e. it is actually displayed on the next line, don't treat this
8883 stopping point as valid; move to the next line instead (unless
8884 that brings us offscreen). */
8885 if (!FRAME_WINDOW_P (it->f)
8886 && op & MOVE_TO_POS
8887 && IT_CHARPOS (*it) == to_charpos
8888 && it->what == IT_CHARACTER
8889 && it->nglyphs > 1
8890 && it->line_wrap == WINDOW_WRAP
8891 && it->current_x == it->last_visible_x - 1
8892 && it->c != '\n'
8893 && it->c != '\t'
8894 && it->vpos < XFASTINT (WGET (it->w, window_end_vpos)))
8895 {
8896 it->continuation_lines_width += it->current_x;
8897 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8898 it->current_y += it->max_ascent + it->max_descent;
8899 ++it->vpos;
8900 last_height = it->max_ascent + it->max_descent;
8901 last_max_ascent = it->max_ascent;
8902 }
8903
8904 if (backup_data)
8905 bidi_unshelve_cache (backup_data, 1);
8906
8907 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8908 }
8909
8910
8911 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8912
8913 If DY > 0, move IT backward at least that many pixels. DY = 0
8914 means move IT backward to the preceding line start or BEGV. This
8915 function may move over more than DY pixels if IT->current_y - DY
8916 ends up in the middle of a line; in this case IT->current_y will be
8917 set to the top of the line moved to. */
8918
8919 void
8920 move_it_vertically_backward (struct it *it, int dy)
8921 {
8922 int nlines, h;
8923 struct it it2, it3;
8924 void *it2data = NULL, *it3data = NULL;
8925 ptrdiff_t start_pos;
8926
8927 move_further_back:
8928 eassert (dy >= 0);
8929
8930 start_pos = IT_CHARPOS (*it);
8931
8932 /* Estimate how many newlines we must move back. */
8933 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8934
8935 /* Set the iterator's position that many lines back. */
8936 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8937 back_to_previous_visible_line_start (it);
8938
8939 /* Reseat the iterator here. When moving backward, we don't want
8940 reseat to skip forward over invisible text, set up the iterator
8941 to deliver from overlay strings at the new position etc. So,
8942 use reseat_1 here. */
8943 reseat_1 (it, it->current.pos, 1);
8944
8945 /* We are now surely at a line start. */
8946 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8947 reordering is in effect. */
8948 it->continuation_lines_width = 0;
8949
8950 /* Move forward and see what y-distance we moved. First move to the
8951 start of the next line so that we get its height. We need this
8952 height to be able to tell whether we reached the specified
8953 y-distance. */
8954 SAVE_IT (it2, *it, it2data);
8955 it2.max_ascent = it2.max_descent = 0;
8956 do
8957 {
8958 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8959 MOVE_TO_POS | MOVE_TO_VPOS);
8960 }
8961 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8962 /* If we are in a display string which starts at START_POS,
8963 and that display string includes a newline, and we are
8964 right after that newline (i.e. at the beginning of a
8965 display line), exit the loop, because otherwise we will
8966 infloop, since move_it_to will see that it is already at
8967 START_POS and will not move. */
8968 || (it2.method == GET_FROM_STRING
8969 && IT_CHARPOS (it2) == start_pos
8970 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8971 eassert (IT_CHARPOS (*it) >= BEGV);
8972 SAVE_IT (it3, it2, it3data);
8973
8974 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8975 eassert (IT_CHARPOS (*it) >= BEGV);
8976 /* H is the actual vertical distance from the position in *IT
8977 and the starting position. */
8978 h = it2.current_y - it->current_y;
8979 /* NLINES is the distance in number of lines. */
8980 nlines = it2.vpos - it->vpos;
8981
8982 /* Correct IT's y and vpos position
8983 so that they are relative to the starting point. */
8984 it->vpos -= nlines;
8985 it->current_y -= h;
8986
8987 if (dy == 0)
8988 {
8989 /* DY == 0 means move to the start of the screen line. The
8990 value of nlines is > 0 if continuation lines were involved,
8991 or if the original IT position was at start of a line. */
8992 RESTORE_IT (it, it, it2data);
8993 if (nlines > 0)
8994 move_it_by_lines (it, nlines);
8995 /* The above code moves us to some position NLINES down,
8996 usually to its first glyph (leftmost in an L2R line), but
8997 that's not necessarily the start of the line, under bidi
8998 reordering. We want to get to the character position
8999 that is immediately after the newline of the previous
9000 line. */
9001 if (it->bidi_p
9002 && !it->continuation_lines_width
9003 && !STRINGP (it->string)
9004 && IT_CHARPOS (*it) > BEGV
9005 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9006 {
9007 ptrdiff_t nl_pos =
9008 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9009
9010 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9011 }
9012 bidi_unshelve_cache (it3data, 1);
9013 }
9014 else
9015 {
9016 /* The y-position we try to reach, relative to *IT.
9017 Note that H has been subtracted in front of the if-statement. */
9018 int target_y = it->current_y + h - dy;
9019 int y0 = it3.current_y;
9020 int y1;
9021 int line_height;
9022
9023 RESTORE_IT (&it3, &it3, it3data);
9024 y1 = line_bottom_y (&it3);
9025 line_height = y1 - y0;
9026 RESTORE_IT (it, it, it2data);
9027 /* If we did not reach target_y, try to move further backward if
9028 we can. If we moved too far backward, try to move forward. */
9029 if (target_y < it->current_y
9030 /* This is heuristic. In a window that's 3 lines high, with
9031 a line height of 13 pixels each, recentering with point
9032 on the bottom line will try to move -39/2 = 19 pixels
9033 backward. Try to avoid moving into the first line. */
9034 && (it->current_y - target_y
9035 > min (window_box_height (it->w), line_height * 2 / 3))
9036 && IT_CHARPOS (*it) > BEGV)
9037 {
9038 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9039 target_y - it->current_y));
9040 dy = it->current_y - target_y;
9041 goto move_further_back;
9042 }
9043 else if (target_y >= it->current_y + line_height
9044 && IT_CHARPOS (*it) < ZV)
9045 {
9046 /* Should move forward by at least one line, maybe more.
9047
9048 Note: Calling move_it_by_lines can be expensive on
9049 terminal frames, where compute_motion is used (via
9050 vmotion) to do the job, when there are very long lines
9051 and truncate-lines is nil. That's the reason for
9052 treating terminal frames specially here. */
9053
9054 if (!FRAME_WINDOW_P (it->f))
9055 move_it_vertically (it, target_y - (it->current_y + line_height));
9056 else
9057 {
9058 do
9059 {
9060 move_it_by_lines (it, 1);
9061 }
9062 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9063 }
9064 }
9065 }
9066 }
9067
9068
9069 /* Move IT by a specified amount of pixel lines DY. DY negative means
9070 move backwards. DY = 0 means move to start of screen line. At the
9071 end, IT will be on the start of a screen line. */
9072
9073 void
9074 move_it_vertically (struct it *it, int dy)
9075 {
9076 if (dy <= 0)
9077 move_it_vertically_backward (it, -dy);
9078 else
9079 {
9080 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9081 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9082 MOVE_TO_POS | MOVE_TO_Y);
9083 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9084
9085 /* If buffer ends in ZV without a newline, move to the start of
9086 the line to satisfy the post-condition. */
9087 if (IT_CHARPOS (*it) == ZV
9088 && ZV > BEGV
9089 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9090 move_it_by_lines (it, 0);
9091 }
9092 }
9093
9094
9095 /* Move iterator IT past the end of the text line it is in. */
9096
9097 void
9098 move_it_past_eol (struct it *it)
9099 {
9100 enum move_it_result rc;
9101
9102 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9103 if (rc == MOVE_NEWLINE_OR_CR)
9104 set_iterator_to_next (it, 0);
9105 }
9106
9107
9108 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9109 negative means move up. DVPOS == 0 means move to the start of the
9110 screen line.
9111
9112 Optimization idea: If we would know that IT->f doesn't use
9113 a face with proportional font, we could be faster for
9114 truncate-lines nil. */
9115
9116 void
9117 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9118 {
9119
9120 /* The commented-out optimization uses vmotion on terminals. This
9121 gives bad results, because elements like it->what, on which
9122 callers such as pos_visible_p rely, aren't updated. */
9123 /* struct position pos;
9124 if (!FRAME_WINDOW_P (it->f))
9125 {
9126 struct text_pos textpos;
9127
9128 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9129 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9130 reseat (it, textpos, 1);
9131 it->vpos += pos.vpos;
9132 it->current_y += pos.vpos;
9133 }
9134 else */
9135
9136 if (dvpos == 0)
9137 {
9138 /* DVPOS == 0 means move to the start of the screen line. */
9139 move_it_vertically_backward (it, 0);
9140 /* Let next call to line_bottom_y calculate real line height */
9141 last_height = 0;
9142 }
9143 else if (dvpos > 0)
9144 {
9145 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9146 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9147 {
9148 /* Only move to the next buffer position if we ended up in a
9149 string from display property, not in an overlay string
9150 (before-string or after-string). That is because the
9151 latter don't conceal the underlying buffer position, so
9152 we can ask to move the iterator to the exact position we
9153 are interested in. Note that, even if we are already at
9154 IT_CHARPOS (*it), the call below is not a no-op, as it
9155 will detect that we are at the end of the string, pop the
9156 iterator, and compute it->current_x and it->hpos
9157 correctly. */
9158 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9159 -1, -1, -1, MOVE_TO_POS);
9160 }
9161 }
9162 else
9163 {
9164 struct it it2;
9165 void *it2data = NULL;
9166 ptrdiff_t start_charpos, i;
9167
9168 /* Start at the beginning of the screen line containing IT's
9169 position. This may actually move vertically backwards,
9170 in case of overlays, so adjust dvpos accordingly. */
9171 dvpos += it->vpos;
9172 move_it_vertically_backward (it, 0);
9173 dvpos -= it->vpos;
9174
9175 /* Go back -DVPOS visible lines and reseat the iterator there. */
9176 start_charpos = IT_CHARPOS (*it);
9177 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9178 back_to_previous_visible_line_start (it);
9179 reseat (it, it->current.pos, 1);
9180
9181 /* Move further back if we end up in a string or an image. */
9182 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9183 {
9184 /* First try to move to start of display line. */
9185 dvpos += it->vpos;
9186 move_it_vertically_backward (it, 0);
9187 dvpos -= it->vpos;
9188 if (IT_POS_VALID_AFTER_MOVE_P (it))
9189 break;
9190 /* If start of line is still in string or image,
9191 move further back. */
9192 back_to_previous_visible_line_start (it);
9193 reseat (it, it->current.pos, 1);
9194 dvpos--;
9195 }
9196
9197 it->current_x = it->hpos = 0;
9198
9199 /* Above call may have moved too far if continuation lines
9200 are involved. Scan forward and see if it did. */
9201 SAVE_IT (it2, *it, it2data);
9202 it2.vpos = it2.current_y = 0;
9203 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9204 it->vpos -= it2.vpos;
9205 it->current_y -= it2.current_y;
9206 it->current_x = it->hpos = 0;
9207
9208 /* If we moved too far back, move IT some lines forward. */
9209 if (it2.vpos > -dvpos)
9210 {
9211 int delta = it2.vpos + dvpos;
9212
9213 RESTORE_IT (&it2, &it2, it2data);
9214 SAVE_IT (it2, *it, it2data);
9215 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9216 /* Move back again if we got too far ahead. */
9217 if (IT_CHARPOS (*it) >= start_charpos)
9218 RESTORE_IT (it, &it2, it2data);
9219 else
9220 bidi_unshelve_cache (it2data, 1);
9221 }
9222 else
9223 RESTORE_IT (it, it, it2data);
9224 }
9225 }
9226
9227 /* Return 1 if IT points into the middle of a display vector. */
9228
9229 int
9230 in_display_vector_p (struct it *it)
9231 {
9232 return (it->method == GET_FROM_DISPLAY_VECTOR
9233 && it->current.dpvec_index > 0
9234 && it->dpvec + it->current.dpvec_index != it->dpend);
9235 }
9236
9237 \f
9238 /***********************************************************************
9239 Messages
9240 ***********************************************************************/
9241
9242
9243 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9244 to *Messages*. */
9245
9246 void
9247 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9248 {
9249 Lisp_Object args[3];
9250 Lisp_Object msg, fmt;
9251 char *buffer;
9252 ptrdiff_t len;
9253 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9254 USE_SAFE_ALLOCA;
9255
9256 /* Do nothing if called asynchronously. Inserting text into
9257 a buffer may call after-change-functions and alike and
9258 that would means running Lisp asynchronously. */
9259 if (handling_signal)
9260 return;
9261
9262 fmt = msg = Qnil;
9263 GCPRO4 (fmt, msg, arg1, arg2);
9264
9265 args[0] = fmt = build_string (format);
9266 args[1] = arg1;
9267 args[2] = arg2;
9268 msg = Fformat (3, args);
9269
9270 len = SBYTES (msg) + 1;
9271 buffer = SAFE_ALLOCA (len);
9272 memcpy (buffer, SDATA (msg), len);
9273
9274 message_dolog (buffer, len - 1, 1, 0);
9275 SAFE_FREE ();
9276
9277 UNGCPRO;
9278 }
9279
9280
9281 /* Output a newline in the *Messages* buffer if "needs" one. */
9282
9283 void
9284 message_log_maybe_newline (void)
9285 {
9286 if (message_log_need_newline)
9287 message_dolog ("", 0, 1, 0);
9288 }
9289
9290
9291 /* Add a string M of length NBYTES to the message log, optionally
9292 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9293 nonzero, means interpret the contents of M as multibyte. This
9294 function calls low-level routines in order to bypass text property
9295 hooks, etc. which might not be safe to run.
9296
9297 This may GC (insert may run before/after change hooks),
9298 so the buffer M must NOT point to a Lisp string. */
9299
9300 void
9301 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9302 {
9303 const unsigned char *msg = (const unsigned char *) m;
9304
9305 if (!NILP (Vmemory_full))
9306 return;
9307
9308 if (!NILP (Vmessage_log_max))
9309 {
9310 struct buffer *oldbuf;
9311 Lisp_Object oldpoint, oldbegv, oldzv;
9312 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9313 ptrdiff_t point_at_end = 0;
9314 ptrdiff_t zv_at_end = 0;
9315 Lisp_Object old_deactivate_mark, tem;
9316 struct gcpro gcpro1;
9317
9318 old_deactivate_mark = Vdeactivate_mark;
9319 oldbuf = current_buffer;
9320 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9321 BVAR (current_buffer, undo_list) = Qt;
9322
9323 oldpoint = message_dolog_marker1;
9324 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9325 oldbegv = message_dolog_marker2;
9326 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9327 oldzv = message_dolog_marker3;
9328 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9329 GCPRO1 (old_deactivate_mark);
9330
9331 if (PT == Z)
9332 point_at_end = 1;
9333 if (ZV == Z)
9334 zv_at_end = 1;
9335
9336 BEGV = BEG;
9337 BEGV_BYTE = BEG_BYTE;
9338 ZV = Z;
9339 ZV_BYTE = Z_BYTE;
9340 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9341
9342 /* Insert the string--maybe converting multibyte to single byte
9343 or vice versa, so that all the text fits the buffer. */
9344 if (multibyte
9345 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9346 {
9347 ptrdiff_t i;
9348 int c, char_bytes;
9349 char work[1];
9350
9351 /* Convert a multibyte string to single-byte
9352 for the *Message* buffer. */
9353 for (i = 0; i < nbytes; i += char_bytes)
9354 {
9355 c = string_char_and_length (msg + i, &char_bytes);
9356 work[0] = (ASCII_CHAR_P (c)
9357 ? c
9358 : multibyte_char_to_unibyte (c));
9359 insert_1_both (work, 1, 1, 1, 0, 0);
9360 }
9361 }
9362 else if (! multibyte
9363 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9364 {
9365 ptrdiff_t i;
9366 int c, char_bytes;
9367 unsigned char str[MAX_MULTIBYTE_LENGTH];
9368 /* Convert a single-byte string to multibyte
9369 for the *Message* buffer. */
9370 for (i = 0; i < nbytes; i++)
9371 {
9372 c = msg[i];
9373 MAKE_CHAR_MULTIBYTE (c);
9374 char_bytes = CHAR_STRING (c, str);
9375 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9376 }
9377 }
9378 else if (nbytes)
9379 insert_1 (m, nbytes, 1, 0, 0);
9380
9381 if (nlflag)
9382 {
9383 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9384 printmax_t dups;
9385 insert_1 ("\n", 1, 1, 0, 0);
9386
9387 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9388 this_bol = PT;
9389 this_bol_byte = PT_BYTE;
9390
9391 /* See if this line duplicates the previous one.
9392 If so, combine duplicates. */
9393 if (this_bol > BEG)
9394 {
9395 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9396 prev_bol = PT;
9397 prev_bol_byte = PT_BYTE;
9398
9399 dups = message_log_check_duplicate (prev_bol_byte,
9400 this_bol_byte);
9401 if (dups)
9402 {
9403 del_range_both (prev_bol, prev_bol_byte,
9404 this_bol, this_bol_byte, 0);
9405 if (dups > 1)
9406 {
9407 char dupstr[sizeof " [ times]"
9408 + INT_STRLEN_BOUND (printmax_t)];
9409
9410 /* If you change this format, don't forget to also
9411 change message_log_check_duplicate. */
9412 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9413 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9414 insert_1 (dupstr, duplen, 1, 0, 1);
9415 }
9416 }
9417 }
9418
9419 /* If we have more than the desired maximum number of lines
9420 in the *Messages* buffer now, delete the oldest ones.
9421 This is safe because we don't have undo in this buffer. */
9422
9423 if (NATNUMP (Vmessage_log_max))
9424 {
9425 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9426 -XFASTINT (Vmessage_log_max) - 1, 0);
9427 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9428 }
9429 }
9430 BEGV = XMARKER (oldbegv)->charpos;
9431 BEGV_BYTE = marker_byte_position (oldbegv);
9432
9433 if (zv_at_end)
9434 {
9435 ZV = Z;
9436 ZV_BYTE = Z_BYTE;
9437 }
9438 else
9439 {
9440 ZV = XMARKER (oldzv)->charpos;
9441 ZV_BYTE = marker_byte_position (oldzv);
9442 }
9443
9444 if (point_at_end)
9445 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9446 else
9447 /* We can't do Fgoto_char (oldpoint) because it will run some
9448 Lisp code. */
9449 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9450 XMARKER (oldpoint)->bytepos);
9451
9452 UNGCPRO;
9453 unchain_marker (XMARKER (oldpoint));
9454 unchain_marker (XMARKER (oldbegv));
9455 unchain_marker (XMARKER (oldzv));
9456
9457 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9458 set_buffer_internal (oldbuf);
9459 if (NILP (tem))
9460 windows_or_buffers_changed = old_windows_or_buffers_changed;
9461 message_log_need_newline = !nlflag;
9462 Vdeactivate_mark = old_deactivate_mark;
9463 }
9464 }
9465
9466
9467 /* We are at the end of the buffer after just having inserted a newline.
9468 (Note: We depend on the fact we won't be crossing the gap.)
9469 Check to see if the most recent message looks a lot like the previous one.
9470 Return 0 if different, 1 if the new one should just replace it, or a
9471 value N > 1 if we should also append " [N times]". */
9472
9473 static intmax_t
9474 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9475 {
9476 ptrdiff_t i;
9477 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9478 int seen_dots = 0;
9479 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9480 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9481
9482 for (i = 0; i < len; i++)
9483 {
9484 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9485 seen_dots = 1;
9486 if (p1[i] != p2[i])
9487 return seen_dots;
9488 }
9489 p1 += len;
9490 if (*p1 == '\n')
9491 return 2;
9492 if (*p1++ == ' ' && *p1++ == '[')
9493 {
9494 char *pend;
9495 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9496 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9497 return n+1;
9498 }
9499 return 0;
9500 }
9501 \f
9502
9503 /* Display an echo area message M with a specified length of NBYTES
9504 bytes. The string may include null characters. If M is 0, clear
9505 out any existing message, and let the mini-buffer text show
9506 through.
9507
9508 This may GC, so the buffer M must NOT point to a Lisp string. */
9509
9510 void
9511 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9512 {
9513 /* First flush out any partial line written with print. */
9514 message_log_maybe_newline ();
9515 if (m)
9516 message_dolog (m, nbytes, 1, multibyte);
9517 message2_nolog (m, nbytes, multibyte);
9518 }
9519
9520
9521 /* The non-logging counterpart of message2. */
9522
9523 void
9524 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9525 {
9526 struct frame *sf = SELECTED_FRAME ();
9527 message_enable_multibyte = multibyte;
9528
9529 if (FRAME_INITIAL_P (sf))
9530 {
9531 if (noninteractive_need_newline)
9532 putc ('\n', stderr);
9533 noninteractive_need_newline = 0;
9534 if (m)
9535 fwrite (m, nbytes, 1, stderr);
9536 if (cursor_in_echo_area == 0)
9537 fprintf (stderr, "\n");
9538 fflush (stderr);
9539 }
9540 /* A null message buffer means that the frame hasn't really been
9541 initialized yet. Error messages get reported properly by
9542 cmd_error, so this must be just an informative message; toss it. */
9543 else if (INTERACTIVE
9544 && sf->glyphs_initialized_p
9545 && FRAME_MESSAGE_BUF (sf))
9546 {
9547 Lisp_Object mini_window;
9548 struct frame *f;
9549
9550 /* Get the frame containing the mini-buffer
9551 that the selected frame is using. */
9552 mini_window = FRAME_MINIBUF_WINDOW (sf);
9553 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9554
9555 FRAME_SAMPLE_VISIBILITY (f);
9556 if (FRAME_VISIBLE_P (sf)
9557 && ! FRAME_VISIBLE_P (f))
9558 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9559
9560 if (m)
9561 {
9562 set_message (m, Qnil, nbytes, multibyte);
9563 if (minibuffer_auto_raise)
9564 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9565 }
9566 else
9567 clear_message (1, 1);
9568
9569 do_pending_window_change (0);
9570 echo_area_display (1);
9571 do_pending_window_change (0);
9572 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9573 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9574 }
9575 }
9576
9577
9578 /* Display an echo area message M with a specified length of NBYTES
9579 bytes. The string may include null characters. If M is not a
9580 string, clear out any existing message, and let the mini-buffer
9581 text show through.
9582
9583 This function cancels echoing. */
9584
9585 void
9586 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9587 {
9588 struct gcpro gcpro1;
9589
9590 GCPRO1 (m);
9591 clear_message (1,1);
9592 cancel_echoing ();
9593
9594 /* First flush out any partial line written with print. */
9595 message_log_maybe_newline ();
9596 if (STRINGP (m))
9597 {
9598 USE_SAFE_ALLOCA;
9599 char *buffer = SAFE_ALLOCA (nbytes);
9600 memcpy (buffer, SDATA (m), nbytes);
9601 message_dolog (buffer, nbytes, 1, multibyte);
9602 SAFE_FREE ();
9603 }
9604 message3_nolog (m, nbytes, multibyte);
9605
9606 UNGCPRO;
9607 }
9608
9609
9610 /* The non-logging version of message3.
9611 This does not cancel echoing, because it is used for echoing.
9612 Perhaps we need to make a separate function for echoing
9613 and make this cancel echoing. */
9614
9615 void
9616 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9617 {
9618 struct frame *sf = SELECTED_FRAME ();
9619 message_enable_multibyte = multibyte;
9620
9621 if (FRAME_INITIAL_P (sf))
9622 {
9623 if (noninteractive_need_newline)
9624 putc ('\n', stderr);
9625 noninteractive_need_newline = 0;
9626 if (STRINGP (m))
9627 fwrite (SDATA (m), nbytes, 1, stderr);
9628 if (cursor_in_echo_area == 0)
9629 fprintf (stderr, "\n");
9630 fflush (stderr);
9631 }
9632 /* A null message buffer means that the frame hasn't really been
9633 initialized yet. Error messages get reported properly by
9634 cmd_error, so this must be just an informative message; toss it. */
9635 else if (INTERACTIVE
9636 && sf->glyphs_initialized_p
9637 && FRAME_MESSAGE_BUF (sf))
9638 {
9639 Lisp_Object mini_window;
9640 Lisp_Object frame;
9641 struct frame *f;
9642
9643 /* Get the frame containing the mini-buffer
9644 that the selected frame is using. */
9645 mini_window = FRAME_MINIBUF_WINDOW (sf);
9646 frame = WGET (XWINDOW (mini_window), frame);
9647 f = XFRAME (frame);
9648
9649 FRAME_SAMPLE_VISIBILITY (f);
9650 if (FRAME_VISIBLE_P (sf)
9651 && !FRAME_VISIBLE_P (f))
9652 Fmake_frame_visible (frame);
9653
9654 if (STRINGP (m) && SCHARS (m) > 0)
9655 {
9656 set_message (NULL, m, nbytes, multibyte);
9657 if (minibuffer_auto_raise)
9658 Fraise_frame (frame);
9659 /* Assume we are not echoing.
9660 (If we are, echo_now will override this.) */
9661 echo_message_buffer = Qnil;
9662 }
9663 else
9664 clear_message (1, 1);
9665
9666 do_pending_window_change (0);
9667 echo_area_display (1);
9668 do_pending_window_change (0);
9669 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9670 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9671 }
9672 }
9673
9674
9675 /* Display a null-terminated echo area message M. If M is 0, clear
9676 out any existing message, and let the mini-buffer text show through.
9677
9678 The buffer M must continue to exist until after the echo area gets
9679 cleared or some other message gets displayed there. Do not pass
9680 text that is stored in a Lisp string. Do not pass text in a buffer
9681 that was alloca'd. */
9682
9683 void
9684 message1 (const char *m)
9685 {
9686 message2 (m, (m ? strlen (m) : 0), 0);
9687 }
9688
9689
9690 /* The non-logging counterpart of message1. */
9691
9692 void
9693 message1_nolog (const char *m)
9694 {
9695 message2_nolog (m, (m ? strlen (m) : 0), 0);
9696 }
9697
9698 /* Display a message M which contains a single %s
9699 which gets replaced with STRING. */
9700
9701 void
9702 message_with_string (const char *m, Lisp_Object string, int log)
9703 {
9704 CHECK_STRING (string);
9705
9706 if (noninteractive)
9707 {
9708 if (m)
9709 {
9710 if (noninteractive_need_newline)
9711 putc ('\n', stderr);
9712 noninteractive_need_newline = 0;
9713 fprintf (stderr, m, SDATA (string));
9714 if (!cursor_in_echo_area)
9715 fprintf (stderr, "\n");
9716 fflush (stderr);
9717 }
9718 }
9719 else if (INTERACTIVE)
9720 {
9721 /* The frame whose minibuffer we're going to display the message on.
9722 It may be larger than the selected frame, so we need
9723 to use its buffer, not the selected frame's buffer. */
9724 Lisp_Object mini_window;
9725 struct frame *f, *sf = SELECTED_FRAME ();
9726
9727 /* Get the frame containing the minibuffer
9728 that the selected frame is using. */
9729 mini_window = FRAME_MINIBUF_WINDOW (sf);
9730 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9731
9732 /* A null message buffer means that the frame hasn't really been
9733 initialized yet. Error messages get reported properly by
9734 cmd_error, so this must be just an informative message; toss it. */
9735 if (FRAME_MESSAGE_BUF (f))
9736 {
9737 Lisp_Object args[2], msg;
9738 struct gcpro gcpro1, gcpro2;
9739
9740 args[0] = build_string (m);
9741 args[1] = msg = string;
9742 GCPRO2 (args[0], msg);
9743 gcpro1.nvars = 2;
9744
9745 msg = Fformat (2, args);
9746
9747 if (log)
9748 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9749 else
9750 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9751
9752 UNGCPRO;
9753
9754 /* Print should start at the beginning of the message
9755 buffer next time. */
9756 message_buf_print = 0;
9757 }
9758 }
9759 }
9760
9761
9762 /* Dump an informative message to the minibuf. If M is 0, clear out
9763 any existing message, and let the mini-buffer text show through. */
9764
9765 static void
9766 vmessage (const char *m, va_list ap)
9767 {
9768 if (noninteractive)
9769 {
9770 if (m)
9771 {
9772 if (noninteractive_need_newline)
9773 putc ('\n', stderr);
9774 noninteractive_need_newline = 0;
9775 vfprintf (stderr, m, ap);
9776 if (cursor_in_echo_area == 0)
9777 fprintf (stderr, "\n");
9778 fflush (stderr);
9779 }
9780 }
9781 else if (INTERACTIVE)
9782 {
9783 /* The frame whose mini-buffer we're going to display the message
9784 on. It may be larger than the selected frame, so we need to
9785 use its buffer, not the selected frame's buffer. */
9786 Lisp_Object mini_window;
9787 struct frame *f, *sf = SELECTED_FRAME ();
9788
9789 /* Get the frame containing the mini-buffer
9790 that the selected frame is using. */
9791 mini_window = FRAME_MINIBUF_WINDOW (sf);
9792 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9793
9794 /* A null message buffer means that the frame hasn't really been
9795 initialized yet. Error messages get reported properly by
9796 cmd_error, so this must be just an informative message; toss
9797 it. */
9798 if (FRAME_MESSAGE_BUF (f))
9799 {
9800 if (m)
9801 {
9802 ptrdiff_t len;
9803
9804 len = doprnt (FRAME_MESSAGE_BUF (f),
9805 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9806
9807 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9808 }
9809 else
9810 message1 (0);
9811
9812 /* Print should start at the beginning of the message
9813 buffer next time. */
9814 message_buf_print = 0;
9815 }
9816 }
9817 }
9818
9819 void
9820 message (const char *m, ...)
9821 {
9822 va_list ap;
9823 va_start (ap, m);
9824 vmessage (m, ap);
9825 va_end (ap);
9826 }
9827
9828
9829 #if 0
9830 /* The non-logging version of message. */
9831
9832 void
9833 message_nolog (const char *m, ...)
9834 {
9835 Lisp_Object old_log_max;
9836 va_list ap;
9837 va_start (ap, m);
9838 old_log_max = Vmessage_log_max;
9839 Vmessage_log_max = Qnil;
9840 vmessage (m, ap);
9841 Vmessage_log_max = old_log_max;
9842 va_end (ap);
9843 }
9844 #endif
9845
9846
9847 /* Display the current message in the current mini-buffer. This is
9848 only called from error handlers in process.c, and is not time
9849 critical. */
9850
9851 void
9852 update_echo_area (void)
9853 {
9854 if (!NILP (echo_area_buffer[0]))
9855 {
9856 Lisp_Object string;
9857 string = Fcurrent_message ();
9858 message3 (string, SBYTES (string),
9859 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9860 }
9861 }
9862
9863
9864 /* Make sure echo area buffers in `echo_buffers' are live.
9865 If they aren't, make new ones. */
9866
9867 static void
9868 ensure_echo_area_buffers (void)
9869 {
9870 int i;
9871
9872 for (i = 0; i < 2; ++i)
9873 if (!BUFFERP (echo_buffer[i])
9874 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9875 {
9876 char name[30];
9877 Lisp_Object old_buffer;
9878 int j;
9879
9880 old_buffer = echo_buffer[i];
9881 echo_buffer[i] = Fget_buffer_create
9882 (make_formatted_string (name, " *Echo Area %d*", i));
9883 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9884 /* to force word wrap in echo area -
9885 it was decided to postpone this*/
9886 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9887
9888 for (j = 0; j < 2; ++j)
9889 if (EQ (old_buffer, echo_area_buffer[j]))
9890 echo_area_buffer[j] = echo_buffer[i];
9891 }
9892 }
9893
9894
9895 /* Call FN with args A1..A4 with either the current or last displayed
9896 echo_area_buffer as current buffer.
9897
9898 WHICH zero means use the current message buffer
9899 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9900 from echo_buffer[] and clear it.
9901
9902 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9903 suitable buffer from echo_buffer[] and clear it.
9904
9905 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9906 that the current message becomes the last displayed one, make
9907 choose a suitable buffer for echo_area_buffer[0], and clear it.
9908
9909 Value is what FN returns. */
9910
9911 static int
9912 with_echo_area_buffer (struct window *w, int which,
9913 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9914 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9915 {
9916 Lisp_Object buffer;
9917 int this_one, the_other, clear_buffer_p, rc;
9918 ptrdiff_t count = SPECPDL_INDEX ();
9919
9920 /* If buffers aren't live, make new ones. */
9921 ensure_echo_area_buffers ();
9922
9923 clear_buffer_p = 0;
9924
9925 if (which == 0)
9926 this_one = 0, the_other = 1;
9927 else if (which > 0)
9928 this_one = 1, the_other = 0;
9929 else
9930 {
9931 this_one = 0, the_other = 1;
9932 clear_buffer_p = 1;
9933
9934 /* We need a fresh one in case the current echo buffer equals
9935 the one containing the last displayed echo area message. */
9936 if (!NILP (echo_area_buffer[this_one])
9937 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9938 echo_area_buffer[this_one] = Qnil;
9939 }
9940
9941 /* Choose a suitable buffer from echo_buffer[] is we don't
9942 have one. */
9943 if (NILP (echo_area_buffer[this_one]))
9944 {
9945 echo_area_buffer[this_one]
9946 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9947 ? echo_buffer[the_other]
9948 : echo_buffer[this_one]);
9949 clear_buffer_p = 1;
9950 }
9951
9952 buffer = echo_area_buffer[this_one];
9953
9954 /* Don't get confused by reusing the buffer used for echoing
9955 for a different purpose. */
9956 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9957 cancel_echoing ();
9958
9959 record_unwind_protect (unwind_with_echo_area_buffer,
9960 with_echo_area_buffer_unwind_data (w));
9961
9962 /* Make the echo area buffer current. Note that for display
9963 purposes, it is not necessary that the displayed window's buffer
9964 == current_buffer, except for text property lookup. So, let's
9965 only set that buffer temporarily here without doing a full
9966 Fset_window_buffer. We must also change w->pointm, though,
9967 because otherwise an assertions in unshow_buffer fails, and Emacs
9968 aborts. */
9969 set_buffer_internal_1 (XBUFFER (buffer));
9970 if (w)
9971 {
9972 WSET (w, buffer, buffer);
9973 set_marker_both (WGET (w, pointm), buffer, BEG, BEG_BYTE);
9974 }
9975
9976 BVAR (current_buffer, undo_list) = Qt;
9977 BVAR (current_buffer, read_only) = Qnil;
9978 specbind (Qinhibit_read_only, Qt);
9979 specbind (Qinhibit_modification_hooks, Qt);
9980
9981 if (clear_buffer_p && Z > BEG)
9982 del_range (BEG, Z);
9983
9984 eassert (BEGV >= BEG);
9985 eassert (ZV <= Z && ZV >= BEGV);
9986
9987 rc = fn (a1, a2, a3, a4);
9988
9989 eassert (BEGV >= BEG);
9990 eassert (ZV <= Z && ZV >= BEGV);
9991
9992 unbind_to (count, Qnil);
9993 return rc;
9994 }
9995
9996
9997 /* Save state that should be preserved around the call to the function
9998 FN called in with_echo_area_buffer. */
9999
10000 static Lisp_Object
10001 with_echo_area_buffer_unwind_data (struct window *w)
10002 {
10003 int i = 0;
10004 Lisp_Object vector, tmp;
10005
10006 /* Reduce consing by keeping one vector in
10007 Vwith_echo_area_save_vector. */
10008 vector = Vwith_echo_area_save_vector;
10009 Vwith_echo_area_save_vector = Qnil;
10010
10011 if (NILP (vector))
10012 vector = Fmake_vector (make_number (7), Qnil);
10013
10014 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10015 ASET (vector, i, Vdeactivate_mark); ++i;
10016 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10017
10018 if (w)
10019 {
10020 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10021 ASET (vector, i, WGET (w, buffer)); ++i;
10022 ASET (vector, i, make_number (XMARKER (WGET (w, pointm))->charpos)); ++i;
10023 ASET (vector, i, make_number (XMARKER (WGET (w, pointm))->bytepos)); ++i;
10024 }
10025 else
10026 {
10027 int end = i + 4;
10028 for (; i < end; ++i)
10029 ASET (vector, i, Qnil);
10030 }
10031
10032 eassert (i == ASIZE (vector));
10033 return vector;
10034 }
10035
10036
10037 /* Restore global state from VECTOR which was created by
10038 with_echo_area_buffer_unwind_data. */
10039
10040 static Lisp_Object
10041 unwind_with_echo_area_buffer (Lisp_Object vector)
10042 {
10043 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10044 Vdeactivate_mark = AREF (vector, 1);
10045 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10046
10047 if (WINDOWP (AREF (vector, 3)))
10048 {
10049 struct window *w;
10050 Lisp_Object buffer, charpos, bytepos;
10051
10052 w = XWINDOW (AREF (vector, 3));
10053 buffer = AREF (vector, 4);
10054 charpos = AREF (vector, 5);
10055 bytepos = AREF (vector, 6);
10056
10057 WSET (w, buffer, buffer);
10058 set_marker_both (WGET (w, pointm), buffer,
10059 XFASTINT (charpos), XFASTINT (bytepos));
10060 }
10061
10062 Vwith_echo_area_save_vector = vector;
10063 return Qnil;
10064 }
10065
10066
10067 /* Set up the echo area for use by print functions. MULTIBYTE_P
10068 non-zero means we will print multibyte. */
10069
10070 void
10071 setup_echo_area_for_printing (int multibyte_p)
10072 {
10073 /* If we can't find an echo area any more, exit. */
10074 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10075 Fkill_emacs (Qnil);
10076
10077 ensure_echo_area_buffers ();
10078
10079 if (!message_buf_print)
10080 {
10081 /* A message has been output since the last time we printed.
10082 Choose a fresh echo area buffer. */
10083 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10084 echo_area_buffer[0] = echo_buffer[1];
10085 else
10086 echo_area_buffer[0] = echo_buffer[0];
10087
10088 /* Switch to that buffer and clear it. */
10089 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10090 BVAR (current_buffer, truncate_lines) = Qnil;
10091
10092 if (Z > BEG)
10093 {
10094 ptrdiff_t count = SPECPDL_INDEX ();
10095 specbind (Qinhibit_read_only, Qt);
10096 /* Note that undo recording is always disabled. */
10097 del_range (BEG, Z);
10098 unbind_to (count, Qnil);
10099 }
10100 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10101
10102 /* Set up the buffer for the multibyteness we need. */
10103 if (multibyte_p
10104 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10105 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10106
10107 /* Raise the frame containing the echo area. */
10108 if (minibuffer_auto_raise)
10109 {
10110 struct frame *sf = SELECTED_FRAME ();
10111 Lisp_Object mini_window;
10112 mini_window = FRAME_MINIBUF_WINDOW (sf);
10113 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10114 }
10115
10116 message_log_maybe_newline ();
10117 message_buf_print = 1;
10118 }
10119 else
10120 {
10121 if (NILP (echo_area_buffer[0]))
10122 {
10123 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10124 echo_area_buffer[0] = echo_buffer[1];
10125 else
10126 echo_area_buffer[0] = echo_buffer[0];
10127 }
10128
10129 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10130 {
10131 /* Someone switched buffers between print requests. */
10132 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10133 BVAR (current_buffer, truncate_lines) = Qnil;
10134 }
10135 }
10136 }
10137
10138
10139 /* Display an echo area message in window W. Value is non-zero if W's
10140 height is changed. If display_last_displayed_message_p is
10141 non-zero, display the message that was last displayed, otherwise
10142 display the current message. */
10143
10144 static int
10145 display_echo_area (struct window *w)
10146 {
10147 int i, no_message_p, window_height_changed_p;
10148
10149 /* Temporarily disable garbage collections while displaying the echo
10150 area. This is done because a GC can print a message itself.
10151 That message would modify the echo area buffer's contents while a
10152 redisplay of the buffer is going on, and seriously confuse
10153 redisplay. */
10154 ptrdiff_t count = inhibit_garbage_collection ();
10155
10156 /* If there is no message, we must call display_echo_area_1
10157 nevertheless because it resizes the window. But we will have to
10158 reset the echo_area_buffer in question to nil at the end because
10159 with_echo_area_buffer will sets it to an empty buffer. */
10160 i = display_last_displayed_message_p ? 1 : 0;
10161 no_message_p = NILP (echo_area_buffer[i]);
10162
10163 window_height_changed_p
10164 = with_echo_area_buffer (w, display_last_displayed_message_p,
10165 display_echo_area_1,
10166 (intptr_t) w, Qnil, 0, 0);
10167
10168 if (no_message_p)
10169 echo_area_buffer[i] = Qnil;
10170
10171 unbind_to (count, Qnil);
10172 return window_height_changed_p;
10173 }
10174
10175
10176 /* Helper for display_echo_area. Display the current buffer which
10177 contains the current echo area message in window W, a mini-window,
10178 a pointer to which is passed in A1. A2..A4 are currently not used.
10179 Change the height of W so that all of the message is displayed.
10180 Value is non-zero if height of W was changed. */
10181
10182 static int
10183 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10184 {
10185 intptr_t i1 = a1;
10186 struct window *w = (struct window *) i1;
10187 Lisp_Object window;
10188 struct text_pos start;
10189 int window_height_changed_p = 0;
10190
10191 /* Do this before displaying, so that we have a large enough glyph
10192 matrix for the display. If we can't get enough space for the
10193 whole text, display the last N lines. That works by setting w->start. */
10194 window_height_changed_p = resize_mini_window (w, 0);
10195
10196 /* Use the starting position chosen by resize_mini_window. */
10197 SET_TEXT_POS_FROM_MARKER (start, WGET (w, start));
10198
10199 /* Display. */
10200 clear_glyph_matrix (w->desired_matrix);
10201 XSETWINDOW (window, w);
10202 try_window (window, start, 0);
10203
10204 return window_height_changed_p;
10205 }
10206
10207
10208 /* Resize the echo area window to exactly the size needed for the
10209 currently displayed message, if there is one. If a mini-buffer
10210 is active, don't shrink it. */
10211
10212 void
10213 resize_echo_area_exactly (void)
10214 {
10215 if (BUFFERP (echo_area_buffer[0])
10216 && WINDOWP (echo_area_window))
10217 {
10218 struct window *w = XWINDOW (echo_area_window);
10219 int resized_p;
10220 Lisp_Object resize_exactly;
10221
10222 if (minibuf_level == 0)
10223 resize_exactly = Qt;
10224 else
10225 resize_exactly = Qnil;
10226
10227 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10228 (intptr_t) w, resize_exactly,
10229 0, 0);
10230 if (resized_p)
10231 {
10232 ++windows_or_buffers_changed;
10233 ++update_mode_lines;
10234 redisplay_internal ();
10235 }
10236 }
10237 }
10238
10239
10240 /* Callback function for with_echo_area_buffer, when used from
10241 resize_echo_area_exactly. A1 contains a pointer to the window to
10242 resize, EXACTLY non-nil means resize the mini-window exactly to the
10243 size of the text displayed. A3 and A4 are not used. Value is what
10244 resize_mini_window returns. */
10245
10246 static int
10247 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10248 {
10249 intptr_t i1 = a1;
10250 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10251 }
10252
10253
10254 /* Resize mini-window W to fit the size of its contents. EXACT_P
10255 means size the window exactly to the size needed. Otherwise, it's
10256 only enlarged until W's buffer is empty.
10257
10258 Set W->start to the right place to begin display. If the whole
10259 contents fit, start at the beginning. Otherwise, start so as
10260 to make the end of the contents appear. This is particularly
10261 important for y-or-n-p, but seems desirable generally.
10262
10263 Value is non-zero if the window height has been changed. */
10264
10265 int
10266 resize_mini_window (struct window *w, int exact_p)
10267 {
10268 struct frame *f = XFRAME (WGET (w, frame));
10269 int window_height_changed_p = 0;
10270
10271 eassert (MINI_WINDOW_P (w));
10272
10273 /* By default, start display at the beginning. */
10274 set_marker_both (WGET (w, start), WGET (w, buffer),
10275 BUF_BEGV (XBUFFER (WGET (w, buffer))),
10276 BUF_BEGV_BYTE (XBUFFER (WGET (w, buffer))));
10277
10278 /* Don't resize windows while redisplaying a window; it would
10279 confuse redisplay functions when the size of the window they are
10280 displaying changes from under them. Such a resizing can happen,
10281 for instance, when which-func prints a long message while
10282 we are running fontification-functions. We're running these
10283 functions with safe_call which binds inhibit-redisplay to t. */
10284 if (!NILP (Vinhibit_redisplay))
10285 return 0;
10286
10287 /* Nil means don't try to resize. */
10288 if (NILP (Vresize_mini_windows)
10289 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10290 return 0;
10291
10292 if (!FRAME_MINIBUF_ONLY_P (f))
10293 {
10294 struct it it;
10295 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10296 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10297 int height;
10298 EMACS_INT max_height;
10299 int unit = FRAME_LINE_HEIGHT (f);
10300 struct text_pos start;
10301 struct buffer *old_current_buffer = NULL;
10302
10303 if (current_buffer != XBUFFER (WGET (w, buffer)))
10304 {
10305 old_current_buffer = current_buffer;
10306 set_buffer_internal (XBUFFER (WGET (w, buffer)));
10307 }
10308
10309 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10310
10311 /* Compute the max. number of lines specified by the user. */
10312 if (FLOATP (Vmax_mini_window_height))
10313 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10314 else if (INTEGERP (Vmax_mini_window_height))
10315 max_height = XINT (Vmax_mini_window_height);
10316 else
10317 max_height = total_height / 4;
10318
10319 /* Correct that max. height if it's bogus. */
10320 max_height = max (1, max_height);
10321 max_height = min (total_height, max_height);
10322
10323 /* Find out the height of the text in the window. */
10324 if (it.line_wrap == TRUNCATE)
10325 height = 1;
10326 else
10327 {
10328 last_height = 0;
10329 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10330 if (it.max_ascent == 0 && it.max_descent == 0)
10331 height = it.current_y + last_height;
10332 else
10333 height = it.current_y + it.max_ascent + it.max_descent;
10334 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10335 height = (height + unit - 1) / unit;
10336 }
10337
10338 /* Compute a suitable window start. */
10339 if (height > max_height)
10340 {
10341 height = max_height;
10342 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10343 move_it_vertically_backward (&it, (height - 1) * unit);
10344 start = it.current.pos;
10345 }
10346 else
10347 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10348 SET_MARKER_FROM_TEXT_POS (WGET (w, start), start);
10349
10350 if (EQ (Vresize_mini_windows, Qgrow_only))
10351 {
10352 /* Let it grow only, until we display an empty message, in which
10353 case the window shrinks again. */
10354 if (height > WINDOW_TOTAL_LINES (w))
10355 {
10356 int old_height = WINDOW_TOTAL_LINES (w);
10357 freeze_window_starts (f, 1);
10358 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10359 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10360 }
10361 else if (height < WINDOW_TOTAL_LINES (w)
10362 && (exact_p || BEGV == ZV))
10363 {
10364 int old_height = WINDOW_TOTAL_LINES (w);
10365 freeze_window_starts (f, 0);
10366 shrink_mini_window (w);
10367 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10368 }
10369 }
10370 else
10371 {
10372 /* Always resize to exact size needed. */
10373 if (height > WINDOW_TOTAL_LINES (w))
10374 {
10375 int old_height = WINDOW_TOTAL_LINES (w);
10376 freeze_window_starts (f, 1);
10377 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10378 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10379 }
10380 else if (height < WINDOW_TOTAL_LINES (w))
10381 {
10382 int old_height = WINDOW_TOTAL_LINES (w);
10383 freeze_window_starts (f, 0);
10384 shrink_mini_window (w);
10385
10386 if (height)
10387 {
10388 freeze_window_starts (f, 1);
10389 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10390 }
10391
10392 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10393 }
10394 }
10395
10396 if (old_current_buffer)
10397 set_buffer_internal (old_current_buffer);
10398 }
10399
10400 return window_height_changed_p;
10401 }
10402
10403
10404 /* Value is the current message, a string, or nil if there is no
10405 current message. */
10406
10407 Lisp_Object
10408 current_message (void)
10409 {
10410 Lisp_Object msg;
10411
10412 if (!BUFFERP (echo_area_buffer[0]))
10413 msg = Qnil;
10414 else
10415 {
10416 with_echo_area_buffer (0, 0, current_message_1,
10417 (intptr_t) &msg, Qnil, 0, 0);
10418 if (NILP (msg))
10419 echo_area_buffer[0] = Qnil;
10420 }
10421
10422 return msg;
10423 }
10424
10425
10426 static int
10427 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10428 {
10429 intptr_t i1 = a1;
10430 Lisp_Object *msg = (Lisp_Object *) i1;
10431
10432 if (Z > BEG)
10433 *msg = make_buffer_string (BEG, Z, 1);
10434 else
10435 *msg = Qnil;
10436 return 0;
10437 }
10438
10439
10440 /* Push the current message on Vmessage_stack for later restoration
10441 by restore_message. Value is non-zero if the current message isn't
10442 empty. This is a relatively infrequent operation, so it's not
10443 worth optimizing. */
10444
10445 int
10446 push_message (void)
10447 {
10448 Lisp_Object msg;
10449 msg = current_message ();
10450 Vmessage_stack = Fcons (msg, Vmessage_stack);
10451 return STRINGP (msg);
10452 }
10453
10454
10455 /* Restore message display from the top of Vmessage_stack. */
10456
10457 void
10458 restore_message (void)
10459 {
10460 Lisp_Object msg;
10461
10462 eassert (CONSP (Vmessage_stack));
10463 msg = XCAR (Vmessage_stack);
10464 if (STRINGP (msg))
10465 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10466 else
10467 message3_nolog (msg, 0, 0);
10468 }
10469
10470
10471 /* Handler for record_unwind_protect calling pop_message. */
10472
10473 Lisp_Object
10474 pop_message_unwind (Lisp_Object dummy)
10475 {
10476 pop_message ();
10477 return Qnil;
10478 }
10479
10480 /* Pop the top-most entry off Vmessage_stack. */
10481
10482 static void
10483 pop_message (void)
10484 {
10485 eassert (CONSP (Vmessage_stack));
10486 Vmessage_stack = XCDR (Vmessage_stack);
10487 }
10488
10489
10490 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10491 exits. If the stack is not empty, we have a missing pop_message
10492 somewhere. */
10493
10494 void
10495 check_message_stack (void)
10496 {
10497 if (!NILP (Vmessage_stack))
10498 abort ();
10499 }
10500
10501
10502 /* Truncate to NCHARS what will be displayed in the echo area the next
10503 time we display it---but don't redisplay it now. */
10504
10505 void
10506 truncate_echo_area (ptrdiff_t nchars)
10507 {
10508 if (nchars == 0)
10509 echo_area_buffer[0] = Qnil;
10510 /* A null message buffer means that the frame hasn't really been
10511 initialized yet. Error messages get reported properly by
10512 cmd_error, so this must be just an informative message; toss it. */
10513 else if (!noninteractive
10514 && INTERACTIVE
10515 && !NILP (echo_area_buffer[0]))
10516 {
10517 struct frame *sf = SELECTED_FRAME ();
10518 if (FRAME_MESSAGE_BUF (sf))
10519 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10520 }
10521 }
10522
10523
10524 /* Helper function for truncate_echo_area. Truncate the current
10525 message to at most NCHARS characters. */
10526
10527 static int
10528 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10529 {
10530 if (BEG + nchars < Z)
10531 del_range (BEG + nchars, Z);
10532 if (Z == BEG)
10533 echo_area_buffer[0] = Qnil;
10534 return 0;
10535 }
10536
10537
10538 /* Set the current message to a substring of S or STRING.
10539
10540 If STRING is a Lisp string, set the message to the first NBYTES
10541 bytes from STRING. NBYTES zero means use the whole string. If
10542 STRING is multibyte, the message will be displayed multibyte.
10543
10544 If S is not null, set the message to the first LEN bytes of S. LEN
10545 zero means use the whole string. MULTIBYTE_P non-zero means S is
10546 multibyte. Display the message multibyte in that case.
10547
10548 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10549 to t before calling set_message_1 (which calls insert).
10550 */
10551
10552 static void
10553 set_message (const char *s, Lisp_Object string,
10554 ptrdiff_t nbytes, int multibyte_p)
10555 {
10556 message_enable_multibyte
10557 = ((s && multibyte_p)
10558 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10559
10560 with_echo_area_buffer (0, -1, set_message_1,
10561 (intptr_t) s, string, nbytes, multibyte_p);
10562 message_buf_print = 0;
10563 help_echo_showing_p = 0;
10564 }
10565
10566
10567 /* Helper function for set_message. Arguments have the same meaning
10568 as there, with A1 corresponding to S and A2 corresponding to STRING
10569 This function is called with the echo area buffer being
10570 current. */
10571
10572 static int
10573 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10574 {
10575 intptr_t i1 = a1;
10576 const char *s = (const char *) i1;
10577 const unsigned char *msg = (const unsigned char *) s;
10578 Lisp_Object string = a2;
10579
10580 /* Change multibyteness of the echo buffer appropriately. */
10581 if (message_enable_multibyte
10582 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10583 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10584
10585 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10586 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10587 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10588
10589 /* Insert new message at BEG. */
10590 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10591
10592 if (STRINGP (string))
10593 {
10594 ptrdiff_t nchars;
10595
10596 if (nbytes == 0)
10597 nbytes = SBYTES (string);
10598 nchars = string_byte_to_char (string, nbytes);
10599
10600 /* This function takes care of single/multibyte conversion. We
10601 just have to ensure that the echo area buffer has the right
10602 setting of enable_multibyte_characters. */
10603 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10604 }
10605 else if (s)
10606 {
10607 if (nbytes == 0)
10608 nbytes = strlen (s);
10609
10610 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10611 {
10612 /* Convert from multi-byte to single-byte. */
10613 ptrdiff_t i;
10614 int c, n;
10615 char work[1];
10616
10617 /* Convert a multibyte string to single-byte. */
10618 for (i = 0; i < nbytes; i += n)
10619 {
10620 c = string_char_and_length (msg + i, &n);
10621 work[0] = (ASCII_CHAR_P (c)
10622 ? c
10623 : multibyte_char_to_unibyte (c));
10624 insert_1_both (work, 1, 1, 1, 0, 0);
10625 }
10626 }
10627 else if (!multibyte_p
10628 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10629 {
10630 /* Convert from single-byte to multi-byte. */
10631 ptrdiff_t i;
10632 int c, n;
10633 unsigned char str[MAX_MULTIBYTE_LENGTH];
10634
10635 /* Convert a single-byte string to multibyte. */
10636 for (i = 0; i < nbytes; i++)
10637 {
10638 c = msg[i];
10639 MAKE_CHAR_MULTIBYTE (c);
10640 n = CHAR_STRING (c, str);
10641 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10642 }
10643 }
10644 else
10645 insert_1 (s, nbytes, 1, 0, 0);
10646 }
10647
10648 return 0;
10649 }
10650
10651
10652 /* Clear messages. CURRENT_P non-zero means clear the current
10653 message. LAST_DISPLAYED_P non-zero means clear the message
10654 last displayed. */
10655
10656 void
10657 clear_message (int current_p, int last_displayed_p)
10658 {
10659 if (current_p)
10660 {
10661 echo_area_buffer[0] = Qnil;
10662 message_cleared_p = 1;
10663 }
10664
10665 if (last_displayed_p)
10666 echo_area_buffer[1] = Qnil;
10667
10668 message_buf_print = 0;
10669 }
10670
10671 /* Clear garbaged frames.
10672
10673 This function is used where the old redisplay called
10674 redraw_garbaged_frames which in turn called redraw_frame which in
10675 turn called clear_frame. The call to clear_frame was a source of
10676 flickering. I believe a clear_frame is not necessary. It should
10677 suffice in the new redisplay to invalidate all current matrices,
10678 and ensure a complete redisplay of all windows. */
10679
10680 static void
10681 clear_garbaged_frames (void)
10682 {
10683 if (frame_garbaged)
10684 {
10685 Lisp_Object tail, frame;
10686 int changed_count = 0;
10687
10688 FOR_EACH_FRAME (tail, frame)
10689 {
10690 struct frame *f = XFRAME (frame);
10691
10692 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10693 {
10694 if (f->resized_p)
10695 {
10696 Fredraw_frame (frame);
10697 f->force_flush_display_p = 1;
10698 }
10699 clear_current_matrices (f);
10700 changed_count++;
10701 f->garbaged = 0;
10702 f->resized_p = 0;
10703 }
10704 }
10705
10706 frame_garbaged = 0;
10707 if (changed_count)
10708 ++windows_or_buffers_changed;
10709 }
10710 }
10711
10712
10713 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10714 is non-zero update selected_frame. Value is non-zero if the
10715 mini-windows height has been changed. */
10716
10717 static int
10718 echo_area_display (int update_frame_p)
10719 {
10720 Lisp_Object mini_window;
10721 struct window *w;
10722 struct frame *f;
10723 int window_height_changed_p = 0;
10724 struct frame *sf = SELECTED_FRAME ();
10725
10726 mini_window = FRAME_MINIBUF_WINDOW (sf);
10727 w = XWINDOW (mini_window);
10728 f = XFRAME (WINDOW_FRAME (w));
10729
10730 /* Don't display if frame is invisible or not yet initialized. */
10731 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10732 return 0;
10733
10734 #ifdef HAVE_WINDOW_SYSTEM
10735 /* When Emacs starts, selected_frame may be the initial terminal
10736 frame. If we let this through, a message would be displayed on
10737 the terminal. */
10738 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10739 return 0;
10740 #endif /* HAVE_WINDOW_SYSTEM */
10741
10742 /* Redraw garbaged frames. */
10743 if (frame_garbaged)
10744 clear_garbaged_frames ();
10745
10746 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10747 {
10748 echo_area_window = mini_window;
10749 window_height_changed_p = display_echo_area (w);
10750 w->must_be_updated_p = 1;
10751
10752 /* Update the display, unless called from redisplay_internal.
10753 Also don't update the screen during redisplay itself. The
10754 update will happen at the end of redisplay, and an update
10755 here could cause confusion. */
10756 if (update_frame_p && !redisplaying_p)
10757 {
10758 int n = 0;
10759
10760 /* If the display update has been interrupted by pending
10761 input, update mode lines in the frame. Due to the
10762 pending input, it might have been that redisplay hasn't
10763 been called, so that mode lines above the echo area are
10764 garbaged. This looks odd, so we prevent it here. */
10765 if (!display_completed)
10766 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10767
10768 if (window_height_changed_p
10769 /* Don't do this if Emacs is shutting down. Redisplay
10770 needs to run hooks. */
10771 && !NILP (Vrun_hooks))
10772 {
10773 /* Must update other windows. Likewise as in other
10774 cases, don't let this update be interrupted by
10775 pending input. */
10776 ptrdiff_t count = SPECPDL_INDEX ();
10777 specbind (Qredisplay_dont_pause, Qt);
10778 windows_or_buffers_changed = 1;
10779 redisplay_internal ();
10780 unbind_to (count, Qnil);
10781 }
10782 else if (FRAME_WINDOW_P (f) && n == 0)
10783 {
10784 /* Window configuration is the same as before.
10785 Can do with a display update of the echo area,
10786 unless we displayed some mode lines. */
10787 update_single_window (w, 1);
10788 FRAME_RIF (f)->flush_display (f);
10789 }
10790 else
10791 update_frame (f, 1, 1);
10792
10793 /* If cursor is in the echo area, make sure that the next
10794 redisplay displays the minibuffer, so that the cursor will
10795 be replaced with what the minibuffer wants. */
10796 if (cursor_in_echo_area)
10797 ++windows_or_buffers_changed;
10798 }
10799 }
10800 else if (!EQ (mini_window, selected_window))
10801 windows_or_buffers_changed++;
10802
10803 /* Last displayed message is now the current message. */
10804 echo_area_buffer[1] = echo_area_buffer[0];
10805 /* Inform read_char that we're not echoing. */
10806 echo_message_buffer = Qnil;
10807
10808 /* Prevent redisplay optimization in redisplay_internal by resetting
10809 this_line_start_pos. This is done because the mini-buffer now
10810 displays the message instead of its buffer text. */
10811 if (EQ (mini_window, selected_window))
10812 CHARPOS (this_line_start_pos) = 0;
10813
10814 return window_height_changed_p;
10815 }
10816
10817
10818 \f
10819 /***********************************************************************
10820 Mode Lines and Frame Titles
10821 ***********************************************************************/
10822
10823 /* A buffer for constructing non-propertized mode-line strings and
10824 frame titles in it; allocated from the heap in init_xdisp and
10825 resized as needed in store_mode_line_noprop_char. */
10826
10827 static char *mode_line_noprop_buf;
10828
10829 /* The buffer's end, and a current output position in it. */
10830
10831 static char *mode_line_noprop_buf_end;
10832 static char *mode_line_noprop_ptr;
10833
10834 #define MODE_LINE_NOPROP_LEN(start) \
10835 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10836
10837 static enum {
10838 MODE_LINE_DISPLAY = 0,
10839 MODE_LINE_TITLE,
10840 MODE_LINE_NOPROP,
10841 MODE_LINE_STRING
10842 } mode_line_target;
10843
10844 /* Alist that caches the results of :propertize.
10845 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10846 static Lisp_Object mode_line_proptrans_alist;
10847
10848 /* List of strings making up the mode-line. */
10849 static Lisp_Object mode_line_string_list;
10850
10851 /* Base face property when building propertized mode line string. */
10852 static Lisp_Object mode_line_string_face;
10853 static Lisp_Object mode_line_string_face_prop;
10854
10855
10856 /* Unwind data for mode line strings */
10857
10858 static Lisp_Object Vmode_line_unwind_vector;
10859
10860 static Lisp_Object
10861 format_mode_line_unwind_data (struct frame *target_frame,
10862 struct buffer *obuf,
10863 Lisp_Object owin,
10864 int save_proptrans)
10865 {
10866 Lisp_Object vector, tmp;
10867
10868 /* Reduce consing by keeping one vector in
10869 Vwith_echo_area_save_vector. */
10870 vector = Vmode_line_unwind_vector;
10871 Vmode_line_unwind_vector = Qnil;
10872
10873 if (NILP (vector))
10874 vector = Fmake_vector (make_number (10), Qnil);
10875
10876 ASET (vector, 0, make_number (mode_line_target));
10877 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10878 ASET (vector, 2, mode_line_string_list);
10879 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10880 ASET (vector, 4, mode_line_string_face);
10881 ASET (vector, 5, mode_line_string_face_prop);
10882
10883 if (obuf)
10884 XSETBUFFER (tmp, obuf);
10885 else
10886 tmp = Qnil;
10887 ASET (vector, 6, tmp);
10888 ASET (vector, 7, owin);
10889 if (target_frame)
10890 {
10891 /* Similarly to `with-selected-window', if the operation selects
10892 a window on another frame, we must restore that frame's
10893 selected window, and (for a tty) the top-frame. */
10894 ASET (vector, 8, target_frame->selected_window);
10895 if (FRAME_TERMCAP_P (target_frame))
10896 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10897 }
10898
10899 return vector;
10900 }
10901
10902 static Lisp_Object
10903 unwind_format_mode_line (Lisp_Object vector)
10904 {
10905 Lisp_Object old_window = AREF (vector, 7);
10906 Lisp_Object target_frame_window = AREF (vector, 8);
10907 Lisp_Object old_top_frame = AREF (vector, 9);
10908
10909 mode_line_target = XINT (AREF (vector, 0));
10910 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10911 mode_line_string_list = AREF (vector, 2);
10912 if (! EQ (AREF (vector, 3), Qt))
10913 mode_line_proptrans_alist = AREF (vector, 3);
10914 mode_line_string_face = AREF (vector, 4);
10915 mode_line_string_face_prop = AREF (vector, 5);
10916
10917 /* Select window before buffer, since it may change the buffer. */
10918 if (!NILP (old_window))
10919 {
10920 /* If the operation that we are unwinding had selected a window
10921 on a different frame, reset its frame-selected-window. For a
10922 text terminal, reset its top-frame if necessary. */
10923 if (!NILP (target_frame_window))
10924 {
10925 Lisp_Object frame
10926 = WINDOW_FRAME (XWINDOW (target_frame_window));
10927
10928 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10929 Fselect_window (target_frame_window, Qt);
10930
10931 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10932 Fselect_frame (old_top_frame, Qt);
10933 }
10934
10935 Fselect_window (old_window, Qt);
10936 }
10937
10938 if (!NILP (AREF (vector, 6)))
10939 {
10940 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10941 ASET (vector, 6, Qnil);
10942 }
10943
10944 Vmode_line_unwind_vector = vector;
10945 return Qnil;
10946 }
10947
10948
10949 /* Store a single character C for the frame title in mode_line_noprop_buf.
10950 Re-allocate mode_line_noprop_buf if necessary. */
10951
10952 static void
10953 store_mode_line_noprop_char (char c)
10954 {
10955 /* If output position has reached the end of the allocated buffer,
10956 increase the buffer's size. */
10957 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10958 {
10959 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10960 ptrdiff_t size = len;
10961 mode_line_noprop_buf =
10962 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10963 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10964 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10965 }
10966
10967 *mode_line_noprop_ptr++ = c;
10968 }
10969
10970
10971 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10972 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10973 characters that yield more columns than PRECISION; PRECISION <= 0
10974 means copy the whole string. Pad with spaces until FIELD_WIDTH
10975 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10976 pad. Called from display_mode_element when it is used to build a
10977 frame title. */
10978
10979 static int
10980 store_mode_line_noprop (const char *string, int field_width, int precision)
10981 {
10982 const unsigned char *str = (const unsigned char *) string;
10983 int n = 0;
10984 ptrdiff_t dummy, nbytes;
10985
10986 /* Copy at most PRECISION chars from STR. */
10987 nbytes = strlen (string);
10988 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10989 while (nbytes--)
10990 store_mode_line_noprop_char (*str++);
10991
10992 /* Fill up with spaces until FIELD_WIDTH reached. */
10993 while (field_width > 0
10994 && n < field_width)
10995 {
10996 store_mode_line_noprop_char (' ');
10997 ++n;
10998 }
10999
11000 return n;
11001 }
11002
11003 /***********************************************************************
11004 Frame Titles
11005 ***********************************************************************/
11006
11007 #ifdef HAVE_WINDOW_SYSTEM
11008
11009 /* Set the title of FRAME, if it has changed. The title format is
11010 Vicon_title_format if FRAME is iconified, otherwise it is
11011 frame_title_format. */
11012
11013 static void
11014 x_consider_frame_title (Lisp_Object frame)
11015 {
11016 struct frame *f = XFRAME (frame);
11017
11018 if (FRAME_WINDOW_P (f)
11019 || FRAME_MINIBUF_ONLY_P (f)
11020 || f->explicit_name)
11021 {
11022 /* Do we have more than one visible frame on this X display? */
11023 Lisp_Object tail;
11024 Lisp_Object fmt;
11025 ptrdiff_t title_start;
11026 char *title;
11027 ptrdiff_t len;
11028 struct it it;
11029 ptrdiff_t count = SPECPDL_INDEX ();
11030
11031 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11032 {
11033 Lisp_Object other_frame = XCAR (tail);
11034 struct frame *tf = XFRAME (other_frame);
11035
11036 if (tf != f
11037 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11038 && !FRAME_MINIBUF_ONLY_P (tf)
11039 && !EQ (other_frame, tip_frame)
11040 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11041 break;
11042 }
11043
11044 /* Set global variable indicating that multiple frames exist. */
11045 multiple_frames = CONSP (tail);
11046
11047 /* Switch to the buffer of selected window of the frame. Set up
11048 mode_line_target so that display_mode_element will output into
11049 mode_line_noprop_buf; then display the title. */
11050 record_unwind_protect (unwind_format_mode_line,
11051 format_mode_line_unwind_data
11052 (f, current_buffer, selected_window, 0));
11053
11054 Fselect_window (f->selected_window, Qt);
11055 set_buffer_internal_1
11056 (XBUFFER (WGET (XWINDOW (f->selected_window), buffer)));
11057 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11058
11059 mode_line_target = MODE_LINE_TITLE;
11060 title_start = MODE_LINE_NOPROP_LEN (0);
11061 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11062 NULL, DEFAULT_FACE_ID);
11063 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11064 len = MODE_LINE_NOPROP_LEN (title_start);
11065 title = mode_line_noprop_buf + title_start;
11066 unbind_to (count, Qnil);
11067
11068 /* Set the title only if it's changed. This avoids consing in
11069 the common case where it hasn't. (If it turns out that we've
11070 already wasted too much time by walking through the list with
11071 display_mode_element, then we might need to optimize at a
11072 higher level than this.) */
11073 if (! STRINGP (f->name)
11074 || SBYTES (f->name) != len
11075 || memcmp (title, SDATA (f->name), len) != 0)
11076 x_implicitly_set_name (f, make_string (title, len), Qnil);
11077 }
11078 }
11079
11080 #endif /* not HAVE_WINDOW_SYSTEM */
11081
11082 \f
11083 /***********************************************************************
11084 Menu Bars
11085 ***********************************************************************/
11086
11087
11088 /* Prepare for redisplay by updating menu-bar item lists when
11089 appropriate. This can call eval. */
11090
11091 void
11092 prepare_menu_bars (void)
11093 {
11094 int all_windows;
11095 struct gcpro gcpro1, gcpro2;
11096 struct frame *f;
11097 Lisp_Object tooltip_frame;
11098
11099 #ifdef HAVE_WINDOW_SYSTEM
11100 tooltip_frame = tip_frame;
11101 #else
11102 tooltip_frame = Qnil;
11103 #endif
11104
11105 /* Update all frame titles based on their buffer names, etc. We do
11106 this before the menu bars so that the buffer-menu will show the
11107 up-to-date frame titles. */
11108 #ifdef HAVE_WINDOW_SYSTEM
11109 if (windows_or_buffers_changed || update_mode_lines)
11110 {
11111 Lisp_Object tail, frame;
11112
11113 FOR_EACH_FRAME (tail, frame)
11114 {
11115 f = XFRAME (frame);
11116 if (!EQ (frame, tooltip_frame)
11117 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11118 x_consider_frame_title (frame);
11119 }
11120 }
11121 #endif /* HAVE_WINDOW_SYSTEM */
11122
11123 /* Update the menu bar item lists, if appropriate. This has to be
11124 done before any actual redisplay or generation of display lines. */
11125 all_windows = (update_mode_lines
11126 || buffer_shared > 1
11127 || windows_or_buffers_changed);
11128 if (all_windows)
11129 {
11130 Lisp_Object tail, frame;
11131 ptrdiff_t count = SPECPDL_INDEX ();
11132 /* 1 means that update_menu_bar has run its hooks
11133 so any further calls to update_menu_bar shouldn't do so again. */
11134 int menu_bar_hooks_run = 0;
11135
11136 record_unwind_save_match_data ();
11137
11138 FOR_EACH_FRAME (tail, frame)
11139 {
11140 f = XFRAME (frame);
11141
11142 /* Ignore tooltip frame. */
11143 if (EQ (frame, tooltip_frame))
11144 continue;
11145
11146 /* If a window on this frame changed size, report that to
11147 the user and clear the size-change flag. */
11148 if (FRAME_WINDOW_SIZES_CHANGED (f))
11149 {
11150 Lisp_Object functions;
11151
11152 /* Clear flag first in case we get an error below. */
11153 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11154 functions = Vwindow_size_change_functions;
11155 GCPRO2 (tail, functions);
11156
11157 while (CONSP (functions))
11158 {
11159 if (!EQ (XCAR (functions), Qt))
11160 call1 (XCAR (functions), frame);
11161 functions = XCDR (functions);
11162 }
11163 UNGCPRO;
11164 }
11165
11166 GCPRO1 (tail);
11167 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11168 #ifdef HAVE_WINDOW_SYSTEM
11169 update_tool_bar (f, 0);
11170 #endif
11171 #ifdef HAVE_NS
11172 if (windows_or_buffers_changed
11173 && FRAME_NS_P (f))
11174 ns_set_doc_edited
11175 (f, Fbuffer_modified_p
11176 (WGET (XWINDOW (f->selected_window), buffer)));
11177 #endif
11178 UNGCPRO;
11179 }
11180
11181 unbind_to (count, Qnil);
11182 }
11183 else
11184 {
11185 struct frame *sf = SELECTED_FRAME ();
11186 update_menu_bar (sf, 1, 0);
11187 #ifdef HAVE_WINDOW_SYSTEM
11188 update_tool_bar (sf, 1);
11189 #endif
11190 }
11191 }
11192
11193
11194 /* Update the menu bar item list for frame F. This has to be done
11195 before we start to fill in any display lines, because it can call
11196 eval.
11197
11198 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11199
11200 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11201 already ran the menu bar hooks for this redisplay, so there
11202 is no need to run them again. The return value is the
11203 updated value of this flag, to pass to the next call. */
11204
11205 static int
11206 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11207 {
11208 Lisp_Object window;
11209 register struct window *w;
11210
11211 /* If called recursively during a menu update, do nothing. This can
11212 happen when, for instance, an activate-menubar-hook causes a
11213 redisplay. */
11214 if (inhibit_menubar_update)
11215 return hooks_run;
11216
11217 window = FRAME_SELECTED_WINDOW (f);
11218 w = XWINDOW (window);
11219
11220 if (FRAME_WINDOW_P (f)
11221 ?
11222 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11223 || defined (HAVE_NS) || defined (USE_GTK)
11224 FRAME_EXTERNAL_MENU_BAR (f)
11225 #else
11226 FRAME_MENU_BAR_LINES (f) > 0
11227 #endif
11228 : FRAME_MENU_BAR_LINES (f) > 0)
11229 {
11230 /* If the user has switched buffers or windows, we need to
11231 recompute to reflect the new bindings. But we'll
11232 recompute when update_mode_lines is set too; that means
11233 that people can use force-mode-line-update to request
11234 that the menu bar be recomputed. The adverse effect on
11235 the rest of the redisplay algorithm is about the same as
11236 windows_or_buffers_changed anyway. */
11237 if (windows_or_buffers_changed
11238 /* This used to test w->update_mode_line, but we believe
11239 there is no need to recompute the menu in that case. */
11240 || update_mode_lines
11241 || ((BUF_SAVE_MODIFF (XBUFFER (WGET (w, buffer)))
11242 < BUF_MODIFF (XBUFFER (WGET (w, buffer))))
11243 != w->last_had_star)
11244 || ((!NILP (Vtransient_mark_mode)
11245 && !NILP (BVAR (XBUFFER (WGET (w, buffer)), mark_active)))
11246 != !NILP (WGET (w, region_showing))))
11247 {
11248 struct buffer *prev = current_buffer;
11249 ptrdiff_t count = SPECPDL_INDEX ();
11250
11251 specbind (Qinhibit_menubar_update, Qt);
11252
11253 set_buffer_internal_1 (XBUFFER (WGET (w, buffer)));
11254 if (save_match_data)
11255 record_unwind_save_match_data ();
11256 if (NILP (Voverriding_local_map_menu_flag))
11257 {
11258 specbind (Qoverriding_terminal_local_map, Qnil);
11259 specbind (Qoverriding_local_map, Qnil);
11260 }
11261
11262 if (!hooks_run)
11263 {
11264 /* Run the Lucid hook. */
11265 safe_run_hooks (Qactivate_menubar_hook);
11266
11267 /* If it has changed current-menubar from previous value,
11268 really recompute the menu-bar from the value. */
11269 if (! NILP (Vlucid_menu_bar_dirty_flag))
11270 call0 (Qrecompute_lucid_menubar);
11271
11272 safe_run_hooks (Qmenu_bar_update_hook);
11273
11274 hooks_run = 1;
11275 }
11276
11277 XSETFRAME (Vmenu_updating_frame, f);
11278 FSET (f, menu_bar_items, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11279
11280 /* Redisplay the menu bar in case we changed it. */
11281 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11282 || defined (HAVE_NS) || defined (USE_GTK)
11283 if (FRAME_WINDOW_P (f))
11284 {
11285 #if defined (HAVE_NS)
11286 /* All frames on Mac OS share the same menubar. So only
11287 the selected frame should be allowed to set it. */
11288 if (f == SELECTED_FRAME ())
11289 #endif
11290 set_frame_menubar (f, 0, 0);
11291 }
11292 else
11293 /* On a terminal screen, the menu bar is an ordinary screen
11294 line, and this makes it get updated. */
11295 w->update_mode_line = 1;
11296 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11297 /* In the non-toolkit version, the menu bar is an ordinary screen
11298 line, and this makes it get updated. */
11299 w->update_mode_line = 1;
11300 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11301
11302 unbind_to (count, Qnil);
11303 set_buffer_internal_1 (prev);
11304 }
11305 }
11306
11307 return hooks_run;
11308 }
11309
11310
11311 \f
11312 /***********************************************************************
11313 Output Cursor
11314 ***********************************************************************/
11315
11316 #ifdef HAVE_WINDOW_SYSTEM
11317
11318 /* EXPORT:
11319 Nominal cursor position -- where to draw output.
11320 HPOS and VPOS are window relative glyph matrix coordinates.
11321 X and Y are window relative pixel coordinates. */
11322
11323 struct cursor_pos output_cursor;
11324
11325
11326 /* EXPORT:
11327 Set the global variable output_cursor to CURSOR. All cursor
11328 positions are relative to updated_window. */
11329
11330 void
11331 set_output_cursor (struct cursor_pos *cursor)
11332 {
11333 output_cursor.hpos = cursor->hpos;
11334 output_cursor.vpos = cursor->vpos;
11335 output_cursor.x = cursor->x;
11336 output_cursor.y = cursor->y;
11337 }
11338
11339
11340 /* EXPORT for RIF:
11341 Set a nominal cursor position.
11342
11343 HPOS and VPOS are column/row positions in a window glyph matrix. X
11344 and Y are window text area relative pixel positions.
11345
11346 If this is done during an update, updated_window will contain the
11347 window that is being updated and the position is the future output
11348 cursor position for that window. If updated_window is null, use
11349 selected_window and display the cursor at the given position. */
11350
11351 void
11352 x_cursor_to (int vpos, int hpos, int y, int x)
11353 {
11354 struct window *w;
11355
11356 /* If updated_window is not set, work on selected_window. */
11357 if (updated_window)
11358 w = updated_window;
11359 else
11360 w = XWINDOW (selected_window);
11361
11362 /* Set the output cursor. */
11363 output_cursor.hpos = hpos;
11364 output_cursor.vpos = vpos;
11365 output_cursor.x = x;
11366 output_cursor.y = y;
11367
11368 /* If not called as part of an update, really display the cursor.
11369 This will also set the cursor position of W. */
11370 if (updated_window == NULL)
11371 {
11372 BLOCK_INPUT;
11373 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11374 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11375 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11376 UNBLOCK_INPUT;
11377 }
11378 }
11379
11380 #endif /* HAVE_WINDOW_SYSTEM */
11381
11382 \f
11383 /***********************************************************************
11384 Tool-bars
11385 ***********************************************************************/
11386
11387 #ifdef HAVE_WINDOW_SYSTEM
11388
11389 /* Where the mouse was last time we reported a mouse event. */
11390
11391 FRAME_PTR last_mouse_frame;
11392
11393 /* Tool-bar item index of the item on which a mouse button was pressed
11394 or -1. */
11395
11396 int last_tool_bar_item;
11397
11398
11399 static Lisp_Object
11400 update_tool_bar_unwind (Lisp_Object frame)
11401 {
11402 selected_frame = frame;
11403 return Qnil;
11404 }
11405
11406 /* Update the tool-bar item list for frame F. This has to be done
11407 before we start to fill in any display lines. Called from
11408 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11409 and restore it here. */
11410
11411 static void
11412 update_tool_bar (struct frame *f, int save_match_data)
11413 {
11414 #if defined (USE_GTK) || defined (HAVE_NS)
11415 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11416 #else
11417 int do_update = WINDOWP (f->tool_bar_window)
11418 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11419 #endif
11420
11421 if (do_update)
11422 {
11423 Lisp_Object window;
11424 struct window *w;
11425
11426 window = FRAME_SELECTED_WINDOW (f);
11427 w = XWINDOW (window);
11428
11429 /* If the user has switched buffers or windows, we need to
11430 recompute to reflect the new bindings. But we'll
11431 recompute when update_mode_lines is set too; that means
11432 that people can use force-mode-line-update to request
11433 that the menu bar be recomputed. The adverse effect on
11434 the rest of the redisplay algorithm is about the same as
11435 windows_or_buffers_changed anyway. */
11436 if (windows_or_buffers_changed
11437 || w->update_mode_line
11438 || update_mode_lines
11439 || ((BUF_SAVE_MODIFF (XBUFFER (WGET (w, buffer)))
11440 < BUF_MODIFF (XBUFFER (WGET (w, buffer))))
11441 != w->last_had_star)
11442 || ((!NILP (Vtransient_mark_mode)
11443 && !NILP (BVAR (XBUFFER (WGET (w, buffer)), mark_active)))
11444 != !NILP (WGET (w, region_showing))))
11445 {
11446 struct buffer *prev = current_buffer;
11447 ptrdiff_t count = SPECPDL_INDEX ();
11448 Lisp_Object frame, new_tool_bar;
11449 int new_n_tool_bar;
11450 struct gcpro gcpro1;
11451
11452 /* Set current_buffer to the buffer of the selected
11453 window of the frame, so that we get the right local
11454 keymaps. */
11455 set_buffer_internal_1 (XBUFFER (WGET (w, buffer)));
11456
11457 /* Save match data, if we must. */
11458 if (save_match_data)
11459 record_unwind_save_match_data ();
11460
11461 /* Make sure that we don't accidentally use bogus keymaps. */
11462 if (NILP (Voverriding_local_map_menu_flag))
11463 {
11464 specbind (Qoverriding_terminal_local_map, Qnil);
11465 specbind (Qoverriding_local_map, Qnil);
11466 }
11467
11468 GCPRO1 (new_tool_bar);
11469
11470 /* We must temporarily set the selected frame to this frame
11471 before calling tool_bar_items, because the calculation of
11472 the tool-bar keymap uses the selected frame (see
11473 `tool-bar-make-keymap' in tool-bar.el). */
11474 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11475 XSETFRAME (frame, f);
11476 selected_frame = frame;
11477
11478 /* Build desired tool-bar items from keymaps. */
11479 new_tool_bar
11480 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11481 &new_n_tool_bar);
11482
11483 /* Redisplay the tool-bar if we changed it. */
11484 if (new_n_tool_bar != f->n_tool_bar_items
11485 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11486 {
11487 /* Redisplay that happens asynchronously due to an expose event
11488 may access f->tool_bar_items. Make sure we update both
11489 variables within BLOCK_INPUT so no such event interrupts. */
11490 BLOCK_INPUT;
11491 FSET (f, tool_bar_items, new_tool_bar);
11492 f->n_tool_bar_items = new_n_tool_bar;
11493 w->update_mode_line = 1;
11494 UNBLOCK_INPUT;
11495 }
11496
11497 UNGCPRO;
11498
11499 unbind_to (count, Qnil);
11500 set_buffer_internal_1 (prev);
11501 }
11502 }
11503 }
11504
11505
11506 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11507 F's desired tool-bar contents. F->tool_bar_items must have
11508 been set up previously by calling prepare_menu_bars. */
11509
11510 static void
11511 build_desired_tool_bar_string (struct frame *f)
11512 {
11513 int i, size, size_needed;
11514 struct gcpro gcpro1, gcpro2, gcpro3;
11515 Lisp_Object image, plist, props;
11516
11517 image = plist = props = Qnil;
11518 GCPRO3 (image, plist, props);
11519
11520 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11521 Otherwise, make a new string. */
11522
11523 /* The size of the string we might be able to reuse. */
11524 size = (STRINGP (f->desired_tool_bar_string)
11525 ? SCHARS (f->desired_tool_bar_string)
11526 : 0);
11527
11528 /* We need one space in the string for each image. */
11529 size_needed = f->n_tool_bar_items;
11530
11531 /* Reuse f->desired_tool_bar_string, if possible. */
11532 if (size < size_needed || NILP (f->desired_tool_bar_string))
11533 FSET (f, desired_tool_bar_string,
11534 Fmake_string (make_number (size_needed), make_number (' ')));
11535 else
11536 {
11537 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11538 Fremove_text_properties (make_number (0), make_number (size),
11539 props, f->desired_tool_bar_string);
11540 }
11541
11542 /* Put a `display' property on the string for the images to display,
11543 put a `menu_item' property on tool-bar items with a value that
11544 is the index of the item in F's tool-bar item vector. */
11545 for (i = 0; i < f->n_tool_bar_items; ++i)
11546 {
11547 #define PROP(IDX) \
11548 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11549
11550 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11551 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11552 int hmargin, vmargin, relief, idx, end;
11553
11554 /* If image is a vector, choose the image according to the
11555 button state. */
11556 image = PROP (TOOL_BAR_ITEM_IMAGES);
11557 if (VECTORP (image))
11558 {
11559 if (enabled_p)
11560 idx = (selected_p
11561 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11562 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11563 else
11564 idx = (selected_p
11565 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11566 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11567
11568 eassert (ASIZE (image) >= idx);
11569 image = AREF (image, idx);
11570 }
11571 else
11572 idx = -1;
11573
11574 /* Ignore invalid image specifications. */
11575 if (!valid_image_p (image))
11576 continue;
11577
11578 /* Display the tool-bar button pressed, or depressed. */
11579 plist = Fcopy_sequence (XCDR (image));
11580
11581 /* Compute margin and relief to draw. */
11582 relief = (tool_bar_button_relief >= 0
11583 ? tool_bar_button_relief
11584 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11585 hmargin = vmargin = relief;
11586
11587 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11588 INT_MAX - max (hmargin, vmargin)))
11589 {
11590 hmargin += XFASTINT (Vtool_bar_button_margin);
11591 vmargin += XFASTINT (Vtool_bar_button_margin);
11592 }
11593 else if (CONSP (Vtool_bar_button_margin))
11594 {
11595 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11596 INT_MAX - hmargin))
11597 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11598
11599 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11600 INT_MAX - vmargin))
11601 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11602 }
11603
11604 if (auto_raise_tool_bar_buttons_p)
11605 {
11606 /* Add a `:relief' property to the image spec if the item is
11607 selected. */
11608 if (selected_p)
11609 {
11610 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11611 hmargin -= relief;
11612 vmargin -= relief;
11613 }
11614 }
11615 else
11616 {
11617 /* If image is selected, display it pressed, i.e. with a
11618 negative relief. If it's not selected, display it with a
11619 raised relief. */
11620 plist = Fplist_put (plist, QCrelief,
11621 (selected_p
11622 ? make_number (-relief)
11623 : make_number (relief)));
11624 hmargin -= relief;
11625 vmargin -= relief;
11626 }
11627
11628 /* Put a margin around the image. */
11629 if (hmargin || vmargin)
11630 {
11631 if (hmargin == vmargin)
11632 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11633 else
11634 plist = Fplist_put (plist, QCmargin,
11635 Fcons (make_number (hmargin),
11636 make_number (vmargin)));
11637 }
11638
11639 /* If button is not enabled, and we don't have special images
11640 for the disabled state, make the image appear disabled by
11641 applying an appropriate algorithm to it. */
11642 if (!enabled_p && idx < 0)
11643 plist = Fplist_put (plist, QCconversion, Qdisabled);
11644
11645 /* Put a `display' text property on the string for the image to
11646 display. Put a `menu-item' property on the string that gives
11647 the start of this item's properties in the tool-bar items
11648 vector. */
11649 image = Fcons (Qimage, plist);
11650 props = list4 (Qdisplay, image,
11651 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11652
11653 /* Let the last image hide all remaining spaces in the tool bar
11654 string. The string can be longer than needed when we reuse a
11655 previous string. */
11656 if (i + 1 == f->n_tool_bar_items)
11657 end = SCHARS (f->desired_tool_bar_string);
11658 else
11659 end = i + 1;
11660 Fadd_text_properties (make_number (i), make_number (end),
11661 props, f->desired_tool_bar_string);
11662 #undef PROP
11663 }
11664
11665 UNGCPRO;
11666 }
11667
11668
11669 /* Display one line of the tool-bar of frame IT->f.
11670
11671 HEIGHT specifies the desired height of the tool-bar line.
11672 If the actual height of the glyph row is less than HEIGHT, the
11673 row's height is increased to HEIGHT, and the icons are centered
11674 vertically in the new height.
11675
11676 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11677 count a final empty row in case the tool-bar width exactly matches
11678 the window width.
11679 */
11680
11681 static void
11682 display_tool_bar_line (struct it *it, int height)
11683 {
11684 struct glyph_row *row = it->glyph_row;
11685 int max_x = it->last_visible_x;
11686 struct glyph *last;
11687
11688 prepare_desired_row (row);
11689 row->y = it->current_y;
11690
11691 /* Note that this isn't made use of if the face hasn't a box,
11692 so there's no need to check the face here. */
11693 it->start_of_box_run_p = 1;
11694
11695 while (it->current_x < max_x)
11696 {
11697 int x, n_glyphs_before, i, nglyphs;
11698 struct it it_before;
11699
11700 /* Get the next display element. */
11701 if (!get_next_display_element (it))
11702 {
11703 /* Don't count empty row if we are counting needed tool-bar lines. */
11704 if (height < 0 && !it->hpos)
11705 return;
11706 break;
11707 }
11708
11709 /* Produce glyphs. */
11710 n_glyphs_before = row->used[TEXT_AREA];
11711 it_before = *it;
11712
11713 PRODUCE_GLYPHS (it);
11714
11715 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11716 i = 0;
11717 x = it_before.current_x;
11718 while (i < nglyphs)
11719 {
11720 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11721
11722 if (x + glyph->pixel_width > max_x)
11723 {
11724 /* Glyph doesn't fit on line. Backtrack. */
11725 row->used[TEXT_AREA] = n_glyphs_before;
11726 *it = it_before;
11727 /* If this is the only glyph on this line, it will never fit on the
11728 tool-bar, so skip it. But ensure there is at least one glyph,
11729 so we don't accidentally disable the tool-bar. */
11730 if (n_glyphs_before == 0
11731 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11732 break;
11733 goto out;
11734 }
11735
11736 ++it->hpos;
11737 x += glyph->pixel_width;
11738 ++i;
11739 }
11740
11741 /* Stop at line end. */
11742 if (ITERATOR_AT_END_OF_LINE_P (it))
11743 break;
11744
11745 set_iterator_to_next (it, 1);
11746 }
11747
11748 out:;
11749
11750 row->displays_text_p = row->used[TEXT_AREA] != 0;
11751
11752 /* Use default face for the border below the tool bar.
11753
11754 FIXME: When auto-resize-tool-bars is grow-only, there is
11755 no additional border below the possibly empty tool-bar lines.
11756 So to make the extra empty lines look "normal", we have to
11757 use the tool-bar face for the border too. */
11758 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11759 it->face_id = DEFAULT_FACE_ID;
11760
11761 extend_face_to_end_of_line (it);
11762 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11763 last->right_box_line_p = 1;
11764 if (last == row->glyphs[TEXT_AREA])
11765 last->left_box_line_p = 1;
11766
11767 /* Make line the desired height and center it vertically. */
11768 if ((height -= it->max_ascent + it->max_descent) > 0)
11769 {
11770 /* Don't add more than one line height. */
11771 height %= FRAME_LINE_HEIGHT (it->f);
11772 it->max_ascent += height / 2;
11773 it->max_descent += (height + 1) / 2;
11774 }
11775
11776 compute_line_metrics (it);
11777
11778 /* If line is empty, make it occupy the rest of the tool-bar. */
11779 if (!row->displays_text_p)
11780 {
11781 row->height = row->phys_height = it->last_visible_y - row->y;
11782 row->visible_height = row->height;
11783 row->ascent = row->phys_ascent = 0;
11784 row->extra_line_spacing = 0;
11785 }
11786
11787 row->full_width_p = 1;
11788 row->continued_p = 0;
11789 row->truncated_on_left_p = 0;
11790 row->truncated_on_right_p = 0;
11791
11792 it->current_x = it->hpos = 0;
11793 it->current_y += row->height;
11794 ++it->vpos;
11795 ++it->glyph_row;
11796 }
11797
11798
11799 /* Max tool-bar height. */
11800
11801 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11802 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11803
11804 /* Value is the number of screen lines needed to make all tool-bar
11805 items of frame F visible. The number of actual rows needed is
11806 returned in *N_ROWS if non-NULL. */
11807
11808 static int
11809 tool_bar_lines_needed (struct frame *f, int *n_rows)
11810 {
11811 struct window *w = XWINDOW (f->tool_bar_window);
11812 struct it it;
11813 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11814 the desired matrix, so use (unused) mode-line row as temporary row to
11815 avoid destroying the first tool-bar row. */
11816 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11817
11818 /* Initialize an iterator for iteration over
11819 F->desired_tool_bar_string in the tool-bar window of frame F. */
11820 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11821 it.first_visible_x = 0;
11822 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11823 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11824 it.paragraph_embedding = L2R;
11825
11826 while (!ITERATOR_AT_END_P (&it))
11827 {
11828 clear_glyph_row (temp_row);
11829 it.glyph_row = temp_row;
11830 display_tool_bar_line (&it, -1);
11831 }
11832 clear_glyph_row (temp_row);
11833
11834 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11835 if (n_rows)
11836 *n_rows = it.vpos > 0 ? it.vpos : -1;
11837
11838 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11839 }
11840
11841
11842 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11843 0, 1, 0,
11844 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11845 (Lisp_Object frame)
11846 {
11847 struct frame *f;
11848 struct window *w;
11849 int nlines = 0;
11850
11851 if (NILP (frame))
11852 frame = selected_frame;
11853 else
11854 CHECK_FRAME (frame);
11855 f = XFRAME (frame);
11856
11857 if (WINDOWP (f->tool_bar_window)
11858 && (w = XWINDOW (f->tool_bar_window),
11859 WINDOW_TOTAL_LINES (w) > 0))
11860 {
11861 update_tool_bar (f, 1);
11862 if (f->n_tool_bar_items)
11863 {
11864 build_desired_tool_bar_string (f);
11865 nlines = tool_bar_lines_needed (f, NULL);
11866 }
11867 }
11868
11869 return make_number (nlines);
11870 }
11871
11872
11873 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11874 height should be changed. */
11875
11876 static int
11877 redisplay_tool_bar (struct frame *f)
11878 {
11879 struct window *w;
11880 struct it it;
11881 struct glyph_row *row;
11882
11883 #if defined (USE_GTK) || defined (HAVE_NS)
11884 if (FRAME_EXTERNAL_TOOL_BAR (f))
11885 update_frame_tool_bar (f);
11886 return 0;
11887 #endif
11888
11889 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11890 do anything. This means you must start with tool-bar-lines
11891 non-zero to get the auto-sizing effect. Or in other words, you
11892 can turn off tool-bars by specifying tool-bar-lines zero. */
11893 if (!WINDOWP (f->tool_bar_window)
11894 || (w = XWINDOW (f->tool_bar_window),
11895 WINDOW_TOTAL_LINES (w) == 0))
11896 return 0;
11897
11898 /* Set up an iterator for the tool-bar window. */
11899 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11900 it.first_visible_x = 0;
11901 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11902 row = it.glyph_row;
11903
11904 /* Build a string that represents the contents of the tool-bar. */
11905 build_desired_tool_bar_string (f);
11906 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11907 /* FIXME: This should be controlled by a user option. But it
11908 doesn't make sense to have an R2L tool bar if the menu bar cannot
11909 be drawn also R2L, and making the menu bar R2L is tricky due
11910 toolkit-specific code that implements it. If an R2L tool bar is
11911 ever supported, display_tool_bar_line should also be augmented to
11912 call unproduce_glyphs like display_line and display_string
11913 do. */
11914 it.paragraph_embedding = L2R;
11915
11916 if (f->n_tool_bar_rows == 0)
11917 {
11918 int nlines;
11919
11920 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11921 nlines != WINDOW_TOTAL_LINES (w)))
11922 {
11923 Lisp_Object frame;
11924 int old_height = WINDOW_TOTAL_LINES (w);
11925
11926 XSETFRAME (frame, f);
11927 Fmodify_frame_parameters (frame,
11928 Fcons (Fcons (Qtool_bar_lines,
11929 make_number (nlines)),
11930 Qnil));
11931 if (WINDOW_TOTAL_LINES (w) != old_height)
11932 {
11933 clear_glyph_matrix (w->desired_matrix);
11934 fonts_changed_p = 1;
11935 return 1;
11936 }
11937 }
11938 }
11939
11940 /* Display as many lines as needed to display all tool-bar items. */
11941
11942 if (f->n_tool_bar_rows > 0)
11943 {
11944 int border, rows, height, extra;
11945
11946 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11947 border = XINT (Vtool_bar_border);
11948 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11949 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11950 else if (EQ (Vtool_bar_border, Qborder_width))
11951 border = f->border_width;
11952 else
11953 border = 0;
11954 if (border < 0)
11955 border = 0;
11956
11957 rows = f->n_tool_bar_rows;
11958 height = max (1, (it.last_visible_y - border) / rows);
11959 extra = it.last_visible_y - border - height * rows;
11960
11961 while (it.current_y < it.last_visible_y)
11962 {
11963 int h = 0;
11964 if (extra > 0 && rows-- > 0)
11965 {
11966 h = (extra + rows - 1) / rows;
11967 extra -= h;
11968 }
11969 display_tool_bar_line (&it, height + h);
11970 }
11971 }
11972 else
11973 {
11974 while (it.current_y < it.last_visible_y)
11975 display_tool_bar_line (&it, 0);
11976 }
11977
11978 /* It doesn't make much sense to try scrolling in the tool-bar
11979 window, so don't do it. */
11980 w->desired_matrix->no_scrolling_p = 1;
11981 w->must_be_updated_p = 1;
11982
11983 if (!NILP (Vauto_resize_tool_bars))
11984 {
11985 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11986 int change_height_p = 0;
11987
11988 /* If we couldn't display everything, change the tool-bar's
11989 height if there is room for more. */
11990 if (IT_STRING_CHARPOS (it) < it.end_charpos
11991 && it.current_y < max_tool_bar_height)
11992 change_height_p = 1;
11993
11994 row = it.glyph_row - 1;
11995
11996 /* If there are blank lines at the end, except for a partially
11997 visible blank line at the end that is smaller than
11998 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11999 if (!row->displays_text_p
12000 && row->height >= FRAME_LINE_HEIGHT (f))
12001 change_height_p = 1;
12002
12003 /* If row displays tool-bar items, but is partially visible,
12004 change the tool-bar's height. */
12005 if (row->displays_text_p
12006 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12007 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12008 change_height_p = 1;
12009
12010 /* Resize windows as needed by changing the `tool-bar-lines'
12011 frame parameter. */
12012 if (change_height_p)
12013 {
12014 Lisp_Object frame;
12015 int old_height = WINDOW_TOTAL_LINES (w);
12016 int nrows;
12017 int nlines = tool_bar_lines_needed (f, &nrows);
12018
12019 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12020 && !f->minimize_tool_bar_window_p)
12021 ? (nlines > old_height)
12022 : (nlines != old_height));
12023 f->minimize_tool_bar_window_p = 0;
12024
12025 if (change_height_p)
12026 {
12027 XSETFRAME (frame, f);
12028 Fmodify_frame_parameters (frame,
12029 Fcons (Fcons (Qtool_bar_lines,
12030 make_number (nlines)),
12031 Qnil));
12032 if (WINDOW_TOTAL_LINES (w) != old_height)
12033 {
12034 clear_glyph_matrix (w->desired_matrix);
12035 f->n_tool_bar_rows = nrows;
12036 fonts_changed_p = 1;
12037 return 1;
12038 }
12039 }
12040 }
12041 }
12042
12043 f->minimize_tool_bar_window_p = 0;
12044 return 0;
12045 }
12046
12047
12048 /* Get information about the tool-bar item which is displayed in GLYPH
12049 on frame F. Return in *PROP_IDX the index where tool-bar item
12050 properties start in F->tool_bar_items. Value is zero if
12051 GLYPH doesn't display a tool-bar item. */
12052
12053 static int
12054 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12055 {
12056 Lisp_Object prop;
12057 int success_p;
12058 int charpos;
12059
12060 /* This function can be called asynchronously, which means we must
12061 exclude any possibility that Fget_text_property signals an
12062 error. */
12063 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12064 charpos = max (0, charpos);
12065
12066 /* Get the text property `menu-item' at pos. The value of that
12067 property is the start index of this item's properties in
12068 F->tool_bar_items. */
12069 prop = Fget_text_property (make_number (charpos),
12070 Qmenu_item, f->current_tool_bar_string);
12071 if (INTEGERP (prop))
12072 {
12073 *prop_idx = XINT (prop);
12074 success_p = 1;
12075 }
12076 else
12077 success_p = 0;
12078
12079 return success_p;
12080 }
12081
12082 \f
12083 /* Get information about the tool-bar item at position X/Y on frame F.
12084 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12085 the current matrix of the tool-bar window of F, or NULL if not
12086 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12087 item in F->tool_bar_items. Value is
12088
12089 -1 if X/Y is not on a tool-bar item
12090 0 if X/Y is on the same item that was highlighted before.
12091 1 otherwise. */
12092
12093 static int
12094 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12095 int *hpos, int *vpos, int *prop_idx)
12096 {
12097 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12098 struct window *w = XWINDOW (f->tool_bar_window);
12099 int area;
12100
12101 /* Find the glyph under X/Y. */
12102 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12103 if (*glyph == NULL)
12104 return -1;
12105
12106 /* Get the start of this tool-bar item's properties in
12107 f->tool_bar_items. */
12108 if (!tool_bar_item_info (f, *glyph, prop_idx))
12109 return -1;
12110
12111 /* Is mouse on the highlighted item? */
12112 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12113 && *vpos >= hlinfo->mouse_face_beg_row
12114 && *vpos <= hlinfo->mouse_face_end_row
12115 && (*vpos > hlinfo->mouse_face_beg_row
12116 || *hpos >= hlinfo->mouse_face_beg_col)
12117 && (*vpos < hlinfo->mouse_face_end_row
12118 || *hpos < hlinfo->mouse_face_end_col
12119 || hlinfo->mouse_face_past_end))
12120 return 0;
12121
12122 return 1;
12123 }
12124
12125
12126 /* EXPORT:
12127 Handle mouse button event on the tool-bar of frame F, at
12128 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12129 0 for button release. MODIFIERS is event modifiers for button
12130 release. */
12131
12132 void
12133 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12134 int modifiers)
12135 {
12136 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12137 struct window *w = XWINDOW (f->tool_bar_window);
12138 int hpos, vpos, prop_idx;
12139 struct glyph *glyph;
12140 Lisp_Object enabled_p;
12141
12142 /* If not on the highlighted tool-bar item, return. */
12143 frame_to_window_pixel_xy (w, &x, &y);
12144 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12145 return;
12146
12147 /* If item is disabled, do nothing. */
12148 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12149 if (NILP (enabled_p))
12150 return;
12151
12152 if (down_p)
12153 {
12154 /* Show item in pressed state. */
12155 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12156 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12157 last_tool_bar_item = prop_idx;
12158 }
12159 else
12160 {
12161 Lisp_Object key, frame;
12162 struct input_event event;
12163 EVENT_INIT (event);
12164
12165 /* Show item in released state. */
12166 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12167 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12168
12169 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12170
12171 XSETFRAME (frame, f);
12172 event.kind = TOOL_BAR_EVENT;
12173 event.frame_or_window = frame;
12174 event.arg = frame;
12175 kbd_buffer_store_event (&event);
12176
12177 event.kind = TOOL_BAR_EVENT;
12178 event.frame_or_window = frame;
12179 event.arg = key;
12180 event.modifiers = modifiers;
12181 kbd_buffer_store_event (&event);
12182 last_tool_bar_item = -1;
12183 }
12184 }
12185
12186
12187 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12188 tool-bar window-relative coordinates X/Y. Called from
12189 note_mouse_highlight. */
12190
12191 static void
12192 note_tool_bar_highlight (struct frame *f, int x, int y)
12193 {
12194 Lisp_Object window = f->tool_bar_window;
12195 struct window *w = XWINDOW (window);
12196 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12197 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12198 int hpos, vpos;
12199 struct glyph *glyph;
12200 struct glyph_row *row;
12201 int i;
12202 Lisp_Object enabled_p;
12203 int prop_idx;
12204 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12205 int mouse_down_p, rc;
12206
12207 /* Function note_mouse_highlight is called with negative X/Y
12208 values when mouse moves outside of the frame. */
12209 if (x <= 0 || y <= 0)
12210 {
12211 clear_mouse_face (hlinfo);
12212 return;
12213 }
12214
12215 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12216 if (rc < 0)
12217 {
12218 /* Not on tool-bar item. */
12219 clear_mouse_face (hlinfo);
12220 return;
12221 }
12222 else if (rc == 0)
12223 /* On same tool-bar item as before. */
12224 goto set_help_echo;
12225
12226 clear_mouse_face (hlinfo);
12227
12228 /* Mouse is down, but on different tool-bar item? */
12229 mouse_down_p = (dpyinfo->grabbed
12230 && f == last_mouse_frame
12231 && FRAME_LIVE_P (f));
12232 if (mouse_down_p
12233 && last_tool_bar_item != prop_idx)
12234 return;
12235
12236 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12237 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12238
12239 /* If tool-bar item is not enabled, don't highlight it. */
12240 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12241 if (!NILP (enabled_p))
12242 {
12243 /* Compute the x-position of the glyph. In front and past the
12244 image is a space. We include this in the highlighted area. */
12245 row = MATRIX_ROW (w->current_matrix, vpos);
12246 for (i = x = 0; i < hpos; ++i)
12247 x += row->glyphs[TEXT_AREA][i].pixel_width;
12248
12249 /* Record this as the current active region. */
12250 hlinfo->mouse_face_beg_col = hpos;
12251 hlinfo->mouse_face_beg_row = vpos;
12252 hlinfo->mouse_face_beg_x = x;
12253 hlinfo->mouse_face_beg_y = row->y;
12254 hlinfo->mouse_face_past_end = 0;
12255
12256 hlinfo->mouse_face_end_col = hpos + 1;
12257 hlinfo->mouse_face_end_row = vpos;
12258 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12259 hlinfo->mouse_face_end_y = row->y;
12260 hlinfo->mouse_face_window = window;
12261 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12262
12263 /* Display it as active. */
12264 show_mouse_face (hlinfo, draw);
12265 hlinfo->mouse_face_image_state = draw;
12266 }
12267
12268 set_help_echo:
12269
12270 /* Set help_echo_string to a help string to display for this tool-bar item.
12271 XTread_socket does the rest. */
12272 help_echo_object = help_echo_window = Qnil;
12273 help_echo_pos = -1;
12274 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12275 if (NILP (help_echo_string))
12276 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12277 }
12278
12279 #endif /* HAVE_WINDOW_SYSTEM */
12280
12281
12282 \f
12283 /************************************************************************
12284 Horizontal scrolling
12285 ************************************************************************/
12286
12287 static int hscroll_window_tree (Lisp_Object);
12288 static int hscroll_windows (Lisp_Object);
12289
12290 /* For all leaf windows in the window tree rooted at WINDOW, set their
12291 hscroll value so that PT is (i) visible in the window, and (ii) so
12292 that it is not within a certain margin at the window's left and
12293 right border. Value is non-zero if any window's hscroll has been
12294 changed. */
12295
12296 static int
12297 hscroll_window_tree (Lisp_Object window)
12298 {
12299 int hscrolled_p = 0;
12300 int hscroll_relative_p = FLOATP (Vhscroll_step);
12301 int hscroll_step_abs = 0;
12302 double hscroll_step_rel = 0;
12303
12304 if (hscroll_relative_p)
12305 {
12306 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12307 if (hscroll_step_rel < 0)
12308 {
12309 hscroll_relative_p = 0;
12310 hscroll_step_abs = 0;
12311 }
12312 }
12313 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12314 {
12315 hscroll_step_abs = XINT (Vhscroll_step);
12316 if (hscroll_step_abs < 0)
12317 hscroll_step_abs = 0;
12318 }
12319 else
12320 hscroll_step_abs = 0;
12321
12322 while (WINDOWP (window))
12323 {
12324 struct window *w = XWINDOW (window);
12325
12326 if (WINDOWP (WGET (w, hchild)))
12327 hscrolled_p |= hscroll_window_tree (WGET (w, hchild));
12328 else if (WINDOWP (WGET (w, vchild)))
12329 hscrolled_p |= hscroll_window_tree (WGET (w, vchild));
12330 else if (w->cursor.vpos >= 0)
12331 {
12332 int h_margin;
12333 int text_area_width;
12334 struct glyph_row *current_cursor_row
12335 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12336 struct glyph_row *desired_cursor_row
12337 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12338 struct glyph_row *cursor_row
12339 = (desired_cursor_row->enabled_p
12340 ? desired_cursor_row
12341 : current_cursor_row);
12342 int row_r2l_p = cursor_row->reversed_p;
12343
12344 text_area_width = window_box_width (w, TEXT_AREA);
12345
12346 /* Scroll when cursor is inside this scroll margin. */
12347 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12348
12349 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, WGET (w, buffer)))
12350 /* For left-to-right rows, hscroll when cursor is either
12351 (i) inside the right hscroll margin, or (ii) if it is
12352 inside the left margin and the window is already
12353 hscrolled. */
12354 && ((!row_r2l_p
12355 && ((w->hscroll
12356 && w->cursor.x <= h_margin)
12357 || (cursor_row->enabled_p
12358 && cursor_row->truncated_on_right_p
12359 && (w->cursor.x >= text_area_width - h_margin))))
12360 /* For right-to-left rows, the logic is similar,
12361 except that rules for scrolling to left and right
12362 are reversed. E.g., if cursor.x <= h_margin, we
12363 need to hscroll "to the right" unconditionally,
12364 and that will scroll the screen to the left so as
12365 to reveal the next portion of the row. */
12366 || (row_r2l_p
12367 && ((cursor_row->enabled_p
12368 /* FIXME: It is confusing to set the
12369 truncated_on_right_p flag when R2L rows
12370 are actually truncated on the left. */
12371 && cursor_row->truncated_on_right_p
12372 && w->cursor.x <= h_margin)
12373 || (w->hscroll
12374 && (w->cursor.x >= text_area_width - h_margin))))))
12375 {
12376 struct it it;
12377 ptrdiff_t hscroll;
12378 struct buffer *saved_current_buffer;
12379 ptrdiff_t pt;
12380 int wanted_x;
12381
12382 /* Find point in a display of infinite width. */
12383 saved_current_buffer = current_buffer;
12384 current_buffer = XBUFFER (WGET (w, buffer));
12385
12386 if (w == XWINDOW (selected_window))
12387 pt = PT;
12388 else
12389 {
12390 pt = marker_position (WGET (w, pointm));
12391 pt = max (BEGV, pt);
12392 pt = min (ZV, pt);
12393 }
12394
12395 /* Move iterator to pt starting at cursor_row->start in
12396 a line with infinite width. */
12397 init_to_row_start (&it, w, cursor_row);
12398 it.last_visible_x = INFINITY;
12399 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12400 current_buffer = saved_current_buffer;
12401
12402 /* Position cursor in window. */
12403 if (!hscroll_relative_p && hscroll_step_abs == 0)
12404 hscroll = max (0, (it.current_x
12405 - (ITERATOR_AT_END_OF_LINE_P (&it)
12406 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12407 : (text_area_width / 2))))
12408 / FRAME_COLUMN_WIDTH (it.f);
12409 else if ((!row_r2l_p
12410 && w->cursor.x >= text_area_width - h_margin)
12411 || (row_r2l_p && w->cursor.x <= h_margin))
12412 {
12413 if (hscroll_relative_p)
12414 wanted_x = text_area_width * (1 - hscroll_step_rel)
12415 - h_margin;
12416 else
12417 wanted_x = text_area_width
12418 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12419 - h_margin;
12420 hscroll
12421 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12422 }
12423 else
12424 {
12425 if (hscroll_relative_p)
12426 wanted_x = text_area_width * hscroll_step_rel
12427 + h_margin;
12428 else
12429 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12430 + h_margin;
12431 hscroll
12432 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12433 }
12434 hscroll = max (hscroll, w->min_hscroll);
12435
12436 /* Don't prevent redisplay optimizations if hscroll
12437 hasn't changed, as it will unnecessarily slow down
12438 redisplay. */
12439 if (w->hscroll != hscroll)
12440 {
12441 XBUFFER (WGET (w, buffer))->prevent_redisplay_optimizations_p = 1;
12442 w->hscroll = hscroll;
12443 hscrolled_p = 1;
12444 }
12445 }
12446 }
12447
12448 window = WGET (w, next);
12449 }
12450
12451 /* Value is non-zero if hscroll of any leaf window has been changed. */
12452 return hscrolled_p;
12453 }
12454
12455
12456 /* Set hscroll so that cursor is visible and not inside horizontal
12457 scroll margins for all windows in the tree rooted at WINDOW. See
12458 also hscroll_window_tree above. Value is non-zero if any window's
12459 hscroll has been changed. If it has, desired matrices on the frame
12460 of WINDOW are cleared. */
12461
12462 static int
12463 hscroll_windows (Lisp_Object window)
12464 {
12465 int hscrolled_p = hscroll_window_tree (window);
12466 if (hscrolled_p)
12467 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12468 return hscrolled_p;
12469 }
12470
12471
12472 \f
12473 /************************************************************************
12474 Redisplay
12475 ************************************************************************/
12476
12477 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12478 to a non-zero value. This is sometimes handy to have in a debugger
12479 session. */
12480
12481 #ifdef GLYPH_DEBUG
12482
12483 /* First and last unchanged row for try_window_id. */
12484
12485 static int debug_first_unchanged_at_end_vpos;
12486 static int debug_last_unchanged_at_beg_vpos;
12487
12488 /* Delta vpos and y. */
12489
12490 static int debug_dvpos, debug_dy;
12491
12492 /* Delta in characters and bytes for try_window_id. */
12493
12494 static ptrdiff_t debug_delta, debug_delta_bytes;
12495
12496 /* Values of window_end_pos and window_end_vpos at the end of
12497 try_window_id. */
12498
12499 static ptrdiff_t debug_end_vpos;
12500
12501 /* Append a string to W->desired_matrix->method. FMT is a printf
12502 format string. If trace_redisplay_p is non-zero also printf the
12503 resulting string to stderr. */
12504
12505 static void debug_method_add (struct window *, char const *, ...)
12506 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12507
12508 static void
12509 debug_method_add (struct window *w, char const *fmt, ...)
12510 {
12511 char *method = w->desired_matrix->method;
12512 int len = strlen (method);
12513 int size = sizeof w->desired_matrix->method;
12514 int remaining = size - len - 1;
12515 va_list ap;
12516
12517 if (len && remaining)
12518 {
12519 method[len] = '|';
12520 --remaining, ++len;
12521 }
12522
12523 va_start (ap, fmt);
12524 vsnprintf (method + len, remaining + 1, fmt, ap);
12525 va_end (ap);
12526
12527 if (trace_redisplay_p)
12528 fprintf (stderr, "%p (%s): %s\n",
12529 w,
12530 ((BUFFERP (WGET (w, buffer))
12531 && STRINGP (BVAR (XBUFFER (WGET (w, buffer)), name)))
12532 ? SSDATA (BVAR (XBUFFER (WGET (w, buffer)), name))
12533 : "no buffer"),
12534 method + len);
12535 }
12536
12537 #endif /* GLYPH_DEBUG */
12538
12539
12540 /* Value is non-zero if all changes in window W, which displays
12541 current_buffer, are in the text between START and END. START is a
12542 buffer position, END is given as a distance from Z. Used in
12543 redisplay_internal for display optimization. */
12544
12545 static inline int
12546 text_outside_line_unchanged_p (struct window *w,
12547 ptrdiff_t start, ptrdiff_t end)
12548 {
12549 int unchanged_p = 1;
12550
12551 /* If text or overlays have changed, see where. */
12552 if (w->last_modified < MODIFF
12553 || w->last_overlay_modified < OVERLAY_MODIFF)
12554 {
12555 /* Gap in the line? */
12556 if (GPT < start || Z - GPT < end)
12557 unchanged_p = 0;
12558
12559 /* Changes start in front of the line, or end after it? */
12560 if (unchanged_p
12561 && (BEG_UNCHANGED < start - 1
12562 || END_UNCHANGED < end))
12563 unchanged_p = 0;
12564
12565 /* If selective display, can't optimize if changes start at the
12566 beginning of the line. */
12567 if (unchanged_p
12568 && INTEGERP (BVAR (current_buffer, selective_display))
12569 && XINT (BVAR (current_buffer, selective_display)) > 0
12570 && (BEG_UNCHANGED < start || GPT <= start))
12571 unchanged_p = 0;
12572
12573 /* If there are overlays at the start or end of the line, these
12574 may have overlay strings with newlines in them. A change at
12575 START, for instance, may actually concern the display of such
12576 overlay strings as well, and they are displayed on different
12577 lines. So, quickly rule out this case. (For the future, it
12578 might be desirable to implement something more telling than
12579 just BEG/END_UNCHANGED.) */
12580 if (unchanged_p)
12581 {
12582 if (BEG + BEG_UNCHANGED == start
12583 && overlay_touches_p (start))
12584 unchanged_p = 0;
12585 if (END_UNCHANGED == end
12586 && overlay_touches_p (Z - end))
12587 unchanged_p = 0;
12588 }
12589
12590 /* Under bidi reordering, adding or deleting a character in the
12591 beginning of a paragraph, before the first strong directional
12592 character, can change the base direction of the paragraph (unless
12593 the buffer specifies a fixed paragraph direction), which will
12594 require to redisplay the whole paragraph. It might be worthwhile
12595 to find the paragraph limits and widen the range of redisplayed
12596 lines to that, but for now just give up this optimization. */
12597 if (!NILP (BVAR (XBUFFER (WGET (w, buffer)), bidi_display_reordering))
12598 && NILP (BVAR (XBUFFER (WGET (w, buffer)), bidi_paragraph_direction)))
12599 unchanged_p = 0;
12600 }
12601
12602 return unchanged_p;
12603 }
12604
12605
12606 /* Do a frame update, taking possible shortcuts into account. This is
12607 the main external entry point for redisplay.
12608
12609 If the last redisplay displayed an echo area message and that message
12610 is no longer requested, we clear the echo area or bring back the
12611 mini-buffer if that is in use. */
12612
12613 void
12614 redisplay (void)
12615 {
12616 redisplay_internal ();
12617 }
12618
12619
12620 static Lisp_Object
12621 overlay_arrow_string_or_property (Lisp_Object var)
12622 {
12623 Lisp_Object val;
12624
12625 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12626 return val;
12627
12628 return Voverlay_arrow_string;
12629 }
12630
12631 /* Return 1 if there are any overlay-arrows in current_buffer. */
12632 static int
12633 overlay_arrow_in_current_buffer_p (void)
12634 {
12635 Lisp_Object vlist;
12636
12637 for (vlist = Voverlay_arrow_variable_list;
12638 CONSP (vlist);
12639 vlist = XCDR (vlist))
12640 {
12641 Lisp_Object var = XCAR (vlist);
12642 Lisp_Object val;
12643
12644 if (!SYMBOLP (var))
12645 continue;
12646 val = find_symbol_value (var);
12647 if (MARKERP (val)
12648 && current_buffer == XMARKER (val)->buffer)
12649 return 1;
12650 }
12651 return 0;
12652 }
12653
12654
12655 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12656 has changed. */
12657
12658 static int
12659 overlay_arrows_changed_p (void)
12660 {
12661 Lisp_Object vlist;
12662
12663 for (vlist = Voverlay_arrow_variable_list;
12664 CONSP (vlist);
12665 vlist = XCDR (vlist))
12666 {
12667 Lisp_Object var = XCAR (vlist);
12668 Lisp_Object val, pstr;
12669
12670 if (!SYMBOLP (var))
12671 continue;
12672 val = find_symbol_value (var);
12673 if (!MARKERP (val))
12674 continue;
12675 if (! EQ (COERCE_MARKER (val),
12676 Fget (var, Qlast_arrow_position))
12677 || ! (pstr = overlay_arrow_string_or_property (var),
12678 EQ (pstr, Fget (var, Qlast_arrow_string))))
12679 return 1;
12680 }
12681 return 0;
12682 }
12683
12684 /* Mark overlay arrows to be updated on next redisplay. */
12685
12686 static void
12687 update_overlay_arrows (int up_to_date)
12688 {
12689 Lisp_Object vlist;
12690
12691 for (vlist = Voverlay_arrow_variable_list;
12692 CONSP (vlist);
12693 vlist = XCDR (vlist))
12694 {
12695 Lisp_Object var = XCAR (vlist);
12696
12697 if (!SYMBOLP (var))
12698 continue;
12699
12700 if (up_to_date > 0)
12701 {
12702 Lisp_Object val = find_symbol_value (var);
12703 Fput (var, Qlast_arrow_position,
12704 COERCE_MARKER (val));
12705 Fput (var, Qlast_arrow_string,
12706 overlay_arrow_string_or_property (var));
12707 }
12708 else if (up_to_date < 0
12709 || !NILP (Fget (var, Qlast_arrow_position)))
12710 {
12711 Fput (var, Qlast_arrow_position, Qt);
12712 Fput (var, Qlast_arrow_string, Qt);
12713 }
12714 }
12715 }
12716
12717
12718 /* Return overlay arrow string to display at row.
12719 Return integer (bitmap number) for arrow bitmap in left fringe.
12720 Return nil if no overlay arrow. */
12721
12722 static Lisp_Object
12723 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12724 {
12725 Lisp_Object vlist;
12726
12727 for (vlist = Voverlay_arrow_variable_list;
12728 CONSP (vlist);
12729 vlist = XCDR (vlist))
12730 {
12731 Lisp_Object var = XCAR (vlist);
12732 Lisp_Object val;
12733
12734 if (!SYMBOLP (var))
12735 continue;
12736
12737 val = find_symbol_value (var);
12738
12739 if (MARKERP (val)
12740 && current_buffer == XMARKER (val)->buffer
12741 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12742 {
12743 if (FRAME_WINDOW_P (it->f)
12744 /* FIXME: if ROW->reversed_p is set, this should test
12745 the right fringe, not the left one. */
12746 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12747 {
12748 #ifdef HAVE_WINDOW_SYSTEM
12749 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12750 {
12751 int fringe_bitmap;
12752 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12753 return make_number (fringe_bitmap);
12754 }
12755 #endif
12756 return make_number (-1); /* Use default arrow bitmap */
12757 }
12758 return overlay_arrow_string_or_property (var);
12759 }
12760 }
12761
12762 return Qnil;
12763 }
12764
12765 /* Return 1 if point moved out of or into a composition. Otherwise
12766 return 0. PREV_BUF and PREV_PT are the last point buffer and
12767 position. BUF and PT are the current point buffer and position. */
12768
12769 static int
12770 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12771 struct buffer *buf, ptrdiff_t pt)
12772 {
12773 ptrdiff_t start, end;
12774 Lisp_Object prop;
12775 Lisp_Object buffer;
12776
12777 XSETBUFFER (buffer, buf);
12778 /* Check a composition at the last point if point moved within the
12779 same buffer. */
12780 if (prev_buf == buf)
12781 {
12782 if (prev_pt == pt)
12783 /* Point didn't move. */
12784 return 0;
12785
12786 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12787 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12788 && COMPOSITION_VALID_P (start, end, prop)
12789 && start < prev_pt && end > prev_pt)
12790 /* The last point was within the composition. Return 1 iff
12791 point moved out of the composition. */
12792 return (pt <= start || pt >= end);
12793 }
12794
12795 /* Check a composition at the current point. */
12796 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12797 && find_composition (pt, -1, &start, &end, &prop, buffer)
12798 && COMPOSITION_VALID_P (start, end, prop)
12799 && start < pt && end > pt);
12800 }
12801
12802
12803 /* Reconsider the setting of B->clip_changed which is displayed
12804 in window W. */
12805
12806 static inline void
12807 reconsider_clip_changes (struct window *w, struct buffer *b)
12808 {
12809 if (b->clip_changed
12810 && !NILP (WGET (w, window_end_valid))
12811 && w->current_matrix->buffer == b
12812 && w->current_matrix->zv == BUF_ZV (b)
12813 && w->current_matrix->begv == BUF_BEGV (b))
12814 b->clip_changed = 0;
12815
12816 /* If display wasn't paused, and W is not a tool bar window, see if
12817 point has been moved into or out of a composition. In that case,
12818 we set b->clip_changed to 1 to force updating the screen. If
12819 b->clip_changed has already been set to 1, we can skip this
12820 check. */
12821 if (!b->clip_changed
12822 && BUFFERP (WGET (w, buffer)) && !NILP (WGET (w, window_end_valid)))
12823 {
12824 ptrdiff_t pt;
12825
12826 if (w == XWINDOW (selected_window))
12827 pt = PT;
12828 else
12829 pt = marker_position (WGET (w, pointm));
12830
12831 if ((w->current_matrix->buffer != XBUFFER (WGET (w, buffer))
12832 || pt != w->last_point)
12833 && check_point_in_composition (w->current_matrix->buffer,
12834 w->last_point,
12835 XBUFFER (WGET (w, buffer)), pt))
12836 b->clip_changed = 1;
12837 }
12838 }
12839 \f
12840
12841 /* Select FRAME to forward the values of frame-local variables into C
12842 variables so that the redisplay routines can access those values
12843 directly. */
12844
12845 static void
12846 select_frame_for_redisplay (Lisp_Object frame)
12847 {
12848 Lisp_Object tail, tem;
12849 Lisp_Object old = selected_frame;
12850 struct Lisp_Symbol *sym;
12851
12852 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12853
12854 selected_frame = frame;
12855
12856 do {
12857 for (tail = XFRAME (frame)->param_alist;
12858 CONSP (tail); tail = XCDR (tail))
12859 if (CONSP (XCAR (tail))
12860 && (tem = XCAR (XCAR (tail)),
12861 SYMBOLP (tem))
12862 && (sym = indirect_variable (XSYMBOL (tem)),
12863 sym->redirect == SYMBOL_LOCALIZED)
12864 && sym->val.blv->frame_local)
12865 /* Use find_symbol_value rather than Fsymbol_value
12866 to avoid an error if it is void. */
12867 find_symbol_value (tem);
12868 } while (!EQ (frame, old) && (frame = old, 1));
12869 }
12870
12871
12872 #define STOP_POLLING \
12873 do { if (! polling_stopped_here) stop_polling (); \
12874 polling_stopped_here = 1; } while (0)
12875
12876 #define RESUME_POLLING \
12877 do { if (polling_stopped_here) start_polling (); \
12878 polling_stopped_here = 0; } while (0)
12879
12880
12881 /* Perhaps in the future avoid recentering windows if it
12882 is not necessary; currently that causes some problems. */
12883
12884 static void
12885 redisplay_internal (void)
12886 {
12887 struct window *w = XWINDOW (selected_window);
12888 struct window *sw;
12889 struct frame *fr;
12890 int pending;
12891 int must_finish = 0;
12892 struct text_pos tlbufpos, tlendpos;
12893 int number_of_visible_frames;
12894 ptrdiff_t count, count1;
12895 struct frame *sf;
12896 int polling_stopped_here = 0;
12897 Lisp_Object old_frame = selected_frame;
12898
12899 /* Non-zero means redisplay has to consider all windows on all
12900 frames. Zero means, only selected_window is considered. */
12901 int consider_all_windows_p;
12902
12903 /* Non-zero means redisplay has to redisplay the miniwindow */
12904 int update_miniwindow_p = 0;
12905
12906 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12907
12908 /* No redisplay if running in batch mode or frame is not yet fully
12909 initialized, or redisplay is explicitly turned off by setting
12910 Vinhibit_redisplay. */
12911 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12912 || !NILP (Vinhibit_redisplay))
12913 return;
12914
12915 /* Don't examine these until after testing Vinhibit_redisplay.
12916 When Emacs is shutting down, perhaps because its connection to
12917 X has dropped, we should not look at them at all. */
12918 fr = XFRAME (WGET (w, frame));
12919 sf = SELECTED_FRAME ();
12920
12921 if (!fr->glyphs_initialized_p)
12922 return;
12923
12924 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12925 if (popup_activated ())
12926 return;
12927 #endif
12928
12929 /* I don't think this happens but let's be paranoid. */
12930 if (redisplaying_p)
12931 return;
12932
12933 /* Record a function that resets redisplaying_p to its old value
12934 when we leave this function. */
12935 count = SPECPDL_INDEX ();
12936 record_unwind_protect (unwind_redisplay,
12937 Fcons (make_number (redisplaying_p), selected_frame));
12938 ++redisplaying_p;
12939 specbind (Qinhibit_free_realized_faces, Qnil);
12940
12941 {
12942 Lisp_Object tail, frame;
12943
12944 FOR_EACH_FRAME (tail, frame)
12945 {
12946 struct frame *f = XFRAME (frame);
12947 f->already_hscrolled_p = 0;
12948 }
12949 }
12950
12951 retry:
12952 /* Remember the currently selected window. */
12953 sw = w;
12954
12955 if (!EQ (old_frame, selected_frame)
12956 && FRAME_LIVE_P (XFRAME (old_frame)))
12957 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12958 selected_frame and selected_window to be temporarily out-of-sync so
12959 when we come back here via `goto retry', we need to resync because we
12960 may need to run Elisp code (via prepare_menu_bars). */
12961 select_frame_for_redisplay (old_frame);
12962
12963 pending = 0;
12964 reconsider_clip_changes (w, current_buffer);
12965 last_escape_glyph_frame = NULL;
12966 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12967 last_glyphless_glyph_frame = NULL;
12968 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12969
12970 /* If new fonts have been loaded that make a glyph matrix adjustment
12971 necessary, do it. */
12972 if (fonts_changed_p)
12973 {
12974 adjust_glyphs (NULL);
12975 ++windows_or_buffers_changed;
12976 fonts_changed_p = 0;
12977 }
12978
12979 /* If face_change_count is non-zero, init_iterator will free all
12980 realized faces, which includes the faces referenced from current
12981 matrices. So, we can't reuse current matrices in this case. */
12982 if (face_change_count)
12983 ++windows_or_buffers_changed;
12984
12985 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12986 && FRAME_TTY (sf)->previous_frame != sf)
12987 {
12988 /* Since frames on a single ASCII terminal share the same
12989 display area, displaying a different frame means redisplay
12990 the whole thing. */
12991 windows_or_buffers_changed++;
12992 SET_FRAME_GARBAGED (sf);
12993 #ifndef DOS_NT
12994 set_tty_color_mode (FRAME_TTY (sf), sf);
12995 #endif
12996 FRAME_TTY (sf)->previous_frame = sf;
12997 }
12998
12999 /* Set the visible flags for all frames. Do this before checking
13000 for resized or garbaged frames; they want to know if their frames
13001 are visible. See the comment in frame.h for
13002 FRAME_SAMPLE_VISIBILITY. */
13003 {
13004 Lisp_Object tail, frame;
13005
13006 number_of_visible_frames = 0;
13007
13008 FOR_EACH_FRAME (tail, frame)
13009 {
13010 struct frame *f = XFRAME (frame);
13011
13012 FRAME_SAMPLE_VISIBILITY (f);
13013 if (FRAME_VISIBLE_P (f))
13014 ++number_of_visible_frames;
13015 clear_desired_matrices (f);
13016 }
13017 }
13018
13019 /* Notice any pending interrupt request to change frame size. */
13020 do_pending_window_change (1);
13021
13022 /* do_pending_window_change could change the selected_window due to
13023 frame resizing which makes the selected window too small. */
13024 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13025 {
13026 sw = w;
13027 reconsider_clip_changes (w, current_buffer);
13028 }
13029
13030 /* Clear frames marked as garbaged. */
13031 if (frame_garbaged)
13032 clear_garbaged_frames ();
13033
13034 /* Build menubar and tool-bar items. */
13035 if (NILP (Vmemory_full))
13036 prepare_menu_bars ();
13037
13038 if (windows_or_buffers_changed)
13039 update_mode_lines++;
13040
13041 /* Detect case that we need to write or remove a star in the mode line. */
13042 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13043 {
13044 w->update_mode_line = 1;
13045 if (buffer_shared > 1)
13046 update_mode_lines++;
13047 }
13048
13049 /* Avoid invocation of point motion hooks by `current_column' below. */
13050 count1 = SPECPDL_INDEX ();
13051 specbind (Qinhibit_point_motion_hooks, Qt);
13052
13053 /* If %c is in the mode line, update it if needed. */
13054 if (!NILP (WGET (w, column_number_displayed))
13055 /* This alternative quickly identifies a common case
13056 where no change is needed. */
13057 && !(PT == w->last_point
13058 && w->last_modified >= MODIFF
13059 && w->last_overlay_modified >= OVERLAY_MODIFF)
13060 && (XFASTINT (WGET (w, column_number_displayed)) != current_column ()))
13061 w->update_mode_line = 1;
13062
13063 unbind_to (count1, Qnil);
13064
13065 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (WGET (w, frame))) = -1;
13066
13067 /* The variable buffer_shared is set in redisplay_window and
13068 indicates that we redisplay a buffer in different windows. See
13069 there. */
13070 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13071 || cursor_type_changed);
13072
13073 /* If specs for an arrow have changed, do thorough redisplay
13074 to ensure we remove any arrow that should no longer exist. */
13075 if (overlay_arrows_changed_p ())
13076 consider_all_windows_p = windows_or_buffers_changed = 1;
13077
13078 /* Normally the message* functions will have already displayed and
13079 updated the echo area, but the frame may have been trashed, or
13080 the update may have been preempted, so display the echo area
13081 again here. Checking message_cleared_p captures the case that
13082 the echo area should be cleared. */
13083 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13084 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13085 || (message_cleared_p
13086 && minibuf_level == 0
13087 /* If the mini-window is currently selected, this means the
13088 echo-area doesn't show through. */
13089 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13090 {
13091 int window_height_changed_p = echo_area_display (0);
13092
13093 if (message_cleared_p)
13094 update_miniwindow_p = 1;
13095
13096 must_finish = 1;
13097
13098 /* If we don't display the current message, don't clear the
13099 message_cleared_p flag, because, if we did, we wouldn't clear
13100 the echo area in the next redisplay which doesn't preserve
13101 the echo area. */
13102 if (!display_last_displayed_message_p)
13103 message_cleared_p = 0;
13104
13105 if (fonts_changed_p)
13106 goto retry;
13107 else if (window_height_changed_p)
13108 {
13109 consider_all_windows_p = 1;
13110 ++update_mode_lines;
13111 ++windows_or_buffers_changed;
13112
13113 /* If window configuration was changed, frames may have been
13114 marked garbaged. Clear them or we will experience
13115 surprises wrt scrolling. */
13116 if (frame_garbaged)
13117 clear_garbaged_frames ();
13118 }
13119 }
13120 else if (EQ (selected_window, minibuf_window)
13121 && (current_buffer->clip_changed
13122 || w->last_modified < MODIFF
13123 || w->last_overlay_modified < OVERLAY_MODIFF)
13124 && resize_mini_window (w, 0))
13125 {
13126 /* Resized active mini-window to fit the size of what it is
13127 showing if its contents might have changed. */
13128 must_finish = 1;
13129 /* FIXME: this causes all frames to be updated, which seems unnecessary
13130 since only the current frame needs to be considered. This function needs
13131 to be rewritten with two variables, consider_all_windows and
13132 consider_all_frames. */
13133 consider_all_windows_p = 1;
13134 ++windows_or_buffers_changed;
13135 ++update_mode_lines;
13136
13137 /* If window configuration was changed, frames may have been
13138 marked garbaged. Clear them or we will experience
13139 surprises wrt scrolling. */
13140 if (frame_garbaged)
13141 clear_garbaged_frames ();
13142 }
13143
13144
13145 /* If showing the region, and mark has changed, we must redisplay
13146 the whole window. The assignment to this_line_start_pos prevents
13147 the optimization directly below this if-statement. */
13148 if (((!NILP (Vtransient_mark_mode)
13149 && !NILP (BVAR (XBUFFER (WGET (w, buffer)), mark_active)))
13150 != !NILP (WGET (w, region_showing)))
13151 || (!NILP (WGET (w, region_showing))
13152 && !EQ (WGET (w, region_showing),
13153 Fmarker_position (BVAR (XBUFFER (WGET (w, buffer)), mark)))))
13154 CHARPOS (this_line_start_pos) = 0;
13155
13156 /* Optimize the case that only the line containing the cursor in the
13157 selected window has changed. Variables starting with this_ are
13158 set in display_line and record information about the line
13159 containing the cursor. */
13160 tlbufpos = this_line_start_pos;
13161 tlendpos = this_line_end_pos;
13162 if (!consider_all_windows_p
13163 && CHARPOS (tlbufpos) > 0
13164 && !w->update_mode_line
13165 && !current_buffer->clip_changed
13166 && !current_buffer->prevent_redisplay_optimizations_p
13167 && FRAME_VISIBLE_P (XFRAME (WGET (w, frame)))
13168 && !FRAME_OBSCURED_P (XFRAME (WGET (w, frame)))
13169 /* Make sure recorded data applies to current buffer, etc. */
13170 && this_line_buffer == current_buffer
13171 && current_buffer == XBUFFER (WGET (w, buffer))
13172 && !w->force_start
13173 && !w->optional_new_start
13174 /* Point must be on the line that we have info recorded about. */
13175 && PT >= CHARPOS (tlbufpos)
13176 && PT <= Z - CHARPOS (tlendpos)
13177 /* All text outside that line, including its final newline,
13178 must be unchanged. */
13179 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13180 CHARPOS (tlendpos)))
13181 {
13182 if (CHARPOS (tlbufpos) > BEGV
13183 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13184 && (CHARPOS (tlbufpos) == ZV
13185 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13186 /* Former continuation line has disappeared by becoming empty. */
13187 goto cancel;
13188 else if (w->last_modified < MODIFF
13189 || w->last_overlay_modified < OVERLAY_MODIFF
13190 || MINI_WINDOW_P (w))
13191 {
13192 /* We have to handle the case of continuation around a
13193 wide-column character (see the comment in indent.c around
13194 line 1340).
13195
13196 For instance, in the following case:
13197
13198 -------- Insert --------
13199 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13200 J_I_ ==> J_I_ `^^' are cursors.
13201 ^^ ^^
13202 -------- --------
13203
13204 As we have to redraw the line above, we cannot use this
13205 optimization. */
13206
13207 struct it it;
13208 int line_height_before = this_line_pixel_height;
13209
13210 /* Note that start_display will handle the case that the
13211 line starting at tlbufpos is a continuation line. */
13212 start_display (&it, w, tlbufpos);
13213
13214 /* Implementation note: It this still necessary? */
13215 if (it.current_x != this_line_start_x)
13216 goto cancel;
13217
13218 TRACE ((stderr, "trying display optimization 1\n"));
13219 w->cursor.vpos = -1;
13220 overlay_arrow_seen = 0;
13221 it.vpos = this_line_vpos;
13222 it.current_y = this_line_y;
13223 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13224 display_line (&it);
13225
13226 /* If line contains point, is not continued,
13227 and ends at same distance from eob as before, we win. */
13228 if (w->cursor.vpos >= 0
13229 /* Line is not continued, otherwise this_line_start_pos
13230 would have been set to 0 in display_line. */
13231 && CHARPOS (this_line_start_pos)
13232 /* Line ends as before. */
13233 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13234 /* Line has same height as before. Otherwise other lines
13235 would have to be shifted up or down. */
13236 && this_line_pixel_height == line_height_before)
13237 {
13238 /* If this is not the window's last line, we must adjust
13239 the charstarts of the lines below. */
13240 if (it.current_y < it.last_visible_y)
13241 {
13242 struct glyph_row *row
13243 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13244 ptrdiff_t delta, delta_bytes;
13245
13246 /* We used to distinguish between two cases here,
13247 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13248 when the line ends in a newline or the end of the
13249 buffer's accessible portion. But both cases did
13250 the same, so they were collapsed. */
13251 delta = (Z
13252 - CHARPOS (tlendpos)
13253 - MATRIX_ROW_START_CHARPOS (row));
13254 delta_bytes = (Z_BYTE
13255 - BYTEPOS (tlendpos)
13256 - MATRIX_ROW_START_BYTEPOS (row));
13257
13258 increment_matrix_positions (w->current_matrix,
13259 this_line_vpos + 1,
13260 w->current_matrix->nrows,
13261 delta, delta_bytes);
13262 }
13263
13264 /* If this row displays text now but previously didn't,
13265 or vice versa, w->window_end_vpos may have to be
13266 adjusted. */
13267 if ((it.glyph_row - 1)->displays_text_p)
13268 {
13269 if (XFASTINT (WGET (w, window_end_vpos)) < this_line_vpos)
13270 WSET (w, window_end_vpos, make_number (this_line_vpos));
13271 }
13272 else if (XFASTINT (WGET (w, window_end_vpos)) == this_line_vpos
13273 && this_line_vpos > 0)
13274 WSET (w, window_end_vpos, make_number (this_line_vpos - 1));
13275 WSET (w, window_end_valid, Qnil);
13276
13277 /* Update hint: No need to try to scroll in update_window. */
13278 w->desired_matrix->no_scrolling_p = 1;
13279
13280 #ifdef GLYPH_DEBUG
13281 *w->desired_matrix->method = 0;
13282 debug_method_add (w, "optimization 1");
13283 #endif
13284 #ifdef HAVE_WINDOW_SYSTEM
13285 update_window_fringes (w, 0);
13286 #endif
13287 goto update;
13288 }
13289 else
13290 goto cancel;
13291 }
13292 else if (/* Cursor position hasn't changed. */
13293 PT == w->last_point
13294 /* Make sure the cursor was last displayed
13295 in this window. Otherwise we have to reposition it. */
13296 && 0 <= w->cursor.vpos
13297 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13298 {
13299 if (!must_finish)
13300 {
13301 do_pending_window_change (1);
13302 /* If selected_window changed, redisplay again. */
13303 if (WINDOWP (selected_window)
13304 && (w = XWINDOW (selected_window)) != sw)
13305 goto retry;
13306
13307 /* We used to always goto end_of_redisplay here, but this
13308 isn't enough if we have a blinking cursor. */
13309 if (w->cursor_off_p == w->last_cursor_off_p)
13310 goto end_of_redisplay;
13311 }
13312 goto update;
13313 }
13314 /* If highlighting the region, or if the cursor is in the echo area,
13315 then we can't just move the cursor. */
13316 else if (! (!NILP (Vtransient_mark_mode)
13317 && !NILP (BVAR (current_buffer, mark_active)))
13318 && (EQ (selected_window,
13319 BVAR (current_buffer, last_selected_window))
13320 || highlight_nonselected_windows)
13321 && NILP (WGET (w, region_showing))
13322 && NILP (Vshow_trailing_whitespace)
13323 && !cursor_in_echo_area)
13324 {
13325 struct it it;
13326 struct glyph_row *row;
13327
13328 /* Skip from tlbufpos to PT and see where it is. Note that
13329 PT may be in invisible text. If so, we will end at the
13330 next visible position. */
13331 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13332 NULL, DEFAULT_FACE_ID);
13333 it.current_x = this_line_start_x;
13334 it.current_y = this_line_y;
13335 it.vpos = this_line_vpos;
13336
13337 /* The call to move_it_to stops in front of PT, but
13338 moves over before-strings. */
13339 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13340
13341 if (it.vpos == this_line_vpos
13342 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13343 row->enabled_p))
13344 {
13345 eassert (this_line_vpos == it.vpos);
13346 eassert (this_line_y == it.current_y);
13347 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13348 #ifdef GLYPH_DEBUG
13349 *w->desired_matrix->method = 0;
13350 debug_method_add (w, "optimization 3");
13351 #endif
13352 goto update;
13353 }
13354 else
13355 goto cancel;
13356 }
13357
13358 cancel:
13359 /* Text changed drastically or point moved off of line. */
13360 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13361 }
13362
13363 CHARPOS (this_line_start_pos) = 0;
13364 consider_all_windows_p |= buffer_shared > 1;
13365 ++clear_face_cache_count;
13366 #ifdef HAVE_WINDOW_SYSTEM
13367 ++clear_image_cache_count;
13368 #endif
13369
13370 /* Build desired matrices, and update the display. If
13371 consider_all_windows_p is non-zero, do it for all windows on all
13372 frames. Otherwise do it for selected_window, only. */
13373
13374 if (consider_all_windows_p)
13375 {
13376 Lisp_Object tail, frame;
13377
13378 FOR_EACH_FRAME (tail, frame)
13379 XFRAME (frame)->updated_p = 0;
13380
13381 /* Recompute # windows showing selected buffer. This will be
13382 incremented each time such a window is displayed. */
13383 buffer_shared = 0;
13384
13385 FOR_EACH_FRAME (tail, frame)
13386 {
13387 struct frame *f = XFRAME (frame);
13388
13389 /* We don't have to do anything for unselected terminal
13390 frames. */
13391 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13392 && !EQ (FRAME_TTY (f)->top_frame, frame))
13393 continue;
13394
13395 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13396 {
13397 if (! EQ (frame, selected_frame))
13398 /* Select the frame, for the sake of frame-local
13399 variables. */
13400 select_frame_for_redisplay (frame);
13401
13402 /* Mark all the scroll bars to be removed; we'll redeem
13403 the ones we want when we redisplay their windows. */
13404 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13405 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13406
13407 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13408 redisplay_windows (FRAME_ROOT_WINDOW (f));
13409
13410 /* The X error handler may have deleted that frame. */
13411 if (!FRAME_LIVE_P (f))
13412 continue;
13413
13414 /* Any scroll bars which redisplay_windows should have
13415 nuked should now go away. */
13416 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13417 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13418
13419 /* If fonts changed, display again. */
13420 /* ??? rms: I suspect it is a mistake to jump all the way
13421 back to retry here. It should just retry this frame. */
13422 if (fonts_changed_p)
13423 goto retry;
13424
13425 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13426 {
13427 /* See if we have to hscroll. */
13428 if (!f->already_hscrolled_p)
13429 {
13430 f->already_hscrolled_p = 1;
13431 if (hscroll_windows (f->root_window))
13432 goto retry;
13433 }
13434
13435 /* Prevent various kinds of signals during display
13436 update. stdio is not robust about handling
13437 signals, which can cause an apparent I/O
13438 error. */
13439 if (interrupt_input)
13440 unrequest_sigio ();
13441 STOP_POLLING;
13442
13443 /* Update the display. */
13444 set_window_update_flags (XWINDOW (f->root_window), 1);
13445 pending |= update_frame (f, 0, 0);
13446 f->updated_p = 1;
13447 }
13448 }
13449 }
13450
13451 if (!EQ (old_frame, selected_frame)
13452 && FRAME_LIVE_P (XFRAME (old_frame)))
13453 /* We played a bit fast-and-loose above and allowed selected_frame
13454 and selected_window to be temporarily out-of-sync but let's make
13455 sure this stays contained. */
13456 select_frame_for_redisplay (old_frame);
13457 eassert (EQ (XFRAME (selected_frame)->selected_window,
13458 selected_window));
13459
13460 if (!pending)
13461 {
13462 /* Do the mark_window_display_accurate after all windows have
13463 been redisplayed because this call resets flags in buffers
13464 which are needed for proper redisplay. */
13465 FOR_EACH_FRAME (tail, frame)
13466 {
13467 struct frame *f = XFRAME (frame);
13468 if (f->updated_p)
13469 {
13470 mark_window_display_accurate (f->root_window, 1);
13471 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13472 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13473 }
13474 }
13475 }
13476 }
13477 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13478 {
13479 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13480 struct frame *mini_frame;
13481
13482 displayed_buffer = XBUFFER (WGET (XWINDOW (selected_window), buffer));
13483 /* Use list_of_error, not Qerror, so that
13484 we catch only errors and don't run the debugger. */
13485 internal_condition_case_1 (redisplay_window_1, selected_window,
13486 list_of_error,
13487 redisplay_window_error);
13488 if (update_miniwindow_p)
13489 internal_condition_case_1 (redisplay_window_1, mini_window,
13490 list_of_error,
13491 redisplay_window_error);
13492
13493 /* Compare desired and current matrices, perform output. */
13494
13495 update:
13496 /* If fonts changed, display again. */
13497 if (fonts_changed_p)
13498 goto retry;
13499
13500 /* Prevent various kinds of signals during display update.
13501 stdio is not robust about handling signals,
13502 which can cause an apparent I/O error. */
13503 if (interrupt_input)
13504 unrequest_sigio ();
13505 STOP_POLLING;
13506
13507 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13508 {
13509 if (hscroll_windows (selected_window))
13510 goto retry;
13511
13512 XWINDOW (selected_window)->must_be_updated_p = 1;
13513 pending = update_frame (sf, 0, 0);
13514 }
13515
13516 /* We may have called echo_area_display at the top of this
13517 function. If the echo area is on another frame, that may
13518 have put text on a frame other than the selected one, so the
13519 above call to update_frame would not have caught it. Catch
13520 it here. */
13521 mini_window = FRAME_MINIBUF_WINDOW (sf);
13522 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13523
13524 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13525 {
13526 XWINDOW (mini_window)->must_be_updated_p = 1;
13527 pending |= update_frame (mini_frame, 0, 0);
13528 if (!pending && hscroll_windows (mini_window))
13529 goto retry;
13530 }
13531 }
13532
13533 /* If display was paused because of pending input, make sure we do a
13534 thorough update the next time. */
13535 if (pending)
13536 {
13537 /* Prevent the optimization at the beginning of
13538 redisplay_internal that tries a single-line update of the
13539 line containing the cursor in the selected window. */
13540 CHARPOS (this_line_start_pos) = 0;
13541
13542 /* Let the overlay arrow be updated the next time. */
13543 update_overlay_arrows (0);
13544
13545 /* If we pause after scrolling, some rows in the current
13546 matrices of some windows are not valid. */
13547 if (!WINDOW_FULL_WIDTH_P (w)
13548 && !FRAME_WINDOW_P (XFRAME (WGET (w, frame))))
13549 update_mode_lines = 1;
13550 }
13551 else
13552 {
13553 if (!consider_all_windows_p)
13554 {
13555 /* This has already been done above if
13556 consider_all_windows_p is set. */
13557 mark_window_display_accurate_1 (w, 1);
13558
13559 /* Say overlay arrows are up to date. */
13560 update_overlay_arrows (1);
13561
13562 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13563 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13564 }
13565
13566 update_mode_lines = 0;
13567 windows_or_buffers_changed = 0;
13568 cursor_type_changed = 0;
13569 }
13570
13571 /* Start SIGIO interrupts coming again. Having them off during the
13572 code above makes it less likely one will discard output, but not
13573 impossible, since there might be stuff in the system buffer here.
13574 But it is much hairier to try to do anything about that. */
13575 if (interrupt_input)
13576 request_sigio ();
13577 RESUME_POLLING;
13578
13579 /* If a frame has become visible which was not before, redisplay
13580 again, so that we display it. Expose events for such a frame
13581 (which it gets when becoming visible) don't call the parts of
13582 redisplay constructing glyphs, so simply exposing a frame won't
13583 display anything in this case. So, we have to display these
13584 frames here explicitly. */
13585 if (!pending)
13586 {
13587 Lisp_Object tail, frame;
13588 int new_count = 0;
13589
13590 FOR_EACH_FRAME (tail, frame)
13591 {
13592 int this_is_visible = 0;
13593
13594 if (XFRAME (frame)->visible)
13595 this_is_visible = 1;
13596 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13597 if (XFRAME (frame)->visible)
13598 this_is_visible = 1;
13599
13600 if (this_is_visible)
13601 new_count++;
13602 }
13603
13604 if (new_count != number_of_visible_frames)
13605 windows_or_buffers_changed++;
13606 }
13607
13608 /* Change frame size now if a change is pending. */
13609 do_pending_window_change (1);
13610
13611 /* If we just did a pending size change, or have additional
13612 visible frames, or selected_window changed, redisplay again. */
13613 if ((windows_or_buffers_changed && !pending)
13614 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13615 goto retry;
13616
13617 /* Clear the face and image caches.
13618
13619 We used to do this only if consider_all_windows_p. But the cache
13620 needs to be cleared if a timer creates images in the current
13621 buffer (e.g. the test case in Bug#6230). */
13622
13623 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13624 {
13625 clear_face_cache (0);
13626 clear_face_cache_count = 0;
13627 }
13628
13629 #ifdef HAVE_WINDOW_SYSTEM
13630 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13631 {
13632 clear_image_caches (Qnil);
13633 clear_image_cache_count = 0;
13634 }
13635 #endif /* HAVE_WINDOW_SYSTEM */
13636
13637 end_of_redisplay:
13638 unbind_to (count, Qnil);
13639 RESUME_POLLING;
13640 }
13641
13642
13643 /* Redisplay, but leave alone any recent echo area message unless
13644 another message has been requested in its place.
13645
13646 This is useful in situations where you need to redisplay but no
13647 user action has occurred, making it inappropriate for the message
13648 area to be cleared. See tracking_off and
13649 wait_reading_process_output for examples of these situations.
13650
13651 FROM_WHERE is an integer saying from where this function was
13652 called. This is useful for debugging. */
13653
13654 void
13655 redisplay_preserve_echo_area (int from_where)
13656 {
13657 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13658
13659 if (!NILP (echo_area_buffer[1]))
13660 {
13661 /* We have a previously displayed message, but no current
13662 message. Redisplay the previous message. */
13663 display_last_displayed_message_p = 1;
13664 redisplay_internal ();
13665 display_last_displayed_message_p = 0;
13666 }
13667 else
13668 redisplay_internal ();
13669
13670 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13671 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13672 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13673 }
13674
13675
13676 /* Function registered with record_unwind_protect in
13677 redisplay_internal. Reset redisplaying_p to the value it had
13678 before redisplay_internal was called, and clear
13679 prevent_freeing_realized_faces_p. It also selects the previously
13680 selected frame, unless it has been deleted (by an X connection
13681 failure during redisplay, for example). */
13682
13683 static Lisp_Object
13684 unwind_redisplay (Lisp_Object val)
13685 {
13686 Lisp_Object old_redisplaying_p, old_frame;
13687
13688 old_redisplaying_p = XCAR (val);
13689 redisplaying_p = XFASTINT (old_redisplaying_p);
13690 old_frame = XCDR (val);
13691 if (! EQ (old_frame, selected_frame)
13692 && FRAME_LIVE_P (XFRAME (old_frame)))
13693 select_frame_for_redisplay (old_frame);
13694 return Qnil;
13695 }
13696
13697
13698 /* Mark the display of window W as accurate or inaccurate. If
13699 ACCURATE_P is non-zero mark display of W as accurate. If
13700 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13701 redisplay_internal is called. */
13702
13703 static void
13704 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13705 {
13706 if (BUFFERP (WGET (w, buffer)))
13707 {
13708 struct buffer *b = XBUFFER (WGET (w, buffer));
13709
13710 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13711 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13712 w->last_had_star
13713 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13714
13715 if (accurate_p)
13716 {
13717 b->clip_changed = 0;
13718 b->prevent_redisplay_optimizations_p = 0;
13719
13720 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13721 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13722 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13723 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13724
13725 w->current_matrix->buffer = b;
13726 w->current_matrix->begv = BUF_BEGV (b);
13727 w->current_matrix->zv = BUF_ZV (b);
13728
13729 w->last_cursor = w->cursor;
13730 w->last_cursor_off_p = w->cursor_off_p;
13731
13732 if (w == XWINDOW (selected_window))
13733 w->last_point = BUF_PT (b);
13734 else
13735 w->last_point = XMARKER (WGET (w, pointm))->charpos;
13736 }
13737 }
13738
13739 if (accurate_p)
13740 {
13741 WSET (w, window_end_valid, WGET (w, buffer));
13742 w->update_mode_line = 0;
13743 }
13744 }
13745
13746
13747 /* Mark the display of windows in the window tree rooted at WINDOW as
13748 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13749 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13750 be redisplayed the next time redisplay_internal is called. */
13751
13752 void
13753 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13754 {
13755 struct window *w;
13756
13757 for (; !NILP (window); window = WGET (w, next))
13758 {
13759 w = XWINDOW (window);
13760 mark_window_display_accurate_1 (w, accurate_p);
13761
13762 if (!NILP (WGET (w, vchild)))
13763 mark_window_display_accurate (WGET (w, vchild), accurate_p);
13764 if (!NILP (WGET (w, hchild)))
13765 mark_window_display_accurate (WGET (w, hchild), accurate_p);
13766 }
13767
13768 if (accurate_p)
13769 {
13770 update_overlay_arrows (1);
13771 }
13772 else
13773 {
13774 /* Force a thorough redisplay the next time by setting
13775 last_arrow_position and last_arrow_string to t, which is
13776 unequal to any useful value of Voverlay_arrow_... */
13777 update_overlay_arrows (-1);
13778 }
13779 }
13780
13781
13782 /* Return value in display table DP (Lisp_Char_Table *) for character
13783 C. Since a display table doesn't have any parent, we don't have to
13784 follow parent. Do not call this function directly but use the
13785 macro DISP_CHAR_VECTOR. */
13786
13787 Lisp_Object
13788 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13789 {
13790 Lisp_Object val;
13791
13792 if (ASCII_CHAR_P (c))
13793 {
13794 val = dp->ascii;
13795 if (SUB_CHAR_TABLE_P (val))
13796 val = XSUB_CHAR_TABLE (val)->contents[c];
13797 }
13798 else
13799 {
13800 Lisp_Object table;
13801
13802 XSETCHAR_TABLE (table, dp);
13803 val = char_table_ref (table, c);
13804 }
13805 if (NILP (val))
13806 val = dp->defalt;
13807 return val;
13808 }
13809
13810
13811 \f
13812 /***********************************************************************
13813 Window Redisplay
13814 ***********************************************************************/
13815
13816 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13817
13818 static void
13819 redisplay_windows (Lisp_Object window)
13820 {
13821 while (!NILP (window))
13822 {
13823 struct window *w = XWINDOW (window);
13824
13825 if (!NILP (WGET (w, hchild)))
13826 redisplay_windows (WGET (w, hchild));
13827 else if (!NILP (WGET (w, vchild)))
13828 redisplay_windows (WGET (w, vchild));
13829 else if (!NILP (WGET (w, buffer)))
13830 {
13831 displayed_buffer = XBUFFER (WGET (w, buffer));
13832 /* Use list_of_error, not Qerror, so that
13833 we catch only errors and don't run the debugger. */
13834 internal_condition_case_1 (redisplay_window_0, window,
13835 list_of_error,
13836 redisplay_window_error);
13837 }
13838
13839 window = WGET (w, next);
13840 }
13841 }
13842
13843 static Lisp_Object
13844 redisplay_window_error (Lisp_Object ignore)
13845 {
13846 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13847 return Qnil;
13848 }
13849
13850 static Lisp_Object
13851 redisplay_window_0 (Lisp_Object window)
13852 {
13853 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13854 redisplay_window (window, 0);
13855 return Qnil;
13856 }
13857
13858 static Lisp_Object
13859 redisplay_window_1 (Lisp_Object window)
13860 {
13861 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13862 redisplay_window (window, 1);
13863 return Qnil;
13864 }
13865 \f
13866
13867 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13868 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13869 which positions recorded in ROW differ from current buffer
13870 positions.
13871
13872 Return 0 if cursor is not on this row, 1 otherwise. */
13873
13874 static int
13875 set_cursor_from_row (struct window *w, struct glyph_row *row,
13876 struct glyph_matrix *matrix,
13877 ptrdiff_t delta, ptrdiff_t delta_bytes,
13878 int dy, int dvpos)
13879 {
13880 struct glyph *glyph = row->glyphs[TEXT_AREA];
13881 struct glyph *end = glyph + row->used[TEXT_AREA];
13882 struct glyph *cursor = NULL;
13883 /* The last known character position in row. */
13884 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13885 int x = row->x;
13886 ptrdiff_t pt_old = PT - delta;
13887 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13888 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13889 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13890 /* A glyph beyond the edge of TEXT_AREA which we should never
13891 touch. */
13892 struct glyph *glyphs_end = end;
13893 /* Non-zero means we've found a match for cursor position, but that
13894 glyph has the avoid_cursor_p flag set. */
13895 int match_with_avoid_cursor = 0;
13896 /* Non-zero means we've seen at least one glyph that came from a
13897 display string. */
13898 int string_seen = 0;
13899 /* Largest and smallest buffer positions seen so far during scan of
13900 glyph row. */
13901 ptrdiff_t bpos_max = pos_before;
13902 ptrdiff_t bpos_min = pos_after;
13903 /* Last buffer position covered by an overlay string with an integer
13904 `cursor' property. */
13905 ptrdiff_t bpos_covered = 0;
13906 /* Non-zero means the display string on which to display the cursor
13907 comes from a text property, not from an overlay. */
13908 int string_from_text_prop = 0;
13909
13910 /* Don't even try doing anything if called for a mode-line or
13911 header-line row, since the rest of the code isn't prepared to
13912 deal with such calamities. */
13913 eassert (!row->mode_line_p);
13914 if (row->mode_line_p)
13915 return 0;
13916
13917 /* Skip over glyphs not having an object at the start and the end of
13918 the row. These are special glyphs like truncation marks on
13919 terminal frames. */
13920 if (row->displays_text_p)
13921 {
13922 if (!row->reversed_p)
13923 {
13924 while (glyph < end
13925 && INTEGERP (glyph->object)
13926 && glyph->charpos < 0)
13927 {
13928 x += glyph->pixel_width;
13929 ++glyph;
13930 }
13931 while (end > glyph
13932 && INTEGERP ((end - 1)->object)
13933 /* CHARPOS is zero for blanks and stretch glyphs
13934 inserted by extend_face_to_end_of_line. */
13935 && (end - 1)->charpos <= 0)
13936 --end;
13937 glyph_before = glyph - 1;
13938 glyph_after = end;
13939 }
13940 else
13941 {
13942 struct glyph *g;
13943
13944 /* If the glyph row is reversed, we need to process it from back
13945 to front, so swap the edge pointers. */
13946 glyphs_end = end = glyph - 1;
13947 glyph += row->used[TEXT_AREA] - 1;
13948
13949 while (glyph > end + 1
13950 && INTEGERP (glyph->object)
13951 && glyph->charpos < 0)
13952 {
13953 --glyph;
13954 x -= glyph->pixel_width;
13955 }
13956 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13957 --glyph;
13958 /* By default, in reversed rows we put the cursor on the
13959 rightmost (first in the reading order) glyph. */
13960 for (g = end + 1; g < glyph; g++)
13961 x += g->pixel_width;
13962 while (end < glyph
13963 && INTEGERP ((end + 1)->object)
13964 && (end + 1)->charpos <= 0)
13965 ++end;
13966 glyph_before = glyph + 1;
13967 glyph_after = end;
13968 }
13969 }
13970 else if (row->reversed_p)
13971 {
13972 /* In R2L rows that don't display text, put the cursor on the
13973 rightmost glyph. Case in point: an empty last line that is
13974 part of an R2L paragraph. */
13975 cursor = end - 1;
13976 /* Avoid placing the cursor on the last glyph of the row, where
13977 on terminal frames we hold the vertical border between
13978 adjacent windows. */
13979 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13980 && !WINDOW_RIGHTMOST_P (w)
13981 && cursor == row->glyphs[LAST_AREA] - 1)
13982 cursor--;
13983 x = -1; /* will be computed below, at label compute_x */
13984 }
13985
13986 /* Step 1: Try to find the glyph whose character position
13987 corresponds to point. If that's not possible, find 2 glyphs
13988 whose character positions are the closest to point, one before
13989 point, the other after it. */
13990 if (!row->reversed_p)
13991 while (/* not marched to end of glyph row */
13992 glyph < end
13993 /* glyph was not inserted by redisplay for internal purposes */
13994 && !INTEGERP (glyph->object))
13995 {
13996 if (BUFFERP (glyph->object))
13997 {
13998 ptrdiff_t dpos = glyph->charpos - pt_old;
13999
14000 if (glyph->charpos > bpos_max)
14001 bpos_max = glyph->charpos;
14002 if (glyph->charpos < bpos_min)
14003 bpos_min = glyph->charpos;
14004 if (!glyph->avoid_cursor_p)
14005 {
14006 /* If we hit point, we've found the glyph on which to
14007 display the cursor. */
14008 if (dpos == 0)
14009 {
14010 match_with_avoid_cursor = 0;
14011 break;
14012 }
14013 /* See if we've found a better approximation to
14014 POS_BEFORE or to POS_AFTER. */
14015 if (0 > dpos && dpos > pos_before - pt_old)
14016 {
14017 pos_before = glyph->charpos;
14018 glyph_before = glyph;
14019 }
14020 else if (0 < dpos && dpos < pos_after - pt_old)
14021 {
14022 pos_after = glyph->charpos;
14023 glyph_after = glyph;
14024 }
14025 }
14026 else if (dpos == 0)
14027 match_with_avoid_cursor = 1;
14028 }
14029 else if (STRINGP (glyph->object))
14030 {
14031 Lisp_Object chprop;
14032 ptrdiff_t glyph_pos = glyph->charpos;
14033
14034 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14035 glyph->object);
14036 if (!NILP (chprop))
14037 {
14038 /* If the string came from a `display' text property,
14039 look up the buffer position of that property and
14040 use that position to update bpos_max, as if we
14041 actually saw such a position in one of the row's
14042 glyphs. This helps with supporting integer values
14043 of `cursor' property on the display string in
14044 situations where most or all of the row's buffer
14045 text is completely covered by display properties,
14046 so that no glyph with valid buffer positions is
14047 ever seen in the row. */
14048 ptrdiff_t prop_pos =
14049 string_buffer_position_lim (glyph->object, pos_before,
14050 pos_after, 0);
14051
14052 if (prop_pos >= pos_before)
14053 bpos_max = prop_pos - 1;
14054 }
14055 if (INTEGERP (chprop))
14056 {
14057 bpos_covered = bpos_max + XINT (chprop);
14058 /* If the `cursor' property covers buffer positions up
14059 to and including point, we should display cursor on
14060 this glyph. Note that, if a `cursor' property on one
14061 of the string's characters has an integer value, we
14062 will break out of the loop below _before_ we get to
14063 the position match above. IOW, integer values of
14064 the `cursor' property override the "exact match for
14065 point" strategy of positioning the cursor. */
14066 /* Implementation note: bpos_max == pt_old when, e.g.,
14067 we are in an empty line, where bpos_max is set to
14068 MATRIX_ROW_START_CHARPOS, see above. */
14069 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14070 {
14071 cursor = glyph;
14072 break;
14073 }
14074 }
14075
14076 string_seen = 1;
14077 }
14078 x += glyph->pixel_width;
14079 ++glyph;
14080 }
14081 else if (glyph > end) /* row is reversed */
14082 while (!INTEGERP (glyph->object))
14083 {
14084 if (BUFFERP (glyph->object))
14085 {
14086 ptrdiff_t dpos = glyph->charpos - pt_old;
14087
14088 if (glyph->charpos > bpos_max)
14089 bpos_max = glyph->charpos;
14090 if (glyph->charpos < bpos_min)
14091 bpos_min = glyph->charpos;
14092 if (!glyph->avoid_cursor_p)
14093 {
14094 if (dpos == 0)
14095 {
14096 match_with_avoid_cursor = 0;
14097 break;
14098 }
14099 if (0 > dpos && dpos > pos_before - pt_old)
14100 {
14101 pos_before = glyph->charpos;
14102 glyph_before = glyph;
14103 }
14104 else if (0 < dpos && dpos < pos_after - pt_old)
14105 {
14106 pos_after = glyph->charpos;
14107 glyph_after = glyph;
14108 }
14109 }
14110 else if (dpos == 0)
14111 match_with_avoid_cursor = 1;
14112 }
14113 else if (STRINGP (glyph->object))
14114 {
14115 Lisp_Object chprop;
14116 ptrdiff_t glyph_pos = glyph->charpos;
14117
14118 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14119 glyph->object);
14120 if (!NILP (chprop))
14121 {
14122 ptrdiff_t prop_pos =
14123 string_buffer_position_lim (glyph->object, pos_before,
14124 pos_after, 0);
14125
14126 if (prop_pos >= pos_before)
14127 bpos_max = prop_pos - 1;
14128 }
14129 if (INTEGERP (chprop))
14130 {
14131 bpos_covered = bpos_max + XINT (chprop);
14132 /* If the `cursor' property covers buffer positions up
14133 to and including point, we should display cursor on
14134 this glyph. */
14135 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14136 {
14137 cursor = glyph;
14138 break;
14139 }
14140 }
14141 string_seen = 1;
14142 }
14143 --glyph;
14144 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14145 {
14146 x--; /* can't use any pixel_width */
14147 break;
14148 }
14149 x -= glyph->pixel_width;
14150 }
14151
14152 /* Step 2: If we didn't find an exact match for point, we need to
14153 look for a proper place to put the cursor among glyphs between
14154 GLYPH_BEFORE and GLYPH_AFTER. */
14155 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14156 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14157 && bpos_covered < pt_old)
14158 {
14159 /* An empty line has a single glyph whose OBJECT is zero and
14160 whose CHARPOS is the position of a newline on that line.
14161 Note that on a TTY, there are more glyphs after that, which
14162 were produced by extend_face_to_end_of_line, but their
14163 CHARPOS is zero or negative. */
14164 int empty_line_p =
14165 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14166 && INTEGERP (glyph->object) && glyph->charpos > 0;
14167
14168 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14169 {
14170 ptrdiff_t ellipsis_pos;
14171
14172 /* Scan back over the ellipsis glyphs. */
14173 if (!row->reversed_p)
14174 {
14175 ellipsis_pos = (glyph - 1)->charpos;
14176 while (glyph > row->glyphs[TEXT_AREA]
14177 && (glyph - 1)->charpos == ellipsis_pos)
14178 glyph--, x -= glyph->pixel_width;
14179 /* That loop always goes one position too far, including
14180 the glyph before the ellipsis. So scan forward over
14181 that one. */
14182 x += glyph->pixel_width;
14183 glyph++;
14184 }
14185 else /* row is reversed */
14186 {
14187 ellipsis_pos = (glyph + 1)->charpos;
14188 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14189 && (glyph + 1)->charpos == ellipsis_pos)
14190 glyph++, x += glyph->pixel_width;
14191 x -= glyph->pixel_width;
14192 glyph--;
14193 }
14194 }
14195 else if (match_with_avoid_cursor)
14196 {
14197 cursor = glyph_after;
14198 x = -1;
14199 }
14200 else if (string_seen)
14201 {
14202 int incr = row->reversed_p ? -1 : +1;
14203
14204 /* Need to find the glyph that came out of a string which is
14205 present at point. That glyph is somewhere between
14206 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14207 positioned between POS_BEFORE and POS_AFTER in the
14208 buffer. */
14209 struct glyph *start, *stop;
14210 ptrdiff_t pos = pos_before;
14211
14212 x = -1;
14213
14214 /* If the row ends in a newline from a display string,
14215 reordering could have moved the glyphs belonging to the
14216 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14217 in this case we extend the search to the last glyph in
14218 the row that was not inserted by redisplay. */
14219 if (row->ends_in_newline_from_string_p)
14220 {
14221 glyph_after = end;
14222 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14223 }
14224
14225 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14226 correspond to POS_BEFORE and POS_AFTER, respectively. We
14227 need START and STOP in the order that corresponds to the
14228 row's direction as given by its reversed_p flag. If the
14229 directionality of characters between POS_BEFORE and
14230 POS_AFTER is the opposite of the row's base direction,
14231 these characters will have been reordered for display,
14232 and we need to reverse START and STOP. */
14233 if (!row->reversed_p)
14234 {
14235 start = min (glyph_before, glyph_after);
14236 stop = max (glyph_before, glyph_after);
14237 }
14238 else
14239 {
14240 start = max (glyph_before, glyph_after);
14241 stop = min (glyph_before, glyph_after);
14242 }
14243 for (glyph = start + incr;
14244 row->reversed_p ? glyph > stop : glyph < stop; )
14245 {
14246
14247 /* Any glyphs that come from the buffer are here because
14248 of bidi reordering. Skip them, and only pay
14249 attention to glyphs that came from some string. */
14250 if (STRINGP (glyph->object))
14251 {
14252 Lisp_Object str;
14253 ptrdiff_t tem;
14254 /* If the display property covers the newline, we
14255 need to search for it one position farther. */
14256 ptrdiff_t lim = pos_after
14257 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14258
14259 string_from_text_prop = 0;
14260 str = glyph->object;
14261 tem = string_buffer_position_lim (str, pos, lim, 0);
14262 if (tem == 0 /* from overlay */
14263 || pos <= tem)
14264 {
14265 /* If the string from which this glyph came is
14266 found in the buffer at point, or at position
14267 that is closer to point than pos_after, then
14268 we've found the glyph we've been looking for.
14269 If it comes from an overlay (tem == 0), and
14270 it has the `cursor' property on one of its
14271 glyphs, record that glyph as a candidate for
14272 displaying the cursor. (As in the
14273 unidirectional version, we will display the
14274 cursor on the last candidate we find.) */
14275 if (tem == 0
14276 || tem == pt_old
14277 || (tem - pt_old > 0 && tem < pos_after))
14278 {
14279 /* The glyphs from this string could have
14280 been reordered. Find the one with the
14281 smallest string position. Or there could
14282 be a character in the string with the
14283 `cursor' property, which means display
14284 cursor on that character's glyph. */
14285 ptrdiff_t strpos = glyph->charpos;
14286
14287 if (tem)
14288 {
14289 cursor = glyph;
14290 string_from_text_prop = 1;
14291 }
14292 for ( ;
14293 (row->reversed_p ? glyph > stop : glyph < stop)
14294 && EQ (glyph->object, str);
14295 glyph += incr)
14296 {
14297 Lisp_Object cprop;
14298 ptrdiff_t gpos = glyph->charpos;
14299
14300 cprop = Fget_char_property (make_number (gpos),
14301 Qcursor,
14302 glyph->object);
14303 if (!NILP (cprop))
14304 {
14305 cursor = glyph;
14306 break;
14307 }
14308 if (tem && glyph->charpos < strpos)
14309 {
14310 strpos = glyph->charpos;
14311 cursor = glyph;
14312 }
14313 }
14314
14315 if (tem == pt_old
14316 || (tem - pt_old > 0 && tem < pos_after))
14317 goto compute_x;
14318 }
14319 if (tem)
14320 pos = tem + 1; /* don't find previous instances */
14321 }
14322 /* This string is not what we want; skip all of the
14323 glyphs that came from it. */
14324 while ((row->reversed_p ? glyph > stop : glyph < stop)
14325 && EQ (glyph->object, str))
14326 glyph += incr;
14327 }
14328 else
14329 glyph += incr;
14330 }
14331
14332 /* If we reached the end of the line, and END was from a string,
14333 the cursor is not on this line. */
14334 if (cursor == NULL
14335 && (row->reversed_p ? glyph <= end : glyph >= end)
14336 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14337 && STRINGP (end->object)
14338 && row->continued_p)
14339 return 0;
14340 }
14341 /* A truncated row may not include PT among its character positions.
14342 Setting the cursor inside the scroll margin will trigger
14343 recalculation of hscroll in hscroll_window_tree. But if a
14344 display string covers point, defer to the string-handling
14345 code below to figure this out. */
14346 else if (row->truncated_on_left_p && pt_old < bpos_min)
14347 {
14348 cursor = glyph_before;
14349 x = -1;
14350 }
14351 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14352 /* Zero-width characters produce no glyphs. */
14353 || (!empty_line_p
14354 && (row->reversed_p
14355 ? glyph_after > glyphs_end
14356 : glyph_after < glyphs_end)))
14357 {
14358 cursor = glyph_after;
14359 x = -1;
14360 }
14361 }
14362
14363 compute_x:
14364 if (cursor != NULL)
14365 glyph = cursor;
14366 else if (glyph == glyphs_end
14367 && pos_before == pos_after
14368 && STRINGP ((row->reversed_p
14369 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14370 : row->glyphs[TEXT_AREA])->object))
14371 {
14372 /* If all the glyphs of this row came from strings, put the
14373 cursor on the first glyph of the row. This avoids having the
14374 cursor outside of the text area in this very rare and hard
14375 use case. */
14376 glyph =
14377 row->reversed_p
14378 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14379 : row->glyphs[TEXT_AREA];
14380 }
14381 if (x < 0)
14382 {
14383 struct glyph *g;
14384
14385 /* Need to compute x that corresponds to GLYPH. */
14386 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14387 {
14388 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14389 abort ();
14390 x += g->pixel_width;
14391 }
14392 }
14393
14394 /* ROW could be part of a continued line, which, under bidi
14395 reordering, might have other rows whose start and end charpos
14396 occlude point. Only set w->cursor if we found a better
14397 approximation to the cursor position than we have from previously
14398 examined candidate rows belonging to the same continued line. */
14399 if (/* we already have a candidate row */
14400 w->cursor.vpos >= 0
14401 /* that candidate is not the row we are processing */
14402 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14403 /* Make sure cursor.vpos specifies a row whose start and end
14404 charpos occlude point, and it is valid candidate for being a
14405 cursor-row. This is because some callers of this function
14406 leave cursor.vpos at the row where the cursor was displayed
14407 during the last redisplay cycle. */
14408 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14409 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14410 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14411 {
14412 struct glyph *g1 =
14413 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14414
14415 /* Don't consider glyphs that are outside TEXT_AREA. */
14416 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14417 return 0;
14418 /* Keep the candidate whose buffer position is the closest to
14419 point or has the `cursor' property. */
14420 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14421 w->cursor.hpos >= 0
14422 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14423 && ((BUFFERP (g1->object)
14424 && (g1->charpos == pt_old /* an exact match always wins */
14425 || (BUFFERP (glyph->object)
14426 && eabs (g1->charpos - pt_old)
14427 < eabs (glyph->charpos - pt_old))))
14428 /* previous candidate is a glyph from a string that has
14429 a non-nil `cursor' property */
14430 || (STRINGP (g1->object)
14431 && (!NILP (Fget_char_property (make_number (g1->charpos),
14432 Qcursor, g1->object))
14433 /* previous candidate is from the same display
14434 string as this one, and the display string
14435 came from a text property */
14436 || (EQ (g1->object, glyph->object)
14437 && string_from_text_prop)
14438 /* this candidate is from newline and its
14439 position is not an exact match */
14440 || (INTEGERP (glyph->object)
14441 && glyph->charpos != pt_old)))))
14442 return 0;
14443 /* If this candidate gives an exact match, use that. */
14444 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14445 /* If this candidate is a glyph created for the
14446 terminating newline of a line, and point is on that
14447 newline, it wins because it's an exact match. */
14448 || (!row->continued_p
14449 && INTEGERP (glyph->object)
14450 && glyph->charpos == 0
14451 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14452 /* Otherwise, keep the candidate that comes from a row
14453 spanning less buffer positions. This may win when one or
14454 both candidate positions are on glyphs that came from
14455 display strings, for which we cannot compare buffer
14456 positions. */
14457 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14458 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14459 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14460 return 0;
14461 }
14462 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14463 w->cursor.x = x;
14464 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14465 w->cursor.y = row->y + dy;
14466
14467 if (w == XWINDOW (selected_window))
14468 {
14469 if (!row->continued_p
14470 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14471 && row->x == 0)
14472 {
14473 this_line_buffer = XBUFFER (WGET (w, buffer));
14474
14475 CHARPOS (this_line_start_pos)
14476 = MATRIX_ROW_START_CHARPOS (row) + delta;
14477 BYTEPOS (this_line_start_pos)
14478 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14479
14480 CHARPOS (this_line_end_pos)
14481 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14482 BYTEPOS (this_line_end_pos)
14483 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14484
14485 this_line_y = w->cursor.y;
14486 this_line_pixel_height = row->height;
14487 this_line_vpos = w->cursor.vpos;
14488 this_line_start_x = row->x;
14489 }
14490 else
14491 CHARPOS (this_line_start_pos) = 0;
14492 }
14493
14494 return 1;
14495 }
14496
14497
14498 /* Run window scroll functions, if any, for WINDOW with new window
14499 start STARTP. Sets the window start of WINDOW to that position.
14500
14501 We assume that the window's buffer is really current. */
14502
14503 static inline struct text_pos
14504 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14505 {
14506 struct window *w = XWINDOW (window);
14507 SET_MARKER_FROM_TEXT_POS (WGET (w, start), startp);
14508
14509 if (current_buffer != XBUFFER (WGET (w, buffer)))
14510 abort ();
14511
14512 if (!NILP (Vwindow_scroll_functions))
14513 {
14514 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14515 make_number (CHARPOS (startp)));
14516 SET_TEXT_POS_FROM_MARKER (startp, WGET (w, start));
14517 /* In case the hook functions switch buffers. */
14518 if (current_buffer != XBUFFER (WGET (w, buffer)))
14519 set_buffer_internal_1 (XBUFFER (WGET (w, buffer)));
14520 }
14521
14522 return startp;
14523 }
14524
14525
14526 /* Make sure the line containing the cursor is fully visible.
14527 A value of 1 means there is nothing to be done.
14528 (Either the line is fully visible, or it cannot be made so,
14529 or we cannot tell.)
14530
14531 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14532 is higher than window.
14533
14534 A value of 0 means the caller should do scrolling
14535 as if point had gone off the screen. */
14536
14537 static int
14538 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14539 {
14540 struct glyph_matrix *matrix;
14541 struct glyph_row *row;
14542 int window_height;
14543
14544 if (!make_cursor_line_fully_visible_p)
14545 return 1;
14546
14547 /* It's not always possible to find the cursor, e.g, when a window
14548 is full of overlay strings. Don't do anything in that case. */
14549 if (w->cursor.vpos < 0)
14550 return 1;
14551
14552 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14553 row = MATRIX_ROW (matrix, w->cursor.vpos);
14554
14555 /* If the cursor row is not partially visible, there's nothing to do. */
14556 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14557 return 1;
14558
14559 /* If the row the cursor is in is taller than the window's height,
14560 it's not clear what to do, so do nothing. */
14561 window_height = window_box_height (w);
14562 if (row->height >= window_height)
14563 {
14564 if (!force_p || MINI_WINDOW_P (w)
14565 || w->vscroll || w->cursor.vpos == 0)
14566 return 1;
14567 }
14568 return 0;
14569 }
14570
14571
14572 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14573 non-zero means only WINDOW is redisplayed in redisplay_internal.
14574 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14575 in redisplay_window to bring a partially visible line into view in
14576 the case that only the cursor has moved.
14577
14578 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14579 last screen line's vertical height extends past the end of the screen.
14580
14581 Value is
14582
14583 1 if scrolling succeeded
14584
14585 0 if scrolling didn't find point.
14586
14587 -1 if new fonts have been loaded so that we must interrupt
14588 redisplay, adjust glyph matrices, and try again. */
14589
14590 enum
14591 {
14592 SCROLLING_SUCCESS,
14593 SCROLLING_FAILED,
14594 SCROLLING_NEED_LARGER_MATRICES
14595 };
14596
14597 /* If scroll-conservatively is more than this, never recenter.
14598
14599 If you change this, don't forget to update the doc string of
14600 `scroll-conservatively' and the Emacs manual. */
14601 #define SCROLL_LIMIT 100
14602
14603 static int
14604 try_scrolling (Lisp_Object window, int just_this_one_p,
14605 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14606 int temp_scroll_step, int last_line_misfit)
14607 {
14608 struct window *w = XWINDOW (window);
14609 struct frame *f = XFRAME (WGET (w, frame));
14610 struct text_pos pos, startp;
14611 struct it it;
14612 int this_scroll_margin, scroll_max, rc, height;
14613 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14614 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14615 Lisp_Object aggressive;
14616 /* We will never try scrolling more than this number of lines. */
14617 int scroll_limit = SCROLL_LIMIT;
14618
14619 #ifdef GLYPH_DEBUG
14620 debug_method_add (w, "try_scrolling");
14621 #endif
14622
14623 SET_TEXT_POS_FROM_MARKER (startp, WGET (w, start));
14624
14625 /* Compute scroll margin height in pixels. We scroll when point is
14626 within this distance from the top or bottom of the window. */
14627 if (scroll_margin > 0)
14628 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14629 * FRAME_LINE_HEIGHT (f);
14630 else
14631 this_scroll_margin = 0;
14632
14633 /* Force arg_scroll_conservatively to have a reasonable value, to
14634 avoid scrolling too far away with slow move_it_* functions. Note
14635 that the user can supply scroll-conservatively equal to
14636 `most-positive-fixnum', which can be larger than INT_MAX. */
14637 if (arg_scroll_conservatively > scroll_limit)
14638 {
14639 arg_scroll_conservatively = scroll_limit + 1;
14640 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14641 }
14642 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14643 /* Compute how much we should try to scroll maximally to bring
14644 point into view. */
14645 scroll_max = (max (scroll_step,
14646 max (arg_scroll_conservatively, temp_scroll_step))
14647 * FRAME_LINE_HEIGHT (f));
14648 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14649 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14650 /* We're trying to scroll because of aggressive scrolling but no
14651 scroll_step is set. Choose an arbitrary one. */
14652 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14653 else
14654 scroll_max = 0;
14655
14656 too_near_end:
14657
14658 /* Decide whether to scroll down. */
14659 if (PT > CHARPOS (startp))
14660 {
14661 int scroll_margin_y;
14662
14663 /* Compute the pixel ypos of the scroll margin, then move IT to
14664 either that ypos or PT, whichever comes first. */
14665 start_display (&it, w, startp);
14666 scroll_margin_y = it.last_visible_y - this_scroll_margin
14667 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14668 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14669 (MOVE_TO_POS | MOVE_TO_Y));
14670
14671 if (PT > CHARPOS (it.current.pos))
14672 {
14673 int y0 = line_bottom_y (&it);
14674 /* Compute how many pixels below window bottom to stop searching
14675 for PT. This avoids costly search for PT that is far away if
14676 the user limited scrolling by a small number of lines, but
14677 always finds PT if scroll_conservatively is set to a large
14678 number, such as most-positive-fixnum. */
14679 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14680 int y_to_move = it.last_visible_y + slack;
14681
14682 /* Compute the distance from the scroll margin to PT or to
14683 the scroll limit, whichever comes first. This should
14684 include the height of the cursor line, to make that line
14685 fully visible. */
14686 move_it_to (&it, PT, -1, y_to_move,
14687 -1, MOVE_TO_POS | MOVE_TO_Y);
14688 dy = line_bottom_y (&it) - y0;
14689
14690 if (dy > scroll_max)
14691 return SCROLLING_FAILED;
14692
14693 if (dy > 0)
14694 scroll_down_p = 1;
14695 }
14696 }
14697
14698 if (scroll_down_p)
14699 {
14700 /* Point is in or below the bottom scroll margin, so move the
14701 window start down. If scrolling conservatively, move it just
14702 enough down to make point visible. If scroll_step is set,
14703 move it down by scroll_step. */
14704 if (arg_scroll_conservatively)
14705 amount_to_scroll
14706 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14707 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14708 else if (scroll_step || temp_scroll_step)
14709 amount_to_scroll = scroll_max;
14710 else
14711 {
14712 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14713 height = WINDOW_BOX_TEXT_HEIGHT (w);
14714 if (NUMBERP (aggressive))
14715 {
14716 double float_amount = XFLOATINT (aggressive) * height;
14717 amount_to_scroll = float_amount;
14718 if (amount_to_scroll == 0 && float_amount > 0)
14719 amount_to_scroll = 1;
14720 /* Don't let point enter the scroll margin near top of
14721 the window. */
14722 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14723 amount_to_scroll = height - 2*this_scroll_margin + dy;
14724 }
14725 }
14726
14727 if (amount_to_scroll <= 0)
14728 return SCROLLING_FAILED;
14729
14730 start_display (&it, w, startp);
14731 if (arg_scroll_conservatively <= scroll_limit)
14732 move_it_vertically (&it, amount_to_scroll);
14733 else
14734 {
14735 /* Extra precision for users who set scroll-conservatively
14736 to a large number: make sure the amount we scroll
14737 the window start is never less than amount_to_scroll,
14738 which was computed as distance from window bottom to
14739 point. This matters when lines at window top and lines
14740 below window bottom have different height. */
14741 struct it it1;
14742 void *it1data = NULL;
14743 /* We use a temporary it1 because line_bottom_y can modify
14744 its argument, if it moves one line down; see there. */
14745 int start_y;
14746
14747 SAVE_IT (it1, it, it1data);
14748 start_y = line_bottom_y (&it1);
14749 do {
14750 RESTORE_IT (&it, &it, it1data);
14751 move_it_by_lines (&it, 1);
14752 SAVE_IT (it1, it, it1data);
14753 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14754 }
14755
14756 /* If STARTP is unchanged, move it down another screen line. */
14757 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14758 move_it_by_lines (&it, 1);
14759 startp = it.current.pos;
14760 }
14761 else
14762 {
14763 struct text_pos scroll_margin_pos = startp;
14764
14765 /* See if point is inside the scroll margin at the top of the
14766 window. */
14767 if (this_scroll_margin)
14768 {
14769 start_display (&it, w, startp);
14770 move_it_vertically (&it, this_scroll_margin);
14771 scroll_margin_pos = it.current.pos;
14772 }
14773
14774 if (PT < CHARPOS (scroll_margin_pos))
14775 {
14776 /* Point is in the scroll margin at the top of the window or
14777 above what is displayed in the window. */
14778 int y0, y_to_move;
14779
14780 /* Compute the vertical distance from PT to the scroll
14781 margin position. Move as far as scroll_max allows, or
14782 one screenful, or 10 screen lines, whichever is largest.
14783 Give up if distance is greater than scroll_max. */
14784 SET_TEXT_POS (pos, PT, PT_BYTE);
14785 start_display (&it, w, pos);
14786 y0 = it.current_y;
14787 y_to_move = max (it.last_visible_y,
14788 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14789 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14790 y_to_move, -1,
14791 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14792 dy = it.current_y - y0;
14793 if (dy > scroll_max)
14794 return SCROLLING_FAILED;
14795
14796 /* Compute new window start. */
14797 start_display (&it, w, startp);
14798
14799 if (arg_scroll_conservatively)
14800 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14801 max (scroll_step, temp_scroll_step));
14802 else if (scroll_step || temp_scroll_step)
14803 amount_to_scroll = scroll_max;
14804 else
14805 {
14806 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14807 height = WINDOW_BOX_TEXT_HEIGHT (w);
14808 if (NUMBERP (aggressive))
14809 {
14810 double float_amount = XFLOATINT (aggressive) * height;
14811 amount_to_scroll = float_amount;
14812 if (amount_to_scroll == 0 && float_amount > 0)
14813 amount_to_scroll = 1;
14814 amount_to_scroll -=
14815 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14816 /* Don't let point enter the scroll margin near
14817 bottom of the window. */
14818 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14819 amount_to_scroll = height - 2*this_scroll_margin + dy;
14820 }
14821 }
14822
14823 if (amount_to_scroll <= 0)
14824 return SCROLLING_FAILED;
14825
14826 move_it_vertically_backward (&it, amount_to_scroll);
14827 startp = it.current.pos;
14828 }
14829 }
14830
14831 /* Run window scroll functions. */
14832 startp = run_window_scroll_functions (window, startp);
14833
14834 /* Display the window. Give up if new fonts are loaded, or if point
14835 doesn't appear. */
14836 if (!try_window (window, startp, 0))
14837 rc = SCROLLING_NEED_LARGER_MATRICES;
14838 else if (w->cursor.vpos < 0)
14839 {
14840 clear_glyph_matrix (w->desired_matrix);
14841 rc = SCROLLING_FAILED;
14842 }
14843 else
14844 {
14845 /* Maybe forget recorded base line for line number display. */
14846 if (!just_this_one_p
14847 || current_buffer->clip_changed
14848 || BEG_UNCHANGED < CHARPOS (startp))
14849 WSET (w, base_line_number, Qnil);
14850
14851 /* If cursor ends up on a partially visible line,
14852 treat that as being off the bottom of the screen. */
14853 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14854 /* It's possible that the cursor is on the first line of the
14855 buffer, which is partially obscured due to a vscroll
14856 (Bug#7537). In that case, avoid looping forever . */
14857 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14858 {
14859 clear_glyph_matrix (w->desired_matrix);
14860 ++extra_scroll_margin_lines;
14861 goto too_near_end;
14862 }
14863 rc = SCROLLING_SUCCESS;
14864 }
14865
14866 return rc;
14867 }
14868
14869
14870 /* Compute a suitable window start for window W if display of W starts
14871 on a continuation line. Value is non-zero if a new window start
14872 was computed.
14873
14874 The new window start will be computed, based on W's width, starting
14875 from the start of the continued line. It is the start of the
14876 screen line with the minimum distance from the old start W->start. */
14877
14878 static int
14879 compute_window_start_on_continuation_line (struct window *w)
14880 {
14881 struct text_pos pos, start_pos;
14882 int window_start_changed_p = 0;
14883
14884 SET_TEXT_POS_FROM_MARKER (start_pos, WGET (w, start));
14885
14886 /* If window start is on a continuation line... Window start may be
14887 < BEGV in case there's invisible text at the start of the
14888 buffer (M-x rmail, for example). */
14889 if (CHARPOS (start_pos) > BEGV
14890 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14891 {
14892 struct it it;
14893 struct glyph_row *row;
14894
14895 /* Handle the case that the window start is out of range. */
14896 if (CHARPOS (start_pos) < BEGV)
14897 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14898 else if (CHARPOS (start_pos) > ZV)
14899 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14900
14901 /* Find the start of the continued line. This should be fast
14902 because scan_buffer is fast (newline cache). */
14903 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14904 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14905 row, DEFAULT_FACE_ID);
14906 reseat_at_previous_visible_line_start (&it);
14907
14908 /* If the line start is "too far" away from the window start,
14909 say it takes too much time to compute a new window start. */
14910 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14911 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14912 {
14913 int min_distance, distance;
14914
14915 /* Move forward by display lines to find the new window
14916 start. If window width was enlarged, the new start can
14917 be expected to be > the old start. If window width was
14918 decreased, the new window start will be < the old start.
14919 So, we're looking for the display line start with the
14920 minimum distance from the old window start. */
14921 pos = it.current.pos;
14922 min_distance = INFINITY;
14923 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14924 distance < min_distance)
14925 {
14926 min_distance = distance;
14927 pos = it.current.pos;
14928 move_it_by_lines (&it, 1);
14929 }
14930
14931 /* Set the window start there. */
14932 SET_MARKER_FROM_TEXT_POS (WGET (w, start), pos);
14933 window_start_changed_p = 1;
14934 }
14935 }
14936
14937 return window_start_changed_p;
14938 }
14939
14940
14941 /* Try cursor movement in case text has not changed in window WINDOW,
14942 with window start STARTP. Value is
14943
14944 CURSOR_MOVEMENT_SUCCESS if successful
14945
14946 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14947
14948 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14949 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14950 we want to scroll as if scroll-step were set to 1. See the code.
14951
14952 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14953 which case we have to abort this redisplay, and adjust matrices
14954 first. */
14955
14956 enum
14957 {
14958 CURSOR_MOVEMENT_SUCCESS,
14959 CURSOR_MOVEMENT_CANNOT_BE_USED,
14960 CURSOR_MOVEMENT_MUST_SCROLL,
14961 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14962 };
14963
14964 static int
14965 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14966 {
14967 struct window *w = XWINDOW (window);
14968 struct frame *f = XFRAME (WGET (w, frame));
14969 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14970
14971 #ifdef GLYPH_DEBUG
14972 if (inhibit_try_cursor_movement)
14973 return rc;
14974 #endif
14975
14976 /* Previously, there was a check for Lisp integer in the
14977 if-statement below. Now, this field is converted to
14978 ptrdiff_t, thus zero means invalid position in a buffer. */
14979 eassert (w->last_point > 0);
14980
14981 /* Handle case where text has not changed, only point, and it has
14982 not moved off the frame. */
14983 if (/* Point may be in this window. */
14984 PT >= CHARPOS (startp)
14985 /* Selective display hasn't changed. */
14986 && !current_buffer->clip_changed
14987 /* Function force-mode-line-update is used to force a thorough
14988 redisplay. It sets either windows_or_buffers_changed or
14989 update_mode_lines. So don't take a shortcut here for these
14990 cases. */
14991 && !update_mode_lines
14992 && !windows_or_buffers_changed
14993 && !cursor_type_changed
14994 /* Can't use this case if highlighting a region. When a
14995 region exists, cursor movement has to do more than just
14996 set the cursor. */
14997 && !(!NILP (Vtransient_mark_mode)
14998 && !NILP (BVAR (current_buffer, mark_active)))
14999 && NILP (WGET (w, region_showing))
15000 && NILP (Vshow_trailing_whitespace)
15001 /* This code is not used for mini-buffer for the sake of the case
15002 of redisplaying to replace an echo area message; since in
15003 that case the mini-buffer contents per se are usually
15004 unchanged. This code is of no real use in the mini-buffer
15005 since the handling of this_line_start_pos, etc., in redisplay
15006 handles the same cases. */
15007 && !EQ (window, minibuf_window)
15008 /* When splitting windows or for new windows, it happens that
15009 redisplay is called with a nil window_end_vpos or one being
15010 larger than the window. This should really be fixed in
15011 window.c. I don't have this on my list, now, so we do
15012 approximately the same as the old redisplay code. --gerd. */
15013 && INTEGERP (WGET (w, window_end_vpos))
15014 && XFASTINT (WGET (w, window_end_vpos)) < w->current_matrix->nrows
15015 && (FRAME_WINDOW_P (f)
15016 || !overlay_arrow_in_current_buffer_p ()))
15017 {
15018 int this_scroll_margin, top_scroll_margin;
15019 struct glyph_row *row = NULL;
15020
15021 #ifdef GLYPH_DEBUG
15022 debug_method_add (w, "cursor movement");
15023 #endif
15024
15025 /* Scroll if point within this distance from the top or bottom
15026 of the window. This is a pixel value. */
15027 if (scroll_margin > 0)
15028 {
15029 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15030 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15031 }
15032 else
15033 this_scroll_margin = 0;
15034
15035 top_scroll_margin = this_scroll_margin;
15036 if (WINDOW_WANTS_HEADER_LINE_P (w))
15037 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15038
15039 /* Start with the row the cursor was displayed during the last
15040 not paused redisplay. Give up if that row is not valid. */
15041 if (w->last_cursor.vpos < 0
15042 || w->last_cursor.vpos >= w->current_matrix->nrows)
15043 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15044 else
15045 {
15046 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15047 if (row->mode_line_p)
15048 ++row;
15049 if (!row->enabled_p)
15050 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15051 }
15052
15053 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15054 {
15055 int scroll_p = 0, must_scroll = 0;
15056 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15057
15058 if (PT > w->last_point)
15059 {
15060 /* Point has moved forward. */
15061 while (MATRIX_ROW_END_CHARPOS (row) < PT
15062 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15063 {
15064 eassert (row->enabled_p);
15065 ++row;
15066 }
15067
15068 /* If the end position of a row equals the start
15069 position of the next row, and PT is at that position,
15070 we would rather display cursor in the next line. */
15071 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15072 && MATRIX_ROW_END_CHARPOS (row) == PT
15073 && row < w->current_matrix->rows
15074 + w->current_matrix->nrows - 1
15075 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15076 && !cursor_row_p (row))
15077 ++row;
15078
15079 /* If within the scroll margin, scroll. Note that
15080 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15081 the next line would be drawn, and that
15082 this_scroll_margin can be zero. */
15083 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15084 || PT > MATRIX_ROW_END_CHARPOS (row)
15085 /* Line is completely visible last line in window
15086 and PT is to be set in the next line. */
15087 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15088 && PT == MATRIX_ROW_END_CHARPOS (row)
15089 && !row->ends_at_zv_p
15090 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15091 scroll_p = 1;
15092 }
15093 else if (PT < w->last_point)
15094 {
15095 /* Cursor has to be moved backward. Note that PT >=
15096 CHARPOS (startp) because of the outer if-statement. */
15097 while (!row->mode_line_p
15098 && (MATRIX_ROW_START_CHARPOS (row) > PT
15099 || (MATRIX_ROW_START_CHARPOS (row) == PT
15100 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15101 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15102 row > w->current_matrix->rows
15103 && (row-1)->ends_in_newline_from_string_p))))
15104 && (row->y > top_scroll_margin
15105 || CHARPOS (startp) == BEGV))
15106 {
15107 eassert (row->enabled_p);
15108 --row;
15109 }
15110
15111 /* Consider the following case: Window starts at BEGV,
15112 there is invisible, intangible text at BEGV, so that
15113 display starts at some point START > BEGV. It can
15114 happen that we are called with PT somewhere between
15115 BEGV and START. Try to handle that case. */
15116 if (row < w->current_matrix->rows
15117 || row->mode_line_p)
15118 {
15119 row = w->current_matrix->rows;
15120 if (row->mode_line_p)
15121 ++row;
15122 }
15123
15124 /* Due to newlines in overlay strings, we may have to
15125 skip forward over overlay strings. */
15126 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15127 && MATRIX_ROW_END_CHARPOS (row) == PT
15128 && !cursor_row_p (row))
15129 ++row;
15130
15131 /* If within the scroll margin, scroll. */
15132 if (row->y < top_scroll_margin
15133 && CHARPOS (startp) != BEGV)
15134 scroll_p = 1;
15135 }
15136 else
15137 {
15138 /* Cursor did not move. So don't scroll even if cursor line
15139 is partially visible, as it was so before. */
15140 rc = CURSOR_MOVEMENT_SUCCESS;
15141 }
15142
15143 if (PT < MATRIX_ROW_START_CHARPOS (row)
15144 || PT > MATRIX_ROW_END_CHARPOS (row))
15145 {
15146 /* if PT is not in the glyph row, give up. */
15147 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15148 must_scroll = 1;
15149 }
15150 else if (rc != CURSOR_MOVEMENT_SUCCESS
15151 && !NILP (BVAR (XBUFFER (WGET (w, buffer)), bidi_display_reordering)))
15152 {
15153 struct glyph_row *row1;
15154
15155 /* If rows are bidi-reordered and point moved, back up
15156 until we find a row that does not belong to a
15157 continuation line. This is because we must consider
15158 all rows of a continued line as candidates for the
15159 new cursor positioning, since row start and end
15160 positions change non-linearly with vertical position
15161 in such rows. */
15162 /* FIXME: Revisit this when glyph ``spilling'' in
15163 continuation lines' rows is implemented for
15164 bidi-reordered rows. */
15165 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15166 MATRIX_ROW_CONTINUATION_LINE_P (row);
15167 --row)
15168 {
15169 /* If we hit the beginning of the displayed portion
15170 without finding the first row of a continued
15171 line, give up. */
15172 if (row <= row1)
15173 {
15174 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15175 break;
15176 }
15177 eassert (row->enabled_p);
15178 }
15179 }
15180 if (must_scroll)
15181 ;
15182 else if (rc != CURSOR_MOVEMENT_SUCCESS
15183 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15184 /* Make sure this isn't a header line by any chance, since
15185 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15186 && !row->mode_line_p
15187 && make_cursor_line_fully_visible_p)
15188 {
15189 if (PT == MATRIX_ROW_END_CHARPOS (row)
15190 && !row->ends_at_zv_p
15191 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15192 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15193 else if (row->height > window_box_height (w))
15194 {
15195 /* If we end up in a partially visible line, let's
15196 make it fully visible, except when it's taller
15197 than the window, in which case we can't do much
15198 about it. */
15199 *scroll_step = 1;
15200 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15201 }
15202 else
15203 {
15204 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15205 if (!cursor_row_fully_visible_p (w, 0, 1))
15206 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15207 else
15208 rc = CURSOR_MOVEMENT_SUCCESS;
15209 }
15210 }
15211 else if (scroll_p)
15212 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15213 else if (rc != CURSOR_MOVEMENT_SUCCESS
15214 && !NILP (BVAR (XBUFFER (WGET (w, buffer)), bidi_display_reordering)))
15215 {
15216 /* With bidi-reordered rows, there could be more than
15217 one candidate row whose start and end positions
15218 occlude point. We need to let set_cursor_from_row
15219 find the best candidate. */
15220 /* FIXME: Revisit this when glyph ``spilling'' in
15221 continuation lines' rows is implemented for
15222 bidi-reordered rows. */
15223 int rv = 0;
15224
15225 do
15226 {
15227 int at_zv_p = 0, exact_match_p = 0;
15228
15229 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15230 && PT <= MATRIX_ROW_END_CHARPOS (row)
15231 && cursor_row_p (row))
15232 rv |= set_cursor_from_row (w, row, w->current_matrix,
15233 0, 0, 0, 0);
15234 /* As soon as we've found the exact match for point,
15235 or the first suitable row whose ends_at_zv_p flag
15236 is set, we are done. */
15237 at_zv_p =
15238 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15239 if (rv && !at_zv_p
15240 && w->cursor.hpos >= 0
15241 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15242 w->cursor.vpos))
15243 {
15244 struct glyph_row *candidate =
15245 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15246 struct glyph *g =
15247 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15248 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15249
15250 exact_match_p =
15251 (BUFFERP (g->object) && g->charpos == PT)
15252 || (INTEGERP (g->object)
15253 && (g->charpos == PT
15254 || (g->charpos == 0 && endpos - 1 == PT)));
15255 }
15256 if (rv && (at_zv_p || exact_match_p))
15257 {
15258 rc = CURSOR_MOVEMENT_SUCCESS;
15259 break;
15260 }
15261 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15262 break;
15263 ++row;
15264 }
15265 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15266 || row->continued_p)
15267 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15268 || (MATRIX_ROW_START_CHARPOS (row) == PT
15269 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15270 /* If we didn't find any candidate rows, or exited the
15271 loop before all the candidates were examined, signal
15272 to the caller that this method failed. */
15273 if (rc != CURSOR_MOVEMENT_SUCCESS
15274 && !(rv
15275 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15276 && !row->continued_p))
15277 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15278 else if (rv)
15279 rc = CURSOR_MOVEMENT_SUCCESS;
15280 }
15281 else
15282 {
15283 do
15284 {
15285 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15286 {
15287 rc = CURSOR_MOVEMENT_SUCCESS;
15288 break;
15289 }
15290 ++row;
15291 }
15292 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15293 && MATRIX_ROW_START_CHARPOS (row) == PT
15294 && cursor_row_p (row));
15295 }
15296 }
15297 }
15298
15299 return rc;
15300 }
15301
15302 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15303 static
15304 #endif
15305 void
15306 set_vertical_scroll_bar (struct window *w)
15307 {
15308 ptrdiff_t start, end, whole;
15309
15310 /* Calculate the start and end positions for the current window.
15311 At some point, it would be nice to choose between scrollbars
15312 which reflect the whole buffer size, with special markers
15313 indicating narrowing, and scrollbars which reflect only the
15314 visible region.
15315
15316 Note that mini-buffers sometimes aren't displaying any text. */
15317 if (!MINI_WINDOW_P (w)
15318 || (w == XWINDOW (minibuf_window)
15319 && NILP (echo_area_buffer[0])))
15320 {
15321 struct buffer *buf = XBUFFER (WGET (w, buffer));
15322 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15323 start = marker_position (WGET (w, start)) - BUF_BEGV (buf);
15324 /* I don't think this is guaranteed to be right. For the
15325 moment, we'll pretend it is. */
15326 end = BUF_Z (buf) - XFASTINT (WGET (w, window_end_pos)) - BUF_BEGV (buf);
15327
15328 if (end < start)
15329 end = start;
15330 if (whole < (end - start))
15331 whole = end - start;
15332 }
15333 else
15334 start = end = whole = 0;
15335
15336 /* Indicate what this scroll bar ought to be displaying now. */
15337 if (FRAME_TERMINAL (XFRAME (WGET (w, frame)))->set_vertical_scroll_bar_hook)
15338 (*FRAME_TERMINAL (XFRAME (WGET (w, frame)))->set_vertical_scroll_bar_hook)
15339 (w, end - start, whole, start);
15340 }
15341
15342
15343 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15344 selected_window is redisplayed.
15345
15346 We can return without actually redisplaying the window if
15347 fonts_changed_p is nonzero. In that case, redisplay_internal will
15348 retry. */
15349
15350 static void
15351 redisplay_window (Lisp_Object window, int just_this_one_p)
15352 {
15353 struct window *w = XWINDOW (window);
15354 struct frame *f = XFRAME (WGET (w, frame));
15355 struct buffer *buffer = XBUFFER (WGET (w, buffer));
15356 struct buffer *old = current_buffer;
15357 struct text_pos lpoint, opoint, startp;
15358 int update_mode_line;
15359 int tem;
15360 struct it it;
15361 /* Record it now because it's overwritten. */
15362 int current_matrix_up_to_date_p = 0;
15363 int used_current_matrix_p = 0;
15364 /* This is less strict than current_matrix_up_to_date_p.
15365 It indicates that the buffer contents and narrowing are unchanged. */
15366 int buffer_unchanged_p = 0;
15367 int temp_scroll_step = 0;
15368 ptrdiff_t count = SPECPDL_INDEX ();
15369 int rc;
15370 int centering_position = -1;
15371 int last_line_misfit = 0;
15372 ptrdiff_t beg_unchanged, end_unchanged;
15373
15374 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15375 opoint = lpoint;
15376
15377 /* W must be a leaf window here. */
15378 eassert (!NILP (WGET (w, buffer)));
15379 #ifdef GLYPH_DEBUG
15380 *w->desired_matrix->method = 0;
15381 #endif
15382
15383 restart:
15384 reconsider_clip_changes (w, buffer);
15385
15386 /* Has the mode line to be updated? */
15387 update_mode_line = (w->update_mode_line
15388 || update_mode_lines
15389 || buffer->clip_changed
15390 || buffer->prevent_redisplay_optimizations_p);
15391
15392 if (MINI_WINDOW_P (w))
15393 {
15394 if (w == XWINDOW (echo_area_window)
15395 && !NILP (echo_area_buffer[0]))
15396 {
15397 if (update_mode_line)
15398 /* We may have to update a tty frame's menu bar or a
15399 tool-bar. Example `M-x C-h C-h C-g'. */
15400 goto finish_menu_bars;
15401 else
15402 /* We've already displayed the echo area glyphs in this window. */
15403 goto finish_scroll_bars;
15404 }
15405 else if ((w != XWINDOW (minibuf_window)
15406 || minibuf_level == 0)
15407 /* When buffer is nonempty, redisplay window normally. */
15408 && BUF_Z (XBUFFER (WGET (w, buffer))) == BUF_BEG (XBUFFER (WGET (w, buffer)))
15409 /* Quail displays non-mini buffers in minibuffer window.
15410 In that case, redisplay the window normally. */
15411 && !NILP (Fmemq (WGET (w, buffer), Vminibuffer_list)))
15412 {
15413 /* W is a mini-buffer window, but it's not active, so clear
15414 it. */
15415 int yb = window_text_bottom_y (w);
15416 struct glyph_row *row;
15417 int y;
15418
15419 for (y = 0, row = w->desired_matrix->rows;
15420 y < yb;
15421 y += row->height, ++row)
15422 blank_row (w, row, y);
15423 goto finish_scroll_bars;
15424 }
15425
15426 clear_glyph_matrix (w->desired_matrix);
15427 }
15428
15429 /* Otherwise set up data on this window; select its buffer and point
15430 value. */
15431 /* Really select the buffer, for the sake of buffer-local
15432 variables. */
15433 set_buffer_internal_1 (XBUFFER (WGET (w, buffer)));
15434
15435 current_matrix_up_to_date_p
15436 = (!NILP (WGET (w, window_end_valid))
15437 && !current_buffer->clip_changed
15438 && !current_buffer->prevent_redisplay_optimizations_p
15439 && w->last_modified >= MODIFF
15440 && w->last_overlay_modified >= OVERLAY_MODIFF);
15441
15442 /* Run the window-bottom-change-functions
15443 if it is possible that the text on the screen has changed
15444 (either due to modification of the text, or any other reason). */
15445 if (!current_matrix_up_to_date_p
15446 && !NILP (Vwindow_text_change_functions))
15447 {
15448 safe_run_hooks (Qwindow_text_change_functions);
15449 goto restart;
15450 }
15451
15452 beg_unchanged = BEG_UNCHANGED;
15453 end_unchanged = END_UNCHANGED;
15454
15455 SET_TEXT_POS (opoint, PT, PT_BYTE);
15456
15457 specbind (Qinhibit_point_motion_hooks, Qt);
15458
15459 buffer_unchanged_p
15460 = (!NILP (WGET (w, window_end_valid))
15461 && !current_buffer->clip_changed
15462 && w->last_modified >= MODIFF
15463 && w->last_overlay_modified >= OVERLAY_MODIFF);
15464
15465 /* When windows_or_buffers_changed is non-zero, we can't rely on
15466 the window end being valid, so set it to nil there. */
15467 if (windows_or_buffers_changed)
15468 {
15469 /* If window starts on a continuation line, maybe adjust the
15470 window start in case the window's width changed. */
15471 if (XMARKER (WGET (w, start))->buffer == current_buffer)
15472 compute_window_start_on_continuation_line (w);
15473
15474 WSET (w, window_end_valid, Qnil);
15475 }
15476
15477 /* Some sanity checks. */
15478 CHECK_WINDOW_END (w);
15479 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15480 abort ();
15481 if (BYTEPOS (opoint) < CHARPOS (opoint))
15482 abort ();
15483
15484 /* If %c is in mode line, update it if needed. */
15485 if (!NILP (WGET (w, column_number_displayed))
15486 /* This alternative quickly identifies a common case
15487 where no change is needed. */
15488 && !(PT == w->last_point
15489 && w->last_modified >= MODIFF
15490 && w->last_overlay_modified >= OVERLAY_MODIFF)
15491 && (XFASTINT (WGET (w, column_number_displayed)) != current_column ()))
15492 update_mode_line = 1;
15493
15494 /* Count number of windows showing the selected buffer. An indirect
15495 buffer counts as its base buffer. */
15496 if (!just_this_one_p)
15497 {
15498 struct buffer *current_base, *window_base;
15499 current_base = current_buffer;
15500 window_base = XBUFFER (WGET (XWINDOW (selected_window), buffer));
15501 if (current_base->base_buffer)
15502 current_base = current_base->base_buffer;
15503 if (window_base->base_buffer)
15504 window_base = window_base->base_buffer;
15505 if (current_base == window_base)
15506 buffer_shared++;
15507 }
15508
15509 /* Point refers normally to the selected window. For any other
15510 window, set up appropriate value. */
15511 if (!EQ (window, selected_window))
15512 {
15513 ptrdiff_t new_pt = XMARKER (WGET (w, pointm))->charpos;
15514 ptrdiff_t new_pt_byte = marker_byte_position (WGET (w, pointm));
15515 if (new_pt < BEGV)
15516 {
15517 new_pt = BEGV;
15518 new_pt_byte = BEGV_BYTE;
15519 set_marker_both (WGET (w, pointm), Qnil, BEGV, BEGV_BYTE);
15520 }
15521 else if (new_pt > (ZV - 1))
15522 {
15523 new_pt = ZV;
15524 new_pt_byte = ZV_BYTE;
15525 set_marker_both (WGET (w, pointm), Qnil, ZV, ZV_BYTE);
15526 }
15527
15528 /* We don't use SET_PT so that the point-motion hooks don't run. */
15529 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15530 }
15531
15532 /* If any of the character widths specified in the display table
15533 have changed, invalidate the width run cache. It's true that
15534 this may be a bit late to catch such changes, but the rest of
15535 redisplay goes (non-fatally) haywire when the display table is
15536 changed, so why should we worry about doing any better? */
15537 if (current_buffer->width_run_cache)
15538 {
15539 struct Lisp_Char_Table *disptab = buffer_display_table ();
15540
15541 if (! disptab_matches_widthtab
15542 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15543 {
15544 invalidate_region_cache (current_buffer,
15545 current_buffer->width_run_cache,
15546 BEG, Z);
15547 recompute_width_table (current_buffer, disptab);
15548 }
15549 }
15550
15551 /* If window-start is screwed up, choose a new one. */
15552 if (XMARKER (WGET (w, start))->buffer != current_buffer)
15553 goto recenter;
15554
15555 SET_TEXT_POS_FROM_MARKER (startp, WGET (w, start));
15556
15557 /* If someone specified a new starting point but did not insist,
15558 check whether it can be used. */
15559 if (w->optional_new_start
15560 && CHARPOS (startp) >= BEGV
15561 && CHARPOS (startp) <= ZV)
15562 {
15563 w->optional_new_start = 0;
15564 start_display (&it, w, startp);
15565 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15566 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15567 if (IT_CHARPOS (it) == PT)
15568 w->force_start = 1;
15569 /* IT may overshoot PT if text at PT is invisible. */
15570 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15571 w->force_start = 1;
15572 }
15573
15574 force_start:
15575
15576 /* Handle case where place to start displaying has been specified,
15577 unless the specified location is outside the accessible range. */
15578 if (w->force_start || w->frozen_window_start_p)
15579 {
15580 /* We set this later on if we have to adjust point. */
15581 int new_vpos = -1;
15582
15583 w->force_start = 0;
15584 w->vscroll = 0;
15585 WSET (w, window_end_valid, Qnil);
15586
15587 /* Forget any recorded base line for line number display. */
15588 if (!buffer_unchanged_p)
15589 WSET (w, base_line_number, Qnil);
15590
15591 /* Redisplay the mode line. Select the buffer properly for that.
15592 Also, run the hook window-scroll-functions
15593 because we have scrolled. */
15594 /* Note, we do this after clearing force_start because
15595 if there's an error, it is better to forget about force_start
15596 than to get into an infinite loop calling the hook functions
15597 and having them get more errors. */
15598 if (!update_mode_line
15599 || ! NILP (Vwindow_scroll_functions))
15600 {
15601 update_mode_line = 1;
15602 w->update_mode_line = 1;
15603 startp = run_window_scroll_functions (window, startp);
15604 }
15605
15606 w->last_modified = 0;
15607 w->last_overlay_modified = 0;
15608 if (CHARPOS (startp) < BEGV)
15609 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15610 else if (CHARPOS (startp) > ZV)
15611 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15612
15613 /* Redisplay, then check if cursor has been set during the
15614 redisplay. Give up if new fonts were loaded. */
15615 /* We used to issue a CHECK_MARGINS argument to try_window here,
15616 but this causes scrolling to fail when point begins inside
15617 the scroll margin (bug#148) -- cyd */
15618 if (!try_window (window, startp, 0))
15619 {
15620 w->force_start = 1;
15621 clear_glyph_matrix (w->desired_matrix);
15622 goto need_larger_matrices;
15623 }
15624
15625 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15626 {
15627 /* If point does not appear, try to move point so it does
15628 appear. The desired matrix has been built above, so we
15629 can use it here. */
15630 new_vpos = window_box_height (w) / 2;
15631 }
15632
15633 if (!cursor_row_fully_visible_p (w, 0, 0))
15634 {
15635 /* Point does appear, but on a line partly visible at end of window.
15636 Move it back to a fully-visible line. */
15637 new_vpos = window_box_height (w);
15638 }
15639
15640 /* If we need to move point for either of the above reasons,
15641 now actually do it. */
15642 if (new_vpos >= 0)
15643 {
15644 struct glyph_row *row;
15645
15646 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15647 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15648 ++row;
15649
15650 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15651 MATRIX_ROW_START_BYTEPOS (row));
15652
15653 if (w != XWINDOW (selected_window))
15654 set_marker_both (WGET (w, pointm), Qnil, PT, PT_BYTE);
15655 else if (current_buffer == old)
15656 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15657
15658 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15659
15660 /* If we are highlighting the region, then we just changed
15661 the region, so redisplay to show it. */
15662 if (!NILP (Vtransient_mark_mode)
15663 && !NILP (BVAR (current_buffer, mark_active)))
15664 {
15665 clear_glyph_matrix (w->desired_matrix);
15666 if (!try_window (window, startp, 0))
15667 goto need_larger_matrices;
15668 }
15669 }
15670
15671 #ifdef GLYPH_DEBUG
15672 debug_method_add (w, "forced window start");
15673 #endif
15674 goto done;
15675 }
15676
15677 /* Handle case where text has not changed, only point, and it has
15678 not moved off the frame, and we are not retrying after hscroll.
15679 (current_matrix_up_to_date_p is nonzero when retrying.) */
15680 if (current_matrix_up_to_date_p
15681 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15682 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15683 {
15684 switch (rc)
15685 {
15686 case CURSOR_MOVEMENT_SUCCESS:
15687 used_current_matrix_p = 1;
15688 goto done;
15689
15690 case CURSOR_MOVEMENT_MUST_SCROLL:
15691 goto try_to_scroll;
15692
15693 default:
15694 abort ();
15695 }
15696 }
15697 /* If current starting point was originally the beginning of a line
15698 but no longer is, find a new starting point. */
15699 else if (w->start_at_line_beg
15700 && !(CHARPOS (startp) <= BEGV
15701 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15702 {
15703 #ifdef GLYPH_DEBUG
15704 debug_method_add (w, "recenter 1");
15705 #endif
15706 goto recenter;
15707 }
15708
15709 /* Try scrolling with try_window_id. Value is > 0 if update has
15710 been done, it is -1 if we know that the same window start will
15711 not work. It is 0 if unsuccessful for some other reason. */
15712 else if ((tem = try_window_id (w)) != 0)
15713 {
15714 #ifdef GLYPH_DEBUG
15715 debug_method_add (w, "try_window_id %d", tem);
15716 #endif
15717
15718 if (fonts_changed_p)
15719 goto need_larger_matrices;
15720 if (tem > 0)
15721 goto done;
15722
15723 /* Otherwise try_window_id has returned -1 which means that we
15724 don't want the alternative below this comment to execute. */
15725 }
15726 else if (CHARPOS (startp) >= BEGV
15727 && CHARPOS (startp) <= ZV
15728 && PT >= CHARPOS (startp)
15729 && (CHARPOS (startp) < ZV
15730 /* Avoid starting at end of buffer. */
15731 || CHARPOS (startp) == BEGV
15732 || (w->last_modified >= MODIFF
15733 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15734 {
15735 int d1, d2, d3, d4, d5, d6;
15736
15737 /* If first window line is a continuation line, and window start
15738 is inside the modified region, but the first change is before
15739 current window start, we must select a new window start.
15740
15741 However, if this is the result of a down-mouse event (e.g. by
15742 extending the mouse-drag-overlay), we don't want to select a
15743 new window start, since that would change the position under
15744 the mouse, resulting in an unwanted mouse-movement rather
15745 than a simple mouse-click. */
15746 if (!w->start_at_line_beg
15747 && NILP (do_mouse_tracking)
15748 && CHARPOS (startp) > BEGV
15749 && CHARPOS (startp) > BEG + beg_unchanged
15750 && CHARPOS (startp) <= Z - end_unchanged
15751 /* Even if w->start_at_line_beg is nil, a new window may
15752 start at a line_beg, since that's how set_buffer_window
15753 sets it. So, we need to check the return value of
15754 compute_window_start_on_continuation_line. (See also
15755 bug#197). */
15756 && XMARKER (WGET (w, start))->buffer == current_buffer
15757 && compute_window_start_on_continuation_line (w)
15758 /* It doesn't make sense to force the window start like we
15759 do at label force_start if it is already known that point
15760 will not be visible in the resulting window, because
15761 doing so will move point from its correct position
15762 instead of scrolling the window to bring point into view.
15763 See bug#9324. */
15764 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15765 {
15766 w->force_start = 1;
15767 SET_TEXT_POS_FROM_MARKER (startp, WGET (w, start));
15768 goto force_start;
15769 }
15770
15771 #ifdef GLYPH_DEBUG
15772 debug_method_add (w, "same window start");
15773 #endif
15774
15775 /* Try to redisplay starting at same place as before.
15776 If point has not moved off frame, accept the results. */
15777 if (!current_matrix_up_to_date_p
15778 /* Don't use try_window_reusing_current_matrix in this case
15779 because a window scroll function can have changed the
15780 buffer. */
15781 || !NILP (Vwindow_scroll_functions)
15782 || MINI_WINDOW_P (w)
15783 || !(used_current_matrix_p
15784 = try_window_reusing_current_matrix (w)))
15785 {
15786 IF_DEBUG (debug_method_add (w, "1"));
15787 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15788 /* -1 means we need to scroll.
15789 0 means we need new matrices, but fonts_changed_p
15790 is set in that case, so we will detect it below. */
15791 goto try_to_scroll;
15792 }
15793
15794 if (fonts_changed_p)
15795 goto need_larger_matrices;
15796
15797 if (w->cursor.vpos >= 0)
15798 {
15799 if (!just_this_one_p
15800 || current_buffer->clip_changed
15801 || BEG_UNCHANGED < CHARPOS (startp))
15802 /* Forget any recorded base line for line number display. */
15803 WSET (w, base_line_number, Qnil);
15804
15805 if (!cursor_row_fully_visible_p (w, 1, 0))
15806 {
15807 clear_glyph_matrix (w->desired_matrix);
15808 last_line_misfit = 1;
15809 }
15810 /* Drop through and scroll. */
15811 else
15812 goto done;
15813 }
15814 else
15815 clear_glyph_matrix (w->desired_matrix);
15816 }
15817
15818 try_to_scroll:
15819
15820 w->last_modified = 0;
15821 w->last_overlay_modified = 0;
15822
15823 /* Redisplay the mode line. Select the buffer properly for that. */
15824 if (!update_mode_line)
15825 {
15826 update_mode_line = 1;
15827 w->update_mode_line = 1;
15828 }
15829
15830 /* Try to scroll by specified few lines. */
15831 if ((scroll_conservatively
15832 || emacs_scroll_step
15833 || temp_scroll_step
15834 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15835 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15836 && CHARPOS (startp) >= BEGV
15837 && CHARPOS (startp) <= ZV)
15838 {
15839 /* The function returns -1 if new fonts were loaded, 1 if
15840 successful, 0 if not successful. */
15841 int ss = try_scrolling (window, just_this_one_p,
15842 scroll_conservatively,
15843 emacs_scroll_step,
15844 temp_scroll_step, last_line_misfit);
15845 switch (ss)
15846 {
15847 case SCROLLING_SUCCESS:
15848 goto done;
15849
15850 case SCROLLING_NEED_LARGER_MATRICES:
15851 goto need_larger_matrices;
15852
15853 case SCROLLING_FAILED:
15854 break;
15855
15856 default:
15857 abort ();
15858 }
15859 }
15860
15861 /* Finally, just choose a place to start which positions point
15862 according to user preferences. */
15863
15864 recenter:
15865
15866 #ifdef GLYPH_DEBUG
15867 debug_method_add (w, "recenter");
15868 #endif
15869
15870 /* w->vscroll = 0; */
15871
15872 /* Forget any previously recorded base line for line number display. */
15873 if (!buffer_unchanged_p)
15874 WSET (w, base_line_number, Qnil);
15875
15876 /* Determine the window start relative to point. */
15877 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15878 it.current_y = it.last_visible_y;
15879 if (centering_position < 0)
15880 {
15881 int margin =
15882 scroll_margin > 0
15883 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15884 : 0;
15885 ptrdiff_t margin_pos = CHARPOS (startp);
15886 Lisp_Object aggressive;
15887 int scrolling_up;
15888
15889 /* If there is a scroll margin at the top of the window, find
15890 its character position. */
15891 if (margin
15892 /* Cannot call start_display if startp is not in the
15893 accessible region of the buffer. This can happen when we
15894 have just switched to a different buffer and/or changed
15895 its restriction. In that case, startp is initialized to
15896 the character position 1 (BEGV) because we did not yet
15897 have chance to display the buffer even once. */
15898 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15899 {
15900 struct it it1;
15901 void *it1data = NULL;
15902
15903 SAVE_IT (it1, it, it1data);
15904 start_display (&it1, w, startp);
15905 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15906 margin_pos = IT_CHARPOS (it1);
15907 RESTORE_IT (&it, &it, it1data);
15908 }
15909 scrolling_up = PT > margin_pos;
15910 aggressive =
15911 scrolling_up
15912 ? BVAR (current_buffer, scroll_up_aggressively)
15913 : BVAR (current_buffer, scroll_down_aggressively);
15914
15915 if (!MINI_WINDOW_P (w)
15916 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15917 {
15918 int pt_offset = 0;
15919
15920 /* Setting scroll-conservatively overrides
15921 scroll-*-aggressively. */
15922 if (!scroll_conservatively && NUMBERP (aggressive))
15923 {
15924 double float_amount = XFLOATINT (aggressive);
15925
15926 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15927 if (pt_offset == 0 && float_amount > 0)
15928 pt_offset = 1;
15929 if (pt_offset && margin > 0)
15930 margin -= 1;
15931 }
15932 /* Compute how much to move the window start backward from
15933 point so that point will be displayed where the user
15934 wants it. */
15935 if (scrolling_up)
15936 {
15937 centering_position = it.last_visible_y;
15938 if (pt_offset)
15939 centering_position -= pt_offset;
15940 centering_position -=
15941 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15942 + WINDOW_HEADER_LINE_HEIGHT (w);
15943 /* Don't let point enter the scroll margin near top of
15944 the window. */
15945 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15946 centering_position = margin * FRAME_LINE_HEIGHT (f);
15947 }
15948 else
15949 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15950 }
15951 else
15952 /* Set the window start half the height of the window backward
15953 from point. */
15954 centering_position = window_box_height (w) / 2;
15955 }
15956 move_it_vertically_backward (&it, centering_position);
15957
15958 eassert (IT_CHARPOS (it) >= BEGV);
15959
15960 /* The function move_it_vertically_backward may move over more
15961 than the specified y-distance. If it->w is small, e.g. a
15962 mini-buffer window, we may end up in front of the window's
15963 display area. Start displaying at the start of the line
15964 containing PT in this case. */
15965 if (it.current_y <= 0)
15966 {
15967 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15968 move_it_vertically_backward (&it, 0);
15969 it.current_y = 0;
15970 }
15971
15972 it.current_x = it.hpos = 0;
15973
15974 /* Set the window start position here explicitly, to avoid an
15975 infinite loop in case the functions in window-scroll-functions
15976 get errors. */
15977 set_marker_both (WGET (w, start), Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15978
15979 /* Run scroll hooks. */
15980 startp = run_window_scroll_functions (window, it.current.pos);
15981
15982 /* Redisplay the window. */
15983 if (!current_matrix_up_to_date_p
15984 || windows_or_buffers_changed
15985 || cursor_type_changed
15986 /* Don't use try_window_reusing_current_matrix in this case
15987 because it can have changed the buffer. */
15988 || !NILP (Vwindow_scroll_functions)
15989 || !just_this_one_p
15990 || MINI_WINDOW_P (w)
15991 || !(used_current_matrix_p
15992 = try_window_reusing_current_matrix (w)))
15993 try_window (window, startp, 0);
15994
15995 /* If new fonts have been loaded (due to fontsets), give up. We
15996 have to start a new redisplay since we need to re-adjust glyph
15997 matrices. */
15998 if (fonts_changed_p)
15999 goto need_larger_matrices;
16000
16001 /* If cursor did not appear assume that the middle of the window is
16002 in the first line of the window. Do it again with the next line.
16003 (Imagine a window of height 100, displaying two lines of height
16004 60. Moving back 50 from it->last_visible_y will end in the first
16005 line.) */
16006 if (w->cursor.vpos < 0)
16007 {
16008 if (!NILP (WGET (w, window_end_valid))
16009 && PT >= Z - XFASTINT (WGET (w, window_end_pos)))
16010 {
16011 clear_glyph_matrix (w->desired_matrix);
16012 move_it_by_lines (&it, 1);
16013 try_window (window, it.current.pos, 0);
16014 }
16015 else if (PT < IT_CHARPOS (it))
16016 {
16017 clear_glyph_matrix (w->desired_matrix);
16018 move_it_by_lines (&it, -1);
16019 try_window (window, it.current.pos, 0);
16020 }
16021 else
16022 {
16023 /* Not much we can do about it. */
16024 }
16025 }
16026
16027 /* Consider the following case: Window starts at BEGV, there is
16028 invisible, intangible text at BEGV, so that display starts at
16029 some point START > BEGV. It can happen that we are called with
16030 PT somewhere between BEGV and START. Try to handle that case. */
16031 if (w->cursor.vpos < 0)
16032 {
16033 struct glyph_row *row = w->current_matrix->rows;
16034 if (row->mode_line_p)
16035 ++row;
16036 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16037 }
16038
16039 if (!cursor_row_fully_visible_p (w, 0, 0))
16040 {
16041 /* If vscroll is enabled, disable it and try again. */
16042 if (w->vscroll)
16043 {
16044 w->vscroll = 0;
16045 clear_glyph_matrix (w->desired_matrix);
16046 goto recenter;
16047 }
16048
16049 /* Users who set scroll-conservatively to a large number want
16050 point just above/below the scroll margin. If we ended up
16051 with point's row partially visible, move the window start to
16052 make that row fully visible and out of the margin. */
16053 if (scroll_conservatively > SCROLL_LIMIT)
16054 {
16055 int margin =
16056 scroll_margin > 0
16057 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16058 : 0;
16059 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16060
16061 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16062 clear_glyph_matrix (w->desired_matrix);
16063 if (1 == try_window (window, it.current.pos,
16064 TRY_WINDOW_CHECK_MARGINS))
16065 goto done;
16066 }
16067
16068 /* If centering point failed to make the whole line visible,
16069 put point at the top instead. That has to make the whole line
16070 visible, if it can be done. */
16071 if (centering_position == 0)
16072 goto done;
16073
16074 clear_glyph_matrix (w->desired_matrix);
16075 centering_position = 0;
16076 goto recenter;
16077 }
16078
16079 done:
16080
16081 SET_TEXT_POS_FROM_MARKER (startp, WGET (w, start));
16082 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16083 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16084
16085 /* Display the mode line, if we must. */
16086 if ((update_mode_line
16087 /* If window not full width, must redo its mode line
16088 if (a) the window to its side is being redone and
16089 (b) we do a frame-based redisplay. This is a consequence
16090 of how inverted lines are drawn in frame-based redisplay. */
16091 || (!just_this_one_p
16092 && !FRAME_WINDOW_P (f)
16093 && !WINDOW_FULL_WIDTH_P (w))
16094 /* Line number to display. */
16095 || INTEGERP (WGET (w, base_line_pos))
16096 /* Column number is displayed and different from the one displayed. */
16097 || (!NILP (WGET (w, column_number_displayed))
16098 && (XFASTINT (WGET (w, column_number_displayed)) != current_column ())))
16099 /* This means that the window has a mode line. */
16100 && (WINDOW_WANTS_MODELINE_P (w)
16101 || WINDOW_WANTS_HEADER_LINE_P (w)))
16102 {
16103 display_mode_lines (w);
16104
16105 /* If mode line height has changed, arrange for a thorough
16106 immediate redisplay using the correct mode line height. */
16107 if (WINDOW_WANTS_MODELINE_P (w)
16108 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16109 {
16110 fonts_changed_p = 1;
16111 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16112 = DESIRED_MODE_LINE_HEIGHT (w);
16113 }
16114
16115 /* If header line height has changed, arrange for a thorough
16116 immediate redisplay using the correct header line height. */
16117 if (WINDOW_WANTS_HEADER_LINE_P (w)
16118 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16119 {
16120 fonts_changed_p = 1;
16121 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16122 = DESIRED_HEADER_LINE_HEIGHT (w);
16123 }
16124
16125 if (fonts_changed_p)
16126 goto need_larger_matrices;
16127 }
16128
16129 if (!line_number_displayed
16130 && !BUFFERP (WGET (w, base_line_pos)))
16131 {
16132 WSET (w, base_line_pos, Qnil);
16133 WSET (w, base_line_number, Qnil);
16134 }
16135
16136 finish_menu_bars:
16137
16138 /* When we reach a frame's selected window, redo the frame's menu bar. */
16139 if (update_mode_line
16140 && EQ (FRAME_SELECTED_WINDOW (f), window))
16141 {
16142 int redisplay_menu_p = 0;
16143
16144 if (FRAME_WINDOW_P (f))
16145 {
16146 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16147 || defined (HAVE_NS) || defined (USE_GTK)
16148 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16149 #else
16150 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16151 #endif
16152 }
16153 else
16154 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16155
16156 if (redisplay_menu_p)
16157 display_menu_bar (w);
16158
16159 #ifdef HAVE_WINDOW_SYSTEM
16160 if (FRAME_WINDOW_P (f))
16161 {
16162 #if defined (USE_GTK) || defined (HAVE_NS)
16163 if (FRAME_EXTERNAL_TOOL_BAR (f))
16164 redisplay_tool_bar (f);
16165 #else
16166 if (WINDOWP (f->tool_bar_window)
16167 && (FRAME_TOOL_BAR_LINES (f) > 0
16168 || !NILP (Vauto_resize_tool_bars))
16169 && redisplay_tool_bar (f))
16170 ignore_mouse_drag_p = 1;
16171 #endif
16172 }
16173 #endif
16174 }
16175
16176 #ifdef HAVE_WINDOW_SYSTEM
16177 if (FRAME_WINDOW_P (f)
16178 && update_window_fringes (w, (just_this_one_p
16179 || (!used_current_matrix_p && !overlay_arrow_seen)
16180 || w->pseudo_window_p)))
16181 {
16182 update_begin (f);
16183 BLOCK_INPUT;
16184 if (draw_window_fringes (w, 1))
16185 x_draw_vertical_border (w);
16186 UNBLOCK_INPUT;
16187 update_end (f);
16188 }
16189 #endif /* HAVE_WINDOW_SYSTEM */
16190
16191 /* We go to this label, with fonts_changed_p nonzero,
16192 if it is necessary to try again using larger glyph matrices.
16193 We have to redeem the scroll bar even in this case,
16194 because the loop in redisplay_internal expects that. */
16195 need_larger_matrices:
16196 ;
16197 finish_scroll_bars:
16198
16199 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16200 {
16201 /* Set the thumb's position and size. */
16202 set_vertical_scroll_bar (w);
16203
16204 /* Note that we actually used the scroll bar attached to this
16205 window, so it shouldn't be deleted at the end of redisplay. */
16206 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16207 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16208 }
16209
16210 /* Restore current_buffer and value of point in it. The window
16211 update may have changed the buffer, so first make sure `opoint'
16212 is still valid (Bug#6177). */
16213 if (CHARPOS (opoint) < BEGV)
16214 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16215 else if (CHARPOS (opoint) > ZV)
16216 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16217 else
16218 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16219
16220 set_buffer_internal_1 (old);
16221 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16222 shorter. This can be caused by log truncation in *Messages*. */
16223 if (CHARPOS (lpoint) <= ZV)
16224 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16225
16226 unbind_to (count, Qnil);
16227 }
16228
16229
16230 /* Build the complete desired matrix of WINDOW with a window start
16231 buffer position POS.
16232
16233 Value is 1 if successful. It is zero if fonts were loaded during
16234 redisplay which makes re-adjusting glyph matrices necessary, and -1
16235 if point would appear in the scroll margins.
16236 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16237 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16238 set in FLAGS.) */
16239
16240 int
16241 try_window (Lisp_Object window, struct text_pos pos, int flags)
16242 {
16243 struct window *w = XWINDOW (window);
16244 struct it it;
16245 struct glyph_row *last_text_row = NULL;
16246 struct frame *f = XFRAME (WGET (w, frame));
16247
16248 /* Make POS the new window start. */
16249 set_marker_both (WGET (w, start), Qnil, CHARPOS (pos), BYTEPOS (pos));
16250
16251 /* Mark cursor position as unknown. No overlay arrow seen. */
16252 w->cursor.vpos = -1;
16253 overlay_arrow_seen = 0;
16254
16255 /* Initialize iterator and info to start at POS. */
16256 start_display (&it, w, pos);
16257
16258 /* Display all lines of W. */
16259 while (it.current_y < it.last_visible_y)
16260 {
16261 if (display_line (&it))
16262 last_text_row = it.glyph_row - 1;
16263 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16264 return 0;
16265 }
16266
16267 /* Don't let the cursor end in the scroll margins. */
16268 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16269 && !MINI_WINDOW_P (w))
16270 {
16271 int this_scroll_margin;
16272
16273 if (scroll_margin > 0)
16274 {
16275 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16276 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16277 }
16278 else
16279 this_scroll_margin = 0;
16280
16281 if ((w->cursor.y >= 0 /* not vscrolled */
16282 && w->cursor.y < this_scroll_margin
16283 && CHARPOS (pos) > BEGV
16284 && IT_CHARPOS (it) < ZV)
16285 /* rms: considering make_cursor_line_fully_visible_p here
16286 seems to give wrong results. We don't want to recenter
16287 when the last line is partly visible, we want to allow
16288 that case to be handled in the usual way. */
16289 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16290 {
16291 w->cursor.vpos = -1;
16292 clear_glyph_matrix (w->desired_matrix);
16293 return -1;
16294 }
16295 }
16296
16297 /* If bottom moved off end of frame, change mode line percentage. */
16298 if (XFASTINT (WGET (w, window_end_pos)) <= 0
16299 && Z != IT_CHARPOS (it))
16300 w->update_mode_line = 1;
16301
16302 /* Set window_end_pos to the offset of the last character displayed
16303 on the window from the end of current_buffer. Set
16304 window_end_vpos to its row number. */
16305 if (last_text_row)
16306 {
16307 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16308 w->window_end_bytepos
16309 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16310 WSET (w, window_end_pos,
16311 make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16312 WSET (w, window_end_vpos,
16313 make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16314 eassert
16315 (MATRIX_ROW (w->desired_matrix,
16316 XFASTINT (WGET (w, window_end_vpos)))->displays_text_p);
16317 }
16318 else
16319 {
16320 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16321 WSET (w, window_end_pos, make_number (Z - ZV));
16322 WSET (w, window_end_vpos, make_number (0));
16323 }
16324
16325 /* But that is not valid info until redisplay finishes. */
16326 WSET (w, window_end_valid, Qnil);
16327 return 1;
16328 }
16329
16330
16331 \f
16332 /************************************************************************
16333 Window redisplay reusing current matrix when buffer has not changed
16334 ************************************************************************/
16335
16336 /* Try redisplay of window W showing an unchanged buffer with a
16337 different window start than the last time it was displayed by
16338 reusing its current matrix. Value is non-zero if successful.
16339 W->start is the new window start. */
16340
16341 static int
16342 try_window_reusing_current_matrix (struct window *w)
16343 {
16344 struct frame *f = XFRAME (WGET (w, frame));
16345 struct glyph_row *bottom_row;
16346 struct it it;
16347 struct run run;
16348 struct text_pos start, new_start;
16349 int nrows_scrolled, i;
16350 struct glyph_row *last_text_row;
16351 struct glyph_row *last_reused_text_row;
16352 struct glyph_row *start_row;
16353 int start_vpos, min_y, max_y;
16354
16355 #ifdef GLYPH_DEBUG
16356 if (inhibit_try_window_reusing)
16357 return 0;
16358 #endif
16359
16360 if (/* This function doesn't handle terminal frames. */
16361 !FRAME_WINDOW_P (f)
16362 /* Don't try to reuse the display if windows have been split
16363 or such. */
16364 || windows_or_buffers_changed
16365 || cursor_type_changed)
16366 return 0;
16367
16368 /* Can't do this if region may have changed. */
16369 if ((!NILP (Vtransient_mark_mode)
16370 && !NILP (BVAR (current_buffer, mark_active)))
16371 || !NILP (WGET (w, region_showing))
16372 || !NILP (Vshow_trailing_whitespace))
16373 return 0;
16374
16375 /* If top-line visibility has changed, give up. */
16376 if (WINDOW_WANTS_HEADER_LINE_P (w)
16377 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16378 return 0;
16379
16380 /* Give up if old or new display is scrolled vertically. We could
16381 make this function handle this, but right now it doesn't. */
16382 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16383 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16384 return 0;
16385
16386 /* The variable new_start now holds the new window start. The old
16387 start `start' can be determined from the current matrix. */
16388 SET_TEXT_POS_FROM_MARKER (new_start, WGET (w, start));
16389 start = start_row->minpos;
16390 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16391
16392 /* Clear the desired matrix for the display below. */
16393 clear_glyph_matrix (w->desired_matrix);
16394
16395 if (CHARPOS (new_start) <= CHARPOS (start))
16396 {
16397 /* Don't use this method if the display starts with an ellipsis
16398 displayed for invisible text. It's not easy to handle that case
16399 below, and it's certainly not worth the effort since this is
16400 not a frequent case. */
16401 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16402 return 0;
16403
16404 IF_DEBUG (debug_method_add (w, "twu1"));
16405
16406 /* Display up to a row that can be reused. The variable
16407 last_text_row is set to the last row displayed that displays
16408 text. Note that it.vpos == 0 if or if not there is a
16409 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16410 start_display (&it, w, new_start);
16411 w->cursor.vpos = -1;
16412 last_text_row = last_reused_text_row = NULL;
16413
16414 while (it.current_y < it.last_visible_y
16415 && !fonts_changed_p)
16416 {
16417 /* If we have reached into the characters in the START row,
16418 that means the line boundaries have changed. So we
16419 can't start copying with the row START. Maybe it will
16420 work to start copying with the following row. */
16421 while (IT_CHARPOS (it) > CHARPOS (start))
16422 {
16423 /* Advance to the next row as the "start". */
16424 start_row++;
16425 start = start_row->minpos;
16426 /* If there are no more rows to try, or just one, give up. */
16427 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16428 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16429 || CHARPOS (start) == ZV)
16430 {
16431 clear_glyph_matrix (w->desired_matrix);
16432 return 0;
16433 }
16434
16435 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16436 }
16437 /* If we have reached alignment, we can copy the rest of the
16438 rows. */
16439 if (IT_CHARPOS (it) == CHARPOS (start)
16440 /* Don't accept "alignment" inside a display vector,
16441 since start_row could have started in the middle of
16442 that same display vector (thus their character
16443 positions match), and we have no way of telling if
16444 that is the case. */
16445 && it.current.dpvec_index < 0)
16446 break;
16447
16448 if (display_line (&it))
16449 last_text_row = it.glyph_row - 1;
16450
16451 }
16452
16453 /* A value of current_y < last_visible_y means that we stopped
16454 at the previous window start, which in turn means that we
16455 have at least one reusable row. */
16456 if (it.current_y < it.last_visible_y)
16457 {
16458 struct glyph_row *row;
16459
16460 /* IT.vpos always starts from 0; it counts text lines. */
16461 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16462
16463 /* Find PT if not already found in the lines displayed. */
16464 if (w->cursor.vpos < 0)
16465 {
16466 int dy = it.current_y - start_row->y;
16467
16468 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16469 row = row_containing_pos (w, PT, row, NULL, dy);
16470 if (row)
16471 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16472 dy, nrows_scrolled);
16473 else
16474 {
16475 clear_glyph_matrix (w->desired_matrix);
16476 return 0;
16477 }
16478 }
16479
16480 /* Scroll the display. Do it before the current matrix is
16481 changed. The problem here is that update has not yet
16482 run, i.e. part of the current matrix is not up to date.
16483 scroll_run_hook will clear the cursor, and use the
16484 current matrix to get the height of the row the cursor is
16485 in. */
16486 run.current_y = start_row->y;
16487 run.desired_y = it.current_y;
16488 run.height = it.last_visible_y - it.current_y;
16489
16490 if (run.height > 0 && run.current_y != run.desired_y)
16491 {
16492 update_begin (f);
16493 FRAME_RIF (f)->update_window_begin_hook (w);
16494 FRAME_RIF (f)->clear_window_mouse_face (w);
16495 FRAME_RIF (f)->scroll_run_hook (w, &run);
16496 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16497 update_end (f);
16498 }
16499
16500 /* Shift current matrix down by nrows_scrolled lines. */
16501 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16502 rotate_matrix (w->current_matrix,
16503 start_vpos,
16504 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16505 nrows_scrolled);
16506
16507 /* Disable lines that must be updated. */
16508 for (i = 0; i < nrows_scrolled; ++i)
16509 (start_row + i)->enabled_p = 0;
16510
16511 /* Re-compute Y positions. */
16512 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16513 max_y = it.last_visible_y;
16514 for (row = start_row + nrows_scrolled;
16515 row < bottom_row;
16516 ++row)
16517 {
16518 row->y = it.current_y;
16519 row->visible_height = row->height;
16520
16521 if (row->y < min_y)
16522 row->visible_height -= min_y - row->y;
16523 if (row->y + row->height > max_y)
16524 row->visible_height -= row->y + row->height - max_y;
16525 if (row->fringe_bitmap_periodic_p)
16526 row->redraw_fringe_bitmaps_p = 1;
16527
16528 it.current_y += row->height;
16529
16530 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16531 last_reused_text_row = row;
16532 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16533 break;
16534 }
16535
16536 /* Disable lines in the current matrix which are now
16537 below the window. */
16538 for (++row; row < bottom_row; ++row)
16539 row->enabled_p = row->mode_line_p = 0;
16540 }
16541
16542 /* Update window_end_pos etc.; last_reused_text_row is the last
16543 reused row from the current matrix containing text, if any.
16544 The value of last_text_row is the last displayed line
16545 containing text. */
16546 if (last_reused_text_row)
16547 {
16548 w->window_end_bytepos
16549 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16550 WSET (w, window_end_pos,
16551 make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16552 WSET (w, window_end_vpos,
16553 make_number (MATRIX_ROW_VPOS (last_reused_text_row, w->current_matrix)));
16554 }
16555 else if (last_text_row)
16556 {
16557 w->window_end_bytepos
16558 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16559 WSET (w, window_end_pos,
16560 make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16561 WSET (w, window_end_vpos,
16562 make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16563 }
16564 else
16565 {
16566 /* This window must be completely empty. */
16567 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16568 WSET (w, window_end_pos, make_number (Z - ZV));
16569 WSET (w, window_end_vpos, make_number (0));
16570 }
16571 WSET (w, window_end_valid, Qnil);
16572
16573 /* Update hint: don't try scrolling again in update_window. */
16574 w->desired_matrix->no_scrolling_p = 1;
16575
16576 #ifdef GLYPH_DEBUG
16577 debug_method_add (w, "try_window_reusing_current_matrix 1");
16578 #endif
16579 return 1;
16580 }
16581 else if (CHARPOS (new_start) > CHARPOS (start))
16582 {
16583 struct glyph_row *pt_row, *row;
16584 struct glyph_row *first_reusable_row;
16585 struct glyph_row *first_row_to_display;
16586 int dy;
16587 int yb = window_text_bottom_y (w);
16588
16589 /* Find the row starting at new_start, if there is one. Don't
16590 reuse a partially visible line at the end. */
16591 first_reusable_row = start_row;
16592 while (first_reusable_row->enabled_p
16593 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16594 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16595 < CHARPOS (new_start)))
16596 ++first_reusable_row;
16597
16598 /* Give up if there is no row to reuse. */
16599 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16600 || !first_reusable_row->enabled_p
16601 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16602 != CHARPOS (new_start)))
16603 return 0;
16604
16605 /* We can reuse fully visible rows beginning with
16606 first_reusable_row to the end of the window. Set
16607 first_row_to_display to the first row that cannot be reused.
16608 Set pt_row to the row containing point, if there is any. */
16609 pt_row = NULL;
16610 for (first_row_to_display = first_reusable_row;
16611 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16612 ++first_row_to_display)
16613 {
16614 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16615 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16616 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16617 && first_row_to_display->ends_at_zv_p
16618 && pt_row == NULL)))
16619 pt_row = first_row_to_display;
16620 }
16621
16622 /* Start displaying at the start of first_row_to_display. */
16623 eassert (first_row_to_display->y < yb);
16624 init_to_row_start (&it, w, first_row_to_display);
16625
16626 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16627 - start_vpos);
16628 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16629 - nrows_scrolled);
16630 it.current_y = (first_row_to_display->y - first_reusable_row->y
16631 + WINDOW_HEADER_LINE_HEIGHT (w));
16632
16633 /* Display lines beginning with first_row_to_display in the
16634 desired matrix. Set last_text_row to the last row displayed
16635 that displays text. */
16636 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16637 if (pt_row == NULL)
16638 w->cursor.vpos = -1;
16639 last_text_row = NULL;
16640 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16641 if (display_line (&it))
16642 last_text_row = it.glyph_row - 1;
16643
16644 /* If point is in a reused row, adjust y and vpos of the cursor
16645 position. */
16646 if (pt_row)
16647 {
16648 w->cursor.vpos -= nrows_scrolled;
16649 w->cursor.y -= first_reusable_row->y - start_row->y;
16650 }
16651
16652 /* Give up if point isn't in a row displayed or reused. (This
16653 also handles the case where w->cursor.vpos < nrows_scrolled
16654 after the calls to display_line, which can happen with scroll
16655 margins. See bug#1295.) */
16656 if (w->cursor.vpos < 0)
16657 {
16658 clear_glyph_matrix (w->desired_matrix);
16659 return 0;
16660 }
16661
16662 /* Scroll the display. */
16663 run.current_y = first_reusable_row->y;
16664 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16665 run.height = it.last_visible_y - run.current_y;
16666 dy = run.current_y - run.desired_y;
16667
16668 if (run.height)
16669 {
16670 update_begin (f);
16671 FRAME_RIF (f)->update_window_begin_hook (w);
16672 FRAME_RIF (f)->clear_window_mouse_face (w);
16673 FRAME_RIF (f)->scroll_run_hook (w, &run);
16674 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16675 update_end (f);
16676 }
16677
16678 /* Adjust Y positions of reused rows. */
16679 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16680 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16681 max_y = it.last_visible_y;
16682 for (row = first_reusable_row; row < first_row_to_display; ++row)
16683 {
16684 row->y -= dy;
16685 row->visible_height = row->height;
16686 if (row->y < min_y)
16687 row->visible_height -= min_y - row->y;
16688 if (row->y + row->height > max_y)
16689 row->visible_height -= row->y + row->height - max_y;
16690 if (row->fringe_bitmap_periodic_p)
16691 row->redraw_fringe_bitmaps_p = 1;
16692 }
16693
16694 /* Scroll the current matrix. */
16695 eassert (nrows_scrolled > 0);
16696 rotate_matrix (w->current_matrix,
16697 start_vpos,
16698 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16699 -nrows_scrolled);
16700
16701 /* Disable rows not reused. */
16702 for (row -= nrows_scrolled; row < bottom_row; ++row)
16703 row->enabled_p = 0;
16704
16705 /* Point may have moved to a different line, so we cannot assume that
16706 the previous cursor position is valid; locate the correct row. */
16707 if (pt_row)
16708 {
16709 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16710 row < bottom_row
16711 && PT >= MATRIX_ROW_END_CHARPOS (row)
16712 && !row->ends_at_zv_p;
16713 row++)
16714 {
16715 w->cursor.vpos++;
16716 w->cursor.y = row->y;
16717 }
16718 if (row < bottom_row)
16719 {
16720 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16721 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16722
16723 /* Can't use this optimization with bidi-reordered glyph
16724 rows, unless cursor is already at point. */
16725 if (!NILP (BVAR (XBUFFER (WGET (w, buffer)), bidi_display_reordering)))
16726 {
16727 if (!(w->cursor.hpos >= 0
16728 && w->cursor.hpos < row->used[TEXT_AREA]
16729 && BUFFERP (glyph->object)
16730 && glyph->charpos == PT))
16731 return 0;
16732 }
16733 else
16734 for (; glyph < end
16735 && (!BUFFERP (glyph->object)
16736 || glyph->charpos < PT);
16737 glyph++)
16738 {
16739 w->cursor.hpos++;
16740 w->cursor.x += glyph->pixel_width;
16741 }
16742 }
16743 }
16744
16745 /* Adjust window end. A null value of last_text_row means that
16746 the window end is in reused rows which in turn means that
16747 only its vpos can have changed. */
16748 if (last_text_row)
16749 {
16750 w->window_end_bytepos
16751 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16752 WSET (w, window_end_pos,
16753 make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16754 WSET (w, window_end_vpos,
16755 make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16756 }
16757 else
16758 {
16759 WSET (w, window_end_vpos,
16760 make_number (XFASTINT (WGET (w, window_end_vpos)) - nrows_scrolled));
16761 }
16762
16763 WSET (w, window_end_valid, Qnil);
16764 w->desired_matrix->no_scrolling_p = 1;
16765
16766 #ifdef GLYPH_DEBUG
16767 debug_method_add (w, "try_window_reusing_current_matrix 2");
16768 #endif
16769 return 1;
16770 }
16771
16772 return 0;
16773 }
16774
16775
16776 \f
16777 /************************************************************************
16778 Window redisplay reusing current matrix when buffer has changed
16779 ************************************************************************/
16780
16781 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16782 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16783 ptrdiff_t *, ptrdiff_t *);
16784 static struct glyph_row *
16785 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16786 struct glyph_row *);
16787
16788
16789 /* Return the last row in MATRIX displaying text. If row START is
16790 non-null, start searching with that row. IT gives the dimensions
16791 of the display. Value is null if matrix is empty; otherwise it is
16792 a pointer to the row found. */
16793
16794 static struct glyph_row *
16795 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16796 struct glyph_row *start)
16797 {
16798 struct glyph_row *row, *row_found;
16799
16800 /* Set row_found to the last row in IT->w's current matrix
16801 displaying text. The loop looks funny but think of partially
16802 visible lines. */
16803 row_found = NULL;
16804 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16805 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16806 {
16807 eassert (row->enabled_p);
16808 row_found = row;
16809 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16810 break;
16811 ++row;
16812 }
16813
16814 return row_found;
16815 }
16816
16817
16818 /* Return the last row in the current matrix of W that is not affected
16819 by changes at the start of current_buffer that occurred since W's
16820 current matrix was built. Value is null if no such row exists.
16821
16822 BEG_UNCHANGED us the number of characters unchanged at the start of
16823 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16824 first changed character in current_buffer. Characters at positions <
16825 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16826 when the current matrix was built. */
16827
16828 static struct glyph_row *
16829 find_last_unchanged_at_beg_row (struct window *w)
16830 {
16831 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16832 struct glyph_row *row;
16833 struct glyph_row *row_found = NULL;
16834 int yb = window_text_bottom_y (w);
16835
16836 /* Find the last row displaying unchanged text. */
16837 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16838 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16839 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16840 ++row)
16841 {
16842 if (/* If row ends before first_changed_pos, it is unchanged,
16843 except in some case. */
16844 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16845 /* When row ends in ZV and we write at ZV it is not
16846 unchanged. */
16847 && !row->ends_at_zv_p
16848 /* When first_changed_pos is the end of a continued line,
16849 row is not unchanged because it may be no longer
16850 continued. */
16851 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16852 && (row->continued_p
16853 || row->exact_window_width_line_p))
16854 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16855 needs to be recomputed, so don't consider this row as
16856 unchanged. This happens when the last line was
16857 bidi-reordered and was killed immediately before this
16858 redisplay cycle. In that case, ROW->end stores the
16859 buffer position of the first visual-order character of
16860 the killed text, which is now beyond ZV. */
16861 && CHARPOS (row->end.pos) <= ZV)
16862 row_found = row;
16863
16864 /* Stop if last visible row. */
16865 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16866 break;
16867 }
16868
16869 return row_found;
16870 }
16871
16872
16873 /* Find the first glyph row in the current matrix of W that is not
16874 affected by changes at the end of current_buffer since the
16875 time W's current matrix was built.
16876
16877 Return in *DELTA the number of chars by which buffer positions in
16878 unchanged text at the end of current_buffer must be adjusted.
16879
16880 Return in *DELTA_BYTES the corresponding number of bytes.
16881
16882 Value is null if no such row exists, i.e. all rows are affected by
16883 changes. */
16884
16885 static struct glyph_row *
16886 find_first_unchanged_at_end_row (struct window *w,
16887 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16888 {
16889 struct glyph_row *row;
16890 struct glyph_row *row_found = NULL;
16891
16892 *delta = *delta_bytes = 0;
16893
16894 /* Display must not have been paused, otherwise the current matrix
16895 is not up to date. */
16896 eassert (!NILP (WGET (w, window_end_valid)));
16897
16898 /* A value of window_end_pos >= END_UNCHANGED means that the window
16899 end is in the range of changed text. If so, there is no
16900 unchanged row at the end of W's current matrix. */
16901 if (XFASTINT (WGET (w, window_end_pos)) >= END_UNCHANGED)
16902 return NULL;
16903
16904 /* Set row to the last row in W's current matrix displaying text. */
16905 row = MATRIX_ROW (w->current_matrix, XFASTINT (WGET (w, window_end_vpos)));
16906
16907 /* If matrix is entirely empty, no unchanged row exists. */
16908 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16909 {
16910 /* The value of row is the last glyph row in the matrix having a
16911 meaningful buffer position in it. The end position of row
16912 corresponds to window_end_pos. This allows us to translate
16913 buffer positions in the current matrix to current buffer
16914 positions for characters not in changed text. */
16915 ptrdiff_t Z_old =
16916 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (WGET (w, window_end_pos));
16917 ptrdiff_t Z_BYTE_old =
16918 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16919 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16920 struct glyph_row *first_text_row
16921 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16922
16923 *delta = Z - Z_old;
16924 *delta_bytes = Z_BYTE - Z_BYTE_old;
16925
16926 /* Set last_unchanged_pos to the buffer position of the last
16927 character in the buffer that has not been changed. Z is the
16928 index + 1 of the last character in current_buffer, i.e. by
16929 subtracting END_UNCHANGED we get the index of the last
16930 unchanged character, and we have to add BEG to get its buffer
16931 position. */
16932 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16933 last_unchanged_pos_old = last_unchanged_pos - *delta;
16934
16935 /* Search backward from ROW for a row displaying a line that
16936 starts at a minimum position >= last_unchanged_pos_old. */
16937 for (; row > first_text_row; --row)
16938 {
16939 /* This used to abort, but it can happen.
16940 It is ok to just stop the search instead here. KFS. */
16941 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16942 break;
16943
16944 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16945 row_found = row;
16946 }
16947 }
16948
16949 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16950
16951 return row_found;
16952 }
16953
16954
16955 /* Make sure that glyph rows in the current matrix of window W
16956 reference the same glyph memory as corresponding rows in the
16957 frame's frame matrix. This function is called after scrolling W's
16958 current matrix on a terminal frame in try_window_id and
16959 try_window_reusing_current_matrix. */
16960
16961 static void
16962 sync_frame_with_window_matrix_rows (struct window *w)
16963 {
16964 struct frame *f = XFRAME (WGET (w, frame));
16965 struct glyph_row *window_row, *window_row_end, *frame_row;
16966
16967 /* Preconditions: W must be a leaf window and full-width. Its frame
16968 must have a frame matrix. */
16969 eassert (NILP (WGET (w, hchild)) && NILP (WGET (w, vchild)));
16970 eassert (WINDOW_FULL_WIDTH_P (w));
16971 eassert (!FRAME_WINDOW_P (f));
16972
16973 /* If W is a full-width window, glyph pointers in W's current matrix
16974 have, by definition, to be the same as glyph pointers in the
16975 corresponding frame matrix. Note that frame matrices have no
16976 marginal areas (see build_frame_matrix). */
16977 window_row = w->current_matrix->rows;
16978 window_row_end = window_row + w->current_matrix->nrows;
16979 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16980 while (window_row < window_row_end)
16981 {
16982 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16983 struct glyph *end = window_row->glyphs[LAST_AREA];
16984
16985 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16986 frame_row->glyphs[TEXT_AREA] = start;
16987 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16988 frame_row->glyphs[LAST_AREA] = end;
16989
16990 /* Disable frame rows whose corresponding window rows have
16991 been disabled in try_window_id. */
16992 if (!window_row->enabled_p)
16993 frame_row->enabled_p = 0;
16994
16995 ++window_row, ++frame_row;
16996 }
16997 }
16998
16999
17000 /* Find the glyph row in window W containing CHARPOS. Consider all
17001 rows between START and END (not inclusive). END null means search
17002 all rows to the end of the display area of W. Value is the row
17003 containing CHARPOS or null. */
17004
17005 struct glyph_row *
17006 row_containing_pos (struct window *w, ptrdiff_t charpos,
17007 struct glyph_row *start, struct glyph_row *end, int dy)
17008 {
17009 struct glyph_row *row = start;
17010 struct glyph_row *best_row = NULL;
17011 ptrdiff_t mindif = BUF_ZV (XBUFFER (WGET (w, buffer))) + 1;
17012 int last_y;
17013
17014 /* If we happen to start on a header-line, skip that. */
17015 if (row->mode_line_p)
17016 ++row;
17017
17018 if ((end && row >= end) || !row->enabled_p)
17019 return NULL;
17020
17021 last_y = window_text_bottom_y (w) - dy;
17022
17023 while (1)
17024 {
17025 /* Give up if we have gone too far. */
17026 if (end && row >= end)
17027 return NULL;
17028 /* This formerly returned if they were equal.
17029 I think that both quantities are of a "last plus one" type;
17030 if so, when they are equal, the row is within the screen. -- rms. */
17031 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17032 return NULL;
17033
17034 /* If it is in this row, return this row. */
17035 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17036 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17037 /* The end position of a row equals the start
17038 position of the next row. If CHARPOS is there, we
17039 would rather display it in the next line, except
17040 when this line ends in ZV. */
17041 && !row->ends_at_zv_p
17042 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17043 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17044 {
17045 struct glyph *g;
17046
17047 if (NILP (BVAR (XBUFFER (WGET (w, buffer)), bidi_display_reordering))
17048 || (!best_row && !row->continued_p))
17049 return row;
17050 /* In bidi-reordered rows, there could be several rows
17051 occluding point, all of them belonging to the same
17052 continued line. We need to find the row which fits
17053 CHARPOS the best. */
17054 for (g = row->glyphs[TEXT_AREA];
17055 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17056 g++)
17057 {
17058 if (!STRINGP (g->object))
17059 {
17060 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17061 {
17062 mindif = eabs (g->charpos - charpos);
17063 best_row = row;
17064 /* Exact match always wins. */
17065 if (mindif == 0)
17066 return best_row;
17067 }
17068 }
17069 }
17070 }
17071 else if (best_row && !row->continued_p)
17072 return best_row;
17073 ++row;
17074 }
17075 }
17076
17077
17078 /* Try to redisplay window W by reusing its existing display. W's
17079 current matrix must be up to date when this function is called,
17080 i.e. window_end_valid must not be nil.
17081
17082 Value is
17083
17084 1 if display has been updated
17085 0 if otherwise unsuccessful
17086 -1 if redisplay with same window start is known not to succeed
17087
17088 The following steps are performed:
17089
17090 1. Find the last row in the current matrix of W that is not
17091 affected by changes at the start of current_buffer. If no such row
17092 is found, give up.
17093
17094 2. Find the first row in W's current matrix that is not affected by
17095 changes at the end of current_buffer. Maybe there is no such row.
17096
17097 3. Display lines beginning with the row + 1 found in step 1 to the
17098 row found in step 2 or, if step 2 didn't find a row, to the end of
17099 the window.
17100
17101 4. If cursor is not known to appear on the window, give up.
17102
17103 5. If display stopped at the row found in step 2, scroll the
17104 display and current matrix as needed.
17105
17106 6. Maybe display some lines at the end of W, if we must. This can
17107 happen under various circumstances, like a partially visible line
17108 becoming fully visible, or because newly displayed lines are displayed
17109 in smaller font sizes.
17110
17111 7. Update W's window end information. */
17112
17113 static int
17114 try_window_id (struct window *w)
17115 {
17116 struct frame *f = XFRAME (WGET (w, frame));
17117 struct glyph_matrix *current_matrix = w->current_matrix;
17118 struct glyph_matrix *desired_matrix = w->desired_matrix;
17119 struct glyph_row *last_unchanged_at_beg_row;
17120 struct glyph_row *first_unchanged_at_end_row;
17121 struct glyph_row *row;
17122 struct glyph_row *bottom_row;
17123 int bottom_vpos;
17124 struct it it;
17125 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17126 int dvpos, dy;
17127 struct text_pos start_pos;
17128 struct run run;
17129 int first_unchanged_at_end_vpos = 0;
17130 struct glyph_row *last_text_row, *last_text_row_at_end;
17131 struct text_pos start;
17132 ptrdiff_t first_changed_charpos, last_changed_charpos;
17133
17134 #ifdef GLYPH_DEBUG
17135 if (inhibit_try_window_id)
17136 return 0;
17137 #endif
17138
17139 /* This is handy for debugging. */
17140 #if 0
17141 #define GIVE_UP(X) \
17142 do { \
17143 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17144 return 0; \
17145 } while (0)
17146 #else
17147 #define GIVE_UP(X) return 0
17148 #endif
17149
17150 SET_TEXT_POS_FROM_MARKER (start, WGET (w, start));
17151
17152 /* Don't use this for mini-windows because these can show
17153 messages and mini-buffers, and we don't handle that here. */
17154 if (MINI_WINDOW_P (w))
17155 GIVE_UP (1);
17156
17157 /* This flag is used to prevent redisplay optimizations. */
17158 if (windows_or_buffers_changed || cursor_type_changed)
17159 GIVE_UP (2);
17160
17161 /* Verify that narrowing has not changed.
17162 Also verify that we were not told to prevent redisplay optimizations.
17163 It would be nice to further
17164 reduce the number of cases where this prevents try_window_id. */
17165 if (current_buffer->clip_changed
17166 || current_buffer->prevent_redisplay_optimizations_p)
17167 GIVE_UP (3);
17168
17169 /* Window must either use window-based redisplay or be full width. */
17170 if (!FRAME_WINDOW_P (f)
17171 && (!FRAME_LINE_INS_DEL_OK (f)
17172 || !WINDOW_FULL_WIDTH_P (w)))
17173 GIVE_UP (4);
17174
17175 /* Give up if point is known NOT to appear in W. */
17176 if (PT < CHARPOS (start))
17177 GIVE_UP (5);
17178
17179 /* Another way to prevent redisplay optimizations. */
17180 if (w->last_modified == 0)
17181 GIVE_UP (6);
17182
17183 /* Verify that window is not hscrolled. */
17184 if (w->hscroll != 0)
17185 GIVE_UP (7);
17186
17187 /* Verify that display wasn't paused. */
17188 if (NILP (WGET (w, window_end_valid)))
17189 GIVE_UP (8);
17190
17191 /* Can't use this if highlighting a region because a cursor movement
17192 will do more than just set the cursor. */
17193 if (!NILP (Vtransient_mark_mode)
17194 && !NILP (BVAR (current_buffer, mark_active)))
17195 GIVE_UP (9);
17196
17197 /* Likewise if highlighting trailing whitespace. */
17198 if (!NILP (Vshow_trailing_whitespace))
17199 GIVE_UP (11);
17200
17201 /* Likewise if showing a region. */
17202 if (!NILP (WGET (w, region_showing)))
17203 GIVE_UP (10);
17204
17205 /* Can't use this if overlay arrow position and/or string have
17206 changed. */
17207 if (overlay_arrows_changed_p ())
17208 GIVE_UP (12);
17209
17210 /* When word-wrap is on, adding a space to the first word of a
17211 wrapped line can change the wrap position, altering the line
17212 above it. It might be worthwhile to handle this more
17213 intelligently, but for now just redisplay from scratch. */
17214 if (!NILP (BVAR (XBUFFER (WGET (w, buffer)), word_wrap)))
17215 GIVE_UP (21);
17216
17217 /* Under bidi reordering, adding or deleting a character in the
17218 beginning of a paragraph, before the first strong directional
17219 character, can change the base direction of the paragraph (unless
17220 the buffer specifies a fixed paragraph direction), which will
17221 require to redisplay the whole paragraph. It might be worthwhile
17222 to find the paragraph limits and widen the range of redisplayed
17223 lines to that, but for now just give up this optimization and
17224 redisplay from scratch. */
17225 if (!NILP (BVAR (XBUFFER (WGET (w, buffer)), bidi_display_reordering))
17226 && NILP (BVAR (XBUFFER (WGET (w, buffer)), bidi_paragraph_direction)))
17227 GIVE_UP (22);
17228
17229 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17230 only if buffer has really changed. The reason is that the gap is
17231 initially at Z for freshly visited files. The code below would
17232 set end_unchanged to 0 in that case. */
17233 if (MODIFF > SAVE_MODIFF
17234 /* This seems to happen sometimes after saving a buffer. */
17235 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17236 {
17237 if (GPT - BEG < BEG_UNCHANGED)
17238 BEG_UNCHANGED = GPT - BEG;
17239 if (Z - GPT < END_UNCHANGED)
17240 END_UNCHANGED = Z - GPT;
17241 }
17242
17243 /* The position of the first and last character that has been changed. */
17244 first_changed_charpos = BEG + BEG_UNCHANGED;
17245 last_changed_charpos = Z - END_UNCHANGED;
17246
17247 /* If window starts after a line end, and the last change is in
17248 front of that newline, then changes don't affect the display.
17249 This case happens with stealth-fontification. Note that although
17250 the display is unchanged, glyph positions in the matrix have to
17251 be adjusted, of course. */
17252 row = MATRIX_ROW (w->current_matrix, XFASTINT (WGET (w, window_end_vpos)));
17253 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17254 && ((last_changed_charpos < CHARPOS (start)
17255 && CHARPOS (start) == BEGV)
17256 || (last_changed_charpos < CHARPOS (start) - 1
17257 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17258 {
17259 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17260 struct glyph_row *r0;
17261
17262 /* Compute how many chars/bytes have been added to or removed
17263 from the buffer. */
17264 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (WGET (w, window_end_pos));
17265 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17266 Z_delta = Z - Z_old;
17267 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17268
17269 /* Give up if PT is not in the window. Note that it already has
17270 been checked at the start of try_window_id that PT is not in
17271 front of the window start. */
17272 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17273 GIVE_UP (13);
17274
17275 /* If window start is unchanged, we can reuse the whole matrix
17276 as is, after adjusting glyph positions. No need to compute
17277 the window end again, since its offset from Z hasn't changed. */
17278 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17279 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17280 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17281 /* PT must not be in a partially visible line. */
17282 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17283 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17284 {
17285 /* Adjust positions in the glyph matrix. */
17286 if (Z_delta || Z_delta_bytes)
17287 {
17288 struct glyph_row *r1
17289 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17290 increment_matrix_positions (w->current_matrix,
17291 MATRIX_ROW_VPOS (r0, current_matrix),
17292 MATRIX_ROW_VPOS (r1, current_matrix),
17293 Z_delta, Z_delta_bytes);
17294 }
17295
17296 /* Set the cursor. */
17297 row = row_containing_pos (w, PT, r0, NULL, 0);
17298 if (row)
17299 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17300 else
17301 abort ();
17302 return 1;
17303 }
17304 }
17305
17306 /* Handle the case that changes are all below what is displayed in
17307 the window, and that PT is in the window. This shortcut cannot
17308 be taken if ZV is visible in the window, and text has been added
17309 there that is visible in the window. */
17310 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17311 /* ZV is not visible in the window, or there are no
17312 changes at ZV, actually. */
17313 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17314 || first_changed_charpos == last_changed_charpos))
17315 {
17316 struct glyph_row *r0;
17317
17318 /* Give up if PT is not in the window. Note that it already has
17319 been checked at the start of try_window_id that PT is not in
17320 front of the window start. */
17321 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17322 GIVE_UP (14);
17323
17324 /* If window start is unchanged, we can reuse the whole matrix
17325 as is, without changing glyph positions since no text has
17326 been added/removed in front of the window end. */
17327 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17328 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17329 /* PT must not be in a partially visible line. */
17330 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17331 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17332 {
17333 /* We have to compute the window end anew since text
17334 could have been added/removed after it. */
17335 WSET (w, window_end_pos,
17336 make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17337 w->window_end_bytepos
17338 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17339
17340 /* Set the cursor. */
17341 row = row_containing_pos (w, PT, r0, NULL, 0);
17342 if (row)
17343 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17344 else
17345 abort ();
17346 return 2;
17347 }
17348 }
17349
17350 /* Give up if window start is in the changed area.
17351
17352 The condition used to read
17353
17354 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17355
17356 but why that was tested escapes me at the moment. */
17357 if (CHARPOS (start) >= first_changed_charpos
17358 && CHARPOS (start) <= last_changed_charpos)
17359 GIVE_UP (15);
17360
17361 /* Check that window start agrees with the start of the first glyph
17362 row in its current matrix. Check this after we know the window
17363 start is not in changed text, otherwise positions would not be
17364 comparable. */
17365 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17366 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17367 GIVE_UP (16);
17368
17369 /* Give up if the window ends in strings. Overlay strings
17370 at the end are difficult to handle, so don't try. */
17371 row = MATRIX_ROW (current_matrix, XFASTINT (WGET (w, window_end_vpos)));
17372 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17373 GIVE_UP (20);
17374
17375 /* Compute the position at which we have to start displaying new
17376 lines. Some of the lines at the top of the window might be
17377 reusable because they are not displaying changed text. Find the
17378 last row in W's current matrix not affected by changes at the
17379 start of current_buffer. Value is null if changes start in the
17380 first line of window. */
17381 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17382 if (last_unchanged_at_beg_row)
17383 {
17384 /* Avoid starting to display in the middle of a character, a TAB
17385 for instance. This is easier than to set up the iterator
17386 exactly, and it's not a frequent case, so the additional
17387 effort wouldn't really pay off. */
17388 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17389 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17390 && last_unchanged_at_beg_row > w->current_matrix->rows)
17391 --last_unchanged_at_beg_row;
17392
17393 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17394 GIVE_UP (17);
17395
17396 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17397 GIVE_UP (18);
17398 start_pos = it.current.pos;
17399
17400 /* Start displaying new lines in the desired matrix at the same
17401 vpos we would use in the current matrix, i.e. below
17402 last_unchanged_at_beg_row. */
17403 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17404 current_matrix);
17405 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17406 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17407
17408 eassert (it.hpos == 0 && it.current_x == 0);
17409 }
17410 else
17411 {
17412 /* There are no reusable lines at the start of the window.
17413 Start displaying in the first text line. */
17414 start_display (&it, w, start);
17415 it.vpos = it.first_vpos;
17416 start_pos = it.current.pos;
17417 }
17418
17419 /* Find the first row that is not affected by changes at the end of
17420 the buffer. Value will be null if there is no unchanged row, in
17421 which case we must redisplay to the end of the window. delta
17422 will be set to the value by which buffer positions beginning with
17423 first_unchanged_at_end_row have to be adjusted due to text
17424 changes. */
17425 first_unchanged_at_end_row
17426 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17427 IF_DEBUG (debug_delta = delta);
17428 IF_DEBUG (debug_delta_bytes = delta_bytes);
17429
17430 /* Set stop_pos to the buffer position up to which we will have to
17431 display new lines. If first_unchanged_at_end_row != NULL, this
17432 is the buffer position of the start of the line displayed in that
17433 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17434 that we don't stop at a buffer position. */
17435 stop_pos = 0;
17436 if (first_unchanged_at_end_row)
17437 {
17438 eassert (last_unchanged_at_beg_row == NULL
17439 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17440
17441 /* If this is a continuation line, move forward to the next one
17442 that isn't. Changes in lines above affect this line.
17443 Caution: this may move first_unchanged_at_end_row to a row
17444 not displaying text. */
17445 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17446 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17447 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17448 < it.last_visible_y))
17449 ++first_unchanged_at_end_row;
17450
17451 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17452 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17453 >= it.last_visible_y))
17454 first_unchanged_at_end_row = NULL;
17455 else
17456 {
17457 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17458 + delta);
17459 first_unchanged_at_end_vpos
17460 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17461 eassert (stop_pos >= Z - END_UNCHANGED);
17462 }
17463 }
17464 else if (last_unchanged_at_beg_row == NULL)
17465 GIVE_UP (19);
17466
17467
17468 #ifdef GLYPH_DEBUG
17469
17470 /* Either there is no unchanged row at the end, or the one we have
17471 now displays text. This is a necessary condition for the window
17472 end pos calculation at the end of this function. */
17473 eassert (first_unchanged_at_end_row == NULL
17474 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17475
17476 debug_last_unchanged_at_beg_vpos
17477 = (last_unchanged_at_beg_row
17478 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17479 : -1);
17480 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17481
17482 #endif /* GLYPH_DEBUG */
17483
17484
17485 /* Display new lines. Set last_text_row to the last new line
17486 displayed which has text on it, i.e. might end up as being the
17487 line where the window_end_vpos is. */
17488 w->cursor.vpos = -1;
17489 last_text_row = NULL;
17490 overlay_arrow_seen = 0;
17491 while (it.current_y < it.last_visible_y
17492 && !fonts_changed_p
17493 && (first_unchanged_at_end_row == NULL
17494 || IT_CHARPOS (it) < stop_pos))
17495 {
17496 if (display_line (&it))
17497 last_text_row = it.glyph_row - 1;
17498 }
17499
17500 if (fonts_changed_p)
17501 return -1;
17502
17503
17504 /* Compute differences in buffer positions, y-positions etc. for
17505 lines reused at the bottom of the window. Compute what we can
17506 scroll. */
17507 if (first_unchanged_at_end_row
17508 /* No lines reused because we displayed everything up to the
17509 bottom of the window. */
17510 && it.current_y < it.last_visible_y)
17511 {
17512 dvpos = (it.vpos
17513 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17514 current_matrix));
17515 dy = it.current_y - first_unchanged_at_end_row->y;
17516 run.current_y = first_unchanged_at_end_row->y;
17517 run.desired_y = run.current_y + dy;
17518 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17519 }
17520 else
17521 {
17522 delta = delta_bytes = dvpos = dy
17523 = run.current_y = run.desired_y = run.height = 0;
17524 first_unchanged_at_end_row = NULL;
17525 }
17526 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17527
17528
17529 /* Find the cursor if not already found. We have to decide whether
17530 PT will appear on this window (it sometimes doesn't, but this is
17531 not a very frequent case.) This decision has to be made before
17532 the current matrix is altered. A value of cursor.vpos < 0 means
17533 that PT is either in one of the lines beginning at
17534 first_unchanged_at_end_row or below the window. Don't care for
17535 lines that might be displayed later at the window end; as
17536 mentioned, this is not a frequent case. */
17537 if (w->cursor.vpos < 0)
17538 {
17539 /* Cursor in unchanged rows at the top? */
17540 if (PT < CHARPOS (start_pos)
17541 && last_unchanged_at_beg_row)
17542 {
17543 row = row_containing_pos (w, PT,
17544 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17545 last_unchanged_at_beg_row + 1, 0);
17546 if (row)
17547 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17548 }
17549
17550 /* Start from first_unchanged_at_end_row looking for PT. */
17551 else if (first_unchanged_at_end_row)
17552 {
17553 row = row_containing_pos (w, PT - delta,
17554 first_unchanged_at_end_row, NULL, 0);
17555 if (row)
17556 set_cursor_from_row (w, row, w->current_matrix, delta,
17557 delta_bytes, dy, dvpos);
17558 }
17559
17560 /* Give up if cursor was not found. */
17561 if (w->cursor.vpos < 0)
17562 {
17563 clear_glyph_matrix (w->desired_matrix);
17564 return -1;
17565 }
17566 }
17567
17568 /* Don't let the cursor end in the scroll margins. */
17569 {
17570 int this_scroll_margin, cursor_height;
17571
17572 this_scroll_margin =
17573 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17574 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17575 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17576
17577 if ((w->cursor.y < this_scroll_margin
17578 && CHARPOS (start) > BEGV)
17579 /* Old redisplay didn't take scroll margin into account at the bottom,
17580 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17581 || (w->cursor.y + (make_cursor_line_fully_visible_p
17582 ? cursor_height + this_scroll_margin
17583 : 1)) > it.last_visible_y)
17584 {
17585 w->cursor.vpos = -1;
17586 clear_glyph_matrix (w->desired_matrix);
17587 return -1;
17588 }
17589 }
17590
17591 /* Scroll the display. Do it before changing the current matrix so
17592 that xterm.c doesn't get confused about where the cursor glyph is
17593 found. */
17594 if (dy && run.height)
17595 {
17596 update_begin (f);
17597
17598 if (FRAME_WINDOW_P (f))
17599 {
17600 FRAME_RIF (f)->update_window_begin_hook (w);
17601 FRAME_RIF (f)->clear_window_mouse_face (w);
17602 FRAME_RIF (f)->scroll_run_hook (w, &run);
17603 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17604 }
17605 else
17606 {
17607 /* Terminal frame. In this case, dvpos gives the number of
17608 lines to scroll by; dvpos < 0 means scroll up. */
17609 int from_vpos
17610 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17611 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17612 int end = (WINDOW_TOP_EDGE_LINE (w)
17613 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17614 + window_internal_height (w));
17615
17616 #if defined (HAVE_GPM) || defined (MSDOS)
17617 x_clear_window_mouse_face (w);
17618 #endif
17619 /* Perform the operation on the screen. */
17620 if (dvpos > 0)
17621 {
17622 /* Scroll last_unchanged_at_beg_row to the end of the
17623 window down dvpos lines. */
17624 set_terminal_window (f, end);
17625
17626 /* On dumb terminals delete dvpos lines at the end
17627 before inserting dvpos empty lines. */
17628 if (!FRAME_SCROLL_REGION_OK (f))
17629 ins_del_lines (f, end - dvpos, -dvpos);
17630
17631 /* Insert dvpos empty lines in front of
17632 last_unchanged_at_beg_row. */
17633 ins_del_lines (f, from, dvpos);
17634 }
17635 else if (dvpos < 0)
17636 {
17637 /* Scroll up last_unchanged_at_beg_vpos to the end of
17638 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17639 set_terminal_window (f, end);
17640
17641 /* Delete dvpos lines in front of
17642 last_unchanged_at_beg_vpos. ins_del_lines will set
17643 the cursor to the given vpos and emit |dvpos| delete
17644 line sequences. */
17645 ins_del_lines (f, from + dvpos, dvpos);
17646
17647 /* On a dumb terminal insert dvpos empty lines at the
17648 end. */
17649 if (!FRAME_SCROLL_REGION_OK (f))
17650 ins_del_lines (f, end + dvpos, -dvpos);
17651 }
17652
17653 set_terminal_window (f, 0);
17654 }
17655
17656 update_end (f);
17657 }
17658
17659 /* Shift reused rows of the current matrix to the right position.
17660 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17661 text. */
17662 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17663 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17664 if (dvpos < 0)
17665 {
17666 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17667 bottom_vpos, dvpos);
17668 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17669 bottom_vpos, 0);
17670 }
17671 else if (dvpos > 0)
17672 {
17673 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17674 bottom_vpos, dvpos);
17675 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17676 first_unchanged_at_end_vpos + dvpos, 0);
17677 }
17678
17679 /* For frame-based redisplay, make sure that current frame and window
17680 matrix are in sync with respect to glyph memory. */
17681 if (!FRAME_WINDOW_P (f))
17682 sync_frame_with_window_matrix_rows (w);
17683
17684 /* Adjust buffer positions in reused rows. */
17685 if (delta || delta_bytes)
17686 increment_matrix_positions (current_matrix,
17687 first_unchanged_at_end_vpos + dvpos,
17688 bottom_vpos, delta, delta_bytes);
17689
17690 /* Adjust Y positions. */
17691 if (dy)
17692 shift_glyph_matrix (w, current_matrix,
17693 first_unchanged_at_end_vpos + dvpos,
17694 bottom_vpos, dy);
17695
17696 if (first_unchanged_at_end_row)
17697 {
17698 first_unchanged_at_end_row += dvpos;
17699 if (first_unchanged_at_end_row->y >= it.last_visible_y
17700 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17701 first_unchanged_at_end_row = NULL;
17702 }
17703
17704 /* If scrolling up, there may be some lines to display at the end of
17705 the window. */
17706 last_text_row_at_end = NULL;
17707 if (dy < 0)
17708 {
17709 /* Scrolling up can leave for example a partially visible line
17710 at the end of the window to be redisplayed. */
17711 /* Set last_row to the glyph row in the current matrix where the
17712 window end line is found. It has been moved up or down in
17713 the matrix by dvpos. */
17714 int last_vpos = XFASTINT (WGET (w, window_end_vpos)) + dvpos;
17715 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17716
17717 /* If last_row is the window end line, it should display text. */
17718 eassert (last_row->displays_text_p);
17719
17720 /* If window end line was partially visible before, begin
17721 displaying at that line. Otherwise begin displaying with the
17722 line following it. */
17723 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17724 {
17725 init_to_row_start (&it, w, last_row);
17726 it.vpos = last_vpos;
17727 it.current_y = last_row->y;
17728 }
17729 else
17730 {
17731 init_to_row_end (&it, w, last_row);
17732 it.vpos = 1 + last_vpos;
17733 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17734 ++last_row;
17735 }
17736
17737 /* We may start in a continuation line. If so, we have to
17738 get the right continuation_lines_width and current_x. */
17739 it.continuation_lines_width = last_row->continuation_lines_width;
17740 it.hpos = it.current_x = 0;
17741
17742 /* Display the rest of the lines at the window end. */
17743 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17744 while (it.current_y < it.last_visible_y
17745 && !fonts_changed_p)
17746 {
17747 /* Is it always sure that the display agrees with lines in
17748 the current matrix? I don't think so, so we mark rows
17749 displayed invalid in the current matrix by setting their
17750 enabled_p flag to zero. */
17751 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17752 if (display_line (&it))
17753 last_text_row_at_end = it.glyph_row - 1;
17754 }
17755 }
17756
17757 /* Update window_end_pos and window_end_vpos. */
17758 if (first_unchanged_at_end_row
17759 && !last_text_row_at_end)
17760 {
17761 /* Window end line if one of the preserved rows from the current
17762 matrix. Set row to the last row displaying text in current
17763 matrix starting at first_unchanged_at_end_row, after
17764 scrolling. */
17765 eassert (first_unchanged_at_end_row->displays_text_p);
17766 row = find_last_row_displaying_text (w->current_matrix, &it,
17767 first_unchanged_at_end_row);
17768 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17769
17770 WSET (w, window_end_pos, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17771 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17772 WSET (w, window_end_vpos,
17773 make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17774 eassert (w->window_end_bytepos >= 0);
17775 IF_DEBUG (debug_method_add (w, "A"));
17776 }
17777 else if (last_text_row_at_end)
17778 {
17779 WSET (w, window_end_pos,
17780 make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17781 w->window_end_bytepos
17782 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17783 WSET (w, window_end_vpos,
17784 make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix)));
17785 eassert (w->window_end_bytepos >= 0);
17786 IF_DEBUG (debug_method_add (w, "B"));
17787 }
17788 else if (last_text_row)
17789 {
17790 /* We have displayed either to the end of the window or at the
17791 end of the window, i.e. the last row with text is to be found
17792 in the desired matrix. */
17793 WSET (w, window_end_pos,
17794 make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17795 w->window_end_bytepos
17796 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17797 WSET (w, window_end_vpos,
17798 make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17799 eassert (w->window_end_bytepos >= 0);
17800 }
17801 else if (first_unchanged_at_end_row == NULL
17802 && last_text_row == NULL
17803 && last_text_row_at_end == NULL)
17804 {
17805 /* Displayed to end of window, but no line containing text was
17806 displayed. Lines were deleted at the end of the window. */
17807 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17808 int vpos = XFASTINT (WGET (w, window_end_vpos));
17809 struct glyph_row *current_row = current_matrix->rows + vpos;
17810 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17811
17812 for (row = NULL;
17813 row == NULL && vpos >= first_vpos;
17814 --vpos, --current_row, --desired_row)
17815 {
17816 if (desired_row->enabled_p)
17817 {
17818 if (desired_row->displays_text_p)
17819 row = desired_row;
17820 }
17821 else if (current_row->displays_text_p)
17822 row = current_row;
17823 }
17824
17825 eassert (row != NULL);
17826 WSET (w, window_end_vpos, make_number (vpos + 1));
17827 WSET (w, window_end_pos, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17828 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17829 eassert (w->window_end_bytepos >= 0);
17830 IF_DEBUG (debug_method_add (w, "C"));
17831 }
17832 else
17833 abort ();
17834
17835 IF_DEBUG (debug_end_pos = XFASTINT (WGET (w, window_end_pos));
17836 debug_end_vpos = XFASTINT (WGET (w, window_end_vpos)));
17837
17838 /* Record that display has not been completed. */
17839 WSET (w, window_end_valid, Qnil);
17840 w->desired_matrix->no_scrolling_p = 1;
17841 return 3;
17842
17843 #undef GIVE_UP
17844 }
17845
17846
17847 \f
17848 /***********************************************************************
17849 More debugging support
17850 ***********************************************************************/
17851
17852 #ifdef GLYPH_DEBUG
17853
17854 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17855 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17856 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17857
17858
17859 /* Dump the contents of glyph matrix MATRIX on stderr.
17860
17861 GLYPHS 0 means don't show glyph contents.
17862 GLYPHS 1 means show glyphs in short form
17863 GLYPHS > 1 means show glyphs in long form. */
17864
17865 void
17866 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17867 {
17868 int i;
17869 for (i = 0; i < matrix->nrows; ++i)
17870 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17871 }
17872
17873
17874 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17875 the glyph row and area where the glyph comes from. */
17876
17877 void
17878 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17879 {
17880 if (glyph->type == CHAR_GLYPH)
17881 {
17882 fprintf (stderr,
17883 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17884 glyph - row->glyphs[TEXT_AREA],
17885 'C',
17886 glyph->charpos,
17887 (BUFFERP (glyph->object)
17888 ? 'B'
17889 : (STRINGP (glyph->object)
17890 ? 'S'
17891 : '-')),
17892 glyph->pixel_width,
17893 glyph->u.ch,
17894 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17895 ? glyph->u.ch
17896 : '.'),
17897 glyph->face_id,
17898 glyph->left_box_line_p,
17899 glyph->right_box_line_p);
17900 }
17901 else if (glyph->type == STRETCH_GLYPH)
17902 {
17903 fprintf (stderr,
17904 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17905 glyph - row->glyphs[TEXT_AREA],
17906 'S',
17907 glyph->charpos,
17908 (BUFFERP (glyph->object)
17909 ? 'B'
17910 : (STRINGP (glyph->object)
17911 ? 'S'
17912 : '-')),
17913 glyph->pixel_width,
17914 0,
17915 '.',
17916 glyph->face_id,
17917 glyph->left_box_line_p,
17918 glyph->right_box_line_p);
17919 }
17920 else if (glyph->type == IMAGE_GLYPH)
17921 {
17922 fprintf (stderr,
17923 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17924 glyph - row->glyphs[TEXT_AREA],
17925 'I',
17926 glyph->charpos,
17927 (BUFFERP (glyph->object)
17928 ? 'B'
17929 : (STRINGP (glyph->object)
17930 ? 'S'
17931 : '-')),
17932 glyph->pixel_width,
17933 glyph->u.img_id,
17934 '.',
17935 glyph->face_id,
17936 glyph->left_box_line_p,
17937 glyph->right_box_line_p);
17938 }
17939 else if (glyph->type == COMPOSITE_GLYPH)
17940 {
17941 fprintf (stderr,
17942 " %5td %4c %6"pI"d %c %3d 0x%05x",
17943 glyph - row->glyphs[TEXT_AREA],
17944 '+',
17945 glyph->charpos,
17946 (BUFFERP (glyph->object)
17947 ? 'B'
17948 : (STRINGP (glyph->object)
17949 ? 'S'
17950 : '-')),
17951 glyph->pixel_width,
17952 glyph->u.cmp.id);
17953 if (glyph->u.cmp.automatic)
17954 fprintf (stderr,
17955 "[%d-%d]",
17956 glyph->slice.cmp.from, glyph->slice.cmp.to);
17957 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17958 glyph->face_id,
17959 glyph->left_box_line_p,
17960 glyph->right_box_line_p);
17961 }
17962 }
17963
17964
17965 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17966 GLYPHS 0 means don't show glyph contents.
17967 GLYPHS 1 means show glyphs in short form
17968 GLYPHS > 1 means show glyphs in long form. */
17969
17970 void
17971 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17972 {
17973 if (glyphs != 1)
17974 {
17975 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17976 fprintf (stderr, "======================================================================\n");
17977
17978 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17979 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17980 vpos,
17981 MATRIX_ROW_START_CHARPOS (row),
17982 MATRIX_ROW_END_CHARPOS (row),
17983 row->used[TEXT_AREA],
17984 row->contains_overlapping_glyphs_p,
17985 row->enabled_p,
17986 row->truncated_on_left_p,
17987 row->truncated_on_right_p,
17988 row->continued_p,
17989 MATRIX_ROW_CONTINUATION_LINE_P (row),
17990 row->displays_text_p,
17991 row->ends_at_zv_p,
17992 row->fill_line_p,
17993 row->ends_in_middle_of_char_p,
17994 row->starts_in_middle_of_char_p,
17995 row->mouse_face_p,
17996 row->x,
17997 row->y,
17998 row->pixel_width,
17999 row->height,
18000 row->visible_height,
18001 row->ascent,
18002 row->phys_ascent);
18003 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18004 row->end.overlay_string_index,
18005 row->continuation_lines_width);
18006 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18007 CHARPOS (row->start.string_pos),
18008 CHARPOS (row->end.string_pos));
18009 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18010 row->end.dpvec_index);
18011 }
18012
18013 if (glyphs > 1)
18014 {
18015 int area;
18016
18017 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18018 {
18019 struct glyph *glyph = row->glyphs[area];
18020 struct glyph *glyph_end = glyph + row->used[area];
18021
18022 /* Glyph for a line end in text. */
18023 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18024 ++glyph_end;
18025
18026 if (glyph < glyph_end)
18027 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18028
18029 for (; glyph < glyph_end; ++glyph)
18030 dump_glyph (row, glyph, area);
18031 }
18032 }
18033 else if (glyphs == 1)
18034 {
18035 int area;
18036
18037 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18038 {
18039 char *s = alloca (row->used[area] + 1);
18040 int i;
18041
18042 for (i = 0; i < row->used[area]; ++i)
18043 {
18044 struct glyph *glyph = row->glyphs[area] + i;
18045 if (glyph->type == CHAR_GLYPH
18046 && glyph->u.ch < 0x80
18047 && glyph->u.ch >= ' ')
18048 s[i] = glyph->u.ch;
18049 else
18050 s[i] = '.';
18051 }
18052
18053 s[i] = '\0';
18054 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18055 }
18056 }
18057 }
18058
18059
18060 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18061 Sdump_glyph_matrix, 0, 1, "p",
18062 doc: /* Dump the current matrix of the selected window to stderr.
18063 Shows contents of glyph row structures. With non-nil
18064 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18065 glyphs in short form, otherwise show glyphs in long form. */)
18066 (Lisp_Object glyphs)
18067 {
18068 struct window *w = XWINDOW (selected_window);
18069 struct buffer *buffer = XBUFFER (WGET (w, buffer));
18070
18071 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18072 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18073 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18074 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18075 fprintf (stderr, "=============================================\n");
18076 dump_glyph_matrix (w->current_matrix,
18077 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18078 return Qnil;
18079 }
18080
18081
18082 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18083 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18084 (void)
18085 {
18086 struct frame *f = XFRAME (selected_frame);
18087 dump_glyph_matrix (f->current_matrix, 1);
18088 return Qnil;
18089 }
18090
18091
18092 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18093 doc: /* Dump glyph row ROW to stderr.
18094 GLYPH 0 means don't dump glyphs.
18095 GLYPH 1 means dump glyphs in short form.
18096 GLYPH > 1 or omitted means dump glyphs in long form. */)
18097 (Lisp_Object row, Lisp_Object glyphs)
18098 {
18099 struct glyph_matrix *matrix;
18100 EMACS_INT vpos;
18101
18102 CHECK_NUMBER (row);
18103 matrix = XWINDOW (selected_window)->current_matrix;
18104 vpos = XINT (row);
18105 if (vpos >= 0 && vpos < matrix->nrows)
18106 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18107 vpos,
18108 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18109 return Qnil;
18110 }
18111
18112
18113 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18114 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18115 GLYPH 0 means don't dump glyphs.
18116 GLYPH 1 means dump glyphs in short form.
18117 GLYPH > 1 or omitted means dump glyphs in long form. */)
18118 (Lisp_Object row, Lisp_Object glyphs)
18119 {
18120 struct frame *sf = SELECTED_FRAME ();
18121 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18122 EMACS_INT vpos;
18123
18124 CHECK_NUMBER (row);
18125 vpos = XINT (row);
18126 if (vpos >= 0 && vpos < m->nrows)
18127 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18128 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18129 return Qnil;
18130 }
18131
18132
18133 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18134 doc: /* Toggle tracing of redisplay.
18135 With ARG, turn tracing on if and only if ARG is positive. */)
18136 (Lisp_Object arg)
18137 {
18138 if (NILP (arg))
18139 trace_redisplay_p = !trace_redisplay_p;
18140 else
18141 {
18142 arg = Fprefix_numeric_value (arg);
18143 trace_redisplay_p = XINT (arg) > 0;
18144 }
18145
18146 return Qnil;
18147 }
18148
18149
18150 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18151 doc: /* Like `format', but print result to stderr.
18152 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18153 (ptrdiff_t nargs, Lisp_Object *args)
18154 {
18155 Lisp_Object s = Fformat (nargs, args);
18156 fprintf (stderr, "%s", SDATA (s));
18157 return Qnil;
18158 }
18159
18160 #endif /* GLYPH_DEBUG */
18161
18162
18163 \f
18164 /***********************************************************************
18165 Building Desired Matrix Rows
18166 ***********************************************************************/
18167
18168 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18169 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18170
18171 static struct glyph_row *
18172 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18173 {
18174 struct frame *f = XFRAME (WINDOW_FRAME (w));
18175 struct buffer *buffer = XBUFFER (WGET (w, buffer));
18176 struct buffer *old = current_buffer;
18177 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18178 int arrow_len = SCHARS (overlay_arrow_string);
18179 const unsigned char *arrow_end = arrow_string + arrow_len;
18180 const unsigned char *p;
18181 struct it it;
18182 int multibyte_p;
18183 int n_glyphs_before;
18184
18185 set_buffer_temp (buffer);
18186 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18187 it.glyph_row->used[TEXT_AREA] = 0;
18188 SET_TEXT_POS (it.position, 0, 0);
18189
18190 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18191 p = arrow_string;
18192 while (p < arrow_end)
18193 {
18194 Lisp_Object face, ilisp;
18195
18196 /* Get the next character. */
18197 if (multibyte_p)
18198 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18199 else
18200 {
18201 it.c = it.char_to_display = *p, it.len = 1;
18202 if (! ASCII_CHAR_P (it.c))
18203 it.char_to_display = BYTE8_TO_CHAR (it.c);
18204 }
18205 p += it.len;
18206
18207 /* Get its face. */
18208 ilisp = make_number (p - arrow_string);
18209 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18210 it.face_id = compute_char_face (f, it.char_to_display, face);
18211
18212 /* Compute its width, get its glyphs. */
18213 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18214 SET_TEXT_POS (it.position, -1, -1);
18215 PRODUCE_GLYPHS (&it);
18216
18217 /* If this character doesn't fit any more in the line, we have
18218 to remove some glyphs. */
18219 if (it.current_x > it.last_visible_x)
18220 {
18221 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18222 break;
18223 }
18224 }
18225
18226 set_buffer_temp (old);
18227 return it.glyph_row;
18228 }
18229
18230
18231 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18232 glyphs to insert is determined by produce_special_glyphs. */
18233
18234 static void
18235 insert_left_trunc_glyphs (struct it *it)
18236 {
18237 struct it truncate_it;
18238 struct glyph *from, *end, *to, *toend;
18239
18240 eassert (!FRAME_WINDOW_P (it->f)
18241 || (!it->glyph_row->reversed_p
18242 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18243 || (it->glyph_row->reversed_p
18244 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18245
18246 /* Get the truncation glyphs. */
18247 truncate_it = *it;
18248 truncate_it.current_x = 0;
18249 truncate_it.face_id = DEFAULT_FACE_ID;
18250 truncate_it.glyph_row = &scratch_glyph_row;
18251 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18252 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18253 truncate_it.object = make_number (0);
18254 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18255
18256 /* Overwrite glyphs from IT with truncation glyphs. */
18257 if (!it->glyph_row->reversed_p)
18258 {
18259 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18260
18261 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18262 end = from + tused;
18263 to = it->glyph_row->glyphs[TEXT_AREA];
18264 toend = to + it->glyph_row->used[TEXT_AREA];
18265 if (FRAME_WINDOW_P (it->f))
18266 {
18267 /* On GUI frames, when variable-size fonts are displayed,
18268 the truncation glyphs may need more pixels than the row's
18269 glyphs they overwrite. We overwrite more glyphs to free
18270 enough screen real estate, and enlarge the stretch glyph
18271 on the right (see display_line), if there is one, to
18272 preserve the screen position of the truncation glyphs on
18273 the right. */
18274 int w = 0;
18275 struct glyph *g = to;
18276 short used;
18277
18278 /* The first glyph could be partially visible, in which case
18279 it->glyph_row->x will be negative. But we want the left
18280 truncation glyphs to be aligned at the left margin of the
18281 window, so we override the x coordinate at which the row
18282 will begin. */
18283 it->glyph_row->x = 0;
18284 while (g < toend && w < it->truncation_pixel_width)
18285 {
18286 w += g->pixel_width;
18287 ++g;
18288 }
18289 if (g - to - tused > 0)
18290 {
18291 memmove (to + tused, g, (toend - g) * sizeof(*g));
18292 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18293 }
18294 used = it->glyph_row->used[TEXT_AREA];
18295 if (it->glyph_row->truncated_on_right_p
18296 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18297 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18298 == STRETCH_GLYPH)
18299 {
18300 int extra = w - it->truncation_pixel_width;
18301
18302 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18303 }
18304 }
18305
18306 while (from < end)
18307 *to++ = *from++;
18308
18309 /* There may be padding glyphs left over. Overwrite them too. */
18310 if (!FRAME_WINDOW_P (it->f))
18311 {
18312 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18313 {
18314 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18315 while (from < end)
18316 *to++ = *from++;
18317 }
18318 }
18319
18320 if (to > toend)
18321 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18322 }
18323 else
18324 {
18325 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18326
18327 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18328 that back to front. */
18329 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18330 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18331 toend = it->glyph_row->glyphs[TEXT_AREA];
18332 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18333 if (FRAME_WINDOW_P (it->f))
18334 {
18335 int w = 0;
18336 struct glyph *g = to;
18337
18338 while (g >= toend && w < it->truncation_pixel_width)
18339 {
18340 w += g->pixel_width;
18341 --g;
18342 }
18343 if (to - g - tused > 0)
18344 to = g + tused;
18345 if (it->glyph_row->truncated_on_right_p
18346 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18347 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18348 {
18349 int extra = w - it->truncation_pixel_width;
18350
18351 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18352 }
18353 }
18354
18355 while (from >= end && to >= toend)
18356 *to-- = *from--;
18357 if (!FRAME_WINDOW_P (it->f))
18358 {
18359 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18360 {
18361 from =
18362 truncate_it.glyph_row->glyphs[TEXT_AREA]
18363 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18364 while (from >= end && to >= toend)
18365 *to-- = *from--;
18366 }
18367 }
18368 if (from >= end)
18369 {
18370 /* Need to free some room before prepending additional
18371 glyphs. */
18372 int move_by = from - end + 1;
18373 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18374 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18375
18376 for ( ; g >= g0; g--)
18377 g[move_by] = *g;
18378 while (from >= end)
18379 *to-- = *from--;
18380 it->glyph_row->used[TEXT_AREA] += move_by;
18381 }
18382 }
18383 }
18384
18385 /* Compute the hash code for ROW. */
18386 unsigned
18387 row_hash (struct glyph_row *row)
18388 {
18389 int area, k;
18390 unsigned hashval = 0;
18391
18392 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18393 for (k = 0; k < row->used[area]; ++k)
18394 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18395 + row->glyphs[area][k].u.val
18396 + row->glyphs[area][k].face_id
18397 + row->glyphs[area][k].padding_p
18398 + (row->glyphs[area][k].type << 2));
18399
18400 return hashval;
18401 }
18402
18403 /* Compute the pixel height and width of IT->glyph_row.
18404
18405 Most of the time, ascent and height of a display line will be equal
18406 to the max_ascent and max_height values of the display iterator
18407 structure. This is not the case if
18408
18409 1. We hit ZV without displaying anything. In this case, max_ascent
18410 and max_height will be zero.
18411
18412 2. We have some glyphs that don't contribute to the line height.
18413 (The glyph row flag contributes_to_line_height_p is for future
18414 pixmap extensions).
18415
18416 The first case is easily covered by using default values because in
18417 these cases, the line height does not really matter, except that it
18418 must not be zero. */
18419
18420 static void
18421 compute_line_metrics (struct it *it)
18422 {
18423 struct glyph_row *row = it->glyph_row;
18424
18425 if (FRAME_WINDOW_P (it->f))
18426 {
18427 int i, min_y, max_y;
18428
18429 /* The line may consist of one space only, that was added to
18430 place the cursor on it. If so, the row's height hasn't been
18431 computed yet. */
18432 if (row->height == 0)
18433 {
18434 if (it->max_ascent + it->max_descent == 0)
18435 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18436 row->ascent = it->max_ascent;
18437 row->height = it->max_ascent + it->max_descent;
18438 row->phys_ascent = it->max_phys_ascent;
18439 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18440 row->extra_line_spacing = it->max_extra_line_spacing;
18441 }
18442
18443 /* Compute the width of this line. */
18444 row->pixel_width = row->x;
18445 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18446 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18447
18448 eassert (row->pixel_width >= 0);
18449 eassert (row->ascent >= 0 && row->height > 0);
18450
18451 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18452 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18453
18454 /* If first line's physical ascent is larger than its logical
18455 ascent, use the physical ascent, and make the row taller.
18456 This makes accented characters fully visible. */
18457 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18458 && row->phys_ascent > row->ascent)
18459 {
18460 row->height += row->phys_ascent - row->ascent;
18461 row->ascent = row->phys_ascent;
18462 }
18463
18464 /* Compute how much of the line is visible. */
18465 row->visible_height = row->height;
18466
18467 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18468 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18469
18470 if (row->y < min_y)
18471 row->visible_height -= min_y - row->y;
18472 if (row->y + row->height > max_y)
18473 row->visible_height -= row->y + row->height - max_y;
18474 }
18475 else
18476 {
18477 row->pixel_width = row->used[TEXT_AREA];
18478 if (row->continued_p)
18479 row->pixel_width -= it->continuation_pixel_width;
18480 else if (row->truncated_on_right_p)
18481 row->pixel_width -= it->truncation_pixel_width;
18482 row->ascent = row->phys_ascent = 0;
18483 row->height = row->phys_height = row->visible_height = 1;
18484 row->extra_line_spacing = 0;
18485 }
18486
18487 /* Compute a hash code for this row. */
18488 row->hash = row_hash (row);
18489
18490 it->max_ascent = it->max_descent = 0;
18491 it->max_phys_ascent = it->max_phys_descent = 0;
18492 }
18493
18494
18495 /* Append one space to the glyph row of iterator IT if doing a
18496 window-based redisplay. The space has the same face as
18497 IT->face_id. Value is non-zero if a space was added.
18498
18499 This function is called to make sure that there is always one glyph
18500 at the end of a glyph row that the cursor can be set on under
18501 window-systems. (If there weren't such a glyph we would not know
18502 how wide and tall a box cursor should be displayed).
18503
18504 At the same time this space let's a nicely handle clearing to the
18505 end of the line if the row ends in italic text. */
18506
18507 static int
18508 append_space_for_newline (struct it *it, int default_face_p)
18509 {
18510 if (FRAME_WINDOW_P (it->f))
18511 {
18512 int n = it->glyph_row->used[TEXT_AREA];
18513
18514 if (it->glyph_row->glyphs[TEXT_AREA] + n
18515 < it->glyph_row->glyphs[1 + TEXT_AREA])
18516 {
18517 /* Save some values that must not be changed.
18518 Must save IT->c and IT->len because otherwise
18519 ITERATOR_AT_END_P wouldn't work anymore after
18520 append_space_for_newline has been called. */
18521 enum display_element_type saved_what = it->what;
18522 int saved_c = it->c, saved_len = it->len;
18523 int saved_char_to_display = it->char_to_display;
18524 int saved_x = it->current_x;
18525 int saved_face_id = it->face_id;
18526 struct text_pos saved_pos;
18527 Lisp_Object saved_object;
18528 struct face *face;
18529
18530 saved_object = it->object;
18531 saved_pos = it->position;
18532
18533 it->what = IT_CHARACTER;
18534 memset (&it->position, 0, sizeof it->position);
18535 it->object = make_number (0);
18536 it->c = it->char_to_display = ' ';
18537 it->len = 1;
18538
18539 /* If the default face was remapped, be sure to use the
18540 remapped face for the appended newline. */
18541 if (default_face_p)
18542 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18543 else if (it->face_before_selective_p)
18544 it->face_id = it->saved_face_id;
18545 face = FACE_FROM_ID (it->f, it->face_id);
18546 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18547
18548 PRODUCE_GLYPHS (it);
18549
18550 it->override_ascent = -1;
18551 it->constrain_row_ascent_descent_p = 0;
18552 it->current_x = saved_x;
18553 it->object = saved_object;
18554 it->position = saved_pos;
18555 it->what = saved_what;
18556 it->face_id = saved_face_id;
18557 it->len = saved_len;
18558 it->c = saved_c;
18559 it->char_to_display = saved_char_to_display;
18560 return 1;
18561 }
18562 }
18563
18564 return 0;
18565 }
18566
18567
18568 /* Extend the face of the last glyph in the text area of IT->glyph_row
18569 to the end of the display line. Called from display_line. If the
18570 glyph row is empty, add a space glyph to it so that we know the
18571 face to draw. Set the glyph row flag fill_line_p. If the glyph
18572 row is R2L, prepend a stretch glyph to cover the empty space to the
18573 left of the leftmost glyph. */
18574
18575 static void
18576 extend_face_to_end_of_line (struct it *it)
18577 {
18578 struct face *face, *default_face;
18579 struct frame *f = it->f;
18580
18581 /* If line is already filled, do nothing. Non window-system frames
18582 get a grace of one more ``pixel'' because their characters are
18583 1-``pixel'' wide, so they hit the equality too early. This grace
18584 is needed only for R2L rows that are not continued, to produce
18585 one extra blank where we could display the cursor. */
18586 if (it->current_x >= it->last_visible_x
18587 + (!FRAME_WINDOW_P (f)
18588 && it->glyph_row->reversed_p
18589 && !it->glyph_row->continued_p))
18590 return;
18591
18592 /* The default face, possibly remapped. */
18593 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18594
18595 /* Face extension extends the background and box of IT->face_id
18596 to the end of the line. If the background equals the background
18597 of the frame, we don't have to do anything. */
18598 if (it->face_before_selective_p)
18599 face = FACE_FROM_ID (f, it->saved_face_id);
18600 else
18601 face = FACE_FROM_ID (f, it->face_id);
18602
18603 if (FRAME_WINDOW_P (f)
18604 && it->glyph_row->displays_text_p
18605 && face->box == FACE_NO_BOX
18606 && face->background == FRAME_BACKGROUND_PIXEL (f)
18607 && !face->stipple
18608 && !it->glyph_row->reversed_p)
18609 return;
18610
18611 /* Set the glyph row flag indicating that the face of the last glyph
18612 in the text area has to be drawn to the end of the text area. */
18613 it->glyph_row->fill_line_p = 1;
18614
18615 /* If current character of IT is not ASCII, make sure we have the
18616 ASCII face. This will be automatically undone the next time
18617 get_next_display_element returns a multibyte character. Note
18618 that the character will always be single byte in unibyte
18619 text. */
18620 if (!ASCII_CHAR_P (it->c))
18621 {
18622 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18623 }
18624
18625 if (FRAME_WINDOW_P (f))
18626 {
18627 /* If the row is empty, add a space with the current face of IT,
18628 so that we know which face to draw. */
18629 if (it->glyph_row->used[TEXT_AREA] == 0)
18630 {
18631 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18632 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18633 it->glyph_row->used[TEXT_AREA] = 1;
18634 }
18635 #ifdef HAVE_WINDOW_SYSTEM
18636 if (it->glyph_row->reversed_p)
18637 {
18638 /* Prepend a stretch glyph to the row, such that the
18639 rightmost glyph will be drawn flushed all the way to the
18640 right margin of the window. The stretch glyph that will
18641 occupy the empty space, if any, to the left of the
18642 glyphs. */
18643 struct font *font = face->font ? face->font : FRAME_FONT (f);
18644 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18645 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18646 struct glyph *g;
18647 int row_width, stretch_ascent, stretch_width;
18648 struct text_pos saved_pos;
18649 int saved_face_id, saved_avoid_cursor;
18650
18651 for (row_width = 0, g = row_start; g < row_end; g++)
18652 row_width += g->pixel_width;
18653 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18654 if (stretch_width > 0)
18655 {
18656 stretch_ascent =
18657 (((it->ascent + it->descent)
18658 * FONT_BASE (font)) / FONT_HEIGHT (font));
18659 saved_pos = it->position;
18660 memset (&it->position, 0, sizeof it->position);
18661 saved_avoid_cursor = it->avoid_cursor_p;
18662 it->avoid_cursor_p = 1;
18663 saved_face_id = it->face_id;
18664 /* The last row's stretch glyph should get the default
18665 face, to avoid painting the rest of the window with
18666 the region face, if the region ends at ZV. */
18667 if (it->glyph_row->ends_at_zv_p)
18668 it->face_id = default_face->id;
18669 else
18670 it->face_id = face->id;
18671 append_stretch_glyph (it, make_number (0), stretch_width,
18672 it->ascent + it->descent, stretch_ascent);
18673 it->position = saved_pos;
18674 it->avoid_cursor_p = saved_avoid_cursor;
18675 it->face_id = saved_face_id;
18676 }
18677 }
18678 #endif /* HAVE_WINDOW_SYSTEM */
18679 }
18680 else
18681 {
18682 /* Save some values that must not be changed. */
18683 int saved_x = it->current_x;
18684 struct text_pos saved_pos;
18685 Lisp_Object saved_object;
18686 enum display_element_type saved_what = it->what;
18687 int saved_face_id = it->face_id;
18688
18689 saved_object = it->object;
18690 saved_pos = it->position;
18691
18692 it->what = IT_CHARACTER;
18693 memset (&it->position, 0, sizeof it->position);
18694 it->object = make_number (0);
18695 it->c = it->char_to_display = ' ';
18696 it->len = 1;
18697 /* The last row's blank glyphs should get the default face, to
18698 avoid painting the rest of the window with the region face,
18699 if the region ends at ZV. */
18700 if (it->glyph_row->ends_at_zv_p)
18701 it->face_id = default_face->id;
18702 else
18703 it->face_id = face->id;
18704
18705 PRODUCE_GLYPHS (it);
18706
18707 while (it->current_x <= it->last_visible_x)
18708 PRODUCE_GLYPHS (it);
18709
18710 /* Don't count these blanks really. It would let us insert a left
18711 truncation glyph below and make us set the cursor on them, maybe. */
18712 it->current_x = saved_x;
18713 it->object = saved_object;
18714 it->position = saved_pos;
18715 it->what = saved_what;
18716 it->face_id = saved_face_id;
18717 }
18718 }
18719
18720
18721 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18722 trailing whitespace. */
18723
18724 static int
18725 trailing_whitespace_p (ptrdiff_t charpos)
18726 {
18727 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18728 int c = 0;
18729
18730 while (bytepos < ZV_BYTE
18731 && (c = FETCH_CHAR (bytepos),
18732 c == ' ' || c == '\t'))
18733 ++bytepos;
18734
18735 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18736 {
18737 if (bytepos != PT_BYTE)
18738 return 1;
18739 }
18740 return 0;
18741 }
18742
18743
18744 /* Highlight trailing whitespace, if any, in ROW. */
18745
18746 static void
18747 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18748 {
18749 int used = row->used[TEXT_AREA];
18750
18751 if (used)
18752 {
18753 struct glyph *start = row->glyphs[TEXT_AREA];
18754 struct glyph *glyph = start + used - 1;
18755
18756 if (row->reversed_p)
18757 {
18758 /* Right-to-left rows need to be processed in the opposite
18759 direction, so swap the edge pointers. */
18760 glyph = start;
18761 start = row->glyphs[TEXT_AREA] + used - 1;
18762 }
18763
18764 /* Skip over glyphs inserted to display the cursor at the
18765 end of a line, for extending the face of the last glyph
18766 to the end of the line on terminals, and for truncation
18767 and continuation glyphs. */
18768 if (!row->reversed_p)
18769 {
18770 while (glyph >= start
18771 && glyph->type == CHAR_GLYPH
18772 && INTEGERP (glyph->object))
18773 --glyph;
18774 }
18775 else
18776 {
18777 while (glyph <= start
18778 && glyph->type == CHAR_GLYPH
18779 && INTEGERP (glyph->object))
18780 ++glyph;
18781 }
18782
18783 /* If last glyph is a space or stretch, and it's trailing
18784 whitespace, set the face of all trailing whitespace glyphs in
18785 IT->glyph_row to `trailing-whitespace'. */
18786 if ((row->reversed_p ? glyph <= start : glyph >= start)
18787 && BUFFERP (glyph->object)
18788 && (glyph->type == STRETCH_GLYPH
18789 || (glyph->type == CHAR_GLYPH
18790 && glyph->u.ch == ' '))
18791 && trailing_whitespace_p (glyph->charpos))
18792 {
18793 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18794 if (face_id < 0)
18795 return;
18796
18797 if (!row->reversed_p)
18798 {
18799 while (glyph >= start
18800 && BUFFERP (glyph->object)
18801 && (glyph->type == STRETCH_GLYPH
18802 || (glyph->type == CHAR_GLYPH
18803 && glyph->u.ch == ' ')))
18804 (glyph--)->face_id = face_id;
18805 }
18806 else
18807 {
18808 while (glyph <= start
18809 && BUFFERP (glyph->object)
18810 && (glyph->type == STRETCH_GLYPH
18811 || (glyph->type == CHAR_GLYPH
18812 && glyph->u.ch == ' ')))
18813 (glyph++)->face_id = face_id;
18814 }
18815 }
18816 }
18817 }
18818
18819
18820 /* Value is non-zero if glyph row ROW should be
18821 used to hold the cursor. */
18822
18823 static int
18824 cursor_row_p (struct glyph_row *row)
18825 {
18826 int result = 1;
18827
18828 if (PT == CHARPOS (row->end.pos)
18829 || PT == MATRIX_ROW_END_CHARPOS (row))
18830 {
18831 /* Suppose the row ends on a string.
18832 Unless the row is continued, that means it ends on a newline
18833 in the string. If it's anything other than a display string
18834 (e.g., a before-string from an overlay), we don't want the
18835 cursor there. (This heuristic seems to give the optimal
18836 behavior for the various types of multi-line strings.)
18837 One exception: if the string has `cursor' property on one of
18838 its characters, we _do_ want the cursor there. */
18839 if (CHARPOS (row->end.string_pos) >= 0)
18840 {
18841 if (row->continued_p)
18842 result = 1;
18843 else
18844 {
18845 /* Check for `display' property. */
18846 struct glyph *beg = row->glyphs[TEXT_AREA];
18847 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18848 struct glyph *glyph;
18849
18850 result = 0;
18851 for (glyph = end; glyph >= beg; --glyph)
18852 if (STRINGP (glyph->object))
18853 {
18854 Lisp_Object prop
18855 = Fget_char_property (make_number (PT),
18856 Qdisplay, Qnil);
18857 result =
18858 (!NILP (prop)
18859 && display_prop_string_p (prop, glyph->object));
18860 /* If there's a `cursor' property on one of the
18861 string's characters, this row is a cursor row,
18862 even though this is not a display string. */
18863 if (!result)
18864 {
18865 Lisp_Object s = glyph->object;
18866
18867 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18868 {
18869 ptrdiff_t gpos = glyph->charpos;
18870
18871 if (!NILP (Fget_char_property (make_number (gpos),
18872 Qcursor, s)))
18873 {
18874 result = 1;
18875 break;
18876 }
18877 }
18878 }
18879 break;
18880 }
18881 }
18882 }
18883 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18884 {
18885 /* If the row ends in middle of a real character,
18886 and the line is continued, we want the cursor here.
18887 That's because CHARPOS (ROW->end.pos) would equal
18888 PT if PT is before the character. */
18889 if (!row->ends_in_ellipsis_p)
18890 result = row->continued_p;
18891 else
18892 /* If the row ends in an ellipsis, then
18893 CHARPOS (ROW->end.pos) will equal point after the
18894 invisible text. We want that position to be displayed
18895 after the ellipsis. */
18896 result = 0;
18897 }
18898 /* If the row ends at ZV, display the cursor at the end of that
18899 row instead of at the start of the row below. */
18900 else if (row->ends_at_zv_p)
18901 result = 1;
18902 else
18903 result = 0;
18904 }
18905
18906 return result;
18907 }
18908
18909 \f
18910
18911 /* Push the property PROP so that it will be rendered at the current
18912 position in IT. Return 1 if PROP was successfully pushed, 0
18913 otherwise. Called from handle_line_prefix to handle the
18914 `line-prefix' and `wrap-prefix' properties. */
18915
18916 static int
18917 push_prefix_prop (struct it *it, Lisp_Object prop)
18918 {
18919 struct text_pos pos =
18920 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18921
18922 eassert (it->method == GET_FROM_BUFFER
18923 || it->method == GET_FROM_DISPLAY_VECTOR
18924 || it->method == GET_FROM_STRING);
18925
18926 /* We need to save the current buffer/string position, so it will be
18927 restored by pop_it, because iterate_out_of_display_property
18928 depends on that being set correctly, but some situations leave
18929 it->position not yet set when this function is called. */
18930 push_it (it, &pos);
18931
18932 if (STRINGP (prop))
18933 {
18934 if (SCHARS (prop) == 0)
18935 {
18936 pop_it (it);
18937 return 0;
18938 }
18939
18940 it->string = prop;
18941 it->string_from_prefix_prop_p = 1;
18942 it->multibyte_p = STRING_MULTIBYTE (it->string);
18943 it->current.overlay_string_index = -1;
18944 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18945 it->end_charpos = it->string_nchars = SCHARS (it->string);
18946 it->method = GET_FROM_STRING;
18947 it->stop_charpos = 0;
18948 it->prev_stop = 0;
18949 it->base_level_stop = 0;
18950
18951 /* Force paragraph direction to be that of the parent
18952 buffer/string. */
18953 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18954 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18955 else
18956 it->paragraph_embedding = L2R;
18957
18958 /* Set up the bidi iterator for this display string. */
18959 if (it->bidi_p)
18960 {
18961 it->bidi_it.string.lstring = it->string;
18962 it->bidi_it.string.s = NULL;
18963 it->bidi_it.string.schars = it->end_charpos;
18964 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18965 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18966 it->bidi_it.string.unibyte = !it->multibyte_p;
18967 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18968 }
18969 }
18970 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18971 {
18972 it->method = GET_FROM_STRETCH;
18973 it->object = prop;
18974 }
18975 #ifdef HAVE_WINDOW_SYSTEM
18976 else if (IMAGEP (prop))
18977 {
18978 it->what = IT_IMAGE;
18979 it->image_id = lookup_image (it->f, prop);
18980 it->method = GET_FROM_IMAGE;
18981 }
18982 #endif /* HAVE_WINDOW_SYSTEM */
18983 else
18984 {
18985 pop_it (it); /* bogus display property, give up */
18986 return 0;
18987 }
18988
18989 return 1;
18990 }
18991
18992 /* Return the character-property PROP at the current position in IT. */
18993
18994 static Lisp_Object
18995 get_it_property (struct it *it, Lisp_Object prop)
18996 {
18997 Lisp_Object position;
18998
18999 if (STRINGP (it->object))
19000 position = make_number (IT_STRING_CHARPOS (*it));
19001 else if (BUFFERP (it->object))
19002 position = make_number (IT_CHARPOS (*it));
19003 else
19004 return Qnil;
19005
19006 return Fget_char_property (position, prop, it->object);
19007 }
19008
19009 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19010
19011 static void
19012 handle_line_prefix (struct it *it)
19013 {
19014 Lisp_Object prefix;
19015
19016 if (it->continuation_lines_width > 0)
19017 {
19018 prefix = get_it_property (it, Qwrap_prefix);
19019 if (NILP (prefix))
19020 prefix = Vwrap_prefix;
19021 }
19022 else
19023 {
19024 prefix = get_it_property (it, Qline_prefix);
19025 if (NILP (prefix))
19026 prefix = Vline_prefix;
19027 }
19028 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19029 {
19030 /* If the prefix is wider than the window, and we try to wrap
19031 it, it would acquire its own wrap prefix, and so on till the
19032 iterator stack overflows. So, don't wrap the prefix. */
19033 it->line_wrap = TRUNCATE;
19034 it->avoid_cursor_p = 1;
19035 }
19036 }
19037
19038 \f
19039
19040 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19041 only for R2L lines from display_line and display_string, when they
19042 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19043 the line/string needs to be continued on the next glyph row. */
19044 static void
19045 unproduce_glyphs (struct it *it, int n)
19046 {
19047 struct glyph *glyph, *end;
19048
19049 eassert (it->glyph_row);
19050 eassert (it->glyph_row->reversed_p);
19051 eassert (it->area == TEXT_AREA);
19052 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19053
19054 if (n > it->glyph_row->used[TEXT_AREA])
19055 n = it->glyph_row->used[TEXT_AREA];
19056 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19057 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19058 for ( ; glyph < end; glyph++)
19059 glyph[-n] = *glyph;
19060 }
19061
19062 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19063 and ROW->maxpos. */
19064 static void
19065 find_row_edges (struct it *it, struct glyph_row *row,
19066 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19067 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19068 {
19069 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19070 lines' rows is implemented for bidi-reordered rows. */
19071
19072 /* ROW->minpos is the value of min_pos, the minimal buffer position
19073 we have in ROW, or ROW->start.pos if that is smaller. */
19074 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19075 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19076 else
19077 /* We didn't find buffer positions smaller than ROW->start, or
19078 didn't find _any_ valid buffer positions in any of the glyphs,
19079 so we must trust the iterator's computed positions. */
19080 row->minpos = row->start.pos;
19081 if (max_pos <= 0)
19082 {
19083 max_pos = CHARPOS (it->current.pos);
19084 max_bpos = BYTEPOS (it->current.pos);
19085 }
19086
19087 /* Here are the various use-cases for ending the row, and the
19088 corresponding values for ROW->maxpos:
19089
19090 Line ends in a newline from buffer eol_pos + 1
19091 Line is continued from buffer max_pos + 1
19092 Line is truncated on right it->current.pos
19093 Line ends in a newline from string max_pos + 1(*)
19094 (*) + 1 only when line ends in a forward scan
19095 Line is continued from string max_pos
19096 Line is continued from display vector max_pos
19097 Line is entirely from a string min_pos == max_pos
19098 Line is entirely from a display vector min_pos == max_pos
19099 Line that ends at ZV ZV
19100
19101 If you discover other use-cases, please add them here as
19102 appropriate. */
19103 if (row->ends_at_zv_p)
19104 row->maxpos = it->current.pos;
19105 else if (row->used[TEXT_AREA])
19106 {
19107 int seen_this_string = 0;
19108 struct glyph_row *r1 = row - 1;
19109
19110 /* Did we see the same display string on the previous row? */
19111 if (STRINGP (it->object)
19112 /* this is not the first row */
19113 && row > it->w->desired_matrix->rows
19114 /* previous row is not the header line */
19115 && !r1->mode_line_p
19116 /* previous row also ends in a newline from a string */
19117 && r1->ends_in_newline_from_string_p)
19118 {
19119 struct glyph *start, *end;
19120
19121 /* Search for the last glyph of the previous row that came
19122 from buffer or string. Depending on whether the row is
19123 L2R or R2L, we need to process it front to back or the
19124 other way round. */
19125 if (!r1->reversed_p)
19126 {
19127 start = r1->glyphs[TEXT_AREA];
19128 end = start + r1->used[TEXT_AREA];
19129 /* Glyphs inserted by redisplay have an integer (zero)
19130 as their object. */
19131 while (end > start
19132 && INTEGERP ((end - 1)->object)
19133 && (end - 1)->charpos <= 0)
19134 --end;
19135 if (end > start)
19136 {
19137 if (EQ ((end - 1)->object, it->object))
19138 seen_this_string = 1;
19139 }
19140 else
19141 /* If all the glyphs of the previous row were inserted
19142 by redisplay, it means the previous row was
19143 produced from a single newline, which is only
19144 possible if that newline came from the same string
19145 as the one which produced this ROW. */
19146 seen_this_string = 1;
19147 }
19148 else
19149 {
19150 end = r1->glyphs[TEXT_AREA] - 1;
19151 start = end + r1->used[TEXT_AREA];
19152 while (end < start
19153 && INTEGERP ((end + 1)->object)
19154 && (end + 1)->charpos <= 0)
19155 ++end;
19156 if (end < start)
19157 {
19158 if (EQ ((end + 1)->object, it->object))
19159 seen_this_string = 1;
19160 }
19161 else
19162 seen_this_string = 1;
19163 }
19164 }
19165 /* Take note of each display string that covers a newline only
19166 once, the first time we see it. This is for when a display
19167 string includes more than one newline in it. */
19168 if (row->ends_in_newline_from_string_p && !seen_this_string)
19169 {
19170 /* If we were scanning the buffer forward when we displayed
19171 the string, we want to account for at least one buffer
19172 position that belongs to this row (position covered by
19173 the display string), so that cursor positioning will
19174 consider this row as a candidate when point is at the end
19175 of the visual line represented by this row. This is not
19176 required when scanning back, because max_pos will already
19177 have a much larger value. */
19178 if (CHARPOS (row->end.pos) > max_pos)
19179 INC_BOTH (max_pos, max_bpos);
19180 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19181 }
19182 else if (CHARPOS (it->eol_pos) > 0)
19183 SET_TEXT_POS (row->maxpos,
19184 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19185 else if (row->continued_p)
19186 {
19187 /* If max_pos is different from IT's current position, it
19188 means IT->method does not belong to the display element
19189 at max_pos. However, it also means that the display
19190 element at max_pos was displayed in its entirety on this
19191 line, which is equivalent to saying that the next line
19192 starts at the next buffer position. */
19193 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19194 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19195 else
19196 {
19197 INC_BOTH (max_pos, max_bpos);
19198 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19199 }
19200 }
19201 else if (row->truncated_on_right_p)
19202 /* display_line already called reseat_at_next_visible_line_start,
19203 which puts the iterator at the beginning of the next line, in
19204 the logical order. */
19205 row->maxpos = it->current.pos;
19206 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19207 /* A line that is entirely from a string/image/stretch... */
19208 row->maxpos = row->minpos;
19209 else
19210 abort ();
19211 }
19212 else
19213 row->maxpos = it->current.pos;
19214 }
19215
19216 /* Construct the glyph row IT->glyph_row in the desired matrix of
19217 IT->w from text at the current position of IT. See dispextern.h
19218 for an overview of struct it. Value is non-zero if
19219 IT->glyph_row displays text, as opposed to a line displaying ZV
19220 only. */
19221
19222 static int
19223 display_line (struct it *it)
19224 {
19225 struct glyph_row *row = it->glyph_row;
19226 Lisp_Object overlay_arrow_string;
19227 struct it wrap_it;
19228 void *wrap_data = NULL;
19229 int may_wrap = 0, wrap_x IF_LINT (= 0);
19230 int wrap_row_used = -1;
19231 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19232 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19233 int wrap_row_extra_line_spacing IF_LINT (= 0);
19234 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19235 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19236 int cvpos;
19237 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19238 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19239
19240 /* We always start displaying at hpos zero even if hscrolled. */
19241 eassert (it->hpos == 0 && it->current_x == 0);
19242
19243 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19244 >= it->w->desired_matrix->nrows)
19245 {
19246 it->w->nrows_scale_factor++;
19247 fonts_changed_p = 1;
19248 return 0;
19249 }
19250
19251 /* Is IT->w showing the region? */
19252 WSET (it->w, region_showing, it->region_beg_charpos > 0 ? Qt : Qnil);
19253
19254 /* Clear the result glyph row and enable it. */
19255 prepare_desired_row (row);
19256
19257 row->y = it->current_y;
19258 row->start = it->start;
19259 row->continuation_lines_width = it->continuation_lines_width;
19260 row->displays_text_p = 1;
19261 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19262 it->starts_in_middle_of_char_p = 0;
19263
19264 /* Arrange the overlays nicely for our purposes. Usually, we call
19265 display_line on only one line at a time, in which case this
19266 can't really hurt too much, or we call it on lines which appear
19267 one after another in the buffer, in which case all calls to
19268 recenter_overlay_lists but the first will be pretty cheap. */
19269 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19270
19271 /* Move over display elements that are not visible because we are
19272 hscrolled. This may stop at an x-position < IT->first_visible_x
19273 if the first glyph is partially visible or if we hit a line end. */
19274 if (it->current_x < it->first_visible_x)
19275 {
19276 enum move_it_result move_result;
19277
19278 this_line_min_pos = row->start.pos;
19279 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19280 MOVE_TO_POS | MOVE_TO_X);
19281 /* If we are under a large hscroll, move_it_in_display_line_to
19282 could hit the end of the line without reaching
19283 it->first_visible_x. Pretend that we did reach it. This is
19284 especially important on a TTY, where we will call
19285 extend_face_to_end_of_line, which needs to know how many
19286 blank glyphs to produce. */
19287 if (it->current_x < it->first_visible_x
19288 && (move_result == MOVE_NEWLINE_OR_CR
19289 || move_result == MOVE_POS_MATCH_OR_ZV))
19290 it->current_x = it->first_visible_x;
19291
19292 /* Record the smallest positions seen while we moved over
19293 display elements that are not visible. This is needed by
19294 redisplay_internal for optimizing the case where the cursor
19295 stays inside the same line. The rest of this function only
19296 considers positions that are actually displayed, so
19297 RECORD_MAX_MIN_POS will not otherwise record positions that
19298 are hscrolled to the left of the left edge of the window. */
19299 min_pos = CHARPOS (this_line_min_pos);
19300 min_bpos = BYTEPOS (this_line_min_pos);
19301 }
19302 else
19303 {
19304 /* We only do this when not calling `move_it_in_display_line_to'
19305 above, because move_it_in_display_line_to calls
19306 handle_line_prefix itself. */
19307 handle_line_prefix (it);
19308 }
19309
19310 /* Get the initial row height. This is either the height of the
19311 text hscrolled, if there is any, or zero. */
19312 row->ascent = it->max_ascent;
19313 row->height = it->max_ascent + it->max_descent;
19314 row->phys_ascent = it->max_phys_ascent;
19315 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19316 row->extra_line_spacing = it->max_extra_line_spacing;
19317
19318 /* Utility macro to record max and min buffer positions seen until now. */
19319 #define RECORD_MAX_MIN_POS(IT) \
19320 do \
19321 { \
19322 int composition_p = !STRINGP ((IT)->string) \
19323 && ((IT)->what == IT_COMPOSITION); \
19324 ptrdiff_t current_pos = \
19325 composition_p ? (IT)->cmp_it.charpos \
19326 : IT_CHARPOS (*(IT)); \
19327 ptrdiff_t current_bpos = \
19328 composition_p ? CHAR_TO_BYTE (current_pos) \
19329 : IT_BYTEPOS (*(IT)); \
19330 if (current_pos < min_pos) \
19331 { \
19332 min_pos = current_pos; \
19333 min_bpos = current_bpos; \
19334 } \
19335 if (IT_CHARPOS (*it) > max_pos) \
19336 { \
19337 max_pos = IT_CHARPOS (*it); \
19338 max_bpos = IT_BYTEPOS (*it); \
19339 } \
19340 } \
19341 while (0)
19342
19343 /* Loop generating characters. The loop is left with IT on the next
19344 character to display. */
19345 while (1)
19346 {
19347 int n_glyphs_before, hpos_before, x_before;
19348 int x, nglyphs;
19349 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19350
19351 /* Retrieve the next thing to display. Value is zero if end of
19352 buffer reached. */
19353 if (!get_next_display_element (it))
19354 {
19355 /* Maybe add a space at the end of this line that is used to
19356 display the cursor there under X. Set the charpos of the
19357 first glyph of blank lines not corresponding to any text
19358 to -1. */
19359 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19360 row->exact_window_width_line_p = 1;
19361 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19362 || row->used[TEXT_AREA] == 0)
19363 {
19364 row->glyphs[TEXT_AREA]->charpos = -1;
19365 row->displays_text_p = 0;
19366
19367 if (!NILP (BVAR (XBUFFER (WGET (it->w, buffer)), indicate_empty_lines))
19368 && (!MINI_WINDOW_P (it->w)
19369 || (minibuf_level && EQ (it->window, minibuf_window))))
19370 row->indicate_empty_line_p = 1;
19371 }
19372
19373 it->continuation_lines_width = 0;
19374 row->ends_at_zv_p = 1;
19375 /* A row that displays right-to-left text must always have
19376 its last face extended all the way to the end of line,
19377 even if this row ends in ZV, because we still write to
19378 the screen left to right. We also need to extend the
19379 last face if the default face is remapped to some
19380 different face, otherwise the functions that clear
19381 portions of the screen will clear with the default face's
19382 background color. */
19383 if (row->reversed_p
19384 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19385 extend_face_to_end_of_line (it);
19386 break;
19387 }
19388
19389 /* Now, get the metrics of what we want to display. This also
19390 generates glyphs in `row' (which is IT->glyph_row). */
19391 n_glyphs_before = row->used[TEXT_AREA];
19392 x = it->current_x;
19393
19394 /* Remember the line height so far in case the next element doesn't
19395 fit on the line. */
19396 if (it->line_wrap != TRUNCATE)
19397 {
19398 ascent = it->max_ascent;
19399 descent = it->max_descent;
19400 phys_ascent = it->max_phys_ascent;
19401 phys_descent = it->max_phys_descent;
19402
19403 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19404 {
19405 if (IT_DISPLAYING_WHITESPACE (it))
19406 may_wrap = 1;
19407 else if (may_wrap)
19408 {
19409 SAVE_IT (wrap_it, *it, wrap_data);
19410 wrap_x = x;
19411 wrap_row_used = row->used[TEXT_AREA];
19412 wrap_row_ascent = row->ascent;
19413 wrap_row_height = row->height;
19414 wrap_row_phys_ascent = row->phys_ascent;
19415 wrap_row_phys_height = row->phys_height;
19416 wrap_row_extra_line_spacing = row->extra_line_spacing;
19417 wrap_row_min_pos = min_pos;
19418 wrap_row_min_bpos = min_bpos;
19419 wrap_row_max_pos = max_pos;
19420 wrap_row_max_bpos = max_bpos;
19421 may_wrap = 0;
19422 }
19423 }
19424 }
19425
19426 PRODUCE_GLYPHS (it);
19427
19428 /* If this display element was in marginal areas, continue with
19429 the next one. */
19430 if (it->area != TEXT_AREA)
19431 {
19432 row->ascent = max (row->ascent, it->max_ascent);
19433 row->height = max (row->height, it->max_ascent + it->max_descent);
19434 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19435 row->phys_height = max (row->phys_height,
19436 it->max_phys_ascent + it->max_phys_descent);
19437 row->extra_line_spacing = max (row->extra_line_spacing,
19438 it->max_extra_line_spacing);
19439 set_iterator_to_next (it, 1);
19440 continue;
19441 }
19442
19443 /* Does the display element fit on the line? If we truncate
19444 lines, we should draw past the right edge of the window. If
19445 we don't truncate, we want to stop so that we can display the
19446 continuation glyph before the right margin. If lines are
19447 continued, there are two possible strategies for characters
19448 resulting in more than 1 glyph (e.g. tabs): Display as many
19449 glyphs as possible in this line and leave the rest for the
19450 continuation line, or display the whole element in the next
19451 line. Original redisplay did the former, so we do it also. */
19452 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19453 hpos_before = it->hpos;
19454 x_before = x;
19455
19456 if (/* Not a newline. */
19457 nglyphs > 0
19458 /* Glyphs produced fit entirely in the line. */
19459 && it->current_x < it->last_visible_x)
19460 {
19461 it->hpos += nglyphs;
19462 row->ascent = max (row->ascent, it->max_ascent);
19463 row->height = max (row->height, it->max_ascent + it->max_descent);
19464 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19465 row->phys_height = max (row->phys_height,
19466 it->max_phys_ascent + it->max_phys_descent);
19467 row->extra_line_spacing = max (row->extra_line_spacing,
19468 it->max_extra_line_spacing);
19469 if (it->current_x - it->pixel_width < it->first_visible_x)
19470 row->x = x - it->first_visible_x;
19471 /* Record the maximum and minimum buffer positions seen so
19472 far in glyphs that will be displayed by this row. */
19473 if (it->bidi_p)
19474 RECORD_MAX_MIN_POS (it);
19475 }
19476 else
19477 {
19478 int i, new_x;
19479 struct glyph *glyph;
19480
19481 for (i = 0; i < nglyphs; ++i, x = new_x)
19482 {
19483 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19484 new_x = x + glyph->pixel_width;
19485
19486 if (/* Lines are continued. */
19487 it->line_wrap != TRUNCATE
19488 && (/* Glyph doesn't fit on the line. */
19489 new_x > it->last_visible_x
19490 /* Or it fits exactly on a window system frame. */
19491 || (new_x == it->last_visible_x
19492 && FRAME_WINDOW_P (it->f)
19493 && (row->reversed_p
19494 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19495 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19496 {
19497 /* End of a continued line. */
19498
19499 if (it->hpos == 0
19500 || (new_x == it->last_visible_x
19501 && FRAME_WINDOW_P (it->f)
19502 && (row->reversed_p
19503 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19504 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19505 {
19506 /* Current glyph is the only one on the line or
19507 fits exactly on the line. We must continue
19508 the line because we can't draw the cursor
19509 after the glyph. */
19510 row->continued_p = 1;
19511 it->current_x = new_x;
19512 it->continuation_lines_width += new_x;
19513 ++it->hpos;
19514 if (i == nglyphs - 1)
19515 {
19516 /* If line-wrap is on, check if a previous
19517 wrap point was found. */
19518 if (wrap_row_used > 0
19519 /* Even if there is a previous wrap
19520 point, continue the line here as
19521 usual, if (i) the previous character
19522 was a space or tab AND (ii) the
19523 current character is not. */
19524 && (!may_wrap
19525 || IT_DISPLAYING_WHITESPACE (it)))
19526 goto back_to_wrap;
19527
19528 /* Record the maximum and minimum buffer
19529 positions seen so far in glyphs that will be
19530 displayed by this row. */
19531 if (it->bidi_p)
19532 RECORD_MAX_MIN_POS (it);
19533 set_iterator_to_next (it, 1);
19534 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19535 {
19536 if (!get_next_display_element (it))
19537 {
19538 row->exact_window_width_line_p = 1;
19539 it->continuation_lines_width = 0;
19540 row->continued_p = 0;
19541 row->ends_at_zv_p = 1;
19542 }
19543 else if (ITERATOR_AT_END_OF_LINE_P (it))
19544 {
19545 row->continued_p = 0;
19546 row->exact_window_width_line_p = 1;
19547 }
19548 }
19549 }
19550 else if (it->bidi_p)
19551 RECORD_MAX_MIN_POS (it);
19552 }
19553 else if (CHAR_GLYPH_PADDING_P (*glyph)
19554 && !FRAME_WINDOW_P (it->f))
19555 {
19556 /* A padding glyph that doesn't fit on this line.
19557 This means the whole character doesn't fit
19558 on the line. */
19559 if (row->reversed_p)
19560 unproduce_glyphs (it, row->used[TEXT_AREA]
19561 - n_glyphs_before);
19562 row->used[TEXT_AREA] = n_glyphs_before;
19563
19564 /* Fill the rest of the row with continuation
19565 glyphs like in 20.x. */
19566 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19567 < row->glyphs[1 + TEXT_AREA])
19568 produce_special_glyphs (it, IT_CONTINUATION);
19569
19570 row->continued_p = 1;
19571 it->current_x = x_before;
19572 it->continuation_lines_width += x_before;
19573
19574 /* Restore the height to what it was before the
19575 element not fitting on the line. */
19576 it->max_ascent = ascent;
19577 it->max_descent = descent;
19578 it->max_phys_ascent = phys_ascent;
19579 it->max_phys_descent = phys_descent;
19580 }
19581 else if (wrap_row_used > 0)
19582 {
19583 back_to_wrap:
19584 if (row->reversed_p)
19585 unproduce_glyphs (it,
19586 row->used[TEXT_AREA] - wrap_row_used);
19587 RESTORE_IT (it, &wrap_it, wrap_data);
19588 it->continuation_lines_width += wrap_x;
19589 row->used[TEXT_AREA] = wrap_row_used;
19590 row->ascent = wrap_row_ascent;
19591 row->height = wrap_row_height;
19592 row->phys_ascent = wrap_row_phys_ascent;
19593 row->phys_height = wrap_row_phys_height;
19594 row->extra_line_spacing = wrap_row_extra_line_spacing;
19595 min_pos = wrap_row_min_pos;
19596 min_bpos = wrap_row_min_bpos;
19597 max_pos = wrap_row_max_pos;
19598 max_bpos = wrap_row_max_bpos;
19599 row->continued_p = 1;
19600 row->ends_at_zv_p = 0;
19601 row->exact_window_width_line_p = 0;
19602 it->continuation_lines_width += x;
19603
19604 /* Make sure that a non-default face is extended
19605 up to the right margin of the window. */
19606 extend_face_to_end_of_line (it);
19607 }
19608 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19609 {
19610 /* A TAB that extends past the right edge of the
19611 window. This produces a single glyph on
19612 window system frames. We leave the glyph in
19613 this row and let it fill the row, but don't
19614 consume the TAB. */
19615 if ((row->reversed_p
19616 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19617 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19618 produce_special_glyphs (it, IT_CONTINUATION);
19619 it->continuation_lines_width += it->last_visible_x;
19620 row->ends_in_middle_of_char_p = 1;
19621 row->continued_p = 1;
19622 glyph->pixel_width = it->last_visible_x - x;
19623 it->starts_in_middle_of_char_p = 1;
19624 }
19625 else
19626 {
19627 /* Something other than a TAB that draws past
19628 the right edge of the window. Restore
19629 positions to values before the element. */
19630 if (row->reversed_p)
19631 unproduce_glyphs (it, row->used[TEXT_AREA]
19632 - (n_glyphs_before + i));
19633 row->used[TEXT_AREA] = n_glyphs_before + i;
19634
19635 /* Display continuation glyphs. */
19636 it->current_x = x_before;
19637 it->continuation_lines_width += x;
19638 if (!FRAME_WINDOW_P (it->f)
19639 || (row->reversed_p
19640 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19641 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19642 produce_special_glyphs (it, IT_CONTINUATION);
19643 row->continued_p = 1;
19644
19645 extend_face_to_end_of_line (it);
19646
19647 if (nglyphs > 1 && i > 0)
19648 {
19649 row->ends_in_middle_of_char_p = 1;
19650 it->starts_in_middle_of_char_p = 1;
19651 }
19652
19653 /* Restore the height to what it was before the
19654 element not fitting on the line. */
19655 it->max_ascent = ascent;
19656 it->max_descent = descent;
19657 it->max_phys_ascent = phys_ascent;
19658 it->max_phys_descent = phys_descent;
19659 }
19660
19661 break;
19662 }
19663 else if (new_x > it->first_visible_x)
19664 {
19665 /* Increment number of glyphs actually displayed. */
19666 ++it->hpos;
19667
19668 /* Record the maximum and minimum buffer positions
19669 seen so far in glyphs that will be displayed by
19670 this row. */
19671 if (it->bidi_p)
19672 RECORD_MAX_MIN_POS (it);
19673
19674 if (x < it->first_visible_x)
19675 /* Glyph is partially visible, i.e. row starts at
19676 negative X position. */
19677 row->x = x - it->first_visible_x;
19678 }
19679 else
19680 {
19681 /* Glyph is completely off the left margin of the
19682 window. This should not happen because of the
19683 move_it_in_display_line at the start of this
19684 function, unless the text display area of the
19685 window is empty. */
19686 eassert (it->first_visible_x <= it->last_visible_x);
19687 }
19688 }
19689 /* Even if this display element produced no glyphs at all,
19690 we want to record its position. */
19691 if (it->bidi_p && nglyphs == 0)
19692 RECORD_MAX_MIN_POS (it);
19693
19694 row->ascent = max (row->ascent, it->max_ascent);
19695 row->height = max (row->height, it->max_ascent + it->max_descent);
19696 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19697 row->phys_height = max (row->phys_height,
19698 it->max_phys_ascent + it->max_phys_descent);
19699 row->extra_line_spacing = max (row->extra_line_spacing,
19700 it->max_extra_line_spacing);
19701
19702 /* End of this display line if row is continued. */
19703 if (row->continued_p || row->ends_at_zv_p)
19704 break;
19705 }
19706
19707 at_end_of_line:
19708 /* Is this a line end? If yes, we're also done, after making
19709 sure that a non-default face is extended up to the right
19710 margin of the window. */
19711 if (ITERATOR_AT_END_OF_LINE_P (it))
19712 {
19713 int used_before = row->used[TEXT_AREA];
19714
19715 row->ends_in_newline_from_string_p = STRINGP (it->object);
19716
19717 /* Add a space at the end of the line that is used to
19718 display the cursor there. */
19719 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19720 append_space_for_newline (it, 0);
19721
19722 /* Extend the face to the end of the line. */
19723 extend_face_to_end_of_line (it);
19724
19725 /* Make sure we have the position. */
19726 if (used_before == 0)
19727 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19728
19729 /* Record the position of the newline, for use in
19730 find_row_edges. */
19731 it->eol_pos = it->current.pos;
19732
19733 /* Consume the line end. This skips over invisible lines. */
19734 set_iterator_to_next (it, 1);
19735 it->continuation_lines_width = 0;
19736 break;
19737 }
19738
19739 /* Proceed with next display element. Note that this skips
19740 over lines invisible because of selective display. */
19741 set_iterator_to_next (it, 1);
19742
19743 /* If we truncate lines, we are done when the last displayed
19744 glyphs reach past the right margin of the window. */
19745 if (it->line_wrap == TRUNCATE
19746 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19747 ? (it->current_x >= it->last_visible_x)
19748 : (it->current_x > it->last_visible_x)))
19749 {
19750 /* Maybe add truncation glyphs. */
19751 if (!FRAME_WINDOW_P (it->f)
19752 || (row->reversed_p
19753 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19754 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19755 {
19756 int i, n;
19757
19758 if (!row->reversed_p)
19759 {
19760 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19761 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19762 break;
19763 }
19764 else
19765 {
19766 for (i = 0; i < row->used[TEXT_AREA]; i++)
19767 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19768 break;
19769 /* Remove any padding glyphs at the front of ROW, to
19770 make room for the truncation glyphs we will be
19771 adding below. The loop below always inserts at
19772 least one truncation glyph, so also remove the
19773 last glyph added to ROW. */
19774 unproduce_glyphs (it, i + 1);
19775 /* Adjust i for the loop below. */
19776 i = row->used[TEXT_AREA] - (i + 1);
19777 }
19778
19779 it->current_x = x_before;
19780 if (!FRAME_WINDOW_P (it->f))
19781 {
19782 for (n = row->used[TEXT_AREA]; i < n; ++i)
19783 {
19784 row->used[TEXT_AREA] = i;
19785 produce_special_glyphs (it, IT_TRUNCATION);
19786 }
19787 }
19788 else
19789 {
19790 row->used[TEXT_AREA] = i;
19791 produce_special_glyphs (it, IT_TRUNCATION);
19792 }
19793 }
19794 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19795 {
19796 /* Don't truncate if we can overflow newline into fringe. */
19797 if (!get_next_display_element (it))
19798 {
19799 it->continuation_lines_width = 0;
19800 row->ends_at_zv_p = 1;
19801 row->exact_window_width_line_p = 1;
19802 break;
19803 }
19804 if (ITERATOR_AT_END_OF_LINE_P (it))
19805 {
19806 row->exact_window_width_line_p = 1;
19807 goto at_end_of_line;
19808 }
19809 it->current_x = x_before;
19810 }
19811
19812 row->truncated_on_right_p = 1;
19813 it->continuation_lines_width = 0;
19814 reseat_at_next_visible_line_start (it, 0);
19815 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19816 it->hpos = hpos_before;
19817 break;
19818 }
19819 }
19820
19821 if (wrap_data)
19822 bidi_unshelve_cache (wrap_data, 1);
19823
19824 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19825 at the left window margin. */
19826 if (it->first_visible_x
19827 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19828 {
19829 if (!FRAME_WINDOW_P (it->f)
19830 || (row->reversed_p
19831 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19832 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19833 insert_left_trunc_glyphs (it);
19834 row->truncated_on_left_p = 1;
19835 }
19836
19837 /* Remember the position at which this line ends.
19838
19839 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19840 cannot be before the call to find_row_edges below, since that is
19841 where these positions are determined. */
19842 row->end = it->current;
19843 if (!it->bidi_p)
19844 {
19845 row->minpos = row->start.pos;
19846 row->maxpos = row->end.pos;
19847 }
19848 else
19849 {
19850 /* ROW->minpos and ROW->maxpos must be the smallest and
19851 `1 + the largest' buffer positions in ROW. But if ROW was
19852 bidi-reordered, these two positions can be anywhere in the
19853 row, so we must determine them now. */
19854 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19855 }
19856
19857 /* If the start of this line is the overlay arrow-position, then
19858 mark this glyph row as the one containing the overlay arrow.
19859 This is clearly a mess with variable size fonts. It would be
19860 better to let it be displayed like cursors under X. */
19861 if ((row->displays_text_p || !overlay_arrow_seen)
19862 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19863 !NILP (overlay_arrow_string)))
19864 {
19865 /* Overlay arrow in window redisplay is a fringe bitmap. */
19866 if (STRINGP (overlay_arrow_string))
19867 {
19868 struct glyph_row *arrow_row
19869 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19870 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19871 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19872 struct glyph *p = row->glyphs[TEXT_AREA];
19873 struct glyph *p2, *end;
19874
19875 /* Copy the arrow glyphs. */
19876 while (glyph < arrow_end)
19877 *p++ = *glyph++;
19878
19879 /* Throw away padding glyphs. */
19880 p2 = p;
19881 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19882 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19883 ++p2;
19884 if (p2 > p)
19885 {
19886 while (p2 < end)
19887 *p++ = *p2++;
19888 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19889 }
19890 }
19891 else
19892 {
19893 eassert (INTEGERP (overlay_arrow_string));
19894 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19895 }
19896 overlay_arrow_seen = 1;
19897 }
19898
19899 /* Highlight trailing whitespace. */
19900 if (!NILP (Vshow_trailing_whitespace))
19901 highlight_trailing_whitespace (it->f, it->glyph_row);
19902
19903 /* Compute pixel dimensions of this line. */
19904 compute_line_metrics (it);
19905
19906 /* Implementation note: No changes in the glyphs of ROW or in their
19907 faces can be done past this point, because compute_line_metrics
19908 computes ROW's hash value and stores it within the glyph_row
19909 structure. */
19910
19911 /* Record whether this row ends inside an ellipsis. */
19912 row->ends_in_ellipsis_p
19913 = (it->method == GET_FROM_DISPLAY_VECTOR
19914 && it->ellipsis_p);
19915
19916 /* Save fringe bitmaps in this row. */
19917 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19918 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19919 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19920 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19921
19922 it->left_user_fringe_bitmap = 0;
19923 it->left_user_fringe_face_id = 0;
19924 it->right_user_fringe_bitmap = 0;
19925 it->right_user_fringe_face_id = 0;
19926
19927 /* Maybe set the cursor. */
19928 cvpos = it->w->cursor.vpos;
19929 if ((cvpos < 0
19930 /* In bidi-reordered rows, keep checking for proper cursor
19931 position even if one has been found already, because buffer
19932 positions in such rows change non-linearly with ROW->VPOS,
19933 when a line is continued. One exception: when we are at ZV,
19934 display cursor on the first suitable glyph row, since all
19935 the empty rows after that also have their position set to ZV. */
19936 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19937 lines' rows is implemented for bidi-reordered rows. */
19938 || (it->bidi_p
19939 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19940 && PT >= MATRIX_ROW_START_CHARPOS (row)
19941 && PT <= MATRIX_ROW_END_CHARPOS (row)
19942 && cursor_row_p (row))
19943 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19944
19945 /* Prepare for the next line. This line starts horizontally at (X
19946 HPOS) = (0 0). Vertical positions are incremented. As a
19947 convenience for the caller, IT->glyph_row is set to the next
19948 row to be used. */
19949 it->current_x = it->hpos = 0;
19950 it->current_y += row->height;
19951 SET_TEXT_POS (it->eol_pos, 0, 0);
19952 ++it->vpos;
19953 ++it->glyph_row;
19954 /* The next row should by default use the same value of the
19955 reversed_p flag as this one. set_iterator_to_next decides when
19956 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19957 the flag accordingly. */
19958 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19959 it->glyph_row->reversed_p = row->reversed_p;
19960 it->start = row->end;
19961 return row->displays_text_p;
19962
19963 #undef RECORD_MAX_MIN_POS
19964 }
19965
19966 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19967 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19968 doc: /* Return paragraph direction at point in BUFFER.
19969 Value is either `left-to-right' or `right-to-left'.
19970 If BUFFER is omitted or nil, it defaults to the current buffer.
19971
19972 Paragraph direction determines how the text in the paragraph is displayed.
19973 In left-to-right paragraphs, text begins at the left margin of the window
19974 and the reading direction is generally left to right. In right-to-left
19975 paragraphs, text begins at the right margin and is read from right to left.
19976
19977 See also `bidi-paragraph-direction'. */)
19978 (Lisp_Object buffer)
19979 {
19980 struct buffer *buf = current_buffer;
19981 struct buffer *old = buf;
19982
19983 if (! NILP (buffer))
19984 {
19985 CHECK_BUFFER (buffer);
19986 buf = XBUFFER (buffer);
19987 }
19988
19989 if (NILP (BVAR (buf, bidi_display_reordering))
19990 || NILP (BVAR (buf, enable_multibyte_characters))
19991 /* When we are loading loadup.el, the character property tables
19992 needed for bidi iteration are not yet available. */
19993 || !NILP (Vpurify_flag))
19994 return Qleft_to_right;
19995 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19996 return BVAR (buf, bidi_paragraph_direction);
19997 else
19998 {
19999 /* Determine the direction from buffer text. We could try to
20000 use current_matrix if it is up to date, but this seems fast
20001 enough as it is. */
20002 struct bidi_it itb;
20003 ptrdiff_t pos = BUF_PT (buf);
20004 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20005 int c;
20006 void *itb_data = bidi_shelve_cache ();
20007
20008 set_buffer_temp (buf);
20009 /* bidi_paragraph_init finds the base direction of the paragraph
20010 by searching forward from paragraph start. We need the base
20011 direction of the current or _previous_ paragraph, so we need
20012 to make sure we are within that paragraph. To that end, find
20013 the previous non-empty line. */
20014 if (pos >= ZV && pos > BEGV)
20015 {
20016 pos--;
20017 bytepos = CHAR_TO_BYTE (pos);
20018 }
20019 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20020 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20021 {
20022 while ((c = FETCH_BYTE (bytepos)) == '\n'
20023 || c == ' ' || c == '\t' || c == '\f')
20024 {
20025 if (bytepos <= BEGV_BYTE)
20026 break;
20027 bytepos--;
20028 pos--;
20029 }
20030 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20031 bytepos--;
20032 }
20033 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20034 itb.paragraph_dir = NEUTRAL_DIR;
20035 itb.string.s = NULL;
20036 itb.string.lstring = Qnil;
20037 itb.string.bufpos = 0;
20038 itb.string.unibyte = 0;
20039 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20040 bidi_unshelve_cache (itb_data, 0);
20041 set_buffer_temp (old);
20042 switch (itb.paragraph_dir)
20043 {
20044 case L2R:
20045 return Qleft_to_right;
20046 break;
20047 case R2L:
20048 return Qright_to_left;
20049 break;
20050 default:
20051 abort ();
20052 }
20053 }
20054 }
20055
20056
20057 \f
20058 /***********************************************************************
20059 Menu Bar
20060 ***********************************************************************/
20061
20062 /* Redisplay the menu bar in the frame for window W.
20063
20064 The menu bar of X frames that don't have X toolkit support is
20065 displayed in a special window W->frame->menu_bar_window.
20066
20067 The menu bar of terminal frames is treated specially as far as
20068 glyph matrices are concerned. Menu bar lines are not part of
20069 windows, so the update is done directly on the frame matrix rows
20070 for the menu bar. */
20071
20072 static void
20073 display_menu_bar (struct window *w)
20074 {
20075 struct frame *f = XFRAME (WINDOW_FRAME (w));
20076 struct it it;
20077 Lisp_Object items;
20078 int i;
20079
20080 /* Don't do all this for graphical frames. */
20081 #ifdef HAVE_NTGUI
20082 if (FRAME_W32_P (f))
20083 return;
20084 #endif
20085 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20086 if (FRAME_X_P (f))
20087 return;
20088 #endif
20089
20090 #ifdef HAVE_NS
20091 if (FRAME_NS_P (f))
20092 return;
20093 #endif /* HAVE_NS */
20094
20095 #ifdef USE_X_TOOLKIT
20096 eassert (!FRAME_WINDOW_P (f));
20097 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20098 it.first_visible_x = 0;
20099 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20100 #else /* not USE_X_TOOLKIT */
20101 if (FRAME_WINDOW_P (f))
20102 {
20103 /* Menu bar lines are displayed in the desired matrix of the
20104 dummy window menu_bar_window. */
20105 struct window *menu_w;
20106 eassert (WINDOWP (f->menu_bar_window));
20107 menu_w = XWINDOW (f->menu_bar_window);
20108 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20109 MENU_FACE_ID);
20110 it.first_visible_x = 0;
20111 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20112 }
20113 else
20114 {
20115 /* This is a TTY frame, i.e. character hpos/vpos are used as
20116 pixel x/y. */
20117 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20118 MENU_FACE_ID);
20119 it.first_visible_x = 0;
20120 it.last_visible_x = FRAME_COLS (f);
20121 }
20122 #endif /* not USE_X_TOOLKIT */
20123
20124 /* FIXME: This should be controlled by a user option. See the
20125 comments in redisplay_tool_bar and display_mode_line about
20126 this. */
20127 it.paragraph_embedding = L2R;
20128
20129 if (! mode_line_inverse_video)
20130 /* Force the menu-bar to be displayed in the default face. */
20131 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20132
20133 /* Clear all rows of the menu bar. */
20134 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20135 {
20136 struct glyph_row *row = it.glyph_row + i;
20137 clear_glyph_row (row);
20138 row->enabled_p = 1;
20139 row->full_width_p = 1;
20140 }
20141
20142 /* Display all items of the menu bar. */
20143 items = FRAME_MENU_BAR_ITEMS (it.f);
20144 for (i = 0; i < ASIZE (items); i += 4)
20145 {
20146 Lisp_Object string;
20147
20148 /* Stop at nil string. */
20149 string = AREF (items, i + 1);
20150 if (NILP (string))
20151 break;
20152
20153 /* Remember where item was displayed. */
20154 ASET (items, i + 3, make_number (it.hpos));
20155
20156 /* Display the item, pad with one space. */
20157 if (it.current_x < it.last_visible_x)
20158 display_string (NULL, string, Qnil, 0, 0, &it,
20159 SCHARS (string) + 1, 0, 0, -1);
20160 }
20161
20162 /* Fill out the line with spaces. */
20163 if (it.current_x < it.last_visible_x)
20164 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20165
20166 /* Compute the total height of the lines. */
20167 compute_line_metrics (&it);
20168 }
20169
20170
20171 \f
20172 /***********************************************************************
20173 Mode Line
20174 ***********************************************************************/
20175
20176 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20177 FORCE is non-zero, redisplay mode lines unconditionally.
20178 Otherwise, redisplay only mode lines that are garbaged. Value is
20179 the number of windows whose mode lines were redisplayed. */
20180
20181 static int
20182 redisplay_mode_lines (Lisp_Object window, int force)
20183 {
20184 int nwindows = 0;
20185
20186 while (!NILP (window))
20187 {
20188 struct window *w = XWINDOW (window);
20189
20190 if (WINDOWP (WGET (w, hchild)))
20191 nwindows += redisplay_mode_lines (WGET (w, hchild), force);
20192 else if (WINDOWP (WGET (w, vchild)))
20193 nwindows += redisplay_mode_lines (WGET (w, vchild), force);
20194 else if (force
20195 || FRAME_GARBAGED_P (XFRAME (WGET (w, frame)))
20196 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20197 {
20198 struct text_pos lpoint;
20199 struct buffer *old = current_buffer;
20200
20201 /* Set the window's buffer for the mode line display. */
20202 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20203 set_buffer_internal_1 (XBUFFER (WGET (w, buffer)));
20204
20205 /* Point refers normally to the selected window. For any
20206 other window, set up appropriate value. */
20207 if (!EQ (window, selected_window))
20208 {
20209 struct text_pos pt;
20210
20211 SET_TEXT_POS_FROM_MARKER (pt, WGET (w, pointm));
20212 if (CHARPOS (pt) < BEGV)
20213 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20214 else if (CHARPOS (pt) > (ZV - 1))
20215 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20216 else
20217 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20218 }
20219
20220 /* Display mode lines. */
20221 clear_glyph_matrix (w->desired_matrix);
20222 if (display_mode_lines (w))
20223 {
20224 ++nwindows;
20225 w->must_be_updated_p = 1;
20226 }
20227
20228 /* Restore old settings. */
20229 set_buffer_internal_1 (old);
20230 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20231 }
20232
20233 window = WGET (w, next);
20234 }
20235
20236 return nwindows;
20237 }
20238
20239
20240 /* Display the mode and/or header line of window W. Value is the
20241 sum number of mode lines and header lines displayed. */
20242
20243 static int
20244 display_mode_lines (struct window *w)
20245 {
20246 Lisp_Object old_selected_window, old_selected_frame;
20247 int n = 0;
20248
20249 old_selected_frame = selected_frame;
20250 selected_frame = WGET (w, frame);
20251 old_selected_window = selected_window;
20252 XSETWINDOW (selected_window, w);
20253
20254 /* These will be set while the mode line specs are processed. */
20255 line_number_displayed = 0;
20256 WSET (w, column_number_displayed, Qnil);
20257
20258 if (WINDOW_WANTS_MODELINE_P (w))
20259 {
20260 struct window *sel_w = XWINDOW (old_selected_window);
20261
20262 /* Select mode line face based on the real selected window. */
20263 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20264 BVAR (current_buffer, mode_line_format));
20265 ++n;
20266 }
20267
20268 if (WINDOW_WANTS_HEADER_LINE_P (w))
20269 {
20270 display_mode_line (w, HEADER_LINE_FACE_ID,
20271 BVAR (current_buffer, header_line_format));
20272 ++n;
20273 }
20274
20275 selected_frame = old_selected_frame;
20276 selected_window = old_selected_window;
20277 return n;
20278 }
20279
20280
20281 /* Display mode or header line of window W. FACE_ID specifies which
20282 line to display; it is either MODE_LINE_FACE_ID or
20283 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20284 display. Value is the pixel height of the mode/header line
20285 displayed. */
20286
20287 static int
20288 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20289 {
20290 struct it it;
20291 struct face *face;
20292 ptrdiff_t count = SPECPDL_INDEX ();
20293
20294 init_iterator (&it, w, -1, -1, NULL, face_id);
20295 /* Don't extend on a previously drawn mode-line.
20296 This may happen if called from pos_visible_p. */
20297 it.glyph_row->enabled_p = 0;
20298 prepare_desired_row (it.glyph_row);
20299
20300 it.glyph_row->mode_line_p = 1;
20301
20302 if (! mode_line_inverse_video)
20303 /* Force the mode-line to be displayed in the default face. */
20304 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20305
20306 /* FIXME: This should be controlled by a user option. But
20307 supporting such an option is not trivial, since the mode line is
20308 made up of many separate strings. */
20309 it.paragraph_embedding = L2R;
20310
20311 record_unwind_protect (unwind_format_mode_line,
20312 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20313
20314 mode_line_target = MODE_LINE_DISPLAY;
20315
20316 /* Temporarily make frame's keyboard the current kboard so that
20317 kboard-local variables in the mode_line_format will get the right
20318 values. */
20319 push_kboard (FRAME_KBOARD (it.f));
20320 record_unwind_save_match_data ();
20321 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20322 pop_kboard ();
20323
20324 unbind_to (count, Qnil);
20325
20326 /* Fill up with spaces. */
20327 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20328
20329 compute_line_metrics (&it);
20330 it.glyph_row->full_width_p = 1;
20331 it.glyph_row->continued_p = 0;
20332 it.glyph_row->truncated_on_left_p = 0;
20333 it.glyph_row->truncated_on_right_p = 0;
20334
20335 /* Make a 3D mode-line have a shadow at its right end. */
20336 face = FACE_FROM_ID (it.f, face_id);
20337 extend_face_to_end_of_line (&it);
20338 if (face->box != FACE_NO_BOX)
20339 {
20340 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20341 + it.glyph_row->used[TEXT_AREA] - 1);
20342 last->right_box_line_p = 1;
20343 }
20344
20345 return it.glyph_row->height;
20346 }
20347
20348 /* Move element ELT in LIST to the front of LIST.
20349 Return the updated list. */
20350
20351 static Lisp_Object
20352 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20353 {
20354 register Lisp_Object tail, prev;
20355 register Lisp_Object tem;
20356
20357 tail = list;
20358 prev = Qnil;
20359 while (CONSP (tail))
20360 {
20361 tem = XCAR (tail);
20362
20363 if (EQ (elt, tem))
20364 {
20365 /* Splice out the link TAIL. */
20366 if (NILP (prev))
20367 list = XCDR (tail);
20368 else
20369 Fsetcdr (prev, XCDR (tail));
20370
20371 /* Now make it the first. */
20372 Fsetcdr (tail, list);
20373 return tail;
20374 }
20375 else
20376 prev = tail;
20377 tail = XCDR (tail);
20378 QUIT;
20379 }
20380
20381 /* Not found--return unchanged LIST. */
20382 return list;
20383 }
20384
20385 /* Contribute ELT to the mode line for window IT->w. How it
20386 translates into text depends on its data type.
20387
20388 IT describes the display environment in which we display, as usual.
20389
20390 DEPTH is the depth in recursion. It is used to prevent
20391 infinite recursion here.
20392
20393 FIELD_WIDTH is the number of characters the display of ELT should
20394 occupy in the mode line, and PRECISION is the maximum number of
20395 characters to display from ELT's representation. See
20396 display_string for details.
20397
20398 Returns the hpos of the end of the text generated by ELT.
20399
20400 PROPS is a property list to add to any string we encounter.
20401
20402 If RISKY is nonzero, remove (disregard) any properties in any string
20403 we encounter, and ignore :eval and :propertize.
20404
20405 The global variable `mode_line_target' determines whether the
20406 output is passed to `store_mode_line_noprop',
20407 `store_mode_line_string', or `display_string'. */
20408
20409 static int
20410 display_mode_element (struct it *it, int depth, int field_width, int precision,
20411 Lisp_Object elt, Lisp_Object props, int risky)
20412 {
20413 int n = 0, field, prec;
20414 int literal = 0;
20415
20416 tail_recurse:
20417 if (depth > 100)
20418 elt = build_string ("*too-deep*");
20419
20420 depth++;
20421
20422 switch (XTYPE (elt))
20423 {
20424 case Lisp_String:
20425 {
20426 /* A string: output it and check for %-constructs within it. */
20427 unsigned char c;
20428 ptrdiff_t offset = 0;
20429
20430 if (SCHARS (elt) > 0
20431 && (!NILP (props) || risky))
20432 {
20433 Lisp_Object oprops, aelt;
20434 oprops = Ftext_properties_at (make_number (0), elt);
20435
20436 /* If the starting string's properties are not what
20437 we want, translate the string. Also, if the string
20438 is risky, do that anyway. */
20439
20440 if (NILP (Fequal (props, oprops)) || risky)
20441 {
20442 /* If the starting string has properties,
20443 merge the specified ones onto the existing ones. */
20444 if (! NILP (oprops) && !risky)
20445 {
20446 Lisp_Object tem;
20447
20448 oprops = Fcopy_sequence (oprops);
20449 tem = props;
20450 while (CONSP (tem))
20451 {
20452 oprops = Fplist_put (oprops, XCAR (tem),
20453 XCAR (XCDR (tem)));
20454 tem = XCDR (XCDR (tem));
20455 }
20456 props = oprops;
20457 }
20458
20459 aelt = Fassoc (elt, mode_line_proptrans_alist);
20460 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20461 {
20462 /* AELT is what we want. Move it to the front
20463 without consing. */
20464 elt = XCAR (aelt);
20465 mode_line_proptrans_alist
20466 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20467 }
20468 else
20469 {
20470 Lisp_Object tem;
20471
20472 /* If AELT has the wrong props, it is useless.
20473 so get rid of it. */
20474 if (! NILP (aelt))
20475 mode_line_proptrans_alist
20476 = Fdelq (aelt, mode_line_proptrans_alist);
20477
20478 elt = Fcopy_sequence (elt);
20479 Fset_text_properties (make_number (0), Flength (elt),
20480 props, elt);
20481 /* Add this item to mode_line_proptrans_alist. */
20482 mode_line_proptrans_alist
20483 = Fcons (Fcons (elt, props),
20484 mode_line_proptrans_alist);
20485 /* Truncate mode_line_proptrans_alist
20486 to at most 50 elements. */
20487 tem = Fnthcdr (make_number (50),
20488 mode_line_proptrans_alist);
20489 if (! NILP (tem))
20490 XSETCDR (tem, Qnil);
20491 }
20492 }
20493 }
20494
20495 offset = 0;
20496
20497 if (literal)
20498 {
20499 prec = precision - n;
20500 switch (mode_line_target)
20501 {
20502 case MODE_LINE_NOPROP:
20503 case MODE_LINE_TITLE:
20504 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20505 break;
20506 case MODE_LINE_STRING:
20507 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20508 break;
20509 case MODE_LINE_DISPLAY:
20510 n += display_string (NULL, elt, Qnil, 0, 0, it,
20511 0, prec, 0, STRING_MULTIBYTE (elt));
20512 break;
20513 }
20514
20515 break;
20516 }
20517
20518 /* Handle the non-literal case. */
20519
20520 while ((precision <= 0 || n < precision)
20521 && SREF (elt, offset) != 0
20522 && (mode_line_target != MODE_LINE_DISPLAY
20523 || it->current_x < it->last_visible_x))
20524 {
20525 ptrdiff_t last_offset = offset;
20526
20527 /* Advance to end of string or next format specifier. */
20528 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20529 ;
20530
20531 if (offset - 1 != last_offset)
20532 {
20533 ptrdiff_t nchars, nbytes;
20534
20535 /* Output to end of string or up to '%'. Field width
20536 is length of string. Don't output more than
20537 PRECISION allows us. */
20538 offset--;
20539
20540 prec = c_string_width (SDATA (elt) + last_offset,
20541 offset - last_offset, precision - n,
20542 &nchars, &nbytes);
20543
20544 switch (mode_line_target)
20545 {
20546 case MODE_LINE_NOPROP:
20547 case MODE_LINE_TITLE:
20548 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20549 break;
20550 case MODE_LINE_STRING:
20551 {
20552 ptrdiff_t bytepos = last_offset;
20553 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20554 ptrdiff_t endpos = (precision <= 0
20555 ? string_byte_to_char (elt, offset)
20556 : charpos + nchars);
20557
20558 n += store_mode_line_string (NULL,
20559 Fsubstring (elt, make_number (charpos),
20560 make_number (endpos)),
20561 0, 0, 0, Qnil);
20562 }
20563 break;
20564 case MODE_LINE_DISPLAY:
20565 {
20566 ptrdiff_t bytepos = last_offset;
20567 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20568
20569 if (precision <= 0)
20570 nchars = string_byte_to_char (elt, offset) - charpos;
20571 n += display_string (NULL, elt, Qnil, 0, charpos,
20572 it, 0, nchars, 0,
20573 STRING_MULTIBYTE (elt));
20574 }
20575 break;
20576 }
20577 }
20578 else /* c == '%' */
20579 {
20580 ptrdiff_t percent_position = offset;
20581
20582 /* Get the specified minimum width. Zero means
20583 don't pad. */
20584 field = 0;
20585 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20586 field = field * 10 + c - '0';
20587
20588 /* Don't pad beyond the total padding allowed. */
20589 if (field_width - n > 0 && field > field_width - n)
20590 field = field_width - n;
20591
20592 /* Note that either PRECISION <= 0 or N < PRECISION. */
20593 prec = precision - n;
20594
20595 if (c == 'M')
20596 n += display_mode_element (it, depth, field, prec,
20597 Vglobal_mode_string, props,
20598 risky);
20599 else if (c != 0)
20600 {
20601 int multibyte;
20602 ptrdiff_t bytepos, charpos;
20603 const char *spec;
20604 Lisp_Object string;
20605
20606 bytepos = percent_position;
20607 charpos = (STRING_MULTIBYTE (elt)
20608 ? string_byte_to_char (elt, bytepos)
20609 : bytepos);
20610 spec = decode_mode_spec (it->w, c, field, &string);
20611 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20612
20613 switch (mode_line_target)
20614 {
20615 case MODE_LINE_NOPROP:
20616 case MODE_LINE_TITLE:
20617 n += store_mode_line_noprop (spec, field, prec);
20618 break;
20619 case MODE_LINE_STRING:
20620 {
20621 Lisp_Object tem = build_string (spec);
20622 props = Ftext_properties_at (make_number (charpos), elt);
20623 /* Should only keep face property in props */
20624 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20625 }
20626 break;
20627 case MODE_LINE_DISPLAY:
20628 {
20629 int nglyphs_before, nwritten;
20630
20631 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20632 nwritten = display_string (spec, string, elt,
20633 charpos, 0, it,
20634 field, prec, 0,
20635 multibyte);
20636
20637 /* Assign to the glyphs written above the
20638 string where the `%x' came from, position
20639 of the `%'. */
20640 if (nwritten > 0)
20641 {
20642 struct glyph *glyph
20643 = (it->glyph_row->glyphs[TEXT_AREA]
20644 + nglyphs_before);
20645 int i;
20646
20647 for (i = 0; i < nwritten; ++i)
20648 {
20649 glyph[i].object = elt;
20650 glyph[i].charpos = charpos;
20651 }
20652
20653 n += nwritten;
20654 }
20655 }
20656 break;
20657 }
20658 }
20659 else /* c == 0 */
20660 break;
20661 }
20662 }
20663 }
20664 break;
20665
20666 case Lisp_Symbol:
20667 /* A symbol: process the value of the symbol recursively
20668 as if it appeared here directly. Avoid error if symbol void.
20669 Special case: if value of symbol is a string, output the string
20670 literally. */
20671 {
20672 register Lisp_Object tem;
20673
20674 /* If the variable is not marked as risky to set
20675 then its contents are risky to use. */
20676 if (NILP (Fget (elt, Qrisky_local_variable)))
20677 risky = 1;
20678
20679 tem = Fboundp (elt);
20680 if (!NILP (tem))
20681 {
20682 tem = Fsymbol_value (elt);
20683 /* If value is a string, output that string literally:
20684 don't check for % within it. */
20685 if (STRINGP (tem))
20686 literal = 1;
20687
20688 if (!EQ (tem, elt))
20689 {
20690 /* Give up right away for nil or t. */
20691 elt = tem;
20692 goto tail_recurse;
20693 }
20694 }
20695 }
20696 break;
20697
20698 case Lisp_Cons:
20699 {
20700 register Lisp_Object car, tem;
20701
20702 /* A cons cell: five distinct cases.
20703 If first element is :eval or :propertize, do something special.
20704 If first element is a string or a cons, process all the elements
20705 and effectively concatenate them.
20706 If first element is a negative number, truncate displaying cdr to
20707 at most that many characters. If positive, pad (with spaces)
20708 to at least that many characters.
20709 If first element is a symbol, process the cadr or caddr recursively
20710 according to whether the symbol's value is non-nil or nil. */
20711 car = XCAR (elt);
20712 if (EQ (car, QCeval))
20713 {
20714 /* An element of the form (:eval FORM) means evaluate FORM
20715 and use the result as mode line elements. */
20716
20717 if (risky)
20718 break;
20719
20720 if (CONSP (XCDR (elt)))
20721 {
20722 Lisp_Object spec;
20723 spec = safe_eval (XCAR (XCDR (elt)));
20724 n += display_mode_element (it, depth, field_width - n,
20725 precision - n, spec, props,
20726 risky);
20727 }
20728 }
20729 else if (EQ (car, QCpropertize))
20730 {
20731 /* An element of the form (:propertize ELT PROPS...)
20732 means display ELT but applying properties PROPS. */
20733
20734 if (risky)
20735 break;
20736
20737 if (CONSP (XCDR (elt)))
20738 n += display_mode_element (it, depth, field_width - n,
20739 precision - n, XCAR (XCDR (elt)),
20740 XCDR (XCDR (elt)), risky);
20741 }
20742 else if (SYMBOLP (car))
20743 {
20744 tem = Fboundp (car);
20745 elt = XCDR (elt);
20746 if (!CONSP (elt))
20747 goto invalid;
20748 /* elt is now the cdr, and we know it is a cons cell.
20749 Use its car if CAR has a non-nil value. */
20750 if (!NILP (tem))
20751 {
20752 tem = Fsymbol_value (car);
20753 if (!NILP (tem))
20754 {
20755 elt = XCAR (elt);
20756 goto tail_recurse;
20757 }
20758 }
20759 /* Symbol's value is nil (or symbol is unbound)
20760 Get the cddr of the original list
20761 and if possible find the caddr and use that. */
20762 elt = XCDR (elt);
20763 if (NILP (elt))
20764 break;
20765 else if (!CONSP (elt))
20766 goto invalid;
20767 elt = XCAR (elt);
20768 goto tail_recurse;
20769 }
20770 else if (INTEGERP (car))
20771 {
20772 register int lim = XINT (car);
20773 elt = XCDR (elt);
20774 if (lim < 0)
20775 {
20776 /* Negative int means reduce maximum width. */
20777 if (precision <= 0)
20778 precision = -lim;
20779 else
20780 precision = min (precision, -lim);
20781 }
20782 else if (lim > 0)
20783 {
20784 /* Padding specified. Don't let it be more than
20785 current maximum. */
20786 if (precision > 0)
20787 lim = min (precision, lim);
20788
20789 /* If that's more padding than already wanted, queue it.
20790 But don't reduce padding already specified even if
20791 that is beyond the current truncation point. */
20792 field_width = max (lim, field_width);
20793 }
20794 goto tail_recurse;
20795 }
20796 else if (STRINGP (car) || CONSP (car))
20797 {
20798 Lisp_Object halftail = elt;
20799 int len = 0;
20800
20801 while (CONSP (elt)
20802 && (precision <= 0 || n < precision))
20803 {
20804 n += display_mode_element (it, depth,
20805 /* Do padding only after the last
20806 element in the list. */
20807 (! CONSP (XCDR (elt))
20808 ? field_width - n
20809 : 0),
20810 precision - n, XCAR (elt),
20811 props, risky);
20812 elt = XCDR (elt);
20813 len++;
20814 if ((len & 1) == 0)
20815 halftail = XCDR (halftail);
20816 /* Check for cycle. */
20817 if (EQ (halftail, elt))
20818 break;
20819 }
20820 }
20821 }
20822 break;
20823
20824 default:
20825 invalid:
20826 elt = build_string ("*invalid*");
20827 goto tail_recurse;
20828 }
20829
20830 /* Pad to FIELD_WIDTH. */
20831 if (field_width > 0 && n < field_width)
20832 {
20833 switch (mode_line_target)
20834 {
20835 case MODE_LINE_NOPROP:
20836 case MODE_LINE_TITLE:
20837 n += store_mode_line_noprop ("", field_width - n, 0);
20838 break;
20839 case MODE_LINE_STRING:
20840 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20841 break;
20842 case MODE_LINE_DISPLAY:
20843 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20844 0, 0, 0);
20845 break;
20846 }
20847 }
20848
20849 return n;
20850 }
20851
20852 /* Store a mode-line string element in mode_line_string_list.
20853
20854 If STRING is non-null, display that C string. Otherwise, the Lisp
20855 string LISP_STRING is displayed.
20856
20857 FIELD_WIDTH is the minimum number of output glyphs to produce.
20858 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20859 with spaces. FIELD_WIDTH <= 0 means don't pad.
20860
20861 PRECISION is the maximum number of characters to output from
20862 STRING. PRECISION <= 0 means don't truncate the string.
20863
20864 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20865 properties to the string.
20866
20867 PROPS are the properties to add to the string.
20868 The mode_line_string_face face property is always added to the string.
20869 */
20870
20871 static int
20872 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20873 int field_width, int precision, Lisp_Object props)
20874 {
20875 ptrdiff_t len;
20876 int n = 0;
20877
20878 if (string != NULL)
20879 {
20880 len = strlen (string);
20881 if (precision > 0 && len > precision)
20882 len = precision;
20883 lisp_string = make_string (string, len);
20884 if (NILP (props))
20885 props = mode_line_string_face_prop;
20886 else if (!NILP (mode_line_string_face))
20887 {
20888 Lisp_Object face = Fplist_get (props, Qface);
20889 props = Fcopy_sequence (props);
20890 if (NILP (face))
20891 face = mode_line_string_face;
20892 else
20893 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20894 props = Fplist_put (props, Qface, face);
20895 }
20896 Fadd_text_properties (make_number (0), make_number (len),
20897 props, lisp_string);
20898 }
20899 else
20900 {
20901 len = XFASTINT (Flength (lisp_string));
20902 if (precision > 0 && len > precision)
20903 {
20904 len = precision;
20905 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20906 precision = -1;
20907 }
20908 if (!NILP (mode_line_string_face))
20909 {
20910 Lisp_Object face;
20911 if (NILP (props))
20912 props = Ftext_properties_at (make_number (0), lisp_string);
20913 face = Fplist_get (props, Qface);
20914 if (NILP (face))
20915 face = mode_line_string_face;
20916 else
20917 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20918 props = Fcons (Qface, Fcons (face, Qnil));
20919 if (copy_string)
20920 lisp_string = Fcopy_sequence (lisp_string);
20921 }
20922 if (!NILP (props))
20923 Fadd_text_properties (make_number (0), make_number (len),
20924 props, lisp_string);
20925 }
20926
20927 if (len > 0)
20928 {
20929 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20930 n += len;
20931 }
20932
20933 if (field_width > len)
20934 {
20935 field_width -= len;
20936 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20937 if (!NILP (props))
20938 Fadd_text_properties (make_number (0), make_number (field_width),
20939 props, lisp_string);
20940 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20941 n += field_width;
20942 }
20943
20944 return n;
20945 }
20946
20947
20948 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20949 1, 4, 0,
20950 doc: /* Format a string out of a mode line format specification.
20951 First arg FORMAT specifies the mode line format (see `mode-line-format'
20952 for details) to use.
20953
20954 By default, the format is evaluated for the currently selected window.
20955
20956 Optional second arg FACE specifies the face property to put on all
20957 characters for which no face is specified. The value nil means the
20958 default face. The value t means whatever face the window's mode line
20959 currently uses (either `mode-line' or `mode-line-inactive',
20960 depending on whether the window is the selected window or not).
20961 An integer value means the value string has no text
20962 properties.
20963
20964 Optional third and fourth args WINDOW and BUFFER specify the window
20965 and buffer to use as the context for the formatting (defaults
20966 are the selected window and the WINDOW's buffer). */)
20967 (Lisp_Object format, Lisp_Object face,
20968 Lisp_Object window, Lisp_Object buffer)
20969 {
20970 struct it it;
20971 int len;
20972 struct window *w;
20973 struct buffer *old_buffer = NULL;
20974 int face_id;
20975 int no_props = INTEGERP (face);
20976 ptrdiff_t count = SPECPDL_INDEX ();
20977 Lisp_Object str;
20978 int string_start = 0;
20979
20980 if (NILP (window))
20981 window = selected_window;
20982 CHECK_WINDOW (window);
20983 w = XWINDOW (window);
20984
20985 if (NILP (buffer))
20986 buffer = WGET (w, buffer);
20987 CHECK_BUFFER (buffer);
20988
20989 /* Make formatting the modeline a non-op when noninteractive, otherwise
20990 there will be problems later caused by a partially initialized frame. */
20991 if (NILP (format) || noninteractive)
20992 return empty_unibyte_string;
20993
20994 if (no_props)
20995 face = Qnil;
20996
20997 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20998 : EQ (face, Qt) ? (EQ (window, selected_window)
20999 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21000 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21001 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21002 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21003 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21004 : DEFAULT_FACE_ID;
21005
21006 if (XBUFFER (buffer) != current_buffer)
21007 old_buffer = current_buffer;
21008
21009 /* Save things including mode_line_proptrans_alist,
21010 and set that to nil so that we don't alter the outer value. */
21011 record_unwind_protect (unwind_format_mode_line,
21012 format_mode_line_unwind_data
21013 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21014 old_buffer, selected_window, 1));
21015 mode_line_proptrans_alist = Qnil;
21016
21017 Fselect_window (window, Qt);
21018 if (old_buffer)
21019 set_buffer_internal_1 (XBUFFER (buffer));
21020
21021 init_iterator (&it, w, -1, -1, NULL, face_id);
21022
21023 if (no_props)
21024 {
21025 mode_line_target = MODE_LINE_NOPROP;
21026 mode_line_string_face_prop = Qnil;
21027 mode_line_string_list = Qnil;
21028 string_start = MODE_LINE_NOPROP_LEN (0);
21029 }
21030 else
21031 {
21032 mode_line_target = MODE_LINE_STRING;
21033 mode_line_string_list = Qnil;
21034 mode_line_string_face = face;
21035 mode_line_string_face_prop
21036 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21037 }
21038
21039 push_kboard (FRAME_KBOARD (it.f));
21040 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21041 pop_kboard ();
21042
21043 if (no_props)
21044 {
21045 len = MODE_LINE_NOPROP_LEN (string_start);
21046 str = make_string (mode_line_noprop_buf + string_start, len);
21047 }
21048 else
21049 {
21050 mode_line_string_list = Fnreverse (mode_line_string_list);
21051 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21052 empty_unibyte_string);
21053 }
21054
21055 unbind_to (count, Qnil);
21056 return str;
21057 }
21058
21059 /* Write a null-terminated, right justified decimal representation of
21060 the positive integer D to BUF using a minimal field width WIDTH. */
21061
21062 static void
21063 pint2str (register char *buf, register int width, register ptrdiff_t d)
21064 {
21065 register char *p = buf;
21066
21067 if (d <= 0)
21068 *p++ = '0';
21069 else
21070 {
21071 while (d > 0)
21072 {
21073 *p++ = d % 10 + '0';
21074 d /= 10;
21075 }
21076 }
21077
21078 for (width -= (int) (p - buf); width > 0; --width)
21079 *p++ = ' ';
21080 *p-- = '\0';
21081 while (p > buf)
21082 {
21083 d = *buf;
21084 *buf++ = *p;
21085 *p-- = d;
21086 }
21087 }
21088
21089 /* Write a null-terminated, right justified decimal and "human
21090 readable" representation of the nonnegative integer D to BUF using
21091 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21092
21093 static const char power_letter[] =
21094 {
21095 0, /* no letter */
21096 'k', /* kilo */
21097 'M', /* mega */
21098 'G', /* giga */
21099 'T', /* tera */
21100 'P', /* peta */
21101 'E', /* exa */
21102 'Z', /* zetta */
21103 'Y' /* yotta */
21104 };
21105
21106 static void
21107 pint2hrstr (char *buf, int width, ptrdiff_t d)
21108 {
21109 /* We aim to represent the nonnegative integer D as
21110 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21111 ptrdiff_t quotient = d;
21112 int remainder = 0;
21113 /* -1 means: do not use TENTHS. */
21114 int tenths = -1;
21115 int exponent = 0;
21116
21117 /* Length of QUOTIENT.TENTHS as a string. */
21118 int length;
21119
21120 char * psuffix;
21121 char * p;
21122
21123 if (1000 <= quotient)
21124 {
21125 /* Scale to the appropriate EXPONENT. */
21126 do
21127 {
21128 remainder = quotient % 1000;
21129 quotient /= 1000;
21130 exponent++;
21131 }
21132 while (1000 <= quotient);
21133
21134 /* Round to nearest and decide whether to use TENTHS or not. */
21135 if (quotient <= 9)
21136 {
21137 tenths = remainder / 100;
21138 if (50 <= remainder % 100)
21139 {
21140 if (tenths < 9)
21141 tenths++;
21142 else
21143 {
21144 quotient++;
21145 if (quotient == 10)
21146 tenths = -1;
21147 else
21148 tenths = 0;
21149 }
21150 }
21151 }
21152 else
21153 if (500 <= remainder)
21154 {
21155 if (quotient < 999)
21156 quotient++;
21157 else
21158 {
21159 quotient = 1;
21160 exponent++;
21161 tenths = 0;
21162 }
21163 }
21164 }
21165
21166 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21167 if (tenths == -1 && quotient <= 99)
21168 if (quotient <= 9)
21169 length = 1;
21170 else
21171 length = 2;
21172 else
21173 length = 3;
21174 p = psuffix = buf + max (width, length);
21175
21176 /* Print EXPONENT. */
21177 *psuffix++ = power_letter[exponent];
21178 *psuffix = '\0';
21179
21180 /* Print TENTHS. */
21181 if (tenths >= 0)
21182 {
21183 *--p = '0' + tenths;
21184 *--p = '.';
21185 }
21186
21187 /* Print QUOTIENT. */
21188 do
21189 {
21190 int digit = quotient % 10;
21191 *--p = '0' + digit;
21192 }
21193 while ((quotient /= 10) != 0);
21194
21195 /* Print leading spaces. */
21196 while (buf < p)
21197 *--p = ' ';
21198 }
21199
21200 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21201 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21202 type of CODING_SYSTEM. Return updated pointer into BUF. */
21203
21204 static unsigned char invalid_eol_type[] = "(*invalid*)";
21205
21206 static char *
21207 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21208 {
21209 Lisp_Object val;
21210 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21211 const unsigned char *eol_str;
21212 int eol_str_len;
21213 /* The EOL conversion we are using. */
21214 Lisp_Object eoltype;
21215
21216 val = CODING_SYSTEM_SPEC (coding_system);
21217 eoltype = Qnil;
21218
21219 if (!VECTORP (val)) /* Not yet decided. */
21220 {
21221 *buf++ = multibyte ? '-' : ' ';
21222 if (eol_flag)
21223 eoltype = eol_mnemonic_undecided;
21224 /* Don't mention EOL conversion if it isn't decided. */
21225 }
21226 else
21227 {
21228 Lisp_Object attrs;
21229 Lisp_Object eolvalue;
21230
21231 attrs = AREF (val, 0);
21232 eolvalue = AREF (val, 2);
21233
21234 *buf++ = multibyte
21235 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21236 : ' ';
21237
21238 if (eol_flag)
21239 {
21240 /* The EOL conversion that is normal on this system. */
21241
21242 if (NILP (eolvalue)) /* Not yet decided. */
21243 eoltype = eol_mnemonic_undecided;
21244 else if (VECTORP (eolvalue)) /* Not yet decided. */
21245 eoltype = eol_mnemonic_undecided;
21246 else /* eolvalue is Qunix, Qdos, or Qmac. */
21247 eoltype = (EQ (eolvalue, Qunix)
21248 ? eol_mnemonic_unix
21249 : (EQ (eolvalue, Qdos) == 1
21250 ? eol_mnemonic_dos : eol_mnemonic_mac));
21251 }
21252 }
21253
21254 if (eol_flag)
21255 {
21256 /* Mention the EOL conversion if it is not the usual one. */
21257 if (STRINGP (eoltype))
21258 {
21259 eol_str = SDATA (eoltype);
21260 eol_str_len = SBYTES (eoltype);
21261 }
21262 else if (CHARACTERP (eoltype))
21263 {
21264 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21265 int c = XFASTINT (eoltype);
21266 eol_str_len = CHAR_STRING (c, tmp);
21267 eol_str = tmp;
21268 }
21269 else
21270 {
21271 eol_str = invalid_eol_type;
21272 eol_str_len = sizeof (invalid_eol_type) - 1;
21273 }
21274 memcpy (buf, eol_str, eol_str_len);
21275 buf += eol_str_len;
21276 }
21277
21278 return buf;
21279 }
21280
21281 /* Return a string for the output of a mode line %-spec for window W,
21282 generated by character C. FIELD_WIDTH > 0 means pad the string
21283 returned with spaces to that value. Return a Lisp string in
21284 *STRING if the resulting string is taken from that Lisp string.
21285
21286 Note we operate on the current buffer for most purposes,
21287 the exception being w->base_line_pos. */
21288
21289 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21290
21291 static const char *
21292 decode_mode_spec (struct window *w, register int c, int field_width,
21293 Lisp_Object *string)
21294 {
21295 Lisp_Object obj;
21296 struct frame *f = XFRAME (WINDOW_FRAME (w));
21297 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21298 struct buffer *b = current_buffer;
21299
21300 obj = Qnil;
21301 *string = Qnil;
21302
21303 switch (c)
21304 {
21305 case '*':
21306 if (!NILP (BVAR (b, read_only)))
21307 return "%";
21308 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21309 return "*";
21310 return "-";
21311
21312 case '+':
21313 /* This differs from %* only for a modified read-only buffer. */
21314 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21315 return "*";
21316 if (!NILP (BVAR (b, read_only)))
21317 return "%";
21318 return "-";
21319
21320 case '&':
21321 /* This differs from %* in ignoring read-only-ness. */
21322 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21323 return "*";
21324 return "-";
21325
21326 case '%':
21327 return "%";
21328
21329 case '[':
21330 {
21331 int i;
21332 char *p;
21333
21334 if (command_loop_level > 5)
21335 return "[[[... ";
21336 p = decode_mode_spec_buf;
21337 for (i = 0; i < command_loop_level; i++)
21338 *p++ = '[';
21339 *p = 0;
21340 return decode_mode_spec_buf;
21341 }
21342
21343 case ']':
21344 {
21345 int i;
21346 char *p;
21347
21348 if (command_loop_level > 5)
21349 return " ...]]]";
21350 p = decode_mode_spec_buf;
21351 for (i = 0; i < command_loop_level; i++)
21352 *p++ = ']';
21353 *p = 0;
21354 return decode_mode_spec_buf;
21355 }
21356
21357 case '-':
21358 {
21359 register int i;
21360
21361 /* Let lots_of_dashes be a string of infinite length. */
21362 if (mode_line_target == MODE_LINE_NOPROP ||
21363 mode_line_target == MODE_LINE_STRING)
21364 return "--";
21365 if (field_width <= 0
21366 || field_width > sizeof (lots_of_dashes))
21367 {
21368 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21369 decode_mode_spec_buf[i] = '-';
21370 decode_mode_spec_buf[i] = '\0';
21371 return decode_mode_spec_buf;
21372 }
21373 else
21374 return lots_of_dashes;
21375 }
21376
21377 case 'b':
21378 obj = BVAR (b, name);
21379 break;
21380
21381 case 'c':
21382 /* %c and %l are ignored in `frame-title-format'.
21383 (In redisplay_internal, the frame title is drawn _before_ the
21384 windows are updated, so the stuff which depends on actual
21385 window contents (such as %l) may fail to render properly, or
21386 even crash emacs.) */
21387 if (mode_line_target == MODE_LINE_TITLE)
21388 return "";
21389 else
21390 {
21391 ptrdiff_t col = current_column ();
21392 WSET (w, column_number_displayed, make_number (col));
21393 pint2str (decode_mode_spec_buf, field_width, col);
21394 return decode_mode_spec_buf;
21395 }
21396
21397 case 'e':
21398 #ifndef SYSTEM_MALLOC
21399 {
21400 if (NILP (Vmemory_full))
21401 return "";
21402 else
21403 return "!MEM FULL! ";
21404 }
21405 #else
21406 return "";
21407 #endif
21408
21409 case 'F':
21410 /* %F displays the frame name. */
21411 if (!NILP (f->title))
21412 return SSDATA (f->title);
21413 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21414 return SSDATA (f->name);
21415 return "Emacs";
21416
21417 case 'f':
21418 obj = BVAR (b, filename);
21419 break;
21420
21421 case 'i':
21422 {
21423 ptrdiff_t size = ZV - BEGV;
21424 pint2str (decode_mode_spec_buf, field_width, size);
21425 return decode_mode_spec_buf;
21426 }
21427
21428 case 'I':
21429 {
21430 ptrdiff_t size = ZV - BEGV;
21431 pint2hrstr (decode_mode_spec_buf, field_width, size);
21432 return decode_mode_spec_buf;
21433 }
21434
21435 case 'l':
21436 {
21437 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21438 ptrdiff_t topline, nlines, height;
21439 ptrdiff_t junk;
21440
21441 /* %c and %l are ignored in `frame-title-format'. */
21442 if (mode_line_target == MODE_LINE_TITLE)
21443 return "";
21444
21445 startpos = XMARKER (WGET (w, start))->charpos;
21446 startpos_byte = marker_byte_position (WGET (w, start));
21447 height = WINDOW_TOTAL_LINES (w);
21448
21449 /* If we decided that this buffer isn't suitable for line numbers,
21450 don't forget that too fast. */
21451 if (EQ (WGET (w, base_line_pos), WGET (w, buffer)))
21452 goto no_value;
21453 /* But do forget it, if the window shows a different buffer now. */
21454 else if (BUFFERP (WGET (w, base_line_pos)))
21455 WSET (w, base_line_pos, Qnil);
21456
21457 /* If the buffer is very big, don't waste time. */
21458 if (INTEGERP (Vline_number_display_limit)
21459 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21460 {
21461 WSET (w, base_line_pos, Qnil);
21462 WSET (w, base_line_number, Qnil);
21463 goto no_value;
21464 }
21465
21466 if (INTEGERP (WGET (w, base_line_number))
21467 && INTEGERP (WGET (w, base_line_pos))
21468 && XFASTINT (WGET (w, base_line_pos)) <= startpos)
21469 {
21470 line = XFASTINT (WGET (w, base_line_number));
21471 linepos = XFASTINT (WGET (w, base_line_pos));
21472 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21473 }
21474 else
21475 {
21476 line = 1;
21477 linepos = BUF_BEGV (b);
21478 linepos_byte = BUF_BEGV_BYTE (b);
21479 }
21480
21481 /* Count lines from base line to window start position. */
21482 nlines = display_count_lines (linepos_byte,
21483 startpos_byte,
21484 startpos, &junk);
21485
21486 topline = nlines + line;
21487
21488 /* Determine a new base line, if the old one is too close
21489 or too far away, or if we did not have one.
21490 "Too close" means it's plausible a scroll-down would
21491 go back past it. */
21492 if (startpos == BUF_BEGV (b))
21493 {
21494 WSET (w, base_line_number, make_number (topline));
21495 WSET (w, base_line_pos, make_number (BUF_BEGV (b)));
21496 }
21497 else if (nlines < height + 25 || nlines > height * 3 + 50
21498 || linepos == BUF_BEGV (b))
21499 {
21500 ptrdiff_t limit = BUF_BEGV (b);
21501 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21502 ptrdiff_t position;
21503 ptrdiff_t distance =
21504 (height * 2 + 30) * line_number_display_limit_width;
21505
21506 if (startpos - distance > limit)
21507 {
21508 limit = startpos - distance;
21509 limit_byte = CHAR_TO_BYTE (limit);
21510 }
21511
21512 nlines = display_count_lines (startpos_byte,
21513 limit_byte,
21514 - (height * 2 + 30),
21515 &position);
21516 /* If we couldn't find the lines we wanted within
21517 line_number_display_limit_width chars per line,
21518 give up on line numbers for this window. */
21519 if (position == limit_byte && limit == startpos - distance)
21520 {
21521 WSET (w, base_line_pos, WGET (w, buffer));
21522 WSET (w, base_line_number, Qnil);
21523 goto no_value;
21524 }
21525
21526 WSET (w, base_line_number, make_number (topline - nlines));
21527 WSET (w, base_line_pos, make_number (BYTE_TO_CHAR (position)));
21528 }
21529
21530 /* Now count lines from the start pos to point. */
21531 nlines = display_count_lines (startpos_byte,
21532 PT_BYTE, PT, &junk);
21533
21534 /* Record that we did display the line number. */
21535 line_number_displayed = 1;
21536
21537 /* Make the string to show. */
21538 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21539 return decode_mode_spec_buf;
21540 no_value:
21541 {
21542 char* p = decode_mode_spec_buf;
21543 int pad = field_width - 2;
21544 while (pad-- > 0)
21545 *p++ = ' ';
21546 *p++ = '?';
21547 *p++ = '?';
21548 *p = '\0';
21549 return decode_mode_spec_buf;
21550 }
21551 }
21552 break;
21553
21554 case 'm':
21555 obj = BVAR (b, mode_name);
21556 break;
21557
21558 case 'n':
21559 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21560 return " Narrow";
21561 break;
21562
21563 case 'p':
21564 {
21565 ptrdiff_t pos = marker_position (WGET (w, start));
21566 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21567
21568 if (XFASTINT (WGET (w, window_end_pos)) <= BUF_Z (b) - BUF_ZV (b))
21569 {
21570 if (pos <= BUF_BEGV (b))
21571 return "All";
21572 else
21573 return "Bottom";
21574 }
21575 else if (pos <= BUF_BEGV (b))
21576 return "Top";
21577 else
21578 {
21579 if (total > 1000000)
21580 /* Do it differently for a large value, to avoid overflow. */
21581 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21582 else
21583 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21584 /* We can't normally display a 3-digit number,
21585 so get us a 2-digit number that is close. */
21586 if (total == 100)
21587 total = 99;
21588 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21589 return decode_mode_spec_buf;
21590 }
21591 }
21592
21593 /* Display percentage of size above the bottom of the screen. */
21594 case 'P':
21595 {
21596 ptrdiff_t toppos = marker_position (WGET (w, start));
21597 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (WGET (w, window_end_pos));
21598 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21599
21600 if (botpos >= BUF_ZV (b))
21601 {
21602 if (toppos <= BUF_BEGV (b))
21603 return "All";
21604 else
21605 return "Bottom";
21606 }
21607 else
21608 {
21609 if (total > 1000000)
21610 /* Do it differently for a large value, to avoid overflow. */
21611 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21612 else
21613 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21614 /* We can't normally display a 3-digit number,
21615 so get us a 2-digit number that is close. */
21616 if (total == 100)
21617 total = 99;
21618 if (toppos <= BUF_BEGV (b))
21619 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21620 else
21621 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21622 return decode_mode_spec_buf;
21623 }
21624 }
21625
21626 case 's':
21627 /* status of process */
21628 obj = Fget_buffer_process (Fcurrent_buffer ());
21629 if (NILP (obj))
21630 return "no process";
21631 #ifndef MSDOS
21632 obj = Fsymbol_name (Fprocess_status (obj));
21633 #endif
21634 break;
21635
21636 case '@':
21637 {
21638 ptrdiff_t count = inhibit_garbage_collection ();
21639 Lisp_Object val = call1 (intern ("file-remote-p"),
21640 BVAR (current_buffer, directory));
21641 unbind_to (count, Qnil);
21642
21643 if (NILP (val))
21644 return "-";
21645 else
21646 return "@";
21647 }
21648
21649 case 't': /* indicate TEXT or BINARY */
21650 return "T";
21651
21652 case 'z':
21653 /* coding-system (not including end-of-line format) */
21654 case 'Z':
21655 /* coding-system (including end-of-line type) */
21656 {
21657 int eol_flag = (c == 'Z');
21658 char *p = decode_mode_spec_buf;
21659
21660 if (! FRAME_WINDOW_P (f))
21661 {
21662 /* No need to mention EOL here--the terminal never needs
21663 to do EOL conversion. */
21664 p = decode_mode_spec_coding (CODING_ID_NAME
21665 (FRAME_KEYBOARD_CODING (f)->id),
21666 p, 0);
21667 p = decode_mode_spec_coding (CODING_ID_NAME
21668 (FRAME_TERMINAL_CODING (f)->id),
21669 p, 0);
21670 }
21671 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21672 p, eol_flag);
21673
21674 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21675 #ifdef subprocesses
21676 obj = Fget_buffer_process (Fcurrent_buffer ());
21677 if (PROCESSP (obj))
21678 {
21679 p = decode_mode_spec_coding
21680 (PGET (XPROCESS (obj), decode_coding_system), p, eol_flag);
21681 p = decode_mode_spec_coding
21682 (PGET (XPROCESS (obj), encode_coding_system), p, eol_flag);
21683 }
21684 #endif /* subprocesses */
21685 #endif /* 0 */
21686 *p = 0;
21687 return decode_mode_spec_buf;
21688 }
21689 }
21690
21691 if (STRINGP (obj))
21692 {
21693 *string = obj;
21694 return SSDATA (obj);
21695 }
21696 else
21697 return "";
21698 }
21699
21700
21701 /* Count up to COUNT lines starting from START_BYTE.
21702 But don't go beyond LIMIT_BYTE.
21703 Return the number of lines thus found (always nonnegative).
21704
21705 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21706
21707 static ptrdiff_t
21708 display_count_lines (ptrdiff_t start_byte,
21709 ptrdiff_t limit_byte, ptrdiff_t count,
21710 ptrdiff_t *byte_pos_ptr)
21711 {
21712 register unsigned char *cursor;
21713 unsigned char *base;
21714
21715 register ptrdiff_t ceiling;
21716 register unsigned char *ceiling_addr;
21717 ptrdiff_t orig_count = count;
21718
21719 /* If we are not in selective display mode,
21720 check only for newlines. */
21721 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21722 && !INTEGERP (BVAR (current_buffer, selective_display)));
21723
21724 if (count > 0)
21725 {
21726 while (start_byte < limit_byte)
21727 {
21728 ceiling = BUFFER_CEILING_OF (start_byte);
21729 ceiling = min (limit_byte - 1, ceiling);
21730 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21731 base = (cursor = BYTE_POS_ADDR (start_byte));
21732 while (1)
21733 {
21734 if (selective_display)
21735 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21736 ;
21737 else
21738 while (*cursor != '\n' && ++cursor != ceiling_addr)
21739 ;
21740
21741 if (cursor != ceiling_addr)
21742 {
21743 if (--count == 0)
21744 {
21745 start_byte += cursor - base + 1;
21746 *byte_pos_ptr = start_byte;
21747 return orig_count;
21748 }
21749 else
21750 if (++cursor == ceiling_addr)
21751 break;
21752 }
21753 else
21754 break;
21755 }
21756 start_byte += cursor - base;
21757 }
21758 }
21759 else
21760 {
21761 while (start_byte > limit_byte)
21762 {
21763 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21764 ceiling = max (limit_byte, ceiling);
21765 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21766 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21767 while (1)
21768 {
21769 if (selective_display)
21770 while (--cursor != ceiling_addr
21771 && *cursor != '\n' && *cursor != 015)
21772 ;
21773 else
21774 while (--cursor != ceiling_addr && *cursor != '\n')
21775 ;
21776
21777 if (cursor != ceiling_addr)
21778 {
21779 if (++count == 0)
21780 {
21781 start_byte += cursor - base + 1;
21782 *byte_pos_ptr = start_byte;
21783 /* When scanning backwards, we should
21784 not count the newline posterior to which we stop. */
21785 return - orig_count - 1;
21786 }
21787 }
21788 else
21789 break;
21790 }
21791 /* Here we add 1 to compensate for the last decrement
21792 of CURSOR, which took it past the valid range. */
21793 start_byte += cursor - base + 1;
21794 }
21795 }
21796
21797 *byte_pos_ptr = limit_byte;
21798
21799 if (count < 0)
21800 return - orig_count + count;
21801 return orig_count - count;
21802
21803 }
21804
21805
21806 \f
21807 /***********************************************************************
21808 Displaying strings
21809 ***********************************************************************/
21810
21811 /* Display a NUL-terminated string, starting with index START.
21812
21813 If STRING is non-null, display that C string. Otherwise, the Lisp
21814 string LISP_STRING is displayed. There's a case that STRING is
21815 non-null and LISP_STRING is not nil. It means STRING is a string
21816 data of LISP_STRING. In that case, we display LISP_STRING while
21817 ignoring its text properties.
21818
21819 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21820 FACE_STRING. Display STRING or LISP_STRING with the face at
21821 FACE_STRING_POS in FACE_STRING:
21822
21823 Display the string in the environment given by IT, but use the
21824 standard display table, temporarily.
21825
21826 FIELD_WIDTH is the minimum number of output glyphs to produce.
21827 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21828 with spaces. If STRING has more characters, more than FIELD_WIDTH
21829 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21830
21831 PRECISION is the maximum number of characters to output from
21832 STRING. PRECISION < 0 means don't truncate the string.
21833
21834 This is roughly equivalent to printf format specifiers:
21835
21836 FIELD_WIDTH PRECISION PRINTF
21837 ----------------------------------------
21838 -1 -1 %s
21839 -1 10 %.10s
21840 10 -1 %10s
21841 20 10 %20.10s
21842
21843 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21844 display them, and < 0 means obey the current buffer's value of
21845 enable_multibyte_characters.
21846
21847 Value is the number of columns displayed. */
21848
21849 static int
21850 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21851 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21852 int field_width, int precision, int max_x, int multibyte)
21853 {
21854 int hpos_at_start = it->hpos;
21855 int saved_face_id = it->face_id;
21856 struct glyph_row *row = it->glyph_row;
21857 ptrdiff_t it_charpos;
21858
21859 /* Initialize the iterator IT for iteration over STRING beginning
21860 with index START. */
21861 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21862 precision, field_width, multibyte);
21863 if (string && STRINGP (lisp_string))
21864 /* LISP_STRING is the one returned by decode_mode_spec. We should
21865 ignore its text properties. */
21866 it->stop_charpos = it->end_charpos;
21867
21868 /* If displaying STRING, set up the face of the iterator from
21869 FACE_STRING, if that's given. */
21870 if (STRINGP (face_string))
21871 {
21872 ptrdiff_t endptr;
21873 struct face *face;
21874
21875 it->face_id
21876 = face_at_string_position (it->w, face_string, face_string_pos,
21877 0, it->region_beg_charpos,
21878 it->region_end_charpos,
21879 &endptr, it->base_face_id, 0);
21880 face = FACE_FROM_ID (it->f, it->face_id);
21881 it->face_box_p = face->box != FACE_NO_BOX;
21882 }
21883
21884 /* Set max_x to the maximum allowed X position. Don't let it go
21885 beyond the right edge of the window. */
21886 if (max_x <= 0)
21887 max_x = it->last_visible_x;
21888 else
21889 max_x = min (max_x, it->last_visible_x);
21890
21891 /* Skip over display elements that are not visible. because IT->w is
21892 hscrolled. */
21893 if (it->current_x < it->first_visible_x)
21894 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21895 MOVE_TO_POS | MOVE_TO_X);
21896
21897 row->ascent = it->max_ascent;
21898 row->height = it->max_ascent + it->max_descent;
21899 row->phys_ascent = it->max_phys_ascent;
21900 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21901 row->extra_line_spacing = it->max_extra_line_spacing;
21902
21903 if (STRINGP (it->string))
21904 it_charpos = IT_STRING_CHARPOS (*it);
21905 else
21906 it_charpos = IT_CHARPOS (*it);
21907
21908 /* This condition is for the case that we are called with current_x
21909 past last_visible_x. */
21910 while (it->current_x < max_x)
21911 {
21912 int x_before, x, n_glyphs_before, i, nglyphs;
21913
21914 /* Get the next display element. */
21915 if (!get_next_display_element (it))
21916 break;
21917
21918 /* Produce glyphs. */
21919 x_before = it->current_x;
21920 n_glyphs_before = row->used[TEXT_AREA];
21921 PRODUCE_GLYPHS (it);
21922
21923 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21924 i = 0;
21925 x = x_before;
21926 while (i < nglyphs)
21927 {
21928 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21929
21930 if (it->line_wrap != TRUNCATE
21931 && x + glyph->pixel_width > max_x)
21932 {
21933 /* End of continued line or max_x reached. */
21934 if (CHAR_GLYPH_PADDING_P (*glyph))
21935 {
21936 /* A wide character is unbreakable. */
21937 if (row->reversed_p)
21938 unproduce_glyphs (it, row->used[TEXT_AREA]
21939 - n_glyphs_before);
21940 row->used[TEXT_AREA] = n_glyphs_before;
21941 it->current_x = x_before;
21942 }
21943 else
21944 {
21945 if (row->reversed_p)
21946 unproduce_glyphs (it, row->used[TEXT_AREA]
21947 - (n_glyphs_before + i));
21948 row->used[TEXT_AREA] = n_glyphs_before + i;
21949 it->current_x = x;
21950 }
21951 break;
21952 }
21953 else if (x + glyph->pixel_width >= it->first_visible_x)
21954 {
21955 /* Glyph is at least partially visible. */
21956 ++it->hpos;
21957 if (x < it->first_visible_x)
21958 row->x = x - it->first_visible_x;
21959 }
21960 else
21961 {
21962 /* Glyph is off the left margin of the display area.
21963 Should not happen. */
21964 abort ();
21965 }
21966
21967 row->ascent = max (row->ascent, it->max_ascent);
21968 row->height = max (row->height, it->max_ascent + it->max_descent);
21969 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21970 row->phys_height = max (row->phys_height,
21971 it->max_phys_ascent + it->max_phys_descent);
21972 row->extra_line_spacing = max (row->extra_line_spacing,
21973 it->max_extra_line_spacing);
21974 x += glyph->pixel_width;
21975 ++i;
21976 }
21977
21978 /* Stop if max_x reached. */
21979 if (i < nglyphs)
21980 break;
21981
21982 /* Stop at line ends. */
21983 if (ITERATOR_AT_END_OF_LINE_P (it))
21984 {
21985 it->continuation_lines_width = 0;
21986 break;
21987 }
21988
21989 set_iterator_to_next (it, 1);
21990 if (STRINGP (it->string))
21991 it_charpos = IT_STRING_CHARPOS (*it);
21992 else
21993 it_charpos = IT_CHARPOS (*it);
21994
21995 /* Stop if truncating at the right edge. */
21996 if (it->line_wrap == TRUNCATE
21997 && it->current_x >= it->last_visible_x)
21998 {
21999 /* Add truncation mark, but don't do it if the line is
22000 truncated at a padding space. */
22001 if (it_charpos < it->string_nchars)
22002 {
22003 if (!FRAME_WINDOW_P (it->f))
22004 {
22005 int ii, n;
22006
22007 if (it->current_x > it->last_visible_x)
22008 {
22009 if (!row->reversed_p)
22010 {
22011 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22012 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22013 break;
22014 }
22015 else
22016 {
22017 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22018 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22019 break;
22020 unproduce_glyphs (it, ii + 1);
22021 ii = row->used[TEXT_AREA] - (ii + 1);
22022 }
22023 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22024 {
22025 row->used[TEXT_AREA] = ii;
22026 produce_special_glyphs (it, IT_TRUNCATION);
22027 }
22028 }
22029 produce_special_glyphs (it, IT_TRUNCATION);
22030 }
22031 row->truncated_on_right_p = 1;
22032 }
22033 break;
22034 }
22035 }
22036
22037 /* Maybe insert a truncation at the left. */
22038 if (it->first_visible_x
22039 && it_charpos > 0)
22040 {
22041 if (!FRAME_WINDOW_P (it->f)
22042 || (row->reversed_p
22043 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22044 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22045 insert_left_trunc_glyphs (it);
22046 row->truncated_on_left_p = 1;
22047 }
22048
22049 it->face_id = saved_face_id;
22050
22051 /* Value is number of columns displayed. */
22052 return it->hpos - hpos_at_start;
22053 }
22054
22055
22056 \f
22057 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22058 appears as an element of LIST or as the car of an element of LIST.
22059 If PROPVAL is a list, compare each element against LIST in that
22060 way, and return 1/2 if any element of PROPVAL is found in LIST.
22061 Otherwise return 0. This function cannot quit.
22062 The return value is 2 if the text is invisible but with an ellipsis
22063 and 1 if it's invisible and without an ellipsis. */
22064
22065 int
22066 invisible_p (register Lisp_Object propval, Lisp_Object list)
22067 {
22068 register Lisp_Object tail, proptail;
22069
22070 for (tail = list; CONSP (tail); tail = XCDR (tail))
22071 {
22072 register Lisp_Object tem;
22073 tem = XCAR (tail);
22074 if (EQ (propval, tem))
22075 return 1;
22076 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22077 return NILP (XCDR (tem)) ? 1 : 2;
22078 }
22079
22080 if (CONSP (propval))
22081 {
22082 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22083 {
22084 Lisp_Object propelt;
22085 propelt = XCAR (proptail);
22086 for (tail = list; CONSP (tail); tail = XCDR (tail))
22087 {
22088 register Lisp_Object tem;
22089 tem = XCAR (tail);
22090 if (EQ (propelt, tem))
22091 return 1;
22092 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22093 return NILP (XCDR (tem)) ? 1 : 2;
22094 }
22095 }
22096 }
22097
22098 return 0;
22099 }
22100
22101 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22102 doc: /* Non-nil if the property makes the text invisible.
22103 POS-OR-PROP can be a marker or number, in which case it is taken to be
22104 a position in the current buffer and the value of the `invisible' property
22105 is checked; or it can be some other value, which is then presumed to be the
22106 value of the `invisible' property of the text of interest.
22107 The non-nil value returned can be t for truly invisible text or something
22108 else if the text is replaced by an ellipsis. */)
22109 (Lisp_Object pos_or_prop)
22110 {
22111 Lisp_Object prop
22112 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22113 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22114 : pos_or_prop);
22115 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22116 return (invis == 0 ? Qnil
22117 : invis == 1 ? Qt
22118 : make_number (invis));
22119 }
22120
22121 /* Calculate a width or height in pixels from a specification using
22122 the following elements:
22123
22124 SPEC ::=
22125 NUM - a (fractional) multiple of the default font width/height
22126 (NUM) - specifies exactly NUM pixels
22127 UNIT - a fixed number of pixels, see below.
22128 ELEMENT - size of a display element in pixels, see below.
22129 (NUM . SPEC) - equals NUM * SPEC
22130 (+ SPEC SPEC ...) - add pixel values
22131 (- SPEC SPEC ...) - subtract pixel values
22132 (- SPEC) - negate pixel value
22133
22134 NUM ::=
22135 INT or FLOAT - a number constant
22136 SYMBOL - use symbol's (buffer local) variable binding.
22137
22138 UNIT ::=
22139 in - pixels per inch *)
22140 mm - pixels per 1/1000 meter *)
22141 cm - pixels per 1/100 meter *)
22142 width - width of current font in pixels.
22143 height - height of current font in pixels.
22144
22145 *) using the ratio(s) defined in display-pixels-per-inch.
22146
22147 ELEMENT ::=
22148
22149 left-fringe - left fringe width in pixels
22150 right-fringe - right fringe width in pixels
22151
22152 left-margin - left margin width in pixels
22153 right-margin - right margin width in pixels
22154
22155 scroll-bar - scroll-bar area width in pixels
22156
22157 Examples:
22158
22159 Pixels corresponding to 5 inches:
22160 (5 . in)
22161
22162 Total width of non-text areas on left side of window (if scroll-bar is on left):
22163 '(space :width (+ left-fringe left-margin scroll-bar))
22164
22165 Align to first text column (in header line):
22166 '(space :align-to 0)
22167
22168 Align to middle of text area minus half the width of variable `my-image'
22169 containing a loaded image:
22170 '(space :align-to (0.5 . (- text my-image)))
22171
22172 Width of left margin minus width of 1 character in the default font:
22173 '(space :width (- left-margin 1))
22174
22175 Width of left margin minus width of 2 characters in the current font:
22176 '(space :width (- left-margin (2 . width)))
22177
22178 Center 1 character over left-margin (in header line):
22179 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22180
22181 Different ways to express width of left fringe plus left margin minus one pixel:
22182 '(space :width (- (+ left-fringe left-margin) (1)))
22183 '(space :width (+ left-fringe left-margin (- (1))))
22184 '(space :width (+ left-fringe left-margin (-1)))
22185
22186 */
22187
22188 #define NUMVAL(X) \
22189 ((INTEGERP (X) || FLOATP (X)) \
22190 ? XFLOATINT (X) \
22191 : - 1)
22192
22193 static int
22194 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22195 struct font *font, int width_p, int *align_to)
22196 {
22197 double pixels;
22198
22199 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22200 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22201
22202 if (NILP (prop))
22203 return OK_PIXELS (0);
22204
22205 eassert (FRAME_LIVE_P (it->f));
22206
22207 if (SYMBOLP (prop))
22208 {
22209 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22210 {
22211 char *unit = SSDATA (SYMBOL_NAME (prop));
22212
22213 if (unit[0] == 'i' && unit[1] == 'n')
22214 pixels = 1.0;
22215 else if (unit[0] == 'm' && unit[1] == 'm')
22216 pixels = 25.4;
22217 else if (unit[0] == 'c' && unit[1] == 'm')
22218 pixels = 2.54;
22219 else
22220 pixels = 0;
22221 if (pixels > 0)
22222 {
22223 double ppi;
22224 #ifdef HAVE_WINDOW_SYSTEM
22225 if (FRAME_WINDOW_P (it->f)
22226 && (ppi = (width_p
22227 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22228 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22229 ppi > 0))
22230 return OK_PIXELS (ppi / pixels);
22231 #endif
22232
22233 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22234 || (CONSP (Vdisplay_pixels_per_inch)
22235 && (ppi = (width_p
22236 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22237 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22238 ppi > 0)))
22239 return OK_PIXELS (ppi / pixels);
22240
22241 return 0;
22242 }
22243 }
22244
22245 #ifdef HAVE_WINDOW_SYSTEM
22246 if (EQ (prop, Qheight))
22247 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22248 if (EQ (prop, Qwidth))
22249 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22250 #else
22251 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22252 return OK_PIXELS (1);
22253 #endif
22254
22255 if (EQ (prop, Qtext))
22256 return OK_PIXELS (width_p
22257 ? window_box_width (it->w, TEXT_AREA)
22258 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22259
22260 if (align_to && *align_to < 0)
22261 {
22262 *res = 0;
22263 if (EQ (prop, Qleft))
22264 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22265 if (EQ (prop, Qright))
22266 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22267 if (EQ (prop, Qcenter))
22268 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22269 + window_box_width (it->w, TEXT_AREA) / 2);
22270 if (EQ (prop, Qleft_fringe))
22271 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22272 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22273 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22274 if (EQ (prop, Qright_fringe))
22275 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22276 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22277 : window_box_right_offset (it->w, TEXT_AREA));
22278 if (EQ (prop, Qleft_margin))
22279 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22280 if (EQ (prop, Qright_margin))
22281 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22282 if (EQ (prop, Qscroll_bar))
22283 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22284 ? 0
22285 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22286 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22287 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22288 : 0)));
22289 }
22290 else
22291 {
22292 if (EQ (prop, Qleft_fringe))
22293 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22294 if (EQ (prop, Qright_fringe))
22295 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22296 if (EQ (prop, Qleft_margin))
22297 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22298 if (EQ (prop, Qright_margin))
22299 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22300 if (EQ (prop, Qscroll_bar))
22301 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22302 }
22303
22304 prop = buffer_local_value_1 (prop, WGET (it->w, buffer));
22305 if (EQ (prop, Qunbound))
22306 prop = Qnil;
22307 }
22308
22309 if (INTEGERP (prop) || FLOATP (prop))
22310 {
22311 int base_unit = (width_p
22312 ? FRAME_COLUMN_WIDTH (it->f)
22313 : FRAME_LINE_HEIGHT (it->f));
22314 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22315 }
22316
22317 if (CONSP (prop))
22318 {
22319 Lisp_Object car = XCAR (prop);
22320 Lisp_Object cdr = XCDR (prop);
22321
22322 if (SYMBOLP (car))
22323 {
22324 #ifdef HAVE_WINDOW_SYSTEM
22325 if (FRAME_WINDOW_P (it->f)
22326 && valid_image_p (prop))
22327 {
22328 ptrdiff_t id = lookup_image (it->f, prop);
22329 struct image *img = IMAGE_FROM_ID (it->f, id);
22330
22331 return OK_PIXELS (width_p ? img->width : img->height);
22332 }
22333 #endif
22334 if (EQ (car, Qplus) || EQ (car, Qminus))
22335 {
22336 int first = 1;
22337 double px;
22338
22339 pixels = 0;
22340 while (CONSP (cdr))
22341 {
22342 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22343 font, width_p, align_to))
22344 return 0;
22345 if (first)
22346 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22347 else
22348 pixels += px;
22349 cdr = XCDR (cdr);
22350 }
22351 if (EQ (car, Qminus))
22352 pixels = -pixels;
22353 return OK_PIXELS (pixels);
22354 }
22355
22356 car = buffer_local_value_1 (car, WGET (it->w, buffer));
22357 if (EQ (car, Qunbound))
22358 car = Qnil;
22359 }
22360
22361 if (INTEGERP (car) || FLOATP (car))
22362 {
22363 double fact;
22364 pixels = XFLOATINT (car);
22365 if (NILP (cdr))
22366 return OK_PIXELS (pixels);
22367 if (calc_pixel_width_or_height (&fact, it, cdr,
22368 font, width_p, align_to))
22369 return OK_PIXELS (pixels * fact);
22370 return 0;
22371 }
22372
22373 return 0;
22374 }
22375
22376 return 0;
22377 }
22378
22379 \f
22380 /***********************************************************************
22381 Glyph Display
22382 ***********************************************************************/
22383
22384 #ifdef HAVE_WINDOW_SYSTEM
22385
22386 #ifdef GLYPH_DEBUG
22387
22388 void
22389 dump_glyph_string (struct glyph_string *s)
22390 {
22391 fprintf (stderr, "glyph string\n");
22392 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22393 s->x, s->y, s->width, s->height);
22394 fprintf (stderr, " ybase = %d\n", s->ybase);
22395 fprintf (stderr, " hl = %d\n", s->hl);
22396 fprintf (stderr, " left overhang = %d, right = %d\n",
22397 s->left_overhang, s->right_overhang);
22398 fprintf (stderr, " nchars = %d\n", s->nchars);
22399 fprintf (stderr, " extends to end of line = %d\n",
22400 s->extends_to_end_of_line_p);
22401 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22402 fprintf (stderr, " bg width = %d\n", s->background_width);
22403 }
22404
22405 #endif /* GLYPH_DEBUG */
22406
22407 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22408 of XChar2b structures for S; it can't be allocated in
22409 init_glyph_string because it must be allocated via `alloca'. W
22410 is the window on which S is drawn. ROW and AREA are the glyph row
22411 and area within the row from which S is constructed. START is the
22412 index of the first glyph structure covered by S. HL is a
22413 face-override for drawing S. */
22414
22415 #ifdef HAVE_NTGUI
22416 #define OPTIONAL_HDC(hdc) HDC hdc,
22417 #define DECLARE_HDC(hdc) HDC hdc;
22418 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22419 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22420 #endif
22421
22422 #ifndef OPTIONAL_HDC
22423 #define OPTIONAL_HDC(hdc)
22424 #define DECLARE_HDC(hdc)
22425 #define ALLOCATE_HDC(hdc, f)
22426 #define RELEASE_HDC(hdc, f)
22427 #endif
22428
22429 static void
22430 init_glyph_string (struct glyph_string *s,
22431 OPTIONAL_HDC (hdc)
22432 XChar2b *char2b, struct window *w, struct glyph_row *row,
22433 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22434 {
22435 memset (s, 0, sizeof *s);
22436 s->w = w;
22437 s->f = XFRAME (WGET (w, frame));
22438 #ifdef HAVE_NTGUI
22439 s->hdc = hdc;
22440 #endif
22441 s->display = FRAME_X_DISPLAY (s->f);
22442 s->window = FRAME_X_WINDOW (s->f);
22443 s->char2b = char2b;
22444 s->hl = hl;
22445 s->row = row;
22446 s->area = area;
22447 s->first_glyph = row->glyphs[area] + start;
22448 s->height = row->height;
22449 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22450 s->ybase = s->y + row->ascent;
22451 }
22452
22453
22454 /* Append the list of glyph strings with head H and tail T to the list
22455 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22456
22457 static inline void
22458 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22459 struct glyph_string *h, struct glyph_string *t)
22460 {
22461 if (h)
22462 {
22463 if (*head)
22464 (*tail)->next = h;
22465 else
22466 *head = h;
22467 h->prev = *tail;
22468 *tail = t;
22469 }
22470 }
22471
22472
22473 /* Prepend the list of glyph strings with head H and tail T to the
22474 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22475 result. */
22476
22477 static inline void
22478 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22479 struct glyph_string *h, struct glyph_string *t)
22480 {
22481 if (h)
22482 {
22483 if (*head)
22484 (*head)->prev = t;
22485 else
22486 *tail = t;
22487 t->next = *head;
22488 *head = h;
22489 }
22490 }
22491
22492
22493 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22494 Set *HEAD and *TAIL to the resulting list. */
22495
22496 static inline void
22497 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22498 struct glyph_string *s)
22499 {
22500 s->next = s->prev = NULL;
22501 append_glyph_string_lists (head, tail, s, s);
22502 }
22503
22504
22505 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22506 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22507 make sure that X resources for the face returned are allocated.
22508 Value is a pointer to a realized face that is ready for display if
22509 DISPLAY_P is non-zero. */
22510
22511 static inline struct face *
22512 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22513 XChar2b *char2b, int display_p)
22514 {
22515 struct face *face = FACE_FROM_ID (f, face_id);
22516
22517 if (face->font)
22518 {
22519 unsigned code = face->font->driver->encode_char (face->font, c);
22520
22521 if (code != FONT_INVALID_CODE)
22522 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22523 else
22524 STORE_XCHAR2B (char2b, 0, 0);
22525 }
22526
22527 /* Make sure X resources of the face are allocated. */
22528 #ifdef HAVE_X_WINDOWS
22529 if (display_p)
22530 #endif
22531 {
22532 eassert (face != NULL);
22533 PREPARE_FACE_FOR_DISPLAY (f, face);
22534 }
22535
22536 return face;
22537 }
22538
22539
22540 /* Get face and two-byte form of character glyph GLYPH on frame F.
22541 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22542 a pointer to a realized face that is ready for display. */
22543
22544 static inline struct face *
22545 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22546 XChar2b *char2b, int *two_byte_p)
22547 {
22548 struct face *face;
22549
22550 eassert (glyph->type == CHAR_GLYPH);
22551 face = FACE_FROM_ID (f, glyph->face_id);
22552
22553 if (two_byte_p)
22554 *two_byte_p = 0;
22555
22556 if (face->font)
22557 {
22558 unsigned code;
22559
22560 if (CHAR_BYTE8_P (glyph->u.ch))
22561 code = CHAR_TO_BYTE8 (glyph->u.ch);
22562 else
22563 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22564
22565 if (code != FONT_INVALID_CODE)
22566 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22567 else
22568 STORE_XCHAR2B (char2b, 0, 0);
22569 }
22570
22571 /* Make sure X resources of the face are allocated. */
22572 eassert (face != NULL);
22573 PREPARE_FACE_FOR_DISPLAY (f, face);
22574 return face;
22575 }
22576
22577
22578 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22579 Return 1 if FONT has a glyph for C, otherwise return 0. */
22580
22581 static inline int
22582 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22583 {
22584 unsigned code;
22585
22586 if (CHAR_BYTE8_P (c))
22587 code = CHAR_TO_BYTE8 (c);
22588 else
22589 code = font->driver->encode_char (font, c);
22590
22591 if (code == FONT_INVALID_CODE)
22592 return 0;
22593 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22594 return 1;
22595 }
22596
22597
22598 /* Fill glyph string S with composition components specified by S->cmp.
22599
22600 BASE_FACE is the base face of the composition.
22601 S->cmp_from is the index of the first component for S.
22602
22603 OVERLAPS non-zero means S should draw the foreground only, and use
22604 its physical height for clipping. See also draw_glyphs.
22605
22606 Value is the index of a component not in S. */
22607
22608 static int
22609 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22610 int overlaps)
22611 {
22612 int i;
22613 /* For all glyphs of this composition, starting at the offset
22614 S->cmp_from, until we reach the end of the definition or encounter a
22615 glyph that requires the different face, add it to S. */
22616 struct face *face;
22617
22618 eassert (s);
22619
22620 s->for_overlaps = overlaps;
22621 s->face = NULL;
22622 s->font = NULL;
22623 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22624 {
22625 int c = COMPOSITION_GLYPH (s->cmp, i);
22626
22627 /* TAB in a composition means display glyphs with padding space
22628 on the left or right. */
22629 if (c != '\t')
22630 {
22631 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22632 -1, Qnil);
22633
22634 face = get_char_face_and_encoding (s->f, c, face_id,
22635 s->char2b + i, 1);
22636 if (face)
22637 {
22638 if (! s->face)
22639 {
22640 s->face = face;
22641 s->font = s->face->font;
22642 }
22643 else if (s->face != face)
22644 break;
22645 }
22646 }
22647 ++s->nchars;
22648 }
22649 s->cmp_to = i;
22650
22651 if (s->face == NULL)
22652 {
22653 s->face = base_face->ascii_face;
22654 s->font = s->face->font;
22655 }
22656
22657 /* All glyph strings for the same composition has the same width,
22658 i.e. the width set for the first component of the composition. */
22659 s->width = s->first_glyph->pixel_width;
22660
22661 /* If the specified font could not be loaded, use the frame's
22662 default font, but record the fact that we couldn't load it in
22663 the glyph string so that we can draw rectangles for the
22664 characters of the glyph string. */
22665 if (s->font == NULL)
22666 {
22667 s->font_not_found_p = 1;
22668 s->font = FRAME_FONT (s->f);
22669 }
22670
22671 /* Adjust base line for subscript/superscript text. */
22672 s->ybase += s->first_glyph->voffset;
22673
22674 /* This glyph string must always be drawn with 16-bit functions. */
22675 s->two_byte_p = 1;
22676
22677 return s->cmp_to;
22678 }
22679
22680 static int
22681 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22682 int start, int end, int overlaps)
22683 {
22684 struct glyph *glyph, *last;
22685 Lisp_Object lgstring;
22686 int i;
22687
22688 s->for_overlaps = overlaps;
22689 glyph = s->row->glyphs[s->area] + start;
22690 last = s->row->glyphs[s->area] + end;
22691 s->cmp_id = glyph->u.cmp.id;
22692 s->cmp_from = glyph->slice.cmp.from;
22693 s->cmp_to = glyph->slice.cmp.to + 1;
22694 s->face = FACE_FROM_ID (s->f, face_id);
22695 lgstring = composition_gstring_from_id (s->cmp_id);
22696 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22697 glyph++;
22698 while (glyph < last
22699 && glyph->u.cmp.automatic
22700 && glyph->u.cmp.id == s->cmp_id
22701 && s->cmp_to == glyph->slice.cmp.from)
22702 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22703
22704 for (i = s->cmp_from; i < s->cmp_to; i++)
22705 {
22706 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22707 unsigned code = LGLYPH_CODE (lglyph);
22708
22709 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22710 }
22711 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22712 return glyph - s->row->glyphs[s->area];
22713 }
22714
22715
22716 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22717 See the comment of fill_glyph_string for arguments.
22718 Value is the index of the first glyph not in S. */
22719
22720
22721 static int
22722 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22723 int start, int end, int overlaps)
22724 {
22725 struct glyph *glyph, *last;
22726 int voffset;
22727
22728 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22729 s->for_overlaps = overlaps;
22730 glyph = s->row->glyphs[s->area] + start;
22731 last = s->row->glyphs[s->area] + end;
22732 voffset = glyph->voffset;
22733 s->face = FACE_FROM_ID (s->f, face_id);
22734 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22735 s->nchars = 1;
22736 s->width = glyph->pixel_width;
22737 glyph++;
22738 while (glyph < last
22739 && glyph->type == GLYPHLESS_GLYPH
22740 && glyph->voffset == voffset
22741 && glyph->face_id == face_id)
22742 {
22743 s->nchars++;
22744 s->width += glyph->pixel_width;
22745 glyph++;
22746 }
22747 s->ybase += voffset;
22748 return glyph - s->row->glyphs[s->area];
22749 }
22750
22751
22752 /* Fill glyph string S from a sequence of character glyphs.
22753
22754 FACE_ID is the face id of the string. START is the index of the
22755 first glyph to consider, END is the index of the last + 1.
22756 OVERLAPS non-zero means S should draw the foreground only, and use
22757 its physical height for clipping. See also draw_glyphs.
22758
22759 Value is the index of the first glyph not in S. */
22760
22761 static int
22762 fill_glyph_string (struct glyph_string *s, int face_id,
22763 int start, int end, int overlaps)
22764 {
22765 struct glyph *glyph, *last;
22766 int voffset;
22767 int glyph_not_available_p;
22768
22769 eassert (s->f == XFRAME (WGET (s->w, frame)));
22770 eassert (s->nchars == 0);
22771 eassert (start >= 0 && end > start);
22772
22773 s->for_overlaps = overlaps;
22774 glyph = s->row->glyphs[s->area] + start;
22775 last = s->row->glyphs[s->area] + end;
22776 voffset = glyph->voffset;
22777 s->padding_p = glyph->padding_p;
22778 glyph_not_available_p = glyph->glyph_not_available_p;
22779
22780 while (glyph < last
22781 && glyph->type == CHAR_GLYPH
22782 && glyph->voffset == voffset
22783 /* Same face id implies same font, nowadays. */
22784 && glyph->face_id == face_id
22785 && glyph->glyph_not_available_p == glyph_not_available_p)
22786 {
22787 int two_byte_p;
22788
22789 s->face = get_glyph_face_and_encoding (s->f, glyph,
22790 s->char2b + s->nchars,
22791 &two_byte_p);
22792 s->two_byte_p = two_byte_p;
22793 ++s->nchars;
22794 eassert (s->nchars <= end - start);
22795 s->width += glyph->pixel_width;
22796 if (glyph++->padding_p != s->padding_p)
22797 break;
22798 }
22799
22800 s->font = s->face->font;
22801
22802 /* If the specified font could not be loaded, use the frame's font,
22803 but record the fact that we couldn't load it in
22804 S->font_not_found_p so that we can draw rectangles for the
22805 characters of the glyph string. */
22806 if (s->font == NULL || glyph_not_available_p)
22807 {
22808 s->font_not_found_p = 1;
22809 s->font = FRAME_FONT (s->f);
22810 }
22811
22812 /* Adjust base line for subscript/superscript text. */
22813 s->ybase += voffset;
22814
22815 eassert (s->face && s->face->gc);
22816 return glyph - s->row->glyphs[s->area];
22817 }
22818
22819
22820 /* Fill glyph string S from image glyph S->first_glyph. */
22821
22822 static void
22823 fill_image_glyph_string (struct glyph_string *s)
22824 {
22825 eassert (s->first_glyph->type == IMAGE_GLYPH);
22826 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22827 eassert (s->img);
22828 s->slice = s->first_glyph->slice.img;
22829 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22830 s->font = s->face->font;
22831 s->width = s->first_glyph->pixel_width;
22832
22833 /* Adjust base line for subscript/superscript text. */
22834 s->ybase += s->first_glyph->voffset;
22835 }
22836
22837
22838 /* Fill glyph string S from a sequence of stretch glyphs.
22839
22840 START is the index of the first glyph to consider,
22841 END is the index of the last + 1.
22842
22843 Value is the index of the first glyph not in S. */
22844
22845 static int
22846 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22847 {
22848 struct glyph *glyph, *last;
22849 int voffset, face_id;
22850
22851 eassert (s->first_glyph->type == STRETCH_GLYPH);
22852
22853 glyph = s->row->glyphs[s->area] + start;
22854 last = s->row->glyphs[s->area] + end;
22855 face_id = glyph->face_id;
22856 s->face = FACE_FROM_ID (s->f, face_id);
22857 s->font = s->face->font;
22858 s->width = glyph->pixel_width;
22859 s->nchars = 1;
22860 voffset = glyph->voffset;
22861
22862 for (++glyph;
22863 (glyph < last
22864 && glyph->type == STRETCH_GLYPH
22865 && glyph->voffset == voffset
22866 && glyph->face_id == face_id);
22867 ++glyph)
22868 s->width += glyph->pixel_width;
22869
22870 /* Adjust base line for subscript/superscript text. */
22871 s->ybase += voffset;
22872
22873 /* The case that face->gc == 0 is handled when drawing the glyph
22874 string by calling PREPARE_FACE_FOR_DISPLAY. */
22875 eassert (s->face);
22876 return glyph - s->row->glyphs[s->area];
22877 }
22878
22879 static struct font_metrics *
22880 get_per_char_metric (struct font *font, XChar2b *char2b)
22881 {
22882 static struct font_metrics metrics;
22883 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22884
22885 if (! font || code == FONT_INVALID_CODE)
22886 return NULL;
22887 font->driver->text_extents (font, &code, 1, &metrics);
22888 return &metrics;
22889 }
22890
22891 /* EXPORT for RIF:
22892 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22893 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22894 assumed to be zero. */
22895
22896 void
22897 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22898 {
22899 *left = *right = 0;
22900
22901 if (glyph->type == CHAR_GLYPH)
22902 {
22903 struct face *face;
22904 XChar2b char2b;
22905 struct font_metrics *pcm;
22906
22907 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22908 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22909 {
22910 if (pcm->rbearing > pcm->width)
22911 *right = pcm->rbearing - pcm->width;
22912 if (pcm->lbearing < 0)
22913 *left = -pcm->lbearing;
22914 }
22915 }
22916 else if (glyph->type == COMPOSITE_GLYPH)
22917 {
22918 if (! glyph->u.cmp.automatic)
22919 {
22920 struct composition *cmp = composition_table[glyph->u.cmp.id];
22921
22922 if (cmp->rbearing > cmp->pixel_width)
22923 *right = cmp->rbearing - cmp->pixel_width;
22924 if (cmp->lbearing < 0)
22925 *left = - cmp->lbearing;
22926 }
22927 else
22928 {
22929 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22930 struct font_metrics metrics;
22931
22932 composition_gstring_width (gstring, glyph->slice.cmp.from,
22933 glyph->slice.cmp.to + 1, &metrics);
22934 if (metrics.rbearing > metrics.width)
22935 *right = metrics.rbearing - metrics.width;
22936 if (metrics.lbearing < 0)
22937 *left = - metrics.lbearing;
22938 }
22939 }
22940 }
22941
22942
22943 /* Return the index of the first glyph preceding glyph string S that
22944 is overwritten by S because of S's left overhang. Value is -1
22945 if no glyphs are overwritten. */
22946
22947 static int
22948 left_overwritten (struct glyph_string *s)
22949 {
22950 int k;
22951
22952 if (s->left_overhang)
22953 {
22954 int x = 0, i;
22955 struct glyph *glyphs = s->row->glyphs[s->area];
22956 int first = s->first_glyph - glyphs;
22957
22958 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22959 x -= glyphs[i].pixel_width;
22960
22961 k = i + 1;
22962 }
22963 else
22964 k = -1;
22965
22966 return k;
22967 }
22968
22969
22970 /* Return the index of the first glyph preceding glyph string S that
22971 is overwriting S because of its right overhang. Value is -1 if no
22972 glyph in front of S overwrites S. */
22973
22974 static int
22975 left_overwriting (struct glyph_string *s)
22976 {
22977 int i, k, x;
22978 struct glyph *glyphs = s->row->glyphs[s->area];
22979 int first = s->first_glyph - glyphs;
22980
22981 k = -1;
22982 x = 0;
22983 for (i = first - 1; i >= 0; --i)
22984 {
22985 int left, right;
22986 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22987 if (x + right > 0)
22988 k = i;
22989 x -= glyphs[i].pixel_width;
22990 }
22991
22992 return k;
22993 }
22994
22995
22996 /* Return the index of the last glyph following glyph string S that is
22997 overwritten by S because of S's right overhang. Value is -1 if
22998 no such glyph is found. */
22999
23000 static int
23001 right_overwritten (struct glyph_string *s)
23002 {
23003 int k = -1;
23004
23005 if (s->right_overhang)
23006 {
23007 int x = 0, i;
23008 struct glyph *glyphs = s->row->glyphs[s->area];
23009 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23010 int end = s->row->used[s->area];
23011
23012 for (i = first; i < end && s->right_overhang > x; ++i)
23013 x += glyphs[i].pixel_width;
23014
23015 k = i;
23016 }
23017
23018 return k;
23019 }
23020
23021
23022 /* Return the index of the last glyph following glyph string S that
23023 overwrites S because of its left overhang. Value is negative
23024 if no such glyph is found. */
23025
23026 static int
23027 right_overwriting (struct glyph_string *s)
23028 {
23029 int i, k, x;
23030 int end = s->row->used[s->area];
23031 struct glyph *glyphs = s->row->glyphs[s->area];
23032 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23033
23034 k = -1;
23035 x = 0;
23036 for (i = first; i < end; ++i)
23037 {
23038 int left, right;
23039 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23040 if (x - left < 0)
23041 k = i;
23042 x += glyphs[i].pixel_width;
23043 }
23044
23045 return k;
23046 }
23047
23048
23049 /* Set background width of glyph string S. START is the index of the
23050 first glyph following S. LAST_X is the right-most x-position + 1
23051 in the drawing area. */
23052
23053 static inline void
23054 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23055 {
23056 /* If the face of this glyph string has to be drawn to the end of
23057 the drawing area, set S->extends_to_end_of_line_p. */
23058
23059 if (start == s->row->used[s->area]
23060 && s->area == TEXT_AREA
23061 && ((s->row->fill_line_p
23062 && (s->hl == DRAW_NORMAL_TEXT
23063 || s->hl == DRAW_IMAGE_RAISED
23064 || s->hl == DRAW_IMAGE_SUNKEN))
23065 || s->hl == DRAW_MOUSE_FACE))
23066 s->extends_to_end_of_line_p = 1;
23067
23068 /* If S extends its face to the end of the line, set its
23069 background_width to the distance to the right edge of the drawing
23070 area. */
23071 if (s->extends_to_end_of_line_p)
23072 s->background_width = last_x - s->x + 1;
23073 else
23074 s->background_width = s->width;
23075 }
23076
23077
23078 /* Compute overhangs and x-positions for glyph string S and its
23079 predecessors, or successors. X is the starting x-position for S.
23080 BACKWARD_P non-zero means process predecessors. */
23081
23082 static void
23083 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23084 {
23085 if (backward_p)
23086 {
23087 while (s)
23088 {
23089 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23090 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23091 x -= s->width;
23092 s->x = x;
23093 s = s->prev;
23094 }
23095 }
23096 else
23097 {
23098 while (s)
23099 {
23100 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23101 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23102 s->x = x;
23103 x += s->width;
23104 s = s->next;
23105 }
23106 }
23107 }
23108
23109
23110
23111 /* The following macros are only called from draw_glyphs below.
23112 They reference the following parameters of that function directly:
23113 `w', `row', `area', and `overlap_p'
23114 as well as the following local variables:
23115 `s', `f', and `hdc' (in W32) */
23116
23117 #ifdef HAVE_NTGUI
23118 /* On W32, silently add local `hdc' variable to argument list of
23119 init_glyph_string. */
23120 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23121 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23122 #else
23123 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23124 init_glyph_string (s, char2b, w, row, area, start, hl)
23125 #endif
23126
23127 /* Add a glyph string for a stretch glyph to the list of strings
23128 between HEAD and TAIL. START is the index of the stretch glyph in
23129 row area AREA of glyph row ROW. END is the index of the last glyph
23130 in that glyph row area. X is the current output position assigned
23131 to the new glyph string constructed. HL overrides that face of the
23132 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23133 is the right-most x-position of the drawing area. */
23134
23135 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23136 and below -- keep them on one line. */
23137 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23138 do \
23139 { \
23140 s = alloca (sizeof *s); \
23141 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23142 START = fill_stretch_glyph_string (s, START, END); \
23143 append_glyph_string (&HEAD, &TAIL, s); \
23144 s->x = (X); \
23145 } \
23146 while (0)
23147
23148
23149 /* Add a glyph string for an image glyph to the list of strings
23150 between HEAD and TAIL. START is the index of the image glyph in
23151 row area AREA of glyph row ROW. END is the index of the last glyph
23152 in that glyph row area. X is the current output position assigned
23153 to the new glyph string constructed. HL overrides that face of the
23154 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23155 is the right-most x-position of the drawing area. */
23156
23157 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23158 do \
23159 { \
23160 s = alloca (sizeof *s); \
23161 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23162 fill_image_glyph_string (s); \
23163 append_glyph_string (&HEAD, &TAIL, s); \
23164 ++START; \
23165 s->x = (X); \
23166 } \
23167 while (0)
23168
23169
23170 /* Add a glyph string for a sequence of character glyphs to the list
23171 of strings between HEAD and TAIL. START is the index of the first
23172 glyph in row area AREA of glyph row ROW that is part of the new
23173 glyph string. END is the index of the last glyph in that glyph row
23174 area. X is the current output position assigned to the new glyph
23175 string constructed. HL overrides that face of the glyph; e.g. it
23176 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23177 right-most x-position of the drawing area. */
23178
23179 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23180 do \
23181 { \
23182 int face_id; \
23183 XChar2b *char2b; \
23184 \
23185 face_id = (row)->glyphs[area][START].face_id; \
23186 \
23187 s = alloca (sizeof *s); \
23188 char2b = alloca ((END - START) * sizeof *char2b); \
23189 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23190 append_glyph_string (&HEAD, &TAIL, s); \
23191 s->x = (X); \
23192 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23193 } \
23194 while (0)
23195
23196
23197 /* Add a glyph string for a composite sequence to the list of strings
23198 between HEAD and TAIL. START is the index of the first glyph in
23199 row area AREA of glyph row ROW that is part of the new glyph
23200 string. END is the index of the last glyph in that glyph row area.
23201 X is the current output position assigned to the new glyph string
23202 constructed. HL overrides that face of the glyph; e.g. it is
23203 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23204 x-position of the drawing area. */
23205
23206 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23207 do { \
23208 int face_id = (row)->glyphs[area][START].face_id; \
23209 struct face *base_face = FACE_FROM_ID (f, face_id); \
23210 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23211 struct composition *cmp = composition_table[cmp_id]; \
23212 XChar2b *char2b; \
23213 struct glyph_string *first_s = NULL; \
23214 int n; \
23215 \
23216 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23217 \
23218 /* Make glyph_strings for each glyph sequence that is drawable by \
23219 the same face, and append them to HEAD/TAIL. */ \
23220 for (n = 0; n < cmp->glyph_len;) \
23221 { \
23222 s = alloca (sizeof *s); \
23223 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23224 append_glyph_string (&(HEAD), &(TAIL), s); \
23225 s->cmp = cmp; \
23226 s->cmp_from = n; \
23227 s->x = (X); \
23228 if (n == 0) \
23229 first_s = s; \
23230 n = fill_composite_glyph_string (s, base_face, overlaps); \
23231 } \
23232 \
23233 ++START; \
23234 s = first_s; \
23235 } while (0)
23236
23237
23238 /* Add a glyph string for a glyph-string sequence to the list of strings
23239 between HEAD and TAIL. */
23240
23241 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23242 do { \
23243 int face_id; \
23244 XChar2b *char2b; \
23245 Lisp_Object gstring; \
23246 \
23247 face_id = (row)->glyphs[area][START].face_id; \
23248 gstring = (composition_gstring_from_id \
23249 ((row)->glyphs[area][START].u.cmp.id)); \
23250 s = alloca (sizeof *s); \
23251 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23252 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23253 append_glyph_string (&(HEAD), &(TAIL), s); \
23254 s->x = (X); \
23255 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23256 } while (0)
23257
23258
23259 /* Add a glyph string for a sequence of glyphless character's glyphs
23260 to the list of strings between HEAD and TAIL. The meanings of
23261 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23262
23263 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23264 do \
23265 { \
23266 int face_id; \
23267 \
23268 face_id = (row)->glyphs[area][START].face_id; \
23269 \
23270 s = alloca (sizeof *s); \
23271 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23272 append_glyph_string (&HEAD, &TAIL, s); \
23273 s->x = (X); \
23274 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23275 overlaps); \
23276 } \
23277 while (0)
23278
23279
23280 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23281 of AREA of glyph row ROW on window W between indices START and END.
23282 HL overrides the face for drawing glyph strings, e.g. it is
23283 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23284 x-positions of the drawing area.
23285
23286 This is an ugly monster macro construct because we must use alloca
23287 to allocate glyph strings (because draw_glyphs can be called
23288 asynchronously). */
23289
23290 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23291 do \
23292 { \
23293 HEAD = TAIL = NULL; \
23294 while (START < END) \
23295 { \
23296 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23297 switch (first_glyph->type) \
23298 { \
23299 case CHAR_GLYPH: \
23300 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23301 HL, X, LAST_X); \
23302 break; \
23303 \
23304 case COMPOSITE_GLYPH: \
23305 if (first_glyph->u.cmp.automatic) \
23306 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23307 HL, X, LAST_X); \
23308 else \
23309 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23310 HL, X, LAST_X); \
23311 break; \
23312 \
23313 case STRETCH_GLYPH: \
23314 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23315 HL, X, LAST_X); \
23316 break; \
23317 \
23318 case IMAGE_GLYPH: \
23319 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23320 HL, X, LAST_X); \
23321 break; \
23322 \
23323 case GLYPHLESS_GLYPH: \
23324 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23325 HL, X, LAST_X); \
23326 break; \
23327 \
23328 default: \
23329 abort (); \
23330 } \
23331 \
23332 if (s) \
23333 { \
23334 set_glyph_string_background_width (s, START, LAST_X); \
23335 (X) += s->width; \
23336 } \
23337 } \
23338 } while (0)
23339
23340
23341 /* Draw glyphs between START and END in AREA of ROW on window W,
23342 starting at x-position X. X is relative to AREA in W. HL is a
23343 face-override with the following meaning:
23344
23345 DRAW_NORMAL_TEXT draw normally
23346 DRAW_CURSOR draw in cursor face
23347 DRAW_MOUSE_FACE draw in mouse face.
23348 DRAW_INVERSE_VIDEO draw in mode line face
23349 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23350 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23351
23352 If OVERLAPS is non-zero, draw only the foreground of characters and
23353 clip to the physical height of ROW. Non-zero value also defines
23354 the overlapping part to be drawn:
23355
23356 OVERLAPS_PRED overlap with preceding rows
23357 OVERLAPS_SUCC overlap with succeeding rows
23358 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23359 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23360
23361 Value is the x-position reached, relative to AREA of W. */
23362
23363 static int
23364 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23365 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23366 enum draw_glyphs_face hl, int overlaps)
23367 {
23368 struct glyph_string *head, *tail;
23369 struct glyph_string *s;
23370 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23371 int i, j, x_reached, last_x, area_left = 0;
23372 struct frame *f = XFRAME (WINDOW_FRAME (w));
23373 DECLARE_HDC (hdc);
23374
23375 ALLOCATE_HDC (hdc, f);
23376
23377 /* Let's rather be paranoid than getting a SEGV. */
23378 end = min (end, row->used[area]);
23379 start = max (0, start);
23380 start = min (end, start);
23381
23382 /* Translate X to frame coordinates. Set last_x to the right
23383 end of the drawing area. */
23384 if (row->full_width_p)
23385 {
23386 /* X is relative to the left edge of W, without scroll bars
23387 or fringes. */
23388 area_left = WINDOW_LEFT_EDGE_X (w);
23389 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23390 }
23391 else
23392 {
23393 area_left = window_box_left (w, area);
23394 last_x = area_left + window_box_width (w, area);
23395 }
23396 x += area_left;
23397
23398 /* Build a doubly-linked list of glyph_string structures between
23399 head and tail from what we have to draw. Note that the macro
23400 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23401 the reason we use a separate variable `i'. */
23402 i = start;
23403 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23404 if (tail)
23405 x_reached = tail->x + tail->background_width;
23406 else
23407 x_reached = x;
23408
23409 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23410 the row, redraw some glyphs in front or following the glyph
23411 strings built above. */
23412 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23413 {
23414 struct glyph_string *h, *t;
23415 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23416 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23417 int check_mouse_face = 0;
23418 int dummy_x = 0;
23419
23420 /* If mouse highlighting is on, we may need to draw adjacent
23421 glyphs using mouse-face highlighting. */
23422 if (area == TEXT_AREA && row->mouse_face_p)
23423 {
23424 struct glyph_row *mouse_beg_row, *mouse_end_row;
23425
23426 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23427 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23428
23429 if (row >= mouse_beg_row && row <= mouse_end_row)
23430 {
23431 check_mouse_face = 1;
23432 mouse_beg_col = (row == mouse_beg_row)
23433 ? hlinfo->mouse_face_beg_col : 0;
23434 mouse_end_col = (row == mouse_end_row)
23435 ? hlinfo->mouse_face_end_col
23436 : row->used[TEXT_AREA];
23437 }
23438 }
23439
23440 /* Compute overhangs for all glyph strings. */
23441 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23442 for (s = head; s; s = s->next)
23443 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23444
23445 /* Prepend glyph strings for glyphs in front of the first glyph
23446 string that are overwritten because of the first glyph
23447 string's left overhang. The background of all strings
23448 prepended must be drawn because the first glyph string
23449 draws over it. */
23450 i = left_overwritten (head);
23451 if (i >= 0)
23452 {
23453 enum draw_glyphs_face overlap_hl;
23454
23455 /* If this row contains mouse highlighting, attempt to draw
23456 the overlapped glyphs with the correct highlight. This
23457 code fails if the overlap encompasses more than one glyph
23458 and mouse-highlight spans only some of these glyphs.
23459 However, making it work perfectly involves a lot more
23460 code, and I don't know if the pathological case occurs in
23461 practice, so we'll stick to this for now. --- cyd */
23462 if (check_mouse_face
23463 && mouse_beg_col < start && mouse_end_col > i)
23464 overlap_hl = DRAW_MOUSE_FACE;
23465 else
23466 overlap_hl = DRAW_NORMAL_TEXT;
23467
23468 j = i;
23469 BUILD_GLYPH_STRINGS (j, start, h, t,
23470 overlap_hl, dummy_x, last_x);
23471 start = i;
23472 compute_overhangs_and_x (t, head->x, 1);
23473 prepend_glyph_string_lists (&head, &tail, h, t);
23474 clip_head = head;
23475 }
23476
23477 /* Prepend glyph strings for glyphs in front of the first glyph
23478 string that overwrite that glyph string because of their
23479 right overhang. For these strings, only the foreground must
23480 be drawn, because it draws over the glyph string at `head'.
23481 The background must not be drawn because this would overwrite
23482 right overhangs of preceding glyphs for which no glyph
23483 strings exist. */
23484 i = left_overwriting (head);
23485 if (i >= 0)
23486 {
23487 enum draw_glyphs_face overlap_hl;
23488
23489 if (check_mouse_face
23490 && mouse_beg_col < start && mouse_end_col > i)
23491 overlap_hl = DRAW_MOUSE_FACE;
23492 else
23493 overlap_hl = DRAW_NORMAL_TEXT;
23494
23495 clip_head = head;
23496 BUILD_GLYPH_STRINGS (i, start, h, t,
23497 overlap_hl, dummy_x, last_x);
23498 for (s = h; s; s = s->next)
23499 s->background_filled_p = 1;
23500 compute_overhangs_and_x (t, head->x, 1);
23501 prepend_glyph_string_lists (&head, &tail, h, t);
23502 }
23503
23504 /* Append glyphs strings for glyphs following the last glyph
23505 string tail that are overwritten by tail. The background of
23506 these strings has to be drawn because tail's foreground draws
23507 over it. */
23508 i = right_overwritten (tail);
23509 if (i >= 0)
23510 {
23511 enum draw_glyphs_face overlap_hl;
23512
23513 if (check_mouse_face
23514 && mouse_beg_col < i && mouse_end_col > end)
23515 overlap_hl = DRAW_MOUSE_FACE;
23516 else
23517 overlap_hl = DRAW_NORMAL_TEXT;
23518
23519 BUILD_GLYPH_STRINGS (end, i, h, t,
23520 overlap_hl, x, last_x);
23521 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23522 we don't have `end = i;' here. */
23523 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23524 append_glyph_string_lists (&head, &tail, h, t);
23525 clip_tail = tail;
23526 }
23527
23528 /* Append glyph strings for glyphs following the last glyph
23529 string tail that overwrite tail. The foreground of such
23530 glyphs has to be drawn because it writes into the background
23531 of tail. The background must not be drawn because it could
23532 paint over the foreground of following glyphs. */
23533 i = right_overwriting (tail);
23534 if (i >= 0)
23535 {
23536 enum draw_glyphs_face overlap_hl;
23537 if (check_mouse_face
23538 && mouse_beg_col < i && mouse_end_col > end)
23539 overlap_hl = DRAW_MOUSE_FACE;
23540 else
23541 overlap_hl = DRAW_NORMAL_TEXT;
23542
23543 clip_tail = tail;
23544 i++; /* We must include the Ith glyph. */
23545 BUILD_GLYPH_STRINGS (end, i, h, t,
23546 overlap_hl, x, last_x);
23547 for (s = h; s; s = s->next)
23548 s->background_filled_p = 1;
23549 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23550 append_glyph_string_lists (&head, &tail, h, t);
23551 }
23552 if (clip_head || clip_tail)
23553 for (s = head; s; s = s->next)
23554 {
23555 s->clip_head = clip_head;
23556 s->clip_tail = clip_tail;
23557 }
23558 }
23559
23560 /* Draw all strings. */
23561 for (s = head; s; s = s->next)
23562 FRAME_RIF (f)->draw_glyph_string (s);
23563
23564 #ifndef HAVE_NS
23565 /* When focus a sole frame and move horizontally, this sets on_p to 0
23566 causing a failure to erase prev cursor position. */
23567 if (area == TEXT_AREA
23568 && !row->full_width_p
23569 /* When drawing overlapping rows, only the glyph strings'
23570 foreground is drawn, which doesn't erase a cursor
23571 completely. */
23572 && !overlaps)
23573 {
23574 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23575 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23576 : (tail ? tail->x + tail->background_width : x));
23577 x0 -= area_left;
23578 x1 -= area_left;
23579
23580 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23581 row->y, MATRIX_ROW_BOTTOM_Y (row));
23582 }
23583 #endif
23584
23585 /* Value is the x-position up to which drawn, relative to AREA of W.
23586 This doesn't include parts drawn because of overhangs. */
23587 if (row->full_width_p)
23588 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23589 else
23590 x_reached -= area_left;
23591
23592 RELEASE_HDC (hdc, f);
23593
23594 return x_reached;
23595 }
23596
23597 /* Expand row matrix if too narrow. Don't expand if area
23598 is not present. */
23599
23600 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23601 { \
23602 if (!fonts_changed_p \
23603 && (it->glyph_row->glyphs[area] \
23604 < it->glyph_row->glyphs[area + 1])) \
23605 { \
23606 it->w->ncols_scale_factor++; \
23607 fonts_changed_p = 1; \
23608 } \
23609 }
23610
23611 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23612 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23613
23614 static inline void
23615 append_glyph (struct it *it)
23616 {
23617 struct glyph *glyph;
23618 enum glyph_row_area area = it->area;
23619
23620 eassert (it->glyph_row);
23621 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23622
23623 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23624 if (glyph < it->glyph_row->glyphs[area + 1])
23625 {
23626 /* If the glyph row is reversed, we need to prepend the glyph
23627 rather than append it. */
23628 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23629 {
23630 struct glyph *g;
23631
23632 /* Make room for the additional glyph. */
23633 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23634 g[1] = *g;
23635 glyph = it->glyph_row->glyphs[area];
23636 }
23637 glyph->charpos = CHARPOS (it->position);
23638 glyph->object = it->object;
23639 if (it->pixel_width > 0)
23640 {
23641 glyph->pixel_width = it->pixel_width;
23642 glyph->padding_p = 0;
23643 }
23644 else
23645 {
23646 /* Assure at least 1-pixel width. Otherwise, cursor can't
23647 be displayed correctly. */
23648 glyph->pixel_width = 1;
23649 glyph->padding_p = 1;
23650 }
23651 glyph->ascent = it->ascent;
23652 glyph->descent = it->descent;
23653 glyph->voffset = it->voffset;
23654 glyph->type = CHAR_GLYPH;
23655 glyph->avoid_cursor_p = it->avoid_cursor_p;
23656 glyph->multibyte_p = it->multibyte_p;
23657 glyph->left_box_line_p = it->start_of_box_run_p;
23658 glyph->right_box_line_p = it->end_of_box_run_p;
23659 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23660 || it->phys_descent > it->descent);
23661 glyph->glyph_not_available_p = it->glyph_not_available_p;
23662 glyph->face_id = it->face_id;
23663 glyph->u.ch = it->char_to_display;
23664 glyph->slice.img = null_glyph_slice;
23665 glyph->font_type = FONT_TYPE_UNKNOWN;
23666 if (it->bidi_p)
23667 {
23668 glyph->resolved_level = it->bidi_it.resolved_level;
23669 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23670 abort ();
23671 glyph->bidi_type = it->bidi_it.type;
23672 }
23673 else
23674 {
23675 glyph->resolved_level = 0;
23676 glyph->bidi_type = UNKNOWN_BT;
23677 }
23678 ++it->glyph_row->used[area];
23679 }
23680 else
23681 IT_EXPAND_MATRIX_WIDTH (it, area);
23682 }
23683
23684 /* Store one glyph for the composition IT->cmp_it.id in
23685 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23686 non-null. */
23687
23688 static inline void
23689 append_composite_glyph (struct it *it)
23690 {
23691 struct glyph *glyph;
23692 enum glyph_row_area area = it->area;
23693
23694 eassert (it->glyph_row);
23695
23696 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23697 if (glyph < it->glyph_row->glyphs[area + 1])
23698 {
23699 /* If the glyph row is reversed, we need to prepend the glyph
23700 rather than append it. */
23701 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23702 {
23703 struct glyph *g;
23704
23705 /* Make room for the new glyph. */
23706 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23707 g[1] = *g;
23708 glyph = it->glyph_row->glyphs[it->area];
23709 }
23710 glyph->charpos = it->cmp_it.charpos;
23711 glyph->object = it->object;
23712 glyph->pixel_width = it->pixel_width;
23713 glyph->ascent = it->ascent;
23714 glyph->descent = it->descent;
23715 glyph->voffset = it->voffset;
23716 glyph->type = COMPOSITE_GLYPH;
23717 if (it->cmp_it.ch < 0)
23718 {
23719 glyph->u.cmp.automatic = 0;
23720 glyph->u.cmp.id = it->cmp_it.id;
23721 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23722 }
23723 else
23724 {
23725 glyph->u.cmp.automatic = 1;
23726 glyph->u.cmp.id = it->cmp_it.id;
23727 glyph->slice.cmp.from = it->cmp_it.from;
23728 glyph->slice.cmp.to = it->cmp_it.to - 1;
23729 }
23730 glyph->avoid_cursor_p = it->avoid_cursor_p;
23731 glyph->multibyte_p = it->multibyte_p;
23732 glyph->left_box_line_p = it->start_of_box_run_p;
23733 glyph->right_box_line_p = it->end_of_box_run_p;
23734 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23735 || it->phys_descent > it->descent);
23736 glyph->padding_p = 0;
23737 glyph->glyph_not_available_p = 0;
23738 glyph->face_id = it->face_id;
23739 glyph->font_type = FONT_TYPE_UNKNOWN;
23740 if (it->bidi_p)
23741 {
23742 glyph->resolved_level = it->bidi_it.resolved_level;
23743 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23744 abort ();
23745 glyph->bidi_type = it->bidi_it.type;
23746 }
23747 ++it->glyph_row->used[area];
23748 }
23749 else
23750 IT_EXPAND_MATRIX_WIDTH (it, area);
23751 }
23752
23753
23754 /* Change IT->ascent and IT->height according to the setting of
23755 IT->voffset. */
23756
23757 static inline void
23758 take_vertical_position_into_account (struct it *it)
23759 {
23760 if (it->voffset)
23761 {
23762 if (it->voffset < 0)
23763 /* Increase the ascent so that we can display the text higher
23764 in the line. */
23765 it->ascent -= it->voffset;
23766 else
23767 /* Increase the descent so that we can display the text lower
23768 in the line. */
23769 it->descent += it->voffset;
23770 }
23771 }
23772
23773
23774 /* Produce glyphs/get display metrics for the image IT is loaded with.
23775 See the description of struct display_iterator in dispextern.h for
23776 an overview of struct display_iterator. */
23777
23778 static void
23779 produce_image_glyph (struct it *it)
23780 {
23781 struct image *img;
23782 struct face *face;
23783 int glyph_ascent, crop;
23784 struct glyph_slice slice;
23785
23786 eassert (it->what == IT_IMAGE);
23787
23788 face = FACE_FROM_ID (it->f, it->face_id);
23789 eassert (face);
23790 /* Make sure X resources of the face is loaded. */
23791 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23792
23793 if (it->image_id < 0)
23794 {
23795 /* Fringe bitmap. */
23796 it->ascent = it->phys_ascent = 0;
23797 it->descent = it->phys_descent = 0;
23798 it->pixel_width = 0;
23799 it->nglyphs = 0;
23800 return;
23801 }
23802
23803 img = IMAGE_FROM_ID (it->f, it->image_id);
23804 eassert (img);
23805 /* Make sure X resources of the image is loaded. */
23806 prepare_image_for_display (it->f, img);
23807
23808 slice.x = slice.y = 0;
23809 slice.width = img->width;
23810 slice.height = img->height;
23811
23812 if (INTEGERP (it->slice.x))
23813 slice.x = XINT (it->slice.x);
23814 else if (FLOATP (it->slice.x))
23815 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23816
23817 if (INTEGERP (it->slice.y))
23818 slice.y = XINT (it->slice.y);
23819 else if (FLOATP (it->slice.y))
23820 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23821
23822 if (INTEGERP (it->slice.width))
23823 slice.width = XINT (it->slice.width);
23824 else if (FLOATP (it->slice.width))
23825 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23826
23827 if (INTEGERP (it->slice.height))
23828 slice.height = XINT (it->slice.height);
23829 else if (FLOATP (it->slice.height))
23830 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23831
23832 if (slice.x >= img->width)
23833 slice.x = img->width;
23834 if (slice.y >= img->height)
23835 slice.y = img->height;
23836 if (slice.x + slice.width >= img->width)
23837 slice.width = img->width - slice.x;
23838 if (slice.y + slice.height > img->height)
23839 slice.height = img->height - slice.y;
23840
23841 if (slice.width == 0 || slice.height == 0)
23842 return;
23843
23844 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23845
23846 it->descent = slice.height - glyph_ascent;
23847 if (slice.y == 0)
23848 it->descent += img->vmargin;
23849 if (slice.y + slice.height == img->height)
23850 it->descent += img->vmargin;
23851 it->phys_descent = it->descent;
23852
23853 it->pixel_width = slice.width;
23854 if (slice.x == 0)
23855 it->pixel_width += img->hmargin;
23856 if (slice.x + slice.width == img->width)
23857 it->pixel_width += img->hmargin;
23858
23859 /* It's quite possible for images to have an ascent greater than
23860 their height, so don't get confused in that case. */
23861 if (it->descent < 0)
23862 it->descent = 0;
23863
23864 it->nglyphs = 1;
23865
23866 if (face->box != FACE_NO_BOX)
23867 {
23868 if (face->box_line_width > 0)
23869 {
23870 if (slice.y == 0)
23871 it->ascent += face->box_line_width;
23872 if (slice.y + slice.height == img->height)
23873 it->descent += face->box_line_width;
23874 }
23875
23876 if (it->start_of_box_run_p && slice.x == 0)
23877 it->pixel_width += eabs (face->box_line_width);
23878 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23879 it->pixel_width += eabs (face->box_line_width);
23880 }
23881
23882 take_vertical_position_into_account (it);
23883
23884 /* Automatically crop wide image glyphs at right edge so we can
23885 draw the cursor on same display row. */
23886 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23887 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23888 {
23889 it->pixel_width -= crop;
23890 slice.width -= crop;
23891 }
23892
23893 if (it->glyph_row)
23894 {
23895 struct glyph *glyph;
23896 enum glyph_row_area area = it->area;
23897
23898 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23899 if (glyph < it->glyph_row->glyphs[area + 1])
23900 {
23901 glyph->charpos = CHARPOS (it->position);
23902 glyph->object = it->object;
23903 glyph->pixel_width = it->pixel_width;
23904 glyph->ascent = glyph_ascent;
23905 glyph->descent = it->descent;
23906 glyph->voffset = it->voffset;
23907 glyph->type = IMAGE_GLYPH;
23908 glyph->avoid_cursor_p = it->avoid_cursor_p;
23909 glyph->multibyte_p = it->multibyte_p;
23910 glyph->left_box_line_p = it->start_of_box_run_p;
23911 glyph->right_box_line_p = it->end_of_box_run_p;
23912 glyph->overlaps_vertically_p = 0;
23913 glyph->padding_p = 0;
23914 glyph->glyph_not_available_p = 0;
23915 glyph->face_id = it->face_id;
23916 glyph->u.img_id = img->id;
23917 glyph->slice.img = slice;
23918 glyph->font_type = FONT_TYPE_UNKNOWN;
23919 if (it->bidi_p)
23920 {
23921 glyph->resolved_level = it->bidi_it.resolved_level;
23922 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23923 abort ();
23924 glyph->bidi_type = it->bidi_it.type;
23925 }
23926 ++it->glyph_row->used[area];
23927 }
23928 else
23929 IT_EXPAND_MATRIX_WIDTH (it, area);
23930 }
23931 }
23932
23933
23934 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23935 of the glyph, WIDTH and HEIGHT are the width and height of the
23936 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23937
23938 static void
23939 append_stretch_glyph (struct it *it, Lisp_Object object,
23940 int width, int height, int ascent)
23941 {
23942 struct glyph *glyph;
23943 enum glyph_row_area area = it->area;
23944
23945 eassert (ascent >= 0 && ascent <= height);
23946
23947 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23948 if (glyph < it->glyph_row->glyphs[area + 1])
23949 {
23950 /* If the glyph row is reversed, we need to prepend the glyph
23951 rather than append it. */
23952 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23953 {
23954 struct glyph *g;
23955
23956 /* Make room for the additional glyph. */
23957 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23958 g[1] = *g;
23959 glyph = it->glyph_row->glyphs[area];
23960 }
23961 glyph->charpos = CHARPOS (it->position);
23962 glyph->object = object;
23963 glyph->pixel_width = width;
23964 glyph->ascent = ascent;
23965 glyph->descent = height - ascent;
23966 glyph->voffset = it->voffset;
23967 glyph->type = STRETCH_GLYPH;
23968 glyph->avoid_cursor_p = it->avoid_cursor_p;
23969 glyph->multibyte_p = it->multibyte_p;
23970 glyph->left_box_line_p = it->start_of_box_run_p;
23971 glyph->right_box_line_p = it->end_of_box_run_p;
23972 glyph->overlaps_vertically_p = 0;
23973 glyph->padding_p = 0;
23974 glyph->glyph_not_available_p = 0;
23975 glyph->face_id = it->face_id;
23976 glyph->u.stretch.ascent = ascent;
23977 glyph->u.stretch.height = height;
23978 glyph->slice.img = null_glyph_slice;
23979 glyph->font_type = FONT_TYPE_UNKNOWN;
23980 if (it->bidi_p)
23981 {
23982 glyph->resolved_level = it->bidi_it.resolved_level;
23983 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23984 abort ();
23985 glyph->bidi_type = it->bidi_it.type;
23986 }
23987 else
23988 {
23989 glyph->resolved_level = 0;
23990 glyph->bidi_type = UNKNOWN_BT;
23991 }
23992 ++it->glyph_row->used[area];
23993 }
23994 else
23995 IT_EXPAND_MATRIX_WIDTH (it, area);
23996 }
23997
23998 #endif /* HAVE_WINDOW_SYSTEM */
23999
24000 /* Produce a stretch glyph for iterator IT. IT->object is the value
24001 of the glyph property displayed. The value must be a list
24002 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24003 being recognized:
24004
24005 1. `:width WIDTH' specifies that the space should be WIDTH *
24006 canonical char width wide. WIDTH may be an integer or floating
24007 point number.
24008
24009 2. `:relative-width FACTOR' specifies that the width of the stretch
24010 should be computed from the width of the first character having the
24011 `glyph' property, and should be FACTOR times that width.
24012
24013 3. `:align-to HPOS' specifies that the space should be wide enough
24014 to reach HPOS, a value in canonical character units.
24015
24016 Exactly one of the above pairs must be present.
24017
24018 4. `:height HEIGHT' specifies that the height of the stretch produced
24019 should be HEIGHT, measured in canonical character units.
24020
24021 5. `:relative-height FACTOR' specifies that the height of the
24022 stretch should be FACTOR times the height of the characters having
24023 the glyph property.
24024
24025 Either none or exactly one of 4 or 5 must be present.
24026
24027 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24028 of the stretch should be used for the ascent of the stretch.
24029 ASCENT must be in the range 0 <= ASCENT <= 100. */
24030
24031 void
24032 produce_stretch_glyph (struct it *it)
24033 {
24034 /* (space :width WIDTH :height HEIGHT ...) */
24035 Lisp_Object prop, plist;
24036 int width = 0, height = 0, align_to = -1;
24037 int zero_width_ok_p = 0;
24038 int ascent = 0;
24039 double tem;
24040 struct face *face = NULL;
24041 struct font *font = NULL;
24042
24043 #ifdef HAVE_WINDOW_SYSTEM
24044 int zero_height_ok_p = 0;
24045
24046 if (FRAME_WINDOW_P (it->f))
24047 {
24048 face = FACE_FROM_ID (it->f, it->face_id);
24049 font = face->font ? face->font : FRAME_FONT (it->f);
24050 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24051 }
24052 #endif
24053
24054 /* List should start with `space'. */
24055 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24056 plist = XCDR (it->object);
24057
24058 /* Compute the width of the stretch. */
24059 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24060 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24061 {
24062 /* Absolute width `:width WIDTH' specified and valid. */
24063 zero_width_ok_p = 1;
24064 width = (int)tem;
24065 }
24066 #ifdef HAVE_WINDOW_SYSTEM
24067 else if (FRAME_WINDOW_P (it->f)
24068 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24069 {
24070 /* Relative width `:relative-width FACTOR' specified and valid.
24071 Compute the width of the characters having the `glyph'
24072 property. */
24073 struct it it2;
24074 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24075
24076 it2 = *it;
24077 if (it->multibyte_p)
24078 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24079 else
24080 {
24081 it2.c = it2.char_to_display = *p, it2.len = 1;
24082 if (! ASCII_CHAR_P (it2.c))
24083 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24084 }
24085
24086 it2.glyph_row = NULL;
24087 it2.what = IT_CHARACTER;
24088 x_produce_glyphs (&it2);
24089 width = NUMVAL (prop) * it2.pixel_width;
24090 }
24091 #endif /* HAVE_WINDOW_SYSTEM */
24092 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24093 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24094 {
24095 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24096 align_to = (align_to < 0
24097 ? 0
24098 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24099 else if (align_to < 0)
24100 align_to = window_box_left_offset (it->w, TEXT_AREA);
24101 width = max (0, (int)tem + align_to - it->current_x);
24102 zero_width_ok_p = 1;
24103 }
24104 else
24105 /* Nothing specified -> width defaults to canonical char width. */
24106 width = FRAME_COLUMN_WIDTH (it->f);
24107
24108 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24109 width = 1;
24110
24111 #ifdef HAVE_WINDOW_SYSTEM
24112 /* Compute height. */
24113 if (FRAME_WINDOW_P (it->f))
24114 {
24115 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24116 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24117 {
24118 height = (int)tem;
24119 zero_height_ok_p = 1;
24120 }
24121 else if (prop = Fplist_get (plist, QCrelative_height),
24122 NUMVAL (prop) > 0)
24123 height = FONT_HEIGHT (font) * NUMVAL (prop);
24124 else
24125 height = FONT_HEIGHT (font);
24126
24127 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24128 height = 1;
24129
24130 /* Compute percentage of height used for ascent. If
24131 `:ascent ASCENT' is present and valid, use that. Otherwise,
24132 derive the ascent from the font in use. */
24133 if (prop = Fplist_get (plist, QCascent),
24134 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24135 ascent = height * NUMVAL (prop) / 100.0;
24136 else if (!NILP (prop)
24137 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24138 ascent = min (max (0, (int)tem), height);
24139 else
24140 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24141 }
24142 else
24143 #endif /* HAVE_WINDOW_SYSTEM */
24144 height = 1;
24145
24146 if (width > 0 && it->line_wrap != TRUNCATE
24147 && it->current_x + width > it->last_visible_x)
24148 {
24149 width = it->last_visible_x - it->current_x;
24150 #ifdef HAVE_WINDOW_SYSTEM
24151 /* Subtract one more pixel from the stretch width, but only on
24152 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24153 width -= FRAME_WINDOW_P (it->f);
24154 #endif
24155 }
24156
24157 if (width > 0 && height > 0 && it->glyph_row)
24158 {
24159 Lisp_Object o_object = it->object;
24160 Lisp_Object object = it->stack[it->sp - 1].string;
24161 int n = width;
24162
24163 if (!STRINGP (object))
24164 object = WGET (it->w, buffer);
24165 #ifdef HAVE_WINDOW_SYSTEM
24166 if (FRAME_WINDOW_P (it->f))
24167 append_stretch_glyph (it, object, width, height, ascent);
24168 else
24169 #endif
24170 {
24171 it->object = object;
24172 it->char_to_display = ' ';
24173 it->pixel_width = it->len = 1;
24174 while (n--)
24175 tty_append_glyph (it);
24176 it->object = o_object;
24177 }
24178 }
24179
24180 it->pixel_width = width;
24181 #ifdef HAVE_WINDOW_SYSTEM
24182 if (FRAME_WINDOW_P (it->f))
24183 {
24184 it->ascent = it->phys_ascent = ascent;
24185 it->descent = it->phys_descent = height - it->ascent;
24186 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24187 take_vertical_position_into_account (it);
24188 }
24189 else
24190 #endif
24191 it->nglyphs = width;
24192 }
24193
24194 /* Get information about special display element WHAT in an
24195 environment described by IT. WHAT is one of IT_TRUNCATION or
24196 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24197 non-null glyph_row member. This function ensures that fields like
24198 face_id, c, len of IT are left untouched. */
24199
24200 static void
24201 produce_special_glyphs (struct it *it, enum display_element_type what)
24202 {
24203 struct it temp_it;
24204 Lisp_Object gc;
24205 GLYPH glyph;
24206
24207 temp_it = *it;
24208 temp_it.object = make_number (0);
24209 memset (&temp_it.current, 0, sizeof temp_it.current);
24210
24211 if (what == IT_CONTINUATION)
24212 {
24213 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24214 if (it->bidi_it.paragraph_dir == R2L)
24215 SET_GLYPH_FROM_CHAR (glyph, '/');
24216 else
24217 SET_GLYPH_FROM_CHAR (glyph, '\\');
24218 if (it->dp
24219 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24220 {
24221 /* FIXME: Should we mirror GC for R2L lines? */
24222 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24223 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24224 }
24225 }
24226 else if (what == IT_TRUNCATION)
24227 {
24228 /* Truncation glyph. */
24229 SET_GLYPH_FROM_CHAR (glyph, '$');
24230 if (it->dp
24231 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24232 {
24233 /* FIXME: Should we mirror GC for R2L lines? */
24234 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24235 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24236 }
24237 }
24238 else
24239 abort ();
24240
24241 #ifdef HAVE_WINDOW_SYSTEM
24242 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24243 is turned off, we precede the truncation/continuation glyphs by a
24244 stretch glyph whose width is computed such that these special
24245 glyphs are aligned at the window margin, even when very different
24246 fonts are used in different glyph rows. */
24247 if (FRAME_WINDOW_P (temp_it.f)
24248 /* init_iterator calls this with it->glyph_row == NULL, and it
24249 wants only the pixel width of the truncation/continuation
24250 glyphs. */
24251 && temp_it.glyph_row
24252 /* insert_left_trunc_glyphs calls us at the beginning of the
24253 row, and it has its own calculation of the stretch glyph
24254 width. */
24255 && temp_it.glyph_row->used[TEXT_AREA] > 0
24256 && (temp_it.glyph_row->reversed_p
24257 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24258 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24259 {
24260 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24261
24262 if (stretch_width > 0)
24263 {
24264 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24265 struct font *font =
24266 face->font ? face->font : FRAME_FONT (temp_it.f);
24267 int stretch_ascent =
24268 (((temp_it.ascent + temp_it.descent)
24269 * FONT_BASE (font)) / FONT_HEIGHT (font));
24270
24271 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24272 temp_it.ascent + temp_it.descent,
24273 stretch_ascent);
24274 }
24275 }
24276 #endif
24277
24278 temp_it.dp = NULL;
24279 temp_it.what = IT_CHARACTER;
24280 temp_it.len = 1;
24281 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24282 temp_it.face_id = GLYPH_FACE (glyph);
24283 temp_it.len = CHAR_BYTES (temp_it.c);
24284
24285 PRODUCE_GLYPHS (&temp_it);
24286 it->pixel_width = temp_it.pixel_width;
24287 it->nglyphs = temp_it.pixel_width;
24288 }
24289
24290 #ifdef HAVE_WINDOW_SYSTEM
24291
24292 /* Calculate line-height and line-spacing properties.
24293 An integer value specifies explicit pixel value.
24294 A float value specifies relative value to current face height.
24295 A cons (float . face-name) specifies relative value to
24296 height of specified face font.
24297
24298 Returns height in pixels, or nil. */
24299
24300
24301 static Lisp_Object
24302 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24303 int boff, int override)
24304 {
24305 Lisp_Object face_name = Qnil;
24306 int ascent, descent, height;
24307
24308 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24309 return val;
24310
24311 if (CONSP (val))
24312 {
24313 face_name = XCAR (val);
24314 val = XCDR (val);
24315 if (!NUMBERP (val))
24316 val = make_number (1);
24317 if (NILP (face_name))
24318 {
24319 height = it->ascent + it->descent;
24320 goto scale;
24321 }
24322 }
24323
24324 if (NILP (face_name))
24325 {
24326 font = FRAME_FONT (it->f);
24327 boff = FRAME_BASELINE_OFFSET (it->f);
24328 }
24329 else if (EQ (face_name, Qt))
24330 {
24331 override = 0;
24332 }
24333 else
24334 {
24335 int face_id;
24336 struct face *face;
24337
24338 face_id = lookup_named_face (it->f, face_name, 0);
24339 if (face_id < 0)
24340 return make_number (-1);
24341
24342 face = FACE_FROM_ID (it->f, face_id);
24343 font = face->font;
24344 if (font == NULL)
24345 return make_number (-1);
24346 boff = font->baseline_offset;
24347 if (font->vertical_centering)
24348 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24349 }
24350
24351 ascent = FONT_BASE (font) + boff;
24352 descent = FONT_DESCENT (font) - boff;
24353
24354 if (override)
24355 {
24356 it->override_ascent = ascent;
24357 it->override_descent = descent;
24358 it->override_boff = boff;
24359 }
24360
24361 height = ascent + descent;
24362
24363 scale:
24364 if (FLOATP (val))
24365 height = (int)(XFLOAT_DATA (val) * height);
24366 else if (INTEGERP (val))
24367 height *= XINT (val);
24368
24369 return make_number (height);
24370 }
24371
24372
24373 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24374 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24375 and only if this is for a character for which no font was found.
24376
24377 If the display method (it->glyphless_method) is
24378 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24379 length of the acronym or the hexadecimal string, UPPER_XOFF and
24380 UPPER_YOFF are pixel offsets for the upper part of the string,
24381 LOWER_XOFF and LOWER_YOFF are for the lower part.
24382
24383 For the other display methods, LEN through LOWER_YOFF are zero. */
24384
24385 static void
24386 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24387 short upper_xoff, short upper_yoff,
24388 short lower_xoff, short lower_yoff)
24389 {
24390 struct glyph *glyph;
24391 enum glyph_row_area area = it->area;
24392
24393 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24394 if (glyph < it->glyph_row->glyphs[area + 1])
24395 {
24396 /* If the glyph row is reversed, we need to prepend the glyph
24397 rather than append it. */
24398 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24399 {
24400 struct glyph *g;
24401
24402 /* Make room for the additional glyph. */
24403 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24404 g[1] = *g;
24405 glyph = it->glyph_row->glyphs[area];
24406 }
24407 glyph->charpos = CHARPOS (it->position);
24408 glyph->object = it->object;
24409 glyph->pixel_width = it->pixel_width;
24410 glyph->ascent = it->ascent;
24411 glyph->descent = it->descent;
24412 glyph->voffset = it->voffset;
24413 glyph->type = GLYPHLESS_GLYPH;
24414 glyph->u.glyphless.method = it->glyphless_method;
24415 glyph->u.glyphless.for_no_font = for_no_font;
24416 glyph->u.glyphless.len = len;
24417 glyph->u.glyphless.ch = it->c;
24418 glyph->slice.glyphless.upper_xoff = upper_xoff;
24419 glyph->slice.glyphless.upper_yoff = upper_yoff;
24420 glyph->slice.glyphless.lower_xoff = lower_xoff;
24421 glyph->slice.glyphless.lower_yoff = lower_yoff;
24422 glyph->avoid_cursor_p = it->avoid_cursor_p;
24423 glyph->multibyte_p = it->multibyte_p;
24424 glyph->left_box_line_p = it->start_of_box_run_p;
24425 glyph->right_box_line_p = it->end_of_box_run_p;
24426 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24427 || it->phys_descent > it->descent);
24428 glyph->padding_p = 0;
24429 glyph->glyph_not_available_p = 0;
24430 glyph->face_id = face_id;
24431 glyph->font_type = FONT_TYPE_UNKNOWN;
24432 if (it->bidi_p)
24433 {
24434 glyph->resolved_level = it->bidi_it.resolved_level;
24435 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24436 abort ();
24437 glyph->bidi_type = it->bidi_it.type;
24438 }
24439 ++it->glyph_row->used[area];
24440 }
24441 else
24442 IT_EXPAND_MATRIX_WIDTH (it, area);
24443 }
24444
24445
24446 /* Produce a glyph for a glyphless character for iterator IT.
24447 IT->glyphless_method specifies which method to use for displaying
24448 the character. See the description of enum
24449 glyphless_display_method in dispextern.h for the detail.
24450
24451 FOR_NO_FONT is nonzero if and only if this is for a character for
24452 which no font was found. ACRONYM, if non-nil, is an acronym string
24453 for the character. */
24454
24455 static void
24456 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24457 {
24458 int face_id;
24459 struct face *face;
24460 struct font *font;
24461 int base_width, base_height, width, height;
24462 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24463 int len;
24464
24465 /* Get the metrics of the base font. We always refer to the current
24466 ASCII face. */
24467 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24468 font = face->font ? face->font : FRAME_FONT (it->f);
24469 it->ascent = FONT_BASE (font) + font->baseline_offset;
24470 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24471 base_height = it->ascent + it->descent;
24472 base_width = font->average_width;
24473
24474 /* Get a face ID for the glyph by utilizing a cache (the same way as
24475 done for `escape-glyph' in get_next_display_element). */
24476 if (it->f == last_glyphless_glyph_frame
24477 && it->face_id == last_glyphless_glyph_face_id)
24478 {
24479 face_id = last_glyphless_glyph_merged_face_id;
24480 }
24481 else
24482 {
24483 /* Merge the `glyphless-char' face into the current face. */
24484 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24485 last_glyphless_glyph_frame = it->f;
24486 last_glyphless_glyph_face_id = it->face_id;
24487 last_glyphless_glyph_merged_face_id = face_id;
24488 }
24489
24490 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24491 {
24492 it->pixel_width = THIN_SPACE_WIDTH;
24493 len = 0;
24494 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24495 }
24496 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24497 {
24498 width = CHAR_WIDTH (it->c);
24499 if (width == 0)
24500 width = 1;
24501 else if (width > 4)
24502 width = 4;
24503 it->pixel_width = base_width * width;
24504 len = 0;
24505 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24506 }
24507 else
24508 {
24509 char buf[7];
24510 const char *str;
24511 unsigned int code[6];
24512 int upper_len;
24513 int ascent, descent;
24514 struct font_metrics metrics_upper, metrics_lower;
24515
24516 face = FACE_FROM_ID (it->f, face_id);
24517 font = face->font ? face->font : FRAME_FONT (it->f);
24518 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24519
24520 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24521 {
24522 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24523 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24524 if (CONSP (acronym))
24525 acronym = XCAR (acronym);
24526 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24527 }
24528 else
24529 {
24530 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24531 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24532 str = buf;
24533 }
24534 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24535 code[len] = font->driver->encode_char (font, str[len]);
24536 upper_len = (len + 1) / 2;
24537 font->driver->text_extents (font, code, upper_len,
24538 &metrics_upper);
24539 font->driver->text_extents (font, code + upper_len, len - upper_len,
24540 &metrics_lower);
24541
24542
24543
24544 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24545 width = max (metrics_upper.width, metrics_lower.width) + 4;
24546 upper_xoff = upper_yoff = 2; /* the typical case */
24547 if (base_width >= width)
24548 {
24549 /* Align the upper to the left, the lower to the right. */
24550 it->pixel_width = base_width;
24551 lower_xoff = base_width - 2 - metrics_lower.width;
24552 }
24553 else
24554 {
24555 /* Center the shorter one. */
24556 it->pixel_width = width;
24557 if (metrics_upper.width >= metrics_lower.width)
24558 lower_xoff = (width - metrics_lower.width) / 2;
24559 else
24560 {
24561 /* FIXME: This code doesn't look right. It formerly was
24562 missing the "lower_xoff = 0;", which couldn't have
24563 been right since it left lower_xoff uninitialized. */
24564 lower_xoff = 0;
24565 upper_xoff = (width - metrics_upper.width) / 2;
24566 }
24567 }
24568
24569 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24570 top, bottom, and between upper and lower strings. */
24571 height = (metrics_upper.ascent + metrics_upper.descent
24572 + metrics_lower.ascent + metrics_lower.descent) + 5;
24573 /* Center vertically.
24574 H:base_height, D:base_descent
24575 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24576
24577 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24578 descent = D - H/2 + h/2;
24579 lower_yoff = descent - 2 - ld;
24580 upper_yoff = lower_yoff - la - 1 - ud; */
24581 ascent = - (it->descent - (base_height + height + 1) / 2);
24582 descent = it->descent - (base_height - height) / 2;
24583 lower_yoff = descent - 2 - metrics_lower.descent;
24584 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24585 - metrics_upper.descent);
24586 /* Don't make the height shorter than the base height. */
24587 if (height > base_height)
24588 {
24589 it->ascent = ascent;
24590 it->descent = descent;
24591 }
24592 }
24593
24594 it->phys_ascent = it->ascent;
24595 it->phys_descent = it->descent;
24596 if (it->glyph_row)
24597 append_glyphless_glyph (it, face_id, for_no_font, len,
24598 upper_xoff, upper_yoff,
24599 lower_xoff, lower_yoff);
24600 it->nglyphs = 1;
24601 take_vertical_position_into_account (it);
24602 }
24603
24604
24605 /* RIF:
24606 Produce glyphs/get display metrics for the display element IT is
24607 loaded with. See the description of struct it in dispextern.h
24608 for an overview of struct it. */
24609
24610 void
24611 x_produce_glyphs (struct it *it)
24612 {
24613 int extra_line_spacing = it->extra_line_spacing;
24614
24615 it->glyph_not_available_p = 0;
24616
24617 if (it->what == IT_CHARACTER)
24618 {
24619 XChar2b char2b;
24620 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24621 struct font *font = face->font;
24622 struct font_metrics *pcm = NULL;
24623 int boff; /* baseline offset */
24624
24625 if (font == NULL)
24626 {
24627 /* When no suitable font is found, display this character by
24628 the method specified in the first extra slot of
24629 Vglyphless_char_display. */
24630 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24631
24632 eassert (it->what == IT_GLYPHLESS);
24633 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24634 goto done;
24635 }
24636
24637 boff = font->baseline_offset;
24638 if (font->vertical_centering)
24639 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24640
24641 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24642 {
24643 int stretched_p;
24644
24645 it->nglyphs = 1;
24646
24647 if (it->override_ascent >= 0)
24648 {
24649 it->ascent = it->override_ascent;
24650 it->descent = it->override_descent;
24651 boff = it->override_boff;
24652 }
24653 else
24654 {
24655 it->ascent = FONT_BASE (font) + boff;
24656 it->descent = FONT_DESCENT (font) - boff;
24657 }
24658
24659 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24660 {
24661 pcm = get_per_char_metric (font, &char2b);
24662 if (pcm->width == 0
24663 && pcm->rbearing == 0 && pcm->lbearing == 0)
24664 pcm = NULL;
24665 }
24666
24667 if (pcm)
24668 {
24669 it->phys_ascent = pcm->ascent + boff;
24670 it->phys_descent = pcm->descent - boff;
24671 it->pixel_width = pcm->width;
24672 }
24673 else
24674 {
24675 it->glyph_not_available_p = 1;
24676 it->phys_ascent = it->ascent;
24677 it->phys_descent = it->descent;
24678 it->pixel_width = font->space_width;
24679 }
24680
24681 if (it->constrain_row_ascent_descent_p)
24682 {
24683 if (it->descent > it->max_descent)
24684 {
24685 it->ascent += it->descent - it->max_descent;
24686 it->descent = it->max_descent;
24687 }
24688 if (it->ascent > it->max_ascent)
24689 {
24690 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24691 it->ascent = it->max_ascent;
24692 }
24693 it->phys_ascent = min (it->phys_ascent, it->ascent);
24694 it->phys_descent = min (it->phys_descent, it->descent);
24695 extra_line_spacing = 0;
24696 }
24697
24698 /* If this is a space inside a region of text with
24699 `space-width' property, change its width. */
24700 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24701 if (stretched_p)
24702 it->pixel_width *= XFLOATINT (it->space_width);
24703
24704 /* If face has a box, add the box thickness to the character
24705 height. If character has a box line to the left and/or
24706 right, add the box line width to the character's width. */
24707 if (face->box != FACE_NO_BOX)
24708 {
24709 int thick = face->box_line_width;
24710
24711 if (thick > 0)
24712 {
24713 it->ascent += thick;
24714 it->descent += thick;
24715 }
24716 else
24717 thick = -thick;
24718
24719 if (it->start_of_box_run_p)
24720 it->pixel_width += thick;
24721 if (it->end_of_box_run_p)
24722 it->pixel_width += thick;
24723 }
24724
24725 /* If face has an overline, add the height of the overline
24726 (1 pixel) and a 1 pixel margin to the character height. */
24727 if (face->overline_p)
24728 it->ascent += overline_margin;
24729
24730 if (it->constrain_row_ascent_descent_p)
24731 {
24732 if (it->ascent > it->max_ascent)
24733 it->ascent = it->max_ascent;
24734 if (it->descent > it->max_descent)
24735 it->descent = it->max_descent;
24736 }
24737
24738 take_vertical_position_into_account (it);
24739
24740 /* If we have to actually produce glyphs, do it. */
24741 if (it->glyph_row)
24742 {
24743 if (stretched_p)
24744 {
24745 /* Translate a space with a `space-width' property
24746 into a stretch glyph. */
24747 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24748 / FONT_HEIGHT (font));
24749 append_stretch_glyph (it, it->object, it->pixel_width,
24750 it->ascent + it->descent, ascent);
24751 }
24752 else
24753 append_glyph (it);
24754
24755 /* If characters with lbearing or rbearing are displayed
24756 in this line, record that fact in a flag of the
24757 glyph row. This is used to optimize X output code. */
24758 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24759 it->glyph_row->contains_overlapping_glyphs_p = 1;
24760 }
24761 if (! stretched_p && it->pixel_width == 0)
24762 /* We assure that all visible glyphs have at least 1-pixel
24763 width. */
24764 it->pixel_width = 1;
24765 }
24766 else if (it->char_to_display == '\n')
24767 {
24768 /* A newline has no width, but we need the height of the
24769 line. But if previous part of the line sets a height,
24770 don't increase that height */
24771
24772 Lisp_Object height;
24773 Lisp_Object total_height = Qnil;
24774
24775 it->override_ascent = -1;
24776 it->pixel_width = 0;
24777 it->nglyphs = 0;
24778
24779 height = get_it_property (it, Qline_height);
24780 /* Split (line-height total-height) list */
24781 if (CONSP (height)
24782 && CONSP (XCDR (height))
24783 && NILP (XCDR (XCDR (height))))
24784 {
24785 total_height = XCAR (XCDR (height));
24786 height = XCAR (height);
24787 }
24788 height = calc_line_height_property (it, height, font, boff, 1);
24789
24790 if (it->override_ascent >= 0)
24791 {
24792 it->ascent = it->override_ascent;
24793 it->descent = it->override_descent;
24794 boff = it->override_boff;
24795 }
24796 else
24797 {
24798 it->ascent = FONT_BASE (font) + boff;
24799 it->descent = FONT_DESCENT (font) - boff;
24800 }
24801
24802 if (EQ (height, Qt))
24803 {
24804 if (it->descent > it->max_descent)
24805 {
24806 it->ascent += it->descent - it->max_descent;
24807 it->descent = it->max_descent;
24808 }
24809 if (it->ascent > it->max_ascent)
24810 {
24811 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24812 it->ascent = it->max_ascent;
24813 }
24814 it->phys_ascent = min (it->phys_ascent, it->ascent);
24815 it->phys_descent = min (it->phys_descent, it->descent);
24816 it->constrain_row_ascent_descent_p = 1;
24817 extra_line_spacing = 0;
24818 }
24819 else
24820 {
24821 Lisp_Object spacing;
24822
24823 it->phys_ascent = it->ascent;
24824 it->phys_descent = it->descent;
24825
24826 if ((it->max_ascent > 0 || it->max_descent > 0)
24827 && face->box != FACE_NO_BOX
24828 && face->box_line_width > 0)
24829 {
24830 it->ascent += face->box_line_width;
24831 it->descent += face->box_line_width;
24832 }
24833 if (!NILP (height)
24834 && XINT (height) > it->ascent + it->descent)
24835 it->ascent = XINT (height) - it->descent;
24836
24837 if (!NILP (total_height))
24838 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24839 else
24840 {
24841 spacing = get_it_property (it, Qline_spacing);
24842 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24843 }
24844 if (INTEGERP (spacing))
24845 {
24846 extra_line_spacing = XINT (spacing);
24847 if (!NILP (total_height))
24848 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24849 }
24850 }
24851 }
24852 else /* i.e. (it->char_to_display == '\t') */
24853 {
24854 if (font->space_width > 0)
24855 {
24856 int tab_width = it->tab_width * font->space_width;
24857 int x = it->current_x + it->continuation_lines_width;
24858 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24859
24860 /* If the distance from the current position to the next tab
24861 stop is less than a space character width, use the
24862 tab stop after that. */
24863 if (next_tab_x - x < font->space_width)
24864 next_tab_x += tab_width;
24865
24866 it->pixel_width = next_tab_x - x;
24867 it->nglyphs = 1;
24868 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24869 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24870
24871 if (it->glyph_row)
24872 {
24873 append_stretch_glyph (it, it->object, it->pixel_width,
24874 it->ascent + it->descent, it->ascent);
24875 }
24876 }
24877 else
24878 {
24879 it->pixel_width = 0;
24880 it->nglyphs = 1;
24881 }
24882 }
24883 }
24884 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24885 {
24886 /* A static composition.
24887
24888 Note: A composition is represented as one glyph in the
24889 glyph matrix. There are no padding glyphs.
24890
24891 Important note: pixel_width, ascent, and descent are the
24892 values of what is drawn by draw_glyphs (i.e. the values of
24893 the overall glyphs composed). */
24894 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24895 int boff; /* baseline offset */
24896 struct composition *cmp = composition_table[it->cmp_it.id];
24897 int glyph_len = cmp->glyph_len;
24898 struct font *font = face->font;
24899
24900 it->nglyphs = 1;
24901
24902 /* If we have not yet calculated pixel size data of glyphs of
24903 the composition for the current face font, calculate them
24904 now. Theoretically, we have to check all fonts for the
24905 glyphs, but that requires much time and memory space. So,
24906 here we check only the font of the first glyph. This may
24907 lead to incorrect display, but it's very rare, and C-l
24908 (recenter-top-bottom) can correct the display anyway. */
24909 if (! cmp->font || cmp->font != font)
24910 {
24911 /* Ascent and descent of the font of the first character
24912 of this composition (adjusted by baseline offset).
24913 Ascent and descent of overall glyphs should not be less
24914 than these, respectively. */
24915 int font_ascent, font_descent, font_height;
24916 /* Bounding box of the overall glyphs. */
24917 int leftmost, rightmost, lowest, highest;
24918 int lbearing, rbearing;
24919 int i, width, ascent, descent;
24920 int left_padded = 0, right_padded = 0;
24921 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24922 XChar2b char2b;
24923 struct font_metrics *pcm;
24924 int font_not_found_p;
24925 ptrdiff_t pos;
24926
24927 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24928 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24929 break;
24930 if (glyph_len < cmp->glyph_len)
24931 right_padded = 1;
24932 for (i = 0; i < glyph_len; i++)
24933 {
24934 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24935 break;
24936 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24937 }
24938 if (i > 0)
24939 left_padded = 1;
24940
24941 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24942 : IT_CHARPOS (*it));
24943 /* If no suitable font is found, use the default font. */
24944 font_not_found_p = font == NULL;
24945 if (font_not_found_p)
24946 {
24947 face = face->ascii_face;
24948 font = face->font;
24949 }
24950 boff = font->baseline_offset;
24951 if (font->vertical_centering)
24952 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24953 font_ascent = FONT_BASE (font) + boff;
24954 font_descent = FONT_DESCENT (font) - boff;
24955 font_height = FONT_HEIGHT (font);
24956
24957 cmp->font = font;
24958
24959 pcm = NULL;
24960 if (! font_not_found_p)
24961 {
24962 get_char_face_and_encoding (it->f, c, it->face_id,
24963 &char2b, 0);
24964 pcm = get_per_char_metric (font, &char2b);
24965 }
24966
24967 /* Initialize the bounding box. */
24968 if (pcm)
24969 {
24970 width = cmp->glyph_len > 0 ? pcm->width : 0;
24971 ascent = pcm->ascent;
24972 descent = pcm->descent;
24973 lbearing = pcm->lbearing;
24974 rbearing = pcm->rbearing;
24975 }
24976 else
24977 {
24978 width = cmp->glyph_len > 0 ? font->space_width : 0;
24979 ascent = FONT_BASE (font);
24980 descent = FONT_DESCENT (font);
24981 lbearing = 0;
24982 rbearing = width;
24983 }
24984
24985 rightmost = width;
24986 leftmost = 0;
24987 lowest = - descent + boff;
24988 highest = ascent + boff;
24989
24990 if (! font_not_found_p
24991 && font->default_ascent
24992 && CHAR_TABLE_P (Vuse_default_ascent)
24993 && !NILP (Faref (Vuse_default_ascent,
24994 make_number (it->char_to_display))))
24995 highest = font->default_ascent + boff;
24996
24997 /* Draw the first glyph at the normal position. It may be
24998 shifted to right later if some other glyphs are drawn
24999 at the left. */
25000 cmp->offsets[i * 2] = 0;
25001 cmp->offsets[i * 2 + 1] = boff;
25002 cmp->lbearing = lbearing;
25003 cmp->rbearing = rbearing;
25004
25005 /* Set cmp->offsets for the remaining glyphs. */
25006 for (i++; i < glyph_len; i++)
25007 {
25008 int left, right, btm, top;
25009 int ch = COMPOSITION_GLYPH (cmp, i);
25010 int face_id;
25011 struct face *this_face;
25012
25013 if (ch == '\t')
25014 ch = ' ';
25015 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25016 this_face = FACE_FROM_ID (it->f, face_id);
25017 font = this_face->font;
25018
25019 if (font == NULL)
25020 pcm = NULL;
25021 else
25022 {
25023 get_char_face_and_encoding (it->f, ch, face_id,
25024 &char2b, 0);
25025 pcm = get_per_char_metric (font, &char2b);
25026 }
25027 if (! pcm)
25028 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25029 else
25030 {
25031 width = pcm->width;
25032 ascent = pcm->ascent;
25033 descent = pcm->descent;
25034 lbearing = pcm->lbearing;
25035 rbearing = pcm->rbearing;
25036 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25037 {
25038 /* Relative composition with or without
25039 alternate chars. */
25040 left = (leftmost + rightmost - width) / 2;
25041 btm = - descent + boff;
25042 if (font->relative_compose
25043 && (! CHAR_TABLE_P (Vignore_relative_composition)
25044 || NILP (Faref (Vignore_relative_composition,
25045 make_number (ch)))))
25046 {
25047
25048 if (- descent >= font->relative_compose)
25049 /* One extra pixel between two glyphs. */
25050 btm = highest + 1;
25051 else if (ascent <= 0)
25052 /* One extra pixel between two glyphs. */
25053 btm = lowest - 1 - ascent - descent;
25054 }
25055 }
25056 else
25057 {
25058 /* A composition rule is specified by an integer
25059 value that encodes global and new reference
25060 points (GREF and NREF). GREF and NREF are
25061 specified by numbers as below:
25062
25063 0---1---2 -- ascent
25064 | |
25065 | |
25066 | |
25067 9--10--11 -- center
25068 | |
25069 ---3---4---5--- baseline
25070 | |
25071 6---7---8 -- descent
25072 */
25073 int rule = COMPOSITION_RULE (cmp, i);
25074 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25075
25076 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25077 grefx = gref % 3, nrefx = nref % 3;
25078 grefy = gref / 3, nrefy = nref / 3;
25079 if (xoff)
25080 xoff = font_height * (xoff - 128) / 256;
25081 if (yoff)
25082 yoff = font_height * (yoff - 128) / 256;
25083
25084 left = (leftmost
25085 + grefx * (rightmost - leftmost) / 2
25086 - nrefx * width / 2
25087 + xoff);
25088
25089 btm = ((grefy == 0 ? highest
25090 : grefy == 1 ? 0
25091 : grefy == 2 ? lowest
25092 : (highest + lowest) / 2)
25093 - (nrefy == 0 ? ascent + descent
25094 : nrefy == 1 ? descent - boff
25095 : nrefy == 2 ? 0
25096 : (ascent + descent) / 2)
25097 + yoff);
25098 }
25099
25100 cmp->offsets[i * 2] = left;
25101 cmp->offsets[i * 2 + 1] = btm + descent;
25102
25103 /* Update the bounding box of the overall glyphs. */
25104 if (width > 0)
25105 {
25106 right = left + width;
25107 if (left < leftmost)
25108 leftmost = left;
25109 if (right > rightmost)
25110 rightmost = right;
25111 }
25112 top = btm + descent + ascent;
25113 if (top > highest)
25114 highest = top;
25115 if (btm < lowest)
25116 lowest = btm;
25117
25118 if (cmp->lbearing > left + lbearing)
25119 cmp->lbearing = left + lbearing;
25120 if (cmp->rbearing < left + rbearing)
25121 cmp->rbearing = left + rbearing;
25122 }
25123 }
25124
25125 /* If there are glyphs whose x-offsets are negative,
25126 shift all glyphs to the right and make all x-offsets
25127 non-negative. */
25128 if (leftmost < 0)
25129 {
25130 for (i = 0; i < cmp->glyph_len; i++)
25131 cmp->offsets[i * 2] -= leftmost;
25132 rightmost -= leftmost;
25133 cmp->lbearing -= leftmost;
25134 cmp->rbearing -= leftmost;
25135 }
25136
25137 if (left_padded && cmp->lbearing < 0)
25138 {
25139 for (i = 0; i < cmp->glyph_len; i++)
25140 cmp->offsets[i * 2] -= cmp->lbearing;
25141 rightmost -= cmp->lbearing;
25142 cmp->rbearing -= cmp->lbearing;
25143 cmp->lbearing = 0;
25144 }
25145 if (right_padded && rightmost < cmp->rbearing)
25146 {
25147 rightmost = cmp->rbearing;
25148 }
25149
25150 cmp->pixel_width = rightmost;
25151 cmp->ascent = highest;
25152 cmp->descent = - lowest;
25153 if (cmp->ascent < font_ascent)
25154 cmp->ascent = font_ascent;
25155 if (cmp->descent < font_descent)
25156 cmp->descent = font_descent;
25157 }
25158
25159 if (it->glyph_row
25160 && (cmp->lbearing < 0
25161 || cmp->rbearing > cmp->pixel_width))
25162 it->glyph_row->contains_overlapping_glyphs_p = 1;
25163
25164 it->pixel_width = cmp->pixel_width;
25165 it->ascent = it->phys_ascent = cmp->ascent;
25166 it->descent = it->phys_descent = cmp->descent;
25167 if (face->box != FACE_NO_BOX)
25168 {
25169 int thick = face->box_line_width;
25170
25171 if (thick > 0)
25172 {
25173 it->ascent += thick;
25174 it->descent += thick;
25175 }
25176 else
25177 thick = - thick;
25178
25179 if (it->start_of_box_run_p)
25180 it->pixel_width += thick;
25181 if (it->end_of_box_run_p)
25182 it->pixel_width += thick;
25183 }
25184
25185 /* If face has an overline, add the height of the overline
25186 (1 pixel) and a 1 pixel margin to the character height. */
25187 if (face->overline_p)
25188 it->ascent += overline_margin;
25189
25190 take_vertical_position_into_account (it);
25191 if (it->ascent < 0)
25192 it->ascent = 0;
25193 if (it->descent < 0)
25194 it->descent = 0;
25195
25196 if (it->glyph_row && cmp->glyph_len > 0)
25197 append_composite_glyph (it);
25198 }
25199 else if (it->what == IT_COMPOSITION)
25200 {
25201 /* A dynamic (automatic) composition. */
25202 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25203 Lisp_Object gstring;
25204 struct font_metrics metrics;
25205
25206 it->nglyphs = 1;
25207
25208 gstring = composition_gstring_from_id (it->cmp_it.id);
25209 it->pixel_width
25210 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25211 &metrics);
25212 if (it->glyph_row
25213 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25214 it->glyph_row->contains_overlapping_glyphs_p = 1;
25215 it->ascent = it->phys_ascent = metrics.ascent;
25216 it->descent = it->phys_descent = metrics.descent;
25217 if (face->box != FACE_NO_BOX)
25218 {
25219 int thick = face->box_line_width;
25220
25221 if (thick > 0)
25222 {
25223 it->ascent += thick;
25224 it->descent += thick;
25225 }
25226 else
25227 thick = - thick;
25228
25229 if (it->start_of_box_run_p)
25230 it->pixel_width += thick;
25231 if (it->end_of_box_run_p)
25232 it->pixel_width += thick;
25233 }
25234 /* If face has an overline, add the height of the overline
25235 (1 pixel) and a 1 pixel margin to the character height. */
25236 if (face->overline_p)
25237 it->ascent += overline_margin;
25238 take_vertical_position_into_account (it);
25239 if (it->ascent < 0)
25240 it->ascent = 0;
25241 if (it->descent < 0)
25242 it->descent = 0;
25243
25244 if (it->glyph_row)
25245 append_composite_glyph (it);
25246 }
25247 else if (it->what == IT_GLYPHLESS)
25248 produce_glyphless_glyph (it, 0, Qnil);
25249 else if (it->what == IT_IMAGE)
25250 produce_image_glyph (it);
25251 else if (it->what == IT_STRETCH)
25252 produce_stretch_glyph (it);
25253
25254 done:
25255 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25256 because this isn't true for images with `:ascent 100'. */
25257 eassert (it->ascent >= 0 && it->descent >= 0);
25258 if (it->area == TEXT_AREA)
25259 it->current_x += it->pixel_width;
25260
25261 if (extra_line_spacing > 0)
25262 {
25263 it->descent += extra_line_spacing;
25264 if (extra_line_spacing > it->max_extra_line_spacing)
25265 it->max_extra_line_spacing = extra_line_spacing;
25266 }
25267
25268 it->max_ascent = max (it->max_ascent, it->ascent);
25269 it->max_descent = max (it->max_descent, it->descent);
25270 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25271 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25272 }
25273
25274 /* EXPORT for RIF:
25275 Output LEN glyphs starting at START at the nominal cursor position.
25276 Advance the nominal cursor over the text. The global variable
25277 updated_window contains the window being updated, updated_row is
25278 the glyph row being updated, and updated_area is the area of that
25279 row being updated. */
25280
25281 void
25282 x_write_glyphs (struct glyph *start, int len)
25283 {
25284 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25285
25286 eassert (updated_window && updated_row);
25287 /* When the window is hscrolled, cursor hpos can legitimately be out
25288 of bounds, but we draw the cursor at the corresponding window
25289 margin in that case. */
25290 if (!updated_row->reversed_p && chpos < 0)
25291 chpos = 0;
25292 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25293 chpos = updated_row->used[TEXT_AREA] - 1;
25294
25295 BLOCK_INPUT;
25296
25297 /* Write glyphs. */
25298
25299 hpos = start - updated_row->glyphs[updated_area];
25300 x = draw_glyphs (updated_window, output_cursor.x,
25301 updated_row, updated_area,
25302 hpos, hpos + len,
25303 DRAW_NORMAL_TEXT, 0);
25304
25305 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25306 if (updated_area == TEXT_AREA
25307 && updated_window->phys_cursor_on_p
25308 && updated_window->phys_cursor.vpos == output_cursor.vpos
25309 && chpos >= hpos
25310 && chpos < hpos + len)
25311 updated_window->phys_cursor_on_p = 0;
25312
25313 UNBLOCK_INPUT;
25314
25315 /* Advance the output cursor. */
25316 output_cursor.hpos += len;
25317 output_cursor.x = x;
25318 }
25319
25320
25321 /* EXPORT for RIF:
25322 Insert LEN glyphs from START at the nominal cursor position. */
25323
25324 void
25325 x_insert_glyphs (struct glyph *start, int len)
25326 {
25327 struct frame *f;
25328 struct window *w;
25329 int line_height, shift_by_width, shifted_region_width;
25330 struct glyph_row *row;
25331 struct glyph *glyph;
25332 int frame_x, frame_y;
25333 ptrdiff_t hpos;
25334
25335 eassert (updated_window && updated_row);
25336 BLOCK_INPUT;
25337 w = updated_window;
25338 f = XFRAME (WINDOW_FRAME (w));
25339
25340 /* Get the height of the line we are in. */
25341 row = updated_row;
25342 line_height = row->height;
25343
25344 /* Get the width of the glyphs to insert. */
25345 shift_by_width = 0;
25346 for (glyph = start; glyph < start + len; ++glyph)
25347 shift_by_width += glyph->pixel_width;
25348
25349 /* Get the width of the region to shift right. */
25350 shifted_region_width = (window_box_width (w, updated_area)
25351 - output_cursor.x
25352 - shift_by_width);
25353
25354 /* Shift right. */
25355 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25356 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25357
25358 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25359 line_height, shift_by_width);
25360
25361 /* Write the glyphs. */
25362 hpos = start - row->glyphs[updated_area];
25363 draw_glyphs (w, output_cursor.x, row, updated_area,
25364 hpos, hpos + len,
25365 DRAW_NORMAL_TEXT, 0);
25366
25367 /* Advance the output cursor. */
25368 output_cursor.hpos += len;
25369 output_cursor.x += shift_by_width;
25370 UNBLOCK_INPUT;
25371 }
25372
25373
25374 /* EXPORT for RIF:
25375 Erase the current text line from the nominal cursor position
25376 (inclusive) to pixel column TO_X (exclusive). The idea is that
25377 everything from TO_X onward is already erased.
25378
25379 TO_X is a pixel position relative to updated_area of
25380 updated_window. TO_X == -1 means clear to the end of this area. */
25381
25382 void
25383 x_clear_end_of_line (int to_x)
25384 {
25385 struct frame *f;
25386 struct window *w = updated_window;
25387 int max_x, min_y, max_y;
25388 int from_x, from_y, to_y;
25389
25390 eassert (updated_window && updated_row);
25391 f = XFRAME (WGET (w, frame));
25392
25393 if (updated_row->full_width_p)
25394 max_x = WINDOW_TOTAL_WIDTH (w);
25395 else
25396 max_x = window_box_width (w, updated_area);
25397 max_y = window_text_bottom_y (w);
25398
25399 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25400 of window. For TO_X > 0, truncate to end of drawing area. */
25401 if (to_x == 0)
25402 return;
25403 else if (to_x < 0)
25404 to_x = max_x;
25405 else
25406 to_x = min (to_x, max_x);
25407
25408 to_y = min (max_y, output_cursor.y + updated_row->height);
25409
25410 /* Notice if the cursor will be cleared by this operation. */
25411 if (!updated_row->full_width_p)
25412 notice_overwritten_cursor (w, updated_area,
25413 output_cursor.x, -1,
25414 updated_row->y,
25415 MATRIX_ROW_BOTTOM_Y (updated_row));
25416
25417 from_x = output_cursor.x;
25418
25419 /* Translate to frame coordinates. */
25420 if (updated_row->full_width_p)
25421 {
25422 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25423 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25424 }
25425 else
25426 {
25427 int area_left = window_box_left (w, updated_area);
25428 from_x += area_left;
25429 to_x += area_left;
25430 }
25431
25432 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25433 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25434 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25435
25436 /* Prevent inadvertently clearing to end of the X window. */
25437 if (to_x > from_x && to_y > from_y)
25438 {
25439 BLOCK_INPUT;
25440 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25441 to_x - from_x, to_y - from_y);
25442 UNBLOCK_INPUT;
25443 }
25444 }
25445
25446 #endif /* HAVE_WINDOW_SYSTEM */
25447
25448
25449 \f
25450 /***********************************************************************
25451 Cursor types
25452 ***********************************************************************/
25453
25454 /* Value is the internal representation of the specified cursor type
25455 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25456 of the bar cursor. */
25457
25458 static enum text_cursor_kinds
25459 get_specified_cursor_type (Lisp_Object arg, int *width)
25460 {
25461 enum text_cursor_kinds type;
25462
25463 if (NILP (arg))
25464 return NO_CURSOR;
25465
25466 if (EQ (arg, Qbox))
25467 return FILLED_BOX_CURSOR;
25468
25469 if (EQ (arg, Qhollow))
25470 return HOLLOW_BOX_CURSOR;
25471
25472 if (EQ (arg, Qbar))
25473 {
25474 *width = 2;
25475 return BAR_CURSOR;
25476 }
25477
25478 if (CONSP (arg)
25479 && EQ (XCAR (arg), Qbar)
25480 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25481 {
25482 *width = XINT (XCDR (arg));
25483 return BAR_CURSOR;
25484 }
25485
25486 if (EQ (arg, Qhbar))
25487 {
25488 *width = 2;
25489 return HBAR_CURSOR;
25490 }
25491
25492 if (CONSP (arg)
25493 && EQ (XCAR (arg), Qhbar)
25494 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25495 {
25496 *width = XINT (XCDR (arg));
25497 return HBAR_CURSOR;
25498 }
25499
25500 /* Treat anything unknown as "hollow box cursor".
25501 It was bad to signal an error; people have trouble fixing
25502 .Xdefaults with Emacs, when it has something bad in it. */
25503 type = HOLLOW_BOX_CURSOR;
25504
25505 return type;
25506 }
25507
25508 /* Set the default cursor types for specified frame. */
25509 void
25510 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25511 {
25512 int width = 1;
25513 Lisp_Object tem;
25514
25515 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25516 FRAME_CURSOR_WIDTH (f) = width;
25517
25518 /* By default, set up the blink-off state depending on the on-state. */
25519
25520 tem = Fassoc (arg, Vblink_cursor_alist);
25521 if (!NILP (tem))
25522 {
25523 FRAME_BLINK_OFF_CURSOR (f)
25524 = get_specified_cursor_type (XCDR (tem), &width);
25525 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25526 }
25527 else
25528 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25529 }
25530
25531
25532 #ifdef HAVE_WINDOW_SYSTEM
25533
25534 /* Return the cursor we want to be displayed in window W. Return
25535 width of bar/hbar cursor through WIDTH arg. Return with
25536 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25537 (i.e. if the `system caret' should track this cursor).
25538
25539 In a mini-buffer window, we want the cursor only to appear if we
25540 are reading input from this window. For the selected window, we
25541 want the cursor type given by the frame parameter or buffer local
25542 setting of cursor-type. If explicitly marked off, draw no cursor.
25543 In all other cases, we want a hollow box cursor. */
25544
25545 static enum text_cursor_kinds
25546 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25547 int *active_cursor)
25548 {
25549 struct frame *f = XFRAME (WGET (w, frame));
25550 struct buffer *b = XBUFFER (WGET (w, buffer));
25551 int cursor_type = DEFAULT_CURSOR;
25552 Lisp_Object alt_cursor;
25553 int non_selected = 0;
25554
25555 *active_cursor = 1;
25556
25557 /* Echo area */
25558 if (cursor_in_echo_area
25559 && FRAME_HAS_MINIBUF_P (f)
25560 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25561 {
25562 if (w == XWINDOW (echo_area_window))
25563 {
25564 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25565 {
25566 *width = FRAME_CURSOR_WIDTH (f);
25567 return FRAME_DESIRED_CURSOR (f);
25568 }
25569 else
25570 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25571 }
25572
25573 *active_cursor = 0;
25574 non_selected = 1;
25575 }
25576
25577 /* Detect a nonselected window or nonselected frame. */
25578 else if (w != XWINDOW (f->selected_window)
25579 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25580 {
25581 *active_cursor = 0;
25582
25583 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25584 return NO_CURSOR;
25585
25586 non_selected = 1;
25587 }
25588
25589 /* Never display a cursor in a window in which cursor-type is nil. */
25590 if (NILP (BVAR (b, cursor_type)))
25591 return NO_CURSOR;
25592
25593 /* Get the normal cursor type for this window. */
25594 if (EQ (BVAR (b, cursor_type), Qt))
25595 {
25596 cursor_type = FRAME_DESIRED_CURSOR (f);
25597 *width = FRAME_CURSOR_WIDTH (f);
25598 }
25599 else
25600 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25601
25602 /* Use cursor-in-non-selected-windows instead
25603 for non-selected window or frame. */
25604 if (non_selected)
25605 {
25606 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25607 if (!EQ (Qt, alt_cursor))
25608 return get_specified_cursor_type (alt_cursor, width);
25609 /* t means modify the normal cursor type. */
25610 if (cursor_type == FILLED_BOX_CURSOR)
25611 cursor_type = HOLLOW_BOX_CURSOR;
25612 else if (cursor_type == BAR_CURSOR && *width > 1)
25613 --*width;
25614 return cursor_type;
25615 }
25616
25617 /* Use normal cursor if not blinked off. */
25618 if (!w->cursor_off_p)
25619 {
25620 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25621 {
25622 if (cursor_type == FILLED_BOX_CURSOR)
25623 {
25624 /* Using a block cursor on large images can be very annoying.
25625 So use a hollow cursor for "large" images.
25626 If image is not transparent (no mask), also use hollow cursor. */
25627 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25628 if (img != NULL && IMAGEP (img->spec))
25629 {
25630 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25631 where N = size of default frame font size.
25632 This should cover most of the "tiny" icons people may use. */
25633 if (!img->mask
25634 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25635 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25636 cursor_type = HOLLOW_BOX_CURSOR;
25637 }
25638 }
25639 else if (cursor_type != NO_CURSOR)
25640 {
25641 /* Display current only supports BOX and HOLLOW cursors for images.
25642 So for now, unconditionally use a HOLLOW cursor when cursor is
25643 not a solid box cursor. */
25644 cursor_type = HOLLOW_BOX_CURSOR;
25645 }
25646 }
25647 return cursor_type;
25648 }
25649
25650 /* Cursor is blinked off, so determine how to "toggle" it. */
25651
25652 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25653 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25654 return get_specified_cursor_type (XCDR (alt_cursor), width);
25655
25656 /* Then see if frame has specified a specific blink off cursor type. */
25657 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25658 {
25659 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25660 return FRAME_BLINK_OFF_CURSOR (f);
25661 }
25662
25663 #if 0
25664 /* Some people liked having a permanently visible blinking cursor,
25665 while others had very strong opinions against it. So it was
25666 decided to remove it. KFS 2003-09-03 */
25667
25668 /* Finally perform built-in cursor blinking:
25669 filled box <-> hollow box
25670 wide [h]bar <-> narrow [h]bar
25671 narrow [h]bar <-> no cursor
25672 other type <-> no cursor */
25673
25674 if (cursor_type == FILLED_BOX_CURSOR)
25675 return HOLLOW_BOX_CURSOR;
25676
25677 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25678 {
25679 *width = 1;
25680 return cursor_type;
25681 }
25682 #endif
25683
25684 return NO_CURSOR;
25685 }
25686
25687
25688 /* Notice when the text cursor of window W has been completely
25689 overwritten by a drawing operation that outputs glyphs in AREA
25690 starting at X0 and ending at X1 in the line starting at Y0 and
25691 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25692 the rest of the line after X0 has been written. Y coordinates
25693 are window-relative. */
25694
25695 static void
25696 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25697 int x0, int x1, int y0, int y1)
25698 {
25699 int cx0, cx1, cy0, cy1;
25700 struct glyph_row *row;
25701
25702 if (!w->phys_cursor_on_p)
25703 return;
25704 if (area != TEXT_AREA)
25705 return;
25706
25707 if (w->phys_cursor.vpos < 0
25708 || w->phys_cursor.vpos >= w->current_matrix->nrows
25709 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25710 !(row->enabled_p && row->displays_text_p)))
25711 return;
25712
25713 if (row->cursor_in_fringe_p)
25714 {
25715 row->cursor_in_fringe_p = 0;
25716 draw_fringe_bitmap (w, row, row->reversed_p);
25717 w->phys_cursor_on_p = 0;
25718 return;
25719 }
25720
25721 cx0 = w->phys_cursor.x;
25722 cx1 = cx0 + w->phys_cursor_width;
25723 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25724 return;
25725
25726 /* The cursor image will be completely removed from the
25727 screen if the output area intersects the cursor area in
25728 y-direction. When we draw in [y0 y1[, and some part of
25729 the cursor is at y < y0, that part must have been drawn
25730 before. When scrolling, the cursor is erased before
25731 actually scrolling, so we don't come here. When not
25732 scrolling, the rows above the old cursor row must have
25733 changed, and in this case these rows must have written
25734 over the cursor image.
25735
25736 Likewise if part of the cursor is below y1, with the
25737 exception of the cursor being in the first blank row at
25738 the buffer and window end because update_text_area
25739 doesn't draw that row. (Except when it does, but
25740 that's handled in update_text_area.) */
25741
25742 cy0 = w->phys_cursor.y;
25743 cy1 = cy0 + w->phys_cursor_height;
25744 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25745 return;
25746
25747 w->phys_cursor_on_p = 0;
25748 }
25749
25750 #endif /* HAVE_WINDOW_SYSTEM */
25751
25752 \f
25753 /************************************************************************
25754 Mouse Face
25755 ************************************************************************/
25756
25757 #ifdef HAVE_WINDOW_SYSTEM
25758
25759 /* EXPORT for RIF:
25760 Fix the display of area AREA of overlapping row ROW in window W
25761 with respect to the overlapping part OVERLAPS. */
25762
25763 void
25764 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25765 enum glyph_row_area area, int overlaps)
25766 {
25767 int i, x;
25768
25769 BLOCK_INPUT;
25770
25771 x = 0;
25772 for (i = 0; i < row->used[area];)
25773 {
25774 if (row->glyphs[area][i].overlaps_vertically_p)
25775 {
25776 int start = i, start_x = x;
25777
25778 do
25779 {
25780 x += row->glyphs[area][i].pixel_width;
25781 ++i;
25782 }
25783 while (i < row->used[area]
25784 && row->glyphs[area][i].overlaps_vertically_p);
25785
25786 draw_glyphs (w, start_x, row, area,
25787 start, i,
25788 DRAW_NORMAL_TEXT, overlaps);
25789 }
25790 else
25791 {
25792 x += row->glyphs[area][i].pixel_width;
25793 ++i;
25794 }
25795 }
25796
25797 UNBLOCK_INPUT;
25798 }
25799
25800
25801 /* EXPORT:
25802 Draw the cursor glyph of window W in glyph row ROW. See the
25803 comment of draw_glyphs for the meaning of HL. */
25804
25805 void
25806 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25807 enum draw_glyphs_face hl)
25808 {
25809 /* If cursor hpos is out of bounds, don't draw garbage. This can
25810 happen in mini-buffer windows when switching between echo area
25811 glyphs and mini-buffer. */
25812 if ((row->reversed_p
25813 ? (w->phys_cursor.hpos >= 0)
25814 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25815 {
25816 int on_p = w->phys_cursor_on_p;
25817 int x1;
25818 int hpos = w->phys_cursor.hpos;
25819
25820 /* When the window is hscrolled, cursor hpos can legitimately be
25821 out of bounds, but we draw the cursor at the corresponding
25822 window margin in that case. */
25823 if (!row->reversed_p && hpos < 0)
25824 hpos = 0;
25825 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25826 hpos = row->used[TEXT_AREA] - 1;
25827
25828 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25829 hl, 0);
25830 w->phys_cursor_on_p = on_p;
25831
25832 if (hl == DRAW_CURSOR)
25833 w->phys_cursor_width = x1 - w->phys_cursor.x;
25834 /* When we erase the cursor, and ROW is overlapped by other
25835 rows, make sure that these overlapping parts of other rows
25836 are redrawn. */
25837 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25838 {
25839 w->phys_cursor_width = x1 - w->phys_cursor.x;
25840
25841 if (row > w->current_matrix->rows
25842 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25843 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25844 OVERLAPS_ERASED_CURSOR);
25845
25846 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25847 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25848 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25849 OVERLAPS_ERASED_CURSOR);
25850 }
25851 }
25852 }
25853
25854
25855 /* EXPORT:
25856 Erase the image of a cursor of window W from the screen. */
25857
25858 void
25859 erase_phys_cursor (struct window *w)
25860 {
25861 struct frame *f = XFRAME (WGET (w, frame));
25862 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25863 int hpos = w->phys_cursor.hpos;
25864 int vpos = w->phys_cursor.vpos;
25865 int mouse_face_here_p = 0;
25866 struct glyph_matrix *active_glyphs = w->current_matrix;
25867 struct glyph_row *cursor_row;
25868 struct glyph *cursor_glyph;
25869 enum draw_glyphs_face hl;
25870
25871 /* No cursor displayed or row invalidated => nothing to do on the
25872 screen. */
25873 if (w->phys_cursor_type == NO_CURSOR)
25874 goto mark_cursor_off;
25875
25876 /* VPOS >= active_glyphs->nrows means that window has been resized.
25877 Don't bother to erase the cursor. */
25878 if (vpos >= active_glyphs->nrows)
25879 goto mark_cursor_off;
25880
25881 /* If row containing cursor is marked invalid, there is nothing we
25882 can do. */
25883 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25884 if (!cursor_row->enabled_p)
25885 goto mark_cursor_off;
25886
25887 /* If line spacing is > 0, old cursor may only be partially visible in
25888 window after split-window. So adjust visible height. */
25889 cursor_row->visible_height = min (cursor_row->visible_height,
25890 window_text_bottom_y (w) - cursor_row->y);
25891
25892 /* If row is completely invisible, don't attempt to delete a cursor which
25893 isn't there. This can happen if cursor is at top of a window, and
25894 we switch to a buffer with a header line in that window. */
25895 if (cursor_row->visible_height <= 0)
25896 goto mark_cursor_off;
25897
25898 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25899 if (cursor_row->cursor_in_fringe_p)
25900 {
25901 cursor_row->cursor_in_fringe_p = 0;
25902 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25903 goto mark_cursor_off;
25904 }
25905
25906 /* This can happen when the new row is shorter than the old one.
25907 In this case, either draw_glyphs or clear_end_of_line
25908 should have cleared the cursor. Note that we wouldn't be
25909 able to erase the cursor in this case because we don't have a
25910 cursor glyph at hand. */
25911 if ((cursor_row->reversed_p
25912 ? (w->phys_cursor.hpos < 0)
25913 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25914 goto mark_cursor_off;
25915
25916 /* When the window is hscrolled, cursor hpos can legitimately be out
25917 of bounds, but we draw the cursor at the corresponding window
25918 margin in that case. */
25919 if (!cursor_row->reversed_p && hpos < 0)
25920 hpos = 0;
25921 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25922 hpos = cursor_row->used[TEXT_AREA] - 1;
25923
25924 /* If the cursor is in the mouse face area, redisplay that when
25925 we clear the cursor. */
25926 if (! NILP (hlinfo->mouse_face_window)
25927 && coords_in_mouse_face_p (w, hpos, vpos)
25928 /* Don't redraw the cursor's spot in mouse face if it is at the
25929 end of a line (on a newline). The cursor appears there, but
25930 mouse highlighting does not. */
25931 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25932 mouse_face_here_p = 1;
25933
25934 /* Maybe clear the display under the cursor. */
25935 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25936 {
25937 int x, y, left_x;
25938 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25939 int width;
25940
25941 cursor_glyph = get_phys_cursor_glyph (w);
25942 if (cursor_glyph == NULL)
25943 goto mark_cursor_off;
25944
25945 width = cursor_glyph->pixel_width;
25946 left_x = window_box_left_offset (w, TEXT_AREA);
25947 x = w->phys_cursor.x;
25948 if (x < left_x)
25949 width -= left_x - x;
25950 width = min (width, window_box_width (w, TEXT_AREA) - x);
25951 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25952 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25953
25954 if (width > 0)
25955 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25956 }
25957
25958 /* Erase the cursor by redrawing the character underneath it. */
25959 if (mouse_face_here_p)
25960 hl = DRAW_MOUSE_FACE;
25961 else
25962 hl = DRAW_NORMAL_TEXT;
25963 draw_phys_cursor_glyph (w, cursor_row, hl);
25964
25965 mark_cursor_off:
25966 w->phys_cursor_on_p = 0;
25967 w->phys_cursor_type = NO_CURSOR;
25968 }
25969
25970
25971 /* EXPORT:
25972 Display or clear cursor of window W. If ON is zero, clear the
25973 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25974 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25975
25976 void
25977 display_and_set_cursor (struct window *w, int on,
25978 int hpos, int vpos, int x, int y)
25979 {
25980 struct frame *f = XFRAME (WGET (w, frame));
25981 int new_cursor_type;
25982 int new_cursor_width;
25983 int active_cursor;
25984 struct glyph_row *glyph_row;
25985 struct glyph *glyph;
25986
25987 /* This is pointless on invisible frames, and dangerous on garbaged
25988 windows and frames; in the latter case, the frame or window may
25989 be in the midst of changing its size, and x and y may be off the
25990 window. */
25991 if (! FRAME_VISIBLE_P (f)
25992 || FRAME_GARBAGED_P (f)
25993 || vpos >= w->current_matrix->nrows
25994 || hpos >= w->current_matrix->matrix_w)
25995 return;
25996
25997 /* If cursor is off and we want it off, return quickly. */
25998 if (!on && !w->phys_cursor_on_p)
25999 return;
26000
26001 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26002 /* If cursor row is not enabled, we don't really know where to
26003 display the cursor. */
26004 if (!glyph_row->enabled_p)
26005 {
26006 w->phys_cursor_on_p = 0;
26007 return;
26008 }
26009
26010 glyph = NULL;
26011 if (!glyph_row->exact_window_width_line_p
26012 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26013 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26014
26015 eassert (interrupt_input_blocked);
26016
26017 /* Set new_cursor_type to the cursor we want to be displayed. */
26018 new_cursor_type = get_window_cursor_type (w, glyph,
26019 &new_cursor_width, &active_cursor);
26020
26021 /* If cursor is currently being shown and we don't want it to be or
26022 it is in the wrong place, or the cursor type is not what we want,
26023 erase it. */
26024 if (w->phys_cursor_on_p
26025 && (!on
26026 || w->phys_cursor.x != x
26027 || w->phys_cursor.y != y
26028 || new_cursor_type != w->phys_cursor_type
26029 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26030 && new_cursor_width != w->phys_cursor_width)))
26031 erase_phys_cursor (w);
26032
26033 /* Don't check phys_cursor_on_p here because that flag is only set
26034 to zero in some cases where we know that the cursor has been
26035 completely erased, to avoid the extra work of erasing the cursor
26036 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26037 still not be visible, or it has only been partly erased. */
26038 if (on)
26039 {
26040 w->phys_cursor_ascent = glyph_row->ascent;
26041 w->phys_cursor_height = glyph_row->height;
26042
26043 /* Set phys_cursor_.* before x_draw_.* is called because some
26044 of them may need the information. */
26045 w->phys_cursor.x = x;
26046 w->phys_cursor.y = glyph_row->y;
26047 w->phys_cursor.hpos = hpos;
26048 w->phys_cursor.vpos = vpos;
26049 }
26050
26051 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26052 new_cursor_type, new_cursor_width,
26053 on, active_cursor);
26054 }
26055
26056
26057 /* Switch the display of W's cursor on or off, according to the value
26058 of ON. */
26059
26060 static void
26061 update_window_cursor (struct window *w, int on)
26062 {
26063 /* Don't update cursor in windows whose frame is in the process
26064 of being deleted. */
26065 if (w->current_matrix)
26066 {
26067 int hpos = w->phys_cursor.hpos;
26068 int vpos = w->phys_cursor.vpos;
26069 struct glyph_row *row;
26070
26071 if (vpos >= w->current_matrix->nrows
26072 || hpos >= w->current_matrix->matrix_w)
26073 return;
26074
26075 row = MATRIX_ROW (w->current_matrix, vpos);
26076
26077 /* When the window is hscrolled, cursor hpos can legitimately be
26078 out of bounds, but we draw the cursor at the corresponding
26079 window margin in that case. */
26080 if (!row->reversed_p && hpos < 0)
26081 hpos = 0;
26082 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26083 hpos = row->used[TEXT_AREA] - 1;
26084
26085 BLOCK_INPUT;
26086 display_and_set_cursor (w, on, hpos, vpos,
26087 w->phys_cursor.x, w->phys_cursor.y);
26088 UNBLOCK_INPUT;
26089 }
26090 }
26091
26092
26093 /* Call update_window_cursor with parameter ON_P on all leaf windows
26094 in the window tree rooted at W. */
26095
26096 static void
26097 update_cursor_in_window_tree (struct window *w, int on_p)
26098 {
26099 while (w)
26100 {
26101 if (!NILP (WGET (w, hchild)))
26102 update_cursor_in_window_tree (XWINDOW (WGET (w, hchild)), on_p);
26103 else if (!NILP (WGET (w, vchild)))
26104 update_cursor_in_window_tree (XWINDOW (WGET (w, vchild)), on_p);
26105 else
26106 update_window_cursor (w, on_p);
26107
26108 w = NILP (WGET (w, next)) ? 0 : XWINDOW (WGET (w, next));
26109 }
26110 }
26111
26112
26113 /* EXPORT:
26114 Display the cursor on window W, or clear it, according to ON_P.
26115 Don't change the cursor's position. */
26116
26117 void
26118 x_update_cursor (struct frame *f, int on_p)
26119 {
26120 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26121 }
26122
26123
26124 /* EXPORT:
26125 Clear the cursor of window W to background color, and mark the
26126 cursor as not shown. This is used when the text where the cursor
26127 is about to be rewritten. */
26128
26129 void
26130 x_clear_cursor (struct window *w)
26131 {
26132 if (FRAME_VISIBLE_P (XFRAME (WGET (w, frame))) && w->phys_cursor_on_p)
26133 update_window_cursor (w, 0);
26134 }
26135
26136 #endif /* HAVE_WINDOW_SYSTEM */
26137
26138 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26139 and MSDOS. */
26140 static void
26141 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26142 int start_hpos, int end_hpos,
26143 enum draw_glyphs_face draw)
26144 {
26145 #ifdef HAVE_WINDOW_SYSTEM
26146 if (FRAME_WINDOW_P (XFRAME (WGET (w, frame))))
26147 {
26148 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26149 return;
26150 }
26151 #endif
26152 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26153 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26154 #endif
26155 }
26156
26157 /* Display the active region described by mouse_face_* according to DRAW. */
26158
26159 static void
26160 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26161 {
26162 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26163 struct frame *f = XFRAME (WINDOW_FRAME (w));
26164
26165 if (/* If window is in the process of being destroyed, don't bother
26166 to do anything. */
26167 w->current_matrix != NULL
26168 /* Don't update mouse highlight if hidden */
26169 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26170 /* Recognize when we are called to operate on rows that don't exist
26171 anymore. This can happen when a window is split. */
26172 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26173 {
26174 int phys_cursor_on_p = w->phys_cursor_on_p;
26175 struct glyph_row *row, *first, *last;
26176
26177 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26178 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26179
26180 for (row = first; row <= last && row->enabled_p; ++row)
26181 {
26182 int start_hpos, end_hpos, start_x;
26183
26184 /* For all but the first row, the highlight starts at column 0. */
26185 if (row == first)
26186 {
26187 /* R2L rows have BEG and END in reversed order, but the
26188 screen drawing geometry is always left to right. So
26189 we need to mirror the beginning and end of the
26190 highlighted area in R2L rows. */
26191 if (!row->reversed_p)
26192 {
26193 start_hpos = hlinfo->mouse_face_beg_col;
26194 start_x = hlinfo->mouse_face_beg_x;
26195 }
26196 else if (row == last)
26197 {
26198 start_hpos = hlinfo->mouse_face_end_col;
26199 start_x = hlinfo->mouse_face_end_x;
26200 }
26201 else
26202 {
26203 start_hpos = 0;
26204 start_x = 0;
26205 }
26206 }
26207 else if (row->reversed_p && row == last)
26208 {
26209 start_hpos = hlinfo->mouse_face_end_col;
26210 start_x = hlinfo->mouse_face_end_x;
26211 }
26212 else
26213 {
26214 start_hpos = 0;
26215 start_x = 0;
26216 }
26217
26218 if (row == last)
26219 {
26220 if (!row->reversed_p)
26221 end_hpos = hlinfo->mouse_face_end_col;
26222 else if (row == first)
26223 end_hpos = hlinfo->mouse_face_beg_col;
26224 else
26225 {
26226 end_hpos = row->used[TEXT_AREA];
26227 if (draw == DRAW_NORMAL_TEXT)
26228 row->fill_line_p = 1; /* Clear to end of line */
26229 }
26230 }
26231 else if (row->reversed_p && row == first)
26232 end_hpos = hlinfo->mouse_face_beg_col;
26233 else
26234 {
26235 end_hpos = row->used[TEXT_AREA];
26236 if (draw == DRAW_NORMAL_TEXT)
26237 row->fill_line_p = 1; /* Clear to end of line */
26238 }
26239
26240 if (end_hpos > start_hpos)
26241 {
26242 draw_row_with_mouse_face (w, start_x, row,
26243 start_hpos, end_hpos, draw);
26244
26245 row->mouse_face_p
26246 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26247 }
26248 }
26249
26250 #ifdef HAVE_WINDOW_SYSTEM
26251 /* When we've written over the cursor, arrange for it to
26252 be displayed again. */
26253 if (FRAME_WINDOW_P (f)
26254 && phys_cursor_on_p && !w->phys_cursor_on_p)
26255 {
26256 int hpos = w->phys_cursor.hpos;
26257
26258 /* When the window is hscrolled, cursor hpos can legitimately be
26259 out of bounds, but we draw the cursor at the corresponding
26260 window margin in that case. */
26261 if (!row->reversed_p && hpos < 0)
26262 hpos = 0;
26263 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26264 hpos = row->used[TEXT_AREA] - 1;
26265
26266 BLOCK_INPUT;
26267 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26268 w->phys_cursor.x, w->phys_cursor.y);
26269 UNBLOCK_INPUT;
26270 }
26271 #endif /* HAVE_WINDOW_SYSTEM */
26272 }
26273
26274 #ifdef HAVE_WINDOW_SYSTEM
26275 /* Change the mouse cursor. */
26276 if (FRAME_WINDOW_P (f))
26277 {
26278 if (draw == DRAW_NORMAL_TEXT
26279 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26280 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26281 else if (draw == DRAW_MOUSE_FACE)
26282 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26283 else
26284 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26285 }
26286 #endif /* HAVE_WINDOW_SYSTEM */
26287 }
26288
26289 /* EXPORT:
26290 Clear out the mouse-highlighted active region.
26291 Redraw it un-highlighted first. Value is non-zero if mouse
26292 face was actually drawn unhighlighted. */
26293
26294 int
26295 clear_mouse_face (Mouse_HLInfo *hlinfo)
26296 {
26297 int cleared = 0;
26298
26299 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26300 {
26301 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26302 cleared = 1;
26303 }
26304
26305 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26306 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26307 hlinfo->mouse_face_window = Qnil;
26308 hlinfo->mouse_face_overlay = Qnil;
26309 return cleared;
26310 }
26311
26312 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26313 within the mouse face on that window. */
26314 static int
26315 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26316 {
26317 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (WGET (w, frame)));
26318
26319 /* Quickly resolve the easy cases. */
26320 if (!(WINDOWP (hlinfo->mouse_face_window)
26321 && XWINDOW (hlinfo->mouse_face_window) == w))
26322 return 0;
26323 if (vpos < hlinfo->mouse_face_beg_row
26324 || vpos > hlinfo->mouse_face_end_row)
26325 return 0;
26326 if (vpos > hlinfo->mouse_face_beg_row
26327 && vpos < hlinfo->mouse_face_end_row)
26328 return 1;
26329
26330 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26331 {
26332 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26333 {
26334 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26335 return 1;
26336 }
26337 else if ((vpos == hlinfo->mouse_face_beg_row
26338 && hpos >= hlinfo->mouse_face_beg_col)
26339 || (vpos == hlinfo->mouse_face_end_row
26340 && hpos < hlinfo->mouse_face_end_col))
26341 return 1;
26342 }
26343 else
26344 {
26345 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26346 {
26347 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26348 return 1;
26349 }
26350 else if ((vpos == hlinfo->mouse_face_beg_row
26351 && hpos <= hlinfo->mouse_face_beg_col)
26352 || (vpos == hlinfo->mouse_face_end_row
26353 && hpos > hlinfo->mouse_face_end_col))
26354 return 1;
26355 }
26356 return 0;
26357 }
26358
26359
26360 /* EXPORT:
26361 Non-zero if physical cursor of window W is within mouse face. */
26362
26363 int
26364 cursor_in_mouse_face_p (struct window *w)
26365 {
26366 int hpos = w->phys_cursor.hpos;
26367 int vpos = w->phys_cursor.vpos;
26368 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26369
26370 /* When the window is hscrolled, cursor hpos can legitimately be out
26371 of bounds, but we draw the cursor at the corresponding window
26372 margin in that case. */
26373 if (!row->reversed_p && hpos < 0)
26374 hpos = 0;
26375 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26376 hpos = row->used[TEXT_AREA] - 1;
26377
26378 return coords_in_mouse_face_p (w, hpos, vpos);
26379 }
26380
26381
26382 \f
26383 /* Find the glyph rows START_ROW and END_ROW of window W that display
26384 characters between buffer positions START_CHARPOS and END_CHARPOS
26385 (excluding END_CHARPOS). DISP_STRING is a display string that
26386 covers these buffer positions. This is similar to
26387 row_containing_pos, but is more accurate when bidi reordering makes
26388 buffer positions change non-linearly with glyph rows. */
26389 static void
26390 rows_from_pos_range (struct window *w,
26391 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26392 Lisp_Object disp_string,
26393 struct glyph_row **start, struct glyph_row **end)
26394 {
26395 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26396 int last_y = window_text_bottom_y (w);
26397 struct glyph_row *row;
26398
26399 *start = NULL;
26400 *end = NULL;
26401
26402 while (!first->enabled_p
26403 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26404 first++;
26405
26406 /* Find the START row. */
26407 for (row = first;
26408 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26409 row++)
26410 {
26411 /* A row can potentially be the START row if the range of the
26412 characters it displays intersects the range
26413 [START_CHARPOS..END_CHARPOS). */
26414 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26415 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26416 /* See the commentary in row_containing_pos, for the
26417 explanation of the complicated way to check whether
26418 some position is beyond the end of the characters
26419 displayed by a row. */
26420 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26421 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26422 && !row->ends_at_zv_p
26423 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26424 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26425 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26426 && !row->ends_at_zv_p
26427 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26428 {
26429 /* Found a candidate row. Now make sure at least one of the
26430 glyphs it displays has a charpos from the range
26431 [START_CHARPOS..END_CHARPOS).
26432
26433 This is not obvious because bidi reordering could make
26434 buffer positions of a row be 1,2,3,102,101,100, and if we
26435 want to highlight characters in [50..60), we don't want
26436 this row, even though [50..60) does intersect [1..103),
26437 the range of character positions given by the row's start
26438 and end positions. */
26439 struct glyph *g = row->glyphs[TEXT_AREA];
26440 struct glyph *e = g + row->used[TEXT_AREA];
26441
26442 while (g < e)
26443 {
26444 if (((BUFFERP (g->object) || INTEGERP (g->object))
26445 && start_charpos <= g->charpos && g->charpos < end_charpos)
26446 /* A glyph that comes from DISP_STRING is by
26447 definition to be highlighted. */
26448 || EQ (g->object, disp_string))
26449 *start = row;
26450 g++;
26451 }
26452 if (*start)
26453 break;
26454 }
26455 }
26456
26457 /* Find the END row. */
26458 if (!*start
26459 /* If the last row is partially visible, start looking for END
26460 from that row, instead of starting from FIRST. */
26461 && !(row->enabled_p
26462 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26463 row = first;
26464 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26465 {
26466 struct glyph_row *next = row + 1;
26467 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26468
26469 if (!next->enabled_p
26470 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26471 /* The first row >= START whose range of displayed characters
26472 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26473 is the row END + 1. */
26474 || (start_charpos < next_start
26475 && end_charpos < next_start)
26476 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26477 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26478 && !next->ends_at_zv_p
26479 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26480 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26481 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26482 && !next->ends_at_zv_p
26483 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26484 {
26485 *end = row;
26486 break;
26487 }
26488 else
26489 {
26490 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26491 but none of the characters it displays are in the range, it is
26492 also END + 1. */
26493 struct glyph *g = next->glyphs[TEXT_AREA];
26494 struct glyph *s = g;
26495 struct glyph *e = g + next->used[TEXT_AREA];
26496
26497 while (g < e)
26498 {
26499 if (((BUFFERP (g->object) || INTEGERP (g->object))
26500 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26501 /* If the buffer position of the first glyph in
26502 the row is equal to END_CHARPOS, it means
26503 the last character to be highlighted is the
26504 newline of ROW, and we must consider NEXT as
26505 END, not END+1. */
26506 || (((!next->reversed_p && g == s)
26507 || (next->reversed_p && g == e - 1))
26508 && (g->charpos == end_charpos
26509 /* Special case for when NEXT is an
26510 empty line at ZV. */
26511 || (g->charpos == -1
26512 && !row->ends_at_zv_p
26513 && next_start == end_charpos)))))
26514 /* A glyph that comes from DISP_STRING is by
26515 definition to be highlighted. */
26516 || EQ (g->object, disp_string))
26517 break;
26518 g++;
26519 }
26520 if (g == e)
26521 {
26522 *end = row;
26523 break;
26524 }
26525 /* The first row that ends at ZV must be the last to be
26526 highlighted. */
26527 else if (next->ends_at_zv_p)
26528 {
26529 *end = next;
26530 break;
26531 }
26532 }
26533 }
26534 }
26535
26536 /* This function sets the mouse_face_* elements of HLINFO, assuming
26537 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26538 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26539 for the overlay or run of text properties specifying the mouse
26540 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26541 before-string and after-string that must also be highlighted.
26542 DISP_STRING, if non-nil, is a display string that may cover some
26543 or all of the highlighted text. */
26544
26545 static void
26546 mouse_face_from_buffer_pos (Lisp_Object window,
26547 Mouse_HLInfo *hlinfo,
26548 ptrdiff_t mouse_charpos,
26549 ptrdiff_t start_charpos,
26550 ptrdiff_t end_charpos,
26551 Lisp_Object before_string,
26552 Lisp_Object after_string,
26553 Lisp_Object disp_string)
26554 {
26555 struct window *w = XWINDOW (window);
26556 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26557 struct glyph_row *r1, *r2;
26558 struct glyph *glyph, *end;
26559 ptrdiff_t ignore, pos;
26560 int x;
26561
26562 eassert (NILP (disp_string) || STRINGP (disp_string));
26563 eassert (NILP (before_string) || STRINGP (before_string));
26564 eassert (NILP (after_string) || STRINGP (after_string));
26565
26566 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26567 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26568 if (r1 == NULL)
26569 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (WGET (w, window_end_vpos)));
26570 /* If the before-string or display-string contains newlines,
26571 rows_from_pos_range skips to its last row. Move back. */
26572 if (!NILP (before_string) || !NILP (disp_string))
26573 {
26574 struct glyph_row *prev;
26575 while ((prev = r1 - 1, prev >= first)
26576 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26577 && prev->used[TEXT_AREA] > 0)
26578 {
26579 struct glyph *beg = prev->glyphs[TEXT_AREA];
26580 glyph = beg + prev->used[TEXT_AREA];
26581 while (--glyph >= beg && INTEGERP (glyph->object));
26582 if (glyph < beg
26583 || !(EQ (glyph->object, before_string)
26584 || EQ (glyph->object, disp_string)))
26585 break;
26586 r1 = prev;
26587 }
26588 }
26589 if (r2 == NULL)
26590 {
26591 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (WGET (w, window_end_vpos)));
26592 hlinfo->mouse_face_past_end = 1;
26593 }
26594 else if (!NILP (after_string))
26595 {
26596 /* If the after-string has newlines, advance to its last row. */
26597 struct glyph_row *next;
26598 struct glyph_row *last
26599 = MATRIX_ROW (w->current_matrix, XFASTINT (WGET (w, window_end_vpos)));
26600
26601 for (next = r2 + 1;
26602 next <= last
26603 && next->used[TEXT_AREA] > 0
26604 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26605 ++next)
26606 r2 = next;
26607 }
26608 /* The rest of the display engine assumes that mouse_face_beg_row is
26609 either above mouse_face_end_row or identical to it. But with
26610 bidi-reordered continued lines, the row for START_CHARPOS could
26611 be below the row for END_CHARPOS. If so, swap the rows and store
26612 them in correct order. */
26613 if (r1->y > r2->y)
26614 {
26615 struct glyph_row *tem = r2;
26616
26617 r2 = r1;
26618 r1 = tem;
26619 }
26620
26621 hlinfo->mouse_face_beg_y = r1->y;
26622 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26623 hlinfo->mouse_face_end_y = r2->y;
26624 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26625
26626 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26627 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26628 could be anywhere in the row and in any order. The strategy
26629 below is to find the leftmost and the rightmost glyph that
26630 belongs to either of these 3 strings, or whose position is
26631 between START_CHARPOS and END_CHARPOS, and highlight all the
26632 glyphs between those two. This may cover more than just the text
26633 between START_CHARPOS and END_CHARPOS if the range of characters
26634 strides the bidi level boundary, e.g. if the beginning is in R2L
26635 text while the end is in L2R text or vice versa. */
26636 if (!r1->reversed_p)
26637 {
26638 /* This row is in a left to right paragraph. Scan it left to
26639 right. */
26640 glyph = r1->glyphs[TEXT_AREA];
26641 end = glyph + r1->used[TEXT_AREA];
26642 x = r1->x;
26643
26644 /* Skip truncation glyphs at the start of the glyph row. */
26645 if (r1->displays_text_p)
26646 for (; glyph < end
26647 && INTEGERP (glyph->object)
26648 && glyph->charpos < 0;
26649 ++glyph)
26650 x += glyph->pixel_width;
26651
26652 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26653 or DISP_STRING, and the first glyph from buffer whose
26654 position is between START_CHARPOS and END_CHARPOS. */
26655 for (; glyph < end
26656 && !INTEGERP (glyph->object)
26657 && !EQ (glyph->object, disp_string)
26658 && !(BUFFERP (glyph->object)
26659 && (glyph->charpos >= start_charpos
26660 && glyph->charpos < end_charpos));
26661 ++glyph)
26662 {
26663 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26664 are present at buffer positions between START_CHARPOS and
26665 END_CHARPOS, or if they come from an overlay. */
26666 if (EQ (glyph->object, before_string))
26667 {
26668 pos = string_buffer_position (before_string,
26669 start_charpos);
26670 /* If pos == 0, it means before_string came from an
26671 overlay, not from a buffer position. */
26672 if (!pos || (pos >= start_charpos && pos < end_charpos))
26673 break;
26674 }
26675 else if (EQ (glyph->object, after_string))
26676 {
26677 pos = string_buffer_position (after_string, end_charpos);
26678 if (!pos || (pos >= start_charpos && pos < end_charpos))
26679 break;
26680 }
26681 x += glyph->pixel_width;
26682 }
26683 hlinfo->mouse_face_beg_x = x;
26684 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26685 }
26686 else
26687 {
26688 /* This row is in a right to left paragraph. Scan it right to
26689 left. */
26690 struct glyph *g;
26691
26692 end = r1->glyphs[TEXT_AREA] - 1;
26693 glyph = end + r1->used[TEXT_AREA];
26694
26695 /* Skip truncation glyphs at the start of the glyph row. */
26696 if (r1->displays_text_p)
26697 for (; glyph > end
26698 && INTEGERP (glyph->object)
26699 && glyph->charpos < 0;
26700 --glyph)
26701 ;
26702
26703 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26704 or DISP_STRING, and the first glyph from buffer whose
26705 position is between START_CHARPOS and END_CHARPOS. */
26706 for (; glyph > end
26707 && !INTEGERP (glyph->object)
26708 && !EQ (glyph->object, disp_string)
26709 && !(BUFFERP (glyph->object)
26710 && (glyph->charpos >= start_charpos
26711 && glyph->charpos < end_charpos));
26712 --glyph)
26713 {
26714 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26715 are present at buffer positions between START_CHARPOS and
26716 END_CHARPOS, or if they come from an overlay. */
26717 if (EQ (glyph->object, before_string))
26718 {
26719 pos = string_buffer_position (before_string, start_charpos);
26720 /* If pos == 0, it means before_string came from an
26721 overlay, not from a buffer position. */
26722 if (!pos || (pos >= start_charpos && pos < end_charpos))
26723 break;
26724 }
26725 else if (EQ (glyph->object, after_string))
26726 {
26727 pos = string_buffer_position (after_string, end_charpos);
26728 if (!pos || (pos >= start_charpos && pos < end_charpos))
26729 break;
26730 }
26731 }
26732
26733 glyph++; /* first glyph to the right of the highlighted area */
26734 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26735 x += g->pixel_width;
26736 hlinfo->mouse_face_beg_x = x;
26737 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26738 }
26739
26740 /* If the highlight ends in a different row, compute GLYPH and END
26741 for the end row. Otherwise, reuse the values computed above for
26742 the row where the highlight begins. */
26743 if (r2 != r1)
26744 {
26745 if (!r2->reversed_p)
26746 {
26747 glyph = r2->glyphs[TEXT_AREA];
26748 end = glyph + r2->used[TEXT_AREA];
26749 x = r2->x;
26750 }
26751 else
26752 {
26753 end = r2->glyphs[TEXT_AREA] - 1;
26754 glyph = end + r2->used[TEXT_AREA];
26755 }
26756 }
26757
26758 if (!r2->reversed_p)
26759 {
26760 /* Skip truncation and continuation glyphs near the end of the
26761 row, and also blanks and stretch glyphs inserted by
26762 extend_face_to_end_of_line. */
26763 while (end > glyph
26764 && INTEGERP ((end - 1)->object))
26765 --end;
26766 /* Scan the rest of the glyph row from the end, looking for the
26767 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26768 DISP_STRING, or whose position is between START_CHARPOS
26769 and END_CHARPOS */
26770 for (--end;
26771 end > glyph
26772 && !INTEGERP (end->object)
26773 && !EQ (end->object, disp_string)
26774 && !(BUFFERP (end->object)
26775 && (end->charpos >= start_charpos
26776 && end->charpos < end_charpos));
26777 --end)
26778 {
26779 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26780 are present at buffer positions between START_CHARPOS and
26781 END_CHARPOS, or if they come from an overlay. */
26782 if (EQ (end->object, before_string))
26783 {
26784 pos = string_buffer_position (before_string, start_charpos);
26785 if (!pos || (pos >= start_charpos && pos < end_charpos))
26786 break;
26787 }
26788 else if (EQ (end->object, after_string))
26789 {
26790 pos = string_buffer_position (after_string, end_charpos);
26791 if (!pos || (pos >= start_charpos && pos < end_charpos))
26792 break;
26793 }
26794 }
26795 /* Find the X coordinate of the last glyph to be highlighted. */
26796 for (; glyph <= end; ++glyph)
26797 x += glyph->pixel_width;
26798
26799 hlinfo->mouse_face_end_x = x;
26800 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26801 }
26802 else
26803 {
26804 /* Skip truncation and continuation glyphs near the end of the
26805 row, and also blanks and stretch glyphs inserted by
26806 extend_face_to_end_of_line. */
26807 x = r2->x;
26808 end++;
26809 while (end < glyph
26810 && INTEGERP (end->object))
26811 {
26812 x += end->pixel_width;
26813 ++end;
26814 }
26815 /* Scan the rest of the glyph row from the end, looking for the
26816 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26817 DISP_STRING, or whose position is between START_CHARPOS
26818 and END_CHARPOS */
26819 for ( ;
26820 end < glyph
26821 && !INTEGERP (end->object)
26822 && !EQ (end->object, disp_string)
26823 && !(BUFFERP (end->object)
26824 && (end->charpos >= start_charpos
26825 && end->charpos < end_charpos));
26826 ++end)
26827 {
26828 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26829 are present at buffer positions between START_CHARPOS and
26830 END_CHARPOS, or if they come from an overlay. */
26831 if (EQ (end->object, before_string))
26832 {
26833 pos = string_buffer_position (before_string, start_charpos);
26834 if (!pos || (pos >= start_charpos && pos < end_charpos))
26835 break;
26836 }
26837 else if (EQ (end->object, after_string))
26838 {
26839 pos = string_buffer_position (after_string, end_charpos);
26840 if (!pos || (pos >= start_charpos && pos < end_charpos))
26841 break;
26842 }
26843 x += end->pixel_width;
26844 }
26845 /* If we exited the above loop because we arrived at the last
26846 glyph of the row, and its buffer position is still not in
26847 range, it means the last character in range is the preceding
26848 newline. Bump the end column and x values to get past the
26849 last glyph. */
26850 if (end == glyph
26851 && BUFFERP (end->object)
26852 && (end->charpos < start_charpos
26853 || end->charpos >= end_charpos))
26854 {
26855 x += end->pixel_width;
26856 ++end;
26857 }
26858 hlinfo->mouse_face_end_x = x;
26859 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26860 }
26861
26862 hlinfo->mouse_face_window = window;
26863 hlinfo->mouse_face_face_id
26864 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26865 mouse_charpos + 1,
26866 !hlinfo->mouse_face_hidden, -1);
26867 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26868 }
26869
26870 /* The following function is not used anymore (replaced with
26871 mouse_face_from_string_pos), but I leave it here for the time
26872 being, in case someone would. */
26873
26874 #if 0 /* not used */
26875
26876 /* Find the position of the glyph for position POS in OBJECT in
26877 window W's current matrix, and return in *X, *Y the pixel
26878 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26879
26880 RIGHT_P non-zero means return the position of the right edge of the
26881 glyph, RIGHT_P zero means return the left edge position.
26882
26883 If no glyph for POS exists in the matrix, return the position of
26884 the glyph with the next smaller position that is in the matrix, if
26885 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26886 exists in the matrix, return the position of the glyph with the
26887 next larger position in OBJECT.
26888
26889 Value is non-zero if a glyph was found. */
26890
26891 static int
26892 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26893 int *hpos, int *vpos, int *x, int *y, int right_p)
26894 {
26895 int yb = window_text_bottom_y (w);
26896 struct glyph_row *r;
26897 struct glyph *best_glyph = NULL;
26898 struct glyph_row *best_row = NULL;
26899 int best_x = 0;
26900
26901 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26902 r->enabled_p && r->y < yb;
26903 ++r)
26904 {
26905 struct glyph *g = r->glyphs[TEXT_AREA];
26906 struct glyph *e = g + r->used[TEXT_AREA];
26907 int gx;
26908
26909 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26910 if (EQ (g->object, object))
26911 {
26912 if (g->charpos == pos)
26913 {
26914 best_glyph = g;
26915 best_x = gx;
26916 best_row = r;
26917 goto found;
26918 }
26919 else if (best_glyph == NULL
26920 || ((eabs (g->charpos - pos)
26921 < eabs (best_glyph->charpos - pos))
26922 && (right_p
26923 ? g->charpos < pos
26924 : g->charpos > pos)))
26925 {
26926 best_glyph = g;
26927 best_x = gx;
26928 best_row = r;
26929 }
26930 }
26931 }
26932
26933 found:
26934
26935 if (best_glyph)
26936 {
26937 *x = best_x;
26938 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26939
26940 if (right_p)
26941 {
26942 *x += best_glyph->pixel_width;
26943 ++*hpos;
26944 }
26945
26946 *y = best_row->y;
26947 *vpos = best_row - w->current_matrix->rows;
26948 }
26949
26950 return best_glyph != NULL;
26951 }
26952 #endif /* not used */
26953
26954 /* Find the positions of the first and the last glyphs in window W's
26955 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26956 (assumed to be a string), and return in HLINFO's mouse_face_*
26957 members the pixel and column/row coordinates of those glyphs. */
26958
26959 static void
26960 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26961 Lisp_Object object,
26962 ptrdiff_t startpos, ptrdiff_t endpos)
26963 {
26964 int yb = window_text_bottom_y (w);
26965 struct glyph_row *r;
26966 struct glyph *g, *e;
26967 int gx;
26968 int found = 0;
26969
26970 /* Find the glyph row with at least one position in the range
26971 [STARTPOS..ENDPOS], and the first glyph in that row whose
26972 position belongs to that range. */
26973 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26974 r->enabled_p && r->y < yb;
26975 ++r)
26976 {
26977 if (!r->reversed_p)
26978 {
26979 g = r->glyphs[TEXT_AREA];
26980 e = g + r->used[TEXT_AREA];
26981 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26982 if (EQ (g->object, object)
26983 && startpos <= g->charpos && g->charpos <= endpos)
26984 {
26985 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26986 hlinfo->mouse_face_beg_y = r->y;
26987 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26988 hlinfo->mouse_face_beg_x = gx;
26989 found = 1;
26990 break;
26991 }
26992 }
26993 else
26994 {
26995 struct glyph *g1;
26996
26997 e = r->glyphs[TEXT_AREA];
26998 g = e + r->used[TEXT_AREA];
26999 for ( ; g > e; --g)
27000 if (EQ ((g-1)->object, object)
27001 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27002 {
27003 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27004 hlinfo->mouse_face_beg_y = r->y;
27005 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27006 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27007 gx += g1->pixel_width;
27008 hlinfo->mouse_face_beg_x = gx;
27009 found = 1;
27010 break;
27011 }
27012 }
27013 if (found)
27014 break;
27015 }
27016
27017 if (!found)
27018 return;
27019
27020 /* Starting with the next row, look for the first row which does NOT
27021 include any glyphs whose positions are in the range. */
27022 for (++r; r->enabled_p && r->y < yb; ++r)
27023 {
27024 g = r->glyphs[TEXT_AREA];
27025 e = g + r->used[TEXT_AREA];
27026 found = 0;
27027 for ( ; g < e; ++g)
27028 if (EQ (g->object, object)
27029 && startpos <= g->charpos && g->charpos <= endpos)
27030 {
27031 found = 1;
27032 break;
27033 }
27034 if (!found)
27035 break;
27036 }
27037
27038 /* The highlighted region ends on the previous row. */
27039 r--;
27040
27041 /* Set the end row and its vertical pixel coordinate. */
27042 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27043 hlinfo->mouse_face_end_y = r->y;
27044
27045 /* Compute and set the end column and the end column's horizontal
27046 pixel coordinate. */
27047 if (!r->reversed_p)
27048 {
27049 g = r->glyphs[TEXT_AREA];
27050 e = g + r->used[TEXT_AREA];
27051 for ( ; e > g; --e)
27052 if (EQ ((e-1)->object, object)
27053 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27054 break;
27055 hlinfo->mouse_face_end_col = e - g;
27056
27057 for (gx = r->x; g < e; ++g)
27058 gx += g->pixel_width;
27059 hlinfo->mouse_face_end_x = gx;
27060 }
27061 else
27062 {
27063 e = r->glyphs[TEXT_AREA];
27064 g = e + r->used[TEXT_AREA];
27065 for (gx = r->x ; e < g; ++e)
27066 {
27067 if (EQ (e->object, object)
27068 && startpos <= e->charpos && e->charpos <= endpos)
27069 break;
27070 gx += e->pixel_width;
27071 }
27072 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27073 hlinfo->mouse_face_end_x = gx;
27074 }
27075 }
27076
27077 #ifdef HAVE_WINDOW_SYSTEM
27078
27079 /* See if position X, Y is within a hot-spot of an image. */
27080
27081 static int
27082 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27083 {
27084 if (!CONSP (hot_spot))
27085 return 0;
27086
27087 if (EQ (XCAR (hot_spot), Qrect))
27088 {
27089 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27090 Lisp_Object rect = XCDR (hot_spot);
27091 Lisp_Object tem;
27092 if (!CONSP (rect))
27093 return 0;
27094 if (!CONSP (XCAR (rect)))
27095 return 0;
27096 if (!CONSP (XCDR (rect)))
27097 return 0;
27098 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27099 return 0;
27100 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27101 return 0;
27102 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27103 return 0;
27104 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27105 return 0;
27106 return 1;
27107 }
27108 else if (EQ (XCAR (hot_spot), Qcircle))
27109 {
27110 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27111 Lisp_Object circ = XCDR (hot_spot);
27112 Lisp_Object lr, lx0, ly0;
27113 if (CONSP (circ)
27114 && CONSP (XCAR (circ))
27115 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27116 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27117 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27118 {
27119 double r = XFLOATINT (lr);
27120 double dx = XINT (lx0) - x;
27121 double dy = XINT (ly0) - y;
27122 return (dx * dx + dy * dy <= r * r);
27123 }
27124 }
27125 else if (EQ (XCAR (hot_spot), Qpoly))
27126 {
27127 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27128 if (VECTORP (XCDR (hot_spot)))
27129 {
27130 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27131 Lisp_Object *poly = v->contents;
27132 ptrdiff_t n = v->header.size;
27133 ptrdiff_t i;
27134 int inside = 0;
27135 Lisp_Object lx, ly;
27136 int x0, y0;
27137
27138 /* Need an even number of coordinates, and at least 3 edges. */
27139 if (n < 6 || n & 1)
27140 return 0;
27141
27142 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27143 If count is odd, we are inside polygon. Pixels on edges
27144 may or may not be included depending on actual geometry of the
27145 polygon. */
27146 if ((lx = poly[n-2], !INTEGERP (lx))
27147 || (ly = poly[n-1], !INTEGERP (lx)))
27148 return 0;
27149 x0 = XINT (lx), y0 = XINT (ly);
27150 for (i = 0; i < n; i += 2)
27151 {
27152 int x1 = x0, y1 = y0;
27153 if ((lx = poly[i], !INTEGERP (lx))
27154 || (ly = poly[i+1], !INTEGERP (ly)))
27155 return 0;
27156 x0 = XINT (lx), y0 = XINT (ly);
27157
27158 /* Does this segment cross the X line? */
27159 if (x0 >= x)
27160 {
27161 if (x1 >= x)
27162 continue;
27163 }
27164 else if (x1 < x)
27165 continue;
27166 if (y > y0 && y > y1)
27167 continue;
27168 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27169 inside = !inside;
27170 }
27171 return inside;
27172 }
27173 }
27174 return 0;
27175 }
27176
27177 Lisp_Object
27178 find_hot_spot (Lisp_Object map, int x, int y)
27179 {
27180 while (CONSP (map))
27181 {
27182 if (CONSP (XCAR (map))
27183 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27184 return XCAR (map);
27185 map = XCDR (map);
27186 }
27187
27188 return Qnil;
27189 }
27190
27191 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27192 3, 3, 0,
27193 doc: /* Lookup in image map MAP coordinates X and Y.
27194 An image map is an alist where each element has the format (AREA ID PLIST).
27195 An AREA is specified as either a rectangle, a circle, or a polygon:
27196 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27197 pixel coordinates of the upper left and bottom right corners.
27198 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27199 and the radius of the circle; r may be a float or integer.
27200 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27201 vector describes one corner in the polygon.
27202 Returns the alist element for the first matching AREA in MAP. */)
27203 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27204 {
27205 if (NILP (map))
27206 return Qnil;
27207
27208 CHECK_NUMBER (x);
27209 CHECK_NUMBER (y);
27210
27211 return find_hot_spot (map,
27212 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27213 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27214 }
27215
27216
27217 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27218 static void
27219 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27220 {
27221 /* Do not change cursor shape while dragging mouse. */
27222 if (!NILP (do_mouse_tracking))
27223 return;
27224
27225 if (!NILP (pointer))
27226 {
27227 if (EQ (pointer, Qarrow))
27228 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27229 else if (EQ (pointer, Qhand))
27230 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27231 else if (EQ (pointer, Qtext))
27232 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27233 else if (EQ (pointer, intern ("hdrag")))
27234 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27235 #ifdef HAVE_X_WINDOWS
27236 else if (EQ (pointer, intern ("vdrag")))
27237 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27238 #endif
27239 else if (EQ (pointer, intern ("hourglass")))
27240 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27241 else if (EQ (pointer, Qmodeline))
27242 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27243 else
27244 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27245 }
27246
27247 if (cursor != No_Cursor)
27248 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27249 }
27250
27251 #endif /* HAVE_WINDOW_SYSTEM */
27252
27253 /* Take proper action when mouse has moved to the mode or header line
27254 or marginal area AREA of window W, x-position X and y-position Y.
27255 X is relative to the start of the text display area of W, so the
27256 width of bitmap areas and scroll bars must be subtracted to get a
27257 position relative to the start of the mode line. */
27258
27259 static void
27260 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27261 enum window_part area)
27262 {
27263 struct window *w = XWINDOW (window);
27264 struct frame *f = XFRAME (WGET (w, frame));
27265 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27266 #ifdef HAVE_WINDOW_SYSTEM
27267 Display_Info *dpyinfo;
27268 #endif
27269 Cursor cursor = No_Cursor;
27270 Lisp_Object pointer = Qnil;
27271 int dx, dy, width, height;
27272 ptrdiff_t charpos;
27273 Lisp_Object string, object = Qnil;
27274 Lisp_Object pos IF_LINT (= Qnil), help;
27275
27276 Lisp_Object mouse_face;
27277 int original_x_pixel = x;
27278 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27279 struct glyph_row *row IF_LINT (= 0);
27280
27281 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27282 {
27283 int x0;
27284 struct glyph *end;
27285
27286 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27287 returns them in row/column units! */
27288 string = mode_line_string (w, area, &x, &y, &charpos,
27289 &object, &dx, &dy, &width, &height);
27290
27291 row = (area == ON_MODE_LINE
27292 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27293 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27294
27295 /* Find the glyph under the mouse pointer. */
27296 if (row->mode_line_p && row->enabled_p)
27297 {
27298 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27299 end = glyph + row->used[TEXT_AREA];
27300
27301 for (x0 = original_x_pixel;
27302 glyph < end && x0 >= glyph->pixel_width;
27303 ++glyph)
27304 x0 -= glyph->pixel_width;
27305
27306 if (glyph >= end)
27307 glyph = NULL;
27308 }
27309 }
27310 else
27311 {
27312 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27313 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27314 returns them in row/column units! */
27315 string = marginal_area_string (w, area, &x, &y, &charpos,
27316 &object, &dx, &dy, &width, &height);
27317 }
27318
27319 help = Qnil;
27320
27321 #ifdef HAVE_WINDOW_SYSTEM
27322 if (IMAGEP (object))
27323 {
27324 Lisp_Object image_map, hotspot;
27325 if ((image_map = Fplist_get (XCDR (object), QCmap),
27326 !NILP (image_map))
27327 && (hotspot = find_hot_spot (image_map, dx, dy),
27328 CONSP (hotspot))
27329 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27330 {
27331 Lisp_Object plist;
27332
27333 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27334 If so, we could look for mouse-enter, mouse-leave
27335 properties in PLIST (and do something...). */
27336 hotspot = XCDR (hotspot);
27337 if (CONSP (hotspot)
27338 && (plist = XCAR (hotspot), CONSP (plist)))
27339 {
27340 pointer = Fplist_get (plist, Qpointer);
27341 if (NILP (pointer))
27342 pointer = Qhand;
27343 help = Fplist_get (plist, Qhelp_echo);
27344 if (!NILP (help))
27345 {
27346 help_echo_string = help;
27347 XSETWINDOW (help_echo_window, w);
27348 help_echo_object = WGET (w, buffer);
27349 help_echo_pos = charpos;
27350 }
27351 }
27352 }
27353 if (NILP (pointer))
27354 pointer = Fplist_get (XCDR (object), QCpointer);
27355 }
27356 #endif /* HAVE_WINDOW_SYSTEM */
27357
27358 if (STRINGP (string))
27359 pos = make_number (charpos);
27360
27361 /* Set the help text and mouse pointer. If the mouse is on a part
27362 of the mode line without any text (e.g. past the right edge of
27363 the mode line text), use the default help text and pointer. */
27364 if (STRINGP (string) || area == ON_MODE_LINE)
27365 {
27366 /* Arrange to display the help by setting the global variables
27367 help_echo_string, help_echo_object, and help_echo_pos. */
27368 if (NILP (help))
27369 {
27370 if (STRINGP (string))
27371 help = Fget_text_property (pos, Qhelp_echo, string);
27372
27373 if (!NILP (help))
27374 {
27375 help_echo_string = help;
27376 XSETWINDOW (help_echo_window, w);
27377 help_echo_object = string;
27378 help_echo_pos = charpos;
27379 }
27380 else if (area == ON_MODE_LINE)
27381 {
27382 Lisp_Object default_help
27383 = buffer_local_value_1 (Qmode_line_default_help_echo,
27384 WGET (w, buffer));
27385
27386 if (STRINGP (default_help))
27387 {
27388 help_echo_string = default_help;
27389 XSETWINDOW (help_echo_window, w);
27390 help_echo_object = Qnil;
27391 help_echo_pos = -1;
27392 }
27393 }
27394 }
27395
27396 #ifdef HAVE_WINDOW_SYSTEM
27397 /* Change the mouse pointer according to what is under it. */
27398 if (FRAME_WINDOW_P (f))
27399 {
27400 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27401 if (STRINGP (string))
27402 {
27403 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27404
27405 if (NILP (pointer))
27406 pointer = Fget_text_property (pos, Qpointer, string);
27407
27408 /* Change the mouse pointer according to what is under X/Y. */
27409 if (NILP (pointer)
27410 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27411 {
27412 Lisp_Object map;
27413 map = Fget_text_property (pos, Qlocal_map, string);
27414 if (!KEYMAPP (map))
27415 map = Fget_text_property (pos, Qkeymap, string);
27416 if (!KEYMAPP (map))
27417 cursor = dpyinfo->vertical_scroll_bar_cursor;
27418 }
27419 }
27420 else
27421 /* Default mode-line pointer. */
27422 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27423 }
27424 #endif
27425 }
27426
27427 /* Change the mouse face according to what is under X/Y. */
27428 if (STRINGP (string))
27429 {
27430 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27431 if (!NILP (mouse_face)
27432 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27433 && glyph)
27434 {
27435 Lisp_Object b, e;
27436
27437 struct glyph * tmp_glyph;
27438
27439 int gpos;
27440 int gseq_length;
27441 int total_pixel_width;
27442 ptrdiff_t begpos, endpos, ignore;
27443
27444 int vpos, hpos;
27445
27446 b = Fprevious_single_property_change (make_number (charpos + 1),
27447 Qmouse_face, string, Qnil);
27448 if (NILP (b))
27449 begpos = 0;
27450 else
27451 begpos = XINT (b);
27452
27453 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27454 if (NILP (e))
27455 endpos = SCHARS (string);
27456 else
27457 endpos = XINT (e);
27458
27459 /* Calculate the glyph position GPOS of GLYPH in the
27460 displayed string, relative to the beginning of the
27461 highlighted part of the string.
27462
27463 Note: GPOS is different from CHARPOS. CHARPOS is the
27464 position of GLYPH in the internal string object. A mode
27465 line string format has structures which are converted to
27466 a flattened string by the Emacs Lisp interpreter. The
27467 internal string is an element of those structures. The
27468 displayed string is the flattened string. */
27469 tmp_glyph = row_start_glyph;
27470 while (tmp_glyph < glyph
27471 && (!(EQ (tmp_glyph->object, glyph->object)
27472 && begpos <= tmp_glyph->charpos
27473 && tmp_glyph->charpos < endpos)))
27474 tmp_glyph++;
27475 gpos = glyph - tmp_glyph;
27476
27477 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27478 the highlighted part of the displayed string to which
27479 GLYPH belongs. Note: GSEQ_LENGTH is different from
27480 SCHARS (STRING), because the latter returns the length of
27481 the internal string. */
27482 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27483 tmp_glyph > glyph
27484 && (!(EQ (tmp_glyph->object, glyph->object)
27485 && begpos <= tmp_glyph->charpos
27486 && tmp_glyph->charpos < endpos));
27487 tmp_glyph--)
27488 ;
27489 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27490
27491 /* Calculate the total pixel width of all the glyphs between
27492 the beginning of the highlighted area and GLYPH. */
27493 total_pixel_width = 0;
27494 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27495 total_pixel_width += tmp_glyph->pixel_width;
27496
27497 /* Pre calculation of re-rendering position. Note: X is in
27498 column units here, after the call to mode_line_string or
27499 marginal_area_string. */
27500 hpos = x - gpos;
27501 vpos = (area == ON_MODE_LINE
27502 ? (w->current_matrix)->nrows - 1
27503 : 0);
27504
27505 /* If GLYPH's position is included in the region that is
27506 already drawn in mouse face, we have nothing to do. */
27507 if ( EQ (window, hlinfo->mouse_face_window)
27508 && (!row->reversed_p
27509 ? (hlinfo->mouse_face_beg_col <= hpos
27510 && hpos < hlinfo->mouse_face_end_col)
27511 /* In R2L rows we swap BEG and END, see below. */
27512 : (hlinfo->mouse_face_end_col <= hpos
27513 && hpos < hlinfo->mouse_face_beg_col))
27514 && hlinfo->mouse_face_beg_row == vpos )
27515 return;
27516
27517 if (clear_mouse_face (hlinfo))
27518 cursor = No_Cursor;
27519
27520 if (!row->reversed_p)
27521 {
27522 hlinfo->mouse_face_beg_col = hpos;
27523 hlinfo->mouse_face_beg_x = original_x_pixel
27524 - (total_pixel_width + dx);
27525 hlinfo->mouse_face_end_col = hpos + gseq_length;
27526 hlinfo->mouse_face_end_x = 0;
27527 }
27528 else
27529 {
27530 /* In R2L rows, show_mouse_face expects BEG and END
27531 coordinates to be swapped. */
27532 hlinfo->mouse_face_end_col = hpos;
27533 hlinfo->mouse_face_end_x = original_x_pixel
27534 - (total_pixel_width + dx);
27535 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27536 hlinfo->mouse_face_beg_x = 0;
27537 }
27538
27539 hlinfo->mouse_face_beg_row = vpos;
27540 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27541 hlinfo->mouse_face_beg_y = 0;
27542 hlinfo->mouse_face_end_y = 0;
27543 hlinfo->mouse_face_past_end = 0;
27544 hlinfo->mouse_face_window = window;
27545
27546 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27547 charpos,
27548 0, 0, 0,
27549 &ignore,
27550 glyph->face_id,
27551 1);
27552 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27553
27554 if (NILP (pointer))
27555 pointer = Qhand;
27556 }
27557 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27558 clear_mouse_face (hlinfo);
27559 }
27560 #ifdef HAVE_WINDOW_SYSTEM
27561 if (FRAME_WINDOW_P (f))
27562 define_frame_cursor1 (f, cursor, pointer);
27563 #endif
27564 }
27565
27566
27567 /* EXPORT:
27568 Take proper action when the mouse has moved to position X, Y on
27569 frame F as regards highlighting characters that have mouse-face
27570 properties. Also de-highlighting chars where the mouse was before.
27571 X and Y can be negative or out of range. */
27572
27573 void
27574 note_mouse_highlight (struct frame *f, int x, int y)
27575 {
27576 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27577 enum window_part part = ON_NOTHING;
27578 Lisp_Object window;
27579 struct window *w;
27580 Cursor cursor = No_Cursor;
27581 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27582 struct buffer *b;
27583
27584 /* When a menu is active, don't highlight because this looks odd. */
27585 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27586 if (popup_activated ())
27587 return;
27588 #endif
27589
27590 if (NILP (Vmouse_highlight)
27591 || !f->glyphs_initialized_p
27592 || f->pointer_invisible)
27593 return;
27594
27595 hlinfo->mouse_face_mouse_x = x;
27596 hlinfo->mouse_face_mouse_y = y;
27597 hlinfo->mouse_face_mouse_frame = f;
27598
27599 if (hlinfo->mouse_face_defer)
27600 return;
27601
27602 if (gc_in_progress)
27603 {
27604 hlinfo->mouse_face_deferred_gc = 1;
27605 return;
27606 }
27607
27608 /* Which window is that in? */
27609 window = window_from_coordinates (f, x, y, &part, 1);
27610
27611 /* If displaying active text in another window, clear that. */
27612 if (! EQ (window, hlinfo->mouse_face_window)
27613 /* Also clear if we move out of text area in same window. */
27614 || (!NILP (hlinfo->mouse_face_window)
27615 && !NILP (window)
27616 && part != ON_TEXT
27617 && part != ON_MODE_LINE
27618 && part != ON_HEADER_LINE))
27619 clear_mouse_face (hlinfo);
27620
27621 /* Not on a window -> return. */
27622 if (!WINDOWP (window))
27623 return;
27624
27625 /* Reset help_echo_string. It will get recomputed below. */
27626 help_echo_string = Qnil;
27627
27628 /* Convert to window-relative pixel coordinates. */
27629 w = XWINDOW (window);
27630 frame_to_window_pixel_xy (w, &x, &y);
27631
27632 #ifdef HAVE_WINDOW_SYSTEM
27633 /* Handle tool-bar window differently since it doesn't display a
27634 buffer. */
27635 if (EQ (window, f->tool_bar_window))
27636 {
27637 note_tool_bar_highlight (f, x, y);
27638 return;
27639 }
27640 #endif
27641
27642 /* Mouse is on the mode, header line or margin? */
27643 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27644 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27645 {
27646 note_mode_line_or_margin_highlight (window, x, y, part);
27647 return;
27648 }
27649
27650 #ifdef HAVE_WINDOW_SYSTEM
27651 if (part == ON_VERTICAL_BORDER)
27652 {
27653 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27654 help_echo_string = build_string ("drag-mouse-1: resize");
27655 }
27656 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27657 || part == ON_SCROLL_BAR)
27658 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27659 else
27660 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27661 #endif
27662
27663 /* Are we in a window whose display is up to date?
27664 And verify the buffer's text has not changed. */
27665 b = XBUFFER (WGET (w, buffer));
27666 if (part == ON_TEXT
27667 && EQ (WGET (w, window_end_valid), WGET (w, buffer))
27668 && w->last_modified == BUF_MODIFF (b)
27669 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27670 {
27671 int hpos, vpos, dx, dy, area = LAST_AREA;
27672 ptrdiff_t pos;
27673 struct glyph *glyph;
27674 Lisp_Object object;
27675 Lisp_Object mouse_face = Qnil, position;
27676 Lisp_Object *overlay_vec = NULL;
27677 ptrdiff_t i, noverlays;
27678 struct buffer *obuf;
27679 ptrdiff_t obegv, ozv;
27680 int same_region;
27681
27682 /* Find the glyph under X/Y. */
27683 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27684
27685 #ifdef HAVE_WINDOW_SYSTEM
27686 /* Look for :pointer property on image. */
27687 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27688 {
27689 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27690 if (img != NULL && IMAGEP (img->spec))
27691 {
27692 Lisp_Object image_map, hotspot;
27693 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27694 !NILP (image_map))
27695 && (hotspot = find_hot_spot (image_map,
27696 glyph->slice.img.x + dx,
27697 glyph->slice.img.y + dy),
27698 CONSP (hotspot))
27699 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27700 {
27701 Lisp_Object plist;
27702
27703 /* Could check XCAR (hotspot) to see if we enter/leave
27704 this hot-spot.
27705 If so, we could look for mouse-enter, mouse-leave
27706 properties in PLIST (and do something...). */
27707 hotspot = XCDR (hotspot);
27708 if (CONSP (hotspot)
27709 && (plist = XCAR (hotspot), CONSP (plist)))
27710 {
27711 pointer = Fplist_get (plist, Qpointer);
27712 if (NILP (pointer))
27713 pointer = Qhand;
27714 help_echo_string = Fplist_get (plist, Qhelp_echo);
27715 if (!NILP (help_echo_string))
27716 {
27717 help_echo_window = window;
27718 help_echo_object = glyph->object;
27719 help_echo_pos = glyph->charpos;
27720 }
27721 }
27722 }
27723 if (NILP (pointer))
27724 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27725 }
27726 }
27727 #endif /* HAVE_WINDOW_SYSTEM */
27728
27729 /* Clear mouse face if X/Y not over text. */
27730 if (glyph == NULL
27731 || area != TEXT_AREA
27732 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27733 /* Glyph's OBJECT is an integer for glyphs inserted by the
27734 display engine for its internal purposes, like truncation
27735 and continuation glyphs and blanks beyond the end of
27736 line's text on text terminals. If we are over such a
27737 glyph, we are not over any text. */
27738 || INTEGERP (glyph->object)
27739 /* R2L rows have a stretch glyph at their front, which
27740 stands for no text, whereas L2R rows have no glyphs at
27741 all beyond the end of text. Treat such stretch glyphs
27742 like we do with NULL glyphs in L2R rows. */
27743 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27744 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27745 && glyph->type == STRETCH_GLYPH
27746 && glyph->avoid_cursor_p))
27747 {
27748 if (clear_mouse_face (hlinfo))
27749 cursor = No_Cursor;
27750 #ifdef HAVE_WINDOW_SYSTEM
27751 if (FRAME_WINDOW_P (f) && NILP (pointer))
27752 {
27753 if (area != TEXT_AREA)
27754 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27755 else
27756 pointer = Vvoid_text_area_pointer;
27757 }
27758 #endif
27759 goto set_cursor;
27760 }
27761
27762 pos = glyph->charpos;
27763 object = glyph->object;
27764 if (!STRINGP (object) && !BUFFERP (object))
27765 goto set_cursor;
27766
27767 /* If we get an out-of-range value, return now; avoid an error. */
27768 if (BUFFERP (object) && pos > BUF_Z (b))
27769 goto set_cursor;
27770
27771 /* Make the window's buffer temporarily current for
27772 overlays_at and compute_char_face. */
27773 obuf = current_buffer;
27774 current_buffer = b;
27775 obegv = BEGV;
27776 ozv = ZV;
27777 BEGV = BEG;
27778 ZV = Z;
27779
27780 /* Is this char mouse-active or does it have help-echo? */
27781 position = make_number (pos);
27782
27783 if (BUFFERP (object))
27784 {
27785 /* Put all the overlays we want in a vector in overlay_vec. */
27786 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27787 /* Sort overlays into increasing priority order. */
27788 noverlays = sort_overlays (overlay_vec, noverlays, w);
27789 }
27790 else
27791 noverlays = 0;
27792
27793 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27794
27795 if (same_region)
27796 cursor = No_Cursor;
27797
27798 /* Check mouse-face highlighting. */
27799 if (! same_region
27800 /* If there exists an overlay with mouse-face overlapping
27801 the one we are currently highlighting, we have to
27802 check if we enter the overlapping overlay, and then
27803 highlight only that. */
27804 || (OVERLAYP (hlinfo->mouse_face_overlay)
27805 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27806 {
27807 /* Find the highest priority overlay with a mouse-face. */
27808 Lisp_Object overlay = Qnil;
27809 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27810 {
27811 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27812 if (!NILP (mouse_face))
27813 overlay = overlay_vec[i];
27814 }
27815
27816 /* If we're highlighting the same overlay as before, there's
27817 no need to do that again. */
27818 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27819 goto check_help_echo;
27820 hlinfo->mouse_face_overlay = overlay;
27821
27822 /* Clear the display of the old active region, if any. */
27823 if (clear_mouse_face (hlinfo))
27824 cursor = No_Cursor;
27825
27826 /* If no overlay applies, get a text property. */
27827 if (NILP (overlay))
27828 mouse_face = Fget_text_property (position, Qmouse_face, object);
27829
27830 /* Next, compute the bounds of the mouse highlighting and
27831 display it. */
27832 if (!NILP (mouse_face) && STRINGP (object))
27833 {
27834 /* The mouse-highlighting comes from a display string
27835 with a mouse-face. */
27836 Lisp_Object s, e;
27837 ptrdiff_t ignore;
27838
27839 s = Fprevious_single_property_change
27840 (make_number (pos + 1), Qmouse_face, object, Qnil);
27841 e = Fnext_single_property_change
27842 (position, Qmouse_face, object, Qnil);
27843 if (NILP (s))
27844 s = make_number (0);
27845 if (NILP (e))
27846 e = make_number (SCHARS (object) - 1);
27847 mouse_face_from_string_pos (w, hlinfo, object,
27848 XINT (s), XINT (e));
27849 hlinfo->mouse_face_past_end = 0;
27850 hlinfo->mouse_face_window = window;
27851 hlinfo->mouse_face_face_id
27852 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27853 glyph->face_id, 1);
27854 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27855 cursor = No_Cursor;
27856 }
27857 else
27858 {
27859 /* The mouse-highlighting, if any, comes from an overlay
27860 or text property in the buffer. */
27861 Lisp_Object buffer IF_LINT (= Qnil);
27862 Lisp_Object disp_string IF_LINT (= Qnil);
27863
27864 if (STRINGP (object))
27865 {
27866 /* If we are on a display string with no mouse-face,
27867 check if the text under it has one. */
27868 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27869 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27870 pos = string_buffer_position (object, start);
27871 if (pos > 0)
27872 {
27873 mouse_face = get_char_property_and_overlay
27874 (make_number (pos), Qmouse_face, WGET (w, buffer), &overlay);
27875 buffer = WGET (w, buffer);
27876 disp_string = object;
27877 }
27878 }
27879 else
27880 {
27881 buffer = object;
27882 disp_string = Qnil;
27883 }
27884
27885 if (!NILP (mouse_face))
27886 {
27887 Lisp_Object before, after;
27888 Lisp_Object before_string, after_string;
27889 /* To correctly find the limits of mouse highlight
27890 in a bidi-reordered buffer, we must not use the
27891 optimization of limiting the search in
27892 previous-single-property-change and
27893 next-single-property-change, because
27894 rows_from_pos_range needs the real start and end
27895 positions to DTRT in this case. That's because
27896 the first row visible in a window does not
27897 necessarily display the character whose position
27898 is the smallest. */
27899 Lisp_Object lim1 =
27900 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27901 ? Fmarker_position (WGET (w, start))
27902 : Qnil;
27903 Lisp_Object lim2 =
27904 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27905 ? make_number (BUF_Z (XBUFFER (buffer))
27906 - XFASTINT (WGET (w, window_end_pos)))
27907 : Qnil;
27908
27909 if (NILP (overlay))
27910 {
27911 /* Handle the text property case. */
27912 before = Fprevious_single_property_change
27913 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27914 after = Fnext_single_property_change
27915 (make_number (pos), Qmouse_face, buffer, lim2);
27916 before_string = after_string = Qnil;
27917 }
27918 else
27919 {
27920 /* Handle the overlay case. */
27921 before = Foverlay_start (overlay);
27922 after = Foverlay_end (overlay);
27923 before_string = Foverlay_get (overlay, Qbefore_string);
27924 after_string = Foverlay_get (overlay, Qafter_string);
27925
27926 if (!STRINGP (before_string)) before_string = Qnil;
27927 if (!STRINGP (after_string)) after_string = Qnil;
27928 }
27929
27930 mouse_face_from_buffer_pos (window, hlinfo, pos,
27931 NILP (before)
27932 ? 1
27933 : XFASTINT (before),
27934 NILP (after)
27935 ? BUF_Z (XBUFFER (buffer))
27936 : XFASTINT (after),
27937 before_string, after_string,
27938 disp_string);
27939 cursor = No_Cursor;
27940 }
27941 }
27942 }
27943
27944 check_help_echo:
27945
27946 /* Look for a `help-echo' property. */
27947 if (NILP (help_echo_string)) {
27948 Lisp_Object help, overlay;
27949
27950 /* Check overlays first. */
27951 help = overlay = Qnil;
27952 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27953 {
27954 overlay = overlay_vec[i];
27955 help = Foverlay_get (overlay, Qhelp_echo);
27956 }
27957
27958 if (!NILP (help))
27959 {
27960 help_echo_string = help;
27961 help_echo_window = window;
27962 help_echo_object = overlay;
27963 help_echo_pos = pos;
27964 }
27965 else
27966 {
27967 Lisp_Object obj = glyph->object;
27968 ptrdiff_t charpos = glyph->charpos;
27969
27970 /* Try text properties. */
27971 if (STRINGP (obj)
27972 && charpos >= 0
27973 && charpos < SCHARS (obj))
27974 {
27975 help = Fget_text_property (make_number (charpos),
27976 Qhelp_echo, obj);
27977 if (NILP (help))
27978 {
27979 /* If the string itself doesn't specify a help-echo,
27980 see if the buffer text ``under'' it does. */
27981 struct glyph_row *r
27982 = MATRIX_ROW (w->current_matrix, vpos);
27983 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27984 ptrdiff_t p = string_buffer_position (obj, start);
27985 if (p > 0)
27986 {
27987 help = Fget_char_property (make_number (p),
27988 Qhelp_echo, WGET (w, buffer));
27989 if (!NILP (help))
27990 {
27991 charpos = p;
27992 obj = WGET (w, buffer);
27993 }
27994 }
27995 }
27996 }
27997 else if (BUFFERP (obj)
27998 && charpos >= BEGV
27999 && charpos < ZV)
28000 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28001 obj);
28002
28003 if (!NILP (help))
28004 {
28005 help_echo_string = help;
28006 help_echo_window = window;
28007 help_echo_object = obj;
28008 help_echo_pos = charpos;
28009 }
28010 }
28011 }
28012
28013 #ifdef HAVE_WINDOW_SYSTEM
28014 /* Look for a `pointer' property. */
28015 if (FRAME_WINDOW_P (f) && NILP (pointer))
28016 {
28017 /* Check overlays first. */
28018 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28019 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28020
28021 if (NILP (pointer))
28022 {
28023 Lisp_Object obj = glyph->object;
28024 ptrdiff_t charpos = glyph->charpos;
28025
28026 /* Try text properties. */
28027 if (STRINGP (obj)
28028 && charpos >= 0
28029 && charpos < SCHARS (obj))
28030 {
28031 pointer = Fget_text_property (make_number (charpos),
28032 Qpointer, obj);
28033 if (NILP (pointer))
28034 {
28035 /* If the string itself doesn't specify a pointer,
28036 see if the buffer text ``under'' it does. */
28037 struct glyph_row *r
28038 = MATRIX_ROW (w->current_matrix, vpos);
28039 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28040 ptrdiff_t p = string_buffer_position (obj, start);
28041 if (p > 0)
28042 pointer = Fget_char_property (make_number (p),
28043 Qpointer, WGET (w, buffer));
28044 }
28045 }
28046 else if (BUFFERP (obj)
28047 && charpos >= BEGV
28048 && charpos < ZV)
28049 pointer = Fget_text_property (make_number (charpos),
28050 Qpointer, obj);
28051 }
28052 }
28053 #endif /* HAVE_WINDOW_SYSTEM */
28054
28055 BEGV = obegv;
28056 ZV = ozv;
28057 current_buffer = obuf;
28058 }
28059
28060 set_cursor:
28061
28062 #ifdef HAVE_WINDOW_SYSTEM
28063 if (FRAME_WINDOW_P (f))
28064 define_frame_cursor1 (f, cursor, pointer);
28065 #else
28066 /* This is here to prevent a compiler error, about "label at end of
28067 compound statement". */
28068 return;
28069 #endif
28070 }
28071
28072
28073 /* EXPORT for RIF:
28074 Clear any mouse-face on window W. This function is part of the
28075 redisplay interface, and is called from try_window_id and similar
28076 functions to ensure the mouse-highlight is off. */
28077
28078 void
28079 x_clear_window_mouse_face (struct window *w)
28080 {
28081 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (WGET (w, frame)));
28082 Lisp_Object window;
28083
28084 BLOCK_INPUT;
28085 XSETWINDOW (window, w);
28086 if (EQ (window, hlinfo->mouse_face_window))
28087 clear_mouse_face (hlinfo);
28088 UNBLOCK_INPUT;
28089 }
28090
28091
28092 /* EXPORT:
28093 Just discard the mouse face information for frame F, if any.
28094 This is used when the size of F is changed. */
28095
28096 void
28097 cancel_mouse_face (struct frame *f)
28098 {
28099 Lisp_Object window;
28100 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28101
28102 window = hlinfo->mouse_face_window;
28103 if (! NILP (window) && XFRAME (WGET (XWINDOW (window), frame)) == f)
28104 {
28105 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28106 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28107 hlinfo->mouse_face_window = Qnil;
28108 }
28109 }
28110
28111
28112 \f
28113 /***********************************************************************
28114 Exposure Events
28115 ***********************************************************************/
28116
28117 #ifdef HAVE_WINDOW_SYSTEM
28118
28119 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28120 which intersects rectangle R. R is in window-relative coordinates. */
28121
28122 static void
28123 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28124 enum glyph_row_area area)
28125 {
28126 struct glyph *first = row->glyphs[area];
28127 struct glyph *end = row->glyphs[area] + row->used[area];
28128 struct glyph *last;
28129 int first_x, start_x, x;
28130
28131 if (area == TEXT_AREA && row->fill_line_p)
28132 /* If row extends face to end of line write the whole line. */
28133 draw_glyphs (w, 0, row, area,
28134 0, row->used[area],
28135 DRAW_NORMAL_TEXT, 0);
28136 else
28137 {
28138 /* Set START_X to the window-relative start position for drawing glyphs of
28139 AREA. The first glyph of the text area can be partially visible.
28140 The first glyphs of other areas cannot. */
28141 start_x = window_box_left_offset (w, area);
28142 x = start_x;
28143 if (area == TEXT_AREA)
28144 x += row->x;
28145
28146 /* Find the first glyph that must be redrawn. */
28147 while (first < end
28148 && x + first->pixel_width < r->x)
28149 {
28150 x += first->pixel_width;
28151 ++first;
28152 }
28153
28154 /* Find the last one. */
28155 last = first;
28156 first_x = x;
28157 while (last < end
28158 && x < r->x + r->width)
28159 {
28160 x += last->pixel_width;
28161 ++last;
28162 }
28163
28164 /* Repaint. */
28165 if (last > first)
28166 draw_glyphs (w, first_x - start_x, row, area,
28167 first - row->glyphs[area], last - row->glyphs[area],
28168 DRAW_NORMAL_TEXT, 0);
28169 }
28170 }
28171
28172
28173 /* Redraw the parts of the glyph row ROW on window W intersecting
28174 rectangle R. R is in window-relative coordinates. Value is
28175 non-zero if mouse-face was overwritten. */
28176
28177 static int
28178 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28179 {
28180 eassert (row->enabled_p);
28181
28182 if (row->mode_line_p || w->pseudo_window_p)
28183 draw_glyphs (w, 0, row, TEXT_AREA,
28184 0, row->used[TEXT_AREA],
28185 DRAW_NORMAL_TEXT, 0);
28186 else
28187 {
28188 if (row->used[LEFT_MARGIN_AREA])
28189 expose_area (w, row, r, LEFT_MARGIN_AREA);
28190 if (row->used[TEXT_AREA])
28191 expose_area (w, row, r, TEXT_AREA);
28192 if (row->used[RIGHT_MARGIN_AREA])
28193 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28194 draw_row_fringe_bitmaps (w, row);
28195 }
28196
28197 return row->mouse_face_p;
28198 }
28199
28200
28201 /* Redraw those parts of glyphs rows during expose event handling that
28202 overlap other rows. Redrawing of an exposed line writes over parts
28203 of lines overlapping that exposed line; this function fixes that.
28204
28205 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28206 row in W's current matrix that is exposed and overlaps other rows.
28207 LAST_OVERLAPPING_ROW is the last such row. */
28208
28209 static void
28210 expose_overlaps (struct window *w,
28211 struct glyph_row *first_overlapping_row,
28212 struct glyph_row *last_overlapping_row,
28213 XRectangle *r)
28214 {
28215 struct glyph_row *row;
28216
28217 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28218 if (row->overlapping_p)
28219 {
28220 eassert (row->enabled_p && !row->mode_line_p);
28221
28222 row->clip = r;
28223 if (row->used[LEFT_MARGIN_AREA])
28224 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28225
28226 if (row->used[TEXT_AREA])
28227 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28228
28229 if (row->used[RIGHT_MARGIN_AREA])
28230 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28231 row->clip = NULL;
28232 }
28233 }
28234
28235
28236 /* Return non-zero if W's cursor intersects rectangle R. */
28237
28238 static int
28239 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28240 {
28241 XRectangle cr, result;
28242 struct glyph *cursor_glyph;
28243 struct glyph_row *row;
28244
28245 if (w->phys_cursor.vpos >= 0
28246 && w->phys_cursor.vpos < w->current_matrix->nrows
28247 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28248 row->enabled_p)
28249 && row->cursor_in_fringe_p)
28250 {
28251 /* Cursor is in the fringe. */
28252 cr.x = window_box_right_offset (w,
28253 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28254 ? RIGHT_MARGIN_AREA
28255 : TEXT_AREA));
28256 cr.y = row->y;
28257 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28258 cr.height = row->height;
28259 return x_intersect_rectangles (&cr, r, &result);
28260 }
28261
28262 cursor_glyph = get_phys_cursor_glyph (w);
28263 if (cursor_glyph)
28264 {
28265 /* r is relative to W's box, but w->phys_cursor.x is relative
28266 to left edge of W's TEXT area. Adjust it. */
28267 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28268 cr.y = w->phys_cursor.y;
28269 cr.width = cursor_glyph->pixel_width;
28270 cr.height = w->phys_cursor_height;
28271 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28272 I assume the effect is the same -- and this is portable. */
28273 return x_intersect_rectangles (&cr, r, &result);
28274 }
28275 /* If we don't understand the format, pretend we're not in the hot-spot. */
28276 return 0;
28277 }
28278
28279
28280 /* EXPORT:
28281 Draw a vertical window border to the right of window W if W doesn't
28282 have vertical scroll bars. */
28283
28284 void
28285 x_draw_vertical_border (struct window *w)
28286 {
28287 struct frame *f = XFRAME (WINDOW_FRAME (w));
28288
28289 /* We could do better, if we knew what type of scroll-bar the adjacent
28290 windows (on either side) have... But we don't :-(
28291 However, I think this works ok. ++KFS 2003-04-25 */
28292
28293 /* Redraw borders between horizontally adjacent windows. Don't
28294 do it for frames with vertical scroll bars because either the
28295 right scroll bar of a window, or the left scroll bar of its
28296 neighbor will suffice as a border. */
28297 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (WGET (w, frame))))
28298 return;
28299
28300 if (!WINDOW_RIGHTMOST_P (w)
28301 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28302 {
28303 int x0, x1, y0, y1;
28304
28305 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28306 y1 -= 1;
28307
28308 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28309 x1 -= 1;
28310
28311 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28312 }
28313 else if (!WINDOW_LEFTMOST_P (w)
28314 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28315 {
28316 int x0, x1, y0, y1;
28317
28318 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28319 y1 -= 1;
28320
28321 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28322 x0 -= 1;
28323
28324 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28325 }
28326 }
28327
28328
28329 /* Redraw the part of window W intersection rectangle FR. Pixel
28330 coordinates in FR are frame-relative. Call this function with
28331 input blocked. Value is non-zero if the exposure overwrites
28332 mouse-face. */
28333
28334 static int
28335 expose_window (struct window *w, XRectangle *fr)
28336 {
28337 struct frame *f = XFRAME (WGET (w, frame));
28338 XRectangle wr, r;
28339 int mouse_face_overwritten_p = 0;
28340
28341 /* If window is not yet fully initialized, do nothing. This can
28342 happen when toolkit scroll bars are used and a window is split.
28343 Reconfiguring the scroll bar will generate an expose for a newly
28344 created window. */
28345 if (w->current_matrix == NULL)
28346 return 0;
28347
28348 /* When we're currently updating the window, display and current
28349 matrix usually don't agree. Arrange for a thorough display
28350 later. */
28351 if (w == updated_window)
28352 {
28353 SET_FRAME_GARBAGED (f);
28354 return 0;
28355 }
28356
28357 /* Frame-relative pixel rectangle of W. */
28358 wr.x = WINDOW_LEFT_EDGE_X (w);
28359 wr.y = WINDOW_TOP_EDGE_Y (w);
28360 wr.width = WINDOW_TOTAL_WIDTH (w);
28361 wr.height = WINDOW_TOTAL_HEIGHT (w);
28362
28363 if (x_intersect_rectangles (fr, &wr, &r))
28364 {
28365 int yb = window_text_bottom_y (w);
28366 struct glyph_row *row;
28367 int cursor_cleared_p, phys_cursor_on_p;
28368 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28369
28370 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28371 r.x, r.y, r.width, r.height));
28372
28373 /* Convert to window coordinates. */
28374 r.x -= WINDOW_LEFT_EDGE_X (w);
28375 r.y -= WINDOW_TOP_EDGE_Y (w);
28376
28377 /* Turn off the cursor. */
28378 if (!w->pseudo_window_p
28379 && phys_cursor_in_rect_p (w, &r))
28380 {
28381 x_clear_cursor (w);
28382 cursor_cleared_p = 1;
28383 }
28384 else
28385 cursor_cleared_p = 0;
28386
28387 /* If the row containing the cursor extends face to end of line,
28388 then expose_area might overwrite the cursor outside the
28389 rectangle and thus notice_overwritten_cursor might clear
28390 w->phys_cursor_on_p. We remember the original value and
28391 check later if it is changed. */
28392 phys_cursor_on_p = w->phys_cursor_on_p;
28393
28394 /* Update lines intersecting rectangle R. */
28395 first_overlapping_row = last_overlapping_row = NULL;
28396 for (row = w->current_matrix->rows;
28397 row->enabled_p;
28398 ++row)
28399 {
28400 int y0 = row->y;
28401 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28402
28403 if ((y0 >= r.y && y0 < r.y + r.height)
28404 || (y1 > r.y && y1 < r.y + r.height)
28405 || (r.y >= y0 && r.y < y1)
28406 || (r.y + r.height > y0 && r.y + r.height < y1))
28407 {
28408 /* A header line may be overlapping, but there is no need
28409 to fix overlapping areas for them. KFS 2005-02-12 */
28410 if (row->overlapping_p && !row->mode_line_p)
28411 {
28412 if (first_overlapping_row == NULL)
28413 first_overlapping_row = row;
28414 last_overlapping_row = row;
28415 }
28416
28417 row->clip = fr;
28418 if (expose_line (w, row, &r))
28419 mouse_face_overwritten_p = 1;
28420 row->clip = NULL;
28421 }
28422 else if (row->overlapping_p)
28423 {
28424 /* We must redraw a row overlapping the exposed area. */
28425 if (y0 < r.y
28426 ? y0 + row->phys_height > r.y
28427 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28428 {
28429 if (first_overlapping_row == NULL)
28430 first_overlapping_row = row;
28431 last_overlapping_row = row;
28432 }
28433 }
28434
28435 if (y1 >= yb)
28436 break;
28437 }
28438
28439 /* Display the mode line if there is one. */
28440 if (WINDOW_WANTS_MODELINE_P (w)
28441 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28442 row->enabled_p)
28443 && row->y < r.y + r.height)
28444 {
28445 if (expose_line (w, row, &r))
28446 mouse_face_overwritten_p = 1;
28447 }
28448
28449 if (!w->pseudo_window_p)
28450 {
28451 /* Fix the display of overlapping rows. */
28452 if (first_overlapping_row)
28453 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28454 fr);
28455
28456 /* Draw border between windows. */
28457 x_draw_vertical_border (w);
28458
28459 /* Turn the cursor on again. */
28460 if (cursor_cleared_p
28461 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28462 update_window_cursor (w, 1);
28463 }
28464 }
28465
28466 return mouse_face_overwritten_p;
28467 }
28468
28469
28470
28471 /* Redraw (parts) of all windows in the window tree rooted at W that
28472 intersect R. R contains frame pixel coordinates. Value is
28473 non-zero if the exposure overwrites mouse-face. */
28474
28475 static int
28476 expose_window_tree (struct window *w, XRectangle *r)
28477 {
28478 struct frame *f = XFRAME (WGET (w, frame));
28479 int mouse_face_overwritten_p = 0;
28480
28481 while (w && !FRAME_GARBAGED_P (f))
28482 {
28483 if (!NILP (WGET (w, hchild)))
28484 mouse_face_overwritten_p
28485 |= expose_window_tree (XWINDOW (WGET (w, hchild)), r);
28486 else if (!NILP (WGET (w, vchild)))
28487 mouse_face_overwritten_p
28488 |= expose_window_tree (XWINDOW (WGET (w, vchild)), r);
28489 else
28490 mouse_face_overwritten_p |= expose_window (w, r);
28491
28492 w = NILP (WGET (w, next)) ? NULL : XWINDOW (WGET (w, next));
28493 }
28494
28495 return mouse_face_overwritten_p;
28496 }
28497
28498
28499 /* EXPORT:
28500 Redisplay an exposed area of frame F. X and Y are the upper-left
28501 corner of the exposed rectangle. W and H are width and height of
28502 the exposed area. All are pixel values. W or H zero means redraw
28503 the entire frame. */
28504
28505 void
28506 expose_frame (struct frame *f, int x, int y, int w, int h)
28507 {
28508 XRectangle r;
28509 int mouse_face_overwritten_p = 0;
28510
28511 TRACE ((stderr, "expose_frame "));
28512
28513 /* No need to redraw if frame will be redrawn soon. */
28514 if (FRAME_GARBAGED_P (f))
28515 {
28516 TRACE ((stderr, " garbaged\n"));
28517 return;
28518 }
28519
28520 /* If basic faces haven't been realized yet, there is no point in
28521 trying to redraw anything. This can happen when we get an expose
28522 event while Emacs is starting, e.g. by moving another window. */
28523 if (FRAME_FACE_CACHE (f) == NULL
28524 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28525 {
28526 TRACE ((stderr, " no faces\n"));
28527 return;
28528 }
28529
28530 if (w == 0 || h == 0)
28531 {
28532 r.x = r.y = 0;
28533 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28534 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28535 }
28536 else
28537 {
28538 r.x = x;
28539 r.y = y;
28540 r.width = w;
28541 r.height = h;
28542 }
28543
28544 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28545 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28546
28547 if (WINDOWP (f->tool_bar_window))
28548 mouse_face_overwritten_p
28549 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28550
28551 #ifdef HAVE_X_WINDOWS
28552 #ifndef MSDOS
28553 #ifndef USE_X_TOOLKIT
28554 if (WINDOWP (f->menu_bar_window))
28555 mouse_face_overwritten_p
28556 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28557 #endif /* not USE_X_TOOLKIT */
28558 #endif
28559 #endif
28560
28561 /* Some window managers support a focus-follows-mouse style with
28562 delayed raising of frames. Imagine a partially obscured frame,
28563 and moving the mouse into partially obscured mouse-face on that
28564 frame. The visible part of the mouse-face will be highlighted,
28565 then the WM raises the obscured frame. With at least one WM, KDE
28566 2.1, Emacs is not getting any event for the raising of the frame
28567 (even tried with SubstructureRedirectMask), only Expose events.
28568 These expose events will draw text normally, i.e. not
28569 highlighted. Which means we must redo the highlight here.
28570 Subsume it under ``we love X''. --gerd 2001-08-15 */
28571 /* Included in Windows version because Windows most likely does not
28572 do the right thing if any third party tool offers
28573 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28574 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28575 {
28576 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28577 if (f == hlinfo->mouse_face_mouse_frame)
28578 {
28579 int mouse_x = hlinfo->mouse_face_mouse_x;
28580 int mouse_y = hlinfo->mouse_face_mouse_y;
28581 clear_mouse_face (hlinfo);
28582 note_mouse_highlight (f, mouse_x, mouse_y);
28583 }
28584 }
28585 }
28586
28587
28588 /* EXPORT:
28589 Determine the intersection of two rectangles R1 and R2. Return
28590 the intersection in *RESULT. Value is non-zero if RESULT is not
28591 empty. */
28592
28593 int
28594 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28595 {
28596 XRectangle *left, *right;
28597 XRectangle *upper, *lower;
28598 int intersection_p = 0;
28599
28600 /* Rearrange so that R1 is the left-most rectangle. */
28601 if (r1->x < r2->x)
28602 left = r1, right = r2;
28603 else
28604 left = r2, right = r1;
28605
28606 /* X0 of the intersection is right.x0, if this is inside R1,
28607 otherwise there is no intersection. */
28608 if (right->x <= left->x + left->width)
28609 {
28610 result->x = right->x;
28611
28612 /* The right end of the intersection is the minimum of
28613 the right ends of left and right. */
28614 result->width = (min (left->x + left->width, right->x + right->width)
28615 - result->x);
28616
28617 /* Same game for Y. */
28618 if (r1->y < r2->y)
28619 upper = r1, lower = r2;
28620 else
28621 upper = r2, lower = r1;
28622
28623 /* The upper end of the intersection is lower.y0, if this is inside
28624 of upper. Otherwise, there is no intersection. */
28625 if (lower->y <= upper->y + upper->height)
28626 {
28627 result->y = lower->y;
28628
28629 /* The lower end of the intersection is the minimum of the lower
28630 ends of upper and lower. */
28631 result->height = (min (lower->y + lower->height,
28632 upper->y + upper->height)
28633 - result->y);
28634 intersection_p = 1;
28635 }
28636 }
28637
28638 return intersection_p;
28639 }
28640
28641 #endif /* HAVE_WINDOW_SYSTEM */
28642
28643 \f
28644 /***********************************************************************
28645 Initialization
28646 ***********************************************************************/
28647
28648 void
28649 syms_of_xdisp (void)
28650 {
28651 Vwith_echo_area_save_vector = Qnil;
28652 staticpro (&Vwith_echo_area_save_vector);
28653
28654 Vmessage_stack = Qnil;
28655 staticpro (&Vmessage_stack);
28656
28657 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28658
28659 message_dolog_marker1 = Fmake_marker ();
28660 staticpro (&message_dolog_marker1);
28661 message_dolog_marker2 = Fmake_marker ();
28662 staticpro (&message_dolog_marker2);
28663 message_dolog_marker3 = Fmake_marker ();
28664 staticpro (&message_dolog_marker3);
28665
28666 #ifdef GLYPH_DEBUG
28667 defsubr (&Sdump_frame_glyph_matrix);
28668 defsubr (&Sdump_glyph_matrix);
28669 defsubr (&Sdump_glyph_row);
28670 defsubr (&Sdump_tool_bar_row);
28671 defsubr (&Strace_redisplay);
28672 defsubr (&Strace_to_stderr);
28673 #endif
28674 #ifdef HAVE_WINDOW_SYSTEM
28675 defsubr (&Stool_bar_lines_needed);
28676 defsubr (&Slookup_image_map);
28677 #endif
28678 defsubr (&Sformat_mode_line);
28679 defsubr (&Sinvisible_p);
28680 defsubr (&Scurrent_bidi_paragraph_direction);
28681
28682 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28683 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28684 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28685 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28686 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28687 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28688 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28689 DEFSYM (Qeval, "eval");
28690 DEFSYM (QCdata, ":data");
28691 DEFSYM (Qdisplay, "display");
28692 DEFSYM (Qspace_width, "space-width");
28693 DEFSYM (Qraise, "raise");
28694 DEFSYM (Qslice, "slice");
28695 DEFSYM (Qspace, "space");
28696 DEFSYM (Qmargin, "margin");
28697 DEFSYM (Qpointer, "pointer");
28698 DEFSYM (Qleft_margin, "left-margin");
28699 DEFSYM (Qright_margin, "right-margin");
28700 DEFSYM (Qcenter, "center");
28701 DEFSYM (Qline_height, "line-height");
28702 DEFSYM (QCalign_to, ":align-to");
28703 DEFSYM (QCrelative_width, ":relative-width");
28704 DEFSYM (QCrelative_height, ":relative-height");
28705 DEFSYM (QCeval, ":eval");
28706 DEFSYM (QCpropertize, ":propertize");
28707 DEFSYM (QCfile, ":file");
28708 DEFSYM (Qfontified, "fontified");
28709 DEFSYM (Qfontification_functions, "fontification-functions");
28710 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28711 DEFSYM (Qescape_glyph, "escape-glyph");
28712 DEFSYM (Qnobreak_space, "nobreak-space");
28713 DEFSYM (Qimage, "image");
28714 DEFSYM (Qtext, "text");
28715 DEFSYM (Qboth, "both");
28716 DEFSYM (Qboth_horiz, "both-horiz");
28717 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28718 DEFSYM (QCmap, ":map");
28719 DEFSYM (QCpointer, ":pointer");
28720 DEFSYM (Qrect, "rect");
28721 DEFSYM (Qcircle, "circle");
28722 DEFSYM (Qpoly, "poly");
28723 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28724 DEFSYM (Qgrow_only, "grow-only");
28725 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28726 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28727 DEFSYM (Qposition, "position");
28728 DEFSYM (Qbuffer_position, "buffer-position");
28729 DEFSYM (Qobject, "object");
28730 DEFSYM (Qbar, "bar");
28731 DEFSYM (Qhbar, "hbar");
28732 DEFSYM (Qbox, "box");
28733 DEFSYM (Qhollow, "hollow");
28734 DEFSYM (Qhand, "hand");
28735 DEFSYM (Qarrow, "arrow");
28736 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28737
28738 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28739 Fcons (intern_c_string ("void-variable"), Qnil)),
28740 Qnil);
28741 staticpro (&list_of_error);
28742
28743 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28744 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28745 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28746 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28747
28748 echo_buffer[0] = echo_buffer[1] = Qnil;
28749 staticpro (&echo_buffer[0]);
28750 staticpro (&echo_buffer[1]);
28751
28752 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28753 staticpro (&echo_area_buffer[0]);
28754 staticpro (&echo_area_buffer[1]);
28755
28756 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28757 staticpro (&Vmessages_buffer_name);
28758
28759 mode_line_proptrans_alist = Qnil;
28760 staticpro (&mode_line_proptrans_alist);
28761 mode_line_string_list = Qnil;
28762 staticpro (&mode_line_string_list);
28763 mode_line_string_face = Qnil;
28764 staticpro (&mode_line_string_face);
28765 mode_line_string_face_prop = Qnil;
28766 staticpro (&mode_line_string_face_prop);
28767 Vmode_line_unwind_vector = Qnil;
28768 staticpro (&Vmode_line_unwind_vector);
28769
28770 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28771
28772 help_echo_string = Qnil;
28773 staticpro (&help_echo_string);
28774 help_echo_object = Qnil;
28775 staticpro (&help_echo_object);
28776 help_echo_window = Qnil;
28777 staticpro (&help_echo_window);
28778 previous_help_echo_string = Qnil;
28779 staticpro (&previous_help_echo_string);
28780 help_echo_pos = -1;
28781
28782 DEFSYM (Qright_to_left, "right-to-left");
28783 DEFSYM (Qleft_to_right, "left-to-right");
28784
28785 #ifdef HAVE_WINDOW_SYSTEM
28786 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28787 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28788 For example, if a block cursor is over a tab, it will be drawn as
28789 wide as that tab on the display. */);
28790 x_stretch_cursor_p = 0;
28791 #endif
28792
28793 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28794 doc: /* Non-nil means highlight trailing whitespace.
28795 The face used for trailing whitespace is `trailing-whitespace'. */);
28796 Vshow_trailing_whitespace = Qnil;
28797
28798 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28799 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28800 If the value is t, Emacs highlights non-ASCII chars which have the
28801 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28802 or `escape-glyph' face respectively.
28803
28804 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28805 U+2011 (non-breaking hyphen) are affected.
28806
28807 Any other non-nil value means to display these characters as a escape
28808 glyph followed by an ordinary space or hyphen.
28809
28810 A value of nil means no special handling of these characters. */);
28811 Vnobreak_char_display = Qt;
28812
28813 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28814 doc: /* The pointer shape to show in void text areas.
28815 A value of nil means to show the text pointer. Other options are `arrow',
28816 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28817 Vvoid_text_area_pointer = Qarrow;
28818
28819 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28820 doc: /* Non-nil means don't actually do any redisplay.
28821 This is used for internal purposes. */);
28822 Vinhibit_redisplay = Qnil;
28823
28824 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28825 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28826 Vglobal_mode_string = Qnil;
28827
28828 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28829 doc: /* Marker for where to display an arrow on top of the buffer text.
28830 This must be the beginning of a line in order to work.
28831 See also `overlay-arrow-string'. */);
28832 Voverlay_arrow_position = Qnil;
28833
28834 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28835 doc: /* String to display as an arrow in non-window frames.
28836 See also `overlay-arrow-position'. */);
28837 Voverlay_arrow_string = build_pure_c_string ("=>");
28838
28839 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28840 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28841 The symbols on this list are examined during redisplay to determine
28842 where to display overlay arrows. */);
28843 Voverlay_arrow_variable_list
28844 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28845
28846 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28847 doc: /* The number of lines to try scrolling a window by when point moves out.
28848 If that fails to bring point back on frame, point is centered instead.
28849 If this is zero, point is always centered after it moves off frame.
28850 If you want scrolling to always be a line at a time, you should set
28851 `scroll-conservatively' to a large value rather than set this to 1. */);
28852
28853 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28854 doc: /* Scroll up to this many lines, to bring point back on screen.
28855 If point moves off-screen, redisplay will scroll by up to
28856 `scroll-conservatively' lines in order to bring point just barely
28857 onto the screen again. If that cannot be done, then redisplay
28858 recenters point as usual.
28859
28860 If the value is greater than 100, redisplay will never recenter point,
28861 but will always scroll just enough text to bring point into view, even
28862 if you move far away.
28863
28864 A value of zero means always recenter point if it moves off screen. */);
28865 scroll_conservatively = 0;
28866
28867 DEFVAR_INT ("scroll-margin", scroll_margin,
28868 doc: /* Number of lines of margin at the top and bottom of a window.
28869 Recenter the window whenever point gets within this many lines
28870 of the top or bottom of the window. */);
28871 scroll_margin = 0;
28872
28873 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28874 doc: /* Pixels per inch value for non-window system displays.
28875 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28876 Vdisplay_pixels_per_inch = make_float (72.0);
28877
28878 #ifdef GLYPH_DEBUG
28879 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28880 #endif
28881
28882 DEFVAR_LISP ("truncate-partial-width-windows",
28883 Vtruncate_partial_width_windows,
28884 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28885 For an integer value, truncate lines in each window narrower than the
28886 full frame width, provided the window width is less than that integer;
28887 otherwise, respect the value of `truncate-lines'.
28888
28889 For any other non-nil value, truncate lines in all windows that do
28890 not span the full frame width.
28891
28892 A value of nil means to respect the value of `truncate-lines'.
28893
28894 If `word-wrap' is enabled, you might want to reduce this. */);
28895 Vtruncate_partial_width_windows = make_number (50);
28896
28897 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28898 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28899 Any other value means to use the appropriate face, `mode-line',
28900 `header-line', or `menu' respectively. */);
28901 mode_line_inverse_video = 1;
28902
28903 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28904 doc: /* Maximum buffer size for which line number should be displayed.
28905 If the buffer is bigger than this, the line number does not appear
28906 in the mode line. A value of nil means no limit. */);
28907 Vline_number_display_limit = Qnil;
28908
28909 DEFVAR_INT ("line-number-display-limit-width",
28910 line_number_display_limit_width,
28911 doc: /* Maximum line width (in characters) for line number display.
28912 If the average length of the lines near point is bigger than this, then the
28913 line number may be omitted from the mode line. */);
28914 line_number_display_limit_width = 200;
28915
28916 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28917 doc: /* Non-nil means highlight region even in nonselected windows. */);
28918 highlight_nonselected_windows = 0;
28919
28920 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28921 doc: /* Non-nil if more than one frame is visible on this display.
28922 Minibuffer-only frames don't count, but iconified frames do.
28923 This variable is not guaranteed to be accurate except while processing
28924 `frame-title-format' and `icon-title-format'. */);
28925
28926 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28927 doc: /* Template for displaying the title bar of visible frames.
28928 \(Assuming the window manager supports this feature.)
28929
28930 This variable has the same structure as `mode-line-format', except that
28931 the %c and %l constructs are ignored. It is used only on frames for
28932 which no explicit name has been set \(see `modify-frame-parameters'). */);
28933
28934 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28935 doc: /* Template for displaying the title bar of an iconified frame.
28936 \(Assuming the window manager supports this feature.)
28937 This variable has the same structure as `mode-line-format' (which see),
28938 and is used only on frames for which no explicit name has been set
28939 \(see `modify-frame-parameters'). */);
28940 Vicon_title_format
28941 = Vframe_title_format
28942 = listn (CONSTYPE_PURE, 3,
28943 intern_c_string ("multiple-frames"),
28944 build_pure_c_string ("%b"),
28945 listn (CONSTYPE_PURE, 4,
28946 empty_unibyte_string,
28947 intern_c_string ("invocation-name"),
28948 build_pure_c_string ("@"),
28949 intern_c_string ("system-name")));
28950
28951 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28952 doc: /* Maximum number of lines to keep in the message log buffer.
28953 If nil, disable message logging. If t, log messages but don't truncate
28954 the buffer when it becomes large. */);
28955 Vmessage_log_max = make_number (100);
28956
28957 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28958 doc: /* Functions called before redisplay, if window sizes have changed.
28959 The value should be a list of functions that take one argument.
28960 Just before redisplay, for each frame, if any of its windows have changed
28961 size since the last redisplay, or have been split or deleted,
28962 all the functions in the list are called, with the frame as argument. */);
28963 Vwindow_size_change_functions = Qnil;
28964
28965 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28966 doc: /* List of functions to call before redisplaying a window with scrolling.
28967 Each function is called with two arguments, the window and its new
28968 display-start position. Note that these functions are also called by
28969 `set-window-buffer'. Also note that the value of `window-end' is not
28970 valid when these functions are called.
28971
28972 Warning: Do not use this feature to alter the way the window
28973 is scrolled. It is not designed for that, and such use probably won't
28974 work. */);
28975 Vwindow_scroll_functions = Qnil;
28976
28977 DEFVAR_LISP ("window-text-change-functions",
28978 Vwindow_text_change_functions,
28979 doc: /* Functions to call in redisplay when text in the window might change. */);
28980 Vwindow_text_change_functions = Qnil;
28981
28982 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28983 doc: /* Functions called when redisplay of a window reaches the end trigger.
28984 Each function is called with two arguments, the window and the end trigger value.
28985 See `set-window-redisplay-end-trigger'. */);
28986 Vredisplay_end_trigger_functions = Qnil;
28987
28988 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28989 doc: /* Non-nil means autoselect window with mouse pointer.
28990 If nil, do not autoselect windows.
28991 A positive number means delay autoselection by that many seconds: a
28992 window is autoselected only after the mouse has remained in that
28993 window for the duration of the delay.
28994 A negative number has a similar effect, but causes windows to be
28995 autoselected only after the mouse has stopped moving. \(Because of
28996 the way Emacs compares mouse events, you will occasionally wait twice
28997 that time before the window gets selected.\)
28998 Any other value means to autoselect window instantaneously when the
28999 mouse pointer enters it.
29000
29001 Autoselection selects the minibuffer only if it is active, and never
29002 unselects the minibuffer if it is active.
29003
29004 When customizing this variable make sure that the actual value of
29005 `focus-follows-mouse' matches the behavior of your window manager. */);
29006 Vmouse_autoselect_window = Qnil;
29007
29008 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29009 doc: /* Non-nil means automatically resize tool-bars.
29010 This dynamically changes the tool-bar's height to the minimum height
29011 that is needed to make all tool-bar items visible.
29012 If value is `grow-only', the tool-bar's height is only increased
29013 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29014 Vauto_resize_tool_bars = Qt;
29015
29016 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29017 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29018 auto_raise_tool_bar_buttons_p = 1;
29019
29020 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29021 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29022 make_cursor_line_fully_visible_p = 1;
29023
29024 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29025 doc: /* Border below tool-bar in pixels.
29026 If an integer, use it as the height of the border.
29027 If it is one of `internal-border-width' or `border-width', use the
29028 value of the corresponding frame parameter.
29029 Otherwise, no border is added below the tool-bar. */);
29030 Vtool_bar_border = Qinternal_border_width;
29031
29032 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29033 doc: /* Margin around tool-bar buttons in pixels.
29034 If an integer, use that for both horizontal and vertical margins.
29035 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29036 HORZ specifying the horizontal margin, and VERT specifying the
29037 vertical margin. */);
29038 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29039
29040 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29041 doc: /* Relief thickness of tool-bar buttons. */);
29042 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29043
29044 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29045 doc: /* Tool bar style to use.
29046 It can be one of
29047 image - show images only
29048 text - show text only
29049 both - show both, text below image
29050 both-horiz - show text to the right of the image
29051 text-image-horiz - show text to the left of the image
29052 any other - use system default or image if no system default.
29053
29054 This variable only affects the GTK+ toolkit version of Emacs. */);
29055 Vtool_bar_style = Qnil;
29056
29057 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29058 doc: /* Maximum number of characters a label can have to be shown.
29059 The tool bar style must also show labels for this to have any effect, see
29060 `tool-bar-style'. */);
29061 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29062
29063 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29064 doc: /* List of functions to call to fontify regions of text.
29065 Each function is called with one argument POS. Functions must
29066 fontify a region starting at POS in the current buffer, and give
29067 fontified regions the property `fontified'. */);
29068 Vfontification_functions = Qnil;
29069 Fmake_variable_buffer_local (Qfontification_functions);
29070
29071 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29072 unibyte_display_via_language_environment,
29073 doc: /* Non-nil means display unibyte text according to language environment.
29074 Specifically, this means that raw bytes in the range 160-255 decimal
29075 are displayed by converting them to the equivalent multibyte characters
29076 according to the current language environment. As a result, they are
29077 displayed according to the current fontset.
29078
29079 Note that this variable affects only how these bytes are displayed,
29080 but does not change the fact they are interpreted as raw bytes. */);
29081 unibyte_display_via_language_environment = 0;
29082
29083 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29084 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29085 If a float, it specifies a fraction of the mini-window frame's height.
29086 If an integer, it specifies a number of lines. */);
29087 Vmax_mini_window_height = make_float (0.25);
29088
29089 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29090 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29091 A value of nil means don't automatically resize mini-windows.
29092 A value of t means resize them to fit the text displayed in them.
29093 A value of `grow-only', the default, means let mini-windows grow only;
29094 they return to their normal size when the minibuffer is closed, or the
29095 echo area becomes empty. */);
29096 Vresize_mini_windows = Qgrow_only;
29097
29098 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29099 doc: /* Alist specifying how to blink the cursor off.
29100 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29101 `cursor-type' frame-parameter or variable equals ON-STATE,
29102 comparing using `equal', Emacs uses OFF-STATE to specify
29103 how to blink it off. ON-STATE and OFF-STATE are values for
29104 the `cursor-type' frame parameter.
29105
29106 If a frame's ON-STATE has no entry in this list,
29107 the frame's other specifications determine how to blink the cursor off. */);
29108 Vblink_cursor_alist = Qnil;
29109
29110 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29111 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29112 If non-nil, windows are automatically scrolled horizontally to make
29113 point visible. */);
29114 automatic_hscrolling_p = 1;
29115 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29116
29117 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29118 doc: /* How many columns away from the window edge point is allowed to get
29119 before automatic hscrolling will horizontally scroll the window. */);
29120 hscroll_margin = 5;
29121
29122 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29123 doc: /* How many columns to scroll the window when point gets too close to the edge.
29124 When point is less than `hscroll-margin' columns from the window
29125 edge, automatic hscrolling will scroll the window by the amount of columns
29126 determined by this variable. If its value is a positive integer, scroll that
29127 many columns. If it's a positive floating-point number, it specifies the
29128 fraction of the window's width to scroll. If it's nil or zero, point will be
29129 centered horizontally after the scroll. Any other value, including negative
29130 numbers, are treated as if the value were zero.
29131
29132 Automatic hscrolling always moves point outside the scroll margin, so if
29133 point was more than scroll step columns inside the margin, the window will
29134 scroll more than the value given by the scroll step.
29135
29136 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29137 and `scroll-right' overrides this variable's effect. */);
29138 Vhscroll_step = make_number (0);
29139
29140 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29141 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29142 Bind this around calls to `message' to let it take effect. */);
29143 message_truncate_lines = 0;
29144
29145 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29146 doc: /* Normal hook run to update the menu bar definitions.
29147 Redisplay runs this hook before it redisplays the menu bar.
29148 This is used to update submenus such as Buffers,
29149 whose contents depend on various data. */);
29150 Vmenu_bar_update_hook = Qnil;
29151
29152 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29153 doc: /* Frame for which we are updating a menu.
29154 The enable predicate for a menu binding should check this variable. */);
29155 Vmenu_updating_frame = Qnil;
29156
29157 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29158 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29159 inhibit_menubar_update = 0;
29160
29161 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29162 doc: /* Prefix prepended to all continuation lines at display time.
29163 The value may be a string, an image, or a stretch-glyph; it is
29164 interpreted in the same way as the value of a `display' text property.
29165
29166 This variable is overridden by any `wrap-prefix' text or overlay
29167 property.
29168
29169 To add a prefix to non-continuation lines, use `line-prefix'. */);
29170 Vwrap_prefix = Qnil;
29171 DEFSYM (Qwrap_prefix, "wrap-prefix");
29172 Fmake_variable_buffer_local (Qwrap_prefix);
29173
29174 DEFVAR_LISP ("line-prefix", Vline_prefix,
29175 doc: /* Prefix prepended to all non-continuation lines at display time.
29176 The value may be a string, an image, or a stretch-glyph; it is
29177 interpreted in the same way as the value of a `display' text property.
29178
29179 This variable is overridden by any `line-prefix' text or overlay
29180 property.
29181
29182 To add a prefix to continuation lines, use `wrap-prefix'. */);
29183 Vline_prefix = Qnil;
29184 DEFSYM (Qline_prefix, "line-prefix");
29185 Fmake_variable_buffer_local (Qline_prefix);
29186
29187 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29188 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29189 inhibit_eval_during_redisplay = 0;
29190
29191 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29192 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29193 inhibit_free_realized_faces = 0;
29194
29195 #ifdef GLYPH_DEBUG
29196 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29197 doc: /* Inhibit try_window_id display optimization. */);
29198 inhibit_try_window_id = 0;
29199
29200 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29201 doc: /* Inhibit try_window_reusing display optimization. */);
29202 inhibit_try_window_reusing = 0;
29203
29204 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29205 doc: /* Inhibit try_cursor_movement display optimization. */);
29206 inhibit_try_cursor_movement = 0;
29207 #endif /* GLYPH_DEBUG */
29208
29209 DEFVAR_INT ("overline-margin", overline_margin,
29210 doc: /* Space between overline and text, in pixels.
29211 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29212 margin to the character height. */);
29213 overline_margin = 2;
29214
29215 DEFVAR_INT ("underline-minimum-offset",
29216 underline_minimum_offset,
29217 doc: /* Minimum distance between baseline and underline.
29218 This can improve legibility of underlined text at small font sizes,
29219 particularly when using variable `x-use-underline-position-properties'
29220 with fonts that specify an UNDERLINE_POSITION relatively close to the
29221 baseline. The default value is 1. */);
29222 underline_minimum_offset = 1;
29223
29224 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29225 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29226 This feature only works when on a window system that can change
29227 cursor shapes. */);
29228 display_hourglass_p = 1;
29229
29230 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29231 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29232 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29233
29234 hourglass_atimer = NULL;
29235 hourglass_shown_p = 0;
29236
29237 DEFSYM (Qglyphless_char, "glyphless-char");
29238 DEFSYM (Qhex_code, "hex-code");
29239 DEFSYM (Qempty_box, "empty-box");
29240 DEFSYM (Qthin_space, "thin-space");
29241 DEFSYM (Qzero_width, "zero-width");
29242
29243 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29244 /* Intern this now in case it isn't already done.
29245 Setting this variable twice is harmless.
29246 But don't staticpro it here--that is done in alloc.c. */
29247 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29248 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29249
29250 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29251 doc: /* Char-table defining glyphless characters.
29252 Each element, if non-nil, should be one of the following:
29253 an ASCII acronym string: display this string in a box
29254 `hex-code': display the hexadecimal code of a character in a box
29255 `empty-box': display as an empty box
29256 `thin-space': display as 1-pixel width space
29257 `zero-width': don't display
29258 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29259 display method for graphical terminals and text terminals respectively.
29260 GRAPHICAL and TEXT should each have one of the values listed above.
29261
29262 The char-table has one extra slot to control the display of a character for
29263 which no font is found. This slot only takes effect on graphical terminals.
29264 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29265 `thin-space'. The default is `empty-box'. */);
29266 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29267 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29268 Qempty_box);
29269 }
29270
29271
29272 /* Initialize this module when Emacs starts. */
29273
29274 void
29275 init_xdisp (void)
29276 {
29277 current_header_line_height = current_mode_line_height = -1;
29278
29279 CHARPOS (this_line_start_pos) = 0;
29280
29281 if (!noninteractive)
29282 {
29283 struct window *m = XWINDOW (minibuf_window);
29284 Lisp_Object frame = WGET (m, frame);
29285 struct frame *f = XFRAME (frame);
29286 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29287 struct window *r = XWINDOW (root);
29288 int i;
29289
29290 echo_area_window = minibuf_window;
29291
29292 WSET (r, top_line, make_number (FRAME_TOP_MARGIN (f)));
29293 WSET (r, total_lines, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29294 WSET (r, total_cols, make_number (FRAME_COLS (f)));
29295 WSET (m, top_line, make_number (FRAME_LINES (f) - 1));
29296 WSET (m, total_lines, make_number (1));
29297 WSET (m, total_cols, make_number (FRAME_COLS (f)));
29298
29299 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29300 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29301 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29302
29303 /* The default ellipsis glyphs `...'. */
29304 for (i = 0; i < 3; ++i)
29305 default_invis_vector[i] = make_number ('.');
29306 }
29307
29308 {
29309 /* Allocate the buffer for frame titles.
29310 Also used for `format-mode-line'. */
29311 int size = 100;
29312 mode_line_noprop_buf = xmalloc (size);
29313 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29314 mode_line_noprop_ptr = mode_line_noprop_buf;
29315 mode_line_target = MODE_LINE_DISPLAY;
29316 }
29317
29318 help_echo_showing_p = 0;
29319 }
29320
29321 /* Since w32 does not support atimers, it defines its own implementation of
29322 the following three functions in w32fns.c. */
29323 #ifndef WINDOWSNT
29324
29325 /* Platform-independent portion of hourglass implementation. */
29326
29327 /* Cancel a currently active hourglass timer, and start a new one. */
29328 void
29329 start_hourglass (void)
29330 {
29331 #if defined (HAVE_WINDOW_SYSTEM)
29332 EMACS_TIME delay;
29333
29334 cancel_hourglass ();
29335
29336 if (INTEGERP (Vhourglass_delay)
29337 && XINT (Vhourglass_delay) > 0)
29338 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29339 TYPE_MAXIMUM (time_t)),
29340 0);
29341 else if (FLOATP (Vhourglass_delay)
29342 && XFLOAT_DATA (Vhourglass_delay) > 0)
29343 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29344 else
29345 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29346
29347 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29348 show_hourglass, NULL);
29349 #endif
29350 }
29351
29352
29353 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29354 shown. */
29355 void
29356 cancel_hourglass (void)
29357 {
29358 #if defined (HAVE_WINDOW_SYSTEM)
29359 if (hourglass_atimer)
29360 {
29361 cancel_atimer (hourglass_atimer);
29362 hourglass_atimer = NULL;
29363 }
29364
29365 if (hourglass_shown_p)
29366 hide_hourglass ();
29367 #endif
29368 }
29369 #endif /* ! WINDOWSNT */