]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
315
316 #include "font.h"
317
318 #ifndef FRAME_X_OUTPUT
319 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
320 #endif
321
322 #define INFINITY 10000000
323
324 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
325 Lisp_Object Qwindow_scroll_functions;
326 static Lisp_Object Qwindow_text_change_functions;
327 static Lisp_Object Qredisplay_end_trigger_functions;
328 Lisp_Object Qinhibit_point_motion_hooks;
329 static Lisp_Object QCeval, QCpropertize;
330 Lisp_Object QCfile, QCdata;
331 static Lisp_Object Qfontified;
332 static Lisp_Object Qgrow_only;
333 static Lisp_Object Qinhibit_eval_during_redisplay;
334 static Lisp_Object Qbuffer_position, Qposition, Qobject;
335 static Lisp_Object Qright_to_left, Qleft_to_right;
336
337 /* Cursor shapes. */
338 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339
340 /* Pointer shapes. */
341 static Lisp_Object Qarrow, Qhand;
342 Lisp_Object Qtext;
343
344 /* Holds the list (error). */
345 static Lisp_Object list_of_error;
346
347 static Lisp_Object Qfontification_functions;
348
349 static Lisp_Object Qwrap_prefix;
350 static Lisp_Object Qline_prefix;
351 static Lisp_Object Qredisplay_internal;
352
353 /* Non-nil means don't actually do any redisplay. */
354
355 Lisp_Object Qinhibit_redisplay;
356
357 /* Names of text properties relevant for redisplay. */
358
359 Lisp_Object Qdisplay;
360
361 Lisp_Object Qspace, QCalign_to;
362 static Lisp_Object QCrelative_width, QCrelative_height;
363 Lisp_Object Qleft_margin, Qright_margin;
364 static Lisp_Object Qspace_width, Qraise;
365 static Lisp_Object Qslice;
366 Lisp_Object Qcenter;
367 static Lisp_Object Qmargin, Qpointer;
368 static Lisp_Object Qline_height;
369
370 /* These setters are used only in this file, so they can be private. */
371 static void
372 wset_base_line_number (struct window *w, Lisp_Object val)
373 {
374 w->base_line_number = val;
375 }
376 static void
377 wset_base_line_pos (struct window *w, Lisp_Object val)
378 {
379 w->base_line_pos = val;
380 }
381 static void
382 wset_column_number_displayed (struct window *w, Lisp_Object val)
383 {
384 w->column_number_displayed = val;
385 }
386 static void
387 wset_region_showing (struct window *w, Lisp_Object val)
388 {
389 w->region_showing = val;
390 }
391
392 #ifdef HAVE_WINDOW_SYSTEM
393
394 /* Test if overflow newline into fringe. Called with iterator IT
395 at or past right window margin, and with IT->current_x set. */
396
397 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
398 (!NILP (Voverflow_newline_into_fringe) \
399 && FRAME_WINDOW_P ((IT)->f) \
400 && ((IT)->bidi_it.paragraph_dir == R2L \
401 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
402 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
403 && (IT)->current_x == (IT)->last_visible_x \
404 && (IT)->line_wrap != WORD_WRAP)
405
406 #else /* !HAVE_WINDOW_SYSTEM */
407 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
408 #endif /* HAVE_WINDOW_SYSTEM */
409
410 /* Test if the display element loaded in IT, or the underlying buffer
411 or string character, is a space or a TAB character. This is used
412 to determine where word wrapping can occur. */
413
414 #define IT_DISPLAYING_WHITESPACE(it) \
415 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
416 || ((STRINGP (it->string) \
417 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
418 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
419 || (it->s \
420 && (it->s[IT_BYTEPOS (*it)] == ' ' \
421 || it->s[IT_BYTEPOS (*it)] == '\t')) \
422 || (IT_BYTEPOS (*it) < ZV_BYTE \
423 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
424 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
425
426 /* Name of the face used to highlight trailing whitespace. */
427
428 static Lisp_Object Qtrailing_whitespace;
429
430 /* Name and number of the face used to highlight escape glyphs. */
431
432 static Lisp_Object Qescape_glyph;
433
434 /* Name and number of the face used to highlight non-breaking spaces. */
435
436 static Lisp_Object Qnobreak_space;
437
438 /* The symbol `image' which is the car of the lists used to represent
439 images in Lisp. Also a tool bar style. */
440
441 Lisp_Object Qimage;
442
443 /* The image map types. */
444 Lisp_Object QCmap;
445 static Lisp_Object QCpointer;
446 static Lisp_Object Qrect, Qcircle, Qpoly;
447
448 /* Tool bar styles */
449 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
450
451 /* Non-zero means print newline to stdout before next mini-buffer
452 message. */
453
454 int noninteractive_need_newline;
455
456 /* Non-zero means print newline to message log before next message. */
457
458 static int message_log_need_newline;
459
460 /* Three markers that message_dolog uses.
461 It could allocate them itself, but that causes trouble
462 in handling memory-full errors. */
463 static Lisp_Object message_dolog_marker1;
464 static Lisp_Object message_dolog_marker2;
465 static Lisp_Object message_dolog_marker3;
466 \f
467 /* The buffer position of the first character appearing entirely or
468 partially on the line of the selected window which contains the
469 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
470 redisplay optimization in redisplay_internal. */
471
472 static struct text_pos this_line_start_pos;
473
474 /* Number of characters past the end of the line above, including the
475 terminating newline. */
476
477 static struct text_pos this_line_end_pos;
478
479 /* The vertical positions and the height of this line. */
480
481 static int this_line_vpos;
482 static int this_line_y;
483 static int this_line_pixel_height;
484
485 /* X position at which this display line starts. Usually zero;
486 negative if first character is partially visible. */
487
488 static int this_line_start_x;
489
490 /* The smallest character position seen by move_it_* functions as they
491 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
492 hscrolled lines, see display_line. */
493
494 static struct text_pos this_line_min_pos;
495
496 /* Buffer that this_line_.* variables are referring to. */
497
498 static struct buffer *this_line_buffer;
499
500
501 /* Values of those variables at last redisplay are stored as
502 properties on `overlay-arrow-position' symbol. However, if
503 Voverlay_arrow_position is a marker, last-arrow-position is its
504 numerical position. */
505
506 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
507
508 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
509 properties on a symbol in overlay-arrow-variable-list. */
510
511 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
512
513 Lisp_Object Qmenu_bar_update_hook;
514
515 /* Nonzero if an overlay arrow has been displayed in this window. */
516
517 static int overlay_arrow_seen;
518
519 /* Vector containing glyphs for an ellipsis `...'. */
520
521 static Lisp_Object default_invis_vector[3];
522
523 /* This is the window where the echo area message was displayed. It
524 is always a mini-buffer window, but it may not be the same window
525 currently active as a mini-buffer. */
526
527 Lisp_Object echo_area_window;
528
529 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
530 pushes the current message and the value of
531 message_enable_multibyte on the stack, the function restore_message
532 pops the stack and displays MESSAGE again. */
533
534 static Lisp_Object Vmessage_stack;
535
536 /* Nonzero means multibyte characters were enabled when the echo area
537 message was specified. */
538
539 static int message_enable_multibyte;
540
541 /* Nonzero if we should redraw the mode lines on the next redisplay. */
542
543 int update_mode_lines;
544
545 /* Nonzero if window sizes or contents have changed since last
546 redisplay that finished. */
547
548 int windows_or_buffers_changed;
549
550 /* Nonzero means a frame's cursor type has been changed. */
551
552 int cursor_type_changed;
553
554 /* Nonzero after display_mode_line if %l was used and it displayed a
555 line number. */
556
557 static int line_number_displayed;
558
559 /* The name of the *Messages* buffer, a string. */
560
561 static Lisp_Object Vmessages_buffer_name;
562
563 /* Current, index 0, and last displayed echo area message. Either
564 buffers from echo_buffers, or nil to indicate no message. */
565
566 Lisp_Object echo_area_buffer[2];
567
568 /* The buffers referenced from echo_area_buffer. */
569
570 static Lisp_Object echo_buffer[2];
571
572 /* A vector saved used in with_area_buffer to reduce consing. */
573
574 static Lisp_Object Vwith_echo_area_save_vector;
575
576 /* Non-zero means display_echo_area should display the last echo area
577 message again. Set by redisplay_preserve_echo_area. */
578
579 static int display_last_displayed_message_p;
580
581 /* Nonzero if echo area is being used by print; zero if being used by
582 message. */
583
584 static int message_buf_print;
585
586 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
587
588 static Lisp_Object Qinhibit_menubar_update;
589 static Lisp_Object Qmessage_truncate_lines;
590
591 /* Set to 1 in clear_message to make redisplay_internal aware
592 of an emptied echo area. */
593
594 static int message_cleared_p;
595
596 /* A scratch glyph row with contents used for generating truncation
597 glyphs. Also used in direct_output_for_insert. */
598
599 #define MAX_SCRATCH_GLYPHS 100
600 static struct glyph_row scratch_glyph_row;
601 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
602
603 /* Ascent and height of the last line processed by move_it_to. */
604
605 static int last_max_ascent, last_height;
606
607 /* Non-zero if there's a help-echo in the echo area. */
608
609 int help_echo_showing_p;
610
611 /* If >= 0, computed, exact values of mode-line and header-line height
612 to use in the macros CURRENT_MODE_LINE_HEIGHT and
613 CURRENT_HEADER_LINE_HEIGHT. */
614
615 int current_mode_line_height, current_header_line_height;
616
617 /* The maximum distance to look ahead for text properties. Values
618 that are too small let us call compute_char_face and similar
619 functions too often which is expensive. Values that are too large
620 let us call compute_char_face and alike too often because we
621 might not be interested in text properties that far away. */
622
623 #define TEXT_PROP_DISTANCE_LIMIT 100
624
625 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
626 iterator state and later restore it. This is needed because the
627 bidi iterator on bidi.c keeps a stacked cache of its states, which
628 is really a singleton. When we use scratch iterator objects to
629 move around the buffer, we can cause the bidi cache to be pushed or
630 popped, and therefore we need to restore the cache state when we
631 return to the original iterator. */
632 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
633 do { \
634 if (CACHE) \
635 bidi_unshelve_cache (CACHE, 1); \
636 ITCOPY = ITORIG; \
637 CACHE = bidi_shelve_cache (); \
638 } while (0)
639
640 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
641 do { \
642 if (pITORIG != pITCOPY) \
643 *(pITORIG) = *(pITCOPY); \
644 bidi_unshelve_cache (CACHE, 0); \
645 CACHE = NULL; \
646 } while (0)
647
648 #ifdef GLYPH_DEBUG
649
650 /* Non-zero means print traces of redisplay if compiled with
651 GLYPH_DEBUG defined. */
652
653 int trace_redisplay_p;
654
655 #endif /* GLYPH_DEBUG */
656
657 #ifdef DEBUG_TRACE_MOVE
658 /* Non-zero means trace with TRACE_MOVE to stderr. */
659 int trace_move;
660
661 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
662 #else
663 #define TRACE_MOVE(x) (void) 0
664 #endif
665
666 static Lisp_Object Qauto_hscroll_mode;
667
668 /* Buffer being redisplayed -- for redisplay_window_error. */
669
670 static struct buffer *displayed_buffer;
671
672 /* Value returned from text property handlers (see below). */
673
674 enum prop_handled
675 {
676 HANDLED_NORMALLY,
677 HANDLED_RECOMPUTE_PROPS,
678 HANDLED_OVERLAY_STRING_CONSUMED,
679 HANDLED_RETURN
680 };
681
682 /* A description of text properties that redisplay is interested
683 in. */
684
685 struct props
686 {
687 /* The name of the property. */
688 Lisp_Object *name;
689
690 /* A unique index for the property. */
691 enum prop_idx idx;
692
693 /* A handler function called to set up iterator IT from the property
694 at IT's current position. Value is used to steer handle_stop. */
695 enum prop_handled (*handler) (struct it *it);
696 };
697
698 static enum prop_handled handle_face_prop (struct it *);
699 static enum prop_handled handle_invisible_prop (struct it *);
700 static enum prop_handled handle_display_prop (struct it *);
701 static enum prop_handled handle_composition_prop (struct it *);
702 static enum prop_handled handle_overlay_change (struct it *);
703 static enum prop_handled handle_fontified_prop (struct it *);
704
705 /* Properties handled by iterators. */
706
707 static struct props it_props[] =
708 {
709 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
710 /* Handle `face' before `display' because some sub-properties of
711 `display' need to know the face. */
712 {&Qface, FACE_PROP_IDX, handle_face_prop},
713 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
714 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
715 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
716 {NULL, 0, NULL}
717 };
718
719 /* Value is the position described by X. If X is a marker, value is
720 the marker_position of X. Otherwise, value is X. */
721
722 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
723
724 /* Enumeration returned by some move_it_.* functions internally. */
725
726 enum move_it_result
727 {
728 /* Not used. Undefined value. */
729 MOVE_UNDEFINED,
730
731 /* Move ended at the requested buffer position or ZV. */
732 MOVE_POS_MATCH_OR_ZV,
733
734 /* Move ended at the requested X pixel position. */
735 MOVE_X_REACHED,
736
737 /* Move within a line ended at the end of a line that must be
738 continued. */
739 MOVE_LINE_CONTINUED,
740
741 /* Move within a line ended at the end of a line that would
742 be displayed truncated. */
743 MOVE_LINE_TRUNCATED,
744
745 /* Move within a line ended at a line end. */
746 MOVE_NEWLINE_OR_CR
747 };
748
749 /* This counter is used to clear the face cache every once in a while
750 in redisplay_internal. It is incremented for each redisplay.
751 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
752 cleared. */
753
754 #define CLEAR_FACE_CACHE_COUNT 500
755 static int clear_face_cache_count;
756
757 /* Similarly for the image cache. */
758
759 #ifdef HAVE_WINDOW_SYSTEM
760 #define CLEAR_IMAGE_CACHE_COUNT 101
761 static int clear_image_cache_count;
762
763 /* Null glyph slice */
764 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
765 #endif
766
767 /* True while redisplay_internal is in progress. */
768
769 bool redisplaying_p;
770
771 static Lisp_Object Qinhibit_free_realized_faces;
772 static Lisp_Object Qmode_line_default_help_echo;
773
774 /* If a string, XTread_socket generates an event to display that string.
775 (The display is done in read_char.) */
776
777 Lisp_Object help_echo_string;
778 Lisp_Object help_echo_window;
779 Lisp_Object help_echo_object;
780 ptrdiff_t help_echo_pos;
781
782 /* Temporary variable for XTread_socket. */
783
784 Lisp_Object previous_help_echo_string;
785
786 /* Platform-independent portion of hourglass implementation. */
787
788 /* Non-zero means an hourglass cursor is currently shown. */
789 int hourglass_shown_p;
790
791 /* If non-null, an asynchronous timer that, when it expires, displays
792 an hourglass cursor on all frames. */
793 struct atimer *hourglass_atimer;
794
795 /* Name of the face used to display glyphless characters. */
796 Lisp_Object Qglyphless_char;
797
798 /* Symbol for the purpose of Vglyphless_char_display. */
799 static Lisp_Object Qglyphless_char_display;
800
801 /* Method symbols for Vglyphless_char_display. */
802 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
803
804 /* Default pixel width of `thin-space' display method. */
805 #define THIN_SPACE_WIDTH 1
806
807 /* Default number of seconds to wait before displaying an hourglass
808 cursor. */
809 #define DEFAULT_HOURGLASS_DELAY 1
810
811 \f
812 /* Function prototypes. */
813
814 static void setup_for_ellipsis (struct it *, int);
815 static void set_iterator_to_next (struct it *, int);
816 static void mark_window_display_accurate_1 (struct window *, int);
817 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
818 static int display_prop_string_p (Lisp_Object, Lisp_Object);
819 static int cursor_row_p (struct glyph_row *);
820 static int redisplay_mode_lines (Lisp_Object, int);
821 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
822
823 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
824
825 static void handle_line_prefix (struct it *);
826
827 static void pint2str (char *, int, ptrdiff_t);
828 static void pint2hrstr (char *, int, ptrdiff_t);
829 static struct text_pos run_window_scroll_functions (Lisp_Object,
830 struct text_pos);
831 static void reconsider_clip_changes (struct window *, struct buffer *);
832 static int text_outside_line_unchanged_p (struct window *,
833 ptrdiff_t, ptrdiff_t);
834 static void store_mode_line_noprop_char (char);
835 static int store_mode_line_noprop (const char *, int, int);
836 static void handle_stop (struct it *);
837 static void handle_stop_backwards (struct it *, ptrdiff_t);
838 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
839 static void ensure_echo_area_buffers (void);
840 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
841 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
842 static int with_echo_area_buffer (struct window *, int,
843 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
844 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
845 static void clear_garbaged_frames (void);
846 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
847 static void pop_message (void);
848 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
849 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
850 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
851 static int display_echo_area (struct window *);
852 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
853 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
854 static Lisp_Object unwind_redisplay (Lisp_Object);
855 static int string_char_and_length (const unsigned char *, int *);
856 static struct text_pos display_prop_end (struct it *, Lisp_Object,
857 struct text_pos);
858 static int compute_window_start_on_continuation_line (struct window *);
859 static void insert_left_trunc_glyphs (struct it *);
860 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
861 Lisp_Object);
862 static void extend_face_to_end_of_line (struct it *);
863 static int append_space_for_newline (struct it *, int);
864 static int cursor_row_fully_visible_p (struct window *, int, int);
865 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
866 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
867 static int trailing_whitespace_p (ptrdiff_t);
868 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
869 static void push_it (struct it *, struct text_pos *);
870 static void iterate_out_of_display_property (struct it *);
871 static void pop_it (struct it *);
872 static void sync_frame_with_window_matrix_rows (struct window *);
873 static void redisplay_internal (void);
874 static int echo_area_display (int);
875 static void redisplay_windows (Lisp_Object);
876 static void redisplay_window (Lisp_Object, int);
877 static Lisp_Object redisplay_window_error (Lisp_Object);
878 static Lisp_Object redisplay_window_0 (Lisp_Object);
879 static Lisp_Object redisplay_window_1 (Lisp_Object);
880 static int set_cursor_from_row (struct window *, struct glyph_row *,
881 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
882 int, int);
883 static int update_menu_bar (struct frame *, int, int);
884 static int try_window_reusing_current_matrix (struct window *);
885 static int try_window_id (struct window *);
886 static int display_line (struct it *);
887 static int display_mode_lines (struct window *);
888 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
889 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
890 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
891 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
892 static void display_menu_bar (struct window *);
893 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
894 ptrdiff_t *);
895 static int display_string (const char *, Lisp_Object, Lisp_Object,
896 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
897 static void compute_line_metrics (struct it *);
898 static void run_redisplay_end_trigger_hook (struct it *);
899 static int get_overlay_strings (struct it *, ptrdiff_t);
900 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
901 static void next_overlay_string (struct it *);
902 static void reseat (struct it *, struct text_pos, int);
903 static void reseat_1 (struct it *, struct text_pos, int);
904 static void back_to_previous_visible_line_start (struct it *);
905 void reseat_at_previous_visible_line_start (struct it *);
906 static void reseat_at_next_visible_line_start (struct it *, int);
907 static int next_element_from_ellipsis (struct it *);
908 static int next_element_from_display_vector (struct it *);
909 static int next_element_from_string (struct it *);
910 static int next_element_from_c_string (struct it *);
911 static int next_element_from_buffer (struct it *);
912 static int next_element_from_composition (struct it *);
913 static int next_element_from_image (struct it *);
914 static int next_element_from_stretch (struct it *);
915 static void load_overlay_strings (struct it *, ptrdiff_t);
916 static int init_from_display_pos (struct it *, struct window *,
917 struct display_pos *);
918 static void reseat_to_string (struct it *, const char *,
919 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
920 static int get_next_display_element (struct it *);
921 static enum move_it_result
922 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
923 enum move_operation_enum);
924 void move_it_vertically_backward (struct it *, int);
925 static void get_visually_first_element (struct it *);
926 static void init_to_row_start (struct it *, struct window *,
927 struct glyph_row *);
928 static int init_to_row_end (struct it *, struct window *,
929 struct glyph_row *);
930 static void back_to_previous_line_start (struct it *);
931 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
932 static struct text_pos string_pos_nchars_ahead (struct text_pos,
933 Lisp_Object, ptrdiff_t);
934 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
935 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
936 static ptrdiff_t number_of_chars (const char *, int);
937 static void compute_stop_pos (struct it *);
938 static void compute_string_pos (struct text_pos *, struct text_pos,
939 Lisp_Object);
940 static int face_before_or_after_it_pos (struct it *, int);
941 static ptrdiff_t next_overlay_change (ptrdiff_t);
942 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
943 Lisp_Object, struct text_pos *, ptrdiff_t, int);
944 static int handle_single_display_spec (struct it *, Lisp_Object,
945 Lisp_Object, Lisp_Object,
946 struct text_pos *, ptrdiff_t, int, int);
947 static int underlying_face_id (struct it *);
948 static int in_ellipses_for_invisible_text_p (struct display_pos *,
949 struct window *);
950
951 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
952 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
953
954 #ifdef HAVE_WINDOW_SYSTEM
955
956 static void x_consider_frame_title (Lisp_Object);
957 static int tool_bar_lines_needed (struct frame *, int *);
958 static void update_tool_bar (struct frame *, int);
959 static void build_desired_tool_bar_string (struct frame *f);
960 static int redisplay_tool_bar (struct frame *);
961 static void display_tool_bar_line (struct it *, int);
962 static void notice_overwritten_cursor (struct window *,
963 enum glyph_row_area,
964 int, int, int, int);
965 static void append_stretch_glyph (struct it *, Lisp_Object,
966 int, int, int);
967
968
969 #endif /* HAVE_WINDOW_SYSTEM */
970
971 static void produce_special_glyphs (struct it *, enum display_element_type);
972 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
973 static int coords_in_mouse_face_p (struct window *, int, int);
974
975
976 \f
977 /***********************************************************************
978 Window display dimensions
979 ***********************************************************************/
980
981 /* Return the bottom boundary y-position for text lines in window W.
982 This is the first y position at which a line cannot start.
983 It is relative to the top of the window.
984
985 This is the height of W minus the height of a mode line, if any. */
986
987 int
988 window_text_bottom_y (struct window *w)
989 {
990 int height = WINDOW_TOTAL_HEIGHT (w);
991
992 if (WINDOW_WANTS_MODELINE_P (w))
993 height -= CURRENT_MODE_LINE_HEIGHT (w);
994 return height;
995 }
996
997 /* Return the pixel width of display area AREA of window W. AREA < 0
998 means return the total width of W, not including fringes to
999 the left and right of the window. */
1000
1001 int
1002 window_box_width (struct window *w, int area)
1003 {
1004 int cols = XFASTINT (w->total_cols);
1005 int pixels = 0;
1006
1007 if (!w->pseudo_window_p)
1008 {
1009 cols -= WINDOW_SCROLL_BAR_COLS (w);
1010
1011 if (area == TEXT_AREA)
1012 {
1013 if (INTEGERP (w->left_margin_cols))
1014 cols -= XFASTINT (w->left_margin_cols);
1015 if (INTEGERP (w->right_margin_cols))
1016 cols -= XFASTINT (w->right_margin_cols);
1017 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1018 }
1019 else if (area == LEFT_MARGIN_AREA)
1020 {
1021 cols = (INTEGERP (w->left_margin_cols)
1022 ? XFASTINT (w->left_margin_cols) : 0);
1023 pixels = 0;
1024 }
1025 else if (area == RIGHT_MARGIN_AREA)
1026 {
1027 cols = (INTEGERP (w->right_margin_cols)
1028 ? XFASTINT (w->right_margin_cols) : 0);
1029 pixels = 0;
1030 }
1031 }
1032
1033 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1034 }
1035
1036
1037 /* Return the pixel height of the display area of window W, not
1038 including mode lines of W, if any. */
1039
1040 int
1041 window_box_height (struct window *w)
1042 {
1043 struct frame *f = XFRAME (w->frame);
1044 int height = WINDOW_TOTAL_HEIGHT (w);
1045
1046 eassert (height >= 0);
1047
1048 /* Note: the code below that determines the mode-line/header-line
1049 height is essentially the same as that contained in the macro
1050 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1051 the appropriate glyph row has its `mode_line_p' flag set,
1052 and if it doesn't, uses estimate_mode_line_height instead. */
1053
1054 if (WINDOW_WANTS_MODELINE_P (w))
1055 {
1056 struct glyph_row *ml_row
1057 = (w->current_matrix && w->current_matrix->rows
1058 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1059 : 0);
1060 if (ml_row && ml_row->mode_line_p)
1061 height -= ml_row->height;
1062 else
1063 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1064 }
1065
1066 if (WINDOW_WANTS_HEADER_LINE_P (w))
1067 {
1068 struct glyph_row *hl_row
1069 = (w->current_matrix && w->current_matrix->rows
1070 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1071 : 0);
1072 if (hl_row && hl_row->mode_line_p)
1073 height -= hl_row->height;
1074 else
1075 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1076 }
1077
1078 /* With a very small font and a mode-line that's taller than
1079 default, we might end up with a negative height. */
1080 return max (0, height);
1081 }
1082
1083 /* Return the window-relative coordinate of the left edge of display
1084 area AREA of window W. AREA < 0 means return the left edge of the
1085 whole window, to the right of the left fringe of W. */
1086
1087 int
1088 window_box_left_offset (struct window *w, int area)
1089 {
1090 int x;
1091
1092 if (w->pseudo_window_p)
1093 return 0;
1094
1095 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1096
1097 if (area == TEXT_AREA)
1098 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1099 + window_box_width (w, LEFT_MARGIN_AREA));
1100 else if (area == RIGHT_MARGIN_AREA)
1101 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1102 + window_box_width (w, LEFT_MARGIN_AREA)
1103 + window_box_width (w, TEXT_AREA)
1104 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1105 ? 0
1106 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1107 else if (area == LEFT_MARGIN_AREA
1108 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1109 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1110
1111 return x;
1112 }
1113
1114
1115 /* Return the window-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1118
1119 int
1120 window_box_right_offset (struct window *w, int area)
1121 {
1122 return window_box_left_offset (w, area) + window_box_width (w, area);
1123 }
1124
1125 /* Return the frame-relative coordinate of the left edge of display
1126 area AREA of window W. AREA < 0 means return the left edge of the
1127 whole window, to the right of the left fringe of W. */
1128
1129 int
1130 window_box_left (struct window *w, int area)
1131 {
1132 struct frame *f = XFRAME (w->frame);
1133 int x;
1134
1135 if (w->pseudo_window_p)
1136 return FRAME_INTERNAL_BORDER_WIDTH (f);
1137
1138 x = (WINDOW_LEFT_EDGE_X (w)
1139 + window_box_left_offset (w, area));
1140
1141 return x;
1142 }
1143
1144
1145 /* Return the frame-relative coordinate of the right edge of display
1146 area AREA of window W. AREA < 0 means return the right edge of the
1147 whole window, to the left of the right fringe of W. */
1148
1149 int
1150 window_box_right (struct window *w, int area)
1151 {
1152 return window_box_left (w, area) + window_box_width (w, area);
1153 }
1154
1155 /* Get the bounding box of the display area AREA of window W, without
1156 mode lines, in frame-relative coordinates. AREA < 0 means the
1157 whole window, not including the left and right fringes of
1158 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1159 coordinates of the upper-left corner of the box. Return in
1160 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1161
1162 void
1163 window_box (struct window *w, int area, int *box_x, int *box_y,
1164 int *box_width, int *box_height)
1165 {
1166 if (box_width)
1167 *box_width = window_box_width (w, area);
1168 if (box_height)
1169 *box_height = window_box_height (w);
1170 if (box_x)
1171 *box_x = window_box_left (w, area);
1172 if (box_y)
1173 {
1174 *box_y = WINDOW_TOP_EDGE_Y (w);
1175 if (WINDOW_WANTS_HEADER_LINE_P (w))
1176 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1177 }
1178 }
1179
1180
1181 /* Get the bounding box of the display area AREA of window W, without
1182 mode lines. AREA < 0 means the whole window, not including the
1183 left and right fringe of the window. Return in *TOP_LEFT_X
1184 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1185 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1186 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1187 box. */
1188
1189 static void
1190 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1191 int *bottom_right_x, int *bottom_right_y)
1192 {
1193 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1194 bottom_right_y);
1195 *bottom_right_x += *top_left_x;
1196 *bottom_right_y += *top_left_y;
1197 }
1198
1199
1200 \f
1201 /***********************************************************************
1202 Utilities
1203 ***********************************************************************/
1204
1205 /* Return the bottom y-position of the line the iterator IT is in.
1206 This can modify IT's settings. */
1207
1208 int
1209 line_bottom_y (struct it *it)
1210 {
1211 int line_height = it->max_ascent + it->max_descent;
1212 int line_top_y = it->current_y;
1213
1214 if (line_height == 0)
1215 {
1216 if (last_height)
1217 line_height = last_height;
1218 else if (IT_CHARPOS (*it) < ZV)
1219 {
1220 move_it_by_lines (it, 1);
1221 line_height = (it->max_ascent || it->max_descent
1222 ? it->max_ascent + it->max_descent
1223 : last_height);
1224 }
1225 else
1226 {
1227 struct glyph_row *row = it->glyph_row;
1228
1229 /* Use the default character height. */
1230 it->glyph_row = NULL;
1231 it->what = IT_CHARACTER;
1232 it->c = ' ';
1233 it->len = 1;
1234 PRODUCE_GLYPHS (it);
1235 line_height = it->ascent + it->descent;
1236 it->glyph_row = row;
1237 }
1238 }
1239
1240 return line_top_y + line_height;
1241 }
1242
1243 /* Subroutine of pos_visible_p below. Extracts a display string, if
1244 any, from the display spec given as its argument. */
1245 static Lisp_Object
1246 string_from_display_spec (Lisp_Object spec)
1247 {
1248 if (CONSP (spec))
1249 {
1250 while (CONSP (spec))
1251 {
1252 if (STRINGP (XCAR (spec)))
1253 return XCAR (spec);
1254 spec = XCDR (spec);
1255 }
1256 }
1257 else if (VECTORP (spec))
1258 {
1259 ptrdiff_t i;
1260
1261 for (i = 0; i < ASIZE (spec); i++)
1262 {
1263 if (STRINGP (AREF (spec, i)))
1264 return AREF (spec, i);
1265 }
1266 return Qnil;
1267 }
1268
1269 return spec;
1270 }
1271
1272
1273 /* Limit insanely large values of W->hscroll on frame F to the largest
1274 value that will still prevent first_visible_x and last_visible_x of
1275 'struct it' from overflowing an int. */
1276 static int
1277 window_hscroll_limited (struct window *w, struct frame *f)
1278 {
1279 ptrdiff_t window_hscroll = w->hscroll;
1280 int window_text_width = window_box_width (w, TEXT_AREA);
1281 int colwidth = FRAME_COLUMN_WIDTH (f);
1282
1283 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1284 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1285
1286 return window_hscroll;
1287 }
1288
1289 /* Return 1 if position CHARPOS is visible in window W.
1290 CHARPOS < 0 means return info about WINDOW_END position.
1291 If visible, set *X and *Y to pixel coordinates of top left corner.
1292 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1293 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1294
1295 int
1296 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1297 int *rtop, int *rbot, int *rowh, int *vpos)
1298 {
1299 struct it it;
1300 void *itdata = bidi_shelve_cache ();
1301 struct text_pos top;
1302 int visible_p = 0;
1303 struct buffer *old_buffer = NULL;
1304
1305 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1306 return visible_p;
1307
1308 if (XBUFFER (w->buffer) != current_buffer)
1309 {
1310 old_buffer = current_buffer;
1311 set_buffer_internal_1 (XBUFFER (w->buffer));
1312 }
1313
1314 SET_TEXT_POS_FROM_MARKER (top, w->start);
1315 /* Scrolling a minibuffer window via scroll bar when the echo area
1316 shows long text sometimes resets the minibuffer contents behind
1317 our backs. */
1318 if (CHARPOS (top) > ZV)
1319 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1320
1321 /* Compute exact mode line heights. */
1322 if (WINDOW_WANTS_MODELINE_P (w))
1323 current_mode_line_height
1324 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1325 BVAR (current_buffer, mode_line_format));
1326
1327 if (WINDOW_WANTS_HEADER_LINE_P (w))
1328 current_header_line_height
1329 = display_mode_line (w, HEADER_LINE_FACE_ID,
1330 BVAR (current_buffer, header_line_format));
1331
1332 start_display (&it, w, top);
1333 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1334 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1335
1336 if (charpos >= 0
1337 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1338 && IT_CHARPOS (it) >= charpos)
1339 /* When scanning backwards under bidi iteration, move_it_to
1340 stops at or _before_ CHARPOS, because it stops at or to
1341 the _right_ of the character at CHARPOS. */
1342 || (it.bidi_p && it.bidi_it.scan_dir == -1
1343 && IT_CHARPOS (it) <= charpos)))
1344 {
1345 /* We have reached CHARPOS, or passed it. How the call to
1346 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1347 or covered by a display property, move_it_to stops at the end
1348 of the invisible text, to the right of CHARPOS. (ii) If
1349 CHARPOS is in a display vector, move_it_to stops on its last
1350 glyph. */
1351 int top_x = it.current_x;
1352 int top_y = it.current_y;
1353 /* Calling line_bottom_y may change it.method, it.position, etc. */
1354 enum it_method it_method = it.method;
1355 int bottom_y = (last_height = 0, line_bottom_y (&it));
1356 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1357
1358 if (top_y < window_top_y)
1359 visible_p = bottom_y > window_top_y;
1360 else if (top_y < it.last_visible_y)
1361 visible_p = 1;
1362 if (bottom_y >= it.last_visible_y
1363 && it.bidi_p && it.bidi_it.scan_dir == -1
1364 && IT_CHARPOS (it) < charpos)
1365 {
1366 /* When the last line of the window is scanned backwards
1367 under bidi iteration, we could be duped into thinking
1368 that we have passed CHARPOS, when in fact move_it_to
1369 simply stopped short of CHARPOS because it reached
1370 last_visible_y. To see if that's what happened, we call
1371 move_it_to again with a slightly larger vertical limit,
1372 and see if it actually moved vertically; if it did, we
1373 didn't really reach CHARPOS, which is beyond window end. */
1374 struct it save_it = it;
1375 /* Why 10? because we don't know how many canonical lines
1376 will the height of the next line(s) be. So we guess. */
1377 int ten_more_lines =
1378 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1379
1380 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1381 MOVE_TO_POS | MOVE_TO_Y);
1382 if (it.current_y > top_y)
1383 visible_p = 0;
1384
1385 it = save_it;
1386 }
1387 if (visible_p)
1388 {
1389 if (it_method == GET_FROM_DISPLAY_VECTOR)
1390 {
1391 /* We stopped on the last glyph of a display vector.
1392 Try and recompute. Hack alert! */
1393 if (charpos < 2 || top.charpos >= charpos)
1394 top_x = it.glyph_row->x;
1395 else
1396 {
1397 struct it it2;
1398 start_display (&it2, w, top);
1399 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1400 get_next_display_element (&it2);
1401 PRODUCE_GLYPHS (&it2);
1402 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1403 || it2.current_x > it2.last_visible_x)
1404 top_x = it.glyph_row->x;
1405 else
1406 {
1407 top_x = it2.current_x;
1408 top_y = it2.current_y;
1409 }
1410 }
1411 }
1412 else if (IT_CHARPOS (it) != charpos)
1413 {
1414 Lisp_Object cpos = make_number (charpos);
1415 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1416 Lisp_Object string = string_from_display_spec (spec);
1417 int newline_in_string = 0;
1418
1419 if (STRINGP (string))
1420 {
1421 const char *s = SSDATA (string);
1422 const char *e = s + SBYTES (string);
1423 while (s < e)
1424 {
1425 if (*s++ == '\n')
1426 {
1427 newline_in_string = 1;
1428 break;
1429 }
1430 }
1431 }
1432 /* The tricky code below is needed because there's a
1433 discrepancy between move_it_to and how we set cursor
1434 when the display line ends in a newline from a
1435 display string. move_it_to will stop _after_ such
1436 display strings, whereas set_cursor_from_row
1437 conspires with cursor_row_p to place the cursor on
1438 the first glyph produced from the display string. */
1439
1440 /* We have overshoot PT because it is covered by a
1441 display property whose value is a string. If the
1442 string includes embedded newlines, we are also in the
1443 wrong display line. Backtrack to the correct line,
1444 where the display string begins. */
1445 if (newline_in_string)
1446 {
1447 Lisp_Object startpos, endpos;
1448 EMACS_INT start, end;
1449 struct it it3;
1450 int it3_moved;
1451
1452 /* Find the first and the last buffer positions
1453 covered by the display string. */
1454 endpos =
1455 Fnext_single_char_property_change (cpos, Qdisplay,
1456 Qnil, Qnil);
1457 startpos =
1458 Fprevious_single_char_property_change (endpos, Qdisplay,
1459 Qnil, Qnil);
1460 start = XFASTINT (startpos);
1461 end = XFASTINT (endpos);
1462 /* Move to the last buffer position before the
1463 display property. */
1464 start_display (&it3, w, top);
1465 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1466 /* Move forward one more line if the position before
1467 the display string is a newline or if it is the
1468 rightmost character on a line that is
1469 continued or word-wrapped. */
1470 if (it3.method == GET_FROM_BUFFER
1471 && it3.c == '\n')
1472 move_it_by_lines (&it3, 1);
1473 else if (move_it_in_display_line_to (&it3, -1,
1474 it3.current_x
1475 + it3.pixel_width,
1476 MOVE_TO_X)
1477 == MOVE_LINE_CONTINUED)
1478 {
1479 move_it_by_lines (&it3, 1);
1480 /* When we are under word-wrap, the #$@%!
1481 move_it_by_lines moves 2 lines, so we need to
1482 fix that up. */
1483 if (it3.line_wrap == WORD_WRAP)
1484 move_it_by_lines (&it3, -1);
1485 }
1486
1487 /* Record the vertical coordinate of the display
1488 line where we wound up. */
1489 top_y = it3.current_y;
1490 if (it3.bidi_p)
1491 {
1492 /* When characters are reordered for display,
1493 the character displayed to the left of the
1494 display string could be _after_ the display
1495 property in the logical order. Use the
1496 smallest vertical position of these two. */
1497 start_display (&it3, w, top);
1498 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1499 if (it3.current_y < top_y)
1500 top_y = it3.current_y;
1501 }
1502 /* Move from the top of the window to the beginning
1503 of the display line where the display string
1504 begins. */
1505 start_display (&it3, w, top);
1506 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1507 /* If it3_moved stays zero after the 'while' loop
1508 below, that means we already were at a newline
1509 before the loop (e.g., the display string begins
1510 with a newline), so we don't need to (and cannot)
1511 inspect the glyphs of it3.glyph_row, because
1512 PRODUCE_GLYPHS will not produce anything for a
1513 newline, and thus it3.glyph_row stays at its
1514 stale content it got at top of the window. */
1515 it3_moved = 0;
1516 /* Finally, advance the iterator until we hit the
1517 first display element whose character position is
1518 CHARPOS, or until the first newline from the
1519 display string, which signals the end of the
1520 display line. */
1521 while (get_next_display_element (&it3))
1522 {
1523 PRODUCE_GLYPHS (&it3);
1524 if (IT_CHARPOS (it3) == charpos
1525 || ITERATOR_AT_END_OF_LINE_P (&it3))
1526 break;
1527 it3_moved = 1;
1528 set_iterator_to_next (&it3, 0);
1529 }
1530 top_x = it3.current_x - it3.pixel_width;
1531 /* Normally, we would exit the above loop because we
1532 found the display element whose character
1533 position is CHARPOS. For the contingency that we
1534 didn't, and stopped at the first newline from the
1535 display string, move back over the glyphs
1536 produced from the string, until we find the
1537 rightmost glyph not from the string. */
1538 if (it3_moved
1539 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1540 {
1541 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1542 + it3.glyph_row->used[TEXT_AREA];
1543
1544 while (EQ ((g - 1)->object, string))
1545 {
1546 --g;
1547 top_x -= g->pixel_width;
1548 }
1549 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1550 + it3.glyph_row->used[TEXT_AREA]);
1551 }
1552 }
1553 }
1554
1555 *x = top_x;
1556 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1557 *rtop = max (0, window_top_y - top_y);
1558 *rbot = max (0, bottom_y - it.last_visible_y);
1559 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1560 - max (top_y, window_top_y)));
1561 *vpos = it.vpos;
1562 }
1563 }
1564 else
1565 {
1566 /* We were asked to provide info about WINDOW_END. */
1567 struct it it2;
1568 void *it2data = NULL;
1569
1570 SAVE_IT (it2, it, it2data);
1571 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1572 move_it_by_lines (&it, 1);
1573 if (charpos < IT_CHARPOS (it)
1574 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1575 {
1576 visible_p = 1;
1577 RESTORE_IT (&it2, &it2, it2data);
1578 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1579 *x = it2.current_x;
1580 *y = it2.current_y + it2.max_ascent - it2.ascent;
1581 *rtop = max (0, -it2.current_y);
1582 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1583 - it.last_visible_y));
1584 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1585 it.last_visible_y)
1586 - max (it2.current_y,
1587 WINDOW_HEADER_LINE_HEIGHT (w))));
1588 *vpos = it2.vpos;
1589 }
1590 else
1591 bidi_unshelve_cache (it2data, 1);
1592 }
1593 bidi_unshelve_cache (itdata, 0);
1594
1595 if (old_buffer)
1596 set_buffer_internal_1 (old_buffer);
1597
1598 current_header_line_height = current_mode_line_height = -1;
1599
1600 if (visible_p && w->hscroll > 0)
1601 *x -=
1602 window_hscroll_limited (w, WINDOW_XFRAME (w))
1603 * WINDOW_FRAME_COLUMN_WIDTH (w);
1604
1605 #if 0
1606 /* Debugging code. */
1607 if (visible_p)
1608 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1609 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1610 else
1611 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1612 #endif
1613
1614 return visible_p;
1615 }
1616
1617
1618 /* Return the next character from STR. Return in *LEN the length of
1619 the character. This is like STRING_CHAR_AND_LENGTH but never
1620 returns an invalid character. If we find one, we return a `?', but
1621 with the length of the invalid character. */
1622
1623 static int
1624 string_char_and_length (const unsigned char *str, int *len)
1625 {
1626 int c;
1627
1628 c = STRING_CHAR_AND_LENGTH (str, *len);
1629 if (!CHAR_VALID_P (c))
1630 /* We may not change the length here because other places in Emacs
1631 don't use this function, i.e. they silently accept invalid
1632 characters. */
1633 c = '?';
1634
1635 return c;
1636 }
1637
1638
1639
1640 /* Given a position POS containing a valid character and byte position
1641 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1642
1643 static struct text_pos
1644 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1645 {
1646 eassert (STRINGP (string) && nchars >= 0);
1647
1648 if (STRING_MULTIBYTE (string))
1649 {
1650 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1651 int len;
1652
1653 while (nchars--)
1654 {
1655 string_char_and_length (p, &len);
1656 p += len;
1657 CHARPOS (pos) += 1;
1658 BYTEPOS (pos) += len;
1659 }
1660 }
1661 else
1662 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1663
1664 return pos;
1665 }
1666
1667
1668 /* Value is the text position, i.e. character and byte position,
1669 for character position CHARPOS in STRING. */
1670
1671 static struct text_pos
1672 string_pos (ptrdiff_t charpos, Lisp_Object string)
1673 {
1674 struct text_pos pos;
1675 eassert (STRINGP (string));
1676 eassert (charpos >= 0);
1677 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1678 return pos;
1679 }
1680
1681
1682 /* Value is a text position, i.e. character and byte position, for
1683 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1684 means recognize multibyte characters. */
1685
1686 static struct text_pos
1687 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1688 {
1689 struct text_pos pos;
1690
1691 eassert (s != NULL);
1692 eassert (charpos >= 0);
1693
1694 if (multibyte_p)
1695 {
1696 int len;
1697
1698 SET_TEXT_POS (pos, 0, 0);
1699 while (charpos--)
1700 {
1701 string_char_and_length ((const unsigned char *) s, &len);
1702 s += len;
1703 CHARPOS (pos) += 1;
1704 BYTEPOS (pos) += len;
1705 }
1706 }
1707 else
1708 SET_TEXT_POS (pos, charpos, charpos);
1709
1710 return pos;
1711 }
1712
1713
1714 /* Value is the number of characters in C string S. MULTIBYTE_P
1715 non-zero means recognize multibyte characters. */
1716
1717 static ptrdiff_t
1718 number_of_chars (const char *s, int multibyte_p)
1719 {
1720 ptrdiff_t nchars;
1721
1722 if (multibyte_p)
1723 {
1724 ptrdiff_t rest = strlen (s);
1725 int len;
1726 const unsigned char *p = (const unsigned char *) s;
1727
1728 for (nchars = 0; rest > 0; ++nchars)
1729 {
1730 string_char_and_length (p, &len);
1731 rest -= len, p += len;
1732 }
1733 }
1734 else
1735 nchars = strlen (s);
1736
1737 return nchars;
1738 }
1739
1740
1741 /* Compute byte position NEWPOS->bytepos corresponding to
1742 NEWPOS->charpos. POS is a known position in string STRING.
1743 NEWPOS->charpos must be >= POS.charpos. */
1744
1745 static void
1746 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1747 {
1748 eassert (STRINGP (string));
1749 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1750
1751 if (STRING_MULTIBYTE (string))
1752 *newpos = string_pos_nchars_ahead (pos, string,
1753 CHARPOS (*newpos) - CHARPOS (pos));
1754 else
1755 BYTEPOS (*newpos) = CHARPOS (*newpos);
1756 }
1757
1758 /* EXPORT:
1759 Return an estimation of the pixel height of mode or header lines on
1760 frame F. FACE_ID specifies what line's height to estimate. */
1761
1762 int
1763 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1764 {
1765 #ifdef HAVE_WINDOW_SYSTEM
1766 if (FRAME_WINDOW_P (f))
1767 {
1768 int height = FONT_HEIGHT (FRAME_FONT (f));
1769
1770 /* This function is called so early when Emacs starts that the face
1771 cache and mode line face are not yet initialized. */
1772 if (FRAME_FACE_CACHE (f))
1773 {
1774 struct face *face = FACE_FROM_ID (f, face_id);
1775 if (face)
1776 {
1777 if (face->font)
1778 height = FONT_HEIGHT (face->font);
1779 if (face->box_line_width > 0)
1780 height += 2 * face->box_line_width;
1781 }
1782 }
1783
1784 return height;
1785 }
1786 #endif
1787
1788 return 1;
1789 }
1790
1791 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1792 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1793 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1794 not force the value into range. */
1795
1796 void
1797 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1798 int *x, int *y, NativeRectangle *bounds, int noclip)
1799 {
1800
1801 #ifdef HAVE_WINDOW_SYSTEM
1802 if (FRAME_WINDOW_P (f))
1803 {
1804 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1805 even for negative values. */
1806 if (pix_x < 0)
1807 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1808 if (pix_y < 0)
1809 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1810
1811 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1812 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1813
1814 if (bounds)
1815 STORE_NATIVE_RECT (*bounds,
1816 FRAME_COL_TO_PIXEL_X (f, pix_x),
1817 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1818 FRAME_COLUMN_WIDTH (f) - 1,
1819 FRAME_LINE_HEIGHT (f) - 1);
1820
1821 if (!noclip)
1822 {
1823 if (pix_x < 0)
1824 pix_x = 0;
1825 else if (pix_x > FRAME_TOTAL_COLS (f))
1826 pix_x = FRAME_TOTAL_COLS (f);
1827
1828 if (pix_y < 0)
1829 pix_y = 0;
1830 else if (pix_y > FRAME_LINES (f))
1831 pix_y = FRAME_LINES (f);
1832 }
1833 }
1834 #endif
1835
1836 *x = pix_x;
1837 *y = pix_y;
1838 }
1839
1840
1841 /* Find the glyph under window-relative coordinates X/Y in window W.
1842 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1843 strings. Return in *HPOS and *VPOS the row and column number of
1844 the glyph found. Return in *AREA the glyph area containing X.
1845 Value is a pointer to the glyph found or null if X/Y is not on
1846 text, or we can't tell because W's current matrix is not up to
1847 date. */
1848
1849 static
1850 struct glyph *
1851 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1852 int *dx, int *dy, int *area)
1853 {
1854 struct glyph *glyph, *end;
1855 struct glyph_row *row = NULL;
1856 int x0, i;
1857
1858 /* Find row containing Y. Give up if some row is not enabled. */
1859 for (i = 0; i < w->current_matrix->nrows; ++i)
1860 {
1861 row = MATRIX_ROW (w->current_matrix, i);
1862 if (!row->enabled_p)
1863 return NULL;
1864 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1865 break;
1866 }
1867
1868 *vpos = i;
1869 *hpos = 0;
1870
1871 /* Give up if Y is not in the window. */
1872 if (i == w->current_matrix->nrows)
1873 return NULL;
1874
1875 /* Get the glyph area containing X. */
1876 if (w->pseudo_window_p)
1877 {
1878 *area = TEXT_AREA;
1879 x0 = 0;
1880 }
1881 else
1882 {
1883 if (x < window_box_left_offset (w, TEXT_AREA))
1884 {
1885 *area = LEFT_MARGIN_AREA;
1886 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1887 }
1888 else if (x < window_box_right_offset (w, TEXT_AREA))
1889 {
1890 *area = TEXT_AREA;
1891 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1892 }
1893 else
1894 {
1895 *area = RIGHT_MARGIN_AREA;
1896 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1897 }
1898 }
1899
1900 /* Find glyph containing X. */
1901 glyph = row->glyphs[*area];
1902 end = glyph + row->used[*area];
1903 x -= x0;
1904 while (glyph < end && x >= glyph->pixel_width)
1905 {
1906 x -= glyph->pixel_width;
1907 ++glyph;
1908 }
1909
1910 if (glyph == end)
1911 return NULL;
1912
1913 if (dx)
1914 {
1915 *dx = x;
1916 *dy = y - (row->y + row->ascent - glyph->ascent);
1917 }
1918
1919 *hpos = glyph - row->glyphs[*area];
1920 return glyph;
1921 }
1922
1923 /* Convert frame-relative x/y to coordinates relative to window W.
1924 Takes pseudo-windows into account. */
1925
1926 static void
1927 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1928 {
1929 if (w->pseudo_window_p)
1930 {
1931 /* A pseudo-window is always full-width, and starts at the
1932 left edge of the frame, plus a frame border. */
1933 struct frame *f = XFRAME (w->frame);
1934 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1935 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1936 }
1937 else
1938 {
1939 *x -= WINDOW_LEFT_EDGE_X (w);
1940 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1941 }
1942 }
1943
1944 #ifdef HAVE_WINDOW_SYSTEM
1945
1946 /* EXPORT:
1947 Return in RECTS[] at most N clipping rectangles for glyph string S.
1948 Return the number of stored rectangles. */
1949
1950 int
1951 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1952 {
1953 XRectangle r;
1954
1955 if (n <= 0)
1956 return 0;
1957
1958 if (s->row->full_width_p)
1959 {
1960 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1961 r.x = WINDOW_LEFT_EDGE_X (s->w);
1962 r.width = WINDOW_TOTAL_WIDTH (s->w);
1963
1964 /* Unless displaying a mode or menu bar line, which are always
1965 fully visible, clip to the visible part of the row. */
1966 if (s->w->pseudo_window_p)
1967 r.height = s->row->visible_height;
1968 else
1969 r.height = s->height;
1970 }
1971 else
1972 {
1973 /* This is a text line that may be partially visible. */
1974 r.x = window_box_left (s->w, s->area);
1975 r.width = window_box_width (s->w, s->area);
1976 r.height = s->row->visible_height;
1977 }
1978
1979 if (s->clip_head)
1980 if (r.x < s->clip_head->x)
1981 {
1982 if (r.width >= s->clip_head->x - r.x)
1983 r.width -= s->clip_head->x - r.x;
1984 else
1985 r.width = 0;
1986 r.x = s->clip_head->x;
1987 }
1988 if (s->clip_tail)
1989 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1990 {
1991 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1992 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1993 else
1994 r.width = 0;
1995 }
1996
1997 /* If S draws overlapping rows, it's sufficient to use the top and
1998 bottom of the window for clipping because this glyph string
1999 intentionally draws over other lines. */
2000 if (s->for_overlaps)
2001 {
2002 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2003 r.height = window_text_bottom_y (s->w) - r.y;
2004
2005 /* Alas, the above simple strategy does not work for the
2006 environments with anti-aliased text: if the same text is
2007 drawn onto the same place multiple times, it gets thicker.
2008 If the overlap we are processing is for the erased cursor, we
2009 take the intersection with the rectangle of the cursor. */
2010 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2011 {
2012 XRectangle rc, r_save = r;
2013
2014 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2015 rc.y = s->w->phys_cursor.y;
2016 rc.width = s->w->phys_cursor_width;
2017 rc.height = s->w->phys_cursor_height;
2018
2019 x_intersect_rectangles (&r_save, &rc, &r);
2020 }
2021 }
2022 else
2023 {
2024 /* Don't use S->y for clipping because it doesn't take partially
2025 visible lines into account. For example, it can be negative for
2026 partially visible lines at the top of a window. */
2027 if (!s->row->full_width_p
2028 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2029 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2030 else
2031 r.y = max (0, s->row->y);
2032 }
2033
2034 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2035
2036 /* If drawing the cursor, don't let glyph draw outside its
2037 advertised boundaries. Cleartype does this under some circumstances. */
2038 if (s->hl == DRAW_CURSOR)
2039 {
2040 struct glyph *glyph = s->first_glyph;
2041 int height, max_y;
2042
2043 if (s->x > r.x)
2044 {
2045 r.width -= s->x - r.x;
2046 r.x = s->x;
2047 }
2048 r.width = min (r.width, glyph->pixel_width);
2049
2050 /* If r.y is below window bottom, ensure that we still see a cursor. */
2051 height = min (glyph->ascent + glyph->descent,
2052 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2053 max_y = window_text_bottom_y (s->w) - height;
2054 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2055 if (s->ybase - glyph->ascent > max_y)
2056 {
2057 r.y = max_y;
2058 r.height = height;
2059 }
2060 else
2061 {
2062 /* Don't draw cursor glyph taller than our actual glyph. */
2063 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2064 if (height < r.height)
2065 {
2066 max_y = r.y + r.height;
2067 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2068 r.height = min (max_y - r.y, height);
2069 }
2070 }
2071 }
2072
2073 if (s->row->clip)
2074 {
2075 XRectangle r_save = r;
2076
2077 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2078 r.width = 0;
2079 }
2080
2081 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2082 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2083 {
2084 #ifdef CONVERT_FROM_XRECT
2085 CONVERT_FROM_XRECT (r, *rects);
2086 #else
2087 *rects = r;
2088 #endif
2089 return 1;
2090 }
2091 else
2092 {
2093 /* If we are processing overlapping and allowed to return
2094 multiple clipping rectangles, we exclude the row of the glyph
2095 string from the clipping rectangle. This is to avoid drawing
2096 the same text on the environment with anti-aliasing. */
2097 #ifdef CONVERT_FROM_XRECT
2098 XRectangle rs[2];
2099 #else
2100 XRectangle *rs = rects;
2101 #endif
2102 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2103
2104 if (s->for_overlaps & OVERLAPS_PRED)
2105 {
2106 rs[i] = r;
2107 if (r.y + r.height > row_y)
2108 {
2109 if (r.y < row_y)
2110 rs[i].height = row_y - r.y;
2111 else
2112 rs[i].height = 0;
2113 }
2114 i++;
2115 }
2116 if (s->for_overlaps & OVERLAPS_SUCC)
2117 {
2118 rs[i] = r;
2119 if (r.y < row_y + s->row->visible_height)
2120 {
2121 if (r.y + r.height > row_y + s->row->visible_height)
2122 {
2123 rs[i].y = row_y + s->row->visible_height;
2124 rs[i].height = r.y + r.height - rs[i].y;
2125 }
2126 else
2127 rs[i].height = 0;
2128 }
2129 i++;
2130 }
2131
2132 n = i;
2133 #ifdef CONVERT_FROM_XRECT
2134 for (i = 0; i < n; i++)
2135 CONVERT_FROM_XRECT (rs[i], rects[i]);
2136 #endif
2137 return n;
2138 }
2139 }
2140
2141 /* EXPORT:
2142 Return in *NR the clipping rectangle for glyph string S. */
2143
2144 void
2145 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2146 {
2147 get_glyph_string_clip_rects (s, nr, 1);
2148 }
2149
2150
2151 /* EXPORT:
2152 Return the position and height of the phys cursor in window W.
2153 Set w->phys_cursor_width to width of phys cursor.
2154 */
2155
2156 void
2157 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2158 struct glyph *glyph, int *xp, int *yp, int *heightp)
2159 {
2160 struct frame *f = XFRAME (WINDOW_FRAME (w));
2161 int x, y, wd, h, h0, y0;
2162
2163 /* Compute the width of the rectangle to draw. If on a stretch
2164 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2165 rectangle as wide as the glyph, but use a canonical character
2166 width instead. */
2167 wd = glyph->pixel_width - 1;
2168 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2169 wd++; /* Why? */
2170 #endif
2171
2172 x = w->phys_cursor.x;
2173 if (x < 0)
2174 {
2175 wd += x;
2176 x = 0;
2177 }
2178
2179 if (glyph->type == STRETCH_GLYPH
2180 && !x_stretch_cursor_p)
2181 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2182 w->phys_cursor_width = wd;
2183
2184 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2185
2186 /* If y is below window bottom, ensure that we still see a cursor. */
2187 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2188
2189 h = max (h0, glyph->ascent + glyph->descent);
2190 h0 = min (h0, glyph->ascent + glyph->descent);
2191
2192 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2193 if (y < y0)
2194 {
2195 h = max (h - (y0 - y) + 1, h0);
2196 y = y0 - 1;
2197 }
2198 else
2199 {
2200 y0 = window_text_bottom_y (w) - h0;
2201 if (y > y0)
2202 {
2203 h += y - y0;
2204 y = y0;
2205 }
2206 }
2207
2208 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2209 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2210 *heightp = h;
2211 }
2212
2213 /*
2214 * Remember which glyph the mouse is over.
2215 */
2216
2217 void
2218 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2219 {
2220 Lisp_Object window;
2221 struct window *w;
2222 struct glyph_row *r, *gr, *end_row;
2223 enum window_part part;
2224 enum glyph_row_area area;
2225 int x, y, width, height;
2226
2227 /* Try to determine frame pixel position and size of the glyph under
2228 frame pixel coordinates X/Y on frame F. */
2229
2230 if (!f->glyphs_initialized_p
2231 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2232 NILP (window)))
2233 {
2234 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2235 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2236 goto virtual_glyph;
2237 }
2238
2239 w = XWINDOW (window);
2240 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2241 height = WINDOW_FRAME_LINE_HEIGHT (w);
2242
2243 x = window_relative_x_coord (w, part, gx);
2244 y = gy - WINDOW_TOP_EDGE_Y (w);
2245
2246 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2247 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2248
2249 if (w->pseudo_window_p)
2250 {
2251 area = TEXT_AREA;
2252 part = ON_MODE_LINE; /* Don't adjust margin. */
2253 goto text_glyph;
2254 }
2255
2256 switch (part)
2257 {
2258 case ON_LEFT_MARGIN:
2259 area = LEFT_MARGIN_AREA;
2260 goto text_glyph;
2261
2262 case ON_RIGHT_MARGIN:
2263 area = RIGHT_MARGIN_AREA;
2264 goto text_glyph;
2265
2266 case ON_HEADER_LINE:
2267 case ON_MODE_LINE:
2268 gr = (part == ON_HEADER_LINE
2269 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2270 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2271 gy = gr->y;
2272 area = TEXT_AREA;
2273 goto text_glyph_row_found;
2274
2275 case ON_TEXT:
2276 area = TEXT_AREA;
2277
2278 text_glyph:
2279 gr = 0; gy = 0;
2280 for (; r <= end_row && r->enabled_p; ++r)
2281 if (r->y + r->height > y)
2282 {
2283 gr = r; gy = r->y;
2284 break;
2285 }
2286
2287 text_glyph_row_found:
2288 if (gr && gy <= y)
2289 {
2290 struct glyph *g = gr->glyphs[area];
2291 struct glyph *end = g + gr->used[area];
2292
2293 height = gr->height;
2294 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2295 if (gx + g->pixel_width > x)
2296 break;
2297
2298 if (g < end)
2299 {
2300 if (g->type == IMAGE_GLYPH)
2301 {
2302 /* Don't remember when mouse is over image, as
2303 image may have hot-spots. */
2304 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2305 return;
2306 }
2307 width = g->pixel_width;
2308 }
2309 else
2310 {
2311 /* Use nominal char spacing at end of line. */
2312 x -= gx;
2313 gx += (x / width) * width;
2314 }
2315
2316 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2317 gx += window_box_left_offset (w, area);
2318 }
2319 else
2320 {
2321 /* Use nominal line height at end of window. */
2322 gx = (x / width) * width;
2323 y -= gy;
2324 gy += (y / height) * height;
2325 }
2326 break;
2327
2328 case ON_LEFT_FRINGE:
2329 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2330 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2331 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2332 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2333 goto row_glyph;
2334
2335 case ON_RIGHT_FRINGE:
2336 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2337 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2338 : window_box_right_offset (w, TEXT_AREA));
2339 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2340 goto row_glyph;
2341
2342 case ON_SCROLL_BAR:
2343 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2344 ? 0
2345 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2346 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2347 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2348 : 0)));
2349 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2350
2351 row_glyph:
2352 gr = 0, gy = 0;
2353 for (; r <= end_row && r->enabled_p; ++r)
2354 if (r->y + r->height > y)
2355 {
2356 gr = r; gy = r->y;
2357 break;
2358 }
2359
2360 if (gr && gy <= y)
2361 height = gr->height;
2362 else
2363 {
2364 /* Use nominal line height at end of window. */
2365 y -= gy;
2366 gy += (y / height) * height;
2367 }
2368 break;
2369
2370 default:
2371 ;
2372 virtual_glyph:
2373 /* If there is no glyph under the mouse, then we divide the screen
2374 into a grid of the smallest glyph in the frame, and use that
2375 as our "glyph". */
2376
2377 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2378 round down even for negative values. */
2379 if (gx < 0)
2380 gx -= width - 1;
2381 if (gy < 0)
2382 gy -= height - 1;
2383
2384 gx = (gx / width) * width;
2385 gy = (gy / height) * height;
2386
2387 goto store_rect;
2388 }
2389
2390 gx += WINDOW_LEFT_EDGE_X (w);
2391 gy += WINDOW_TOP_EDGE_Y (w);
2392
2393 store_rect:
2394 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2395
2396 /* Visible feedback for debugging. */
2397 #if 0
2398 #if HAVE_X_WINDOWS
2399 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2400 f->output_data.x->normal_gc,
2401 gx, gy, width, height);
2402 #endif
2403 #endif
2404 }
2405
2406
2407 #endif /* HAVE_WINDOW_SYSTEM */
2408
2409 \f
2410 /***********************************************************************
2411 Lisp form evaluation
2412 ***********************************************************************/
2413
2414 /* Error handler for safe_eval and safe_call. */
2415
2416 static Lisp_Object
2417 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2418 {
2419 add_to_log ("Error during redisplay: %S signaled %S",
2420 Flist (nargs, args), arg);
2421 return Qnil;
2422 }
2423
2424 /* Call function FUNC with the rest of NARGS - 1 arguments
2425 following. Return the result, or nil if something went
2426 wrong. Prevent redisplay during the evaluation. */
2427
2428 Lisp_Object
2429 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2430 {
2431 Lisp_Object val;
2432
2433 if (inhibit_eval_during_redisplay)
2434 val = Qnil;
2435 else
2436 {
2437 va_list ap;
2438 ptrdiff_t i;
2439 ptrdiff_t count = SPECPDL_INDEX ();
2440 struct gcpro gcpro1;
2441 Lisp_Object *args = alloca (nargs * word_size);
2442
2443 args[0] = func;
2444 va_start (ap, func);
2445 for (i = 1; i < nargs; i++)
2446 args[i] = va_arg (ap, Lisp_Object);
2447 va_end (ap);
2448
2449 GCPRO1 (args[0]);
2450 gcpro1.nvars = nargs;
2451 specbind (Qinhibit_redisplay, Qt);
2452 /* Use Qt to ensure debugger does not run,
2453 so there is no possibility of wanting to redisplay. */
2454 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2455 safe_eval_handler);
2456 UNGCPRO;
2457 val = unbind_to (count, val);
2458 }
2459
2460 return val;
2461 }
2462
2463
2464 /* Call function FN with one argument ARG.
2465 Return the result, or nil if something went wrong. */
2466
2467 Lisp_Object
2468 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2469 {
2470 return safe_call (2, fn, arg);
2471 }
2472
2473 static Lisp_Object Qeval;
2474
2475 Lisp_Object
2476 safe_eval (Lisp_Object sexpr)
2477 {
2478 return safe_call1 (Qeval, sexpr);
2479 }
2480
2481 /* Call function FN with two arguments ARG1 and ARG2.
2482 Return the result, or nil if something went wrong. */
2483
2484 Lisp_Object
2485 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2486 {
2487 return safe_call (3, fn, arg1, arg2);
2488 }
2489
2490
2491 \f
2492 /***********************************************************************
2493 Debugging
2494 ***********************************************************************/
2495
2496 #if 0
2497
2498 /* Define CHECK_IT to perform sanity checks on iterators.
2499 This is for debugging. It is too slow to do unconditionally. */
2500
2501 static void
2502 check_it (struct it *it)
2503 {
2504 if (it->method == GET_FROM_STRING)
2505 {
2506 eassert (STRINGP (it->string));
2507 eassert (IT_STRING_CHARPOS (*it) >= 0);
2508 }
2509 else
2510 {
2511 eassert (IT_STRING_CHARPOS (*it) < 0);
2512 if (it->method == GET_FROM_BUFFER)
2513 {
2514 /* Check that character and byte positions agree. */
2515 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2516 }
2517 }
2518
2519 if (it->dpvec)
2520 eassert (it->current.dpvec_index >= 0);
2521 else
2522 eassert (it->current.dpvec_index < 0);
2523 }
2524
2525 #define CHECK_IT(IT) check_it ((IT))
2526
2527 #else /* not 0 */
2528
2529 #define CHECK_IT(IT) (void) 0
2530
2531 #endif /* not 0 */
2532
2533
2534 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2535
2536 /* Check that the window end of window W is what we expect it
2537 to be---the last row in the current matrix displaying text. */
2538
2539 static void
2540 check_window_end (struct window *w)
2541 {
2542 if (!MINI_WINDOW_P (w)
2543 && !NILP (w->window_end_valid))
2544 {
2545 struct glyph_row *row;
2546 eassert ((row = MATRIX_ROW (w->current_matrix,
2547 XFASTINT (w->window_end_vpos)),
2548 !row->enabled_p
2549 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2550 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2551 }
2552 }
2553
2554 #define CHECK_WINDOW_END(W) check_window_end ((W))
2555
2556 #else
2557
2558 #define CHECK_WINDOW_END(W) (void) 0
2559
2560 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2561
2562 /* Return mark position if current buffer has the region of non-zero length,
2563 or -1 otherwise. */
2564
2565 static ptrdiff_t
2566 markpos_of_region (void)
2567 {
2568 if (!NILP (Vtransient_mark_mode)
2569 && !NILP (BVAR (current_buffer, mark_active))
2570 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2571 {
2572 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2573
2574 if (markpos != PT)
2575 return markpos;
2576 }
2577 return -1;
2578 }
2579
2580 /***********************************************************************
2581 Iterator initialization
2582 ***********************************************************************/
2583
2584 /* Initialize IT for displaying current_buffer in window W, starting
2585 at character position CHARPOS. CHARPOS < 0 means that no buffer
2586 position is specified which is useful when the iterator is assigned
2587 a position later. BYTEPOS is the byte position corresponding to
2588 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2589
2590 If ROW is not null, calls to produce_glyphs with IT as parameter
2591 will produce glyphs in that row.
2592
2593 BASE_FACE_ID is the id of a base face to use. It must be one of
2594 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2595 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2596 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2597
2598 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2599 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2600 will be initialized to use the corresponding mode line glyph row of
2601 the desired matrix of W. */
2602
2603 void
2604 init_iterator (struct it *it, struct window *w,
2605 ptrdiff_t charpos, ptrdiff_t bytepos,
2606 struct glyph_row *row, enum face_id base_face_id)
2607 {
2608 ptrdiff_t markpos;
2609 enum face_id remapped_base_face_id = base_face_id;
2610
2611 /* Some precondition checks. */
2612 eassert (w != NULL && it != NULL);
2613 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2614 && charpos <= ZV));
2615
2616 /* If face attributes have been changed since the last redisplay,
2617 free realized faces now because they depend on face definitions
2618 that might have changed. Don't free faces while there might be
2619 desired matrices pending which reference these faces. */
2620 if (face_change_count && !inhibit_free_realized_faces)
2621 {
2622 face_change_count = 0;
2623 free_all_realized_faces (Qnil);
2624 }
2625
2626 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2627 if (! NILP (Vface_remapping_alist))
2628 remapped_base_face_id
2629 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2630
2631 /* Use one of the mode line rows of W's desired matrix if
2632 appropriate. */
2633 if (row == NULL)
2634 {
2635 if (base_face_id == MODE_LINE_FACE_ID
2636 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2637 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2638 else if (base_face_id == HEADER_LINE_FACE_ID)
2639 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2640 }
2641
2642 /* Clear IT. */
2643 memset (it, 0, sizeof *it);
2644 it->current.overlay_string_index = -1;
2645 it->current.dpvec_index = -1;
2646 it->base_face_id = remapped_base_face_id;
2647 it->string = Qnil;
2648 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2649 it->paragraph_embedding = L2R;
2650 it->bidi_it.string.lstring = Qnil;
2651 it->bidi_it.string.s = NULL;
2652 it->bidi_it.string.bufpos = 0;
2653
2654 /* The window in which we iterate over current_buffer: */
2655 XSETWINDOW (it->window, w);
2656 it->w = w;
2657 it->f = XFRAME (w->frame);
2658
2659 it->cmp_it.id = -1;
2660
2661 /* Extra space between lines (on window systems only). */
2662 if (base_face_id == DEFAULT_FACE_ID
2663 && FRAME_WINDOW_P (it->f))
2664 {
2665 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2666 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2667 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2668 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2669 * FRAME_LINE_HEIGHT (it->f));
2670 else if (it->f->extra_line_spacing > 0)
2671 it->extra_line_spacing = it->f->extra_line_spacing;
2672 it->max_extra_line_spacing = 0;
2673 }
2674
2675 /* If realized faces have been removed, e.g. because of face
2676 attribute changes of named faces, recompute them. When running
2677 in batch mode, the face cache of the initial frame is null. If
2678 we happen to get called, make a dummy face cache. */
2679 if (FRAME_FACE_CACHE (it->f) == NULL)
2680 init_frame_faces (it->f);
2681 if (FRAME_FACE_CACHE (it->f)->used == 0)
2682 recompute_basic_faces (it->f);
2683
2684 /* Current value of the `slice', `space-width', and 'height' properties. */
2685 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2686 it->space_width = Qnil;
2687 it->font_height = Qnil;
2688 it->override_ascent = -1;
2689
2690 /* Are control characters displayed as `^C'? */
2691 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2692
2693 /* -1 means everything between a CR and the following line end
2694 is invisible. >0 means lines indented more than this value are
2695 invisible. */
2696 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2697 ? (clip_to_bounds
2698 (-1, XINT (BVAR (current_buffer, selective_display)),
2699 PTRDIFF_MAX))
2700 : (!NILP (BVAR (current_buffer, selective_display))
2701 ? -1 : 0));
2702 it->selective_display_ellipsis_p
2703 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2704
2705 /* Display table to use. */
2706 it->dp = window_display_table (w);
2707
2708 /* Are multibyte characters enabled in current_buffer? */
2709 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2710
2711 /* If visible region is of non-zero length, set IT->region_beg_charpos
2712 and IT->region_end_charpos to the start and end of a visible region
2713 in window IT->w. Set both to -1 to indicate no region. */
2714 markpos = markpos_of_region ();
2715 if (0 <= markpos
2716 /* Maybe highlight only in selected window. */
2717 && (/* Either show region everywhere. */
2718 highlight_nonselected_windows
2719 /* Or show region in the selected window. */
2720 || w == XWINDOW (selected_window)
2721 /* Or show the region if we are in the mini-buffer and W is
2722 the window the mini-buffer refers to. */
2723 || (MINI_WINDOW_P (XWINDOW (selected_window))
2724 && WINDOWP (minibuf_selected_window)
2725 && w == XWINDOW (minibuf_selected_window))))
2726 {
2727 it->region_beg_charpos = min (PT, markpos);
2728 it->region_end_charpos = max (PT, markpos);
2729 }
2730 else
2731 it->region_beg_charpos = it->region_end_charpos = -1;
2732
2733 /* Get the position at which the redisplay_end_trigger hook should
2734 be run, if it is to be run at all. */
2735 if (MARKERP (w->redisplay_end_trigger)
2736 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2737 it->redisplay_end_trigger_charpos
2738 = marker_position (w->redisplay_end_trigger);
2739 else if (INTEGERP (w->redisplay_end_trigger))
2740 it->redisplay_end_trigger_charpos =
2741 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2742
2743 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2744
2745 /* Are lines in the display truncated? */
2746 if (base_face_id != DEFAULT_FACE_ID
2747 || it->w->hscroll
2748 || (! WINDOW_FULL_WIDTH_P (it->w)
2749 && ((!NILP (Vtruncate_partial_width_windows)
2750 && !INTEGERP (Vtruncate_partial_width_windows))
2751 || (INTEGERP (Vtruncate_partial_width_windows)
2752 && (WINDOW_TOTAL_COLS (it->w)
2753 < XINT (Vtruncate_partial_width_windows))))))
2754 it->line_wrap = TRUNCATE;
2755 else if (NILP (BVAR (current_buffer, truncate_lines)))
2756 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2757 ? WINDOW_WRAP : WORD_WRAP;
2758 else
2759 it->line_wrap = TRUNCATE;
2760
2761 /* Get dimensions of truncation and continuation glyphs. These are
2762 displayed as fringe bitmaps under X, but we need them for such
2763 frames when the fringes are turned off. But leave the dimensions
2764 zero for tooltip frames, as these glyphs look ugly there and also
2765 sabotage calculations of tooltip dimensions in x-show-tip. */
2766 #ifdef HAVE_WINDOW_SYSTEM
2767 if (!(FRAME_WINDOW_P (it->f)
2768 && FRAMEP (tip_frame)
2769 && it->f == XFRAME (tip_frame)))
2770 #endif
2771 {
2772 if (it->line_wrap == TRUNCATE)
2773 {
2774 /* We will need the truncation glyph. */
2775 eassert (it->glyph_row == NULL);
2776 produce_special_glyphs (it, IT_TRUNCATION);
2777 it->truncation_pixel_width = it->pixel_width;
2778 }
2779 else
2780 {
2781 /* We will need the continuation glyph. */
2782 eassert (it->glyph_row == NULL);
2783 produce_special_glyphs (it, IT_CONTINUATION);
2784 it->continuation_pixel_width = it->pixel_width;
2785 }
2786 }
2787
2788 /* Reset these values to zero because the produce_special_glyphs
2789 above has changed them. */
2790 it->pixel_width = it->ascent = it->descent = 0;
2791 it->phys_ascent = it->phys_descent = 0;
2792
2793 /* Set this after getting the dimensions of truncation and
2794 continuation glyphs, so that we don't produce glyphs when calling
2795 produce_special_glyphs, above. */
2796 it->glyph_row = row;
2797 it->area = TEXT_AREA;
2798
2799 /* Forget any previous info about this row being reversed. */
2800 if (it->glyph_row)
2801 it->glyph_row->reversed_p = 0;
2802
2803 /* Get the dimensions of the display area. The display area
2804 consists of the visible window area plus a horizontally scrolled
2805 part to the left of the window. All x-values are relative to the
2806 start of this total display area. */
2807 if (base_face_id != DEFAULT_FACE_ID)
2808 {
2809 /* Mode lines, menu bar in terminal frames. */
2810 it->first_visible_x = 0;
2811 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2812 }
2813 else
2814 {
2815 it->first_visible_x =
2816 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2817 it->last_visible_x = (it->first_visible_x
2818 + window_box_width (w, TEXT_AREA));
2819
2820 /* If we truncate lines, leave room for the truncation glyph(s) at
2821 the right margin. Otherwise, leave room for the continuation
2822 glyph(s). Done only if the window has no fringes. Since we
2823 don't know at this point whether there will be any R2L lines in
2824 the window, we reserve space for truncation/continuation glyphs
2825 even if only one of the fringes is absent. */
2826 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2827 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2828 {
2829 if (it->line_wrap == TRUNCATE)
2830 it->last_visible_x -= it->truncation_pixel_width;
2831 else
2832 it->last_visible_x -= it->continuation_pixel_width;
2833 }
2834
2835 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2836 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2837 }
2838
2839 /* Leave room for a border glyph. */
2840 if (!FRAME_WINDOW_P (it->f)
2841 && !WINDOW_RIGHTMOST_P (it->w))
2842 it->last_visible_x -= 1;
2843
2844 it->last_visible_y = window_text_bottom_y (w);
2845
2846 /* For mode lines and alike, arrange for the first glyph having a
2847 left box line if the face specifies a box. */
2848 if (base_face_id != DEFAULT_FACE_ID)
2849 {
2850 struct face *face;
2851
2852 it->face_id = remapped_base_face_id;
2853
2854 /* If we have a boxed mode line, make the first character appear
2855 with a left box line. */
2856 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2857 if (face->box != FACE_NO_BOX)
2858 it->start_of_box_run_p = 1;
2859 }
2860
2861 /* If a buffer position was specified, set the iterator there,
2862 getting overlays and face properties from that position. */
2863 if (charpos >= BUF_BEG (current_buffer))
2864 {
2865 it->end_charpos = ZV;
2866 IT_CHARPOS (*it) = charpos;
2867
2868 /* We will rely on `reseat' to set this up properly, via
2869 handle_face_prop. */
2870 it->face_id = it->base_face_id;
2871
2872 /* Compute byte position if not specified. */
2873 if (bytepos < charpos)
2874 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2875 else
2876 IT_BYTEPOS (*it) = bytepos;
2877
2878 it->start = it->current;
2879 /* Do we need to reorder bidirectional text? Not if this is a
2880 unibyte buffer: by definition, none of the single-byte
2881 characters are strong R2L, so no reordering is needed. And
2882 bidi.c doesn't support unibyte buffers anyway. Also, don't
2883 reorder while we are loading loadup.el, since the tables of
2884 character properties needed for reordering are not yet
2885 available. */
2886 it->bidi_p =
2887 NILP (Vpurify_flag)
2888 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2889 && it->multibyte_p;
2890
2891 /* If we are to reorder bidirectional text, init the bidi
2892 iterator. */
2893 if (it->bidi_p)
2894 {
2895 /* Note the paragraph direction that this buffer wants to
2896 use. */
2897 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2898 Qleft_to_right))
2899 it->paragraph_embedding = L2R;
2900 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2901 Qright_to_left))
2902 it->paragraph_embedding = R2L;
2903 else
2904 it->paragraph_embedding = NEUTRAL_DIR;
2905 bidi_unshelve_cache (NULL, 0);
2906 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2907 &it->bidi_it);
2908 }
2909
2910 /* Compute faces etc. */
2911 reseat (it, it->current.pos, 1);
2912 }
2913
2914 CHECK_IT (it);
2915 }
2916
2917
2918 /* Initialize IT for the display of window W with window start POS. */
2919
2920 void
2921 start_display (struct it *it, struct window *w, struct text_pos pos)
2922 {
2923 struct glyph_row *row;
2924 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2925
2926 row = w->desired_matrix->rows + first_vpos;
2927 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2928 it->first_vpos = first_vpos;
2929
2930 /* Don't reseat to previous visible line start if current start
2931 position is in a string or image. */
2932 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2933 {
2934 int start_at_line_beg_p;
2935 int first_y = it->current_y;
2936
2937 /* If window start is not at a line start, skip forward to POS to
2938 get the correct continuation lines width. */
2939 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2940 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2941 if (!start_at_line_beg_p)
2942 {
2943 int new_x;
2944
2945 reseat_at_previous_visible_line_start (it);
2946 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2947
2948 new_x = it->current_x + it->pixel_width;
2949
2950 /* If lines are continued, this line may end in the middle
2951 of a multi-glyph character (e.g. a control character
2952 displayed as \003, or in the middle of an overlay
2953 string). In this case move_it_to above will not have
2954 taken us to the start of the continuation line but to the
2955 end of the continued line. */
2956 if (it->current_x > 0
2957 && it->line_wrap != TRUNCATE /* Lines are continued. */
2958 && (/* And glyph doesn't fit on the line. */
2959 new_x > it->last_visible_x
2960 /* Or it fits exactly and we're on a window
2961 system frame. */
2962 || (new_x == it->last_visible_x
2963 && FRAME_WINDOW_P (it->f)
2964 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2965 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2966 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2967 {
2968 if ((it->current.dpvec_index >= 0
2969 || it->current.overlay_string_index >= 0)
2970 /* If we are on a newline from a display vector or
2971 overlay string, then we are already at the end of
2972 a screen line; no need to go to the next line in
2973 that case, as this line is not really continued.
2974 (If we do go to the next line, C-e will not DTRT.) */
2975 && it->c != '\n')
2976 {
2977 set_iterator_to_next (it, 1);
2978 move_it_in_display_line_to (it, -1, -1, 0);
2979 }
2980
2981 it->continuation_lines_width += it->current_x;
2982 }
2983 /* If the character at POS is displayed via a display
2984 vector, move_it_to above stops at the final glyph of
2985 IT->dpvec. To make the caller redisplay that character
2986 again (a.k.a. start at POS), we need to reset the
2987 dpvec_index to the beginning of IT->dpvec. */
2988 else if (it->current.dpvec_index >= 0)
2989 it->current.dpvec_index = 0;
2990
2991 /* We're starting a new display line, not affected by the
2992 height of the continued line, so clear the appropriate
2993 fields in the iterator structure. */
2994 it->max_ascent = it->max_descent = 0;
2995 it->max_phys_ascent = it->max_phys_descent = 0;
2996
2997 it->current_y = first_y;
2998 it->vpos = 0;
2999 it->current_x = it->hpos = 0;
3000 }
3001 }
3002 }
3003
3004
3005 /* Return 1 if POS is a position in ellipses displayed for invisible
3006 text. W is the window we display, for text property lookup. */
3007
3008 static int
3009 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3010 {
3011 Lisp_Object prop, window;
3012 int ellipses_p = 0;
3013 ptrdiff_t charpos = CHARPOS (pos->pos);
3014
3015 /* If POS specifies a position in a display vector, this might
3016 be for an ellipsis displayed for invisible text. We won't
3017 get the iterator set up for delivering that ellipsis unless
3018 we make sure that it gets aware of the invisible text. */
3019 if (pos->dpvec_index >= 0
3020 && pos->overlay_string_index < 0
3021 && CHARPOS (pos->string_pos) < 0
3022 && charpos > BEGV
3023 && (XSETWINDOW (window, w),
3024 prop = Fget_char_property (make_number (charpos),
3025 Qinvisible, window),
3026 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3027 {
3028 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3029 window);
3030 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3031 }
3032
3033 return ellipses_p;
3034 }
3035
3036
3037 /* Initialize IT for stepping through current_buffer in window W,
3038 starting at position POS that includes overlay string and display
3039 vector/ control character translation position information. Value
3040 is zero if there are overlay strings with newlines at POS. */
3041
3042 static int
3043 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3044 {
3045 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3046 int i, overlay_strings_with_newlines = 0;
3047
3048 /* If POS specifies a position in a display vector, this might
3049 be for an ellipsis displayed for invisible text. We won't
3050 get the iterator set up for delivering that ellipsis unless
3051 we make sure that it gets aware of the invisible text. */
3052 if (in_ellipses_for_invisible_text_p (pos, w))
3053 {
3054 --charpos;
3055 bytepos = 0;
3056 }
3057
3058 /* Keep in mind: the call to reseat in init_iterator skips invisible
3059 text, so we might end up at a position different from POS. This
3060 is only a problem when POS is a row start after a newline and an
3061 overlay starts there with an after-string, and the overlay has an
3062 invisible property. Since we don't skip invisible text in
3063 display_line and elsewhere immediately after consuming the
3064 newline before the row start, such a POS will not be in a string,
3065 but the call to init_iterator below will move us to the
3066 after-string. */
3067 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3068
3069 /* This only scans the current chunk -- it should scan all chunks.
3070 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3071 to 16 in 22.1 to make this a lesser problem. */
3072 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3073 {
3074 const char *s = SSDATA (it->overlay_strings[i]);
3075 const char *e = s + SBYTES (it->overlay_strings[i]);
3076
3077 while (s < e && *s != '\n')
3078 ++s;
3079
3080 if (s < e)
3081 {
3082 overlay_strings_with_newlines = 1;
3083 break;
3084 }
3085 }
3086
3087 /* If position is within an overlay string, set up IT to the right
3088 overlay string. */
3089 if (pos->overlay_string_index >= 0)
3090 {
3091 int relative_index;
3092
3093 /* If the first overlay string happens to have a `display'
3094 property for an image, the iterator will be set up for that
3095 image, and we have to undo that setup first before we can
3096 correct the overlay string index. */
3097 if (it->method == GET_FROM_IMAGE)
3098 pop_it (it);
3099
3100 /* We already have the first chunk of overlay strings in
3101 IT->overlay_strings. Load more until the one for
3102 pos->overlay_string_index is in IT->overlay_strings. */
3103 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3104 {
3105 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3106 it->current.overlay_string_index = 0;
3107 while (n--)
3108 {
3109 load_overlay_strings (it, 0);
3110 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3111 }
3112 }
3113
3114 it->current.overlay_string_index = pos->overlay_string_index;
3115 relative_index = (it->current.overlay_string_index
3116 % OVERLAY_STRING_CHUNK_SIZE);
3117 it->string = it->overlay_strings[relative_index];
3118 eassert (STRINGP (it->string));
3119 it->current.string_pos = pos->string_pos;
3120 it->method = GET_FROM_STRING;
3121 it->end_charpos = SCHARS (it->string);
3122 /* Set up the bidi iterator for this overlay string. */
3123 if (it->bidi_p)
3124 {
3125 it->bidi_it.string.lstring = it->string;
3126 it->bidi_it.string.s = NULL;
3127 it->bidi_it.string.schars = SCHARS (it->string);
3128 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3129 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3130 it->bidi_it.string.unibyte = !it->multibyte_p;
3131 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3132 FRAME_WINDOW_P (it->f), &it->bidi_it);
3133
3134 /* Synchronize the state of the bidi iterator with
3135 pos->string_pos. For any string position other than
3136 zero, this will be done automagically when we resume
3137 iteration over the string and get_visually_first_element
3138 is called. But if string_pos is zero, and the string is
3139 to be reordered for display, we need to resync manually,
3140 since it could be that the iteration state recorded in
3141 pos ended at string_pos of 0 moving backwards in string. */
3142 if (CHARPOS (pos->string_pos) == 0)
3143 {
3144 get_visually_first_element (it);
3145 if (IT_STRING_CHARPOS (*it) != 0)
3146 do {
3147 /* Paranoia. */
3148 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3149 bidi_move_to_visually_next (&it->bidi_it);
3150 } while (it->bidi_it.charpos != 0);
3151 }
3152 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3153 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3154 }
3155 }
3156
3157 if (CHARPOS (pos->string_pos) >= 0)
3158 {
3159 /* Recorded position is not in an overlay string, but in another
3160 string. This can only be a string from a `display' property.
3161 IT should already be filled with that string. */
3162 it->current.string_pos = pos->string_pos;
3163 eassert (STRINGP (it->string));
3164 if (it->bidi_p)
3165 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3166 FRAME_WINDOW_P (it->f), &it->bidi_it);
3167 }
3168
3169 /* Restore position in display vector translations, control
3170 character translations or ellipses. */
3171 if (pos->dpvec_index >= 0)
3172 {
3173 if (it->dpvec == NULL)
3174 get_next_display_element (it);
3175 eassert (it->dpvec && it->current.dpvec_index == 0);
3176 it->current.dpvec_index = pos->dpvec_index;
3177 }
3178
3179 CHECK_IT (it);
3180 return !overlay_strings_with_newlines;
3181 }
3182
3183
3184 /* Initialize IT for stepping through current_buffer in window W
3185 starting at ROW->start. */
3186
3187 static void
3188 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3189 {
3190 init_from_display_pos (it, w, &row->start);
3191 it->start = row->start;
3192 it->continuation_lines_width = row->continuation_lines_width;
3193 CHECK_IT (it);
3194 }
3195
3196
3197 /* Initialize IT for stepping through current_buffer in window W
3198 starting in the line following ROW, i.e. starting at ROW->end.
3199 Value is zero if there are overlay strings with newlines at ROW's
3200 end position. */
3201
3202 static int
3203 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3204 {
3205 int success = 0;
3206
3207 if (init_from_display_pos (it, w, &row->end))
3208 {
3209 if (row->continued_p)
3210 it->continuation_lines_width
3211 = row->continuation_lines_width + row->pixel_width;
3212 CHECK_IT (it);
3213 success = 1;
3214 }
3215
3216 return success;
3217 }
3218
3219
3220
3221 \f
3222 /***********************************************************************
3223 Text properties
3224 ***********************************************************************/
3225
3226 /* Called when IT reaches IT->stop_charpos. Handle text property and
3227 overlay changes. Set IT->stop_charpos to the next position where
3228 to stop. */
3229
3230 static void
3231 handle_stop (struct it *it)
3232 {
3233 enum prop_handled handled;
3234 int handle_overlay_change_p;
3235 struct props *p;
3236
3237 it->dpvec = NULL;
3238 it->current.dpvec_index = -1;
3239 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3240 it->ignore_overlay_strings_at_pos_p = 0;
3241 it->ellipsis_p = 0;
3242
3243 /* Use face of preceding text for ellipsis (if invisible) */
3244 if (it->selective_display_ellipsis_p)
3245 it->saved_face_id = it->face_id;
3246
3247 do
3248 {
3249 handled = HANDLED_NORMALLY;
3250
3251 /* Call text property handlers. */
3252 for (p = it_props; p->handler; ++p)
3253 {
3254 handled = p->handler (it);
3255
3256 if (handled == HANDLED_RECOMPUTE_PROPS)
3257 break;
3258 else if (handled == HANDLED_RETURN)
3259 {
3260 /* We still want to show before and after strings from
3261 overlays even if the actual buffer text is replaced. */
3262 if (!handle_overlay_change_p
3263 || it->sp > 1
3264 /* Don't call get_overlay_strings_1 if we already
3265 have overlay strings loaded, because doing so
3266 will load them again and push the iterator state
3267 onto the stack one more time, which is not
3268 expected by the rest of the code that processes
3269 overlay strings. */
3270 || (it->current.overlay_string_index < 0
3271 ? !get_overlay_strings_1 (it, 0, 0)
3272 : 0))
3273 {
3274 if (it->ellipsis_p)
3275 setup_for_ellipsis (it, 0);
3276 /* When handling a display spec, we might load an
3277 empty string. In that case, discard it here. We
3278 used to discard it in handle_single_display_spec,
3279 but that causes get_overlay_strings_1, above, to
3280 ignore overlay strings that we must check. */
3281 if (STRINGP (it->string) && !SCHARS (it->string))
3282 pop_it (it);
3283 return;
3284 }
3285 else if (STRINGP (it->string) && !SCHARS (it->string))
3286 pop_it (it);
3287 else
3288 {
3289 it->ignore_overlay_strings_at_pos_p = 1;
3290 it->string_from_display_prop_p = 0;
3291 it->from_disp_prop_p = 0;
3292 handle_overlay_change_p = 0;
3293 }
3294 handled = HANDLED_RECOMPUTE_PROPS;
3295 break;
3296 }
3297 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3298 handle_overlay_change_p = 0;
3299 }
3300
3301 if (handled != HANDLED_RECOMPUTE_PROPS)
3302 {
3303 /* Don't check for overlay strings below when set to deliver
3304 characters from a display vector. */
3305 if (it->method == GET_FROM_DISPLAY_VECTOR)
3306 handle_overlay_change_p = 0;
3307
3308 /* Handle overlay changes.
3309 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3310 if it finds overlays. */
3311 if (handle_overlay_change_p)
3312 handled = handle_overlay_change (it);
3313 }
3314
3315 if (it->ellipsis_p)
3316 {
3317 setup_for_ellipsis (it, 0);
3318 break;
3319 }
3320 }
3321 while (handled == HANDLED_RECOMPUTE_PROPS);
3322
3323 /* Determine where to stop next. */
3324 if (handled == HANDLED_NORMALLY)
3325 compute_stop_pos (it);
3326 }
3327
3328
3329 /* Compute IT->stop_charpos from text property and overlay change
3330 information for IT's current position. */
3331
3332 static void
3333 compute_stop_pos (struct it *it)
3334 {
3335 register INTERVAL iv, next_iv;
3336 Lisp_Object object, limit, position;
3337 ptrdiff_t charpos, bytepos;
3338
3339 if (STRINGP (it->string))
3340 {
3341 /* Strings are usually short, so don't limit the search for
3342 properties. */
3343 it->stop_charpos = it->end_charpos;
3344 object = it->string;
3345 limit = Qnil;
3346 charpos = IT_STRING_CHARPOS (*it);
3347 bytepos = IT_STRING_BYTEPOS (*it);
3348 }
3349 else
3350 {
3351 ptrdiff_t pos;
3352
3353 /* If end_charpos is out of range for some reason, such as a
3354 misbehaving display function, rationalize it (Bug#5984). */
3355 if (it->end_charpos > ZV)
3356 it->end_charpos = ZV;
3357 it->stop_charpos = it->end_charpos;
3358
3359 /* If next overlay change is in front of the current stop pos
3360 (which is IT->end_charpos), stop there. Note: value of
3361 next_overlay_change is point-max if no overlay change
3362 follows. */
3363 charpos = IT_CHARPOS (*it);
3364 bytepos = IT_BYTEPOS (*it);
3365 pos = next_overlay_change (charpos);
3366 if (pos < it->stop_charpos)
3367 it->stop_charpos = pos;
3368
3369 /* If showing the region, we have to stop at the region
3370 start or end because the face might change there. */
3371 if (it->region_beg_charpos > 0)
3372 {
3373 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3374 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3375 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3376 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3377 }
3378
3379 /* Set up variables for computing the stop position from text
3380 property changes. */
3381 XSETBUFFER (object, current_buffer);
3382 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3383 }
3384
3385 /* Get the interval containing IT's position. Value is a null
3386 interval if there isn't such an interval. */
3387 position = make_number (charpos);
3388 iv = validate_interval_range (object, &position, &position, 0);
3389 if (iv)
3390 {
3391 Lisp_Object values_here[LAST_PROP_IDX];
3392 struct props *p;
3393
3394 /* Get properties here. */
3395 for (p = it_props; p->handler; ++p)
3396 values_here[p->idx] = textget (iv->plist, *p->name);
3397
3398 /* Look for an interval following iv that has different
3399 properties. */
3400 for (next_iv = next_interval (iv);
3401 (next_iv
3402 && (NILP (limit)
3403 || XFASTINT (limit) > next_iv->position));
3404 next_iv = next_interval (next_iv))
3405 {
3406 for (p = it_props; p->handler; ++p)
3407 {
3408 Lisp_Object new_value;
3409
3410 new_value = textget (next_iv->plist, *p->name);
3411 if (!EQ (values_here[p->idx], new_value))
3412 break;
3413 }
3414
3415 if (p->handler)
3416 break;
3417 }
3418
3419 if (next_iv)
3420 {
3421 if (INTEGERP (limit)
3422 && next_iv->position >= XFASTINT (limit))
3423 /* No text property change up to limit. */
3424 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3425 else
3426 /* Text properties change in next_iv. */
3427 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3428 }
3429 }
3430
3431 if (it->cmp_it.id < 0)
3432 {
3433 ptrdiff_t stoppos = it->end_charpos;
3434
3435 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3436 stoppos = -1;
3437 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3438 stoppos, it->string);
3439 }
3440
3441 eassert (STRINGP (it->string)
3442 || (it->stop_charpos >= BEGV
3443 && it->stop_charpos >= IT_CHARPOS (*it)));
3444 }
3445
3446
3447 /* Return the position of the next overlay change after POS in
3448 current_buffer. Value is point-max if no overlay change
3449 follows. This is like `next-overlay-change' but doesn't use
3450 xmalloc. */
3451
3452 static ptrdiff_t
3453 next_overlay_change (ptrdiff_t pos)
3454 {
3455 ptrdiff_t i, noverlays;
3456 ptrdiff_t endpos;
3457 Lisp_Object *overlays;
3458
3459 /* Get all overlays at the given position. */
3460 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3461
3462 /* If any of these overlays ends before endpos,
3463 use its ending point instead. */
3464 for (i = 0; i < noverlays; ++i)
3465 {
3466 Lisp_Object oend;
3467 ptrdiff_t oendpos;
3468
3469 oend = OVERLAY_END (overlays[i]);
3470 oendpos = OVERLAY_POSITION (oend);
3471 endpos = min (endpos, oendpos);
3472 }
3473
3474 return endpos;
3475 }
3476
3477 /* How many characters forward to search for a display property or
3478 display string. Searching too far forward makes the bidi display
3479 sluggish, especially in small windows. */
3480 #define MAX_DISP_SCAN 250
3481
3482 /* Return the character position of a display string at or after
3483 position specified by POSITION. If no display string exists at or
3484 after POSITION, return ZV. A display string is either an overlay
3485 with `display' property whose value is a string, or a `display'
3486 text property whose value is a string. STRING is data about the
3487 string to iterate; if STRING->lstring is nil, we are iterating a
3488 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3489 on a GUI frame. DISP_PROP is set to zero if we searched
3490 MAX_DISP_SCAN characters forward without finding any display
3491 strings, non-zero otherwise. It is set to 2 if the display string
3492 uses any kind of `(space ...)' spec that will produce a stretch of
3493 white space in the text area. */
3494 ptrdiff_t
3495 compute_display_string_pos (struct text_pos *position,
3496 struct bidi_string_data *string,
3497 int frame_window_p, int *disp_prop)
3498 {
3499 /* OBJECT = nil means current buffer. */
3500 Lisp_Object object =
3501 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3502 Lisp_Object pos, spec, limpos;
3503 int string_p = (string && (STRINGP (string->lstring) || string->s));
3504 ptrdiff_t eob = string_p ? string->schars : ZV;
3505 ptrdiff_t begb = string_p ? 0 : BEGV;
3506 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3507 ptrdiff_t lim =
3508 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3509 struct text_pos tpos;
3510 int rv = 0;
3511
3512 *disp_prop = 1;
3513
3514 if (charpos >= eob
3515 /* We don't support display properties whose values are strings
3516 that have display string properties. */
3517 || string->from_disp_str
3518 /* C strings cannot have display properties. */
3519 || (string->s && !STRINGP (object)))
3520 {
3521 *disp_prop = 0;
3522 return eob;
3523 }
3524
3525 /* If the character at CHARPOS is where the display string begins,
3526 return CHARPOS. */
3527 pos = make_number (charpos);
3528 if (STRINGP (object))
3529 bufpos = string->bufpos;
3530 else
3531 bufpos = charpos;
3532 tpos = *position;
3533 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3534 && (charpos <= begb
3535 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3536 object),
3537 spec))
3538 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3539 frame_window_p)))
3540 {
3541 if (rv == 2)
3542 *disp_prop = 2;
3543 return charpos;
3544 }
3545
3546 /* Look forward for the first character with a `display' property
3547 that will replace the underlying text when displayed. */
3548 limpos = make_number (lim);
3549 do {
3550 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3551 CHARPOS (tpos) = XFASTINT (pos);
3552 if (CHARPOS (tpos) >= lim)
3553 {
3554 *disp_prop = 0;
3555 break;
3556 }
3557 if (STRINGP (object))
3558 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3559 else
3560 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3561 spec = Fget_char_property (pos, Qdisplay, object);
3562 if (!STRINGP (object))
3563 bufpos = CHARPOS (tpos);
3564 } while (NILP (spec)
3565 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3566 bufpos, frame_window_p)));
3567 if (rv == 2)
3568 *disp_prop = 2;
3569
3570 return CHARPOS (tpos);
3571 }
3572
3573 /* Return the character position of the end of the display string that
3574 started at CHARPOS. If there's no display string at CHARPOS,
3575 return -1. A display string is either an overlay with `display'
3576 property whose value is a string or a `display' text property whose
3577 value is a string. */
3578 ptrdiff_t
3579 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3580 {
3581 /* OBJECT = nil means current buffer. */
3582 Lisp_Object object =
3583 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3584 Lisp_Object pos = make_number (charpos);
3585 ptrdiff_t eob =
3586 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3587
3588 if (charpos >= eob || (string->s && !STRINGP (object)))
3589 return eob;
3590
3591 /* It could happen that the display property or overlay was removed
3592 since we found it in compute_display_string_pos above. One way
3593 this can happen is if JIT font-lock was called (through
3594 handle_fontified_prop), and jit-lock-functions remove text
3595 properties or overlays from the portion of buffer that includes
3596 CHARPOS. Muse mode is known to do that, for example. In this
3597 case, we return -1 to the caller, to signal that no display
3598 string is actually present at CHARPOS. See bidi_fetch_char for
3599 how this is handled.
3600
3601 An alternative would be to never look for display properties past
3602 it->stop_charpos. But neither compute_display_string_pos nor
3603 bidi_fetch_char that calls it know or care where the next
3604 stop_charpos is. */
3605 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3606 return -1;
3607
3608 /* Look forward for the first character where the `display' property
3609 changes. */
3610 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3611
3612 return XFASTINT (pos);
3613 }
3614
3615
3616 \f
3617 /***********************************************************************
3618 Fontification
3619 ***********************************************************************/
3620
3621 /* Handle changes in the `fontified' property of the current buffer by
3622 calling hook functions from Qfontification_functions to fontify
3623 regions of text. */
3624
3625 static enum prop_handled
3626 handle_fontified_prop (struct it *it)
3627 {
3628 Lisp_Object prop, pos;
3629 enum prop_handled handled = HANDLED_NORMALLY;
3630
3631 if (!NILP (Vmemory_full))
3632 return handled;
3633
3634 /* Get the value of the `fontified' property at IT's current buffer
3635 position. (The `fontified' property doesn't have a special
3636 meaning in strings.) If the value is nil, call functions from
3637 Qfontification_functions. */
3638 if (!STRINGP (it->string)
3639 && it->s == NULL
3640 && !NILP (Vfontification_functions)
3641 && !NILP (Vrun_hooks)
3642 && (pos = make_number (IT_CHARPOS (*it)),
3643 prop = Fget_char_property (pos, Qfontified, Qnil),
3644 /* Ignore the special cased nil value always present at EOB since
3645 no amount of fontifying will be able to change it. */
3646 NILP (prop) && IT_CHARPOS (*it) < Z))
3647 {
3648 ptrdiff_t count = SPECPDL_INDEX ();
3649 Lisp_Object val;
3650 struct buffer *obuf = current_buffer;
3651 int begv = BEGV, zv = ZV;
3652 int old_clip_changed = current_buffer->clip_changed;
3653
3654 val = Vfontification_functions;
3655 specbind (Qfontification_functions, Qnil);
3656
3657 eassert (it->end_charpos == ZV);
3658
3659 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3660 safe_call1 (val, pos);
3661 else
3662 {
3663 Lisp_Object fns, fn;
3664 struct gcpro gcpro1, gcpro2;
3665
3666 fns = Qnil;
3667 GCPRO2 (val, fns);
3668
3669 for (; CONSP (val); val = XCDR (val))
3670 {
3671 fn = XCAR (val);
3672
3673 if (EQ (fn, Qt))
3674 {
3675 /* A value of t indicates this hook has a local
3676 binding; it means to run the global binding too.
3677 In a global value, t should not occur. If it
3678 does, we must ignore it to avoid an endless
3679 loop. */
3680 for (fns = Fdefault_value (Qfontification_functions);
3681 CONSP (fns);
3682 fns = XCDR (fns))
3683 {
3684 fn = XCAR (fns);
3685 if (!EQ (fn, Qt))
3686 safe_call1 (fn, pos);
3687 }
3688 }
3689 else
3690 safe_call1 (fn, pos);
3691 }
3692
3693 UNGCPRO;
3694 }
3695
3696 unbind_to (count, Qnil);
3697
3698 /* Fontification functions routinely call `save-restriction'.
3699 Normally, this tags clip_changed, which can confuse redisplay
3700 (see discussion in Bug#6671). Since we don't perform any
3701 special handling of fontification changes in the case where
3702 `save-restriction' isn't called, there's no point doing so in
3703 this case either. So, if the buffer's restrictions are
3704 actually left unchanged, reset clip_changed. */
3705 if (obuf == current_buffer)
3706 {
3707 if (begv == BEGV && zv == ZV)
3708 current_buffer->clip_changed = old_clip_changed;
3709 }
3710 /* There isn't much we can reasonably do to protect against
3711 misbehaving fontification, but here's a fig leaf. */
3712 else if (BUFFER_LIVE_P (obuf))
3713 set_buffer_internal_1 (obuf);
3714
3715 /* The fontification code may have added/removed text.
3716 It could do even a lot worse, but let's at least protect against
3717 the most obvious case where only the text past `pos' gets changed',
3718 as is/was done in grep.el where some escapes sequences are turned
3719 into face properties (bug#7876). */
3720 it->end_charpos = ZV;
3721
3722 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3723 something. This avoids an endless loop if they failed to
3724 fontify the text for which reason ever. */
3725 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3726 handled = HANDLED_RECOMPUTE_PROPS;
3727 }
3728
3729 return handled;
3730 }
3731
3732
3733 \f
3734 /***********************************************************************
3735 Faces
3736 ***********************************************************************/
3737
3738 /* Set up iterator IT from face properties at its current position.
3739 Called from handle_stop. */
3740
3741 static enum prop_handled
3742 handle_face_prop (struct it *it)
3743 {
3744 int new_face_id;
3745 ptrdiff_t next_stop;
3746
3747 if (!STRINGP (it->string))
3748 {
3749 new_face_id
3750 = face_at_buffer_position (it->w,
3751 IT_CHARPOS (*it),
3752 it->region_beg_charpos,
3753 it->region_end_charpos,
3754 &next_stop,
3755 (IT_CHARPOS (*it)
3756 + TEXT_PROP_DISTANCE_LIMIT),
3757 0, it->base_face_id);
3758
3759 /* Is this a start of a run of characters with box face?
3760 Caveat: this can be called for a freshly initialized
3761 iterator; face_id is -1 in this case. We know that the new
3762 face will not change until limit, i.e. if the new face has a
3763 box, all characters up to limit will have one. But, as
3764 usual, we don't know whether limit is really the end. */
3765 if (new_face_id != it->face_id)
3766 {
3767 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3768 /* If it->face_id is -1, old_face below will be NULL, see
3769 the definition of FACE_FROM_ID. This will happen if this
3770 is the initial call that gets the face. */
3771 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3772
3773 /* If the value of face_id of the iterator is -1, we have to
3774 look in front of IT's position and see whether there is a
3775 face there that's different from new_face_id. */
3776 if (!old_face && IT_CHARPOS (*it) > BEG)
3777 {
3778 int prev_face_id = face_before_it_pos (it);
3779
3780 old_face = FACE_FROM_ID (it->f, prev_face_id);
3781 }
3782
3783 /* If the new face has a box, but the old face does not,
3784 this is the start of a run of characters with box face,
3785 i.e. this character has a shadow on the left side. */
3786 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3787 && (old_face == NULL || !old_face->box));
3788 it->face_box_p = new_face->box != FACE_NO_BOX;
3789 }
3790 }
3791 else
3792 {
3793 int base_face_id;
3794 ptrdiff_t bufpos;
3795 int i;
3796 Lisp_Object from_overlay
3797 = (it->current.overlay_string_index >= 0
3798 ? it->string_overlays[it->current.overlay_string_index
3799 % OVERLAY_STRING_CHUNK_SIZE]
3800 : Qnil);
3801
3802 /* See if we got to this string directly or indirectly from
3803 an overlay property. That includes the before-string or
3804 after-string of an overlay, strings in display properties
3805 provided by an overlay, their text properties, etc.
3806
3807 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3808 if (! NILP (from_overlay))
3809 for (i = it->sp - 1; i >= 0; i--)
3810 {
3811 if (it->stack[i].current.overlay_string_index >= 0)
3812 from_overlay
3813 = it->string_overlays[it->stack[i].current.overlay_string_index
3814 % OVERLAY_STRING_CHUNK_SIZE];
3815 else if (! NILP (it->stack[i].from_overlay))
3816 from_overlay = it->stack[i].from_overlay;
3817
3818 if (!NILP (from_overlay))
3819 break;
3820 }
3821
3822 if (! NILP (from_overlay))
3823 {
3824 bufpos = IT_CHARPOS (*it);
3825 /* For a string from an overlay, the base face depends
3826 only on text properties and ignores overlays. */
3827 base_face_id
3828 = face_for_overlay_string (it->w,
3829 IT_CHARPOS (*it),
3830 it->region_beg_charpos,
3831 it->region_end_charpos,
3832 &next_stop,
3833 (IT_CHARPOS (*it)
3834 + TEXT_PROP_DISTANCE_LIMIT),
3835 0,
3836 from_overlay);
3837 }
3838 else
3839 {
3840 bufpos = 0;
3841
3842 /* For strings from a `display' property, use the face at
3843 IT's current buffer position as the base face to merge
3844 with, so that overlay strings appear in the same face as
3845 surrounding text, unless they specify their own
3846 faces. */
3847 base_face_id = it->string_from_prefix_prop_p
3848 ? DEFAULT_FACE_ID
3849 : underlying_face_id (it);
3850 }
3851
3852 new_face_id = face_at_string_position (it->w,
3853 it->string,
3854 IT_STRING_CHARPOS (*it),
3855 bufpos,
3856 it->region_beg_charpos,
3857 it->region_end_charpos,
3858 &next_stop,
3859 base_face_id, 0);
3860
3861 /* Is this a start of a run of characters with box? Caveat:
3862 this can be called for a freshly allocated iterator; face_id
3863 is -1 is this case. We know that the new face will not
3864 change until the next check pos, i.e. if the new face has a
3865 box, all characters up to that position will have a
3866 box. But, as usual, we don't know whether that position
3867 is really the end. */
3868 if (new_face_id != it->face_id)
3869 {
3870 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3871 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3872
3873 /* If new face has a box but old face hasn't, this is the
3874 start of a run of characters with box, i.e. it has a
3875 shadow on the left side. */
3876 it->start_of_box_run_p
3877 = new_face->box && (old_face == NULL || !old_face->box);
3878 it->face_box_p = new_face->box != FACE_NO_BOX;
3879 }
3880 }
3881
3882 it->face_id = new_face_id;
3883 return HANDLED_NORMALLY;
3884 }
3885
3886
3887 /* Return the ID of the face ``underlying'' IT's current position,
3888 which is in a string. If the iterator is associated with a
3889 buffer, return the face at IT's current buffer position.
3890 Otherwise, use the iterator's base_face_id. */
3891
3892 static int
3893 underlying_face_id (struct it *it)
3894 {
3895 int face_id = it->base_face_id, i;
3896
3897 eassert (STRINGP (it->string));
3898
3899 for (i = it->sp - 1; i >= 0; --i)
3900 if (NILP (it->stack[i].string))
3901 face_id = it->stack[i].face_id;
3902
3903 return face_id;
3904 }
3905
3906
3907 /* Compute the face one character before or after the current position
3908 of IT, in the visual order. BEFORE_P non-zero means get the face
3909 in front (to the left in L2R paragraphs, to the right in R2L
3910 paragraphs) of IT's screen position. Value is the ID of the face. */
3911
3912 static int
3913 face_before_or_after_it_pos (struct it *it, int before_p)
3914 {
3915 int face_id, limit;
3916 ptrdiff_t next_check_charpos;
3917 struct it it_copy;
3918 void *it_copy_data = NULL;
3919
3920 eassert (it->s == NULL);
3921
3922 if (STRINGP (it->string))
3923 {
3924 ptrdiff_t bufpos, charpos;
3925 int base_face_id;
3926
3927 /* No face change past the end of the string (for the case
3928 we are padding with spaces). No face change before the
3929 string start. */
3930 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3931 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3932 return it->face_id;
3933
3934 if (!it->bidi_p)
3935 {
3936 /* Set charpos to the position before or after IT's current
3937 position, in the logical order, which in the non-bidi
3938 case is the same as the visual order. */
3939 if (before_p)
3940 charpos = IT_STRING_CHARPOS (*it) - 1;
3941 else if (it->what == IT_COMPOSITION)
3942 /* For composition, we must check the character after the
3943 composition. */
3944 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3945 else
3946 charpos = IT_STRING_CHARPOS (*it) + 1;
3947 }
3948 else
3949 {
3950 if (before_p)
3951 {
3952 /* With bidi iteration, the character before the current
3953 in the visual order cannot be found by simple
3954 iteration, because "reverse" reordering is not
3955 supported. Instead, we need to use the move_it_*
3956 family of functions. */
3957 /* Ignore face changes before the first visible
3958 character on this display line. */
3959 if (it->current_x <= it->first_visible_x)
3960 return it->face_id;
3961 SAVE_IT (it_copy, *it, it_copy_data);
3962 /* Implementation note: Since move_it_in_display_line
3963 works in the iterator geometry, and thinks the first
3964 character is always the leftmost, even in R2L lines,
3965 we don't need to distinguish between the R2L and L2R
3966 cases here. */
3967 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3968 it_copy.current_x - 1, MOVE_TO_X);
3969 charpos = IT_STRING_CHARPOS (it_copy);
3970 RESTORE_IT (it, it, it_copy_data);
3971 }
3972 else
3973 {
3974 /* Set charpos to the string position of the character
3975 that comes after IT's current position in the visual
3976 order. */
3977 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3978
3979 it_copy = *it;
3980 while (n--)
3981 bidi_move_to_visually_next (&it_copy.bidi_it);
3982
3983 charpos = it_copy.bidi_it.charpos;
3984 }
3985 }
3986 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3987
3988 if (it->current.overlay_string_index >= 0)
3989 bufpos = IT_CHARPOS (*it);
3990 else
3991 bufpos = 0;
3992
3993 base_face_id = underlying_face_id (it);
3994
3995 /* Get the face for ASCII, or unibyte. */
3996 face_id = face_at_string_position (it->w,
3997 it->string,
3998 charpos,
3999 bufpos,
4000 it->region_beg_charpos,
4001 it->region_end_charpos,
4002 &next_check_charpos,
4003 base_face_id, 0);
4004
4005 /* Correct the face for charsets different from ASCII. Do it
4006 for the multibyte case only. The face returned above is
4007 suitable for unibyte text if IT->string is unibyte. */
4008 if (STRING_MULTIBYTE (it->string))
4009 {
4010 struct text_pos pos1 = string_pos (charpos, it->string);
4011 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4012 int c, len;
4013 struct face *face = FACE_FROM_ID (it->f, face_id);
4014
4015 c = string_char_and_length (p, &len);
4016 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4017 }
4018 }
4019 else
4020 {
4021 struct text_pos pos;
4022
4023 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4024 || (IT_CHARPOS (*it) <= BEGV && before_p))
4025 return it->face_id;
4026
4027 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4028 pos = it->current.pos;
4029
4030 if (!it->bidi_p)
4031 {
4032 if (before_p)
4033 DEC_TEXT_POS (pos, it->multibyte_p);
4034 else
4035 {
4036 if (it->what == IT_COMPOSITION)
4037 {
4038 /* For composition, we must check the position after
4039 the composition. */
4040 pos.charpos += it->cmp_it.nchars;
4041 pos.bytepos += it->len;
4042 }
4043 else
4044 INC_TEXT_POS (pos, it->multibyte_p);
4045 }
4046 }
4047 else
4048 {
4049 if (before_p)
4050 {
4051 /* With bidi iteration, the character before the current
4052 in the visual order cannot be found by simple
4053 iteration, because "reverse" reordering is not
4054 supported. Instead, we need to use the move_it_*
4055 family of functions. */
4056 /* Ignore face changes before the first visible
4057 character on this display line. */
4058 if (it->current_x <= it->first_visible_x)
4059 return it->face_id;
4060 SAVE_IT (it_copy, *it, it_copy_data);
4061 /* Implementation note: Since move_it_in_display_line
4062 works in the iterator geometry, and thinks the first
4063 character is always the leftmost, even in R2L lines,
4064 we don't need to distinguish between the R2L and L2R
4065 cases here. */
4066 move_it_in_display_line (&it_copy, ZV,
4067 it_copy.current_x - 1, MOVE_TO_X);
4068 pos = it_copy.current.pos;
4069 RESTORE_IT (it, it, it_copy_data);
4070 }
4071 else
4072 {
4073 /* Set charpos to the buffer position of the character
4074 that comes after IT's current position in the visual
4075 order. */
4076 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4077
4078 it_copy = *it;
4079 while (n--)
4080 bidi_move_to_visually_next (&it_copy.bidi_it);
4081
4082 SET_TEXT_POS (pos,
4083 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4084 }
4085 }
4086 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4087
4088 /* Determine face for CHARSET_ASCII, or unibyte. */
4089 face_id = face_at_buffer_position (it->w,
4090 CHARPOS (pos),
4091 it->region_beg_charpos,
4092 it->region_end_charpos,
4093 &next_check_charpos,
4094 limit, 0, -1);
4095
4096 /* Correct the face for charsets different from ASCII. Do it
4097 for the multibyte case only. The face returned above is
4098 suitable for unibyte text if current_buffer is unibyte. */
4099 if (it->multibyte_p)
4100 {
4101 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4102 struct face *face = FACE_FROM_ID (it->f, face_id);
4103 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4104 }
4105 }
4106
4107 return face_id;
4108 }
4109
4110
4111 \f
4112 /***********************************************************************
4113 Invisible text
4114 ***********************************************************************/
4115
4116 /* Set up iterator IT from invisible properties at its current
4117 position. Called from handle_stop. */
4118
4119 static enum prop_handled
4120 handle_invisible_prop (struct it *it)
4121 {
4122 enum prop_handled handled = HANDLED_NORMALLY;
4123 int invis_p;
4124 Lisp_Object prop;
4125
4126 if (STRINGP (it->string))
4127 {
4128 Lisp_Object end_charpos, limit, charpos;
4129
4130 /* Get the value of the invisible text property at the
4131 current position. Value will be nil if there is no such
4132 property. */
4133 charpos = make_number (IT_STRING_CHARPOS (*it));
4134 prop = Fget_text_property (charpos, Qinvisible, it->string);
4135 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136
4137 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4138 {
4139 /* Record whether we have to display an ellipsis for the
4140 invisible text. */
4141 int display_ellipsis_p = (invis_p == 2);
4142 ptrdiff_t len, endpos;
4143
4144 handled = HANDLED_RECOMPUTE_PROPS;
4145
4146 /* Get the position at which the next visible text can be
4147 found in IT->string, if any. */
4148 endpos = len = SCHARS (it->string);
4149 XSETINT (limit, len);
4150 do
4151 {
4152 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4153 it->string, limit);
4154 if (INTEGERP (end_charpos))
4155 {
4156 endpos = XFASTINT (end_charpos);
4157 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4158 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4159 if (invis_p == 2)
4160 display_ellipsis_p = 1;
4161 }
4162 }
4163 while (invis_p && endpos < len);
4164
4165 if (display_ellipsis_p)
4166 it->ellipsis_p = 1;
4167
4168 if (endpos < len)
4169 {
4170 /* Text at END_CHARPOS is visible. Move IT there. */
4171 struct text_pos old;
4172 ptrdiff_t oldpos;
4173
4174 old = it->current.string_pos;
4175 oldpos = CHARPOS (old);
4176 if (it->bidi_p)
4177 {
4178 if (it->bidi_it.first_elt
4179 && it->bidi_it.charpos < SCHARS (it->string))
4180 bidi_paragraph_init (it->paragraph_embedding,
4181 &it->bidi_it, 1);
4182 /* Bidi-iterate out of the invisible text. */
4183 do
4184 {
4185 bidi_move_to_visually_next (&it->bidi_it);
4186 }
4187 while (oldpos <= it->bidi_it.charpos
4188 && it->bidi_it.charpos < endpos);
4189
4190 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4191 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4192 if (IT_CHARPOS (*it) >= endpos)
4193 it->prev_stop = endpos;
4194 }
4195 else
4196 {
4197 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4198 compute_string_pos (&it->current.string_pos, old, it->string);
4199 }
4200 }
4201 else
4202 {
4203 /* The rest of the string is invisible. If this is an
4204 overlay string, proceed with the next overlay string
4205 or whatever comes and return a character from there. */
4206 if (it->current.overlay_string_index >= 0
4207 && !display_ellipsis_p)
4208 {
4209 next_overlay_string (it);
4210 /* Don't check for overlay strings when we just
4211 finished processing them. */
4212 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4213 }
4214 else
4215 {
4216 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4217 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4218 }
4219 }
4220 }
4221 }
4222 else
4223 {
4224 ptrdiff_t newpos, next_stop, start_charpos, tem;
4225 Lisp_Object pos, overlay;
4226
4227 /* First of all, is there invisible text at this position? */
4228 tem = start_charpos = IT_CHARPOS (*it);
4229 pos = make_number (tem);
4230 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4231 &overlay);
4232 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4233
4234 /* If we are on invisible text, skip over it. */
4235 if (invis_p && start_charpos < it->end_charpos)
4236 {
4237 /* Record whether we have to display an ellipsis for the
4238 invisible text. */
4239 int display_ellipsis_p = invis_p == 2;
4240
4241 handled = HANDLED_RECOMPUTE_PROPS;
4242
4243 /* Loop skipping over invisible text. The loop is left at
4244 ZV or with IT on the first char being visible again. */
4245 do
4246 {
4247 /* Try to skip some invisible text. Return value is the
4248 position reached which can be equal to where we start
4249 if there is nothing invisible there. This skips both
4250 over invisible text properties and overlays with
4251 invisible property. */
4252 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4253
4254 /* If we skipped nothing at all we weren't at invisible
4255 text in the first place. If everything to the end of
4256 the buffer was skipped, end the loop. */
4257 if (newpos == tem || newpos >= ZV)
4258 invis_p = 0;
4259 else
4260 {
4261 /* We skipped some characters but not necessarily
4262 all there are. Check if we ended up on visible
4263 text. Fget_char_property returns the property of
4264 the char before the given position, i.e. if we
4265 get invis_p = 0, this means that the char at
4266 newpos is visible. */
4267 pos = make_number (newpos);
4268 prop = Fget_char_property (pos, Qinvisible, it->window);
4269 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4270 }
4271
4272 /* If we ended up on invisible text, proceed to
4273 skip starting with next_stop. */
4274 if (invis_p)
4275 tem = next_stop;
4276
4277 /* If there are adjacent invisible texts, don't lose the
4278 second one's ellipsis. */
4279 if (invis_p == 2)
4280 display_ellipsis_p = 1;
4281 }
4282 while (invis_p);
4283
4284 /* The position newpos is now either ZV or on visible text. */
4285 if (it->bidi_p)
4286 {
4287 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4288 int on_newline =
4289 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4290 int after_newline =
4291 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4292
4293 /* If the invisible text ends on a newline or on a
4294 character after a newline, we can avoid the costly,
4295 character by character, bidi iteration to NEWPOS, and
4296 instead simply reseat the iterator there. That's
4297 because all bidi reordering information is tossed at
4298 the newline. This is a big win for modes that hide
4299 complete lines, like Outline, Org, etc. */
4300 if (on_newline || after_newline)
4301 {
4302 struct text_pos tpos;
4303 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4304
4305 SET_TEXT_POS (tpos, newpos, bpos);
4306 reseat_1 (it, tpos, 0);
4307 /* If we reseat on a newline/ZV, we need to prep the
4308 bidi iterator for advancing to the next character
4309 after the newline/EOB, keeping the current paragraph
4310 direction (so that PRODUCE_GLYPHS does TRT wrt
4311 prepending/appending glyphs to a glyph row). */
4312 if (on_newline)
4313 {
4314 it->bidi_it.first_elt = 0;
4315 it->bidi_it.paragraph_dir = pdir;
4316 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4317 it->bidi_it.nchars = 1;
4318 it->bidi_it.ch_len = 1;
4319 }
4320 }
4321 else /* Must use the slow method. */
4322 {
4323 /* With bidi iteration, the region of invisible text
4324 could start and/or end in the middle of a
4325 non-base embedding level. Therefore, we need to
4326 skip invisible text using the bidi iterator,
4327 starting at IT's current position, until we find
4328 ourselves outside of the invisible text.
4329 Skipping invisible text _after_ bidi iteration
4330 avoids affecting the visual order of the
4331 displayed text when invisible properties are
4332 added or removed. */
4333 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4334 {
4335 /* If we were `reseat'ed to a new paragraph,
4336 determine the paragraph base direction. We
4337 need to do it now because
4338 next_element_from_buffer may not have a
4339 chance to do it, if we are going to skip any
4340 text at the beginning, which resets the
4341 FIRST_ELT flag. */
4342 bidi_paragraph_init (it->paragraph_embedding,
4343 &it->bidi_it, 1);
4344 }
4345 do
4346 {
4347 bidi_move_to_visually_next (&it->bidi_it);
4348 }
4349 while (it->stop_charpos <= it->bidi_it.charpos
4350 && it->bidi_it.charpos < newpos);
4351 IT_CHARPOS (*it) = it->bidi_it.charpos;
4352 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4353 /* If we overstepped NEWPOS, record its position in
4354 the iterator, so that we skip invisible text if
4355 later the bidi iteration lands us in the
4356 invisible region again. */
4357 if (IT_CHARPOS (*it) >= newpos)
4358 it->prev_stop = newpos;
4359 }
4360 }
4361 else
4362 {
4363 IT_CHARPOS (*it) = newpos;
4364 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4365 }
4366
4367 /* If there are before-strings at the start of invisible
4368 text, and the text is invisible because of a text
4369 property, arrange to show before-strings because 20.x did
4370 it that way. (If the text is invisible because of an
4371 overlay property instead of a text property, this is
4372 already handled in the overlay code.) */
4373 if (NILP (overlay)
4374 && get_overlay_strings (it, it->stop_charpos))
4375 {
4376 handled = HANDLED_RECOMPUTE_PROPS;
4377 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4378 }
4379 else if (display_ellipsis_p)
4380 {
4381 /* Make sure that the glyphs of the ellipsis will get
4382 correct `charpos' values. If we would not update
4383 it->position here, the glyphs would belong to the
4384 last visible character _before_ the invisible
4385 text, which confuses `set_cursor_from_row'.
4386
4387 We use the last invisible position instead of the
4388 first because this way the cursor is always drawn on
4389 the first "." of the ellipsis, whenever PT is inside
4390 the invisible text. Otherwise the cursor would be
4391 placed _after_ the ellipsis when the point is after the
4392 first invisible character. */
4393 if (!STRINGP (it->object))
4394 {
4395 it->position.charpos = newpos - 1;
4396 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4397 }
4398 it->ellipsis_p = 1;
4399 /* Let the ellipsis display before
4400 considering any properties of the following char.
4401 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4402 handled = HANDLED_RETURN;
4403 }
4404 }
4405 }
4406
4407 return handled;
4408 }
4409
4410
4411 /* Make iterator IT return `...' next.
4412 Replaces LEN characters from buffer. */
4413
4414 static void
4415 setup_for_ellipsis (struct it *it, int len)
4416 {
4417 /* Use the display table definition for `...'. Invalid glyphs
4418 will be handled by the method returning elements from dpvec. */
4419 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4420 {
4421 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4422 it->dpvec = v->contents;
4423 it->dpend = v->contents + v->header.size;
4424 }
4425 else
4426 {
4427 /* Default `...'. */
4428 it->dpvec = default_invis_vector;
4429 it->dpend = default_invis_vector + 3;
4430 }
4431
4432 it->dpvec_char_len = len;
4433 it->current.dpvec_index = 0;
4434 it->dpvec_face_id = -1;
4435
4436 /* Remember the current face id in case glyphs specify faces.
4437 IT's face is restored in set_iterator_to_next.
4438 saved_face_id was set to preceding char's face in handle_stop. */
4439 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4440 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4441
4442 it->method = GET_FROM_DISPLAY_VECTOR;
4443 it->ellipsis_p = 1;
4444 }
4445
4446
4447 \f
4448 /***********************************************************************
4449 'display' property
4450 ***********************************************************************/
4451
4452 /* Set up iterator IT from `display' property at its current position.
4453 Called from handle_stop.
4454 We return HANDLED_RETURN if some part of the display property
4455 overrides the display of the buffer text itself.
4456 Otherwise we return HANDLED_NORMALLY. */
4457
4458 static enum prop_handled
4459 handle_display_prop (struct it *it)
4460 {
4461 Lisp_Object propval, object, overlay;
4462 struct text_pos *position;
4463 ptrdiff_t bufpos;
4464 /* Nonzero if some property replaces the display of the text itself. */
4465 int display_replaced_p = 0;
4466
4467 if (STRINGP (it->string))
4468 {
4469 object = it->string;
4470 position = &it->current.string_pos;
4471 bufpos = CHARPOS (it->current.pos);
4472 }
4473 else
4474 {
4475 XSETWINDOW (object, it->w);
4476 position = &it->current.pos;
4477 bufpos = CHARPOS (*position);
4478 }
4479
4480 /* Reset those iterator values set from display property values. */
4481 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4482 it->space_width = Qnil;
4483 it->font_height = Qnil;
4484 it->voffset = 0;
4485
4486 /* We don't support recursive `display' properties, i.e. string
4487 values that have a string `display' property, that have a string
4488 `display' property etc. */
4489 if (!it->string_from_display_prop_p)
4490 it->area = TEXT_AREA;
4491
4492 propval = get_char_property_and_overlay (make_number (position->charpos),
4493 Qdisplay, object, &overlay);
4494 if (NILP (propval))
4495 return HANDLED_NORMALLY;
4496 /* Now OVERLAY is the overlay that gave us this property, or nil
4497 if it was a text property. */
4498
4499 if (!STRINGP (it->string))
4500 object = it->w->buffer;
4501
4502 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4503 position, bufpos,
4504 FRAME_WINDOW_P (it->f));
4505
4506 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4507 }
4508
4509 /* Subroutine of handle_display_prop. Returns non-zero if the display
4510 specification in SPEC is a replacing specification, i.e. it would
4511 replace the text covered by `display' property with something else,
4512 such as an image or a display string. If SPEC includes any kind or
4513 `(space ...) specification, the value is 2; this is used by
4514 compute_display_string_pos, which see.
4515
4516 See handle_single_display_spec for documentation of arguments.
4517 frame_window_p is non-zero if the window being redisplayed is on a
4518 GUI frame; this argument is used only if IT is NULL, see below.
4519
4520 IT can be NULL, if this is called by the bidi reordering code
4521 through compute_display_string_pos, which see. In that case, this
4522 function only examines SPEC, but does not otherwise "handle" it, in
4523 the sense that it doesn't set up members of IT from the display
4524 spec. */
4525 static int
4526 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4527 Lisp_Object overlay, struct text_pos *position,
4528 ptrdiff_t bufpos, int frame_window_p)
4529 {
4530 int replacing_p = 0;
4531 int rv;
4532
4533 if (CONSP (spec)
4534 /* Simple specifications. */
4535 && !EQ (XCAR (spec), Qimage)
4536 && !EQ (XCAR (spec), Qspace)
4537 && !EQ (XCAR (spec), Qwhen)
4538 && !EQ (XCAR (spec), Qslice)
4539 && !EQ (XCAR (spec), Qspace_width)
4540 && !EQ (XCAR (spec), Qheight)
4541 && !EQ (XCAR (spec), Qraise)
4542 /* Marginal area specifications. */
4543 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4544 && !EQ (XCAR (spec), Qleft_fringe)
4545 && !EQ (XCAR (spec), Qright_fringe)
4546 && !NILP (XCAR (spec)))
4547 {
4548 for (; CONSP (spec); spec = XCDR (spec))
4549 {
4550 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4551 overlay, position, bufpos,
4552 replacing_p, frame_window_p)))
4553 {
4554 replacing_p = rv;
4555 /* If some text in a string is replaced, `position' no
4556 longer points to the position of `object'. */
4557 if (!it || STRINGP (object))
4558 break;
4559 }
4560 }
4561 }
4562 else if (VECTORP (spec))
4563 {
4564 ptrdiff_t i;
4565 for (i = 0; i < ASIZE (spec); ++i)
4566 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4567 overlay, position, bufpos,
4568 replacing_p, frame_window_p)))
4569 {
4570 replacing_p = rv;
4571 /* If some text in a string is replaced, `position' no
4572 longer points to the position of `object'. */
4573 if (!it || STRINGP (object))
4574 break;
4575 }
4576 }
4577 else
4578 {
4579 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4580 position, bufpos, 0,
4581 frame_window_p)))
4582 replacing_p = rv;
4583 }
4584
4585 return replacing_p;
4586 }
4587
4588 /* Value is the position of the end of the `display' property starting
4589 at START_POS in OBJECT. */
4590
4591 static struct text_pos
4592 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4593 {
4594 Lisp_Object end;
4595 struct text_pos end_pos;
4596
4597 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4598 Qdisplay, object, Qnil);
4599 CHARPOS (end_pos) = XFASTINT (end);
4600 if (STRINGP (object))
4601 compute_string_pos (&end_pos, start_pos, it->string);
4602 else
4603 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4604
4605 return end_pos;
4606 }
4607
4608
4609 /* Set up IT from a single `display' property specification SPEC. OBJECT
4610 is the object in which the `display' property was found. *POSITION
4611 is the position in OBJECT at which the `display' property was found.
4612 BUFPOS is the buffer position of OBJECT (different from POSITION if
4613 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4614 previously saw a display specification which already replaced text
4615 display with something else, for example an image; we ignore such
4616 properties after the first one has been processed.
4617
4618 OVERLAY is the overlay this `display' property came from,
4619 or nil if it was a text property.
4620
4621 If SPEC is a `space' or `image' specification, and in some other
4622 cases too, set *POSITION to the position where the `display'
4623 property ends.
4624
4625 If IT is NULL, only examine the property specification in SPEC, but
4626 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4627 is intended to be displayed in a window on a GUI frame.
4628
4629 Value is non-zero if something was found which replaces the display
4630 of buffer or string text. */
4631
4632 static int
4633 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4634 Lisp_Object overlay, struct text_pos *position,
4635 ptrdiff_t bufpos, int display_replaced_p,
4636 int frame_window_p)
4637 {
4638 Lisp_Object form;
4639 Lisp_Object location, value;
4640 struct text_pos start_pos = *position;
4641 int valid_p;
4642
4643 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4644 If the result is non-nil, use VALUE instead of SPEC. */
4645 form = Qt;
4646 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4647 {
4648 spec = XCDR (spec);
4649 if (!CONSP (spec))
4650 return 0;
4651 form = XCAR (spec);
4652 spec = XCDR (spec);
4653 }
4654
4655 if (!NILP (form) && !EQ (form, Qt))
4656 {
4657 ptrdiff_t count = SPECPDL_INDEX ();
4658 struct gcpro gcpro1;
4659
4660 /* Bind `object' to the object having the `display' property, a
4661 buffer or string. Bind `position' to the position in the
4662 object where the property was found, and `buffer-position'
4663 to the current position in the buffer. */
4664
4665 if (NILP (object))
4666 XSETBUFFER (object, current_buffer);
4667 specbind (Qobject, object);
4668 specbind (Qposition, make_number (CHARPOS (*position)));
4669 specbind (Qbuffer_position, make_number (bufpos));
4670 GCPRO1 (form);
4671 form = safe_eval (form);
4672 UNGCPRO;
4673 unbind_to (count, Qnil);
4674 }
4675
4676 if (NILP (form))
4677 return 0;
4678
4679 /* Handle `(height HEIGHT)' specifications. */
4680 if (CONSP (spec)
4681 && EQ (XCAR (spec), Qheight)
4682 && CONSP (XCDR (spec)))
4683 {
4684 if (it)
4685 {
4686 if (!FRAME_WINDOW_P (it->f))
4687 return 0;
4688
4689 it->font_height = XCAR (XCDR (spec));
4690 if (!NILP (it->font_height))
4691 {
4692 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4693 int new_height = -1;
4694
4695 if (CONSP (it->font_height)
4696 && (EQ (XCAR (it->font_height), Qplus)
4697 || EQ (XCAR (it->font_height), Qminus))
4698 && CONSP (XCDR (it->font_height))
4699 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4700 {
4701 /* `(+ N)' or `(- N)' where N is an integer. */
4702 int steps = XINT (XCAR (XCDR (it->font_height)));
4703 if (EQ (XCAR (it->font_height), Qplus))
4704 steps = - steps;
4705 it->face_id = smaller_face (it->f, it->face_id, steps);
4706 }
4707 else if (FUNCTIONP (it->font_height))
4708 {
4709 /* Call function with current height as argument.
4710 Value is the new height. */
4711 Lisp_Object height;
4712 height = safe_call1 (it->font_height,
4713 face->lface[LFACE_HEIGHT_INDEX]);
4714 if (NUMBERP (height))
4715 new_height = XFLOATINT (height);
4716 }
4717 else if (NUMBERP (it->font_height))
4718 {
4719 /* Value is a multiple of the canonical char height. */
4720 struct face *f;
4721
4722 f = FACE_FROM_ID (it->f,
4723 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4724 new_height = (XFLOATINT (it->font_height)
4725 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4726 }
4727 else
4728 {
4729 /* Evaluate IT->font_height with `height' bound to the
4730 current specified height to get the new height. */
4731 ptrdiff_t count = SPECPDL_INDEX ();
4732
4733 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4734 value = safe_eval (it->font_height);
4735 unbind_to (count, Qnil);
4736
4737 if (NUMBERP (value))
4738 new_height = XFLOATINT (value);
4739 }
4740
4741 if (new_height > 0)
4742 it->face_id = face_with_height (it->f, it->face_id, new_height);
4743 }
4744 }
4745
4746 return 0;
4747 }
4748
4749 /* Handle `(space-width WIDTH)'. */
4750 if (CONSP (spec)
4751 && EQ (XCAR (spec), Qspace_width)
4752 && CONSP (XCDR (spec)))
4753 {
4754 if (it)
4755 {
4756 if (!FRAME_WINDOW_P (it->f))
4757 return 0;
4758
4759 value = XCAR (XCDR (spec));
4760 if (NUMBERP (value) && XFLOATINT (value) > 0)
4761 it->space_width = value;
4762 }
4763
4764 return 0;
4765 }
4766
4767 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4768 if (CONSP (spec)
4769 && EQ (XCAR (spec), Qslice))
4770 {
4771 Lisp_Object tem;
4772
4773 if (it)
4774 {
4775 if (!FRAME_WINDOW_P (it->f))
4776 return 0;
4777
4778 if (tem = XCDR (spec), CONSP (tem))
4779 {
4780 it->slice.x = XCAR (tem);
4781 if (tem = XCDR (tem), CONSP (tem))
4782 {
4783 it->slice.y = XCAR (tem);
4784 if (tem = XCDR (tem), CONSP (tem))
4785 {
4786 it->slice.width = XCAR (tem);
4787 if (tem = XCDR (tem), CONSP (tem))
4788 it->slice.height = XCAR (tem);
4789 }
4790 }
4791 }
4792 }
4793
4794 return 0;
4795 }
4796
4797 /* Handle `(raise FACTOR)'. */
4798 if (CONSP (spec)
4799 && EQ (XCAR (spec), Qraise)
4800 && CONSP (XCDR (spec)))
4801 {
4802 if (it)
4803 {
4804 if (!FRAME_WINDOW_P (it->f))
4805 return 0;
4806
4807 #ifdef HAVE_WINDOW_SYSTEM
4808 value = XCAR (XCDR (spec));
4809 if (NUMBERP (value))
4810 {
4811 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4812 it->voffset = - (XFLOATINT (value)
4813 * (FONT_HEIGHT (face->font)));
4814 }
4815 #endif /* HAVE_WINDOW_SYSTEM */
4816 }
4817
4818 return 0;
4819 }
4820
4821 /* Don't handle the other kinds of display specifications
4822 inside a string that we got from a `display' property. */
4823 if (it && it->string_from_display_prop_p)
4824 return 0;
4825
4826 /* Characters having this form of property are not displayed, so
4827 we have to find the end of the property. */
4828 if (it)
4829 {
4830 start_pos = *position;
4831 *position = display_prop_end (it, object, start_pos);
4832 }
4833 value = Qnil;
4834
4835 /* Stop the scan at that end position--we assume that all
4836 text properties change there. */
4837 if (it)
4838 it->stop_charpos = position->charpos;
4839
4840 /* Handle `(left-fringe BITMAP [FACE])'
4841 and `(right-fringe BITMAP [FACE])'. */
4842 if (CONSP (spec)
4843 && (EQ (XCAR (spec), Qleft_fringe)
4844 || EQ (XCAR (spec), Qright_fringe))
4845 && CONSP (XCDR (spec)))
4846 {
4847 int fringe_bitmap;
4848
4849 if (it)
4850 {
4851 if (!FRAME_WINDOW_P (it->f))
4852 /* If we return here, POSITION has been advanced
4853 across the text with this property. */
4854 {
4855 /* Synchronize the bidi iterator with POSITION. This is
4856 needed because we are not going to push the iterator
4857 on behalf of this display property, so there will be
4858 no pop_it call to do this synchronization for us. */
4859 if (it->bidi_p)
4860 {
4861 it->position = *position;
4862 iterate_out_of_display_property (it);
4863 *position = it->position;
4864 }
4865 return 1;
4866 }
4867 }
4868 else if (!frame_window_p)
4869 return 1;
4870
4871 #ifdef HAVE_WINDOW_SYSTEM
4872 value = XCAR (XCDR (spec));
4873 if (!SYMBOLP (value)
4874 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4875 /* If we return here, POSITION has been advanced
4876 across the text with this property. */
4877 {
4878 if (it && it->bidi_p)
4879 {
4880 it->position = *position;
4881 iterate_out_of_display_property (it);
4882 *position = it->position;
4883 }
4884 return 1;
4885 }
4886
4887 if (it)
4888 {
4889 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4890
4891 if (CONSP (XCDR (XCDR (spec))))
4892 {
4893 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4894 int face_id2 = lookup_derived_face (it->f, face_name,
4895 FRINGE_FACE_ID, 0);
4896 if (face_id2 >= 0)
4897 face_id = face_id2;
4898 }
4899
4900 /* Save current settings of IT so that we can restore them
4901 when we are finished with the glyph property value. */
4902 push_it (it, position);
4903
4904 it->area = TEXT_AREA;
4905 it->what = IT_IMAGE;
4906 it->image_id = -1; /* no image */
4907 it->position = start_pos;
4908 it->object = NILP (object) ? it->w->buffer : object;
4909 it->method = GET_FROM_IMAGE;
4910 it->from_overlay = Qnil;
4911 it->face_id = face_id;
4912 it->from_disp_prop_p = 1;
4913
4914 /* Say that we haven't consumed the characters with
4915 `display' property yet. The call to pop_it in
4916 set_iterator_to_next will clean this up. */
4917 *position = start_pos;
4918
4919 if (EQ (XCAR (spec), Qleft_fringe))
4920 {
4921 it->left_user_fringe_bitmap = fringe_bitmap;
4922 it->left_user_fringe_face_id = face_id;
4923 }
4924 else
4925 {
4926 it->right_user_fringe_bitmap = fringe_bitmap;
4927 it->right_user_fringe_face_id = face_id;
4928 }
4929 }
4930 #endif /* HAVE_WINDOW_SYSTEM */
4931 return 1;
4932 }
4933
4934 /* Prepare to handle `((margin left-margin) ...)',
4935 `((margin right-margin) ...)' and `((margin nil) ...)'
4936 prefixes for display specifications. */
4937 location = Qunbound;
4938 if (CONSP (spec) && CONSP (XCAR (spec)))
4939 {
4940 Lisp_Object tem;
4941
4942 value = XCDR (spec);
4943 if (CONSP (value))
4944 value = XCAR (value);
4945
4946 tem = XCAR (spec);
4947 if (EQ (XCAR (tem), Qmargin)
4948 && (tem = XCDR (tem),
4949 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4950 (NILP (tem)
4951 || EQ (tem, Qleft_margin)
4952 || EQ (tem, Qright_margin))))
4953 location = tem;
4954 }
4955
4956 if (EQ (location, Qunbound))
4957 {
4958 location = Qnil;
4959 value = spec;
4960 }
4961
4962 /* After this point, VALUE is the property after any
4963 margin prefix has been stripped. It must be a string,
4964 an image specification, or `(space ...)'.
4965
4966 LOCATION specifies where to display: `left-margin',
4967 `right-margin' or nil. */
4968
4969 valid_p = (STRINGP (value)
4970 #ifdef HAVE_WINDOW_SYSTEM
4971 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4972 && valid_image_p (value))
4973 #endif /* not HAVE_WINDOW_SYSTEM */
4974 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4975
4976 if (valid_p && !display_replaced_p)
4977 {
4978 int retval = 1;
4979
4980 if (!it)
4981 {
4982 /* Callers need to know whether the display spec is any kind
4983 of `(space ...)' spec that is about to affect text-area
4984 display. */
4985 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4986 retval = 2;
4987 return retval;
4988 }
4989
4990 /* Save current settings of IT so that we can restore them
4991 when we are finished with the glyph property value. */
4992 push_it (it, position);
4993 it->from_overlay = overlay;
4994 it->from_disp_prop_p = 1;
4995
4996 if (NILP (location))
4997 it->area = TEXT_AREA;
4998 else if (EQ (location, Qleft_margin))
4999 it->area = LEFT_MARGIN_AREA;
5000 else
5001 it->area = RIGHT_MARGIN_AREA;
5002
5003 if (STRINGP (value))
5004 {
5005 it->string = value;
5006 it->multibyte_p = STRING_MULTIBYTE (it->string);
5007 it->current.overlay_string_index = -1;
5008 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5009 it->end_charpos = it->string_nchars = SCHARS (it->string);
5010 it->method = GET_FROM_STRING;
5011 it->stop_charpos = 0;
5012 it->prev_stop = 0;
5013 it->base_level_stop = 0;
5014 it->string_from_display_prop_p = 1;
5015 /* Say that we haven't consumed the characters with
5016 `display' property yet. The call to pop_it in
5017 set_iterator_to_next will clean this up. */
5018 if (BUFFERP (object))
5019 *position = start_pos;
5020
5021 /* Force paragraph direction to be that of the parent
5022 object. If the parent object's paragraph direction is
5023 not yet determined, default to L2R. */
5024 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5025 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5026 else
5027 it->paragraph_embedding = L2R;
5028
5029 /* Set up the bidi iterator for this display string. */
5030 if (it->bidi_p)
5031 {
5032 it->bidi_it.string.lstring = it->string;
5033 it->bidi_it.string.s = NULL;
5034 it->bidi_it.string.schars = it->end_charpos;
5035 it->bidi_it.string.bufpos = bufpos;
5036 it->bidi_it.string.from_disp_str = 1;
5037 it->bidi_it.string.unibyte = !it->multibyte_p;
5038 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5039 }
5040 }
5041 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5042 {
5043 it->method = GET_FROM_STRETCH;
5044 it->object = value;
5045 *position = it->position = start_pos;
5046 retval = 1 + (it->area == TEXT_AREA);
5047 }
5048 #ifdef HAVE_WINDOW_SYSTEM
5049 else
5050 {
5051 it->what = IT_IMAGE;
5052 it->image_id = lookup_image (it->f, value);
5053 it->position = start_pos;
5054 it->object = NILP (object) ? it->w->buffer : object;
5055 it->method = GET_FROM_IMAGE;
5056
5057 /* Say that we haven't consumed the characters with
5058 `display' property yet. The call to pop_it in
5059 set_iterator_to_next will clean this up. */
5060 *position = start_pos;
5061 }
5062 #endif /* HAVE_WINDOW_SYSTEM */
5063
5064 return retval;
5065 }
5066
5067 /* Invalid property or property not supported. Restore
5068 POSITION to what it was before. */
5069 *position = start_pos;
5070 return 0;
5071 }
5072
5073 /* Check if PROP is a display property value whose text should be
5074 treated as intangible. OVERLAY is the overlay from which PROP
5075 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5076 specify the buffer position covered by PROP. */
5077
5078 int
5079 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5080 ptrdiff_t charpos, ptrdiff_t bytepos)
5081 {
5082 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5083 struct text_pos position;
5084
5085 SET_TEXT_POS (position, charpos, bytepos);
5086 return handle_display_spec (NULL, prop, Qnil, overlay,
5087 &position, charpos, frame_window_p);
5088 }
5089
5090
5091 /* Return 1 if PROP is a display sub-property value containing STRING.
5092
5093 Implementation note: this and the following function are really
5094 special cases of handle_display_spec and
5095 handle_single_display_spec, and should ideally use the same code.
5096 Until they do, these two pairs must be consistent and must be
5097 modified in sync. */
5098
5099 static int
5100 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5101 {
5102 if (EQ (string, prop))
5103 return 1;
5104
5105 /* Skip over `when FORM'. */
5106 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5107 {
5108 prop = XCDR (prop);
5109 if (!CONSP (prop))
5110 return 0;
5111 /* Actually, the condition following `when' should be eval'ed,
5112 like handle_single_display_spec does, and we should return
5113 zero if it evaluates to nil. However, this function is
5114 called only when the buffer was already displayed and some
5115 glyph in the glyph matrix was found to come from a display
5116 string. Therefore, the condition was already evaluated, and
5117 the result was non-nil, otherwise the display string wouldn't
5118 have been displayed and we would have never been called for
5119 this property. Thus, we can skip the evaluation and assume
5120 its result is non-nil. */
5121 prop = XCDR (prop);
5122 }
5123
5124 if (CONSP (prop))
5125 /* Skip over `margin LOCATION'. */
5126 if (EQ (XCAR (prop), Qmargin))
5127 {
5128 prop = XCDR (prop);
5129 if (!CONSP (prop))
5130 return 0;
5131
5132 prop = XCDR (prop);
5133 if (!CONSP (prop))
5134 return 0;
5135 }
5136
5137 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5138 }
5139
5140
5141 /* Return 1 if STRING appears in the `display' property PROP. */
5142
5143 static int
5144 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5145 {
5146 if (CONSP (prop)
5147 && !EQ (XCAR (prop), Qwhen)
5148 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5149 {
5150 /* A list of sub-properties. */
5151 while (CONSP (prop))
5152 {
5153 if (single_display_spec_string_p (XCAR (prop), string))
5154 return 1;
5155 prop = XCDR (prop);
5156 }
5157 }
5158 else if (VECTORP (prop))
5159 {
5160 /* A vector of sub-properties. */
5161 ptrdiff_t i;
5162 for (i = 0; i < ASIZE (prop); ++i)
5163 if (single_display_spec_string_p (AREF (prop, i), string))
5164 return 1;
5165 }
5166 else
5167 return single_display_spec_string_p (prop, string);
5168
5169 return 0;
5170 }
5171
5172 /* Look for STRING in overlays and text properties in the current
5173 buffer, between character positions FROM and TO (excluding TO).
5174 BACK_P non-zero means look back (in this case, TO is supposed to be
5175 less than FROM).
5176 Value is the first character position where STRING was found, or
5177 zero if it wasn't found before hitting TO.
5178
5179 This function may only use code that doesn't eval because it is
5180 called asynchronously from note_mouse_highlight. */
5181
5182 static ptrdiff_t
5183 string_buffer_position_lim (Lisp_Object string,
5184 ptrdiff_t from, ptrdiff_t to, int back_p)
5185 {
5186 Lisp_Object limit, prop, pos;
5187 int found = 0;
5188
5189 pos = make_number (max (from, BEGV));
5190
5191 if (!back_p) /* looking forward */
5192 {
5193 limit = make_number (min (to, ZV));
5194 while (!found && !EQ (pos, limit))
5195 {
5196 prop = Fget_char_property (pos, Qdisplay, Qnil);
5197 if (!NILP (prop) && display_prop_string_p (prop, string))
5198 found = 1;
5199 else
5200 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5201 limit);
5202 }
5203 }
5204 else /* looking back */
5205 {
5206 limit = make_number (max (to, BEGV));
5207 while (!found && !EQ (pos, limit))
5208 {
5209 prop = Fget_char_property (pos, Qdisplay, Qnil);
5210 if (!NILP (prop) && display_prop_string_p (prop, string))
5211 found = 1;
5212 else
5213 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5214 limit);
5215 }
5216 }
5217
5218 return found ? XINT (pos) : 0;
5219 }
5220
5221 /* Determine which buffer position in current buffer STRING comes from.
5222 AROUND_CHARPOS is an approximate position where it could come from.
5223 Value is the buffer position or 0 if it couldn't be determined.
5224
5225 This function is necessary because we don't record buffer positions
5226 in glyphs generated from strings (to keep struct glyph small).
5227 This function may only use code that doesn't eval because it is
5228 called asynchronously from note_mouse_highlight. */
5229
5230 static ptrdiff_t
5231 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5232 {
5233 const int MAX_DISTANCE = 1000;
5234 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5235 around_charpos + MAX_DISTANCE,
5236 0);
5237
5238 if (!found)
5239 found = string_buffer_position_lim (string, around_charpos,
5240 around_charpos - MAX_DISTANCE, 1);
5241 return found;
5242 }
5243
5244
5245 \f
5246 /***********************************************************************
5247 `composition' property
5248 ***********************************************************************/
5249
5250 /* Set up iterator IT from `composition' property at its current
5251 position. Called from handle_stop. */
5252
5253 static enum prop_handled
5254 handle_composition_prop (struct it *it)
5255 {
5256 Lisp_Object prop, string;
5257 ptrdiff_t pos, pos_byte, start, end;
5258
5259 if (STRINGP (it->string))
5260 {
5261 unsigned char *s;
5262
5263 pos = IT_STRING_CHARPOS (*it);
5264 pos_byte = IT_STRING_BYTEPOS (*it);
5265 string = it->string;
5266 s = SDATA (string) + pos_byte;
5267 it->c = STRING_CHAR (s);
5268 }
5269 else
5270 {
5271 pos = IT_CHARPOS (*it);
5272 pos_byte = IT_BYTEPOS (*it);
5273 string = Qnil;
5274 it->c = FETCH_CHAR (pos_byte);
5275 }
5276
5277 /* If there's a valid composition and point is not inside of the
5278 composition (in the case that the composition is from the current
5279 buffer), draw a glyph composed from the composition components. */
5280 if (find_composition (pos, -1, &start, &end, &prop, string)
5281 && COMPOSITION_VALID_P (start, end, prop)
5282 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5283 {
5284 if (start < pos)
5285 /* As we can't handle this situation (perhaps font-lock added
5286 a new composition), we just return here hoping that next
5287 redisplay will detect this composition much earlier. */
5288 return HANDLED_NORMALLY;
5289 if (start != pos)
5290 {
5291 if (STRINGP (it->string))
5292 pos_byte = string_char_to_byte (it->string, start);
5293 else
5294 pos_byte = CHAR_TO_BYTE (start);
5295 }
5296 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5297 prop, string);
5298
5299 if (it->cmp_it.id >= 0)
5300 {
5301 it->cmp_it.ch = -1;
5302 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5303 it->cmp_it.nglyphs = -1;
5304 }
5305 }
5306
5307 return HANDLED_NORMALLY;
5308 }
5309
5310
5311 \f
5312 /***********************************************************************
5313 Overlay strings
5314 ***********************************************************************/
5315
5316 /* The following structure is used to record overlay strings for
5317 later sorting in load_overlay_strings. */
5318
5319 struct overlay_entry
5320 {
5321 Lisp_Object overlay;
5322 Lisp_Object string;
5323 EMACS_INT priority;
5324 int after_string_p;
5325 };
5326
5327
5328 /* Set up iterator IT from overlay strings at its current position.
5329 Called from handle_stop. */
5330
5331 static enum prop_handled
5332 handle_overlay_change (struct it *it)
5333 {
5334 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5335 return HANDLED_RECOMPUTE_PROPS;
5336 else
5337 return HANDLED_NORMALLY;
5338 }
5339
5340
5341 /* Set up the next overlay string for delivery by IT, if there is an
5342 overlay string to deliver. Called by set_iterator_to_next when the
5343 end of the current overlay string is reached. If there are more
5344 overlay strings to display, IT->string and
5345 IT->current.overlay_string_index are set appropriately here.
5346 Otherwise IT->string is set to nil. */
5347
5348 static void
5349 next_overlay_string (struct it *it)
5350 {
5351 ++it->current.overlay_string_index;
5352 if (it->current.overlay_string_index == it->n_overlay_strings)
5353 {
5354 /* No more overlay strings. Restore IT's settings to what
5355 they were before overlay strings were processed, and
5356 continue to deliver from current_buffer. */
5357
5358 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5359 pop_it (it);
5360 eassert (it->sp > 0
5361 || (NILP (it->string)
5362 && it->method == GET_FROM_BUFFER
5363 && it->stop_charpos >= BEGV
5364 && it->stop_charpos <= it->end_charpos));
5365 it->current.overlay_string_index = -1;
5366 it->n_overlay_strings = 0;
5367 it->overlay_strings_charpos = -1;
5368 /* If there's an empty display string on the stack, pop the
5369 stack, to resync the bidi iterator with IT's position. Such
5370 empty strings are pushed onto the stack in
5371 get_overlay_strings_1. */
5372 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5373 pop_it (it);
5374
5375 /* If we're at the end of the buffer, record that we have
5376 processed the overlay strings there already, so that
5377 next_element_from_buffer doesn't try it again. */
5378 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5379 it->overlay_strings_at_end_processed_p = 1;
5380 }
5381 else
5382 {
5383 /* There are more overlay strings to process. If
5384 IT->current.overlay_string_index has advanced to a position
5385 where we must load IT->overlay_strings with more strings, do
5386 it. We must load at the IT->overlay_strings_charpos where
5387 IT->n_overlay_strings was originally computed; when invisible
5388 text is present, this might not be IT_CHARPOS (Bug#7016). */
5389 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5390
5391 if (it->current.overlay_string_index && i == 0)
5392 load_overlay_strings (it, it->overlay_strings_charpos);
5393
5394 /* Initialize IT to deliver display elements from the overlay
5395 string. */
5396 it->string = it->overlay_strings[i];
5397 it->multibyte_p = STRING_MULTIBYTE (it->string);
5398 SET_TEXT_POS (it->current.string_pos, 0, 0);
5399 it->method = GET_FROM_STRING;
5400 it->stop_charpos = 0;
5401 it->end_charpos = SCHARS (it->string);
5402 if (it->cmp_it.stop_pos >= 0)
5403 it->cmp_it.stop_pos = 0;
5404 it->prev_stop = 0;
5405 it->base_level_stop = 0;
5406
5407 /* Set up the bidi iterator for this overlay string. */
5408 if (it->bidi_p)
5409 {
5410 it->bidi_it.string.lstring = it->string;
5411 it->bidi_it.string.s = NULL;
5412 it->bidi_it.string.schars = SCHARS (it->string);
5413 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5414 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5415 it->bidi_it.string.unibyte = !it->multibyte_p;
5416 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5417 }
5418 }
5419
5420 CHECK_IT (it);
5421 }
5422
5423
5424 /* Compare two overlay_entry structures E1 and E2. Used as a
5425 comparison function for qsort in load_overlay_strings. Overlay
5426 strings for the same position are sorted so that
5427
5428 1. All after-strings come in front of before-strings, except
5429 when they come from the same overlay.
5430
5431 2. Within after-strings, strings are sorted so that overlay strings
5432 from overlays with higher priorities come first.
5433
5434 2. Within before-strings, strings are sorted so that overlay
5435 strings from overlays with higher priorities come last.
5436
5437 Value is analogous to strcmp. */
5438
5439
5440 static int
5441 compare_overlay_entries (const void *e1, const void *e2)
5442 {
5443 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5444 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5445 int result;
5446
5447 if (entry1->after_string_p != entry2->after_string_p)
5448 {
5449 /* Let after-strings appear in front of before-strings if
5450 they come from different overlays. */
5451 if (EQ (entry1->overlay, entry2->overlay))
5452 result = entry1->after_string_p ? 1 : -1;
5453 else
5454 result = entry1->after_string_p ? -1 : 1;
5455 }
5456 else if (entry1->priority != entry2->priority)
5457 {
5458 if (entry1->after_string_p)
5459 /* After-strings sorted in order of decreasing priority. */
5460 result = entry2->priority < entry1->priority ? -1 : 1;
5461 else
5462 /* Before-strings sorted in order of increasing priority. */
5463 result = entry1->priority < entry2->priority ? -1 : 1;
5464 }
5465 else
5466 result = 0;
5467
5468 return result;
5469 }
5470
5471
5472 /* Load the vector IT->overlay_strings with overlay strings from IT's
5473 current buffer position, or from CHARPOS if that is > 0. Set
5474 IT->n_overlays to the total number of overlay strings found.
5475
5476 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5477 a time. On entry into load_overlay_strings,
5478 IT->current.overlay_string_index gives the number of overlay
5479 strings that have already been loaded by previous calls to this
5480 function.
5481
5482 IT->add_overlay_start contains an additional overlay start
5483 position to consider for taking overlay strings from, if non-zero.
5484 This position comes into play when the overlay has an `invisible'
5485 property, and both before and after-strings. When we've skipped to
5486 the end of the overlay, because of its `invisible' property, we
5487 nevertheless want its before-string to appear.
5488 IT->add_overlay_start will contain the overlay start position
5489 in this case.
5490
5491 Overlay strings are sorted so that after-string strings come in
5492 front of before-string strings. Within before and after-strings,
5493 strings are sorted by overlay priority. See also function
5494 compare_overlay_entries. */
5495
5496 static void
5497 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5498 {
5499 Lisp_Object overlay, window, str, invisible;
5500 struct Lisp_Overlay *ov;
5501 ptrdiff_t start, end;
5502 ptrdiff_t size = 20;
5503 ptrdiff_t n = 0, i, j;
5504 int invis_p;
5505 struct overlay_entry *entries = alloca (size * sizeof *entries);
5506 USE_SAFE_ALLOCA;
5507
5508 if (charpos <= 0)
5509 charpos = IT_CHARPOS (*it);
5510
5511 /* Append the overlay string STRING of overlay OVERLAY to vector
5512 `entries' which has size `size' and currently contains `n'
5513 elements. AFTER_P non-zero means STRING is an after-string of
5514 OVERLAY. */
5515 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5516 do \
5517 { \
5518 Lisp_Object priority; \
5519 \
5520 if (n == size) \
5521 { \
5522 struct overlay_entry *old = entries; \
5523 SAFE_NALLOCA (entries, 2, size); \
5524 memcpy (entries, old, size * sizeof *entries); \
5525 size *= 2; \
5526 } \
5527 \
5528 entries[n].string = (STRING); \
5529 entries[n].overlay = (OVERLAY); \
5530 priority = Foverlay_get ((OVERLAY), Qpriority); \
5531 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5532 entries[n].after_string_p = (AFTER_P); \
5533 ++n; \
5534 } \
5535 while (0)
5536
5537 /* Process overlay before the overlay center. */
5538 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5539 {
5540 XSETMISC (overlay, ov);
5541 eassert (OVERLAYP (overlay));
5542 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5543 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5544
5545 if (end < charpos)
5546 break;
5547
5548 /* Skip this overlay if it doesn't start or end at IT's current
5549 position. */
5550 if (end != charpos && start != charpos)
5551 continue;
5552
5553 /* Skip this overlay if it doesn't apply to IT->w. */
5554 window = Foverlay_get (overlay, Qwindow);
5555 if (WINDOWP (window) && XWINDOW (window) != it->w)
5556 continue;
5557
5558 /* If the text ``under'' the overlay is invisible, both before-
5559 and after-strings from this overlay are visible; start and
5560 end position are indistinguishable. */
5561 invisible = Foverlay_get (overlay, Qinvisible);
5562 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5563
5564 /* If overlay has a non-empty before-string, record it. */
5565 if ((start == charpos || (end == charpos && invis_p))
5566 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5567 && SCHARS (str))
5568 RECORD_OVERLAY_STRING (overlay, str, 0);
5569
5570 /* If overlay has a non-empty after-string, record it. */
5571 if ((end == charpos || (start == charpos && invis_p))
5572 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5573 && SCHARS (str))
5574 RECORD_OVERLAY_STRING (overlay, str, 1);
5575 }
5576
5577 /* Process overlays after the overlay center. */
5578 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5579 {
5580 XSETMISC (overlay, ov);
5581 eassert (OVERLAYP (overlay));
5582 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5583 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5584
5585 if (start > charpos)
5586 break;
5587
5588 /* Skip this overlay if it doesn't start or end at IT's current
5589 position. */
5590 if (end != charpos && start != charpos)
5591 continue;
5592
5593 /* Skip this overlay if it doesn't apply to IT->w. */
5594 window = Foverlay_get (overlay, Qwindow);
5595 if (WINDOWP (window) && XWINDOW (window) != it->w)
5596 continue;
5597
5598 /* If the text ``under'' the overlay is invisible, it has a zero
5599 dimension, and both before- and after-strings apply. */
5600 invisible = Foverlay_get (overlay, Qinvisible);
5601 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5602
5603 /* If overlay has a non-empty before-string, record it. */
5604 if ((start == charpos || (end == charpos && invis_p))
5605 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5606 && SCHARS (str))
5607 RECORD_OVERLAY_STRING (overlay, str, 0);
5608
5609 /* If overlay has a non-empty after-string, record it. */
5610 if ((end == charpos || (start == charpos && invis_p))
5611 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5612 && SCHARS (str))
5613 RECORD_OVERLAY_STRING (overlay, str, 1);
5614 }
5615
5616 #undef RECORD_OVERLAY_STRING
5617
5618 /* Sort entries. */
5619 if (n > 1)
5620 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5621
5622 /* Record number of overlay strings, and where we computed it. */
5623 it->n_overlay_strings = n;
5624 it->overlay_strings_charpos = charpos;
5625
5626 /* IT->current.overlay_string_index is the number of overlay strings
5627 that have already been consumed by IT. Copy some of the
5628 remaining overlay strings to IT->overlay_strings. */
5629 i = 0;
5630 j = it->current.overlay_string_index;
5631 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5632 {
5633 it->overlay_strings[i] = entries[j].string;
5634 it->string_overlays[i++] = entries[j++].overlay;
5635 }
5636
5637 CHECK_IT (it);
5638 SAFE_FREE ();
5639 }
5640
5641
5642 /* Get the first chunk of overlay strings at IT's current buffer
5643 position, or at CHARPOS if that is > 0. Value is non-zero if at
5644 least one overlay string was found. */
5645
5646 static int
5647 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5648 {
5649 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5650 process. This fills IT->overlay_strings with strings, and sets
5651 IT->n_overlay_strings to the total number of strings to process.
5652 IT->pos.overlay_string_index has to be set temporarily to zero
5653 because load_overlay_strings needs this; it must be set to -1
5654 when no overlay strings are found because a zero value would
5655 indicate a position in the first overlay string. */
5656 it->current.overlay_string_index = 0;
5657 load_overlay_strings (it, charpos);
5658
5659 /* If we found overlay strings, set up IT to deliver display
5660 elements from the first one. Otherwise set up IT to deliver
5661 from current_buffer. */
5662 if (it->n_overlay_strings)
5663 {
5664 /* Make sure we know settings in current_buffer, so that we can
5665 restore meaningful values when we're done with the overlay
5666 strings. */
5667 if (compute_stop_p)
5668 compute_stop_pos (it);
5669 eassert (it->face_id >= 0);
5670
5671 /* Save IT's settings. They are restored after all overlay
5672 strings have been processed. */
5673 eassert (!compute_stop_p || it->sp == 0);
5674
5675 /* When called from handle_stop, there might be an empty display
5676 string loaded. In that case, don't bother saving it. But
5677 don't use this optimization with the bidi iterator, since we
5678 need the corresponding pop_it call to resync the bidi
5679 iterator's position with IT's position, after we are done
5680 with the overlay strings. (The corresponding call to pop_it
5681 in case of an empty display string is in
5682 next_overlay_string.) */
5683 if (!(!it->bidi_p
5684 && STRINGP (it->string) && !SCHARS (it->string)))
5685 push_it (it, NULL);
5686
5687 /* Set up IT to deliver display elements from the first overlay
5688 string. */
5689 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5690 it->string = it->overlay_strings[0];
5691 it->from_overlay = Qnil;
5692 it->stop_charpos = 0;
5693 eassert (STRINGP (it->string));
5694 it->end_charpos = SCHARS (it->string);
5695 it->prev_stop = 0;
5696 it->base_level_stop = 0;
5697 it->multibyte_p = STRING_MULTIBYTE (it->string);
5698 it->method = GET_FROM_STRING;
5699 it->from_disp_prop_p = 0;
5700
5701 /* Force paragraph direction to be that of the parent
5702 buffer. */
5703 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5704 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5705 else
5706 it->paragraph_embedding = L2R;
5707
5708 /* Set up the bidi iterator for this overlay string. */
5709 if (it->bidi_p)
5710 {
5711 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5712
5713 it->bidi_it.string.lstring = it->string;
5714 it->bidi_it.string.s = NULL;
5715 it->bidi_it.string.schars = SCHARS (it->string);
5716 it->bidi_it.string.bufpos = pos;
5717 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5718 it->bidi_it.string.unibyte = !it->multibyte_p;
5719 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5720 }
5721 return 1;
5722 }
5723
5724 it->current.overlay_string_index = -1;
5725 return 0;
5726 }
5727
5728 static int
5729 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5730 {
5731 it->string = Qnil;
5732 it->method = GET_FROM_BUFFER;
5733
5734 (void) get_overlay_strings_1 (it, charpos, 1);
5735
5736 CHECK_IT (it);
5737
5738 /* Value is non-zero if we found at least one overlay string. */
5739 return STRINGP (it->string);
5740 }
5741
5742
5743 \f
5744 /***********************************************************************
5745 Saving and restoring state
5746 ***********************************************************************/
5747
5748 /* Save current settings of IT on IT->stack. Called, for example,
5749 before setting up IT for an overlay string, to be able to restore
5750 IT's settings to what they were after the overlay string has been
5751 processed. If POSITION is non-NULL, it is the position to save on
5752 the stack instead of IT->position. */
5753
5754 static void
5755 push_it (struct it *it, struct text_pos *position)
5756 {
5757 struct iterator_stack_entry *p;
5758
5759 eassert (it->sp < IT_STACK_SIZE);
5760 p = it->stack + it->sp;
5761
5762 p->stop_charpos = it->stop_charpos;
5763 p->prev_stop = it->prev_stop;
5764 p->base_level_stop = it->base_level_stop;
5765 p->cmp_it = it->cmp_it;
5766 eassert (it->face_id >= 0);
5767 p->face_id = it->face_id;
5768 p->string = it->string;
5769 p->method = it->method;
5770 p->from_overlay = it->from_overlay;
5771 switch (p->method)
5772 {
5773 case GET_FROM_IMAGE:
5774 p->u.image.object = it->object;
5775 p->u.image.image_id = it->image_id;
5776 p->u.image.slice = it->slice;
5777 break;
5778 case GET_FROM_STRETCH:
5779 p->u.stretch.object = it->object;
5780 break;
5781 }
5782 p->position = position ? *position : it->position;
5783 p->current = it->current;
5784 p->end_charpos = it->end_charpos;
5785 p->string_nchars = it->string_nchars;
5786 p->area = it->area;
5787 p->multibyte_p = it->multibyte_p;
5788 p->avoid_cursor_p = it->avoid_cursor_p;
5789 p->space_width = it->space_width;
5790 p->font_height = it->font_height;
5791 p->voffset = it->voffset;
5792 p->string_from_display_prop_p = it->string_from_display_prop_p;
5793 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5794 p->display_ellipsis_p = 0;
5795 p->line_wrap = it->line_wrap;
5796 p->bidi_p = it->bidi_p;
5797 p->paragraph_embedding = it->paragraph_embedding;
5798 p->from_disp_prop_p = it->from_disp_prop_p;
5799 ++it->sp;
5800
5801 /* Save the state of the bidi iterator as well. */
5802 if (it->bidi_p)
5803 bidi_push_it (&it->bidi_it);
5804 }
5805
5806 static void
5807 iterate_out_of_display_property (struct it *it)
5808 {
5809 int buffer_p = !STRINGP (it->string);
5810 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5811 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5812
5813 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5814
5815 /* Maybe initialize paragraph direction. If we are at the beginning
5816 of a new paragraph, next_element_from_buffer may not have a
5817 chance to do that. */
5818 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5819 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5820 /* prev_stop can be zero, so check against BEGV as well. */
5821 while (it->bidi_it.charpos >= bob
5822 && it->prev_stop <= it->bidi_it.charpos
5823 && it->bidi_it.charpos < CHARPOS (it->position)
5824 && it->bidi_it.charpos < eob)
5825 bidi_move_to_visually_next (&it->bidi_it);
5826 /* Record the stop_pos we just crossed, for when we cross it
5827 back, maybe. */
5828 if (it->bidi_it.charpos > CHARPOS (it->position))
5829 it->prev_stop = CHARPOS (it->position);
5830 /* If we ended up not where pop_it put us, resync IT's
5831 positional members with the bidi iterator. */
5832 if (it->bidi_it.charpos != CHARPOS (it->position))
5833 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5834 if (buffer_p)
5835 it->current.pos = it->position;
5836 else
5837 it->current.string_pos = it->position;
5838 }
5839
5840 /* Restore IT's settings from IT->stack. Called, for example, when no
5841 more overlay strings must be processed, and we return to delivering
5842 display elements from a buffer, or when the end of a string from a
5843 `display' property is reached and we return to delivering display
5844 elements from an overlay string, or from a buffer. */
5845
5846 static void
5847 pop_it (struct it *it)
5848 {
5849 struct iterator_stack_entry *p;
5850 int from_display_prop = it->from_disp_prop_p;
5851
5852 eassert (it->sp > 0);
5853 --it->sp;
5854 p = it->stack + it->sp;
5855 it->stop_charpos = p->stop_charpos;
5856 it->prev_stop = p->prev_stop;
5857 it->base_level_stop = p->base_level_stop;
5858 it->cmp_it = p->cmp_it;
5859 it->face_id = p->face_id;
5860 it->current = p->current;
5861 it->position = p->position;
5862 it->string = p->string;
5863 it->from_overlay = p->from_overlay;
5864 if (NILP (it->string))
5865 SET_TEXT_POS (it->current.string_pos, -1, -1);
5866 it->method = p->method;
5867 switch (it->method)
5868 {
5869 case GET_FROM_IMAGE:
5870 it->image_id = p->u.image.image_id;
5871 it->object = p->u.image.object;
5872 it->slice = p->u.image.slice;
5873 break;
5874 case GET_FROM_STRETCH:
5875 it->object = p->u.stretch.object;
5876 break;
5877 case GET_FROM_BUFFER:
5878 it->object = it->w->buffer;
5879 break;
5880 case GET_FROM_STRING:
5881 it->object = it->string;
5882 break;
5883 case GET_FROM_DISPLAY_VECTOR:
5884 if (it->s)
5885 it->method = GET_FROM_C_STRING;
5886 else if (STRINGP (it->string))
5887 it->method = GET_FROM_STRING;
5888 else
5889 {
5890 it->method = GET_FROM_BUFFER;
5891 it->object = it->w->buffer;
5892 }
5893 }
5894 it->end_charpos = p->end_charpos;
5895 it->string_nchars = p->string_nchars;
5896 it->area = p->area;
5897 it->multibyte_p = p->multibyte_p;
5898 it->avoid_cursor_p = p->avoid_cursor_p;
5899 it->space_width = p->space_width;
5900 it->font_height = p->font_height;
5901 it->voffset = p->voffset;
5902 it->string_from_display_prop_p = p->string_from_display_prop_p;
5903 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5904 it->line_wrap = p->line_wrap;
5905 it->bidi_p = p->bidi_p;
5906 it->paragraph_embedding = p->paragraph_embedding;
5907 it->from_disp_prop_p = p->from_disp_prop_p;
5908 if (it->bidi_p)
5909 {
5910 bidi_pop_it (&it->bidi_it);
5911 /* Bidi-iterate until we get out of the portion of text, if any,
5912 covered by a `display' text property or by an overlay with
5913 `display' property. (We cannot just jump there, because the
5914 internal coherency of the bidi iterator state can not be
5915 preserved across such jumps.) We also must determine the
5916 paragraph base direction if the overlay we just processed is
5917 at the beginning of a new paragraph. */
5918 if (from_display_prop
5919 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5920 iterate_out_of_display_property (it);
5921
5922 eassert ((BUFFERP (it->object)
5923 && IT_CHARPOS (*it) == it->bidi_it.charpos
5924 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5925 || (STRINGP (it->object)
5926 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5927 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5928 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5929 }
5930 }
5931
5932
5933 \f
5934 /***********************************************************************
5935 Moving over lines
5936 ***********************************************************************/
5937
5938 /* Set IT's current position to the previous line start. */
5939
5940 static void
5941 back_to_previous_line_start (struct it *it)
5942 {
5943 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5944 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5945 }
5946
5947
5948 /* Move IT to the next line start.
5949
5950 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5951 we skipped over part of the text (as opposed to moving the iterator
5952 continuously over the text). Otherwise, don't change the value
5953 of *SKIPPED_P.
5954
5955 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5956 iterator on the newline, if it was found.
5957
5958 Newlines may come from buffer text, overlay strings, or strings
5959 displayed via the `display' property. That's the reason we can't
5960 simply use find_next_newline_no_quit.
5961
5962 Note that this function may not skip over invisible text that is so
5963 because of text properties and immediately follows a newline. If
5964 it would, function reseat_at_next_visible_line_start, when called
5965 from set_iterator_to_next, would effectively make invisible
5966 characters following a newline part of the wrong glyph row, which
5967 leads to wrong cursor motion. */
5968
5969 static int
5970 forward_to_next_line_start (struct it *it, int *skipped_p,
5971 struct bidi_it *bidi_it_prev)
5972 {
5973 ptrdiff_t old_selective;
5974 int newline_found_p, n;
5975 const int MAX_NEWLINE_DISTANCE = 500;
5976
5977 /* If already on a newline, just consume it to avoid unintended
5978 skipping over invisible text below. */
5979 if (it->what == IT_CHARACTER
5980 && it->c == '\n'
5981 && CHARPOS (it->position) == IT_CHARPOS (*it))
5982 {
5983 if (it->bidi_p && bidi_it_prev)
5984 *bidi_it_prev = it->bidi_it;
5985 set_iterator_to_next (it, 0);
5986 it->c = 0;
5987 return 1;
5988 }
5989
5990 /* Don't handle selective display in the following. It's (a)
5991 unnecessary because it's done by the caller, and (b) leads to an
5992 infinite recursion because next_element_from_ellipsis indirectly
5993 calls this function. */
5994 old_selective = it->selective;
5995 it->selective = 0;
5996
5997 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5998 from buffer text. */
5999 for (n = newline_found_p = 0;
6000 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6001 n += STRINGP (it->string) ? 0 : 1)
6002 {
6003 if (!get_next_display_element (it))
6004 return 0;
6005 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6006 if (newline_found_p && it->bidi_p && bidi_it_prev)
6007 *bidi_it_prev = it->bidi_it;
6008 set_iterator_to_next (it, 0);
6009 }
6010
6011 /* If we didn't find a newline near enough, see if we can use a
6012 short-cut. */
6013 if (!newline_found_p)
6014 {
6015 ptrdiff_t start = IT_CHARPOS (*it);
6016 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
6017 Lisp_Object pos;
6018
6019 eassert (!STRINGP (it->string));
6020
6021 /* If there isn't any `display' property in sight, and no
6022 overlays, we can just use the position of the newline in
6023 buffer text. */
6024 if (it->stop_charpos >= limit
6025 || ((pos = Fnext_single_property_change (make_number (start),
6026 Qdisplay, Qnil,
6027 make_number (limit)),
6028 NILP (pos))
6029 && next_overlay_change (start) == ZV))
6030 {
6031 if (!it->bidi_p)
6032 {
6033 IT_CHARPOS (*it) = limit;
6034 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6035 }
6036 else
6037 {
6038 struct bidi_it bprev;
6039
6040 /* Help bidi.c avoid expensive searches for display
6041 properties and overlays, by telling it that there are
6042 none up to `limit'. */
6043 if (it->bidi_it.disp_pos < limit)
6044 {
6045 it->bidi_it.disp_pos = limit;
6046 it->bidi_it.disp_prop = 0;
6047 }
6048 do {
6049 bprev = it->bidi_it;
6050 bidi_move_to_visually_next (&it->bidi_it);
6051 } while (it->bidi_it.charpos != limit);
6052 IT_CHARPOS (*it) = limit;
6053 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6054 if (bidi_it_prev)
6055 *bidi_it_prev = bprev;
6056 }
6057 *skipped_p = newline_found_p = 1;
6058 }
6059 else
6060 {
6061 while (get_next_display_element (it)
6062 && !newline_found_p)
6063 {
6064 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6065 if (newline_found_p && it->bidi_p && bidi_it_prev)
6066 *bidi_it_prev = it->bidi_it;
6067 set_iterator_to_next (it, 0);
6068 }
6069 }
6070 }
6071
6072 it->selective = old_selective;
6073 return newline_found_p;
6074 }
6075
6076
6077 /* Set IT's current position to the previous visible line start. Skip
6078 invisible text that is so either due to text properties or due to
6079 selective display. Caution: this does not change IT->current_x and
6080 IT->hpos. */
6081
6082 static void
6083 back_to_previous_visible_line_start (struct it *it)
6084 {
6085 while (IT_CHARPOS (*it) > BEGV)
6086 {
6087 back_to_previous_line_start (it);
6088
6089 if (IT_CHARPOS (*it) <= BEGV)
6090 break;
6091
6092 /* If selective > 0, then lines indented more than its value are
6093 invisible. */
6094 if (it->selective > 0
6095 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6096 it->selective))
6097 continue;
6098
6099 /* Check the newline before point for invisibility. */
6100 {
6101 Lisp_Object prop;
6102 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6103 Qinvisible, it->window);
6104 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6105 continue;
6106 }
6107
6108 if (IT_CHARPOS (*it) <= BEGV)
6109 break;
6110
6111 {
6112 struct it it2;
6113 void *it2data = NULL;
6114 ptrdiff_t pos;
6115 ptrdiff_t beg, end;
6116 Lisp_Object val, overlay;
6117
6118 SAVE_IT (it2, *it, it2data);
6119
6120 /* If newline is part of a composition, continue from start of composition */
6121 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6122 && beg < IT_CHARPOS (*it))
6123 goto replaced;
6124
6125 /* If newline is replaced by a display property, find start of overlay
6126 or interval and continue search from that point. */
6127 pos = --IT_CHARPOS (it2);
6128 --IT_BYTEPOS (it2);
6129 it2.sp = 0;
6130 bidi_unshelve_cache (NULL, 0);
6131 it2.string_from_display_prop_p = 0;
6132 it2.from_disp_prop_p = 0;
6133 if (handle_display_prop (&it2) == HANDLED_RETURN
6134 && !NILP (val = get_char_property_and_overlay
6135 (make_number (pos), Qdisplay, Qnil, &overlay))
6136 && (OVERLAYP (overlay)
6137 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6138 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6139 {
6140 RESTORE_IT (it, it, it2data);
6141 goto replaced;
6142 }
6143
6144 /* Newline is not replaced by anything -- so we are done. */
6145 RESTORE_IT (it, it, it2data);
6146 break;
6147
6148 replaced:
6149 if (beg < BEGV)
6150 beg = BEGV;
6151 IT_CHARPOS (*it) = beg;
6152 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6153 }
6154 }
6155
6156 it->continuation_lines_width = 0;
6157
6158 eassert (IT_CHARPOS (*it) >= BEGV);
6159 eassert (IT_CHARPOS (*it) == BEGV
6160 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6161 CHECK_IT (it);
6162 }
6163
6164
6165 /* Reseat iterator IT at the previous visible line start. Skip
6166 invisible text that is so either due to text properties or due to
6167 selective display. At the end, update IT's overlay information,
6168 face information etc. */
6169
6170 void
6171 reseat_at_previous_visible_line_start (struct it *it)
6172 {
6173 back_to_previous_visible_line_start (it);
6174 reseat (it, it->current.pos, 1);
6175 CHECK_IT (it);
6176 }
6177
6178
6179 /* Reseat iterator IT on the next visible line start in the current
6180 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6181 preceding the line start. Skip over invisible text that is so
6182 because of selective display. Compute faces, overlays etc at the
6183 new position. Note that this function does not skip over text that
6184 is invisible because of text properties. */
6185
6186 static void
6187 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6188 {
6189 int newline_found_p, skipped_p = 0;
6190 struct bidi_it bidi_it_prev;
6191
6192 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6193
6194 /* Skip over lines that are invisible because they are indented
6195 more than the value of IT->selective. */
6196 if (it->selective > 0)
6197 while (IT_CHARPOS (*it) < ZV
6198 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6199 it->selective))
6200 {
6201 eassert (IT_BYTEPOS (*it) == BEGV
6202 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6203 newline_found_p =
6204 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6205 }
6206
6207 /* Position on the newline if that's what's requested. */
6208 if (on_newline_p && newline_found_p)
6209 {
6210 if (STRINGP (it->string))
6211 {
6212 if (IT_STRING_CHARPOS (*it) > 0)
6213 {
6214 if (!it->bidi_p)
6215 {
6216 --IT_STRING_CHARPOS (*it);
6217 --IT_STRING_BYTEPOS (*it);
6218 }
6219 else
6220 {
6221 /* We need to restore the bidi iterator to the state
6222 it had on the newline, and resync the IT's
6223 position with that. */
6224 it->bidi_it = bidi_it_prev;
6225 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6226 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6227 }
6228 }
6229 }
6230 else if (IT_CHARPOS (*it) > BEGV)
6231 {
6232 if (!it->bidi_p)
6233 {
6234 --IT_CHARPOS (*it);
6235 --IT_BYTEPOS (*it);
6236 }
6237 else
6238 {
6239 /* We need to restore the bidi iterator to the state it
6240 had on the newline and resync IT with that. */
6241 it->bidi_it = bidi_it_prev;
6242 IT_CHARPOS (*it) = it->bidi_it.charpos;
6243 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6244 }
6245 reseat (it, it->current.pos, 0);
6246 }
6247 }
6248 else if (skipped_p)
6249 reseat (it, it->current.pos, 0);
6250
6251 CHECK_IT (it);
6252 }
6253
6254
6255 \f
6256 /***********************************************************************
6257 Changing an iterator's position
6258 ***********************************************************************/
6259
6260 /* Change IT's current position to POS in current_buffer. If FORCE_P
6261 is non-zero, always check for text properties at the new position.
6262 Otherwise, text properties are only looked up if POS >=
6263 IT->check_charpos of a property. */
6264
6265 static void
6266 reseat (struct it *it, struct text_pos pos, int force_p)
6267 {
6268 ptrdiff_t original_pos = IT_CHARPOS (*it);
6269
6270 reseat_1 (it, pos, 0);
6271
6272 /* Determine where to check text properties. Avoid doing it
6273 where possible because text property lookup is very expensive. */
6274 if (force_p
6275 || CHARPOS (pos) > it->stop_charpos
6276 || CHARPOS (pos) < original_pos)
6277 {
6278 if (it->bidi_p)
6279 {
6280 /* For bidi iteration, we need to prime prev_stop and
6281 base_level_stop with our best estimations. */
6282 /* Implementation note: Of course, POS is not necessarily a
6283 stop position, so assigning prev_pos to it is a lie; we
6284 should have called compute_stop_backwards. However, if
6285 the current buffer does not include any R2L characters,
6286 that call would be a waste of cycles, because the
6287 iterator will never move back, and thus never cross this
6288 "fake" stop position. So we delay that backward search
6289 until the time we really need it, in next_element_from_buffer. */
6290 if (CHARPOS (pos) != it->prev_stop)
6291 it->prev_stop = CHARPOS (pos);
6292 if (CHARPOS (pos) < it->base_level_stop)
6293 it->base_level_stop = 0; /* meaning it's unknown */
6294 handle_stop (it);
6295 }
6296 else
6297 {
6298 handle_stop (it);
6299 it->prev_stop = it->base_level_stop = 0;
6300 }
6301
6302 }
6303
6304 CHECK_IT (it);
6305 }
6306
6307
6308 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6309 IT->stop_pos to POS, also. */
6310
6311 static void
6312 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6313 {
6314 /* Don't call this function when scanning a C string. */
6315 eassert (it->s == NULL);
6316
6317 /* POS must be a reasonable value. */
6318 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6319
6320 it->current.pos = it->position = pos;
6321 it->end_charpos = ZV;
6322 it->dpvec = NULL;
6323 it->current.dpvec_index = -1;
6324 it->current.overlay_string_index = -1;
6325 IT_STRING_CHARPOS (*it) = -1;
6326 IT_STRING_BYTEPOS (*it) = -1;
6327 it->string = Qnil;
6328 it->method = GET_FROM_BUFFER;
6329 it->object = it->w->buffer;
6330 it->area = TEXT_AREA;
6331 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6332 it->sp = 0;
6333 it->string_from_display_prop_p = 0;
6334 it->string_from_prefix_prop_p = 0;
6335
6336 it->from_disp_prop_p = 0;
6337 it->face_before_selective_p = 0;
6338 if (it->bidi_p)
6339 {
6340 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6341 &it->bidi_it);
6342 bidi_unshelve_cache (NULL, 0);
6343 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6344 it->bidi_it.string.s = NULL;
6345 it->bidi_it.string.lstring = Qnil;
6346 it->bidi_it.string.bufpos = 0;
6347 it->bidi_it.string.unibyte = 0;
6348 }
6349
6350 if (set_stop_p)
6351 {
6352 it->stop_charpos = CHARPOS (pos);
6353 it->base_level_stop = CHARPOS (pos);
6354 }
6355 /* This make the information stored in it->cmp_it invalidate. */
6356 it->cmp_it.id = -1;
6357 }
6358
6359
6360 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6361 If S is non-null, it is a C string to iterate over. Otherwise,
6362 STRING gives a Lisp string to iterate over.
6363
6364 If PRECISION > 0, don't return more then PRECISION number of
6365 characters from the string.
6366
6367 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6368 characters have been returned. FIELD_WIDTH < 0 means an infinite
6369 field width.
6370
6371 MULTIBYTE = 0 means disable processing of multibyte characters,
6372 MULTIBYTE > 0 means enable it,
6373 MULTIBYTE < 0 means use IT->multibyte_p.
6374
6375 IT must be initialized via a prior call to init_iterator before
6376 calling this function. */
6377
6378 static void
6379 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6380 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6381 int multibyte)
6382 {
6383 /* No region in strings. */
6384 it->region_beg_charpos = it->region_end_charpos = -1;
6385
6386 /* No text property checks performed by default, but see below. */
6387 it->stop_charpos = -1;
6388
6389 /* Set iterator position and end position. */
6390 memset (&it->current, 0, sizeof it->current);
6391 it->current.overlay_string_index = -1;
6392 it->current.dpvec_index = -1;
6393 eassert (charpos >= 0);
6394
6395 /* If STRING is specified, use its multibyteness, otherwise use the
6396 setting of MULTIBYTE, if specified. */
6397 if (multibyte >= 0)
6398 it->multibyte_p = multibyte > 0;
6399
6400 /* Bidirectional reordering of strings is controlled by the default
6401 value of bidi-display-reordering. Don't try to reorder while
6402 loading loadup.el, as the necessary character property tables are
6403 not yet available. */
6404 it->bidi_p =
6405 NILP (Vpurify_flag)
6406 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6407
6408 if (s == NULL)
6409 {
6410 eassert (STRINGP (string));
6411 it->string = string;
6412 it->s = NULL;
6413 it->end_charpos = it->string_nchars = SCHARS (string);
6414 it->method = GET_FROM_STRING;
6415 it->current.string_pos = string_pos (charpos, string);
6416
6417 if (it->bidi_p)
6418 {
6419 it->bidi_it.string.lstring = string;
6420 it->bidi_it.string.s = NULL;
6421 it->bidi_it.string.schars = it->end_charpos;
6422 it->bidi_it.string.bufpos = 0;
6423 it->bidi_it.string.from_disp_str = 0;
6424 it->bidi_it.string.unibyte = !it->multibyte_p;
6425 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6426 FRAME_WINDOW_P (it->f), &it->bidi_it);
6427 }
6428 }
6429 else
6430 {
6431 it->s = (const unsigned char *) s;
6432 it->string = Qnil;
6433
6434 /* Note that we use IT->current.pos, not it->current.string_pos,
6435 for displaying C strings. */
6436 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6437 if (it->multibyte_p)
6438 {
6439 it->current.pos = c_string_pos (charpos, s, 1);
6440 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6441 }
6442 else
6443 {
6444 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6445 it->end_charpos = it->string_nchars = strlen (s);
6446 }
6447
6448 if (it->bidi_p)
6449 {
6450 it->bidi_it.string.lstring = Qnil;
6451 it->bidi_it.string.s = (const unsigned char *) s;
6452 it->bidi_it.string.schars = it->end_charpos;
6453 it->bidi_it.string.bufpos = 0;
6454 it->bidi_it.string.from_disp_str = 0;
6455 it->bidi_it.string.unibyte = !it->multibyte_p;
6456 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6457 &it->bidi_it);
6458 }
6459 it->method = GET_FROM_C_STRING;
6460 }
6461
6462 /* PRECISION > 0 means don't return more than PRECISION characters
6463 from the string. */
6464 if (precision > 0 && it->end_charpos - charpos > precision)
6465 {
6466 it->end_charpos = it->string_nchars = charpos + precision;
6467 if (it->bidi_p)
6468 it->bidi_it.string.schars = it->end_charpos;
6469 }
6470
6471 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6472 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6473 FIELD_WIDTH < 0 means infinite field width. This is useful for
6474 padding with `-' at the end of a mode line. */
6475 if (field_width < 0)
6476 field_width = INFINITY;
6477 /* Implementation note: We deliberately don't enlarge
6478 it->bidi_it.string.schars here to fit it->end_charpos, because
6479 the bidi iterator cannot produce characters out of thin air. */
6480 if (field_width > it->end_charpos - charpos)
6481 it->end_charpos = charpos + field_width;
6482
6483 /* Use the standard display table for displaying strings. */
6484 if (DISP_TABLE_P (Vstandard_display_table))
6485 it->dp = XCHAR_TABLE (Vstandard_display_table);
6486
6487 it->stop_charpos = charpos;
6488 it->prev_stop = charpos;
6489 it->base_level_stop = 0;
6490 if (it->bidi_p)
6491 {
6492 it->bidi_it.first_elt = 1;
6493 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6494 it->bidi_it.disp_pos = -1;
6495 }
6496 if (s == NULL && it->multibyte_p)
6497 {
6498 ptrdiff_t endpos = SCHARS (it->string);
6499 if (endpos > it->end_charpos)
6500 endpos = it->end_charpos;
6501 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6502 it->string);
6503 }
6504 CHECK_IT (it);
6505 }
6506
6507
6508 \f
6509 /***********************************************************************
6510 Iteration
6511 ***********************************************************************/
6512
6513 /* Map enum it_method value to corresponding next_element_from_* function. */
6514
6515 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6516 {
6517 next_element_from_buffer,
6518 next_element_from_display_vector,
6519 next_element_from_string,
6520 next_element_from_c_string,
6521 next_element_from_image,
6522 next_element_from_stretch
6523 };
6524
6525 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6526
6527
6528 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6529 (possibly with the following characters). */
6530
6531 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6532 ((IT)->cmp_it.id >= 0 \
6533 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6534 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6535 END_CHARPOS, (IT)->w, \
6536 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6537 (IT)->string)))
6538
6539
6540 /* Lookup the char-table Vglyphless_char_display for character C (-1
6541 if we want information for no-font case), and return the display
6542 method symbol. By side-effect, update it->what and
6543 it->glyphless_method. This function is called from
6544 get_next_display_element for each character element, and from
6545 x_produce_glyphs when no suitable font was found. */
6546
6547 Lisp_Object
6548 lookup_glyphless_char_display (int c, struct it *it)
6549 {
6550 Lisp_Object glyphless_method = Qnil;
6551
6552 if (CHAR_TABLE_P (Vglyphless_char_display)
6553 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6554 {
6555 if (c >= 0)
6556 {
6557 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6558 if (CONSP (glyphless_method))
6559 glyphless_method = FRAME_WINDOW_P (it->f)
6560 ? XCAR (glyphless_method)
6561 : XCDR (glyphless_method);
6562 }
6563 else
6564 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6565 }
6566
6567 retry:
6568 if (NILP (glyphless_method))
6569 {
6570 if (c >= 0)
6571 /* The default is to display the character by a proper font. */
6572 return Qnil;
6573 /* The default for the no-font case is to display an empty box. */
6574 glyphless_method = Qempty_box;
6575 }
6576 if (EQ (glyphless_method, Qzero_width))
6577 {
6578 if (c >= 0)
6579 return glyphless_method;
6580 /* This method can't be used for the no-font case. */
6581 glyphless_method = Qempty_box;
6582 }
6583 if (EQ (glyphless_method, Qthin_space))
6584 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6585 else if (EQ (glyphless_method, Qempty_box))
6586 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6587 else if (EQ (glyphless_method, Qhex_code))
6588 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6589 else if (STRINGP (glyphless_method))
6590 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6591 else
6592 {
6593 /* Invalid value. We use the default method. */
6594 glyphless_method = Qnil;
6595 goto retry;
6596 }
6597 it->what = IT_GLYPHLESS;
6598 return glyphless_method;
6599 }
6600
6601 /* Load IT's display element fields with information about the next
6602 display element from the current position of IT. Value is zero if
6603 end of buffer (or C string) is reached. */
6604
6605 static struct frame *last_escape_glyph_frame = NULL;
6606 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6607 static int last_escape_glyph_merged_face_id = 0;
6608
6609 struct frame *last_glyphless_glyph_frame = NULL;
6610 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6611 int last_glyphless_glyph_merged_face_id = 0;
6612
6613 static int
6614 get_next_display_element (struct it *it)
6615 {
6616 /* Non-zero means that we found a display element. Zero means that
6617 we hit the end of what we iterate over. Performance note: the
6618 function pointer `method' used here turns out to be faster than
6619 using a sequence of if-statements. */
6620 int success_p;
6621
6622 get_next:
6623 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6624
6625 if (it->what == IT_CHARACTER)
6626 {
6627 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6628 and only if (a) the resolved directionality of that character
6629 is R..." */
6630 /* FIXME: Do we need an exception for characters from display
6631 tables? */
6632 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6633 it->c = bidi_mirror_char (it->c);
6634 /* Map via display table or translate control characters.
6635 IT->c, IT->len etc. have been set to the next character by
6636 the function call above. If we have a display table, and it
6637 contains an entry for IT->c, translate it. Don't do this if
6638 IT->c itself comes from a display table, otherwise we could
6639 end up in an infinite recursion. (An alternative could be to
6640 count the recursion depth of this function and signal an
6641 error when a certain maximum depth is reached.) Is it worth
6642 it? */
6643 if (success_p && it->dpvec == NULL)
6644 {
6645 Lisp_Object dv;
6646 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6647 int nonascii_space_p = 0;
6648 int nonascii_hyphen_p = 0;
6649 int c = it->c; /* This is the character to display. */
6650
6651 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6652 {
6653 eassert (SINGLE_BYTE_CHAR_P (c));
6654 if (unibyte_display_via_language_environment)
6655 {
6656 c = DECODE_CHAR (unibyte, c);
6657 if (c < 0)
6658 c = BYTE8_TO_CHAR (it->c);
6659 }
6660 else
6661 c = BYTE8_TO_CHAR (it->c);
6662 }
6663
6664 if (it->dp
6665 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6666 VECTORP (dv)))
6667 {
6668 struct Lisp_Vector *v = XVECTOR (dv);
6669
6670 /* Return the first character from the display table
6671 entry, if not empty. If empty, don't display the
6672 current character. */
6673 if (v->header.size)
6674 {
6675 it->dpvec_char_len = it->len;
6676 it->dpvec = v->contents;
6677 it->dpend = v->contents + v->header.size;
6678 it->current.dpvec_index = 0;
6679 it->dpvec_face_id = -1;
6680 it->saved_face_id = it->face_id;
6681 it->method = GET_FROM_DISPLAY_VECTOR;
6682 it->ellipsis_p = 0;
6683 }
6684 else
6685 {
6686 set_iterator_to_next (it, 0);
6687 }
6688 goto get_next;
6689 }
6690
6691 if (! NILP (lookup_glyphless_char_display (c, it)))
6692 {
6693 if (it->what == IT_GLYPHLESS)
6694 goto done;
6695 /* Don't display this character. */
6696 set_iterator_to_next (it, 0);
6697 goto get_next;
6698 }
6699
6700 /* If `nobreak-char-display' is non-nil, we display
6701 non-ASCII spaces and hyphens specially. */
6702 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6703 {
6704 if (c == 0xA0)
6705 nonascii_space_p = 1;
6706 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6707 nonascii_hyphen_p = 1;
6708 }
6709
6710 /* Translate control characters into `\003' or `^C' form.
6711 Control characters coming from a display table entry are
6712 currently not translated because we use IT->dpvec to hold
6713 the translation. This could easily be changed but I
6714 don't believe that it is worth doing.
6715
6716 The characters handled by `nobreak-char-display' must be
6717 translated too.
6718
6719 Non-printable characters and raw-byte characters are also
6720 translated to octal form. */
6721 if (((c < ' ' || c == 127) /* ASCII control chars */
6722 ? (it->area != TEXT_AREA
6723 /* In mode line, treat \n, \t like other crl chars. */
6724 || (c != '\t'
6725 && it->glyph_row
6726 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6727 || (c != '\n' && c != '\t'))
6728 : (nonascii_space_p
6729 || nonascii_hyphen_p
6730 || CHAR_BYTE8_P (c)
6731 || ! CHAR_PRINTABLE_P (c))))
6732 {
6733 /* C is a control character, non-ASCII space/hyphen,
6734 raw-byte, or a non-printable character which must be
6735 displayed either as '\003' or as `^C' where the '\\'
6736 and '^' can be defined in the display table. Fill
6737 IT->ctl_chars with glyphs for what we have to
6738 display. Then, set IT->dpvec to these glyphs. */
6739 Lisp_Object gc;
6740 int ctl_len;
6741 int face_id;
6742 int lface_id = 0;
6743 int escape_glyph;
6744
6745 /* Handle control characters with ^. */
6746
6747 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6748 {
6749 int g;
6750
6751 g = '^'; /* default glyph for Control */
6752 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6753 if (it->dp
6754 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6755 {
6756 g = GLYPH_CODE_CHAR (gc);
6757 lface_id = GLYPH_CODE_FACE (gc);
6758 }
6759 if (lface_id)
6760 {
6761 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6762 }
6763 else if (it->f == last_escape_glyph_frame
6764 && it->face_id == last_escape_glyph_face_id)
6765 {
6766 face_id = last_escape_glyph_merged_face_id;
6767 }
6768 else
6769 {
6770 /* Merge the escape-glyph face into the current face. */
6771 face_id = merge_faces (it->f, Qescape_glyph, 0,
6772 it->face_id);
6773 last_escape_glyph_frame = it->f;
6774 last_escape_glyph_face_id = it->face_id;
6775 last_escape_glyph_merged_face_id = face_id;
6776 }
6777
6778 XSETINT (it->ctl_chars[0], g);
6779 XSETINT (it->ctl_chars[1], c ^ 0100);
6780 ctl_len = 2;
6781 goto display_control;
6782 }
6783
6784 /* Handle non-ascii space in the mode where it only gets
6785 highlighting. */
6786
6787 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6788 {
6789 /* Merge `nobreak-space' into the current face. */
6790 face_id = merge_faces (it->f, Qnobreak_space, 0,
6791 it->face_id);
6792 XSETINT (it->ctl_chars[0], ' ');
6793 ctl_len = 1;
6794 goto display_control;
6795 }
6796
6797 /* Handle sequences that start with the "escape glyph". */
6798
6799 /* the default escape glyph is \. */
6800 escape_glyph = '\\';
6801
6802 if (it->dp
6803 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6804 {
6805 escape_glyph = GLYPH_CODE_CHAR (gc);
6806 lface_id = GLYPH_CODE_FACE (gc);
6807 }
6808 if (lface_id)
6809 {
6810 /* The display table specified a face.
6811 Merge it into face_id and also into escape_glyph. */
6812 face_id = merge_faces (it->f, Qt, lface_id,
6813 it->face_id);
6814 }
6815 else if (it->f == last_escape_glyph_frame
6816 && it->face_id == last_escape_glyph_face_id)
6817 {
6818 face_id = last_escape_glyph_merged_face_id;
6819 }
6820 else
6821 {
6822 /* Merge the escape-glyph face into the current face. */
6823 face_id = merge_faces (it->f, Qescape_glyph, 0,
6824 it->face_id);
6825 last_escape_glyph_frame = it->f;
6826 last_escape_glyph_face_id = it->face_id;
6827 last_escape_glyph_merged_face_id = face_id;
6828 }
6829
6830 /* Draw non-ASCII hyphen with just highlighting: */
6831
6832 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6833 {
6834 XSETINT (it->ctl_chars[0], '-');
6835 ctl_len = 1;
6836 goto display_control;
6837 }
6838
6839 /* Draw non-ASCII space/hyphen with escape glyph: */
6840
6841 if (nonascii_space_p || nonascii_hyphen_p)
6842 {
6843 XSETINT (it->ctl_chars[0], escape_glyph);
6844 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6845 ctl_len = 2;
6846 goto display_control;
6847 }
6848
6849 {
6850 char str[10];
6851 int len, i;
6852
6853 if (CHAR_BYTE8_P (c))
6854 /* Display \200 instead of \17777600. */
6855 c = CHAR_TO_BYTE8 (c);
6856 len = sprintf (str, "%03o", c);
6857
6858 XSETINT (it->ctl_chars[0], escape_glyph);
6859 for (i = 0; i < len; i++)
6860 XSETINT (it->ctl_chars[i + 1], str[i]);
6861 ctl_len = len + 1;
6862 }
6863
6864 display_control:
6865 /* Set up IT->dpvec and return first character from it. */
6866 it->dpvec_char_len = it->len;
6867 it->dpvec = it->ctl_chars;
6868 it->dpend = it->dpvec + ctl_len;
6869 it->current.dpvec_index = 0;
6870 it->dpvec_face_id = face_id;
6871 it->saved_face_id = it->face_id;
6872 it->method = GET_FROM_DISPLAY_VECTOR;
6873 it->ellipsis_p = 0;
6874 goto get_next;
6875 }
6876 it->char_to_display = c;
6877 }
6878 else if (success_p)
6879 {
6880 it->char_to_display = it->c;
6881 }
6882 }
6883
6884 /* Adjust face id for a multibyte character. There are no multibyte
6885 character in unibyte text. */
6886 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6887 && it->multibyte_p
6888 && success_p
6889 && FRAME_WINDOW_P (it->f))
6890 {
6891 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6892
6893 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6894 {
6895 /* Automatic composition with glyph-string. */
6896 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6897
6898 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6899 }
6900 else
6901 {
6902 ptrdiff_t pos = (it->s ? -1
6903 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6904 : IT_CHARPOS (*it));
6905 int c;
6906
6907 if (it->what == IT_CHARACTER)
6908 c = it->char_to_display;
6909 else
6910 {
6911 struct composition *cmp = composition_table[it->cmp_it.id];
6912 int i;
6913
6914 c = ' ';
6915 for (i = 0; i < cmp->glyph_len; i++)
6916 /* TAB in a composition means display glyphs with
6917 padding space on the left or right. */
6918 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6919 break;
6920 }
6921 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6922 }
6923 }
6924
6925 done:
6926 /* Is this character the last one of a run of characters with
6927 box? If yes, set IT->end_of_box_run_p to 1. */
6928 if (it->face_box_p
6929 && it->s == NULL)
6930 {
6931 if (it->method == GET_FROM_STRING && it->sp)
6932 {
6933 int face_id = underlying_face_id (it);
6934 struct face *face = FACE_FROM_ID (it->f, face_id);
6935
6936 if (face)
6937 {
6938 if (face->box == FACE_NO_BOX)
6939 {
6940 /* If the box comes from face properties in a
6941 display string, check faces in that string. */
6942 int string_face_id = face_after_it_pos (it);
6943 it->end_of_box_run_p
6944 = (FACE_FROM_ID (it->f, string_face_id)->box
6945 == FACE_NO_BOX);
6946 }
6947 /* Otherwise, the box comes from the underlying face.
6948 If this is the last string character displayed, check
6949 the next buffer location. */
6950 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6951 && (it->current.overlay_string_index
6952 == it->n_overlay_strings - 1))
6953 {
6954 ptrdiff_t ignore;
6955 int next_face_id;
6956 struct text_pos pos = it->current.pos;
6957 INC_TEXT_POS (pos, it->multibyte_p);
6958
6959 next_face_id = face_at_buffer_position
6960 (it->w, CHARPOS (pos), it->region_beg_charpos,
6961 it->region_end_charpos, &ignore,
6962 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6963 -1);
6964 it->end_of_box_run_p
6965 = (FACE_FROM_ID (it->f, next_face_id)->box
6966 == FACE_NO_BOX);
6967 }
6968 }
6969 }
6970 else
6971 {
6972 int face_id = face_after_it_pos (it);
6973 it->end_of_box_run_p
6974 = (face_id != it->face_id
6975 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6976 }
6977 }
6978 /* If we reached the end of the object we've been iterating (e.g., a
6979 display string or an overlay string), and there's something on
6980 IT->stack, proceed with what's on the stack. It doesn't make
6981 sense to return zero if there's unprocessed stuff on the stack,
6982 because otherwise that stuff will never be displayed. */
6983 if (!success_p && it->sp > 0)
6984 {
6985 set_iterator_to_next (it, 0);
6986 success_p = get_next_display_element (it);
6987 }
6988
6989 /* Value is 0 if end of buffer or string reached. */
6990 return success_p;
6991 }
6992
6993
6994 /* Move IT to the next display element.
6995
6996 RESEAT_P non-zero means if called on a newline in buffer text,
6997 skip to the next visible line start.
6998
6999 Functions get_next_display_element and set_iterator_to_next are
7000 separate because I find this arrangement easier to handle than a
7001 get_next_display_element function that also increments IT's
7002 position. The way it is we can first look at an iterator's current
7003 display element, decide whether it fits on a line, and if it does,
7004 increment the iterator position. The other way around we probably
7005 would either need a flag indicating whether the iterator has to be
7006 incremented the next time, or we would have to implement a
7007 decrement position function which would not be easy to write. */
7008
7009 void
7010 set_iterator_to_next (struct it *it, int reseat_p)
7011 {
7012 /* Reset flags indicating start and end of a sequence of characters
7013 with box. Reset them at the start of this function because
7014 moving the iterator to a new position might set them. */
7015 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7016
7017 switch (it->method)
7018 {
7019 case GET_FROM_BUFFER:
7020 /* The current display element of IT is a character from
7021 current_buffer. Advance in the buffer, and maybe skip over
7022 invisible lines that are so because of selective display. */
7023 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7024 reseat_at_next_visible_line_start (it, 0);
7025 else if (it->cmp_it.id >= 0)
7026 {
7027 /* We are currently getting glyphs from a composition. */
7028 int i;
7029
7030 if (! it->bidi_p)
7031 {
7032 IT_CHARPOS (*it) += it->cmp_it.nchars;
7033 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7034 if (it->cmp_it.to < it->cmp_it.nglyphs)
7035 {
7036 it->cmp_it.from = it->cmp_it.to;
7037 }
7038 else
7039 {
7040 it->cmp_it.id = -1;
7041 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7042 IT_BYTEPOS (*it),
7043 it->end_charpos, Qnil);
7044 }
7045 }
7046 else if (! it->cmp_it.reversed_p)
7047 {
7048 /* Composition created while scanning forward. */
7049 /* Update IT's char/byte positions to point to the first
7050 character of the next grapheme cluster, or to the
7051 character visually after the current composition. */
7052 for (i = 0; i < it->cmp_it.nchars; i++)
7053 bidi_move_to_visually_next (&it->bidi_it);
7054 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7055 IT_CHARPOS (*it) = it->bidi_it.charpos;
7056
7057 if (it->cmp_it.to < it->cmp_it.nglyphs)
7058 {
7059 /* Proceed to the next grapheme cluster. */
7060 it->cmp_it.from = it->cmp_it.to;
7061 }
7062 else
7063 {
7064 /* No more grapheme clusters in this composition.
7065 Find the next stop position. */
7066 ptrdiff_t stop = it->end_charpos;
7067 if (it->bidi_it.scan_dir < 0)
7068 /* Now we are scanning backward and don't know
7069 where to stop. */
7070 stop = -1;
7071 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7072 IT_BYTEPOS (*it), stop, Qnil);
7073 }
7074 }
7075 else
7076 {
7077 /* Composition created while scanning backward. */
7078 /* Update IT's char/byte positions to point to the last
7079 character of the previous grapheme cluster, or the
7080 character visually after the current composition. */
7081 for (i = 0; i < it->cmp_it.nchars; i++)
7082 bidi_move_to_visually_next (&it->bidi_it);
7083 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7084 IT_CHARPOS (*it) = it->bidi_it.charpos;
7085 if (it->cmp_it.from > 0)
7086 {
7087 /* Proceed to the previous grapheme cluster. */
7088 it->cmp_it.to = it->cmp_it.from;
7089 }
7090 else
7091 {
7092 /* No more grapheme clusters in this composition.
7093 Find the next stop position. */
7094 ptrdiff_t stop = it->end_charpos;
7095 if (it->bidi_it.scan_dir < 0)
7096 /* Now we are scanning backward and don't know
7097 where to stop. */
7098 stop = -1;
7099 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7100 IT_BYTEPOS (*it), stop, Qnil);
7101 }
7102 }
7103 }
7104 else
7105 {
7106 eassert (it->len != 0);
7107
7108 if (!it->bidi_p)
7109 {
7110 IT_BYTEPOS (*it) += it->len;
7111 IT_CHARPOS (*it) += 1;
7112 }
7113 else
7114 {
7115 int prev_scan_dir = it->bidi_it.scan_dir;
7116 /* If this is a new paragraph, determine its base
7117 direction (a.k.a. its base embedding level). */
7118 if (it->bidi_it.new_paragraph)
7119 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7120 bidi_move_to_visually_next (&it->bidi_it);
7121 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7122 IT_CHARPOS (*it) = it->bidi_it.charpos;
7123 if (prev_scan_dir != it->bidi_it.scan_dir)
7124 {
7125 /* As the scan direction was changed, we must
7126 re-compute the stop position for composition. */
7127 ptrdiff_t stop = it->end_charpos;
7128 if (it->bidi_it.scan_dir < 0)
7129 stop = -1;
7130 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7131 IT_BYTEPOS (*it), stop, Qnil);
7132 }
7133 }
7134 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7135 }
7136 break;
7137
7138 case GET_FROM_C_STRING:
7139 /* Current display element of IT is from a C string. */
7140 if (!it->bidi_p
7141 /* If the string position is beyond string's end, it means
7142 next_element_from_c_string is padding the string with
7143 blanks, in which case we bypass the bidi iterator,
7144 because it cannot deal with such virtual characters. */
7145 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7146 {
7147 IT_BYTEPOS (*it) += it->len;
7148 IT_CHARPOS (*it) += 1;
7149 }
7150 else
7151 {
7152 bidi_move_to_visually_next (&it->bidi_it);
7153 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7154 IT_CHARPOS (*it) = it->bidi_it.charpos;
7155 }
7156 break;
7157
7158 case GET_FROM_DISPLAY_VECTOR:
7159 /* Current display element of IT is from a display table entry.
7160 Advance in the display table definition. Reset it to null if
7161 end reached, and continue with characters from buffers/
7162 strings. */
7163 ++it->current.dpvec_index;
7164
7165 /* Restore face of the iterator to what they were before the
7166 display vector entry (these entries may contain faces). */
7167 it->face_id = it->saved_face_id;
7168
7169 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7170 {
7171 int recheck_faces = it->ellipsis_p;
7172
7173 if (it->s)
7174 it->method = GET_FROM_C_STRING;
7175 else if (STRINGP (it->string))
7176 it->method = GET_FROM_STRING;
7177 else
7178 {
7179 it->method = GET_FROM_BUFFER;
7180 it->object = it->w->buffer;
7181 }
7182
7183 it->dpvec = NULL;
7184 it->current.dpvec_index = -1;
7185
7186 /* Skip over characters which were displayed via IT->dpvec. */
7187 if (it->dpvec_char_len < 0)
7188 reseat_at_next_visible_line_start (it, 1);
7189 else if (it->dpvec_char_len > 0)
7190 {
7191 if (it->method == GET_FROM_STRING
7192 && it->n_overlay_strings > 0)
7193 it->ignore_overlay_strings_at_pos_p = 1;
7194 it->len = it->dpvec_char_len;
7195 set_iterator_to_next (it, reseat_p);
7196 }
7197
7198 /* Maybe recheck faces after display vector */
7199 if (recheck_faces)
7200 it->stop_charpos = IT_CHARPOS (*it);
7201 }
7202 break;
7203
7204 case GET_FROM_STRING:
7205 /* Current display element is a character from a Lisp string. */
7206 eassert (it->s == NULL && STRINGP (it->string));
7207 /* Don't advance past string end. These conditions are true
7208 when set_iterator_to_next is called at the end of
7209 get_next_display_element, in which case the Lisp string is
7210 already exhausted, and all we want is pop the iterator
7211 stack. */
7212 if (it->current.overlay_string_index >= 0)
7213 {
7214 /* This is an overlay string, so there's no padding with
7215 spaces, and the number of characters in the string is
7216 where the string ends. */
7217 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7218 goto consider_string_end;
7219 }
7220 else
7221 {
7222 /* Not an overlay string. There could be padding, so test
7223 against it->end_charpos . */
7224 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7225 goto consider_string_end;
7226 }
7227 if (it->cmp_it.id >= 0)
7228 {
7229 int i;
7230
7231 if (! it->bidi_p)
7232 {
7233 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7234 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7235 if (it->cmp_it.to < it->cmp_it.nglyphs)
7236 it->cmp_it.from = it->cmp_it.to;
7237 else
7238 {
7239 it->cmp_it.id = -1;
7240 composition_compute_stop_pos (&it->cmp_it,
7241 IT_STRING_CHARPOS (*it),
7242 IT_STRING_BYTEPOS (*it),
7243 it->end_charpos, it->string);
7244 }
7245 }
7246 else if (! it->cmp_it.reversed_p)
7247 {
7248 for (i = 0; i < it->cmp_it.nchars; i++)
7249 bidi_move_to_visually_next (&it->bidi_it);
7250 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7251 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7252
7253 if (it->cmp_it.to < it->cmp_it.nglyphs)
7254 it->cmp_it.from = it->cmp_it.to;
7255 else
7256 {
7257 ptrdiff_t stop = it->end_charpos;
7258 if (it->bidi_it.scan_dir < 0)
7259 stop = -1;
7260 composition_compute_stop_pos (&it->cmp_it,
7261 IT_STRING_CHARPOS (*it),
7262 IT_STRING_BYTEPOS (*it), stop,
7263 it->string);
7264 }
7265 }
7266 else
7267 {
7268 for (i = 0; i < it->cmp_it.nchars; i++)
7269 bidi_move_to_visually_next (&it->bidi_it);
7270 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7271 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7272 if (it->cmp_it.from > 0)
7273 it->cmp_it.to = it->cmp_it.from;
7274 else
7275 {
7276 ptrdiff_t stop = it->end_charpos;
7277 if (it->bidi_it.scan_dir < 0)
7278 stop = -1;
7279 composition_compute_stop_pos (&it->cmp_it,
7280 IT_STRING_CHARPOS (*it),
7281 IT_STRING_BYTEPOS (*it), stop,
7282 it->string);
7283 }
7284 }
7285 }
7286 else
7287 {
7288 if (!it->bidi_p
7289 /* If the string position is beyond string's end, it
7290 means next_element_from_string is padding the string
7291 with blanks, in which case we bypass the bidi
7292 iterator, because it cannot deal with such virtual
7293 characters. */
7294 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7295 {
7296 IT_STRING_BYTEPOS (*it) += it->len;
7297 IT_STRING_CHARPOS (*it) += 1;
7298 }
7299 else
7300 {
7301 int prev_scan_dir = it->bidi_it.scan_dir;
7302
7303 bidi_move_to_visually_next (&it->bidi_it);
7304 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7305 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7306 if (prev_scan_dir != it->bidi_it.scan_dir)
7307 {
7308 ptrdiff_t stop = it->end_charpos;
7309
7310 if (it->bidi_it.scan_dir < 0)
7311 stop = -1;
7312 composition_compute_stop_pos (&it->cmp_it,
7313 IT_STRING_CHARPOS (*it),
7314 IT_STRING_BYTEPOS (*it), stop,
7315 it->string);
7316 }
7317 }
7318 }
7319
7320 consider_string_end:
7321
7322 if (it->current.overlay_string_index >= 0)
7323 {
7324 /* IT->string is an overlay string. Advance to the
7325 next, if there is one. */
7326 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7327 {
7328 it->ellipsis_p = 0;
7329 next_overlay_string (it);
7330 if (it->ellipsis_p)
7331 setup_for_ellipsis (it, 0);
7332 }
7333 }
7334 else
7335 {
7336 /* IT->string is not an overlay string. If we reached
7337 its end, and there is something on IT->stack, proceed
7338 with what is on the stack. This can be either another
7339 string, this time an overlay string, or a buffer. */
7340 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7341 && it->sp > 0)
7342 {
7343 pop_it (it);
7344 if (it->method == GET_FROM_STRING)
7345 goto consider_string_end;
7346 }
7347 }
7348 break;
7349
7350 case GET_FROM_IMAGE:
7351 case GET_FROM_STRETCH:
7352 /* The position etc with which we have to proceed are on
7353 the stack. The position may be at the end of a string,
7354 if the `display' property takes up the whole string. */
7355 eassert (it->sp > 0);
7356 pop_it (it);
7357 if (it->method == GET_FROM_STRING)
7358 goto consider_string_end;
7359 break;
7360
7361 default:
7362 /* There are no other methods defined, so this should be a bug. */
7363 emacs_abort ();
7364 }
7365
7366 eassert (it->method != GET_FROM_STRING
7367 || (STRINGP (it->string)
7368 && IT_STRING_CHARPOS (*it) >= 0));
7369 }
7370
7371 /* Load IT's display element fields with information about the next
7372 display element which comes from a display table entry or from the
7373 result of translating a control character to one of the forms `^C'
7374 or `\003'.
7375
7376 IT->dpvec holds the glyphs to return as characters.
7377 IT->saved_face_id holds the face id before the display vector--it
7378 is restored into IT->face_id in set_iterator_to_next. */
7379
7380 static int
7381 next_element_from_display_vector (struct it *it)
7382 {
7383 Lisp_Object gc;
7384
7385 /* Precondition. */
7386 eassert (it->dpvec && it->current.dpvec_index >= 0);
7387
7388 it->face_id = it->saved_face_id;
7389
7390 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7391 That seemed totally bogus - so I changed it... */
7392 gc = it->dpvec[it->current.dpvec_index];
7393
7394 if (GLYPH_CODE_P (gc))
7395 {
7396 it->c = GLYPH_CODE_CHAR (gc);
7397 it->len = CHAR_BYTES (it->c);
7398
7399 /* The entry may contain a face id to use. Such a face id is
7400 the id of a Lisp face, not a realized face. A face id of
7401 zero means no face is specified. */
7402 if (it->dpvec_face_id >= 0)
7403 it->face_id = it->dpvec_face_id;
7404 else
7405 {
7406 int lface_id = GLYPH_CODE_FACE (gc);
7407 if (lface_id > 0)
7408 it->face_id = merge_faces (it->f, Qt, lface_id,
7409 it->saved_face_id);
7410 }
7411 }
7412 else
7413 /* Display table entry is invalid. Return a space. */
7414 it->c = ' ', it->len = 1;
7415
7416 /* Don't change position and object of the iterator here. They are
7417 still the values of the character that had this display table
7418 entry or was translated, and that's what we want. */
7419 it->what = IT_CHARACTER;
7420 return 1;
7421 }
7422
7423 /* Get the first element of string/buffer in the visual order, after
7424 being reseated to a new position in a string or a buffer. */
7425 static void
7426 get_visually_first_element (struct it *it)
7427 {
7428 int string_p = STRINGP (it->string) || it->s;
7429 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7430 ptrdiff_t bob = (string_p ? 0 : BEGV);
7431
7432 if (STRINGP (it->string))
7433 {
7434 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7435 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7436 }
7437 else
7438 {
7439 it->bidi_it.charpos = IT_CHARPOS (*it);
7440 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7441 }
7442
7443 if (it->bidi_it.charpos == eob)
7444 {
7445 /* Nothing to do, but reset the FIRST_ELT flag, like
7446 bidi_paragraph_init does, because we are not going to
7447 call it. */
7448 it->bidi_it.first_elt = 0;
7449 }
7450 else if (it->bidi_it.charpos == bob
7451 || (!string_p
7452 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7453 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7454 {
7455 /* If we are at the beginning of a line/string, we can produce
7456 the next element right away. */
7457 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7458 bidi_move_to_visually_next (&it->bidi_it);
7459 }
7460 else
7461 {
7462 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7463
7464 /* We need to prime the bidi iterator starting at the line's or
7465 string's beginning, before we will be able to produce the
7466 next element. */
7467 if (string_p)
7468 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7469 else
7470 {
7471 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7472 -1);
7473 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7474 }
7475 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7476 do
7477 {
7478 /* Now return to buffer/string position where we were asked
7479 to get the next display element, and produce that. */
7480 bidi_move_to_visually_next (&it->bidi_it);
7481 }
7482 while (it->bidi_it.bytepos != orig_bytepos
7483 && it->bidi_it.charpos < eob);
7484 }
7485
7486 /* Adjust IT's position information to where we ended up. */
7487 if (STRINGP (it->string))
7488 {
7489 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7490 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7491 }
7492 else
7493 {
7494 IT_CHARPOS (*it) = it->bidi_it.charpos;
7495 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7496 }
7497
7498 if (STRINGP (it->string) || !it->s)
7499 {
7500 ptrdiff_t stop, charpos, bytepos;
7501
7502 if (STRINGP (it->string))
7503 {
7504 eassert (!it->s);
7505 stop = SCHARS (it->string);
7506 if (stop > it->end_charpos)
7507 stop = it->end_charpos;
7508 charpos = IT_STRING_CHARPOS (*it);
7509 bytepos = IT_STRING_BYTEPOS (*it);
7510 }
7511 else
7512 {
7513 stop = it->end_charpos;
7514 charpos = IT_CHARPOS (*it);
7515 bytepos = IT_BYTEPOS (*it);
7516 }
7517 if (it->bidi_it.scan_dir < 0)
7518 stop = -1;
7519 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7520 it->string);
7521 }
7522 }
7523
7524 /* Load IT with the next display element from Lisp string IT->string.
7525 IT->current.string_pos is the current position within the string.
7526 If IT->current.overlay_string_index >= 0, the Lisp string is an
7527 overlay string. */
7528
7529 static int
7530 next_element_from_string (struct it *it)
7531 {
7532 struct text_pos position;
7533
7534 eassert (STRINGP (it->string));
7535 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7536 eassert (IT_STRING_CHARPOS (*it) >= 0);
7537 position = it->current.string_pos;
7538
7539 /* With bidi reordering, the character to display might not be the
7540 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7541 that we were reseat()ed to a new string, whose paragraph
7542 direction is not known. */
7543 if (it->bidi_p && it->bidi_it.first_elt)
7544 {
7545 get_visually_first_element (it);
7546 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7547 }
7548
7549 /* Time to check for invisible text? */
7550 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7551 {
7552 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7553 {
7554 if (!(!it->bidi_p
7555 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7556 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7557 {
7558 /* With bidi non-linear iteration, we could find
7559 ourselves far beyond the last computed stop_charpos,
7560 with several other stop positions in between that we
7561 missed. Scan them all now, in buffer's logical
7562 order, until we find and handle the last stop_charpos
7563 that precedes our current position. */
7564 handle_stop_backwards (it, it->stop_charpos);
7565 return GET_NEXT_DISPLAY_ELEMENT (it);
7566 }
7567 else
7568 {
7569 if (it->bidi_p)
7570 {
7571 /* Take note of the stop position we just moved
7572 across, for when we will move back across it. */
7573 it->prev_stop = it->stop_charpos;
7574 /* If we are at base paragraph embedding level, take
7575 note of the last stop position seen at this
7576 level. */
7577 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7578 it->base_level_stop = it->stop_charpos;
7579 }
7580 handle_stop (it);
7581
7582 /* Since a handler may have changed IT->method, we must
7583 recurse here. */
7584 return GET_NEXT_DISPLAY_ELEMENT (it);
7585 }
7586 }
7587 else if (it->bidi_p
7588 /* If we are before prev_stop, we may have overstepped
7589 on our way backwards a stop_pos, and if so, we need
7590 to handle that stop_pos. */
7591 && IT_STRING_CHARPOS (*it) < it->prev_stop
7592 /* We can sometimes back up for reasons that have nothing
7593 to do with bidi reordering. E.g., compositions. The
7594 code below is only needed when we are above the base
7595 embedding level, so test for that explicitly. */
7596 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7597 {
7598 /* If we lost track of base_level_stop, we have no better
7599 place for handle_stop_backwards to start from than string
7600 beginning. This happens, e.g., when we were reseated to
7601 the previous screenful of text by vertical-motion. */
7602 if (it->base_level_stop <= 0
7603 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7604 it->base_level_stop = 0;
7605 handle_stop_backwards (it, it->base_level_stop);
7606 return GET_NEXT_DISPLAY_ELEMENT (it);
7607 }
7608 }
7609
7610 if (it->current.overlay_string_index >= 0)
7611 {
7612 /* Get the next character from an overlay string. In overlay
7613 strings, there is no field width or padding with spaces to
7614 do. */
7615 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7616 {
7617 it->what = IT_EOB;
7618 return 0;
7619 }
7620 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7621 IT_STRING_BYTEPOS (*it),
7622 it->bidi_it.scan_dir < 0
7623 ? -1
7624 : SCHARS (it->string))
7625 && next_element_from_composition (it))
7626 {
7627 return 1;
7628 }
7629 else if (STRING_MULTIBYTE (it->string))
7630 {
7631 const unsigned char *s = (SDATA (it->string)
7632 + IT_STRING_BYTEPOS (*it));
7633 it->c = string_char_and_length (s, &it->len);
7634 }
7635 else
7636 {
7637 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7638 it->len = 1;
7639 }
7640 }
7641 else
7642 {
7643 /* Get the next character from a Lisp string that is not an
7644 overlay string. Such strings come from the mode line, for
7645 example. We may have to pad with spaces, or truncate the
7646 string. See also next_element_from_c_string. */
7647 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7648 {
7649 it->what = IT_EOB;
7650 return 0;
7651 }
7652 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7653 {
7654 /* Pad with spaces. */
7655 it->c = ' ', it->len = 1;
7656 CHARPOS (position) = BYTEPOS (position) = -1;
7657 }
7658 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7659 IT_STRING_BYTEPOS (*it),
7660 it->bidi_it.scan_dir < 0
7661 ? -1
7662 : it->string_nchars)
7663 && next_element_from_composition (it))
7664 {
7665 return 1;
7666 }
7667 else if (STRING_MULTIBYTE (it->string))
7668 {
7669 const unsigned char *s = (SDATA (it->string)
7670 + IT_STRING_BYTEPOS (*it));
7671 it->c = string_char_and_length (s, &it->len);
7672 }
7673 else
7674 {
7675 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7676 it->len = 1;
7677 }
7678 }
7679
7680 /* Record what we have and where it came from. */
7681 it->what = IT_CHARACTER;
7682 it->object = it->string;
7683 it->position = position;
7684 return 1;
7685 }
7686
7687
7688 /* Load IT with next display element from C string IT->s.
7689 IT->string_nchars is the maximum number of characters to return
7690 from the string. IT->end_charpos may be greater than
7691 IT->string_nchars when this function is called, in which case we
7692 may have to return padding spaces. Value is zero if end of string
7693 reached, including padding spaces. */
7694
7695 static int
7696 next_element_from_c_string (struct it *it)
7697 {
7698 int success_p = 1;
7699
7700 eassert (it->s);
7701 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7702 it->what = IT_CHARACTER;
7703 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7704 it->object = Qnil;
7705
7706 /* With bidi reordering, the character to display might not be the
7707 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7708 we were reseated to a new string, whose paragraph direction is
7709 not known. */
7710 if (it->bidi_p && it->bidi_it.first_elt)
7711 get_visually_first_element (it);
7712
7713 /* IT's position can be greater than IT->string_nchars in case a
7714 field width or precision has been specified when the iterator was
7715 initialized. */
7716 if (IT_CHARPOS (*it) >= it->end_charpos)
7717 {
7718 /* End of the game. */
7719 it->what = IT_EOB;
7720 success_p = 0;
7721 }
7722 else if (IT_CHARPOS (*it) >= it->string_nchars)
7723 {
7724 /* Pad with spaces. */
7725 it->c = ' ', it->len = 1;
7726 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7727 }
7728 else if (it->multibyte_p)
7729 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7730 else
7731 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7732
7733 return success_p;
7734 }
7735
7736
7737 /* Set up IT to return characters from an ellipsis, if appropriate.
7738 The definition of the ellipsis glyphs may come from a display table
7739 entry. This function fills IT with the first glyph from the
7740 ellipsis if an ellipsis is to be displayed. */
7741
7742 static int
7743 next_element_from_ellipsis (struct it *it)
7744 {
7745 if (it->selective_display_ellipsis_p)
7746 setup_for_ellipsis (it, it->len);
7747 else
7748 {
7749 /* The face at the current position may be different from the
7750 face we find after the invisible text. Remember what it
7751 was in IT->saved_face_id, and signal that it's there by
7752 setting face_before_selective_p. */
7753 it->saved_face_id = it->face_id;
7754 it->method = GET_FROM_BUFFER;
7755 it->object = it->w->buffer;
7756 reseat_at_next_visible_line_start (it, 1);
7757 it->face_before_selective_p = 1;
7758 }
7759
7760 return GET_NEXT_DISPLAY_ELEMENT (it);
7761 }
7762
7763
7764 /* Deliver an image display element. The iterator IT is already
7765 filled with image information (done in handle_display_prop). Value
7766 is always 1. */
7767
7768
7769 static int
7770 next_element_from_image (struct it *it)
7771 {
7772 it->what = IT_IMAGE;
7773 it->ignore_overlay_strings_at_pos_p = 0;
7774 return 1;
7775 }
7776
7777
7778 /* Fill iterator IT with next display element from a stretch glyph
7779 property. IT->object is the value of the text property. Value is
7780 always 1. */
7781
7782 static int
7783 next_element_from_stretch (struct it *it)
7784 {
7785 it->what = IT_STRETCH;
7786 return 1;
7787 }
7788
7789 /* Scan backwards from IT's current position until we find a stop
7790 position, or until BEGV. This is called when we find ourself
7791 before both the last known prev_stop and base_level_stop while
7792 reordering bidirectional text. */
7793
7794 static void
7795 compute_stop_pos_backwards (struct it *it)
7796 {
7797 const int SCAN_BACK_LIMIT = 1000;
7798 struct text_pos pos;
7799 struct display_pos save_current = it->current;
7800 struct text_pos save_position = it->position;
7801 ptrdiff_t charpos = IT_CHARPOS (*it);
7802 ptrdiff_t where_we_are = charpos;
7803 ptrdiff_t save_stop_pos = it->stop_charpos;
7804 ptrdiff_t save_end_pos = it->end_charpos;
7805
7806 eassert (NILP (it->string) && !it->s);
7807 eassert (it->bidi_p);
7808 it->bidi_p = 0;
7809 do
7810 {
7811 it->end_charpos = min (charpos + 1, ZV);
7812 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7813 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7814 reseat_1 (it, pos, 0);
7815 compute_stop_pos (it);
7816 /* We must advance forward, right? */
7817 if (it->stop_charpos <= charpos)
7818 emacs_abort ();
7819 }
7820 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7821
7822 if (it->stop_charpos <= where_we_are)
7823 it->prev_stop = it->stop_charpos;
7824 else
7825 it->prev_stop = BEGV;
7826 it->bidi_p = 1;
7827 it->current = save_current;
7828 it->position = save_position;
7829 it->stop_charpos = save_stop_pos;
7830 it->end_charpos = save_end_pos;
7831 }
7832
7833 /* Scan forward from CHARPOS in the current buffer/string, until we
7834 find a stop position > current IT's position. Then handle the stop
7835 position before that. This is called when we bump into a stop
7836 position while reordering bidirectional text. CHARPOS should be
7837 the last previously processed stop_pos (or BEGV/0, if none were
7838 processed yet) whose position is less that IT's current
7839 position. */
7840
7841 static void
7842 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7843 {
7844 int bufp = !STRINGP (it->string);
7845 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7846 struct display_pos save_current = it->current;
7847 struct text_pos save_position = it->position;
7848 struct text_pos pos1;
7849 ptrdiff_t next_stop;
7850
7851 /* Scan in strict logical order. */
7852 eassert (it->bidi_p);
7853 it->bidi_p = 0;
7854 do
7855 {
7856 it->prev_stop = charpos;
7857 if (bufp)
7858 {
7859 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7860 reseat_1 (it, pos1, 0);
7861 }
7862 else
7863 it->current.string_pos = string_pos (charpos, it->string);
7864 compute_stop_pos (it);
7865 /* We must advance forward, right? */
7866 if (it->stop_charpos <= it->prev_stop)
7867 emacs_abort ();
7868 charpos = it->stop_charpos;
7869 }
7870 while (charpos <= where_we_are);
7871
7872 it->bidi_p = 1;
7873 it->current = save_current;
7874 it->position = save_position;
7875 next_stop = it->stop_charpos;
7876 it->stop_charpos = it->prev_stop;
7877 handle_stop (it);
7878 it->stop_charpos = next_stop;
7879 }
7880
7881 /* Load IT with the next display element from current_buffer. Value
7882 is zero if end of buffer reached. IT->stop_charpos is the next
7883 position at which to stop and check for text properties or buffer
7884 end. */
7885
7886 static int
7887 next_element_from_buffer (struct it *it)
7888 {
7889 int success_p = 1;
7890
7891 eassert (IT_CHARPOS (*it) >= BEGV);
7892 eassert (NILP (it->string) && !it->s);
7893 eassert (!it->bidi_p
7894 || (EQ (it->bidi_it.string.lstring, Qnil)
7895 && it->bidi_it.string.s == NULL));
7896
7897 /* With bidi reordering, the character to display might not be the
7898 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7899 we were reseat()ed to a new buffer position, which is potentially
7900 a different paragraph. */
7901 if (it->bidi_p && it->bidi_it.first_elt)
7902 {
7903 get_visually_first_element (it);
7904 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7905 }
7906
7907 if (IT_CHARPOS (*it) >= it->stop_charpos)
7908 {
7909 if (IT_CHARPOS (*it) >= it->end_charpos)
7910 {
7911 int overlay_strings_follow_p;
7912
7913 /* End of the game, except when overlay strings follow that
7914 haven't been returned yet. */
7915 if (it->overlay_strings_at_end_processed_p)
7916 overlay_strings_follow_p = 0;
7917 else
7918 {
7919 it->overlay_strings_at_end_processed_p = 1;
7920 overlay_strings_follow_p = get_overlay_strings (it, 0);
7921 }
7922
7923 if (overlay_strings_follow_p)
7924 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7925 else
7926 {
7927 it->what = IT_EOB;
7928 it->position = it->current.pos;
7929 success_p = 0;
7930 }
7931 }
7932 else if (!(!it->bidi_p
7933 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7934 || IT_CHARPOS (*it) == it->stop_charpos))
7935 {
7936 /* With bidi non-linear iteration, we could find ourselves
7937 far beyond the last computed stop_charpos, with several
7938 other stop positions in between that we missed. Scan
7939 them all now, in buffer's logical order, until we find
7940 and handle the last stop_charpos that precedes our
7941 current position. */
7942 handle_stop_backwards (it, it->stop_charpos);
7943 return GET_NEXT_DISPLAY_ELEMENT (it);
7944 }
7945 else
7946 {
7947 if (it->bidi_p)
7948 {
7949 /* Take note of the stop position we just moved across,
7950 for when we will move back across it. */
7951 it->prev_stop = it->stop_charpos;
7952 /* If we are at base paragraph embedding level, take
7953 note of the last stop position seen at this
7954 level. */
7955 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7956 it->base_level_stop = it->stop_charpos;
7957 }
7958 handle_stop (it);
7959 return GET_NEXT_DISPLAY_ELEMENT (it);
7960 }
7961 }
7962 else if (it->bidi_p
7963 /* If we are before prev_stop, we may have overstepped on
7964 our way backwards a stop_pos, and if so, we need to
7965 handle that stop_pos. */
7966 && IT_CHARPOS (*it) < it->prev_stop
7967 /* We can sometimes back up for reasons that have nothing
7968 to do with bidi reordering. E.g., compositions. The
7969 code below is only needed when we are above the base
7970 embedding level, so test for that explicitly. */
7971 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7972 {
7973 if (it->base_level_stop <= 0
7974 || IT_CHARPOS (*it) < it->base_level_stop)
7975 {
7976 /* If we lost track of base_level_stop, we need to find
7977 prev_stop by looking backwards. This happens, e.g., when
7978 we were reseated to the previous screenful of text by
7979 vertical-motion. */
7980 it->base_level_stop = BEGV;
7981 compute_stop_pos_backwards (it);
7982 handle_stop_backwards (it, it->prev_stop);
7983 }
7984 else
7985 handle_stop_backwards (it, it->base_level_stop);
7986 return GET_NEXT_DISPLAY_ELEMENT (it);
7987 }
7988 else
7989 {
7990 /* No face changes, overlays etc. in sight, so just return a
7991 character from current_buffer. */
7992 unsigned char *p;
7993 ptrdiff_t stop;
7994
7995 /* Maybe run the redisplay end trigger hook. Performance note:
7996 This doesn't seem to cost measurable time. */
7997 if (it->redisplay_end_trigger_charpos
7998 && it->glyph_row
7999 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8000 run_redisplay_end_trigger_hook (it);
8001
8002 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8003 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8004 stop)
8005 && next_element_from_composition (it))
8006 {
8007 return 1;
8008 }
8009
8010 /* Get the next character, maybe multibyte. */
8011 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8012 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8013 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8014 else
8015 it->c = *p, it->len = 1;
8016
8017 /* Record what we have and where it came from. */
8018 it->what = IT_CHARACTER;
8019 it->object = it->w->buffer;
8020 it->position = it->current.pos;
8021
8022 /* Normally we return the character found above, except when we
8023 really want to return an ellipsis for selective display. */
8024 if (it->selective)
8025 {
8026 if (it->c == '\n')
8027 {
8028 /* A value of selective > 0 means hide lines indented more
8029 than that number of columns. */
8030 if (it->selective > 0
8031 && IT_CHARPOS (*it) + 1 < ZV
8032 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8033 IT_BYTEPOS (*it) + 1,
8034 it->selective))
8035 {
8036 success_p = next_element_from_ellipsis (it);
8037 it->dpvec_char_len = -1;
8038 }
8039 }
8040 else if (it->c == '\r' && it->selective == -1)
8041 {
8042 /* A value of selective == -1 means that everything from the
8043 CR to the end of the line is invisible, with maybe an
8044 ellipsis displayed for it. */
8045 success_p = next_element_from_ellipsis (it);
8046 it->dpvec_char_len = -1;
8047 }
8048 }
8049 }
8050
8051 /* Value is zero if end of buffer reached. */
8052 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8053 return success_p;
8054 }
8055
8056
8057 /* Run the redisplay end trigger hook for IT. */
8058
8059 static void
8060 run_redisplay_end_trigger_hook (struct it *it)
8061 {
8062 Lisp_Object args[3];
8063
8064 /* IT->glyph_row should be non-null, i.e. we should be actually
8065 displaying something, or otherwise we should not run the hook. */
8066 eassert (it->glyph_row);
8067
8068 /* Set up hook arguments. */
8069 args[0] = Qredisplay_end_trigger_functions;
8070 args[1] = it->window;
8071 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8072 it->redisplay_end_trigger_charpos = 0;
8073
8074 /* Since we are *trying* to run these functions, don't try to run
8075 them again, even if they get an error. */
8076 wset_redisplay_end_trigger (it->w, Qnil);
8077 Frun_hook_with_args (3, args);
8078
8079 /* Notice if it changed the face of the character we are on. */
8080 handle_face_prop (it);
8081 }
8082
8083
8084 /* Deliver a composition display element. Unlike the other
8085 next_element_from_XXX, this function is not registered in the array
8086 get_next_element[]. It is called from next_element_from_buffer and
8087 next_element_from_string when necessary. */
8088
8089 static int
8090 next_element_from_composition (struct it *it)
8091 {
8092 it->what = IT_COMPOSITION;
8093 it->len = it->cmp_it.nbytes;
8094 if (STRINGP (it->string))
8095 {
8096 if (it->c < 0)
8097 {
8098 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8099 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8100 return 0;
8101 }
8102 it->position = it->current.string_pos;
8103 it->object = it->string;
8104 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8105 IT_STRING_BYTEPOS (*it), it->string);
8106 }
8107 else
8108 {
8109 if (it->c < 0)
8110 {
8111 IT_CHARPOS (*it) += it->cmp_it.nchars;
8112 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8113 if (it->bidi_p)
8114 {
8115 if (it->bidi_it.new_paragraph)
8116 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8117 /* Resync the bidi iterator with IT's new position.
8118 FIXME: this doesn't support bidirectional text. */
8119 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8120 bidi_move_to_visually_next (&it->bidi_it);
8121 }
8122 return 0;
8123 }
8124 it->position = it->current.pos;
8125 it->object = it->w->buffer;
8126 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8127 IT_BYTEPOS (*it), Qnil);
8128 }
8129 return 1;
8130 }
8131
8132
8133 \f
8134 /***********************************************************************
8135 Moving an iterator without producing glyphs
8136 ***********************************************************************/
8137
8138 /* Check if iterator is at a position corresponding to a valid buffer
8139 position after some move_it_ call. */
8140
8141 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8142 ((it)->method == GET_FROM_STRING \
8143 ? IT_STRING_CHARPOS (*it) == 0 \
8144 : 1)
8145
8146
8147 /* Move iterator IT to a specified buffer or X position within one
8148 line on the display without producing glyphs.
8149
8150 OP should be a bit mask including some or all of these bits:
8151 MOVE_TO_X: Stop upon reaching x-position TO_X.
8152 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8153 Regardless of OP's value, stop upon reaching the end of the display line.
8154
8155 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8156 This means, in particular, that TO_X includes window's horizontal
8157 scroll amount.
8158
8159 The return value has several possible values that
8160 say what condition caused the scan to stop:
8161
8162 MOVE_POS_MATCH_OR_ZV
8163 - when TO_POS or ZV was reached.
8164
8165 MOVE_X_REACHED
8166 -when TO_X was reached before TO_POS or ZV were reached.
8167
8168 MOVE_LINE_CONTINUED
8169 - when we reached the end of the display area and the line must
8170 be continued.
8171
8172 MOVE_LINE_TRUNCATED
8173 - when we reached the end of the display area and the line is
8174 truncated.
8175
8176 MOVE_NEWLINE_OR_CR
8177 - when we stopped at a line end, i.e. a newline or a CR and selective
8178 display is on. */
8179
8180 static enum move_it_result
8181 move_it_in_display_line_to (struct it *it,
8182 ptrdiff_t to_charpos, int to_x,
8183 enum move_operation_enum op)
8184 {
8185 enum move_it_result result = MOVE_UNDEFINED;
8186 struct glyph_row *saved_glyph_row;
8187 struct it wrap_it, atpos_it, atx_it, ppos_it;
8188 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8189 void *ppos_data = NULL;
8190 int may_wrap = 0;
8191 enum it_method prev_method = it->method;
8192 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8193 int saw_smaller_pos = prev_pos < to_charpos;
8194
8195 /* Don't produce glyphs in produce_glyphs. */
8196 saved_glyph_row = it->glyph_row;
8197 it->glyph_row = NULL;
8198
8199 /* Use wrap_it to save a copy of IT wherever a word wrap could
8200 occur. Use atpos_it to save a copy of IT at the desired buffer
8201 position, if found, so that we can scan ahead and check if the
8202 word later overshoots the window edge. Use atx_it similarly, for
8203 pixel positions. */
8204 wrap_it.sp = -1;
8205 atpos_it.sp = -1;
8206 atx_it.sp = -1;
8207
8208 /* Use ppos_it under bidi reordering to save a copy of IT for the
8209 position > CHARPOS that is the closest to CHARPOS. We restore
8210 that position in IT when we have scanned the entire display line
8211 without finding a match for CHARPOS and all the character
8212 positions are greater than CHARPOS. */
8213 if (it->bidi_p)
8214 {
8215 SAVE_IT (ppos_it, *it, ppos_data);
8216 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8217 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8218 SAVE_IT (ppos_it, *it, ppos_data);
8219 }
8220
8221 #define BUFFER_POS_REACHED_P() \
8222 ((op & MOVE_TO_POS) != 0 \
8223 && BUFFERP (it->object) \
8224 && (IT_CHARPOS (*it) == to_charpos \
8225 || ((!it->bidi_p \
8226 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8227 && IT_CHARPOS (*it) > to_charpos) \
8228 || (it->what == IT_COMPOSITION \
8229 && ((IT_CHARPOS (*it) > to_charpos \
8230 && to_charpos >= it->cmp_it.charpos) \
8231 || (IT_CHARPOS (*it) < to_charpos \
8232 && to_charpos <= it->cmp_it.charpos)))) \
8233 && (it->method == GET_FROM_BUFFER \
8234 || (it->method == GET_FROM_DISPLAY_VECTOR \
8235 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8236
8237 /* If there's a line-/wrap-prefix, handle it. */
8238 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8239 && it->current_y < it->last_visible_y)
8240 handle_line_prefix (it);
8241
8242 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8243 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8244
8245 while (1)
8246 {
8247 int x, i, ascent = 0, descent = 0;
8248
8249 /* Utility macro to reset an iterator with x, ascent, and descent. */
8250 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8251 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8252 (IT)->max_descent = descent)
8253
8254 /* Stop if we move beyond TO_CHARPOS (after an image or a
8255 display string or stretch glyph). */
8256 if ((op & MOVE_TO_POS) != 0
8257 && BUFFERP (it->object)
8258 && it->method == GET_FROM_BUFFER
8259 && (((!it->bidi_p
8260 /* When the iterator is at base embedding level, we
8261 are guaranteed that characters are delivered for
8262 display in strictly increasing order of their
8263 buffer positions. */
8264 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8265 && IT_CHARPOS (*it) > to_charpos)
8266 || (it->bidi_p
8267 && (prev_method == GET_FROM_IMAGE
8268 || prev_method == GET_FROM_STRETCH
8269 || prev_method == GET_FROM_STRING)
8270 /* Passed TO_CHARPOS from left to right. */
8271 && ((prev_pos < to_charpos
8272 && IT_CHARPOS (*it) > to_charpos)
8273 /* Passed TO_CHARPOS from right to left. */
8274 || (prev_pos > to_charpos
8275 && IT_CHARPOS (*it) < to_charpos)))))
8276 {
8277 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8278 {
8279 result = MOVE_POS_MATCH_OR_ZV;
8280 break;
8281 }
8282 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8283 /* If wrap_it is valid, the current position might be in a
8284 word that is wrapped. So, save the iterator in
8285 atpos_it and continue to see if wrapping happens. */
8286 SAVE_IT (atpos_it, *it, atpos_data);
8287 }
8288
8289 /* Stop when ZV reached.
8290 We used to stop here when TO_CHARPOS reached as well, but that is
8291 too soon if this glyph does not fit on this line. So we handle it
8292 explicitly below. */
8293 if (!get_next_display_element (it))
8294 {
8295 result = MOVE_POS_MATCH_OR_ZV;
8296 break;
8297 }
8298
8299 if (it->line_wrap == TRUNCATE)
8300 {
8301 if (BUFFER_POS_REACHED_P ())
8302 {
8303 result = MOVE_POS_MATCH_OR_ZV;
8304 break;
8305 }
8306 }
8307 else
8308 {
8309 if (it->line_wrap == WORD_WRAP)
8310 {
8311 if (IT_DISPLAYING_WHITESPACE (it))
8312 may_wrap = 1;
8313 else if (may_wrap)
8314 {
8315 /* We have reached a glyph that follows one or more
8316 whitespace characters. If the position is
8317 already found, we are done. */
8318 if (atpos_it.sp >= 0)
8319 {
8320 RESTORE_IT (it, &atpos_it, atpos_data);
8321 result = MOVE_POS_MATCH_OR_ZV;
8322 goto done;
8323 }
8324 if (atx_it.sp >= 0)
8325 {
8326 RESTORE_IT (it, &atx_it, atx_data);
8327 result = MOVE_X_REACHED;
8328 goto done;
8329 }
8330 /* Otherwise, we can wrap here. */
8331 SAVE_IT (wrap_it, *it, wrap_data);
8332 may_wrap = 0;
8333 }
8334 }
8335 }
8336
8337 /* Remember the line height for the current line, in case
8338 the next element doesn't fit on the line. */
8339 ascent = it->max_ascent;
8340 descent = it->max_descent;
8341
8342 /* The call to produce_glyphs will get the metrics of the
8343 display element IT is loaded with. Record the x-position
8344 before this display element, in case it doesn't fit on the
8345 line. */
8346 x = it->current_x;
8347
8348 PRODUCE_GLYPHS (it);
8349
8350 if (it->area != TEXT_AREA)
8351 {
8352 prev_method = it->method;
8353 if (it->method == GET_FROM_BUFFER)
8354 prev_pos = IT_CHARPOS (*it);
8355 set_iterator_to_next (it, 1);
8356 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8357 SET_TEXT_POS (this_line_min_pos,
8358 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8359 if (it->bidi_p
8360 && (op & MOVE_TO_POS)
8361 && IT_CHARPOS (*it) > to_charpos
8362 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8363 SAVE_IT (ppos_it, *it, ppos_data);
8364 continue;
8365 }
8366
8367 /* The number of glyphs we get back in IT->nglyphs will normally
8368 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8369 character on a terminal frame, or (iii) a line end. For the
8370 second case, IT->nglyphs - 1 padding glyphs will be present.
8371 (On X frames, there is only one glyph produced for a
8372 composite character.)
8373
8374 The behavior implemented below means, for continuation lines,
8375 that as many spaces of a TAB as fit on the current line are
8376 displayed there. For terminal frames, as many glyphs of a
8377 multi-glyph character are displayed in the current line, too.
8378 This is what the old redisplay code did, and we keep it that
8379 way. Under X, the whole shape of a complex character must
8380 fit on the line or it will be completely displayed in the
8381 next line.
8382
8383 Note that both for tabs and padding glyphs, all glyphs have
8384 the same width. */
8385 if (it->nglyphs)
8386 {
8387 /* More than one glyph or glyph doesn't fit on line. All
8388 glyphs have the same width. */
8389 int single_glyph_width = it->pixel_width / it->nglyphs;
8390 int new_x;
8391 int x_before_this_char = x;
8392 int hpos_before_this_char = it->hpos;
8393
8394 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8395 {
8396 new_x = x + single_glyph_width;
8397
8398 /* We want to leave anything reaching TO_X to the caller. */
8399 if ((op & MOVE_TO_X) && new_x > to_x)
8400 {
8401 if (BUFFER_POS_REACHED_P ())
8402 {
8403 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8404 goto buffer_pos_reached;
8405 if (atpos_it.sp < 0)
8406 {
8407 SAVE_IT (atpos_it, *it, atpos_data);
8408 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8409 }
8410 }
8411 else
8412 {
8413 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8414 {
8415 it->current_x = x;
8416 result = MOVE_X_REACHED;
8417 break;
8418 }
8419 if (atx_it.sp < 0)
8420 {
8421 SAVE_IT (atx_it, *it, atx_data);
8422 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8423 }
8424 }
8425 }
8426
8427 if (/* Lines are continued. */
8428 it->line_wrap != TRUNCATE
8429 && (/* And glyph doesn't fit on the line. */
8430 new_x > it->last_visible_x
8431 /* Or it fits exactly and we're on a window
8432 system frame. */
8433 || (new_x == it->last_visible_x
8434 && FRAME_WINDOW_P (it->f)
8435 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8436 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8437 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8438 {
8439 if (/* IT->hpos == 0 means the very first glyph
8440 doesn't fit on the line, e.g. a wide image. */
8441 it->hpos == 0
8442 || (new_x == it->last_visible_x
8443 && FRAME_WINDOW_P (it->f)))
8444 {
8445 ++it->hpos;
8446 it->current_x = new_x;
8447
8448 /* The character's last glyph just barely fits
8449 in this row. */
8450 if (i == it->nglyphs - 1)
8451 {
8452 /* If this is the destination position,
8453 return a position *before* it in this row,
8454 now that we know it fits in this row. */
8455 if (BUFFER_POS_REACHED_P ())
8456 {
8457 if (it->line_wrap != WORD_WRAP
8458 || wrap_it.sp < 0)
8459 {
8460 it->hpos = hpos_before_this_char;
8461 it->current_x = x_before_this_char;
8462 result = MOVE_POS_MATCH_OR_ZV;
8463 break;
8464 }
8465 if (it->line_wrap == WORD_WRAP
8466 && atpos_it.sp < 0)
8467 {
8468 SAVE_IT (atpos_it, *it, atpos_data);
8469 atpos_it.current_x = x_before_this_char;
8470 atpos_it.hpos = hpos_before_this_char;
8471 }
8472 }
8473
8474 prev_method = it->method;
8475 if (it->method == GET_FROM_BUFFER)
8476 prev_pos = IT_CHARPOS (*it);
8477 set_iterator_to_next (it, 1);
8478 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8479 SET_TEXT_POS (this_line_min_pos,
8480 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8481 /* On graphical terminals, newlines may
8482 "overflow" into the fringe if
8483 overflow-newline-into-fringe is non-nil.
8484 On text terminals, and on graphical
8485 terminals with no right margin, newlines
8486 may overflow into the last glyph on the
8487 display line.*/
8488 if (!FRAME_WINDOW_P (it->f)
8489 || ((it->bidi_p
8490 && it->bidi_it.paragraph_dir == R2L)
8491 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8492 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8493 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8494 {
8495 if (!get_next_display_element (it))
8496 {
8497 result = MOVE_POS_MATCH_OR_ZV;
8498 break;
8499 }
8500 if (BUFFER_POS_REACHED_P ())
8501 {
8502 if (ITERATOR_AT_END_OF_LINE_P (it))
8503 result = MOVE_POS_MATCH_OR_ZV;
8504 else
8505 result = MOVE_LINE_CONTINUED;
8506 break;
8507 }
8508 if (ITERATOR_AT_END_OF_LINE_P (it))
8509 {
8510 result = MOVE_NEWLINE_OR_CR;
8511 break;
8512 }
8513 }
8514 }
8515 }
8516 else
8517 IT_RESET_X_ASCENT_DESCENT (it);
8518
8519 if (wrap_it.sp >= 0)
8520 {
8521 RESTORE_IT (it, &wrap_it, wrap_data);
8522 atpos_it.sp = -1;
8523 atx_it.sp = -1;
8524 }
8525
8526 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8527 IT_CHARPOS (*it)));
8528 result = MOVE_LINE_CONTINUED;
8529 break;
8530 }
8531
8532 if (BUFFER_POS_REACHED_P ())
8533 {
8534 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8535 goto buffer_pos_reached;
8536 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8537 {
8538 SAVE_IT (atpos_it, *it, atpos_data);
8539 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8540 }
8541 }
8542
8543 if (new_x > it->first_visible_x)
8544 {
8545 /* Glyph is visible. Increment number of glyphs that
8546 would be displayed. */
8547 ++it->hpos;
8548 }
8549 }
8550
8551 if (result != MOVE_UNDEFINED)
8552 break;
8553 }
8554 else if (BUFFER_POS_REACHED_P ())
8555 {
8556 buffer_pos_reached:
8557 IT_RESET_X_ASCENT_DESCENT (it);
8558 result = MOVE_POS_MATCH_OR_ZV;
8559 break;
8560 }
8561 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8562 {
8563 /* Stop when TO_X specified and reached. This check is
8564 necessary here because of lines consisting of a line end,
8565 only. The line end will not produce any glyphs and we
8566 would never get MOVE_X_REACHED. */
8567 eassert (it->nglyphs == 0);
8568 result = MOVE_X_REACHED;
8569 break;
8570 }
8571
8572 /* Is this a line end? If yes, we're done. */
8573 if (ITERATOR_AT_END_OF_LINE_P (it))
8574 {
8575 /* If we are past TO_CHARPOS, but never saw any character
8576 positions smaller than TO_CHARPOS, return
8577 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8578 did. */
8579 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8580 {
8581 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8582 {
8583 if (IT_CHARPOS (ppos_it) < ZV)
8584 {
8585 RESTORE_IT (it, &ppos_it, ppos_data);
8586 result = MOVE_POS_MATCH_OR_ZV;
8587 }
8588 else
8589 goto buffer_pos_reached;
8590 }
8591 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8592 && IT_CHARPOS (*it) > to_charpos)
8593 goto buffer_pos_reached;
8594 else
8595 result = MOVE_NEWLINE_OR_CR;
8596 }
8597 else
8598 result = MOVE_NEWLINE_OR_CR;
8599 break;
8600 }
8601
8602 prev_method = it->method;
8603 if (it->method == GET_FROM_BUFFER)
8604 prev_pos = IT_CHARPOS (*it);
8605 /* The current display element has been consumed. Advance
8606 to the next. */
8607 set_iterator_to_next (it, 1);
8608 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8609 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8610 if (IT_CHARPOS (*it) < to_charpos)
8611 saw_smaller_pos = 1;
8612 if (it->bidi_p
8613 && (op & MOVE_TO_POS)
8614 && IT_CHARPOS (*it) >= to_charpos
8615 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8616 SAVE_IT (ppos_it, *it, ppos_data);
8617
8618 /* Stop if lines are truncated and IT's current x-position is
8619 past the right edge of the window now. */
8620 if (it->line_wrap == TRUNCATE
8621 && it->current_x >= it->last_visible_x)
8622 {
8623 if (!FRAME_WINDOW_P (it->f)
8624 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8625 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8626 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8627 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8628 {
8629 int at_eob_p = 0;
8630
8631 if ((at_eob_p = !get_next_display_element (it))
8632 || BUFFER_POS_REACHED_P ()
8633 /* If we are past TO_CHARPOS, but never saw any
8634 character positions smaller than TO_CHARPOS,
8635 return MOVE_POS_MATCH_OR_ZV, like the
8636 unidirectional display did. */
8637 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8638 && !saw_smaller_pos
8639 && IT_CHARPOS (*it) > to_charpos))
8640 {
8641 if (it->bidi_p
8642 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8643 RESTORE_IT (it, &ppos_it, ppos_data);
8644 result = MOVE_POS_MATCH_OR_ZV;
8645 break;
8646 }
8647 if (ITERATOR_AT_END_OF_LINE_P (it))
8648 {
8649 result = MOVE_NEWLINE_OR_CR;
8650 break;
8651 }
8652 }
8653 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8654 && !saw_smaller_pos
8655 && IT_CHARPOS (*it) > to_charpos)
8656 {
8657 if (IT_CHARPOS (ppos_it) < ZV)
8658 RESTORE_IT (it, &ppos_it, ppos_data);
8659 result = MOVE_POS_MATCH_OR_ZV;
8660 break;
8661 }
8662 result = MOVE_LINE_TRUNCATED;
8663 break;
8664 }
8665 #undef IT_RESET_X_ASCENT_DESCENT
8666 }
8667
8668 #undef BUFFER_POS_REACHED_P
8669
8670 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8671 restore the saved iterator. */
8672 if (atpos_it.sp >= 0)
8673 RESTORE_IT (it, &atpos_it, atpos_data);
8674 else if (atx_it.sp >= 0)
8675 RESTORE_IT (it, &atx_it, atx_data);
8676
8677 done:
8678
8679 if (atpos_data)
8680 bidi_unshelve_cache (atpos_data, 1);
8681 if (atx_data)
8682 bidi_unshelve_cache (atx_data, 1);
8683 if (wrap_data)
8684 bidi_unshelve_cache (wrap_data, 1);
8685 if (ppos_data)
8686 bidi_unshelve_cache (ppos_data, 1);
8687
8688 /* Restore the iterator settings altered at the beginning of this
8689 function. */
8690 it->glyph_row = saved_glyph_row;
8691 return result;
8692 }
8693
8694 /* For external use. */
8695 void
8696 move_it_in_display_line (struct it *it,
8697 ptrdiff_t to_charpos, int to_x,
8698 enum move_operation_enum op)
8699 {
8700 if (it->line_wrap == WORD_WRAP
8701 && (op & MOVE_TO_X))
8702 {
8703 struct it save_it;
8704 void *save_data = NULL;
8705 int skip;
8706
8707 SAVE_IT (save_it, *it, save_data);
8708 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8709 /* When word-wrap is on, TO_X may lie past the end
8710 of a wrapped line. Then it->current is the
8711 character on the next line, so backtrack to the
8712 space before the wrap point. */
8713 if (skip == MOVE_LINE_CONTINUED)
8714 {
8715 int prev_x = max (it->current_x - 1, 0);
8716 RESTORE_IT (it, &save_it, save_data);
8717 move_it_in_display_line_to
8718 (it, -1, prev_x, MOVE_TO_X);
8719 }
8720 else
8721 bidi_unshelve_cache (save_data, 1);
8722 }
8723 else
8724 move_it_in_display_line_to (it, to_charpos, to_x, op);
8725 }
8726
8727
8728 /* Move IT forward until it satisfies one or more of the criteria in
8729 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8730
8731 OP is a bit-mask that specifies where to stop, and in particular,
8732 which of those four position arguments makes a difference. See the
8733 description of enum move_operation_enum.
8734
8735 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8736 screen line, this function will set IT to the next position that is
8737 displayed to the right of TO_CHARPOS on the screen. */
8738
8739 void
8740 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8741 {
8742 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8743 int line_height, line_start_x = 0, reached = 0;
8744 void *backup_data = NULL;
8745
8746 for (;;)
8747 {
8748 if (op & MOVE_TO_VPOS)
8749 {
8750 /* If no TO_CHARPOS and no TO_X specified, stop at the
8751 start of the line TO_VPOS. */
8752 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8753 {
8754 if (it->vpos == to_vpos)
8755 {
8756 reached = 1;
8757 break;
8758 }
8759 else
8760 skip = move_it_in_display_line_to (it, -1, -1, 0);
8761 }
8762 else
8763 {
8764 /* TO_VPOS >= 0 means stop at TO_X in the line at
8765 TO_VPOS, or at TO_POS, whichever comes first. */
8766 if (it->vpos == to_vpos)
8767 {
8768 reached = 2;
8769 break;
8770 }
8771
8772 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8773
8774 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8775 {
8776 reached = 3;
8777 break;
8778 }
8779 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8780 {
8781 /* We have reached TO_X but not in the line we want. */
8782 skip = move_it_in_display_line_to (it, to_charpos,
8783 -1, MOVE_TO_POS);
8784 if (skip == MOVE_POS_MATCH_OR_ZV)
8785 {
8786 reached = 4;
8787 break;
8788 }
8789 }
8790 }
8791 }
8792 else if (op & MOVE_TO_Y)
8793 {
8794 struct it it_backup;
8795
8796 if (it->line_wrap == WORD_WRAP)
8797 SAVE_IT (it_backup, *it, backup_data);
8798
8799 /* TO_Y specified means stop at TO_X in the line containing
8800 TO_Y---or at TO_CHARPOS if this is reached first. The
8801 problem is that we can't really tell whether the line
8802 contains TO_Y before we have completely scanned it, and
8803 this may skip past TO_X. What we do is to first scan to
8804 TO_X.
8805
8806 If TO_X is not specified, use a TO_X of zero. The reason
8807 is to make the outcome of this function more predictable.
8808 If we didn't use TO_X == 0, we would stop at the end of
8809 the line which is probably not what a caller would expect
8810 to happen. */
8811 skip = move_it_in_display_line_to
8812 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8813 (MOVE_TO_X | (op & MOVE_TO_POS)));
8814
8815 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8816 if (skip == MOVE_POS_MATCH_OR_ZV)
8817 reached = 5;
8818 else if (skip == MOVE_X_REACHED)
8819 {
8820 /* If TO_X was reached, we want to know whether TO_Y is
8821 in the line. We know this is the case if the already
8822 scanned glyphs make the line tall enough. Otherwise,
8823 we must check by scanning the rest of the line. */
8824 line_height = it->max_ascent + it->max_descent;
8825 if (to_y >= it->current_y
8826 && to_y < it->current_y + line_height)
8827 {
8828 reached = 6;
8829 break;
8830 }
8831 SAVE_IT (it_backup, *it, backup_data);
8832 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8833 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8834 op & MOVE_TO_POS);
8835 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8836 line_height = it->max_ascent + it->max_descent;
8837 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8838
8839 if (to_y >= it->current_y
8840 && to_y < it->current_y + line_height)
8841 {
8842 /* If TO_Y is in this line and TO_X was reached
8843 above, we scanned too far. We have to restore
8844 IT's settings to the ones before skipping. But
8845 keep the more accurate values of max_ascent and
8846 max_descent we've found while skipping the rest
8847 of the line, for the sake of callers, such as
8848 pos_visible_p, that need to know the line
8849 height. */
8850 int max_ascent = it->max_ascent;
8851 int max_descent = it->max_descent;
8852
8853 RESTORE_IT (it, &it_backup, backup_data);
8854 it->max_ascent = max_ascent;
8855 it->max_descent = max_descent;
8856 reached = 6;
8857 }
8858 else
8859 {
8860 skip = skip2;
8861 if (skip == MOVE_POS_MATCH_OR_ZV)
8862 reached = 7;
8863 }
8864 }
8865 else
8866 {
8867 /* Check whether TO_Y is in this line. */
8868 line_height = it->max_ascent + it->max_descent;
8869 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8870
8871 if (to_y >= it->current_y
8872 && to_y < it->current_y + line_height)
8873 {
8874 /* When word-wrap is on, TO_X may lie past the end
8875 of a wrapped line. Then it->current is the
8876 character on the next line, so backtrack to the
8877 space before the wrap point. */
8878 if (skip == MOVE_LINE_CONTINUED
8879 && it->line_wrap == WORD_WRAP)
8880 {
8881 int prev_x = max (it->current_x - 1, 0);
8882 RESTORE_IT (it, &it_backup, backup_data);
8883 skip = move_it_in_display_line_to
8884 (it, -1, prev_x, MOVE_TO_X);
8885 }
8886 reached = 6;
8887 }
8888 }
8889
8890 if (reached)
8891 break;
8892 }
8893 else if (BUFFERP (it->object)
8894 && (it->method == GET_FROM_BUFFER
8895 || it->method == GET_FROM_STRETCH)
8896 && IT_CHARPOS (*it) >= to_charpos
8897 /* Under bidi iteration, a call to set_iterator_to_next
8898 can scan far beyond to_charpos if the initial
8899 portion of the next line needs to be reordered. In
8900 that case, give move_it_in_display_line_to another
8901 chance below. */
8902 && !(it->bidi_p
8903 && it->bidi_it.scan_dir == -1))
8904 skip = MOVE_POS_MATCH_OR_ZV;
8905 else
8906 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8907
8908 switch (skip)
8909 {
8910 case MOVE_POS_MATCH_OR_ZV:
8911 reached = 8;
8912 goto out;
8913
8914 case MOVE_NEWLINE_OR_CR:
8915 set_iterator_to_next (it, 1);
8916 it->continuation_lines_width = 0;
8917 break;
8918
8919 case MOVE_LINE_TRUNCATED:
8920 it->continuation_lines_width = 0;
8921 reseat_at_next_visible_line_start (it, 0);
8922 if ((op & MOVE_TO_POS) != 0
8923 && IT_CHARPOS (*it) > to_charpos)
8924 {
8925 reached = 9;
8926 goto out;
8927 }
8928 break;
8929
8930 case MOVE_LINE_CONTINUED:
8931 /* For continued lines ending in a tab, some of the glyphs
8932 associated with the tab are displayed on the current
8933 line. Since it->current_x does not include these glyphs,
8934 we use it->last_visible_x instead. */
8935 if (it->c == '\t')
8936 {
8937 it->continuation_lines_width += it->last_visible_x;
8938 /* When moving by vpos, ensure that the iterator really
8939 advances to the next line (bug#847, bug#969). Fixme:
8940 do we need to do this in other circumstances? */
8941 if (it->current_x != it->last_visible_x
8942 && (op & MOVE_TO_VPOS)
8943 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8944 {
8945 line_start_x = it->current_x + it->pixel_width
8946 - it->last_visible_x;
8947 set_iterator_to_next (it, 0);
8948 }
8949 }
8950 else
8951 it->continuation_lines_width += it->current_x;
8952 break;
8953
8954 default:
8955 emacs_abort ();
8956 }
8957
8958 /* Reset/increment for the next run. */
8959 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8960 it->current_x = line_start_x;
8961 line_start_x = 0;
8962 it->hpos = 0;
8963 it->current_y += it->max_ascent + it->max_descent;
8964 ++it->vpos;
8965 last_height = it->max_ascent + it->max_descent;
8966 last_max_ascent = it->max_ascent;
8967 it->max_ascent = it->max_descent = 0;
8968 }
8969
8970 out:
8971
8972 /* On text terminals, we may stop at the end of a line in the middle
8973 of a multi-character glyph. If the glyph itself is continued,
8974 i.e. it is actually displayed on the next line, don't treat this
8975 stopping point as valid; move to the next line instead (unless
8976 that brings us offscreen). */
8977 if (!FRAME_WINDOW_P (it->f)
8978 && op & MOVE_TO_POS
8979 && IT_CHARPOS (*it) == to_charpos
8980 && it->what == IT_CHARACTER
8981 && it->nglyphs > 1
8982 && it->line_wrap == WINDOW_WRAP
8983 && it->current_x == it->last_visible_x - 1
8984 && it->c != '\n'
8985 && it->c != '\t'
8986 && it->vpos < XFASTINT (it->w->window_end_vpos))
8987 {
8988 it->continuation_lines_width += it->current_x;
8989 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8990 it->current_y += it->max_ascent + it->max_descent;
8991 ++it->vpos;
8992 last_height = it->max_ascent + it->max_descent;
8993 last_max_ascent = it->max_ascent;
8994 }
8995
8996 if (backup_data)
8997 bidi_unshelve_cache (backup_data, 1);
8998
8999 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9000 }
9001
9002
9003 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9004
9005 If DY > 0, move IT backward at least that many pixels. DY = 0
9006 means move IT backward to the preceding line start or BEGV. This
9007 function may move over more than DY pixels if IT->current_y - DY
9008 ends up in the middle of a line; in this case IT->current_y will be
9009 set to the top of the line moved to. */
9010
9011 void
9012 move_it_vertically_backward (struct it *it, int dy)
9013 {
9014 int nlines, h;
9015 struct it it2, it3;
9016 void *it2data = NULL, *it3data = NULL;
9017 ptrdiff_t start_pos;
9018
9019 move_further_back:
9020 eassert (dy >= 0);
9021
9022 start_pos = IT_CHARPOS (*it);
9023
9024 /* Estimate how many newlines we must move back. */
9025 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9026
9027 /* Set the iterator's position that many lines back. */
9028 while (nlines-- && IT_CHARPOS (*it) > BEGV)
9029 back_to_previous_visible_line_start (it);
9030
9031 /* Reseat the iterator here. When moving backward, we don't want
9032 reseat to skip forward over invisible text, set up the iterator
9033 to deliver from overlay strings at the new position etc. So,
9034 use reseat_1 here. */
9035 reseat_1 (it, it->current.pos, 1);
9036
9037 /* We are now surely at a line start. */
9038 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9039 reordering is in effect. */
9040 it->continuation_lines_width = 0;
9041
9042 /* Move forward and see what y-distance we moved. First move to the
9043 start of the next line so that we get its height. We need this
9044 height to be able to tell whether we reached the specified
9045 y-distance. */
9046 SAVE_IT (it2, *it, it2data);
9047 it2.max_ascent = it2.max_descent = 0;
9048 do
9049 {
9050 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9051 MOVE_TO_POS | MOVE_TO_VPOS);
9052 }
9053 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9054 /* If we are in a display string which starts at START_POS,
9055 and that display string includes a newline, and we are
9056 right after that newline (i.e. at the beginning of a
9057 display line), exit the loop, because otherwise we will
9058 infloop, since move_it_to will see that it is already at
9059 START_POS and will not move. */
9060 || (it2.method == GET_FROM_STRING
9061 && IT_CHARPOS (it2) == start_pos
9062 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9063 eassert (IT_CHARPOS (*it) >= BEGV);
9064 SAVE_IT (it3, it2, it3data);
9065
9066 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9067 eassert (IT_CHARPOS (*it) >= BEGV);
9068 /* H is the actual vertical distance from the position in *IT
9069 and the starting position. */
9070 h = it2.current_y - it->current_y;
9071 /* NLINES is the distance in number of lines. */
9072 nlines = it2.vpos - it->vpos;
9073
9074 /* Correct IT's y and vpos position
9075 so that they are relative to the starting point. */
9076 it->vpos -= nlines;
9077 it->current_y -= h;
9078
9079 if (dy == 0)
9080 {
9081 /* DY == 0 means move to the start of the screen line. The
9082 value of nlines is > 0 if continuation lines were involved,
9083 or if the original IT position was at start of a line. */
9084 RESTORE_IT (it, it, it2data);
9085 if (nlines > 0)
9086 move_it_by_lines (it, nlines);
9087 /* The above code moves us to some position NLINES down,
9088 usually to its first glyph (leftmost in an L2R line), but
9089 that's not necessarily the start of the line, under bidi
9090 reordering. We want to get to the character position
9091 that is immediately after the newline of the previous
9092 line. */
9093 if (it->bidi_p
9094 && !it->continuation_lines_width
9095 && !STRINGP (it->string)
9096 && IT_CHARPOS (*it) > BEGV
9097 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9098 {
9099 ptrdiff_t nl_pos =
9100 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9101
9102 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9103 }
9104 bidi_unshelve_cache (it3data, 1);
9105 }
9106 else
9107 {
9108 /* The y-position we try to reach, relative to *IT.
9109 Note that H has been subtracted in front of the if-statement. */
9110 int target_y = it->current_y + h - dy;
9111 int y0 = it3.current_y;
9112 int y1;
9113 int line_height;
9114
9115 RESTORE_IT (&it3, &it3, it3data);
9116 y1 = line_bottom_y (&it3);
9117 line_height = y1 - y0;
9118 RESTORE_IT (it, it, it2data);
9119 /* If we did not reach target_y, try to move further backward if
9120 we can. If we moved too far backward, try to move forward. */
9121 if (target_y < it->current_y
9122 /* This is heuristic. In a window that's 3 lines high, with
9123 a line height of 13 pixels each, recentering with point
9124 on the bottom line will try to move -39/2 = 19 pixels
9125 backward. Try to avoid moving into the first line. */
9126 && (it->current_y - target_y
9127 > min (window_box_height (it->w), line_height * 2 / 3))
9128 && IT_CHARPOS (*it) > BEGV)
9129 {
9130 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9131 target_y - it->current_y));
9132 dy = it->current_y - target_y;
9133 goto move_further_back;
9134 }
9135 else if (target_y >= it->current_y + line_height
9136 && IT_CHARPOS (*it) < ZV)
9137 {
9138 /* Should move forward by at least one line, maybe more.
9139
9140 Note: Calling move_it_by_lines can be expensive on
9141 terminal frames, where compute_motion is used (via
9142 vmotion) to do the job, when there are very long lines
9143 and truncate-lines is nil. That's the reason for
9144 treating terminal frames specially here. */
9145
9146 if (!FRAME_WINDOW_P (it->f))
9147 move_it_vertically (it, target_y - (it->current_y + line_height));
9148 else
9149 {
9150 do
9151 {
9152 move_it_by_lines (it, 1);
9153 }
9154 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9155 }
9156 }
9157 }
9158 }
9159
9160
9161 /* Move IT by a specified amount of pixel lines DY. DY negative means
9162 move backwards. DY = 0 means move to start of screen line. At the
9163 end, IT will be on the start of a screen line. */
9164
9165 void
9166 move_it_vertically (struct it *it, int dy)
9167 {
9168 if (dy <= 0)
9169 move_it_vertically_backward (it, -dy);
9170 else
9171 {
9172 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9173 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9174 MOVE_TO_POS | MOVE_TO_Y);
9175 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9176
9177 /* If buffer ends in ZV without a newline, move to the start of
9178 the line to satisfy the post-condition. */
9179 if (IT_CHARPOS (*it) == ZV
9180 && ZV > BEGV
9181 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9182 move_it_by_lines (it, 0);
9183 }
9184 }
9185
9186
9187 /* Move iterator IT past the end of the text line it is in. */
9188
9189 void
9190 move_it_past_eol (struct it *it)
9191 {
9192 enum move_it_result rc;
9193
9194 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9195 if (rc == MOVE_NEWLINE_OR_CR)
9196 set_iterator_to_next (it, 0);
9197 }
9198
9199
9200 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9201 negative means move up. DVPOS == 0 means move to the start of the
9202 screen line.
9203
9204 Optimization idea: If we would know that IT->f doesn't use
9205 a face with proportional font, we could be faster for
9206 truncate-lines nil. */
9207
9208 void
9209 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9210 {
9211
9212 /* The commented-out optimization uses vmotion on terminals. This
9213 gives bad results, because elements like it->what, on which
9214 callers such as pos_visible_p rely, aren't updated. */
9215 /* struct position pos;
9216 if (!FRAME_WINDOW_P (it->f))
9217 {
9218 struct text_pos textpos;
9219
9220 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9221 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9222 reseat (it, textpos, 1);
9223 it->vpos += pos.vpos;
9224 it->current_y += pos.vpos;
9225 }
9226 else */
9227
9228 if (dvpos == 0)
9229 {
9230 /* DVPOS == 0 means move to the start of the screen line. */
9231 move_it_vertically_backward (it, 0);
9232 /* Let next call to line_bottom_y calculate real line height */
9233 last_height = 0;
9234 }
9235 else if (dvpos > 0)
9236 {
9237 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9238 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9239 {
9240 /* Only move to the next buffer position if we ended up in a
9241 string from display property, not in an overlay string
9242 (before-string or after-string). That is because the
9243 latter don't conceal the underlying buffer position, so
9244 we can ask to move the iterator to the exact position we
9245 are interested in. Note that, even if we are already at
9246 IT_CHARPOS (*it), the call below is not a no-op, as it
9247 will detect that we are at the end of the string, pop the
9248 iterator, and compute it->current_x and it->hpos
9249 correctly. */
9250 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9251 -1, -1, -1, MOVE_TO_POS);
9252 }
9253 }
9254 else
9255 {
9256 struct it it2;
9257 void *it2data = NULL;
9258 ptrdiff_t start_charpos, i;
9259
9260 /* Start at the beginning of the screen line containing IT's
9261 position. This may actually move vertically backwards,
9262 in case of overlays, so adjust dvpos accordingly. */
9263 dvpos += it->vpos;
9264 move_it_vertically_backward (it, 0);
9265 dvpos -= it->vpos;
9266
9267 /* Go back -DVPOS visible lines and reseat the iterator there. */
9268 start_charpos = IT_CHARPOS (*it);
9269 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9270 back_to_previous_visible_line_start (it);
9271 reseat (it, it->current.pos, 1);
9272
9273 /* Move further back if we end up in a string or an image. */
9274 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9275 {
9276 /* First try to move to start of display line. */
9277 dvpos += it->vpos;
9278 move_it_vertically_backward (it, 0);
9279 dvpos -= it->vpos;
9280 if (IT_POS_VALID_AFTER_MOVE_P (it))
9281 break;
9282 /* If start of line is still in string or image,
9283 move further back. */
9284 back_to_previous_visible_line_start (it);
9285 reseat (it, it->current.pos, 1);
9286 dvpos--;
9287 }
9288
9289 it->current_x = it->hpos = 0;
9290
9291 /* Above call may have moved too far if continuation lines
9292 are involved. Scan forward and see if it did. */
9293 SAVE_IT (it2, *it, it2data);
9294 it2.vpos = it2.current_y = 0;
9295 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9296 it->vpos -= it2.vpos;
9297 it->current_y -= it2.current_y;
9298 it->current_x = it->hpos = 0;
9299
9300 /* If we moved too far back, move IT some lines forward. */
9301 if (it2.vpos > -dvpos)
9302 {
9303 int delta = it2.vpos + dvpos;
9304
9305 RESTORE_IT (&it2, &it2, it2data);
9306 SAVE_IT (it2, *it, it2data);
9307 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9308 /* Move back again if we got too far ahead. */
9309 if (IT_CHARPOS (*it) >= start_charpos)
9310 RESTORE_IT (it, &it2, it2data);
9311 else
9312 bidi_unshelve_cache (it2data, 1);
9313 }
9314 else
9315 RESTORE_IT (it, it, it2data);
9316 }
9317 }
9318
9319 /* Return 1 if IT points into the middle of a display vector. */
9320
9321 int
9322 in_display_vector_p (struct it *it)
9323 {
9324 return (it->method == GET_FROM_DISPLAY_VECTOR
9325 && it->current.dpvec_index > 0
9326 && it->dpvec + it->current.dpvec_index != it->dpend);
9327 }
9328
9329 \f
9330 /***********************************************************************
9331 Messages
9332 ***********************************************************************/
9333
9334
9335 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9336 to *Messages*. */
9337
9338 void
9339 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9340 {
9341 Lisp_Object args[3];
9342 Lisp_Object msg, fmt;
9343 char *buffer;
9344 ptrdiff_t len;
9345 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9346 USE_SAFE_ALLOCA;
9347
9348 fmt = msg = Qnil;
9349 GCPRO4 (fmt, msg, arg1, arg2);
9350
9351 args[0] = fmt = build_string (format);
9352 args[1] = arg1;
9353 args[2] = arg2;
9354 msg = Fformat (3, args);
9355
9356 len = SBYTES (msg) + 1;
9357 buffer = SAFE_ALLOCA (len);
9358 memcpy (buffer, SDATA (msg), len);
9359
9360 message_dolog (buffer, len - 1, 1, 0);
9361 SAFE_FREE ();
9362
9363 UNGCPRO;
9364 }
9365
9366
9367 /* Output a newline in the *Messages* buffer if "needs" one. */
9368
9369 void
9370 message_log_maybe_newline (void)
9371 {
9372 if (message_log_need_newline)
9373 message_dolog ("", 0, 1, 0);
9374 }
9375
9376
9377 /* Add a string M of length NBYTES to the message log, optionally
9378 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9379 nonzero, means interpret the contents of M as multibyte. This
9380 function calls low-level routines in order to bypass text property
9381 hooks, etc. which might not be safe to run.
9382
9383 This may GC (insert may run before/after change hooks),
9384 so the buffer M must NOT point to a Lisp string. */
9385
9386 void
9387 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9388 {
9389 const unsigned char *msg = (const unsigned char *) m;
9390
9391 if (!NILP (Vmemory_full))
9392 return;
9393
9394 if (!NILP (Vmessage_log_max))
9395 {
9396 struct buffer *oldbuf;
9397 Lisp_Object oldpoint, oldbegv, oldzv;
9398 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9399 ptrdiff_t point_at_end = 0;
9400 ptrdiff_t zv_at_end = 0;
9401 Lisp_Object old_deactivate_mark;
9402 bool shown;
9403 struct gcpro gcpro1;
9404
9405 old_deactivate_mark = Vdeactivate_mark;
9406 oldbuf = current_buffer;
9407 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9408 bset_undo_list (current_buffer, Qt);
9409
9410 oldpoint = message_dolog_marker1;
9411 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9412 oldbegv = message_dolog_marker2;
9413 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9414 oldzv = message_dolog_marker3;
9415 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9416 GCPRO1 (old_deactivate_mark);
9417
9418 if (PT == Z)
9419 point_at_end = 1;
9420 if (ZV == Z)
9421 zv_at_end = 1;
9422
9423 BEGV = BEG;
9424 BEGV_BYTE = BEG_BYTE;
9425 ZV = Z;
9426 ZV_BYTE = Z_BYTE;
9427 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9428
9429 /* Insert the string--maybe converting multibyte to single byte
9430 or vice versa, so that all the text fits the buffer. */
9431 if (multibyte
9432 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9433 {
9434 ptrdiff_t i;
9435 int c, char_bytes;
9436 char work[1];
9437
9438 /* Convert a multibyte string to single-byte
9439 for the *Message* buffer. */
9440 for (i = 0; i < nbytes; i += char_bytes)
9441 {
9442 c = string_char_and_length (msg + i, &char_bytes);
9443 work[0] = (ASCII_CHAR_P (c)
9444 ? c
9445 : multibyte_char_to_unibyte (c));
9446 insert_1_both (work, 1, 1, 1, 0, 0);
9447 }
9448 }
9449 else if (! multibyte
9450 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9451 {
9452 ptrdiff_t i;
9453 int c, char_bytes;
9454 unsigned char str[MAX_MULTIBYTE_LENGTH];
9455 /* Convert a single-byte string to multibyte
9456 for the *Message* buffer. */
9457 for (i = 0; i < nbytes; i++)
9458 {
9459 c = msg[i];
9460 MAKE_CHAR_MULTIBYTE (c);
9461 char_bytes = CHAR_STRING (c, str);
9462 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9463 }
9464 }
9465 else if (nbytes)
9466 insert_1 (m, nbytes, 1, 0, 0);
9467
9468 if (nlflag)
9469 {
9470 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9471 printmax_t dups;
9472 insert_1 ("\n", 1, 1, 0, 0);
9473
9474 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9475 this_bol = PT;
9476 this_bol_byte = PT_BYTE;
9477
9478 /* See if this line duplicates the previous one.
9479 If so, combine duplicates. */
9480 if (this_bol > BEG)
9481 {
9482 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9483 prev_bol = PT;
9484 prev_bol_byte = PT_BYTE;
9485
9486 dups = message_log_check_duplicate (prev_bol_byte,
9487 this_bol_byte);
9488 if (dups)
9489 {
9490 del_range_both (prev_bol, prev_bol_byte,
9491 this_bol, this_bol_byte, 0);
9492 if (dups > 1)
9493 {
9494 char dupstr[sizeof " [ times]"
9495 + INT_STRLEN_BOUND (printmax_t)];
9496
9497 /* If you change this format, don't forget to also
9498 change message_log_check_duplicate. */
9499 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9500 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9501 insert_1 (dupstr, duplen, 1, 0, 1);
9502 }
9503 }
9504 }
9505
9506 /* If we have more than the desired maximum number of lines
9507 in the *Messages* buffer now, delete the oldest ones.
9508 This is safe because we don't have undo in this buffer. */
9509
9510 if (NATNUMP (Vmessage_log_max))
9511 {
9512 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9513 -XFASTINT (Vmessage_log_max) - 1, 0);
9514 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9515 }
9516 }
9517 BEGV = marker_position (oldbegv);
9518 BEGV_BYTE = marker_byte_position (oldbegv);
9519
9520 if (zv_at_end)
9521 {
9522 ZV = Z;
9523 ZV_BYTE = Z_BYTE;
9524 }
9525 else
9526 {
9527 ZV = marker_position (oldzv);
9528 ZV_BYTE = marker_byte_position (oldzv);
9529 }
9530
9531 if (point_at_end)
9532 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9533 else
9534 /* We can't do Fgoto_char (oldpoint) because it will run some
9535 Lisp code. */
9536 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9537 marker_byte_position (oldpoint));
9538
9539 UNGCPRO;
9540 unchain_marker (XMARKER (oldpoint));
9541 unchain_marker (XMARKER (oldbegv));
9542 unchain_marker (XMARKER (oldzv));
9543
9544 shown = buffer_window_count (current_buffer) > 0;
9545 set_buffer_internal (oldbuf);
9546 if (!shown)
9547 windows_or_buffers_changed = old_windows_or_buffers_changed;
9548 message_log_need_newline = !nlflag;
9549 Vdeactivate_mark = old_deactivate_mark;
9550 }
9551 }
9552
9553
9554 /* We are at the end of the buffer after just having inserted a newline.
9555 (Note: We depend on the fact we won't be crossing the gap.)
9556 Check to see if the most recent message looks a lot like the previous one.
9557 Return 0 if different, 1 if the new one should just replace it, or a
9558 value N > 1 if we should also append " [N times]". */
9559
9560 static intmax_t
9561 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9562 {
9563 ptrdiff_t i;
9564 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9565 int seen_dots = 0;
9566 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9567 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9568
9569 for (i = 0; i < len; i++)
9570 {
9571 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9572 seen_dots = 1;
9573 if (p1[i] != p2[i])
9574 return seen_dots;
9575 }
9576 p1 += len;
9577 if (*p1 == '\n')
9578 return 2;
9579 if (*p1++ == ' ' && *p1++ == '[')
9580 {
9581 char *pend;
9582 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9583 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9584 return n+1;
9585 }
9586 return 0;
9587 }
9588 \f
9589
9590 /* Display an echo area message M with a specified length of NBYTES
9591 bytes. The string may include null characters. If M is 0, clear
9592 out any existing message, and let the mini-buffer text show
9593 through.
9594
9595 This may GC, so the buffer M must NOT point to a Lisp string. */
9596
9597 void
9598 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9599 {
9600 /* First flush out any partial line written with print. */
9601 message_log_maybe_newline ();
9602 if (m)
9603 message_dolog (m, nbytes, 1, multibyte);
9604 message2_nolog (m, nbytes, multibyte);
9605 }
9606
9607
9608 /* The non-logging counterpart of message2. */
9609
9610 void
9611 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9612 {
9613 struct frame *sf = SELECTED_FRAME ();
9614 message_enable_multibyte = multibyte;
9615
9616 if (FRAME_INITIAL_P (sf))
9617 {
9618 if (noninteractive_need_newline)
9619 putc ('\n', stderr);
9620 noninteractive_need_newline = 0;
9621 if (m)
9622 fwrite (m, nbytes, 1, stderr);
9623 if (cursor_in_echo_area == 0)
9624 fprintf (stderr, "\n");
9625 fflush (stderr);
9626 }
9627 /* A null message buffer means that the frame hasn't really been
9628 initialized yet. Error messages get reported properly by
9629 cmd_error, so this must be just an informative message; toss it. */
9630 else if (INTERACTIVE
9631 && sf->glyphs_initialized_p
9632 && FRAME_MESSAGE_BUF (sf))
9633 {
9634 Lisp_Object mini_window;
9635 struct frame *f;
9636
9637 /* Get the frame containing the mini-buffer
9638 that the selected frame is using. */
9639 mini_window = FRAME_MINIBUF_WINDOW (sf);
9640 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9641
9642 FRAME_SAMPLE_VISIBILITY (f);
9643 if (FRAME_VISIBLE_P (sf)
9644 && ! FRAME_VISIBLE_P (f))
9645 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9646
9647 if (m)
9648 {
9649 set_message (m, Qnil, nbytes, multibyte);
9650 if (minibuffer_auto_raise)
9651 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9652 }
9653 else
9654 clear_message (1, 1);
9655
9656 do_pending_window_change (0);
9657 echo_area_display (1);
9658 do_pending_window_change (0);
9659 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9660 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9661 }
9662 }
9663
9664
9665 /* Display an echo area message M with a specified length of NBYTES
9666 bytes. The string may include null characters. If M is not a
9667 string, clear out any existing message, and let the mini-buffer
9668 text show through.
9669
9670 This function cancels echoing. */
9671
9672 void
9673 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9674 {
9675 struct gcpro gcpro1;
9676
9677 GCPRO1 (m);
9678 clear_message (1,1);
9679 cancel_echoing ();
9680
9681 /* First flush out any partial line written with print. */
9682 message_log_maybe_newline ();
9683 if (STRINGP (m))
9684 {
9685 USE_SAFE_ALLOCA;
9686 char *buffer = SAFE_ALLOCA (nbytes);
9687 memcpy (buffer, SDATA (m), nbytes);
9688 message_dolog (buffer, nbytes, 1, multibyte);
9689 SAFE_FREE ();
9690 }
9691 message3_nolog (m, nbytes, multibyte);
9692
9693 UNGCPRO;
9694 }
9695
9696
9697 /* The non-logging version of message3.
9698 This does not cancel echoing, because it is used for echoing.
9699 Perhaps we need to make a separate function for echoing
9700 and make this cancel echoing. */
9701
9702 void
9703 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9704 {
9705 struct frame *sf = SELECTED_FRAME ();
9706 message_enable_multibyte = multibyte;
9707
9708 if (FRAME_INITIAL_P (sf))
9709 {
9710 if (noninteractive_need_newline)
9711 putc ('\n', stderr);
9712 noninteractive_need_newline = 0;
9713 if (STRINGP (m))
9714 fwrite (SDATA (m), nbytes, 1, stderr);
9715 if (cursor_in_echo_area == 0)
9716 fprintf (stderr, "\n");
9717 fflush (stderr);
9718 }
9719 /* A null message buffer means that the frame hasn't really been
9720 initialized yet. Error messages get reported properly by
9721 cmd_error, so this must be just an informative message; toss it. */
9722 else if (INTERACTIVE
9723 && sf->glyphs_initialized_p
9724 && FRAME_MESSAGE_BUF (sf))
9725 {
9726 Lisp_Object mini_window;
9727 Lisp_Object frame;
9728 struct frame *f;
9729
9730 /* Get the frame containing the mini-buffer
9731 that the selected frame is using. */
9732 mini_window = FRAME_MINIBUF_WINDOW (sf);
9733 frame = XWINDOW (mini_window)->frame;
9734 f = XFRAME (frame);
9735
9736 FRAME_SAMPLE_VISIBILITY (f);
9737 if (FRAME_VISIBLE_P (sf)
9738 && !FRAME_VISIBLE_P (f))
9739 Fmake_frame_visible (frame);
9740
9741 if (STRINGP (m) && SCHARS (m) > 0)
9742 {
9743 set_message (NULL, m, nbytes, multibyte);
9744 if (minibuffer_auto_raise)
9745 Fraise_frame (frame);
9746 /* Assume we are not echoing.
9747 (If we are, echo_now will override this.) */
9748 echo_message_buffer = Qnil;
9749 }
9750 else
9751 clear_message (1, 1);
9752
9753 do_pending_window_change (0);
9754 echo_area_display (1);
9755 do_pending_window_change (0);
9756 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9757 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9758 }
9759 }
9760
9761
9762 /* Display a null-terminated echo area message M. If M is 0, clear
9763 out any existing message, and let the mini-buffer text show through.
9764
9765 The buffer M must continue to exist until after the echo area gets
9766 cleared or some other message gets displayed there. Do not pass
9767 text that is stored in a Lisp string. Do not pass text in a buffer
9768 that was alloca'd. */
9769
9770 void
9771 message1 (const char *m)
9772 {
9773 message2 (m, (m ? strlen (m) : 0), 0);
9774 }
9775
9776
9777 /* The non-logging counterpart of message1. */
9778
9779 void
9780 message1_nolog (const char *m)
9781 {
9782 message2_nolog (m, (m ? strlen (m) : 0), 0);
9783 }
9784
9785 /* Display a message M which contains a single %s
9786 which gets replaced with STRING. */
9787
9788 void
9789 message_with_string (const char *m, Lisp_Object string, int log)
9790 {
9791 CHECK_STRING (string);
9792
9793 if (noninteractive)
9794 {
9795 if (m)
9796 {
9797 if (noninteractive_need_newline)
9798 putc ('\n', stderr);
9799 noninteractive_need_newline = 0;
9800 fprintf (stderr, m, SDATA (string));
9801 if (!cursor_in_echo_area)
9802 fprintf (stderr, "\n");
9803 fflush (stderr);
9804 }
9805 }
9806 else if (INTERACTIVE)
9807 {
9808 /* The frame whose minibuffer we're going to display the message on.
9809 It may be larger than the selected frame, so we need
9810 to use its buffer, not the selected frame's buffer. */
9811 Lisp_Object mini_window;
9812 struct frame *f, *sf = SELECTED_FRAME ();
9813
9814 /* Get the frame containing the minibuffer
9815 that the selected frame is using. */
9816 mini_window = FRAME_MINIBUF_WINDOW (sf);
9817 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9818
9819 /* A null message buffer means that the frame hasn't really been
9820 initialized yet. Error messages get reported properly by
9821 cmd_error, so this must be just an informative message; toss it. */
9822 if (FRAME_MESSAGE_BUF (f))
9823 {
9824 Lisp_Object args[2], msg;
9825 struct gcpro gcpro1, gcpro2;
9826
9827 args[0] = build_string (m);
9828 args[1] = msg = string;
9829 GCPRO2 (args[0], msg);
9830 gcpro1.nvars = 2;
9831
9832 msg = Fformat (2, args);
9833
9834 if (log)
9835 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9836 else
9837 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9838
9839 UNGCPRO;
9840
9841 /* Print should start at the beginning of the message
9842 buffer next time. */
9843 message_buf_print = 0;
9844 }
9845 }
9846 }
9847
9848
9849 /* Dump an informative message to the minibuf. If M is 0, clear out
9850 any existing message, and let the mini-buffer text show through. */
9851
9852 static void
9853 vmessage (const char *m, va_list ap)
9854 {
9855 if (noninteractive)
9856 {
9857 if (m)
9858 {
9859 if (noninteractive_need_newline)
9860 putc ('\n', stderr);
9861 noninteractive_need_newline = 0;
9862 vfprintf (stderr, m, ap);
9863 if (cursor_in_echo_area == 0)
9864 fprintf (stderr, "\n");
9865 fflush (stderr);
9866 }
9867 }
9868 else if (INTERACTIVE)
9869 {
9870 /* The frame whose mini-buffer we're going to display the message
9871 on. It may be larger than the selected frame, so we need to
9872 use its buffer, not the selected frame's buffer. */
9873 Lisp_Object mini_window;
9874 struct frame *f, *sf = SELECTED_FRAME ();
9875
9876 /* Get the frame containing the mini-buffer
9877 that the selected frame is using. */
9878 mini_window = FRAME_MINIBUF_WINDOW (sf);
9879 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9880
9881 /* A null message buffer means that the frame hasn't really been
9882 initialized yet. Error messages get reported properly by
9883 cmd_error, so this must be just an informative message; toss
9884 it. */
9885 if (FRAME_MESSAGE_BUF (f))
9886 {
9887 if (m)
9888 {
9889 ptrdiff_t len;
9890
9891 len = doprnt (FRAME_MESSAGE_BUF (f),
9892 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9893
9894 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9895 }
9896 else
9897 message1 (0);
9898
9899 /* Print should start at the beginning of the message
9900 buffer next time. */
9901 message_buf_print = 0;
9902 }
9903 }
9904 }
9905
9906 void
9907 message (const char *m, ...)
9908 {
9909 va_list ap;
9910 va_start (ap, m);
9911 vmessage (m, ap);
9912 va_end (ap);
9913 }
9914
9915
9916 #if 0
9917 /* The non-logging version of message. */
9918
9919 void
9920 message_nolog (const char *m, ...)
9921 {
9922 Lisp_Object old_log_max;
9923 va_list ap;
9924 va_start (ap, m);
9925 old_log_max = Vmessage_log_max;
9926 Vmessage_log_max = Qnil;
9927 vmessage (m, ap);
9928 Vmessage_log_max = old_log_max;
9929 va_end (ap);
9930 }
9931 #endif
9932
9933
9934 /* Display the current message in the current mini-buffer. This is
9935 only called from error handlers in process.c, and is not time
9936 critical. */
9937
9938 void
9939 update_echo_area (void)
9940 {
9941 if (!NILP (echo_area_buffer[0]))
9942 {
9943 Lisp_Object string;
9944 string = Fcurrent_message ();
9945 message3 (string, SBYTES (string),
9946 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9947 }
9948 }
9949
9950
9951 /* Make sure echo area buffers in `echo_buffers' are live.
9952 If they aren't, make new ones. */
9953
9954 static void
9955 ensure_echo_area_buffers (void)
9956 {
9957 int i;
9958
9959 for (i = 0; i < 2; ++i)
9960 if (!BUFFERP (echo_buffer[i])
9961 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9962 {
9963 char name[30];
9964 Lisp_Object old_buffer;
9965 int j;
9966
9967 old_buffer = echo_buffer[i];
9968 echo_buffer[i] = Fget_buffer_create
9969 (make_formatted_string (name, " *Echo Area %d*", i));
9970 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9971 /* to force word wrap in echo area -
9972 it was decided to postpone this*/
9973 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9974
9975 for (j = 0; j < 2; ++j)
9976 if (EQ (old_buffer, echo_area_buffer[j]))
9977 echo_area_buffer[j] = echo_buffer[i];
9978 }
9979 }
9980
9981
9982 /* Call FN with args A1..A4 with either the current or last displayed
9983 echo_area_buffer as current buffer.
9984
9985 WHICH zero means use the current message buffer
9986 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9987 from echo_buffer[] and clear it.
9988
9989 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9990 suitable buffer from echo_buffer[] and clear it.
9991
9992 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9993 that the current message becomes the last displayed one, make
9994 choose a suitable buffer for echo_area_buffer[0], and clear it.
9995
9996 Value is what FN returns. */
9997
9998 static int
9999 with_echo_area_buffer (struct window *w, int which,
10000 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
10001 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10002 {
10003 Lisp_Object buffer;
10004 int this_one, the_other, clear_buffer_p, rc;
10005 ptrdiff_t count = SPECPDL_INDEX ();
10006
10007 /* If buffers aren't live, make new ones. */
10008 ensure_echo_area_buffers ();
10009
10010 clear_buffer_p = 0;
10011
10012 if (which == 0)
10013 this_one = 0, the_other = 1;
10014 else if (which > 0)
10015 this_one = 1, the_other = 0;
10016 else
10017 {
10018 this_one = 0, the_other = 1;
10019 clear_buffer_p = 1;
10020
10021 /* We need a fresh one in case the current echo buffer equals
10022 the one containing the last displayed echo area message. */
10023 if (!NILP (echo_area_buffer[this_one])
10024 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10025 echo_area_buffer[this_one] = Qnil;
10026 }
10027
10028 /* Choose a suitable buffer from echo_buffer[] is we don't
10029 have one. */
10030 if (NILP (echo_area_buffer[this_one]))
10031 {
10032 echo_area_buffer[this_one]
10033 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10034 ? echo_buffer[the_other]
10035 : echo_buffer[this_one]);
10036 clear_buffer_p = 1;
10037 }
10038
10039 buffer = echo_area_buffer[this_one];
10040
10041 /* Don't get confused by reusing the buffer used for echoing
10042 for a different purpose. */
10043 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10044 cancel_echoing ();
10045
10046 record_unwind_protect (unwind_with_echo_area_buffer,
10047 with_echo_area_buffer_unwind_data (w));
10048
10049 /* Make the echo area buffer current. Note that for display
10050 purposes, it is not necessary that the displayed window's buffer
10051 == current_buffer, except for text property lookup. So, let's
10052 only set that buffer temporarily here without doing a full
10053 Fset_window_buffer. We must also change w->pointm, though,
10054 because otherwise an assertions in unshow_buffer fails, and Emacs
10055 aborts. */
10056 set_buffer_internal_1 (XBUFFER (buffer));
10057 if (w)
10058 {
10059 wset_buffer (w, buffer);
10060 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10061 }
10062
10063 bset_undo_list (current_buffer, Qt);
10064 bset_read_only (current_buffer, Qnil);
10065 specbind (Qinhibit_read_only, Qt);
10066 specbind (Qinhibit_modification_hooks, Qt);
10067
10068 if (clear_buffer_p && Z > BEG)
10069 del_range (BEG, Z);
10070
10071 eassert (BEGV >= BEG);
10072 eassert (ZV <= Z && ZV >= BEGV);
10073
10074 rc = fn (a1, a2, a3, a4);
10075
10076 eassert (BEGV >= BEG);
10077 eassert (ZV <= Z && ZV >= BEGV);
10078
10079 unbind_to (count, Qnil);
10080 return rc;
10081 }
10082
10083
10084 /* Save state that should be preserved around the call to the function
10085 FN called in with_echo_area_buffer. */
10086
10087 static Lisp_Object
10088 with_echo_area_buffer_unwind_data (struct window *w)
10089 {
10090 int i = 0;
10091 Lisp_Object vector, tmp;
10092
10093 /* Reduce consing by keeping one vector in
10094 Vwith_echo_area_save_vector. */
10095 vector = Vwith_echo_area_save_vector;
10096 Vwith_echo_area_save_vector = Qnil;
10097
10098 if (NILP (vector))
10099 vector = Fmake_vector (make_number (7), Qnil);
10100
10101 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10102 ASET (vector, i, Vdeactivate_mark); ++i;
10103 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10104
10105 if (w)
10106 {
10107 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10108 ASET (vector, i, w->buffer); ++i;
10109 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10110 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10111 }
10112 else
10113 {
10114 int end = i + 4;
10115 for (; i < end; ++i)
10116 ASET (vector, i, Qnil);
10117 }
10118
10119 eassert (i == ASIZE (vector));
10120 return vector;
10121 }
10122
10123
10124 /* Restore global state from VECTOR which was created by
10125 with_echo_area_buffer_unwind_data. */
10126
10127 static Lisp_Object
10128 unwind_with_echo_area_buffer (Lisp_Object vector)
10129 {
10130 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10131 Vdeactivate_mark = AREF (vector, 1);
10132 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10133
10134 if (WINDOWP (AREF (vector, 3)))
10135 {
10136 struct window *w;
10137 Lisp_Object buffer, charpos, bytepos;
10138
10139 w = XWINDOW (AREF (vector, 3));
10140 buffer = AREF (vector, 4);
10141 charpos = AREF (vector, 5);
10142 bytepos = AREF (vector, 6);
10143
10144 wset_buffer (w, buffer);
10145 set_marker_both (w->pointm, buffer,
10146 XFASTINT (charpos), XFASTINT (bytepos));
10147 }
10148
10149 Vwith_echo_area_save_vector = vector;
10150 return Qnil;
10151 }
10152
10153
10154 /* Set up the echo area for use by print functions. MULTIBYTE_P
10155 non-zero means we will print multibyte. */
10156
10157 void
10158 setup_echo_area_for_printing (int multibyte_p)
10159 {
10160 /* If we can't find an echo area any more, exit. */
10161 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10162 Fkill_emacs (Qnil);
10163
10164 ensure_echo_area_buffers ();
10165
10166 if (!message_buf_print)
10167 {
10168 /* A message has been output since the last time we printed.
10169 Choose a fresh echo area buffer. */
10170 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10171 echo_area_buffer[0] = echo_buffer[1];
10172 else
10173 echo_area_buffer[0] = echo_buffer[0];
10174
10175 /* Switch to that buffer and clear it. */
10176 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10177 bset_truncate_lines (current_buffer, Qnil);
10178
10179 if (Z > BEG)
10180 {
10181 ptrdiff_t count = SPECPDL_INDEX ();
10182 specbind (Qinhibit_read_only, Qt);
10183 /* Note that undo recording is always disabled. */
10184 del_range (BEG, Z);
10185 unbind_to (count, Qnil);
10186 }
10187 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10188
10189 /* Set up the buffer for the multibyteness we need. */
10190 if (multibyte_p
10191 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10192 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10193
10194 /* Raise the frame containing the echo area. */
10195 if (minibuffer_auto_raise)
10196 {
10197 struct frame *sf = SELECTED_FRAME ();
10198 Lisp_Object mini_window;
10199 mini_window = FRAME_MINIBUF_WINDOW (sf);
10200 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10201 }
10202
10203 message_log_maybe_newline ();
10204 message_buf_print = 1;
10205 }
10206 else
10207 {
10208 if (NILP (echo_area_buffer[0]))
10209 {
10210 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10211 echo_area_buffer[0] = echo_buffer[1];
10212 else
10213 echo_area_buffer[0] = echo_buffer[0];
10214 }
10215
10216 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10217 {
10218 /* Someone switched buffers between print requests. */
10219 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10220 bset_truncate_lines (current_buffer, Qnil);
10221 }
10222 }
10223 }
10224
10225
10226 /* Display an echo area message in window W. Value is non-zero if W's
10227 height is changed. If display_last_displayed_message_p is
10228 non-zero, display the message that was last displayed, otherwise
10229 display the current message. */
10230
10231 static int
10232 display_echo_area (struct window *w)
10233 {
10234 int i, no_message_p, window_height_changed_p;
10235
10236 /* Temporarily disable garbage collections while displaying the echo
10237 area. This is done because a GC can print a message itself.
10238 That message would modify the echo area buffer's contents while a
10239 redisplay of the buffer is going on, and seriously confuse
10240 redisplay. */
10241 ptrdiff_t count = inhibit_garbage_collection ();
10242
10243 /* If there is no message, we must call display_echo_area_1
10244 nevertheless because it resizes the window. But we will have to
10245 reset the echo_area_buffer in question to nil at the end because
10246 with_echo_area_buffer will sets it to an empty buffer. */
10247 i = display_last_displayed_message_p ? 1 : 0;
10248 no_message_p = NILP (echo_area_buffer[i]);
10249
10250 window_height_changed_p
10251 = with_echo_area_buffer (w, display_last_displayed_message_p,
10252 display_echo_area_1,
10253 (intptr_t) w, Qnil, 0, 0);
10254
10255 if (no_message_p)
10256 echo_area_buffer[i] = Qnil;
10257
10258 unbind_to (count, Qnil);
10259 return window_height_changed_p;
10260 }
10261
10262
10263 /* Helper for display_echo_area. Display the current buffer which
10264 contains the current echo area message in window W, a mini-window,
10265 a pointer to which is passed in A1. A2..A4 are currently not used.
10266 Change the height of W so that all of the message is displayed.
10267 Value is non-zero if height of W was changed. */
10268
10269 static int
10270 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10271 {
10272 intptr_t i1 = a1;
10273 struct window *w = (struct window *) i1;
10274 Lisp_Object window;
10275 struct text_pos start;
10276 int window_height_changed_p = 0;
10277
10278 /* Do this before displaying, so that we have a large enough glyph
10279 matrix for the display. If we can't get enough space for the
10280 whole text, display the last N lines. That works by setting w->start. */
10281 window_height_changed_p = resize_mini_window (w, 0);
10282
10283 /* Use the starting position chosen by resize_mini_window. */
10284 SET_TEXT_POS_FROM_MARKER (start, w->start);
10285
10286 /* Display. */
10287 clear_glyph_matrix (w->desired_matrix);
10288 XSETWINDOW (window, w);
10289 try_window (window, start, 0);
10290
10291 return window_height_changed_p;
10292 }
10293
10294
10295 /* Resize the echo area window to exactly the size needed for the
10296 currently displayed message, if there is one. If a mini-buffer
10297 is active, don't shrink it. */
10298
10299 void
10300 resize_echo_area_exactly (void)
10301 {
10302 if (BUFFERP (echo_area_buffer[0])
10303 && WINDOWP (echo_area_window))
10304 {
10305 struct window *w = XWINDOW (echo_area_window);
10306 int resized_p;
10307 Lisp_Object resize_exactly;
10308
10309 if (minibuf_level == 0)
10310 resize_exactly = Qt;
10311 else
10312 resize_exactly = Qnil;
10313
10314 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10315 (intptr_t) w, resize_exactly,
10316 0, 0);
10317 if (resized_p)
10318 {
10319 ++windows_or_buffers_changed;
10320 ++update_mode_lines;
10321 redisplay_internal ();
10322 }
10323 }
10324 }
10325
10326
10327 /* Callback function for with_echo_area_buffer, when used from
10328 resize_echo_area_exactly. A1 contains a pointer to the window to
10329 resize, EXACTLY non-nil means resize the mini-window exactly to the
10330 size of the text displayed. A3 and A4 are not used. Value is what
10331 resize_mini_window returns. */
10332
10333 static int
10334 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10335 {
10336 intptr_t i1 = a1;
10337 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10338 }
10339
10340
10341 /* Resize mini-window W to fit the size of its contents. EXACT_P
10342 means size the window exactly to the size needed. Otherwise, it's
10343 only enlarged until W's buffer is empty.
10344
10345 Set W->start to the right place to begin display. If the whole
10346 contents fit, start at the beginning. Otherwise, start so as
10347 to make the end of the contents appear. This is particularly
10348 important for y-or-n-p, but seems desirable generally.
10349
10350 Value is non-zero if the window height has been changed. */
10351
10352 int
10353 resize_mini_window (struct window *w, int exact_p)
10354 {
10355 struct frame *f = XFRAME (w->frame);
10356 int window_height_changed_p = 0;
10357
10358 eassert (MINI_WINDOW_P (w));
10359
10360 /* By default, start display at the beginning. */
10361 set_marker_both (w->start, w->buffer,
10362 BUF_BEGV (XBUFFER (w->buffer)),
10363 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10364
10365 /* Don't resize windows while redisplaying a window; it would
10366 confuse redisplay functions when the size of the window they are
10367 displaying changes from under them. Such a resizing can happen,
10368 for instance, when which-func prints a long message while
10369 we are running fontification-functions. We're running these
10370 functions with safe_call which binds inhibit-redisplay to t. */
10371 if (!NILP (Vinhibit_redisplay))
10372 return 0;
10373
10374 /* Nil means don't try to resize. */
10375 if (NILP (Vresize_mini_windows)
10376 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10377 return 0;
10378
10379 if (!FRAME_MINIBUF_ONLY_P (f))
10380 {
10381 struct it it;
10382 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10383 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10384 int height;
10385 EMACS_INT max_height;
10386 int unit = FRAME_LINE_HEIGHT (f);
10387 struct text_pos start;
10388 struct buffer *old_current_buffer = NULL;
10389
10390 if (current_buffer != XBUFFER (w->buffer))
10391 {
10392 old_current_buffer = current_buffer;
10393 set_buffer_internal (XBUFFER (w->buffer));
10394 }
10395
10396 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10397
10398 /* Compute the max. number of lines specified by the user. */
10399 if (FLOATP (Vmax_mini_window_height))
10400 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10401 else if (INTEGERP (Vmax_mini_window_height))
10402 max_height = XINT (Vmax_mini_window_height);
10403 else
10404 max_height = total_height / 4;
10405
10406 /* Correct that max. height if it's bogus. */
10407 max_height = clip_to_bounds (1, max_height, total_height);
10408
10409 /* Find out the height of the text in the window. */
10410 if (it.line_wrap == TRUNCATE)
10411 height = 1;
10412 else
10413 {
10414 last_height = 0;
10415 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10416 if (it.max_ascent == 0 && it.max_descent == 0)
10417 height = it.current_y + last_height;
10418 else
10419 height = it.current_y + it.max_ascent + it.max_descent;
10420 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10421 height = (height + unit - 1) / unit;
10422 }
10423
10424 /* Compute a suitable window start. */
10425 if (height > max_height)
10426 {
10427 height = max_height;
10428 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10429 move_it_vertically_backward (&it, (height - 1) * unit);
10430 start = it.current.pos;
10431 }
10432 else
10433 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10434 SET_MARKER_FROM_TEXT_POS (w->start, start);
10435
10436 if (EQ (Vresize_mini_windows, Qgrow_only))
10437 {
10438 /* Let it grow only, until we display an empty message, in which
10439 case the window shrinks again. */
10440 if (height > WINDOW_TOTAL_LINES (w))
10441 {
10442 int old_height = WINDOW_TOTAL_LINES (w);
10443 freeze_window_starts (f, 1);
10444 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10445 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10446 }
10447 else if (height < WINDOW_TOTAL_LINES (w)
10448 && (exact_p || BEGV == ZV))
10449 {
10450 int old_height = WINDOW_TOTAL_LINES (w);
10451 freeze_window_starts (f, 0);
10452 shrink_mini_window (w);
10453 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10454 }
10455 }
10456 else
10457 {
10458 /* Always resize to exact size needed. */
10459 if (height > WINDOW_TOTAL_LINES (w))
10460 {
10461 int old_height = WINDOW_TOTAL_LINES (w);
10462 freeze_window_starts (f, 1);
10463 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10464 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10465 }
10466 else if (height < WINDOW_TOTAL_LINES (w))
10467 {
10468 int old_height = WINDOW_TOTAL_LINES (w);
10469 freeze_window_starts (f, 0);
10470 shrink_mini_window (w);
10471
10472 if (height)
10473 {
10474 freeze_window_starts (f, 1);
10475 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10476 }
10477
10478 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10479 }
10480 }
10481
10482 if (old_current_buffer)
10483 set_buffer_internal (old_current_buffer);
10484 }
10485
10486 return window_height_changed_p;
10487 }
10488
10489
10490 /* Value is the current message, a string, or nil if there is no
10491 current message. */
10492
10493 Lisp_Object
10494 current_message (void)
10495 {
10496 Lisp_Object msg;
10497
10498 if (!BUFFERP (echo_area_buffer[0]))
10499 msg = Qnil;
10500 else
10501 {
10502 with_echo_area_buffer (0, 0, current_message_1,
10503 (intptr_t) &msg, Qnil, 0, 0);
10504 if (NILP (msg))
10505 echo_area_buffer[0] = Qnil;
10506 }
10507
10508 return msg;
10509 }
10510
10511
10512 static int
10513 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10514 {
10515 intptr_t i1 = a1;
10516 Lisp_Object *msg = (Lisp_Object *) i1;
10517
10518 if (Z > BEG)
10519 *msg = make_buffer_string (BEG, Z, 1);
10520 else
10521 *msg = Qnil;
10522 return 0;
10523 }
10524
10525
10526 /* Push the current message on Vmessage_stack for later restoration
10527 by restore_message. Value is non-zero if the current message isn't
10528 empty. This is a relatively infrequent operation, so it's not
10529 worth optimizing. */
10530
10531 bool
10532 push_message (void)
10533 {
10534 Lisp_Object msg = current_message ();
10535 Vmessage_stack = Fcons (msg, Vmessage_stack);
10536 return STRINGP (msg);
10537 }
10538
10539
10540 /* Restore message display from the top of Vmessage_stack. */
10541
10542 void
10543 restore_message (void)
10544 {
10545 Lisp_Object msg;
10546
10547 eassert (CONSP (Vmessage_stack));
10548 msg = XCAR (Vmessage_stack);
10549 if (STRINGP (msg))
10550 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10551 else
10552 message3_nolog (msg, 0, 0);
10553 }
10554
10555
10556 /* Handler for record_unwind_protect calling pop_message. */
10557
10558 Lisp_Object
10559 pop_message_unwind (Lisp_Object dummy)
10560 {
10561 pop_message ();
10562 return Qnil;
10563 }
10564
10565 /* Pop the top-most entry off Vmessage_stack. */
10566
10567 static void
10568 pop_message (void)
10569 {
10570 eassert (CONSP (Vmessage_stack));
10571 Vmessage_stack = XCDR (Vmessage_stack);
10572 }
10573
10574
10575 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10576 exits. If the stack is not empty, we have a missing pop_message
10577 somewhere. */
10578
10579 void
10580 check_message_stack (void)
10581 {
10582 if (!NILP (Vmessage_stack))
10583 emacs_abort ();
10584 }
10585
10586
10587 /* Truncate to NCHARS what will be displayed in the echo area the next
10588 time we display it---but don't redisplay it now. */
10589
10590 void
10591 truncate_echo_area (ptrdiff_t nchars)
10592 {
10593 if (nchars == 0)
10594 echo_area_buffer[0] = Qnil;
10595 /* A null message buffer means that the frame hasn't really been
10596 initialized yet. Error messages get reported properly by
10597 cmd_error, so this must be just an informative message; toss it. */
10598 else if (!noninteractive
10599 && INTERACTIVE
10600 && !NILP (echo_area_buffer[0]))
10601 {
10602 struct frame *sf = SELECTED_FRAME ();
10603 if (FRAME_MESSAGE_BUF (sf))
10604 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10605 }
10606 }
10607
10608
10609 /* Helper function for truncate_echo_area. Truncate the current
10610 message to at most NCHARS characters. */
10611
10612 static int
10613 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10614 {
10615 if (BEG + nchars < Z)
10616 del_range (BEG + nchars, Z);
10617 if (Z == BEG)
10618 echo_area_buffer[0] = Qnil;
10619 return 0;
10620 }
10621
10622 /* Set the current message to a substring of S or STRING.
10623
10624 If STRING is a Lisp string, set the message to the first NBYTES
10625 bytes from STRING. NBYTES zero means use the whole string. If
10626 STRING is multibyte, the message will be displayed multibyte.
10627
10628 If S is not null, set the message to the first LEN bytes of S. LEN
10629 zero means use the whole string. MULTIBYTE_P non-zero means S is
10630 multibyte. Display the message multibyte in that case.
10631
10632 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10633 to t before calling set_message_1 (which calls insert).
10634 */
10635
10636 static void
10637 set_message (const char *s, Lisp_Object string,
10638 ptrdiff_t nbytes, int multibyte_p)
10639 {
10640 message_enable_multibyte
10641 = ((s && multibyte_p)
10642 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10643
10644 with_echo_area_buffer (0, -1, set_message_1,
10645 (intptr_t) s, string, nbytes, multibyte_p);
10646 message_buf_print = 0;
10647 help_echo_showing_p = 0;
10648
10649 if (STRINGP (Vdebug_on_message)
10650 && fast_string_match (Vdebug_on_message, string) >= 0)
10651 call_debugger (list2 (Qerror, string));
10652 }
10653
10654
10655 /* Helper function for set_message. Arguments have the same meaning
10656 as there, with A1 corresponding to S and A2 corresponding to STRING
10657 This function is called with the echo area buffer being
10658 current. */
10659
10660 static int
10661 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10662 {
10663 intptr_t i1 = a1;
10664 const char *s = (const char *) i1;
10665 const unsigned char *msg = (const unsigned char *) s;
10666 Lisp_Object string = a2;
10667
10668 /* Change multibyteness of the echo buffer appropriately. */
10669 if (message_enable_multibyte
10670 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10671 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10672
10673 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10674 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10675 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10676
10677 /* Insert new message at BEG. */
10678 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10679
10680 if (STRINGP (string))
10681 {
10682 ptrdiff_t nchars;
10683
10684 if (nbytes == 0)
10685 nbytes = SBYTES (string);
10686 nchars = string_byte_to_char (string, nbytes);
10687
10688 /* This function takes care of single/multibyte conversion. We
10689 just have to ensure that the echo area buffer has the right
10690 setting of enable_multibyte_characters. */
10691 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10692 }
10693 else if (s)
10694 {
10695 if (nbytes == 0)
10696 nbytes = strlen (s);
10697
10698 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10699 {
10700 /* Convert from multi-byte to single-byte. */
10701 ptrdiff_t i;
10702 int c, n;
10703 char work[1];
10704
10705 /* Convert a multibyte string to single-byte. */
10706 for (i = 0; i < nbytes; i += n)
10707 {
10708 c = string_char_and_length (msg + i, &n);
10709 work[0] = (ASCII_CHAR_P (c)
10710 ? c
10711 : multibyte_char_to_unibyte (c));
10712 insert_1_both (work, 1, 1, 1, 0, 0);
10713 }
10714 }
10715 else if (!multibyte_p
10716 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10717 {
10718 /* Convert from single-byte to multi-byte. */
10719 ptrdiff_t i;
10720 int c, n;
10721 unsigned char str[MAX_MULTIBYTE_LENGTH];
10722
10723 /* Convert a single-byte string to multibyte. */
10724 for (i = 0; i < nbytes; i++)
10725 {
10726 c = msg[i];
10727 MAKE_CHAR_MULTIBYTE (c);
10728 n = CHAR_STRING (c, str);
10729 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10730 }
10731 }
10732 else
10733 insert_1 (s, nbytes, 1, 0, 0);
10734 }
10735
10736 return 0;
10737 }
10738
10739
10740 /* Clear messages. CURRENT_P non-zero means clear the current
10741 message. LAST_DISPLAYED_P non-zero means clear the message
10742 last displayed. */
10743
10744 void
10745 clear_message (int current_p, int last_displayed_p)
10746 {
10747 if (current_p)
10748 {
10749 echo_area_buffer[0] = Qnil;
10750 message_cleared_p = 1;
10751 }
10752
10753 if (last_displayed_p)
10754 echo_area_buffer[1] = Qnil;
10755
10756 message_buf_print = 0;
10757 }
10758
10759 /* Clear garbaged frames.
10760
10761 This function is used where the old redisplay called
10762 redraw_garbaged_frames which in turn called redraw_frame which in
10763 turn called clear_frame. The call to clear_frame was a source of
10764 flickering. I believe a clear_frame is not necessary. It should
10765 suffice in the new redisplay to invalidate all current matrices,
10766 and ensure a complete redisplay of all windows. */
10767
10768 static void
10769 clear_garbaged_frames (void)
10770 {
10771 if (frame_garbaged)
10772 {
10773 Lisp_Object tail, frame;
10774 int changed_count = 0;
10775
10776 FOR_EACH_FRAME (tail, frame)
10777 {
10778 struct frame *f = XFRAME (frame);
10779
10780 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10781 {
10782 if (f->resized_p)
10783 {
10784 redraw_frame (f);
10785 f->force_flush_display_p = 1;
10786 }
10787 clear_current_matrices (f);
10788 changed_count++;
10789 f->garbaged = 0;
10790 f->resized_p = 0;
10791 }
10792 }
10793
10794 frame_garbaged = 0;
10795 if (changed_count)
10796 ++windows_or_buffers_changed;
10797 }
10798 }
10799
10800
10801 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10802 is non-zero update selected_frame. Value is non-zero if the
10803 mini-windows height has been changed. */
10804
10805 static int
10806 echo_area_display (int update_frame_p)
10807 {
10808 Lisp_Object mini_window;
10809 struct window *w;
10810 struct frame *f;
10811 int window_height_changed_p = 0;
10812 struct frame *sf = SELECTED_FRAME ();
10813
10814 mini_window = FRAME_MINIBUF_WINDOW (sf);
10815 w = XWINDOW (mini_window);
10816 f = XFRAME (WINDOW_FRAME (w));
10817
10818 /* Don't display if frame is invisible or not yet initialized. */
10819 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10820 return 0;
10821
10822 #ifdef HAVE_WINDOW_SYSTEM
10823 /* When Emacs starts, selected_frame may be the initial terminal
10824 frame. If we let this through, a message would be displayed on
10825 the terminal. */
10826 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10827 return 0;
10828 #endif /* HAVE_WINDOW_SYSTEM */
10829
10830 /* Redraw garbaged frames. */
10831 clear_garbaged_frames ();
10832
10833 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10834 {
10835 echo_area_window = mini_window;
10836 window_height_changed_p = display_echo_area (w);
10837 w->must_be_updated_p = 1;
10838
10839 /* Update the display, unless called from redisplay_internal.
10840 Also don't update the screen during redisplay itself. The
10841 update will happen at the end of redisplay, and an update
10842 here could cause confusion. */
10843 if (update_frame_p && !redisplaying_p)
10844 {
10845 int n = 0;
10846
10847 /* If the display update has been interrupted by pending
10848 input, update mode lines in the frame. Due to the
10849 pending input, it might have been that redisplay hasn't
10850 been called, so that mode lines above the echo area are
10851 garbaged. This looks odd, so we prevent it here. */
10852 if (!display_completed)
10853 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10854
10855 if (window_height_changed_p
10856 /* Don't do this if Emacs is shutting down. Redisplay
10857 needs to run hooks. */
10858 && !NILP (Vrun_hooks))
10859 {
10860 /* Must update other windows. Likewise as in other
10861 cases, don't let this update be interrupted by
10862 pending input. */
10863 ptrdiff_t count = SPECPDL_INDEX ();
10864 specbind (Qredisplay_dont_pause, Qt);
10865 windows_or_buffers_changed = 1;
10866 redisplay_internal ();
10867 unbind_to (count, Qnil);
10868 }
10869 else if (FRAME_WINDOW_P (f) && n == 0)
10870 {
10871 /* Window configuration is the same as before.
10872 Can do with a display update of the echo area,
10873 unless we displayed some mode lines. */
10874 update_single_window (w, 1);
10875 FRAME_RIF (f)->flush_display (f);
10876 }
10877 else
10878 update_frame (f, 1, 1);
10879
10880 /* If cursor is in the echo area, make sure that the next
10881 redisplay displays the minibuffer, so that the cursor will
10882 be replaced with what the minibuffer wants. */
10883 if (cursor_in_echo_area)
10884 ++windows_or_buffers_changed;
10885 }
10886 }
10887 else if (!EQ (mini_window, selected_window))
10888 windows_or_buffers_changed++;
10889
10890 /* Last displayed message is now the current message. */
10891 echo_area_buffer[1] = echo_area_buffer[0];
10892 /* Inform read_char that we're not echoing. */
10893 echo_message_buffer = Qnil;
10894
10895 /* Prevent redisplay optimization in redisplay_internal by resetting
10896 this_line_start_pos. This is done because the mini-buffer now
10897 displays the message instead of its buffer text. */
10898 if (EQ (mini_window, selected_window))
10899 CHARPOS (this_line_start_pos) = 0;
10900
10901 return window_height_changed_p;
10902 }
10903
10904 /* Nonzero if the current window's buffer is shown in more than one
10905 window and was modified since last redisplay. */
10906
10907 static int
10908 buffer_shared_and_changed (void)
10909 {
10910 return (buffer_window_count (current_buffer) > 1
10911 && UNCHANGED_MODIFIED < MODIFF);
10912 }
10913
10914 /* Nonzero if W doesn't reflect the actual state of current buffer due
10915 to its text or overlays change. FIXME: this may be called when
10916 XBUFFER (w->buffer) != current_buffer, which looks suspicious. */
10917
10918 static int
10919 window_outdated (struct window *w)
10920 {
10921 return (w->last_modified < MODIFF
10922 || w->last_overlay_modified < OVERLAY_MODIFF);
10923 }
10924
10925 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10926 is enabled and mark of W's buffer was changed since last W's update. */
10927
10928 static int
10929 window_buffer_changed (struct window *w)
10930 {
10931 struct buffer *b = XBUFFER (w->buffer);
10932
10933 eassert (BUFFER_LIVE_P (b));
10934
10935 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10936 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10937 != !NILP (w->region_showing)));
10938 }
10939
10940 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10941
10942 static int
10943 mode_line_update_needed (struct window *w)
10944 {
10945 return (!NILP (w->column_number_displayed)
10946 && !(PT == w->last_point && !window_outdated (w))
10947 && (XFASTINT (w->column_number_displayed) != current_column ()));
10948 }
10949
10950 /***********************************************************************
10951 Mode Lines and Frame Titles
10952 ***********************************************************************/
10953
10954 /* A buffer for constructing non-propertized mode-line strings and
10955 frame titles in it; allocated from the heap in init_xdisp and
10956 resized as needed in store_mode_line_noprop_char. */
10957
10958 static char *mode_line_noprop_buf;
10959
10960 /* The buffer's end, and a current output position in it. */
10961
10962 static char *mode_line_noprop_buf_end;
10963 static char *mode_line_noprop_ptr;
10964
10965 #define MODE_LINE_NOPROP_LEN(start) \
10966 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10967
10968 static enum {
10969 MODE_LINE_DISPLAY = 0,
10970 MODE_LINE_TITLE,
10971 MODE_LINE_NOPROP,
10972 MODE_LINE_STRING
10973 } mode_line_target;
10974
10975 /* Alist that caches the results of :propertize.
10976 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10977 static Lisp_Object mode_line_proptrans_alist;
10978
10979 /* List of strings making up the mode-line. */
10980 static Lisp_Object mode_line_string_list;
10981
10982 /* Base face property when building propertized mode line string. */
10983 static Lisp_Object mode_line_string_face;
10984 static Lisp_Object mode_line_string_face_prop;
10985
10986
10987 /* Unwind data for mode line strings */
10988
10989 static Lisp_Object Vmode_line_unwind_vector;
10990
10991 static Lisp_Object
10992 format_mode_line_unwind_data (struct frame *target_frame,
10993 struct buffer *obuf,
10994 Lisp_Object owin,
10995 int save_proptrans)
10996 {
10997 Lisp_Object vector, tmp;
10998
10999 /* Reduce consing by keeping one vector in
11000 Vwith_echo_area_save_vector. */
11001 vector = Vmode_line_unwind_vector;
11002 Vmode_line_unwind_vector = Qnil;
11003
11004 if (NILP (vector))
11005 vector = Fmake_vector (make_number (10), Qnil);
11006
11007 ASET (vector, 0, make_number (mode_line_target));
11008 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11009 ASET (vector, 2, mode_line_string_list);
11010 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11011 ASET (vector, 4, mode_line_string_face);
11012 ASET (vector, 5, mode_line_string_face_prop);
11013
11014 if (obuf)
11015 XSETBUFFER (tmp, obuf);
11016 else
11017 tmp = Qnil;
11018 ASET (vector, 6, tmp);
11019 ASET (vector, 7, owin);
11020 if (target_frame)
11021 {
11022 /* Similarly to `with-selected-window', if the operation selects
11023 a window on another frame, we must restore that frame's
11024 selected window, and (for a tty) the top-frame. */
11025 ASET (vector, 8, target_frame->selected_window);
11026 if (FRAME_TERMCAP_P (target_frame))
11027 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11028 }
11029
11030 return vector;
11031 }
11032
11033 static Lisp_Object
11034 unwind_format_mode_line (Lisp_Object vector)
11035 {
11036 Lisp_Object old_window = AREF (vector, 7);
11037 Lisp_Object target_frame_window = AREF (vector, 8);
11038 Lisp_Object old_top_frame = AREF (vector, 9);
11039
11040 mode_line_target = XINT (AREF (vector, 0));
11041 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11042 mode_line_string_list = AREF (vector, 2);
11043 if (! EQ (AREF (vector, 3), Qt))
11044 mode_line_proptrans_alist = AREF (vector, 3);
11045 mode_line_string_face = AREF (vector, 4);
11046 mode_line_string_face_prop = AREF (vector, 5);
11047
11048 /* Select window before buffer, since it may change the buffer. */
11049 if (!NILP (old_window))
11050 {
11051 /* If the operation that we are unwinding had selected a window
11052 on a different frame, reset its frame-selected-window. For a
11053 text terminal, reset its top-frame if necessary. */
11054 if (!NILP (target_frame_window))
11055 {
11056 Lisp_Object frame
11057 = WINDOW_FRAME (XWINDOW (target_frame_window));
11058
11059 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11060 Fselect_window (target_frame_window, Qt);
11061
11062 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11063 Fselect_frame (old_top_frame, Qt);
11064 }
11065
11066 Fselect_window (old_window, Qt);
11067 }
11068
11069 if (!NILP (AREF (vector, 6)))
11070 {
11071 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11072 ASET (vector, 6, Qnil);
11073 }
11074
11075 Vmode_line_unwind_vector = vector;
11076 return Qnil;
11077 }
11078
11079
11080 /* Store a single character C for the frame title in mode_line_noprop_buf.
11081 Re-allocate mode_line_noprop_buf if necessary. */
11082
11083 static void
11084 store_mode_line_noprop_char (char c)
11085 {
11086 /* If output position has reached the end of the allocated buffer,
11087 increase the buffer's size. */
11088 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11089 {
11090 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11091 ptrdiff_t size = len;
11092 mode_line_noprop_buf =
11093 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11094 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11095 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11096 }
11097
11098 *mode_line_noprop_ptr++ = c;
11099 }
11100
11101
11102 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11103 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11104 characters that yield more columns than PRECISION; PRECISION <= 0
11105 means copy the whole string. Pad with spaces until FIELD_WIDTH
11106 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11107 pad. Called from display_mode_element when it is used to build a
11108 frame title. */
11109
11110 static int
11111 store_mode_line_noprop (const char *string, int field_width, int precision)
11112 {
11113 const unsigned char *str = (const unsigned char *) string;
11114 int n = 0;
11115 ptrdiff_t dummy, nbytes;
11116
11117 /* Copy at most PRECISION chars from STR. */
11118 nbytes = strlen (string);
11119 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11120 while (nbytes--)
11121 store_mode_line_noprop_char (*str++);
11122
11123 /* Fill up with spaces until FIELD_WIDTH reached. */
11124 while (field_width > 0
11125 && n < field_width)
11126 {
11127 store_mode_line_noprop_char (' ');
11128 ++n;
11129 }
11130
11131 return n;
11132 }
11133
11134 /***********************************************************************
11135 Frame Titles
11136 ***********************************************************************/
11137
11138 #ifdef HAVE_WINDOW_SYSTEM
11139
11140 /* Set the title of FRAME, if it has changed. The title format is
11141 Vicon_title_format if FRAME is iconified, otherwise it is
11142 frame_title_format. */
11143
11144 static void
11145 x_consider_frame_title (Lisp_Object frame)
11146 {
11147 struct frame *f = XFRAME (frame);
11148
11149 if (FRAME_WINDOW_P (f)
11150 || FRAME_MINIBUF_ONLY_P (f)
11151 || f->explicit_name)
11152 {
11153 /* Do we have more than one visible frame on this X display? */
11154 Lisp_Object tail, other_frame, fmt;
11155 ptrdiff_t title_start;
11156 char *title;
11157 ptrdiff_t len;
11158 struct it it;
11159 ptrdiff_t count = SPECPDL_INDEX ();
11160
11161 FOR_EACH_FRAME (tail, other_frame)
11162 {
11163 struct frame *tf = XFRAME (other_frame);
11164
11165 if (tf != f
11166 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11167 && !FRAME_MINIBUF_ONLY_P (tf)
11168 && !EQ (other_frame, tip_frame)
11169 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11170 break;
11171 }
11172
11173 /* Set global variable indicating that multiple frames exist. */
11174 multiple_frames = CONSP (tail);
11175
11176 /* Switch to the buffer of selected window of the frame. Set up
11177 mode_line_target so that display_mode_element will output into
11178 mode_line_noprop_buf; then display the title. */
11179 record_unwind_protect (unwind_format_mode_line,
11180 format_mode_line_unwind_data
11181 (f, current_buffer, selected_window, 0));
11182
11183 Fselect_window (f->selected_window, Qt);
11184 set_buffer_internal_1
11185 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11186 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11187
11188 mode_line_target = MODE_LINE_TITLE;
11189 title_start = MODE_LINE_NOPROP_LEN (0);
11190 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11191 NULL, DEFAULT_FACE_ID);
11192 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11193 len = MODE_LINE_NOPROP_LEN (title_start);
11194 title = mode_line_noprop_buf + title_start;
11195 unbind_to (count, Qnil);
11196
11197 /* Set the title only if it's changed. This avoids consing in
11198 the common case where it hasn't. (If it turns out that we've
11199 already wasted too much time by walking through the list with
11200 display_mode_element, then we might need to optimize at a
11201 higher level than this.) */
11202 if (! STRINGP (f->name)
11203 || SBYTES (f->name) != len
11204 || memcmp (title, SDATA (f->name), len) != 0)
11205 x_implicitly_set_name (f, make_string (title, len), Qnil);
11206 }
11207 }
11208
11209 #endif /* not HAVE_WINDOW_SYSTEM */
11210
11211 \f
11212 /***********************************************************************
11213 Menu Bars
11214 ***********************************************************************/
11215
11216
11217 /* Prepare for redisplay by updating menu-bar item lists when
11218 appropriate. This can call eval. */
11219
11220 void
11221 prepare_menu_bars (void)
11222 {
11223 int all_windows;
11224 struct gcpro gcpro1, gcpro2;
11225 struct frame *f;
11226 Lisp_Object tooltip_frame;
11227
11228 #ifdef HAVE_WINDOW_SYSTEM
11229 tooltip_frame = tip_frame;
11230 #else
11231 tooltip_frame = Qnil;
11232 #endif
11233
11234 /* Update all frame titles based on their buffer names, etc. We do
11235 this before the menu bars so that the buffer-menu will show the
11236 up-to-date frame titles. */
11237 #ifdef HAVE_WINDOW_SYSTEM
11238 if (windows_or_buffers_changed || update_mode_lines)
11239 {
11240 Lisp_Object tail, frame;
11241
11242 FOR_EACH_FRAME (tail, frame)
11243 {
11244 f = XFRAME (frame);
11245 if (!EQ (frame, tooltip_frame)
11246 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11247 x_consider_frame_title (frame);
11248 }
11249 }
11250 #endif /* HAVE_WINDOW_SYSTEM */
11251
11252 /* Update the menu bar item lists, if appropriate. This has to be
11253 done before any actual redisplay or generation of display lines. */
11254 all_windows = (update_mode_lines
11255 || buffer_shared_and_changed ()
11256 || windows_or_buffers_changed);
11257 if (all_windows)
11258 {
11259 Lisp_Object tail, frame;
11260 ptrdiff_t count = SPECPDL_INDEX ();
11261 /* 1 means that update_menu_bar has run its hooks
11262 so any further calls to update_menu_bar shouldn't do so again. */
11263 int menu_bar_hooks_run = 0;
11264
11265 record_unwind_save_match_data ();
11266
11267 FOR_EACH_FRAME (tail, frame)
11268 {
11269 f = XFRAME (frame);
11270
11271 /* Ignore tooltip frame. */
11272 if (EQ (frame, tooltip_frame))
11273 continue;
11274
11275 /* If a window on this frame changed size, report that to
11276 the user and clear the size-change flag. */
11277 if (FRAME_WINDOW_SIZES_CHANGED (f))
11278 {
11279 Lisp_Object functions;
11280
11281 /* Clear flag first in case we get an error below. */
11282 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11283 functions = Vwindow_size_change_functions;
11284 GCPRO2 (tail, functions);
11285
11286 while (CONSP (functions))
11287 {
11288 if (!EQ (XCAR (functions), Qt))
11289 call1 (XCAR (functions), frame);
11290 functions = XCDR (functions);
11291 }
11292 UNGCPRO;
11293 }
11294
11295 GCPRO1 (tail);
11296 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11297 #ifdef HAVE_WINDOW_SYSTEM
11298 update_tool_bar (f, 0);
11299 #endif
11300 #ifdef HAVE_NS
11301 if (windows_or_buffers_changed
11302 && FRAME_NS_P (f))
11303 ns_set_doc_edited
11304 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11305 #endif
11306 UNGCPRO;
11307 }
11308
11309 unbind_to (count, Qnil);
11310 }
11311 else
11312 {
11313 struct frame *sf = SELECTED_FRAME ();
11314 update_menu_bar (sf, 1, 0);
11315 #ifdef HAVE_WINDOW_SYSTEM
11316 update_tool_bar (sf, 1);
11317 #endif
11318 }
11319 }
11320
11321
11322 /* Update the menu bar item list for frame F. This has to be done
11323 before we start to fill in any display lines, because it can call
11324 eval.
11325
11326 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11327
11328 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11329 already ran the menu bar hooks for this redisplay, so there
11330 is no need to run them again. The return value is the
11331 updated value of this flag, to pass to the next call. */
11332
11333 static int
11334 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11335 {
11336 Lisp_Object window;
11337 register struct window *w;
11338
11339 /* If called recursively during a menu update, do nothing. This can
11340 happen when, for instance, an activate-menubar-hook causes a
11341 redisplay. */
11342 if (inhibit_menubar_update)
11343 return hooks_run;
11344
11345 window = FRAME_SELECTED_WINDOW (f);
11346 w = XWINDOW (window);
11347
11348 if (FRAME_WINDOW_P (f)
11349 ?
11350 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11351 || defined (HAVE_NS) || defined (USE_GTK)
11352 FRAME_EXTERNAL_MENU_BAR (f)
11353 #else
11354 FRAME_MENU_BAR_LINES (f) > 0
11355 #endif
11356 : FRAME_MENU_BAR_LINES (f) > 0)
11357 {
11358 /* If the user has switched buffers or windows, we need to
11359 recompute to reflect the new bindings. But we'll
11360 recompute when update_mode_lines is set too; that means
11361 that people can use force-mode-line-update to request
11362 that the menu bar be recomputed. The adverse effect on
11363 the rest of the redisplay algorithm is about the same as
11364 windows_or_buffers_changed anyway. */
11365 if (windows_or_buffers_changed
11366 /* This used to test w->update_mode_line, but we believe
11367 there is no need to recompute the menu in that case. */
11368 || update_mode_lines
11369 || window_buffer_changed (w))
11370 {
11371 struct buffer *prev = current_buffer;
11372 ptrdiff_t count = SPECPDL_INDEX ();
11373
11374 specbind (Qinhibit_menubar_update, Qt);
11375
11376 set_buffer_internal_1 (XBUFFER (w->buffer));
11377 if (save_match_data)
11378 record_unwind_save_match_data ();
11379 if (NILP (Voverriding_local_map_menu_flag))
11380 {
11381 specbind (Qoverriding_terminal_local_map, Qnil);
11382 specbind (Qoverriding_local_map, Qnil);
11383 }
11384
11385 if (!hooks_run)
11386 {
11387 /* Run the Lucid hook. */
11388 safe_run_hooks (Qactivate_menubar_hook);
11389
11390 /* If it has changed current-menubar from previous value,
11391 really recompute the menu-bar from the value. */
11392 if (! NILP (Vlucid_menu_bar_dirty_flag))
11393 call0 (Qrecompute_lucid_menubar);
11394
11395 safe_run_hooks (Qmenu_bar_update_hook);
11396
11397 hooks_run = 1;
11398 }
11399
11400 XSETFRAME (Vmenu_updating_frame, f);
11401 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11402
11403 /* Redisplay the menu bar in case we changed it. */
11404 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11405 || defined (HAVE_NS) || defined (USE_GTK)
11406 if (FRAME_WINDOW_P (f))
11407 {
11408 #if defined (HAVE_NS)
11409 /* All frames on Mac OS share the same menubar. So only
11410 the selected frame should be allowed to set it. */
11411 if (f == SELECTED_FRAME ())
11412 #endif
11413 set_frame_menubar (f, 0, 0);
11414 }
11415 else
11416 /* On a terminal screen, the menu bar is an ordinary screen
11417 line, and this makes it get updated. */
11418 w->update_mode_line = 1;
11419 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11420 /* In the non-toolkit version, the menu bar is an ordinary screen
11421 line, and this makes it get updated. */
11422 w->update_mode_line = 1;
11423 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11424
11425 unbind_to (count, Qnil);
11426 set_buffer_internal_1 (prev);
11427 }
11428 }
11429
11430 return hooks_run;
11431 }
11432
11433
11434 \f
11435 /***********************************************************************
11436 Output Cursor
11437 ***********************************************************************/
11438
11439 #ifdef HAVE_WINDOW_SYSTEM
11440
11441 /* EXPORT:
11442 Nominal cursor position -- where to draw output.
11443 HPOS and VPOS are window relative glyph matrix coordinates.
11444 X and Y are window relative pixel coordinates. */
11445
11446 struct cursor_pos output_cursor;
11447
11448
11449 /* EXPORT:
11450 Set the global variable output_cursor to CURSOR. All cursor
11451 positions are relative to updated_window. */
11452
11453 void
11454 set_output_cursor (struct cursor_pos *cursor)
11455 {
11456 output_cursor.hpos = cursor->hpos;
11457 output_cursor.vpos = cursor->vpos;
11458 output_cursor.x = cursor->x;
11459 output_cursor.y = cursor->y;
11460 }
11461
11462
11463 /* EXPORT for RIF:
11464 Set a nominal cursor position.
11465
11466 HPOS and VPOS are column/row positions in a window glyph matrix. X
11467 and Y are window text area relative pixel positions.
11468
11469 If this is done during an update, updated_window will contain the
11470 window that is being updated and the position is the future output
11471 cursor position for that window. If updated_window is null, use
11472 selected_window and display the cursor at the given position. */
11473
11474 void
11475 x_cursor_to (int vpos, int hpos, int y, int x)
11476 {
11477 struct window *w;
11478
11479 /* If updated_window is not set, work on selected_window. */
11480 if (updated_window)
11481 w = updated_window;
11482 else
11483 w = XWINDOW (selected_window);
11484
11485 /* Set the output cursor. */
11486 output_cursor.hpos = hpos;
11487 output_cursor.vpos = vpos;
11488 output_cursor.x = x;
11489 output_cursor.y = y;
11490
11491 /* If not called as part of an update, really display the cursor.
11492 This will also set the cursor position of W. */
11493 if (updated_window == NULL)
11494 {
11495 block_input ();
11496 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11497 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11498 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11499 unblock_input ();
11500 }
11501 }
11502
11503 #endif /* HAVE_WINDOW_SYSTEM */
11504
11505 \f
11506 /***********************************************************************
11507 Tool-bars
11508 ***********************************************************************/
11509
11510 #ifdef HAVE_WINDOW_SYSTEM
11511
11512 /* Where the mouse was last time we reported a mouse event. */
11513
11514 FRAME_PTR last_mouse_frame;
11515
11516 /* Tool-bar item index of the item on which a mouse button was pressed
11517 or -1. */
11518
11519 int last_tool_bar_item;
11520
11521 /* Select `frame' temporarily without running all the code in
11522 do_switch_frame.
11523 FIXME: Maybe do_switch_frame should be trimmed down similarly
11524 when `norecord' is set. */
11525 static Lisp_Object
11526 fast_set_selected_frame (Lisp_Object frame)
11527 {
11528 if (!EQ (selected_frame, frame))
11529 {
11530 selected_frame = frame;
11531 selected_window = XFRAME (frame)->selected_window;
11532 }
11533 return Qnil;
11534 }
11535
11536 /* Update the tool-bar item list for frame F. This has to be done
11537 before we start to fill in any display lines. Called from
11538 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11539 and restore it here. */
11540
11541 static void
11542 update_tool_bar (struct frame *f, int save_match_data)
11543 {
11544 #if defined (USE_GTK) || defined (HAVE_NS)
11545 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11546 #else
11547 int do_update = WINDOWP (f->tool_bar_window)
11548 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11549 #endif
11550
11551 if (do_update)
11552 {
11553 Lisp_Object window;
11554 struct window *w;
11555
11556 window = FRAME_SELECTED_WINDOW (f);
11557 w = XWINDOW (window);
11558
11559 /* If the user has switched buffers or windows, we need to
11560 recompute to reflect the new bindings. But we'll
11561 recompute when update_mode_lines is set too; that means
11562 that people can use force-mode-line-update to request
11563 that the menu bar be recomputed. The adverse effect on
11564 the rest of the redisplay algorithm is about the same as
11565 windows_or_buffers_changed anyway. */
11566 if (windows_or_buffers_changed
11567 || w->update_mode_line
11568 || update_mode_lines
11569 || window_buffer_changed (w))
11570 {
11571 struct buffer *prev = current_buffer;
11572 ptrdiff_t count = SPECPDL_INDEX ();
11573 Lisp_Object frame, new_tool_bar;
11574 int new_n_tool_bar;
11575 struct gcpro gcpro1;
11576
11577 /* Set current_buffer to the buffer of the selected
11578 window of the frame, so that we get the right local
11579 keymaps. */
11580 set_buffer_internal_1 (XBUFFER (w->buffer));
11581
11582 /* Save match data, if we must. */
11583 if (save_match_data)
11584 record_unwind_save_match_data ();
11585
11586 /* Make sure that we don't accidentally use bogus keymaps. */
11587 if (NILP (Voverriding_local_map_menu_flag))
11588 {
11589 specbind (Qoverriding_terminal_local_map, Qnil);
11590 specbind (Qoverriding_local_map, Qnil);
11591 }
11592
11593 GCPRO1 (new_tool_bar);
11594
11595 /* We must temporarily set the selected frame to this frame
11596 before calling tool_bar_items, because the calculation of
11597 the tool-bar keymap uses the selected frame (see
11598 `tool-bar-make-keymap' in tool-bar.el). */
11599 eassert (EQ (selected_window,
11600 /* Since we only explicitly preserve selected_frame,
11601 check that selected_window would be redundant. */
11602 XFRAME (selected_frame)->selected_window));
11603 record_unwind_protect (fast_set_selected_frame, selected_frame);
11604 XSETFRAME (frame, f);
11605 fast_set_selected_frame (frame);
11606
11607 /* Build desired tool-bar items from keymaps. */
11608 new_tool_bar
11609 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11610 &new_n_tool_bar);
11611
11612 /* Redisplay the tool-bar if we changed it. */
11613 if (new_n_tool_bar != f->n_tool_bar_items
11614 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11615 {
11616 /* Redisplay that happens asynchronously due to an expose event
11617 may access f->tool_bar_items. Make sure we update both
11618 variables within BLOCK_INPUT so no such event interrupts. */
11619 block_input ();
11620 fset_tool_bar_items (f, new_tool_bar);
11621 f->n_tool_bar_items = new_n_tool_bar;
11622 w->update_mode_line = 1;
11623 unblock_input ();
11624 }
11625
11626 UNGCPRO;
11627
11628 unbind_to (count, Qnil);
11629 set_buffer_internal_1 (prev);
11630 }
11631 }
11632 }
11633
11634
11635 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11636 F's desired tool-bar contents. F->tool_bar_items must have
11637 been set up previously by calling prepare_menu_bars. */
11638
11639 static void
11640 build_desired_tool_bar_string (struct frame *f)
11641 {
11642 int i, size, size_needed;
11643 struct gcpro gcpro1, gcpro2, gcpro3;
11644 Lisp_Object image, plist, props;
11645
11646 image = plist = props = Qnil;
11647 GCPRO3 (image, plist, props);
11648
11649 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11650 Otherwise, make a new string. */
11651
11652 /* The size of the string we might be able to reuse. */
11653 size = (STRINGP (f->desired_tool_bar_string)
11654 ? SCHARS (f->desired_tool_bar_string)
11655 : 0);
11656
11657 /* We need one space in the string for each image. */
11658 size_needed = f->n_tool_bar_items;
11659
11660 /* Reuse f->desired_tool_bar_string, if possible. */
11661 if (size < size_needed || NILP (f->desired_tool_bar_string))
11662 fset_desired_tool_bar_string
11663 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11664 else
11665 {
11666 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11667 Fremove_text_properties (make_number (0), make_number (size),
11668 props, f->desired_tool_bar_string);
11669 }
11670
11671 /* Put a `display' property on the string for the images to display,
11672 put a `menu_item' property on tool-bar items with a value that
11673 is the index of the item in F's tool-bar item vector. */
11674 for (i = 0; i < f->n_tool_bar_items; ++i)
11675 {
11676 #define PROP(IDX) \
11677 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11678
11679 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11680 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11681 int hmargin, vmargin, relief, idx, end;
11682
11683 /* If image is a vector, choose the image according to the
11684 button state. */
11685 image = PROP (TOOL_BAR_ITEM_IMAGES);
11686 if (VECTORP (image))
11687 {
11688 if (enabled_p)
11689 idx = (selected_p
11690 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11691 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11692 else
11693 idx = (selected_p
11694 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11695 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11696
11697 eassert (ASIZE (image) >= idx);
11698 image = AREF (image, idx);
11699 }
11700 else
11701 idx = -1;
11702
11703 /* Ignore invalid image specifications. */
11704 if (!valid_image_p (image))
11705 continue;
11706
11707 /* Display the tool-bar button pressed, or depressed. */
11708 plist = Fcopy_sequence (XCDR (image));
11709
11710 /* Compute margin and relief to draw. */
11711 relief = (tool_bar_button_relief >= 0
11712 ? tool_bar_button_relief
11713 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11714 hmargin = vmargin = relief;
11715
11716 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11717 INT_MAX - max (hmargin, vmargin)))
11718 {
11719 hmargin += XFASTINT (Vtool_bar_button_margin);
11720 vmargin += XFASTINT (Vtool_bar_button_margin);
11721 }
11722 else if (CONSP (Vtool_bar_button_margin))
11723 {
11724 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11725 INT_MAX - hmargin))
11726 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11727
11728 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11729 INT_MAX - vmargin))
11730 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11731 }
11732
11733 if (auto_raise_tool_bar_buttons_p)
11734 {
11735 /* Add a `:relief' property to the image spec if the item is
11736 selected. */
11737 if (selected_p)
11738 {
11739 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11740 hmargin -= relief;
11741 vmargin -= relief;
11742 }
11743 }
11744 else
11745 {
11746 /* If image is selected, display it pressed, i.e. with a
11747 negative relief. If it's not selected, display it with a
11748 raised relief. */
11749 plist = Fplist_put (plist, QCrelief,
11750 (selected_p
11751 ? make_number (-relief)
11752 : make_number (relief)));
11753 hmargin -= relief;
11754 vmargin -= relief;
11755 }
11756
11757 /* Put a margin around the image. */
11758 if (hmargin || vmargin)
11759 {
11760 if (hmargin == vmargin)
11761 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11762 else
11763 plist = Fplist_put (plist, QCmargin,
11764 Fcons (make_number (hmargin),
11765 make_number (vmargin)));
11766 }
11767
11768 /* If button is not enabled, and we don't have special images
11769 for the disabled state, make the image appear disabled by
11770 applying an appropriate algorithm to it. */
11771 if (!enabled_p && idx < 0)
11772 plist = Fplist_put (plist, QCconversion, Qdisabled);
11773
11774 /* Put a `display' text property on the string for the image to
11775 display. Put a `menu-item' property on the string that gives
11776 the start of this item's properties in the tool-bar items
11777 vector. */
11778 image = Fcons (Qimage, plist);
11779 props = list4 (Qdisplay, image,
11780 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11781
11782 /* Let the last image hide all remaining spaces in the tool bar
11783 string. The string can be longer than needed when we reuse a
11784 previous string. */
11785 if (i + 1 == f->n_tool_bar_items)
11786 end = SCHARS (f->desired_tool_bar_string);
11787 else
11788 end = i + 1;
11789 Fadd_text_properties (make_number (i), make_number (end),
11790 props, f->desired_tool_bar_string);
11791 #undef PROP
11792 }
11793
11794 UNGCPRO;
11795 }
11796
11797
11798 /* Display one line of the tool-bar of frame IT->f.
11799
11800 HEIGHT specifies the desired height of the tool-bar line.
11801 If the actual height of the glyph row is less than HEIGHT, the
11802 row's height is increased to HEIGHT, and the icons are centered
11803 vertically in the new height.
11804
11805 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11806 count a final empty row in case the tool-bar width exactly matches
11807 the window width.
11808 */
11809
11810 static void
11811 display_tool_bar_line (struct it *it, int height)
11812 {
11813 struct glyph_row *row = it->glyph_row;
11814 int max_x = it->last_visible_x;
11815 struct glyph *last;
11816
11817 prepare_desired_row (row);
11818 row->y = it->current_y;
11819
11820 /* Note that this isn't made use of if the face hasn't a box,
11821 so there's no need to check the face here. */
11822 it->start_of_box_run_p = 1;
11823
11824 while (it->current_x < max_x)
11825 {
11826 int x, n_glyphs_before, i, nglyphs;
11827 struct it it_before;
11828
11829 /* Get the next display element. */
11830 if (!get_next_display_element (it))
11831 {
11832 /* Don't count empty row if we are counting needed tool-bar lines. */
11833 if (height < 0 && !it->hpos)
11834 return;
11835 break;
11836 }
11837
11838 /* Produce glyphs. */
11839 n_glyphs_before = row->used[TEXT_AREA];
11840 it_before = *it;
11841
11842 PRODUCE_GLYPHS (it);
11843
11844 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11845 i = 0;
11846 x = it_before.current_x;
11847 while (i < nglyphs)
11848 {
11849 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11850
11851 if (x + glyph->pixel_width > max_x)
11852 {
11853 /* Glyph doesn't fit on line. Backtrack. */
11854 row->used[TEXT_AREA] = n_glyphs_before;
11855 *it = it_before;
11856 /* If this is the only glyph on this line, it will never fit on the
11857 tool-bar, so skip it. But ensure there is at least one glyph,
11858 so we don't accidentally disable the tool-bar. */
11859 if (n_glyphs_before == 0
11860 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11861 break;
11862 goto out;
11863 }
11864
11865 ++it->hpos;
11866 x += glyph->pixel_width;
11867 ++i;
11868 }
11869
11870 /* Stop at line end. */
11871 if (ITERATOR_AT_END_OF_LINE_P (it))
11872 break;
11873
11874 set_iterator_to_next (it, 1);
11875 }
11876
11877 out:;
11878
11879 row->displays_text_p = row->used[TEXT_AREA] != 0;
11880
11881 /* Use default face for the border below the tool bar.
11882
11883 FIXME: When auto-resize-tool-bars is grow-only, there is
11884 no additional border below the possibly empty tool-bar lines.
11885 So to make the extra empty lines look "normal", we have to
11886 use the tool-bar face for the border too. */
11887 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11888 it->face_id = DEFAULT_FACE_ID;
11889
11890 extend_face_to_end_of_line (it);
11891 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11892 last->right_box_line_p = 1;
11893 if (last == row->glyphs[TEXT_AREA])
11894 last->left_box_line_p = 1;
11895
11896 /* Make line the desired height and center it vertically. */
11897 if ((height -= it->max_ascent + it->max_descent) > 0)
11898 {
11899 /* Don't add more than one line height. */
11900 height %= FRAME_LINE_HEIGHT (it->f);
11901 it->max_ascent += height / 2;
11902 it->max_descent += (height + 1) / 2;
11903 }
11904
11905 compute_line_metrics (it);
11906
11907 /* If line is empty, make it occupy the rest of the tool-bar. */
11908 if (!row->displays_text_p)
11909 {
11910 row->height = row->phys_height = it->last_visible_y - row->y;
11911 row->visible_height = row->height;
11912 row->ascent = row->phys_ascent = 0;
11913 row->extra_line_spacing = 0;
11914 }
11915
11916 row->full_width_p = 1;
11917 row->continued_p = 0;
11918 row->truncated_on_left_p = 0;
11919 row->truncated_on_right_p = 0;
11920
11921 it->current_x = it->hpos = 0;
11922 it->current_y += row->height;
11923 ++it->vpos;
11924 ++it->glyph_row;
11925 }
11926
11927
11928 /* Max tool-bar height. */
11929
11930 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11931 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11932
11933 /* Value is the number of screen lines needed to make all tool-bar
11934 items of frame F visible. The number of actual rows needed is
11935 returned in *N_ROWS if non-NULL. */
11936
11937 static int
11938 tool_bar_lines_needed (struct frame *f, int *n_rows)
11939 {
11940 struct window *w = XWINDOW (f->tool_bar_window);
11941 struct it it;
11942 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11943 the desired matrix, so use (unused) mode-line row as temporary row to
11944 avoid destroying the first tool-bar row. */
11945 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11946
11947 /* Initialize an iterator for iteration over
11948 F->desired_tool_bar_string in the tool-bar window of frame F. */
11949 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11950 it.first_visible_x = 0;
11951 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11952 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11953 it.paragraph_embedding = L2R;
11954
11955 while (!ITERATOR_AT_END_P (&it))
11956 {
11957 clear_glyph_row (temp_row);
11958 it.glyph_row = temp_row;
11959 display_tool_bar_line (&it, -1);
11960 }
11961 clear_glyph_row (temp_row);
11962
11963 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11964 if (n_rows)
11965 *n_rows = it.vpos > 0 ? it.vpos : -1;
11966
11967 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11968 }
11969
11970
11971 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11972 0, 1, 0,
11973 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11974 If FRAME is nil or omitted, use the selected frame. */)
11975 (Lisp_Object frame)
11976 {
11977 struct frame *f = decode_any_frame (frame);
11978 struct window *w;
11979 int nlines = 0;
11980
11981 if (WINDOWP (f->tool_bar_window)
11982 && (w = XWINDOW (f->tool_bar_window),
11983 WINDOW_TOTAL_LINES (w) > 0))
11984 {
11985 update_tool_bar (f, 1);
11986 if (f->n_tool_bar_items)
11987 {
11988 build_desired_tool_bar_string (f);
11989 nlines = tool_bar_lines_needed (f, NULL);
11990 }
11991 }
11992
11993 return make_number (nlines);
11994 }
11995
11996
11997 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11998 height should be changed. */
11999
12000 static int
12001 redisplay_tool_bar (struct frame *f)
12002 {
12003 struct window *w;
12004 struct it it;
12005 struct glyph_row *row;
12006
12007 #if defined (USE_GTK) || defined (HAVE_NS)
12008 if (FRAME_EXTERNAL_TOOL_BAR (f))
12009 update_frame_tool_bar (f);
12010 return 0;
12011 #endif
12012
12013 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12014 do anything. This means you must start with tool-bar-lines
12015 non-zero to get the auto-sizing effect. Or in other words, you
12016 can turn off tool-bars by specifying tool-bar-lines zero. */
12017 if (!WINDOWP (f->tool_bar_window)
12018 || (w = XWINDOW (f->tool_bar_window),
12019 WINDOW_TOTAL_LINES (w) == 0))
12020 return 0;
12021
12022 /* Set up an iterator for the tool-bar window. */
12023 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12024 it.first_visible_x = 0;
12025 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12026 row = it.glyph_row;
12027
12028 /* Build a string that represents the contents of the tool-bar. */
12029 build_desired_tool_bar_string (f);
12030 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12031 /* FIXME: This should be controlled by a user option. But it
12032 doesn't make sense to have an R2L tool bar if the menu bar cannot
12033 be drawn also R2L, and making the menu bar R2L is tricky due
12034 toolkit-specific code that implements it. If an R2L tool bar is
12035 ever supported, display_tool_bar_line should also be augmented to
12036 call unproduce_glyphs like display_line and display_string
12037 do. */
12038 it.paragraph_embedding = L2R;
12039
12040 if (f->n_tool_bar_rows == 0)
12041 {
12042 int nlines;
12043
12044 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12045 nlines != WINDOW_TOTAL_LINES (w)))
12046 {
12047 Lisp_Object frame;
12048 int old_height = WINDOW_TOTAL_LINES (w);
12049
12050 XSETFRAME (frame, f);
12051 Fmodify_frame_parameters (frame,
12052 Fcons (Fcons (Qtool_bar_lines,
12053 make_number (nlines)),
12054 Qnil));
12055 if (WINDOW_TOTAL_LINES (w) != old_height)
12056 {
12057 clear_glyph_matrix (w->desired_matrix);
12058 fonts_changed_p = 1;
12059 return 1;
12060 }
12061 }
12062 }
12063
12064 /* Display as many lines as needed to display all tool-bar items. */
12065
12066 if (f->n_tool_bar_rows > 0)
12067 {
12068 int border, rows, height, extra;
12069
12070 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12071 border = XINT (Vtool_bar_border);
12072 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12073 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12074 else if (EQ (Vtool_bar_border, Qborder_width))
12075 border = f->border_width;
12076 else
12077 border = 0;
12078 if (border < 0)
12079 border = 0;
12080
12081 rows = f->n_tool_bar_rows;
12082 height = max (1, (it.last_visible_y - border) / rows);
12083 extra = it.last_visible_y - border - height * rows;
12084
12085 while (it.current_y < it.last_visible_y)
12086 {
12087 int h = 0;
12088 if (extra > 0 && rows-- > 0)
12089 {
12090 h = (extra + rows - 1) / rows;
12091 extra -= h;
12092 }
12093 display_tool_bar_line (&it, height + h);
12094 }
12095 }
12096 else
12097 {
12098 while (it.current_y < it.last_visible_y)
12099 display_tool_bar_line (&it, 0);
12100 }
12101
12102 /* It doesn't make much sense to try scrolling in the tool-bar
12103 window, so don't do it. */
12104 w->desired_matrix->no_scrolling_p = 1;
12105 w->must_be_updated_p = 1;
12106
12107 if (!NILP (Vauto_resize_tool_bars))
12108 {
12109 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12110 int change_height_p = 0;
12111
12112 /* If we couldn't display everything, change the tool-bar's
12113 height if there is room for more. */
12114 if (IT_STRING_CHARPOS (it) < it.end_charpos
12115 && it.current_y < max_tool_bar_height)
12116 change_height_p = 1;
12117
12118 row = it.glyph_row - 1;
12119
12120 /* If there are blank lines at the end, except for a partially
12121 visible blank line at the end that is smaller than
12122 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12123 if (!row->displays_text_p
12124 && row->height >= FRAME_LINE_HEIGHT (f))
12125 change_height_p = 1;
12126
12127 /* If row displays tool-bar items, but is partially visible,
12128 change the tool-bar's height. */
12129 if (row->displays_text_p
12130 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12131 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12132 change_height_p = 1;
12133
12134 /* Resize windows as needed by changing the `tool-bar-lines'
12135 frame parameter. */
12136 if (change_height_p)
12137 {
12138 Lisp_Object frame;
12139 int old_height = WINDOW_TOTAL_LINES (w);
12140 int nrows;
12141 int nlines = tool_bar_lines_needed (f, &nrows);
12142
12143 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12144 && !f->minimize_tool_bar_window_p)
12145 ? (nlines > old_height)
12146 : (nlines != old_height));
12147 f->minimize_tool_bar_window_p = 0;
12148
12149 if (change_height_p)
12150 {
12151 XSETFRAME (frame, f);
12152 Fmodify_frame_parameters (frame,
12153 Fcons (Fcons (Qtool_bar_lines,
12154 make_number (nlines)),
12155 Qnil));
12156 if (WINDOW_TOTAL_LINES (w) != old_height)
12157 {
12158 clear_glyph_matrix (w->desired_matrix);
12159 f->n_tool_bar_rows = nrows;
12160 fonts_changed_p = 1;
12161 return 1;
12162 }
12163 }
12164 }
12165 }
12166
12167 f->minimize_tool_bar_window_p = 0;
12168 return 0;
12169 }
12170
12171
12172 /* Get information about the tool-bar item which is displayed in GLYPH
12173 on frame F. Return in *PROP_IDX the index where tool-bar item
12174 properties start in F->tool_bar_items. Value is zero if
12175 GLYPH doesn't display a tool-bar item. */
12176
12177 static int
12178 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12179 {
12180 Lisp_Object prop;
12181 int success_p;
12182 int charpos;
12183
12184 /* This function can be called asynchronously, which means we must
12185 exclude any possibility that Fget_text_property signals an
12186 error. */
12187 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12188 charpos = max (0, charpos);
12189
12190 /* Get the text property `menu-item' at pos. The value of that
12191 property is the start index of this item's properties in
12192 F->tool_bar_items. */
12193 prop = Fget_text_property (make_number (charpos),
12194 Qmenu_item, f->current_tool_bar_string);
12195 if (INTEGERP (prop))
12196 {
12197 *prop_idx = XINT (prop);
12198 success_p = 1;
12199 }
12200 else
12201 success_p = 0;
12202
12203 return success_p;
12204 }
12205
12206 \f
12207 /* Get information about the tool-bar item at position X/Y on frame F.
12208 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12209 the current matrix of the tool-bar window of F, or NULL if not
12210 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12211 item in F->tool_bar_items. Value is
12212
12213 -1 if X/Y is not on a tool-bar item
12214 0 if X/Y is on the same item that was highlighted before.
12215 1 otherwise. */
12216
12217 static int
12218 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12219 int *hpos, int *vpos, int *prop_idx)
12220 {
12221 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12222 struct window *w = XWINDOW (f->tool_bar_window);
12223 int area;
12224
12225 /* Find the glyph under X/Y. */
12226 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12227 if (*glyph == NULL)
12228 return -1;
12229
12230 /* Get the start of this tool-bar item's properties in
12231 f->tool_bar_items. */
12232 if (!tool_bar_item_info (f, *glyph, prop_idx))
12233 return -1;
12234
12235 /* Is mouse on the highlighted item? */
12236 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12237 && *vpos >= hlinfo->mouse_face_beg_row
12238 && *vpos <= hlinfo->mouse_face_end_row
12239 && (*vpos > hlinfo->mouse_face_beg_row
12240 || *hpos >= hlinfo->mouse_face_beg_col)
12241 && (*vpos < hlinfo->mouse_face_end_row
12242 || *hpos < hlinfo->mouse_face_end_col
12243 || hlinfo->mouse_face_past_end))
12244 return 0;
12245
12246 return 1;
12247 }
12248
12249
12250 /* EXPORT:
12251 Handle mouse button event on the tool-bar of frame F, at
12252 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12253 0 for button release. MODIFIERS is event modifiers for button
12254 release. */
12255
12256 void
12257 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12258 int modifiers)
12259 {
12260 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12261 struct window *w = XWINDOW (f->tool_bar_window);
12262 int hpos, vpos, prop_idx;
12263 struct glyph *glyph;
12264 Lisp_Object enabled_p;
12265
12266 /* If not on the highlighted tool-bar item, return. */
12267 frame_to_window_pixel_xy (w, &x, &y);
12268 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12269 return;
12270
12271 /* If item is disabled, do nothing. */
12272 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12273 if (NILP (enabled_p))
12274 return;
12275
12276 if (down_p)
12277 {
12278 /* Show item in pressed state. */
12279 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12280 last_tool_bar_item = prop_idx;
12281 }
12282 else
12283 {
12284 Lisp_Object key, frame;
12285 struct input_event event;
12286 EVENT_INIT (event);
12287
12288 /* Show item in released state. */
12289 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12290
12291 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12292
12293 XSETFRAME (frame, f);
12294 event.kind = TOOL_BAR_EVENT;
12295 event.frame_or_window = frame;
12296 event.arg = frame;
12297 kbd_buffer_store_event (&event);
12298
12299 event.kind = TOOL_BAR_EVENT;
12300 event.frame_or_window = frame;
12301 event.arg = key;
12302 event.modifiers = modifiers;
12303 kbd_buffer_store_event (&event);
12304 last_tool_bar_item = -1;
12305 }
12306 }
12307
12308
12309 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12310 tool-bar window-relative coordinates X/Y. Called from
12311 note_mouse_highlight. */
12312
12313 static void
12314 note_tool_bar_highlight (struct frame *f, int x, int y)
12315 {
12316 Lisp_Object window = f->tool_bar_window;
12317 struct window *w = XWINDOW (window);
12318 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12319 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12320 int hpos, vpos;
12321 struct glyph *glyph;
12322 struct glyph_row *row;
12323 int i;
12324 Lisp_Object enabled_p;
12325 int prop_idx;
12326 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12327 int mouse_down_p, rc;
12328
12329 /* Function note_mouse_highlight is called with negative X/Y
12330 values when mouse moves outside of the frame. */
12331 if (x <= 0 || y <= 0)
12332 {
12333 clear_mouse_face (hlinfo);
12334 return;
12335 }
12336
12337 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12338 if (rc < 0)
12339 {
12340 /* Not on tool-bar item. */
12341 clear_mouse_face (hlinfo);
12342 return;
12343 }
12344 else if (rc == 0)
12345 /* On same tool-bar item as before. */
12346 goto set_help_echo;
12347
12348 clear_mouse_face (hlinfo);
12349
12350 /* Mouse is down, but on different tool-bar item? */
12351 mouse_down_p = (dpyinfo->grabbed
12352 && f == last_mouse_frame
12353 && FRAME_LIVE_P (f));
12354 if (mouse_down_p
12355 && last_tool_bar_item != prop_idx)
12356 return;
12357
12358 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12359
12360 /* If tool-bar item is not enabled, don't highlight it. */
12361 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12362 if (!NILP (enabled_p))
12363 {
12364 /* Compute the x-position of the glyph. In front and past the
12365 image is a space. We include this in the highlighted area. */
12366 row = MATRIX_ROW (w->current_matrix, vpos);
12367 for (i = x = 0; i < hpos; ++i)
12368 x += row->glyphs[TEXT_AREA][i].pixel_width;
12369
12370 /* Record this as the current active region. */
12371 hlinfo->mouse_face_beg_col = hpos;
12372 hlinfo->mouse_face_beg_row = vpos;
12373 hlinfo->mouse_face_beg_x = x;
12374 hlinfo->mouse_face_beg_y = row->y;
12375 hlinfo->mouse_face_past_end = 0;
12376
12377 hlinfo->mouse_face_end_col = hpos + 1;
12378 hlinfo->mouse_face_end_row = vpos;
12379 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12380 hlinfo->mouse_face_end_y = row->y;
12381 hlinfo->mouse_face_window = window;
12382 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12383
12384 /* Display it as active. */
12385 show_mouse_face (hlinfo, draw);
12386 }
12387
12388 set_help_echo:
12389
12390 /* Set help_echo_string to a help string to display for this tool-bar item.
12391 XTread_socket does the rest. */
12392 help_echo_object = help_echo_window = Qnil;
12393 help_echo_pos = -1;
12394 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12395 if (NILP (help_echo_string))
12396 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12397 }
12398
12399 #endif /* HAVE_WINDOW_SYSTEM */
12400
12401
12402 \f
12403 /************************************************************************
12404 Horizontal scrolling
12405 ************************************************************************/
12406
12407 static int hscroll_window_tree (Lisp_Object);
12408 static int hscroll_windows (Lisp_Object);
12409
12410 /* For all leaf windows in the window tree rooted at WINDOW, set their
12411 hscroll value so that PT is (i) visible in the window, and (ii) so
12412 that it is not within a certain margin at the window's left and
12413 right border. Value is non-zero if any window's hscroll has been
12414 changed. */
12415
12416 static int
12417 hscroll_window_tree (Lisp_Object window)
12418 {
12419 int hscrolled_p = 0;
12420 int hscroll_relative_p = FLOATP (Vhscroll_step);
12421 int hscroll_step_abs = 0;
12422 double hscroll_step_rel = 0;
12423
12424 if (hscroll_relative_p)
12425 {
12426 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12427 if (hscroll_step_rel < 0)
12428 {
12429 hscroll_relative_p = 0;
12430 hscroll_step_abs = 0;
12431 }
12432 }
12433 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12434 {
12435 hscroll_step_abs = XINT (Vhscroll_step);
12436 if (hscroll_step_abs < 0)
12437 hscroll_step_abs = 0;
12438 }
12439 else
12440 hscroll_step_abs = 0;
12441
12442 while (WINDOWP (window))
12443 {
12444 struct window *w = XWINDOW (window);
12445
12446 if (WINDOWP (w->hchild))
12447 hscrolled_p |= hscroll_window_tree (w->hchild);
12448 else if (WINDOWP (w->vchild))
12449 hscrolled_p |= hscroll_window_tree (w->vchild);
12450 else if (w->cursor.vpos >= 0)
12451 {
12452 int h_margin;
12453 int text_area_width;
12454 struct glyph_row *current_cursor_row
12455 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12456 struct glyph_row *desired_cursor_row
12457 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12458 struct glyph_row *cursor_row
12459 = (desired_cursor_row->enabled_p
12460 ? desired_cursor_row
12461 : current_cursor_row);
12462 int row_r2l_p = cursor_row->reversed_p;
12463
12464 text_area_width = window_box_width (w, TEXT_AREA);
12465
12466 /* Scroll when cursor is inside this scroll margin. */
12467 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12468
12469 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12470 /* For left-to-right rows, hscroll when cursor is either
12471 (i) inside the right hscroll margin, or (ii) if it is
12472 inside the left margin and the window is already
12473 hscrolled. */
12474 && ((!row_r2l_p
12475 && ((w->hscroll
12476 && w->cursor.x <= h_margin)
12477 || (cursor_row->enabled_p
12478 && cursor_row->truncated_on_right_p
12479 && (w->cursor.x >= text_area_width - h_margin))))
12480 /* For right-to-left rows, the logic is similar,
12481 except that rules for scrolling to left and right
12482 are reversed. E.g., if cursor.x <= h_margin, we
12483 need to hscroll "to the right" unconditionally,
12484 and that will scroll the screen to the left so as
12485 to reveal the next portion of the row. */
12486 || (row_r2l_p
12487 && ((cursor_row->enabled_p
12488 /* FIXME: It is confusing to set the
12489 truncated_on_right_p flag when R2L rows
12490 are actually truncated on the left. */
12491 && cursor_row->truncated_on_right_p
12492 && w->cursor.x <= h_margin)
12493 || (w->hscroll
12494 && (w->cursor.x >= text_area_width - h_margin))))))
12495 {
12496 struct it it;
12497 ptrdiff_t hscroll;
12498 struct buffer *saved_current_buffer;
12499 ptrdiff_t pt;
12500 int wanted_x;
12501
12502 /* Find point in a display of infinite width. */
12503 saved_current_buffer = current_buffer;
12504 current_buffer = XBUFFER (w->buffer);
12505
12506 if (w == XWINDOW (selected_window))
12507 pt = PT;
12508 else
12509 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12510
12511 /* Move iterator to pt starting at cursor_row->start in
12512 a line with infinite width. */
12513 init_to_row_start (&it, w, cursor_row);
12514 it.last_visible_x = INFINITY;
12515 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12516 current_buffer = saved_current_buffer;
12517
12518 /* Position cursor in window. */
12519 if (!hscroll_relative_p && hscroll_step_abs == 0)
12520 hscroll = max (0, (it.current_x
12521 - (ITERATOR_AT_END_OF_LINE_P (&it)
12522 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12523 : (text_area_width / 2))))
12524 / FRAME_COLUMN_WIDTH (it.f);
12525 else if ((!row_r2l_p
12526 && w->cursor.x >= text_area_width - h_margin)
12527 || (row_r2l_p && w->cursor.x <= h_margin))
12528 {
12529 if (hscroll_relative_p)
12530 wanted_x = text_area_width * (1 - hscroll_step_rel)
12531 - h_margin;
12532 else
12533 wanted_x = text_area_width
12534 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12535 - h_margin;
12536 hscroll
12537 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12538 }
12539 else
12540 {
12541 if (hscroll_relative_p)
12542 wanted_x = text_area_width * hscroll_step_rel
12543 + h_margin;
12544 else
12545 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12546 + h_margin;
12547 hscroll
12548 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12549 }
12550 hscroll = max (hscroll, w->min_hscroll);
12551
12552 /* Don't prevent redisplay optimizations if hscroll
12553 hasn't changed, as it will unnecessarily slow down
12554 redisplay. */
12555 if (w->hscroll != hscroll)
12556 {
12557 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12558 w->hscroll = hscroll;
12559 hscrolled_p = 1;
12560 }
12561 }
12562 }
12563
12564 window = w->next;
12565 }
12566
12567 /* Value is non-zero if hscroll of any leaf window has been changed. */
12568 return hscrolled_p;
12569 }
12570
12571
12572 /* Set hscroll so that cursor is visible and not inside horizontal
12573 scroll margins for all windows in the tree rooted at WINDOW. See
12574 also hscroll_window_tree above. Value is non-zero if any window's
12575 hscroll has been changed. If it has, desired matrices on the frame
12576 of WINDOW are cleared. */
12577
12578 static int
12579 hscroll_windows (Lisp_Object window)
12580 {
12581 int hscrolled_p = hscroll_window_tree (window);
12582 if (hscrolled_p)
12583 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12584 return hscrolled_p;
12585 }
12586
12587
12588 \f
12589 /************************************************************************
12590 Redisplay
12591 ************************************************************************/
12592
12593 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12594 to a non-zero value. This is sometimes handy to have in a debugger
12595 session. */
12596
12597 #ifdef GLYPH_DEBUG
12598
12599 /* First and last unchanged row for try_window_id. */
12600
12601 static int debug_first_unchanged_at_end_vpos;
12602 static int debug_last_unchanged_at_beg_vpos;
12603
12604 /* Delta vpos and y. */
12605
12606 static int debug_dvpos, debug_dy;
12607
12608 /* Delta in characters and bytes for try_window_id. */
12609
12610 static ptrdiff_t debug_delta, debug_delta_bytes;
12611
12612 /* Values of window_end_pos and window_end_vpos at the end of
12613 try_window_id. */
12614
12615 static ptrdiff_t debug_end_vpos;
12616
12617 /* Append a string to W->desired_matrix->method. FMT is a printf
12618 format string. If trace_redisplay_p is non-zero also printf the
12619 resulting string to stderr. */
12620
12621 static void debug_method_add (struct window *, char const *, ...)
12622 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12623
12624 static void
12625 debug_method_add (struct window *w, char const *fmt, ...)
12626 {
12627 char *method = w->desired_matrix->method;
12628 int len = strlen (method);
12629 int size = sizeof w->desired_matrix->method;
12630 int remaining = size - len - 1;
12631 va_list ap;
12632
12633 if (len && remaining)
12634 {
12635 method[len] = '|';
12636 --remaining, ++len;
12637 }
12638
12639 va_start (ap, fmt);
12640 vsnprintf (method + len, remaining + 1, fmt, ap);
12641 va_end (ap);
12642
12643 if (trace_redisplay_p)
12644 fprintf (stderr, "%p (%s): %s\n",
12645 w,
12646 ((BUFFERP (w->buffer)
12647 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12648 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12649 : "no buffer"),
12650 method + len);
12651 }
12652
12653 #endif /* GLYPH_DEBUG */
12654
12655
12656 /* Value is non-zero if all changes in window W, which displays
12657 current_buffer, are in the text between START and END. START is a
12658 buffer position, END is given as a distance from Z. Used in
12659 redisplay_internal for display optimization. */
12660
12661 static int
12662 text_outside_line_unchanged_p (struct window *w,
12663 ptrdiff_t start, ptrdiff_t end)
12664 {
12665 int unchanged_p = 1;
12666
12667 /* If text or overlays have changed, see where. */
12668 if (window_outdated (w))
12669 {
12670 /* Gap in the line? */
12671 if (GPT < start || Z - GPT < end)
12672 unchanged_p = 0;
12673
12674 /* Changes start in front of the line, or end after it? */
12675 if (unchanged_p
12676 && (BEG_UNCHANGED < start - 1
12677 || END_UNCHANGED < end))
12678 unchanged_p = 0;
12679
12680 /* If selective display, can't optimize if changes start at the
12681 beginning of the line. */
12682 if (unchanged_p
12683 && INTEGERP (BVAR (current_buffer, selective_display))
12684 && XINT (BVAR (current_buffer, selective_display)) > 0
12685 && (BEG_UNCHANGED < start || GPT <= start))
12686 unchanged_p = 0;
12687
12688 /* If there are overlays at the start or end of the line, these
12689 may have overlay strings with newlines in them. A change at
12690 START, for instance, may actually concern the display of such
12691 overlay strings as well, and they are displayed on different
12692 lines. So, quickly rule out this case. (For the future, it
12693 might be desirable to implement something more telling than
12694 just BEG/END_UNCHANGED.) */
12695 if (unchanged_p)
12696 {
12697 if (BEG + BEG_UNCHANGED == start
12698 && overlay_touches_p (start))
12699 unchanged_p = 0;
12700 if (END_UNCHANGED == end
12701 && overlay_touches_p (Z - end))
12702 unchanged_p = 0;
12703 }
12704
12705 /* Under bidi reordering, adding or deleting a character in the
12706 beginning of a paragraph, before the first strong directional
12707 character, can change the base direction of the paragraph (unless
12708 the buffer specifies a fixed paragraph direction), which will
12709 require to redisplay the whole paragraph. It might be worthwhile
12710 to find the paragraph limits and widen the range of redisplayed
12711 lines to that, but for now just give up this optimization. */
12712 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12713 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12714 unchanged_p = 0;
12715 }
12716
12717 return unchanged_p;
12718 }
12719
12720
12721 /* Do a frame update, taking possible shortcuts into account. This is
12722 the main external entry point for redisplay.
12723
12724 If the last redisplay displayed an echo area message and that message
12725 is no longer requested, we clear the echo area or bring back the
12726 mini-buffer if that is in use. */
12727
12728 void
12729 redisplay (void)
12730 {
12731 redisplay_internal ();
12732 }
12733
12734
12735 static Lisp_Object
12736 overlay_arrow_string_or_property (Lisp_Object var)
12737 {
12738 Lisp_Object val;
12739
12740 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12741 return val;
12742
12743 return Voverlay_arrow_string;
12744 }
12745
12746 /* Return 1 if there are any overlay-arrows in current_buffer. */
12747 static int
12748 overlay_arrow_in_current_buffer_p (void)
12749 {
12750 Lisp_Object vlist;
12751
12752 for (vlist = Voverlay_arrow_variable_list;
12753 CONSP (vlist);
12754 vlist = XCDR (vlist))
12755 {
12756 Lisp_Object var = XCAR (vlist);
12757 Lisp_Object val;
12758
12759 if (!SYMBOLP (var))
12760 continue;
12761 val = find_symbol_value (var);
12762 if (MARKERP (val)
12763 && current_buffer == XMARKER (val)->buffer)
12764 return 1;
12765 }
12766 return 0;
12767 }
12768
12769
12770 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12771 has changed. */
12772
12773 static int
12774 overlay_arrows_changed_p (void)
12775 {
12776 Lisp_Object vlist;
12777
12778 for (vlist = Voverlay_arrow_variable_list;
12779 CONSP (vlist);
12780 vlist = XCDR (vlist))
12781 {
12782 Lisp_Object var = XCAR (vlist);
12783 Lisp_Object val, pstr;
12784
12785 if (!SYMBOLP (var))
12786 continue;
12787 val = find_symbol_value (var);
12788 if (!MARKERP (val))
12789 continue;
12790 if (! EQ (COERCE_MARKER (val),
12791 Fget (var, Qlast_arrow_position))
12792 || ! (pstr = overlay_arrow_string_or_property (var),
12793 EQ (pstr, Fget (var, Qlast_arrow_string))))
12794 return 1;
12795 }
12796 return 0;
12797 }
12798
12799 /* Mark overlay arrows to be updated on next redisplay. */
12800
12801 static void
12802 update_overlay_arrows (int up_to_date)
12803 {
12804 Lisp_Object vlist;
12805
12806 for (vlist = Voverlay_arrow_variable_list;
12807 CONSP (vlist);
12808 vlist = XCDR (vlist))
12809 {
12810 Lisp_Object var = XCAR (vlist);
12811
12812 if (!SYMBOLP (var))
12813 continue;
12814
12815 if (up_to_date > 0)
12816 {
12817 Lisp_Object val = find_symbol_value (var);
12818 Fput (var, Qlast_arrow_position,
12819 COERCE_MARKER (val));
12820 Fput (var, Qlast_arrow_string,
12821 overlay_arrow_string_or_property (var));
12822 }
12823 else if (up_to_date < 0
12824 || !NILP (Fget (var, Qlast_arrow_position)))
12825 {
12826 Fput (var, Qlast_arrow_position, Qt);
12827 Fput (var, Qlast_arrow_string, Qt);
12828 }
12829 }
12830 }
12831
12832
12833 /* Return overlay arrow string to display at row.
12834 Return integer (bitmap number) for arrow bitmap in left fringe.
12835 Return nil if no overlay arrow. */
12836
12837 static Lisp_Object
12838 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12839 {
12840 Lisp_Object vlist;
12841
12842 for (vlist = Voverlay_arrow_variable_list;
12843 CONSP (vlist);
12844 vlist = XCDR (vlist))
12845 {
12846 Lisp_Object var = XCAR (vlist);
12847 Lisp_Object val;
12848
12849 if (!SYMBOLP (var))
12850 continue;
12851
12852 val = find_symbol_value (var);
12853
12854 if (MARKERP (val)
12855 && current_buffer == XMARKER (val)->buffer
12856 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12857 {
12858 if (FRAME_WINDOW_P (it->f)
12859 /* FIXME: if ROW->reversed_p is set, this should test
12860 the right fringe, not the left one. */
12861 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12862 {
12863 #ifdef HAVE_WINDOW_SYSTEM
12864 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12865 {
12866 int fringe_bitmap;
12867 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12868 return make_number (fringe_bitmap);
12869 }
12870 #endif
12871 return make_number (-1); /* Use default arrow bitmap. */
12872 }
12873 return overlay_arrow_string_or_property (var);
12874 }
12875 }
12876
12877 return Qnil;
12878 }
12879
12880 /* Return 1 if point moved out of or into a composition. Otherwise
12881 return 0. PREV_BUF and PREV_PT are the last point buffer and
12882 position. BUF and PT are the current point buffer and position. */
12883
12884 static int
12885 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12886 struct buffer *buf, ptrdiff_t pt)
12887 {
12888 ptrdiff_t start, end;
12889 Lisp_Object prop;
12890 Lisp_Object buffer;
12891
12892 XSETBUFFER (buffer, buf);
12893 /* Check a composition at the last point if point moved within the
12894 same buffer. */
12895 if (prev_buf == buf)
12896 {
12897 if (prev_pt == pt)
12898 /* Point didn't move. */
12899 return 0;
12900
12901 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12902 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12903 && COMPOSITION_VALID_P (start, end, prop)
12904 && start < prev_pt && end > prev_pt)
12905 /* The last point was within the composition. Return 1 iff
12906 point moved out of the composition. */
12907 return (pt <= start || pt >= end);
12908 }
12909
12910 /* Check a composition at the current point. */
12911 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12912 && find_composition (pt, -1, &start, &end, &prop, buffer)
12913 && COMPOSITION_VALID_P (start, end, prop)
12914 && start < pt && end > pt);
12915 }
12916
12917
12918 /* Reconsider the setting of B->clip_changed which is displayed
12919 in window W. */
12920
12921 static void
12922 reconsider_clip_changes (struct window *w, struct buffer *b)
12923 {
12924 if (b->clip_changed
12925 && !NILP (w->window_end_valid)
12926 && w->current_matrix->buffer == b
12927 && w->current_matrix->zv == BUF_ZV (b)
12928 && w->current_matrix->begv == BUF_BEGV (b))
12929 b->clip_changed = 0;
12930
12931 /* If display wasn't paused, and W is not a tool bar window, see if
12932 point has been moved into or out of a composition. In that case,
12933 we set b->clip_changed to 1 to force updating the screen. If
12934 b->clip_changed has already been set to 1, we can skip this
12935 check. */
12936 if (!b->clip_changed
12937 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12938 {
12939 ptrdiff_t pt;
12940
12941 if (w == XWINDOW (selected_window))
12942 pt = PT;
12943 else
12944 pt = marker_position (w->pointm);
12945
12946 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12947 || pt != w->last_point)
12948 && check_point_in_composition (w->current_matrix->buffer,
12949 w->last_point,
12950 XBUFFER (w->buffer), pt))
12951 b->clip_changed = 1;
12952 }
12953 }
12954 \f
12955
12956 #define STOP_POLLING \
12957 do { if (! polling_stopped_here) stop_polling (); \
12958 polling_stopped_here = 1; } while (0)
12959
12960 #define RESUME_POLLING \
12961 do { if (polling_stopped_here) start_polling (); \
12962 polling_stopped_here = 0; } while (0)
12963
12964
12965 /* Perhaps in the future avoid recentering windows if it
12966 is not necessary; currently that causes some problems. */
12967
12968 static void
12969 redisplay_internal (void)
12970 {
12971 struct window *w = XWINDOW (selected_window);
12972 struct window *sw;
12973 struct frame *fr;
12974 int pending;
12975 int must_finish = 0;
12976 struct text_pos tlbufpos, tlendpos;
12977 int number_of_visible_frames;
12978 ptrdiff_t count, count1;
12979 struct frame *sf;
12980 int polling_stopped_here = 0;
12981 Lisp_Object tail, frame;
12982 struct backtrace backtrace;
12983
12984 /* Non-zero means redisplay has to consider all windows on all
12985 frames. Zero means, only selected_window is considered. */
12986 int consider_all_windows_p;
12987
12988 /* Non-zero means redisplay has to redisplay the miniwindow. */
12989 int update_miniwindow_p = 0;
12990
12991 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12992
12993 /* No redisplay if running in batch mode or frame is not yet fully
12994 initialized, or redisplay is explicitly turned off by setting
12995 Vinhibit_redisplay. */
12996 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12997 || !NILP (Vinhibit_redisplay))
12998 return;
12999
13000 /* Don't examine these until after testing Vinhibit_redisplay.
13001 When Emacs is shutting down, perhaps because its connection to
13002 X has dropped, we should not look at them at all. */
13003 fr = XFRAME (w->frame);
13004 sf = SELECTED_FRAME ();
13005
13006 if (!fr->glyphs_initialized_p)
13007 return;
13008
13009 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13010 if (popup_activated ())
13011 return;
13012 #endif
13013
13014 /* I don't think this happens but let's be paranoid. */
13015 if (redisplaying_p)
13016 return;
13017
13018 /* Record a function that clears redisplaying_p
13019 when we leave this function. */
13020 count = SPECPDL_INDEX ();
13021 record_unwind_protect (unwind_redisplay, selected_frame);
13022 redisplaying_p = 1;
13023 specbind (Qinhibit_free_realized_faces, Qnil);
13024
13025 /* Record this function, so it appears on the profiler's backtraces. */
13026 backtrace.next = backtrace_list;
13027 backtrace.function = Qredisplay_internal;
13028 backtrace.args = &Qnil;
13029 backtrace.nargs = 0;
13030 backtrace.debug_on_exit = 0;
13031 backtrace_list = &backtrace;
13032
13033 FOR_EACH_FRAME (tail, frame)
13034 XFRAME (frame)->already_hscrolled_p = 0;
13035
13036 retry:
13037 /* Remember the currently selected window. */
13038 sw = w;
13039
13040 pending = 0;
13041 reconsider_clip_changes (w, current_buffer);
13042 last_escape_glyph_frame = NULL;
13043 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13044 last_glyphless_glyph_frame = NULL;
13045 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13046
13047 /* If new fonts have been loaded that make a glyph matrix adjustment
13048 necessary, do it. */
13049 if (fonts_changed_p)
13050 {
13051 adjust_glyphs (NULL);
13052 ++windows_or_buffers_changed;
13053 fonts_changed_p = 0;
13054 }
13055
13056 /* If face_change_count is non-zero, init_iterator will free all
13057 realized faces, which includes the faces referenced from current
13058 matrices. So, we can't reuse current matrices in this case. */
13059 if (face_change_count)
13060 ++windows_or_buffers_changed;
13061
13062 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13063 && FRAME_TTY (sf)->previous_frame != sf)
13064 {
13065 /* Since frames on a single ASCII terminal share the same
13066 display area, displaying a different frame means redisplay
13067 the whole thing. */
13068 windows_or_buffers_changed++;
13069 SET_FRAME_GARBAGED (sf);
13070 #ifndef DOS_NT
13071 set_tty_color_mode (FRAME_TTY (sf), sf);
13072 #endif
13073 FRAME_TTY (sf)->previous_frame = sf;
13074 }
13075
13076 /* Set the visible flags for all frames. Do this before checking for
13077 resized or garbaged frames; they want to know if their frames are
13078 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13079 number_of_visible_frames = 0;
13080
13081 FOR_EACH_FRAME (tail, frame)
13082 {
13083 struct frame *f = XFRAME (frame);
13084
13085 FRAME_SAMPLE_VISIBILITY (f);
13086 if (FRAME_VISIBLE_P (f))
13087 ++number_of_visible_frames;
13088 clear_desired_matrices (f);
13089 }
13090
13091 /* Notice any pending interrupt request to change frame size. */
13092 do_pending_window_change (1);
13093
13094 /* do_pending_window_change could change the selected_window due to
13095 frame resizing which makes the selected window too small. */
13096 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13097 {
13098 sw = w;
13099 reconsider_clip_changes (w, current_buffer);
13100 }
13101
13102 /* Clear frames marked as garbaged. */
13103 clear_garbaged_frames ();
13104
13105 /* Build menubar and tool-bar items. */
13106 if (NILP (Vmemory_full))
13107 prepare_menu_bars ();
13108
13109 if (windows_or_buffers_changed)
13110 update_mode_lines++;
13111
13112 /* Detect case that we need to write or remove a star in the mode line. */
13113 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13114 {
13115 w->update_mode_line = 1;
13116 if (buffer_shared_and_changed ())
13117 update_mode_lines++;
13118 }
13119
13120 /* Avoid invocation of point motion hooks by `current_column' below. */
13121 count1 = SPECPDL_INDEX ();
13122 specbind (Qinhibit_point_motion_hooks, Qt);
13123
13124 if (mode_line_update_needed (w))
13125 w->update_mode_line = 1;
13126
13127 unbind_to (count1, Qnil);
13128
13129 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13130
13131 consider_all_windows_p = (update_mode_lines
13132 || buffer_shared_and_changed ()
13133 || cursor_type_changed);
13134
13135 /* If specs for an arrow have changed, do thorough redisplay
13136 to ensure we remove any arrow that should no longer exist. */
13137 if (overlay_arrows_changed_p ())
13138 consider_all_windows_p = windows_or_buffers_changed = 1;
13139
13140 /* Normally the message* functions will have already displayed and
13141 updated the echo area, but the frame may have been trashed, or
13142 the update may have been preempted, so display the echo area
13143 again here. Checking message_cleared_p captures the case that
13144 the echo area should be cleared. */
13145 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13146 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13147 || (message_cleared_p
13148 && minibuf_level == 0
13149 /* If the mini-window is currently selected, this means the
13150 echo-area doesn't show through. */
13151 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13152 {
13153 int window_height_changed_p = echo_area_display (0);
13154
13155 if (message_cleared_p)
13156 update_miniwindow_p = 1;
13157
13158 must_finish = 1;
13159
13160 /* If we don't display the current message, don't clear the
13161 message_cleared_p flag, because, if we did, we wouldn't clear
13162 the echo area in the next redisplay which doesn't preserve
13163 the echo area. */
13164 if (!display_last_displayed_message_p)
13165 message_cleared_p = 0;
13166
13167 if (fonts_changed_p)
13168 goto retry;
13169 else if (window_height_changed_p)
13170 {
13171 consider_all_windows_p = 1;
13172 ++update_mode_lines;
13173 ++windows_or_buffers_changed;
13174
13175 /* If window configuration was changed, frames may have been
13176 marked garbaged. Clear them or we will experience
13177 surprises wrt scrolling. */
13178 clear_garbaged_frames ();
13179 }
13180 }
13181 else if (EQ (selected_window, minibuf_window)
13182 && (current_buffer->clip_changed || window_outdated (w))
13183 && resize_mini_window (w, 0))
13184 {
13185 /* Resized active mini-window to fit the size of what it is
13186 showing if its contents might have changed. */
13187 must_finish = 1;
13188 /* FIXME: this causes all frames to be updated, which seems unnecessary
13189 since only the current frame needs to be considered. This function
13190 needs to be rewritten with two variables, consider_all_windows and
13191 consider_all_frames. */
13192 consider_all_windows_p = 1;
13193 ++windows_or_buffers_changed;
13194 ++update_mode_lines;
13195
13196 /* If window configuration was changed, frames may have been
13197 marked garbaged. Clear them or we will experience
13198 surprises wrt scrolling. */
13199 clear_garbaged_frames ();
13200 }
13201
13202
13203 /* If showing the region, and mark has changed, we must redisplay
13204 the whole window. The assignment to this_line_start_pos prevents
13205 the optimization directly below this if-statement. */
13206 if (((!NILP (Vtransient_mark_mode)
13207 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13208 != !NILP (w->region_showing))
13209 || (!NILP (w->region_showing)
13210 && !EQ (w->region_showing,
13211 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13212 CHARPOS (this_line_start_pos) = 0;
13213
13214 /* Optimize the case that only the line containing the cursor in the
13215 selected window has changed. Variables starting with this_ are
13216 set in display_line and record information about the line
13217 containing the cursor. */
13218 tlbufpos = this_line_start_pos;
13219 tlendpos = this_line_end_pos;
13220 if (!consider_all_windows_p
13221 && CHARPOS (tlbufpos) > 0
13222 && !w->update_mode_line
13223 && !current_buffer->clip_changed
13224 && !current_buffer->prevent_redisplay_optimizations_p
13225 && FRAME_VISIBLE_P (XFRAME (w->frame))
13226 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13227 /* Make sure recorded data applies to current buffer, etc. */
13228 && this_line_buffer == current_buffer
13229 && current_buffer == XBUFFER (w->buffer)
13230 && !w->force_start
13231 && !w->optional_new_start
13232 /* Point must be on the line that we have info recorded about. */
13233 && PT >= CHARPOS (tlbufpos)
13234 && PT <= Z - CHARPOS (tlendpos)
13235 /* All text outside that line, including its final newline,
13236 must be unchanged. */
13237 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13238 CHARPOS (tlendpos)))
13239 {
13240 if (CHARPOS (tlbufpos) > BEGV
13241 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13242 && (CHARPOS (tlbufpos) == ZV
13243 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13244 /* Former continuation line has disappeared by becoming empty. */
13245 goto cancel;
13246 else if (window_outdated (w) || MINI_WINDOW_P (w))
13247 {
13248 /* We have to handle the case of continuation around a
13249 wide-column character (see the comment in indent.c around
13250 line 1340).
13251
13252 For instance, in the following case:
13253
13254 -------- Insert --------
13255 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13256 J_I_ ==> J_I_ `^^' are cursors.
13257 ^^ ^^
13258 -------- --------
13259
13260 As we have to redraw the line above, we cannot use this
13261 optimization. */
13262
13263 struct it it;
13264 int line_height_before = this_line_pixel_height;
13265
13266 /* Note that start_display will handle the case that the
13267 line starting at tlbufpos is a continuation line. */
13268 start_display (&it, w, tlbufpos);
13269
13270 /* Implementation note: It this still necessary? */
13271 if (it.current_x != this_line_start_x)
13272 goto cancel;
13273
13274 TRACE ((stderr, "trying display optimization 1\n"));
13275 w->cursor.vpos = -1;
13276 overlay_arrow_seen = 0;
13277 it.vpos = this_line_vpos;
13278 it.current_y = this_line_y;
13279 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13280 display_line (&it);
13281
13282 /* If line contains point, is not continued,
13283 and ends at same distance from eob as before, we win. */
13284 if (w->cursor.vpos >= 0
13285 /* Line is not continued, otherwise this_line_start_pos
13286 would have been set to 0 in display_line. */
13287 && CHARPOS (this_line_start_pos)
13288 /* Line ends as before. */
13289 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13290 /* Line has same height as before. Otherwise other lines
13291 would have to be shifted up or down. */
13292 && this_line_pixel_height == line_height_before)
13293 {
13294 /* If this is not the window's last line, we must adjust
13295 the charstarts of the lines below. */
13296 if (it.current_y < it.last_visible_y)
13297 {
13298 struct glyph_row *row
13299 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13300 ptrdiff_t delta, delta_bytes;
13301
13302 /* We used to distinguish between two cases here,
13303 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13304 when the line ends in a newline or the end of the
13305 buffer's accessible portion. But both cases did
13306 the same, so they were collapsed. */
13307 delta = (Z
13308 - CHARPOS (tlendpos)
13309 - MATRIX_ROW_START_CHARPOS (row));
13310 delta_bytes = (Z_BYTE
13311 - BYTEPOS (tlendpos)
13312 - MATRIX_ROW_START_BYTEPOS (row));
13313
13314 increment_matrix_positions (w->current_matrix,
13315 this_line_vpos + 1,
13316 w->current_matrix->nrows,
13317 delta, delta_bytes);
13318 }
13319
13320 /* If this row displays text now but previously didn't,
13321 or vice versa, w->window_end_vpos may have to be
13322 adjusted. */
13323 if ((it.glyph_row - 1)->displays_text_p)
13324 {
13325 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13326 wset_window_end_vpos (w, make_number (this_line_vpos));
13327 }
13328 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13329 && this_line_vpos > 0)
13330 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13331 wset_window_end_valid (w, Qnil);
13332
13333 /* Update hint: No need to try to scroll in update_window. */
13334 w->desired_matrix->no_scrolling_p = 1;
13335
13336 #ifdef GLYPH_DEBUG
13337 *w->desired_matrix->method = 0;
13338 debug_method_add (w, "optimization 1");
13339 #endif
13340 #ifdef HAVE_WINDOW_SYSTEM
13341 update_window_fringes (w, 0);
13342 #endif
13343 goto update;
13344 }
13345 else
13346 goto cancel;
13347 }
13348 else if (/* Cursor position hasn't changed. */
13349 PT == w->last_point
13350 /* Make sure the cursor was last displayed
13351 in this window. Otherwise we have to reposition it. */
13352 && 0 <= w->cursor.vpos
13353 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13354 {
13355 if (!must_finish)
13356 {
13357 do_pending_window_change (1);
13358 /* If selected_window changed, redisplay again. */
13359 if (WINDOWP (selected_window)
13360 && (w = XWINDOW (selected_window)) != sw)
13361 goto retry;
13362
13363 /* We used to always goto end_of_redisplay here, but this
13364 isn't enough if we have a blinking cursor. */
13365 if (w->cursor_off_p == w->last_cursor_off_p)
13366 goto end_of_redisplay;
13367 }
13368 goto update;
13369 }
13370 /* If highlighting the region, or if the cursor is in the echo area,
13371 then we can't just move the cursor. */
13372 else if (! (!NILP (Vtransient_mark_mode)
13373 && !NILP (BVAR (current_buffer, mark_active)))
13374 && (EQ (selected_window,
13375 BVAR (current_buffer, last_selected_window))
13376 || highlight_nonselected_windows)
13377 && NILP (w->region_showing)
13378 && NILP (Vshow_trailing_whitespace)
13379 && !cursor_in_echo_area)
13380 {
13381 struct it it;
13382 struct glyph_row *row;
13383
13384 /* Skip from tlbufpos to PT and see where it is. Note that
13385 PT may be in invisible text. If so, we will end at the
13386 next visible position. */
13387 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13388 NULL, DEFAULT_FACE_ID);
13389 it.current_x = this_line_start_x;
13390 it.current_y = this_line_y;
13391 it.vpos = this_line_vpos;
13392
13393 /* The call to move_it_to stops in front of PT, but
13394 moves over before-strings. */
13395 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13396
13397 if (it.vpos == this_line_vpos
13398 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13399 row->enabled_p))
13400 {
13401 eassert (this_line_vpos == it.vpos);
13402 eassert (this_line_y == it.current_y);
13403 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13404 #ifdef GLYPH_DEBUG
13405 *w->desired_matrix->method = 0;
13406 debug_method_add (w, "optimization 3");
13407 #endif
13408 goto update;
13409 }
13410 else
13411 goto cancel;
13412 }
13413
13414 cancel:
13415 /* Text changed drastically or point moved off of line. */
13416 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13417 }
13418
13419 CHARPOS (this_line_start_pos) = 0;
13420 consider_all_windows_p |= buffer_shared_and_changed ();
13421 ++clear_face_cache_count;
13422 #ifdef HAVE_WINDOW_SYSTEM
13423 ++clear_image_cache_count;
13424 #endif
13425
13426 /* Build desired matrices, and update the display. If
13427 consider_all_windows_p is non-zero, do it for all windows on all
13428 frames. Otherwise do it for selected_window, only. */
13429
13430 if (consider_all_windows_p)
13431 {
13432 FOR_EACH_FRAME (tail, frame)
13433 XFRAME (frame)->updated_p = 0;
13434
13435 FOR_EACH_FRAME (tail, frame)
13436 {
13437 struct frame *f = XFRAME (frame);
13438
13439 /* We don't have to do anything for unselected terminal
13440 frames. */
13441 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13442 && !EQ (FRAME_TTY (f)->top_frame, frame))
13443 continue;
13444
13445 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13446 {
13447 /* Mark all the scroll bars to be removed; we'll redeem
13448 the ones we want when we redisplay their windows. */
13449 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13450 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13451
13452 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13453 redisplay_windows (FRAME_ROOT_WINDOW (f));
13454
13455 /* The X error handler may have deleted that frame. */
13456 if (!FRAME_LIVE_P (f))
13457 continue;
13458
13459 /* Any scroll bars which redisplay_windows should have
13460 nuked should now go away. */
13461 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13462 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13463
13464 /* If fonts changed, display again. */
13465 /* ??? rms: I suspect it is a mistake to jump all the way
13466 back to retry here. It should just retry this frame. */
13467 if (fonts_changed_p)
13468 goto retry;
13469
13470 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13471 {
13472 /* See if we have to hscroll. */
13473 if (!f->already_hscrolled_p)
13474 {
13475 f->already_hscrolled_p = 1;
13476 if (hscroll_windows (f->root_window))
13477 goto retry;
13478 }
13479
13480 /* Prevent various kinds of signals during display
13481 update. stdio is not robust about handling
13482 signals, which can cause an apparent I/O
13483 error. */
13484 if (interrupt_input)
13485 unrequest_sigio ();
13486 STOP_POLLING;
13487
13488 /* Update the display. */
13489 set_window_update_flags (XWINDOW (f->root_window), 1);
13490 pending |= update_frame (f, 0, 0);
13491 f->updated_p = 1;
13492 }
13493 }
13494 }
13495
13496 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13497
13498 if (!pending)
13499 {
13500 /* Do the mark_window_display_accurate after all windows have
13501 been redisplayed because this call resets flags in buffers
13502 which are needed for proper redisplay. */
13503 FOR_EACH_FRAME (tail, frame)
13504 {
13505 struct frame *f = XFRAME (frame);
13506 if (f->updated_p)
13507 {
13508 mark_window_display_accurate (f->root_window, 1);
13509 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13510 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13511 }
13512 }
13513 }
13514 }
13515 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13516 {
13517 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13518 struct frame *mini_frame;
13519
13520 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13521 /* Use list_of_error, not Qerror, so that
13522 we catch only errors and don't run the debugger. */
13523 internal_condition_case_1 (redisplay_window_1, selected_window,
13524 list_of_error,
13525 redisplay_window_error);
13526 if (update_miniwindow_p)
13527 internal_condition_case_1 (redisplay_window_1, mini_window,
13528 list_of_error,
13529 redisplay_window_error);
13530
13531 /* Compare desired and current matrices, perform output. */
13532
13533 update:
13534 /* If fonts changed, display again. */
13535 if (fonts_changed_p)
13536 goto retry;
13537
13538 /* Prevent various kinds of signals during display update.
13539 stdio is not robust about handling signals,
13540 which can cause an apparent I/O error. */
13541 if (interrupt_input)
13542 unrequest_sigio ();
13543 STOP_POLLING;
13544
13545 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13546 {
13547 if (hscroll_windows (selected_window))
13548 goto retry;
13549
13550 XWINDOW (selected_window)->must_be_updated_p = 1;
13551 pending = update_frame (sf, 0, 0);
13552 }
13553
13554 /* We may have called echo_area_display at the top of this
13555 function. If the echo area is on another frame, that may
13556 have put text on a frame other than the selected one, so the
13557 above call to update_frame would not have caught it. Catch
13558 it here. */
13559 mini_window = FRAME_MINIBUF_WINDOW (sf);
13560 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13561
13562 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13563 {
13564 XWINDOW (mini_window)->must_be_updated_p = 1;
13565 pending |= update_frame (mini_frame, 0, 0);
13566 if (!pending && hscroll_windows (mini_window))
13567 goto retry;
13568 }
13569 }
13570
13571 /* If display was paused because of pending input, make sure we do a
13572 thorough update the next time. */
13573 if (pending)
13574 {
13575 /* Prevent the optimization at the beginning of
13576 redisplay_internal that tries a single-line update of the
13577 line containing the cursor in the selected window. */
13578 CHARPOS (this_line_start_pos) = 0;
13579
13580 /* Let the overlay arrow be updated the next time. */
13581 update_overlay_arrows (0);
13582
13583 /* If we pause after scrolling, some rows in the current
13584 matrices of some windows are not valid. */
13585 if (!WINDOW_FULL_WIDTH_P (w)
13586 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13587 update_mode_lines = 1;
13588 }
13589 else
13590 {
13591 if (!consider_all_windows_p)
13592 {
13593 /* This has already been done above if
13594 consider_all_windows_p is set. */
13595 mark_window_display_accurate_1 (w, 1);
13596
13597 /* Say overlay arrows are up to date. */
13598 update_overlay_arrows (1);
13599
13600 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13601 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13602 }
13603
13604 update_mode_lines = 0;
13605 windows_or_buffers_changed = 0;
13606 cursor_type_changed = 0;
13607 }
13608
13609 /* Start SIGIO interrupts coming again. Having them off during the
13610 code above makes it less likely one will discard output, but not
13611 impossible, since there might be stuff in the system buffer here.
13612 But it is much hairier to try to do anything about that. */
13613 if (interrupt_input)
13614 request_sigio ();
13615 RESUME_POLLING;
13616
13617 /* If a frame has become visible which was not before, redisplay
13618 again, so that we display it. Expose events for such a frame
13619 (which it gets when becoming visible) don't call the parts of
13620 redisplay constructing glyphs, so simply exposing a frame won't
13621 display anything in this case. So, we have to display these
13622 frames here explicitly. */
13623 if (!pending)
13624 {
13625 int new_count = 0;
13626
13627 FOR_EACH_FRAME (tail, frame)
13628 {
13629 int this_is_visible = 0;
13630
13631 if (XFRAME (frame)->visible)
13632 this_is_visible = 1;
13633 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13634 if (XFRAME (frame)->visible)
13635 this_is_visible = 1;
13636
13637 if (this_is_visible)
13638 new_count++;
13639 }
13640
13641 if (new_count != number_of_visible_frames)
13642 windows_or_buffers_changed++;
13643 }
13644
13645 /* Change frame size now if a change is pending. */
13646 do_pending_window_change (1);
13647
13648 /* If we just did a pending size change, or have additional
13649 visible frames, or selected_window changed, redisplay again. */
13650 if ((windows_or_buffers_changed && !pending)
13651 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13652 goto retry;
13653
13654 /* Clear the face and image caches.
13655
13656 We used to do this only if consider_all_windows_p. But the cache
13657 needs to be cleared if a timer creates images in the current
13658 buffer (e.g. the test case in Bug#6230). */
13659
13660 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13661 {
13662 clear_face_cache (0);
13663 clear_face_cache_count = 0;
13664 }
13665
13666 #ifdef HAVE_WINDOW_SYSTEM
13667 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13668 {
13669 clear_image_caches (Qnil);
13670 clear_image_cache_count = 0;
13671 }
13672 #endif /* HAVE_WINDOW_SYSTEM */
13673
13674 end_of_redisplay:
13675 backtrace_list = backtrace.next;
13676 unbind_to (count, Qnil);
13677 RESUME_POLLING;
13678 }
13679
13680
13681 /* Redisplay, but leave alone any recent echo area message unless
13682 another message has been requested in its place.
13683
13684 This is useful in situations where you need to redisplay but no
13685 user action has occurred, making it inappropriate for the message
13686 area to be cleared. See tracking_off and
13687 wait_reading_process_output for examples of these situations.
13688
13689 FROM_WHERE is an integer saying from where this function was
13690 called. This is useful for debugging. */
13691
13692 void
13693 redisplay_preserve_echo_area (int from_where)
13694 {
13695 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13696
13697 if (!NILP (echo_area_buffer[1]))
13698 {
13699 /* We have a previously displayed message, but no current
13700 message. Redisplay the previous message. */
13701 display_last_displayed_message_p = 1;
13702 redisplay_internal ();
13703 display_last_displayed_message_p = 0;
13704 }
13705 else
13706 redisplay_internal ();
13707
13708 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13709 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13710 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13711 }
13712
13713
13714 /* Function registered with record_unwind_protect in redisplay_internal.
13715 Clear redisplaying_p. Also select the previously selected frame. */
13716
13717 static Lisp_Object
13718 unwind_redisplay (Lisp_Object old_frame)
13719 {
13720 redisplaying_p = 0;
13721 return Qnil;
13722 }
13723
13724
13725 /* Mark the display of window W as accurate or inaccurate. If
13726 ACCURATE_P is non-zero mark display of W as accurate. If
13727 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13728 redisplay_internal is called. */
13729
13730 static void
13731 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13732 {
13733 if (BUFFERP (w->buffer))
13734 {
13735 struct buffer *b = XBUFFER (w->buffer);
13736
13737 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13738 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13739 w->last_had_star
13740 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13741
13742 if (accurate_p)
13743 {
13744 b->clip_changed = 0;
13745 b->prevent_redisplay_optimizations_p = 0;
13746
13747 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13748 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13749 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13750 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13751
13752 w->current_matrix->buffer = b;
13753 w->current_matrix->begv = BUF_BEGV (b);
13754 w->current_matrix->zv = BUF_ZV (b);
13755
13756 w->last_cursor = w->cursor;
13757 w->last_cursor_off_p = w->cursor_off_p;
13758
13759 if (w == XWINDOW (selected_window))
13760 w->last_point = BUF_PT (b);
13761 else
13762 w->last_point = marker_position (w->pointm);
13763 }
13764 }
13765
13766 if (accurate_p)
13767 {
13768 wset_window_end_valid (w, w->buffer);
13769 w->update_mode_line = 0;
13770 }
13771 }
13772
13773
13774 /* Mark the display of windows in the window tree rooted at WINDOW as
13775 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13776 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13777 be redisplayed the next time redisplay_internal is called. */
13778
13779 void
13780 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13781 {
13782 struct window *w;
13783
13784 for (; !NILP (window); window = w->next)
13785 {
13786 w = XWINDOW (window);
13787 mark_window_display_accurate_1 (w, accurate_p);
13788
13789 if (!NILP (w->vchild))
13790 mark_window_display_accurate (w->vchild, accurate_p);
13791 if (!NILP (w->hchild))
13792 mark_window_display_accurate (w->hchild, accurate_p);
13793 }
13794
13795 if (accurate_p)
13796 {
13797 update_overlay_arrows (1);
13798 }
13799 else
13800 {
13801 /* Force a thorough redisplay the next time by setting
13802 last_arrow_position and last_arrow_string to t, which is
13803 unequal to any useful value of Voverlay_arrow_... */
13804 update_overlay_arrows (-1);
13805 }
13806 }
13807
13808
13809 /* Return value in display table DP (Lisp_Char_Table *) for character
13810 C. Since a display table doesn't have any parent, we don't have to
13811 follow parent. Do not call this function directly but use the
13812 macro DISP_CHAR_VECTOR. */
13813
13814 Lisp_Object
13815 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13816 {
13817 Lisp_Object val;
13818
13819 if (ASCII_CHAR_P (c))
13820 {
13821 val = dp->ascii;
13822 if (SUB_CHAR_TABLE_P (val))
13823 val = XSUB_CHAR_TABLE (val)->contents[c];
13824 }
13825 else
13826 {
13827 Lisp_Object table;
13828
13829 XSETCHAR_TABLE (table, dp);
13830 val = char_table_ref (table, c);
13831 }
13832 if (NILP (val))
13833 val = dp->defalt;
13834 return val;
13835 }
13836
13837
13838 \f
13839 /***********************************************************************
13840 Window Redisplay
13841 ***********************************************************************/
13842
13843 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13844
13845 static void
13846 redisplay_windows (Lisp_Object window)
13847 {
13848 while (!NILP (window))
13849 {
13850 struct window *w = XWINDOW (window);
13851
13852 if (!NILP (w->hchild))
13853 redisplay_windows (w->hchild);
13854 else if (!NILP (w->vchild))
13855 redisplay_windows (w->vchild);
13856 else if (!NILP (w->buffer))
13857 {
13858 displayed_buffer = XBUFFER (w->buffer);
13859 /* Use list_of_error, not Qerror, so that
13860 we catch only errors and don't run the debugger. */
13861 internal_condition_case_1 (redisplay_window_0, window,
13862 list_of_error,
13863 redisplay_window_error);
13864 }
13865
13866 window = w->next;
13867 }
13868 }
13869
13870 static Lisp_Object
13871 redisplay_window_error (Lisp_Object ignore)
13872 {
13873 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13874 return Qnil;
13875 }
13876
13877 static Lisp_Object
13878 redisplay_window_0 (Lisp_Object window)
13879 {
13880 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13881 redisplay_window (window, 0);
13882 return Qnil;
13883 }
13884
13885 static Lisp_Object
13886 redisplay_window_1 (Lisp_Object window)
13887 {
13888 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13889 redisplay_window (window, 1);
13890 return Qnil;
13891 }
13892 \f
13893
13894 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13895 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13896 which positions recorded in ROW differ from current buffer
13897 positions.
13898
13899 Return 0 if cursor is not on this row, 1 otherwise. */
13900
13901 static int
13902 set_cursor_from_row (struct window *w, struct glyph_row *row,
13903 struct glyph_matrix *matrix,
13904 ptrdiff_t delta, ptrdiff_t delta_bytes,
13905 int dy, int dvpos)
13906 {
13907 struct glyph *glyph = row->glyphs[TEXT_AREA];
13908 struct glyph *end = glyph + row->used[TEXT_AREA];
13909 struct glyph *cursor = NULL;
13910 /* The last known character position in row. */
13911 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13912 int x = row->x;
13913 ptrdiff_t pt_old = PT - delta;
13914 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13915 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13916 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13917 /* A glyph beyond the edge of TEXT_AREA which we should never
13918 touch. */
13919 struct glyph *glyphs_end = end;
13920 /* Non-zero means we've found a match for cursor position, but that
13921 glyph has the avoid_cursor_p flag set. */
13922 int match_with_avoid_cursor = 0;
13923 /* Non-zero means we've seen at least one glyph that came from a
13924 display string. */
13925 int string_seen = 0;
13926 /* Largest and smallest buffer positions seen so far during scan of
13927 glyph row. */
13928 ptrdiff_t bpos_max = pos_before;
13929 ptrdiff_t bpos_min = pos_after;
13930 /* Last buffer position covered by an overlay string with an integer
13931 `cursor' property. */
13932 ptrdiff_t bpos_covered = 0;
13933 /* Non-zero means the display string on which to display the cursor
13934 comes from a text property, not from an overlay. */
13935 int string_from_text_prop = 0;
13936
13937 /* Don't even try doing anything if called for a mode-line or
13938 header-line row, since the rest of the code isn't prepared to
13939 deal with such calamities. */
13940 eassert (!row->mode_line_p);
13941 if (row->mode_line_p)
13942 return 0;
13943
13944 /* Skip over glyphs not having an object at the start and the end of
13945 the row. These are special glyphs like truncation marks on
13946 terminal frames. */
13947 if (row->displays_text_p)
13948 {
13949 if (!row->reversed_p)
13950 {
13951 while (glyph < end
13952 && INTEGERP (glyph->object)
13953 && glyph->charpos < 0)
13954 {
13955 x += glyph->pixel_width;
13956 ++glyph;
13957 }
13958 while (end > glyph
13959 && INTEGERP ((end - 1)->object)
13960 /* CHARPOS is zero for blanks and stretch glyphs
13961 inserted by extend_face_to_end_of_line. */
13962 && (end - 1)->charpos <= 0)
13963 --end;
13964 glyph_before = glyph - 1;
13965 glyph_after = end;
13966 }
13967 else
13968 {
13969 struct glyph *g;
13970
13971 /* If the glyph row is reversed, we need to process it from back
13972 to front, so swap the edge pointers. */
13973 glyphs_end = end = glyph - 1;
13974 glyph += row->used[TEXT_AREA] - 1;
13975
13976 while (glyph > end + 1
13977 && INTEGERP (glyph->object)
13978 && glyph->charpos < 0)
13979 {
13980 --glyph;
13981 x -= glyph->pixel_width;
13982 }
13983 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13984 --glyph;
13985 /* By default, in reversed rows we put the cursor on the
13986 rightmost (first in the reading order) glyph. */
13987 for (g = end + 1; g < glyph; g++)
13988 x += g->pixel_width;
13989 while (end < glyph
13990 && INTEGERP ((end + 1)->object)
13991 && (end + 1)->charpos <= 0)
13992 ++end;
13993 glyph_before = glyph + 1;
13994 glyph_after = end;
13995 }
13996 }
13997 else if (row->reversed_p)
13998 {
13999 /* In R2L rows that don't display text, put the cursor on the
14000 rightmost glyph. Case in point: an empty last line that is
14001 part of an R2L paragraph. */
14002 cursor = end - 1;
14003 /* Avoid placing the cursor on the last glyph of the row, where
14004 on terminal frames we hold the vertical border between
14005 adjacent windows. */
14006 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14007 && !WINDOW_RIGHTMOST_P (w)
14008 && cursor == row->glyphs[LAST_AREA] - 1)
14009 cursor--;
14010 x = -1; /* will be computed below, at label compute_x */
14011 }
14012
14013 /* Step 1: Try to find the glyph whose character position
14014 corresponds to point. If that's not possible, find 2 glyphs
14015 whose character positions are the closest to point, one before
14016 point, the other after it. */
14017 if (!row->reversed_p)
14018 while (/* not marched to end of glyph row */
14019 glyph < end
14020 /* glyph was not inserted by redisplay for internal purposes */
14021 && !INTEGERP (glyph->object))
14022 {
14023 if (BUFFERP (glyph->object))
14024 {
14025 ptrdiff_t dpos = glyph->charpos - pt_old;
14026
14027 if (glyph->charpos > bpos_max)
14028 bpos_max = glyph->charpos;
14029 if (glyph->charpos < bpos_min)
14030 bpos_min = glyph->charpos;
14031 if (!glyph->avoid_cursor_p)
14032 {
14033 /* If we hit point, we've found the glyph on which to
14034 display the cursor. */
14035 if (dpos == 0)
14036 {
14037 match_with_avoid_cursor = 0;
14038 break;
14039 }
14040 /* See if we've found a better approximation to
14041 POS_BEFORE or to POS_AFTER. */
14042 if (0 > dpos && dpos > pos_before - pt_old)
14043 {
14044 pos_before = glyph->charpos;
14045 glyph_before = glyph;
14046 }
14047 else if (0 < dpos && dpos < pos_after - pt_old)
14048 {
14049 pos_after = glyph->charpos;
14050 glyph_after = glyph;
14051 }
14052 }
14053 else if (dpos == 0)
14054 match_with_avoid_cursor = 1;
14055 }
14056 else if (STRINGP (glyph->object))
14057 {
14058 Lisp_Object chprop;
14059 ptrdiff_t glyph_pos = glyph->charpos;
14060
14061 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14062 glyph->object);
14063 if (!NILP (chprop))
14064 {
14065 /* If the string came from a `display' text property,
14066 look up the buffer position of that property and
14067 use that position to update bpos_max, as if we
14068 actually saw such a position in one of the row's
14069 glyphs. This helps with supporting integer values
14070 of `cursor' property on the display string in
14071 situations where most or all of the row's buffer
14072 text is completely covered by display properties,
14073 so that no glyph with valid buffer positions is
14074 ever seen in the row. */
14075 ptrdiff_t prop_pos =
14076 string_buffer_position_lim (glyph->object, pos_before,
14077 pos_after, 0);
14078
14079 if (prop_pos >= pos_before)
14080 bpos_max = prop_pos - 1;
14081 }
14082 if (INTEGERP (chprop))
14083 {
14084 bpos_covered = bpos_max + XINT (chprop);
14085 /* If the `cursor' property covers buffer positions up
14086 to and including point, we should display cursor on
14087 this glyph. Note that, if a `cursor' property on one
14088 of the string's characters has an integer value, we
14089 will break out of the loop below _before_ we get to
14090 the position match above. IOW, integer values of
14091 the `cursor' property override the "exact match for
14092 point" strategy of positioning the cursor. */
14093 /* Implementation note: bpos_max == pt_old when, e.g.,
14094 we are in an empty line, where bpos_max is set to
14095 MATRIX_ROW_START_CHARPOS, see above. */
14096 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14097 {
14098 cursor = glyph;
14099 break;
14100 }
14101 }
14102
14103 string_seen = 1;
14104 }
14105 x += glyph->pixel_width;
14106 ++glyph;
14107 }
14108 else if (glyph > end) /* row is reversed */
14109 while (!INTEGERP (glyph->object))
14110 {
14111 if (BUFFERP (glyph->object))
14112 {
14113 ptrdiff_t dpos = glyph->charpos - pt_old;
14114
14115 if (glyph->charpos > bpos_max)
14116 bpos_max = glyph->charpos;
14117 if (glyph->charpos < bpos_min)
14118 bpos_min = glyph->charpos;
14119 if (!glyph->avoid_cursor_p)
14120 {
14121 if (dpos == 0)
14122 {
14123 match_with_avoid_cursor = 0;
14124 break;
14125 }
14126 if (0 > dpos && dpos > pos_before - pt_old)
14127 {
14128 pos_before = glyph->charpos;
14129 glyph_before = glyph;
14130 }
14131 else if (0 < dpos && dpos < pos_after - pt_old)
14132 {
14133 pos_after = glyph->charpos;
14134 glyph_after = glyph;
14135 }
14136 }
14137 else if (dpos == 0)
14138 match_with_avoid_cursor = 1;
14139 }
14140 else if (STRINGP (glyph->object))
14141 {
14142 Lisp_Object chprop;
14143 ptrdiff_t glyph_pos = glyph->charpos;
14144
14145 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14146 glyph->object);
14147 if (!NILP (chprop))
14148 {
14149 ptrdiff_t prop_pos =
14150 string_buffer_position_lim (glyph->object, pos_before,
14151 pos_after, 0);
14152
14153 if (prop_pos >= pos_before)
14154 bpos_max = prop_pos - 1;
14155 }
14156 if (INTEGERP (chprop))
14157 {
14158 bpos_covered = bpos_max + XINT (chprop);
14159 /* If the `cursor' property covers buffer positions up
14160 to and including point, we should display cursor on
14161 this glyph. */
14162 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14163 {
14164 cursor = glyph;
14165 break;
14166 }
14167 }
14168 string_seen = 1;
14169 }
14170 --glyph;
14171 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14172 {
14173 x--; /* can't use any pixel_width */
14174 break;
14175 }
14176 x -= glyph->pixel_width;
14177 }
14178
14179 /* Step 2: If we didn't find an exact match for point, we need to
14180 look for a proper place to put the cursor among glyphs between
14181 GLYPH_BEFORE and GLYPH_AFTER. */
14182 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14183 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14184 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14185 {
14186 /* An empty line has a single glyph whose OBJECT is zero and
14187 whose CHARPOS is the position of a newline on that line.
14188 Note that on a TTY, there are more glyphs after that, which
14189 were produced by extend_face_to_end_of_line, but their
14190 CHARPOS is zero or negative. */
14191 int empty_line_p =
14192 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14193 && INTEGERP (glyph->object) && glyph->charpos > 0
14194 /* On a TTY, continued and truncated rows also have a glyph at
14195 their end whose OBJECT is zero and whose CHARPOS is
14196 positive (the continuation and truncation glyphs), but such
14197 rows are obviously not "empty". */
14198 && !(row->continued_p || row->truncated_on_right_p);
14199
14200 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14201 {
14202 ptrdiff_t ellipsis_pos;
14203
14204 /* Scan back over the ellipsis glyphs. */
14205 if (!row->reversed_p)
14206 {
14207 ellipsis_pos = (glyph - 1)->charpos;
14208 while (glyph > row->glyphs[TEXT_AREA]
14209 && (glyph - 1)->charpos == ellipsis_pos)
14210 glyph--, x -= glyph->pixel_width;
14211 /* That loop always goes one position too far, including
14212 the glyph before the ellipsis. So scan forward over
14213 that one. */
14214 x += glyph->pixel_width;
14215 glyph++;
14216 }
14217 else /* row is reversed */
14218 {
14219 ellipsis_pos = (glyph + 1)->charpos;
14220 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14221 && (glyph + 1)->charpos == ellipsis_pos)
14222 glyph++, x += glyph->pixel_width;
14223 x -= glyph->pixel_width;
14224 glyph--;
14225 }
14226 }
14227 else if (match_with_avoid_cursor)
14228 {
14229 cursor = glyph_after;
14230 x = -1;
14231 }
14232 else if (string_seen)
14233 {
14234 int incr = row->reversed_p ? -1 : +1;
14235
14236 /* Need to find the glyph that came out of a string which is
14237 present at point. That glyph is somewhere between
14238 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14239 positioned between POS_BEFORE and POS_AFTER in the
14240 buffer. */
14241 struct glyph *start, *stop;
14242 ptrdiff_t pos = pos_before;
14243
14244 x = -1;
14245
14246 /* If the row ends in a newline from a display string,
14247 reordering could have moved the glyphs belonging to the
14248 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14249 in this case we extend the search to the last glyph in
14250 the row that was not inserted by redisplay. */
14251 if (row->ends_in_newline_from_string_p)
14252 {
14253 glyph_after = end;
14254 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14255 }
14256
14257 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14258 correspond to POS_BEFORE and POS_AFTER, respectively. We
14259 need START and STOP in the order that corresponds to the
14260 row's direction as given by its reversed_p flag. If the
14261 directionality of characters between POS_BEFORE and
14262 POS_AFTER is the opposite of the row's base direction,
14263 these characters will have been reordered for display,
14264 and we need to reverse START and STOP. */
14265 if (!row->reversed_p)
14266 {
14267 start = min (glyph_before, glyph_after);
14268 stop = max (glyph_before, glyph_after);
14269 }
14270 else
14271 {
14272 start = max (glyph_before, glyph_after);
14273 stop = min (glyph_before, glyph_after);
14274 }
14275 for (glyph = start + incr;
14276 row->reversed_p ? glyph > stop : glyph < stop; )
14277 {
14278
14279 /* Any glyphs that come from the buffer are here because
14280 of bidi reordering. Skip them, and only pay
14281 attention to glyphs that came from some string. */
14282 if (STRINGP (glyph->object))
14283 {
14284 Lisp_Object str;
14285 ptrdiff_t tem;
14286 /* If the display property covers the newline, we
14287 need to search for it one position farther. */
14288 ptrdiff_t lim = pos_after
14289 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14290
14291 string_from_text_prop = 0;
14292 str = glyph->object;
14293 tem = string_buffer_position_lim (str, pos, lim, 0);
14294 if (tem == 0 /* from overlay */
14295 || pos <= tem)
14296 {
14297 /* If the string from which this glyph came is
14298 found in the buffer at point, or at position
14299 that is closer to point than pos_after, then
14300 we've found the glyph we've been looking for.
14301 If it comes from an overlay (tem == 0), and
14302 it has the `cursor' property on one of its
14303 glyphs, record that glyph as a candidate for
14304 displaying the cursor. (As in the
14305 unidirectional version, we will display the
14306 cursor on the last candidate we find.) */
14307 if (tem == 0
14308 || tem == pt_old
14309 || (tem - pt_old > 0 && tem < pos_after))
14310 {
14311 /* The glyphs from this string could have
14312 been reordered. Find the one with the
14313 smallest string position. Or there could
14314 be a character in the string with the
14315 `cursor' property, which means display
14316 cursor on that character's glyph. */
14317 ptrdiff_t strpos = glyph->charpos;
14318
14319 if (tem)
14320 {
14321 cursor = glyph;
14322 string_from_text_prop = 1;
14323 }
14324 for ( ;
14325 (row->reversed_p ? glyph > stop : glyph < stop)
14326 && EQ (glyph->object, str);
14327 glyph += incr)
14328 {
14329 Lisp_Object cprop;
14330 ptrdiff_t gpos = glyph->charpos;
14331
14332 cprop = Fget_char_property (make_number (gpos),
14333 Qcursor,
14334 glyph->object);
14335 if (!NILP (cprop))
14336 {
14337 cursor = glyph;
14338 break;
14339 }
14340 if (tem && glyph->charpos < strpos)
14341 {
14342 strpos = glyph->charpos;
14343 cursor = glyph;
14344 }
14345 }
14346
14347 if (tem == pt_old
14348 || (tem - pt_old > 0 && tem < pos_after))
14349 goto compute_x;
14350 }
14351 if (tem)
14352 pos = tem + 1; /* don't find previous instances */
14353 }
14354 /* This string is not what we want; skip all of the
14355 glyphs that came from it. */
14356 while ((row->reversed_p ? glyph > stop : glyph < stop)
14357 && EQ (glyph->object, str))
14358 glyph += incr;
14359 }
14360 else
14361 glyph += incr;
14362 }
14363
14364 /* If we reached the end of the line, and END was from a string,
14365 the cursor is not on this line. */
14366 if (cursor == NULL
14367 && (row->reversed_p ? glyph <= end : glyph >= end)
14368 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14369 && STRINGP (end->object)
14370 && row->continued_p)
14371 return 0;
14372 }
14373 /* A truncated row may not include PT among its character positions.
14374 Setting the cursor inside the scroll margin will trigger
14375 recalculation of hscroll in hscroll_window_tree. But if a
14376 display string covers point, defer to the string-handling
14377 code below to figure this out. */
14378 else if (row->truncated_on_left_p && pt_old < bpos_min)
14379 {
14380 cursor = glyph_before;
14381 x = -1;
14382 }
14383 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14384 /* Zero-width characters produce no glyphs. */
14385 || (!empty_line_p
14386 && (row->reversed_p
14387 ? glyph_after > glyphs_end
14388 : glyph_after < glyphs_end)))
14389 {
14390 cursor = glyph_after;
14391 x = -1;
14392 }
14393 }
14394
14395 compute_x:
14396 if (cursor != NULL)
14397 glyph = cursor;
14398 else if (glyph == glyphs_end
14399 && pos_before == pos_after
14400 && STRINGP ((row->reversed_p
14401 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14402 : row->glyphs[TEXT_AREA])->object))
14403 {
14404 /* If all the glyphs of this row came from strings, put the
14405 cursor on the first glyph of the row. This avoids having the
14406 cursor outside of the text area in this very rare and hard
14407 use case. */
14408 glyph =
14409 row->reversed_p
14410 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14411 : row->glyphs[TEXT_AREA];
14412 }
14413 if (x < 0)
14414 {
14415 struct glyph *g;
14416
14417 /* Need to compute x that corresponds to GLYPH. */
14418 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14419 {
14420 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14421 emacs_abort ();
14422 x += g->pixel_width;
14423 }
14424 }
14425
14426 /* ROW could be part of a continued line, which, under bidi
14427 reordering, might have other rows whose start and end charpos
14428 occlude point. Only set w->cursor if we found a better
14429 approximation to the cursor position than we have from previously
14430 examined candidate rows belonging to the same continued line. */
14431 if (/* we already have a candidate row */
14432 w->cursor.vpos >= 0
14433 /* that candidate is not the row we are processing */
14434 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14435 /* Make sure cursor.vpos specifies a row whose start and end
14436 charpos occlude point, and it is valid candidate for being a
14437 cursor-row. This is because some callers of this function
14438 leave cursor.vpos at the row where the cursor was displayed
14439 during the last redisplay cycle. */
14440 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14441 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14442 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14443 {
14444 struct glyph *g1 =
14445 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14446
14447 /* Don't consider glyphs that are outside TEXT_AREA. */
14448 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14449 return 0;
14450 /* Keep the candidate whose buffer position is the closest to
14451 point or has the `cursor' property. */
14452 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14453 w->cursor.hpos >= 0
14454 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14455 && ((BUFFERP (g1->object)
14456 && (g1->charpos == pt_old /* an exact match always wins */
14457 || (BUFFERP (glyph->object)
14458 && eabs (g1->charpos - pt_old)
14459 < eabs (glyph->charpos - pt_old))))
14460 /* previous candidate is a glyph from a string that has
14461 a non-nil `cursor' property */
14462 || (STRINGP (g1->object)
14463 && (!NILP (Fget_char_property (make_number (g1->charpos),
14464 Qcursor, g1->object))
14465 /* previous candidate is from the same display
14466 string as this one, and the display string
14467 came from a text property */
14468 || (EQ (g1->object, glyph->object)
14469 && string_from_text_prop)
14470 /* this candidate is from newline and its
14471 position is not an exact match */
14472 || (INTEGERP (glyph->object)
14473 && glyph->charpos != pt_old)))))
14474 return 0;
14475 /* If this candidate gives an exact match, use that. */
14476 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14477 /* If this candidate is a glyph created for the
14478 terminating newline of a line, and point is on that
14479 newline, it wins because it's an exact match. */
14480 || (!row->continued_p
14481 && INTEGERP (glyph->object)
14482 && glyph->charpos == 0
14483 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14484 /* Otherwise, keep the candidate that comes from a row
14485 spanning less buffer positions. This may win when one or
14486 both candidate positions are on glyphs that came from
14487 display strings, for which we cannot compare buffer
14488 positions. */
14489 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14490 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14491 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14492 return 0;
14493 }
14494 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14495 w->cursor.x = x;
14496 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14497 w->cursor.y = row->y + dy;
14498
14499 if (w == XWINDOW (selected_window))
14500 {
14501 if (!row->continued_p
14502 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14503 && row->x == 0)
14504 {
14505 this_line_buffer = XBUFFER (w->buffer);
14506
14507 CHARPOS (this_line_start_pos)
14508 = MATRIX_ROW_START_CHARPOS (row) + delta;
14509 BYTEPOS (this_line_start_pos)
14510 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14511
14512 CHARPOS (this_line_end_pos)
14513 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14514 BYTEPOS (this_line_end_pos)
14515 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14516
14517 this_line_y = w->cursor.y;
14518 this_line_pixel_height = row->height;
14519 this_line_vpos = w->cursor.vpos;
14520 this_line_start_x = row->x;
14521 }
14522 else
14523 CHARPOS (this_line_start_pos) = 0;
14524 }
14525
14526 return 1;
14527 }
14528
14529
14530 /* Run window scroll functions, if any, for WINDOW with new window
14531 start STARTP. Sets the window start of WINDOW to that position.
14532
14533 We assume that the window's buffer is really current. */
14534
14535 static struct text_pos
14536 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14537 {
14538 struct window *w = XWINDOW (window);
14539 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14540
14541 if (current_buffer != XBUFFER (w->buffer))
14542 emacs_abort ();
14543
14544 if (!NILP (Vwindow_scroll_functions))
14545 {
14546 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14547 make_number (CHARPOS (startp)));
14548 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14549 /* In case the hook functions switch buffers. */
14550 set_buffer_internal (XBUFFER (w->buffer));
14551 }
14552
14553 return startp;
14554 }
14555
14556
14557 /* Make sure the line containing the cursor is fully visible.
14558 A value of 1 means there is nothing to be done.
14559 (Either the line is fully visible, or it cannot be made so,
14560 or we cannot tell.)
14561
14562 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14563 is higher than window.
14564
14565 A value of 0 means the caller should do scrolling
14566 as if point had gone off the screen. */
14567
14568 static int
14569 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14570 {
14571 struct glyph_matrix *matrix;
14572 struct glyph_row *row;
14573 int window_height;
14574
14575 if (!make_cursor_line_fully_visible_p)
14576 return 1;
14577
14578 /* It's not always possible to find the cursor, e.g, when a window
14579 is full of overlay strings. Don't do anything in that case. */
14580 if (w->cursor.vpos < 0)
14581 return 1;
14582
14583 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14584 row = MATRIX_ROW (matrix, w->cursor.vpos);
14585
14586 /* If the cursor row is not partially visible, there's nothing to do. */
14587 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14588 return 1;
14589
14590 /* If the row the cursor is in is taller than the window's height,
14591 it's not clear what to do, so do nothing. */
14592 window_height = window_box_height (w);
14593 if (row->height >= window_height)
14594 {
14595 if (!force_p || MINI_WINDOW_P (w)
14596 || w->vscroll || w->cursor.vpos == 0)
14597 return 1;
14598 }
14599 return 0;
14600 }
14601
14602
14603 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14604 non-zero means only WINDOW is redisplayed in redisplay_internal.
14605 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14606 in redisplay_window to bring a partially visible line into view in
14607 the case that only the cursor has moved.
14608
14609 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14610 last screen line's vertical height extends past the end of the screen.
14611
14612 Value is
14613
14614 1 if scrolling succeeded
14615
14616 0 if scrolling didn't find point.
14617
14618 -1 if new fonts have been loaded so that we must interrupt
14619 redisplay, adjust glyph matrices, and try again. */
14620
14621 enum
14622 {
14623 SCROLLING_SUCCESS,
14624 SCROLLING_FAILED,
14625 SCROLLING_NEED_LARGER_MATRICES
14626 };
14627
14628 /* If scroll-conservatively is more than this, never recenter.
14629
14630 If you change this, don't forget to update the doc string of
14631 `scroll-conservatively' and the Emacs manual. */
14632 #define SCROLL_LIMIT 100
14633
14634 static int
14635 try_scrolling (Lisp_Object window, int just_this_one_p,
14636 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14637 int temp_scroll_step, int last_line_misfit)
14638 {
14639 struct window *w = XWINDOW (window);
14640 struct frame *f = XFRAME (w->frame);
14641 struct text_pos pos, startp;
14642 struct it it;
14643 int this_scroll_margin, scroll_max, rc, height;
14644 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14645 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14646 Lisp_Object aggressive;
14647 /* We will never try scrolling more than this number of lines. */
14648 int scroll_limit = SCROLL_LIMIT;
14649
14650 #ifdef GLYPH_DEBUG
14651 debug_method_add (w, "try_scrolling");
14652 #endif
14653
14654 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14655
14656 /* Compute scroll margin height in pixels. We scroll when point is
14657 within this distance from the top or bottom of the window. */
14658 if (scroll_margin > 0)
14659 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14660 * FRAME_LINE_HEIGHT (f);
14661 else
14662 this_scroll_margin = 0;
14663
14664 /* Force arg_scroll_conservatively to have a reasonable value, to
14665 avoid scrolling too far away with slow move_it_* functions. Note
14666 that the user can supply scroll-conservatively equal to
14667 `most-positive-fixnum', which can be larger than INT_MAX. */
14668 if (arg_scroll_conservatively > scroll_limit)
14669 {
14670 arg_scroll_conservatively = scroll_limit + 1;
14671 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14672 }
14673 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14674 /* Compute how much we should try to scroll maximally to bring
14675 point into view. */
14676 scroll_max = (max (scroll_step,
14677 max (arg_scroll_conservatively, temp_scroll_step))
14678 * FRAME_LINE_HEIGHT (f));
14679 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14680 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14681 /* We're trying to scroll because of aggressive scrolling but no
14682 scroll_step is set. Choose an arbitrary one. */
14683 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14684 else
14685 scroll_max = 0;
14686
14687 too_near_end:
14688
14689 /* Decide whether to scroll down. */
14690 if (PT > CHARPOS (startp))
14691 {
14692 int scroll_margin_y;
14693
14694 /* Compute the pixel ypos of the scroll margin, then move IT to
14695 either that ypos or PT, whichever comes first. */
14696 start_display (&it, w, startp);
14697 scroll_margin_y = it.last_visible_y - this_scroll_margin
14698 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14699 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14700 (MOVE_TO_POS | MOVE_TO_Y));
14701
14702 if (PT > CHARPOS (it.current.pos))
14703 {
14704 int y0 = line_bottom_y (&it);
14705 /* Compute how many pixels below window bottom to stop searching
14706 for PT. This avoids costly search for PT that is far away if
14707 the user limited scrolling by a small number of lines, but
14708 always finds PT if scroll_conservatively is set to a large
14709 number, such as most-positive-fixnum. */
14710 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14711 int y_to_move = it.last_visible_y + slack;
14712
14713 /* Compute the distance from the scroll margin to PT or to
14714 the scroll limit, whichever comes first. This should
14715 include the height of the cursor line, to make that line
14716 fully visible. */
14717 move_it_to (&it, PT, -1, y_to_move,
14718 -1, MOVE_TO_POS | MOVE_TO_Y);
14719 dy = line_bottom_y (&it) - y0;
14720
14721 if (dy > scroll_max)
14722 return SCROLLING_FAILED;
14723
14724 if (dy > 0)
14725 scroll_down_p = 1;
14726 }
14727 }
14728
14729 if (scroll_down_p)
14730 {
14731 /* Point is in or below the bottom scroll margin, so move the
14732 window start down. If scrolling conservatively, move it just
14733 enough down to make point visible. If scroll_step is set,
14734 move it down by scroll_step. */
14735 if (arg_scroll_conservatively)
14736 amount_to_scroll
14737 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14738 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14739 else if (scroll_step || temp_scroll_step)
14740 amount_to_scroll = scroll_max;
14741 else
14742 {
14743 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14744 height = WINDOW_BOX_TEXT_HEIGHT (w);
14745 if (NUMBERP (aggressive))
14746 {
14747 double float_amount = XFLOATINT (aggressive) * height;
14748 int aggressive_scroll = float_amount;
14749 if (aggressive_scroll == 0 && float_amount > 0)
14750 aggressive_scroll = 1;
14751 /* Don't let point enter the scroll margin near top of
14752 the window. This could happen if the value of
14753 scroll_up_aggressively is too large and there are
14754 non-zero margins, because scroll_up_aggressively
14755 means put point that fraction of window height
14756 _from_the_bottom_margin_. */
14757 if (aggressive_scroll + 2*this_scroll_margin > height)
14758 aggressive_scroll = height - 2*this_scroll_margin;
14759 amount_to_scroll = dy + aggressive_scroll;
14760 }
14761 }
14762
14763 if (amount_to_scroll <= 0)
14764 return SCROLLING_FAILED;
14765
14766 start_display (&it, w, startp);
14767 if (arg_scroll_conservatively <= scroll_limit)
14768 move_it_vertically (&it, amount_to_scroll);
14769 else
14770 {
14771 /* Extra precision for users who set scroll-conservatively
14772 to a large number: make sure the amount we scroll
14773 the window start is never less than amount_to_scroll,
14774 which was computed as distance from window bottom to
14775 point. This matters when lines at window top and lines
14776 below window bottom have different height. */
14777 struct it it1;
14778 void *it1data = NULL;
14779 /* We use a temporary it1 because line_bottom_y can modify
14780 its argument, if it moves one line down; see there. */
14781 int start_y;
14782
14783 SAVE_IT (it1, it, it1data);
14784 start_y = line_bottom_y (&it1);
14785 do {
14786 RESTORE_IT (&it, &it, it1data);
14787 move_it_by_lines (&it, 1);
14788 SAVE_IT (it1, it, it1data);
14789 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14790 }
14791
14792 /* If STARTP is unchanged, move it down another screen line. */
14793 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14794 move_it_by_lines (&it, 1);
14795 startp = it.current.pos;
14796 }
14797 else
14798 {
14799 struct text_pos scroll_margin_pos = startp;
14800
14801 /* See if point is inside the scroll margin at the top of the
14802 window. */
14803 if (this_scroll_margin)
14804 {
14805 start_display (&it, w, startp);
14806 move_it_vertically (&it, this_scroll_margin);
14807 scroll_margin_pos = it.current.pos;
14808 }
14809
14810 if (PT < CHARPOS (scroll_margin_pos))
14811 {
14812 /* Point is in the scroll margin at the top of the window or
14813 above what is displayed in the window. */
14814 int y0, y_to_move;
14815
14816 /* Compute the vertical distance from PT to the scroll
14817 margin position. Move as far as scroll_max allows, or
14818 one screenful, or 10 screen lines, whichever is largest.
14819 Give up if distance is greater than scroll_max or if we
14820 didn't reach the scroll margin position. */
14821 SET_TEXT_POS (pos, PT, PT_BYTE);
14822 start_display (&it, w, pos);
14823 y0 = it.current_y;
14824 y_to_move = max (it.last_visible_y,
14825 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14826 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14827 y_to_move, -1,
14828 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14829 dy = it.current_y - y0;
14830 if (dy > scroll_max
14831 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14832 return SCROLLING_FAILED;
14833
14834 /* Compute new window start. */
14835 start_display (&it, w, startp);
14836
14837 if (arg_scroll_conservatively)
14838 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14839 max (scroll_step, temp_scroll_step));
14840 else if (scroll_step || temp_scroll_step)
14841 amount_to_scroll = scroll_max;
14842 else
14843 {
14844 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14845 height = WINDOW_BOX_TEXT_HEIGHT (w);
14846 if (NUMBERP (aggressive))
14847 {
14848 double float_amount = XFLOATINT (aggressive) * height;
14849 int aggressive_scroll = float_amount;
14850 if (aggressive_scroll == 0 && float_amount > 0)
14851 aggressive_scroll = 1;
14852 /* Don't let point enter the scroll margin near
14853 bottom of the window, if the value of
14854 scroll_down_aggressively happens to be too
14855 large. */
14856 if (aggressive_scroll + 2*this_scroll_margin > height)
14857 aggressive_scroll = height - 2*this_scroll_margin;
14858 amount_to_scroll = dy + aggressive_scroll;
14859 }
14860 }
14861
14862 if (amount_to_scroll <= 0)
14863 return SCROLLING_FAILED;
14864
14865 move_it_vertically_backward (&it, amount_to_scroll);
14866 startp = it.current.pos;
14867 }
14868 }
14869
14870 /* Run window scroll functions. */
14871 startp = run_window_scroll_functions (window, startp);
14872
14873 /* Display the window. Give up if new fonts are loaded, or if point
14874 doesn't appear. */
14875 if (!try_window (window, startp, 0))
14876 rc = SCROLLING_NEED_LARGER_MATRICES;
14877 else if (w->cursor.vpos < 0)
14878 {
14879 clear_glyph_matrix (w->desired_matrix);
14880 rc = SCROLLING_FAILED;
14881 }
14882 else
14883 {
14884 /* Maybe forget recorded base line for line number display. */
14885 if (!just_this_one_p
14886 || current_buffer->clip_changed
14887 || BEG_UNCHANGED < CHARPOS (startp))
14888 wset_base_line_number (w, Qnil);
14889
14890 /* If cursor ends up on a partially visible line,
14891 treat that as being off the bottom of the screen. */
14892 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14893 /* It's possible that the cursor is on the first line of the
14894 buffer, which is partially obscured due to a vscroll
14895 (Bug#7537). In that case, avoid looping forever . */
14896 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14897 {
14898 clear_glyph_matrix (w->desired_matrix);
14899 ++extra_scroll_margin_lines;
14900 goto too_near_end;
14901 }
14902 rc = SCROLLING_SUCCESS;
14903 }
14904
14905 return rc;
14906 }
14907
14908
14909 /* Compute a suitable window start for window W if display of W starts
14910 on a continuation line. Value is non-zero if a new window start
14911 was computed.
14912
14913 The new window start will be computed, based on W's width, starting
14914 from the start of the continued line. It is the start of the
14915 screen line with the minimum distance from the old start W->start. */
14916
14917 static int
14918 compute_window_start_on_continuation_line (struct window *w)
14919 {
14920 struct text_pos pos, start_pos;
14921 int window_start_changed_p = 0;
14922
14923 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14924
14925 /* If window start is on a continuation line... Window start may be
14926 < BEGV in case there's invisible text at the start of the
14927 buffer (M-x rmail, for example). */
14928 if (CHARPOS (start_pos) > BEGV
14929 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14930 {
14931 struct it it;
14932 struct glyph_row *row;
14933
14934 /* Handle the case that the window start is out of range. */
14935 if (CHARPOS (start_pos) < BEGV)
14936 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14937 else if (CHARPOS (start_pos) > ZV)
14938 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14939
14940 /* Find the start of the continued line. This should be fast
14941 because scan_buffer is fast (newline cache). */
14942 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14943 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14944 row, DEFAULT_FACE_ID);
14945 reseat_at_previous_visible_line_start (&it);
14946
14947 /* If the line start is "too far" away from the window start,
14948 say it takes too much time to compute a new window start. */
14949 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14950 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14951 {
14952 int min_distance, distance;
14953
14954 /* Move forward by display lines to find the new window
14955 start. If window width was enlarged, the new start can
14956 be expected to be > the old start. If window width was
14957 decreased, the new window start will be < the old start.
14958 So, we're looking for the display line start with the
14959 minimum distance from the old window start. */
14960 pos = it.current.pos;
14961 min_distance = INFINITY;
14962 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14963 distance < min_distance)
14964 {
14965 min_distance = distance;
14966 pos = it.current.pos;
14967 move_it_by_lines (&it, 1);
14968 }
14969
14970 /* Set the window start there. */
14971 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14972 window_start_changed_p = 1;
14973 }
14974 }
14975
14976 return window_start_changed_p;
14977 }
14978
14979
14980 /* Try cursor movement in case text has not changed in window WINDOW,
14981 with window start STARTP. Value is
14982
14983 CURSOR_MOVEMENT_SUCCESS if successful
14984
14985 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14986
14987 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14988 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14989 we want to scroll as if scroll-step were set to 1. See the code.
14990
14991 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14992 which case we have to abort this redisplay, and adjust matrices
14993 first. */
14994
14995 enum
14996 {
14997 CURSOR_MOVEMENT_SUCCESS,
14998 CURSOR_MOVEMENT_CANNOT_BE_USED,
14999 CURSOR_MOVEMENT_MUST_SCROLL,
15000 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15001 };
15002
15003 static int
15004 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15005 {
15006 struct window *w = XWINDOW (window);
15007 struct frame *f = XFRAME (w->frame);
15008 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15009
15010 #ifdef GLYPH_DEBUG
15011 if (inhibit_try_cursor_movement)
15012 return rc;
15013 #endif
15014
15015 /* Previously, there was a check for Lisp integer in the
15016 if-statement below. Now, this field is converted to
15017 ptrdiff_t, thus zero means invalid position in a buffer. */
15018 eassert (w->last_point > 0);
15019
15020 /* Handle case where text has not changed, only point, and it has
15021 not moved off the frame. */
15022 if (/* Point may be in this window. */
15023 PT >= CHARPOS (startp)
15024 /* Selective display hasn't changed. */
15025 && !current_buffer->clip_changed
15026 /* Function force-mode-line-update is used to force a thorough
15027 redisplay. It sets either windows_or_buffers_changed or
15028 update_mode_lines. So don't take a shortcut here for these
15029 cases. */
15030 && !update_mode_lines
15031 && !windows_or_buffers_changed
15032 && !cursor_type_changed
15033 /* Can't use this case if highlighting a region. When a
15034 region exists, cursor movement has to do more than just
15035 set the cursor. */
15036 && markpos_of_region () < 0
15037 && NILP (w->region_showing)
15038 && NILP (Vshow_trailing_whitespace)
15039 /* This code is not used for mini-buffer for the sake of the case
15040 of redisplaying to replace an echo area message; since in
15041 that case the mini-buffer contents per se are usually
15042 unchanged. This code is of no real use in the mini-buffer
15043 since the handling of this_line_start_pos, etc., in redisplay
15044 handles the same cases. */
15045 && !EQ (window, minibuf_window)
15046 /* When splitting windows or for new windows, it happens that
15047 redisplay is called with a nil window_end_vpos or one being
15048 larger than the window. This should really be fixed in
15049 window.c. I don't have this on my list, now, so we do
15050 approximately the same as the old redisplay code. --gerd. */
15051 && INTEGERP (w->window_end_vpos)
15052 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15053 && (FRAME_WINDOW_P (f)
15054 || !overlay_arrow_in_current_buffer_p ()))
15055 {
15056 int this_scroll_margin, top_scroll_margin;
15057 struct glyph_row *row = NULL;
15058
15059 #ifdef GLYPH_DEBUG
15060 debug_method_add (w, "cursor movement");
15061 #endif
15062
15063 /* Scroll if point within this distance from the top or bottom
15064 of the window. This is a pixel value. */
15065 if (scroll_margin > 0)
15066 {
15067 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15068 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15069 }
15070 else
15071 this_scroll_margin = 0;
15072
15073 top_scroll_margin = this_scroll_margin;
15074 if (WINDOW_WANTS_HEADER_LINE_P (w))
15075 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15076
15077 /* Start with the row the cursor was displayed during the last
15078 not paused redisplay. Give up if that row is not valid. */
15079 if (w->last_cursor.vpos < 0
15080 || w->last_cursor.vpos >= w->current_matrix->nrows)
15081 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15082 else
15083 {
15084 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15085 if (row->mode_line_p)
15086 ++row;
15087 if (!row->enabled_p)
15088 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15089 }
15090
15091 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15092 {
15093 int scroll_p = 0, must_scroll = 0;
15094 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15095
15096 if (PT > w->last_point)
15097 {
15098 /* Point has moved forward. */
15099 while (MATRIX_ROW_END_CHARPOS (row) < PT
15100 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15101 {
15102 eassert (row->enabled_p);
15103 ++row;
15104 }
15105
15106 /* If the end position of a row equals the start
15107 position of the next row, and PT is at that position,
15108 we would rather display cursor in the next line. */
15109 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15110 && MATRIX_ROW_END_CHARPOS (row) == PT
15111 && row < w->current_matrix->rows
15112 + w->current_matrix->nrows - 1
15113 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15114 && !cursor_row_p (row))
15115 ++row;
15116
15117 /* If within the scroll margin, scroll. Note that
15118 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15119 the next line would be drawn, and that
15120 this_scroll_margin can be zero. */
15121 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15122 || PT > MATRIX_ROW_END_CHARPOS (row)
15123 /* Line is completely visible last line in window
15124 and PT is to be set in the next line. */
15125 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15126 && PT == MATRIX_ROW_END_CHARPOS (row)
15127 && !row->ends_at_zv_p
15128 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15129 scroll_p = 1;
15130 }
15131 else if (PT < w->last_point)
15132 {
15133 /* Cursor has to be moved backward. Note that PT >=
15134 CHARPOS (startp) because of the outer if-statement. */
15135 while (!row->mode_line_p
15136 && (MATRIX_ROW_START_CHARPOS (row) > PT
15137 || (MATRIX_ROW_START_CHARPOS (row) == PT
15138 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15139 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15140 row > w->current_matrix->rows
15141 && (row-1)->ends_in_newline_from_string_p))))
15142 && (row->y > top_scroll_margin
15143 || CHARPOS (startp) == BEGV))
15144 {
15145 eassert (row->enabled_p);
15146 --row;
15147 }
15148
15149 /* Consider the following case: Window starts at BEGV,
15150 there is invisible, intangible text at BEGV, so that
15151 display starts at some point START > BEGV. It can
15152 happen that we are called with PT somewhere between
15153 BEGV and START. Try to handle that case. */
15154 if (row < w->current_matrix->rows
15155 || row->mode_line_p)
15156 {
15157 row = w->current_matrix->rows;
15158 if (row->mode_line_p)
15159 ++row;
15160 }
15161
15162 /* Due to newlines in overlay strings, we may have to
15163 skip forward over overlay strings. */
15164 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15165 && MATRIX_ROW_END_CHARPOS (row) == PT
15166 && !cursor_row_p (row))
15167 ++row;
15168
15169 /* If within the scroll margin, scroll. */
15170 if (row->y < top_scroll_margin
15171 && CHARPOS (startp) != BEGV)
15172 scroll_p = 1;
15173 }
15174 else
15175 {
15176 /* Cursor did not move. So don't scroll even if cursor line
15177 is partially visible, as it was so before. */
15178 rc = CURSOR_MOVEMENT_SUCCESS;
15179 }
15180
15181 if (PT < MATRIX_ROW_START_CHARPOS (row)
15182 || PT > MATRIX_ROW_END_CHARPOS (row))
15183 {
15184 /* if PT is not in the glyph row, give up. */
15185 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15186 must_scroll = 1;
15187 }
15188 else if (rc != CURSOR_MOVEMENT_SUCCESS
15189 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15190 {
15191 struct glyph_row *row1;
15192
15193 /* If rows are bidi-reordered and point moved, back up
15194 until we find a row that does not belong to a
15195 continuation line. This is because we must consider
15196 all rows of a continued line as candidates for the
15197 new cursor positioning, since row start and end
15198 positions change non-linearly with vertical position
15199 in such rows. */
15200 /* FIXME: Revisit this when glyph ``spilling'' in
15201 continuation lines' rows is implemented for
15202 bidi-reordered rows. */
15203 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15204 MATRIX_ROW_CONTINUATION_LINE_P (row);
15205 --row)
15206 {
15207 /* If we hit the beginning of the displayed portion
15208 without finding the first row of a continued
15209 line, give up. */
15210 if (row <= row1)
15211 {
15212 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15213 break;
15214 }
15215 eassert (row->enabled_p);
15216 }
15217 }
15218 if (must_scroll)
15219 ;
15220 else if (rc != CURSOR_MOVEMENT_SUCCESS
15221 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15222 /* Make sure this isn't a header line by any chance, since
15223 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15224 && !row->mode_line_p
15225 && make_cursor_line_fully_visible_p)
15226 {
15227 if (PT == MATRIX_ROW_END_CHARPOS (row)
15228 && !row->ends_at_zv_p
15229 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15230 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15231 else if (row->height > window_box_height (w))
15232 {
15233 /* If we end up in a partially visible line, let's
15234 make it fully visible, except when it's taller
15235 than the window, in which case we can't do much
15236 about it. */
15237 *scroll_step = 1;
15238 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15239 }
15240 else
15241 {
15242 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15243 if (!cursor_row_fully_visible_p (w, 0, 1))
15244 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15245 else
15246 rc = CURSOR_MOVEMENT_SUCCESS;
15247 }
15248 }
15249 else if (scroll_p)
15250 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15251 else if (rc != CURSOR_MOVEMENT_SUCCESS
15252 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15253 {
15254 /* With bidi-reordered rows, there could be more than
15255 one candidate row whose start and end positions
15256 occlude point. We need to let set_cursor_from_row
15257 find the best candidate. */
15258 /* FIXME: Revisit this when glyph ``spilling'' in
15259 continuation lines' rows is implemented for
15260 bidi-reordered rows. */
15261 int rv = 0;
15262
15263 do
15264 {
15265 int at_zv_p = 0, exact_match_p = 0;
15266
15267 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15268 && PT <= MATRIX_ROW_END_CHARPOS (row)
15269 && cursor_row_p (row))
15270 rv |= set_cursor_from_row (w, row, w->current_matrix,
15271 0, 0, 0, 0);
15272 /* As soon as we've found the exact match for point,
15273 or the first suitable row whose ends_at_zv_p flag
15274 is set, we are done. */
15275 at_zv_p =
15276 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15277 if (rv && !at_zv_p
15278 && w->cursor.hpos >= 0
15279 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15280 w->cursor.vpos))
15281 {
15282 struct glyph_row *candidate =
15283 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15284 struct glyph *g =
15285 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15286 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15287
15288 exact_match_p =
15289 (BUFFERP (g->object) && g->charpos == PT)
15290 || (INTEGERP (g->object)
15291 && (g->charpos == PT
15292 || (g->charpos == 0 && endpos - 1 == PT)));
15293 }
15294 if (rv && (at_zv_p || exact_match_p))
15295 {
15296 rc = CURSOR_MOVEMENT_SUCCESS;
15297 break;
15298 }
15299 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15300 break;
15301 ++row;
15302 }
15303 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15304 || row->continued_p)
15305 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15306 || (MATRIX_ROW_START_CHARPOS (row) == PT
15307 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15308 /* If we didn't find any candidate rows, or exited the
15309 loop before all the candidates were examined, signal
15310 to the caller that this method failed. */
15311 if (rc != CURSOR_MOVEMENT_SUCCESS
15312 && !(rv
15313 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15314 && !row->continued_p))
15315 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15316 else if (rv)
15317 rc = CURSOR_MOVEMENT_SUCCESS;
15318 }
15319 else
15320 {
15321 do
15322 {
15323 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15324 {
15325 rc = CURSOR_MOVEMENT_SUCCESS;
15326 break;
15327 }
15328 ++row;
15329 }
15330 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15331 && MATRIX_ROW_START_CHARPOS (row) == PT
15332 && cursor_row_p (row));
15333 }
15334 }
15335 }
15336
15337 return rc;
15338 }
15339
15340 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15341 static
15342 #endif
15343 void
15344 set_vertical_scroll_bar (struct window *w)
15345 {
15346 ptrdiff_t start, end, whole;
15347
15348 /* Calculate the start and end positions for the current window.
15349 At some point, it would be nice to choose between scrollbars
15350 which reflect the whole buffer size, with special markers
15351 indicating narrowing, and scrollbars which reflect only the
15352 visible region.
15353
15354 Note that mini-buffers sometimes aren't displaying any text. */
15355 if (!MINI_WINDOW_P (w)
15356 || (w == XWINDOW (minibuf_window)
15357 && NILP (echo_area_buffer[0])))
15358 {
15359 struct buffer *buf = XBUFFER (w->buffer);
15360 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15361 start = marker_position (w->start) - BUF_BEGV (buf);
15362 /* I don't think this is guaranteed to be right. For the
15363 moment, we'll pretend it is. */
15364 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15365
15366 if (end < start)
15367 end = start;
15368 if (whole < (end - start))
15369 whole = end - start;
15370 }
15371 else
15372 start = end = whole = 0;
15373
15374 /* Indicate what this scroll bar ought to be displaying now. */
15375 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15376 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15377 (w, end - start, whole, start);
15378 }
15379
15380
15381 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15382 selected_window is redisplayed.
15383
15384 We can return without actually redisplaying the window if
15385 fonts_changed_p. In that case, redisplay_internal will
15386 retry. */
15387
15388 static void
15389 redisplay_window (Lisp_Object window, int just_this_one_p)
15390 {
15391 struct window *w = XWINDOW (window);
15392 struct frame *f = XFRAME (w->frame);
15393 struct buffer *buffer = XBUFFER (w->buffer);
15394 struct buffer *old = current_buffer;
15395 struct text_pos lpoint, opoint, startp;
15396 int update_mode_line;
15397 int tem;
15398 struct it it;
15399 /* Record it now because it's overwritten. */
15400 int current_matrix_up_to_date_p = 0;
15401 int used_current_matrix_p = 0;
15402 /* This is less strict than current_matrix_up_to_date_p.
15403 It indicates that the buffer contents and narrowing are unchanged. */
15404 int buffer_unchanged_p = 0;
15405 int temp_scroll_step = 0;
15406 ptrdiff_t count = SPECPDL_INDEX ();
15407 int rc;
15408 int centering_position = -1;
15409 int last_line_misfit = 0;
15410 ptrdiff_t beg_unchanged, end_unchanged;
15411
15412 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15413 opoint = lpoint;
15414
15415 /* W must be a leaf window here. */
15416 eassert (!NILP (w->buffer));
15417 #ifdef GLYPH_DEBUG
15418 *w->desired_matrix->method = 0;
15419 #endif
15420
15421 restart:
15422 reconsider_clip_changes (w, buffer);
15423
15424 /* Has the mode line to be updated? */
15425 update_mode_line = (w->update_mode_line
15426 || update_mode_lines
15427 || buffer->clip_changed
15428 || buffer->prevent_redisplay_optimizations_p);
15429
15430 if (MINI_WINDOW_P (w))
15431 {
15432 if (w == XWINDOW (echo_area_window)
15433 && !NILP (echo_area_buffer[0]))
15434 {
15435 if (update_mode_line)
15436 /* We may have to update a tty frame's menu bar or a
15437 tool-bar. Example `M-x C-h C-h C-g'. */
15438 goto finish_menu_bars;
15439 else
15440 /* We've already displayed the echo area glyphs in this window. */
15441 goto finish_scroll_bars;
15442 }
15443 else if ((w != XWINDOW (minibuf_window)
15444 || minibuf_level == 0)
15445 /* When buffer is nonempty, redisplay window normally. */
15446 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15447 /* Quail displays non-mini buffers in minibuffer window.
15448 In that case, redisplay the window normally. */
15449 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15450 {
15451 /* W is a mini-buffer window, but it's not active, so clear
15452 it. */
15453 int yb = window_text_bottom_y (w);
15454 struct glyph_row *row;
15455 int y;
15456
15457 for (y = 0, row = w->desired_matrix->rows;
15458 y < yb;
15459 y += row->height, ++row)
15460 blank_row (w, row, y);
15461 goto finish_scroll_bars;
15462 }
15463
15464 clear_glyph_matrix (w->desired_matrix);
15465 }
15466
15467 /* Otherwise set up data on this window; select its buffer and point
15468 value. */
15469 /* Really select the buffer, for the sake of buffer-local
15470 variables. */
15471 set_buffer_internal_1 (XBUFFER (w->buffer));
15472
15473 current_matrix_up_to_date_p
15474 = (!NILP (w->window_end_valid)
15475 && !current_buffer->clip_changed
15476 && !current_buffer->prevent_redisplay_optimizations_p
15477 && !window_outdated (w));
15478
15479 /* Run the window-bottom-change-functions
15480 if it is possible that the text on the screen has changed
15481 (either due to modification of the text, or any other reason). */
15482 if (!current_matrix_up_to_date_p
15483 && !NILP (Vwindow_text_change_functions))
15484 {
15485 safe_run_hooks (Qwindow_text_change_functions);
15486 goto restart;
15487 }
15488
15489 beg_unchanged = BEG_UNCHANGED;
15490 end_unchanged = END_UNCHANGED;
15491
15492 SET_TEXT_POS (opoint, PT, PT_BYTE);
15493
15494 specbind (Qinhibit_point_motion_hooks, Qt);
15495
15496 buffer_unchanged_p
15497 = (!NILP (w->window_end_valid)
15498 && !current_buffer->clip_changed
15499 && !window_outdated (w));
15500
15501 /* When windows_or_buffers_changed is non-zero, we can't rely on
15502 the window end being valid, so set it to nil there. */
15503 if (windows_or_buffers_changed)
15504 {
15505 /* If window starts on a continuation line, maybe adjust the
15506 window start in case the window's width changed. */
15507 if (XMARKER (w->start)->buffer == current_buffer)
15508 compute_window_start_on_continuation_line (w);
15509
15510 wset_window_end_valid (w, Qnil);
15511 }
15512
15513 /* Some sanity checks. */
15514 CHECK_WINDOW_END (w);
15515 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15516 emacs_abort ();
15517 if (BYTEPOS (opoint) < CHARPOS (opoint))
15518 emacs_abort ();
15519
15520 if (mode_line_update_needed (w))
15521 update_mode_line = 1;
15522
15523 /* Point refers normally to the selected window. For any other
15524 window, set up appropriate value. */
15525 if (!EQ (window, selected_window))
15526 {
15527 ptrdiff_t new_pt = marker_position (w->pointm);
15528 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15529 if (new_pt < BEGV)
15530 {
15531 new_pt = BEGV;
15532 new_pt_byte = BEGV_BYTE;
15533 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15534 }
15535 else if (new_pt > (ZV - 1))
15536 {
15537 new_pt = ZV;
15538 new_pt_byte = ZV_BYTE;
15539 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15540 }
15541
15542 /* We don't use SET_PT so that the point-motion hooks don't run. */
15543 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15544 }
15545
15546 /* If any of the character widths specified in the display table
15547 have changed, invalidate the width run cache. It's true that
15548 this may be a bit late to catch such changes, but the rest of
15549 redisplay goes (non-fatally) haywire when the display table is
15550 changed, so why should we worry about doing any better? */
15551 if (current_buffer->width_run_cache)
15552 {
15553 struct Lisp_Char_Table *disptab = buffer_display_table ();
15554
15555 if (! disptab_matches_widthtab
15556 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15557 {
15558 invalidate_region_cache (current_buffer,
15559 current_buffer->width_run_cache,
15560 BEG, Z);
15561 recompute_width_table (current_buffer, disptab);
15562 }
15563 }
15564
15565 /* If window-start is screwed up, choose a new one. */
15566 if (XMARKER (w->start)->buffer != current_buffer)
15567 goto recenter;
15568
15569 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15570
15571 /* If someone specified a new starting point but did not insist,
15572 check whether it can be used. */
15573 if (w->optional_new_start
15574 && CHARPOS (startp) >= BEGV
15575 && CHARPOS (startp) <= ZV)
15576 {
15577 w->optional_new_start = 0;
15578 start_display (&it, w, startp);
15579 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15580 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15581 if (IT_CHARPOS (it) == PT)
15582 w->force_start = 1;
15583 /* IT may overshoot PT if text at PT is invisible. */
15584 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15585 w->force_start = 1;
15586 }
15587
15588 force_start:
15589
15590 /* Handle case where place to start displaying has been specified,
15591 unless the specified location is outside the accessible range. */
15592 if (w->force_start || w->frozen_window_start_p)
15593 {
15594 /* We set this later on if we have to adjust point. */
15595 int new_vpos = -1;
15596
15597 w->force_start = 0;
15598 w->vscroll = 0;
15599 wset_window_end_valid (w, Qnil);
15600
15601 /* Forget any recorded base line for line number display. */
15602 if (!buffer_unchanged_p)
15603 wset_base_line_number (w, Qnil);
15604
15605 /* Redisplay the mode line. Select the buffer properly for that.
15606 Also, run the hook window-scroll-functions
15607 because we have scrolled. */
15608 /* Note, we do this after clearing force_start because
15609 if there's an error, it is better to forget about force_start
15610 than to get into an infinite loop calling the hook functions
15611 and having them get more errors. */
15612 if (!update_mode_line
15613 || ! NILP (Vwindow_scroll_functions))
15614 {
15615 update_mode_line = 1;
15616 w->update_mode_line = 1;
15617 startp = run_window_scroll_functions (window, startp);
15618 }
15619
15620 w->last_modified = 0;
15621 w->last_overlay_modified = 0;
15622 if (CHARPOS (startp) < BEGV)
15623 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15624 else if (CHARPOS (startp) > ZV)
15625 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15626
15627 /* Redisplay, then check if cursor has been set during the
15628 redisplay. Give up if new fonts were loaded. */
15629 /* We used to issue a CHECK_MARGINS argument to try_window here,
15630 but this causes scrolling to fail when point begins inside
15631 the scroll margin (bug#148) -- cyd */
15632 if (!try_window (window, startp, 0))
15633 {
15634 w->force_start = 1;
15635 clear_glyph_matrix (w->desired_matrix);
15636 goto need_larger_matrices;
15637 }
15638
15639 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15640 {
15641 /* If point does not appear, try to move point so it does
15642 appear. The desired matrix has been built above, so we
15643 can use it here. */
15644 new_vpos = window_box_height (w) / 2;
15645 }
15646
15647 if (!cursor_row_fully_visible_p (w, 0, 0))
15648 {
15649 /* Point does appear, but on a line partly visible at end of window.
15650 Move it back to a fully-visible line. */
15651 new_vpos = window_box_height (w);
15652 }
15653 else if (w->cursor.vpos >=0)
15654 {
15655 /* Some people insist on not letting point enter the scroll
15656 margin, even though this part handles windows that didn't
15657 scroll at all. */
15658 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15659 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15660 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15661
15662 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15663 below, which finds the row to move point to, advances by
15664 the Y coordinate of the _next_ row, see the definition of
15665 MATRIX_ROW_BOTTOM_Y. */
15666 if (w->cursor.vpos < margin + header_line)
15667 new_vpos
15668 = pixel_margin + (header_line
15669 ? CURRENT_HEADER_LINE_HEIGHT (w)
15670 : 0) + FRAME_LINE_HEIGHT (f);
15671 else
15672 {
15673 int window_height = window_box_height (w);
15674
15675 if (header_line)
15676 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15677 if (w->cursor.y >= window_height - pixel_margin)
15678 new_vpos = window_height - pixel_margin;
15679 }
15680 }
15681
15682 /* If we need to move point for either of the above reasons,
15683 now actually do it. */
15684 if (new_vpos >= 0)
15685 {
15686 struct glyph_row *row;
15687
15688 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15689 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15690 ++row;
15691
15692 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15693 MATRIX_ROW_START_BYTEPOS (row));
15694
15695 if (w != XWINDOW (selected_window))
15696 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15697 else if (current_buffer == old)
15698 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15699
15700 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15701
15702 /* If we are highlighting the region, then we just changed
15703 the region, so redisplay to show it. */
15704 if (0 <= markpos_of_region ())
15705 {
15706 clear_glyph_matrix (w->desired_matrix);
15707 if (!try_window (window, startp, 0))
15708 goto need_larger_matrices;
15709 }
15710 }
15711
15712 #ifdef GLYPH_DEBUG
15713 debug_method_add (w, "forced window start");
15714 #endif
15715 goto done;
15716 }
15717
15718 /* Handle case where text has not changed, only point, and it has
15719 not moved off the frame, and we are not retrying after hscroll.
15720 (current_matrix_up_to_date_p is nonzero when retrying.) */
15721 if (current_matrix_up_to_date_p
15722 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15723 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15724 {
15725 switch (rc)
15726 {
15727 case CURSOR_MOVEMENT_SUCCESS:
15728 used_current_matrix_p = 1;
15729 goto done;
15730
15731 case CURSOR_MOVEMENT_MUST_SCROLL:
15732 goto try_to_scroll;
15733
15734 default:
15735 emacs_abort ();
15736 }
15737 }
15738 /* If current starting point was originally the beginning of a line
15739 but no longer is, find a new starting point. */
15740 else if (w->start_at_line_beg
15741 && !(CHARPOS (startp) <= BEGV
15742 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15743 {
15744 #ifdef GLYPH_DEBUG
15745 debug_method_add (w, "recenter 1");
15746 #endif
15747 goto recenter;
15748 }
15749
15750 /* Try scrolling with try_window_id. Value is > 0 if update has
15751 been done, it is -1 if we know that the same window start will
15752 not work. It is 0 if unsuccessful for some other reason. */
15753 else if ((tem = try_window_id (w)) != 0)
15754 {
15755 #ifdef GLYPH_DEBUG
15756 debug_method_add (w, "try_window_id %d", tem);
15757 #endif
15758
15759 if (fonts_changed_p)
15760 goto need_larger_matrices;
15761 if (tem > 0)
15762 goto done;
15763
15764 /* Otherwise try_window_id has returned -1 which means that we
15765 don't want the alternative below this comment to execute. */
15766 }
15767 else if (CHARPOS (startp) >= BEGV
15768 && CHARPOS (startp) <= ZV
15769 && PT >= CHARPOS (startp)
15770 && (CHARPOS (startp) < ZV
15771 /* Avoid starting at end of buffer. */
15772 || CHARPOS (startp) == BEGV
15773 || !window_outdated (w)))
15774 {
15775 int d1, d2, d3, d4, d5, d6;
15776
15777 /* If first window line is a continuation line, and window start
15778 is inside the modified region, but the first change is before
15779 current window start, we must select a new window start.
15780
15781 However, if this is the result of a down-mouse event (e.g. by
15782 extending the mouse-drag-overlay), we don't want to select a
15783 new window start, since that would change the position under
15784 the mouse, resulting in an unwanted mouse-movement rather
15785 than a simple mouse-click. */
15786 if (!w->start_at_line_beg
15787 && NILP (do_mouse_tracking)
15788 && CHARPOS (startp) > BEGV
15789 && CHARPOS (startp) > BEG + beg_unchanged
15790 && CHARPOS (startp) <= Z - end_unchanged
15791 /* Even if w->start_at_line_beg is nil, a new window may
15792 start at a line_beg, since that's how set_buffer_window
15793 sets it. So, we need to check the return value of
15794 compute_window_start_on_continuation_line. (See also
15795 bug#197). */
15796 && XMARKER (w->start)->buffer == current_buffer
15797 && compute_window_start_on_continuation_line (w)
15798 /* It doesn't make sense to force the window start like we
15799 do at label force_start if it is already known that point
15800 will not be visible in the resulting window, because
15801 doing so will move point from its correct position
15802 instead of scrolling the window to bring point into view.
15803 See bug#9324. */
15804 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15805 {
15806 w->force_start = 1;
15807 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15808 goto force_start;
15809 }
15810
15811 #ifdef GLYPH_DEBUG
15812 debug_method_add (w, "same window start");
15813 #endif
15814
15815 /* Try to redisplay starting at same place as before.
15816 If point has not moved off frame, accept the results. */
15817 if (!current_matrix_up_to_date_p
15818 /* Don't use try_window_reusing_current_matrix in this case
15819 because a window scroll function can have changed the
15820 buffer. */
15821 || !NILP (Vwindow_scroll_functions)
15822 || MINI_WINDOW_P (w)
15823 || !(used_current_matrix_p
15824 = try_window_reusing_current_matrix (w)))
15825 {
15826 IF_DEBUG (debug_method_add (w, "1"));
15827 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15828 /* -1 means we need to scroll.
15829 0 means we need new matrices, but fonts_changed_p
15830 is set in that case, so we will detect it below. */
15831 goto try_to_scroll;
15832 }
15833
15834 if (fonts_changed_p)
15835 goto need_larger_matrices;
15836
15837 if (w->cursor.vpos >= 0)
15838 {
15839 if (!just_this_one_p
15840 || current_buffer->clip_changed
15841 || BEG_UNCHANGED < CHARPOS (startp))
15842 /* Forget any recorded base line for line number display. */
15843 wset_base_line_number (w, Qnil);
15844
15845 if (!cursor_row_fully_visible_p (w, 1, 0))
15846 {
15847 clear_glyph_matrix (w->desired_matrix);
15848 last_line_misfit = 1;
15849 }
15850 /* Drop through and scroll. */
15851 else
15852 goto done;
15853 }
15854 else
15855 clear_glyph_matrix (w->desired_matrix);
15856 }
15857
15858 try_to_scroll:
15859
15860 w->last_modified = 0;
15861 w->last_overlay_modified = 0;
15862
15863 /* Redisplay the mode line. Select the buffer properly for that. */
15864 if (!update_mode_line)
15865 {
15866 update_mode_line = 1;
15867 w->update_mode_line = 1;
15868 }
15869
15870 /* Try to scroll by specified few lines. */
15871 if ((scroll_conservatively
15872 || emacs_scroll_step
15873 || temp_scroll_step
15874 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15875 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15876 && CHARPOS (startp) >= BEGV
15877 && CHARPOS (startp) <= ZV)
15878 {
15879 /* The function returns -1 if new fonts were loaded, 1 if
15880 successful, 0 if not successful. */
15881 int ss = try_scrolling (window, just_this_one_p,
15882 scroll_conservatively,
15883 emacs_scroll_step,
15884 temp_scroll_step, last_line_misfit);
15885 switch (ss)
15886 {
15887 case SCROLLING_SUCCESS:
15888 goto done;
15889
15890 case SCROLLING_NEED_LARGER_MATRICES:
15891 goto need_larger_matrices;
15892
15893 case SCROLLING_FAILED:
15894 break;
15895
15896 default:
15897 emacs_abort ();
15898 }
15899 }
15900
15901 /* Finally, just choose a place to start which positions point
15902 according to user preferences. */
15903
15904 recenter:
15905
15906 #ifdef GLYPH_DEBUG
15907 debug_method_add (w, "recenter");
15908 #endif
15909
15910 /* w->vscroll = 0; */
15911
15912 /* Forget any previously recorded base line for line number display. */
15913 if (!buffer_unchanged_p)
15914 wset_base_line_number (w, Qnil);
15915
15916 /* Determine the window start relative to point. */
15917 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15918 it.current_y = it.last_visible_y;
15919 if (centering_position < 0)
15920 {
15921 int margin =
15922 scroll_margin > 0
15923 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15924 : 0;
15925 ptrdiff_t margin_pos = CHARPOS (startp);
15926 Lisp_Object aggressive;
15927 int scrolling_up;
15928
15929 /* If there is a scroll margin at the top of the window, find
15930 its character position. */
15931 if (margin
15932 /* Cannot call start_display if startp is not in the
15933 accessible region of the buffer. This can happen when we
15934 have just switched to a different buffer and/or changed
15935 its restriction. In that case, startp is initialized to
15936 the character position 1 (BEGV) because we did not yet
15937 have chance to display the buffer even once. */
15938 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15939 {
15940 struct it it1;
15941 void *it1data = NULL;
15942
15943 SAVE_IT (it1, it, it1data);
15944 start_display (&it1, w, startp);
15945 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15946 margin_pos = IT_CHARPOS (it1);
15947 RESTORE_IT (&it, &it, it1data);
15948 }
15949 scrolling_up = PT > margin_pos;
15950 aggressive =
15951 scrolling_up
15952 ? BVAR (current_buffer, scroll_up_aggressively)
15953 : BVAR (current_buffer, scroll_down_aggressively);
15954
15955 if (!MINI_WINDOW_P (w)
15956 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15957 {
15958 int pt_offset = 0;
15959
15960 /* Setting scroll-conservatively overrides
15961 scroll-*-aggressively. */
15962 if (!scroll_conservatively && NUMBERP (aggressive))
15963 {
15964 double float_amount = XFLOATINT (aggressive);
15965
15966 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15967 if (pt_offset == 0 && float_amount > 0)
15968 pt_offset = 1;
15969 if (pt_offset && margin > 0)
15970 margin -= 1;
15971 }
15972 /* Compute how much to move the window start backward from
15973 point so that point will be displayed where the user
15974 wants it. */
15975 if (scrolling_up)
15976 {
15977 centering_position = it.last_visible_y;
15978 if (pt_offset)
15979 centering_position -= pt_offset;
15980 centering_position -=
15981 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15982 + WINDOW_HEADER_LINE_HEIGHT (w);
15983 /* Don't let point enter the scroll margin near top of
15984 the window. */
15985 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15986 centering_position = margin * FRAME_LINE_HEIGHT (f);
15987 }
15988 else
15989 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15990 }
15991 else
15992 /* Set the window start half the height of the window backward
15993 from point. */
15994 centering_position = window_box_height (w) / 2;
15995 }
15996 move_it_vertically_backward (&it, centering_position);
15997
15998 eassert (IT_CHARPOS (it) >= BEGV);
15999
16000 /* The function move_it_vertically_backward may move over more
16001 than the specified y-distance. If it->w is small, e.g. a
16002 mini-buffer window, we may end up in front of the window's
16003 display area. Start displaying at the start of the line
16004 containing PT in this case. */
16005 if (it.current_y <= 0)
16006 {
16007 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16008 move_it_vertically_backward (&it, 0);
16009 it.current_y = 0;
16010 }
16011
16012 it.current_x = it.hpos = 0;
16013
16014 /* Set the window start position here explicitly, to avoid an
16015 infinite loop in case the functions in window-scroll-functions
16016 get errors. */
16017 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16018
16019 /* Run scroll hooks. */
16020 startp = run_window_scroll_functions (window, it.current.pos);
16021
16022 /* Redisplay the window. */
16023 if (!current_matrix_up_to_date_p
16024 || windows_or_buffers_changed
16025 || cursor_type_changed
16026 /* Don't use try_window_reusing_current_matrix in this case
16027 because it can have changed the buffer. */
16028 || !NILP (Vwindow_scroll_functions)
16029 || !just_this_one_p
16030 || MINI_WINDOW_P (w)
16031 || !(used_current_matrix_p
16032 = try_window_reusing_current_matrix (w)))
16033 try_window (window, startp, 0);
16034
16035 /* If new fonts have been loaded (due to fontsets), give up. We
16036 have to start a new redisplay since we need to re-adjust glyph
16037 matrices. */
16038 if (fonts_changed_p)
16039 goto need_larger_matrices;
16040
16041 /* If cursor did not appear assume that the middle of the window is
16042 in the first line of the window. Do it again with the next line.
16043 (Imagine a window of height 100, displaying two lines of height
16044 60. Moving back 50 from it->last_visible_y will end in the first
16045 line.) */
16046 if (w->cursor.vpos < 0)
16047 {
16048 if (!NILP (w->window_end_valid)
16049 && PT >= Z - XFASTINT (w->window_end_pos))
16050 {
16051 clear_glyph_matrix (w->desired_matrix);
16052 move_it_by_lines (&it, 1);
16053 try_window (window, it.current.pos, 0);
16054 }
16055 else if (PT < IT_CHARPOS (it))
16056 {
16057 clear_glyph_matrix (w->desired_matrix);
16058 move_it_by_lines (&it, -1);
16059 try_window (window, it.current.pos, 0);
16060 }
16061 else
16062 {
16063 /* Not much we can do about it. */
16064 }
16065 }
16066
16067 /* Consider the following case: Window starts at BEGV, there is
16068 invisible, intangible text at BEGV, so that display starts at
16069 some point START > BEGV. It can happen that we are called with
16070 PT somewhere between BEGV and START. Try to handle that case. */
16071 if (w->cursor.vpos < 0)
16072 {
16073 struct glyph_row *row = w->current_matrix->rows;
16074 if (row->mode_line_p)
16075 ++row;
16076 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16077 }
16078
16079 if (!cursor_row_fully_visible_p (w, 0, 0))
16080 {
16081 /* If vscroll is enabled, disable it and try again. */
16082 if (w->vscroll)
16083 {
16084 w->vscroll = 0;
16085 clear_glyph_matrix (w->desired_matrix);
16086 goto recenter;
16087 }
16088
16089 /* Users who set scroll-conservatively to a large number want
16090 point just above/below the scroll margin. If we ended up
16091 with point's row partially visible, move the window start to
16092 make that row fully visible and out of the margin. */
16093 if (scroll_conservatively > SCROLL_LIMIT)
16094 {
16095 int margin =
16096 scroll_margin > 0
16097 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16098 : 0;
16099 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16100
16101 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16102 clear_glyph_matrix (w->desired_matrix);
16103 if (1 == try_window (window, it.current.pos,
16104 TRY_WINDOW_CHECK_MARGINS))
16105 goto done;
16106 }
16107
16108 /* If centering point failed to make the whole line visible,
16109 put point at the top instead. That has to make the whole line
16110 visible, if it can be done. */
16111 if (centering_position == 0)
16112 goto done;
16113
16114 clear_glyph_matrix (w->desired_matrix);
16115 centering_position = 0;
16116 goto recenter;
16117 }
16118
16119 done:
16120
16121 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16122 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16123 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16124
16125 /* Display the mode line, if we must. */
16126 if ((update_mode_line
16127 /* If window not full width, must redo its mode line
16128 if (a) the window to its side is being redone and
16129 (b) we do a frame-based redisplay. This is a consequence
16130 of how inverted lines are drawn in frame-based redisplay. */
16131 || (!just_this_one_p
16132 && !FRAME_WINDOW_P (f)
16133 && !WINDOW_FULL_WIDTH_P (w))
16134 /* Line number to display. */
16135 || INTEGERP (w->base_line_pos)
16136 /* Column number is displayed and different from the one displayed. */
16137 || (!NILP (w->column_number_displayed)
16138 && (XFASTINT (w->column_number_displayed) != current_column ())))
16139 /* This means that the window has a mode line. */
16140 && (WINDOW_WANTS_MODELINE_P (w)
16141 || WINDOW_WANTS_HEADER_LINE_P (w)))
16142 {
16143 display_mode_lines (w);
16144
16145 /* If mode line height has changed, arrange for a thorough
16146 immediate redisplay using the correct mode line height. */
16147 if (WINDOW_WANTS_MODELINE_P (w)
16148 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16149 {
16150 fonts_changed_p = 1;
16151 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16152 = DESIRED_MODE_LINE_HEIGHT (w);
16153 }
16154
16155 /* If header line height has changed, arrange for a thorough
16156 immediate redisplay using the correct header line height. */
16157 if (WINDOW_WANTS_HEADER_LINE_P (w)
16158 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16159 {
16160 fonts_changed_p = 1;
16161 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16162 = DESIRED_HEADER_LINE_HEIGHT (w);
16163 }
16164
16165 if (fonts_changed_p)
16166 goto need_larger_matrices;
16167 }
16168
16169 if (!line_number_displayed
16170 && !BUFFERP (w->base_line_pos))
16171 {
16172 wset_base_line_pos (w, Qnil);
16173 wset_base_line_number (w, Qnil);
16174 }
16175
16176 finish_menu_bars:
16177
16178 /* When we reach a frame's selected window, redo the frame's menu bar. */
16179 if (update_mode_line
16180 && EQ (FRAME_SELECTED_WINDOW (f), window))
16181 {
16182 int redisplay_menu_p = 0;
16183
16184 if (FRAME_WINDOW_P (f))
16185 {
16186 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16187 || defined (HAVE_NS) || defined (USE_GTK)
16188 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16189 #else
16190 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16191 #endif
16192 }
16193 else
16194 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16195
16196 if (redisplay_menu_p)
16197 display_menu_bar (w);
16198
16199 #ifdef HAVE_WINDOW_SYSTEM
16200 if (FRAME_WINDOW_P (f))
16201 {
16202 #if defined (USE_GTK) || defined (HAVE_NS)
16203 if (FRAME_EXTERNAL_TOOL_BAR (f))
16204 redisplay_tool_bar (f);
16205 #else
16206 if (WINDOWP (f->tool_bar_window)
16207 && (FRAME_TOOL_BAR_LINES (f) > 0
16208 || !NILP (Vauto_resize_tool_bars))
16209 && redisplay_tool_bar (f))
16210 ignore_mouse_drag_p = 1;
16211 #endif
16212 }
16213 #endif
16214 }
16215
16216 #ifdef HAVE_WINDOW_SYSTEM
16217 if (FRAME_WINDOW_P (f)
16218 && update_window_fringes (w, (just_this_one_p
16219 || (!used_current_matrix_p && !overlay_arrow_seen)
16220 || w->pseudo_window_p)))
16221 {
16222 update_begin (f);
16223 block_input ();
16224 if (draw_window_fringes (w, 1))
16225 x_draw_vertical_border (w);
16226 unblock_input ();
16227 update_end (f);
16228 }
16229 #endif /* HAVE_WINDOW_SYSTEM */
16230
16231 /* We go to this label, with fonts_changed_p set,
16232 if it is necessary to try again using larger glyph matrices.
16233 We have to redeem the scroll bar even in this case,
16234 because the loop in redisplay_internal expects that. */
16235 need_larger_matrices:
16236 ;
16237 finish_scroll_bars:
16238
16239 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16240 {
16241 /* Set the thumb's position and size. */
16242 set_vertical_scroll_bar (w);
16243
16244 /* Note that we actually used the scroll bar attached to this
16245 window, so it shouldn't be deleted at the end of redisplay. */
16246 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16247 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16248 }
16249
16250 /* Restore current_buffer and value of point in it. The window
16251 update may have changed the buffer, so first make sure `opoint'
16252 is still valid (Bug#6177). */
16253 if (CHARPOS (opoint) < BEGV)
16254 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16255 else if (CHARPOS (opoint) > ZV)
16256 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16257 else
16258 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16259
16260 set_buffer_internal_1 (old);
16261 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16262 shorter. This can be caused by log truncation in *Messages*. */
16263 if (CHARPOS (lpoint) <= ZV)
16264 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16265
16266 unbind_to (count, Qnil);
16267 }
16268
16269
16270 /* Build the complete desired matrix of WINDOW with a window start
16271 buffer position POS.
16272
16273 Value is 1 if successful. It is zero if fonts were loaded during
16274 redisplay which makes re-adjusting glyph matrices necessary, and -1
16275 if point would appear in the scroll margins.
16276 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16277 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16278 set in FLAGS.) */
16279
16280 int
16281 try_window (Lisp_Object window, struct text_pos pos, int flags)
16282 {
16283 struct window *w = XWINDOW (window);
16284 struct it it;
16285 struct glyph_row *last_text_row = NULL;
16286 struct frame *f = XFRAME (w->frame);
16287
16288 /* Make POS the new window start. */
16289 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16290
16291 /* Mark cursor position as unknown. No overlay arrow seen. */
16292 w->cursor.vpos = -1;
16293 overlay_arrow_seen = 0;
16294
16295 /* Initialize iterator and info to start at POS. */
16296 start_display (&it, w, pos);
16297
16298 /* Display all lines of W. */
16299 while (it.current_y < it.last_visible_y)
16300 {
16301 if (display_line (&it))
16302 last_text_row = it.glyph_row - 1;
16303 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16304 return 0;
16305 }
16306
16307 /* Don't let the cursor end in the scroll margins. */
16308 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16309 && !MINI_WINDOW_P (w))
16310 {
16311 int this_scroll_margin;
16312
16313 if (scroll_margin > 0)
16314 {
16315 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16316 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16317 }
16318 else
16319 this_scroll_margin = 0;
16320
16321 if ((w->cursor.y >= 0 /* not vscrolled */
16322 && w->cursor.y < this_scroll_margin
16323 && CHARPOS (pos) > BEGV
16324 && IT_CHARPOS (it) < ZV)
16325 /* rms: considering make_cursor_line_fully_visible_p here
16326 seems to give wrong results. We don't want to recenter
16327 when the last line is partly visible, we want to allow
16328 that case to be handled in the usual way. */
16329 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16330 {
16331 w->cursor.vpos = -1;
16332 clear_glyph_matrix (w->desired_matrix);
16333 return -1;
16334 }
16335 }
16336
16337 /* If bottom moved off end of frame, change mode line percentage. */
16338 if (XFASTINT (w->window_end_pos) <= 0
16339 && Z != IT_CHARPOS (it))
16340 w->update_mode_line = 1;
16341
16342 /* Set window_end_pos to the offset of the last character displayed
16343 on the window from the end of current_buffer. Set
16344 window_end_vpos to its row number. */
16345 if (last_text_row)
16346 {
16347 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16348 w->window_end_bytepos
16349 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16350 wset_window_end_pos
16351 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16352 wset_window_end_vpos
16353 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16354 eassert
16355 (MATRIX_ROW (w->desired_matrix,
16356 XFASTINT (w->window_end_vpos))->displays_text_p);
16357 }
16358 else
16359 {
16360 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16361 wset_window_end_pos (w, make_number (Z - ZV));
16362 wset_window_end_vpos (w, make_number (0));
16363 }
16364
16365 /* But that is not valid info until redisplay finishes. */
16366 wset_window_end_valid (w, Qnil);
16367 return 1;
16368 }
16369
16370
16371 \f
16372 /************************************************************************
16373 Window redisplay reusing current matrix when buffer has not changed
16374 ************************************************************************/
16375
16376 /* Try redisplay of window W showing an unchanged buffer with a
16377 different window start than the last time it was displayed by
16378 reusing its current matrix. Value is non-zero if successful.
16379 W->start is the new window start. */
16380
16381 static int
16382 try_window_reusing_current_matrix (struct window *w)
16383 {
16384 struct frame *f = XFRAME (w->frame);
16385 struct glyph_row *bottom_row;
16386 struct it it;
16387 struct run run;
16388 struct text_pos start, new_start;
16389 int nrows_scrolled, i;
16390 struct glyph_row *last_text_row;
16391 struct glyph_row *last_reused_text_row;
16392 struct glyph_row *start_row;
16393 int start_vpos, min_y, max_y;
16394
16395 #ifdef GLYPH_DEBUG
16396 if (inhibit_try_window_reusing)
16397 return 0;
16398 #endif
16399
16400 if (/* This function doesn't handle terminal frames. */
16401 !FRAME_WINDOW_P (f)
16402 /* Don't try to reuse the display if windows have been split
16403 or such. */
16404 || windows_or_buffers_changed
16405 || cursor_type_changed)
16406 return 0;
16407
16408 /* Can't do this if region may have changed. */
16409 if (0 <= markpos_of_region ()
16410 || !NILP (w->region_showing)
16411 || !NILP (Vshow_trailing_whitespace))
16412 return 0;
16413
16414 /* If top-line visibility has changed, give up. */
16415 if (WINDOW_WANTS_HEADER_LINE_P (w)
16416 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16417 return 0;
16418
16419 /* Give up if old or new display is scrolled vertically. We could
16420 make this function handle this, but right now it doesn't. */
16421 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16422 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16423 return 0;
16424
16425 /* The variable new_start now holds the new window start. The old
16426 start `start' can be determined from the current matrix. */
16427 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16428 start = start_row->minpos;
16429 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16430
16431 /* Clear the desired matrix for the display below. */
16432 clear_glyph_matrix (w->desired_matrix);
16433
16434 if (CHARPOS (new_start) <= CHARPOS (start))
16435 {
16436 /* Don't use this method if the display starts with an ellipsis
16437 displayed for invisible text. It's not easy to handle that case
16438 below, and it's certainly not worth the effort since this is
16439 not a frequent case. */
16440 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16441 return 0;
16442
16443 IF_DEBUG (debug_method_add (w, "twu1"));
16444
16445 /* Display up to a row that can be reused. The variable
16446 last_text_row is set to the last row displayed that displays
16447 text. Note that it.vpos == 0 if or if not there is a
16448 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16449 start_display (&it, w, new_start);
16450 w->cursor.vpos = -1;
16451 last_text_row = last_reused_text_row = NULL;
16452
16453 while (it.current_y < it.last_visible_y
16454 && !fonts_changed_p)
16455 {
16456 /* If we have reached into the characters in the START row,
16457 that means the line boundaries have changed. So we
16458 can't start copying with the row START. Maybe it will
16459 work to start copying with the following row. */
16460 while (IT_CHARPOS (it) > CHARPOS (start))
16461 {
16462 /* Advance to the next row as the "start". */
16463 start_row++;
16464 start = start_row->minpos;
16465 /* If there are no more rows to try, or just one, give up. */
16466 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16467 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16468 || CHARPOS (start) == ZV)
16469 {
16470 clear_glyph_matrix (w->desired_matrix);
16471 return 0;
16472 }
16473
16474 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16475 }
16476 /* If we have reached alignment, we can copy the rest of the
16477 rows. */
16478 if (IT_CHARPOS (it) == CHARPOS (start)
16479 /* Don't accept "alignment" inside a display vector,
16480 since start_row could have started in the middle of
16481 that same display vector (thus their character
16482 positions match), and we have no way of telling if
16483 that is the case. */
16484 && it.current.dpvec_index < 0)
16485 break;
16486
16487 if (display_line (&it))
16488 last_text_row = it.glyph_row - 1;
16489
16490 }
16491
16492 /* A value of current_y < last_visible_y means that we stopped
16493 at the previous window start, which in turn means that we
16494 have at least one reusable row. */
16495 if (it.current_y < it.last_visible_y)
16496 {
16497 struct glyph_row *row;
16498
16499 /* IT.vpos always starts from 0; it counts text lines. */
16500 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16501
16502 /* Find PT if not already found in the lines displayed. */
16503 if (w->cursor.vpos < 0)
16504 {
16505 int dy = it.current_y - start_row->y;
16506
16507 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16508 row = row_containing_pos (w, PT, row, NULL, dy);
16509 if (row)
16510 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16511 dy, nrows_scrolled);
16512 else
16513 {
16514 clear_glyph_matrix (w->desired_matrix);
16515 return 0;
16516 }
16517 }
16518
16519 /* Scroll the display. Do it before the current matrix is
16520 changed. The problem here is that update has not yet
16521 run, i.e. part of the current matrix is not up to date.
16522 scroll_run_hook will clear the cursor, and use the
16523 current matrix to get the height of the row the cursor is
16524 in. */
16525 run.current_y = start_row->y;
16526 run.desired_y = it.current_y;
16527 run.height = it.last_visible_y - it.current_y;
16528
16529 if (run.height > 0 && run.current_y != run.desired_y)
16530 {
16531 update_begin (f);
16532 FRAME_RIF (f)->update_window_begin_hook (w);
16533 FRAME_RIF (f)->clear_window_mouse_face (w);
16534 FRAME_RIF (f)->scroll_run_hook (w, &run);
16535 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16536 update_end (f);
16537 }
16538
16539 /* Shift current matrix down by nrows_scrolled lines. */
16540 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16541 rotate_matrix (w->current_matrix,
16542 start_vpos,
16543 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16544 nrows_scrolled);
16545
16546 /* Disable lines that must be updated. */
16547 for (i = 0; i < nrows_scrolled; ++i)
16548 (start_row + i)->enabled_p = 0;
16549
16550 /* Re-compute Y positions. */
16551 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16552 max_y = it.last_visible_y;
16553 for (row = start_row + nrows_scrolled;
16554 row < bottom_row;
16555 ++row)
16556 {
16557 row->y = it.current_y;
16558 row->visible_height = row->height;
16559
16560 if (row->y < min_y)
16561 row->visible_height -= min_y - row->y;
16562 if (row->y + row->height > max_y)
16563 row->visible_height -= row->y + row->height - max_y;
16564 if (row->fringe_bitmap_periodic_p)
16565 row->redraw_fringe_bitmaps_p = 1;
16566
16567 it.current_y += row->height;
16568
16569 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16570 last_reused_text_row = row;
16571 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16572 break;
16573 }
16574
16575 /* Disable lines in the current matrix which are now
16576 below the window. */
16577 for (++row; row < bottom_row; ++row)
16578 row->enabled_p = row->mode_line_p = 0;
16579 }
16580
16581 /* Update window_end_pos etc.; last_reused_text_row is the last
16582 reused row from the current matrix containing text, if any.
16583 The value of last_text_row is the last displayed line
16584 containing text. */
16585 if (last_reused_text_row)
16586 {
16587 w->window_end_bytepos
16588 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16589 wset_window_end_pos
16590 (w, make_number (Z
16591 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16592 wset_window_end_vpos
16593 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16594 w->current_matrix)));
16595 }
16596 else if (last_text_row)
16597 {
16598 w->window_end_bytepos
16599 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16600 wset_window_end_pos
16601 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16602 wset_window_end_vpos
16603 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16604 w->desired_matrix)));
16605 }
16606 else
16607 {
16608 /* This window must be completely empty. */
16609 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16610 wset_window_end_pos (w, make_number (Z - ZV));
16611 wset_window_end_vpos (w, make_number (0));
16612 }
16613 wset_window_end_valid (w, Qnil);
16614
16615 /* Update hint: don't try scrolling again in update_window. */
16616 w->desired_matrix->no_scrolling_p = 1;
16617
16618 #ifdef GLYPH_DEBUG
16619 debug_method_add (w, "try_window_reusing_current_matrix 1");
16620 #endif
16621 return 1;
16622 }
16623 else if (CHARPOS (new_start) > CHARPOS (start))
16624 {
16625 struct glyph_row *pt_row, *row;
16626 struct glyph_row *first_reusable_row;
16627 struct glyph_row *first_row_to_display;
16628 int dy;
16629 int yb = window_text_bottom_y (w);
16630
16631 /* Find the row starting at new_start, if there is one. Don't
16632 reuse a partially visible line at the end. */
16633 first_reusable_row = start_row;
16634 while (first_reusable_row->enabled_p
16635 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16636 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16637 < CHARPOS (new_start)))
16638 ++first_reusable_row;
16639
16640 /* Give up if there is no row to reuse. */
16641 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16642 || !first_reusable_row->enabled_p
16643 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16644 != CHARPOS (new_start)))
16645 return 0;
16646
16647 /* We can reuse fully visible rows beginning with
16648 first_reusable_row to the end of the window. Set
16649 first_row_to_display to the first row that cannot be reused.
16650 Set pt_row to the row containing point, if there is any. */
16651 pt_row = NULL;
16652 for (first_row_to_display = first_reusable_row;
16653 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16654 ++first_row_to_display)
16655 {
16656 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16657 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16658 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16659 && first_row_to_display->ends_at_zv_p
16660 && pt_row == NULL)))
16661 pt_row = first_row_to_display;
16662 }
16663
16664 /* Start displaying at the start of first_row_to_display. */
16665 eassert (first_row_to_display->y < yb);
16666 init_to_row_start (&it, w, first_row_to_display);
16667
16668 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16669 - start_vpos);
16670 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16671 - nrows_scrolled);
16672 it.current_y = (first_row_to_display->y - first_reusable_row->y
16673 + WINDOW_HEADER_LINE_HEIGHT (w));
16674
16675 /* Display lines beginning with first_row_to_display in the
16676 desired matrix. Set last_text_row to the last row displayed
16677 that displays text. */
16678 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16679 if (pt_row == NULL)
16680 w->cursor.vpos = -1;
16681 last_text_row = NULL;
16682 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16683 if (display_line (&it))
16684 last_text_row = it.glyph_row - 1;
16685
16686 /* If point is in a reused row, adjust y and vpos of the cursor
16687 position. */
16688 if (pt_row)
16689 {
16690 w->cursor.vpos -= nrows_scrolled;
16691 w->cursor.y -= first_reusable_row->y - start_row->y;
16692 }
16693
16694 /* Give up if point isn't in a row displayed or reused. (This
16695 also handles the case where w->cursor.vpos < nrows_scrolled
16696 after the calls to display_line, which can happen with scroll
16697 margins. See bug#1295.) */
16698 if (w->cursor.vpos < 0)
16699 {
16700 clear_glyph_matrix (w->desired_matrix);
16701 return 0;
16702 }
16703
16704 /* Scroll the display. */
16705 run.current_y = first_reusable_row->y;
16706 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16707 run.height = it.last_visible_y - run.current_y;
16708 dy = run.current_y - run.desired_y;
16709
16710 if (run.height)
16711 {
16712 update_begin (f);
16713 FRAME_RIF (f)->update_window_begin_hook (w);
16714 FRAME_RIF (f)->clear_window_mouse_face (w);
16715 FRAME_RIF (f)->scroll_run_hook (w, &run);
16716 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16717 update_end (f);
16718 }
16719
16720 /* Adjust Y positions of reused rows. */
16721 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16722 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16723 max_y = it.last_visible_y;
16724 for (row = first_reusable_row; row < first_row_to_display; ++row)
16725 {
16726 row->y -= dy;
16727 row->visible_height = row->height;
16728 if (row->y < min_y)
16729 row->visible_height -= min_y - row->y;
16730 if (row->y + row->height > max_y)
16731 row->visible_height -= row->y + row->height - max_y;
16732 if (row->fringe_bitmap_periodic_p)
16733 row->redraw_fringe_bitmaps_p = 1;
16734 }
16735
16736 /* Scroll the current matrix. */
16737 eassert (nrows_scrolled > 0);
16738 rotate_matrix (w->current_matrix,
16739 start_vpos,
16740 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16741 -nrows_scrolled);
16742
16743 /* Disable rows not reused. */
16744 for (row -= nrows_scrolled; row < bottom_row; ++row)
16745 row->enabled_p = 0;
16746
16747 /* Point may have moved to a different line, so we cannot assume that
16748 the previous cursor position is valid; locate the correct row. */
16749 if (pt_row)
16750 {
16751 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16752 row < bottom_row
16753 && PT >= MATRIX_ROW_END_CHARPOS (row)
16754 && !row->ends_at_zv_p;
16755 row++)
16756 {
16757 w->cursor.vpos++;
16758 w->cursor.y = row->y;
16759 }
16760 if (row < bottom_row)
16761 {
16762 /* Can't simply scan the row for point with
16763 bidi-reordered glyph rows. Let set_cursor_from_row
16764 figure out where to put the cursor, and if it fails,
16765 give up. */
16766 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16767 {
16768 if (!set_cursor_from_row (w, row, w->current_matrix,
16769 0, 0, 0, 0))
16770 {
16771 clear_glyph_matrix (w->desired_matrix);
16772 return 0;
16773 }
16774 }
16775 else
16776 {
16777 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16778 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16779
16780 for (; glyph < end
16781 && (!BUFFERP (glyph->object)
16782 || glyph->charpos < PT);
16783 glyph++)
16784 {
16785 w->cursor.hpos++;
16786 w->cursor.x += glyph->pixel_width;
16787 }
16788 }
16789 }
16790 }
16791
16792 /* Adjust window end. A null value of last_text_row means that
16793 the window end is in reused rows which in turn means that
16794 only its vpos can have changed. */
16795 if (last_text_row)
16796 {
16797 w->window_end_bytepos
16798 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16799 wset_window_end_pos
16800 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16801 wset_window_end_vpos
16802 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16803 w->desired_matrix)));
16804 }
16805 else
16806 {
16807 wset_window_end_vpos
16808 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16809 }
16810
16811 wset_window_end_valid (w, Qnil);
16812 w->desired_matrix->no_scrolling_p = 1;
16813
16814 #ifdef GLYPH_DEBUG
16815 debug_method_add (w, "try_window_reusing_current_matrix 2");
16816 #endif
16817 return 1;
16818 }
16819
16820 return 0;
16821 }
16822
16823
16824 \f
16825 /************************************************************************
16826 Window redisplay reusing current matrix when buffer has changed
16827 ************************************************************************/
16828
16829 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16830 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16831 ptrdiff_t *, ptrdiff_t *);
16832 static struct glyph_row *
16833 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16834 struct glyph_row *);
16835
16836
16837 /* Return the last row in MATRIX displaying text. If row START is
16838 non-null, start searching with that row. IT gives the dimensions
16839 of the display. Value is null if matrix is empty; otherwise it is
16840 a pointer to the row found. */
16841
16842 static struct glyph_row *
16843 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16844 struct glyph_row *start)
16845 {
16846 struct glyph_row *row, *row_found;
16847
16848 /* Set row_found to the last row in IT->w's current matrix
16849 displaying text. The loop looks funny but think of partially
16850 visible lines. */
16851 row_found = NULL;
16852 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16853 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16854 {
16855 eassert (row->enabled_p);
16856 row_found = row;
16857 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16858 break;
16859 ++row;
16860 }
16861
16862 return row_found;
16863 }
16864
16865
16866 /* Return the last row in the current matrix of W that is not affected
16867 by changes at the start of current_buffer that occurred since W's
16868 current matrix was built. Value is null if no such row exists.
16869
16870 BEG_UNCHANGED us the number of characters unchanged at the start of
16871 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16872 first changed character in current_buffer. Characters at positions <
16873 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16874 when the current matrix was built. */
16875
16876 static struct glyph_row *
16877 find_last_unchanged_at_beg_row (struct window *w)
16878 {
16879 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16880 struct glyph_row *row;
16881 struct glyph_row *row_found = NULL;
16882 int yb = window_text_bottom_y (w);
16883
16884 /* Find the last row displaying unchanged text. */
16885 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16886 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16887 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16888 ++row)
16889 {
16890 if (/* If row ends before first_changed_pos, it is unchanged,
16891 except in some case. */
16892 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16893 /* When row ends in ZV and we write at ZV it is not
16894 unchanged. */
16895 && !row->ends_at_zv_p
16896 /* When first_changed_pos is the end of a continued line,
16897 row is not unchanged because it may be no longer
16898 continued. */
16899 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16900 && (row->continued_p
16901 || row->exact_window_width_line_p))
16902 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16903 needs to be recomputed, so don't consider this row as
16904 unchanged. This happens when the last line was
16905 bidi-reordered and was killed immediately before this
16906 redisplay cycle. In that case, ROW->end stores the
16907 buffer position of the first visual-order character of
16908 the killed text, which is now beyond ZV. */
16909 && CHARPOS (row->end.pos) <= ZV)
16910 row_found = row;
16911
16912 /* Stop if last visible row. */
16913 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16914 break;
16915 }
16916
16917 return row_found;
16918 }
16919
16920
16921 /* Find the first glyph row in the current matrix of W that is not
16922 affected by changes at the end of current_buffer since the
16923 time W's current matrix was built.
16924
16925 Return in *DELTA the number of chars by which buffer positions in
16926 unchanged text at the end of current_buffer must be adjusted.
16927
16928 Return in *DELTA_BYTES the corresponding number of bytes.
16929
16930 Value is null if no such row exists, i.e. all rows are affected by
16931 changes. */
16932
16933 static struct glyph_row *
16934 find_first_unchanged_at_end_row (struct window *w,
16935 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16936 {
16937 struct glyph_row *row;
16938 struct glyph_row *row_found = NULL;
16939
16940 *delta = *delta_bytes = 0;
16941
16942 /* Display must not have been paused, otherwise the current matrix
16943 is not up to date. */
16944 eassert (!NILP (w->window_end_valid));
16945
16946 /* A value of window_end_pos >= END_UNCHANGED means that the window
16947 end is in the range of changed text. If so, there is no
16948 unchanged row at the end of W's current matrix. */
16949 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16950 return NULL;
16951
16952 /* Set row to the last row in W's current matrix displaying text. */
16953 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16954
16955 /* If matrix is entirely empty, no unchanged row exists. */
16956 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16957 {
16958 /* The value of row is the last glyph row in the matrix having a
16959 meaningful buffer position in it. The end position of row
16960 corresponds to window_end_pos. This allows us to translate
16961 buffer positions in the current matrix to current buffer
16962 positions for characters not in changed text. */
16963 ptrdiff_t Z_old =
16964 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16965 ptrdiff_t Z_BYTE_old =
16966 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16967 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16968 struct glyph_row *first_text_row
16969 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16970
16971 *delta = Z - Z_old;
16972 *delta_bytes = Z_BYTE - Z_BYTE_old;
16973
16974 /* Set last_unchanged_pos to the buffer position of the last
16975 character in the buffer that has not been changed. Z is the
16976 index + 1 of the last character in current_buffer, i.e. by
16977 subtracting END_UNCHANGED we get the index of the last
16978 unchanged character, and we have to add BEG to get its buffer
16979 position. */
16980 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16981 last_unchanged_pos_old = last_unchanged_pos - *delta;
16982
16983 /* Search backward from ROW for a row displaying a line that
16984 starts at a minimum position >= last_unchanged_pos_old. */
16985 for (; row > first_text_row; --row)
16986 {
16987 /* This used to abort, but it can happen.
16988 It is ok to just stop the search instead here. KFS. */
16989 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16990 break;
16991
16992 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16993 row_found = row;
16994 }
16995 }
16996
16997 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16998
16999 return row_found;
17000 }
17001
17002
17003 /* Make sure that glyph rows in the current matrix of window W
17004 reference the same glyph memory as corresponding rows in the
17005 frame's frame matrix. This function is called after scrolling W's
17006 current matrix on a terminal frame in try_window_id and
17007 try_window_reusing_current_matrix. */
17008
17009 static void
17010 sync_frame_with_window_matrix_rows (struct window *w)
17011 {
17012 struct frame *f = XFRAME (w->frame);
17013 struct glyph_row *window_row, *window_row_end, *frame_row;
17014
17015 /* Preconditions: W must be a leaf window and full-width. Its frame
17016 must have a frame matrix. */
17017 eassert (NILP (w->hchild) && NILP (w->vchild));
17018 eassert (WINDOW_FULL_WIDTH_P (w));
17019 eassert (!FRAME_WINDOW_P (f));
17020
17021 /* If W is a full-width window, glyph pointers in W's current matrix
17022 have, by definition, to be the same as glyph pointers in the
17023 corresponding frame matrix. Note that frame matrices have no
17024 marginal areas (see build_frame_matrix). */
17025 window_row = w->current_matrix->rows;
17026 window_row_end = window_row + w->current_matrix->nrows;
17027 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17028 while (window_row < window_row_end)
17029 {
17030 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17031 struct glyph *end = window_row->glyphs[LAST_AREA];
17032
17033 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17034 frame_row->glyphs[TEXT_AREA] = start;
17035 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17036 frame_row->glyphs[LAST_AREA] = end;
17037
17038 /* Disable frame rows whose corresponding window rows have
17039 been disabled in try_window_id. */
17040 if (!window_row->enabled_p)
17041 frame_row->enabled_p = 0;
17042
17043 ++window_row, ++frame_row;
17044 }
17045 }
17046
17047
17048 /* Find the glyph row in window W containing CHARPOS. Consider all
17049 rows between START and END (not inclusive). END null means search
17050 all rows to the end of the display area of W. Value is the row
17051 containing CHARPOS or null. */
17052
17053 struct glyph_row *
17054 row_containing_pos (struct window *w, ptrdiff_t charpos,
17055 struct glyph_row *start, struct glyph_row *end, int dy)
17056 {
17057 struct glyph_row *row = start;
17058 struct glyph_row *best_row = NULL;
17059 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17060 int last_y;
17061
17062 /* If we happen to start on a header-line, skip that. */
17063 if (row->mode_line_p)
17064 ++row;
17065
17066 if ((end && row >= end) || !row->enabled_p)
17067 return NULL;
17068
17069 last_y = window_text_bottom_y (w) - dy;
17070
17071 while (1)
17072 {
17073 /* Give up if we have gone too far. */
17074 if (end && row >= end)
17075 return NULL;
17076 /* This formerly returned if they were equal.
17077 I think that both quantities are of a "last plus one" type;
17078 if so, when they are equal, the row is within the screen. -- rms. */
17079 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17080 return NULL;
17081
17082 /* If it is in this row, return this row. */
17083 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17084 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17085 /* The end position of a row equals the start
17086 position of the next row. If CHARPOS is there, we
17087 would rather display it in the next line, except
17088 when this line ends in ZV. */
17089 && !row->ends_at_zv_p
17090 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17091 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17092 {
17093 struct glyph *g;
17094
17095 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17096 || (!best_row && !row->continued_p))
17097 return row;
17098 /* In bidi-reordered rows, there could be several rows
17099 occluding point, all of them belonging to the same
17100 continued line. We need to find the row which fits
17101 CHARPOS the best. */
17102 for (g = row->glyphs[TEXT_AREA];
17103 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17104 g++)
17105 {
17106 if (!STRINGP (g->object))
17107 {
17108 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17109 {
17110 mindif = eabs (g->charpos - charpos);
17111 best_row = row;
17112 /* Exact match always wins. */
17113 if (mindif == 0)
17114 return best_row;
17115 }
17116 }
17117 }
17118 }
17119 else if (best_row && !row->continued_p)
17120 return best_row;
17121 ++row;
17122 }
17123 }
17124
17125
17126 /* Try to redisplay window W by reusing its existing display. W's
17127 current matrix must be up to date when this function is called,
17128 i.e. window_end_valid must not be nil.
17129
17130 Value is
17131
17132 1 if display has been updated
17133 0 if otherwise unsuccessful
17134 -1 if redisplay with same window start is known not to succeed
17135
17136 The following steps are performed:
17137
17138 1. Find the last row in the current matrix of W that is not
17139 affected by changes at the start of current_buffer. If no such row
17140 is found, give up.
17141
17142 2. Find the first row in W's current matrix that is not affected by
17143 changes at the end of current_buffer. Maybe there is no such row.
17144
17145 3. Display lines beginning with the row + 1 found in step 1 to the
17146 row found in step 2 or, if step 2 didn't find a row, to the end of
17147 the window.
17148
17149 4. If cursor is not known to appear on the window, give up.
17150
17151 5. If display stopped at the row found in step 2, scroll the
17152 display and current matrix as needed.
17153
17154 6. Maybe display some lines at the end of W, if we must. This can
17155 happen under various circumstances, like a partially visible line
17156 becoming fully visible, or because newly displayed lines are displayed
17157 in smaller font sizes.
17158
17159 7. Update W's window end information. */
17160
17161 static int
17162 try_window_id (struct window *w)
17163 {
17164 struct frame *f = XFRAME (w->frame);
17165 struct glyph_matrix *current_matrix = w->current_matrix;
17166 struct glyph_matrix *desired_matrix = w->desired_matrix;
17167 struct glyph_row *last_unchanged_at_beg_row;
17168 struct glyph_row *first_unchanged_at_end_row;
17169 struct glyph_row *row;
17170 struct glyph_row *bottom_row;
17171 int bottom_vpos;
17172 struct it it;
17173 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17174 int dvpos, dy;
17175 struct text_pos start_pos;
17176 struct run run;
17177 int first_unchanged_at_end_vpos = 0;
17178 struct glyph_row *last_text_row, *last_text_row_at_end;
17179 struct text_pos start;
17180 ptrdiff_t first_changed_charpos, last_changed_charpos;
17181
17182 #ifdef GLYPH_DEBUG
17183 if (inhibit_try_window_id)
17184 return 0;
17185 #endif
17186
17187 /* This is handy for debugging. */
17188 #if 0
17189 #define GIVE_UP(X) \
17190 do { \
17191 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17192 return 0; \
17193 } while (0)
17194 #else
17195 #define GIVE_UP(X) return 0
17196 #endif
17197
17198 SET_TEXT_POS_FROM_MARKER (start, w->start);
17199
17200 /* Don't use this for mini-windows because these can show
17201 messages and mini-buffers, and we don't handle that here. */
17202 if (MINI_WINDOW_P (w))
17203 GIVE_UP (1);
17204
17205 /* This flag is used to prevent redisplay optimizations. */
17206 if (windows_or_buffers_changed || cursor_type_changed)
17207 GIVE_UP (2);
17208
17209 /* Verify that narrowing has not changed.
17210 Also verify that we were not told to prevent redisplay optimizations.
17211 It would be nice to further
17212 reduce the number of cases where this prevents try_window_id. */
17213 if (current_buffer->clip_changed
17214 || current_buffer->prevent_redisplay_optimizations_p)
17215 GIVE_UP (3);
17216
17217 /* Window must either use window-based redisplay or be full width. */
17218 if (!FRAME_WINDOW_P (f)
17219 && (!FRAME_LINE_INS_DEL_OK (f)
17220 || !WINDOW_FULL_WIDTH_P (w)))
17221 GIVE_UP (4);
17222
17223 /* Give up if point is known NOT to appear in W. */
17224 if (PT < CHARPOS (start))
17225 GIVE_UP (5);
17226
17227 /* Another way to prevent redisplay optimizations. */
17228 if (w->last_modified == 0)
17229 GIVE_UP (6);
17230
17231 /* Verify that window is not hscrolled. */
17232 if (w->hscroll != 0)
17233 GIVE_UP (7);
17234
17235 /* Verify that display wasn't paused. */
17236 if (NILP (w->window_end_valid))
17237 GIVE_UP (8);
17238
17239 /* Can't use this if highlighting a region because a cursor movement
17240 will do more than just set the cursor. */
17241 if (0 <= markpos_of_region ())
17242 GIVE_UP (9);
17243
17244 /* Likewise if highlighting trailing whitespace. */
17245 if (!NILP (Vshow_trailing_whitespace))
17246 GIVE_UP (11);
17247
17248 /* Likewise if showing a region. */
17249 if (!NILP (w->region_showing))
17250 GIVE_UP (10);
17251
17252 /* Can't use this if overlay arrow position and/or string have
17253 changed. */
17254 if (overlay_arrows_changed_p ())
17255 GIVE_UP (12);
17256
17257 /* When word-wrap is on, adding a space to the first word of a
17258 wrapped line can change the wrap position, altering the line
17259 above it. It might be worthwhile to handle this more
17260 intelligently, but for now just redisplay from scratch. */
17261 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17262 GIVE_UP (21);
17263
17264 /* Under bidi reordering, adding or deleting a character in the
17265 beginning of a paragraph, before the first strong directional
17266 character, can change the base direction of the paragraph (unless
17267 the buffer specifies a fixed paragraph direction), which will
17268 require to redisplay the whole paragraph. It might be worthwhile
17269 to find the paragraph limits and widen the range of redisplayed
17270 lines to that, but for now just give up this optimization and
17271 redisplay from scratch. */
17272 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17273 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17274 GIVE_UP (22);
17275
17276 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17277 only if buffer has really changed. The reason is that the gap is
17278 initially at Z for freshly visited files. The code below would
17279 set end_unchanged to 0 in that case. */
17280 if (MODIFF > SAVE_MODIFF
17281 /* This seems to happen sometimes after saving a buffer. */
17282 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17283 {
17284 if (GPT - BEG < BEG_UNCHANGED)
17285 BEG_UNCHANGED = GPT - BEG;
17286 if (Z - GPT < END_UNCHANGED)
17287 END_UNCHANGED = Z - GPT;
17288 }
17289
17290 /* The position of the first and last character that has been changed. */
17291 first_changed_charpos = BEG + BEG_UNCHANGED;
17292 last_changed_charpos = Z - END_UNCHANGED;
17293
17294 /* If window starts after a line end, and the last change is in
17295 front of that newline, then changes don't affect the display.
17296 This case happens with stealth-fontification. Note that although
17297 the display is unchanged, glyph positions in the matrix have to
17298 be adjusted, of course. */
17299 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17300 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17301 && ((last_changed_charpos < CHARPOS (start)
17302 && CHARPOS (start) == BEGV)
17303 || (last_changed_charpos < CHARPOS (start) - 1
17304 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17305 {
17306 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17307 struct glyph_row *r0;
17308
17309 /* Compute how many chars/bytes have been added to or removed
17310 from the buffer. */
17311 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17312 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17313 Z_delta = Z - Z_old;
17314 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17315
17316 /* Give up if PT is not in the window. Note that it already has
17317 been checked at the start of try_window_id that PT is not in
17318 front of the window start. */
17319 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17320 GIVE_UP (13);
17321
17322 /* If window start is unchanged, we can reuse the whole matrix
17323 as is, after adjusting glyph positions. No need to compute
17324 the window end again, since its offset from Z hasn't changed. */
17325 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17326 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17327 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17328 /* PT must not be in a partially visible line. */
17329 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17330 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17331 {
17332 /* Adjust positions in the glyph matrix. */
17333 if (Z_delta || Z_delta_bytes)
17334 {
17335 struct glyph_row *r1
17336 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17337 increment_matrix_positions (w->current_matrix,
17338 MATRIX_ROW_VPOS (r0, current_matrix),
17339 MATRIX_ROW_VPOS (r1, current_matrix),
17340 Z_delta, Z_delta_bytes);
17341 }
17342
17343 /* Set the cursor. */
17344 row = row_containing_pos (w, PT, r0, NULL, 0);
17345 if (row)
17346 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17347 else
17348 emacs_abort ();
17349 return 1;
17350 }
17351 }
17352
17353 /* Handle the case that changes are all below what is displayed in
17354 the window, and that PT is in the window. This shortcut cannot
17355 be taken if ZV is visible in the window, and text has been added
17356 there that is visible in the window. */
17357 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17358 /* ZV is not visible in the window, or there are no
17359 changes at ZV, actually. */
17360 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17361 || first_changed_charpos == last_changed_charpos))
17362 {
17363 struct glyph_row *r0;
17364
17365 /* Give up if PT is not in the window. Note that it already has
17366 been checked at the start of try_window_id that PT is not in
17367 front of the window start. */
17368 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17369 GIVE_UP (14);
17370
17371 /* If window start is unchanged, we can reuse the whole matrix
17372 as is, without changing glyph positions since no text has
17373 been added/removed in front of the window end. */
17374 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17375 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17376 /* PT must not be in a partially visible line. */
17377 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17378 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17379 {
17380 /* We have to compute the window end anew since text
17381 could have been added/removed after it. */
17382 wset_window_end_pos
17383 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17384 w->window_end_bytepos
17385 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17386
17387 /* Set the cursor. */
17388 row = row_containing_pos (w, PT, r0, NULL, 0);
17389 if (row)
17390 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17391 else
17392 emacs_abort ();
17393 return 2;
17394 }
17395 }
17396
17397 /* Give up if window start is in the changed area.
17398
17399 The condition used to read
17400
17401 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17402
17403 but why that was tested escapes me at the moment. */
17404 if (CHARPOS (start) >= first_changed_charpos
17405 && CHARPOS (start) <= last_changed_charpos)
17406 GIVE_UP (15);
17407
17408 /* Check that window start agrees with the start of the first glyph
17409 row in its current matrix. Check this after we know the window
17410 start is not in changed text, otherwise positions would not be
17411 comparable. */
17412 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17413 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17414 GIVE_UP (16);
17415
17416 /* Give up if the window ends in strings. Overlay strings
17417 at the end are difficult to handle, so don't try. */
17418 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17419 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17420 GIVE_UP (20);
17421
17422 /* Compute the position at which we have to start displaying new
17423 lines. Some of the lines at the top of the window might be
17424 reusable because they are not displaying changed text. Find the
17425 last row in W's current matrix not affected by changes at the
17426 start of current_buffer. Value is null if changes start in the
17427 first line of window. */
17428 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17429 if (last_unchanged_at_beg_row)
17430 {
17431 /* Avoid starting to display in the middle of a character, a TAB
17432 for instance. This is easier than to set up the iterator
17433 exactly, and it's not a frequent case, so the additional
17434 effort wouldn't really pay off. */
17435 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17436 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17437 && last_unchanged_at_beg_row > w->current_matrix->rows)
17438 --last_unchanged_at_beg_row;
17439
17440 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17441 GIVE_UP (17);
17442
17443 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17444 GIVE_UP (18);
17445 start_pos = it.current.pos;
17446
17447 /* Start displaying new lines in the desired matrix at the same
17448 vpos we would use in the current matrix, i.e. below
17449 last_unchanged_at_beg_row. */
17450 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17451 current_matrix);
17452 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17453 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17454
17455 eassert (it.hpos == 0 && it.current_x == 0);
17456 }
17457 else
17458 {
17459 /* There are no reusable lines at the start of the window.
17460 Start displaying in the first text line. */
17461 start_display (&it, w, start);
17462 it.vpos = it.first_vpos;
17463 start_pos = it.current.pos;
17464 }
17465
17466 /* Find the first row that is not affected by changes at the end of
17467 the buffer. Value will be null if there is no unchanged row, in
17468 which case we must redisplay to the end of the window. delta
17469 will be set to the value by which buffer positions beginning with
17470 first_unchanged_at_end_row have to be adjusted due to text
17471 changes. */
17472 first_unchanged_at_end_row
17473 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17474 IF_DEBUG (debug_delta = delta);
17475 IF_DEBUG (debug_delta_bytes = delta_bytes);
17476
17477 /* Set stop_pos to the buffer position up to which we will have to
17478 display new lines. If first_unchanged_at_end_row != NULL, this
17479 is the buffer position of the start of the line displayed in that
17480 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17481 that we don't stop at a buffer position. */
17482 stop_pos = 0;
17483 if (first_unchanged_at_end_row)
17484 {
17485 eassert (last_unchanged_at_beg_row == NULL
17486 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17487
17488 /* If this is a continuation line, move forward to the next one
17489 that isn't. Changes in lines above affect this line.
17490 Caution: this may move first_unchanged_at_end_row to a row
17491 not displaying text. */
17492 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17493 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17494 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17495 < it.last_visible_y))
17496 ++first_unchanged_at_end_row;
17497
17498 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17499 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17500 >= it.last_visible_y))
17501 first_unchanged_at_end_row = NULL;
17502 else
17503 {
17504 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17505 + delta);
17506 first_unchanged_at_end_vpos
17507 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17508 eassert (stop_pos >= Z - END_UNCHANGED);
17509 }
17510 }
17511 else if (last_unchanged_at_beg_row == NULL)
17512 GIVE_UP (19);
17513
17514
17515 #ifdef GLYPH_DEBUG
17516
17517 /* Either there is no unchanged row at the end, or the one we have
17518 now displays text. This is a necessary condition for the window
17519 end pos calculation at the end of this function. */
17520 eassert (first_unchanged_at_end_row == NULL
17521 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17522
17523 debug_last_unchanged_at_beg_vpos
17524 = (last_unchanged_at_beg_row
17525 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17526 : -1);
17527 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17528
17529 #endif /* GLYPH_DEBUG */
17530
17531
17532 /* Display new lines. Set last_text_row to the last new line
17533 displayed which has text on it, i.e. might end up as being the
17534 line where the window_end_vpos is. */
17535 w->cursor.vpos = -1;
17536 last_text_row = NULL;
17537 overlay_arrow_seen = 0;
17538 while (it.current_y < it.last_visible_y
17539 && !fonts_changed_p
17540 && (first_unchanged_at_end_row == NULL
17541 || IT_CHARPOS (it) < stop_pos))
17542 {
17543 if (display_line (&it))
17544 last_text_row = it.glyph_row - 1;
17545 }
17546
17547 if (fonts_changed_p)
17548 return -1;
17549
17550
17551 /* Compute differences in buffer positions, y-positions etc. for
17552 lines reused at the bottom of the window. Compute what we can
17553 scroll. */
17554 if (first_unchanged_at_end_row
17555 /* No lines reused because we displayed everything up to the
17556 bottom of the window. */
17557 && it.current_y < it.last_visible_y)
17558 {
17559 dvpos = (it.vpos
17560 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17561 current_matrix));
17562 dy = it.current_y - first_unchanged_at_end_row->y;
17563 run.current_y = first_unchanged_at_end_row->y;
17564 run.desired_y = run.current_y + dy;
17565 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17566 }
17567 else
17568 {
17569 delta = delta_bytes = dvpos = dy
17570 = run.current_y = run.desired_y = run.height = 0;
17571 first_unchanged_at_end_row = NULL;
17572 }
17573 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17574
17575
17576 /* Find the cursor if not already found. We have to decide whether
17577 PT will appear on this window (it sometimes doesn't, but this is
17578 not a very frequent case.) This decision has to be made before
17579 the current matrix is altered. A value of cursor.vpos < 0 means
17580 that PT is either in one of the lines beginning at
17581 first_unchanged_at_end_row or below the window. Don't care for
17582 lines that might be displayed later at the window end; as
17583 mentioned, this is not a frequent case. */
17584 if (w->cursor.vpos < 0)
17585 {
17586 /* Cursor in unchanged rows at the top? */
17587 if (PT < CHARPOS (start_pos)
17588 && last_unchanged_at_beg_row)
17589 {
17590 row = row_containing_pos (w, PT,
17591 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17592 last_unchanged_at_beg_row + 1, 0);
17593 if (row)
17594 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17595 }
17596
17597 /* Start from first_unchanged_at_end_row looking for PT. */
17598 else if (first_unchanged_at_end_row)
17599 {
17600 row = row_containing_pos (w, PT - delta,
17601 first_unchanged_at_end_row, NULL, 0);
17602 if (row)
17603 set_cursor_from_row (w, row, w->current_matrix, delta,
17604 delta_bytes, dy, dvpos);
17605 }
17606
17607 /* Give up if cursor was not found. */
17608 if (w->cursor.vpos < 0)
17609 {
17610 clear_glyph_matrix (w->desired_matrix);
17611 return -1;
17612 }
17613 }
17614
17615 /* Don't let the cursor end in the scroll margins. */
17616 {
17617 int this_scroll_margin, cursor_height;
17618
17619 this_scroll_margin =
17620 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17621 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17622 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17623
17624 if ((w->cursor.y < this_scroll_margin
17625 && CHARPOS (start) > BEGV)
17626 /* Old redisplay didn't take scroll margin into account at the bottom,
17627 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17628 || (w->cursor.y + (make_cursor_line_fully_visible_p
17629 ? cursor_height + this_scroll_margin
17630 : 1)) > it.last_visible_y)
17631 {
17632 w->cursor.vpos = -1;
17633 clear_glyph_matrix (w->desired_matrix);
17634 return -1;
17635 }
17636 }
17637
17638 /* Scroll the display. Do it before changing the current matrix so
17639 that xterm.c doesn't get confused about where the cursor glyph is
17640 found. */
17641 if (dy && run.height)
17642 {
17643 update_begin (f);
17644
17645 if (FRAME_WINDOW_P (f))
17646 {
17647 FRAME_RIF (f)->update_window_begin_hook (w);
17648 FRAME_RIF (f)->clear_window_mouse_face (w);
17649 FRAME_RIF (f)->scroll_run_hook (w, &run);
17650 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17651 }
17652 else
17653 {
17654 /* Terminal frame. In this case, dvpos gives the number of
17655 lines to scroll by; dvpos < 0 means scroll up. */
17656 int from_vpos
17657 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17658 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17659 int end = (WINDOW_TOP_EDGE_LINE (w)
17660 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17661 + window_internal_height (w));
17662
17663 #if defined (HAVE_GPM) || defined (MSDOS)
17664 x_clear_window_mouse_face (w);
17665 #endif
17666 /* Perform the operation on the screen. */
17667 if (dvpos > 0)
17668 {
17669 /* Scroll last_unchanged_at_beg_row to the end of the
17670 window down dvpos lines. */
17671 set_terminal_window (f, end);
17672
17673 /* On dumb terminals delete dvpos lines at the end
17674 before inserting dvpos empty lines. */
17675 if (!FRAME_SCROLL_REGION_OK (f))
17676 ins_del_lines (f, end - dvpos, -dvpos);
17677
17678 /* Insert dvpos empty lines in front of
17679 last_unchanged_at_beg_row. */
17680 ins_del_lines (f, from, dvpos);
17681 }
17682 else if (dvpos < 0)
17683 {
17684 /* Scroll up last_unchanged_at_beg_vpos to the end of
17685 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17686 set_terminal_window (f, end);
17687
17688 /* Delete dvpos lines in front of
17689 last_unchanged_at_beg_vpos. ins_del_lines will set
17690 the cursor to the given vpos and emit |dvpos| delete
17691 line sequences. */
17692 ins_del_lines (f, from + dvpos, dvpos);
17693
17694 /* On a dumb terminal insert dvpos empty lines at the
17695 end. */
17696 if (!FRAME_SCROLL_REGION_OK (f))
17697 ins_del_lines (f, end + dvpos, -dvpos);
17698 }
17699
17700 set_terminal_window (f, 0);
17701 }
17702
17703 update_end (f);
17704 }
17705
17706 /* Shift reused rows of the current matrix to the right position.
17707 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17708 text. */
17709 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17710 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17711 if (dvpos < 0)
17712 {
17713 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17714 bottom_vpos, dvpos);
17715 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17716 bottom_vpos);
17717 }
17718 else if (dvpos > 0)
17719 {
17720 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17721 bottom_vpos, dvpos);
17722 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17723 first_unchanged_at_end_vpos + dvpos);
17724 }
17725
17726 /* For frame-based redisplay, make sure that current frame and window
17727 matrix are in sync with respect to glyph memory. */
17728 if (!FRAME_WINDOW_P (f))
17729 sync_frame_with_window_matrix_rows (w);
17730
17731 /* Adjust buffer positions in reused rows. */
17732 if (delta || delta_bytes)
17733 increment_matrix_positions (current_matrix,
17734 first_unchanged_at_end_vpos + dvpos,
17735 bottom_vpos, delta, delta_bytes);
17736
17737 /* Adjust Y positions. */
17738 if (dy)
17739 shift_glyph_matrix (w, current_matrix,
17740 first_unchanged_at_end_vpos + dvpos,
17741 bottom_vpos, dy);
17742
17743 if (first_unchanged_at_end_row)
17744 {
17745 first_unchanged_at_end_row += dvpos;
17746 if (first_unchanged_at_end_row->y >= it.last_visible_y
17747 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17748 first_unchanged_at_end_row = NULL;
17749 }
17750
17751 /* If scrolling up, there may be some lines to display at the end of
17752 the window. */
17753 last_text_row_at_end = NULL;
17754 if (dy < 0)
17755 {
17756 /* Scrolling up can leave for example a partially visible line
17757 at the end of the window to be redisplayed. */
17758 /* Set last_row to the glyph row in the current matrix where the
17759 window end line is found. It has been moved up or down in
17760 the matrix by dvpos. */
17761 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17762 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17763
17764 /* If last_row is the window end line, it should display text. */
17765 eassert (last_row->displays_text_p);
17766
17767 /* If window end line was partially visible before, begin
17768 displaying at that line. Otherwise begin displaying with the
17769 line following it. */
17770 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17771 {
17772 init_to_row_start (&it, w, last_row);
17773 it.vpos = last_vpos;
17774 it.current_y = last_row->y;
17775 }
17776 else
17777 {
17778 init_to_row_end (&it, w, last_row);
17779 it.vpos = 1 + last_vpos;
17780 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17781 ++last_row;
17782 }
17783
17784 /* We may start in a continuation line. If so, we have to
17785 get the right continuation_lines_width and current_x. */
17786 it.continuation_lines_width = last_row->continuation_lines_width;
17787 it.hpos = it.current_x = 0;
17788
17789 /* Display the rest of the lines at the window end. */
17790 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17791 while (it.current_y < it.last_visible_y
17792 && !fonts_changed_p)
17793 {
17794 /* Is it always sure that the display agrees with lines in
17795 the current matrix? I don't think so, so we mark rows
17796 displayed invalid in the current matrix by setting their
17797 enabled_p flag to zero. */
17798 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17799 if (display_line (&it))
17800 last_text_row_at_end = it.glyph_row - 1;
17801 }
17802 }
17803
17804 /* Update window_end_pos and window_end_vpos. */
17805 if (first_unchanged_at_end_row
17806 && !last_text_row_at_end)
17807 {
17808 /* Window end line if one of the preserved rows from the current
17809 matrix. Set row to the last row displaying text in current
17810 matrix starting at first_unchanged_at_end_row, after
17811 scrolling. */
17812 eassert (first_unchanged_at_end_row->displays_text_p);
17813 row = find_last_row_displaying_text (w->current_matrix, &it,
17814 first_unchanged_at_end_row);
17815 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17816
17817 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17818 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17819 wset_window_end_vpos
17820 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17821 eassert (w->window_end_bytepos >= 0);
17822 IF_DEBUG (debug_method_add (w, "A"));
17823 }
17824 else if (last_text_row_at_end)
17825 {
17826 wset_window_end_pos
17827 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17828 w->window_end_bytepos
17829 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17830 wset_window_end_vpos
17831 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17832 desired_matrix)));
17833 eassert (w->window_end_bytepos >= 0);
17834 IF_DEBUG (debug_method_add (w, "B"));
17835 }
17836 else if (last_text_row)
17837 {
17838 /* We have displayed either to the end of the window or at the
17839 end of the window, i.e. the last row with text is to be found
17840 in the desired matrix. */
17841 wset_window_end_pos
17842 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17843 w->window_end_bytepos
17844 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17845 wset_window_end_vpos
17846 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17847 eassert (w->window_end_bytepos >= 0);
17848 }
17849 else if (first_unchanged_at_end_row == NULL
17850 && last_text_row == NULL
17851 && last_text_row_at_end == NULL)
17852 {
17853 /* Displayed to end of window, but no line containing text was
17854 displayed. Lines were deleted at the end of the window. */
17855 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17856 int vpos = XFASTINT (w->window_end_vpos);
17857 struct glyph_row *current_row = current_matrix->rows + vpos;
17858 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17859
17860 for (row = NULL;
17861 row == NULL && vpos >= first_vpos;
17862 --vpos, --current_row, --desired_row)
17863 {
17864 if (desired_row->enabled_p)
17865 {
17866 if (desired_row->displays_text_p)
17867 row = desired_row;
17868 }
17869 else if (current_row->displays_text_p)
17870 row = current_row;
17871 }
17872
17873 eassert (row != NULL);
17874 wset_window_end_vpos (w, make_number (vpos + 1));
17875 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17876 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17877 eassert (w->window_end_bytepos >= 0);
17878 IF_DEBUG (debug_method_add (w, "C"));
17879 }
17880 else
17881 emacs_abort ();
17882
17883 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17884 debug_end_vpos = XFASTINT (w->window_end_vpos));
17885
17886 /* Record that display has not been completed. */
17887 wset_window_end_valid (w, Qnil);
17888 w->desired_matrix->no_scrolling_p = 1;
17889 return 3;
17890
17891 #undef GIVE_UP
17892 }
17893
17894
17895 \f
17896 /***********************************************************************
17897 More debugging support
17898 ***********************************************************************/
17899
17900 #ifdef GLYPH_DEBUG
17901
17902 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17903 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17904 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17905
17906
17907 /* Dump the contents of glyph matrix MATRIX on stderr.
17908
17909 GLYPHS 0 means don't show glyph contents.
17910 GLYPHS 1 means show glyphs in short form
17911 GLYPHS > 1 means show glyphs in long form. */
17912
17913 void
17914 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17915 {
17916 int i;
17917 for (i = 0; i < matrix->nrows; ++i)
17918 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17919 }
17920
17921
17922 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17923 the glyph row and area where the glyph comes from. */
17924
17925 void
17926 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17927 {
17928 if (glyph->type == CHAR_GLYPH)
17929 {
17930 fprintf (stderr,
17931 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17932 glyph - row->glyphs[TEXT_AREA],
17933 'C',
17934 glyph->charpos,
17935 (BUFFERP (glyph->object)
17936 ? 'B'
17937 : (STRINGP (glyph->object)
17938 ? 'S'
17939 : '-')),
17940 glyph->pixel_width,
17941 glyph->u.ch,
17942 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17943 ? glyph->u.ch
17944 : '.'),
17945 glyph->face_id,
17946 glyph->left_box_line_p,
17947 glyph->right_box_line_p);
17948 }
17949 else if (glyph->type == STRETCH_GLYPH)
17950 {
17951 fprintf (stderr,
17952 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17953 glyph - row->glyphs[TEXT_AREA],
17954 'S',
17955 glyph->charpos,
17956 (BUFFERP (glyph->object)
17957 ? 'B'
17958 : (STRINGP (glyph->object)
17959 ? 'S'
17960 : '-')),
17961 glyph->pixel_width,
17962 0,
17963 '.',
17964 glyph->face_id,
17965 glyph->left_box_line_p,
17966 glyph->right_box_line_p);
17967 }
17968 else if (glyph->type == IMAGE_GLYPH)
17969 {
17970 fprintf (stderr,
17971 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17972 glyph - row->glyphs[TEXT_AREA],
17973 'I',
17974 glyph->charpos,
17975 (BUFFERP (glyph->object)
17976 ? 'B'
17977 : (STRINGP (glyph->object)
17978 ? 'S'
17979 : '-')),
17980 glyph->pixel_width,
17981 glyph->u.img_id,
17982 '.',
17983 glyph->face_id,
17984 glyph->left_box_line_p,
17985 glyph->right_box_line_p);
17986 }
17987 else if (glyph->type == COMPOSITE_GLYPH)
17988 {
17989 fprintf (stderr,
17990 " %5td %4c %6"pI"d %c %3d 0x%05x",
17991 glyph - row->glyphs[TEXT_AREA],
17992 '+',
17993 glyph->charpos,
17994 (BUFFERP (glyph->object)
17995 ? 'B'
17996 : (STRINGP (glyph->object)
17997 ? 'S'
17998 : '-')),
17999 glyph->pixel_width,
18000 glyph->u.cmp.id);
18001 if (glyph->u.cmp.automatic)
18002 fprintf (stderr,
18003 "[%d-%d]",
18004 glyph->slice.cmp.from, glyph->slice.cmp.to);
18005 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18006 glyph->face_id,
18007 glyph->left_box_line_p,
18008 glyph->right_box_line_p);
18009 }
18010 }
18011
18012
18013 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18014 GLYPHS 0 means don't show glyph contents.
18015 GLYPHS 1 means show glyphs in short form
18016 GLYPHS > 1 means show glyphs in long form. */
18017
18018 void
18019 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18020 {
18021 if (glyphs != 1)
18022 {
18023 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18024 fprintf (stderr, "======================================================================\n");
18025
18026 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18027 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18028 vpos,
18029 MATRIX_ROW_START_CHARPOS (row),
18030 MATRIX_ROW_END_CHARPOS (row),
18031 row->used[TEXT_AREA],
18032 row->contains_overlapping_glyphs_p,
18033 row->enabled_p,
18034 row->truncated_on_left_p,
18035 row->truncated_on_right_p,
18036 row->continued_p,
18037 MATRIX_ROW_CONTINUATION_LINE_P (row),
18038 row->displays_text_p,
18039 row->ends_at_zv_p,
18040 row->fill_line_p,
18041 row->ends_in_middle_of_char_p,
18042 row->starts_in_middle_of_char_p,
18043 row->mouse_face_p,
18044 row->x,
18045 row->y,
18046 row->pixel_width,
18047 row->height,
18048 row->visible_height,
18049 row->ascent,
18050 row->phys_ascent);
18051 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18052 row->end.overlay_string_index,
18053 row->continuation_lines_width);
18054 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18055 CHARPOS (row->start.string_pos),
18056 CHARPOS (row->end.string_pos));
18057 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18058 row->end.dpvec_index);
18059 }
18060
18061 if (glyphs > 1)
18062 {
18063 int area;
18064
18065 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18066 {
18067 struct glyph *glyph = row->glyphs[area];
18068 struct glyph *glyph_end = glyph + row->used[area];
18069
18070 /* Glyph for a line end in text. */
18071 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18072 ++glyph_end;
18073
18074 if (glyph < glyph_end)
18075 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18076
18077 for (; glyph < glyph_end; ++glyph)
18078 dump_glyph (row, glyph, area);
18079 }
18080 }
18081 else if (glyphs == 1)
18082 {
18083 int area;
18084
18085 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18086 {
18087 char *s = alloca (row->used[area] + 1);
18088 int i;
18089
18090 for (i = 0; i < row->used[area]; ++i)
18091 {
18092 struct glyph *glyph = row->glyphs[area] + i;
18093 if (glyph->type == CHAR_GLYPH
18094 && glyph->u.ch < 0x80
18095 && glyph->u.ch >= ' ')
18096 s[i] = glyph->u.ch;
18097 else
18098 s[i] = '.';
18099 }
18100
18101 s[i] = '\0';
18102 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18103 }
18104 }
18105 }
18106
18107
18108 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18109 Sdump_glyph_matrix, 0, 1, "p",
18110 doc: /* Dump the current matrix of the selected window to stderr.
18111 Shows contents of glyph row structures. With non-nil
18112 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18113 glyphs in short form, otherwise show glyphs in long form. */)
18114 (Lisp_Object glyphs)
18115 {
18116 struct window *w = XWINDOW (selected_window);
18117 struct buffer *buffer = XBUFFER (w->buffer);
18118
18119 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18120 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18121 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18122 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18123 fprintf (stderr, "=============================================\n");
18124 dump_glyph_matrix (w->current_matrix,
18125 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18126 return Qnil;
18127 }
18128
18129
18130 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18131 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18132 (void)
18133 {
18134 struct frame *f = XFRAME (selected_frame);
18135 dump_glyph_matrix (f->current_matrix, 1);
18136 return Qnil;
18137 }
18138
18139
18140 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18141 doc: /* Dump glyph row ROW to stderr.
18142 GLYPH 0 means don't dump glyphs.
18143 GLYPH 1 means dump glyphs in short form.
18144 GLYPH > 1 or omitted means dump glyphs in long form. */)
18145 (Lisp_Object row, Lisp_Object glyphs)
18146 {
18147 struct glyph_matrix *matrix;
18148 EMACS_INT vpos;
18149
18150 CHECK_NUMBER (row);
18151 matrix = XWINDOW (selected_window)->current_matrix;
18152 vpos = XINT (row);
18153 if (vpos >= 0 && vpos < matrix->nrows)
18154 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18155 vpos,
18156 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18157 return Qnil;
18158 }
18159
18160
18161 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18162 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18163 GLYPH 0 means don't dump glyphs.
18164 GLYPH 1 means dump glyphs in short form.
18165 GLYPH > 1 or omitted means dump glyphs in long form. */)
18166 (Lisp_Object row, Lisp_Object glyphs)
18167 {
18168 struct frame *sf = SELECTED_FRAME ();
18169 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18170 EMACS_INT vpos;
18171
18172 CHECK_NUMBER (row);
18173 vpos = XINT (row);
18174 if (vpos >= 0 && vpos < m->nrows)
18175 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18176 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18177 return Qnil;
18178 }
18179
18180
18181 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18182 doc: /* Toggle tracing of redisplay.
18183 With ARG, turn tracing on if and only if ARG is positive. */)
18184 (Lisp_Object arg)
18185 {
18186 if (NILP (arg))
18187 trace_redisplay_p = !trace_redisplay_p;
18188 else
18189 {
18190 arg = Fprefix_numeric_value (arg);
18191 trace_redisplay_p = XINT (arg) > 0;
18192 }
18193
18194 return Qnil;
18195 }
18196
18197
18198 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18199 doc: /* Like `format', but print result to stderr.
18200 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18201 (ptrdiff_t nargs, Lisp_Object *args)
18202 {
18203 Lisp_Object s = Fformat (nargs, args);
18204 fprintf (stderr, "%s", SDATA (s));
18205 return Qnil;
18206 }
18207
18208 #endif /* GLYPH_DEBUG */
18209
18210
18211 \f
18212 /***********************************************************************
18213 Building Desired Matrix Rows
18214 ***********************************************************************/
18215
18216 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18217 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18218
18219 static struct glyph_row *
18220 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18221 {
18222 struct frame *f = XFRAME (WINDOW_FRAME (w));
18223 struct buffer *buffer = XBUFFER (w->buffer);
18224 struct buffer *old = current_buffer;
18225 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18226 int arrow_len = SCHARS (overlay_arrow_string);
18227 const unsigned char *arrow_end = arrow_string + arrow_len;
18228 const unsigned char *p;
18229 struct it it;
18230 int multibyte_p;
18231 int n_glyphs_before;
18232
18233 set_buffer_temp (buffer);
18234 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18235 it.glyph_row->used[TEXT_AREA] = 0;
18236 SET_TEXT_POS (it.position, 0, 0);
18237
18238 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18239 p = arrow_string;
18240 while (p < arrow_end)
18241 {
18242 Lisp_Object face, ilisp;
18243
18244 /* Get the next character. */
18245 if (multibyte_p)
18246 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18247 else
18248 {
18249 it.c = it.char_to_display = *p, it.len = 1;
18250 if (! ASCII_CHAR_P (it.c))
18251 it.char_to_display = BYTE8_TO_CHAR (it.c);
18252 }
18253 p += it.len;
18254
18255 /* Get its face. */
18256 ilisp = make_number (p - arrow_string);
18257 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18258 it.face_id = compute_char_face (f, it.char_to_display, face);
18259
18260 /* Compute its width, get its glyphs. */
18261 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18262 SET_TEXT_POS (it.position, -1, -1);
18263 PRODUCE_GLYPHS (&it);
18264
18265 /* If this character doesn't fit any more in the line, we have
18266 to remove some glyphs. */
18267 if (it.current_x > it.last_visible_x)
18268 {
18269 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18270 break;
18271 }
18272 }
18273
18274 set_buffer_temp (old);
18275 return it.glyph_row;
18276 }
18277
18278
18279 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18280 glyphs to insert is determined by produce_special_glyphs. */
18281
18282 static void
18283 insert_left_trunc_glyphs (struct it *it)
18284 {
18285 struct it truncate_it;
18286 struct glyph *from, *end, *to, *toend;
18287
18288 eassert (!FRAME_WINDOW_P (it->f)
18289 || (!it->glyph_row->reversed_p
18290 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18291 || (it->glyph_row->reversed_p
18292 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18293
18294 /* Get the truncation glyphs. */
18295 truncate_it = *it;
18296 truncate_it.current_x = 0;
18297 truncate_it.face_id = DEFAULT_FACE_ID;
18298 truncate_it.glyph_row = &scratch_glyph_row;
18299 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18300 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18301 truncate_it.object = make_number (0);
18302 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18303
18304 /* Overwrite glyphs from IT with truncation glyphs. */
18305 if (!it->glyph_row->reversed_p)
18306 {
18307 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18308
18309 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18310 end = from + tused;
18311 to = it->glyph_row->glyphs[TEXT_AREA];
18312 toend = to + it->glyph_row->used[TEXT_AREA];
18313 if (FRAME_WINDOW_P (it->f))
18314 {
18315 /* On GUI frames, when variable-size fonts are displayed,
18316 the truncation glyphs may need more pixels than the row's
18317 glyphs they overwrite. We overwrite more glyphs to free
18318 enough screen real estate, and enlarge the stretch glyph
18319 on the right (see display_line), if there is one, to
18320 preserve the screen position of the truncation glyphs on
18321 the right. */
18322 int w = 0;
18323 struct glyph *g = to;
18324 short used;
18325
18326 /* The first glyph could be partially visible, in which case
18327 it->glyph_row->x will be negative. But we want the left
18328 truncation glyphs to be aligned at the left margin of the
18329 window, so we override the x coordinate at which the row
18330 will begin. */
18331 it->glyph_row->x = 0;
18332 while (g < toend && w < it->truncation_pixel_width)
18333 {
18334 w += g->pixel_width;
18335 ++g;
18336 }
18337 if (g - to - tused > 0)
18338 {
18339 memmove (to + tused, g, (toend - g) * sizeof(*g));
18340 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18341 }
18342 used = it->glyph_row->used[TEXT_AREA];
18343 if (it->glyph_row->truncated_on_right_p
18344 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18345 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18346 == STRETCH_GLYPH)
18347 {
18348 int extra = w - it->truncation_pixel_width;
18349
18350 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18351 }
18352 }
18353
18354 while (from < end)
18355 *to++ = *from++;
18356
18357 /* There may be padding glyphs left over. Overwrite them too. */
18358 if (!FRAME_WINDOW_P (it->f))
18359 {
18360 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18361 {
18362 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18363 while (from < end)
18364 *to++ = *from++;
18365 }
18366 }
18367
18368 if (to > toend)
18369 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18370 }
18371 else
18372 {
18373 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18374
18375 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18376 that back to front. */
18377 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18378 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18379 toend = it->glyph_row->glyphs[TEXT_AREA];
18380 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18381 if (FRAME_WINDOW_P (it->f))
18382 {
18383 int w = 0;
18384 struct glyph *g = to;
18385
18386 while (g >= toend && w < it->truncation_pixel_width)
18387 {
18388 w += g->pixel_width;
18389 --g;
18390 }
18391 if (to - g - tused > 0)
18392 to = g + tused;
18393 if (it->glyph_row->truncated_on_right_p
18394 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18395 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18396 {
18397 int extra = w - it->truncation_pixel_width;
18398
18399 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18400 }
18401 }
18402
18403 while (from >= end && to >= toend)
18404 *to-- = *from--;
18405 if (!FRAME_WINDOW_P (it->f))
18406 {
18407 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18408 {
18409 from =
18410 truncate_it.glyph_row->glyphs[TEXT_AREA]
18411 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18412 while (from >= end && to >= toend)
18413 *to-- = *from--;
18414 }
18415 }
18416 if (from >= end)
18417 {
18418 /* Need to free some room before prepending additional
18419 glyphs. */
18420 int move_by = from - end + 1;
18421 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18422 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18423
18424 for ( ; g >= g0; g--)
18425 g[move_by] = *g;
18426 while (from >= end)
18427 *to-- = *from--;
18428 it->glyph_row->used[TEXT_AREA] += move_by;
18429 }
18430 }
18431 }
18432
18433 /* Compute the hash code for ROW. */
18434 unsigned
18435 row_hash (struct glyph_row *row)
18436 {
18437 int area, k;
18438 unsigned hashval = 0;
18439
18440 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18441 for (k = 0; k < row->used[area]; ++k)
18442 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18443 + row->glyphs[area][k].u.val
18444 + row->glyphs[area][k].face_id
18445 + row->glyphs[area][k].padding_p
18446 + (row->glyphs[area][k].type << 2));
18447
18448 return hashval;
18449 }
18450
18451 /* Compute the pixel height and width of IT->glyph_row.
18452
18453 Most of the time, ascent and height of a display line will be equal
18454 to the max_ascent and max_height values of the display iterator
18455 structure. This is not the case if
18456
18457 1. We hit ZV without displaying anything. In this case, max_ascent
18458 and max_height will be zero.
18459
18460 2. We have some glyphs that don't contribute to the line height.
18461 (The glyph row flag contributes_to_line_height_p is for future
18462 pixmap extensions).
18463
18464 The first case is easily covered by using default values because in
18465 these cases, the line height does not really matter, except that it
18466 must not be zero. */
18467
18468 static void
18469 compute_line_metrics (struct it *it)
18470 {
18471 struct glyph_row *row = it->glyph_row;
18472
18473 if (FRAME_WINDOW_P (it->f))
18474 {
18475 int i, min_y, max_y;
18476
18477 /* The line may consist of one space only, that was added to
18478 place the cursor on it. If so, the row's height hasn't been
18479 computed yet. */
18480 if (row->height == 0)
18481 {
18482 if (it->max_ascent + it->max_descent == 0)
18483 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18484 row->ascent = it->max_ascent;
18485 row->height = it->max_ascent + it->max_descent;
18486 row->phys_ascent = it->max_phys_ascent;
18487 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18488 row->extra_line_spacing = it->max_extra_line_spacing;
18489 }
18490
18491 /* Compute the width of this line. */
18492 row->pixel_width = row->x;
18493 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18494 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18495
18496 eassert (row->pixel_width >= 0);
18497 eassert (row->ascent >= 0 && row->height > 0);
18498
18499 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18500 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18501
18502 /* If first line's physical ascent is larger than its logical
18503 ascent, use the physical ascent, and make the row taller.
18504 This makes accented characters fully visible. */
18505 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18506 && row->phys_ascent > row->ascent)
18507 {
18508 row->height += row->phys_ascent - row->ascent;
18509 row->ascent = row->phys_ascent;
18510 }
18511
18512 /* Compute how much of the line is visible. */
18513 row->visible_height = row->height;
18514
18515 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18516 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18517
18518 if (row->y < min_y)
18519 row->visible_height -= min_y - row->y;
18520 if (row->y + row->height > max_y)
18521 row->visible_height -= row->y + row->height - max_y;
18522 }
18523 else
18524 {
18525 row->pixel_width = row->used[TEXT_AREA];
18526 if (row->continued_p)
18527 row->pixel_width -= it->continuation_pixel_width;
18528 else if (row->truncated_on_right_p)
18529 row->pixel_width -= it->truncation_pixel_width;
18530 row->ascent = row->phys_ascent = 0;
18531 row->height = row->phys_height = row->visible_height = 1;
18532 row->extra_line_spacing = 0;
18533 }
18534
18535 /* Compute a hash code for this row. */
18536 row->hash = row_hash (row);
18537
18538 it->max_ascent = it->max_descent = 0;
18539 it->max_phys_ascent = it->max_phys_descent = 0;
18540 }
18541
18542
18543 /* Append one space to the glyph row of iterator IT if doing a
18544 window-based redisplay. The space has the same face as
18545 IT->face_id. Value is non-zero if a space was added.
18546
18547 This function is called to make sure that there is always one glyph
18548 at the end of a glyph row that the cursor can be set on under
18549 window-systems. (If there weren't such a glyph we would not know
18550 how wide and tall a box cursor should be displayed).
18551
18552 At the same time this space let's a nicely handle clearing to the
18553 end of the line if the row ends in italic text. */
18554
18555 static int
18556 append_space_for_newline (struct it *it, int default_face_p)
18557 {
18558 if (FRAME_WINDOW_P (it->f))
18559 {
18560 int n = it->glyph_row->used[TEXT_AREA];
18561
18562 if (it->glyph_row->glyphs[TEXT_AREA] + n
18563 < it->glyph_row->glyphs[1 + TEXT_AREA])
18564 {
18565 /* Save some values that must not be changed.
18566 Must save IT->c and IT->len because otherwise
18567 ITERATOR_AT_END_P wouldn't work anymore after
18568 append_space_for_newline has been called. */
18569 enum display_element_type saved_what = it->what;
18570 int saved_c = it->c, saved_len = it->len;
18571 int saved_char_to_display = it->char_to_display;
18572 int saved_x = it->current_x;
18573 int saved_face_id = it->face_id;
18574 int saved_box_end = it->end_of_box_run_p;
18575 struct text_pos saved_pos;
18576 Lisp_Object saved_object;
18577 struct face *face;
18578
18579 saved_object = it->object;
18580 saved_pos = it->position;
18581
18582 it->what = IT_CHARACTER;
18583 memset (&it->position, 0, sizeof it->position);
18584 it->object = make_number (0);
18585 it->c = it->char_to_display = ' ';
18586 it->len = 1;
18587
18588 /* If the default face was remapped, be sure to use the
18589 remapped face for the appended newline. */
18590 if (default_face_p)
18591 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18592 else if (it->face_before_selective_p)
18593 it->face_id = it->saved_face_id;
18594 face = FACE_FROM_ID (it->f, it->face_id);
18595 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18596 /* In R2L rows, we will prepend a stretch glyph that will
18597 have the end_of_box_run_p flag set for it, so there's no
18598 need for the appended newline glyph to have that flag
18599 set. */
18600 if (it->glyph_row->reversed_p
18601 /* But if the appended newline glyph goes all the way to
18602 the end of the row, there will be no stretch glyph,
18603 so leave the box flag set. */
18604 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18605 it->end_of_box_run_p = 0;
18606
18607 PRODUCE_GLYPHS (it);
18608
18609 it->override_ascent = -1;
18610 it->constrain_row_ascent_descent_p = 0;
18611 it->current_x = saved_x;
18612 it->object = saved_object;
18613 it->position = saved_pos;
18614 it->what = saved_what;
18615 it->face_id = saved_face_id;
18616 it->len = saved_len;
18617 it->c = saved_c;
18618 it->char_to_display = saved_char_to_display;
18619 it->end_of_box_run_p = saved_box_end;
18620 return 1;
18621 }
18622 }
18623
18624 return 0;
18625 }
18626
18627
18628 /* Extend the face of the last glyph in the text area of IT->glyph_row
18629 to the end of the display line. Called from display_line. If the
18630 glyph row is empty, add a space glyph to it so that we know the
18631 face to draw. Set the glyph row flag fill_line_p. If the glyph
18632 row is R2L, prepend a stretch glyph to cover the empty space to the
18633 left of the leftmost glyph. */
18634
18635 static void
18636 extend_face_to_end_of_line (struct it *it)
18637 {
18638 struct face *face, *default_face;
18639 struct frame *f = it->f;
18640
18641 /* If line is already filled, do nothing. Non window-system frames
18642 get a grace of one more ``pixel'' because their characters are
18643 1-``pixel'' wide, so they hit the equality too early. This grace
18644 is needed only for R2L rows that are not continued, to produce
18645 one extra blank where we could display the cursor. */
18646 if (it->current_x >= it->last_visible_x
18647 + (!FRAME_WINDOW_P (f)
18648 && it->glyph_row->reversed_p
18649 && !it->glyph_row->continued_p))
18650 return;
18651
18652 /* The default face, possibly remapped. */
18653 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18654
18655 /* Face extension extends the background and box of IT->face_id
18656 to the end of the line. If the background equals the background
18657 of the frame, we don't have to do anything. */
18658 if (it->face_before_selective_p)
18659 face = FACE_FROM_ID (f, it->saved_face_id);
18660 else
18661 face = FACE_FROM_ID (f, it->face_id);
18662
18663 if (FRAME_WINDOW_P (f)
18664 && it->glyph_row->displays_text_p
18665 && face->box == FACE_NO_BOX
18666 && face->background == FRAME_BACKGROUND_PIXEL (f)
18667 && !face->stipple
18668 && !it->glyph_row->reversed_p)
18669 return;
18670
18671 /* Set the glyph row flag indicating that the face of the last glyph
18672 in the text area has to be drawn to the end of the text area. */
18673 it->glyph_row->fill_line_p = 1;
18674
18675 /* If current character of IT is not ASCII, make sure we have the
18676 ASCII face. This will be automatically undone the next time
18677 get_next_display_element returns a multibyte character. Note
18678 that the character will always be single byte in unibyte
18679 text. */
18680 if (!ASCII_CHAR_P (it->c))
18681 {
18682 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18683 }
18684
18685 if (FRAME_WINDOW_P (f))
18686 {
18687 /* If the row is empty, add a space with the current face of IT,
18688 so that we know which face to draw. */
18689 if (it->glyph_row->used[TEXT_AREA] == 0)
18690 {
18691 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18692 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18693 it->glyph_row->used[TEXT_AREA] = 1;
18694 }
18695 #ifdef HAVE_WINDOW_SYSTEM
18696 if (it->glyph_row->reversed_p)
18697 {
18698 /* Prepend a stretch glyph to the row, such that the
18699 rightmost glyph will be drawn flushed all the way to the
18700 right margin of the window. The stretch glyph that will
18701 occupy the empty space, if any, to the left of the
18702 glyphs. */
18703 struct font *font = face->font ? face->font : FRAME_FONT (f);
18704 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18705 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18706 struct glyph *g;
18707 int row_width, stretch_ascent, stretch_width;
18708 struct text_pos saved_pos;
18709 int saved_face_id, saved_avoid_cursor, saved_box_start;
18710
18711 for (row_width = 0, g = row_start; g < row_end; g++)
18712 row_width += g->pixel_width;
18713 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18714 if (stretch_width > 0)
18715 {
18716 stretch_ascent =
18717 (((it->ascent + it->descent)
18718 * FONT_BASE (font)) / FONT_HEIGHT (font));
18719 saved_pos = it->position;
18720 memset (&it->position, 0, sizeof it->position);
18721 saved_avoid_cursor = it->avoid_cursor_p;
18722 it->avoid_cursor_p = 1;
18723 saved_face_id = it->face_id;
18724 saved_box_start = it->start_of_box_run_p;
18725 /* The last row's stretch glyph should get the default
18726 face, to avoid painting the rest of the window with
18727 the region face, if the region ends at ZV. */
18728 if (it->glyph_row->ends_at_zv_p)
18729 it->face_id = default_face->id;
18730 else
18731 it->face_id = face->id;
18732 it->start_of_box_run_p = 0;
18733 append_stretch_glyph (it, make_number (0), stretch_width,
18734 it->ascent + it->descent, stretch_ascent);
18735 it->position = saved_pos;
18736 it->avoid_cursor_p = saved_avoid_cursor;
18737 it->face_id = saved_face_id;
18738 it->start_of_box_run_p = saved_box_start;
18739 }
18740 }
18741 #endif /* HAVE_WINDOW_SYSTEM */
18742 }
18743 else
18744 {
18745 /* Save some values that must not be changed. */
18746 int saved_x = it->current_x;
18747 struct text_pos saved_pos;
18748 Lisp_Object saved_object;
18749 enum display_element_type saved_what = it->what;
18750 int saved_face_id = it->face_id;
18751
18752 saved_object = it->object;
18753 saved_pos = it->position;
18754
18755 it->what = IT_CHARACTER;
18756 memset (&it->position, 0, sizeof it->position);
18757 it->object = make_number (0);
18758 it->c = it->char_to_display = ' ';
18759 it->len = 1;
18760 /* The last row's blank glyphs should get the default face, to
18761 avoid painting the rest of the window with the region face,
18762 if the region ends at ZV. */
18763 if (it->glyph_row->ends_at_zv_p)
18764 it->face_id = default_face->id;
18765 else
18766 it->face_id = face->id;
18767
18768 PRODUCE_GLYPHS (it);
18769
18770 while (it->current_x <= it->last_visible_x)
18771 PRODUCE_GLYPHS (it);
18772
18773 /* Don't count these blanks really. It would let us insert a left
18774 truncation glyph below and make us set the cursor on them, maybe. */
18775 it->current_x = saved_x;
18776 it->object = saved_object;
18777 it->position = saved_pos;
18778 it->what = saved_what;
18779 it->face_id = saved_face_id;
18780 }
18781 }
18782
18783
18784 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18785 trailing whitespace. */
18786
18787 static int
18788 trailing_whitespace_p (ptrdiff_t charpos)
18789 {
18790 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18791 int c = 0;
18792
18793 while (bytepos < ZV_BYTE
18794 && (c = FETCH_CHAR (bytepos),
18795 c == ' ' || c == '\t'))
18796 ++bytepos;
18797
18798 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18799 {
18800 if (bytepos != PT_BYTE)
18801 return 1;
18802 }
18803 return 0;
18804 }
18805
18806
18807 /* Highlight trailing whitespace, if any, in ROW. */
18808
18809 static void
18810 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18811 {
18812 int used = row->used[TEXT_AREA];
18813
18814 if (used)
18815 {
18816 struct glyph *start = row->glyphs[TEXT_AREA];
18817 struct glyph *glyph = start + used - 1;
18818
18819 if (row->reversed_p)
18820 {
18821 /* Right-to-left rows need to be processed in the opposite
18822 direction, so swap the edge pointers. */
18823 glyph = start;
18824 start = row->glyphs[TEXT_AREA] + used - 1;
18825 }
18826
18827 /* Skip over glyphs inserted to display the cursor at the
18828 end of a line, for extending the face of the last glyph
18829 to the end of the line on terminals, and for truncation
18830 and continuation glyphs. */
18831 if (!row->reversed_p)
18832 {
18833 while (glyph >= start
18834 && glyph->type == CHAR_GLYPH
18835 && INTEGERP (glyph->object))
18836 --glyph;
18837 }
18838 else
18839 {
18840 while (glyph <= start
18841 && glyph->type == CHAR_GLYPH
18842 && INTEGERP (glyph->object))
18843 ++glyph;
18844 }
18845
18846 /* If last glyph is a space or stretch, and it's trailing
18847 whitespace, set the face of all trailing whitespace glyphs in
18848 IT->glyph_row to `trailing-whitespace'. */
18849 if ((row->reversed_p ? glyph <= start : glyph >= start)
18850 && BUFFERP (glyph->object)
18851 && (glyph->type == STRETCH_GLYPH
18852 || (glyph->type == CHAR_GLYPH
18853 && glyph->u.ch == ' '))
18854 && trailing_whitespace_p (glyph->charpos))
18855 {
18856 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18857 if (face_id < 0)
18858 return;
18859
18860 if (!row->reversed_p)
18861 {
18862 while (glyph >= start
18863 && BUFFERP (glyph->object)
18864 && (glyph->type == STRETCH_GLYPH
18865 || (glyph->type == CHAR_GLYPH
18866 && glyph->u.ch == ' ')))
18867 (glyph--)->face_id = face_id;
18868 }
18869 else
18870 {
18871 while (glyph <= start
18872 && BUFFERP (glyph->object)
18873 && (glyph->type == STRETCH_GLYPH
18874 || (glyph->type == CHAR_GLYPH
18875 && glyph->u.ch == ' ')))
18876 (glyph++)->face_id = face_id;
18877 }
18878 }
18879 }
18880 }
18881
18882
18883 /* Value is non-zero if glyph row ROW should be
18884 used to hold the cursor. */
18885
18886 static int
18887 cursor_row_p (struct glyph_row *row)
18888 {
18889 int result = 1;
18890
18891 if (PT == CHARPOS (row->end.pos)
18892 || PT == MATRIX_ROW_END_CHARPOS (row))
18893 {
18894 /* Suppose the row ends on a string.
18895 Unless the row is continued, that means it ends on a newline
18896 in the string. If it's anything other than a display string
18897 (e.g., a before-string from an overlay), we don't want the
18898 cursor there. (This heuristic seems to give the optimal
18899 behavior for the various types of multi-line strings.)
18900 One exception: if the string has `cursor' property on one of
18901 its characters, we _do_ want the cursor there. */
18902 if (CHARPOS (row->end.string_pos) >= 0)
18903 {
18904 if (row->continued_p)
18905 result = 1;
18906 else
18907 {
18908 /* Check for `display' property. */
18909 struct glyph *beg = row->glyphs[TEXT_AREA];
18910 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18911 struct glyph *glyph;
18912
18913 result = 0;
18914 for (glyph = end; glyph >= beg; --glyph)
18915 if (STRINGP (glyph->object))
18916 {
18917 Lisp_Object prop
18918 = Fget_char_property (make_number (PT),
18919 Qdisplay, Qnil);
18920 result =
18921 (!NILP (prop)
18922 && display_prop_string_p (prop, glyph->object));
18923 /* If there's a `cursor' property on one of the
18924 string's characters, this row is a cursor row,
18925 even though this is not a display string. */
18926 if (!result)
18927 {
18928 Lisp_Object s = glyph->object;
18929
18930 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18931 {
18932 ptrdiff_t gpos = glyph->charpos;
18933
18934 if (!NILP (Fget_char_property (make_number (gpos),
18935 Qcursor, s)))
18936 {
18937 result = 1;
18938 break;
18939 }
18940 }
18941 }
18942 break;
18943 }
18944 }
18945 }
18946 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18947 {
18948 /* If the row ends in middle of a real character,
18949 and the line is continued, we want the cursor here.
18950 That's because CHARPOS (ROW->end.pos) would equal
18951 PT if PT is before the character. */
18952 if (!row->ends_in_ellipsis_p)
18953 result = row->continued_p;
18954 else
18955 /* If the row ends in an ellipsis, then
18956 CHARPOS (ROW->end.pos) will equal point after the
18957 invisible text. We want that position to be displayed
18958 after the ellipsis. */
18959 result = 0;
18960 }
18961 /* If the row ends at ZV, display the cursor at the end of that
18962 row instead of at the start of the row below. */
18963 else if (row->ends_at_zv_p)
18964 result = 1;
18965 else
18966 result = 0;
18967 }
18968
18969 return result;
18970 }
18971
18972 \f
18973
18974 /* Push the property PROP so that it will be rendered at the current
18975 position in IT. Return 1 if PROP was successfully pushed, 0
18976 otherwise. Called from handle_line_prefix to handle the
18977 `line-prefix' and `wrap-prefix' properties. */
18978
18979 static int
18980 push_prefix_prop (struct it *it, Lisp_Object prop)
18981 {
18982 struct text_pos pos =
18983 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18984
18985 eassert (it->method == GET_FROM_BUFFER
18986 || it->method == GET_FROM_DISPLAY_VECTOR
18987 || it->method == GET_FROM_STRING);
18988
18989 /* We need to save the current buffer/string position, so it will be
18990 restored by pop_it, because iterate_out_of_display_property
18991 depends on that being set correctly, but some situations leave
18992 it->position not yet set when this function is called. */
18993 push_it (it, &pos);
18994
18995 if (STRINGP (prop))
18996 {
18997 if (SCHARS (prop) == 0)
18998 {
18999 pop_it (it);
19000 return 0;
19001 }
19002
19003 it->string = prop;
19004 it->string_from_prefix_prop_p = 1;
19005 it->multibyte_p = STRING_MULTIBYTE (it->string);
19006 it->current.overlay_string_index = -1;
19007 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19008 it->end_charpos = it->string_nchars = SCHARS (it->string);
19009 it->method = GET_FROM_STRING;
19010 it->stop_charpos = 0;
19011 it->prev_stop = 0;
19012 it->base_level_stop = 0;
19013
19014 /* Force paragraph direction to be that of the parent
19015 buffer/string. */
19016 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19017 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19018 else
19019 it->paragraph_embedding = L2R;
19020
19021 /* Set up the bidi iterator for this display string. */
19022 if (it->bidi_p)
19023 {
19024 it->bidi_it.string.lstring = it->string;
19025 it->bidi_it.string.s = NULL;
19026 it->bidi_it.string.schars = it->end_charpos;
19027 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19028 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19029 it->bidi_it.string.unibyte = !it->multibyte_p;
19030 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19031 }
19032 }
19033 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19034 {
19035 it->method = GET_FROM_STRETCH;
19036 it->object = prop;
19037 }
19038 #ifdef HAVE_WINDOW_SYSTEM
19039 else if (IMAGEP (prop))
19040 {
19041 it->what = IT_IMAGE;
19042 it->image_id = lookup_image (it->f, prop);
19043 it->method = GET_FROM_IMAGE;
19044 }
19045 #endif /* HAVE_WINDOW_SYSTEM */
19046 else
19047 {
19048 pop_it (it); /* bogus display property, give up */
19049 return 0;
19050 }
19051
19052 return 1;
19053 }
19054
19055 /* Return the character-property PROP at the current position in IT. */
19056
19057 static Lisp_Object
19058 get_it_property (struct it *it, Lisp_Object prop)
19059 {
19060 Lisp_Object position;
19061
19062 if (STRINGP (it->object))
19063 position = make_number (IT_STRING_CHARPOS (*it));
19064 else if (BUFFERP (it->object))
19065 position = make_number (IT_CHARPOS (*it));
19066 else
19067 return Qnil;
19068
19069 return Fget_char_property (position, prop, it->object);
19070 }
19071
19072 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19073
19074 static void
19075 handle_line_prefix (struct it *it)
19076 {
19077 Lisp_Object prefix;
19078
19079 if (it->continuation_lines_width > 0)
19080 {
19081 prefix = get_it_property (it, Qwrap_prefix);
19082 if (NILP (prefix))
19083 prefix = Vwrap_prefix;
19084 }
19085 else
19086 {
19087 prefix = get_it_property (it, Qline_prefix);
19088 if (NILP (prefix))
19089 prefix = Vline_prefix;
19090 }
19091 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19092 {
19093 /* If the prefix is wider than the window, and we try to wrap
19094 it, it would acquire its own wrap prefix, and so on till the
19095 iterator stack overflows. So, don't wrap the prefix. */
19096 it->line_wrap = TRUNCATE;
19097 it->avoid_cursor_p = 1;
19098 }
19099 }
19100
19101 \f
19102
19103 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19104 only for R2L lines from display_line and display_string, when they
19105 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19106 the line/string needs to be continued on the next glyph row. */
19107 static void
19108 unproduce_glyphs (struct it *it, int n)
19109 {
19110 struct glyph *glyph, *end;
19111
19112 eassert (it->glyph_row);
19113 eassert (it->glyph_row->reversed_p);
19114 eassert (it->area == TEXT_AREA);
19115 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19116
19117 if (n > it->glyph_row->used[TEXT_AREA])
19118 n = it->glyph_row->used[TEXT_AREA];
19119 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19120 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19121 for ( ; glyph < end; glyph++)
19122 glyph[-n] = *glyph;
19123 }
19124
19125 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19126 and ROW->maxpos. */
19127 static void
19128 find_row_edges (struct it *it, struct glyph_row *row,
19129 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19130 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19131 {
19132 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19133 lines' rows is implemented for bidi-reordered rows. */
19134
19135 /* ROW->minpos is the value of min_pos, the minimal buffer position
19136 we have in ROW, or ROW->start.pos if that is smaller. */
19137 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19138 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19139 else
19140 /* We didn't find buffer positions smaller than ROW->start, or
19141 didn't find _any_ valid buffer positions in any of the glyphs,
19142 so we must trust the iterator's computed positions. */
19143 row->minpos = row->start.pos;
19144 if (max_pos <= 0)
19145 {
19146 max_pos = CHARPOS (it->current.pos);
19147 max_bpos = BYTEPOS (it->current.pos);
19148 }
19149
19150 /* Here are the various use-cases for ending the row, and the
19151 corresponding values for ROW->maxpos:
19152
19153 Line ends in a newline from buffer eol_pos + 1
19154 Line is continued from buffer max_pos + 1
19155 Line is truncated on right it->current.pos
19156 Line ends in a newline from string max_pos + 1(*)
19157 (*) + 1 only when line ends in a forward scan
19158 Line is continued from string max_pos
19159 Line is continued from display vector max_pos
19160 Line is entirely from a string min_pos == max_pos
19161 Line is entirely from a display vector min_pos == max_pos
19162 Line that ends at ZV ZV
19163
19164 If you discover other use-cases, please add them here as
19165 appropriate. */
19166 if (row->ends_at_zv_p)
19167 row->maxpos = it->current.pos;
19168 else if (row->used[TEXT_AREA])
19169 {
19170 int seen_this_string = 0;
19171 struct glyph_row *r1 = row - 1;
19172
19173 /* Did we see the same display string on the previous row? */
19174 if (STRINGP (it->object)
19175 /* this is not the first row */
19176 && row > it->w->desired_matrix->rows
19177 /* previous row is not the header line */
19178 && !r1->mode_line_p
19179 /* previous row also ends in a newline from a string */
19180 && r1->ends_in_newline_from_string_p)
19181 {
19182 struct glyph *start, *end;
19183
19184 /* Search for the last glyph of the previous row that came
19185 from buffer or string. Depending on whether the row is
19186 L2R or R2L, we need to process it front to back or the
19187 other way round. */
19188 if (!r1->reversed_p)
19189 {
19190 start = r1->glyphs[TEXT_AREA];
19191 end = start + r1->used[TEXT_AREA];
19192 /* Glyphs inserted by redisplay have an integer (zero)
19193 as their object. */
19194 while (end > start
19195 && INTEGERP ((end - 1)->object)
19196 && (end - 1)->charpos <= 0)
19197 --end;
19198 if (end > start)
19199 {
19200 if (EQ ((end - 1)->object, it->object))
19201 seen_this_string = 1;
19202 }
19203 else
19204 /* If all the glyphs of the previous row were inserted
19205 by redisplay, it means the previous row was
19206 produced from a single newline, which is only
19207 possible if that newline came from the same string
19208 as the one which produced this ROW. */
19209 seen_this_string = 1;
19210 }
19211 else
19212 {
19213 end = r1->glyphs[TEXT_AREA] - 1;
19214 start = end + r1->used[TEXT_AREA];
19215 while (end < start
19216 && INTEGERP ((end + 1)->object)
19217 && (end + 1)->charpos <= 0)
19218 ++end;
19219 if (end < start)
19220 {
19221 if (EQ ((end + 1)->object, it->object))
19222 seen_this_string = 1;
19223 }
19224 else
19225 seen_this_string = 1;
19226 }
19227 }
19228 /* Take note of each display string that covers a newline only
19229 once, the first time we see it. This is for when a display
19230 string includes more than one newline in it. */
19231 if (row->ends_in_newline_from_string_p && !seen_this_string)
19232 {
19233 /* If we were scanning the buffer forward when we displayed
19234 the string, we want to account for at least one buffer
19235 position that belongs to this row (position covered by
19236 the display string), so that cursor positioning will
19237 consider this row as a candidate when point is at the end
19238 of the visual line represented by this row. This is not
19239 required when scanning back, because max_pos will already
19240 have a much larger value. */
19241 if (CHARPOS (row->end.pos) > max_pos)
19242 INC_BOTH (max_pos, max_bpos);
19243 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19244 }
19245 else if (CHARPOS (it->eol_pos) > 0)
19246 SET_TEXT_POS (row->maxpos,
19247 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19248 else if (row->continued_p)
19249 {
19250 /* If max_pos is different from IT's current position, it
19251 means IT->method does not belong to the display element
19252 at max_pos. However, it also means that the display
19253 element at max_pos was displayed in its entirety on this
19254 line, which is equivalent to saying that the next line
19255 starts at the next buffer position. */
19256 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19257 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19258 else
19259 {
19260 INC_BOTH (max_pos, max_bpos);
19261 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19262 }
19263 }
19264 else if (row->truncated_on_right_p)
19265 /* display_line already called reseat_at_next_visible_line_start,
19266 which puts the iterator at the beginning of the next line, in
19267 the logical order. */
19268 row->maxpos = it->current.pos;
19269 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19270 /* A line that is entirely from a string/image/stretch... */
19271 row->maxpos = row->minpos;
19272 else
19273 emacs_abort ();
19274 }
19275 else
19276 row->maxpos = it->current.pos;
19277 }
19278
19279 /* Construct the glyph row IT->glyph_row in the desired matrix of
19280 IT->w from text at the current position of IT. See dispextern.h
19281 for an overview of struct it. Value is non-zero if
19282 IT->glyph_row displays text, as opposed to a line displaying ZV
19283 only. */
19284
19285 static int
19286 display_line (struct it *it)
19287 {
19288 struct glyph_row *row = it->glyph_row;
19289 Lisp_Object overlay_arrow_string;
19290 struct it wrap_it;
19291 void *wrap_data = NULL;
19292 int may_wrap = 0, wrap_x IF_LINT (= 0);
19293 int wrap_row_used = -1;
19294 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19295 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19296 int wrap_row_extra_line_spacing IF_LINT (= 0);
19297 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19298 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19299 int cvpos;
19300 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19301 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19302
19303 /* We always start displaying at hpos zero even if hscrolled. */
19304 eassert (it->hpos == 0 && it->current_x == 0);
19305
19306 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19307 >= it->w->desired_matrix->nrows)
19308 {
19309 it->w->nrows_scale_factor++;
19310 fonts_changed_p = 1;
19311 return 0;
19312 }
19313
19314 /* Is IT->w showing the region? */
19315 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19316
19317 /* Clear the result glyph row and enable it. */
19318 prepare_desired_row (row);
19319
19320 row->y = it->current_y;
19321 row->start = it->start;
19322 row->continuation_lines_width = it->continuation_lines_width;
19323 row->displays_text_p = 1;
19324 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19325 it->starts_in_middle_of_char_p = 0;
19326
19327 /* Arrange the overlays nicely for our purposes. Usually, we call
19328 display_line on only one line at a time, in which case this
19329 can't really hurt too much, or we call it on lines which appear
19330 one after another in the buffer, in which case all calls to
19331 recenter_overlay_lists but the first will be pretty cheap. */
19332 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19333
19334 /* Move over display elements that are not visible because we are
19335 hscrolled. This may stop at an x-position < IT->first_visible_x
19336 if the first glyph is partially visible or if we hit a line end. */
19337 if (it->current_x < it->first_visible_x)
19338 {
19339 enum move_it_result move_result;
19340
19341 this_line_min_pos = row->start.pos;
19342 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19343 MOVE_TO_POS | MOVE_TO_X);
19344 /* If we are under a large hscroll, move_it_in_display_line_to
19345 could hit the end of the line without reaching
19346 it->first_visible_x. Pretend that we did reach it. This is
19347 especially important on a TTY, where we will call
19348 extend_face_to_end_of_line, which needs to know how many
19349 blank glyphs to produce. */
19350 if (it->current_x < it->first_visible_x
19351 && (move_result == MOVE_NEWLINE_OR_CR
19352 || move_result == MOVE_POS_MATCH_OR_ZV))
19353 it->current_x = it->first_visible_x;
19354
19355 /* Record the smallest positions seen while we moved over
19356 display elements that are not visible. This is needed by
19357 redisplay_internal for optimizing the case where the cursor
19358 stays inside the same line. The rest of this function only
19359 considers positions that are actually displayed, so
19360 RECORD_MAX_MIN_POS will not otherwise record positions that
19361 are hscrolled to the left of the left edge of the window. */
19362 min_pos = CHARPOS (this_line_min_pos);
19363 min_bpos = BYTEPOS (this_line_min_pos);
19364 }
19365 else
19366 {
19367 /* We only do this when not calling `move_it_in_display_line_to'
19368 above, because move_it_in_display_line_to calls
19369 handle_line_prefix itself. */
19370 handle_line_prefix (it);
19371 }
19372
19373 /* Get the initial row height. This is either the height of the
19374 text hscrolled, if there is any, or zero. */
19375 row->ascent = it->max_ascent;
19376 row->height = it->max_ascent + it->max_descent;
19377 row->phys_ascent = it->max_phys_ascent;
19378 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19379 row->extra_line_spacing = it->max_extra_line_spacing;
19380
19381 /* Utility macro to record max and min buffer positions seen until now. */
19382 #define RECORD_MAX_MIN_POS(IT) \
19383 do \
19384 { \
19385 int composition_p = !STRINGP ((IT)->string) \
19386 && ((IT)->what == IT_COMPOSITION); \
19387 ptrdiff_t current_pos = \
19388 composition_p ? (IT)->cmp_it.charpos \
19389 : IT_CHARPOS (*(IT)); \
19390 ptrdiff_t current_bpos = \
19391 composition_p ? CHAR_TO_BYTE (current_pos) \
19392 : IT_BYTEPOS (*(IT)); \
19393 if (current_pos < min_pos) \
19394 { \
19395 min_pos = current_pos; \
19396 min_bpos = current_bpos; \
19397 } \
19398 if (IT_CHARPOS (*it) > max_pos) \
19399 { \
19400 max_pos = IT_CHARPOS (*it); \
19401 max_bpos = IT_BYTEPOS (*it); \
19402 } \
19403 } \
19404 while (0)
19405
19406 /* Loop generating characters. The loop is left with IT on the next
19407 character to display. */
19408 while (1)
19409 {
19410 int n_glyphs_before, hpos_before, x_before;
19411 int x, nglyphs;
19412 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19413
19414 /* Retrieve the next thing to display. Value is zero if end of
19415 buffer reached. */
19416 if (!get_next_display_element (it))
19417 {
19418 /* Maybe add a space at the end of this line that is used to
19419 display the cursor there under X. Set the charpos of the
19420 first glyph of blank lines not corresponding to any text
19421 to -1. */
19422 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19423 row->exact_window_width_line_p = 1;
19424 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19425 || row->used[TEXT_AREA] == 0)
19426 {
19427 row->glyphs[TEXT_AREA]->charpos = -1;
19428 row->displays_text_p = 0;
19429
19430 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19431 && (!MINI_WINDOW_P (it->w)
19432 || (minibuf_level && EQ (it->window, minibuf_window))))
19433 row->indicate_empty_line_p = 1;
19434 }
19435
19436 it->continuation_lines_width = 0;
19437 row->ends_at_zv_p = 1;
19438 /* A row that displays right-to-left text must always have
19439 its last face extended all the way to the end of line,
19440 even if this row ends in ZV, because we still write to
19441 the screen left to right. We also need to extend the
19442 last face if the default face is remapped to some
19443 different face, otherwise the functions that clear
19444 portions of the screen will clear with the default face's
19445 background color. */
19446 if (row->reversed_p
19447 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19448 extend_face_to_end_of_line (it);
19449 break;
19450 }
19451
19452 /* Now, get the metrics of what we want to display. This also
19453 generates glyphs in `row' (which is IT->glyph_row). */
19454 n_glyphs_before = row->used[TEXT_AREA];
19455 x = it->current_x;
19456
19457 /* Remember the line height so far in case the next element doesn't
19458 fit on the line. */
19459 if (it->line_wrap != TRUNCATE)
19460 {
19461 ascent = it->max_ascent;
19462 descent = it->max_descent;
19463 phys_ascent = it->max_phys_ascent;
19464 phys_descent = it->max_phys_descent;
19465
19466 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19467 {
19468 if (IT_DISPLAYING_WHITESPACE (it))
19469 may_wrap = 1;
19470 else if (may_wrap)
19471 {
19472 SAVE_IT (wrap_it, *it, wrap_data);
19473 wrap_x = x;
19474 wrap_row_used = row->used[TEXT_AREA];
19475 wrap_row_ascent = row->ascent;
19476 wrap_row_height = row->height;
19477 wrap_row_phys_ascent = row->phys_ascent;
19478 wrap_row_phys_height = row->phys_height;
19479 wrap_row_extra_line_spacing = row->extra_line_spacing;
19480 wrap_row_min_pos = min_pos;
19481 wrap_row_min_bpos = min_bpos;
19482 wrap_row_max_pos = max_pos;
19483 wrap_row_max_bpos = max_bpos;
19484 may_wrap = 0;
19485 }
19486 }
19487 }
19488
19489 PRODUCE_GLYPHS (it);
19490
19491 /* If this display element was in marginal areas, continue with
19492 the next one. */
19493 if (it->area != TEXT_AREA)
19494 {
19495 row->ascent = max (row->ascent, it->max_ascent);
19496 row->height = max (row->height, it->max_ascent + it->max_descent);
19497 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19498 row->phys_height = max (row->phys_height,
19499 it->max_phys_ascent + it->max_phys_descent);
19500 row->extra_line_spacing = max (row->extra_line_spacing,
19501 it->max_extra_line_spacing);
19502 set_iterator_to_next (it, 1);
19503 continue;
19504 }
19505
19506 /* Does the display element fit on the line? If we truncate
19507 lines, we should draw past the right edge of the window. If
19508 we don't truncate, we want to stop so that we can display the
19509 continuation glyph before the right margin. If lines are
19510 continued, there are two possible strategies for characters
19511 resulting in more than 1 glyph (e.g. tabs): Display as many
19512 glyphs as possible in this line and leave the rest for the
19513 continuation line, or display the whole element in the next
19514 line. Original redisplay did the former, so we do it also. */
19515 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19516 hpos_before = it->hpos;
19517 x_before = x;
19518
19519 if (/* Not a newline. */
19520 nglyphs > 0
19521 /* Glyphs produced fit entirely in the line. */
19522 && it->current_x < it->last_visible_x)
19523 {
19524 it->hpos += nglyphs;
19525 row->ascent = max (row->ascent, it->max_ascent);
19526 row->height = max (row->height, it->max_ascent + it->max_descent);
19527 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19528 row->phys_height = max (row->phys_height,
19529 it->max_phys_ascent + it->max_phys_descent);
19530 row->extra_line_spacing = max (row->extra_line_spacing,
19531 it->max_extra_line_spacing);
19532 if (it->current_x - it->pixel_width < it->first_visible_x)
19533 row->x = x - it->first_visible_x;
19534 /* Record the maximum and minimum buffer positions seen so
19535 far in glyphs that will be displayed by this row. */
19536 if (it->bidi_p)
19537 RECORD_MAX_MIN_POS (it);
19538 }
19539 else
19540 {
19541 int i, new_x;
19542 struct glyph *glyph;
19543
19544 for (i = 0; i < nglyphs; ++i, x = new_x)
19545 {
19546 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19547 new_x = x + glyph->pixel_width;
19548
19549 if (/* Lines are continued. */
19550 it->line_wrap != TRUNCATE
19551 && (/* Glyph doesn't fit on the line. */
19552 new_x > it->last_visible_x
19553 /* Or it fits exactly on a window system frame. */
19554 || (new_x == it->last_visible_x
19555 && FRAME_WINDOW_P (it->f)
19556 && (row->reversed_p
19557 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19558 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19559 {
19560 /* End of a continued line. */
19561
19562 if (it->hpos == 0
19563 || (new_x == it->last_visible_x
19564 && FRAME_WINDOW_P (it->f)
19565 && (row->reversed_p
19566 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19567 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19568 {
19569 /* Current glyph is the only one on the line or
19570 fits exactly on the line. We must continue
19571 the line because we can't draw the cursor
19572 after the glyph. */
19573 row->continued_p = 1;
19574 it->current_x = new_x;
19575 it->continuation_lines_width += new_x;
19576 ++it->hpos;
19577 if (i == nglyphs - 1)
19578 {
19579 /* If line-wrap is on, check if a previous
19580 wrap point was found. */
19581 if (wrap_row_used > 0
19582 /* Even if there is a previous wrap
19583 point, continue the line here as
19584 usual, if (i) the previous character
19585 was a space or tab AND (ii) the
19586 current character is not. */
19587 && (!may_wrap
19588 || IT_DISPLAYING_WHITESPACE (it)))
19589 goto back_to_wrap;
19590
19591 /* Record the maximum and minimum buffer
19592 positions seen so far in glyphs that will be
19593 displayed by this row. */
19594 if (it->bidi_p)
19595 RECORD_MAX_MIN_POS (it);
19596 set_iterator_to_next (it, 1);
19597 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19598 {
19599 if (!get_next_display_element (it))
19600 {
19601 row->exact_window_width_line_p = 1;
19602 it->continuation_lines_width = 0;
19603 row->continued_p = 0;
19604 row->ends_at_zv_p = 1;
19605 }
19606 else if (ITERATOR_AT_END_OF_LINE_P (it))
19607 {
19608 row->continued_p = 0;
19609 row->exact_window_width_line_p = 1;
19610 }
19611 }
19612 }
19613 else if (it->bidi_p)
19614 RECORD_MAX_MIN_POS (it);
19615 }
19616 else if (CHAR_GLYPH_PADDING_P (*glyph)
19617 && !FRAME_WINDOW_P (it->f))
19618 {
19619 /* A padding glyph that doesn't fit on this line.
19620 This means the whole character doesn't fit
19621 on the line. */
19622 if (row->reversed_p)
19623 unproduce_glyphs (it, row->used[TEXT_AREA]
19624 - n_glyphs_before);
19625 row->used[TEXT_AREA] = n_glyphs_before;
19626
19627 /* Fill the rest of the row with continuation
19628 glyphs like in 20.x. */
19629 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19630 < row->glyphs[1 + TEXT_AREA])
19631 produce_special_glyphs (it, IT_CONTINUATION);
19632
19633 row->continued_p = 1;
19634 it->current_x = x_before;
19635 it->continuation_lines_width += x_before;
19636
19637 /* Restore the height to what it was before the
19638 element not fitting on the line. */
19639 it->max_ascent = ascent;
19640 it->max_descent = descent;
19641 it->max_phys_ascent = phys_ascent;
19642 it->max_phys_descent = phys_descent;
19643 }
19644 else if (wrap_row_used > 0)
19645 {
19646 back_to_wrap:
19647 if (row->reversed_p)
19648 unproduce_glyphs (it,
19649 row->used[TEXT_AREA] - wrap_row_used);
19650 RESTORE_IT (it, &wrap_it, wrap_data);
19651 it->continuation_lines_width += wrap_x;
19652 row->used[TEXT_AREA] = wrap_row_used;
19653 row->ascent = wrap_row_ascent;
19654 row->height = wrap_row_height;
19655 row->phys_ascent = wrap_row_phys_ascent;
19656 row->phys_height = wrap_row_phys_height;
19657 row->extra_line_spacing = wrap_row_extra_line_spacing;
19658 min_pos = wrap_row_min_pos;
19659 min_bpos = wrap_row_min_bpos;
19660 max_pos = wrap_row_max_pos;
19661 max_bpos = wrap_row_max_bpos;
19662 row->continued_p = 1;
19663 row->ends_at_zv_p = 0;
19664 row->exact_window_width_line_p = 0;
19665 it->continuation_lines_width += x;
19666
19667 /* Make sure that a non-default face is extended
19668 up to the right margin of the window. */
19669 extend_face_to_end_of_line (it);
19670 }
19671 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19672 {
19673 /* A TAB that extends past the right edge of the
19674 window. This produces a single glyph on
19675 window system frames. We leave the glyph in
19676 this row and let it fill the row, but don't
19677 consume the TAB. */
19678 if ((row->reversed_p
19679 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19680 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19681 produce_special_glyphs (it, IT_CONTINUATION);
19682 it->continuation_lines_width += it->last_visible_x;
19683 row->ends_in_middle_of_char_p = 1;
19684 row->continued_p = 1;
19685 glyph->pixel_width = it->last_visible_x - x;
19686 it->starts_in_middle_of_char_p = 1;
19687 }
19688 else
19689 {
19690 /* Something other than a TAB that draws past
19691 the right edge of the window. Restore
19692 positions to values before the element. */
19693 if (row->reversed_p)
19694 unproduce_glyphs (it, row->used[TEXT_AREA]
19695 - (n_glyphs_before + i));
19696 row->used[TEXT_AREA] = n_glyphs_before + i;
19697
19698 /* Display continuation glyphs. */
19699 it->current_x = x_before;
19700 it->continuation_lines_width += x;
19701 if (!FRAME_WINDOW_P (it->f)
19702 || (row->reversed_p
19703 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19704 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19705 produce_special_glyphs (it, IT_CONTINUATION);
19706 row->continued_p = 1;
19707
19708 extend_face_to_end_of_line (it);
19709
19710 if (nglyphs > 1 && i > 0)
19711 {
19712 row->ends_in_middle_of_char_p = 1;
19713 it->starts_in_middle_of_char_p = 1;
19714 }
19715
19716 /* Restore the height to what it was before the
19717 element not fitting on the line. */
19718 it->max_ascent = ascent;
19719 it->max_descent = descent;
19720 it->max_phys_ascent = phys_ascent;
19721 it->max_phys_descent = phys_descent;
19722 }
19723
19724 break;
19725 }
19726 else if (new_x > it->first_visible_x)
19727 {
19728 /* Increment number of glyphs actually displayed. */
19729 ++it->hpos;
19730
19731 /* Record the maximum and minimum buffer positions
19732 seen so far in glyphs that will be displayed by
19733 this row. */
19734 if (it->bidi_p)
19735 RECORD_MAX_MIN_POS (it);
19736
19737 if (x < it->first_visible_x)
19738 /* Glyph is partially visible, i.e. row starts at
19739 negative X position. */
19740 row->x = x - it->first_visible_x;
19741 }
19742 else
19743 {
19744 /* Glyph is completely off the left margin of the
19745 window. This should not happen because of the
19746 move_it_in_display_line at the start of this
19747 function, unless the text display area of the
19748 window is empty. */
19749 eassert (it->first_visible_x <= it->last_visible_x);
19750 }
19751 }
19752 /* Even if this display element produced no glyphs at all,
19753 we want to record its position. */
19754 if (it->bidi_p && nglyphs == 0)
19755 RECORD_MAX_MIN_POS (it);
19756
19757 row->ascent = max (row->ascent, it->max_ascent);
19758 row->height = max (row->height, it->max_ascent + it->max_descent);
19759 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19760 row->phys_height = max (row->phys_height,
19761 it->max_phys_ascent + it->max_phys_descent);
19762 row->extra_line_spacing = max (row->extra_line_spacing,
19763 it->max_extra_line_spacing);
19764
19765 /* End of this display line if row is continued. */
19766 if (row->continued_p || row->ends_at_zv_p)
19767 break;
19768 }
19769
19770 at_end_of_line:
19771 /* Is this a line end? If yes, we're also done, after making
19772 sure that a non-default face is extended up to the right
19773 margin of the window. */
19774 if (ITERATOR_AT_END_OF_LINE_P (it))
19775 {
19776 int used_before = row->used[TEXT_AREA];
19777
19778 row->ends_in_newline_from_string_p = STRINGP (it->object);
19779
19780 /* Add a space at the end of the line that is used to
19781 display the cursor there. */
19782 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19783 append_space_for_newline (it, 0);
19784
19785 /* Extend the face to the end of the line. */
19786 extend_face_to_end_of_line (it);
19787
19788 /* Make sure we have the position. */
19789 if (used_before == 0)
19790 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19791
19792 /* Record the position of the newline, for use in
19793 find_row_edges. */
19794 it->eol_pos = it->current.pos;
19795
19796 /* Consume the line end. This skips over invisible lines. */
19797 set_iterator_to_next (it, 1);
19798 it->continuation_lines_width = 0;
19799 break;
19800 }
19801
19802 /* Proceed with next display element. Note that this skips
19803 over lines invisible because of selective display. */
19804 set_iterator_to_next (it, 1);
19805
19806 /* If we truncate lines, we are done when the last displayed
19807 glyphs reach past the right margin of the window. */
19808 if (it->line_wrap == TRUNCATE
19809 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19810 ? (it->current_x >= it->last_visible_x)
19811 : (it->current_x > it->last_visible_x)))
19812 {
19813 /* Maybe add truncation glyphs. */
19814 if (!FRAME_WINDOW_P (it->f)
19815 || (row->reversed_p
19816 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19817 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19818 {
19819 int i, n;
19820
19821 if (!row->reversed_p)
19822 {
19823 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19824 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19825 break;
19826 }
19827 else
19828 {
19829 for (i = 0; i < row->used[TEXT_AREA]; i++)
19830 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19831 break;
19832 /* Remove any padding glyphs at the front of ROW, to
19833 make room for the truncation glyphs we will be
19834 adding below. The loop below always inserts at
19835 least one truncation glyph, so also remove the
19836 last glyph added to ROW. */
19837 unproduce_glyphs (it, i + 1);
19838 /* Adjust i for the loop below. */
19839 i = row->used[TEXT_AREA] - (i + 1);
19840 }
19841
19842 it->current_x = x_before;
19843 if (!FRAME_WINDOW_P (it->f))
19844 {
19845 for (n = row->used[TEXT_AREA]; i < n; ++i)
19846 {
19847 row->used[TEXT_AREA] = i;
19848 produce_special_glyphs (it, IT_TRUNCATION);
19849 }
19850 }
19851 else
19852 {
19853 row->used[TEXT_AREA] = i;
19854 produce_special_glyphs (it, IT_TRUNCATION);
19855 }
19856 }
19857 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19858 {
19859 /* Don't truncate if we can overflow newline into fringe. */
19860 if (!get_next_display_element (it))
19861 {
19862 it->continuation_lines_width = 0;
19863 row->ends_at_zv_p = 1;
19864 row->exact_window_width_line_p = 1;
19865 break;
19866 }
19867 if (ITERATOR_AT_END_OF_LINE_P (it))
19868 {
19869 row->exact_window_width_line_p = 1;
19870 goto at_end_of_line;
19871 }
19872 it->current_x = x_before;
19873 }
19874
19875 row->truncated_on_right_p = 1;
19876 it->continuation_lines_width = 0;
19877 reseat_at_next_visible_line_start (it, 0);
19878 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19879 it->hpos = hpos_before;
19880 break;
19881 }
19882 }
19883
19884 if (wrap_data)
19885 bidi_unshelve_cache (wrap_data, 1);
19886
19887 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19888 at the left window margin. */
19889 if (it->first_visible_x
19890 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19891 {
19892 if (!FRAME_WINDOW_P (it->f)
19893 || (row->reversed_p
19894 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19895 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19896 insert_left_trunc_glyphs (it);
19897 row->truncated_on_left_p = 1;
19898 }
19899
19900 /* Remember the position at which this line ends.
19901
19902 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19903 cannot be before the call to find_row_edges below, since that is
19904 where these positions are determined. */
19905 row->end = it->current;
19906 if (!it->bidi_p)
19907 {
19908 row->minpos = row->start.pos;
19909 row->maxpos = row->end.pos;
19910 }
19911 else
19912 {
19913 /* ROW->minpos and ROW->maxpos must be the smallest and
19914 `1 + the largest' buffer positions in ROW. But if ROW was
19915 bidi-reordered, these two positions can be anywhere in the
19916 row, so we must determine them now. */
19917 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19918 }
19919
19920 /* If the start of this line is the overlay arrow-position, then
19921 mark this glyph row as the one containing the overlay arrow.
19922 This is clearly a mess with variable size fonts. It would be
19923 better to let it be displayed like cursors under X. */
19924 if ((row->displays_text_p || !overlay_arrow_seen)
19925 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19926 !NILP (overlay_arrow_string)))
19927 {
19928 /* Overlay arrow in window redisplay is a fringe bitmap. */
19929 if (STRINGP (overlay_arrow_string))
19930 {
19931 struct glyph_row *arrow_row
19932 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19933 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19934 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19935 struct glyph *p = row->glyphs[TEXT_AREA];
19936 struct glyph *p2, *end;
19937
19938 /* Copy the arrow glyphs. */
19939 while (glyph < arrow_end)
19940 *p++ = *glyph++;
19941
19942 /* Throw away padding glyphs. */
19943 p2 = p;
19944 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19945 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19946 ++p2;
19947 if (p2 > p)
19948 {
19949 while (p2 < end)
19950 *p++ = *p2++;
19951 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19952 }
19953 }
19954 else
19955 {
19956 eassert (INTEGERP (overlay_arrow_string));
19957 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19958 }
19959 overlay_arrow_seen = 1;
19960 }
19961
19962 /* Highlight trailing whitespace. */
19963 if (!NILP (Vshow_trailing_whitespace))
19964 highlight_trailing_whitespace (it->f, it->glyph_row);
19965
19966 /* Compute pixel dimensions of this line. */
19967 compute_line_metrics (it);
19968
19969 /* Implementation note: No changes in the glyphs of ROW or in their
19970 faces can be done past this point, because compute_line_metrics
19971 computes ROW's hash value and stores it within the glyph_row
19972 structure. */
19973
19974 /* Record whether this row ends inside an ellipsis. */
19975 row->ends_in_ellipsis_p
19976 = (it->method == GET_FROM_DISPLAY_VECTOR
19977 && it->ellipsis_p);
19978
19979 /* Save fringe bitmaps in this row. */
19980 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19981 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19982 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19983 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19984
19985 it->left_user_fringe_bitmap = 0;
19986 it->left_user_fringe_face_id = 0;
19987 it->right_user_fringe_bitmap = 0;
19988 it->right_user_fringe_face_id = 0;
19989
19990 /* Maybe set the cursor. */
19991 cvpos = it->w->cursor.vpos;
19992 if ((cvpos < 0
19993 /* In bidi-reordered rows, keep checking for proper cursor
19994 position even if one has been found already, because buffer
19995 positions in such rows change non-linearly with ROW->VPOS,
19996 when a line is continued. One exception: when we are at ZV,
19997 display cursor on the first suitable glyph row, since all
19998 the empty rows after that also have their position set to ZV. */
19999 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20000 lines' rows is implemented for bidi-reordered rows. */
20001 || (it->bidi_p
20002 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20003 && PT >= MATRIX_ROW_START_CHARPOS (row)
20004 && PT <= MATRIX_ROW_END_CHARPOS (row)
20005 && cursor_row_p (row))
20006 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20007
20008 /* Prepare for the next line. This line starts horizontally at (X
20009 HPOS) = (0 0). Vertical positions are incremented. As a
20010 convenience for the caller, IT->glyph_row is set to the next
20011 row to be used. */
20012 it->current_x = it->hpos = 0;
20013 it->current_y += row->height;
20014 SET_TEXT_POS (it->eol_pos, 0, 0);
20015 ++it->vpos;
20016 ++it->glyph_row;
20017 /* The next row should by default use the same value of the
20018 reversed_p flag as this one. set_iterator_to_next decides when
20019 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20020 the flag accordingly. */
20021 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20022 it->glyph_row->reversed_p = row->reversed_p;
20023 it->start = row->end;
20024 return row->displays_text_p;
20025
20026 #undef RECORD_MAX_MIN_POS
20027 }
20028
20029 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20030 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20031 doc: /* Return paragraph direction at point in BUFFER.
20032 Value is either `left-to-right' or `right-to-left'.
20033 If BUFFER is omitted or nil, it defaults to the current buffer.
20034
20035 Paragraph direction determines how the text in the paragraph is displayed.
20036 In left-to-right paragraphs, text begins at the left margin of the window
20037 and the reading direction is generally left to right. In right-to-left
20038 paragraphs, text begins at the right margin and is read from right to left.
20039
20040 See also `bidi-paragraph-direction'. */)
20041 (Lisp_Object buffer)
20042 {
20043 struct buffer *buf = current_buffer;
20044 struct buffer *old = buf;
20045
20046 if (! NILP (buffer))
20047 {
20048 CHECK_BUFFER (buffer);
20049 buf = XBUFFER (buffer);
20050 }
20051
20052 if (NILP (BVAR (buf, bidi_display_reordering))
20053 || NILP (BVAR (buf, enable_multibyte_characters))
20054 /* When we are loading loadup.el, the character property tables
20055 needed for bidi iteration are not yet available. */
20056 || !NILP (Vpurify_flag))
20057 return Qleft_to_right;
20058 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20059 return BVAR (buf, bidi_paragraph_direction);
20060 else
20061 {
20062 /* Determine the direction from buffer text. We could try to
20063 use current_matrix if it is up to date, but this seems fast
20064 enough as it is. */
20065 struct bidi_it itb;
20066 ptrdiff_t pos = BUF_PT (buf);
20067 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20068 int c;
20069 void *itb_data = bidi_shelve_cache ();
20070
20071 set_buffer_temp (buf);
20072 /* bidi_paragraph_init finds the base direction of the paragraph
20073 by searching forward from paragraph start. We need the base
20074 direction of the current or _previous_ paragraph, so we need
20075 to make sure we are within that paragraph. To that end, find
20076 the previous non-empty line. */
20077 if (pos >= ZV && pos > BEGV)
20078 {
20079 pos--;
20080 bytepos = CHAR_TO_BYTE (pos);
20081 }
20082 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20083 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20084 {
20085 while ((c = FETCH_BYTE (bytepos)) == '\n'
20086 || c == ' ' || c == '\t' || c == '\f')
20087 {
20088 if (bytepos <= BEGV_BYTE)
20089 break;
20090 bytepos--;
20091 pos--;
20092 }
20093 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20094 bytepos--;
20095 }
20096 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20097 itb.paragraph_dir = NEUTRAL_DIR;
20098 itb.string.s = NULL;
20099 itb.string.lstring = Qnil;
20100 itb.string.bufpos = 0;
20101 itb.string.unibyte = 0;
20102 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20103 bidi_unshelve_cache (itb_data, 0);
20104 set_buffer_temp (old);
20105 switch (itb.paragraph_dir)
20106 {
20107 case L2R:
20108 return Qleft_to_right;
20109 break;
20110 case R2L:
20111 return Qright_to_left;
20112 break;
20113 default:
20114 emacs_abort ();
20115 }
20116 }
20117 }
20118
20119
20120 \f
20121 /***********************************************************************
20122 Menu Bar
20123 ***********************************************************************/
20124
20125 /* Redisplay the menu bar in the frame for window W.
20126
20127 The menu bar of X frames that don't have X toolkit support is
20128 displayed in a special window W->frame->menu_bar_window.
20129
20130 The menu bar of terminal frames is treated specially as far as
20131 glyph matrices are concerned. Menu bar lines are not part of
20132 windows, so the update is done directly on the frame matrix rows
20133 for the menu bar. */
20134
20135 static void
20136 display_menu_bar (struct window *w)
20137 {
20138 struct frame *f = XFRAME (WINDOW_FRAME (w));
20139 struct it it;
20140 Lisp_Object items;
20141 int i;
20142
20143 /* Don't do all this for graphical frames. */
20144 #ifdef HAVE_NTGUI
20145 if (FRAME_W32_P (f))
20146 return;
20147 #endif
20148 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20149 if (FRAME_X_P (f))
20150 return;
20151 #endif
20152
20153 #ifdef HAVE_NS
20154 if (FRAME_NS_P (f))
20155 return;
20156 #endif /* HAVE_NS */
20157
20158 #ifdef USE_X_TOOLKIT
20159 eassert (!FRAME_WINDOW_P (f));
20160 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20161 it.first_visible_x = 0;
20162 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20163 #else /* not USE_X_TOOLKIT */
20164 if (FRAME_WINDOW_P (f))
20165 {
20166 /* Menu bar lines are displayed in the desired matrix of the
20167 dummy window menu_bar_window. */
20168 struct window *menu_w;
20169 eassert (WINDOWP (f->menu_bar_window));
20170 menu_w = XWINDOW (f->menu_bar_window);
20171 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20172 MENU_FACE_ID);
20173 it.first_visible_x = 0;
20174 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20175 }
20176 else
20177 {
20178 /* This is a TTY frame, i.e. character hpos/vpos are used as
20179 pixel x/y. */
20180 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20181 MENU_FACE_ID);
20182 it.first_visible_x = 0;
20183 it.last_visible_x = FRAME_COLS (f);
20184 }
20185 #endif /* not USE_X_TOOLKIT */
20186
20187 /* FIXME: This should be controlled by a user option. See the
20188 comments in redisplay_tool_bar and display_mode_line about
20189 this. */
20190 it.paragraph_embedding = L2R;
20191
20192 /* Clear all rows of the menu bar. */
20193 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20194 {
20195 struct glyph_row *row = it.glyph_row + i;
20196 clear_glyph_row (row);
20197 row->enabled_p = 1;
20198 row->full_width_p = 1;
20199 }
20200
20201 /* Display all items of the menu bar. */
20202 items = FRAME_MENU_BAR_ITEMS (it.f);
20203 for (i = 0; i < ASIZE (items); i += 4)
20204 {
20205 Lisp_Object string;
20206
20207 /* Stop at nil string. */
20208 string = AREF (items, i + 1);
20209 if (NILP (string))
20210 break;
20211
20212 /* Remember where item was displayed. */
20213 ASET (items, i + 3, make_number (it.hpos));
20214
20215 /* Display the item, pad with one space. */
20216 if (it.current_x < it.last_visible_x)
20217 display_string (NULL, string, Qnil, 0, 0, &it,
20218 SCHARS (string) + 1, 0, 0, -1);
20219 }
20220
20221 /* Fill out the line with spaces. */
20222 if (it.current_x < it.last_visible_x)
20223 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20224
20225 /* Compute the total height of the lines. */
20226 compute_line_metrics (&it);
20227 }
20228
20229
20230 \f
20231 /***********************************************************************
20232 Mode Line
20233 ***********************************************************************/
20234
20235 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20236 FORCE is non-zero, redisplay mode lines unconditionally.
20237 Otherwise, redisplay only mode lines that are garbaged. Value is
20238 the number of windows whose mode lines were redisplayed. */
20239
20240 static int
20241 redisplay_mode_lines (Lisp_Object window, int force)
20242 {
20243 int nwindows = 0;
20244
20245 while (!NILP (window))
20246 {
20247 struct window *w = XWINDOW (window);
20248
20249 if (WINDOWP (w->hchild))
20250 nwindows += redisplay_mode_lines (w->hchild, force);
20251 else if (WINDOWP (w->vchild))
20252 nwindows += redisplay_mode_lines (w->vchild, force);
20253 else if (force
20254 || FRAME_GARBAGED_P (XFRAME (w->frame))
20255 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20256 {
20257 struct text_pos lpoint;
20258 struct buffer *old = current_buffer;
20259
20260 /* Set the window's buffer for the mode line display. */
20261 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20262 set_buffer_internal_1 (XBUFFER (w->buffer));
20263
20264 /* Point refers normally to the selected window. For any
20265 other window, set up appropriate value. */
20266 if (!EQ (window, selected_window))
20267 {
20268 struct text_pos pt;
20269
20270 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20271 if (CHARPOS (pt) < BEGV)
20272 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20273 else if (CHARPOS (pt) > (ZV - 1))
20274 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20275 else
20276 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20277 }
20278
20279 /* Display mode lines. */
20280 clear_glyph_matrix (w->desired_matrix);
20281 if (display_mode_lines (w))
20282 {
20283 ++nwindows;
20284 w->must_be_updated_p = 1;
20285 }
20286
20287 /* Restore old settings. */
20288 set_buffer_internal_1 (old);
20289 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20290 }
20291
20292 window = w->next;
20293 }
20294
20295 return nwindows;
20296 }
20297
20298
20299 /* Display the mode and/or header line of window W. Value is the
20300 sum number of mode lines and header lines displayed. */
20301
20302 static int
20303 display_mode_lines (struct window *w)
20304 {
20305 Lisp_Object old_selected_window = selected_window;
20306 Lisp_Object old_selected_frame = selected_frame;
20307 Lisp_Object new_frame = w->frame;
20308 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20309 int n = 0;
20310
20311 selected_frame = new_frame;
20312 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20313 or window's point, then we'd need select_window_1 here as well. */
20314 XSETWINDOW (selected_window, w);
20315 XFRAME (new_frame)->selected_window = selected_window;
20316
20317 /* These will be set while the mode line specs are processed. */
20318 line_number_displayed = 0;
20319 wset_column_number_displayed (w, Qnil);
20320
20321 if (WINDOW_WANTS_MODELINE_P (w))
20322 {
20323 struct window *sel_w = XWINDOW (old_selected_window);
20324
20325 /* Select mode line face based on the real selected window. */
20326 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20327 BVAR (current_buffer, mode_line_format));
20328 ++n;
20329 }
20330
20331 if (WINDOW_WANTS_HEADER_LINE_P (w))
20332 {
20333 display_mode_line (w, HEADER_LINE_FACE_ID,
20334 BVAR (current_buffer, header_line_format));
20335 ++n;
20336 }
20337
20338 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20339 selected_frame = old_selected_frame;
20340 selected_window = old_selected_window;
20341 return n;
20342 }
20343
20344
20345 /* Display mode or header line of window W. FACE_ID specifies which
20346 line to display; it is either MODE_LINE_FACE_ID or
20347 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20348 display. Value is the pixel height of the mode/header line
20349 displayed. */
20350
20351 static int
20352 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20353 {
20354 struct it it;
20355 struct face *face;
20356 ptrdiff_t count = SPECPDL_INDEX ();
20357
20358 init_iterator (&it, w, -1, -1, NULL, face_id);
20359 /* Don't extend on a previously drawn mode-line.
20360 This may happen if called from pos_visible_p. */
20361 it.glyph_row->enabled_p = 0;
20362 prepare_desired_row (it.glyph_row);
20363
20364 it.glyph_row->mode_line_p = 1;
20365
20366 /* FIXME: This should be controlled by a user option. But
20367 supporting such an option is not trivial, since the mode line is
20368 made up of many separate strings. */
20369 it.paragraph_embedding = L2R;
20370
20371 record_unwind_protect (unwind_format_mode_line,
20372 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20373
20374 mode_line_target = MODE_LINE_DISPLAY;
20375
20376 /* Temporarily make frame's keyboard the current kboard so that
20377 kboard-local variables in the mode_line_format will get the right
20378 values. */
20379 push_kboard (FRAME_KBOARD (it.f));
20380 record_unwind_save_match_data ();
20381 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20382 pop_kboard ();
20383
20384 unbind_to (count, Qnil);
20385
20386 /* Fill up with spaces. */
20387 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20388
20389 compute_line_metrics (&it);
20390 it.glyph_row->full_width_p = 1;
20391 it.glyph_row->continued_p = 0;
20392 it.glyph_row->truncated_on_left_p = 0;
20393 it.glyph_row->truncated_on_right_p = 0;
20394
20395 /* Make a 3D mode-line have a shadow at its right end. */
20396 face = FACE_FROM_ID (it.f, face_id);
20397 extend_face_to_end_of_line (&it);
20398 if (face->box != FACE_NO_BOX)
20399 {
20400 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20401 + it.glyph_row->used[TEXT_AREA] - 1);
20402 last->right_box_line_p = 1;
20403 }
20404
20405 return it.glyph_row->height;
20406 }
20407
20408 /* Move element ELT in LIST to the front of LIST.
20409 Return the updated list. */
20410
20411 static Lisp_Object
20412 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20413 {
20414 register Lisp_Object tail, prev;
20415 register Lisp_Object tem;
20416
20417 tail = list;
20418 prev = Qnil;
20419 while (CONSP (tail))
20420 {
20421 tem = XCAR (tail);
20422
20423 if (EQ (elt, tem))
20424 {
20425 /* Splice out the link TAIL. */
20426 if (NILP (prev))
20427 list = XCDR (tail);
20428 else
20429 Fsetcdr (prev, XCDR (tail));
20430
20431 /* Now make it the first. */
20432 Fsetcdr (tail, list);
20433 return tail;
20434 }
20435 else
20436 prev = tail;
20437 tail = XCDR (tail);
20438 QUIT;
20439 }
20440
20441 /* Not found--return unchanged LIST. */
20442 return list;
20443 }
20444
20445 /* Contribute ELT to the mode line for window IT->w. How it
20446 translates into text depends on its data type.
20447
20448 IT describes the display environment in which we display, as usual.
20449
20450 DEPTH is the depth in recursion. It is used to prevent
20451 infinite recursion here.
20452
20453 FIELD_WIDTH is the number of characters the display of ELT should
20454 occupy in the mode line, and PRECISION is the maximum number of
20455 characters to display from ELT's representation. See
20456 display_string for details.
20457
20458 Returns the hpos of the end of the text generated by ELT.
20459
20460 PROPS is a property list to add to any string we encounter.
20461
20462 If RISKY is nonzero, remove (disregard) any properties in any string
20463 we encounter, and ignore :eval and :propertize.
20464
20465 The global variable `mode_line_target' determines whether the
20466 output is passed to `store_mode_line_noprop',
20467 `store_mode_line_string', or `display_string'. */
20468
20469 static int
20470 display_mode_element (struct it *it, int depth, int field_width, int precision,
20471 Lisp_Object elt, Lisp_Object props, int risky)
20472 {
20473 int n = 0, field, prec;
20474 int literal = 0;
20475
20476 tail_recurse:
20477 if (depth > 100)
20478 elt = build_string ("*too-deep*");
20479
20480 depth++;
20481
20482 switch (XTYPE (elt))
20483 {
20484 case Lisp_String:
20485 {
20486 /* A string: output it and check for %-constructs within it. */
20487 unsigned char c;
20488 ptrdiff_t offset = 0;
20489
20490 if (SCHARS (elt) > 0
20491 && (!NILP (props) || risky))
20492 {
20493 Lisp_Object oprops, aelt;
20494 oprops = Ftext_properties_at (make_number (0), elt);
20495
20496 /* If the starting string's properties are not what
20497 we want, translate the string. Also, if the string
20498 is risky, do that anyway. */
20499
20500 if (NILP (Fequal (props, oprops)) || risky)
20501 {
20502 /* If the starting string has properties,
20503 merge the specified ones onto the existing ones. */
20504 if (! NILP (oprops) && !risky)
20505 {
20506 Lisp_Object tem;
20507
20508 oprops = Fcopy_sequence (oprops);
20509 tem = props;
20510 while (CONSP (tem))
20511 {
20512 oprops = Fplist_put (oprops, XCAR (tem),
20513 XCAR (XCDR (tem)));
20514 tem = XCDR (XCDR (tem));
20515 }
20516 props = oprops;
20517 }
20518
20519 aelt = Fassoc (elt, mode_line_proptrans_alist);
20520 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20521 {
20522 /* AELT is what we want. Move it to the front
20523 without consing. */
20524 elt = XCAR (aelt);
20525 mode_line_proptrans_alist
20526 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20527 }
20528 else
20529 {
20530 Lisp_Object tem;
20531
20532 /* If AELT has the wrong props, it is useless.
20533 so get rid of it. */
20534 if (! NILP (aelt))
20535 mode_line_proptrans_alist
20536 = Fdelq (aelt, mode_line_proptrans_alist);
20537
20538 elt = Fcopy_sequence (elt);
20539 Fset_text_properties (make_number (0), Flength (elt),
20540 props, elt);
20541 /* Add this item to mode_line_proptrans_alist. */
20542 mode_line_proptrans_alist
20543 = Fcons (Fcons (elt, props),
20544 mode_line_proptrans_alist);
20545 /* Truncate mode_line_proptrans_alist
20546 to at most 50 elements. */
20547 tem = Fnthcdr (make_number (50),
20548 mode_line_proptrans_alist);
20549 if (! NILP (tem))
20550 XSETCDR (tem, Qnil);
20551 }
20552 }
20553 }
20554
20555 offset = 0;
20556
20557 if (literal)
20558 {
20559 prec = precision - n;
20560 switch (mode_line_target)
20561 {
20562 case MODE_LINE_NOPROP:
20563 case MODE_LINE_TITLE:
20564 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20565 break;
20566 case MODE_LINE_STRING:
20567 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20568 break;
20569 case MODE_LINE_DISPLAY:
20570 n += display_string (NULL, elt, Qnil, 0, 0, it,
20571 0, prec, 0, STRING_MULTIBYTE (elt));
20572 break;
20573 }
20574
20575 break;
20576 }
20577
20578 /* Handle the non-literal case. */
20579
20580 while ((precision <= 0 || n < precision)
20581 && SREF (elt, offset) != 0
20582 && (mode_line_target != MODE_LINE_DISPLAY
20583 || it->current_x < it->last_visible_x))
20584 {
20585 ptrdiff_t last_offset = offset;
20586
20587 /* Advance to end of string or next format specifier. */
20588 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20589 ;
20590
20591 if (offset - 1 != last_offset)
20592 {
20593 ptrdiff_t nchars, nbytes;
20594
20595 /* Output to end of string or up to '%'. Field width
20596 is length of string. Don't output more than
20597 PRECISION allows us. */
20598 offset--;
20599
20600 prec = c_string_width (SDATA (elt) + last_offset,
20601 offset - last_offset, precision - n,
20602 &nchars, &nbytes);
20603
20604 switch (mode_line_target)
20605 {
20606 case MODE_LINE_NOPROP:
20607 case MODE_LINE_TITLE:
20608 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20609 break;
20610 case MODE_LINE_STRING:
20611 {
20612 ptrdiff_t bytepos = last_offset;
20613 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20614 ptrdiff_t endpos = (precision <= 0
20615 ? string_byte_to_char (elt, offset)
20616 : charpos + nchars);
20617
20618 n += store_mode_line_string (NULL,
20619 Fsubstring (elt, make_number (charpos),
20620 make_number (endpos)),
20621 0, 0, 0, Qnil);
20622 }
20623 break;
20624 case MODE_LINE_DISPLAY:
20625 {
20626 ptrdiff_t bytepos = last_offset;
20627 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20628
20629 if (precision <= 0)
20630 nchars = string_byte_to_char (elt, offset) - charpos;
20631 n += display_string (NULL, elt, Qnil, 0, charpos,
20632 it, 0, nchars, 0,
20633 STRING_MULTIBYTE (elt));
20634 }
20635 break;
20636 }
20637 }
20638 else /* c == '%' */
20639 {
20640 ptrdiff_t percent_position = offset;
20641
20642 /* Get the specified minimum width. Zero means
20643 don't pad. */
20644 field = 0;
20645 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20646 field = field * 10 + c - '0';
20647
20648 /* Don't pad beyond the total padding allowed. */
20649 if (field_width - n > 0 && field > field_width - n)
20650 field = field_width - n;
20651
20652 /* Note that either PRECISION <= 0 or N < PRECISION. */
20653 prec = precision - n;
20654
20655 if (c == 'M')
20656 n += display_mode_element (it, depth, field, prec,
20657 Vglobal_mode_string, props,
20658 risky);
20659 else if (c != 0)
20660 {
20661 int multibyte;
20662 ptrdiff_t bytepos, charpos;
20663 const char *spec;
20664 Lisp_Object string;
20665
20666 bytepos = percent_position;
20667 charpos = (STRING_MULTIBYTE (elt)
20668 ? string_byte_to_char (elt, bytepos)
20669 : bytepos);
20670 spec = decode_mode_spec (it->w, c, field, &string);
20671 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20672
20673 switch (mode_line_target)
20674 {
20675 case MODE_LINE_NOPROP:
20676 case MODE_LINE_TITLE:
20677 n += store_mode_line_noprop (spec, field, prec);
20678 break;
20679 case MODE_LINE_STRING:
20680 {
20681 Lisp_Object tem = build_string (spec);
20682 props = Ftext_properties_at (make_number (charpos), elt);
20683 /* Should only keep face property in props */
20684 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20685 }
20686 break;
20687 case MODE_LINE_DISPLAY:
20688 {
20689 int nglyphs_before, nwritten;
20690
20691 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20692 nwritten = display_string (spec, string, elt,
20693 charpos, 0, it,
20694 field, prec, 0,
20695 multibyte);
20696
20697 /* Assign to the glyphs written above the
20698 string where the `%x' came from, position
20699 of the `%'. */
20700 if (nwritten > 0)
20701 {
20702 struct glyph *glyph
20703 = (it->glyph_row->glyphs[TEXT_AREA]
20704 + nglyphs_before);
20705 int i;
20706
20707 for (i = 0; i < nwritten; ++i)
20708 {
20709 glyph[i].object = elt;
20710 glyph[i].charpos = charpos;
20711 }
20712
20713 n += nwritten;
20714 }
20715 }
20716 break;
20717 }
20718 }
20719 else /* c == 0 */
20720 break;
20721 }
20722 }
20723 }
20724 break;
20725
20726 case Lisp_Symbol:
20727 /* A symbol: process the value of the symbol recursively
20728 as if it appeared here directly. Avoid error if symbol void.
20729 Special case: if value of symbol is a string, output the string
20730 literally. */
20731 {
20732 register Lisp_Object tem;
20733
20734 /* If the variable is not marked as risky to set
20735 then its contents are risky to use. */
20736 if (NILP (Fget (elt, Qrisky_local_variable)))
20737 risky = 1;
20738
20739 tem = Fboundp (elt);
20740 if (!NILP (tem))
20741 {
20742 tem = Fsymbol_value (elt);
20743 /* If value is a string, output that string literally:
20744 don't check for % within it. */
20745 if (STRINGP (tem))
20746 literal = 1;
20747
20748 if (!EQ (tem, elt))
20749 {
20750 /* Give up right away for nil or t. */
20751 elt = tem;
20752 goto tail_recurse;
20753 }
20754 }
20755 }
20756 break;
20757
20758 case Lisp_Cons:
20759 {
20760 register Lisp_Object car, tem;
20761
20762 /* A cons cell: five distinct cases.
20763 If first element is :eval or :propertize, do something special.
20764 If first element is a string or a cons, process all the elements
20765 and effectively concatenate them.
20766 If first element is a negative number, truncate displaying cdr to
20767 at most that many characters. If positive, pad (with spaces)
20768 to at least that many characters.
20769 If first element is a symbol, process the cadr or caddr recursively
20770 according to whether the symbol's value is non-nil or nil. */
20771 car = XCAR (elt);
20772 if (EQ (car, QCeval))
20773 {
20774 /* An element of the form (:eval FORM) means evaluate FORM
20775 and use the result as mode line elements. */
20776
20777 if (risky)
20778 break;
20779
20780 if (CONSP (XCDR (elt)))
20781 {
20782 Lisp_Object spec;
20783 spec = safe_eval (XCAR (XCDR (elt)));
20784 n += display_mode_element (it, depth, field_width - n,
20785 precision - n, spec, props,
20786 risky);
20787 }
20788 }
20789 else if (EQ (car, QCpropertize))
20790 {
20791 /* An element of the form (:propertize ELT PROPS...)
20792 means display ELT but applying properties PROPS. */
20793
20794 if (risky)
20795 break;
20796
20797 if (CONSP (XCDR (elt)))
20798 n += display_mode_element (it, depth, field_width - n,
20799 precision - n, XCAR (XCDR (elt)),
20800 XCDR (XCDR (elt)), risky);
20801 }
20802 else if (SYMBOLP (car))
20803 {
20804 tem = Fboundp (car);
20805 elt = XCDR (elt);
20806 if (!CONSP (elt))
20807 goto invalid;
20808 /* elt is now the cdr, and we know it is a cons cell.
20809 Use its car if CAR has a non-nil value. */
20810 if (!NILP (tem))
20811 {
20812 tem = Fsymbol_value (car);
20813 if (!NILP (tem))
20814 {
20815 elt = XCAR (elt);
20816 goto tail_recurse;
20817 }
20818 }
20819 /* Symbol's value is nil (or symbol is unbound)
20820 Get the cddr of the original list
20821 and if possible find the caddr and use that. */
20822 elt = XCDR (elt);
20823 if (NILP (elt))
20824 break;
20825 else if (!CONSP (elt))
20826 goto invalid;
20827 elt = XCAR (elt);
20828 goto tail_recurse;
20829 }
20830 else if (INTEGERP (car))
20831 {
20832 register int lim = XINT (car);
20833 elt = XCDR (elt);
20834 if (lim < 0)
20835 {
20836 /* Negative int means reduce maximum width. */
20837 if (precision <= 0)
20838 precision = -lim;
20839 else
20840 precision = min (precision, -lim);
20841 }
20842 else if (lim > 0)
20843 {
20844 /* Padding specified. Don't let it be more than
20845 current maximum. */
20846 if (precision > 0)
20847 lim = min (precision, lim);
20848
20849 /* If that's more padding than already wanted, queue it.
20850 But don't reduce padding already specified even if
20851 that is beyond the current truncation point. */
20852 field_width = max (lim, field_width);
20853 }
20854 goto tail_recurse;
20855 }
20856 else if (STRINGP (car) || CONSP (car))
20857 {
20858 Lisp_Object halftail = elt;
20859 int len = 0;
20860
20861 while (CONSP (elt)
20862 && (precision <= 0 || n < precision))
20863 {
20864 n += display_mode_element (it, depth,
20865 /* Do padding only after the last
20866 element in the list. */
20867 (! CONSP (XCDR (elt))
20868 ? field_width - n
20869 : 0),
20870 precision - n, XCAR (elt),
20871 props, risky);
20872 elt = XCDR (elt);
20873 len++;
20874 if ((len & 1) == 0)
20875 halftail = XCDR (halftail);
20876 /* Check for cycle. */
20877 if (EQ (halftail, elt))
20878 break;
20879 }
20880 }
20881 }
20882 break;
20883
20884 default:
20885 invalid:
20886 elt = build_string ("*invalid*");
20887 goto tail_recurse;
20888 }
20889
20890 /* Pad to FIELD_WIDTH. */
20891 if (field_width > 0 && n < field_width)
20892 {
20893 switch (mode_line_target)
20894 {
20895 case MODE_LINE_NOPROP:
20896 case MODE_LINE_TITLE:
20897 n += store_mode_line_noprop ("", field_width - n, 0);
20898 break;
20899 case MODE_LINE_STRING:
20900 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20901 break;
20902 case MODE_LINE_DISPLAY:
20903 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20904 0, 0, 0);
20905 break;
20906 }
20907 }
20908
20909 return n;
20910 }
20911
20912 /* Store a mode-line string element in mode_line_string_list.
20913
20914 If STRING is non-null, display that C string. Otherwise, the Lisp
20915 string LISP_STRING is displayed.
20916
20917 FIELD_WIDTH is the minimum number of output glyphs to produce.
20918 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20919 with spaces. FIELD_WIDTH <= 0 means don't pad.
20920
20921 PRECISION is the maximum number of characters to output from
20922 STRING. PRECISION <= 0 means don't truncate the string.
20923
20924 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20925 properties to the string.
20926
20927 PROPS are the properties to add to the string.
20928 The mode_line_string_face face property is always added to the string.
20929 */
20930
20931 static int
20932 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20933 int field_width, int precision, Lisp_Object props)
20934 {
20935 ptrdiff_t len;
20936 int n = 0;
20937
20938 if (string != NULL)
20939 {
20940 len = strlen (string);
20941 if (precision > 0 && len > precision)
20942 len = precision;
20943 lisp_string = make_string (string, len);
20944 if (NILP (props))
20945 props = mode_line_string_face_prop;
20946 else if (!NILP (mode_line_string_face))
20947 {
20948 Lisp_Object face = Fplist_get (props, Qface);
20949 props = Fcopy_sequence (props);
20950 if (NILP (face))
20951 face = mode_line_string_face;
20952 else
20953 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20954 props = Fplist_put (props, Qface, face);
20955 }
20956 Fadd_text_properties (make_number (0), make_number (len),
20957 props, lisp_string);
20958 }
20959 else
20960 {
20961 len = XFASTINT (Flength (lisp_string));
20962 if (precision > 0 && len > precision)
20963 {
20964 len = precision;
20965 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20966 precision = -1;
20967 }
20968 if (!NILP (mode_line_string_face))
20969 {
20970 Lisp_Object face;
20971 if (NILP (props))
20972 props = Ftext_properties_at (make_number (0), lisp_string);
20973 face = Fplist_get (props, Qface);
20974 if (NILP (face))
20975 face = mode_line_string_face;
20976 else
20977 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20978 props = Fcons (Qface, Fcons (face, Qnil));
20979 if (copy_string)
20980 lisp_string = Fcopy_sequence (lisp_string);
20981 }
20982 if (!NILP (props))
20983 Fadd_text_properties (make_number (0), make_number (len),
20984 props, lisp_string);
20985 }
20986
20987 if (len > 0)
20988 {
20989 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20990 n += len;
20991 }
20992
20993 if (field_width > len)
20994 {
20995 field_width -= len;
20996 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20997 if (!NILP (props))
20998 Fadd_text_properties (make_number (0), make_number (field_width),
20999 props, lisp_string);
21000 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21001 n += field_width;
21002 }
21003
21004 return n;
21005 }
21006
21007
21008 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21009 1, 4, 0,
21010 doc: /* Format a string out of a mode line format specification.
21011 First arg FORMAT specifies the mode line format (see `mode-line-format'
21012 for details) to use.
21013
21014 By default, the format is evaluated for the currently selected window.
21015
21016 Optional second arg FACE specifies the face property to put on all
21017 characters for which no face is specified. The value nil means the
21018 default face. The value t means whatever face the window's mode line
21019 currently uses (either `mode-line' or `mode-line-inactive',
21020 depending on whether the window is the selected window or not).
21021 An integer value means the value string has no text
21022 properties.
21023
21024 Optional third and fourth args WINDOW and BUFFER specify the window
21025 and buffer to use as the context for the formatting (defaults
21026 are the selected window and the WINDOW's buffer). */)
21027 (Lisp_Object format, Lisp_Object face,
21028 Lisp_Object window, Lisp_Object buffer)
21029 {
21030 struct it it;
21031 int len;
21032 struct window *w;
21033 struct buffer *old_buffer = NULL;
21034 int face_id;
21035 int no_props = INTEGERP (face);
21036 ptrdiff_t count = SPECPDL_INDEX ();
21037 Lisp_Object str;
21038 int string_start = 0;
21039
21040 w = decode_any_window (window);
21041 XSETWINDOW (window, w);
21042
21043 if (NILP (buffer))
21044 buffer = w->buffer;
21045 CHECK_BUFFER (buffer);
21046
21047 /* Make formatting the modeline a non-op when noninteractive, otherwise
21048 there will be problems later caused by a partially initialized frame. */
21049 if (NILP (format) || noninteractive)
21050 return empty_unibyte_string;
21051
21052 if (no_props)
21053 face = Qnil;
21054
21055 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21056 : EQ (face, Qt) ? (EQ (window, selected_window)
21057 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21058 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21059 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21060 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21061 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21062 : DEFAULT_FACE_ID;
21063
21064 old_buffer = current_buffer;
21065
21066 /* Save things including mode_line_proptrans_alist,
21067 and set that to nil so that we don't alter the outer value. */
21068 record_unwind_protect (unwind_format_mode_line,
21069 format_mode_line_unwind_data
21070 (XFRAME (WINDOW_FRAME (w)),
21071 old_buffer, selected_window, 1));
21072 mode_line_proptrans_alist = Qnil;
21073
21074 Fselect_window (window, Qt);
21075 set_buffer_internal_1 (XBUFFER (buffer));
21076
21077 init_iterator (&it, w, -1, -1, NULL, face_id);
21078
21079 if (no_props)
21080 {
21081 mode_line_target = MODE_LINE_NOPROP;
21082 mode_line_string_face_prop = Qnil;
21083 mode_line_string_list = Qnil;
21084 string_start = MODE_LINE_NOPROP_LEN (0);
21085 }
21086 else
21087 {
21088 mode_line_target = MODE_LINE_STRING;
21089 mode_line_string_list = Qnil;
21090 mode_line_string_face = face;
21091 mode_line_string_face_prop
21092 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21093 }
21094
21095 push_kboard (FRAME_KBOARD (it.f));
21096 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21097 pop_kboard ();
21098
21099 if (no_props)
21100 {
21101 len = MODE_LINE_NOPROP_LEN (string_start);
21102 str = make_string (mode_line_noprop_buf + string_start, len);
21103 }
21104 else
21105 {
21106 mode_line_string_list = Fnreverse (mode_line_string_list);
21107 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21108 empty_unibyte_string);
21109 }
21110
21111 unbind_to (count, Qnil);
21112 return str;
21113 }
21114
21115 /* Write a null-terminated, right justified decimal representation of
21116 the positive integer D to BUF using a minimal field width WIDTH. */
21117
21118 static void
21119 pint2str (register char *buf, register int width, register ptrdiff_t d)
21120 {
21121 register char *p = buf;
21122
21123 if (d <= 0)
21124 *p++ = '0';
21125 else
21126 {
21127 while (d > 0)
21128 {
21129 *p++ = d % 10 + '0';
21130 d /= 10;
21131 }
21132 }
21133
21134 for (width -= (int) (p - buf); width > 0; --width)
21135 *p++ = ' ';
21136 *p-- = '\0';
21137 while (p > buf)
21138 {
21139 d = *buf;
21140 *buf++ = *p;
21141 *p-- = d;
21142 }
21143 }
21144
21145 /* Write a null-terminated, right justified decimal and "human
21146 readable" representation of the nonnegative integer D to BUF using
21147 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21148
21149 static const char power_letter[] =
21150 {
21151 0, /* no letter */
21152 'k', /* kilo */
21153 'M', /* mega */
21154 'G', /* giga */
21155 'T', /* tera */
21156 'P', /* peta */
21157 'E', /* exa */
21158 'Z', /* zetta */
21159 'Y' /* yotta */
21160 };
21161
21162 static void
21163 pint2hrstr (char *buf, int width, ptrdiff_t d)
21164 {
21165 /* We aim to represent the nonnegative integer D as
21166 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21167 ptrdiff_t quotient = d;
21168 int remainder = 0;
21169 /* -1 means: do not use TENTHS. */
21170 int tenths = -1;
21171 int exponent = 0;
21172
21173 /* Length of QUOTIENT.TENTHS as a string. */
21174 int length;
21175
21176 char * psuffix;
21177 char * p;
21178
21179 if (1000 <= quotient)
21180 {
21181 /* Scale to the appropriate EXPONENT. */
21182 do
21183 {
21184 remainder = quotient % 1000;
21185 quotient /= 1000;
21186 exponent++;
21187 }
21188 while (1000 <= quotient);
21189
21190 /* Round to nearest and decide whether to use TENTHS or not. */
21191 if (quotient <= 9)
21192 {
21193 tenths = remainder / 100;
21194 if (50 <= remainder % 100)
21195 {
21196 if (tenths < 9)
21197 tenths++;
21198 else
21199 {
21200 quotient++;
21201 if (quotient == 10)
21202 tenths = -1;
21203 else
21204 tenths = 0;
21205 }
21206 }
21207 }
21208 else
21209 if (500 <= remainder)
21210 {
21211 if (quotient < 999)
21212 quotient++;
21213 else
21214 {
21215 quotient = 1;
21216 exponent++;
21217 tenths = 0;
21218 }
21219 }
21220 }
21221
21222 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21223 if (tenths == -1 && quotient <= 99)
21224 if (quotient <= 9)
21225 length = 1;
21226 else
21227 length = 2;
21228 else
21229 length = 3;
21230 p = psuffix = buf + max (width, length);
21231
21232 /* Print EXPONENT. */
21233 *psuffix++ = power_letter[exponent];
21234 *psuffix = '\0';
21235
21236 /* Print TENTHS. */
21237 if (tenths >= 0)
21238 {
21239 *--p = '0' + tenths;
21240 *--p = '.';
21241 }
21242
21243 /* Print QUOTIENT. */
21244 do
21245 {
21246 int digit = quotient % 10;
21247 *--p = '0' + digit;
21248 }
21249 while ((quotient /= 10) != 0);
21250
21251 /* Print leading spaces. */
21252 while (buf < p)
21253 *--p = ' ';
21254 }
21255
21256 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21257 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21258 type of CODING_SYSTEM. Return updated pointer into BUF. */
21259
21260 static unsigned char invalid_eol_type[] = "(*invalid*)";
21261
21262 static char *
21263 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21264 {
21265 Lisp_Object val;
21266 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21267 const unsigned char *eol_str;
21268 int eol_str_len;
21269 /* The EOL conversion we are using. */
21270 Lisp_Object eoltype;
21271
21272 val = CODING_SYSTEM_SPEC (coding_system);
21273 eoltype = Qnil;
21274
21275 if (!VECTORP (val)) /* Not yet decided. */
21276 {
21277 *buf++ = multibyte ? '-' : ' ';
21278 if (eol_flag)
21279 eoltype = eol_mnemonic_undecided;
21280 /* Don't mention EOL conversion if it isn't decided. */
21281 }
21282 else
21283 {
21284 Lisp_Object attrs;
21285 Lisp_Object eolvalue;
21286
21287 attrs = AREF (val, 0);
21288 eolvalue = AREF (val, 2);
21289
21290 *buf++ = multibyte
21291 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21292 : ' ';
21293
21294 if (eol_flag)
21295 {
21296 /* The EOL conversion that is normal on this system. */
21297
21298 if (NILP (eolvalue)) /* Not yet decided. */
21299 eoltype = eol_mnemonic_undecided;
21300 else if (VECTORP (eolvalue)) /* Not yet decided. */
21301 eoltype = eol_mnemonic_undecided;
21302 else /* eolvalue is Qunix, Qdos, or Qmac. */
21303 eoltype = (EQ (eolvalue, Qunix)
21304 ? eol_mnemonic_unix
21305 : (EQ (eolvalue, Qdos) == 1
21306 ? eol_mnemonic_dos : eol_mnemonic_mac));
21307 }
21308 }
21309
21310 if (eol_flag)
21311 {
21312 /* Mention the EOL conversion if it is not the usual one. */
21313 if (STRINGP (eoltype))
21314 {
21315 eol_str = SDATA (eoltype);
21316 eol_str_len = SBYTES (eoltype);
21317 }
21318 else if (CHARACTERP (eoltype))
21319 {
21320 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21321 int c = XFASTINT (eoltype);
21322 eol_str_len = CHAR_STRING (c, tmp);
21323 eol_str = tmp;
21324 }
21325 else
21326 {
21327 eol_str = invalid_eol_type;
21328 eol_str_len = sizeof (invalid_eol_type) - 1;
21329 }
21330 memcpy (buf, eol_str, eol_str_len);
21331 buf += eol_str_len;
21332 }
21333
21334 return buf;
21335 }
21336
21337 /* Return a string for the output of a mode line %-spec for window W,
21338 generated by character C. FIELD_WIDTH > 0 means pad the string
21339 returned with spaces to that value. Return a Lisp string in
21340 *STRING if the resulting string is taken from that Lisp string.
21341
21342 Note we operate on the current buffer for most purposes,
21343 the exception being w->base_line_pos. */
21344
21345 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21346
21347 static const char *
21348 decode_mode_spec (struct window *w, register int c, int field_width,
21349 Lisp_Object *string)
21350 {
21351 Lisp_Object obj;
21352 struct frame *f = XFRAME (WINDOW_FRAME (w));
21353 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21354 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21355 produce strings from numerical values, so limit preposterously
21356 large values of FIELD_WIDTH to avoid overrunning the buffer's
21357 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21358 bytes plus the terminating null. */
21359 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21360 struct buffer *b = current_buffer;
21361
21362 obj = Qnil;
21363 *string = Qnil;
21364
21365 switch (c)
21366 {
21367 case '*':
21368 if (!NILP (BVAR (b, read_only)))
21369 return "%";
21370 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21371 return "*";
21372 return "-";
21373
21374 case '+':
21375 /* This differs from %* only for a modified read-only buffer. */
21376 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21377 return "*";
21378 if (!NILP (BVAR (b, read_only)))
21379 return "%";
21380 return "-";
21381
21382 case '&':
21383 /* This differs from %* in ignoring read-only-ness. */
21384 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21385 return "*";
21386 return "-";
21387
21388 case '%':
21389 return "%";
21390
21391 case '[':
21392 {
21393 int i;
21394 char *p;
21395
21396 if (command_loop_level > 5)
21397 return "[[[... ";
21398 p = decode_mode_spec_buf;
21399 for (i = 0; i < command_loop_level; i++)
21400 *p++ = '[';
21401 *p = 0;
21402 return decode_mode_spec_buf;
21403 }
21404
21405 case ']':
21406 {
21407 int i;
21408 char *p;
21409
21410 if (command_loop_level > 5)
21411 return " ...]]]";
21412 p = decode_mode_spec_buf;
21413 for (i = 0; i < command_loop_level; i++)
21414 *p++ = ']';
21415 *p = 0;
21416 return decode_mode_spec_buf;
21417 }
21418
21419 case '-':
21420 {
21421 register int i;
21422
21423 /* Let lots_of_dashes be a string of infinite length. */
21424 if (mode_line_target == MODE_LINE_NOPROP
21425 || mode_line_target == MODE_LINE_STRING)
21426 return "--";
21427 if (field_width <= 0
21428 || field_width > sizeof (lots_of_dashes))
21429 {
21430 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21431 decode_mode_spec_buf[i] = '-';
21432 decode_mode_spec_buf[i] = '\0';
21433 return decode_mode_spec_buf;
21434 }
21435 else
21436 return lots_of_dashes;
21437 }
21438
21439 case 'b':
21440 obj = BVAR (b, name);
21441 break;
21442
21443 case 'c':
21444 /* %c and %l are ignored in `frame-title-format'.
21445 (In redisplay_internal, the frame title is drawn _before_ the
21446 windows are updated, so the stuff which depends on actual
21447 window contents (such as %l) may fail to render properly, or
21448 even crash emacs.) */
21449 if (mode_line_target == MODE_LINE_TITLE)
21450 return "";
21451 else
21452 {
21453 ptrdiff_t col = current_column ();
21454 wset_column_number_displayed (w, make_number (col));
21455 pint2str (decode_mode_spec_buf, width, col);
21456 return decode_mode_spec_buf;
21457 }
21458
21459 case 'e':
21460 #ifndef SYSTEM_MALLOC
21461 {
21462 if (NILP (Vmemory_full))
21463 return "";
21464 else
21465 return "!MEM FULL! ";
21466 }
21467 #else
21468 return "";
21469 #endif
21470
21471 case 'F':
21472 /* %F displays the frame name. */
21473 if (!NILP (f->title))
21474 return SSDATA (f->title);
21475 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21476 return SSDATA (f->name);
21477 return "Emacs";
21478
21479 case 'f':
21480 obj = BVAR (b, filename);
21481 break;
21482
21483 case 'i':
21484 {
21485 ptrdiff_t size = ZV - BEGV;
21486 pint2str (decode_mode_spec_buf, width, size);
21487 return decode_mode_spec_buf;
21488 }
21489
21490 case 'I':
21491 {
21492 ptrdiff_t size = ZV - BEGV;
21493 pint2hrstr (decode_mode_spec_buf, width, size);
21494 return decode_mode_spec_buf;
21495 }
21496
21497 case 'l':
21498 {
21499 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21500 ptrdiff_t topline, nlines, height;
21501 ptrdiff_t junk;
21502
21503 /* %c and %l are ignored in `frame-title-format'. */
21504 if (mode_line_target == MODE_LINE_TITLE)
21505 return "";
21506
21507 startpos = marker_position (w->start);
21508 startpos_byte = marker_byte_position (w->start);
21509 height = WINDOW_TOTAL_LINES (w);
21510
21511 /* If we decided that this buffer isn't suitable for line numbers,
21512 don't forget that too fast. */
21513 if (EQ (w->base_line_pos, w->buffer))
21514 goto no_value;
21515 /* But do forget it, if the window shows a different buffer now. */
21516 else if (BUFFERP (w->base_line_pos))
21517 wset_base_line_pos (w, Qnil);
21518
21519 /* If the buffer is very big, don't waste time. */
21520 if (INTEGERP (Vline_number_display_limit)
21521 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21522 {
21523 wset_base_line_pos (w, Qnil);
21524 wset_base_line_number (w, Qnil);
21525 goto no_value;
21526 }
21527
21528 if (INTEGERP (w->base_line_number)
21529 && INTEGERP (w->base_line_pos)
21530 && XFASTINT (w->base_line_pos) <= startpos)
21531 {
21532 line = XFASTINT (w->base_line_number);
21533 linepos = XFASTINT (w->base_line_pos);
21534 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21535 }
21536 else
21537 {
21538 line = 1;
21539 linepos = BUF_BEGV (b);
21540 linepos_byte = BUF_BEGV_BYTE (b);
21541 }
21542
21543 /* Count lines from base line to window start position. */
21544 nlines = display_count_lines (linepos_byte,
21545 startpos_byte,
21546 startpos, &junk);
21547
21548 topline = nlines + line;
21549
21550 /* Determine a new base line, if the old one is too close
21551 or too far away, or if we did not have one.
21552 "Too close" means it's plausible a scroll-down would
21553 go back past it. */
21554 if (startpos == BUF_BEGV (b))
21555 {
21556 wset_base_line_number (w, make_number (topline));
21557 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21558 }
21559 else if (nlines < height + 25 || nlines > height * 3 + 50
21560 || linepos == BUF_BEGV (b))
21561 {
21562 ptrdiff_t limit = BUF_BEGV (b);
21563 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21564 ptrdiff_t position;
21565 ptrdiff_t distance =
21566 (height * 2 + 30) * line_number_display_limit_width;
21567
21568 if (startpos - distance > limit)
21569 {
21570 limit = startpos - distance;
21571 limit_byte = CHAR_TO_BYTE (limit);
21572 }
21573
21574 nlines = display_count_lines (startpos_byte,
21575 limit_byte,
21576 - (height * 2 + 30),
21577 &position);
21578 /* If we couldn't find the lines we wanted within
21579 line_number_display_limit_width chars per line,
21580 give up on line numbers for this window. */
21581 if (position == limit_byte && limit == startpos - distance)
21582 {
21583 wset_base_line_pos (w, w->buffer);
21584 wset_base_line_number (w, Qnil);
21585 goto no_value;
21586 }
21587
21588 wset_base_line_number (w, make_number (topline - nlines));
21589 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21590 }
21591
21592 /* Now count lines from the start pos to point. */
21593 nlines = display_count_lines (startpos_byte,
21594 PT_BYTE, PT, &junk);
21595
21596 /* Record that we did display the line number. */
21597 line_number_displayed = 1;
21598
21599 /* Make the string to show. */
21600 pint2str (decode_mode_spec_buf, width, topline + nlines);
21601 return decode_mode_spec_buf;
21602 no_value:
21603 {
21604 char* p = decode_mode_spec_buf;
21605 int pad = width - 2;
21606 while (pad-- > 0)
21607 *p++ = ' ';
21608 *p++ = '?';
21609 *p++ = '?';
21610 *p = '\0';
21611 return decode_mode_spec_buf;
21612 }
21613 }
21614 break;
21615
21616 case 'm':
21617 obj = BVAR (b, mode_name);
21618 break;
21619
21620 case 'n':
21621 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21622 return " Narrow";
21623 break;
21624
21625 case 'p':
21626 {
21627 ptrdiff_t pos = marker_position (w->start);
21628 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21629
21630 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21631 {
21632 if (pos <= BUF_BEGV (b))
21633 return "All";
21634 else
21635 return "Bottom";
21636 }
21637 else if (pos <= BUF_BEGV (b))
21638 return "Top";
21639 else
21640 {
21641 if (total > 1000000)
21642 /* Do it differently for a large value, to avoid overflow. */
21643 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21644 else
21645 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21646 /* We can't normally display a 3-digit number,
21647 so get us a 2-digit number that is close. */
21648 if (total == 100)
21649 total = 99;
21650 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21651 return decode_mode_spec_buf;
21652 }
21653 }
21654
21655 /* Display percentage of size above the bottom of the screen. */
21656 case 'P':
21657 {
21658 ptrdiff_t toppos = marker_position (w->start);
21659 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21660 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21661
21662 if (botpos >= BUF_ZV (b))
21663 {
21664 if (toppos <= BUF_BEGV (b))
21665 return "All";
21666 else
21667 return "Bottom";
21668 }
21669 else
21670 {
21671 if (total > 1000000)
21672 /* Do it differently for a large value, to avoid overflow. */
21673 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21674 else
21675 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21676 /* We can't normally display a 3-digit number,
21677 so get us a 2-digit number that is close. */
21678 if (total == 100)
21679 total = 99;
21680 if (toppos <= BUF_BEGV (b))
21681 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21682 else
21683 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21684 return decode_mode_spec_buf;
21685 }
21686 }
21687
21688 case 's':
21689 /* status of process */
21690 obj = Fget_buffer_process (Fcurrent_buffer ());
21691 if (NILP (obj))
21692 return "no process";
21693 #ifndef MSDOS
21694 obj = Fsymbol_name (Fprocess_status (obj));
21695 #endif
21696 break;
21697
21698 case '@':
21699 {
21700 ptrdiff_t count = inhibit_garbage_collection ();
21701 Lisp_Object val = call1 (intern ("file-remote-p"),
21702 BVAR (current_buffer, directory));
21703 unbind_to (count, Qnil);
21704
21705 if (NILP (val))
21706 return "-";
21707 else
21708 return "@";
21709 }
21710
21711 case 't': /* indicate TEXT or BINARY */
21712 return "T";
21713
21714 case 'z':
21715 /* coding-system (not including end-of-line format) */
21716 case 'Z':
21717 /* coding-system (including end-of-line type) */
21718 {
21719 int eol_flag = (c == 'Z');
21720 char *p = decode_mode_spec_buf;
21721
21722 if (! FRAME_WINDOW_P (f))
21723 {
21724 /* No need to mention EOL here--the terminal never needs
21725 to do EOL conversion. */
21726 p = decode_mode_spec_coding (CODING_ID_NAME
21727 (FRAME_KEYBOARD_CODING (f)->id),
21728 p, 0);
21729 p = decode_mode_spec_coding (CODING_ID_NAME
21730 (FRAME_TERMINAL_CODING (f)->id),
21731 p, 0);
21732 }
21733 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21734 p, eol_flag);
21735
21736 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21737 #ifdef subprocesses
21738 obj = Fget_buffer_process (Fcurrent_buffer ());
21739 if (PROCESSP (obj))
21740 {
21741 p = decode_mode_spec_coding
21742 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21743 p = decode_mode_spec_coding
21744 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21745 }
21746 #endif /* subprocesses */
21747 #endif /* 0 */
21748 *p = 0;
21749 return decode_mode_spec_buf;
21750 }
21751 }
21752
21753 if (STRINGP (obj))
21754 {
21755 *string = obj;
21756 return SSDATA (obj);
21757 }
21758 else
21759 return "";
21760 }
21761
21762
21763 /* Count up to COUNT lines starting from START_BYTE.
21764 But don't go beyond LIMIT_BYTE.
21765 Return the number of lines thus found (always nonnegative).
21766
21767 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21768
21769 static ptrdiff_t
21770 display_count_lines (ptrdiff_t start_byte,
21771 ptrdiff_t limit_byte, ptrdiff_t count,
21772 ptrdiff_t *byte_pos_ptr)
21773 {
21774 register unsigned char *cursor;
21775 unsigned char *base;
21776
21777 register ptrdiff_t ceiling;
21778 register unsigned char *ceiling_addr;
21779 ptrdiff_t orig_count = count;
21780
21781 /* If we are not in selective display mode,
21782 check only for newlines. */
21783 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21784 && !INTEGERP (BVAR (current_buffer, selective_display)));
21785
21786 if (count > 0)
21787 {
21788 while (start_byte < limit_byte)
21789 {
21790 ceiling = BUFFER_CEILING_OF (start_byte);
21791 ceiling = min (limit_byte - 1, ceiling);
21792 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21793 base = (cursor = BYTE_POS_ADDR (start_byte));
21794 while (1)
21795 {
21796 if (selective_display)
21797 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21798 ;
21799 else
21800 while (*cursor != '\n' && ++cursor != ceiling_addr)
21801 ;
21802
21803 if (cursor != ceiling_addr)
21804 {
21805 if (--count == 0)
21806 {
21807 start_byte += cursor - base + 1;
21808 *byte_pos_ptr = start_byte;
21809 return orig_count;
21810 }
21811 else
21812 if (++cursor == ceiling_addr)
21813 break;
21814 }
21815 else
21816 break;
21817 }
21818 start_byte += cursor - base;
21819 }
21820 }
21821 else
21822 {
21823 while (start_byte > limit_byte)
21824 {
21825 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21826 ceiling = max (limit_byte, ceiling);
21827 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21828 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21829 while (1)
21830 {
21831 if (selective_display)
21832 while (--cursor != ceiling_addr
21833 && *cursor != '\n' && *cursor != 015)
21834 ;
21835 else
21836 while (--cursor != ceiling_addr && *cursor != '\n')
21837 ;
21838
21839 if (cursor != ceiling_addr)
21840 {
21841 if (++count == 0)
21842 {
21843 start_byte += cursor - base + 1;
21844 *byte_pos_ptr = start_byte;
21845 /* When scanning backwards, we should
21846 not count the newline posterior to which we stop. */
21847 return - orig_count - 1;
21848 }
21849 }
21850 else
21851 break;
21852 }
21853 /* Here we add 1 to compensate for the last decrement
21854 of CURSOR, which took it past the valid range. */
21855 start_byte += cursor - base + 1;
21856 }
21857 }
21858
21859 *byte_pos_ptr = limit_byte;
21860
21861 if (count < 0)
21862 return - orig_count + count;
21863 return orig_count - count;
21864
21865 }
21866
21867
21868 \f
21869 /***********************************************************************
21870 Displaying strings
21871 ***********************************************************************/
21872
21873 /* Display a NUL-terminated string, starting with index START.
21874
21875 If STRING is non-null, display that C string. Otherwise, the Lisp
21876 string LISP_STRING is displayed. There's a case that STRING is
21877 non-null and LISP_STRING is not nil. It means STRING is a string
21878 data of LISP_STRING. In that case, we display LISP_STRING while
21879 ignoring its text properties.
21880
21881 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21882 FACE_STRING. Display STRING or LISP_STRING with the face at
21883 FACE_STRING_POS in FACE_STRING:
21884
21885 Display the string in the environment given by IT, but use the
21886 standard display table, temporarily.
21887
21888 FIELD_WIDTH is the minimum number of output glyphs to produce.
21889 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21890 with spaces. If STRING has more characters, more than FIELD_WIDTH
21891 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21892
21893 PRECISION is the maximum number of characters to output from
21894 STRING. PRECISION < 0 means don't truncate the string.
21895
21896 This is roughly equivalent to printf format specifiers:
21897
21898 FIELD_WIDTH PRECISION PRINTF
21899 ----------------------------------------
21900 -1 -1 %s
21901 -1 10 %.10s
21902 10 -1 %10s
21903 20 10 %20.10s
21904
21905 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21906 display them, and < 0 means obey the current buffer's value of
21907 enable_multibyte_characters.
21908
21909 Value is the number of columns displayed. */
21910
21911 static int
21912 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21913 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21914 int field_width, int precision, int max_x, int multibyte)
21915 {
21916 int hpos_at_start = it->hpos;
21917 int saved_face_id = it->face_id;
21918 struct glyph_row *row = it->glyph_row;
21919 ptrdiff_t it_charpos;
21920
21921 /* Initialize the iterator IT for iteration over STRING beginning
21922 with index START. */
21923 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21924 precision, field_width, multibyte);
21925 if (string && STRINGP (lisp_string))
21926 /* LISP_STRING is the one returned by decode_mode_spec. We should
21927 ignore its text properties. */
21928 it->stop_charpos = it->end_charpos;
21929
21930 /* If displaying STRING, set up the face of the iterator from
21931 FACE_STRING, if that's given. */
21932 if (STRINGP (face_string))
21933 {
21934 ptrdiff_t endptr;
21935 struct face *face;
21936
21937 it->face_id
21938 = face_at_string_position (it->w, face_string, face_string_pos,
21939 0, it->region_beg_charpos,
21940 it->region_end_charpos,
21941 &endptr, it->base_face_id, 0);
21942 face = FACE_FROM_ID (it->f, it->face_id);
21943 it->face_box_p = face->box != FACE_NO_BOX;
21944 }
21945
21946 /* Set max_x to the maximum allowed X position. Don't let it go
21947 beyond the right edge of the window. */
21948 if (max_x <= 0)
21949 max_x = it->last_visible_x;
21950 else
21951 max_x = min (max_x, it->last_visible_x);
21952
21953 /* Skip over display elements that are not visible. because IT->w is
21954 hscrolled. */
21955 if (it->current_x < it->first_visible_x)
21956 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21957 MOVE_TO_POS | MOVE_TO_X);
21958
21959 row->ascent = it->max_ascent;
21960 row->height = it->max_ascent + it->max_descent;
21961 row->phys_ascent = it->max_phys_ascent;
21962 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21963 row->extra_line_spacing = it->max_extra_line_spacing;
21964
21965 if (STRINGP (it->string))
21966 it_charpos = IT_STRING_CHARPOS (*it);
21967 else
21968 it_charpos = IT_CHARPOS (*it);
21969
21970 /* This condition is for the case that we are called with current_x
21971 past last_visible_x. */
21972 while (it->current_x < max_x)
21973 {
21974 int x_before, x, n_glyphs_before, i, nglyphs;
21975
21976 /* Get the next display element. */
21977 if (!get_next_display_element (it))
21978 break;
21979
21980 /* Produce glyphs. */
21981 x_before = it->current_x;
21982 n_glyphs_before = row->used[TEXT_AREA];
21983 PRODUCE_GLYPHS (it);
21984
21985 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21986 i = 0;
21987 x = x_before;
21988 while (i < nglyphs)
21989 {
21990 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21991
21992 if (it->line_wrap != TRUNCATE
21993 && x + glyph->pixel_width > max_x)
21994 {
21995 /* End of continued line or max_x reached. */
21996 if (CHAR_GLYPH_PADDING_P (*glyph))
21997 {
21998 /* A wide character is unbreakable. */
21999 if (row->reversed_p)
22000 unproduce_glyphs (it, row->used[TEXT_AREA]
22001 - n_glyphs_before);
22002 row->used[TEXT_AREA] = n_glyphs_before;
22003 it->current_x = x_before;
22004 }
22005 else
22006 {
22007 if (row->reversed_p)
22008 unproduce_glyphs (it, row->used[TEXT_AREA]
22009 - (n_glyphs_before + i));
22010 row->used[TEXT_AREA] = n_glyphs_before + i;
22011 it->current_x = x;
22012 }
22013 break;
22014 }
22015 else if (x + glyph->pixel_width >= it->first_visible_x)
22016 {
22017 /* Glyph is at least partially visible. */
22018 ++it->hpos;
22019 if (x < it->first_visible_x)
22020 row->x = x - it->first_visible_x;
22021 }
22022 else
22023 {
22024 /* Glyph is off the left margin of the display area.
22025 Should not happen. */
22026 emacs_abort ();
22027 }
22028
22029 row->ascent = max (row->ascent, it->max_ascent);
22030 row->height = max (row->height, it->max_ascent + it->max_descent);
22031 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22032 row->phys_height = max (row->phys_height,
22033 it->max_phys_ascent + it->max_phys_descent);
22034 row->extra_line_spacing = max (row->extra_line_spacing,
22035 it->max_extra_line_spacing);
22036 x += glyph->pixel_width;
22037 ++i;
22038 }
22039
22040 /* Stop if max_x reached. */
22041 if (i < nglyphs)
22042 break;
22043
22044 /* Stop at line ends. */
22045 if (ITERATOR_AT_END_OF_LINE_P (it))
22046 {
22047 it->continuation_lines_width = 0;
22048 break;
22049 }
22050
22051 set_iterator_to_next (it, 1);
22052 if (STRINGP (it->string))
22053 it_charpos = IT_STRING_CHARPOS (*it);
22054 else
22055 it_charpos = IT_CHARPOS (*it);
22056
22057 /* Stop if truncating at the right edge. */
22058 if (it->line_wrap == TRUNCATE
22059 && it->current_x >= it->last_visible_x)
22060 {
22061 /* Add truncation mark, but don't do it if the line is
22062 truncated at a padding space. */
22063 if (it_charpos < it->string_nchars)
22064 {
22065 if (!FRAME_WINDOW_P (it->f))
22066 {
22067 int ii, n;
22068
22069 if (it->current_x > it->last_visible_x)
22070 {
22071 if (!row->reversed_p)
22072 {
22073 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22074 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22075 break;
22076 }
22077 else
22078 {
22079 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22080 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22081 break;
22082 unproduce_glyphs (it, ii + 1);
22083 ii = row->used[TEXT_AREA] - (ii + 1);
22084 }
22085 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22086 {
22087 row->used[TEXT_AREA] = ii;
22088 produce_special_glyphs (it, IT_TRUNCATION);
22089 }
22090 }
22091 produce_special_glyphs (it, IT_TRUNCATION);
22092 }
22093 row->truncated_on_right_p = 1;
22094 }
22095 break;
22096 }
22097 }
22098
22099 /* Maybe insert a truncation at the left. */
22100 if (it->first_visible_x
22101 && it_charpos > 0)
22102 {
22103 if (!FRAME_WINDOW_P (it->f)
22104 || (row->reversed_p
22105 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22106 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22107 insert_left_trunc_glyphs (it);
22108 row->truncated_on_left_p = 1;
22109 }
22110
22111 it->face_id = saved_face_id;
22112
22113 /* Value is number of columns displayed. */
22114 return it->hpos - hpos_at_start;
22115 }
22116
22117
22118 \f
22119 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22120 appears as an element of LIST or as the car of an element of LIST.
22121 If PROPVAL is a list, compare each element against LIST in that
22122 way, and return 1/2 if any element of PROPVAL is found in LIST.
22123 Otherwise return 0. This function cannot quit.
22124 The return value is 2 if the text is invisible but with an ellipsis
22125 and 1 if it's invisible and without an ellipsis. */
22126
22127 int
22128 invisible_p (register Lisp_Object propval, Lisp_Object list)
22129 {
22130 register Lisp_Object tail, proptail;
22131
22132 for (tail = list; CONSP (tail); tail = XCDR (tail))
22133 {
22134 register Lisp_Object tem;
22135 tem = XCAR (tail);
22136 if (EQ (propval, tem))
22137 return 1;
22138 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22139 return NILP (XCDR (tem)) ? 1 : 2;
22140 }
22141
22142 if (CONSP (propval))
22143 {
22144 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22145 {
22146 Lisp_Object propelt;
22147 propelt = XCAR (proptail);
22148 for (tail = list; CONSP (tail); tail = XCDR (tail))
22149 {
22150 register Lisp_Object tem;
22151 tem = XCAR (tail);
22152 if (EQ (propelt, tem))
22153 return 1;
22154 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22155 return NILP (XCDR (tem)) ? 1 : 2;
22156 }
22157 }
22158 }
22159
22160 return 0;
22161 }
22162
22163 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22164 doc: /* Non-nil if the property makes the text invisible.
22165 POS-OR-PROP can be a marker or number, in which case it is taken to be
22166 a position in the current buffer and the value of the `invisible' property
22167 is checked; or it can be some other value, which is then presumed to be the
22168 value of the `invisible' property of the text of interest.
22169 The non-nil value returned can be t for truly invisible text or something
22170 else if the text is replaced by an ellipsis. */)
22171 (Lisp_Object pos_or_prop)
22172 {
22173 Lisp_Object prop
22174 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22175 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22176 : pos_or_prop);
22177 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22178 return (invis == 0 ? Qnil
22179 : invis == 1 ? Qt
22180 : make_number (invis));
22181 }
22182
22183 /* Calculate a width or height in pixels from a specification using
22184 the following elements:
22185
22186 SPEC ::=
22187 NUM - a (fractional) multiple of the default font width/height
22188 (NUM) - specifies exactly NUM pixels
22189 UNIT - a fixed number of pixels, see below.
22190 ELEMENT - size of a display element in pixels, see below.
22191 (NUM . SPEC) - equals NUM * SPEC
22192 (+ SPEC SPEC ...) - add pixel values
22193 (- SPEC SPEC ...) - subtract pixel values
22194 (- SPEC) - negate pixel value
22195
22196 NUM ::=
22197 INT or FLOAT - a number constant
22198 SYMBOL - use symbol's (buffer local) variable binding.
22199
22200 UNIT ::=
22201 in - pixels per inch *)
22202 mm - pixels per 1/1000 meter *)
22203 cm - pixels per 1/100 meter *)
22204 width - width of current font in pixels.
22205 height - height of current font in pixels.
22206
22207 *) using the ratio(s) defined in display-pixels-per-inch.
22208
22209 ELEMENT ::=
22210
22211 left-fringe - left fringe width in pixels
22212 right-fringe - right fringe width in pixels
22213
22214 left-margin - left margin width in pixels
22215 right-margin - right margin width in pixels
22216
22217 scroll-bar - scroll-bar area width in pixels
22218
22219 Examples:
22220
22221 Pixels corresponding to 5 inches:
22222 (5 . in)
22223
22224 Total width of non-text areas on left side of window (if scroll-bar is on left):
22225 '(space :width (+ left-fringe left-margin scroll-bar))
22226
22227 Align to first text column (in header line):
22228 '(space :align-to 0)
22229
22230 Align to middle of text area minus half the width of variable `my-image'
22231 containing a loaded image:
22232 '(space :align-to (0.5 . (- text my-image)))
22233
22234 Width of left margin minus width of 1 character in the default font:
22235 '(space :width (- left-margin 1))
22236
22237 Width of left margin minus width of 2 characters in the current font:
22238 '(space :width (- left-margin (2 . width)))
22239
22240 Center 1 character over left-margin (in header line):
22241 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22242
22243 Different ways to express width of left fringe plus left margin minus one pixel:
22244 '(space :width (- (+ left-fringe left-margin) (1)))
22245 '(space :width (+ left-fringe left-margin (- (1))))
22246 '(space :width (+ left-fringe left-margin (-1)))
22247
22248 */
22249
22250 #define NUMVAL(X) \
22251 ((INTEGERP (X) || FLOATP (X)) \
22252 ? XFLOATINT (X) \
22253 : - 1)
22254
22255 static int
22256 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22257 struct font *font, int width_p, int *align_to)
22258 {
22259 double pixels;
22260
22261 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22262 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22263
22264 if (NILP (prop))
22265 return OK_PIXELS (0);
22266
22267 eassert (FRAME_LIVE_P (it->f));
22268
22269 if (SYMBOLP (prop))
22270 {
22271 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22272 {
22273 char *unit = SSDATA (SYMBOL_NAME (prop));
22274
22275 if (unit[0] == 'i' && unit[1] == 'n')
22276 pixels = 1.0;
22277 else if (unit[0] == 'm' && unit[1] == 'm')
22278 pixels = 25.4;
22279 else if (unit[0] == 'c' && unit[1] == 'm')
22280 pixels = 2.54;
22281 else
22282 pixels = 0;
22283 if (pixels > 0)
22284 {
22285 double ppi;
22286 #ifdef HAVE_WINDOW_SYSTEM
22287 if (FRAME_WINDOW_P (it->f)
22288 && (ppi = (width_p
22289 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22290 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22291 ppi > 0))
22292 return OK_PIXELS (ppi / pixels);
22293 #endif
22294
22295 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22296 || (CONSP (Vdisplay_pixels_per_inch)
22297 && (ppi = (width_p
22298 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22299 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22300 ppi > 0)))
22301 return OK_PIXELS (ppi / pixels);
22302
22303 return 0;
22304 }
22305 }
22306
22307 #ifdef HAVE_WINDOW_SYSTEM
22308 if (EQ (prop, Qheight))
22309 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22310 if (EQ (prop, Qwidth))
22311 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22312 #else
22313 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22314 return OK_PIXELS (1);
22315 #endif
22316
22317 if (EQ (prop, Qtext))
22318 return OK_PIXELS (width_p
22319 ? window_box_width (it->w, TEXT_AREA)
22320 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22321
22322 if (align_to && *align_to < 0)
22323 {
22324 *res = 0;
22325 if (EQ (prop, Qleft))
22326 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22327 if (EQ (prop, Qright))
22328 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22329 if (EQ (prop, Qcenter))
22330 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22331 + window_box_width (it->w, TEXT_AREA) / 2);
22332 if (EQ (prop, Qleft_fringe))
22333 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22334 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22335 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22336 if (EQ (prop, Qright_fringe))
22337 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22338 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22339 : window_box_right_offset (it->w, TEXT_AREA));
22340 if (EQ (prop, Qleft_margin))
22341 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22342 if (EQ (prop, Qright_margin))
22343 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22344 if (EQ (prop, Qscroll_bar))
22345 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22346 ? 0
22347 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22348 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22349 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22350 : 0)));
22351 }
22352 else
22353 {
22354 if (EQ (prop, Qleft_fringe))
22355 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22356 if (EQ (prop, Qright_fringe))
22357 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22358 if (EQ (prop, Qleft_margin))
22359 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22360 if (EQ (prop, Qright_margin))
22361 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22362 if (EQ (prop, Qscroll_bar))
22363 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22364 }
22365
22366 prop = buffer_local_value_1 (prop, it->w->buffer);
22367 if (EQ (prop, Qunbound))
22368 prop = Qnil;
22369 }
22370
22371 if (INTEGERP (prop) || FLOATP (prop))
22372 {
22373 int base_unit = (width_p
22374 ? FRAME_COLUMN_WIDTH (it->f)
22375 : FRAME_LINE_HEIGHT (it->f));
22376 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22377 }
22378
22379 if (CONSP (prop))
22380 {
22381 Lisp_Object car = XCAR (prop);
22382 Lisp_Object cdr = XCDR (prop);
22383
22384 if (SYMBOLP (car))
22385 {
22386 #ifdef HAVE_WINDOW_SYSTEM
22387 if (FRAME_WINDOW_P (it->f)
22388 && valid_image_p (prop))
22389 {
22390 ptrdiff_t id = lookup_image (it->f, prop);
22391 struct image *img = IMAGE_FROM_ID (it->f, id);
22392
22393 return OK_PIXELS (width_p ? img->width : img->height);
22394 }
22395 #endif
22396 if (EQ (car, Qplus) || EQ (car, Qminus))
22397 {
22398 int first = 1;
22399 double px;
22400
22401 pixels = 0;
22402 while (CONSP (cdr))
22403 {
22404 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22405 font, width_p, align_to))
22406 return 0;
22407 if (first)
22408 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22409 else
22410 pixels += px;
22411 cdr = XCDR (cdr);
22412 }
22413 if (EQ (car, Qminus))
22414 pixels = -pixels;
22415 return OK_PIXELS (pixels);
22416 }
22417
22418 car = buffer_local_value_1 (car, it->w->buffer);
22419 if (EQ (car, Qunbound))
22420 car = Qnil;
22421 }
22422
22423 if (INTEGERP (car) || FLOATP (car))
22424 {
22425 double fact;
22426 pixels = XFLOATINT (car);
22427 if (NILP (cdr))
22428 return OK_PIXELS (pixels);
22429 if (calc_pixel_width_or_height (&fact, it, cdr,
22430 font, width_p, align_to))
22431 return OK_PIXELS (pixels * fact);
22432 return 0;
22433 }
22434
22435 return 0;
22436 }
22437
22438 return 0;
22439 }
22440
22441 \f
22442 /***********************************************************************
22443 Glyph Display
22444 ***********************************************************************/
22445
22446 #ifdef HAVE_WINDOW_SYSTEM
22447
22448 #ifdef GLYPH_DEBUG
22449
22450 void
22451 dump_glyph_string (struct glyph_string *s)
22452 {
22453 fprintf (stderr, "glyph string\n");
22454 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22455 s->x, s->y, s->width, s->height);
22456 fprintf (stderr, " ybase = %d\n", s->ybase);
22457 fprintf (stderr, " hl = %d\n", s->hl);
22458 fprintf (stderr, " left overhang = %d, right = %d\n",
22459 s->left_overhang, s->right_overhang);
22460 fprintf (stderr, " nchars = %d\n", s->nchars);
22461 fprintf (stderr, " extends to end of line = %d\n",
22462 s->extends_to_end_of_line_p);
22463 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22464 fprintf (stderr, " bg width = %d\n", s->background_width);
22465 }
22466
22467 #endif /* GLYPH_DEBUG */
22468
22469 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22470 of XChar2b structures for S; it can't be allocated in
22471 init_glyph_string because it must be allocated via `alloca'. W
22472 is the window on which S is drawn. ROW and AREA are the glyph row
22473 and area within the row from which S is constructed. START is the
22474 index of the first glyph structure covered by S. HL is a
22475 face-override for drawing S. */
22476
22477 #ifdef HAVE_NTGUI
22478 #define OPTIONAL_HDC(hdc) HDC hdc,
22479 #define DECLARE_HDC(hdc) HDC hdc;
22480 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22481 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22482 #endif
22483
22484 #ifndef OPTIONAL_HDC
22485 #define OPTIONAL_HDC(hdc)
22486 #define DECLARE_HDC(hdc)
22487 #define ALLOCATE_HDC(hdc, f)
22488 #define RELEASE_HDC(hdc, f)
22489 #endif
22490
22491 static void
22492 init_glyph_string (struct glyph_string *s,
22493 OPTIONAL_HDC (hdc)
22494 XChar2b *char2b, struct window *w, struct glyph_row *row,
22495 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22496 {
22497 memset (s, 0, sizeof *s);
22498 s->w = w;
22499 s->f = XFRAME (w->frame);
22500 #ifdef HAVE_NTGUI
22501 s->hdc = hdc;
22502 #endif
22503 s->display = FRAME_X_DISPLAY (s->f);
22504 s->window = FRAME_X_WINDOW (s->f);
22505 s->char2b = char2b;
22506 s->hl = hl;
22507 s->row = row;
22508 s->area = area;
22509 s->first_glyph = row->glyphs[area] + start;
22510 s->height = row->height;
22511 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22512 s->ybase = s->y + row->ascent;
22513 }
22514
22515
22516 /* Append the list of glyph strings with head H and tail T to the list
22517 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22518
22519 static void
22520 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22521 struct glyph_string *h, struct glyph_string *t)
22522 {
22523 if (h)
22524 {
22525 if (*head)
22526 (*tail)->next = h;
22527 else
22528 *head = h;
22529 h->prev = *tail;
22530 *tail = t;
22531 }
22532 }
22533
22534
22535 /* Prepend the list of glyph strings with head H and tail T to the
22536 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22537 result. */
22538
22539 static void
22540 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22541 struct glyph_string *h, struct glyph_string *t)
22542 {
22543 if (h)
22544 {
22545 if (*head)
22546 (*head)->prev = t;
22547 else
22548 *tail = t;
22549 t->next = *head;
22550 *head = h;
22551 }
22552 }
22553
22554
22555 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22556 Set *HEAD and *TAIL to the resulting list. */
22557
22558 static void
22559 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22560 struct glyph_string *s)
22561 {
22562 s->next = s->prev = NULL;
22563 append_glyph_string_lists (head, tail, s, s);
22564 }
22565
22566
22567 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22568 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22569 make sure that X resources for the face returned are allocated.
22570 Value is a pointer to a realized face that is ready for display if
22571 DISPLAY_P is non-zero. */
22572
22573 static struct face *
22574 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22575 XChar2b *char2b, int display_p)
22576 {
22577 struct face *face = FACE_FROM_ID (f, face_id);
22578
22579 if (face->font)
22580 {
22581 unsigned code = face->font->driver->encode_char (face->font, c);
22582
22583 if (code != FONT_INVALID_CODE)
22584 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22585 else
22586 STORE_XCHAR2B (char2b, 0, 0);
22587 }
22588
22589 /* Make sure X resources of the face are allocated. */
22590 #ifdef HAVE_X_WINDOWS
22591 if (display_p)
22592 #endif
22593 {
22594 eassert (face != NULL);
22595 PREPARE_FACE_FOR_DISPLAY (f, face);
22596 }
22597
22598 return face;
22599 }
22600
22601
22602 /* Get face and two-byte form of character glyph GLYPH on frame F.
22603 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22604 a pointer to a realized face that is ready for display. */
22605
22606 static struct face *
22607 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22608 XChar2b *char2b, int *two_byte_p)
22609 {
22610 struct face *face;
22611
22612 eassert (glyph->type == CHAR_GLYPH);
22613 face = FACE_FROM_ID (f, glyph->face_id);
22614
22615 if (two_byte_p)
22616 *two_byte_p = 0;
22617
22618 if (face->font)
22619 {
22620 unsigned code;
22621
22622 if (CHAR_BYTE8_P (glyph->u.ch))
22623 code = CHAR_TO_BYTE8 (glyph->u.ch);
22624 else
22625 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22626
22627 if (code != FONT_INVALID_CODE)
22628 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22629 else
22630 STORE_XCHAR2B (char2b, 0, 0);
22631 }
22632
22633 /* Make sure X resources of the face are allocated. */
22634 eassert (face != NULL);
22635 PREPARE_FACE_FOR_DISPLAY (f, face);
22636 return face;
22637 }
22638
22639
22640 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22641 Return 1 if FONT has a glyph for C, otherwise return 0. */
22642
22643 static int
22644 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22645 {
22646 unsigned code;
22647
22648 if (CHAR_BYTE8_P (c))
22649 code = CHAR_TO_BYTE8 (c);
22650 else
22651 code = font->driver->encode_char (font, c);
22652
22653 if (code == FONT_INVALID_CODE)
22654 return 0;
22655 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22656 return 1;
22657 }
22658
22659
22660 /* Fill glyph string S with composition components specified by S->cmp.
22661
22662 BASE_FACE is the base face of the composition.
22663 S->cmp_from is the index of the first component for S.
22664
22665 OVERLAPS non-zero means S should draw the foreground only, and use
22666 its physical height for clipping. See also draw_glyphs.
22667
22668 Value is the index of a component not in S. */
22669
22670 static int
22671 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22672 int overlaps)
22673 {
22674 int i;
22675 /* For all glyphs of this composition, starting at the offset
22676 S->cmp_from, until we reach the end of the definition or encounter a
22677 glyph that requires the different face, add it to S. */
22678 struct face *face;
22679
22680 eassert (s);
22681
22682 s->for_overlaps = overlaps;
22683 s->face = NULL;
22684 s->font = NULL;
22685 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22686 {
22687 int c = COMPOSITION_GLYPH (s->cmp, i);
22688
22689 /* TAB in a composition means display glyphs with padding space
22690 on the left or right. */
22691 if (c != '\t')
22692 {
22693 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22694 -1, Qnil);
22695
22696 face = get_char_face_and_encoding (s->f, c, face_id,
22697 s->char2b + i, 1);
22698 if (face)
22699 {
22700 if (! s->face)
22701 {
22702 s->face = face;
22703 s->font = s->face->font;
22704 }
22705 else if (s->face != face)
22706 break;
22707 }
22708 }
22709 ++s->nchars;
22710 }
22711 s->cmp_to = i;
22712
22713 if (s->face == NULL)
22714 {
22715 s->face = base_face->ascii_face;
22716 s->font = s->face->font;
22717 }
22718
22719 /* All glyph strings for the same composition has the same width,
22720 i.e. the width set for the first component of the composition. */
22721 s->width = s->first_glyph->pixel_width;
22722
22723 /* If the specified font could not be loaded, use the frame's
22724 default font, but record the fact that we couldn't load it in
22725 the glyph string so that we can draw rectangles for the
22726 characters of the glyph string. */
22727 if (s->font == NULL)
22728 {
22729 s->font_not_found_p = 1;
22730 s->font = FRAME_FONT (s->f);
22731 }
22732
22733 /* Adjust base line for subscript/superscript text. */
22734 s->ybase += s->first_glyph->voffset;
22735
22736 /* This glyph string must always be drawn with 16-bit functions. */
22737 s->two_byte_p = 1;
22738
22739 return s->cmp_to;
22740 }
22741
22742 static int
22743 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22744 int start, int end, int overlaps)
22745 {
22746 struct glyph *glyph, *last;
22747 Lisp_Object lgstring;
22748 int i;
22749
22750 s->for_overlaps = overlaps;
22751 glyph = s->row->glyphs[s->area] + start;
22752 last = s->row->glyphs[s->area] + end;
22753 s->cmp_id = glyph->u.cmp.id;
22754 s->cmp_from = glyph->slice.cmp.from;
22755 s->cmp_to = glyph->slice.cmp.to + 1;
22756 s->face = FACE_FROM_ID (s->f, face_id);
22757 lgstring = composition_gstring_from_id (s->cmp_id);
22758 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22759 glyph++;
22760 while (glyph < last
22761 && glyph->u.cmp.automatic
22762 && glyph->u.cmp.id == s->cmp_id
22763 && s->cmp_to == glyph->slice.cmp.from)
22764 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22765
22766 for (i = s->cmp_from; i < s->cmp_to; i++)
22767 {
22768 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22769 unsigned code = LGLYPH_CODE (lglyph);
22770
22771 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22772 }
22773 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22774 return glyph - s->row->glyphs[s->area];
22775 }
22776
22777
22778 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22779 See the comment of fill_glyph_string for arguments.
22780 Value is the index of the first glyph not in S. */
22781
22782
22783 static int
22784 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22785 int start, int end, int overlaps)
22786 {
22787 struct glyph *glyph, *last;
22788 int voffset;
22789
22790 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22791 s->for_overlaps = overlaps;
22792 glyph = s->row->glyphs[s->area] + start;
22793 last = s->row->glyphs[s->area] + end;
22794 voffset = glyph->voffset;
22795 s->face = FACE_FROM_ID (s->f, face_id);
22796 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22797 s->nchars = 1;
22798 s->width = glyph->pixel_width;
22799 glyph++;
22800 while (glyph < last
22801 && glyph->type == GLYPHLESS_GLYPH
22802 && glyph->voffset == voffset
22803 && glyph->face_id == face_id)
22804 {
22805 s->nchars++;
22806 s->width += glyph->pixel_width;
22807 glyph++;
22808 }
22809 s->ybase += voffset;
22810 return glyph - s->row->glyphs[s->area];
22811 }
22812
22813
22814 /* Fill glyph string S from a sequence of character glyphs.
22815
22816 FACE_ID is the face id of the string. START is the index of the
22817 first glyph to consider, END is the index of the last + 1.
22818 OVERLAPS non-zero means S should draw the foreground only, and use
22819 its physical height for clipping. See also draw_glyphs.
22820
22821 Value is the index of the first glyph not in S. */
22822
22823 static int
22824 fill_glyph_string (struct glyph_string *s, int face_id,
22825 int start, int end, int overlaps)
22826 {
22827 struct glyph *glyph, *last;
22828 int voffset;
22829 int glyph_not_available_p;
22830
22831 eassert (s->f == XFRAME (s->w->frame));
22832 eassert (s->nchars == 0);
22833 eassert (start >= 0 && end > start);
22834
22835 s->for_overlaps = overlaps;
22836 glyph = s->row->glyphs[s->area] + start;
22837 last = s->row->glyphs[s->area] + end;
22838 voffset = glyph->voffset;
22839 s->padding_p = glyph->padding_p;
22840 glyph_not_available_p = glyph->glyph_not_available_p;
22841
22842 while (glyph < last
22843 && glyph->type == CHAR_GLYPH
22844 && glyph->voffset == voffset
22845 /* Same face id implies same font, nowadays. */
22846 && glyph->face_id == face_id
22847 && glyph->glyph_not_available_p == glyph_not_available_p)
22848 {
22849 int two_byte_p;
22850
22851 s->face = get_glyph_face_and_encoding (s->f, glyph,
22852 s->char2b + s->nchars,
22853 &two_byte_p);
22854 s->two_byte_p = two_byte_p;
22855 ++s->nchars;
22856 eassert (s->nchars <= end - start);
22857 s->width += glyph->pixel_width;
22858 if (glyph++->padding_p != s->padding_p)
22859 break;
22860 }
22861
22862 s->font = s->face->font;
22863
22864 /* If the specified font could not be loaded, use the frame's font,
22865 but record the fact that we couldn't load it in
22866 S->font_not_found_p so that we can draw rectangles for the
22867 characters of the glyph string. */
22868 if (s->font == NULL || glyph_not_available_p)
22869 {
22870 s->font_not_found_p = 1;
22871 s->font = FRAME_FONT (s->f);
22872 }
22873
22874 /* Adjust base line for subscript/superscript text. */
22875 s->ybase += voffset;
22876
22877 eassert (s->face && s->face->gc);
22878 return glyph - s->row->glyphs[s->area];
22879 }
22880
22881
22882 /* Fill glyph string S from image glyph S->first_glyph. */
22883
22884 static void
22885 fill_image_glyph_string (struct glyph_string *s)
22886 {
22887 eassert (s->first_glyph->type == IMAGE_GLYPH);
22888 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22889 eassert (s->img);
22890 s->slice = s->first_glyph->slice.img;
22891 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22892 s->font = s->face->font;
22893 s->width = s->first_glyph->pixel_width;
22894
22895 /* Adjust base line for subscript/superscript text. */
22896 s->ybase += s->first_glyph->voffset;
22897 }
22898
22899
22900 /* Fill glyph string S from a sequence of stretch glyphs.
22901
22902 START is the index of the first glyph to consider,
22903 END is the index of the last + 1.
22904
22905 Value is the index of the first glyph not in S. */
22906
22907 static int
22908 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22909 {
22910 struct glyph *glyph, *last;
22911 int voffset, face_id;
22912
22913 eassert (s->first_glyph->type == STRETCH_GLYPH);
22914
22915 glyph = s->row->glyphs[s->area] + start;
22916 last = s->row->glyphs[s->area] + end;
22917 face_id = glyph->face_id;
22918 s->face = FACE_FROM_ID (s->f, face_id);
22919 s->font = s->face->font;
22920 s->width = glyph->pixel_width;
22921 s->nchars = 1;
22922 voffset = glyph->voffset;
22923
22924 for (++glyph;
22925 (glyph < last
22926 && glyph->type == STRETCH_GLYPH
22927 && glyph->voffset == voffset
22928 && glyph->face_id == face_id);
22929 ++glyph)
22930 s->width += glyph->pixel_width;
22931
22932 /* Adjust base line for subscript/superscript text. */
22933 s->ybase += voffset;
22934
22935 /* The case that face->gc == 0 is handled when drawing the glyph
22936 string by calling PREPARE_FACE_FOR_DISPLAY. */
22937 eassert (s->face);
22938 return glyph - s->row->glyphs[s->area];
22939 }
22940
22941 static struct font_metrics *
22942 get_per_char_metric (struct font *font, XChar2b *char2b)
22943 {
22944 static struct font_metrics metrics;
22945 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22946
22947 if (! font || code == FONT_INVALID_CODE)
22948 return NULL;
22949 font->driver->text_extents (font, &code, 1, &metrics);
22950 return &metrics;
22951 }
22952
22953 /* EXPORT for RIF:
22954 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22955 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22956 assumed to be zero. */
22957
22958 void
22959 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22960 {
22961 *left = *right = 0;
22962
22963 if (glyph->type == CHAR_GLYPH)
22964 {
22965 struct face *face;
22966 XChar2b char2b;
22967 struct font_metrics *pcm;
22968
22969 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22970 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22971 {
22972 if (pcm->rbearing > pcm->width)
22973 *right = pcm->rbearing - pcm->width;
22974 if (pcm->lbearing < 0)
22975 *left = -pcm->lbearing;
22976 }
22977 }
22978 else if (glyph->type == COMPOSITE_GLYPH)
22979 {
22980 if (! glyph->u.cmp.automatic)
22981 {
22982 struct composition *cmp = composition_table[glyph->u.cmp.id];
22983
22984 if (cmp->rbearing > cmp->pixel_width)
22985 *right = cmp->rbearing - cmp->pixel_width;
22986 if (cmp->lbearing < 0)
22987 *left = - cmp->lbearing;
22988 }
22989 else
22990 {
22991 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22992 struct font_metrics metrics;
22993
22994 composition_gstring_width (gstring, glyph->slice.cmp.from,
22995 glyph->slice.cmp.to + 1, &metrics);
22996 if (metrics.rbearing > metrics.width)
22997 *right = metrics.rbearing - metrics.width;
22998 if (metrics.lbearing < 0)
22999 *left = - metrics.lbearing;
23000 }
23001 }
23002 }
23003
23004
23005 /* Return the index of the first glyph preceding glyph string S that
23006 is overwritten by S because of S's left overhang. Value is -1
23007 if no glyphs are overwritten. */
23008
23009 static int
23010 left_overwritten (struct glyph_string *s)
23011 {
23012 int k;
23013
23014 if (s->left_overhang)
23015 {
23016 int x = 0, i;
23017 struct glyph *glyphs = s->row->glyphs[s->area];
23018 int first = s->first_glyph - glyphs;
23019
23020 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23021 x -= glyphs[i].pixel_width;
23022
23023 k = i + 1;
23024 }
23025 else
23026 k = -1;
23027
23028 return k;
23029 }
23030
23031
23032 /* Return the index of the first glyph preceding glyph string S that
23033 is overwriting S because of its right overhang. Value is -1 if no
23034 glyph in front of S overwrites S. */
23035
23036 static int
23037 left_overwriting (struct glyph_string *s)
23038 {
23039 int i, k, x;
23040 struct glyph *glyphs = s->row->glyphs[s->area];
23041 int first = s->first_glyph - glyphs;
23042
23043 k = -1;
23044 x = 0;
23045 for (i = first - 1; i >= 0; --i)
23046 {
23047 int left, right;
23048 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23049 if (x + right > 0)
23050 k = i;
23051 x -= glyphs[i].pixel_width;
23052 }
23053
23054 return k;
23055 }
23056
23057
23058 /* Return the index of the last glyph following glyph string S that is
23059 overwritten by S because of S's right overhang. Value is -1 if
23060 no such glyph is found. */
23061
23062 static int
23063 right_overwritten (struct glyph_string *s)
23064 {
23065 int k = -1;
23066
23067 if (s->right_overhang)
23068 {
23069 int x = 0, i;
23070 struct glyph *glyphs = s->row->glyphs[s->area];
23071 int first = (s->first_glyph - glyphs
23072 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23073 int end = s->row->used[s->area];
23074
23075 for (i = first; i < end && s->right_overhang > x; ++i)
23076 x += glyphs[i].pixel_width;
23077
23078 k = i;
23079 }
23080
23081 return k;
23082 }
23083
23084
23085 /* Return the index of the last glyph following glyph string S that
23086 overwrites S because of its left overhang. Value is negative
23087 if no such glyph is found. */
23088
23089 static int
23090 right_overwriting (struct glyph_string *s)
23091 {
23092 int i, k, x;
23093 int end = s->row->used[s->area];
23094 struct glyph *glyphs = s->row->glyphs[s->area];
23095 int first = (s->first_glyph - glyphs
23096 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23097
23098 k = -1;
23099 x = 0;
23100 for (i = first; i < end; ++i)
23101 {
23102 int left, right;
23103 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23104 if (x - left < 0)
23105 k = i;
23106 x += glyphs[i].pixel_width;
23107 }
23108
23109 return k;
23110 }
23111
23112
23113 /* Set background width of glyph string S. START is the index of the
23114 first glyph following S. LAST_X is the right-most x-position + 1
23115 in the drawing area. */
23116
23117 static void
23118 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23119 {
23120 /* If the face of this glyph string has to be drawn to the end of
23121 the drawing area, set S->extends_to_end_of_line_p. */
23122
23123 if (start == s->row->used[s->area]
23124 && s->area == TEXT_AREA
23125 && ((s->row->fill_line_p
23126 && (s->hl == DRAW_NORMAL_TEXT
23127 || s->hl == DRAW_IMAGE_RAISED
23128 || s->hl == DRAW_IMAGE_SUNKEN))
23129 || s->hl == DRAW_MOUSE_FACE))
23130 s->extends_to_end_of_line_p = 1;
23131
23132 /* If S extends its face to the end of the line, set its
23133 background_width to the distance to the right edge of the drawing
23134 area. */
23135 if (s->extends_to_end_of_line_p)
23136 s->background_width = last_x - s->x + 1;
23137 else
23138 s->background_width = s->width;
23139 }
23140
23141
23142 /* Compute overhangs and x-positions for glyph string S and its
23143 predecessors, or successors. X is the starting x-position for S.
23144 BACKWARD_P non-zero means process predecessors. */
23145
23146 static void
23147 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23148 {
23149 if (backward_p)
23150 {
23151 while (s)
23152 {
23153 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23154 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23155 x -= s->width;
23156 s->x = x;
23157 s = s->prev;
23158 }
23159 }
23160 else
23161 {
23162 while (s)
23163 {
23164 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23165 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23166 s->x = x;
23167 x += s->width;
23168 s = s->next;
23169 }
23170 }
23171 }
23172
23173
23174
23175 /* The following macros are only called from draw_glyphs below.
23176 They reference the following parameters of that function directly:
23177 `w', `row', `area', and `overlap_p'
23178 as well as the following local variables:
23179 `s', `f', and `hdc' (in W32) */
23180
23181 #ifdef HAVE_NTGUI
23182 /* On W32, silently add local `hdc' variable to argument list of
23183 init_glyph_string. */
23184 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23185 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23186 #else
23187 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23188 init_glyph_string (s, char2b, w, row, area, start, hl)
23189 #endif
23190
23191 /* Add a glyph string for a stretch glyph to the list of strings
23192 between HEAD and TAIL. START is the index of the stretch glyph in
23193 row area AREA of glyph row ROW. END is the index of the last glyph
23194 in that glyph row area. X is the current output position assigned
23195 to the new glyph string constructed. HL overrides that face of the
23196 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23197 is the right-most x-position of the drawing area. */
23198
23199 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23200 and below -- keep them on one line. */
23201 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23202 do \
23203 { \
23204 s = alloca (sizeof *s); \
23205 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23206 START = fill_stretch_glyph_string (s, START, END); \
23207 append_glyph_string (&HEAD, &TAIL, s); \
23208 s->x = (X); \
23209 } \
23210 while (0)
23211
23212
23213 /* Add a glyph string for an image glyph to the list of strings
23214 between HEAD and TAIL. START is the index of the image glyph in
23215 row area AREA of glyph row ROW. END is the index of the last glyph
23216 in that glyph row area. X is the current output position assigned
23217 to the new glyph string constructed. HL overrides that face of the
23218 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23219 is the right-most x-position of the drawing area. */
23220
23221 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23222 do \
23223 { \
23224 s = alloca (sizeof *s); \
23225 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23226 fill_image_glyph_string (s); \
23227 append_glyph_string (&HEAD, &TAIL, s); \
23228 ++START; \
23229 s->x = (X); \
23230 } \
23231 while (0)
23232
23233
23234 /* Add a glyph string for a sequence of character glyphs to the list
23235 of strings between HEAD and TAIL. START is the index of the first
23236 glyph in row area AREA of glyph row ROW that is part of the new
23237 glyph string. END is the index of the last glyph in that glyph row
23238 area. X is the current output position assigned to the new glyph
23239 string constructed. HL overrides that face of the glyph; e.g. it
23240 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23241 right-most x-position of the drawing area. */
23242
23243 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23244 do \
23245 { \
23246 int face_id; \
23247 XChar2b *char2b; \
23248 \
23249 face_id = (row)->glyphs[area][START].face_id; \
23250 \
23251 s = alloca (sizeof *s); \
23252 char2b = alloca ((END - START) * sizeof *char2b); \
23253 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23254 append_glyph_string (&HEAD, &TAIL, s); \
23255 s->x = (X); \
23256 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23257 } \
23258 while (0)
23259
23260
23261 /* Add a glyph string for a composite sequence to the list of strings
23262 between HEAD and TAIL. START is the index of the first glyph in
23263 row area AREA of glyph row ROW that is part of the new glyph
23264 string. END is the index of the last glyph in that glyph row area.
23265 X is the current output position assigned to the new glyph string
23266 constructed. HL overrides that face of the glyph; e.g. it is
23267 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23268 x-position of the drawing area. */
23269
23270 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23271 do { \
23272 int face_id = (row)->glyphs[area][START].face_id; \
23273 struct face *base_face = FACE_FROM_ID (f, face_id); \
23274 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23275 struct composition *cmp = composition_table[cmp_id]; \
23276 XChar2b *char2b; \
23277 struct glyph_string *first_s = NULL; \
23278 int n; \
23279 \
23280 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23281 \
23282 /* Make glyph_strings for each glyph sequence that is drawable by \
23283 the same face, and append them to HEAD/TAIL. */ \
23284 for (n = 0; n < cmp->glyph_len;) \
23285 { \
23286 s = alloca (sizeof *s); \
23287 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23288 append_glyph_string (&(HEAD), &(TAIL), s); \
23289 s->cmp = cmp; \
23290 s->cmp_from = n; \
23291 s->x = (X); \
23292 if (n == 0) \
23293 first_s = s; \
23294 n = fill_composite_glyph_string (s, base_face, overlaps); \
23295 } \
23296 \
23297 ++START; \
23298 s = first_s; \
23299 } while (0)
23300
23301
23302 /* Add a glyph string for a glyph-string sequence to the list of strings
23303 between HEAD and TAIL. */
23304
23305 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23306 do { \
23307 int face_id; \
23308 XChar2b *char2b; \
23309 Lisp_Object gstring; \
23310 \
23311 face_id = (row)->glyphs[area][START].face_id; \
23312 gstring = (composition_gstring_from_id \
23313 ((row)->glyphs[area][START].u.cmp.id)); \
23314 s = alloca (sizeof *s); \
23315 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23316 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23317 append_glyph_string (&(HEAD), &(TAIL), s); \
23318 s->x = (X); \
23319 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23320 } while (0)
23321
23322
23323 /* Add a glyph string for a sequence of glyphless character's glyphs
23324 to the list of strings between HEAD and TAIL. The meanings of
23325 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23326
23327 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23328 do \
23329 { \
23330 int face_id; \
23331 \
23332 face_id = (row)->glyphs[area][START].face_id; \
23333 \
23334 s = alloca (sizeof *s); \
23335 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23336 append_glyph_string (&HEAD, &TAIL, s); \
23337 s->x = (X); \
23338 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23339 overlaps); \
23340 } \
23341 while (0)
23342
23343
23344 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23345 of AREA of glyph row ROW on window W between indices START and END.
23346 HL overrides the face for drawing glyph strings, e.g. it is
23347 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23348 x-positions of the drawing area.
23349
23350 This is an ugly monster macro construct because we must use alloca
23351 to allocate glyph strings (because draw_glyphs can be called
23352 asynchronously). */
23353
23354 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23355 do \
23356 { \
23357 HEAD = TAIL = NULL; \
23358 while (START < END) \
23359 { \
23360 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23361 switch (first_glyph->type) \
23362 { \
23363 case CHAR_GLYPH: \
23364 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23365 HL, X, LAST_X); \
23366 break; \
23367 \
23368 case COMPOSITE_GLYPH: \
23369 if (first_glyph->u.cmp.automatic) \
23370 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23371 HL, X, LAST_X); \
23372 else \
23373 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23374 HL, X, LAST_X); \
23375 break; \
23376 \
23377 case STRETCH_GLYPH: \
23378 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23379 HL, X, LAST_X); \
23380 break; \
23381 \
23382 case IMAGE_GLYPH: \
23383 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23384 HL, X, LAST_X); \
23385 break; \
23386 \
23387 case GLYPHLESS_GLYPH: \
23388 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23389 HL, X, LAST_X); \
23390 break; \
23391 \
23392 default: \
23393 emacs_abort (); \
23394 } \
23395 \
23396 if (s) \
23397 { \
23398 set_glyph_string_background_width (s, START, LAST_X); \
23399 (X) += s->width; \
23400 } \
23401 } \
23402 } while (0)
23403
23404
23405 /* Draw glyphs between START and END in AREA of ROW on window W,
23406 starting at x-position X. X is relative to AREA in W. HL is a
23407 face-override with the following meaning:
23408
23409 DRAW_NORMAL_TEXT draw normally
23410 DRAW_CURSOR draw in cursor face
23411 DRAW_MOUSE_FACE draw in mouse face.
23412 DRAW_INVERSE_VIDEO draw in mode line face
23413 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23414 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23415
23416 If OVERLAPS is non-zero, draw only the foreground of characters and
23417 clip to the physical height of ROW. Non-zero value also defines
23418 the overlapping part to be drawn:
23419
23420 OVERLAPS_PRED overlap with preceding rows
23421 OVERLAPS_SUCC overlap with succeeding rows
23422 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23423 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23424
23425 Value is the x-position reached, relative to AREA of W. */
23426
23427 static int
23428 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23429 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23430 enum draw_glyphs_face hl, int overlaps)
23431 {
23432 struct glyph_string *head, *tail;
23433 struct glyph_string *s;
23434 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23435 int i, j, x_reached, last_x, area_left = 0;
23436 struct frame *f = XFRAME (WINDOW_FRAME (w));
23437 DECLARE_HDC (hdc);
23438
23439 ALLOCATE_HDC (hdc, f);
23440
23441 /* Let's rather be paranoid than getting a SEGV. */
23442 end = min (end, row->used[area]);
23443 start = clip_to_bounds (0, start, end);
23444
23445 /* Translate X to frame coordinates. Set last_x to the right
23446 end of the drawing area. */
23447 if (row->full_width_p)
23448 {
23449 /* X is relative to the left edge of W, without scroll bars
23450 or fringes. */
23451 area_left = WINDOW_LEFT_EDGE_X (w);
23452 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23453 }
23454 else
23455 {
23456 area_left = window_box_left (w, area);
23457 last_x = area_left + window_box_width (w, area);
23458 }
23459 x += area_left;
23460
23461 /* Build a doubly-linked list of glyph_string structures between
23462 head and tail from what we have to draw. Note that the macro
23463 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23464 the reason we use a separate variable `i'. */
23465 i = start;
23466 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23467 if (tail)
23468 x_reached = tail->x + tail->background_width;
23469 else
23470 x_reached = x;
23471
23472 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23473 the row, redraw some glyphs in front or following the glyph
23474 strings built above. */
23475 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23476 {
23477 struct glyph_string *h, *t;
23478 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23479 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23480 int check_mouse_face = 0;
23481 int dummy_x = 0;
23482
23483 /* If mouse highlighting is on, we may need to draw adjacent
23484 glyphs using mouse-face highlighting. */
23485 if (area == TEXT_AREA && row->mouse_face_p
23486 && hlinfo->mouse_face_beg_row >= 0
23487 && hlinfo->mouse_face_end_row >= 0)
23488 {
23489 struct glyph_row *mouse_beg_row, *mouse_end_row;
23490
23491 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23492 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23493
23494 if (row >= mouse_beg_row && row <= mouse_end_row)
23495 {
23496 check_mouse_face = 1;
23497 mouse_beg_col = (row == mouse_beg_row)
23498 ? hlinfo->mouse_face_beg_col : 0;
23499 mouse_end_col = (row == mouse_end_row)
23500 ? hlinfo->mouse_face_end_col
23501 : row->used[TEXT_AREA];
23502 }
23503 }
23504
23505 /* Compute overhangs for all glyph strings. */
23506 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23507 for (s = head; s; s = s->next)
23508 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23509
23510 /* Prepend glyph strings for glyphs in front of the first glyph
23511 string that are overwritten because of the first glyph
23512 string's left overhang. The background of all strings
23513 prepended must be drawn because the first glyph string
23514 draws over it. */
23515 i = left_overwritten (head);
23516 if (i >= 0)
23517 {
23518 enum draw_glyphs_face overlap_hl;
23519
23520 /* If this row contains mouse highlighting, attempt to draw
23521 the overlapped glyphs with the correct highlight. This
23522 code fails if the overlap encompasses more than one glyph
23523 and mouse-highlight spans only some of these glyphs.
23524 However, making it work perfectly involves a lot more
23525 code, and I don't know if the pathological case occurs in
23526 practice, so we'll stick to this for now. --- cyd */
23527 if (check_mouse_face
23528 && mouse_beg_col < start && mouse_end_col > i)
23529 overlap_hl = DRAW_MOUSE_FACE;
23530 else
23531 overlap_hl = DRAW_NORMAL_TEXT;
23532
23533 j = i;
23534 BUILD_GLYPH_STRINGS (j, start, h, t,
23535 overlap_hl, dummy_x, last_x);
23536 start = i;
23537 compute_overhangs_and_x (t, head->x, 1);
23538 prepend_glyph_string_lists (&head, &tail, h, t);
23539 clip_head = head;
23540 }
23541
23542 /* Prepend glyph strings for glyphs in front of the first glyph
23543 string that overwrite that glyph string because of their
23544 right overhang. For these strings, only the foreground must
23545 be drawn, because it draws over the glyph string at `head'.
23546 The background must not be drawn because this would overwrite
23547 right overhangs of preceding glyphs for which no glyph
23548 strings exist. */
23549 i = left_overwriting (head);
23550 if (i >= 0)
23551 {
23552 enum draw_glyphs_face overlap_hl;
23553
23554 if (check_mouse_face
23555 && mouse_beg_col < start && mouse_end_col > i)
23556 overlap_hl = DRAW_MOUSE_FACE;
23557 else
23558 overlap_hl = DRAW_NORMAL_TEXT;
23559
23560 clip_head = head;
23561 BUILD_GLYPH_STRINGS (i, start, h, t,
23562 overlap_hl, dummy_x, last_x);
23563 for (s = h; s; s = s->next)
23564 s->background_filled_p = 1;
23565 compute_overhangs_and_x (t, head->x, 1);
23566 prepend_glyph_string_lists (&head, &tail, h, t);
23567 }
23568
23569 /* Append glyphs strings for glyphs following the last glyph
23570 string tail that are overwritten by tail. The background of
23571 these strings has to be drawn because tail's foreground draws
23572 over it. */
23573 i = right_overwritten (tail);
23574 if (i >= 0)
23575 {
23576 enum draw_glyphs_face overlap_hl;
23577
23578 if (check_mouse_face
23579 && mouse_beg_col < i && mouse_end_col > end)
23580 overlap_hl = DRAW_MOUSE_FACE;
23581 else
23582 overlap_hl = DRAW_NORMAL_TEXT;
23583
23584 BUILD_GLYPH_STRINGS (end, i, h, t,
23585 overlap_hl, x, last_x);
23586 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23587 we don't have `end = i;' here. */
23588 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23589 append_glyph_string_lists (&head, &tail, h, t);
23590 clip_tail = tail;
23591 }
23592
23593 /* Append glyph strings for glyphs following the last glyph
23594 string tail that overwrite tail. The foreground of such
23595 glyphs has to be drawn because it writes into the background
23596 of tail. The background must not be drawn because it could
23597 paint over the foreground of following glyphs. */
23598 i = right_overwriting (tail);
23599 if (i >= 0)
23600 {
23601 enum draw_glyphs_face overlap_hl;
23602 if (check_mouse_face
23603 && mouse_beg_col < i && mouse_end_col > end)
23604 overlap_hl = DRAW_MOUSE_FACE;
23605 else
23606 overlap_hl = DRAW_NORMAL_TEXT;
23607
23608 clip_tail = tail;
23609 i++; /* We must include the Ith glyph. */
23610 BUILD_GLYPH_STRINGS (end, i, h, t,
23611 overlap_hl, x, last_x);
23612 for (s = h; s; s = s->next)
23613 s->background_filled_p = 1;
23614 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23615 append_glyph_string_lists (&head, &tail, h, t);
23616 }
23617 if (clip_head || clip_tail)
23618 for (s = head; s; s = s->next)
23619 {
23620 s->clip_head = clip_head;
23621 s->clip_tail = clip_tail;
23622 }
23623 }
23624
23625 /* Draw all strings. */
23626 for (s = head; s; s = s->next)
23627 FRAME_RIF (f)->draw_glyph_string (s);
23628
23629 #ifndef HAVE_NS
23630 /* When focus a sole frame and move horizontally, this sets on_p to 0
23631 causing a failure to erase prev cursor position. */
23632 if (area == TEXT_AREA
23633 && !row->full_width_p
23634 /* When drawing overlapping rows, only the glyph strings'
23635 foreground is drawn, which doesn't erase a cursor
23636 completely. */
23637 && !overlaps)
23638 {
23639 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23640 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23641 : (tail ? tail->x + tail->background_width : x));
23642 x0 -= area_left;
23643 x1 -= area_left;
23644
23645 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23646 row->y, MATRIX_ROW_BOTTOM_Y (row));
23647 }
23648 #endif
23649
23650 /* Value is the x-position up to which drawn, relative to AREA of W.
23651 This doesn't include parts drawn because of overhangs. */
23652 if (row->full_width_p)
23653 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23654 else
23655 x_reached -= area_left;
23656
23657 RELEASE_HDC (hdc, f);
23658
23659 return x_reached;
23660 }
23661
23662 /* Expand row matrix if too narrow. Don't expand if area
23663 is not present. */
23664
23665 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23666 { \
23667 if (!fonts_changed_p \
23668 && (it->glyph_row->glyphs[area] \
23669 < it->glyph_row->glyphs[area + 1])) \
23670 { \
23671 it->w->ncols_scale_factor++; \
23672 fonts_changed_p = 1; \
23673 } \
23674 }
23675
23676 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23677 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23678
23679 static void
23680 append_glyph (struct it *it)
23681 {
23682 struct glyph *glyph;
23683 enum glyph_row_area area = it->area;
23684
23685 eassert (it->glyph_row);
23686 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23687
23688 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23689 if (glyph < it->glyph_row->glyphs[area + 1])
23690 {
23691 /* If the glyph row is reversed, we need to prepend the glyph
23692 rather than append it. */
23693 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23694 {
23695 struct glyph *g;
23696
23697 /* Make room for the additional glyph. */
23698 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23699 g[1] = *g;
23700 glyph = it->glyph_row->glyphs[area];
23701 }
23702 glyph->charpos = CHARPOS (it->position);
23703 glyph->object = it->object;
23704 if (it->pixel_width > 0)
23705 {
23706 glyph->pixel_width = it->pixel_width;
23707 glyph->padding_p = 0;
23708 }
23709 else
23710 {
23711 /* Assure at least 1-pixel width. Otherwise, cursor can't
23712 be displayed correctly. */
23713 glyph->pixel_width = 1;
23714 glyph->padding_p = 1;
23715 }
23716 glyph->ascent = it->ascent;
23717 glyph->descent = it->descent;
23718 glyph->voffset = it->voffset;
23719 glyph->type = CHAR_GLYPH;
23720 glyph->avoid_cursor_p = it->avoid_cursor_p;
23721 glyph->multibyte_p = it->multibyte_p;
23722 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23723 {
23724 /* In R2L rows, the left and the right box edges need to be
23725 drawn in reverse direction. */
23726 glyph->right_box_line_p = it->start_of_box_run_p;
23727 glyph->left_box_line_p = it->end_of_box_run_p;
23728 }
23729 else
23730 {
23731 glyph->left_box_line_p = it->start_of_box_run_p;
23732 glyph->right_box_line_p = it->end_of_box_run_p;
23733 }
23734 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23735 || it->phys_descent > it->descent);
23736 glyph->glyph_not_available_p = it->glyph_not_available_p;
23737 glyph->face_id = it->face_id;
23738 glyph->u.ch = it->char_to_display;
23739 glyph->slice.img = null_glyph_slice;
23740 glyph->font_type = FONT_TYPE_UNKNOWN;
23741 if (it->bidi_p)
23742 {
23743 glyph->resolved_level = it->bidi_it.resolved_level;
23744 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23745 emacs_abort ();
23746 glyph->bidi_type = it->bidi_it.type;
23747 }
23748 else
23749 {
23750 glyph->resolved_level = 0;
23751 glyph->bidi_type = UNKNOWN_BT;
23752 }
23753 ++it->glyph_row->used[area];
23754 }
23755 else
23756 IT_EXPAND_MATRIX_WIDTH (it, area);
23757 }
23758
23759 /* Store one glyph for the composition IT->cmp_it.id in
23760 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23761 non-null. */
23762
23763 static void
23764 append_composite_glyph (struct it *it)
23765 {
23766 struct glyph *glyph;
23767 enum glyph_row_area area = it->area;
23768
23769 eassert (it->glyph_row);
23770
23771 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23772 if (glyph < it->glyph_row->glyphs[area + 1])
23773 {
23774 /* If the glyph row is reversed, we need to prepend the glyph
23775 rather than append it. */
23776 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23777 {
23778 struct glyph *g;
23779
23780 /* Make room for the new glyph. */
23781 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23782 g[1] = *g;
23783 glyph = it->glyph_row->glyphs[it->area];
23784 }
23785 glyph->charpos = it->cmp_it.charpos;
23786 glyph->object = it->object;
23787 glyph->pixel_width = it->pixel_width;
23788 glyph->ascent = it->ascent;
23789 glyph->descent = it->descent;
23790 glyph->voffset = it->voffset;
23791 glyph->type = COMPOSITE_GLYPH;
23792 if (it->cmp_it.ch < 0)
23793 {
23794 glyph->u.cmp.automatic = 0;
23795 glyph->u.cmp.id = it->cmp_it.id;
23796 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23797 }
23798 else
23799 {
23800 glyph->u.cmp.automatic = 1;
23801 glyph->u.cmp.id = it->cmp_it.id;
23802 glyph->slice.cmp.from = it->cmp_it.from;
23803 glyph->slice.cmp.to = it->cmp_it.to - 1;
23804 }
23805 glyph->avoid_cursor_p = it->avoid_cursor_p;
23806 glyph->multibyte_p = it->multibyte_p;
23807 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23808 {
23809 /* In R2L rows, the left and the right box edges need to be
23810 drawn in reverse direction. */
23811 glyph->right_box_line_p = it->start_of_box_run_p;
23812 glyph->left_box_line_p = it->end_of_box_run_p;
23813 }
23814 else
23815 {
23816 glyph->left_box_line_p = it->start_of_box_run_p;
23817 glyph->right_box_line_p = it->end_of_box_run_p;
23818 }
23819 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23820 || it->phys_descent > it->descent);
23821 glyph->padding_p = 0;
23822 glyph->glyph_not_available_p = 0;
23823 glyph->face_id = it->face_id;
23824 glyph->font_type = FONT_TYPE_UNKNOWN;
23825 if (it->bidi_p)
23826 {
23827 glyph->resolved_level = it->bidi_it.resolved_level;
23828 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23829 emacs_abort ();
23830 glyph->bidi_type = it->bidi_it.type;
23831 }
23832 ++it->glyph_row->used[area];
23833 }
23834 else
23835 IT_EXPAND_MATRIX_WIDTH (it, area);
23836 }
23837
23838
23839 /* Change IT->ascent and IT->height according to the setting of
23840 IT->voffset. */
23841
23842 static void
23843 take_vertical_position_into_account (struct it *it)
23844 {
23845 if (it->voffset)
23846 {
23847 if (it->voffset < 0)
23848 /* Increase the ascent so that we can display the text higher
23849 in the line. */
23850 it->ascent -= it->voffset;
23851 else
23852 /* Increase the descent so that we can display the text lower
23853 in the line. */
23854 it->descent += it->voffset;
23855 }
23856 }
23857
23858
23859 /* Produce glyphs/get display metrics for the image IT is loaded with.
23860 See the description of struct display_iterator in dispextern.h for
23861 an overview of struct display_iterator. */
23862
23863 static void
23864 produce_image_glyph (struct it *it)
23865 {
23866 struct image *img;
23867 struct face *face;
23868 int glyph_ascent, crop;
23869 struct glyph_slice slice;
23870
23871 eassert (it->what == IT_IMAGE);
23872
23873 face = FACE_FROM_ID (it->f, it->face_id);
23874 eassert (face);
23875 /* Make sure X resources of the face is loaded. */
23876 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23877
23878 if (it->image_id < 0)
23879 {
23880 /* Fringe bitmap. */
23881 it->ascent = it->phys_ascent = 0;
23882 it->descent = it->phys_descent = 0;
23883 it->pixel_width = 0;
23884 it->nglyphs = 0;
23885 return;
23886 }
23887
23888 img = IMAGE_FROM_ID (it->f, it->image_id);
23889 eassert (img);
23890 /* Make sure X resources of the image is loaded. */
23891 prepare_image_for_display (it->f, img);
23892
23893 slice.x = slice.y = 0;
23894 slice.width = img->width;
23895 slice.height = img->height;
23896
23897 if (INTEGERP (it->slice.x))
23898 slice.x = XINT (it->slice.x);
23899 else if (FLOATP (it->slice.x))
23900 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23901
23902 if (INTEGERP (it->slice.y))
23903 slice.y = XINT (it->slice.y);
23904 else if (FLOATP (it->slice.y))
23905 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23906
23907 if (INTEGERP (it->slice.width))
23908 slice.width = XINT (it->slice.width);
23909 else if (FLOATP (it->slice.width))
23910 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23911
23912 if (INTEGERP (it->slice.height))
23913 slice.height = XINT (it->slice.height);
23914 else if (FLOATP (it->slice.height))
23915 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23916
23917 if (slice.x >= img->width)
23918 slice.x = img->width;
23919 if (slice.y >= img->height)
23920 slice.y = img->height;
23921 if (slice.x + slice.width >= img->width)
23922 slice.width = img->width - slice.x;
23923 if (slice.y + slice.height > img->height)
23924 slice.height = img->height - slice.y;
23925
23926 if (slice.width == 0 || slice.height == 0)
23927 return;
23928
23929 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23930
23931 it->descent = slice.height - glyph_ascent;
23932 if (slice.y == 0)
23933 it->descent += img->vmargin;
23934 if (slice.y + slice.height == img->height)
23935 it->descent += img->vmargin;
23936 it->phys_descent = it->descent;
23937
23938 it->pixel_width = slice.width;
23939 if (slice.x == 0)
23940 it->pixel_width += img->hmargin;
23941 if (slice.x + slice.width == img->width)
23942 it->pixel_width += img->hmargin;
23943
23944 /* It's quite possible for images to have an ascent greater than
23945 their height, so don't get confused in that case. */
23946 if (it->descent < 0)
23947 it->descent = 0;
23948
23949 it->nglyphs = 1;
23950
23951 if (face->box != FACE_NO_BOX)
23952 {
23953 if (face->box_line_width > 0)
23954 {
23955 if (slice.y == 0)
23956 it->ascent += face->box_line_width;
23957 if (slice.y + slice.height == img->height)
23958 it->descent += face->box_line_width;
23959 }
23960
23961 if (it->start_of_box_run_p && slice.x == 0)
23962 it->pixel_width += eabs (face->box_line_width);
23963 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23964 it->pixel_width += eabs (face->box_line_width);
23965 }
23966
23967 take_vertical_position_into_account (it);
23968
23969 /* Automatically crop wide image glyphs at right edge so we can
23970 draw the cursor on same display row. */
23971 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23972 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23973 {
23974 it->pixel_width -= crop;
23975 slice.width -= crop;
23976 }
23977
23978 if (it->glyph_row)
23979 {
23980 struct glyph *glyph;
23981 enum glyph_row_area area = it->area;
23982
23983 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23984 if (glyph < it->glyph_row->glyphs[area + 1])
23985 {
23986 glyph->charpos = CHARPOS (it->position);
23987 glyph->object = it->object;
23988 glyph->pixel_width = it->pixel_width;
23989 glyph->ascent = glyph_ascent;
23990 glyph->descent = it->descent;
23991 glyph->voffset = it->voffset;
23992 glyph->type = IMAGE_GLYPH;
23993 glyph->avoid_cursor_p = it->avoid_cursor_p;
23994 glyph->multibyte_p = it->multibyte_p;
23995 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23996 {
23997 /* In R2L rows, the left and the right box edges need to be
23998 drawn in reverse direction. */
23999 glyph->right_box_line_p = it->start_of_box_run_p;
24000 glyph->left_box_line_p = it->end_of_box_run_p;
24001 }
24002 else
24003 {
24004 glyph->left_box_line_p = it->start_of_box_run_p;
24005 glyph->right_box_line_p = it->end_of_box_run_p;
24006 }
24007 glyph->overlaps_vertically_p = 0;
24008 glyph->padding_p = 0;
24009 glyph->glyph_not_available_p = 0;
24010 glyph->face_id = it->face_id;
24011 glyph->u.img_id = img->id;
24012 glyph->slice.img = slice;
24013 glyph->font_type = FONT_TYPE_UNKNOWN;
24014 if (it->bidi_p)
24015 {
24016 glyph->resolved_level = it->bidi_it.resolved_level;
24017 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24018 emacs_abort ();
24019 glyph->bidi_type = it->bidi_it.type;
24020 }
24021 ++it->glyph_row->used[area];
24022 }
24023 else
24024 IT_EXPAND_MATRIX_WIDTH (it, area);
24025 }
24026 }
24027
24028
24029 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24030 of the glyph, WIDTH and HEIGHT are the width and height of the
24031 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24032
24033 static void
24034 append_stretch_glyph (struct it *it, Lisp_Object object,
24035 int width, int height, int ascent)
24036 {
24037 struct glyph *glyph;
24038 enum glyph_row_area area = it->area;
24039
24040 eassert (ascent >= 0 && ascent <= height);
24041
24042 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24043 if (glyph < it->glyph_row->glyphs[area + 1])
24044 {
24045 /* If the glyph row is reversed, we need to prepend the glyph
24046 rather than append it. */
24047 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24048 {
24049 struct glyph *g;
24050
24051 /* Make room for the additional glyph. */
24052 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24053 g[1] = *g;
24054 glyph = it->glyph_row->glyphs[area];
24055 }
24056 glyph->charpos = CHARPOS (it->position);
24057 glyph->object = object;
24058 glyph->pixel_width = width;
24059 glyph->ascent = ascent;
24060 glyph->descent = height - ascent;
24061 glyph->voffset = it->voffset;
24062 glyph->type = STRETCH_GLYPH;
24063 glyph->avoid_cursor_p = it->avoid_cursor_p;
24064 glyph->multibyte_p = it->multibyte_p;
24065 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24066 {
24067 /* In R2L rows, the left and the right box edges need to be
24068 drawn in reverse direction. */
24069 glyph->right_box_line_p = it->start_of_box_run_p;
24070 glyph->left_box_line_p = it->end_of_box_run_p;
24071 }
24072 else
24073 {
24074 glyph->left_box_line_p = it->start_of_box_run_p;
24075 glyph->right_box_line_p = it->end_of_box_run_p;
24076 }
24077 glyph->overlaps_vertically_p = 0;
24078 glyph->padding_p = 0;
24079 glyph->glyph_not_available_p = 0;
24080 glyph->face_id = it->face_id;
24081 glyph->u.stretch.ascent = ascent;
24082 glyph->u.stretch.height = height;
24083 glyph->slice.img = null_glyph_slice;
24084 glyph->font_type = FONT_TYPE_UNKNOWN;
24085 if (it->bidi_p)
24086 {
24087 glyph->resolved_level = it->bidi_it.resolved_level;
24088 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24089 emacs_abort ();
24090 glyph->bidi_type = it->bidi_it.type;
24091 }
24092 else
24093 {
24094 glyph->resolved_level = 0;
24095 glyph->bidi_type = UNKNOWN_BT;
24096 }
24097 ++it->glyph_row->used[area];
24098 }
24099 else
24100 IT_EXPAND_MATRIX_WIDTH (it, area);
24101 }
24102
24103 #endif /* HAVE_WINDOW_SYSTEM */
24104
24105 /* Produce a stretch glyph for iterator IT. IT->object is the value
24106 of the glyph property displayed. The value must be a list
24107 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24108 being recognized:
24109
24110 1. `:width WIDTH' specifies that the space should be WIDTH *
24111 canonical char width wide. WIDTH may be an integer or floating
24112 point number.
24113
24114 2. `:relative-width FACTOR' specifies that the width of the stretch
24115 should be computed from the width of the first character having the
24116 `glyph' property, and should be FACTOR times that width.
24117
24118 3. `:align-to HPOS' specifies that the space should be wide enough
24119 to reach HPOS, a value in canonical character units.
24120
24121 Exactly one of the above pairs must be present.
24122
24123 4. `:height HEIGHT' specifies that the height of the stretch produced
24124 should be HEIGHT, measured in canonical character units.
24125
24126 5. `:relative-height FACTOR' specifies that the height of the
24127 stretch should be FACTOR times the height of the characters having
24128 the glyph property.
24129
24130 Either none or exactly one of 4 or 5 must be present.
24131
24132 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24133 of the stretch should be used for the ascent of the stretch.
24134 ASCENT must be in the range 0 <= ASCENT <= 100. */
24135
24136 void
24137 produce_stretch_glyph (struct it *it)
24138 {
24139 /* (space :width WIDTH :height HEIGHT ...) */
24140 Lisp_Object prop, plist;
24141 int width = 0, height = 0, align_to = -1;
24142 int zero_width_ok_p = 0;
24143 double tem;
24144 struct font *font = NULL;
24145
24146 #ifdef HAVE_WINDOW_SYSTEM
24147 int ascent = 0;
24148 int zero_height_ok_p = 0;
24149
24150 if (FRAME_WINDOW_P (it->f))
24151 {
24152 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24153 font = face->font ? face->font : FRAME_FONT (it->f);
24154 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24155 }
24156 #endif
24157
24158 /* List should start with `space'. */
24159 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24160 plist = XCDR (it->object);
24161
24162 /* Compute the width of the stretch. */
24163 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24164 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24165 {
24166 /* Absolute width `:width WIDTH' specified and valid. */
24167 zero_width_ok_p = 1;
24168 width = (int)tem;
24169 }
24170 #ifdef HAVE_WINDOW_SYSTEM
24171 else if (FRAME_WINDOW_P (it->f)
24172 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24173 {
24174 /* Relative width `:relative-width FACTOR' specified and valid.
24175 Compute the width of the characters having the `glyph'
24176 property. */
24177 struct it it2;
24178 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24179
24180 it2 = *it;
24181 if (it->multibyte_p)
24182 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24183 else
24184 {
24185 it2.c = it2.char_to_display = *p, it2.len = 1;
24186 if (! ASCII_CHAR_P (it2.c))
24187 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24188 }
24189
24190 it2.glyph_row = NULL;
24191 it2.what = IT_CHARACTER;
24192 x_produce_glyphs (&it2);
24193 width = NUMVAL (prop) * it2.pixel_width;
24194 }
24195 #endif /* HAVE_WINDOW_SYSTEM */
24196 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24197 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24198 {
24199 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24200 align_to = (align_to < 0
24201 ? 0
24202 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24203 else if (align_to < 0)
24204 align_to = window_box_left_offset (it->w, TEXT_AREA);
24205 width = max (0, (int)tem + align_to - it->current_x);
24206 zero_width_ok_p = 1;
24207 }
24208 else
24209 /* Nothing specified -> width defaults to canonical char width. */
24210 width = FRAME_COLUMN_WIDTH (it->f);
24211
24212 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24213 width = 1;
24214
24215 #ifdef HAVE_WINDOW_SYSTEM
24216 /* Compute height. */
24217 if (FRAME_WINDOW_P (it->f))
24218 {
24219 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24220 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24221 {
24222 height = (int)tem;
24223 zero_height_ok_p = 1;
24224 }
24225 else if (prop = Fplist_get (plist, QCrelative_height),
24226 NUMVAL (prop) > 0)
24227 height = FONT_HEIGHT (font) * NUMVAL (prop);
24228 else
24229 height = FONT_HEIGHT (font);
24230
24231 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24232 height = 1;
24233
24234 /* Compute percentage of height used for ascent. If
24235 `:ascent ASCENT' is present and valid, use that. Otherwise,
24236 derive the ascent from the font in use. */
24237 if (prop = Fplist_get (plist, QCascent),
24238 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24239 ascent = height * NUMVAL (prop) / 100.0;
24240 else if (!NILP (prop)
24241 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24242 ascent = min (max (0, (int)tem), height);
24243 else
24244 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24245 }
24246 else
24247 #endif /* HAVE_WINDOW_SYSTEM */
24248 height = 1;
24249
24250 if (width > 0 && it->line_wrap != TRUNCATE
24251 && it->current_x + width > it->last_visible_x)
24252 {
24253 width = it->last_visible_x - it->current_x;
24254 #ifdef HAVE_WINDOW_SYSTEM
24255 /* Subtract one more pixel from the stretch width, but only on
24256 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24257 width -= FRAME_WINDOW_P (it->f);
24258 #endif
24259 }
24260
24261 if (width > 0 && height > 0 && it->glyph_row)
24262 {
24263 Lisp_Object o_object = it->object;
24264 Lisp_Object object = it->stack[it->sp - 1].string;
24265 int n = width;
24266
24267 if (!STRINGP (object))
24268 object = it->w->buffer;
24269 #ifdef HAVE_WINDOW_SYSTEM
24270 if (FRAME_WINDOW_P (it->f))
24271 append_stretch_glyph (it, object, width, height, ascent);
24272 else
24273 #endif
24274 {
24275 it->object = object;
24276 it->char_to_display = ' ';
24277 it->pixel_width = it->len = 1;
24278 while (n--)
24279 tty_append_glyph (it);
24280 it->object = o_object;
24281 }
24282 }
24283
24284 it->pixel_width = width;
24285 #ifdef HAVE_WINDOW_SYSTEM
24286 if (FRAME_WINDOW_P (it->f))
24287 {
24288 it->ascent = it->phys_ascent = ascent;
24289 it->descent = it->phys_descent = height - it->ascent;
24290 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24291 take_vertical_position_into_account (it);
24292 }
24293 else
24294 #endif
24295 it->nglyphs = width;
24296 }
24297
24298 /* Get information about special display element WHAT in an
24299 environment described by IT. WHAT is one of IT_TRUNCATION or
24300 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24301 non-null glyph_row member. This function ensures that fields like
24302 face_id, c, len of IT are left untouched. */
24303
24304 static void
24305 produce_special_glyphs (struct it *it, enum display_element_type what)
24306 {
24307 struct it temp_it;
24308 Lisp_Object gc;
24309 GLYPH glyph;
24310
24311 temp_it = *it;
24312 temp_it.object = make_number (0);
24313 memset (&temp_it.current, 0, sizeof temp_it.current);
24314
24315 if (what == IT_CONTINUATION)
24316 {
24317 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24318 if (it->bidi_it.paragraph_dir == R2L)
24319 SET_GLYPH_FROM_CHAR (glyph, '/');
24320 else
24321 SET_GLYPH_FROM_CHAR (glyph, '\\');
24322 if (it->dp
24323 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24324 {
24325 /* FIXME: Should we mirror GC for R2L lines? */
24326 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24327 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24328 }
24329 }
24330 else if (what == IT_TRUNCATION)
24331 {
24332 /* Truncation glyph. */
24333 SET_GLYPH_FROM_CHAR (glyph, '$');
24334 if (it->dp
24335 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24336 {
24337 /* FIXME: Should we mirror GC for R2L lines? */
24338 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24339 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24340 }
24341 }
24342 else
24343 emacs_abort ();
24344
24345 #ifdef HAVE_WINDOW_SYSTEM
24346 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24347 is turned off, we precede the truncation/continuation glyphs by a
24348 stretch glyph whose width is computed such that these special
24349 glyphs are aligned at the window margin, even when very different
24350 fonts are used in different glyph rows. */
24351 if (FRAME_WINDOW_P (temp_it.f)
24352 /* init_iterator calls this with it->glyph_row == NULL, and it
24353 wants only the pixel width of the truncation/continuation
24354 glyphs. */
24355 && temp_it.glyph_row
24356 /* insert_left_trunc_glyphs calls us at the beginning of the
24357 row, and it has its own calculation of the stretch glyph
24358 width. */
24359 && temp_it.glyph_row->used[TEXT_AREA] > 0
24360 && (temp_it.glyph_row->reversed_p
24361 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24362 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24363 {
24364 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24365
24366 if (stretch_width > 0)
24367 {
24368 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24369 struct font *font =
24370 face->font ? face->font : FRAME_FONT (temp_it.f);
24371 int stretch_ascent =
24372 (((temp_it.ascent + temp_it.descent)
24373 * FONT_BASE (font)) / FONT_HEIGHT (font));
24374
24375 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24376 temp_it.ascent + temp_it.descent,
24377 stretch_ascent);
24378 }
24379 }
24380 #endif
24381
24382 temp_it.dp = NULL;
24383 temp_it.what = IT_CHARACTER;
24384 temp_it.len = 1;
24385 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24386 temp_it.face_id = GLYPH_FACE (glyph);
24387 temp_it.len = CHAR_BYTES (temp_it.c);
24388
24389 PRODUCE_GLYPHS (&temp_it);
24390 it->pixel_width = temp_it.pixel_width;
24391 it->nglyphs = temp_it.pixel_width;
24392 }
24393
24394 #ifdef HAVE_WINDOW_SYSTEM
24395
24396 /* Calculate line-height and line-spacing properties.
24397 An integer value specifies explicit pixel value.
24398 A float value specifies relative value to current face height.
24399 A cons (float . face-name) specifies relative value to
24400 height of specified face font.
24401
24402 Returns height in pixels, or nil. */
24403
24404
24405 static Lisp_Object
24406 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24407 int boff, int override)
24408 {
24409 Lisp_Object face_name = Qnil;
24410 int ascent, descent, height;
24411
24412 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24413 return val;
24414
24415 if (CONSP (val))
24416 {
24417 face_name = XCAR (val);
24418 val = XCDR (val);
24419 if (!NUMBERP (val))
24420 val = make_number (1);
24421 if (NILP (face_name))
24422 {
24423 height = it->ascent + it->descent;
24424 goto scale;
24425 }
24426 }
24427
24428 if (NILP (face_name))
24429 {
24430 font = FRAME_FONT (it->f);
24431 boff = FRAME_BASELINE_OFFSET (it->f);
24432 }
24433 else if (EQ (face_name, Qt))
24434 {
24435 override = 0;
24436 }
24437 else
24438 {
24439 int face_id;
24440 struct face *face;
24441
24442 face_id = lookup_named_face (it->f, face_name, 0);
24443 if (face_id < 0)
24444 return make_number (-1);
24445
24446 face = FACE_FROM_ID (it->f, face_id);
24447 font = face->font;
24448 if (font == NULL)
24449 return make_number (-1);
24450 boff = font->baseline_offset;
24451 if (font->vertical_centering)
24452 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24453 }
24454
24455 ascent = FONT_BASE (font) + boff;
24456 descent = FONT_DESCENT (font) - boff;
24457
24458 if (override)
24459 {
24460 it->override_ascent = ascent;
24461 it->override_descent = descent;
24462 it->override_boff = boff;
24463 }
24464
24465 height = ascent + descent;
24466
24467 scale:
24468 if (FLOATP (val))
24469 height = (int)(XFLOAT_DATA (val) * height);
24470 else if (INTEGERP (val))
24471 height *= XINT (val);
24472
24473 return make_number (height);
24474 }
24475
24476
24477 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24478 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24479 and only if this is for a character for which no font was found.
24480
24481 If the display method (it->glyphless_method) is
24482 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24483 length of the acronym or the hexadecimal string, UPPER_XOFF and
24484 UPPER_YOFF are pixel offsets for the upper part of the string,
24485 LOWER_XOFF and LOWER_YOFF are for the lower part.
24486
24487 For the other display methods, LEN through LOWER_YOFF are zero. */
24488
24489 static void
24490 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24491 short upper_xoff, short upper_yoff,
24492 short lower_xoff, short lower_yoff)
24493 {
24494 struct glyph *glyph;
24495 enum glyph_row_area area = it->area;
24496
24497 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24498 if (glyph < it->glyph_row->glyphs[area + 1])
24499 {
24500 /* If the glyph row is reversed, we need to prepend the glyph
24501 rather than append it. */
24502 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24503 {
24504 struct glyph *g;
24505
24506 /* Make room for the additional glyph. */
24507 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24508 g[1] = *g;
24509 glyph = it->glyph_row->glyphs[area];
24510 }
24511 glyph->charpos = CHARPOS (it->position);
24512 glyph->object = it->object;
24513 glyph->pixel_width = it->pixel_width;
24514 glyph->ascent = it->ascent;
24515 glyph->descent = it->descent;
24516 glyph->voffset = it->voffset;
24517 glyph->type = GLYPHLESS_GLYPH;
24518 glyph->u.glyphless.method = it->glyphless_method;
24519 glyph->u.glyphless.for_no_font = for_no_font;
24520 glyph->u.glyphless.len = len;
24521 glyph->u.glyphless.ch = it->c;
24522 glyph->slice.glyphless.upper_xoff = upper_xoff;
24523 glyph->slice.glyphless.upper_yoff = upper_yoff;
24524 glyph->slice.glyphless.lower_xoff = lower_xoff;
24525 glyph->slice.glyphless.lower_yoff = lower_yoff;
24526 glyph->avoid_cursor_p = it->avoid_cursor_p;
24527 glyph->multibyte_p = it->multibyte_p;
24528 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24529 {
24530 /* In R2L rows, the left and the right box edges need to be
24531 drawn in reverse direction. */
24532 glyph->right_box_line_p = it->start_of_box_run_p;
24533 glyph->left_box_line_p = it->end_of_box_run_p;
24534 }
24535 else
24536 {
24537 glyph->left_box_line_p = it->start_of_box_run_p;
24538 glyph->right_box_line_p = it->end_of_box_run_p;
24539 }
24540 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24541 || it->phys_descent > it->descent);
24542 glyph->padding_p = 0;
24543 glyph->glyph_not_available_p = 0;
24544 glyph->face_id = face_id;
24545 glyph->font_type = FONT_TYPE_UNKNOWN;
24546 if (it->bidi_p)
24547 {
24548 glyph->resolved_level = it->bidi_it.resolved_level;
24549 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24550 emacs_abort ();
24551 glyph->bidi_type = it->bidi_it.type;
24552 }
24553 ++it->glyph_row->used[area];
24554 }
24555 else
24556 IT_EXPAND_MATRIX_WIDTH (it, area);
24557 }
24558
24559
24560 /* Produce a glyph for a glyphless character for iterator IT.
24561 IT->glyphless_method specifies which method to use for displaying
24562 the character. See the description of enum
24563 glyphless_display_method in dispextern.h for the detail.
24564
24565 FOR_NO_FONT is nonzero if and only if this is for a character for
24566 which no font was found. ACRONYM, if non-nil, is an acronym string
24567 for the character. */
24568
24569 static void
24570 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24571 {
24572 int face_id;
24573 struct face *face;
24574 struct font *font;
24575 int base_width, base_height, width, height;
24576 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24577 int len;
24578
24579 /* Get the metrics of the base font. We always refer to the current
24580 ASCII face. */
24581 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24582 font = face->font ? face->font : FRAME_FONT (it->f);
24583 it->ascent = FONT_BASE (font) + font->baseline_offset;
24584 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24585 base_height = it->ascent + it->descent;
24586 base_width = font->average_width;
24587
24588 /* Get a face ID for the glyph by utilizing a cache (the same way as
24589 done for `escape-glyph' in get_next_display_element). */
24590 if (it->f == last_glyphless_glyph_frame
24591 && it->face_id == last_glyphless_glyph_face_id)
24592 {
24593 face_id = last_glyphless_glyph_merged_face_id;
24594 }
24595 else
24596 {
24597 /* Merge the `glyphless-char' face into the current face. */
24598 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24599 last_glyphless_glyph_frame = it->f;
24600 last_glyphless_glyph_face_id = it->face_id;
24601 last_glyphless_glyph_merged_face_id = face_id;
24602 }
24603
24604 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24605 {
24606 it->pixel_width = THIN_SPACE_WIDTH;
24607 len = 0;
24608 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24609 }
24610 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24611 {
24612 width = CHAR_WIDTH (it->c);
24613 if (width == 0)
24614 width = 1;
24615 else if (width > 4)
24616 width = 4;
24617 it->pixel_width = base_width * width;
24618 len = 0;
24619 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24620 }
24621 else
24622 {
24623 char buf[7];
24624 const char *str;
24625 unsigned int code[6];
24626 int upper_len;
24627 int ascent, descent;
24628 struct font_metrics metrics_upper, metrics_lower;
24629
24630 face = FACE_FROM_ID (it->f, face_id);
24631 font = face->font ? face->font : FRAME_FONT (it->f);
24632 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24633
24634 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24635 {
24636 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24637 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24638 if (CONSP (acronym))
24639 acronym = XCAR (acronym);
24640 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24641 }
24642 else
24643 {
24644 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24645 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24646 str = buf;
24647 }
24648 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24649 code[len] = font->driver->encode_char (font, str[len]);
24650 upper_len = (len + 1) / 2;
24651 font->driver->text_extents (font, code, upper_len,
24652 &metrics_upper);
24653 font->driver->text_extents (font, code + upper_len, len - upper_len,
24654 &metrics_lower);
24655
24656
24657
24658 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24659 width = max (metrics_upper.width, metrics_lower.width) + 4;
24660 upper_xoff = upper_yoff = 2; /* the typical case */
24661 if (base_width >= width)
24662 {
24663 /* Align the upper to the left, the lower to the right. */
24664 it->pixel_width = base_width;
24665 lower_xoff = base_width - 2 - metrics_lower.width;
24666 }
24667 else
24668 {
24669 /* Center the shorter one. */
24670 it->pixel_width = width;
24671 if (metrics_upper.width >= metrics_lower.width)
24672 lower_xoff = (width - metrics_lower.width) / 2;
24673 else
24674 {
24675 /* FIXME: This code doesn't look right. It formerly was
24676 missing the "lower_xoff = 0;", which couldn't have
24677 been right since it left lower_xoff uninitialized. */
24678 lower_xoff = 0;
24679 upper_xoff = (width - metrics_upper.width) / 2;
24680 }
24681 }
24682
24683 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24684 top, bottom, and between upper and lower strings. */
24685 height = (metrics_upper.ascent + metrics_upper.descent
24686 + metrics_lower.ascent + metrics_lower.descent) + 5;
24687 /* Center vertically.
24688 H:base_height, D:base_descent
24689 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24690
24691 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24692 descent = D - H/2 + h/2;
24693 lower_yoff = descent - 2 - ld;
24694 upper_yoff = lower_yoff - la - 1 - ud; */
24695 ascent = - (it->descent - (base_height + height + 1) / 2);
24696 descent = it->descent - (base_height - height) / 2;
24697 lower_yoff = descent - 2 - metrics_lower.descent;
24698 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24699 - metrics_upper.descent);
24700 /* Don't make the height shorter than the base height. */
24701 if (height > base_height)
24702 {
24703 it->ascent = ascent;
24704 it->descent = descent;
24705 }
24706 }
24707
24708 it->phys_ascent = it->ascent;
24709 it->phys_descent = it->descent;
24710 if (it->glyph_row)
24711 append_glyphless_glyph (it, face_id, for_no_font, len,
24712 upper_xoff, upper_yoff,
24713 lower_xoff, lower_yoff);
24714 it->nglyphs = 1;
24715 take_vertical_position_into_account (it);
24716 }
24717
24718
24719 /* RIF:
24720 Produce glyphs/get display metrics for the display element IT is
24721 loaded with. See the description of struct it in dispextern.h
24722 for an overview of struct it. */
24723
24724 void
24725 x_produce_glyphs (struct it *it)
24726 {
24727 int extra_line_spacing = it->extra_line_spacing;
24728
24729 it->glyph_not_available_p = 0;
24730
24731 if (it->what == IT_CHARACTER)
24732 {
24733 XChar2b char2b;
24734 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24735 struct font *font = face->font;
24736 struct font_metrics *pcm = NULL;
24737 int boff; /* baseline offset */
24738
24739 if (font == NULL)
24740 {
24741 /* When no suitable font is found, display this character by
24742 the method specified in the first extra slot of
24743 Vglyphless_char_display. */
24744 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24745
24746 eassert (it->what == IT_GLYPHLESS);
24747 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24748 goto done;
24749 }
24750
24751 boff = font->baseline_offset;
24752 if (font->vertical_centering)
24753 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24754
24755 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24756 {
24757 int stretched_p;
24758
24759 it->nglyphs = 1;
24760
24761 if (it->override_ascent >= 0)
24762 {
24763 it->ascent = it->override_ascent;
24764 it->descent = it->override_descent;
24765 boff = it->override_boff;
24766 }
24767 else
24768 {
24769 it->ascent = FONT_BASE (font) + boff;
24770 it->descent = FONT_DESCENT (font) - boff;
24771 }
24772
24773 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24774 {
24775 pcm = get_per_char_metric (font, &char2b);
24776 if (pcm->width == 0
24777 && pcm->rbearing == 0 && pcm->lbearing == 0)
24778 pcm = NULL;
24779 }
24780
24781 if (pcm)
24782 {
24783 it->phys_ascent = pcm->ascent + boff;
24784 it->phys_descent = pcm->descent - boff;
24785 it->pixel_width = pcm->width;
24786 }
24787 else
24788 {
24789 it->glyph_not_available_p = 1;
24790 it->phys_ascent = it->ascent;
24791 it->phys_descent = it->descent;
24792 it->pixel_width = font->space_width;
24793 }
24794
24795 if (it->constrain_row_ascent_descent_p)
24796 {
24797 if (it->descent > it->max_descent)
24798 {
24799 it->ascent += it->descent - it->max_descent;
24800 it->descent = it->max_descent;
24801 }
24802 if (it->ascent > it->max_ascent)
24803 {
24804 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24805 it->ascent = it->max_ascent;
24806 }
24807 it->phys_ascent = min (it->phys_ascent, it->ascent);
24808 it->phys_descent = min (it->phys_descent, it->descent);
24809 extra_line_spacing = 0;
24810 }
24811
24812 /* If this is a space inside a region of text with
24813 `space-width' property, change its width. */
24814 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24815 if (stretched_p)
24816 it->pixel_width *= XFLOATINT (it->space_width);
24817
24818 /* If face has a box, add the box thickness to the character
24819 height. If character has a box line to the left and/or
24820 right, add the box line width to the character's width. */
24821 if (face->box != FACE_NO_BOX)
24822 {
24823 int thick = face->box_line_width;
24824
24825 if (thick > 0)
24826 {
24827 it->ascent += thick;
24828 it->descent += thick;
24829 }
24830 else
24831 thick = -thick;
24832
24833 if (it->start_of_box_run_p)
24834 it->pixel_width += thick;
24835 if (it->end_of_box_run_p)
24836 it->pixel_width += thick;
24837 }
24838
24839 /* If face has an overline, add the height of the overline
24840 (1 pixel) and a 1 pixel margin to the character height. */
24841 if (face->overline_p)
24842 it->ascent += overline_margin;
24843
24844 if (it->constrain_row_ascent_descent_p)
24845 {
24846 if (it->ascent > it->max_ascent)
24847 it->ascent = it->max_ascent;
24848 if (it->descent > it->max_descent)
24849 it->descent = it->max_descent;
24850 }
24851
24852 take_vertical_position_into_account (it);
24853
24854 /* If we have to actually produce glyphs, do it. */
24855 if (it->glyph_row)
24856 {
24857 if (stretched_p)
24858 {
24859 /* Translate a space with a `space-width' property
24860 into a stretch glyph. */
24861 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24862 / FONT_HEIGHT (font));
24863 append_stretch_glyph (it, it->object, it->pixel_width,
24864 it->ascent + it->descent, ascent);
24865 }
24866 else
24867 append_glyph (it);
24868
24869 /* If characters with lbearing or rbearing are displayed
24870 in this line, record that fact in a flag of the
24871 glyph row. This is used to optimize X output code. */
24872 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24873 it->glyph_row->contains_overlapping_glyphs_p = 1;
24874 }
24875 if (! stretched_p && it->pixel_width == 0)
24876 /* We assure that all visible glyphs have at least 1-pixel
24877 width. */
24878 it->pixel_width = 1;
24879 }
24880 else if (it->char_to_display == '\n')
24881 {
24882 /* A newline has no width, but we need the height of the
24883 line. But if previous part of the line sets a height,
24884 don't increase that height */
24885
24886 Lisp_Object height;
24887 Lisp_Object total_height = Qnil;
24888
24889 it->override_ascent = -1;
24890 it->pixel_width = 0;
24891 it->nglyphs = 0;
24892
24893 height = get_it_property (it, Qline_height);
24894 /* Split (line-height total-height) list */
24895 if (CONSP (height)
24896 && CONSP (XCDR (height))
24897 && NILP (XCDR (XCDR (height))))
24898 {
24899 total_height = XCAR (XCDR (height));
24900 height = XCAR (height);
24901 }
24902 height = calc_line_height_property (it, height, font, boff, 1);
24903
24904 if (it->override_ascent >= 0)
24905 {
24906 it->ascent = it->override_ascent;
24907 it->descent = it->override_descent;
24908 boff = it->override_boff;
24909 }
24910 else
24911 {
24912 it->ascent = FONT_BASE (font) + boff;
24913 it->descent = FONT_DESCENT (font) - boff;
24914 }
24915
24916 if (EQ (height, Qt))
24917 {
24918 if (it->descent > it->max_descent)
24919 {
24920 it->ascent += it->descent - it->max_descent;
24921 it->descent = it->max_descent;
24922 }
24923 if (it->ascent > it->max_ascent)
24924 {
24925 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24926 it->ascent = it->max_ascent;
24927 }
24928 it->phys_ascent = min (it->phys_ascent, it->ascent);
24929 it->phys_descent = min (it->phys_descent, it->descent);
24930 it->constrain_row_ascent_descent_p = 1;
24931 extra_line_spacing = 0;
24932 }
24933 else
24934 {
24935 Lisp_Object spacing;
24936
24937 it->phys_ascent = it->ascent;
24938 it->phys_descent = it->descent;
24939
24940 if ((it->max_ascent > 0 || it->max_descent > 0)
24941 && face->box != FACE_NO_BOX
24942 && face->box_line_width > 0)
24943 {
24944 it->ascent += face->box_line_width;
24945 it->descent += face->box_line_width;
24946 }
24947 if (!NILP (height)
24948 && XINT (height) > it->ascent + it->descent)
24949 it->ascent = XINT (height) - it->descent;
24950
24951 if (!NILP (total_height))
24952 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24953 else
24954 {
24955 spacing = get_it_property (it, Qline_spacing);
24956 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24957 }
24958 if (INTEGERP (spacing))
24959 {
24960 extra_line_spacing = XINT (spacing);
24961 if (!NILP (total_height))
24962 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24963 }
24964 }
24965 }
24966 else /* i.e. (it->char_to_display == '\t') */
24967 {
24968 if (font->space_width > 0)
24969 {
24970 int tab_width = it->tab_width * font->space_width;
24971 int x = it->current_x + it->continuation_lines_width;
24972 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24973
24974 /* If the distance from the current position to the next tab
24975 stop is less than a space character width, use the
24976 tab stop after that. */
24977 if (next_tab_x - x < font->space_width)
24978 next_tab_x += tab_width;
24979
24980 it->pixel_width = next_tab_x - x;
24981 it->nglyphs = 1;
24982 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24983 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24984
24985 if (it->glyph_row)
24986 {
24987 append_stretch_glyph (it, it->object, it->pixel_width,
24988 it->ascent + it->descent, it->ascent);
24989 }
24990 }
24991 else
24992 {
24993 it->pixel_width = 0;
24994 it->nglyphs = 1;
24995 }
24996 }
24997 }
24998 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24999 {
25000 /* A static composition.
25001
25002 Note: A composition is represented as one glyph in the
25003 glyph matrix. There are no padding glyphs.
25004
25005 Important note: pixel_width, ascent, and descent are the
25006 values of what is drawn by draw_glyphs (i.e. the values of
25007 the overall glyphs composed). */
25008 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25009 int boff; /* baseline offset */
25010 struct composition *cmp = composition_table[it->cmp_it.id];
25011 int glyph_len = cmp->glyph_len;
25012 struct font *font = face->font;
25013
25014 it->nglyphs = 1;
25015
25016 /* If we have not yet calculated pixel size data of glyphs of
25017 the composition for the current face font, calculate them
25018 now. Theoretically, we have to check all fonts for the
25019 glyphs, but that requires much time and memory space. So,
25020 here we check only the font of the first glyph. This may
25021 lead to incorrect display, but it's very rare, and C-l
25022 (recenter-top-bottom) can correct the display anyway. */
25023 if (! cmp->font || cmp->font != font)
25024 {
25025 /* Ascent and descent of the font of the first character
25026 of this composition (adjusted by baseline offset).
25027 Ascent and descent of overall glyphs should not be less
25028 than these, respectively. */
25029 int font_ascent, font_descent, font_height;
25030 /* Bounding box of the overall glyphs. */
25031 int leftmost, rightmost, lowest, highest;
25032 int lbearing, rbearing;
25033 int i, width, ascent, descent;
25034 int left_padded = 0, right_padded = 0;
25035 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25036 XChar2b char2b;
25037 struct font_metrics *pcm;
25038 int font_not_found_p;
25039 ptrdiff_t pos;
25040
25041 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25042 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25043 break;
25044 if (glyph_len < cmp->glyph_len)
25045 right_padded = 1;
25046 for (i = 0; i < glyph_len; i++)
25047 {
25048 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25049 break;
25050 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25051 }
25052 if (i > 0)
25053 left_padded = 1;
25054
25055 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25056 : IT_CHARPOS (*it));
25057 /* If no suitable font is found, use the default font. */
25058 font_not_found_p = font == NULL;
25059 if (font_not_found_p)
25060 {
25061 face = face->ascii_face;
25062 font = face->font;
25063 }
25064 boff = font->baseline_offset;
25065 if (font->vertical_centering)
25066 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25067 font_ascent = FONT_BASE (font) + boff;
25068 font_descent = FONT_DESCENT (font) - boff;
25069 font_height = FONT_HEIGHT (font);
25070
25071 cmp->font = font;
25072
25073 pcm = NULL;
25074 if (! font_not_found_p)
25075 {
25076 get_char_face_and_encoding (it->f, c, it->face_id,
25077 &char2b, 0);
25078 pcm = get_per_char_metric (font, &char2b);
25079 }
25080
25081 /* Initialize the bounding box. */
25082 if (pcm)
25083 {
25084 width = cmp->glyph_len > 0 ? pcm->width : 0;
25085 ascent = pcm->ascent;
25086 descent = pcm->descent;
25087 lbearing = pcm->lbearing;
25088 rbearing = pcm->rbearing;
25089 }
25090 else
25091 {
25092 width = cmp->glyph_len > 0 ? font->space_width : 0;
25093 ascent = FONT_BASE (font);
25094 descent = FONT_DESCENT (font);
25095 lbearing = 0;
25096 rbearing = width;
25097 }
25098
25099 rightmost = width;
25100 leftmost = 0;
25101 lowest = - descent + boff;
25102 highest = ascent + boff;
25103
25104 if (! font_not_found_p
25105 && font->default_ascent
25106 && CHAR_TABLE_P (Vuse_default_ascent)
25107 && !NILP (Faref (Vuse_default_ascent,
25108 make_number (it->char_to_display))))
25109 highest = font->default_ascent + boff;
25110
25111 /* Draw the first glyph at the normal position. It may be
25112 shifted to right later if some other glyphs are drawn
25113 at the left. */
25114 cmp->offsets[i * 2] = 0;
25115 cmp->offsets[i * 2 + 1] = boff;
25116 cmp->lbearing = lbearing;
25117 cmp->rbearing = rbearing;
25118
25119 /* Set cmp->offsets for the remaining glyphs. */
25120 for (i++; i < glyph_len; i++)
25121 {
25122 int left, right, btm, top;
25123 int ch = COMPOSITION_GLYPH (cmp, i);
25124 int face_id;
25125 struct face *this_face;
25126
25127 if (ch == '\t')
25128 ch = ' ';
25129 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25130 this_face = FACE_FROM_ID (it->f, face_id);
25131 font = this_face->font;
25132
25133 if (font == NULL)
25134 pcm = NULL;
25135 else
25136 {
25137 get_char_face_and_encoding (it->f, ch, face_id,
25138 &char2b, 0);
25139 pcm = get_per_char_metric (font, &char2b);
25140 }
25141 if (! pcm)
25142 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25143 else
25144 {
25145 width = pcm->width;
25146 ascent = pcm->ascent;
25147 descent = pcm->descent;
25148 lbearing = pcm->lbearing;
25149 rbearing = pcm->rbearing;
25150 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25151 {
25152 /* Relative composition with or without
25153 alternate chars. */
25154 left = (leftmost + rightmost - width) / 2;
25155 btm = - descent + boff;
25156 if (font->relative_compose
25157 && (! CHAR_TABLE_P (Vignore_relative_composition)
25158 || NILP (Faref (Vignore_relative_composition,
25159 make_number (ch)))))
25160 {
25161
25162 if (- descent >= font->relative_compose)
25163 /* One extra pixel between two glyphs. */
25164 btm = highest + 1;
25165 else if (ascent <= 0)
25166 /* One extra pixel between two glyphs. */
25167 btm = lowest - 1 - ascent - descent;
25168 }
25169 }
25170 else
25171 {
25172 /* A composition rule is specified by an integer
25173 value that encodes global and new reference
25174 points (GREF and NREF). GREF and NREF are
25175 specified by numbers as below:
25176
25177 0---1---2 -- ascent
25178 | |
25179 | |
25180 | |
25181 9--10--11 -- center
25182 | |
25183 ---3---4---5--- baseline
25184 | |
25185 6---7---8 -- descent
25186 */
25187 int rule = COMPOSITION_RULE (cmp, i);
25188 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25189
25190 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25191 grefx = gref % 3, nrefx = nref % 3;
25192 grefy = gref / 3, nrefy = nref / 3;
25193 if (xoff)
25194 xoff = font_height * (xoff - 128) / 256;
25195 if (yoff)
25196 yoff = font_height * (yoff - 128) / 256;
25197
25198 left = (leftmost
25199 + grefx * (rightmost - leftmost) / 2
25200 - nrefx * width / 2
25201 + xoff);
25202
25203 btm = ((grefy == 0 ? highest
25204 : grefy == 1 ? 0
25205 : grefy == 2 ? lowest
25206 : (highest + lowest) / 2)
25207 - (nrefy == 0 ? ascent + descent
25208 : nrefy == 1 ? descent - boff
25209 : nrefy == 2 ? 0
25210 : (ascent + descent) / 2)
25211 + yoff);
25212 }
25213
25214 cmp->offsets[i * 2] = left;
25215 cmp->offsets[i * 2 + 1] = btm + descent;
25216
25217 /* Update the bounding box of the overall glyphs. */
25218 if (width > 0)
25219 {
25220 right = left + width;
25221 if (left < leftmost)
25222 leftmost = left;
25223 if (right > rightmost)
25224 rightmost = right;
25225 }
25226 top = btm + descent + ascent;
25227 if (top > highest)
25228 highest = top;
25229 if (btm < lowest)
25230 lowest = btm;
25231
25232 if (cmp->lbearing > left + lbearing)
25233 cmp->lbearing = left + lbearing;
25234 if (cmp->rbearing < left + rbearing)
25235 cmp->rbearing = left + rbearing;
25236 }
25237 }
25238
25239 /* If there are glyphs whose x-offsets are negative,
25240 shift all glyphs to the right and make all x-offsets
25241 non-negative. */
25242 if (leftmost < 0)
25243 {
25244 for (i = 0; i < cmp->glyph_len; i++)
25245 cmp->offsets[i * 2] -= leftmost;
25246 rightmost -= leftmost;
25247 cmp->lbearing -= leftmost;
25248 cmp->rbearing -= leftmost;
25249 }
25250
25251 if (left_padded && cmp->lbearing < 0)
25252 {
25253 for (i = 0; i < cmp->glyph_len; i++)
25254 cmp->offsets[i * 2] -= cmp->lbearing;
25255 rightmost -= cmp->lbearing;
25256 cmp->rbearing -= cmp->lbearing;
25257 cmp->lbearing = 0;
25258 }
25259 if (right_padded && rightmost < cmp->rbearing)
25260 {
25261 rightmost = cmp->rbearing;
25262 }
25263
25264 cmp->pixel_width = rightmost;
25265 cmp->ascent = highest;
25266 cmp->descent = - lowest;
25267 if (cmp->ascent < font_ascent)
25268 cmp->ascent = font_ascent;
25269 if (cmp->descent < font_descent)
25270 cmp->descent = font_descent;
25271 }
25272
25273 if (it->glyph_row
25274 && (cmp->lbearing < 0
25275 || cmp->rbearing > cmp->pixel_width))
25276 it->glyph_row->contains_overlapping_glyphs_p = 1;
25277
25278 it->pixel_width = cmp->pixel_width;
25279 it->ascent = it->phys_ascent = cmp->ascent;
25280 it->descent = it->phys_descent = cmp->descent;
25281 if (face->box != FACE_NO_BOX)
25282 {
25283 int thick = face->box_line_width;
25284
25285 if (thick > 0)
25286 {
25287 it->ascent += thick;
25288 it->descent += thick;
25289 }
25290 else
25291 thick = - thick;
25292
25293 if (it->start_of_box_run_p)
25294 it->pixel_width += thick;
25295 if (it->end_of_box_run_p)
25296 it->pixel_width += thick;
25297 }
25298
25299 /* If face has an overline, add the height of the overline
25300 (1 pixel) and a 1 pixel margin to the character height. */
25301 if (face->overline_p)
25302 it->ascent += overline_margin;
25303
25304 take_vertical_position_into_account (it);
25305 if (it->ascent < 0)
25306 it->ascent = 0;
25307 if (it->descent < 0)
25308 it->descent = 0;
25309
25310 if (it->glyph_row && cmp->glyph_len > 0)
25311 append_composite_glyph (it);
25312 }
25313 else if (it->what == IT_COMPOSITION)
25314 {
25315 /* A dynamic (automatic) composition. */
25316 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25317 Lisp_Object gstring;
25318 struct font_metrics metrics;
25319
25320 it->nglyphs = 1;
25321
25322 gstring = composition_gstring_from_id (it->cmp_it.id);
25323 it->pixel_width
25324 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25325 &metrics);
25326 if (it->glyph_row
25327 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25328 it->glyph_row->contains_overlapping_glyphs_p = 1;
25329 it->ascent = it->phys_ascent = metrics.ascent;
25330 it->descent = it->phys_descent = metrics.descent;
25331 if (face->box != FACE_NO_BOX)
25332 {
25333 int thick = face->box_line_width;
25334
25335 if (thick > 0)
25336 {
25337 it->ascent += thick;
25338 it->descent += thick;
25339 }
25340 else
25341 thick = - thick;
25342
25343 if (it->start_of_box_run_p)
25344 it->pixel_width += thick;
25345 if (it->end_of_box_run_p)
25346 it->pixel_width += thick;
25347 }
25348 /* If face has an overline, add the height of the overline
25349 (1 pixel) and a 1 pixel margin to the character height. */
25350 if (face->overline_p)
25351 it->ascent += overline_margin;
25352 take_vertical_position_into_account (it);
25353 if (it->ascent < 0)
25354 it->ascent = 0;
25355 if (it->descent < 0)
25356 it->descent = 0;
25357
25358 if (it->glyph_row)
25359 append_composite_glyph (it);
25360 }
25361 else if (it->what == IT_GLYPHLESS)
25362 produce_glyphless_glyph (it, 0, Qnil);
25363 else if (it->what == IT_IMAGE)
25364 produce_image_glyph (it);
25365 else if (it->what == IT_STRETCH)
25366 produce_stretch_glyph (it);
25367
25368 done:
25369 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25370 because this isn't true for images with `:ascent 100'. */
25371 eassert (it->ascent >= 0 && it->descent >= 0);
25372 if (it->area == TEXT_AREA)
25373 it->current_x += it->pixel_width;
25374
25375 if (extra_line_spacing > 0)
25376 {
25377 it->descent += extra_line_spacing;
25378 if (extra_line_spacing > it->max_extra_line_spacing)
25379 it->max_extra_line_spacing = extra_line_spacing;
25380 }
25381
25382 it->max_ascent = max (it->max_ascent, it->ascent);
25383 it->max_descent = max (it->max_descent, it->descent);
25384 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25385 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25386 }
25387
25388 /* EXPORT for RIF:
25389 Output LEN glyphs starting at START at the nominal cursor position.
25390 Advance the nominal cursor over the text. The global variable
25391 updated_window contains the window being updated, updated_row is
25392 the glyph row being updated, and updated_area is the area of that
25393 row being updated. */
25394
25395 void
25396 x_write_glyphs (struct glyph *start, int len)
25397 {
25398 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25399
25400 eassert (updated_window && updated_row);
25401 /* When the window is hscrolled, cursor hpos can legitimately be out
25402 of bounds, but we draw the cursor at the corresponding window
25403 margin in that case. */
25404 if (!updated_row->reversed_p && chpos < 0)
25405 chpos = 0;
25406 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25407 chpos = updated_row->used[TEXT_AREA] - 1;
25408
25409 block_input ();
25410
25411 /* Write glyphs. */
25412
25413 hpos = start - updated_row->glyphs[updated_area];
25414 x = draw_glyphs (updated_window, output_cursor.x,
25415 updated_row, updated_area,
25416 hpos, hpos + len,
25417 DRAW_NORMAL_TEXT, 0);
25418
25419 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25420 if (updated_area == TEXT_AREA
25421 && updated_window->phys_cursor_on_p
25422 && updated_window->phys_cursor.vpos == output_cursor.vpos
25423 && chpos >= hpos
25424 && chpos < hpos + len)
25425 updated_window->phys_cursor_on_p = 0;
25426
25427 unblock_input ();
25428
25429 /* Advance the output cursor. */
25430 output_cursor.hpos += len;
25431 output_cursor.x = x;
25432 }
25433
25434
25435 /* EXPORT for RIF:
25436 Insert LEN glyphs from START at the nominal cursor position. */
25437
25438 void
25439 x_insert_glyphs (struct glyph *start, int len)
25440 {
25441 struct frame *f;
25442 struct window *w;
25443 int line_height, shift_by_width, shifted_region_width;
25444 struct glyph_row *row;
25445 struct glyph *glyph;
25446 int frame_x, frame_y;
25447 ptrdiff_t hpos;
25448
25449 eassert (updated_window && updated_row);
25450 block_input ();
25451 w = updated_window;
25452 f = XFRAME (WINDOW_FRAME (w));
25453
25454 /* Get the height of the line we are in. */
25455 row = updated_row;
25456 line_height = row->height;
25457
25458 /* Get the width of the glyphs to insert. */
25459 shift_by_width = 0;
25460 for (glyph = start; glyph < start + len; ++glyph)
25461 shift_by_width += glyph->pixel_width;
25462
25463 /* Get the width of the region to shift right. */
25464 shifted_region_width = (window_box_width (w, updated_area)
25465 - output_cursor.x
25466 - shift_by_width);
25467
25468 /* Shift right. */
25469 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25470 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25471
25472 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25473 line_height, shift_by_width);
25474
25475 /* Write the glyphs. */
25476 hpos = start - row->glyphs[updated_area];
25477 draw_glyphs (w, output_cursor.x, row, updated_area,
25478 hpos, hpos + len,
25479 DRAW_NORMAL_TEXT, 0);
25480
25481 /* Advance the output cursor. */
25482 output_cursor.hpos += len;
25483 output_cursor.x += shift_by_width;
25484 unblock_input ();
25485 }
25486
25487
25488 /* EXPORT for RIF:
25489 Erase the current text line from the nominal cursor position
25490 (inclusive) to pixel column TO_X (exclusive). The idea is that
25491 everything from TO_X onward is already erased.
25492
25493 TO_X is a pixel position relative to updated_area of
25494 updated_window. TO_X == -1 means clear to the end of this area. */
25495
25496 void
25497 x_clear_end_of_line (int to_x)
25498 {
25499 struct frame *f;
25500 struct window *w = updated_window;
25501 int max_x, min_y, max_y;
25502 int from_x, from_y, to_y;
25503
25504 eassert (updated_window && updated_row);
25505 f = XFRAME (w->frame);
25506
25507 if (updated_row->full_width_p)
25508 max_x = WINDOW_TOTAL_WIDTH (w);
25509 else
25510 max_x = window_box_width (w, updated_area);
25511 max_y = window_text_bottom_y (w);
25512
25513 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25514 of window. For TO_X > 0, truncate to end of drawing area. */
25515 if (to_x == 0)
25516 return;
25517 else if (to_x < 0)
25518 to_x = max_x;
25519 else
25520 to_x = min (to_x, max_x);
25521
25522 to_y = min (max_y, output_cursor.y + updated_row->height);
25523
25524 /* Notice if the cursor will be cleared by this operation. */
25525 if (!updated_row->full_width_p)
25526 notice_overwritten_cursor (w, updated_area,
25527 output_cursor.x, -1,
25528 updated_row->y,
25529 MATRIX_ROW_BOTTOM_Y (updated_row));
25530
25531 from_x = output_cursor.x;
25532
25533 /* Translate to frame coordinates. */
25534 if (updated_row->full_width_p)
25535 {
25536 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25537 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25538 }
25539 else
25540 {
25541 int area_left = window_box_left (w, updated_area);
25542 from_x += area_left;
25543 to_x += area_left;
25544 }
25545
25546 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25547 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25548 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25549
25550 /* Prevent inadvertently clearing to end of the X window. */
25551 if (to_x > from_x && to_y > from_y)
25552 {
25553 block_input ();
25554 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25555 to_x - from_x, to_y - from_y);
25556 unblock_input ();
25557 }
25558 }
25559
25560 #endif /* HAVE_WINDOW_SYSTEM */
25561
25562
25563 \f
25564 /***********************************************************************
25565 Cursor types
25566 ***********************************************************************/
25567
25568 /* Value is the internal representation of the specified cursor type
25569 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25570 of the bar cursor. */
25571
25572 static enum text_cursor_kinds
25573 get_specified_cursor_type (Lisp_Object arg, int *width)
25574 {
25575 enum text_cursor_kinds type;
25576
25577 if (NILP (arg))
25578 return NO_CURSOR;
25579
25580 if (EQ (arg, Qbox))
25581 return FILLED_BOX_CURSOR;
25582
25583 if (EQ (arg, Qhollow))
25584 return HOLLOW_BOX_CURSOR;
25585
25586 if (EQ (arg, Qbar))
25587 {
25588 *width = 2;
25589 return BAR_CURSOR;
25590 }
25591
25592 if (CONSP (arg)
25593 && EQ (XCAR (arg), Qbar)
25594 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25595 {
25596 *width = XINT (XCDR (arg));
25597 return BAR_CURSOR;
25598 }
25599
25600 if (EQ (arg, Qhbar))
25601 {
25602 *width = 2;
25603 return HBAR_CURSOR;
25604 }
25605
25606 if (CONSP (arg)
25607 && EQ (XCAR (arg), Qhbar)
25608 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25609 {
25610 *width = XINT (XCDR (arg));
25611 return HBAR_CURSOR;
25612 }
25613
25614 /* Treat anything unknown as "hollow box cursor".
25615 It was bad to signal an error; people have trouble fixing
25616 .Xdefaults with Emacs, when it has something bad in it. */
25617 type = HOLLOW_BOX_CURSOR;
25618
25619 return type;
25620 }
25621
25622 /* Set the default cursor types for specified frame. */
25623 void
25624 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25625 {
25626 int width = 1;
25627 Lisp_Object tem;
25628
25629 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25630 FRAME_CURSOR_WIDTH (f) = width;
25631
25632 /* By default, set up the blink-off state depending on the on-state. */
25633
25634 tem = Fassoc (arg, Vblink_cursor_alist);
25635 if (!NILP (tem))
25636 {
25637 FRAME_BLINK_OFF_CURSOR (f)
25638 = get_specified_cursor_type (XCDR (tem), &width);
25639 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25640 }
25641 else
25642 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25643 }
25644
25645
25646 #ifdef HAVE_WINDOW_SYSTEM
25647
25648 /* Return the cursor we want to be displayed in window W. Return
25649 width of bar/hbar cursor through WIDTH arg. Return with
25650 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25651 (i.e. if the `system caret' should track this cursor).
25652
25653 In a mini-buffer window, we want the cursor only to appear if we
25654 are reading input from this window. For the selected window, we
25655 want the cursor type given by the frame parameter or buffer local
25656 setting of cursor-type. If explicitly marked off, draw no cursor.
25657 In all other cases, we want a hollow box cursor. */
25658
25659 static enum text_cursor_kinds
25660 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25661 int *active_cursor)
25662 {
25663 struct frame *f = XFRAME (w->frame);
25664 struct buffer *b = XBUFFER (w->buffer);
25665 int cursor_type = DEFAULT_CURSOR;
25666 Lisp_Object alt_cursor;
25667 int non_selected = 0;
25668
25669 *active_cursor = 1;
25670
25671 /* Echo area */
25672 if (cursor_in_echo_area
25673 && FRAME_HAS_MINIBUF_P (f)
25674 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25675 {
25676 if (w == XWINDOW (echo_area_window))
25677 {
25678 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25679 {
25680 *width = FRAME_CURSOR_WIDTH (f);
25681 return FRAME_DESIRED_CURSOR (f);
25682 }
25683 else
25684 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25685 }
25686
25687 *active_cursor = 0;
25688 non_selected = 1;
25689 }
25690
25691 /* Detect a nonselected window or nonselected frame. */
25692 else if (w != XWINDOW (f->selected_window)
25693 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25694 {
25695 *active_cursor = 0;
25696
25697 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25698 return NO_CURSOR;
25699
25700 non_selected = 1;
25701 }
25702
25703 /* Never display a cursor in a window in which cursor-type is nil. */
25704 if (NILP (BVAR (b, cursor_type)))
25705 return NO_CURSOR;
25706
25707 /* Get the normal cursor type for this window. */
25708 if (EQ (BVAR (b, cursor_type), Qt))
25709 {
25710 cursor_type = FRAME_DESIRED_CURSOR (f);
25711 *width = FRAME_CURSOR_WIDTH (f);
25712 }
25713 else
25714 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25715
25716 /* Use cursor-in-non-selected-windows instead
25717 for non-selected window or frame. */
25718 if (non_selected)
25719 {
25720 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25721 if (!EQ (Qt, alt_cursor))
25722 return get_specified_cursor_type (alt_cursor, width);
25723 /* t means modify the normal cursor type. */
25724 if (cursor_type == FILLED_BOX_CURSOR)
25725 cursor_type = HOLLOW_BOX_CURSOR;
25726 else if (cursor_type == BAR_CURSOR && *width > 1)
25727 --*width;
25728 return cursor_type;
25729 }
25730
25731 /* Use normal cursor if not blinked off. */
25732 if (!w->cursor_off_p)
25733 {
25734 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25735 {
25736 if (cursor_type == FILLED_BOX_CURSOR)
25737 {
25738 /* Using a block cursor on large images can be very annoying.
25739 So use a hollow cursor for "large" images.
25740 If image is not transparent (no mask), also use hollow cursor. */
25741 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25742 if (img != NULL && IMAGEP (img->spec))
25743 {
25744 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25745 where N = size of default frame font size.
25746 This should cover most of the "tiny" icons people may use. */
25747 if (!img->mask
25748 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25749 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25750 cursor_type = HOLLOW_BOX_CURSOR;
25751 }
25752 }
25753 else if (cursor_type != NO_CURSOR)
25754 {
25755 /* Display current only supports BOX and HOLLOW cursors for images.
25756 So for now, unconditionally use a HOLLOW cursor when cursor is
25757 not a solid box cursor. */
25758 cursor_type = HOLLOW_BOX_CURSOR;
25759 }
25760 }
25761 return cursor_type;
25762 }
25763
25764 /* Cursor is blinked off, so determine how to "toggle" it. */
25765
25766 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25767 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25768 return get_specified_cursor_type (XCDR (alt_cursor), width);
25769
25770 /* Then see if frame has specified a specific blink off cursor type. */
25771 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25772 {
25773 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25774 return FRAME_BLINK_OFF_CURSOR (f);
25775 }
25776
25777 #if 0
25778 /* Some people liked having a permanently visible blinking cursor,
25779 while others had very strong opinions against it. So it was
25780 decided to remove it. KFS 2003-09-03 */
25781
25782 /* Finally perform built-in cursor blinking:
25783 filled box <-> hollow box
25784 wide [h]bar <-> narrow [h]bar
25785 narrow [h]bar <-> no cursor
25786 other type <-> no cursor */
25787
25788 if (cursor_type == FILLED_BOX_CURSOR)
25789 return HOLLOW_BOX_CURSOR;
25790
25791 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25792 {
25793 *width = 1;
25794 return cursor_type;
25795 }
25796 #endif
25797
25798 return NO_CURSOR;
25799 }
25800
25801
25802 /* Notice when the text cursor of window W has been completely
25803 overwritten by a drawing operation that outputs glyphs in AREA
25804 starting at X0 and ending at X1 in the line starting at Y0 and
25805 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25806 the rest of the line after X0 has been written. Y coordinates
25807 are window-relative. */
25808
25809 static void
25810 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25811 int x0, int x1, int y0, int y1)
25812 {
25813 int cx0, cx1, cy0, cy1;
25814 struct glyph_row *row;
25815
25816 if (!w->phys_cursor_on_p)
25817 return;
25818 if (area != TEXT_AREA)
25819 return;
25820
25821 if (w->phys_cursor.vpos < 0
25822 || w->phys_cursor.vpos >= w->current_matrix->nrows
25823 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25824 !(row->enabled_p && row->displays_text_p)))
25825 return;
25826
25827 if (row->cursor_in_fringe_p)
25828 {
25829 row->cursor_in_fringe_p = 0;
25830 draw_fringe_bitmap (w, row, row->reversed_p);
25831 w->phys_cursor_on_p = 0;
25832 return;
25833 }
25834
25835 cx0 = w->phys_cursor.x;
25836 cx1 = cx0 + w->phys_cursor_width;
25837 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25838 return;
25839
25840 /* The cursor image will be completely removed from the
25841 screen if the output area intersects the cursor area in
25842 y-direction. When we draw in [y0 y1[, and some part of
25843 the cursor is at y < y0, that part must have been drawn
25844 before. When scrolling, the cursor is erased before
25845 actually scrolling, so we don't come here. When not
25846 scrolling, the rows above the old cursor row must have
25847 changed, and in this case these rows must have written
25848 over the cursor image.
25849
25850 Likewise if part of the cursor is below y1, with the
25851 exception of the cursor being in the first blank row at
25852 the buffer and window end because update_text_area
25853 doesn't draw that row. (Except when it does, but
25854 that's handled in update_text_area.) */
25855
25856 cy0 = w->phys_cursor.y;
25857 cy1 = cy0 + w->phys_cursor_height;
25858 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25859 return;
25860
25861 w->phys_cursor_on_p = 0;
25862 }
25863
25864 #endif /* HAVE_WINDOW_SYSTEM */
25865
25866 \f
25867 /************************************************************************
25868 Mouse Face
25869 ************************************************************************/
25870
25871 #ifdef HAVE_WINDOW_SYSTEM
25872
25873 /* EXPORT for RIF:
25874 Fix the display of area AREA of overlapping row ROW in window W
25875 with respect to the overlapping part OVERLAPS. */
25876
25877 void
25878 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25879 enum glyph_row_area area, int overlaps)
25880 {
25881 int i, x;
25882
25883 block_input ();
25884
25885 x = 0;
25886 for (i = 0; i < row->used[area];)
25887 {
25888 if (row->glyphs[area][i].overlaps_vertically_p)
25889 {
25890 int start = i, start_x = x;
25891
25892 do
25893 {
25894 x += row->glyphs[area][i].pixel_width;
25895 ++i;
25896 }
25897 while (i < row->used[area]
25898 && row->glyphs[area][i].overlaps_vertically_p);
25899
25900 draw_glyphs (w, start_x, row, area,
25901 start, i,
25902 DRAW_NORMAL_TEXT, overlaps);
25903 }
25904 else
25905 {
25906 x += row->glyphs[area][i].pixel_width;
25907 ++i;
25908 }
25909 }
25910
25911 unblock_input ();
25912 }
25913
25914
25915 /* EXPORT:
25916 Draw the cursor glyph of window W in glyph row ROW. See the
25917 comment of draw_glyphs for the meaning of HL. */
25918
25919 void
25920 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25921 enum draw_glyphs_face hl)
25922 {
25923 /* If cursor hpos is out of bounds, don't draw garbage. This can
25924 happen in mini-buffer windows when switching between echo area
25925 glyphs and mini-buffer. */
25926 if ((row->reversed_p
25927 ? (w->phys_cursor.hpos >= 0)
25928 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25929 {
25930 int on_p = w->phys_cursor_on_p;
25931 int x1;
25932 int hpos = w->phys_cursor.hpos;
25933
25934 /* When the window is hscrolled, cursor hpos can legitimately be
25935 out of bounds, but we draw the cursor at the corresponding
25936 window margin in that case. */
25937 if (!row->reversed_p && hpos < 0)
25938 hpos = 0;
25939 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25940 hpos = row->used[TEXT_AREA] - 1;
25941
25942 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25943 hl, 0);
25944 w->phys_cursor_on_p = on_p;
25945
25946 if (hl == DRAW_CURSOR)
25947 w->phys_cursor_width = x1 - w->phys_cursor.x;
25948 /* When we erase the cursor, and ROW is overlapped by other
25949 rows, make sure that these overlapping parts of other rows
25950 are redrawn. */
25951 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25952 {
25953 w->phys_cursor_width = x1 - w->phys_cursor.x;
25954
25955 if (row > w->current_matrix->rows
25956 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25957 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25958 OVERLAPS_ERASED_CURSOR);
25959
25960 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25961 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25962 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25963 OVERLAPS_ERASED_CURSOR);
25964 }
25965 }
25966 }
25967
25968
25969 /* EXPORT:
25970 Erase the image of a cursor of window W from the screen. */
25971
25972 void
25973 erase_phys_cursor (struct window *w)
25974 {
25975 struct frame *f = XFRAME (w->frame);
25976 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25977 int hpos = w->phys_cursor.hpos;
25978 int vpos = w->phys_cursor.vpos;
25979 int mouse_face_here_p = 0;
25980 struct glyph_matrix *active_glyphs = w->current_matrix;
25981 struct glyph_row *cursor_row;
25982 struct glyph *cursor_glyph;
25983 enum draw_glyphs_face hl;
25984
25985 /* No cursor displayed or row invalidated => nothing to do on the
25986 screen. */
25987 if (w->phys_cursor_type == NO_CURSOR)
25988 goto mark_cursor_off;
25989
25990 /* VPOS >= active_glyphs->nrows means that window has been resized.
25991 Don't bother to erase the cursor. */
25992 if (vpos >= active_glyphs->nrows)
25993 goto mark_cursor_off;
25994
25995 /* If row containing cursor is marked invalid, there is nothing we
25996 can do. */
25997 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25998 if (!cursor_row->enabled_p)
25999 goto mark_cursor_off;
26000
26001 /* If line spacing is > 0, old cursor may only be partially visible in
26002 window after split-window. So adjust visible height. */
26003 cursor_row->visible_height = min (cursor_row->visible_height,
26004 window_text_bottom_y (w) - cursor_row->y);
26005
26006 /* If row is completely invisible, don't attempt to delete a cursor which
26007 isn't there. This can happen if cursor is at top of a window, and
26008 we switch to a buffer with a header line in that window. */
26009 if (cursor_row->visible_height <= 0)
26010 goto mark_cursor_off;
26011
26012 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26013 if (cursor_row->cursor_in_fringe_p)
26014 {
26015 cursor_row->cursor_in_fringe_p = 0;
26016 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26017 goto mark_cursor_off;
26018 }
26019
26020 /* This can happen when the new row is shorter than the old one.
26021 In this case, either draw_glyphs or clear_end_of_line
26022 should have cleared the cursor. Note that we wouldn't be
26023 able to erase the cursor in this case because we don't have a
26024 cursor glyph at hand. */
26025 if ((cursor_row->reversed_p
26026 ? (w->phys_cursor.hpos < 0)
26027 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26028 goto mark_cursor_off;
26029
26030 /* When the window is hscrolled, cursor hpos can legitimately be out
26031 of bounds, but we draw the cursor at the corresponding window
26032 margin in that case. */
26033 if (!cursor_row->reversed_p && hpos < 0)
26034 hpos = 0;
26035 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26036 hpos = cursor_row->used[TEXT_AREA] - 1;
26037
26038 /* If the cursor is in the mouse face area, redisplay that when
26039 we clear the cursor. */
26040 if (! NILP (hlinfo->mouse_face_window)
26041 && coords_in_mouse_face_p (w, hpos, vpos)
26042 /* Don't redraw the cursor's spot in mouse face if it is at the
26043 end of a line (on a newline). The cursor appears there, but
26044 mouse highlighting does not. */
26045 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26046 mouse_face_here_p = 1;
26047
26048 /* Maybe clear the display under the cursor. */
26049 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26050 {
26051 int x, y, left_x;
26052 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26053 int width;
26054
26055 cursor_glyph = get_phys_cursor_glyph (w);
26056 if (cursor_glyph == NULL)
26057 goto mark_cursor_off;
26058
26059 width = cursor_glyph->pixel_width;
26060 left_x = window_box_left_offset (w, TEXT_AREA);
26061 x = w->phys_cursor.x;
26062 if (x < left_x)
26063 width -= left_x - x;
26064 width = min (width, window_box_width (w, TEXT_AREA) - x);
26065 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26066 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26067
26068 if (width > 0)
26069 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26070 }
26071
26072 /* Erase the cursor by redrawing the character underneath it. */
26073 if (mouse_face_here_p)
26074 hl = DRAW_MOUSE_FACE;
26075 else
26076 hl = DRAW_NORMAL_TEXT;
26077 draw_phys_cursor_glyph (w, cursor_row, hl);
26078
26079 mark_cursor_off:
26080 w->phys_cursor_on_p = 0;
26081 w->phys_cursor_type = NO_CURSOR;
26082 }
26083
26084
26085 /* EXPORT:
26086 Display or clear cursor of window W. If ON is zero, clear the
26087 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26088 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26089
26090 void
26091 display_and_set_cursor (struct window *w, int on,
26092 int hpos, int vpos, int x, int y)
26093 {
26094 struct frame *f = XFRAME (w->frame);
26095 int new_cursor_type;
26096 int new_cursor_width;
26097 int active_cursor;
26098 struct glyph_row *glyph_row;
26099 struct glyph *glyph;
26100
26101 /* This is pointless on invisible frames, and dangerous on garbaged
26102 windows and frames; in the latter case, the frame or window may
26103 be in the midst of changing its size, and x and y may be off the
26104 window. */
26105 if (! FRAME_VISIBLE_P (f)
26106 || FRAME_GARBAGED_P (f)
26107 || vpos >= w->current_matrix->nrows
26108 || hpos >= w->current_matrix->matrix_w)
26109 return;
26110
26111 /* If cursor is off and we want it off, return quickly. */
26112 if (!on && !w->phys_cursor_on_p)
26113 return;
26114
26115 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26116 /* If cursor row is not enabled, we don't really know where to
26117 display the cursor. */
26118 if (!glyph_row->enabled_p)
26119 {
26120 w->phys_cursor_on_p = 0;
26121 return;
26122 }
26123
26124 glyph = NULL;
26125 if (!glyph_row->exact_window_width_line_p
26126 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26127 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26128
26129 eassert (input_blocked_p ());
26130
26131 /* Set new_cursor_type to the cursor we want to be displayed. */
26132 new_cursor_type = get_window_cursor_type (w, glyph,
26133 &new_cursor_width, &active_cursor);
26134
26135 /* If cursor is currently being shown and we don't want it to be or
26136 it is in the wrong place, or the cursor type is not what we want,
26137 erase it. */
26138 if (w->phys_cursor_on_p
26139 && (!on
26140 || w->phys_cursor.x != x
26141 || w->phys_cursor.y != y
26142 || new_cursor_type != w->phys_cursor_type
26143 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26144 && new_cursor_width != w->phys_cursor_width)))
26145 erase_phys_cursor (w);
26146
26147 /* Don't check phys_cursor_on_p here because that flag is only set
26148 to zero in some cases where we know that the cursor has been
26149 completely erased, to avoid the extra work of erasing the cursor
26150 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26151 still not be visible, or it has only been partly erased. */
26152 if (on)
26153 {
26154 w->phys_cursor_ascent = glyph_row->ascent;
26155 w->phys_cursor_height = glyph_row->height;
26156
26157 /* Set phys_cursor_.* before x_draw_.* is called because some
26158 of them may need the information. */
26159 w->phys_cursor.x = x;
26160 w->phys_cursor.y = glyph_row->y;
26161 w->phys_cursor.hpos = hpos;
26162 w->phys_cursor.vpos = vpos;
26163 }
26164
26165 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26166 new_cursor_type, new_cursor_width,
26167 on, active_cursor);
26168 }
26169
26170
26171 /* Switch the display of W's cursor on or off, according to the value
26172 of ON. */
26173
26174 static void
26175 update_window_cursor (struct window *w, int on)
26176 {
26177 /* Don't update cursor in windows whose frame is in the process
26178 of being deleted. */
26179 if (w->current_matrix)
26180 {
26181 int hpos = w->phys_cursor.hpos;
26182 int vpos = w->phys_cursor.vpos;
26183 struct glyph_row *row;
26184
26185 if (vpos >= w->current_matrix->nrows
26186 || hpos >= w->current_matrix->matrix_w)
26187 return;
26188
26189 row = MATRIX_ROW (w->current_matrix, vpos);
26190
26191 /* When the window is hscrolled, cursor hpos can legitimately be
26192 out of bounds, but we draw the cursor at the corresponding
26193 window margin in that case. */
26194 if (!row->reversed_p && hpos < 0)
26195 hpos = 0;
26196 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26197 hpos = row->used[TEXT_AREA] - 1;
26198
26199 block_input ();
26200 display_and_set_cursor (w, on, hpos, vpos,
26201 w->phys_cursor.x, w->phys_cursor.y);
26202 unblock_input ();
26203 }
26204 }
26205
26206
26207 /* Call update_window_cursor with parameter ON_P on all leaf windows
26208 in the window tree rooted at W. */
26209
26210 static void
26211 update_cursor_in_window_tree (struct window *w, int on_p)
26212 {
26213 while (w)
26214 {
26215 if (!NILP (w->hchild))
26216 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26217 else if (!NILP (w->vchild))
26218 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26219 else
26220 update_window_cursor (w, on_p);
26221
26222 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26223 }
26224 }
26225
26226
26227 /* EXPORT:
26228 Display the cursor on window W, or clear it, according to ON_P.
26229 Don't change the cursor's position. */
26230
26231 void
26232 x_update_cursor (struct frame *f, int on_p)
26233 {
26234 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26235 }
26236
26237
26238 /* EXPORT:
26239 Clear the cursor of window W to background color, and mark the
26240 cursor as not shown. This is used when the text where the cursor
26241 is about to be rewritten. */
26242
26243 void
26244 x_clear_cursor (struct window *w)
26245 {
26246 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26247 update_window_cursor (w, 0);
26248 }
26249
26250 #endif /* HAVE_WINDOW_SYSTEM */
26251
26252 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26253 and MSDOS. */
26254 static void
26255 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26256 int start_hpos, int end_hpos,
26257 enum draw_glyphs_face draw)
26258 {
26259 #ifdef HAVE_WINDOW_SYSTEM
26260 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26261 {
26262 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26263 return;
26264 }
26265 #endif
26266 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26267 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26268 #endif
26269 }
26270
26271 /* Display the active region described by mouse_face_* according to DRAW. */
26272
26273 static void
26274 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26275 {
26276 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26277 struct frame *f = XFRAME (WINDOW_FRAME (w));
26278
26279 if (/* If window is in the process of being destroyed, don't bother
26280 to do anything. */
26281 w->current_matrix != NULL
26282 /* Don't update mouse highlight if hidden */
26283 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26284 /* Recognize when we are called to operate on rows that don't exist
26285 anymore. This can happen when a window is split. */
26286 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26287 {
26288 int phys_cursor_on_p = w->phys_cursor_on_p;
26289 struct glyph_row *row, *first, *last;
26290
26291 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26292 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26293
26294 for (row = first; row <= last && row->enabled_p; ++row)
26295 {
26296 int start_hpos, end_hpos, start_x;
26297
26298 /* For all but the first row, the highlight starts at column 0. */
26299 if (row == first)
26300 {
26301 /* R2L rows have BEG and END in reversed order, but the
26302 screen drawing geometry is always left to right. So
26303 we need to mirror the beginning and end of the
26304 highlighted area in R2L rows. */
26305 if (!row->reversed_p)
26306 {
26307 start_hpos = hlinfo->mouse_face_beg_col;
26308 start_x = hlinfo->mouse_face_beg_x;
26309 }
26310 else if (row == last)
26311 {
26312 start_hpos = hlinfo->mouse_face_end_col;
26313 start_x = hlinfo->mouse_face_end_x;
26314 }
26315 else
26316 {
26317 start_hpos = 0;
26318 start_x = 0;
26319 }
26320 }
26321 else if (row->reversed_p && row == last)
26322 {
26323 start_hpos = hlinfo->mouse_face_end_col;
26324 start_x = hlinfo->mouse_face_end_x;
26325 }
26326 else
26327 {
26328 start_hpos = 0;
26329 start_x = 0;
26330 }
26331
26332 if (row == last)
26333 {
26334 if (!row->reversed_p)
26335 end_hpos = hlinfo->mouse_face_end_col;
26336 else if (row == first)
26337 end_hpos = hlinfo->mouse_face_beg_col;
26338 else
26339 {
26340 end_hpos = row->used[TEXT_AREA];
26341 if (draw == DRAW_NORMAL_TEXT)
26342 row->fill_line_p = 1; /* Clear to end of line */
26343 }
26344 }
26345 else if (row->reversed_p && row == first)
26346 end_hpos = hlinfo->mouse_face_beg_col;
26347 else
26348 {
26349 end_hpos = row->used[TEXT_AREA];
26350 if (draw == DRAW_NORMAL_TEXT)
26351 row->fill_line_p = 1; /* Clear to end of line */
26352 }
26353
26354 if (end_hpos > start_hpos)
26355 {
26356 draw_row_with_mouse_face (w, start_x, row,
26357 start_hpos, end_hpos, draw);
26358
26359 row->mouse_face_p
26360 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26361 }
26362 }
26363
26364 #ifdef HAVE_WINDOW_SYSTEM
26365 /* When we've written over the cursor, arrange for it to
26366 be displayed again. */
26367 if (FRAME_WINDOW_P (f)
26368 && phys_cursor_on_p && !w->phys_cursor_on_p)
26369 {
26370 int hpos = w->phys_cursor.hpos;
26371
26372 /* When the window is hscrolled, cursor hpos can legitimately be
26373 out of bounds, but we draw the cursor at the corresponding
26374 window margin in that case. */
26375 if (!row->reversed_p && hpos < 0)
26376 hpos = 0;
26377 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26378 hpos = row->used[TEXT_AREA] - 1;
26379
26380 block_input ();
26381 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26382 w->phys_cursor.x, w->phys_cursor.y);
26383 unblock_input ();
26384 }
26385 #endif /* HAVE_WINDOW_SYSTEM */
26386 }
26387
26388 #ifdef HAVE_WINDOW_SYSTEM
26389 /* Change the mouse cursor. */
26390 if (FRAME_WINDOW_P (f))
26391 {
26392 if (draw == DRAW_NORMAL_TEXT
26393 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26394 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26395 else if (draw == DRAW_MOUSE_FACE)
26396 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26397 else
26398 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26399 }
26400 #endif /* HAVE_WINDOW_SYSTEM */
26401 }
26402
26403 /* EXPORT:
26404 Clear out the mouse-highlighted active region.
26405 Redraw it un-highlighted first. Value is non-zero if mouse
26406 face was actually drawn unhighlighted. */
26407
26408 int
26409 clear_mouse_face (Mouse_HLInfo *hlinfo)
26410 {
26411 int cleared = 0;
26412
26413 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26414 {
26415 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26416 cleared = 1;
26417 }
26418
26419 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26420 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26421 hlinfo->mouse_face_window = Qnil;
26422 hlinfo->mouse_face_overlay = Qnil;
26423 return cleared;
26424 }
26425
26426 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26427 within the mouse face on that window. */
26428 static int
26429 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26430 {
26431 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26432
26433 /* Quickly resolve the easy cases. */
26434 if (!(WINDOWP (hlinfo->mouse_face_window)
26435 && XWINDOW (hlinfo->mouse_face_window) == w))
26436 return 0;
26437 if (vpos < hlinfo->mouse_face_beg_row
26438 || vpos > hlinfo->mouse_face_end_row)
26439 return 0;
26440 if (vpos > hlinfo->mouse_face_beg_row
26441 && vpos < hlinfo->mouse_face_end_row)
26442 return 1;
26443
26444 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26445 {
26446 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26447 {
26448 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26449 return 1;
26450 }
26451 else if ((vpos == hlinfo->mouse_face_beg_row
26452 && hpos >= hlinfo->mouse_face_beg_col)
26453 || (vpos == hlinfo->mouse_face_end_row
26454 && hpos < hlinfo->mouse_face_end_col))
26455 return 1;
26456 }
26457 else
26458 {
26459 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26460 {
26461 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26462 return 1;
26463 }
26464 else if ((vpos == hlinfo->mouse_face_beg_row
26465 && hpos <= hlinfo->mouse_face_beg_col)
26466 || (vpos == hlinfo->mouse_face_end_row
26467 && hpos > hlinfo->mouse_face_end_col))
26468 return 1;
26469 }
26470 return 0;
26471 }
26472
26473
26474 /* EXPORT:
26475 Non-zero if physical cursor of window W is within mouse face. */
26476
26477 int
26478 cursor_in_mouse_face_p (struct window *w)
26479 {
26480 int hpos = w->phys_cursor.hpos;
26481 int vpos = w->phys_cursor.vpos;
26482 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26483
26484 /* When the window is hscrolled, cursor hpos can legitimately be out
26485 of bounds, but we draw the cursor at the corresponding window
26486 margin in that case. */
26487 if (!row->reversed_p && hpos < 0)
26488 hpos = 0;
26489 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26490 hpos = row->used[TEXT_AREA] - 1;
26491
26492 return coords_in_mouse_face_p (w, hpos, vpos);
26493 }
26494
26495
26496 \f
26497 /* Find the glyph rows START_ROW and END_ROW of window W that display
26498 characters between buffer positions START_CHARPOS and END_CHARPOS
26499 (excluding END_CHARPOS). DISP_STRING is a display string that
26500 covers these buffer positions. This is similar to
26501 row_containing_pos, but is more accurate when bidi reordering makes
26502 buffer positions change non-linearly with glyph rows. */
26503 static void
26504 rows_from_pos_range (struct window *w,
26505 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26506 Lisp_Object disp_string,
26507 struct glyph_row **start, struct glyph_row **end)
26508 {
26509 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26510 int last_y = window_text_bottom_y (w);
26511 struct glyph_row *row;
26512
26513 *start = NULL;
26514 *end = NULL;
26515
26516 while (!first->enabled_p
26517 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26518 first++;
26519
26520 /* Find the START row. */
26521 for (row = first;
26522 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26523 row++)
26524 {
26525 /* A row can potentially be the START row if the range of the
26526 characters it displays intersects the range
26527 [START_CHARPOS..END_CHARPOS). */
26528 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26529 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26530 /* See the commentary in row_containing_pos, for the
26531 explanation of the complicated way to check whether
26532 some position is beyond the end of the characters
26533 displayed by a row. */
26534 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26535 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26536 && !row->ends_at_zv_p
26537 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26538 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26539 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26540 && !row->ends_at_zv_p
26541 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26542 {
26543 /* Found a candidate row. Now make sure at least one of the
26544 glyphs it displays has a charpos from the range
26545 [START_CHARPOS..END_CHARPOS).
26546
26547 This is not obvious because bidi reordering could make
26548 buffer positions of a row be 1,2,3,102,101,100, and if we
26549 want to highlight characters in [50..60), we don't want
26550 this row, even though [50..60) does intersect [1..103),
26551 the range of character positions given by the row's start
26552 and end positions. */
26553 struct glyph *g = row->glyphs[TEXT_AREA];
26554 struct glyph *e = g + row->used[TEXT_AREA];
26555
26556 while (g < e)
26557 {
26558 if (((BUFFERP (g->object) || INTEGERP (g->object))
26559 && start_charpos <= g->charpos && g->charpos < end_charpos)
26560 /* A glyph that comes from DISP_STRING is by
26561 definition to be highlighted. */
26562 || EQ (g->object, disp_string))
26563 *start = row;
26564 g++;
26565 }
26566 if (*start)
26567 break;
26568 }
26569 }
26570
26571 /* Find the END row. */
26572 if (!*start
26573 /* If the last row is partially visible, start looking for END
26574 from that row, instead of starting from FIRST. */
26575 && !(row->enabled_p
26576 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26577 row = first;
26578 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26579 {
26580 struct glyph_row *next = row + 1;
26581 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26582
26583 if (!next->enabled_p
26584 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26585 /* The first row >= START whose range of displayed characters
26586 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26587 is the row END + 1. */
26588 || (start_charpos < next_start
26589 && end_charpos < next_start)
26590 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26591 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26592 && !next->ends_at_zv_p
26593 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26594 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26595 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26596 && !next->ends_at_zv_p
26597 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26598 {
26599 *end = row;
26600 break;
26601 }
26602 else
26603 {
26604 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26605 but none of the characters it displays are in the range, it is
26606 also END + 1. */
26607 struct glyph *g = next->glyphs[TEXT_AREA];
26608 struct glyph *s = g;
26609 struct glyph *e = g + next->used[TEXT_AREA];
26610
26611 while (g < e)
26612 {
26613 if (((BUFFERP (g->object) || INTEGERP (g->object))
26614 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26615 /* If the buffer position of the first glyph in
26616 the row is equal to END_CHARPOS, it means
26617 the last character to be highlighted is the
26618 newline of ROW, and we must consider NEXT as
26619 END, not END+1. */
26620 || (((!next->reversed_p && g == s)
26621 || (next->reversed_p && g == e - 1))
26622 && (g->charpos == end_charpos
26623 /* Special case for when NEXT is an
26624 empty line at ZV. */
26625 || (g->charpos == -1
26626 && !row->ends_at_zv_p
26627 && next_start == end_charpos)))))
26628 /* A glyph that comes from DISP_STRING is by
26629 definition to be highlighted. */
26630 || EQ (g->object, disp_string))
26631 break;
26632 g++;
26633 }
26634 if (g == e)
26635 {
26636 *end = row;
26637 break;
26638 }
26639 /* The first row that ends at ZV must be the last to be
26640 highlighted. */
26641 else if (next->ends_at_zv_p)
26642 {
26643 *end = next;
26644 break;
26645 }
26646 }
26647 }
26648 }
26649
26650 /* This function sets the mouse_face_* elements of HLINFO, assuming
26651 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26652 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26653 for the overlay or run of text properties specifying the mouse
26654 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26655 before-string and after-string that must also be highlighted.
26656 DISP_STRING, if non-nil, is a display string that may cover some
26657 or all of the highlighted text. */
26658
26659 static void
26660 mouse_face_from_buffer_pos (Lisp_Object window,
26661 Mouse_HLInfo *hlinfo,
26662 ptrdiff_t mouse_charpos,
26663 ptrdiff_t start_charpos,
26664 ptrdiff_t end_charpos,
26665 Lisp_Object before_string,
26666 Lisp_Object after_string,
26667 Lisp_Object disp_string)
26668 {
26669 struct window *w = XWINDOW (window);
26670 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26671 struct glyph_row *r1, *r2;
26672 struct glyph *glyph, *end;
26673 ptrdiff_t ignore, pos;
26674 int x;
26675
26676 eassert (NILP (disp_string) || STRINGP (disp_string));
26677 eassert (NILP (before_string) || STRINGP (before_string));
26678 eassert (NILP (after_string) || STRINGP (after_string));
26679
26680 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26681 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26682 if (r1 == NULL)
26683 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26684 /* If the before-string or display-string contains newlines,
26685 rows_from_pos_range skips to its last row. Move back. */
26686 if (!NILP (before_string) || !NILP (disp_string))
26687 {
26688 struct glyph_row *prev;
26689 while ((prev = r1 - 1, prev >= first)
26690 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26691 && prev->used[TEXT_AREA] > 0)
26692 {
26693 struct glyph *beg = prev->glyphs[TEXT_AREA];
26694 glyph = beg + prev->used[TEXT_AREA];
26695 while (--glyph >= beg && INTEGERP (glyph->object));
26696 if (glyph < beg
26697 || !(EQ (glyph->object, before_string)
26698 || EQ (glyph->object, disp_string)))
26699 break;
26700 r1 = prev;
26701 }
26702 }
26703 if (r2 == NULL)
26704 {
26705 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26706 hlinfo->mouse_face_past_end = 1;
26707 }
26708 else if (!NILP (after_string))
26709 {
26710 /* If the after-string has newlines, advance to its last row. */
26711 struct glyph_row *next;
26712 struct glyph_row *last
26713 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26714
26715 for (next = r2 + 1;
26716 next <= last
26717 && next->used[TEXT_AREA] > 0
26718 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26719 ++next)
26720 r2 = next;
26721 }
26722 /* The rest of the display engine assumes that mouse_face_beg_row is
26723 either above mouse_face_end_row or identical to it. But with
26724 bidi-reordered continued lines, the row for START_CHARPOS could
26725 be below the row for END_CHARPOS. If so, swap the rows and store
26726 them in correct order. */
26727 if (r1->y > r2->y)
26728 {
26729 struct glyph_row *tem = r2;
26730
26731 r2 = r1;
26732 r1 = tem;
26733 }
26734
26735 hlinfo->mouse_face_beg_y = r1->y;
26736 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26737 hlinfo->mouse_face_end_y = r2->y;
26738 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26739
26740 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26741 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26742 could be anywhere in the row and in any order. The strategy
26743 below is to find the leftmost and the rightmost glyph that
26744 belongs to either of these 3 strings, or whose position is
26745 between START_CHARPOS and END_CHARPOS, and highlight all the
26746 glyphs between those two. This may cover more than just the text
26747 between START_CHARPOS and END_CHARPOS if the range of characters
26748 strides the bidi level boundary, e.g. if the beginning is in R2L
26749 text while the end is in L2R text or vice versa. */
26750 if (!r1->reversed_p)
26751 {
26752 /* This row is in a left to right paragraph. Scan it left to
26753 right. */
26754 glyph = r1->glyphs[TEXT_AREA];
26755 end = glyph + r1->used[TEXT_AREA];
26756 x = r1->x;
26757
26758 /* Skip truncation glyphs at the start of the glyph row. */
26759 if (r1->displays_text_p)
26760 for (; glyph < end
26761 && INTEGERP (glyph->object)
26762 && glyph->charpos < 0;
26763 ++glyph)
26764 x += glyph->pixel_width;
26765
26766 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26767 or DISP_STRING, and the first glyph from buffer whose
26768 position is between START_CHARPOS and END_CHARPOS. */
26769 for (; glyph < end
26770 && !INTEGERP (glyph->object)
26771 && !EQ (glyph->object, disp_string)
26772 && !(BUFFERP (glyph->object)
26773 && (glyph->charpos >= start_charpos
26774 && glyph->charpos < end_charpos));
26775 ++glyph)
26776 {
26777 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26778 are present at buffer positions between START_CHARPOS and
26779 END_CHARPOS, or if they come from an overlay. */
26780 if (EQ (glyph->object, before_string))
26781 {
26782 pos = string_buffer_position (before_string,
26783 start_charpos);
26784 /* If pos == 0, it means before_string came from an
26785 overlay, not from a buffer position. */
26786 if (!pos || (pos >= start_charpos && pos < end_charpos))
26787 break;
26788 }
26789 else if (EQ (glyph->object, after_string))
26790 {
26791 pos = string_buffer_position (after_string, end_charpos);
26792 if (!pos || (pos >= start_charpos && pos < end_charpos))
26793 break;
26794 }
26795 x += glyph->pixel_width;
26796 }
26797 hlinfo->mouse_face_beg_x = x;
26798 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26799 }
26800 else
26801 {
26802 /* This row is in a right to left paragraph. Scan it right to
26803 left. */
26804 struct glyph *g;
26805
26806 end = r1->glyphs[TEXT_AREA] - 1;
26807 glyph = end + r1->used[TEXT_AREA];
26808
26809 /* Skip truncation glyphs at the start of the glyph row. */
26810 if (r1->displays_text_p)
26811 for (; glyph > end
26812 && INTEGERP (glyph->object)
26813 && glyph->charpos < 0;
26814 --glyph)
26815 ;
26816
26817 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26818 or DISP_STRING, and the first glyph from buffer whose
26819 position is between START_CHARPOS and END_CHARPOS. */
26820 for (; glyph > end
26821 && !INTEGERP (glyph->object)
26822 && !EQ (glyph->object, disp_string)
26823 && !(BUFFERP (glyph->object)
26824 && (glyph->charpos >= start_charpos
26825 && glyph->charpos < end_charpos));
26826 --glyph)
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 (glyph->object, before_string))
26832 {
26833 pos = string_buffer_position (before_string, start_charpos);
26834 /* If pos == 0, it means before_string came from an
26835 overlay, not from a buffer position. */
26836 if (!pos || (pos >= start_charpos && pos < end_charpos))
26837 break;
26838 }
26839 else if (EQ (glyph->object, after_string))
26840 {
26841 pos = string_buffer_position (after_string, end_charpos);
26842 if (!pos || (pos >= start_charpos && pos < end_charpos))
26843 break;
26844 }
26845 }
26846
26847 glyph++; /* first glyph to the right of the highlighted area */
26848 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26849 x += g->pixel_width;
26850 hlinfo->mouse_face_beg_x = x;
26851 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26852 }
26853
26854 /* If the highlight ends in a different row, compute GLYPH and END
26855 for the end row. Otherwise, reuse the values computed above for
26856 the row where the highlight begins. */
26857 if (r2 != r1)
26858 {
26859 if (!r2->reversed_p)
26860 {
26861 glyph = r2->glyphs[TEXT_AREA];
26862 end = glyph + r2->used[TEXT_AREA];
26863 x = r2->x;
26864 }
26865 else
26866 {
26867 end = r2->glyphs[TEXT_AREA] - 1;
26868 glyph = end + r2->used[TEXT_AREA];
26869 }
26870 }
26871
26872 if (!r2->reversed_p)
26873 {
26874 /* Skip truncation and continuation glyphs near the end of the
26875 row, and also blanks and stretch glyphs inserted by
26876 extend_face_to_end_of_line. */
26877 while (end > glyph
26878 && INTEGERP ((end - 1)->object))
26879 --end;
26880 /* Scan the rest of the glyph row from the end, looking for the
26881 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26882 DISP_STRING, or whose position is between START_CHARPOS
26883 and END_CHARPOS */
26884 for (--end;
26885 end > glyph
26886 && !INTEGERP (end->object)
26887 && !EQ (end->object, disp_string)
26888 && !(BUFFERP (end->object)
26889 && (end->charpos >= start_charpos
26890 && end->charpos < end_charpos));
26891 --end)
26892 {
26893 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26894 are present at buffer positions between START_CHARPOS and
26895 END_CHARPOS, or if they come from an overlay. */
26896 if (EQ (end->object, before_string))
26897 {
26898 pos = string_buffer_position (before_string, start_charpos);
26899 if (!pos || (pos >= start_charpos && pos < end_charpos))
26900 break;
26901 }
26902 else if (EQ (end->object, after_string))
26903 {
26904 pos = string_buffer_position (after_string, end_charpos);
26905 if (!pos || (pos >= start_charpos && pos < end_charpos))
26906 break;
26907 }
26908 }
26909 /* Find the X coordinate of the last glyph to be highlighted. */
26910 for (; glyph <= end; ++glyph)
26911 x += glyph->pixel_width;
26912
26913 hlinfo->mouse_face_end_x = x;
26914 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26915 }
26916 else
26917 {
26918 /* Skip truncation and continuation glyphs near the end of the
26919 row, and also blanks and stretch glyphs inserted by
26920 extend_face_to_end_of_line. */
26921 x = r2->x;
26922 end++;
26923 while (end < glyph
26924 && INTEGERP (end->object))
26925 {
26926 x += end->pixel_width;
26927 ++end;
26928 }
26929 /* Scan the rest of the glyph row from the end, looking for the
26930 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26931 DISP_STRING, or whose position is between START_CHARPOS
26932 and END_CHARPOS */
26933 for ( ;
26934 end < glyph
26935 && !INTEGERP (end->object)
26936 && !EQ (end->object, disp_string)
26937 && !(BUFFERP (end->object)
26938 && (end->charpos >= start_charpos
26939 && end->charpos < end_charpos));
26940 ++end)
26941 {
26942 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26943 are present at buffer positions between START_CHARPOS and
26944 END_CHARPOS, or if they come from an overlay. */
26945 if (EQ (end->object, before_string))
26946 {
26947 pos = string_buffer_position (before_string, start_charpos);
26948 if (!pos || (pos >= start_charpos && pos < end_charpos))
26949 break;
26950 }
26951 else if (EQ (end->object, after_string))
26952 {
26953 pos = string_buffer_position (after_string, end_charpos);
26954 if (!pos || (pos >= start_charpos && pos < end_charpos))
26955 break;
26956 }
26957 x += end->pixel_width;
26958 }
26959 /* If we exited the above loop because we arrived at the last
26960 glyph of the row, and its buffer position is still not in
26961 range, it means the last character in range is the preceding
26962 newline. Bump the end column and x values to get past the
26963 last glyph. */
26964 if (end == glyph
26965 && BUFFERP (end->object)
26966 && (end->charpos < start_charpos
26967 || end->charpos >= end_charpos))
26968 {
26969 x += end->pixel_width;
26970 ++end;
26971 }
26972 hlinfo->mouse_face_end_x = x;
26973 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26974 }
26975
26976 hlinfo->mouse_face_window = window;
26977 hlinfo->mouse_face_face_id
26978 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26979 mouse_charpos + 1,
26980 !hlinfo->mouse_face_hidden, -1);
26981 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26982 }
26983
26984 /* The following function is not used anymore (replaced with
26985 mouse_face_from_string_pos), but I leave it here for the time
26986 being, in case someone would. */
26987
26988 #if 0 /* not used */
26989
26990 /* Find the position of the glyph for position POS in OBJECT in
26991 window W's current matrix, and return in *X, *Y the pixel
26992 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26993
26994 RIGHT_P non-zero means return the position of the right edge of the
26995 glyph, RIGHT_P zero means return the left edge position.
26996
26997 If no glyph for POS exists in the matrix, return the position of
26998 the glyph with the next smaller position that is in the matrix, if
26999 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27000 exists in the matrix, return the position of the glyph with the
27001 next larger position in OBJECT.
27002
27003 Value is non-zero if a glyph was found. */
27004
27005 static int
27006 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27007 int *hpos, int *vpos, int *x, int *y, int right_p)
27008 {
27009 int yb = window_text_bottom_y (w);
27010 struct glyph_row *r;
27011 struct glyph *best_glyph = NULL;
27012 struct glyph_row *best_row = NULL;
27013 int best_x = 0;
27014
27015 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27016 r->enabled_p && r->y < yb;
27017 ++r)
27018 {
27019 struct glyph *g = r->glyphs[TEXT_AREA];
27020 struct glyph *e = g + r->used[TEXT_AREA];
27021 int gx;
27022
27023 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27024 if (EQ (g->object, object))
27025 {
27026 if (g->charpos == pos)
27027 {
27028 best_glyph = g;
27029 best_x = gx;
27030 best_row = r;
27031 goto found;
27032 }
27033 else if (best_glyph == NULL
27034 || ((eabs (g->charpos - pos)
27035 < eabs (best_glyph->charpos - pos))
27036 && (right_p
27037 ? g->charpos < pos
27038 : g->charpos > pos)))
27039 {
27040 best_glyph = g;
27041 best_x = gx;
27042 best_row = r;
27043 }
27044 }
27045 }
27046
27047 found:
27048
27049 if (best_glyph)
27050 {
27051 *x = best_x;
27052 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27053
27054 if (right_p)
27055 {
27056 *x += best_glyph->pixel_width;
27057 ++*hpos;
27058 }
27059
27060 *y = best_row->y;
27061 *vpos = best_row - w->current_matrix->rows;
27062 }
27063
27064 return best_glyph != NULL;
27065 }
27066 #endif /* not used */
27067
27068 /* Find the positions of the first and the last glyphs in window W's
27069 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27070 (assumed to be a string), and return in HLINFO's mouse_face_*
27071 members the pixel and column/row coordinates of those glyphs. */
27072
27073 static void
27074 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27075 Lisp_Object object,
27076 ptrdiff_t startpos, ptrdiff_t endpos)
27077 {
27078 int yb = window_text_bottom_y (w);
27079 struct glyph_row *r;
27080 struct glyph *g, *e;
27081 int gx;
27082 int found = 0;
27083
27084 /* Find the glyph row with at least one position in the range
27085 [STARTPOS..ENDPOS], and the first glyph in that row whose
27086 position belongs to that range. */
27087 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27088 r->enabled_p && r->y < yb;
27089 ++r)
27090 {
27091 if (!r->reversed_p)
27092 {
27093 g = r->glyphs[TEXT_AREA];
27094 e = g + r->used[TEXT_AREA];
27095 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27096 if (EQ (g->object, object)
27097 && startpos <= g->charpos && g->charpos <= endpos)
27098 {
27099 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27100 hlinfo->mouse_face_beg_y = r->y;
27101 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27102 hlinfo->mouse_face_beg_x = gx;
27103 found = 1;
27104 break;
27105 }
27106 }
27107 else
27108 {
27109 struct glyph *g1;
27110
27111 e = r->glyphs[TEXT_AREA];
27112 g = e + r->used[TEXT_AREA];
27113 for ( ; g > e; --g)
27114 if (EQ ((g-1)->object, object)
27115 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27116 {
27117 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27118 hlinfo->mouse_face_beg_y = r->y;
27119 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27120 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27121 gx += g1->pixel_width;
27122 hlinfo->mouse_face_beg_x = gx;
27123 found = 1;
27124 break;
27125 }
27126 }
27127 if (found)
27128 break;
27129 }
27130
27131 if (!found)
27132 return;
27133
27134 /* Starting with the next row, look for the first row which does NOT
27135 include any glyphs whose positions are in the range. */
27136 for (++r; r->enabled_p && r->y < yb; ++r)
27137 {
27138 g = r->glyphs[TEXT_AREA];
27139 e = g + r->used[TEXT_AREA];
27140 found = 0;
27141 for ( ; g < e; ++g)
27142 if (EQ (g->object, object)
27143 && startpos <= g->charpos && g->charpos <= endpos)
27144 {
27145 found = 1;
27146 break;
27147 }
27148 if (!found)
27149 break;
27150 }
27151
27152 /* The highlighted region ends on the previous row. */
27153 r--;
27154
27155 /* Set the end row and its vertical pixel coordinate. */
27156 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27157 hlinfo->mouse_face_end_y = r->y;
27158
27159 /* Compute and set the end column and the end column's horizontal
27160 pixel coordinate. */
27161 if (!r->reversed_p)
27162 {
27163 g = r->glyphs[TEXT_AREA];
27164 e = g + r->used[TEXT_AREA];
27165 for ( ; e > g; --e)
27166 if (EQ ((e-1)->object, object)
27167 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27168 break;
27169 hlinfo->mouse_face_end_col = e - g;
27170
27171 for (gx = r->x; g < e; ++g)
27172 gx += g->pixel_width;
27173 hlinfo->mouse_face_end_x = gx;
27174 }
27175 else
27176 {
27177 e = r->glyphs[TEXT_AREA];
27178 g = e + r->used[TEXT_AREA];
27179 for (gx = r->x ; e < g; ++e)
27180 {
27181 if (EQ (e->object, object)
27182 && startpos <= e->charpos && e->charpos <= endpos)
27183 break;
27184 gx += e->pixel_width;
27185 }
27186 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27187 hlinfo->mouse_face_end_x = gx;
27188 }
27189 }
27190
27191 #ifdef HAVE_WINDOW_SYSTEM
27192
27193 /* See if position X, Y is within a hot-spot of an image. */
27194
27195 static int
27196 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27197 {
27198 if (!CONSP (hot_spot))
27199 return 0;
27200
27201 if (EQ (XCAR (hot_spot), Qrect))
27202 {
27203 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27204 Lisp_Object rect = XCDR (hot_spot);
27205 Lisp_Object tem;
27206 if (!CONSP (rect))
27207 return 0;
27208 if (!CONSP (XCAR (rect)))
27209 return 0;
27210 if (!CONSP (XCDR (rect)))
27211 return 0;
27212 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27213 return 0;
27214 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27215 return 0;
27216 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27217 return 0;
27218 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27219 return 0;
27220 return 1;
27221 }
27222 else if (EQ (XCAR (hot_spot), Qcircle))
27223 {
27224 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27225 Lisp_Object circ = XCDR (hot_spot);
27226 Lisp_Object lr, lx0, ly0;
27227 if (CONSP (circ)
27228 && CONSP (XCAR (circ))
27229 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27230 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27231 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27232 {
27233 double r = XFLOATINT (lr);
27234 double dx = XINT (lx0) - x;
27235 double dy = XINT (ly0) - y;
27236 return (dx * dx + dy * dy <= r * r);
27237 }
27238 }
27239 else if (EQ (XCAR (hot_spot), Qpoly))
27240 {
27241 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27242 if (VECTORP (XCDR (hot_spot)))
27243 {
27244 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27245 Lisp_Object *poly = v->contents;
27246 ptrdiff_t n = v->header.size;
27247 ptrdiff_t i;
27248 int inside = 0;
27249 Lisp_Object lx, ly;
27250 int x0, y0;
27251
27252 /* Need an even number of coordinates, and at least 3 edges. */
27253 if (n < 6 || n & 1)
27254 return 0;
27255
27256 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27257 If count is odd, we are inside polygon. Pixels on edges
27258 may or may not be included depending on actual geometry of the
27259 polygon. */
27260 if ((lx = poly[n-2], !INTEGERP (lx))
27261 || (ly = poly[n-1], !INTEGERP (lx)))
27262 return 0;
27263 x0 = XINT (lx), y0 = XINT (ly);
27264 for (i = 0; i < n; i += 2)
27265 {
27266 int x1 = x0, y1 = y0;
27267 if ((lx = poly[i], !INTEGERP (lx))
27268 || (ly = poly[i+1], !INTEGERP (ly)))
27269 return 0;
27270 x0 = XINT (lx), y0 = XINT (ly);
27271
27272 /* Does this segment cross the X line? */
27273 if (x0 >= x)
27274 {
27275 if (x1 >= x)
27276 continue;
27277 }
27278 else if (x1 < x)
27279 continue;
27280 if (y > y0 && y > y1)
27281 continue;
27282 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27283 inside = !inside;
27284 }
27285 return inside;
27286 }
27287 }
27288 return 0;
27289 }
27290
27291 Lisp_Object
27292 find_hot_spot (Lisp_Object map, int x, int y)
27293 {
27294 while (CONSP (map))
27295 {
27296 if (CONSP (XCAR (map))
27297 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27298 return XCAR (map);
27299 map = XCDR (map);
27300 }
27301
27302 return Qnil;
27303 }
27304
27305 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27306 3, 3, 0,
27307 doc: /* Lookup in image map MAP coordinates X and Y.
27308 An image map is an alist where each element has the format (AREA ID PLIST).
27309 An AREA is specified as either a rectangle, a circle, or a polygon:
27310 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27311 pixel coordinates of the upper left and bottom right corners.
27312 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27313 and the radius of the circle; r may be a float or integer.
27314 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27315 vector describes one corner in the polygon.
27316 Returns the alist element for the first matching AREA in MAP. */)
27317 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27318 {
27319 if (NILP (map))
27320 return Qnil;
27321
27322 CHECK_NUMBER (x);
27323 CHECK_NUMBER (y);
27324
27325 return find_hot_spot (map,
27326 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27327 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27328 }
27329
27330
27331 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27332 static void
27333 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27334 {
27335 /* Do not change cursor shape while dragging mouse. */
27336 if (!NILP (do_mouse_tracking))
27337 return;
27338
27339 if (!NILP (pointer))
27340 {
27341 if (EQ (pointer, Qarrow))
27342 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27343 else if (EQ (pointer, Qhand))
27344 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27345 else if (EQ (pointer, Qtext))
27346 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27347 else if (EQ (pointer, intern ("hdrag")))
27348 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27349 #ifdef HAVE_X_WINDOWS
27350 else if (EQ (pointer, intern ("vdrag")))
27351 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27352 #endif
27353 else if (EQ (pointer, intern ("hourglass")))
27354 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27355 else if (EQ (pointer, Qmodeline))
27356 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27357 else
27358 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27359 }
27360
27361 if (cursor != No_Cursor)
27362 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27363 }
27364
27365 #endif /* HAVE_WINDOW_SYSTEM */
27366
27367 /* Take proper action when mouse has moved to the mode or header line
27368 or marginal area AREA of window W, x-position X and y-position Y.
27369 X is relative to the start of the text display area of W, so the
27370 width of bitmap areas and scroll bars must be subtracted to get a
27371 position relative to the start of the mode line. */
27372
27373 static void
27374 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27375 enum window_part area)
27376 {
27377 struct window *w = XWINDOW (window);
27378 struct frame *f = XFRAME (w->frame);
27379 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27380 #ifdef HAVE_WINDOW_SYSTEM
27381 Display_Info *dpyinfo;
27382 #endif
27383 Cursor cursor = No_Cursor;
27384 Lisp_Object pointer = Qnil;
27385 int dx, dy, width, height;
27386 ptrdiff_t charpos;
27387 Lisp_Object string, object = Qnil;
27388 Lisp_Object pos IF_LINT (= Qnil), help;
27389
27390 Lisp_Object mouse_face;
27391 int original_x_pixel = x;
27392 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27393 struct glyph_row *row IF_LINT (= 0);
27394
27395 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27396 {
27397 int x0;
27398 struct glyph *end;
27399
27400 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27401 returns them in row/column units! */
27402 string = mode_line_string (w, area, &x, &y, &charpos,
27403 &object, &dx, &dy, &width, &height);
27404
27405 row = (area == ON_MODE_LINE
27406 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27407 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27408
27409 /* Find the glyph under the mouse pointer. */
27410 if (row->mode_line_p && row->enabled_p)
27411 {
27412 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27413 end = glyph + row->used[TEXT_AREA];
27414
27415 for (x0 = original_x_pixel;
27416 glyph < end && x0 >= glyph->pixel_width;
27417 ++glyph)
27418 x0 -= glyph->pixel_width;
27419
27420 if (glyph >= end)
27421 glyph = NULL;
27422 }
27423 }
27424 else
27425 {
27426 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27427 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27428 returns them in row/column units! */
27429 string = marginal_area_string (w, area, &x, &y, &charpos,
27430 &object, &dx, &dy, &width, &height);
27431 }
27432
27433 help = Qnil;
27434
27435 #ifdef HAVE_WINDOW_SYSTEM
27436 if (IMAGEP (object))
27437 {
27438 Lisp_Object image_map, hotspot;
27439 if ((image_map = Fplist_get (XCDR (object), QCmap),
27440 !NILP (image_map))
27441 && (hotspot = find_hot_spot (image_map, dx, dy),
27442 CONSP (hotspot))
27443 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27444 {
27445 Lisp_Object plist;
27446
27447 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27448 If so, we could look for mouse-enter, mouse-leave
27449 properties in PLIST (and do something...). */
27450 hotspot = XCDR (hotspot);
27451 if (CONSP (hotspot)
27452 && (plist = XCAR (hotspot), CONSP (plist)))
27453 {
27454 pointer = Fplist_get (plist, Qpointer);
27455 if (NILP (pointer))
27456 pointer = Qhand;
27457 help = Fplist_get (plist, Qhelp_echo);
27458 if (!NILP (help))
27459 {
27460 help_echo_string = help;
27461 XSETWINDOW (help_echo_window, w);
27462 help_echo_object = w->buffer;
27463 help_echo_pos = charpos;
27464 }
27465 }
27466 }
27467 if (NILP (pointer))
27468 pointer = Fplist_get (XCDR (object), QCpointer);
27469 }
27470 #endif /* HAVE_WINDOW_SYSTEM */
27471
27472 if (STRINGP (string))
27473 pos = make_number (charpos);
27474
27475 /* Set the help text and mouse pointer. If the mouse is on a part
27476 of the mode line without any text (e.g. past the right edge of
27477 the mode line text), use the default help text and pointer. */
27478 if (STRINGP (string) || area == ON_MODE_LINE)
27479 {
27480 /* Arrange to display the help by setting the global variables
27481 help_echo_string, help_echo_object, and help_echo_pos. */
27482 if (NILP (help))
27483 {
27484 if (STRINGP (string))
27485 help = Fget_text_property (pos, Qhelp_echo, string);
27486
27487 if (!NILP (help))
27488 {
27489 help_echo_string = help;
27490 XSETWINDOW (help_echo_window, w);
27491 help_echo_object = string;
27492 help_echo_pos = charpos;
27493 }
27494 else if (area == ON_MODE_LINE)
27495 {
27496 Lisp_Object default_help
27497 = buffer_local_value_1 (Qmode_line_default_help_echo,
27498 w->buffer);
27499
27500 if (STRINGP (default_help))
27501 {
27502 help_echo_string = default_help;
27503 XSETWINDOW (help_echo_window, w);
27504 help_echo_object = Qnil;
27505 help_echo_pos = -1;
27506 }
27507 }
27508 }
27509
27510 #ifdef HAVE_WINDOW_SYSTEM
27511 /* Change the mouse pointer according to what is under it. */
27512 if (FRAME_WINDOW_P (f))
27513 {
27514 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27515 if (STRINGP (string))
27516 {
27517 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27518
27519 if (NILP (pointer))
27520 pointer = Fget_text_property (pos, Qpointer, string);
27521
27522 /* Change the mouse pointer according to what is under X/Y. */
27523 if (NILP (pointer)
27524 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27525 {
27526 Lisp_Object map;
27527 map = Fget_text_property (pos, Qlocal_map, string);
27528 if (!KEYMAPP (map))
27529 map = Fget_text_property (pos, Qkeymap, string);
27530 if (!KEYMAPP (map))
27531 cursor = dpyinfo->vertical_scroll_bar_cursor;
27532 }
27533 }
27534 else
27535 /* Default mode-line pointer. */
27536 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27537 }
27538 #endif
27539 }
27540
27541 /* Change the mouse face according to what is under X/Y. */
27542 if (STRINGP (string))
27543 {
27544 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27545 if (!NILP (mouse_face)
27546 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27547 && glyph)
27548 {
27549 Lisp_Object b, e;
27550
27551 struct glyph * tmp_glyph;
27552
27553 int gpos;
27554 int gseq_length;
27555 int total_pixel_width;
27556 ptrdiff_t begpos, endpos, ignore;
27557
27558 int vpos, hpos;
27559
27560 b = Fprevious_single_property_change (make_number (charpos + 1),
27561 Qmouse_face, string, Qnil);
27562 if (NILP (b))
27563 begpos = 0;
27564 else
27565 begpos = XINT (b);
27566
27567 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27568 if (NILP (e))
27569 endpos = SCHARS (string);
27570 else
27571 endpos = XINT (e);
27572
27573 /* Calculate the glyph position GPOS of GLYPH in the
27574 displayed string, relative to the beginning of the
27575 highlighted part of the string.
27576
27577 Note: GPOS is different from CHARPOS. CHARPOS is the
27578 position of GLYPH in the internal string object. A mode
27579 line string format has structures which are converted to
27580 a flattened string by the Emacs Lisp interpreter. The
27581 internal string is an element of those structures. The
27582 displayed string is the flattened string. */
27583 tmp_glyph = row_start_glyph;
27584 while (tmp_glyph < glyph
27585 && (!(EQ (tmp_glyph->object, glyph->object)
27586 && begpos <= tmp_glyph->charpos
27587 && tmp_glyph->charpos < endpos)))
27588 tmp_glyph++;
27589 gpos = glyph - tmp_glyph;
27590
27591 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27592 the highlighted part of the displayed string to which
27593 GLYPH belongs. Note: GSEQ_LENGTH is different from
27594 SCHARS (STRING), because the latter returns the length of
27595 the internal string. */
27596 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27597 tmp_glyph > glyph
27598 && (!(EQ (tmp_glyph->object, glyph->object)
27599 && begpos <= tmp_glyph->charpos
27600 && tmp_glyph->charpos < endpos));
27601 tmp_glyph--)
27602 ;
27603 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27604
27605 /* Calculate the total pixel width of all the glyphs between
27606 the beginning of the highlighted area and GLYPH. */
27607 total_pixel_width = 0;
27608 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27609 total_pixel_width += tmp_glyph->pixel_width;
27610
27611 /* Pre calculation of re-rendering position. Note: X is in
27612 column units here, after the call to mode_line_string or
27613 marginal_area_string. */
27614 hpos = x - gpos;
27615 vpos = (area == ON_MODE_LINE
27616 ? (w->current_matrix)->nrows - 1
27617 : 0);
27618
27619 /* If GLYPH's position is included in the region that is
27620 already drawn in mouse face, we have nothing to do. */
27621 if ( EQ (window, hlinfo->mouse_face_window)
27622 && (!row->reversed_p
27623 ? (hlinfo->mouse_face_beg_col <= hpos
27624 && hpos < hlinfo->mouse_face_end_col)
27625 /* In R2L rows we swap BEG and END, see below. */
27626 : (hlinfo->mouse_face_end_col <= hpos
27627 && hpos < hlinfo->mouse_face_beg_col))
27628 && hlinfo->mouse_face_beg_row == vpos )
27629 return;
27630
27631 if (clear_mouse_face (hlinfo))
27632 cursor = No_Cursor;
27633
27634 if (!row->reversed_p)
27635 {
27636 hlinfo->mouse_face_beg_col = hpos;
27637 hlinfo->mouse_face_beg_x = original_x_pixel
27638 - (total_pixel_width + dx);
27639 hlinfo->mouse_face_end_col = hpos + gseq_length;
27640 hlinfo->mouse_face_end_x = 0;
27641 }
27642 else
27643 {
27644 /* In R2L rows, show_mouse_face expects BEG and END
27645 coordinates to be swapped. */
27646 hlinfo->mouse_face_end_col = hpos;
27647 hlinfo->mouse_face_end_x = original_x_pixel
27648 - (total_pixel_width + dx);
27649 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27650 hlinfo->mouse_face_beg_x = 0;
27651 }
27652
27653 hlinfo->mouse_face_beg_row = vpos;
27654 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27655 hlinfo->mouse_face_beg_y = 0;
27656 hlinfo->mouse_face_end_y = 0;
27657 hlinfo->mouse_face_past_end = 0;
27658 hlinfo->mouse_face_window = window;
27659
27660 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27661 charpos,
27662 0, 0, 0,
27663 &ignore,
27664 glyph->face_id,
27665 1);
27666 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27667
27668 if (NILP (pointer))
27669 pointer = Qhand;
27670 }
27671 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27672 clear_mouse_face (hlinfo);
27673 }
27674 #ifdef HAVE_WINDOW_SYSTEM
27675 if (FRAME_WINDOW_P (f))
27676 define_frame_cursor1 (f, cursor, pointer);
27677 #endif
27678 }
27679
27680
27681 /* EXPORT:
27682 Take proper action when the mouse has moved to position X, Y on
27683 frame F as regards highlighting characters that have mouse-face
27684 properties. Also de-highlighting chars where the mouse was before.
27685 X and Y can be negative or out of range. */
27686
27687 void
27688 note_mouse_highlight (struct frame *f, int x, int y)
27689 {
27690 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27691 enum window_part part = ON_NOTHING;
27692 Lisp_Object window;
27693 struct window *w;
27694 Cursor cursor = No_Cursor;
27695 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27696 struct buffer *b;
27697
27698 /* When a menu is active, don't highlight because this looks odd. */
27699 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27700 if (popup_activated ())
27701 return;
27702 #endif
27703
27704 if (NILP (Vmouse_highlight)
27705 || !f->glyphs_initialized_p
27706 || f->pointer_invisible)
27707 return;
27708
27709 hlinfo->mouse_face_mouse_x = x;
27710 hlinfo->mouse_face_mouse_y = y;
27711 hlinfo->mouse_face_mouse_frame = f;
27712
27713 if (hlinfo->mouse_face_defer)
27714 return;
27715
27716 /* Which window is that in? */
27717 window = window_from_coordinates (f, x, y, &part, 1);
27718
27719 /* If displaying active text in another window, clear that. */
27720 if (! EQ (window, hlinfo->mouse_face_window)
27721 /* Also clear if we move out of text area in same window. */
27722 || (!NILP (hlinfo->mouse_face_window)
27723 && !NILP (window)
27724 && part != ON_TEXT
27725 && part != ON_MODE_LINE
27726 && part != ON_HEADER_LINE))
27727 clear_mouse_face (hlinfo);
27728
27729 /* Not on a window -> return. */
27730 if (!WINDOWP (window))
27731 return;
27732
27733 /* Reset help_echo_string. It will get recomputed below. */
27734 help_echo_string = Qnil;
27735
27736 /* Convert to window-relative pixel coordinates. */
27737 w = XWINDOW (window);
27738 frame_to_window_pixel_xy (w, &x, &y);
27739
27740 #ifdef HAVE_WINDOW_SYSTEM
27741 /* Handle tool-bar window differently since it doesn't display a
27742 buffer. */
27743 if (EQ (window, f->tool_bar_window))
27744 {
27745 note_tool_bar_highlight (f, x, y);
27746 return;
27747 }
27748 #endif
27749
27750 /* Mouse is on the mode, header line or margin? */
27751 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27752 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27753 {
27754 note_mode_line_or_margin_highlight (window, x, y, part);
27755 return;
27756 }
27757
27758 #ifdef HAVE_WINDOW_SYSTEM
27759 if (part == ON_VERTICAL_BORDER)
27760 {
27761 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27762 help_echo_string = build_string ("drag-mouse-1: resize");
27763 }
27764 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27765 || part == ON_SCROLL_BAR)
27766 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27767 else
27768 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27769 #endif
27770
27771 /* Are we in a window whose display is up to date?
27772 And verify the buffer's text has not changed. */
27773 b = XBUFFER (w->buffer);
27774 if (part == ON_TEXT
27775 && EQ (w->window_end_valid, w->buffer)
27776 && w->last_modified == BUF_MODIFF (b)
27777 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27778 {
27779 int hpos, vpos, dx, dy, area = LAST_AREA;
27780 ptrdiff_t pos;
27781 struct glyph *glyph;
27782 Lisp_Object object;
27783 Lisp_Object mouse_face = Qnil, position;
27784 Lisp_Object *overlay_vec = NULL;
27785 ptrdiff_t i, noverlays;
27786 struct buffer *obuf;
27787 ptrdiff_t obegv, ozv;
27788 int same_region;
27789
27790 /* Find the glyph under X/Y. */
27791 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27792
27793 #ifdef HAVE_WINDOW_SYSTEM
27794 /* Look for :pointer property on image. */
27795 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27796 {
27797 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27798 if (img != NULL && IMAGEP (img->spec))
27799 {
27800 Lisp_Object image_map, hotspot;
27801 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27802 !NILP (image_map))
27803 && (hotspot = find_hot_spot (image_map,
27804 glyph->slice.img.x + dx,
27805 glyph->slice.img.y + dy),
27806 CONSP (hotspot))
27807 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27808 {
27809 Lisp_Object plist;
27810
27811 /* Could check XCAR (hotspot) to see if we enter/leave
27812 this hot-spot.
27813 If so, we could look for mouse-enter, mouse-leave
27814 properties in PLIST (and do something...). */
27815 hotspot = XCDR (hotspot);
27816 if (CONSP (hotspot)
27817 && (plist = XCAR (hotspot), CONSP (plist)))
27818 {
27819 pointer = Fplist_get (plist, Qpointer);
27820 if (NILP (pointer))
27821 pointer = Qhand;
27822 help_echo_string = Fplist_get (plist, Qhelp_echo);
27823 if (!NILP (help_echo_string))
27824 {
27825 help_echo_window = window;
27826 help_echo_object = glyph->object;
27827 help_echo_pos = glyph->charpos;
27828 }
27829 }
27830 }
27831 if (NILP (pointer))
27832 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27833 }
27834 }
27835 #endif /* HAVE_WINDOW_SYSTEM */
27836
27837 /* Clear mouse face if X/Y not over text. */
27838 if (glyph == NULL
27839 || area != TEXT_AREA
27840 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27841 /* Glyph's OBJECT is an integer for glyphs inserted by the
27842 display engine for its internal purposes, like truncation
27843 and continuation glyphs and blanks beyond the end of
27844 line's text on text terminals. If we are over such a
27845 glyph, we are not over any text. */
27846 || INTEGERP (glyph->object)
27847 /* R2L rows have a stretch glyph at their front, which
27848 stands for no text, whereas L2R rows have no glyphs at
27849 all beyond the end of text. Treat such stretch glyphs
27850 like we do with NULL glyphs in L2R rows. */
27851 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27852 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27853 && glyph->type == STRETCH_GLYPH
27854 && glyph->avoid_cursor_p))
27855 {
27856 if (clear_mouse_face (hlinfo))
27857 cursor = No_Cursor;
27858 #ifdef HAVE_WINDOW_SYSTEM
27859 if (FRAME_WINDOW_P (f) && NILP (pointer))
27860 {
27861 if (area != TEXT_AREA)
27862 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27863 else
27864 pointer = Vvoid_text_area_pointer;
27865 }
27866 #endif
27867 goto set_cursor;
27868 }
27869
27870 pos = glyph->charpos;
27871 object = glyph->object;
27872 if (!STRINGP (object) && !BUFFERP (object))
27873 goto set_cursor;
27874
27875 /* If we get an out-of-range value, return now; avoid an error. */
27876 if (BUFFERP (object) && pos > BUF_Z (b))
27877 goto set_cursor;
27878
27879 /* Make the window's buffer temporarily current for
27880 overlays_at and compute_char_face. */
27881 obuf = current_buffer;
27882 current_buffer = b;
27883 obegv = BEGV;
27884 ozv = ZV;
27885 BEGV = BEG;
27886 ZV = Z;
27887
27888 /* Is this char mouse-active or does it have help-echo? */
27889 position = make_number (pos);
27890
27891 if (BUFFERP (object))
27892 {
27893 /* Put all the overlays we want in a vector in overlay_vec. */
27894 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27895 /* Sort overlays into increasing priority order. */
27896 noverlays = sort_overlays (overlay_vec, noverlays, w);
27897 }
27898 else
27899 noverlays = 0;
27900
27901 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27902
27903 if (same_region)
27904 cursor = No_Cursor;
27905
27906 /* Check mouse-face highlighting. */
27907 if (! same_region
27908 /* If there exists an overlay with mouse-face overlapping
27909 the one we are currently highlighting, we have to
27910 check if we enter the overlapping overlay, and then
27911 highlight only that. */
27912 || (OVERLAYP (hlinfo->mouse_face_overlay)
27913 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27914 {
27915 /* Find the highest priority overlay with a mouse-face. */
27916 Lisp_Object overlay = Qnil;
27917 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27918 {
27919 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27920 if (!NILP (mouse_face))
27921 overlay = overlay_vec[i];
27922 }
27923
27924 /* If we're highlighting the same overlay as before, there's
27925 no need to do that again. */
27926 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27927 goto check_help_echo;
27928 hlinfo->mouse_face_overlay = overlay;
27929
27930 /* Clear the display of the old active region, if any. */
27931 if (clear_mouse_face (hlinfo))
27932 cursor = No_Cursor;
27933
27934 /* If no overlay applies, get a text property. */
27935 if (NILP (overlay))
27936 mouse_face = Fget_text_property (position, Qmouse_face, object);
27937
27938 /* Next, compute the bounds of the mouse highlighting and
27939 display it. */
27940 if (!NILP (mouse_face) && STRINGP (object))
27941 {
27942 /* The mouse-highlighting comes from a display string
27943 with a mouse-face. */
27944 Lisp_Object s, e;
27945 ptrdiff_t ignore;
27946
27947 s = Fprevious_single_property_change
27948 (make_number (pos + 1), Qmouse_face, object, Qnil);
27949 e = Fnext_single_property_change
27950 (position, Qmouse_face, object, Qnil);
27951 if (NILP (s))
27952 s = make_number (0);
27953 if (NILP (e))
27954 e = make_number (SCHARS (object) - 1);
27955 mouse_face_from_string_pos (w, hlinfo, object,
27956 XINT (s), XINT (e));
27957 hlinfo->mouse_face_past_end = 0;
27958 hlinfo->mouse_face_window = window;
27959 hlinfo->mouse_face_face_id
27960 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27961 glyph->face_id, 1);
27962 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27963 cursor = No_Cursor;
27964 }
27965 else
27966 {
27967 /* The mouse-highlighting, if any, comes from an overlay
27968 or text property in the buffer. */
27969 Lisp_Object buffer IF_LINT (= Qnil);
27970 Lisp_Object disp_string IF_LINT (= Qnil);
27971
27972 if (STRINGP (object))
27973 {
27974 /* If we are on a display string with no mouse-face,
27975 check if the text under it has one. */
27976 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27977 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27978 pos = string_buffer_position (object, start);
27979 if (pos > 0)
27980 {
27981 mouse_face = get_char_property_and_overlay
27982 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27983 buffer = w->buffer;
27984 disp_string = object;
27985 }
27986 }
27987 else
27988 {
27989 buffer = object;
27990 disp_string = Qnil;
27991 }
27992
27993 if (!NILP (mouse_face))
27994 {
27995 Lisp_Object before, after;
27996 Lisp_Object before_string, after_string;
27997 /* To correctly find the limits of mouse highlight
27998 in a bidi-reordered buffer, we must not use the
27999 optimization of limiting the search in
28000 previous-single-property-change and
28001 next-single-property-change, because
28002 rows_from_pos_range needs the real start and end
28003 positions to DTRT in this case. That's because
28004 the first row visible in a window does not
28005 necessarily display the character whose position
28006 is the smallest. */
28007 Lisp_Object lim1 =
28008 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28009 ? Fmarker_position (w->start)
28010 : Qnil;
28011 Lisp_Object lim2 =
28012 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28013 ? make_number (BUF_Z (XBUFFER (buffer))
28014 - XFASTINT (w->window_end_pos))
28015 : Qnil;
28016
28017 if (NILP (overlay))
28018 {
28019 /* Handle the text property case. */
28020 before = Fprevious_single_property_change
28021 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28022 after = Fnext_single_property_change
28023 (make_number (pos), Qmouse_face, buffer, lim2);
28024 before_string = after_string = Qnil;
28025 }
28026 else
28027 {
28028 /* Handle the overlay case. */
28029 before = Foverlay_start (overlay);
28030 after = Foverlay_end (overlay);
28031 before_string = Foverlay_get (overlay, Qbefore_string);
28032 after_string = Foverlay_get (overlay, Qafter_string);
28033
28034 if (!STRINGP (before_string)) before_string = Qnil;
28035 if (!STRINGP (after_string)) after_string = Qnil;
28036 }
28037
28038 mouse_face_from_buffer_pos (window, hlinfo, pos,
28039 NILP (before)
28040 ? 1
28041 : XFASTINT (before),
28042 NILP (after)
28043 ? BUF_Z (XBUFFER (buffer))
28044 : XFASTINT (after),
28045 before_string, after_string,
28046 disp_string);
28047 cursor = No_Cursor;
28048 }
28049 }
28050 }
28051
28052 check_help_echo:
28053
28054 /* Look for a `help-echo' property. */
28055 if (NILP (help_echo_string)) {
28056 Lisp_Object help, overlay;
28057
28058 /* Check overlays first. */
28059 help = overlay = Qnil;
28060 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28061 {
28062 overlay = overlay_vec[i];
28063 help = Foverlay_get (overlay, Qhelp_echo);
28064 }
28065
28066 if (!NILP (help))
28067 {
28068 help_echo_string = help;
28069 help_echo_window = window;
28070 help_echo_object = overlay;
28071 help_echo_pos = pos;
28072 }
28073 else
28074 {
28075 Lisp_Object obj = glyph->object;
28076 ptrdiff_t charpos = glyph->charpos;
28077
28078 /* Try text properties. */
28079 if (STRINGP (obj)
28080 && charpos >= 0
28081 && charpos < SCHARS (obj))
28082 {
28083 help = Fget_text_property (make_number (charpos),
28084 Qhelp_echo, obj);
28085 if (NILP (help))
28086 {
28087 /* If the string itself doesn't specify a help-echo,
28088 see if the buffer text ``under'' it does. */
28089 struct glyph_row *r
28090 = MATRIX_ROW (w->current_matrix, vpos);
28091 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28092 ptrdiff_t p = string_buffer_position (obj, start);
28093 if (p > 0)
28094 {
28095 help = Fget_char_property (make_number (p),
28096 Qhelp_echo, w->buffer);
28097 if (!NILP (help))
28098 {
28099 charpos = p;
28100 obj = w->buffer;
28101 }
28102 }
28103 }
28104 }
28105 else if (BUFFERP (obj)
28106 && charpos >= BEGV
28107 && charpos < ZV)
28108 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28109 obj);
28110
28111 if (!NILP (help))
28112 {
28113 help_echo_string = help;
28114 help_echo_window = window;
28115 help_echo_object = obj;
28116 help_echo_pos = charpos;
28117 }
28118 }
28119 }
28120
28121 #ifdef HAVE_WINDOW_SYSTEM
28122 /* Look for a `pointer' property. */
28123 if (FRAME_WINDOW_P (f) && NILP (pointer))
28124 {
28125 /* Check overlays first. */
28126 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28127 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28128
28129 if (NILP (pointer))
28130 {
28131 Lisp_Object obj = glyph->object;
28132 ptrdiff_t charpos = glyph->charpos;
28133
28134 /* Try text properties. */
28135 if (STRINGP (obj)
28136 && charpos >= 0
28137 && charpos < SCHARS (obj))
28138 {
28139 pointer = Fget_text_property (make_number (charpos),
28140 Qpointer, obj);
28141 if (NILP (pointer))
28142 {
28143 /* If the string itself doesn't specify a pointer,
28144 see if the buffer text ``under'' it does. */
28145 struct glyph_row *r
28146 = MATRIX_ROW (w->current_matrix, vpos);
28147 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28148 ptrdiff_t p = string_buffer_position (obj, start);
28149 if (p > 0)
28150 pointer = Fget_char_property (make_number (p),
28151 Qpointer, w->buffer);
28152 }
28153 }
28154 else if (BUFFERP (obj)
28155 && charpos >= BEGV
28156 && charpos < ZV)
28157 pointer = Fget_text_property (make_number (charpos),
28158 Qpointer, obj);
28159 }
28160 }
28161 #endif /* HAVE_WINDOW_SYSTEM */
28162
28163 BEGV = obegv;
28164 ZV = ozv;
28165 current_buffer = obuf;
28166 }
28167
28168 set_cursor:
28169
28170 #ifdef HAVE_WINDOW_SYSTEM
28171 if (FRAME_WINDOW_P (f))
28172 define_frame_cursor1 (f, cursor, pointer);
28173 #else
28174 /* This is here to prevent a compiler error, about "label at end of
28175 compound statement". */
28176 return;
28177 #endif
28178 }
28179
28180
28181 /* EXPORT for RIF:
28182 Clear any mouse-face on window W. This function is part of the
28183 redisplay interface, and is called from try_window_id and similar
28184 functions to ensure the mouse-highlight is off. */
28185
28186 void
28187 x_clear_window_mouse_face (struct window *w)
28188 {
28189 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28190 Lisp_Object window;
28191
28192 block_input ();
28193 XSETWINDOW (window, w);
28194 if (EQ (window, hlinfo->mouse_face_window))
28195 clear_mouse_face (hlinfo);
28196 unblock_input ();
28197 }
28198
28199
28200 /* EXPORT:
28201 Just discard the mouse face information for frame F, if any.
28202 This is used when the size of F is changed. */
28203
28204 void
28205 cancel_mouse_face (struct frame *f)
28206 {
28207 Lisp_Object window;
28208 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28209
28210 window = hlinfo->mouse_face_window;
28211 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28212 {
28213 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28214 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28215 hlinfo->mouse_face_window = Qnil;
28216 }
28217 }
28218
28219
28220 \f
28221 /***********************************************************************
28222 Exposure Events
28223 ***********************************************************************/
28224
28225 #ifdef HAVE_WINDOW_SYSTEM
28226
28227 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28228 which intersects rectangle R. R is in window-relative coordinates. */
28229
28230 static void
28231 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28232 enum glyph_row_area area)
28233 {
28234 struct glyph *first = row->glyphs[area];
28235 struct glyph *end = row->glyphs[area] + row->used[area];
28236 struct glyph *last;
28237 int first_x, start_x, x;
28238
28239 if (area == TEXT_AREA && row->fill_line_p)
28240 /* If row extends face to end of line write the whole line. */
28241 draw_glyphs (w, 0, row, area,
28242 0, row->used[area],
28243 DRAW_NORMAL_TEXT, 0);
28244 else
28245 {
28246 /* Set START_X to the window-relative start position for drawing glyphs of
28247 AREA. The first glyph of the text area can be partially visible.
28248 The first glyphs of other areas cannot. */
28249 start_x = window_box_left_offset (w, area);
28250 x = start_x;
28251 if (area == TEXT_AREA)
28252 x += row->x;
28253
28254 /* Find the first glyph that must be redrawn. */
28255 while (first < end
28256 && x + first->pixel_width < r->x)
28257 {
28258 x += first->pixel_width;
28259 ++first;
28260 }
28261
28262 /* Find the last one. */
28263 last = first;
28264 first_x = x;
28265 while (last < end
28266 && x < r->x + r->width)
28267 {
28268 x += last->pixel_width;
28269 ++last;
28270 }
28271
28272 /* Repaint. */
28273 if (last > first)
28274 draw_glyphs (w, first_x - start_x, row, area,
28275 first - row->glyphs[area], last - row->glyphs[area],
28276 DRAW_NORMAL_TEXT, 0);
28277 }
28278 }
28279
28280
28281 /* Redraw the parts of the glyph row ROW on window W intersecting
28282 rectangle R. R is in window-relative coordinates. Value is
28283 non-zero if mouse-face was overwritten. */
28284
28285 static int
28286 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28287 {
28288 eassert (row->enabled_p);
28289
28290 if (row->mode_line_p || w->pseudo_window_p)
28291 draw_glyphs (w, 0, row, TEXT_AREA,
28292 0, row->used[TEXT_AREA],
28293 DRAW_NORMAL_TEXT, 0);
28294 else
28295 {
28296 if (row->used[LEFT_MARGIN_AREA])
28297 expose_area (w, row, r, LEFT_MARGIN_AREA);
28298 if (row->used[TEXT_AREA])
28299 expose_area (w, row, r, TEXT_AREA);
28300 if (row->used[RIGHT_MARGIN_AREA])
28301 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28302 draw_row_fringe_bitmaps (w, row);
28303 }
28304
28305 return row->mouse_face_p;
28306 }
28307
28308
28309 /* Redraw those parts of glyphs rows during expose event handling that
28310 overlap other rows. Redrawing of an exposed line writes over parts
28311 of lines overlapping that exposed line; this function fixes that.
28312
28313 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28314 row in W's current matrix that is exposed and overlaps other rows.
28315 LAST_OVERLAPPING_ROW is the last such row. */
28316
28317 static void
28318 expose_overlaps (struct window *w,
28319 struct glyph_row *first_overlapping_row,
28320 struct glyph_row *last_overlapping_row,
28321 XRectangle *r)
28322 {
28323 struct glyph_row *row;
28324
28325 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28326 if (row->overlapping_p)
28327 {
28328 eassert (row->enabled_p && !row->mode_line_p);
28329
28330 row->clip = r;
28331 if (row->used[LEFT_MARGIN_AREA])
28332 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28333
28334 if (row->used[TEXT_AREA])
28335 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28336
28337 if (row->used[RIGHT_MARGIN_AREA])
28338 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28339 row->clip = NULL;
28340 }
28341 }
28342
28343
28344 /* Return non-zero if W's cursor intersects rectangle R. */
28345
28346 static int
28347 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28348 {
28349 XRectangle cr, result;
28350 struct glyph *cursor_glyph;
28351 struct glyph_row *row;
28352
28353 if (w->phys_cursor.vpos >= 0
28354 && w->phys_cursor.vpos < w->current_matrix->nrows
28355 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28356 row->enabled_p)
28357 && row->cursor_in_fringe_p)
28358 {
28359 /* Cursor is in the fringe. */
28360 cr.x = window_box_right_offset (w,
28361 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28362 ? RIGHT_MARGIN_AREA
28363 : TEXT_AREA));
28364 cr.y = row->y;
28365 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28366 cr.height = row->height;
28367 return x_intersect_rectangles (&cr, r, &result);
28368 }
28369
28370 cursor_glyph = get_phys_cursor_glyph (w);
28371 if (cursor_glyph)
28372 {
28373 /* r is relative to W's box, but w->phys_cursor.x is relative
28374 to left edge of W's TEXT area. Adjust it. */
28375 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28376 cr.y = w->phys_cursor.y;
28377 cr.width = cursor_glyph->pixel_width;
28378 cr.height = w->phys_cursor_height;
28379 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28380 I assume the effect is the same -- and this is portable. */
28381 return x_intersect_rectangles (&cr, r, &result);
28382 }
28383 /* If we don't understand the format, pretend we're not in the hot-spot. */
28384 return 0;
28385 }
28386
28387
28388 /* EXPORT:
28389 Draw a vertical window border to the right of window W if W doesn't
28390 have vertical scroll bars. */
28391
28392 void
28393 x_draw_vertical_border (struct window *w)
28394 {
28395 struct frame *f = XFRAME (WINDOW_FRAME (w));
28396
28397 /* We could do better, if we knew what type of scroll-bar the adjacent
28398 windows (on either side) have... But we don't :-(
28399 However, I think this works ok. ++KFS 2003-04-25 */
28400
28401 /* Redraw borders between horizontally adjacent windows. Don't
28402 do it for frames with vertical scroll bars because either the
28403 right scroll bar of a window, or the left scroll bar of its
28404 neighbor will suffice as a border. */
28405 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28406 return;
28407
28408 if (!WINDOW_RIGHTMOST_P (w)
28409 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28410 {
28411 int x0, x1, y0, y1;
28412
28413 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28414 y1 -= 1;
28415
28416 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28417 x1 -= 1;
28418
28419 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28420 }
28421 else if (!WINDOW_LEFTMOST_P (w)
28422 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28423 {
28424 int x0, x1, y0, y1;
28425
28426 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28427 y1 -= 1;
28428
28429 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28430 x0 -= 1;
28431
28432 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28433 }
28434 }
28435
28436
28437 /* Redraw the part of window W intersection rectangle FR. Pixel
28438 coordinates in FR are frame-relative. Call this function with
28439 input blocked. Value is non-zero if the exposure overwrites
28440 mouse-face. */
28441
28442 static int
28443 expose_window (struct window *w, XRectangle *fr)
28444 {
28445 struct frame *f = XFRAME (w->frame);
28446 XRectangle wr, r;
28447 int mouse_face_overwritten_p = 0;
28448
28449 /* If window is not yet fully initialized, do nothing. This can
28450 happen when toolkit scroll bars are used and a window is split.
28451 Reconfiguring the scroll bar will generate an expose for a newly
28452 created window. */
28453 if (w->current_matrix == NULL)
28454 return 0;
28455
28456 /* When we're currently updating the window, display and current
28457 matrix usually don't agree. Arrange for a thorough display
28458 later. */
28459 if (w == updated_window)
28460 {
28461 SET_FRAME_GARBAGED (f);
28462 return 0;
28463 }
28464
28465 /* Frame-relative pixel rectangle of W. */
28466 wr.x = WINDOW_LEFT_EDGE_X (w);
28467 wr.y = WINDOW_TOP_EDGE_Y (w);
28468 wr.width = WINDOW_TOTAL_WIDTH (w);
28469 wr.height = WINDOW_TOTAL_HEIGHT (w);
28470
28471 if (x_intersect_rectangles (fr, &wr, &r))
28472 {
28473 int yb = window_text_bottom_y (w);
28474 struct glyph_row *row;
28475 int cursor_cleared_p, phys_cursor_on_p;
28476 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28477
28478 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28479 r.x, r.y, r.width, r.height));
28480
28481 /* Convert to window coordinates. */
28482 r.x -= WINDOW_LEFT_EDGE_X (w);
28483 r.y -= WINDOW_TOP_EDGE_Y (w);
28484
28485 /* Turn off the cursor. */
28486 if (!w->pseudo_window_p
28487 && phys_cursor_in_rect_p (w, &r))
28488 {
28489 x_clear_cursor (w);
28490 cursor_cleared_p = 1;
28491 }
28492 else
28493 cursor_cleared_p = 0;
28494
28495 /* If the row containing the cursor extends face to end of line,
28496 then expose_area might overwrite the cursor outside the
28497 rectangle and thus notice_overwritten_cursor might clear
28498 w->phys_cursor_on_p. We remember the original value and
28499 check later if it is changed. */
28500 phys_cursor_on_p = w->phys_cursor_on_p;
28501
28502 /* Update lines intersecting rectangle R. */
28503 first_overlapping_row = last_overlapping_row = NULL;
28504 for (row = w->current_matrix->rows;
28505 row->enabled_p;
28506 ++row)
28507 {
28508 int y0 = row->y;
28509 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28510
28511 if ((y0 >= r.y && y0 < r.y + r.height)
28512 || (y1 > r.y && y1 < r.y + r.height)
28513 || (r.y >= y0 && r.y < y1)
28514 || (r.y + r.height > y0 && r.y + r.height < y1))
28515 {
28516 /* A header line may be overlapping, but there is no need
28517 to fix overlapping areas for them. KFS 2005-02-12 */
28518 if (row->overlapping_p && !row->mode_line_p)
28519 {
28520 if (first_overlapping_row == NULL)
28521 first_overlapping_row = row;
28522 last_overlapping_row = row;
28523 }
28524
28525 row->clip = fr;
28526 if (expose_line (w, row, &r))
28527 mouse_face_overwritten_p = 1;
28528 row->clip = NULL;
28529 }
28530 else if (row->overlapping_p)
28531 {
28532 /* We must redraw a row overlapping the exposed area. */
28533 if (y0 < r.y
28534 ? y0 + row->phys_height > r.y
28535 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28536 {
28537 if (first_overlapping_row == NULL)
28538 first_overlapping_row = row;
28539 last_overlapping_row = row;
28540 }
28541 }
28542
28543 if (y1 >= yb)
28544 break;
28545 }
28546
28547 /* Display the mode line if there is one. */
28548 if (WINDOW_WANTS_MODELINE_P (w)
28549 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28550 row->enabled_p)
28551 && row->y < r.y + r.height)
28552 {
28553 if (expose_line (w, row, &r))
28554 mouse_face_overwritten_p = 1;
28555 }
28556
28557 if (!w->pseudo_window_p)
28558 {
28559 /* Fix the display of overlapping rows. */
28560 if (first_overlapping_row)
28561 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28562 fr);
28563
28564 /* Draw border between windows. */
28565 x_draw_vertical_border (w);
28566
28567 /* Turn the cursor on again. */
28568 if (cursor_cleared_p
28569 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28570 update_window_cursor (w, 1);
28571 }
28572 }
28573
28574 return mouse_face_overwritten_p;
28575 }
28576
28577
28578
28579 /* Redraw (parts) of all windows in the window tree rooted at W that
28580 intersect R. R contains frame pixel coordinates. Value is
28581 non-zero if the exposure overwrites mouse-face. */
28582
28583 static int
28584 expose_window_tree (struct window *w, XRectangle *r)
28585 {
28586 struct frame *f = XFRAME (w->frame);
28587 int mouse_face_overwritten_p = 0;
28588
28589 while (w && !FRAME_GARBAGED_P (f))
28590 {
28591 if (!NILP (w->hchild))
28592 mouse_face_overwritten_p
28593 |= expose_window_tree (XWINDOW (w->hchild), r);
28594 else if (!NILP (w->vchild))
28595 mouse_face_overwritten_p
28596 |= expose_window_tree (XWINDOW (w->vchild), r);
28597 else
28598 mouse_face_overwritten_p |= expose_window (w, r);
28599
28600 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28601 }
28602
28603 return mouse_face_overwritten_p;
28604 }
28605
28606
28607 /* EXPORT:
28608 Redisplay an exposed area of frame F. X and Y are the upper-left
28609 corner of the exposed rectangle. W and H are width and height of
28610 the exposed area. All are pixel values. W or H zero means redraw
28611 the entire frame. */
28612
28613 void
28614 expose_frame (struct frame *f, int x, int y, int w, int h)
28615 {
28616 XRectangle r;
28617 int mouse_face_overwritten_p = 0;
28618
28619 TRACE ((stderr, "expose_frame "));
28620
28621 /* No need to redraw if frame will be redrawn soon. */
28622 if (FRAME_GARBAGED_P (f))
28623 {
28624 TRACE ((stderr, " garbaged\n"));
28625 return;
28626 }
28627
28628 /* If basic faces haven't been realized yet, there is no point in
28629 trying to redraw anything. This can happen when we get an expose
28630 event while Emacs is starting, e.g. by moving another window. */
28631 if (FRAME_FACE_CACHE (f) == NULL
28632 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28633 {
28634 TRACE ((stderr, " no faces\n"));
28635 return;
28636 }
28637
28638 if (w == 0 || h == 0)
28639 {
28640 r.x = r.y = 0;
28641 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28642 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28643 }
28644 else
28645 {
28646 r.x = x;
28647 r.y = y;
28648 r.width = w;
28649 r.height = h;
28650 }
28651
28652 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28653 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28654
28655 if (WINDOWP (f->tool_bar_window))
28656 mouse_face_overwritten_p
28657 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28658
28659 #ifdef HAVE_X_WINDOWS
28660 #ifndef MSDOS
28661 #ifndef USE_X_TOOLKIT
28662 if (WINDOWP (f->menu_bar_window))
28663 mouse_face_overwritten_p
28664 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28665 #endif /* not USE_X_TOOLKIT */
28666 #endif
28667 #endif
28668
28669 /* Some window managers support a focus-follows-mouse style with
28670 delayed raising of frames. Imagine a partially obscured frame,
28671 and moving the mouse into partially obscured mouse-face on that
28672 frame. The visible part of the mouse-face will be highlighted,
28673 then the WM raises the obscured frame. With at least one WM, KDE
28674 2.1, Emacs is not getting any event for the raising of the frame
28675 (even tried with SubstructureRedirectMask), only Expose events.
28676 These expose events will draw text normally, i.e. not
28677 highlighted. Which means we must redo the highlight here.
28678 Subsume it under ``we love X''. --gerd 2001-08-15 */
28679 /* Included in Windows version because Windows most likely does not
28680 do the right thing if any third party tool offers
28681 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28682 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28683 {
28684 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28685 if (f == hlinfo->mouse_face_mouse_frame)
28686 {
28687 int mouse_x = hlinfo->mouse_face_mouse_x;
28688 int mouse_y = hlinfo->mouse_face_mouse_y;
28689 clear_mouse_face (hlinfo);
28690 note_mouse_highlight (f, mouse_x, mouse_y);
28691 }
28692 }
28693 }
28694
28695
28696 /* EXPORT:
28697 Determine the intersection of two rectangles R1 and R2. Return
28698 the intersection in *RESULT. Value is non-zero if RESULT is not
28699 empty. */
28700
28701 int
28702 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28703 {
28704 XRectangle *left, *right;
28705 XRectangle *upper, *lower;
28706 int intersection_p = 0;
28707
28708 /* Rearrange so that R1 is the left-most rectangle. */
28709 if (r1->x < r2->x)
28710 left = r1, right = r2;
28711 else
28712 left = r2, right = r1;
28713
28714 /* X0 of the intersection is right.x0, if this is inside R1,
28715 otherwise there is no intersection. */
28716 if (right->x <= left->x + left->width)
28717 {
28718 result->x = right->x;
28719
28720 /* The right end of the intersection is the minimum of
28721 the right ends of left and right. */
28722 result->width = (min (left->x + left->width, right->x + right->width)
28723 - result->x);
28724
28725 /* Same game for Y. */
28726 if (r1->y < r2->y)
28727 upper = r1, lower = r2;
28728 else
28729 upper = r2, lower = r1;
28730
28731 /* The upper end of the intersection is lower.y0, if this is inside
28732 of upper. Otherwise, there is no intersection. */
28733 if (lower->y <= upper->y + upper->height)
28734 {
28735 result->y = lower->y;
28736
28737 /* The lower end of the intersection is the minimum of the lower
28738 ends of upper and lower. */
28739 result->height = (min (lower->y + lower->height,
28740 upper->y + upper->height)
28741 - result->y);
28742 intersection_p = 1;
28743 }
28744 }
28745
28746 return intersection_p;
28747 }
28748
28749 #endif /* HAVE_WINDOW_SYSTEM */
28750
28751 \f
28752 /***********************************************************************
28753 Initialization
28754 ***********************************************************************/
28755
28756 void
28757 syms_of_xdisp (void)
28758 {
28759 Vwith_echo_area_save_vector = Qnil;
28760 staticpro (&Vwith_echo_area_save_vector);
28761
28762 Vmessage_stack = Qnil;
28763 staticpro (&Vmessage_stack);
28764
28765 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28766 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28767
28768 message_dolog_marker1 = Fmake_marker ();
28769 staticpro (&message_dolog_marker1);
28770 message_dolog_marker2 = Fmake_marker ();
28771 staticpro (&message_dolog_marker2);
28772 message_dolog_marker3 = Fmake_marker ();
28773 staticpro (&message_dolog_marker3);
28774
28775 #ifdef GLYPH_DEBUG
28776 defsubr (&Sdump_frame_glyph_matrix);
28777 defsubr (&Sdump_glyph_matrix);
28778 defsubr (&Sdump_glyph_row);
28779 defsubr (&Sdump_tool_bar_row);
28780 defsubr (&Strace_redisplay);
28781 defsubr (&Strace_to_stderr);
28782 #endif
28783 #ifdef HAVE_WINDOW_SYSTEM
28784 defsubr (&Stool_bar_lines_needed);
28785 defsubr (&Slookup_image_map);
28786 #endif
28787 defsubr (&Sformat_mode_line);
28788 defsubr (&Sinvisible_p);
28789 defsubr (&Scurrent_bidi_paragraph_direction);
28790
28791 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28792 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28793 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28794 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28795 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28796 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28797 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28798 DEFSYM (Qeval, "eval");
28799 DEFSYM (QCdata, ":data");
28800 DEFSYM (Qdisplay, "display");
28801 DEFSYM (Qspace_width, "space-width");
28802 DEFSYM (Qraise, "raise");
28803 DEFSYM (Qslice, "slice");
28804 DEFSYM (Qspace, "space");
28805 DEFSYM (Qmargin, "margin");
28806 DEFSYM (Qpointer, "pointer");
28807 DEFSYM (Qleft_margin, "left-margin");
28808 DEFSYM (Qright_margin, "right-margin");
28809 DEFSYM (Qcenter, "center");
28810 DEFSYM (Qline_height, "line-height");
28811 DEFSYM (QCalign_to, ":align-to");
28812 DEFSYM (QCrelative_width, ":relative-width");
28813 DEFSYM (QCrelative_height, ":relative-height");
28814 DEFSYM (QCeval, ":eval");
28815 DEFSYM (QCpropertize, ":propertize");
28816 DEFSYM (QCfile, ":file");
28817 DEFSYM (Qfontified, "fontified");
28818 DEFSYM (Qfontification_functions, "fontification-functions");
28819 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28820 DEFSYM (Qescape_glyph, "escape-glyph");
28821 DEFSYM (Qnobreak_space, "nobreak-space");
28822 DEFSYM (Qimage, "image");
28823 DEFSYM (Qtext, "text");
28824 DEFSYM (Qboth, "both");
28825 DEFSYM (Qboth_horiz, "both-horiz");
28826 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28827 DEFSYM (QCmap, ":map");
28828 DEFSYM (QCpointer, ":pointer");
28829 DEFSYM (Qrect, "rect");
28830 DEFSYM (Qcircle, "circle");
28831 DEFSYM (Qpoly, "poly");
28832 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28833 DEFSYM (Qgrow_only, "grow-only");
28834 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28835 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28836 DEFSYM (Qposition, "position");
28837 DEFSYM (Qbuffer_position, "buffer-position");
28838 DEFSYM (Qobject, "object");
28839 DEFSYM (Qbar, "bar");
28840 DEFSYM (Qhbar, "hbar");
28841 DEFSYM (Qbox, "box");
28842 DEFSYM (Qhollow, "hollow");
28843 DEFSYM (Qhand, "hand");
28844 DEFSYM (Qarrow, "arrow");
28845 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28846
28847 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28848 Fcons (intern_c_string ("void-variable"), Qnil)),
28849 Qnil);
28850 staticpro (&list_of_error);
28851
28852 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28853 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28854 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28855 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28856
28857 echo_buffer[0] = echo_buffer[1] = Qnil;
28858 staticpro (&echo_buffer[0]);
28859 staticpro (&echo_buffer[1]);
28860
28861 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28862 staticpro (&echo_area_buffer[0]);
28863 staticpro (&echo_area_buffer[1]);
28864
28865 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28866 staticpro (&Vmessages_buffer_name);
28867
28868 mode_line_proptrans_alist = Qnil;
28869 staticpro (&mode_line_proptrans_alist);
28870 mode_line_string_list = Qnil;
28871 staticpro (&mode_line_string_list);
28872 mode_line_string_face = Qnil;
28873 staticpro (&mode_line_string_face);
28874 mode_line_string_face_prop = Qnil;
28875 staticpro (&mode_line_string_face_prop);
28876 Vmode_line_unwind_vector = Qnil;
28877 staticpro (&Vmode_line_unwind_vector);
28878
28879 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28880
28881 help_echo_string = Qnil;
28882 staticpro (&help_echo_string);
28883 help_echo_object = Qnil;
28884 staticpro (&help_echo_object);
28885 help_echo_window = Qnil;
28886 staticpro (&help_echo_window);
28887 previous_help_echo_string = Qnil;
28888 staticpro (&previous_help_echo_string);
28889 help_echo_pos = -1;
28890
28891 DEFSYM (Qright_to_left, "right-to-left");
28892 DEFSYM (Qleft_to_right, "left-to-right");
28893
28894 #ifdef HAVE_WINDOW_SYSTEM
28895 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28896 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28897 For example, if a block cursor is over a tab, it will be drawn as
28898 wide as that tab on the display. */);
28899 x_stretch_cursor_p = 0;
28900 #endif
28901
28902 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28903 doc: /* Non-nil means highlight trailing whitespace.
28904 The face used for trailing whitespace is `trailing-whitespace'. */);
28905 Vshow_trailing_whitespace = Qnil;
28906
28907 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28908 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28909 If the value is t, Emacs highlights non-ASCII chars which have the
28910 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28911 or `escape-glyph' face respectively.
28912
28913 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28914 U+2011 (non-breaking hyphen) are affected.
28915
28916 Any other non-nil value means to display these characters as a escape
28917 glyph followed by an ordinary space or hyphen.
28918
28919 A value of nil means no special handling of these characters. */);
28920 Vnobreak_char_display = Qt;
28921
28922 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28923 doc: /* The pointer shape to show in void text areas.
28924 A value of nil means to show the text pointer. Other options are `arrow',
28925 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28926 Vvoid_text_area_pointer = Qarrow;
28927
28928 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28929 doc: /* Non-nil means don't actually do any redisplay.
28930 This is used for internal purposes. */);
28931 Vinhibit_redisplay = Qnil;
28932
28933 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28934 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28935 Vglobal_mode_string = Qnil;
28936
28937 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28938 doc: /* Marker for where to display an arrow on top of the buffer text.
28939 This must be the beginning of a line in order to work.
28940 See also `overlay-arrow-string'. */);
28941 Voverlay_arrow_position = Qnil;
28942
28943 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28944 doc: /* String to display as an arrow in non-window frames.
28945 See also `overlay-arrow-position'. */);
28946 Voverlay_arrow_string = build_pure_c_string ("=>");
28947
28948 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28949 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28950 The symbols on this list are examined during redisplay to determine
28951 where to display overlay arrows. */);
28952 Voverlay_arrow_variable_list
28953 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28954
28955 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28956 doc: /* The number of lines to try scrolling a window by when point moves out.
28957 If that fails to bring point back on frame, point is centered instead.
28958 If this is zero, point is always centered after it moves off frame.
28959 If you want scrolling to always be a line at a time, you should set
28960 `scroll-conservatively' to a large value rather than set this to 1. */);
28961
28962 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28963 doc: /* Scroll up to this many lines, to bring point back on screen.
28964 If point moves off-screen, redisplay will scroll by up to
28965 `scroll-conservatively' lines in order to bring point just barely
28966 onto the screen again. If that cannot be done, then redisplay
28967 recenters point as usual.
28968
28969 If the value is greater than 100, redisplay will never recenter point,
28970 but will always scroll just enough text to bring point into view, even
28971 if you move far away.
28972
28973 A value of zero means always recenter point if it moves off screen. */);
28974 scroll_conservatively = 0;
28975
28976 DEFVAR_INT ("scroll-margin", scroll_margin,
28977 doc: /* Number of lines of margin at the top and bottom of a window.
28978 Recenter the window whenever point gets within this many lines
28979 of the top or bottom of the window. */);
28980 scroll_margin = 0;
28981
28982 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28983 doc: /* Pixels per inch value for non-window system displays.
28984 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28985 Vdisplay_pixels_per_inch = make_float (72.0);
28986
28987 #ifdef GLYPH_DEBUG
28988 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28989 #endif
28990
28991 DEFVAR_LISP ("truncate-partial-width-windows",
28992 Vtruncate_partial_width_windows,
28993 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28994 For an integer value, truncate lines in each window narrower than the
28995 full frame width, provided the window width is less than that integer;
28996 otherwise, respect the value of `truncate-lines'.
28997
28998 For any other non-nil value, truncate lines in all windows that do
28999 not span the full frame width.
29000
29001 A value of nil means to respect the value of `truncate-lines'.
29002
29003 If `word-wrap' is enabled, you might want to reduce this. */);
29004 Vtruncate_partial_width_windows = make_number (50);
29005
29006 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29007 doc: /* Maximum buffer size for which line number should be displayed.
29008 If the buffer is bigger than this, the line number does not appear
29009 in the mode line. A value of nil means no limit. */);
29010 Vline_number_display_limit = Qnil;
29011
29012 DEFVAR_INT ("line-number-display-limit-width",
29013 line_number_display_limit_width,
29014 doc: /* Maximum line width (in characters) for line number display.
29015 If the average length of the lines near point is bigger than this, then the
29016 line number may be omitted from the mode line. */);
29017 line_number_display_limit_width = 200;
29018
29019 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29020 doc: /* Non-nil means highlight region even in nonselected windows. */);
29021 highlight_nonselected_windows = 0;
29022
29023 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29024 doc: /* Non-nil if more than one frame is visible on this display.
29025 Minibuffer-only frames don't count, but iconified frames do.
29026 This variable is not guaranteed to be accurate except while processing
29027 `frame-title-format' and `icon-title-format'. */);
29028
29029 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29030 doc: /* Template for displaying the title bar of visible frames.
29031 \(Assuming the window manager supports this feature.)
29032
29033 This variable has the same structure as `mode-line-format', except that
29034 the %c and %l constructs are ignored. It is used only on frames for
29035 which no explicit name has been set \(see `modify-frame-parameters'). */);
29036
29037 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29038 doc: /* Template for displaying the title bar of an iconified frame.
29039 \(Assuming the window manager supports this feature.)
29040 This variable has the same structure as `mode-line-format' (which see),
29041 and is used only on frames for which no explicit name has been set
29042 \(see `modify-frame-parameters'). */);
29043 Vicon_title_format
29044 = Vframe_title_format
29045 = listn (CONSTYPE_PURE, 3,
29046 intern_c_string ("multiple-frames"),
29047 build_pure_c_string ("%b"),
29048 listn (CONSTYPE_PURE, 4,
29049 empty_unibyte_string,
29050 intern_c_string ("invocation-name"),
29051 build_pure_c_string ("@"),
29052 intern_c_string ("system-name")));
29053
29054 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29055 doc: /* Maximum number of lines to keep in the message log buffer.
29056 If nil, disable message logging. If t, log messages but don't truncate
29057 the buffer when it becomes large. */);
29058 Vmessage_log_max = make_number (1000);
29059
29060 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29061 doc: /* Functions called before redisplay, if window sizes have changed.
29062 The value should be a list of functions that take one argument.
29063 Just before redisplay, for each frame, if any of its windows have changed
29064 size since the last redisplay, or have been split or deleted,
29065 all the functions in the list are called, with the frame as argument. */);
29066 Vwindow_size_change_functions = Qnil;
29067
29068 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29069 doc: /* List of functions to call before redisplaying a window with scrolling.
29070 Each function is called with two arguments, the window and its new
29071 display-start position. Note that these functions are also called by
29072 `set-window-buffer'. Also note that the value of `window-end' is not
29073 valid when these functions are called.
29074
29075 Warning: Do not use this feature to alter the way the window
29076 is scrolled. It is not designed for that, and such use probably won't
29077 work. */);
29078 Vwindow_scroll_functions = Qnil;
29079
29080 DEFVAR_LISP ("window-text-change-functions",
29081 Vwindow_text_change_functions,
29082 doc: /* Functions to call in redisplay when text in the window might change. */);
29083 Vwindow_text_change_functions = Qnil;
29084
29085 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29086 doc: /* Functions called when redisplay of a window reaches the end trigger.
29087 Each function is called with two arguments, the window and the end trigger value.
29088 See `set-window-redisplay-end-trigger'. */);
29089 Vredisplay_end_trigger_functions = Qnil;
29090
29091 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29092 doc: /* Non-nil means autoselect window with mouse pointer.
29093 If nil, do not autoselect windows.
29094 A positive number means delay autoselection by that many seconds: a
29095 window is autoselected only after the mouse has remained in that
29096 window for the duration of the delay.
29097 A negative number has a similar effect, but causes windows to be
29098 autoselected only after the mouse has stopped moving. \(Because of
29099 the way Emacs compares mouse events, you will occasionally wait twice
29100 that time before the window gets selected.\)
29101 Any other value means to autoselect window instantaneously when the
29102 mouse pointer enters it.
29103
29104 Autoselection selects the minibuffer only if it is active, and never
29105 unselects the minibuffer if it is active.
29106
29107 When customizing this variable make sure that the actual value of
29108 `focus-follows-mouse' matches the behavior of your window manager. */);
29109 Vmouse_autoselect_window = Qnil;
29110
29111 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29112 doc: /* Non-nil means automatically resize tool-bars.
29113 This dynamically changes the tool-bar's height to the minimum height
29114 that is needed to make all tool-bar items visible.
29115 If value is `grow-only', the tool-bar's height is only increased
29116 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29117 Vauto_resize_tool_bars = Qt;
29118
29119 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29120 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29121 auto_raise_tool_bar_buttons_p = 1;
29122
29123 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29124 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29125 make_cursor_line_fully_visible_p = 1;
29126
29127 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29128 doc: /* Border below tool-bar in pixels.
29129 If an integer, use it as the height of the border.
29130 If it is one of `internal-border-width' or `border-width', use the
29131 value of the corresponding frame parameter.
29132 Otherwise, no border is added below the tool-bar. */);
29133 Vtool_bar_border = Qinternal_border_width;
29134
29135 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29136 doc: /* Margin around tool-bar buttons in pixels.
29137 If an integer, use that for both horizontal and vertical margins.
29138 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29139 HORZ specifying the horizontal margin, and VERT specifying the
29140 vertical margin. */);
29141 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29142
29143 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29144 doc: /* Relief thickness of tool-bar buttons. */);
29145 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29146
29147 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29148 doc: /* Tool bar style to use.
29149 It can be one of
29150 image - show images only
29151 text - show text only
29152 both - show both, text below image
29153 both-horiz - show text to the right of the image
29154 text-image-horiz - show text to the left of the image
29155 any other - use system default or image if no system default.
29156
29157 This variable only affects the GTK+ toolkit version of Emacs. */);
29158 Vtool_bar_style = Qnil;
29159
29160 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29161 doc: /* Maximum number of characters a label can have to be shown.
29162 The tool bar style must also show labels for this to have any effect, see
29163 `tool-bar-style'. */);
29164 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29165
29166 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29167 doc: /* List of functions to call to fontify regions of text.
29168 Each function is called with one argument POS. Functions must
29169 fontify a region starting at POS in the current buffer, and give
29170 fontified regions the property `fontified'. */);
29171 Vfontification_functions = Qnil;
29172 Fmake_variable_buffer_local (Qfontification_functions);
29173
29174 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29175 unibyte_display_via_language_environment,
29176 doc: /* Non-nil means display unibyte text according to language environment.
29177 Specifically, this means that raw bytes in the range 160-255 decimal
29178 are displayed by converting them to the equivalent multibyte characters
29179 according to the current language environment. As a result, they are
29180 displayed according to the current fontset.
29181
29182 Note that this variable affects only how these bytes are displayed,
29183 but does not change the fact they are interpreted as raw bytes. */);
29184 unibyte_display_via_language_environment = 0;
29185
29186 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29187 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29188 If a float, it specifies a fraction of the mini-window frame's height.
29189 If an integer, it specifies a number of lines. */);
29190 Vmax_mini_window_height = make_float (0.25);
29191
29192 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29193 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29194 A value of nil means don't automatically resize mini-windows.
29195 A value of t means resize them to fit the text displayed in them.
29196 A value of `grow-only', the default, means let mini-windows grow only;
29197 they return to their normal size when the minibuffer is closed, or the
29198 echo area becomes empty. */);
29199 Vresize_mini_windows = Qgrow_only;
29200
29201 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29202 doc: /* Alist specifying how to blink the cursor off.
29203 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29204 `cursor-type' frame-parameter or variable equals ON-STATE,
29205 comparing using `equal', Emacs uses OFF-STATE to specify
29206 how to blink it off. ON-STATE and OFF-STATE are values for
29207 the `cursor-type' frame parameter.
29208
29209 If a frame's ON-STATE has no entry in this list,
29210 the frame's other specifications determine how to blink the cursor off. */);
29211 Vblink_cursor_alist = Qnil;
29212
29213 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29214 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29215 If non-nil, windows are automatically scrolled horizontally to make
29216 point visible. */);
29217 automatic_hscrolling_p = 1;
29218 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29219
29220 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29221 doc: /* How many columns away from the window edge point is allowed to get
29222 before automatic hscrolling will horizontally scroll the window. */);
29223 hscroll_margin = 5;
29224
29225 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29226 doc: /* How many columns to scroll the window when point gets too close to the edge.
29227 When point is less than `hscroll-margin' columns from the window
29228 edge, automatic hscrolling will scroll the window by the amount of columns
29229 determined by this variable. If its value is a positive integer, scroll that
29230 many columns. If it's a positive floating-point number, it specifies the
29231 fraction of the window's width to scroll. If it's nil or zero, point will be
29232 centered horizontally after the scroll. Any other value, including negative
29233 numbers, are treated as if the value were zero.
29234
29235 Automatic hscrolling always moves point outside the scroll margin, so if
29236 point was more than scroll step columns inside the margin, the window will
29237 scroll more than the value given by the scroll step.
29238
29239 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29240 and `scroll-right' overrides this variable's effect. */);
29241 Vhscroll_step = make_number (0);
29242
29243 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29244 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29245 Bind this around calls to `message' to let it take effect. */);
29246 message_truncate_lines = 0;
29247
29248 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29249 doc: /* Normal hook run to update the menu bar definitions.
29250 Redisplay runs this hook before it redisplays the menu bar.
29251 This is used to update submenus such as Buffers,
29252 whose contents depend on various data. */);
29253 Vmenu_bar_update_hook = Qnil;
29254
29255 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29256 doc: /* Frame for which we are updating a menu.
29257 The enable predicate for a menu binding should check this variable. */);
29258 Vmenu_updating_frame = Qnil;
29259
29260 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29261 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29262 inhibit_menubar_update = 0;
29263
29264 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29265 doc: /* Prefix prepended to all continuation lines at display time.
29266 The value may be a string, an image, or a stretch-glyph; it is
29267 interpreted in the same way as the value of a `display' text property.
29268
29269 This variable is overridden by any `wrap-prefix' text or overlay
29270 property.
29271
29272 To add a prefix to non-continuation lines, use `line-prefix'. */);
29273 Vwrap_prefix = Qnil;
29274 DEFSYM (Qwrap_prefix, "wrap-prefix");
29275 Fmake_variable_buffer_local (Qwrap_prefix);
29276
29277 DEFVAR_LISP ("line-prefix", Vline_prefix,
29278 doc: /* Prefix prepended to all non-continuation lines at display time.
29279 The value may be a string, an image, or a stretch-glyph; it is
29280 interpreted in the same way as the value of a `display' text property.
29281
29282 This variable is overridden by any `line-prefix' text or overlay
29283 property.
29284
29285 To add a prefix to continuation lines, use `wrap-prefix'. */);
29286 Vline_prefix = Qnil;
29287 DEFSYM (Qline_prefix, "line-prefix");
29288 Fmake_variable_buffer_local (Qline_prefix);
29289
29290 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29291 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29292 inhibit_eval_during_redisplay = 0;
29293
29294 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29295 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29296 inhibit_free_realized_faces = 0;
29297
29298 #ifdef GLYPH_DEBUG
29299 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29300 doc: /* Inhibit try_window_id display optimization. */);
29301 inhibit_try_window_id = 0;
29302
29303 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29304 doc: /* Inhibit try_window_reusing display optimization. */);
29305 inhibit_try_window_reusing = 0;
29306
29307 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29308 doc: /* Inhibit try_cursor_movement display optimization. */);
29309 inhibit_try_cursor_movement = 0;
29310 #endif /* GLYPH_DEBUG */
29311
29312 DEFVAR_INT ("overline-margin", overline_margin,
29313 doc: /* Space between overline and text, in pixels.
29314 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29315 margin to the character height. */);
29316 overline_margin = 2;
29317
29318 DEFVAR_INT ("underline-minimum-offset",
29319 underline_minimum_offset,
29320 doc: /* Minimum distance between baseline and underline.
29321 This can improve legibility of underlined text at small font sizes,
29322 particularly when using variable `x-use-underline-position-properties'
29323 with fonts that specify an UNDERLINE_POSITION relatively close to the
29324 baseline. The default value is 1. */);
29325 underline_minimum_offset = 1;
29326
29327 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29328 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29329 This feature only works when on a window system that can change
29330 cursor shapes. */);
29331 display_hourglass_p = 1;
29332
29333 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29334 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29335 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29336
29337 hourglass_atimer = NULL;
29338 hourglass_shown_p = 0;
29339
29340 DEFSYM (Qglyphless_char, "glyphless-char");
29341 DEFSYM (Qhex_code, "hex-code");
29342 DEFSYM (Qempty_box, "empty-box");
29343 DEFSYM (Qthin_space, "thin-space");
29344 DEFSYM (Qzero_width, "zero-width");
29345
29346 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29347 /* Intern this now in case it isn't already done.
29348 Setting this variable twice is harmless.
29349 But don't staticpro it here--that is done in alloc.c. */
29350 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29351 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29352
29353 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29354 doc: /* Char-table defining glyphless characters.
29355 Each element, if non-nil, should be one of the following:
29356 an ASCII acronym string: display this string in a box
29357 `hex-code': display the hexadecimal code of a character in a box
29358 `empty-box': display as an empty box
29359 `thin-space': display as 1-pixel width space
29360 `zero-width': don't display
29361 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29362 display method for graphical terminals and text terminals respectively.
29363 GRAPHICAL and TEXT should each have one of the values listed above.
29364
29365 The char-table has one extra slot to control the display of a character for
29366 which no font is found. This slot only takes effect on graphical terminals.
29367 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29368 `thin-space'. The default is `empty-box'. */);
29369 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29370 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29371 Qempty_box);
29372
29373 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29374 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29375 Vdebug_on_message = Qnil;
29376 }
29377
29378
29379 /* Initialize this module when Emacs starts. */
29380
29381 void
29382 init_xdisp (void)
29383 {
29384 current_header_line_height = current_mode_line_height = -1;
29385
29386 CHARPOS (this_line_start_pos) = 0;
29387
29388 if (!noninteractive)
29389 {
29390 struct window *m = XWINDOW (minibuf_window);
29391 Lisp_Object frame = m->frame;
29392 struct frame *f = XFRAME (frame);
29393 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29394 struct window *r = XWINDOW (root);
29395 int i;
29396
29397 echo_area_window = minibuf_window;
29398
29399 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29400 wset_total_lines
29401 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29402 wset_total_cols (r, make_number (FRAME_COLS (f)));
29403 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29404 wset_total_lines (m, make_number (1));
29405 wset_total_cols (m, make_number (FRAME_COLS (f)));
29406
29407 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29408 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29409 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29410
29411 /* The default ellipsis glyphs `...'. */
29412 for (i = 0; i < 3; ++i)
29413 default_invis_vector[i] = make_number ('.');
29414 }
29415
29416 {
29417 /* Allocate the buffer for frame titles.
29418 Also used for `format-mode-line'. */
29419 int size = 100;
29420 mode_line_noprop_buf = xmalloc (size);
29421 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29422 mode_line_noprop_ptr = mode_line_noprop_buf;
29423 mode_line_target = MODE_LINE_DISPLAY;
29424 }
29425
29426 help_echo_showing_p = 0;
29427 }
29428
29429 /* Platform-independent portion of hourglass implementation. */
29430
29431 /* Cancel a currently active hourglass timer, and start a new one. */
29432 void
29433 start_hourglass (void)
29434 {
29435 #if defined (HAVE_WINDOW_SYSTEM)
29436 EMACS_TIME delay;
29437
29438 cancel_hourglass ();
29439
29440 if (INTEGERP (Vhourglass_delay)
29441 && XINT (Vhourglass_delay) > 0)
29442 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29443 TYPE_MAXIMUM (time_t)),
29444 0);
29445 else if (FLOATP (Vhourglass_delay)
29446 && XFLOAT_DATA (Vhourglass_delay) > 0)
29447 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29448 else
29449 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29450
29451 #ifdef HAVE_NTGUI
29452 {
29453 extern void w32_note_current_window (void);
29454 w32_note_current_window ();
29455 }
29456 #endif /* HAVE_NTGUI */
29457
29458 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29459 show_hourglass, NULL);
29460 #endif
29461 }
29462
29463
29464 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29465 shown. */
29466 void
29467 cancel_hourglass (void)
29468 {
29469 #if defined (HAVE_WINDOW_SYSTEM)
29470 if (hourglass_atimer)
29471 {
29472 cancel_atimer (hourglass_atimer);
29473 hourglass_atimer = NULL;
29474 }
29475
29476 if (hourglass_shown_p)
29477 hide_hourglass ();
29478 #endif
29479 }